Borrar filtros
Borrar filtros

Determine the current position of an object in the context of drag and drop

4 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Voss
Voss el 17 de En. de 2022
Editada: Voss el 17 de En. de 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

Más respuestas (1)

Stefan
Stefan el 17 de En. de 2022
Thx for your help.
By releasing the mousebutton (button up) x shuold be the same als the mouse and y should be 0.1 or 0.3
I was running the script at my PC but unfortunately its a little bit buggy:
Error using matlab.graphics.shape.TextBox/vertcat
Cannot convert double value 7.00012 to a handle
Error in ismember>ismemberClassTypes (line 429)
ab = [a(:);b(:)];
Error in ismember>ismemberR2012a (line 204)
lia = ismemberClassTypes(a,b);
Error in ismember (line 95)
lia = ismemberR2012a(A,B);
Error in minimalExample2/dropObject (line 37)
if ismember(dragging,a)
Error while evaluating Figure WindowButtonUpFcn.
And the dragged object sticks to the mouse coursor all the time.
  3 comentarios
Voss
Voss el 17 de En. de 2022
Does that work? If so, please mark my answer as Accepted. If not, please post the problem and I'll see if I can fix it. Thanks!
Stefan
Stefan el 18 de En. de 2022
Thank you for your help.
Sorry for the late answer, sleep is important from time to time ^^
I was a little surprised to see where the first answer had gone. I have now tested the remaining answer again and am very satisfied.
Thank you very much.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by