Page 1 of 1

Local x/y mouse pos inside a gadget

Posted: Thu Sep 20, 2007 11:45 pm
by Mistrel
Here is a really neat trick for finding the local mouse position within a gadget.

This is adapted from a method I found from a code snippet in the forum. I thought it would be useful enough to post here.

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
gadget=Frame3DGadget(#PB_Any, 10, 10, WindowWidth(0)-20, WindowHeight(0)-20, "", #PB_Frame3D_Flat)
	
Repeat
event=WaitWindowEvent()
If event
	GetCursorPos_(@cp.point)
	GetWindowRect_(GadgetID(gadget),gr.RECT)
	If PtInRect_(@gr, cp\x, cp\y)
		If GetAsyncKeyState_(#VK_LBUTTON)&32768
			Debug Str(cp\x-gr\left)+"\"+Str(cp\y-gr\top)
		EndIf
	EndIf
EndIf

Until event=#PB_Event_CloseWindow

Posted: Sat Sep 22, 2007 2:06 pm
by netmaestro
The MapWindowPoints_( api is very useful here as an augmentation to this code. It converts the POINT structure where the cursor position is stored from screen coordinates to gadget coordinates, making any further arithmetic unnecessary:

Code: Select all

   If PtInRect_(@gr, cp\x, cp\y) 
      MapWindowPoints_(#Null, GadgetID(gadget), cp, 1) ; cp now contains gadget coords
      If GetAsyncKeyState_(#VK_LBUTTON)&32768 
         Debug Str(cp\x)+"\"+Str(cp\y) 
      EndIf 
   EndIf 

Posted: Sat Sep 22, 2007 11:12 pm
by Mistrel
That's great. Thank you. :)

Posted: Tue Oct 02, 2007 11:14 am
by popstatic
Hey, very intersting code snippet, actuallly I was wondering if I could you use the

Code: Select all

PtInRect_(@gr, cp\x, cp\y)
thing in a fullscreenned game, (for detecting if the mouse is over a sprite in the game menu actually).

do you think it may work? because my sprites don't have the

Code: Select all

GadgetID(gadget)
needed by the GetWindowRect_ api, itself needed by PtInRect_

thanx in advance....

Posted: Tue Oct 02, 2007 3:11 pm
by netmaestro
In a fullscreened game you'll be using a sprite for the mouse pointer, so you can use SpriteCollision() to serve this purpose.

Posted: Wed Oct 03, 2007 11:41 am
by popstatic
great, thx for your answer, I must say that I'm a begginer @ fullscreen programming, and and didn't even thought it could be solved that way!

thank you!

Re: Local x/y mouse pos inside a gadget

Posted: Sat Nov 28, 2009 10:15 am
by Michael Vogel
I start to write a simple code for retrieving the column number of a listgadget under the mouse cursor and found this snippet above...
...which seems to have been adapted a little bit to work in PB4.3+:

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

gadget=Frame3DGadget(#PB_Any, 10, 10, WindowWidth(0)-20, WindowHeight(0)-20, "", #PB_Frame3D_Flat)

Repeat
	event=WaitWindowEvent()
	If event
		GetCursorPos_(@cp.point)
		GetWindowRect_(GadgetID(gadget),gr.RECT)
		If PtInRect_(@gr,PeekQ(@cp))
			MapWindowPoints_(#Null, GadgetID(gadget),cp,1) ; cp now contains gadget coords
			;If GetAsyncKeyState_(#VK_LBUTTON)&32768
			Debug Str(cp\x)+"\"+Str(cp\y)
		EndIf
		;EndIf
	EndIf

ForEver

Re: Local x/y mouse pos inside a gadget

Posted: Wed Dec 02, 2009 2:39 am
by STARGĂ…TE
why use API :

Code: Select all

OpenWindow(0,0,0,320,240,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

gadget=Frame3DGadget(#PB_Any, 10, 10, WindowWidth(0)-20, WindowHeight(0)-20, "", #PB_Frame3D_Flat)

Repeat
   event=WaitWindowEvent()
   If event
     x = WindowMouseX(0)-GadgetX(gadget) : y = WindowMouseY(0)-GadgetY(gadget) 
     If x >= 0 And y >= 0 And x < GadgetWidth(gadget) And y < GadgetHeight(gadget)
       Debug Str(x)+"\"+Str(y)
     EndIf
   EndIf

ForEver