-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoveable_item.m
181 lines (156 loc) · 6.05 KB
/
moveable_item.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function moveable_item(h, updateFcn, doneFcn,varargin)
% MOVEABLE_ITEM makes a graphical item (line) draggable
%
% MOVEABLE_ITEM( h , updateFcn , doneFcn ); makes the graphics object h draggable, by checking
% for when it gets a mousedown. Then, it makes a copy of h, with some aesthetic modifications
% which is directly moved around the axes while the mouse is down. As the item is moved around
% UPDATEFCN will be repeatedly called. It will be provided the handle for the copy and a [x y]
% delta array. Upon finishing, the DONEFCN will be called, with the handle to the copy. The
% copy is then deleted.
%
% If the escape key is pressed, then the DONEFCN will not be called, and the copy will simply
% be deleted.
%
% UPDATEFCN should accept two values, like: function myUpdateFcn ( handleToCopy , deltas) where
% HANDLETOCOPY can be used to access the new x and y positions. DELTAS is an [x y] pair that
% contains the offset between the original figure and the copy.
%
%
% MOVEABLE_ITEM(..., name, value [,etc]) allows you to name additional properties for the copy.
% That is, you can specify any of the graphical properties, such as Makrer, LineWidth, etc.
%
% MOVEABLE_ITEM('demo') will run a basic example with a couple items on a plot that can be moved
% around
%
% example:
%
% ax = gca;
% h = plot(ax, [1;2;3],[2; 1; 3],'o-');
% hold on;
%
% % this will be used to show how far we dragged the item
% txt = text(mean(ax.XLim), mean(ax.YLim),'','Tag','description');
%
% % this will show how far we dragged the item, and is called as we drag around
% updateFcn=@(x,delta) set(txt,'String', sprintf('deltas: [ %g , %g]',delta));
%
% % this will move our original plot to the new location, once we let up on the mouse button.
% doneFcn=@(x)set(h,'XData',x.XData,'YData',x.YData);
%
% MOVEABLE_ITEM(h,updateFcn,doneFcn, 'Marker','+');
%
% By Celso G Reyes, PhD 2018
if nargin==1 && (ischar(h) || isstring(h)) && h=="demo"
demo();
return
end
COPY.Tag='itemBeingMoved';
switch(h.Type)
case 'line'
COPY.Marker = 'o';
COPY.MarkerEdgeColor = 'r';
COPY.MarkerFaceColor = 'y';
COPY.LineWidth = 2;
COPY.Color = 'g';
COPY.LineStyle = '--';
case 'scatter'
COPY.Marker = 'o';
COPY.MarkerEdgeColor = 'r';
COPY.MarkerFaceColor = 'y';
COPY.LineWidth = 2;
COPY.CData= [0 0 1];
case 'text'
COPY.color=mean([h.Color;[1 1 1]]);
end
if ~isempty(varargin)
p=inputParser;
p.KeepUnmatched = true;
p.parse(varargin{:});
fn=fieldnames(p.Unmatched);
for i=1:numel(fn)
COPY.(fn{i})=p.Unmatched.(fn{i});
end
end
prev_WindowButtonUpFcn = @do_nothing;
prev_WindowButtonMotionFcn = @do_nothing;
prev_Pointer = 'arrow';
origin=[nan nan];
newpos=[nan nan];
ax=ancestor(h,'axes');
fig=ancestor(h,'figure');
if isempty(updateFcn)
updateFcn=@do_nothing;
end
if isempty(doneFcn)
doneFcn=@do_nothing;
end
h.ButtonDownFcn = @startmove;
c=gobjects(1);
prev_axesMode={ax.XLimMode, ax.YLimMode};
function startmove(src,ev)
% create a copy of the item, and move that whenever the mouse moves
c=copyobj(h,ax);
set(c,COPY);
prev_Pointer=fig.Pointer;
fig.Pointer='cross';
origin = ax.CurrentPoint(1,[1 2]);
newpos = origin;
prev_WindowButtonUpFcn=fig.WindowButtonUpFcn;
prev_WindowButtonMotionFcn=fig.WindowButtonMotionFcn;
ax.XLimMode='manual';
ax.YLimMode='manual';
fig.WindowButtonUpFcn=@endmove;
fig.WindowButtonMotionFcn=@shiftItem;
end
function endmove(~,~)
fig.WindowButtonMotionFcn=prev_WindowButtonMotionFcn;
fig.WindowButtonUpFcn=prev_WindowButtonUpFcn;
if fig.CurrentCharacter==char(27) %escape
% don't update it!
fig.CurrentCharacter=' ';
else
doneFcn(c);
end
delete(c);
fig.Pointer=prev_Pointer;
ax.XLimMode=prev_axesMode{1};
ax.YLimMode=prev_axesMode{2};
end
function shiftItem(src,ev)
newpos=ax.CurrentPoint(1,[1 2]);
deltas=newpos - origin;
switch c.Type
case 'text'
c.Position=h.Position + [deltas(1), deltas(2), 0];
otherwise
c.XData=h.XData+deltas(1);
c.YData=h.YData+deltas(2);
end
updateFcn(c,deltas)
end
function do_nothing(~,~)
end
function demo()
% demonstrate how the movable_item works
f=figure('Name','moveable_item demo');
ax = gca;
h = plot(ax, [1;2;3],[2; 1; 3],'o-');
hold on;
h2 = plot(ax, [0;4;2],[1.5; 1; 2.5],'o-');
% this will be used to show how far we dragged the item
txt = text(mean(ax.XLim), mean(ax.YLim),'unmoved','Tag','description');
% this will show how far we dragged the item, and is called as we drag around
updateFcn=@(x,delta) set(txt,'String', sprintf('deltas: [ %g , %g]',delta));
% this will move our original plot to the new location, once we let up on the mouse button.
doneFcn=@(x)set(h,'XData',x.XData,'YData',x.YData);
% change the fist plot will also update the deltas message
moveable_item(h,updateFcn,doneFcn, 'Marker','+');
moveable_item(h2, [], @donedragging);
% make the text movable too. and change color. why not?
moveable_item(txt,[],@(x)set(txt,'Position',x.Position,'Color',rand(1,3)));
function donedragging(movedObj)
h2.XData=movedObj.XData;
h2.YData=movedObj.YData;
end
end
end