Set Image/Event Position to Mouse Position
by Nicolai Dutka · in Torque Game Engine · 02/04/2008 (3:17 pm) · 10 replies
I am trying to make a "dockable" gui system in which you can click and hold on a gui item, drag it to where ever you choose, and release to leave it there.
I have the guiBitmapCtrl and guiMouseEventCtrl in place and named appropriately so they work together. I have the onMouseEnter and onMouseLeave functions working already too. I just need the click and release functions.
Basically, what it boils down to is: How to I do a "getMousePosition" via script? (So I can use that with a schedule to keep the image on the mouse until you let go)
I have the guiBitmapCtrl and guiMouseEventCtrl in place and named appropriately so they work together. I have the onMouseEnter and onMouseLeave functions working already too. I just need the click and release functions.
Basically, what it boils down to is: How to I do a "getMousePosition" via script? (So I can use that with a schedule to keep the image on the mouse until you let go)
#2
02/05/2008 (4:28 pm)
Now what I am wondering is, how would I "bring to front" the item being clicked so it doesn't get any weird layered effects with other gui items?
#3
02/08/2008 (8:07 pm)
Anyone? I need this "bring to front" feature and haven't been able to figure it out...
#4
Ok, now that I have it in the front and following the mouse, I have a more advanced issue.
I am setting the image's position equal to the mouse position. As you can imagine, this uses the image's "0,0" and sets the image equal to mouse position using that, so, the top left corner of the image snaps to the mouse. Well, what if I click on the middle of the image, or the bottom right? The image still snaps to the mouse from top-left....
How can I make the image follow the mouse WITHOUT that kind of snapping? Bear in mind, I am using Torque 3d, so the TGB functions of "mounting" does NOT work here....
(So if I click and hold in the middle, i can drag around the image by the spot of the image i clicked, NOT the top-left corner (0,0)....)
02/21/2008 (7:56 pm)
I got it, there is a "bringToFront" command.... Thing is, it's BACKWARDS!! I had to use "pushToBack" to get it to work instead.Ok, now that I have it in the front and following the mouse, I have a more advanced issue.
I am setting the image's position equal to the mouse position. As you can imagine, this uses the image's "0,0" and sets the image equal to mouse position using that, so, the top left corner of the image snaps to the mouse. Well, what if I click on the middle of the image, or the bottom right? The image still snaps to the mouse from top-left....
How can I make the image follow the mouse WITHOUT that kind of snapping? Bear in mind, I am using Torque 3d, so the TGB functions of "mounting" does NOT work here....
(So if I click and hold in the middle, i can drag around the image by the spot of the image i clicked, NOT the top-left corner (0,0)....)
#5
if the mouse is at 100, 100
and the GUI element is positioned at 90,90
that means the GUI is up and to the left 10 pixels each way.
so if you click at 200,200 and want the image to follow, you must rememeber the offset previously, which was 10,10
So you'd want to set the image to 190,190 instead of 200,200
I wrote a little method for dragging gui objects around on the screen using something like this
02/21/2008 (9:17 pm)
You need to factor in the offset from the cursor to the gui item.if the mouse is at 100, 100
and the GUI element is positioned at 90,90
that means the GUI is up and to the left 10 pixels each way.
so if you click at 200,200 and want the image to follow, you must rememeber the offset previously, which was 10,10
So you'd want to set the image to 190,190 instead of 200,200
I wrote a little method for dragging gui objects around on the screen using something like this
#6
02/21/2008 (9:42 pm)
Yes, I know, and I tried that too. The problem is, the picture then will NEVER MOVE. It thinks it's position should be the mouse position minus offset which.... hmmm that equals it's CURRENT position which means.... NEVER MOVES. Even when you move the mouse, it just sits there...
#7
one will be... probably onmousedown.
This is where you get the offset.
onmousedrag or onmousemove is where the magic happens
imageposition = mouse cursor - offset.
02/21/2008 (10:53 pm)
On, well you need to have two separate functionsone will be... probably onmousedown.
This is where you get the offset.
onmousedrag or onmousemove is where the magic happens
imageposition = mouse cursor - offset.
#8
Heres ascript file i used for moving gui elements. If i remember correctly, mousehandler was a GUI control that accepted mouse clicks, and the things i was moving were inside it.
02/21/2008 (10:59 pm)
$MMouseDown=0;
$MMouseDrag=0;
function mouseHandler::onRightMouseDown(%this) {
error("rightmouse down");
$RMouseDown=1;
echo("right mouse down"@$RMouseDown);
$MMouseDown=0;
}
function mouseHandler::onMouseUp(%this) {
// error("mouse up");
// $MMouseDown=0;
}
function mouseHandler::onMouseMove(%this) {
if ($MMouseDown){ //Button is down, let's try to drag
%parent=mousehandler.getgroup();
%parentx= getWord(%parent.getposition(),0);
%parenty= getWord(%parent.getposition(),1);
//echo($MMX);
//echo($MMY);
}
else{ echo($MMouseDown); }
}
function mouseHandler::onMouseDown(%this, %eventMod, %mousexy, %click){
//Assign Current Mouse XY
$MMouseXY=%mousexy;
}
function mouseHandler::onMouseDragged(%this, %eventMod, %mousexy, %click){
echo("mod "@%eventMod);
echo("click "@%click);
//This is the Object you're really wanting to move
%parent=%this.getGroup();
//Current Mouse Coords
%MouseX = getWord(%mousexy,0);
%MouseY = getWord(%mousexy,1);
//Current Coords of the Object to drag
%parentX = getWord(%parent.getposition(),0);
%parentY = getWord(%parent.getposition(),1);
//Previous Mouse XY
%PrevMouseX = getWord($MMouseXY,0);
%PrevMouseY = getWord($MMouseXY,1);
//Difference Between the Current and Previous Mouse XY
%DoffsetX=-(%PrevMouseX-%MouseX);
%DoffsetY=-(%PrevMouseY-%MouseY);
//Set the Object to it's new XY
%parent.position=%parentX+%DoffsetX SPC %parentY+%DoffsetY;
//Assign Current Mouse XY
$MMouseXY=%mousexy;
}Heres ascript file i used for moving gui elements. If i remember correctly, mousehandler was a GUI control that accepted mouse clicks, and the things i was moving were inside it.
#9
02/21/2008 (11:08 pm)
And all of those are valid? "onMouseMove", "onRightMouseDown" i've never heard of these before....
#10
on a secondary note
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13886 this might be what you need
02/21/2008 (11:18 pm)
They are valid on certain types of gui controls.on a secondary note
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13886 this might be what you need
Torque 3D Owner Nicolai Dutka
Canvas.getCursorPos();