Code archives/Miscellaneous/Simple Event Handling

This code has been declared by its author to be Public Domain code.

Download source code

Simple Event Handling by deps2005
Found a simple event handling tutorial and wanted to try it out in BlitzMax.

The original one can be found here: http://www.gamedev.net/reference/articles/article2141.asp
Another one here: http://gpwiki.org/index.php/Programming_Techniques:Events

Usage example:
Type Tevent
	Field t:Int
	Field arg1:Int, arg2:Int
EndType


Type EventHandler

	' Overload this
	Method EventHandler( e:Tevent ) 
	EndMethod


	Field _nexthandler:EventHandler

	Method init()
		EventDispatcher.RegisterHandler(Self)
	EndMethod
	

	Method GetNextHandler:EventHandler()
		Return _nexthandler
	EndMethod
	

	Method SetNextHandler( n:EventHandler )
		_nexthandler = n
	EndMethod
	
	
	Method SendEvent( event_type:Int, arg1:Int = 0, arg2:Int = 0 )
		EventDispatcher.SendEvent( event_type, arg1, arg2 )
	EndMethod

EndType




Type EventDispatcher

	Global _devicelist:EventHandler

	Function RegisterHandler( device:EventHandler )
		device.SetNextHandler( _devicelist )
		_devicelist = device
	EndFunction
	
	Function SendEvent( event_type:Int, arg1:Int = 0, arg2:Int = 0 )
		Local e:Tevent = New Tevent
		e.t = event_type
		e.arg1 = arg1
		e.arg2 = arg2
		Local curDevice:EventHandler = _deviceList
		While curDevice
			curDevice.EventHandler(e)
			curDevice = CurDevice.GetNextHandler()
		Wend
	EndFunction


EndType

Comments

Macguffin2009
Thanks! Adapting this to the needs of my game - much appreciated.


Code Archives Forum