Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: PaulineV on Mon 20/05/2019 21:38:13

Title: Drag and Drop - Gui element on top [SOLVED]
Post by: PaulineV on Mon 20/05/2019 21:38:13
Hi AGS community !

I am creating a game where a woman is looking for an important information in her flat.
In one of the room, there is a pile of papers. When the charcter interacts with it, a GUI opens. Each one of the paper is a Gui Button. My intention is that the player can drag and drop the papers in order to find the good one with the correct information.

My code is mainly inspired from this post : https://www.adventuregamestudio.co.uk/forums/index.php?topic=50909.0

Here is my code, adapted :

Code (ags) Select

bool WasDragging;
int OldMyItemX;
int OldMyItemY;
int DragOffsetX;
int DragOffsetY;

function repeatedly_execute()
{
if (mouse.IsButtonDown(eMouseLeft)) {
        // have we pressed a button over our object?
        GUIControl *theGuiB = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
        if (!WasDragging && (theGuiB == b_Papier1 || theGuiB == b_Papier2)) {
            // just started draggin? remember where you took it
            OldMyItemX = theGuiB.X;
            OldMyItemY = theGuiB.Y;
            DragOffsetX = mouse.x - theGuiB.X;
            DragOffsetY = mouse.y - theGuiB.Y;
            WasDragging = true;
        }
        else if (WasDragging) {
            // mouse button still pressed? continue dragging
            theGuiB.X = mouse.x - DragOffsetX;
            theGuiB.Y = mouse.y - DragOffsetY;
        }
    } else if (WasDragging) {
        // was dragging but mouse button now released? drop it
        WasDragging = false;
    }
  }
}


I did it with only two papers for now, b_Paper1 and b_Paper2. It is working quite nicely exept from the situation where I go over a paper when I'm dragging the other one. It automatically switches to a dragging of the other paper.

If someone has an idea, it would be great !
Thanks in advance !
Title: Re: Drag and Drop - Gui element on top
Post by: Cassiebsg on Mon 20/05/2019 21:57:12
There's aDrag and Drop module here: https://www.adventuregamestudio.co.uk/forums/index.php?topic=53332.0

Maybe it'll help or save you work.  ;)
Title: Re: Drag and Drop - Gui element on top
Post by: PaulineV on Mon 20/05/2019 22:32:47
Yeah, I previously tried the Drag and Drop module for objects in a room but I kept having the following issue :

this.GhostGUI.BackgroundGraphic = slot;
Error running function 'repeatedly_execute_always':
Error: Null pointer referenced

So this is why I tried to do it another way  ;-D
Title: Re: Drag and Drop - Gui element on top
Post by: eri0o on Mon 20/05/2019 22:53:41
Did you have the Ghost GUI?
Title: Re: Drag and Drop - Gui element on top
Post by: Crimson Wizard on Mon 20/05/2019 23:52:51
Quote from: PaulineV on Mon 20/05/2019 22:32:47
Yeah, I previously tried the Drag and Drop module for objects in a room but I kept having the following issue :

this.GhostGUI.BackgroundGraphic = slot;
Error running function 'repeatedly_execute_always':
Error: Null pointer referenced

So this is why I tried to do it another way  ;-D


Well, yes, you have to assign actual GUI to GhostGUI property before using it. Quote from the post:
QuoteDepending on DragMove property value, this module either moves the actual object on screen at real-time (eDDCmnMoveSelf mode), or moves its "ghost" clone image on overlay (eDDCmnMoveGhostOverlay mode) or custom GUI (eDDCmnMoveGhostGUI mode). Overlay is created automatically, but you must assign GhostGUI if you want to use GUI mode.

The Demo game has an example in one of the rooms.
Title: Re: Drag and Drop - Gui element on top
Post by: Khris on Tue 21/05/2019 00:41:34
The problem with your code is that it doesn't store the GUIControl that is being dragged.
So while you drag, the inner else if will always test as true, and thus whatever theGuiB is will get dragged.

The solution is to turn WasDragging from a bool to a GUIControl* and store the dragged control in it. Then you need to change the code so it updates the coordinates of WasDragging, not theGuiB.
Title: Re: Drag and Drop - Gui element on top
Post by: PaulineV on Tue 21/05/2019 13:10:52
When I tried the Drag and Drop module, I used room objects so I thought I was in the situation "moves the actual object on screen at real-time" so I didn't need the GhostGUI.
Apparently I did not understand quite well this module, I am very new to AGS coding and this seems huge !  :-D

For my purpose, I think that a transparent room with one object for each paper is a better solution than the one I presented before (with GUI and GUIButtons). Indeed, because papers are on top of each other and in different rotation, it seems that GuiButtons are not appopriate because it allows to click on the part of the button where there is no image (the button still have a squared delimitation).

I will try the Demo Game of the Drag and Drop module and try to implement it.

Thank you to the three of you !

Title: Re: Drag and Drop - Gui element on top [SOLVED]
Post by: Crimson Wizard on Tue 21/05/2019 21:40:27
Quote from: PaulineV on Tue 21/05/2019 13:10:52
When I tried the Drag and Drop module, I used room objects so I thought I was in the situation "moves the actual object on screen at real-time" so I didn't need the GhostGUI.
Apparently I did not understand quite well this module, I am very new to AGS coding and this seems huge !  :-D

There are 3 modes:
- move object itself
- move its image on GUI
- move its image on Overlay

DragDropCommon.DragMove property sets current mode.

But it is still possible that the module has mistake, or documentation is not clear enough. So please tell whether you managed to use it in the end.
Title: Re: Drag and Drop - Gui element on top [SOLVED]
Post by: PaulineV on Wed 22/05/2019 13:16:10
Quote from: Crimson Wizard on Tue 21/05/2019 21:40:27
Quote from: PaulineV on Tue 21/05/2019 13:10:52
When I tried the Drag and Drop module, I used room objects so I thought I was in the situation "moves the actual object on screen at real-time" so I didn't need the GhostGUI.
Apparently I did not understand quite well this module, I am very new to AGS coding and this seems huge !  :-D

There are 3 modes:
- move object itself
- move its image on GUI
- move its image on Overlay

DragDropCommon.DragMove property sets current mode.

But it is still possible that the module has mistake, or documentation is not clear enough. So please tell whether you managed to use it in the end.

I cleaned everything which was about drag and drop attempts in my code and I took exemple on one of the rooms in the demo game : works perfectly !  ;-D