Canvas and Mouse

Archives Forums/BlitzPlus Programming/Canvas and Mouse

JPD(Posted 2004) [#1]
Hello, 1 big problem:

How can I check if my mouse ( MouseX(canvaswindow) ) is within the canvas?

I have a picture as canvas on my gui. Now I want to pic a color from this image. When I hit the mousebutton anywhere on the gui my function to pic color is starting up. It should only start up when the mouse is within the canvas and the mousebutten hit. I hope you understand what I mean :-)

JPD ...


Pepsi(Posted 2004) [#2]
Hi, In checking with events with waitevent, use $205 and $206 events to see if you are "in" or "out" any given canvas. Use EventSource to find which canvas you are entering or leaving. Here, you can simply set global flags to specify if the mouse is "in" or "out" any given canvas where as you can make sure when you hit the mousebutton that your 'pic color' function will only get called when supposed to.

below is sort of pseudo code on checking if the mouse has entered or exited any canvas:
format_code('
;$205 - Mouse enter
;Generated when the mouse pointer enters a canvas gadget.
Case $205
Select EventSource()
Case MainCanvas
DebugLog "enter MainCanvas"

Case ColorPickerCanvas
DebugLog "enter ColorPickerCanvas"
End Select

;$206 - Mouse leave
;Generated when the mouse pointer leaves a canvas gadget.
Case $206
Select EventSource()
Case MainCanvas
DebugLog "exit MainCanvas"

Case ColorPickerCanvas
DebugLog "exit ColorPickerCanvas"
End Select
')

Hope this helps...


JPD(Posted 2004) [#3]
Yes, it works, but i had to create a second canvas as background. Is there an other way? Sure not or?

JPD


Pepsi(Posted 2004) [#4]
Are you intially using one canvas for everything? If so, then you just check a rectangular region around your image and see if the mouse position is inside or outside of that region.

Or... do you have a seperate canvas for this image you want to pick colors from? If so, using the pseudo method I shown above should work for you.

You said you had to create a second canvas as a background? Did you use a second canvas for your image to pick color from before I replied the first time?


Pepsi(Posted 2004) [#5]
If you are only using one canvas and just want to check to see if your mouse is over your image that is on that canvas, you can do something like this:

format_code('
If (MouseX(canvaswindow)>ImageX) And (MouseY(canvaswindow)>ImageY) And (MouseX(canvaswindow)<(ImageX+ImageWidth)) And (MouseY(canvaswindow)<(ImageY+ImageHeight))
flagMouseOverColorPickImage=True
Else
flagMouseOverColorPickImage=False
EndIf
')


EOF(Posted 2004) [#6]
This will work. when the $201 'mouse clicked' event occurs just check that the event source is the canvas as well:
format_code('canvas=CreateCanvas(...)

Repeat
event=WaitEvent()
Select event
Case $201 ; mouse clicked
mx=MouseX(canvas) : gy=MouseY(canvas)
If EventSource()=canvas
If EventData()=1 ;lmb
;;; LEFT BUTTON CLICKED IN CANVAS
EndIf
If EventData()=2 ;rmb
;;; RIGHT BUTTON CLICKED IN CANVAS
EndIf
EndIf
End Select
Until quit=True')


JPD(Posted 2004) [#7]
OK, I'll testing both variations ... :-) ...