Drag and drop axes (Rush hour game)

3 views (last 30 days)
Jovi Tchean
Jovi Tchean on 28 Jun 2013
Right now I'm busy re-creating the Rush hour game with MATLAB Guide (keep in mind that I'm completely new to MATLAB). What I'm doing now is that I've created the gamefield using axes (a 6x6 field, so 36 axes). On top of that I've created the actual cars (also with axes) and displayed them by using imshow.
Example code:
carPlayer = imread('D:\Aschaffenburg\Engineering Project\Images\Car_player01.png');
imshow(carPlayer, 'Parent', handles.car_player01);
Screenshot:
Now I want to get drag and drop working on the cars. So I've started working on tracking the mouse.
Code:
% function drag_and_drop should be attached to WindowButtonMotionFcn handles = guihandles(RushHourGUI);
pos = get(RushHourGUI, 'currentpoint'); % get mouse location on figure
x = pos(1); y = pos(2); % assign locations to x and y
set(handles.lbl_x, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.lbl_y, 'string', ['y loc:' num2str(y)]); % update text for y loc
% get position information of the uicontrol
bounds = get(handles.car_player01,'position');
lx = bounds(1); ly = bounds(2);
lw = bounds(3); lh = bounds(4);
The mouse tracking is working and it prints it on the screen, but the "hotspot" (to see if the mouse is in the boundaries of the axes) for the car player (which you have to move) is not working. However it is already giving me an error:
Reference to non-existent field 'car_player01'.
Even though the axes is right there, it still doesn't properly see it. The m file drag_and_drop requests it from the main file RushourGUI.m. So I added the m.file to the main by doing this:
function RushHourGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
set(hObject, 'WindowButtonMotionFcn', @drag_and_drop);
guidata(hObject, handles);
Can this affect it in any way??
After that I want to enable the uicontrol:
*% test to see if mouse is within the uicontrol.
if x >= lx && x <= (lx + lw) && y >= ly && y <= (ly + lh)
%set enable to off the whole uicontrol is hotspot
%set(handles.car_player01, 'enable', 'off');guide
setfigptr('hand', handles.fig_mouse);
set(handles.car_player01, 'ButtonDownFcn', @grab);
else
% re-enable the uicontrol
%set(handles.car_player01, 'enable', 'on');
setfigptr('arrow', handles.fig_mouse);
end*
As you can see the 3 sets are commented out because the "enable" is not a valid property. Since I'm pretty new to MATLAB I really have no clue on how to fix this.
Any help would be appreciated!
Thanks in advance. If you have any questions, please let me know.

Answers (2)

Tom
Tom on 28 Jun 2013
To be honest I don't quite follow all of this, but what I can see is that the handles structure isn't passed to other callbacks by default - you have to add it as an extra argument.
set(hObject, 'WindowButtonMotionFcn', {@drag_and_drop,handles});
  1 Comment
Jovi Tchean
Jovi Tchean on 28 Jun 2013
Maybe I should be more specific.
Do you any way to implement drag and drop on axes? I got it working with text boxes, but I don't know if you can set images in a text box so I I'm trying it with axes now.

Sign in to comment.


Tom
Tom on 28 Jun 2013
function Drag_Axes_Test
close all
clc
F = figure('Position',[200 100 900 500]);
AX = axes('Units','Pixels','Position',[300 100 300 300]);
text(0.5, 0.5, 'Drag Me','FontSize',20,'HorizontalAlignment','Center')
set(F,'WindowButtonMotionFcn',{@Drag_Callback,AX})
%set up flags for if button is held down
set(F,'WindowButtonDownFcn',@(h,e) set(h,'UserData',true))
set(F,'WindowButtonUpFcn',@(h,e) set(h,'UserData',false))
set(F,'UserData',false) %Initialise data
function Drag_Callback(F,~,AX)
if ~get(F,'UserData')
return
end
curPos = get(F,'CurrentPoint');
AXPos = get(AX,'Position');
%check if cursor is in over axes:
AXLimX=[AXPos(1),AXPos(1),AXPos(1)+AXPos(3),AXPos(1)+AXPos(3)];
AXLimY=[AXPos(2),AXPos(2)+AXPos(4),AXPos(2)+AXPos(4),AXPos(2)];
if inpolygon (curPos(1),curPos(2),AXLimX,AXLimY)
%move axes (centre over cursor)
set(AX,'Position',[curPos(1) - AXPos(3)/2, curPos(2)- AXPos(4)/2, AXPos(3), AXPos(4)])
end

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!