Page 1 of 1

Canvas gadget mouseX, MouseY

Posted: Sat Jan 11, 2014 4:21 pm
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.)

Re: Canvas gadget mouseX, MouseY

Posted: Sat Jan 11, 2014 11:52 pm
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.

Re: Canvas gadget mouseX, MouseY

Posted: Sun Jan 12, 2014 9:28 am
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. :)