function [p] = gui_mesh_plot(p,mesh_plot_command,parent)

% gui_mesh_plot - GUI for plotting meshes
%
% Usage: [p] = gui_mesh_plot(p,command,[parent])
%
% command   - either 'init' or 'plot'
% parent    - optional, GUI parent window
%
% The functionality is further described in mesh_plot
%

% $Revision: 1.1 $ $Date: 2009-04-28 22:13:56 $

% Licence:  GNU GPL, no express or implied warranties
% History:  02/2002, Darren.Weber_at_radiology.ucsf.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ~exist('mesh_plot_command','var'),
  mesh_plot_command = 'init';
end

if ~exist('p','var'),
 [p] = mesh_open;
elseif isempty(p),
 [p] = mesh_open(p);
elseif isfield(p,'mesh'),
  if isfield(p.mesh,'data'),
    if isempty(p.mesh.data),
     [p] = mesh_open(p);
    end
  else
   [p] = mesh_open(p);
  end
end


switch mesh_plot_command,
  
  case 'init',
    
   [p] = mesh_plot(p);
    
    if exist('parent','var'),
      MESHPlot = INIT(p,parent);
    else
      MESHPlot = INIT(p);
    end
    
  case 'return',
    
    MESHPlot = get(gcbf,'Userdata');
    if isequal(get(MESHPlot.handles.Bhold,'Value'),0),
     [p] = gui_updateparent(MESHPlot);
      close gcbf;
    else
     [p] = gui_updateparent(MESHPlot,0);
    end
    fprintf('GUI_MESH_PLOT: Plot parameters returned.\n');
    
  otherwise,
    close(p.mesh.plot.fig);
    close gcbf;
end

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [MESHPlot] = INIT(p,parent),

% GUI General Parameters

meshPlotPosition = get(p.mesh.plot.fig,'Position');

GUIleft = meshPlotPosition(1) + meshPlotPosition(3) + (0.05 * meshPlotPosition(3));
GUIbottom = meshPlotPosition(2);

GUIwidth  = meshPlotPosition(3) / 2;
GUIheight = meshPlotPosition(4);

GUI = figure('Name','MESH Plot','Tag','MESH_PLOT',...
  'NumberTitle','off','units','pixels',...
  'MenuBar','none','Position',[GUIleft GUIbottom GUIwidth GUIheight]);


set(GUI,'HandleVisibility','callback');

MESHPlot.gui = GUI;

Font.FontName   = 'Helvetica';
Font.FontUnits  = 'Pixels';
Font.FontSize   = 12;
Font.FontWeight = 'normal';
Font.FontAngle  = 'normal';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mesh Data Selection and Parameters

% Provide some GUI callbacks to change lighting...
G.Fmesh = uicontrol('Parent',GUI,'Style','frame','Units','Normalized',Font, ...
  'Position',[.04 .62 .92 .36],...
  'TooltipString','Mesh Parameters',...
  'HorizontalAlignment','left');
G.Tmesh = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.06 .90 .4 .05],...
  'TooltipString','Mesh Parameters',...
  'String','Mesh','HorizontalAlignment','left');

% Overlay checkbox
G.Boverlay = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
  'Position',[.50 .90 .4 .05],'String','Overlay','BusyAction','queue',...
  'TooltipString','Overlay MESH Plots',...
  'Value',p.mesh.plot.overlay,'HorizontalAlignment', 'center');

% Mesh Type
G.Title_data = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .82 .3 .05],...
  'String','Mesh Type:','HorizontalAlignment','left');
G.PmeshType = uicontrol('Tag','PmeshType','Parent',GUI,'Style','popupmenu',...
  'Units','Normalized',Font,  ...
  'Position',[.50 .82 .4 .05],...
  'String',p.mesh.data.meshtype,'Value',p.mesh.current,...
  'TooltipString','Mesh/Tesselation Layer',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.current = get(MESHPlot.handles.PmeshType,''Value''); ',...
  'MESHPlot.p.mesh.plot.overlay = get(MESHPlot.handles.Boverlay,''Value'');',...
  'MESHPlot.p = mesh_plot(MESHPlot.p);',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));

G.Tedgecolor = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .75 .3 .05],...
  'TooltipString','Mesh Edge Color: 3 by 0-1 RGB float values or ''none''',...
  'String','Edge Color:','HorizontalAlignment','left');

G.Eedgecolor = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.edgecolor','Units','Normalized',Font,  ...
  'Position',[.50 .75 .4 .05],...
  'String',p.mesh.plot.edgecolor,...
  'TooltipString','Mesh Edge Color: 0-1 RGB values or ''none''',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.edgecolor = get(MESHPlot.handles.Eedgecolor,''String'');',...
  'if findstr(MESHPlot.p.mesh.plot.edgecolor,''none''), ',...
  '   set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''EdgeColor'',''none'');',...
  'else, ',...
  '   edgecolor = str2num(MESHPlot.p.mesh.plot.edgecolor); ',...
  '   if and(isequal(size(edgecolor,1),1),isequal(size(edgecolor,2),3)), ',...
  '      set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''EdgeColor'',edgecolor);',...
  '   else, ',...
  '      msg = sprintf(''EdgeColor must be ''''none'''' or 1x3 element RGB values between 0-1\n'');',...
  '      warndlg(msg,''Mesh Plot Warning'');',...
  '   end; ',...
  'end; ',...
  'set(gcbf,''Userdata'',MESHPlot); clear edgecolor MESHPlot;'));

G.Tfacecolor = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .70 .3 .05],...
  'TooltipString','Mesh Face Color: 3 by 0-1 RGB float values or ''none''',...
  'String','Face Color:','HorizontalAlignment','left');
G.Efacecolor = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.facecolor','Units','Normalized',Font,  ...
  'Position',[.50 .70 .4 .05],...
  'String',sprintf('%2.1f %2.1f %2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'FaceColor')),...
  'TooltipString','Face Edge Color: 0-1 RGB values or ''none''',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.facecolor = get(MESHPlot.handles.Efacecolor,''String'');',...
  'if findstr(MESHPlot.p.mesh.plot.facecolor,''none''), ',...
  '   set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''FaceColor'',''none'');',...
  'else, ',...
  '   facecolor = str2num(MESHPlot.p.mesh.plot.facecolor); ',...
  '   if and(isequal(size(facecolor,1),1),isequal(size(facecolor,2),3)), ',...
  '      set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''FaceColor'',facecolor);',...
  '   else, ',...
  '      msg = sprintf(''FaceColor must be ''''none'''' or 1x3 element RGB values between 0-1\n'');',...
  '      warndlg(msg,''Mesh Plot Warning'');',...
  '   end; ',...
  'end; ',...
  'set(gcbf,''Userdata'',MESHPlot); clear facecolor MESHPlot;'));

% Face Transparency...
G.Tfacealpha = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .65 .3 .05],...
  'TooltipString','Face Transparency: 0 transparent, 1 opaque, 0-1 float',...
  'String','Face Alpha:','HorizontalAlignment','left');
G.Efacealpha = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.facealpha','Units','Normalized',Font,  ...
  'Position',[.50 .65 .4 .05],...
  'String',sprintf('%2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'FaceAlpha')),...
  'TooltipString','Face Transparency: 0 transparent, 1 opaque, 0-1 float',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.facealpha = get(MESHPlot.handles.Efacealpha,''String'');',...
  'if MESHPlot.p.mesh.plot.facealpha, ',...
  '   facealpha = str2num(MESHPlot.p.mesh.plot.facealpha); ',...
  '   if isequal(size(facealpha),[1 1]), ',...
  '      set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''FaceAlpha'',facealpha);',...
  '   else, ',...
  '      msg = sprintf(''FaceAlpha must be 1 element float values between 0-1\n'');',...
  '      warndlg(msg,''Mesh Plot Warning'');',...
  '   end; ',...
  'else, ',...
  '   set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''FaceAlpha'',1);',...
  'end; ',...
  'set(gcbf,''Userdata'',MESHPlot); clear facealpha MESHPlot;'));



% Provide some GUI callbacks to change lighting...
G.Flight = uicontrol('Parent',GUI,'Style','frame','Units','Normalized',Font, ...
  'Position',[.04 .15 .92 .45],...
  'TooltipString','Lighting Parameters',...
  'HorizontalAlignment','left');
G.Tlight = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.06 .54 .45 .05],...
  'TooltipString','Lighting Parameters',...
  'String','Light','HorizontalAlignment','left');

G.Tambient = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .45 .45 .05],...
  'TooltipString','Ambient Strength: 0-1 values',...
  'String','Ambient Strength:','HorizontalAlignment','left');
G.Eambient = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.ambient','Units','Normalized',Font,  ...
  'Position',[.65 .45 .2 .05],...
  'String',sprintf('%2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'AmbientStrength')),...
  'TooltipString','Ambient Strength: 0-1 values',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.ambient = get(MESHPlot.handles.Eambient,''String'');',...
  'set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''AmbientStrength'',str2num(MESHPlot.p.mesh.plot.ambient));',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));

G.Tdiffuse = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .40 .45 .05],...
  'TooltipString','Diffuse Strength: 0-1 values',...
  'String','Diffuse Strength:','HorizontalAlignment','left');
G.Ediffuse = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.diffuse','Units','Normalized',Font,  ...
  'Position',[.65 .40 .2 .05],...
  'String',sprintf('%2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'DiffuseStrength')),...
  'TooltipString','Diffuse Strength: 0-1 values',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.diffuse = get(MESHPlot.handles.Ediffuse,''String'');',...
  'set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''DiffuseStrength'',str2num(MESHPlot.p.mesh.plot.diffuse));',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));

G.Tspecular = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .35 .45 .05],...
  'TooltipString','Specular Strength: 0-1 values',...
  'String','Specular Strength:','HorizontalAlignment','left');
G.Especular = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.specular','Units','Normalized',Font,  ...
  'Position',[.65 .35 .2 .05],...
  'String',sprintf('%2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'SpecularStrength')),...
  'TooltipString','Specular Strength: 0-1 values',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.specular = get(MESHPlot.handles.Especular,''String'');',...
  'set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''SpecularStrength'',str2num(MESHPlot.p.mesh.plot.specular));',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));

G.Tspecexp = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .30 .45 .05],...
  'TooltipString','Specular Exponent',...
  'String','Specular Exponent:','HorizontalAlignment','left');
G.Especexp = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.specexp','Units','Normalized',Font,  ...
  'Position',[.65 .30 .2 .05],...
  'String',sprintf('%d',get(p.mesh.plot.Hpatch{p.mesh.current},'SpecularExponent')),...
  'TooltipString','Specular Exponent',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.specexp = get(MESHPlot.handles.Especexp,''String'');',...
  'set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''SpecularExponent'',str2num(MESHPlot.p.mesh.plot.specexp));',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));

G.Tspeccolor = uicontrol('Parent',GUI,'Style','text','Units','Normalized',Font, ...
  'Position',[.10 .20 .45 .08],...
  'TooltipString','Specular Color Reflectance: 0-1 values',...
  'String','Specular Color Reflectance:','HorizontalAlignment','left');
G.Especcolor = uicontrol('Parent',GUI,'Style','edit',...
  'Tag','p.mesh.plot.Hpatch.speccolor','Units','Normalized',Font,  ...
  'Position',[.65 .21 .20 .07],...
  'String',sprintf('%2.1f',get(p.mesh.plot.Hpatch{p.mesh.current},'SpecularColorReflectance')),...
  'TooltipString','Specular Color Reflectance: 0-1 values',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p.mesh.plot.speccolor = get(MESHPlot.handles.Especcolor,''String'');',...
  'set(MESHPlot.p.mesh.plot.Hpatch{MESHPlot.p.mesh.current},''SpecularColorReflectance'',str2num(MESHPlot.p.mesh.plot.speccolor));',...
  'set(gcbf,''Userdata'',MESHPlot); clear MESHPlot;'));







%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Font.FontWeight = 'bold';

% RETURN, return file parameters
G.Breturn = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.05 .01 .275 .05],...
  'String','RETURN','BusyAction','queue',...
  'TooltipString','Return the plot parameters and close.',...
  'BackgroundColor',[0.0 0.5 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'p = gui_mesh_plot(MESHPlot.p,''return'');',...
  'clear MESHPlot;'));

% CANCEL, return file parameters
G.Bcancel = uicontrol('Parent',GUI,'Style','pushbutton','Units','Normalized', Font, ...
  'Position',[.40 .01 .25 .05],...
  'String','CLOSE','BusyAction','queue',...
  'TooltipString','Close, do not return the plot parameters.',...
  'BackgroundColor',[0.75 0.0 0.0],...
  'ForegroundColor', [1 1 1], 'HorizontalAlignment', 'center',...
  'Callback',strcat('MESHPlot = get(gcbf,''Userdata'');',...
  'MESHPlot.p = gui_mesh_plot(MESHPlot.p,''close'');',...
  'clear MESHPlot;'));

% Hold GUI Open checkbox
G.Bhold = uicontrol('Parent',GUI,'Style','checkbox','Units','Normalized', Font, ...
  'Position',[.70 .01 .25 .05],'String','Hold','BusyAction','queue',...
  'TooltipString','MESH Plot GUI remains open after ''Return'' commands.',...
  'Value',p.hold,'HorizontalAlignment', 'center');


% Store userdata
if exist('parent','var'), MESHPlot.parent.gui = parent; end
MESHPlot.handles = G;
MESHPlot.p = p;
set(GUI,'Userdata',MESHPlot);
figure(MESHPlot.gui);

return
