CEGUI and WxMax

BlitzMax Modules Forums/Brucey's Modules/CEGUI and WxMax

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

Here's a wee update :

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxglmax2D
Import wx.wxGLCanvas
Import wx.wxTimer
Import BaH.CEGUI
Import wx.wxMouseEvent

SetGraphicsDriver wxGLGraphicsDriver(),GRAPHICS_BACKBUFFER|GRAPHICS_DEPTHBUFFER

' Start the app !!!!!
New MyApp.run()

cegui_cleanup()

Type MyApp Extends wxApp

	Field frame:MyFrame

	Method OnInit:Int()

		frame = MyFrame(New MyFrame.Create(,,"wxMax + CEGUI", -1, -1, 800, 600))

		frame.Center()
		frame.show()
		
		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 ax#, ay#, tim#

	Field helperWindow:TCEWindow
	Field helperLabel:TCEWindow
	Field helperText:TCEEditbox
	Field helperSubmit:TCEPushButton
	
	Field initialized:Int

	Method OnInit()
	
		SetCursor(New wxCursor.StockCreate(wxCURSOR_BLANK))

		timer = New wxTimer.Create(Self)

		EnablePolledInput(Self)

		ConnectAny(wxEVT_TIMER, OnTimer)
'		ConnectAny(wxEVT_TIMER, OnTick)
		ConnectAny(wxEVT_KEY_DOWN, OnKeyDown)
		ConnectAny(wxEVT_KEY_UP, OnKeyUp)
		ConnectAny(wxEVT_CHAR, OnKeyChar)
		ConnectAny(wxEVT_MOUSE_EVENTS, OnMouse)

		timer.Start(16)
	End Method

	Function OnTimer(event:wxEvent)
		MyCanvas(event.parent).Refresh()
	End Function
	
	Method OnPaint(event:wxPaintEvent)
		Render()
	End Method

	Method Render()

		SetGraphics CanvasGraphics2D( Self )
		
		Cls

		DrawText MouseX() + ", " + MouseY(), 20, 60

		If Not initialized Then
		
			Flip
			Flip
		
			initialized = True

			Init_CEGUI(Null, True)
			
			TCESchemeManager.loadScheme("datafiles/schemes/WindowsLook.scheme")
			
			If Not TCEFontManager.isFontPresent( "DejaVuSans-10" ) Then
				TCEFontManager.createFont( "datafiles/fonts/DejaVuSans-10.font" )
			End If
	
			TCESystem.setDefaultMouseCursor("WindowsLook", "MouseArrow")
	
			Local root:TCEWindow = TCEWindowManager.loadWindowLayout("datafiles/layouts/helper.layout")
			TCESystem.setGUISheet(root)
			
			helperWindow = TCEWindowManager.getWindow("HelperWindow")
			helperWindow.Hide()
			
			helperLabel = TCEWindowManager.getWindow("Helper/label")
			helperText = TCEEditbox(TCEWindowManager.getWindow("Helper/Editbox"))
			helperSubmit = TCEPushButton(TCEWindowManager.getWindow("Helper/Submit"))
			
			helperSubmit.subscribeEvent(TCEPushButton.EventClicked, submitted) ' click
			helperText.subscribeEvent(TCEEditbox.EventTextAccepted, submitted) ' enter

			RequestValue("Hello", "Some stuff")
		End If

		TCESystem.renderGUI()
		
		Flip
	
	End Method

	Function submitted:Int(args:TCEEventArgs)
	End Function

	Method RequestValue(title:String, label:String)
		helperWindow.setText(title)
		helperLabel.setText(label)

		helperText.setText("")

		helperWindow.show()
		
		helperText.activate()
	End Method

	Function OnKeyDown(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		Local key:Int = MapWxKeyCodeToBlitz(evt.GetKeyCode())
		EmitEvent(CreateEvent( EVENT_KEYDOWN, event.parent, key)) ' event for Max2D

		TCESystem.injectKeyDown(key)

		event.Skip()
	End Function

	Function OnKeyUp(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		Local key:Int = MapWxKeyCodeToBlitz(evt.GetKeyCode())
		EmitEvent(CreateEvent( EVENT_KEYUP, event.parent, key)) ' event for Max2D
		
		TCESystem.injectKeyUp(key)
		
		event.Skip()
	End Function

	Function OnKeyChar(event:wxEvent)
		Local evt:wxKeyEvent = wxKeyEvent(event)
		'Local key:Int = MapWxKeyCodeToBlitz(evt.GetKeyCode())
		'EmitEvent(CreateEvent( EVENT_KEYDOWN, event.parent, key)) ' event for Max2D

		TCESystem.injectChar(Asc(evt.GetUnicodeKey()))

		event.Skip()
	End Function
	
	Function OnMouse(event:wxEvent)
		If MyCanvas(event.parent).initialized Then
			Local evt:wxMouseEvent = wxMouseEvent(event)
		
			Local elapsed:Float = (MilliSecs() - cegui_startTime)/1000.0
	
			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)) ' event for Max2D

					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMousePosition(x, y)
					
				Case wxEVT_LEFT_DOWN
					EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 1)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonDown(1)
				Case wxEVT_LEFT_UP
					EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 1)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonUp(1)
				
				Case wxEVT_LEFT_DCLICK
				
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonDown(1)
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonUp(1)
				
				Case wxEVT_RIGHT_DOWN
					EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 2)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonDown(2)
				Case wxEVT_RIGHT_UP
					EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 2)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonUp(2)
				Case wxEVT_MIDDLE_DOWN
					EmitEvent(CreateEvent( EVENT_MOUSEDOWN, event.parent, 3)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonDown(3)
				Case wxEVT_MIDDLE_UP
					EmitEvent(CreateEvent( EVENT_MOUSEUP, event.parent, 3)) ' event for Max2D
	
					TCESystem.injectTimePulse(elapsed)
					TCESystem.injectMouseButtonUp(3)
				Case wxEVT_MOUSEWHEEL
	
				Case wxEVT_ENTER_WINDOW
					wxWindow(Event.parent).SetCursor(New wxCursor.StockCreate(wxCURSOR_BLANK))
	
				Case wxEVT_LEAVE_WINDOW
					wxWindow(Event.parent).SetCursor(New wxCursor.StockCreate(wxCURSOR_ARROW))
	
			End Select

		End If
		event.Skip()
	End Function

End Type


It seems on Windows, OnMouse can be called before the window has finished initialising - don't ask... I don't know!
So in effect, we were trying to inject mouse movement into non initialised CEGUI...
The actual change is this, in the OnMouse function :
	If MyCanvas(event.parent).initialized Then


And yes, the mouse isn't hiding on Windows... I believe I know why, but am not too concerned about it for now. :-p