1. % Convert current
  2. % Test As:
  3. %
  4. % plot(1:50, peaks(50))
  5. % hoverlines(gca);
  6. %
  7. function hoverlines( varargin )
  8. % takes one or zero arguments
  9. switch nargin
  10. case 0, ax = gca;
  11. case 1, ax = varargin{1};
  12. otherwise, error('bad args');
  13. end
  14. % get figure
  15. fig = get(ax,'parent');
  16. % get all line children of the axes
  17. lines = findobj( get(ax,'children'), 'type', 'line');
  18. if isempty(lines),
  19. warning('no lines, nothing done');
  20. return
  21. end
  22. % set hover behavior
  23. setbehavior(lines);
  24. % reset all sitting
  25. for h = lines(:)'
  26. d = get(h,'userdata');
  27. d.sit();
  28. end
  29. set(ax,'userdata',lines(1));
  30. set(fig,'windowButtonMotionFcn',@hover)
  31. end
  32.  
  33. %%
  34. function hover(fig, ignore)
  35. % click
  36. rob = java.awt.Robot();
  37. rob.mousePress(java.awt.event.InputEvent.BUTTON1_MASK);
  38. rob.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
  39.  
  40. % get object under cursor
  41. hovering = get(fig,'currentObject');
  42. % return if hovering over same object
  43. lastone = get(gca,'userdata');
  44. if hovering == lastone, return; end
  45. % get behavior data
  46. hData = get(hovering,'userdata');
  47. % hovering over some other type of object perhaps
  48. if ~isfield( hData, 'sit' ), return; end
  49. % ok, stand up
  50. hData.stand();
  51. % sit-down previous
  52. hData = get(lastone,'userdata');
  53. hData.sit();
  54. % store as lastone
  55. set(gca,'userData',hovering);
  56. end
  57.  
  58. %% Set hover behavior
  59. function setbehavior( hs )
  60. for h = hs(:)'
  61. high = get(h,'color');
  62. dim = rgb2hsv(high);
  63. dim(2) = 0.1;
  64. dim(3) = 0.9;
  65. dim = hsv2rgb(dim);
  66.  
  67. hov.sit = @() set(h ...
  68. ,'color', dim ...
  69. ,'linewidth', 1 ...
  70. ... ,'marker', 'none' ...
  71. );
  72. hov.stand = @() set(h ...
  73. ,'color', high ...
  74. ,'linewidth', 2 ...
  75. ... ,'marker', '*' ...
  76. );
  77. set(h,'userdata',hov);
  78. end
  79. end
  80.