Drag and set Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mshall
Posts: 35
Joined: 13 Jul 2018, 16:42
Contact:

Drag and set

22 Oct 2018, 19:49

Hello,
I'm working on a game in AHK. I'd like some help with a drag and drop for some pictures. I have some pictures off to the side, and they need to be placed on a grid of squares.

An example would be how auto gui creator lets you drag a button from its title bar to set on a grid (your gui window)

So I have 5 squares in a line, and I'd like to drag a 30 x 30 picture and drop it on one of those 5 (30p x 30p) squares. It doesn't need to be perfect, but setting it with the mouse is a requirement.

Code: Select all

#Warn
#NoEnv
SetWorkingDir %A_ScriptDir%\Resources\

Gui, Add, Picture, x30 y390 w30 h30 , %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x60 y390 w30 h30 , %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x90 y390 w30 h30 , %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x120 y390 w30 h30 , %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x150 y390 w30 h30 , %A_WorkingDir%\gridbox.png

Gui, Add, Picture, x380 y402 w30 h30, %A_WorkingDir%\placeablepicture1.png
Gui, Add, Picture, x415 y402 w30 h30, %A_WorkingDir%\placeablepicture2.png

Gui, Color, 1c3968
Gui, Show, x590 y118 h718 w500, GUI Window
Return
There are some buttons, and some other pictures, but they don't interfere with anything.
There is currently no main loop.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Drag and set

23 Oct 2018, 01:14

Try this:

Code: Select all

#NoEnv
CoordMode, Mouse, Client
SetWorkingDir %A_ScriptDir%\Resources\

Gui, Add, Picture, x30 y390 w30 h30, %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x60 y390 w30 h30, %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x90 y390 w30 h30, %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x120 y390 w30 h30, %A_WorkingDir%\gridbox.png
Gui, Add, Picture, x150 y390 w30 h30, %A_WorkingDir%\gridbox.png

Gui, Add, Picture, x380 y402 w30 h30 gPickup, %A_WorkingDir%\placeablepicture1.png
Gui, Add, Picture, x415 y402 w30 h30 gPickup, %A_WorkingDir%\placeablepicture2.png

Gui, Color, 1c3968
Gui, Show, x50 y118 h718 w500, GUI Window
return

GuiClose:
ExitApp



;-------------------------------------------------------------------------------
Pickup(hCtrl) { ; to be placed on a grid of squares
;-------------------------------------------------------------------------------
    ; collect info about hCtrl
    GuiControlGet, Ctrl_, Pos, %hCtrl%
    GuiControlGet, Picked,, %hCtrl%

    ; follow mouse
    While GetKeyState("LButton") {
        MouseGetPos, m_X, m_Y
        GuiControl, Move, %hCtrl%, x%m_X% y%m_Y%
    }

    ; drop back picture
    GuiControl, Move, %hCtrl%, x%Ctrl_X% y%Ctrl_Y%

    ; if target hit, copy picture
    If (m_Y >= 390 and m_Y < 420)
        Loop, 5
            If (m_X >= A_Index * 30 and m_X < (A_Index + 1) * 30)
                GuiControl,, Static%A_Index%, %Picked%
}
I hope that helps.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Drag and set  Topic is solved

23 Oct 2018, 04:07

pixel corrected Pickup(), not perfect yet, but better than before.

Code: Select all



;-------------------------------------------------------------------------------
Pickup(hCtrl) { ; to be placed on a grid of squares
;-------------------------------------------------------------------------------
    GuiControlGet, Ctrl_, Pos, %hCtrl%
    GuiControlGet, Picked,, %hCtrl%
    MouseGetPos, x0, y0

    While GetKeyState("LButton") {
        MouseGetPos, MouseX, MouseY
        new_X := Ctrl_X + MouseX - x0
        new_Y := Ctrl_Y + MouseY - y0
        GuiControl, Move, %hCtrl%, x%new_X% y%new_Y%
    }
    GuiControl, Move, %hCtrl%, x%Ctrl_X% y%Ctrl_Y%

    ; if dropped on grid of squares, copy picture
    If (new_Y // 30 = 13)
        Loop, 5
            If (new_X // 30 = A_Index)
                GuiControl,, Static%A_Index%, %Picked%
}
Edit: using // 30 to reduce the number of comparisons
User avatar
mshall
Posts: 35
Joined: 13 Jul 2018, 16:42
Contact:

Re: Drag and set

23 Oct 2018, 09:07

Thank you, this appears to work for dragging, the second one looks and works better.
It doesn't appear to allow me to anchor it anywhere. When you drop it, it resets at the original position, and then occasionally will change a text box to the name of the picture that was moved.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Drag and set

23 Oct 2018, 09:41

There are some buttons, and some other pictures, but they don't interfere with anything.
occasionally will change a text box
I have hard-coded the Static1, Static2, ... Static5. You could adjust your controls, sounds like a text box is interfering.
Add the interfering stuff after the grid.

To drop the picture anywhere, delete or comment out the line that puts it back.

Code: Select all

 ; GuiControl, Move, %hCtrl%, x%Ctrl_X% y%Ctrl_Y%
User avatar
mshall
Posts: 35
Joined: 13 Jul 2018, 16:42
Contact:

Re: Drag and set

23 Oct 2018, 09:46

yep actually I just fixed it by doing that. Thank you for all of your help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww and 269 guests