wxGLCanvas and mouse position and keyhit/down

BlitzMax Modules Forums/Brucey's Modules/wxGLCanvas and mouse position and keyhit/down

I'm rewriting the fry designer (see sig) in wx, using a wxGLCanvas for the graphics.

Since frygui uses the normal mousehit/down and mousex/y functions I decided to find a way to allow those to work instead of hacking it to use wxevents (I didn't want to modify brl.polledevents, although it would've been easy to do that way). The 'wx.mod\samples\graphics_tests\graphics_switch_test.bmx' sample had what I needed to get started, however keys aren't registering and the mouse position is off.

Any thoughts?

NOTE: I am using a wxGLCanvas instead of just wxglmax2d in a standard graphics window (like the graphic test sample), although the sample didn't seem to get keys either.

EDIT: Actually, MouseX() and MouseY() are returning the mouse position on the whole screen, instead of just the frame.

Have you tried my example key handling code mentioned HERE ?

It should allow you to use a standard Max2D-style update method where you check for keys.

Hmm... the mouse works fine in the graphics_switch_test on Mac... go figure :-p

(trying to remember where I actually used that key-handling code! - for an example)

Okay... a cut/paste job from some working (on Mac at least) code :


Type MyCanvas Extends wxGLCanvas

	Field timer:wxTimer


	Method OnInit()
		SetBackgroundStyle(wxBG_STYLE_CUSTOM)
	
		timer = New wxTimer.Create(Self)

		EnablePolledInput(Self)

		ConnectAny(wxEVT_TIMER, OnTick)
		ConnectAny(wxEVT_KEY_DOWN, OnKeyDown)
		ConnectAny(wxEVT_KEY_UP, OnKeyUp)

		timer.Start(17)
	End Method

	Function OnTick(event:wxEvent)
		wxWindow(event.parent).Refresh()
	End Function
	
	Function OnKeyDown(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYDOWN, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function

	Function OnKeyUp(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYUP, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function

	Method OnPaint(event:wxPaintEvent)
		Render()
	End Method

	Method Render()

		SetGraphics CanvasGraphics2D( Self )
		
		If KeyDown(key_up) Then
			' ...
		End If

		Cls

			' ...

		Flip
	
	End Method
End Type



The mouse is fine in the graphics_switch_test, but its using a standard graphics window instead of a wxGLCanvas.

I'll test the key code you linked to.

but its using a standard graphics window

heh... so it is, by jove....

Keys work now, I tried EnablePolledInput(Self) and it didn't register anything, changed it back to EnablePolledInput() (which is what I had the first time, from the sample) and the mousehits and keyhits were working again.. mouse position is still buggerd though.

How's about this :

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxglmax2D
Import wx.wxTimer
Import wx.wxPanel
Import wx.wxMouseEvent

SetGraphicsDriver GLMax2DDriver()

New MyApp.run()

Type MyApp Extends wxApp

	Field frame:MyFrame

	Method OnInit:Int()

		frame = MyFrame(New MyFrame.Create(,,"",,,500, 500))
		
		frame.show(True)
		frame.Center()
		SetTopWindow(frame)

		Return True
	End Method

End Type

Type MyFrame Extends wxFrame

	Field canvas:MyCanvas

	Method OnInit()
	
		canvas = MyCanvas(New MyCanvas.Create(Self, -1, GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER))
	
		ConnectAny(wxEVT_CLOSE, OnClose)
	End Method
	
	Function OnClose(event:wxEvent)
		MyFrame(event.parent).canvas.timer.Stop() ' we really need to stop the timer on Mac...
		event.Skip()
	End Function

End Type

Type MyCanvas Extends wxGLCanvas

	Field timer:wxTimer
	Field curFPS:Int, curTime:Int, prevTime:Int, checkTime:Int, fpscounter:Int
	Field initialized:Int

	Method OnInit()
		SetBackgroundStyle(wxBG_STYLE_CUSTOM)
	
		timer = New wxTimer.Create(Self)

		EnablePolledInput(Self)

		ConnectAny(wxEVT_TIMER, OnTick)
		ConnectAny(wxEVT_KEY_DOWN, OnKeyDown)
		ConnectAny(wxEVT_KEY_UP, OnKeyUp)
		ConnectAny(wxEVT_MOUSE_EVENTS, OnMouse)

		timer.Start(17)
	End Method

	Function OnTick(event:wxEvent)
		wxWindow(event.parent).Refresh()
	End Function
	
	Function OnKeyDown(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYDOWN, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function

	Function OnKeyUp(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		EmitEvent(CreateEvent( EVENT_KEYUP, event.parent, MapWxKeyCodeToBlitz(evt.GetKeyCode())))
		event.Skip()
	End Function
	
	Function OnMouse(event:wxEvent)
		Local evt:wxMouseEvent = wxMouseEvent(event)
		Select evt.GetEventType()
			Case wxEVT_MOTION
				Local x:Int, y:Int
				evt.GetPosition(x, y)
				EmitEvent(CreateEvent( EVENT_MOUSEMOVE, event.parent, 0, 0, x, y))
			Case wxEVT_LEFT_DOWN
				EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 1))
			Case wxEVT_LEFT_UP
				EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 1))
			Case wxEVT_RIGHT_DOWN
				EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 2))
			Case wxEVT_RIGHT_UP
				EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 2))
			Case wxEVT_MIDDLE_DOWN
				EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 3))
			Case wxEVT_MIDDLE_UP
				EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 3))
			Case wxEVT_MOUSEWHEEL
				
		End Select
		event.Skip()
	End Function

	Method OnPaint(event:wxPaintEvent)
		Render()
	End Method
	
	Method intialize()
		SetBlend ALPHABLEND
		SetClsColor(0, 0, 0)

		initialized = True
	End Method

	Method Render()

		SetGraphics CanvasGraphics2D( Self )
		
		If Not initialized Then
			intialize()
		End If

		Cls
	
		SetColor(255, 255, 255)
		
		DrawText MouseX() + ", " + MouseY(), 20, 60
		
		DrawText "Left    : " + MouseDown(1), 20, 80
		DrawText "Right   : " + MouseDown(2), 20, 100
		DrawText "Middle  : " + MouseDown(3), 20, 120
		
		For Local i:Int = 0 Until 26
			DrawText Chr(65 + i) + " : " + KeyDown(KEY_A + i), 200, 10 + i * 15
		Next

		' ++++++++++++++++
		' FPS Counter
		curTime = $7FFFFFFF & MilliSecs()
		
		If curTime > checkTime Or prevTime > curTime Then
			checkTime = curTime + 1000
			curFPS = fpscounter
			fpscounter = 0
		Else
			fpscounter = fpscounter + 1
		End If
		prevTime = curTime
	
		DrawText"FPS: "+curfps, 10,10
		' ++++++++++++++++
	
		Flip
	
	End Method
End Type


Ahha! Thanks Brucey.

I'll sneak back in later for graphviz in wxWidgets ;)

EDIT: This should also work for the mouse wheel (untested):
			Case wxEVT_MOUSEWHEEL
				EmitEvent(CreateEvent(EVENT_MOUSEWHEEL, event.Parent, evt.GetWheelRotation()))


Thanks for wxMax Brucey!
This is some really handy code for converting pre-wxMax code!