MouseOverGadget

Share your advanced PureBasic knowledge/code with the community.
USCode
Addict
Addict
Posts: 924
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

MouseOverGadget

Post by USCode »

Code updated for 5.20+

Since I was up most of the night last night with a sick child :-(, I figured I might as well make some use of my time and write a quick cross-platform PB4 procedure I thought might be useful called MouseOverGadget.
However, after I'd already finished it I found several folks have beat me to it already:
http://www.purebasic.fr/english/viewtop ... dget+event
BUT, I thought I'd share the code anyways, maybe it will be of use to someone else:

Code: Select all

Procedure.b MouseOverGadget(window.l, gadget.l, offsetX.l=0, offsetY.l=0)
  
  gadgetX.l = GadgetX(gadget) + offsetX
  gadgetY.l = GadgetY(gadget) + offsetY
  gadgetW.l = GadgetWidth(gadget)
  gadgetH.l = GadgetHeight(gadget)
  mouseX.l = WindowMouseX(window)
  mouseY.l = WindowMouseY(window)
  
  If (mouseX >= gadgetX) And (mouseY >= gadgetY) And (mouseX <= (gadgetX + gadgetW - 1)) And (mouseY <= (gadgetY + gadgetH - 1))
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure

If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine)
  ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
  Repeat 
    If MouseOverGadget(0,3)
      Debug "Over gadget at "+Str(WindowMouseX(window))+", "+Str(WindowMouseY(window))
    EndIf
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
And another example where the gadget is on a tab panel container, tab Panel 2:

Code: Select all

Procedure.b MouseOverGadget(window.l, gadget.l, offsetX.l=0, offsetY.l=0)
  
  gadgetX.l = GadgetX(gadget) + offsetX
  gadgetY.l = GadgetY(gadget) + offsetY
  gadgetW.l = GadgetWidth(gadget)
  gadgetH.l = GadgetHeight(gadget)
  mouseX.l = WindowMouseX(window)
  mouseY.l = WindowMouseY(window)
  
  If (mouseX >= gadgetX) And (mouseY >= gadgetY) And (mouseX <= (gadgetX + gadgetW - 1)) And (mouseY <= (gadgetY + gadgetH - 1))
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure

If OpenWindow(0, 0, 0, 322, 220, "PanelGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  PanelGadget     (0, 8, 8, 306, 203)
  AddGadgetItem (0, -1, "Panel 1")
  PanelGadget (1, 5, 5, 290, 166)
  AddGadgetItem(1, -1, "Sub-Panel 1")
  AddGadgetItem(1, -1, "Sub-Panel 2")
  AddGadgetItem(1, -1, "Sub-Panel 3")
  CloseGadgetList()
  AddGadgetItem (0, -1,"Panel 2")
  ButtonGadget(2, 10, 15, 80, 24,"Button 1")
  ButtonGadget(3, 95, 15, 80, 24,"Button 2")
  CloseGadgetList()
  Repeat     
    If MouseOverGadget(0,3,GadgetX(0), GadgetY(0)+GetGadgetAttribute(0,#PB_Panel_TabHeight))
      Debug "Over gadget at "+Str(WindowMouseX(window))+", "+Str(WindowMouseY(window))
    EndIf
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
The parameters are as follows:
MouseOverGadget(
Gadget's Window#,
Gadget#,
Offset on X axis if gadget is in a container like a panel gadget (optional),
Offset on Y axis (optional),
)
Returns TRUE if the mouse is moving over the gadget.

:idea: As an observation, it seems that it would be very useful if PB4 could be enhanced to generate some events like mouse in, mouse out, mouse over, mouse move, etc. for us? Cross-platform of course please! Certainly all the supported platforms generate these events that Fred could tap in to?
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Nice trick :) I second the request. The ability to have events that fire when mouse is over, in, or out :)
Post Reply