Canvas gadget mouseX, MouseY

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Canvas gadget mouseX, MouseY

Post by srod »

Hi,

the canvas gadget is now (as of PB 5.21 LTS) really really useful for custom control freaks like myself, - very pleased. :)

However, just one request please. The #PB_Canvas_MouseX and #PB_Canvas_MouseY attributes are very useful, but can only be used following an event specific to the canvas gadget. Any chance these can be altered so that they return valid results even when an event is not being processed? The reason I ask is that I use a lot of timers with canvas gadgets and they ideally need to be able to locate the cursor in client coords in a cross-platform manner when no events may have occurred. On Windows of course we can use MapWindowPoints_() etc. Unsure what the equivalent is on other platforms? I have tried posting dummy events in order to try and fool #PB_Canvas_MouseX, but no joy! :) I have workarounds since my timers are presently only running when a mouse button is down and so I can track the cursor through #PB_Event_MouseMove etc, but it is a bit messy.

Thanks.

(My apologies if this has already been requested.)
I may look like a mule, but I'm not a complete ass.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Canvas gadget mouseX, MouseY

Post by Danilo »

srod wrote:Unsure what the equivalent is on other platforms?
Maybe something like this (tested on Mac OS X):

Code: Select all

Procedure GadgetMouseX(gadget)
    Protected mx = DesktopMouseX() - GadgetX(gadget, #PB_Gadget_ScreenCoordinate)
    Protected my = DesktopMouseY() - GadgetY(gadget, #PB_Gadget_ScreenCoordinate)
    If mx < 0 Or my < 0 Or mx >= GadgetWidth(gadget) Or my >= GadgetHeight(gadget)
        ProcedureReturn -1
    EndIf
    ProcedureReturn mx
EndProcedure

Procedure GadgetMouseY(gadget)
    Protected mx = DesktopMouseX() - GadgetX(gadget, #PB_Gadget_ScreenCoordinate)
    Protected my = DesktopMouseY() - GadgetY(gadget, #PB_Gadget_ScreenCoordinate)
    If mx < 0 Or my < 0 Or mx >= GadgetWidth(gadget) Or my >= GadgetHeight(gadget)
        ProcedureReturn -1
    EndIf
    ProcedureReturn my
EndProcedure

If OpenWindow(0, 0, 0, 800, 600, "GadgetMouseX/Y", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 10, 10, 780, 580)
    
    Repeat
        Event = WindowEvent()
        
        Delay(50)
        Debug "X: "+GadgetMouseX(0)+" Y: "+GadgetMouseY(0)
    Until Event = #PB_Event_CloseWindow
EndIf
But test it with multi-monitor configurations, where monitors/desktops can sit at -x and -y.

EDIT: Tested on Windows with 2nd monitor on the left side (-2560,0). Works.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Canvas gadget mouseX, MouseY

Post by srod »

Ah, didn't notice the changes to GadgetX() etc. That is what happens when I am away from PB for so long. :) I presume if the gadget has a border then your calculations will be off by a pixel or two as it is the client coords that I ideally need. Still I can live with that. Cannot test right now as am on Android tablet.

Thanks Danilo.

**EDIT : yes a border makes a difference of 2 pixels on Windows. Unsure on other platforms. No problem mind as I don't use borders on these gadgets! Prefer to draw my own. :)
I may look like a mule, but I'm not a complete ass.
Post Reply