KeyPress function on data rather on figure

3 次查看(过去 30 天)
Hi,
I am trying to implement a key-press function for basically a fly through scene, i.e. updating the cam target and cam pos through a patched surface.
However, the key-press is acting on the figure itself instead of the plotted data. Is there another way to implement it/what am I doing wrong?
Thanks, Ali
Example code:
patch_fig=figure;
p=patch('Faces',faces,'Vertices',vertices,'FaceColor','red');
set(patch_fig,'KeyPressFcn',{@fh_keypress_handle, patch_fig});
%%function%%
function fh_keypress_handle(H,E,patch_fig)
hAx=gca;
[cPosition, cTarget, cViewAngle, cViewVector, cRightVector, cUpVector] = getcamerageometry(hAx);
current_pos = campos(hAx);
current_camtarget = camtarget(hAx);
switch E.Key
%%Move in x direction
case 'uparrow'
if isempty(E.Modifier)
campos(current_pos + cViewVector);
camtarget([current_camtarget+cViewVector]);
camlight(hlight,'headlight')
case 'downarrow'
if isempty(E.Modifier)
campos(current_pos - cViewVector);
camtarget([current_camtarget-cViewVector]);
end

回答(2 个)

Walter Roberson
Walter Roberson 2017-2-7
You should probably use a figure WindowKeypressFcn callback instead of a figure KeyPressFcn. The KeyPressFcn has the tendency to give typing focus to the figure.
But first alter all of your cam* calls to pass in the axes. And instead of fetching the axes using gca(), pass it in instead of patch_fig (which is not used in your routine.)
  3 个评论
Walter Roberson
Walter Roberson 2017-2-7
The axes can go as the first argument to the cam* calls.
I would fix the axes stuff up first before switching to WindowKeyPressFcn; you might not have to switch that.
Rheeda
Rheeda 2017-2-7
Hi Walter,
I've update the code to pass in the camera properties in the axes handle, and have changed to WindowPres but the up/down arrows still move the figure itself (KeyPress gives the same response).
Am I still doing something wrong?
Thanks for your help, Rheeda
patch_fig=figure;
p=patch('Faces',faces,'Vertices',vertices,'FaceColor','red');
reducepatch(p,0.7);
Centreofmass=[mean(vertices(:,1)), mean(vertices(:,2)),mean(vertices(:,3))];
hAx=gca;
[cPosition, cTarget, cViewAngle, cViewVector, cRightVector, cUpVector] = getcamerageometry(hAx);
hAx.CameraPosition=[Centreofmass(1), Centreofmass(2),Centreofmass(3)];
hAx.CameraTarget= [cTarget(1) cTarget(2) cTarget(3)];
set(patch_fig,'WindowKeypressFcn',{@fh_keypress_handle_edit, hAx});
function fh_keypress_handle_edit(H,E,hAx)
current_pos = hAx.CameraPosition;
current_camtarget = hAx.CameraTarget;
[cPosition, cTarget, cViewAngle, cViewVector, cRightVector, cUpVector] = getcamerageometry(hAx);
[azimuth,elevation,r] = cart2sph(hAx.CameraTarget(1), hAx.CameraTarget(2), hAx.CameraTarget(3));
switch E.Key
%%Move in x direction
case 'uparrow'
if isempty(E.Modifier)
campos(current_pos + cViewVector);
camtarget([current_camtarget+cViewVector]);
hAx.CameraPosition=current_pos + cViewVector;
hAx.CameraTarget= current_camtarget+cViewVector;
case 'downarrow'
if isempty(E.Modifier)
campos(current_pos - cViewVector);
camtarget([current_camtarget-cViewVector]);
hAx.CameraPosition=current_pos + cViewVector;
hAx.CameraTarget= current_camtarget+cViewVector;
end

请先登录,再进行评论。


Rheeda
Rheeda 2017-2-7
编辑:Walter Roberson 2017-2-7
All I need in my script was:
camproj perspective
camva(50)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by