guyts
guyts

Reputation: 949

MATLAB - Detect double mouse click on figure

I'm using impoly to allow the user to edit a polygon on a figure. Right now, I'm using pause() to detect when user is done, but I'd rather if it were a double-mouse click (similar to what roipoly does).

I cannot use roipoly though, since it does not allow an initial polygon be plotted, which is necessary.

Any ideas on how to get that?

Upvotes: 2

Views: 758

Answers (1)

gnovice
gnovice

Reputation: 125854

The impoly tool appears to modify the WindowButtonDownFcn, WindowButtonMotionFcn, WindowButtonUpFcn, WindowKeyPressFcn, and WindowKeyReleaseFcn callbacks of the figure window. I had originally thought that you couldn't modify any of these because they would be overwritten by the callback function used by impoly for its functionality. However, it turns out that they can still be invoked properly. This gives you a few more options:


Modify WindowButtonDownFcn:

To add the ability to detect a double-click, you would have to use the WindowButtonDownFcn callback. For example:

set(gcf, 'WindowButtonDownFcn', @double_click_fcn);
h = impoly();

% Define this function somewhere (nested, local, etc.):
function double_click_fcn(hSource, ~)
  if strcmp(get(hSource, 'SelectionType'), 'open')
    % Advance to next frame
  end
end


Modify WindowScrollWheelFcn:

Whenever I create a GUI where I have to scroll through a number of time points/plots/images, I like to use the WindowScrollWheelFcn callback to advance (scroll up) or rewind (scroll down) the data. You could use it to scroll from frame to frame, displaying whatever polygon has already been drawn (if there is one) or allowing the user to create a new one. For example:

set(gcf, 'WindowScrollWheelFcn', @scroll_fcn)
h = impoly();

% Define this function somewhere (nested, local, etc.):
function scroll_fcn(~, eventData)
  if (eventData.VerticalScrollCount < 0)
    % Mouse has been scrolled up; move to next frame
  else
    % Mouse has been scrolled down; move to previous frame
  end
end


Modify WindowKeyPressFcn

You could also use the WindowKeyPressFcn callback to allow you to advance frames using keyboard buttons, like the left and right arrow keys. For example:

set(gcf, 'WindowKeyPressFcn', @keypress_fcn)
h = impoly();

% Define this function somewhere (nested, local, etc.):
function keypress_fcn(~, eventData)
  switch eventData.Key
    case 'rightarrow'
      % Right arrow pressed; move to next frame
    case 'leftarrow'
      % Left arrow pressed; move to previous frame
  end
end


For more information on creating all these callbacks, see here.

Upvotes: 2

Related Questions