TGB Drop And Drag Mouse Down not recognized witinin GUI
by Pieter · in Torque Game Builder · 11/23/2011 (8:54 am) · 12 replies
I am trying to implement DROP & Drag inside my Inventory system, but for starters the mouse need to register that it has been pressed.
When I attached a command to a control it gets executed, but it does not register when the mouse is held down for dragging?
I even used the code as in this tread:
http://www.garagegames.com/community/forums/viewthread/79327
Aslo with the same result, Mouse down is not recognized?
What is there that I am missing?
I have TGB pro 1.7.5.
Any help would be appreciated.
Pieter
When I attached a command to a control it gets executed, but it does not register when the mouse is held down for dragging?
I even used the code as in this tread:
http://www.garagegames.com/community/forums/viewthread/79327
Aslo with the same result, Mouse down is not recognized?
What is there that I am missing?
I have TGB pro 1.7.5.
Any help would be appreciated.
Pieter
#2
I use GuiControl's
I can give some samples if needed
As said in my post, the command on the control gets executed.
I have done furter testing and it does recognize the on enter and on leave but none of the others like on mousedown or on on mouseup.
11/28/2011 (10:15 am)
Hi William, Thks for the reply.I use GuiControl's
I can give some samples if needed
As said in my post, the command on the control gets executed.
I have done furter testing and it does recognize the on enter and on leave but none of the others like on mousedown or on on mouseup.
#3
=== BEGIN QUOTE ===
Imagine my pleasant surprise when looking through the new codebase to find that there is a very nice script based dragdrop and very flexible drag and drop solution now built right in. Maybe I missed it on some release notes or something, but I thought I would share a little bit about it. It looks like it's been in TGB for some time. It's used in the gui editor to make new gui controls but you can use it in your game to do TONS of handy gameplay functions.
The name of the GUI control is:
GuiDragAndDropControl
(You can find a nice working implementation in tools/guiEditor/gui/guiEditor.ed.cs.)
Basically, if your control implements these SCRIPT callbacks:
It can respond to the drag event as a drop target as it happens. To make a drop source, you only need to make sure the control extends onMouseDragged with the appropriate script callback. Here's an example of a simple use which turns any control into a drag source, displaying a copy of the bitmap that was on the button as it is dragged
To register this on my playgui, I just implemented the previously mentioned callbacks. I wanted drag and drop to give the mousedrag events to playgui so it could keep track of the current mouse over object as well, so I adjusted void GuiDragAndDropControl::onMouseDragged(const GuiEvent& event) to pump the onMouseDragged event to the current playgui. Now I can drag and drop onto an ingame inworld object as well as any GUI control.
Because the payload can be anything, you can set whatever dynamic fields you need in script to tell it that it's an inventory item, or a skill, or a healing potion.. whatever your needs are.
=== END QUOTE ===
11/28/2011 (4:04 pm)
Are you placing your draggable items as guiBitmapCtrls? Those don't have draggable behavior. There is a class called GuiDragAndDropControl that can help. Here's a quote from another thread (www.garagegames.com/community/forums/viewthread/79327). I'm sure this example will need to be tweaked, but it's an excellent start.=== BEGIN QUOTE ===
Imagine my pleasant surprise when looking through the new codebase to find that there is a very nice script based dragdrop and very flexible drag and drop solution now built right in. Maybe I missed it on some release notes or something, but I thought I would share a little bit about it. It looks like it's been in TGB for some time. It's used in the gui editor to make new gui controls but you can use it in your game to do TONS of handy gameplay functions.
The name of the GUI control is:
GuiDragAndDropControl
(You can find a nice working implementation in tools/guiEditor/gui/guiEditor.ed.cs.)
Basically, if your control implements these SCRIPT callbacks:
function MyControl::onControlDragged(%this, %payload, %position)
{
}
function MyControl::onControlDragEnter(%this, %payload, %position)
{
}
function MyControl::onControlDragExit(%this, %payload, %position)
{
}
function MyControl::onControlDropped(%this, %payload, %position)
{
}It can respond to the drag event as a drop target as it happens. To make a drop source, you only need to make sure the control extends onMouseDragged with the appropriate script callback. Here's an example of a simple use which turns any control into a drag source, displaying a copy of the bitmap that was on the button as it is dragged
function guiBitmapButtonCtrl::onMouseDragged(%this)
{
%position = %this.getGlobalPosition();
%cursorpos = Canvas.getCursorPos();
%payload = new GuiBitmapCtrl() {
profile = "GuiDefaultProfile";
horizSizing = "width";
vertSizing = "height";
position = "0 0";
extent = "32 32";
minExtent = "8 8";
visible = "1";
helpTag = "0";
bitmap = %this.getBitmap();
wrap = "0";
};
%xOffset = getWord(%payload.extent, 0) /2;
%yOffset = getWord(%payload.extent, 1) / 2;
// position where the drag will start, to prevent visible jumping. Down and to the right also
%xPos = getWord(%cursorpos, 0) + 32; //%xOffset;
%yPos = getWord(%cursorpos, 1) + 32; //%yOffset;
//Now create the drag and drop control which will be deleted when mouse is up, delivering its payload
%dragCtrl = new GuiDragAndDropControl() {
canSaveDynamicFields = "0";
Profile = "GuiDefaultProfile";
HorizSizing = "right";
VertSizing = "bottom";
Position = %xPos SPC %yPos;
extent = %payload.extent;
MinExtent = "32 32";
canSave = "1";
Visible = "1";
hovertime = "1000";
deleteOnMouseUp = true;
};
%dragCtrl.add(%payload);
Canvas.getContent().add(%dragCtrl);
//We want the new dragdrop to be offset from the cursor
%dragCtrl.startDragging(%xOffset -32, %yOffset - 32);
}To register this on my playgui, I just implemented the previously mentioned callbacks. I wanted drag and drop to give the mousedrag events to playgui so it could keep track of the current mouse over object as well, so I adjusted void GuiDragAndDropControl::onMouseDragged(const GuiEvent& event) to pump the onMouseDragged event to the current playgui. Now I can drag and drop onto an ingame inworld object as well as any GUI control.
Because the payload can be anything, you can set whatever dynamic fields you need in script to tell it that it's an inventory item, or a skill, or a healing potion.. whatever your needs are.
=== END QUOTE ===
#4
I am aware of the thread you referred to. It is the same one I mentioned at the start of my thread.
As said, I used the sample code to create a test in the level editor. I used the code as supplied by Vince Gee, further down in the thread.
In using the exact code I have the problem that I don't get the mouse down of mouse drag to show anything? What must happend to kick that off?
Ok let me just explain what I have done may be thats where the problem is.
I have a created the following
dropanddrag.Gui which contains the gui as per example
dropanddragGui.cs with the functions as per example
My game.cs contains this
function startGame(%level)
{
exec("./dropanddragGui.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
// sceneWindow2D.setUseObjectMouseEvents( true );
// even tried this but makes no difference !!
sceneWindow2D.loadLevel(%level);
Canvas.pushDialog(DragDropGui);
}
ps. the Gui get exec in main.cs
When I run the level it comes up but no action with the mouse happens apart from on enter & on leave
11/28/2011 (11:12 pm)
I place them as GuibitmapButtonCtrl. I am aware of the thread you referred to. It is the same one I mentioned at the start of my thread.
As said, I used the sample code to create a test in the level editor. I used the code as supplied by Vince Gee, further down in the thread.
In using the exact code I have the problem that I don't get the mouse down of mouse drag to show anything? What must happend to kick that off?
Ok let me just explain what I have done may be thats where the problem is.
I have a created the following
dropanddrag.Gui which contains the gui as per example
dropanddragGui.cs with the functions as per example
My game.cs contains this
function startGame(%level)
{
exec("./dropanddragGui.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
// sceneWindow2D.setUseObjectMouseEvents( true );
// even tried this but makes no difference !!
sceneWindow2D.loadLevel(%level);
Canvas.pushDialog(DragDropGui);
}
ps. the Gui get exec in main.cs
When I run the level it comes up but no action with the mouse happens apart from on enter & on leave
#5
I have an idea, but it's a total shot in the dark. You can wrap the "GuiBitmapButtonCtrl" inside a "GuiMouseEventCtrl". That object sends every mouse event it gets to TorqueScript.
That's the only obvious way I see to get all mouse events to a GUI object.
11/29/2011 (8:46 am)
Now that I'm looking at the source code, I don't see how it could work. The GuiBitmapButtonCtrl doesn't sent out a "mouse down" or "mouse dragged" event.I have an idea, but it's a total shot in the dark. You can wrap the "GuiBitmapButtonCtrl" inside a "GuiMouseEventCtrl". That object sends every mouse event it gets to TorqueScript.
new GuiControl() {
new GuiMouseEventCtrl(DragHelper) {
new GuiBitmapButtonCtrl(Button) {
};
};
};
function DragHelper::onMouseDown( %this, %mod, %loc, %clicks )
{
echo( "In DragHelper::onMouseDown!" );
}
function DragHelper::onMouseDragged( %this, %mod, %loc, %clicks )
{
echo( "In DragHelper::onMouseDragged!" );
}
function DragHelper::onMouseUp( %this, %mod, %loc, %clicks )
{
echo( "In DragHelper::onMouseUp!" );
}That's the only obvious way I see to get all mouse events to a GUI object.
#6
Or is there another way?
I have a work arround whereby I attach the bitmap to the cursor and then droping it where you want it again.
If you have any other thoughts please let me know?
11/30/2011 (2:36 am)
It is still a problem because the GuiBitmapButtonCtrl is still on top of the GuiMouseEventCtrl blocking mouse events, you want the GuiMouseEventCtrl to be firstresponder but you can't do that anymore.Or is there another way?
I have a work arround whereby I attach the bitmap to the cursor and then droping it where you want it again.
If you have any other thoughts please let me know?
#7
11/30/2011 (8:26 am)
If you know C++, the easiest solution would be to build a new GUI element. You can start with the GuiBitmapCtrl, call it something like GuiDraggableBitmap, and just make sure to add the functions for the mouse events. You can look at the GuiMouseEventCtrl to see how easy it is to send events to TorqueScript.
#8
11/30/2011 (11:41 am)
If you were to set the GuiBitmapButtonCtrl profile to GuiModelessDialogProfile , that would set the modal = false. That is supposed to let mouse events through I believe.
#10
I am using my solution which I mentioned, by attaching the Bitmap to the cursor and then I drop it again in the place I want to move it to. It work fine in that you don't have to hold down the mouse button.
It is click and Drop and if there is something in that spot you want to Drop it, it just pick-up in the same action, or not if you don't want it to.
I will when I have more time see if I can change the source code and do the changes as mentioned just to see if I can get it to work.
Will post it here when I have done it.
01/12/2012 (11:23 pm)
Vald I,I am using my solution which I mentioned, by attaching the Bitmap to the cursor and then I drop it again in the place I want to move it to. It work fine in that you don't have to hold down the mouse button.
It is click and Drop and if there is something in that spot you want to Drop it, it just pick-up in the same action, or not if you don't want it to.
I will when I have more time see if I can change the source code and do the changes as mentioned just to see if I can get it to work.
Will post it here when I have done it.
#11
And it works like a charm. Hope this helps!
08/31/2013 (9:32 am)
Hi all, not sure if anybody has figured it out yet but just wanted to share my success so far in getting this to work. I gave William's idea a go and I found it works quite well if you actually do the opposite. That is to say, if you wrap the "GuiMouseEventCtrl" inside of the "GuiBitmapButtonCtrl". I then assigned the "GuiMouseEventCtrl" my DragDrop class with a onMousDragged function. Then I get any needed info from my bitmapctrl (such as the bitmap) with the mouseeventctrl's parentgroup (aka the bitmapctrl). Example:function DragDrop::onMouseDragged(%this)
{
%position = %this.getGlobalPosition();
%cursorpos = Canvas.getCursorPos();
%payload = new GuiBitmapCtrl() {
profile = "GuiDefaultProfile";
horizSizing = "width";
vertSizing = "height";
position = "0 0";
extent = "32 32";
minExtent = "8 8";
visible = "1";
helpTag = "0";
bitmap = %this.parentGroup.bitmap;
wrap = "0";
};And it works like a charm. Hope this helps!
#12
08/31/2013 (7:13 pm)
And I have a mostly complete inventory system example in T2D MIT in https://github.com/RichardRanft/Torque2D/tree/inventory
Associate William Lee Sims
Machine Code Games