Determine the current position of an object in the context of drag and drop
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have two different drag n drop fields. a and b.
When moving an a(cc) field and then releasing it, I want the X position to be the same as the mouse and the Y position to be set to 0.1. The b(cc) field should behave like a(cc) but snap to 0.3 in the y-direction.
Everything I tried so far didn't work.
Thx for your Help.
function minimalExample
dragging = [];
orPos = [];
global a;
global b;
global numberOfFields;
numberOfFields = 2;
f = figure('WindowButtonUpFcn',@dropObject,...
'units','normalized',...
'position', [0.1 0.2 0.35 0.5],...
'WindowButtonMotionFcn',@moveObject);
for cc = 1:numberOfFields
a(cc) = annotation('textbox',...
'position',[(0.1*cc) 0.1 0.08 0.1],...
'String', ['a # ' num2str(cc)],...
'Tag', ['a' num2str(cc)],...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]),...
'ButtonDownFcn',@dragObject);
b(cc) = annotation('textbox',...
'position',[(0.1*cc) 0.3 0.08 0.1],...
'String', ['b # ' num2str(cc)],...
'Tag', ['a' num2str(cc)],...
'BackgroundColor',hsv2rgb([0.0 0.5 0.66]),...
'ButtonDownFcn',@dragObject);
end
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end
0 Kommentare
Akzeptierte Antwort
Voss
am 17 Jan. 2022
Bearbeitet: Voss
am 17 Jan. 2022
Basically, in the motion function and button up function, you have to check whether dragging is an element of a or of b and set the y-coordinate accordingly.
(It wasn't clear to me whether you want to constrain the y-coordinate the whole time the annotation is dragged or just when it is released, so I put the option to do it either way. See the variable do_move_freely.)
function minimalExample
dragging = [];
orPos = [];
do_move_freely = true; % move the object with the mouse, then snap when mouse button is released
% do_move_freely = false; % constrain the y-coordinate to 0.1 or 0.3 the entire time the object is being moved
global a;
global b;
global numberOfFields;
numberOfFields = 2;
f = figure('WindowButtonUpFcn',@dropObject,...
'units','normalized',...
'position', [0.1 0.2 0.35 0.5],...
'WindowButtonMotionFcn',@moveObject);
for cc = 1:numberOfFields
a(cc) = annotation('textbox',...
'position',[(0.1*cc) 0.1 0.08 0.1],...
'String', ['a # ' num2str(cc)],...
'Tag', ['a' num2str(cc)],...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]),...
'ButtonDownFcn',@dragObject);
b(cc) = annotation('textbox',...
'position',[(0.1*cc) 0.3 0.08 0.1],...
'String', ['b # ' num2str(cc)],...
'Tag', ['a' num2str(cc)],...
'BackgroundColor',hsv2rgb([0.0 0.5 0.66]),...
'ButtonDownFcn',@dragObject);
end
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
setPos = get(dragging,'Position') + [posDiff(1:2) 0 0];
if ismember(double(dragging),double(a))
setPos(2) = 0.1;
else
setPos(2) = 0.3;
end
set(dragging,'Position',setPos);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
setPos = get(dragging,'Position') + [posDiff(1:2) 0 0];
if ~do_move_freely
if ismember(double(dragging),double(a))
setPos(2) = 0.1;
else
setPos(2) = 0.3;
end
end
set(dragging,'Position',setPos);
end
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!