Reference:Rio:Eventlistener

From Post-Apocalyptic RPG wiki

Jump to: navigation, search

Code reference.png This article covers outdated code reference information!

Code reference articles are stored at the wiki for legacy reasons. They are often outdated and duplicate Epydoc code documentation and department-specific tutorials. Code reference articles need to be reviewed by the programming department to see if they provide any useful information that is not available elsewhere at the wiki. After they have been reviewed and their unique information has been moved to more appropriate articles, the code reference articles should be deleted.


Contents

EventListenerBase Class

All classes that are going to be implementing event handling functions (e.g. mousePressed, keyPressed, keyReleased, mouseMoved) must inherit from the EventListenerBase Class.

To do this, import the EventListenerBase class:

from scripts.common import eventlistenerbase 

...

class ApplicationListener(eventlistenerbase.EventListenerBase)

The above code will allow all of the functions defined below to be overloaded with your own implemented functionality.


__init__()

When you define the __init__() function of a class that is inheriting from the EventListenerBase class, you must invoke the initialization function of the super class (EventListenerBase). The EventListenerBase constructor needs to know which events that this derived class is going to be listening to. In the case below the World class will be listening to mouse events and keyboard events.

class World(eventlistenerbase.EventListenerBase):
	def __init__(self, engine):
		super(World, self).__init__(engine, regMouse=True, regKeys=True)

In the next example, the ApplicationListener class is going to be receiving:

  • Keyboard events
  • Command events (Seems to be used only for the quit command, but I don't know how the onCommand function receives its events)
  • Console events (when you bring up the console using F10 and input a command)
  • Widget events (though Widget event flag variable is not used in anyway in the constructor, so again, still trying to find out what this flag is for).
class ApplicationListener(eventlistenerbase.EventListenerBase):
	def __init__(self, engine, world):
		super(ApplicationListener, self).__init__(engine,regKeys=True,regCmd=True, regMouse=False, regConsole=True, regWidget=True)

Overloadable Functions

The following functions are the functionality available to any class that inherits the EventListenerBase class. They are all fairly straight forward. All the event handlers below receive and event variable, evt, that has information regarding the event depending on the event that received that event.

For example, a mouse handler event (e.g. mousePressed, mouseReleased, etc) will receive information regarding the mouse, such as position, if a button was clicked, which button was clicked, the delta the mouse wheel was moved, or if the event is going to be handled by another widget.

Same idea with the key handler events (e.g. keyPressed and keyReleased), which will have information on which key was pressed or released.

Further research will need to be done on onCommand, onToolsClick, and onWidgetAction (Not sure what they do).

  • mousePressed(self, evt)
  • mouseReleased(self, evt)
  • mouseEntered(self, evt)
  • mouseExited(self, evt)
  • mouseClicked(self, evt)
  • mouseWheelMovedUp(self, evt)
  • mouseWheelMovedDown(self, evt)
  • mouseMoved(self, evt)
  • mouseDragged(self, evt)
  • keyPressed(self, evt)
  • keyReleased(self, evt)
  • onCommand(self, evt)
  • onToolsClick(self, evt)
  • onConsoleCommand(self, evt)
  • onWidgetAction(self, evt)

The Event Container Variable

KeyEvent class source: [[1]] MouseEvent class source: [[2]]

With the magic of SWIG, you can call all of the public functions in python.

However, just as an introduction, the most important functions for accessing the event and to delegate what to do based on the event will be listed below with a description:

Key Event

  • evt.getKey().getValue() - you can do a comparison against the value of the key like the code below:

def keyPressed(self, evt):
	keyval = evt.getKey().getValue()
	consumed = False
	if keyval == fife.Key.ESCAPE:
		self.quit = True
		evt.consume()
	elif keyval == fife.Key.F10:
		self.engine.getGuiManager().getConsole().toggleShowHide()
		evt.consume()

where fife.Key.ESCAPE and fife.Key.F10 are statically defined values.

  • evt.getKey.getAsString() - This function returns a string that holds the value of the key pressed. For example if you pressed the p key, the function will return a string that just has p in it. (I do not know if it is guaranteed if the returned string will be in upper or lower case)
def keyPressed(self, evt):
	keystr = evt.getKey().getAsString().lower()
	consumed = False
	if keystr == 'p':
		self.engine.getRenderBackend().captureScreen('screenshot.png')
		evt.consume()

Mouse Event

  • evt.getButton() - This function will return 1 of 5 values:
             EMPTY = 0,
             LEFT = 1,
             RIGHT = 2,
             MIDDLE = 4,
             UNKNOWN_BUTTON = 8

You can also use the defined FIFE mouse types provided for comparisons

		if (evt.getButton() == fife.MouseEvent.LEFT):
			foo1()
		elif (evt.getButton() == fife.MouseEvent.RIGHT):
			foo2()
  • evt.getX() or evt.getY() - Get real X or Y coordinates of within the game window. The coordinates (0,0) is the top left corner.
Personal tools