Help!! Where are the mouseEvents?

BlitzMax Forums/BlitzMax Beginners Area/Help!! Where are the mouseEvents?

If you take the createCanvas.bmx example and you add a line to show the mouseX() and mouseY() the mouseposition remains 0,0!!!!

Check this:
' createcanvas.bmx

Strict 

Global GAME_WIDTH=320
Global GAME_HEIGHT=240

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS)

' create a canvas for our game

Local canvas:TGadget=CreateCanvas(0,0,320,240,window)

' create an update timer

CreateTimer 60

While WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			RedrawGadget canvas

		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			SetOrigin 160,120
			SetLineWidth 5
			Cls
			DrawText String(MouseX()) +","+String(MouseY()), 10,10
			Local t=MilliSecs()
			DrawLine 0,0,120*Cos(t),120*Sin(t)
			DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
			Flip

		Case EVENT_MOUSEMOVE
			Print "MOVE!"

		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


How can I check the mouse location in a graphics canvas?

' createcanvas.bmx

Strict 

Global GAME_WIDTH=320
Global GAME_HEIGHT=240

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS)

' create a canvas for our game

Local canvas:TGadget=CreateCanvas(0,0,320,240,window)

' create an update timer

CreateTimer 60

Local MX:Int = 0 'MouseX()
Local MY:Int = 0 'MouseY()
Local MB:Int = 0 'MouseButton()

While WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			RedrawGadget canvas

		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			SetOrigin 160,120
			SetLineWidth 5
			Cls
			DrawText  MX +","+ MY + " --> " + MB, 10,10
			Local t=MilliSecs()
			DrawLine 0,0,120*Cos(t),120*Sin(t)
			DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
			Flip
		
		'MouseEvents
		Case EVENT_MOUSEMOVE
			MX = EventX()
			MY = EventY()
			
		Case EVENT_MOUSEDOWN
			MB = EventData()
		
		Case EVENT_MOUSEUP
			MB = 0

		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


I dont' remember, but MouseX() MouseY() work with Graphics enabled, to retrieve info about the pointer under MaxGUI you need to check EventX(), EventY() [I'm at work at the moment...so I can't check].

MouseX/Y are Graphics mode functions

EventX() and EventY() are the ones you want... but they'll only be available from you EVENT_MOUSEMOVE event, not EVENT_GADGETPAINT.

:-)

Edit : or klepto can rewrite it for you :-p

Ok. I found the answer myself. I checked the event objects reference.

I was trying to make a nice combination of a graphical interface and TGadgets but that seems hard to realize.

Perhaps anyone of you can give me some advice.
I'm making a prototype of a not-windows-like user interface: interactive graphic objects, a user can manipulate with the mouse (like click and drag). Some objects have a popup menu (also not windows like). But in this popup a user can select a numeric value and changes that. For this value I was planned to use a TextField gadget with a Slider gadget (the stepper version). Everything works fine so far until I discovered that mouseEvents in a CanvasGraphics area are handled differently.

I see two approaches:
1. create my own "textfield-with-stepper"-gadget object or
2. create a new mouseEvent controller using EventID()

I have no idea what to choose. Any help is very much appreciated!

Here is a second Version using MouseEvents on the one hand and
the traditional MouseX() etc commands. To get the normal Key and Mouse commands to work within a Canvas you have to enable the 'polled Input'. This is done by using the undocumented command 'EnablePolleDInput()'. But keep in mind that when using this you don't get the whole CPU Advantage a only eventdriven app does have.

' createcanvas.bmx

Strict 

Global GAME_WIDTH=320
Global GAME_HEIGHT=240

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS)

' create a canvas for our game

Local canvas:TGadget=CreateCanvas(0,0,320,240,window)

EnablePolledInput()

' create an update timer

CreateTimer 60

Local MX:Int = 0 'MouseX()
Local MY:Int = 0 'MouseY()
Local MB:Int = 0 'MouseButton()

While WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			RedrawGadget canvas

		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			SetOrigin 160,120
			SetLineWidth 5
			Cls
			DrawText  MX +","+ MY + " --> " + MB, 10,10
			DrawText  MouseX()+","+ MouseY() + " --> " + MouseDown(1), 10,30

			Local t=MilliSecs()
			DrawLine 0,0,120*Cos(t),120*Sin(t)
			DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
			Flip
		
		'MouseEvents
		Case EVENT_MOUSEMOVE
			MX = EventX()
			MY = EventY()
			
		Case EVENT_MOUSEDOWN
			MB = EventData()
		
		Case EVENT_MOUSEUP
			MB = 0

		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


EnabledPolledInput()...hmmm
That does the trick!
Don't you think I can safely use it? After all, it is the easiest solution.

I have used it many times without any problems. If you want to use it, use it. Some people will say it is not save but for me everything was fine using this. So keep using this.

Klept02: many thanks!
Based on your example, I wrote a mouseType to save the status of the mouse.
' createcanvas.bmx

Strict 

Global GAME_WIDTH=320
Global GAME_HEIGHT=240

' create a centered window with client size GAME_WIDTH,GAME_HEIGHT

Local wx=(ClientWidth(Desktop())-GAME_WIDTH)/2
Local wy=(ClientHeight(Desktop())-GAME_HEIGHT)/2

Local window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS)

' create a canvas for our game

Local canvas:TGadget=CreateCanvas(0,0,320,240,window)
Local mouse:TMouse = New TMouse
' create an update timer

CreateTimer 60

While WaitEvent()
	Select EventID()
		Case EVENT_TIMERTICK
			RedrawGadget canvas

		Case EVENT_GADGETPAINT
			SetGraphics CanvasGraphics(canvas)
			SetOrigin 160,120
			SetLineWidth 5
			Cls
			DrawText String(mouse.getX()) +","+String(mouse.getY())+ "-->"+String(mouse.leftMouseDown()), 10,10
			Local t=MilliSecs()
			DrawLine 0,0,120*Cos(t),120*Sin(t)
			DrawLine 0,0,80*Cos(t/60),80*Sin(t/60)
			Flip

		Case EVENT_MOUSEMOVE
			mouse.setMousePos(EventX(), EventY())
		Case EVENT_MOUSEDOWN
			mouse.doMouseDown(EventData())
		Case EVENT_MOUSEUP
			mouse.doMouseUp(EventData())
		
		Case EVENT_WINDOWCLOSE
			FreeGadget canvas
			End

		Case EVENT_APPTERMINATE
			End
	End Select
Wend


Type TMouse
	Field piX% = 0
	Field piY% = 0
	Field pbLeftMouseDown:Byte = False
	Field pbRightMouseDown:Byte = False
	
	Method setMousePos(x%, y%)
		piX = x
		piY = y
	End Method
	
	Method getX%()
		Return piX
	End Method
	
	Method getY%()
		Return piY
	End Method
	
	Method doMouseDown(btn%)
		Select btn
			Case 1 pbLeftMouseDown = True
			Case 2 pbRightMouseDown = True
		End Select
	End Method
	
	Method doMouseUp(btn%)
		Select btn
			Case 1 pbLeftMouseDown = False
			Case 2 pbRightMouseDown = False
		End Select
	End Method
	
	Method leftMouseDown()
		Return pbLeftMouseDown
	End Method
	
	Method rightMouseDown()
		Return pbRightMouseDown
	End Method
	
End Type