Home > chronux_2_00 > spikesort > utility > uitools > private > CB_movieplayer.m

CB_movieplayer

PURPOSE ^

CB_MOVIEPLAYER Callback for UImyfunc.

SYNOPSIS ^

function CB_movieplayer(handle, events)

DESCRIPTION ^

CB_MOVIEPLAYER    Callback for UImyfunc.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function CB_movieplayer(handle, events)
0002 %CB_MOVIEPLAYER    Callback for UImyfunc.
0003 
0004 persistent running direction renderer;
0005 if (isempty(running)), running = 0;  end;
0006 if (isempty(direction)), direction = 1;  end;
0007 
0008 info  = guidata(handle);  % get data & associated info
0009 
0010 % Constants
0011 fpsupdate = 8;
0012 
0013 switch(handle),
0014     case {info.menu.intp, info.menu.rndr}       % TOGGLE COLOR INTERPOLATION
0015         % OpenGL is sometimes faster, depending on hardware.
0016         % However, OpenGL interpolates in RGB rather than through the colormap.
0017         togglecheck(handle);
0018         if (onoff2bool(get(info.menu.intp,'Checked')) || info.hires),
0019             shade = 'interp';  xlim = [1,info.N];   ylim = [1,info.M];   % don't need pad
0020         else
0021             shade = 'flat';    xlim = [1,info.N+1]; ylim = [1,info.M+1]; % need pad'd row/col
0022         end
0023         if (onoff2bool(get(info.menu.rndr, 'Checked'))),
0024             renderer = 'opengl';
0025         else
0026             renderer = 'zbuffer';
0027         end
0028         shading(info.haxs, shade);   set(info.hfig, 'Renderer', renderer);
0029         set(info.haxs, 'XLim', xlim, 'YLim', ylim);    
0030         
0031     case ({info.menu.play, info.menu.rply}),   % FORWARD OR REVERSE PLAY
0032         check = togglecheck(handle);
0033         renderer = get(info.hfig, 'Renderer');        
0034         if (check)  % starting to play
0035             if (handle == info.menu.play)  % prevent switching directions while playing
0036                 direction =  1;  set(info.menu.rply, 'Enable', 'off');
0037             else
0038                 direction = -1;  set(info.menu.play, 'Enable', 'off');
0039             end;
0040             set(info.menu.parm, 'Enable', 'off');
0041             running = 1;   % anyone else can set this to 0 to interrupt play
0042             counter = 1;   fps = 0;   tic;   % keep track of display frame rate
0043             next = info.frame;
0044             while (running)
0045                 next = next + direction*(info.skip);   % try to increment ...
0046                 if (next>info.P), direction = -1;  break;  end;
0047                 if (next < 1),    direction =  1;  break; end;
0048                 updateframe(info, next);   info.frame = next;
0049                 set(info.hfig, 'Name', sprintf('%s  FPS: % 5.3f', renderer, fps));
0050                 drawnow;  pause(info.slow);
0051 
0052                 if (counter == fpsupdate)   % frame rate updates
0053                     counter = 1;   fps = fpsupdate/toc;  tic;
0054                 else  counter = counter + 1;
0055                 end
0056             end
0057             running = 0;
0058             set([info.menu.play, info.menu.rply, info.menu.parm], 'Enable', 'on', 'Checked', 'off');
0059             set(info.hfig, 'Name', 'UImovieplayer');
0060             guidata(info.hfig, info);
0061         else
0062             running = 0;   % signal an interruption
0063         end
0064         
0065     case info.menu.parm,     % TIMING PARAMETERS
0066         params = {'SKIP: frames advanced per step', ...
0067                   'SLOW: pause interval during playback (msec)', ...
0068                   'Fs:   temporal sampling rate (Hz)'};
0069         defaults = {sprintf('%d', info.skip), sprintf('%5.3f', info.slow), sprintf('%8.6f', info.Fs)};
0070         values = inputdlg(params, '', 1, defaults);
0071         if (~isempty(values))   % validate parameters
0072             skip = str2num(values{1});  skip = round(skip);
0073             if (~isempty(skip) && skip > 0 && skip < info.P), info.skip = skip;  end;
0074             slow = str2num(values{2});
0075             if (~isempty(slow) && slow > 0), info.slow = slow;  end;
0076             Fs   = str2num(values{3});
0077             if (~isempty(Fs) && Fs > 0), info.T = (1:info.P)/Fs;  info.Fs = Fs;  end;
0078         end
0079         updateframe(info, info.frame);
0080         info = redraw_timeseries(info);
0081         guidata(info.hfig, info);
0082         
0083     case info.hfig,          % KEY PRESS HANDLER
0084         key = get(info.hfig, 'CurrentChar');
0085         if (key == ' ')   % pause always works
0086             if (running),  running = 0;
0087             else 
0088                 if (direction == 1),  CB_movieplayer(info.menu.play);
0089                 else                  CB_movieplayer(info.menu.rply);
0090                 end
0091             end
0092         end
0093         if (~running)     % but other keypresses are ignored if movie is playing
0094             next = info.frame;
0095             switch(key)
0096                 case '<',  next = 1;
0097                 case ',',  next = next - info.skip;
0098                 case '?',  % next = next;
0099                 case '.',  next = next + info.skip;
0100                 case '>',  next = info.P;
0101                 otherwise, return;
0102             end
0103             if (next > 0 && next <= info.P)
0104                 updateframe(info, next);   info.frame = next;
0105             end
0106             guidata(info.hfig, info);
0107         end
0108         
0109     case info.hdat,         % TIME SERIES HANDLER
0110         % clicking in the image draws a time series for corresponding
0111         % pixel in the lower time-series axes
0112         if (~running && strcmp(get(info.hfig, 'SelectionType'), 'normal'))
0113              click = floor(get(info.haxs, 'CurrentPoint'));  click = click(1,1:2)';
0114             if (click(1)>=1 && click(1)<=info.N && click(2)>=1 && click(2)<=info.M)
0115                 info.tslist = unique([click info.tslist]', 'rows')';
0116                 info = redraw_timeseries(info);
0117                 guidata(info.hfig, info);
0118             end
0119         end
0120         
0121     case info.hax2,         % TIME SERIES AXES
0122         if (~running)
0123             switch(get(info.hfig, 'SelectionType')),
0124                 case 'open',  % dbl-click clears the axes
0125                     cla;   legend off;
0126                     if (ishandle(info.hdtx)),  delete(info.hdtx);  end;
0127                     info.tslist = [];   info.hcsr = [];   info.hdtx = [];
0128                     guidata(info.hfig, info);
0129                 otherwise,
0130             end
0131         end
0132     
0133     case {info.menu.dxdt, info.menu.legd}    % TIME SERIES AXES -- special functions
0134         if (~running)
0135             check = togglecheck(handle);
0136             info = redraw_timeseries(info);
0137             guidata(info.hfig, info);
0138         end
0139         
0140     case info.hcsr,
0141         if (~running && strcmp(get(info.hfig,'SelectionType'),'normal'))
0142             wide = get(info.hax2, 'Position');  wide = wide(3);                
0143             xlim = get(info.hax2, 'XLim');
0144             dataperpixel = (xlim(2)-xlim(1)) ./ wide;  % cursor motion scale
0145             
0146             time = get(handle, 'XData');    time = time(1);  % starting pt
0147             
0148             info.buttondown = 1;   guidata(handle, info);
0149             pntA = get(info.hfig, 'CurrentPoint');
0150             while (info.buttondown)
0151                 running = 1;  % make sure that we're not interrupted
0152                 pntB = get(info.hfig, 'CurrentPoint');
0153                 newtime = time + (pntB(1)-pntA(1))*dataperpixel;  
0154                 newtime = max(min(newtime, xlim(2)), xlim(1));  % clip to axes
0155                 newframe = round(newtime*info.Fs);
0156                 newframe = max(min(newframe,info.P),1);
0157 
0158                 set(info.hcsr, 'XData',  [newtime newtime]);
0159                 updateframe(info, newframe);     drawnow;
0160                 info = guidata(handle);
0161             end
0162             running = 0;  info.frame = newframe;
0163             guidata(handle, info);
0164         end
0165         
0166     otherwise,
0167         error('Error in UImovieplayer callback: unknown function.');
0168 end
0169 
0170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0171 %%%%%%%%%%%%%%%%%%%%%%%%% HELPER Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%
0172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0173 
0174 function check = togglecheck(handle)
0175 % When HANDLE is a handle to a UI menu, its 'Checked' property
0176 %  is toggled between 'off' and 'on', and a boolean value representing
0177 %  the new state ('off'->0, 'on'->1) is returned.
0178 check = ~onoff2bool(get(handle,'Checked'));
0179 set(handle, 'Checked', bool2onoff(check));
0180 
0181 function updateframe(info, framenum)
0182 % Updates data and title/time displays
0183 set(info.httl, 'String', sprintf('Time: % 8.3f', info.T(framenum)));
0184 set(info.hdat, 'CData',  [double(info.data(:,:,framenum)) info.COLpad; info.ROWpad]);
0185 if (~isempty(info.hcsr))
0186     set(info.hcsr, 'XData',  [info.T(framenum) info.T(framenum)]);
0187 end
0188 
0189 
0190 function info = redraw_timeseries(info)
0191 % Redraw the time-series axes
0192 if (~isempty(info.tslist))
0193     N = size(info.tslist,2);
0194     
0195     axes(info.hax2);
0196     subdata = [];  leg = {};
0197     for k = 1:N    % extract pixel time series; do this fresh each time
0198         subdata = cat(2, subdata, squeeze(info.data(info.tslist(2,k), info.tslist(1,k), :)));
0199         leg = cat(2, leg, {sprintf('R:%d/C:%d',info.tslist(2,k),info.tslist(1,k))}); % legend
0200     end
0201     
0202     clim = info.CLIM;
0203     if (onoff2bool(get(info.menu.dxdt, 'Checked'))),
0204         subdata = diff(double([subdata(1,:); subdata]), [], 1);
0205         clim = minmax(subdata);
0206     end
0207     axes(info.hax2);  plot(info.T, subdata);  set(info.hax2, 'XLim', [info.T(1), info.T(end)]);
0208     if (onoff2bool(get(info.menu.legd, 'Checked'))), legend(leg);  end;
0209     
0210     % reset the button down callback & draw reference line
0211     info.hcsr = line([0 0], clim, 'LineWidth', 2, 'Color', [0.6 0.6 0.6], 'LineStyle', '--');
0212     set([info.hcsr,info.hax2], 'ButtonDownFcn', @CB_movieplayer);
0213     
0214     % Draw markers on the data to show time series source points
0215     axes(info.haxs);  hold on;
0216     if (ishandle(info.hdtx)),  delete(info.hdtx);  end;
0217     pts = cat(1, mat2cell(info.tslist(1,:),1, ones(N,1)), ...  % dirty trick to plot pts in diff colors
0218                mat2cell(info.tslist(2,:),1,ones(N,1)), mat2cell(repmat('x',1,N),1,ones(N,1)));
0219     hold on;  info.hdtx = plot(pts{:});  hold off;
0220     set(info.hax2, 'YLim', clim);
0221 end

Generated on Fri 15-Aug-2008 11:35:42 by m2html © 2003