📄 mplay.m
字号:
% Setup movie area
defaultN = 64;
haxis = axes( ...
'parent',hfig, ...
'pos',[0 0 1 1], ...
'vis','off', ...
'xlim',[1 defaultN], ...
'ylim', [1 defaultN], ...
'ydir','reverse', ...
'xlimmode','manual',...
'ylimmode','manual',...
'zlimmode','manual',...
'climmode','manual',...
'alimmode','manual',...
'layer','bottom',...
'nextplot','add', ...
'dataaspectratio',[1 1 1], ...
'drawmode','fast');
himage = image(...
'parent',haxis, ...
'cdata',zeros(defaultN), ...
'xdata',1:defaultN, ...
'ydata',1:defaultN, ...
'erase','none');
% Setup default figure data
%
ud = get(hfig,'userdata');
ud.hfig = hfig;
ud.haxis = haxis;
ud.himage = himage;
ud.htoolbar = CreateButtonBar(hfig);
ud.htimer = [];
ud.hStatusBar = AddStatusBar(hfig);
ud.loopmode = 0; % non-looping
ud.fbmode = 0; % no fwd/bkwd playback
ud.fbrev = 0; % not in rev-playback
ud.paused = 0;
ud.nextframe = 1;
ud.currframe = 0;
ud.jumpto_frame = 1;
ud.fps = 0; % frames per second
ud.varName = ''; % variable name passed to player
ud.numFrames = 0;
ud.frameRows = 0;
ud.frameCols = 0;
ud.cmapexpr = 'gray(256)'; % default
ud.cmap = gray(256); % default
ud.UpdateImageFcn = [];
ud.Movie = [];
ud.MovieFormat = '';
set(hfig,'userdata', ud);
% Finishing touches:
% - show GUI
% - enable resize fcn
set(hfig, ...
'colormap', ud.cmap, ...
'resizefcn', @ResizeFcn);
% -------------------------------------------------------------------------
function KeypressFcn(hcb, eventStruct)
% Handle keypresses in main window
hfig = gcbf;
key = get(hfig, 'CurrentChar');
if ~isempty(key), % unix keyboard has an empty key
switch lower(key)
case char(29) % right
cb_step_fwd(hfig,[]);
case char(28) % left
cb_step_back(hfig,[]);
case 'f'
cb_ffwd(hfig,[]);
case 'r'
cb_rewind(hfig,[]);
case 's'
cb_stop(hfig,[]);
case 'p'
cb_play(hfig,[]); % play/pause
case char(30) % up
cb_goto_start(hfig,[]);
case char(31) % down
cb_goto_end(hfig,[]);
case 'j' % jump to
EditFrameNum(hfig);
case 'l' % loop
cb_loop(hfig,[]);
case 'x' % fwd/bkwd playback
cb_fbmode(hfig,[]);
case char(13) % Enter
% Open parameters dialog
EditProperties(hfig);
end
end
% --------------------------------------------------------
function DeleteFcn(hcb, eventStruct)
% How to get here:
% Close all force
% delete(h)
hfig=gcbf;
ud = get(hfig,'userdata');
if ~isempty(ud),
stop(ud.htimer); % Shut off timer if running
end
delete(hfig); % Close window
% --------------------------------------------------------
function ResizeFcn(hcb, eventStruct)
fd = get(hcb,'UserData'); % hcb = hfig
hAll = fd.hStatusBar.All;
% Get positions and compute resize offsets:
fig_pos = get(hcb,'pos');
frame_pos = get(fd.hStatusBar.Region,'pos');
frame_rt = frame_pos(1)+frame_pos(3)-2;
delta = fig_pos(3)-frame_rt;
% Adjust positions:
for i=1:length(hAll),
pos = get(hAll(i),'pos');
pos(1) = pos(1) + delta;
set(hAll(i),'pos',pos);
end
% Separately adjust hStatusBar.Region
% Grow it wider, don't move it:
pos = frame_pos;
pos(3)=pos(3)+delta;
set(fd.hStatusBar.Region,'pos',pos);
% --------------------------------------------------------
function hStatusBar = AddStatusBar(hfig)
hfig_pos = get(hfig,'pos');
CR = sprintf('\n');
% Status region frame
bg = [1 1 1]*.8;
pos=[0 0 hfig_pos(3)+2 22];
hStatusBar.Region = uicontrol('parent',hfig, ...
'style','frame', ...
'units','pix', ...
'pos',pos, ...
'backgr', bg, ...
'foregr', [1 1 1]);
% Render right after background frame, so when resizing occurs,
% this will be "overwritten" by other data
hStatusBar.StatusText = uicontrol('parent',hfig, ...
'style','text', ...
'units','pix', ...
'pos',[2 1 100 16], ...
'string','Ready', ...
'horiz','left', ...
'backgr',bg, ...
'foregr','k');
% Indents for display/trace timing
pos1 = [pos(3)-64 1 60 17];
[hStatusBar.FPS, hAll1] = makeStatusBarIndent(hfig,bg,pos1);
set(hStatusBar.FPS,'string','FPS: 0', ...
'tooltip' ,['Frames Per Second']);
set(hAll1,'ButtonDownFcn',@cb_properties);
pos2 = [pos(3)-128 1 60 17];
[hStatusBar.FrameNum, hAll2] = makeStatusBarIndent(hfig,bg,pos2);
set(hStatusBar.FrameNum,'string','0:0', ...
'tooltip', ['Current Frame : Total Frames']);
set(hAll2,'ButtonDownFcn',@cb_jumpto);
pos2 = [pos(3)-148-74 1 90 17];
[hStatusBar.FrameSize, hAll3] = makeStatusBarIndent(hfig,bg,pos2);
set(hStatusBar.FrameSize,'string','(FrameSize)', ...
'tooltip', '(framesize readout)');
% set(hAll3,'ButtonDownFcn',@cb_properties);
% Group all status bar widget handles together, for resizing:
hStatusBar.All = [hAll1 hAll2 hAll3];
% -------------------------------------------------------------------------
function [h, hall] = makeStatusBarIndent(hfig,bg,pos1)
hall(1)=uicontrol('parent',hfig, ...
'style','frame', ...
'units','pix', ...
'pos',pos1, ...
'backgr', bg, ...
'foregr', [1 1 1]*.4);
pos2=pos1;
pos2(4)=1;
hall(2)=uicontrol('parent',hfig, ...
'style','frame', ...
'units','pix', ...
'pos',pos2, ...
'backgr', [1 1 1], ...
'foregr', [1 1 1]);
pos2=[pos1(1)+pos1(3)-1 pos1(2) 1 pos1(4)];
hall(3)=uicontrol('parent',hfig, ...
'style','frame', ...
'units','pix', ...
'pos',pos2, ...
'backgr', [1 1 1], ...
'foregr', [1 1 1]);
hall(4) = uicontrol('parent',hfig, ...
'style','text', ...
'units','pix', ...
'horiz','left', ...
'fontweight','light', ...
'fontsize',8, ...
'pos',pos1+[2 2 -3 -3], ...
'string', 'test', ...
'backgr', bg, ...
'foregr', [0 0 0]);
h=hall(4); % main text widget
% -------------------------------------------------------------------------
function UpdateStandardStatusText(hfig)
% Setup some status bar text, indicating
% current scope state
%
ud = get(hfig,'userdata');
isRunning = strcmp(get(ud.htimer,'Running'),'on');
isPaused = ~isRunning & ud.paused;
isStopped = ~isRunning & ~ud.paused;
if isStopped, str = 'Stopped';
elseif isPaused, str = 'Paused';
else str = 'Playing';
end
UpdateStatusText(hfig,str);
% -------------------------------------------------------------------------
function UpdateStatusText(hfig,str)
% Set arbitrary text into status region
if isempty(hfig), return; end
fd = get(hfig,'UserData');
set(fd.hStatusBar.StatusText,'string',str);
% --------------------------------------------------------
function UpdateGUIControls(hfig)
% Update GUI state, including:
% - button enable states/icons
% - status bar text
UpdateButtonEnables(hfig);
UpdateStandardStatusText(hfig);
% --------------------------------------------------------
function UpdateButtonEnables(hfig)
% Button states:
% Enabled=1
% Disabled=0
% stopped paused running
% Props (all 1)
% Truesz (all 1)
% Export (all 1)
%
% 1st (all 1)
% Rew (all 1)
% StepRev 1 1 0
% Stop 0 1 1
% Play 1 1 1
% StepFwd 1 1 0
% FFwd (all 1)
% Last (all 1)
%
% JumpTo
% Loop
% Fwd/Bkwd Play
ud = get(hfig,'userdata');
isRunning = strcmp(get(ud.htimer,'Running'),'on');
isPaused = ~isRunning && ud.paused;
isStopped = ~isRunning && ~ud.paused;
if isStopped, ena = [1 0 1 1];
elseif isPaused, ena = [1 1 1 1];
else ena = [0 1 1 0];
end
hchild = flipud(get(ud.htoolbar,'children'));
for i=1:4,
if ena(i), s='on'; else s='off'; end
set(hchild(5+i),'enable',s);
end
% --------------------------------------------------------
function htoolbar = CreateButtonBar(hfig)
% Create button bar
% Get a bunch of playback-related icons
icons = mergefields(load('audiotoolbaricons'), load('mplay_icons'));
CR = sprintf('\n');
htoolbar = uitoolbar(hfig); % Create toolbar
setappdata(htoolbar,'icons',icons); % Store icons in toolbar appdata
uipushtool(htoolbar, ...
'cdata', icons.params, ...
'tooltip','Properties...', ...
'click', @cb_properties);
uipushtool(htoolbar, ...
'cdata', icons.fit_to_view, ...
'tooltip','True size', ...
'click', @cb_truesize);
hexp=uipushtool(htoolbar, ...
'cdata', icons.export_imview, ...
'tooltip','Export to IMVIEW', ...
'click', @cb_export_imview);
% Check for IMVIEW
if ~exist('imview','file'),
% set(hexp,'enable','off');
set(hexp,'tooltip', 'Export to Workspace');
end
uipushtool(htoolbar, ...
'cdata', icons.goto_start_default, ...
'tooltip','Go to start', ...
'separator','on', ...
'click', @cb_goto_start);
uipushtool(htoolbar, ...
'cdata', icons.rewind_default, ...
'tooltip','Rewind', ...
'click', @cb_rewind);
uipushtool(htoolbar, ...
'cdata', icons.step_back, ...
'tooltip','Step back', ...
'click', @cb_step_back);
uipushtool(htoolbar, ...
'cdata', icons.stop_default, ...
'tooltip','Stop', ...
'click', @cb_stop);
uipushtool(htoolbar, ...
'cdata', icons.play_on, ...
'tooltip','Play', ...
'tag','Play/Pause', ...
'click', @cb_play);
uipushtool(htoolbar, ...
'cdata', icons.step_fwd, ...
'tooltip','Step forward', ...
'click', @cb_step_fwd);
uipushtool(htoolbar, ...
'cdata', icons.ffwd_default, ...
'tooltip','Fast forward', ...
'click', @cb_ffwd);
uipushtool(htoolbar, ...
'cdata', icons.goto_end_default, ...
'tooltip','Go to end', ...
'click', @cb_goto_end);
uipushtool(htoolbar, ...
'cdata', icons.jump_to, ...
'separator','on', ...
'tooltip','Jump to...', ...
'click', @cb_jumpto);
uipushtool(htoolbar, ...
'cdata', icons.loop_off, ...
'tooltip',['Repeat playback'], ...
'tag','loopbutton', ...
'click', @cb_loop);
uipushtool(htoolbar, ...
'cdata', icons.fwdbk_play_off, ...
'tooltip',['<default>'], ...
'tag','fbbutton', ...
'click', @cb_fbmode);
% --------------------------------------------------------
function icons = get_icons_from_fig(hfig)
udfig = get(hfig,'userdata');
udtb = getappdata(udfig.htoolbar);
icons = udtb.icons;
% --------------------------------------------------------
function cb_goto_start(hbutton, eventStruct, hfig)
% goto start button callback
% jump to frame 1, cancel bkwd playback (if on)
if nargin<3, hfig = gcbf; end
ud = get(hfig,'userdata');
if ud.currframe ~= 1, % prevent repeated presses
ud.currframe = 1;
ud.nextframe = 1;
ud.fbrev = 0; % cancel any bkwd playback set(hfig,'userdata',ud);
ShowMovieFrame(hfig);
end
% --------------------------------------------------------
function cb_rewind(hbutton, eventStruct, hfig)
% Rewind button callback
% Backup "stepsize" frames
if nargin<3, hfig = gcbf; end
ud = get(hfig,'userdata');
stepsize = 10;
if ud.fbmode,
% fwd/back playback mode
if ud.fbrev,
% in reverse ... increment
if ud.currframe >= ud.numFrames-stepsize,
% hit last frame ... go to reverse
ud.currframe = ud.numFrames;
ud.fbrev=0; % no more reverse playback
else
ud.currframe = ud.currframe+stepsize;
end
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -