CEGUI and WxMax

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

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Drop this in the raknet examples folder to test (without having to change the CEGUI scheme/datafile stuff).
You'll also need to update CEGUI. I've added a second param to CEGUI_init() which disables the built-in event handling, since it wasn't working here with wxMax (Looks like a bug with AddHook tbh).

Seems to work on Mac. Not tested elsewhere yet.

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
		
			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)
		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
		event.Skip()
	End Function

End Type


Yes, the event stuff is pretty hefty. It's always there (in Max2D etc), but that's usually hidden away from you - what you don't know about won't hurt you, eh?
Here I'm passing events to both Max2D and CEGUI. (except of course we're using my wx version of GLMax2D - because we need to use our own GL implementation - Had Mark used Factories, like he has with image loaders and the likes, we could have done this all a bit more tidier... oh well).

Obviously this is just a hacked together mess, but it shows the two running together. :-)