Right now I have a code that allows me to a move a DrawRect on my screen.
The issue is it locks the mouse to the top right of the box when I click it.
I want the box to be locked to the position of where it was clicked, if you understand when im saying.
I've tried numerous math equations to try to fix the issue, but none of them seem to work.
Any ideas?
Here is the code currently:
[code]
if (input.IsMouseDown(MOUSE_LEFT)) then
local mx = gui.MouseX();
local my = gui.MouseY();
if (mx > posx - 5 && mx < posx + sizew && my > posy - 5 && my < posy + sizeh && curselect == false) then
curselect = true;
end
if (curselect == true) then
posx = mx;
posy = my;
end
elseif (curselect == true) then
curselect = false;
end
[/code]
posx is the x position of the box
posy is the y position of the box
sizew is the width of the box
sizeh is the height of the box
Thanks for any answers!
Anyone?
When you "grab" the rectangle, in your case, when curselect is set to true, store offset values from the mouse position to the origin of the rectangle. Then, when you're setting the position of the rectangle to mx and my, add the offset from when you first grabbed it.
Take a look at this image, it should help you visualize it.
[t]https://i.gyazo.com/29f4acf1495ac2d4b3cd8bdcf95e9719.png[/t]
Obviously not to scale.
Assume the red rectangle is your mouse position, essentially where you clicked when you wanted to grab it.
Assume the outer rectangle is your view port, originating at (0, 0) ending at (1920, 1080)
The other rectangle, is the rectangle you want to grab.
This is untested, but just thinking about it, it should work.
P.S.
[QUOTE]
The issue is it locks the mouse to the top right of the box when I click it.
I want the box to be locked to the position of where it was clicked, if you understand when im saying.
[/QUOTE]
I'm not sure if I gave you the answer I wanted based on those two lines.
I think what you want to do is move a rectangle around, but with your current method, you're setting the rectangle's origin to the mouse position, when what you want to do is, move the rectangle but not snap it to the mouse position, rather, just have it move from where the mouse grabbed it.
[lua]local _posx, _posy
local function MoveDrawRect()
local x, y = gui.MousePos()
local SizeX = 150
local SizeY = 75
if ( input.IsMouseDown( MOUSE_LEFT ) ) then
local w, h = ScrW(), ScrH()
if ( curselect ) then
posx = math.Clamp( x - _posx, SizeX, w - SizeX )
posy = math.Clamp( y - _posy, SizeY, h - SizeY )
return
end
if ( not curselect and ( posx - SizeX ) < x and ( posx + SizeX ) > x and ( posy - SizeY ) < y and ( posy + SizeY ) > y ) then
_posx = x - posx
_posy = y - posy
curselect = true
end
else
curselect = false
end
end[/lua]
This should work, might not though. Put it in a think hook.
[QUOTE=McDunkable;50530214]-snip-[/QUOTE]
Thank you! This helped a lot.
The only issue im having now is that the box goes to the top left of the screen, instead of locking onto where the mouse clicked it. Any idea on how to fix that?
Anyone?
Sorry, you need to Log In to post a reply to this thread.