Dragable rectangle with text
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, im trying to make a rectangle that is labelled which can be dragged. However i only managed to do it with annotations, and want to change it to dragging with a rectangle. How do i do it?
This is my code:
function drag_drop
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','StudyTable','ButtonDownFcn',@dragObject);
a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','CoffeeTable','ButtonDownFcn',@dragObject);
a = annotation('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
rectangle('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
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
4 comentarios
Rik
el 7 de Dic. de 2020
It only seems that way because of the lack of proper indentation. All functions are nested functions within the main function, so they actually share some variables. I would prefer a more explicit method of sharing handles, but this should work.
J. Alex Lee
el 7 de Dic. de 2020
Editada: J. Alex Lee
el 7 de Dic. de 2020
well, i actually tried to run the code...the problem is not implementation of drag-and-drop...the problem is about endowing a rectangle class with some text. [edit] removed code that doesn't answer the OP, and made answer below.
Respuestas (1)
J. Alex Lee
el 7 de Dic. de 2020
Ugly, but minimal changes to original code:
function drag_drop
close all
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
% a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','StudyTable','ButtonDownFcn',@dragObject);
% a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','CoffeeTable','ButtonDownFcn',@dragObject);
% a = annotation('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
tmp = text(0.35,0.35,'Room','VerticalAlignment','bottom');
a = rectangle('position',[0.35, 0.35, 0.35, 0.35],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
tmp = text(0.1,0.1,'StudyTable','VerticalAlignment','bottom');
b = rectangle('position',[0.1, 0.1, 0.1, 0.1],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
tmp = text(0.1,0.1,'CoffeeTable','VerticalAlignment','bottom');
c = rectangle('position',[0.1, 0.1, 0.1, 0.1],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
set(gca,'XLim',[0,1],'YLim',[0,1])
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;
rectpos = get(dragging,'Position') + [posDiff(1:2) 0 0];
set(dragging,'Position',rectpos);
set(dragging.UserData,'Position',rectpos(1:3));
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
rectpos = get(dragging,'Position') + [posDiff(1:2) 0 0];
set(dragging,'Position',rectpos);
set(dragging.UserData,'Position',rectpos(1:3));
end
end
end
3 comentarios
J. Alex Lee
el 8 de Dic. de 2020
Editada: J. Alex Lee
el 8 de Dic. de 2020
hmm, you ran the above code and it failed? what version of matlab are you running? i ran on 2020b.
By the way you have to grab the edge of the rectangles. I just confirmed that if you specify a FaceColor, you can grab anywhere in the rectangle.
J. Alex Lee
el 8 de Dic. de 2020
I think to do anything more sophisticated you will want to organize your box+labels into some class and treat the thing as an "app", which will draw on OOP concepts.
WIth rectangles I bet you can only rotate 90 degress at a time, which would amount to just inverting the height/width, but then you need to make decisions like which corner of the box wil be your reference point for positioning.
If you want general angles, look into patch() or fill() maybe
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!