Local x/y mouse pos inside a gadget

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Local x/y mouse pos inside a gadget

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

That's great. Thank you. :)
popstatic
New User
New User
Posts: 4
Joined: Tue Mar 28, 2006 11:24 pm
Location: Paris, France.

Post 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....
You're pretty when I'm drunk, and I'm pretty fuckin' drunk.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
popstatic
New User
New User
Posts: 4
Joined: Tue Mar 28, 2006 11:24 pm
Location: Paris, France.

Post 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!
You're pretty when I'm drunk, and I'm pretty fuckin' drunk.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Local x/y mouse pos inside a gadget

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Local x/y mouse pos inside a gadget

Post 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 
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply