Home > bioelectromagnetism > mouse_rotate.m

mouse_rotate

PURPOSE ^

mouse_rotate - Left click to 3Drotate a matlab figure graphic

SYNOPSIS ^

function mouse_rotate(command,H)

DESCRIPTION ^

 mouse_rotate - Left click to 3Drotate a matlab figure graphic

 mouse_rotate(command,H)
 
 The mouse 'up' command switches between rotating and not
 rotating the figure.  The mouse 'rotate' command, when enabled,
 rotates the figure in the direction of mouse movement 
 within the figure.
 
 H is the figure userdata, unless provided otherwise.  It
 contains various fields, see the .m file for details.
 
 The figure context menu holds access to this function
 even after 'exit' (ie, right click in figure window
 away from any axis, data, colorbar, etc objects).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function mouse_rotate(command,H)
0002 
0003 % mouse_rotate - Left click to 3Drotate a matlab figure graphic
0004 %
0005 % mouse_rotate(command,H)
0006 %
0007 % The mouse 'up' command switches between rotating and not
0008 % rotating the figure.  The mouse 'rotate' command, when enabled,
0009 % rotates the figure in the direction of mouse movement
0010 % within the figure.
0011 %
0012 % H is the figure userdata, unless provided otherwise.  It
0013 % contains various fields, see the .m file for details.
0014 %
0015 % The figure context menu holds access to this function
0016 % even after 'exit' (ie, right click in figure window
0017 % away from any axis, data, colorbar, etc objects).
0018 %
0019 
0020 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $
0021 
0022 %Licence:            GNU GPL, no implied or express warranties
0023 %Created:  10/1995 - Eric Soroos
0024 %Modified: 02/2002 - Darren.Weber_at_radiology.ucsf.edu
0025 %                  - integrated all commands into one function .m file
0026 %                  - changed rotation to mouse direction rather than
0027 %                    anti-mouse direction and during mouse up rather
0028 %                    than mouse down.
0029 %                  - changed target to the 'view' property of gca
0030 %                  - added elevation,azimuth,view2/3,exit uicontrols
0031 %                    and uicontext menu.
0032 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0033 
0034 if ~exist('command','var'), command = 'init'; end
0035 
0036 
0037 command = lower(command);
0038 
0039 switch command,
0040   
0041   case 'init',
0042     
0043     if exist('H','var'),
0044       if ~isempty(H),
0045         H = INIT(H);
0046       end
0047     else
0048       H = get(gcbf,'userdata');
0049       H = INIT(H);
0050     end
0051     
0052   case 'up',
0053     
0054     if ~exist('H','var'), H = get(gcbf,'userdata'); end
0055     if isempty(H),        H = get(gcbf,'userdata'); end
0056     if isempty(H),        H = get(gcf, 'userdata'); end
0057     
0058     %This switches between rotation and no rotation.
0059     %On a button press, it checks whether the motion function is
0060     %set - if yes, it clears it, otherwise it adds it.
0061     if isempty(get(H.gui,'WindowButtonMotionFcn')),
0062       set(H.gui,'WindowButtonMotionFcn','mouse_rotate(''rotate'');');
0063       %set(H.gui,'WindowButtonDownFcn','mouse_rotate(''up''); ');
0064       %set(H.info,'Value',1);
0065     else
0066       H.pos.current = get(H.axis,'View');
0067       %fprintf('\nCurrent Figure View: Az = %6.4f\tEl = %6.4f\n\n',H.pos.current);
0068       set(H.gui,'WindowButtonMotionFcn',[]);
0069       %set(H.gui,'WindowButtonDownFcn',[]);
0070       %set(H.info,'Value',0);
0071     end
0072     
0073   case 'rotate',
0074     
0075     if ~exist('H','var'), H = get(gcbf,'userdata'); end
0076     if isempty(H),        H = get(gcbf,'userdata'); end
0077     if isempty(H),        H = get(gcf, 'userdata'); end
0078     
0079     % rotates the figure in the direction of mouse movement
0080     % i.e. in the center, 0 altitude, 0 azimuth. Going up and down
0081     % changes the altitude, left and right changes the azimuth.
0082     H.pos.fig = get(H.gui,'position');
0083     H.pos.mouse = get(0,'pointerlocation');
0084     H.pos.relative = -1 * (((H.pos.mouse(1:2) - H.pos.fig(1:2))./ H.pos.fig(3:4))-.5).*[360,190];
0085     set(H.axis,'View',H.pos.relative);
0086     if ishandle(H.Az), set(H.Az,'String',sprintf('Az = %4.0f',H.pos.relative(1))); end
0087     if ishandle(H.El), set(H.El,'String',sprintf('El = %4.0f',H.pos.relative(2))); end
0088     
0089     % Adjust the light angle
0090     if isfield(H,'light'),
0091       if ishandle(H.light), delete(H.light); end
0092       H.light = camlight('headlight','infinite');
0093       set(H.light,'color',[1 1 1]/length(H.light)/1.2);
0094     end
0095     %     else
0096     %         H.light = camlight('headlight','infinite');
0097     %         set(H.light,'color',[1 1 1]/length(H.light)/1.2);
0098     %     end
0099     
0100     set(H.gui,'userdata',H);
0101     
0102   case 'view',
0103     
0104     if ~exist('H','var'), H = get(gcbf,'userdata'); end
0105     if isempty(H),        H = get(gcbf,'userdata'); end
0106     if isempty(H),        H = get(gcf, 'userdata'); end
0107     
0108     v = get(H.View,'Value');
0109     set(H.axis,'View',H.data.view(v,:));
0110     set(H.Az,'String',sprintf('Az = %4.0f',H.data.view(v,1)));
0111     set(H.El,'String',sprintf('El = %4.0f',H.data.view(v,2)));
0112     
0113     % Find and remove all light children of axes
0114     AC = get(H.axis,'Children');
0115     for c = 1:length(AC),
0116       ctype = get(AC(c),'type');
0117       if isequal(ctype,'light'),
0118         delete(AC(c));
0119       end
0120     end
0121     
0122     % Create a new light
0123     H.light = camlight('headlight','infinite');
0124     set(H.light,'color',[1 1 1]/length(H.light)/1.2);
0125     
0126     set(H.gui,'userdata',H);
0127     
0128     
0129   otherwise,
0130     
0131 end
0132 
0133 return
0134 
0135 
0136 
0137 
0138 
0139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0141 function [H] = INIT(H),
0142 
0143 %H = get(H,'userdata');
0144 
0145 % only one per figure
0146 if isfield(H,'El'),
0147   if ~isempty(H.El),
0148     return;
0149   end
0150 end
0151 
0152 H.gui = gcf;
0153 H.axis = gca;
0154 
0155 set(H.gui,'DoubleBuffer','on','WindowButtonDownFcn','mouse_rotate(''up''); ');
0156 
0157 % enable right click access to mouse_rotate
0158 menu=uicontextmenu;
0159 a=uimenu(menu,'Label','Rotate','Callback','mouse_rotate; ');
0160 if isempty(get(H.gui,'uicontextmenu')),
0161   set(H.gui,'uicontextmenu',menu);
0162 end
0163 if isempty(get(H.axis,'uicontextmenu')),
0164   set(H.axis,'uicontextmenu',menu);
0165   axsibs = get(H.axis,'Children');
0166   for i=1:length(axsibs),
0167     type = get(axsibs(i),'Type');
0168     if isequal(type,'patch'),
0169       if isempty(get(axsibs(i),'uicontextmenu')),
0170         set(axsibs(i),'uicontextmenu',menu);
0171       end
0172     end
0173   end
0174 end
0175 
0176 % Match background figure colour
0177 bgcolor = get(H.gui,'Color');
0178 % Try to adapt the foreground colour a little
0179 black = find(bgcolor <= .6);
0180 fgcolor = [0 0 0]; %black text
0181 if length(black)>2, fgcolor = [1 1 1]; end
0182 
0183 Font.FontName   = 'Helvetica';
0184 Font.FontUnits  = 'Pixels';
0185 Font.FontSize   = 12;
0186 Font.FontWeight = 'bold';
0187 Font.FontAngle  = 'normal';
0188 
0189 H.Az  = uicontrol('Parent',H.gui,'Style','text',Font,...
0190   'Units','Normalized','Position',[.05 .0 .1 .05],...
0191   'BackGroundColor',bgcolor,...
0192   'ForeGroundColor',fgcolor,...
0193   'String','','TooltipString','Azimuth','HorizontalAlignment','left');
0194 H.El  = uicontrol('Parent',H.gui,'Style','text',Font,...
0195   'Units','Normalized','Position',[.15 .0 .1 .05],...
0196   'BackGroundColor',bgcolor,...
0197   'ForeGroundColor',fgcolor,...
0198   'String','','TooltipString','Elevation','HorizontalAlignment','left');
0199 
0200 H.Info = uicontrol('Parent',H.gui,'Style','radiobutton',Font,...
0201   'Units','Normalized','Position',[.65 .0 .1 .05],...
0202   'HorizontalAlignment','left',...
0203   'BackGroundColor',bgcolor,...
0204   'ForeGroundColor',fgcolor,...
0205   'String','Rotate','Value',1,...
0206   'TooltipString','When on, click in figure to switch on/off 3D rotation.',...
0207   'Callback',strcat('H = get(gcbf,''userdata''); ',...
0208   'rotate = get(H.Info,''Value''); ',...
0209   'if rotate, ',...
0210   'set(H.gui,''WindowButtonDownFcn'',''mouse_rotate(''''up'''');''); ',...
0211   'else, ',...
0212   'set(H.gui,''WindowButtonDownFcn'',[]); ',...
0213   'set(H.gui,''WindowButtonMotionFcn'',[]); ',...
0214   'end; ',...
0215   'clear H rotate;'));
0216 
0217 H.data.viewstr = {'2D' '3D' 'Top' 'Bottom' 'Front' 'Back' 'Left' 'Right'};
0218 H.data.view = [ 0 90; -37.5 30; 0 90; 0 -90; 180 0; 0 0; -90 0; 90 0 ];
0219 
0220 H.View = uicontrol('Parent',H.gui,'Style','Popup',...
0221   'Units','Normalized','Position',[.78 .0 .1 .05],...
0222   'Tag','VIEW','BackGroundColor','w',...
0223   'TooltipString','Select view.',...
0224   'String',H.data.viewstr,...
0225   'CallBack','mouse_rotate(''view'');');
0226 
0227 H.RClose = uicontrol('Parent',H.gui,'Style','pushbutton',...
0228   'Units','Normalized','Position',[.9 .0 .1 .05],...
0229   'String','Close','Value',0,...
0230   'TooltipString','Use right click context menu after ''Close'' to get 3D rotation back.',...
0231   'Callback',strcat('H = get(gcbf,''userdata''); ',...
0232   'delete(H.RClose); delete(H.Info); ',...
0233   'delete(H.El); delete(H.Az); ',...
0234   'delete(H.View); ',...
0235   'fields = fieldnames(H); ',...
0236   'trashfields(1) = strmatch(''El'',   fields,''exact''); ',...
0237   'trashfields(2) = strmatch(''Az'',   fields,''exact''); ',...
0238   'trashfields(3) = strmatch(''Info'', fields,''exact''); ',...
0239   'trashfields(4) = strmatch(''View'', fields,''exact''); ',...
0240   'trashfields(5) = strmatch(''RClose'',fields,''exact''); ',...
0241   'trashfields(6) = strmatch(''pos'',fields,''exact''); ',...
0242   'H = setfield(H,char(fields(trashfields(1))),[]); ',...
0243   'H = setfield(H,char(fields(trashfields(2))),[]); ',...
0244   'H = setfield(H,char(fields(trashfields(3))),[]); ',...
0245   'H = setfield(H,char(fields(trashfields(4))),[]); ',...
0246   'H = setfield(H,char(fields(trashfields(5))),[]); ',...
0247   'H = setfield(H,char(fields(trashfields(6))),[]); ',...
0248   'H.data = setfield(H.data,''view'',[]); ',...
0249   'H.data = setfield(H.data,''viewstr'',[]); ',...
0250   'set(H.gui,''WindowButtonMotionFcn'',[]); ',...
0251   'set(H.gui,''WindowButtonDownFcn'',[]); ',...
0252   'set(H.gui,''WindowButtonUpFcn'',[]); ',...
0253   'set(H.gui,''userdata'',H); clear H trashfields fields; '));
0254 
0255 H.pos.current = get(H.axis,'View');
0256 set(H.Az,'String',sprintf('Az = %4.0f',H.pos.current(1)));
0257 set(H.El,'String',sprintf('El = %4.0f',H.pos.current(2)));
0258 
0259 set(H.gui,'userdata',H);
0260 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003