Is mouse over canvas???

BlitzPlus Forums/BlitzPlus Programming/Is mouse over canvas???

Lazze(Posted 2004) [#1]
How do i check to see if the mouse is over a specific canvas?
I'm coding a function to create a tooltip which should only pop up when the mouse is over certain areas of the canvas, but it also tends to popup outside the canvas because my areachecking is somewhat buggy. A solution would be to check if the mouse is inside the canvas....

Lazze


CS_TBL(Posted 2004) [#2]
If EventID()=$201 ; mouse move over a canvas ?
  If EventSource()=MyCanvas  ; which canvas ?
    Notify "yay!"
    
    ; top-left ?
    If EventX()>0 and EventX()<24 and EventY()>0 and EventY()<23
      Notify "YAY!!"
    EndIf


  EndIf
EndIf



Lazze(Posted 2004) [#3]
Maybe I should've been more specific :o) My popup pops up when the mouse hovers over the specific area (no clicking) and nothing has happened for x millisecs. Basically what i need is a function like IsMouseOverCanvas(canvas) True/False kind of thing ;o) - or a solution that gives me the same result. I suppose it could be done whith event $205/$206 and setting a flag when the mouse leaves/enters the canvas but I have this strange feeling that I'm likely to miss this particulary event once in a while, leaving the flag set the wrong way - if you get my point...

Lazze


RedRidingHood(Posted 2004) [#4]
Maybe you could use the MouseX(MyCanvas)and MouseY(MyCanvas) commands?

or maybe use the $205 to detect when your user enters the canvas and then use MouseXSpeed(MyCanvas)or $203 to see if the mouse is moving and then if it's not, do your popup.


Lazze(Posted 2004) [#5]
The problem with MouseX(MyCanvas)/MouseY(MyCanvas) is that they just stop counting when you exit the canvas (ex. you exceed the right edge of the canvas it just remembers the last x-value registered etc.). In other words, they allways shows the mouse as on the canvas. Would've been nice if they counted relative to the canvas, but apparently they don't - correct me if I'm wrong.
Using $205/$206 isn't the solution either. Maybe my code is crappy :o) but it does'nt catch all the leave/enter events. So basically I'm trying to figure out another solution. Has'nt come up with one yet...

Lazze


RedRidingHood(Posted 2004) [#6]
Hmmmm..
That is kind of a tricky one. I'm fresh out of ideas. About the only thing I can think of is just to try rearranging your code until the $205 etc commands work properly.

Blitz Plus seems to be kind of fussy about how your code is arranged. For example, I was trying to use the Mousehit command and couldn't get it to work right in a function but when I turned the function into a subroutine it started working just fine. Don't ask me why, I have no clue. It could have been anything from how Blitz works with Functions or how I had my condition testing set up.


EOF(Posted 2004) [#7]
Lazze,

Try this. It needs a couple of userlibs adding before you run it...

The function in the example can be used to test if the mouse is over *any* Blitz gadget.

; MouseOverGadget() - example

; USERLIBS ---------------------------------------
; .lib "user32.dll"
; api_GetCursorPos% (lpPoint*) : "GetCursorPos"
; api_ClientToScreen% (hWnd%,lpPoint*) : "ClientToScreen"
; ------------------------------------------------


; required by both api_ calls
Type POINTTYPE
 Field x%,y%
End Type
Global point.POINTTYPE=New POINTTYPE

; GUI
win=CreateWindow("Mouse/Canvas Test",160,120,340,300,Desktop(),1)
can=CreateCanvas(20,30,250,100,win)
bt=CreateButton("button",60,150,70,22,win)
sl=CreateSlider(60,190,240,24,win)
SetBuffer CanvasBuffer(can)

; LOOP
Repeat
 Select WaitEvent(1)
  Case $803 : Exit ; window [X]
  Case $103 : If EventData()=27 Exit ; key [ESC]
  Default
  Cls
  Text 10,10,"Mouse over canvas = "+MouseOverGadget(can)
  Text 10,30,"Mouse over button = "+MouseOverGadget(bt)
  Text 10,50,"Mouse over slider = "+MouseOverGadget(sl)
  FlipCanvas can
 End Select
Forever

End

; return TRUE if mouse is over a gadget
Function MouseOverGadget(gad)
 point\x=0 : point\y=0
 api_ClientToScreen(QueryObject(gad,1),point)
 Local gadX%=point\x , gadY%=point\y
 api_GetCursorPos(point)
 Local mX%=point\x , mY%=point\y
 If mX>=gadX And mX<gadX+GadgetWidth(gad)
  If mY>=gadY And mY<gadY+GadgetHeight(gad)
   Return True
  EndIf
 EndIf
End Function



Lazze(Posted 2004) [#8]
Thank you, thank you, thank you....:o) This is exactly what I was looking for. Works just fine :o)

Edit:
Well - works allmost fine. Unfortunately my canvas is larger than the active window (for scrolling purposes), which this function does'nt take into account. But I can probably work around it by placing a panel or canvas in the right size underneath it and use that to test on.

Lazze


Cold Harbour(Posted 2004) [#9]
JimB, nice chunk of code that. Works really well.

Thanks for sharing.


rdodson41(Posted 2004) [#10]
Try RectsOverlap() to see if the the mouse is over the canvas.

If RectsOverlap(MouseX(canvas),MouseY(canvas),1,1,canvasx,canvasy,canvasw,canvash) Then
   Notify "YAY!!!"
End if