changeset 324:e249fa887259

* Split EventListener into several internal subclasses. This fixes issue with multiple inheritance where each BaseClass.__init__() call would overwrite the previous one. This meant that you could only add EventListener to one type of event managers. * MapView now adds maps and layers to EventListener * LayerEditor should now fetch only NameClash exceptions when creating or editing layers
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 21 Aug 2009 14:39:54 +0000
parents b64ce990abb1
children 54de5aebf732
files clients/editor/plugins/LayerTool.py clients/editor/scripts/editor.py clients/editor/scripts/events/events.py clients/editor/scripts/mapview.py
diffstat 4 files changed, 93 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/clients/editor/plugins/LayerTool.py	Tue Aug 18 18:03:54 2009 +0000
+++ b/clients/editor/plugins/LayerTool.py	Fri Aug 21 14:39:54 2009 +0000
@@ -444,7 +444,7 @@
 			try:
 				layer = self.map.createLayer(str(layerId), cellgrid)
 				
-			except:
+			except fife.NameClash:
 				print 'The layer ' + str(layerId) + ' already exists!'
 				return
 		else:
--- a/clients/editor/scripts/editor.py	Tue Aug 18 18:03:54 2009 +0000
+++ b/clients/editor/scripts/editor.py	Fri Aug 21 14:39:54 2009 +0000
@@ -391,10 +391,10 @@
 		type = event.getType()
 		
 		if type == fife.KeyEvent.PRESSED:
-			self._eventlistener.keyPressed(event)
+			keyPressed.send(sender=self._maparea, event=event)
 		
 		elif type == fife.KeyEvent.RELEASED:
-			self._eventlistener.keyReleased(event)
+			keyReleased.send(sender=self._maparea, event=event)
 			
 		
 
--- a/clients/editor/scripts/events/events.py	Tue Aug 18 18:03:54 2009 +0000
+++ b/clients/editor/scripts/events/events.py	Fri Aug 21 14:39:54 2009 +0000
@@ -46,7 +46,7 @@
 		self.modifiers = {"alt":False,"ctrl":False,"shift":False,"meta":False}
 		self.signal = None
 
-class EventListener(IKeyListener, ICommandListener, IMouseListener, LayerChangeListener, MapChangeListener, ConsoleExecuter):
+class EventListener:
 	# NOTE: As FIFEdit currently covers the entire screen with widgets,
 	#		FIFE doesn't receive any mouse or key events. Therefore we have to add
 	#		mouse and key event tracking for the central widget
@@ -56,27 +56,28 @@
 		
 		eventmanager = self.engine.getEventManager()
 		self.keysequences = []
-
-		IKeyListener.__init__(self)
-		eventmanager.addKeyListener(self)
 		
-		ICommandListener.__init__(self)
-		eventmanager.addCommandListener(self)
-		
-		IMouseListener.__init__(self)
-		eventmanager.addMouseListener(self)
-		
-		ConsoleExecuter.__init__(self)
-		self.engine.getGuiManager().getConsole().setConsoleExecuter(self)
-		
-		MapChangeListener.__init__(self)
-		LayerChangeListener.__init__(self)
+		self.keylistener			= _IKeyListener(engine)
+		self.mouselistener			= _IMouseListener(engine)
+		self.mapchangelistener		= _MapChangeListener(engine)
+		self.layerchangelistener	= _LayerChangeListener(engine)
+		self.commandlistener		= _ICommandListener(engine)
+		self.consoleexecuter		= _ConsoleExecuter(engine)
 		
 		self.controlPressed = False
 		self.altPressed		= False
 		self.shiftPressed	= False
 		self.metaPressed	= False
 		
+		eventmanager.addKeyListener(self.keylistener)
+		eventmanager.addCommandListener(self.commandlistener)
+		eventmanager.addMouseListener(self.mouselistener)
+		self.engine.getGuiManager().getConsole().setConsoleExecuter(self.consoleexecuter)
+		
+		keyPressed.connect(self.keyPressed)
+		keyReleased.connect(self.keyReleased)
+		
+
 	def getKeySequenceSignal(self, key, modifiers=[]):
 		# Parse modifiers
 		mods = {"alt":False,"ctrl":False,"shift":False,"meta":False}
@@ -100,17 +101,16 @@
 		self.keysequences.append(keysequence)
 		
 		return keysequence.signal
-	
-	#--- Listener methods ---#
+		
 	# IKeyListener
-	def keyPressed(self, evt):
-		keyval = evt.getKey().getValue()
-		keystr = evt.getKey().getAsString().lower()
+	def keyPressed(self, event):
+		keyval = event.getKey().getValue()
+		keystr = event.getKey().getAsString().lower()
 		
-		self.controlPressed = evt.isControlPressed()
-		self.altPressed		= evt.isAltPressed()
-		self.shiftPressed	= evt.isShiftPressed()
-		self.metaPressed	= evt.isMetaPressed()
+		self.controlPressed = event.isControlPressed()
+		self.altPressed		= event.isAltPressed()
+		self.shiftPressed	= event.isShiftPressed()
+		self.metaPressed	= event.isMetaPressed()
 		
 		if keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
 			self.controlPressed = True
@@ -135,19 +135,15 @@
 			if k.modifiers["shift"]	!= self.shiftPressed: continue
 			if k.modifiers["meta"] != self.metaPressed: continue
 			if keyval != k.key: continue
-			k.signal.send(sender=self, event=evt)
-			
-		keyPressed.send(sender=self.engine, event=evt)
-		
-		evt.consume()
+			k.signal.send(sender=self, event=event)
 
-	def keyReleased(self, evt):
-		keyval = evt.getKey().getValue()
+	def keyReleased(self, event):
+		keyval = event.getKey().getValue()
 		
-		self.controlPressed = evt.isControlPressed()
-		self.altPressed		= evt.isAltPressed()
-		self.shiftPressed	= evt.isShiftPressed()
-		self.metaPressed	= evt.isMetaPressed()
+		self.controlPressed = event.isControlPressed()
+		self.altPressed		= event.isAltPressed()
+		self.shiftPressed	= event.isShiftPressed()
+		self.metaPressed	= event.isMetaPressed()
 		
 		if keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
 			self.controlPressed = False
@@ -157,9 +153,26 @@
 			self.altPressed = False
 		elif keyval in (fife.Key.RIGHT_META, fife.Key.LEFT_META):
 			self.metaPressed = False
-			
+
+	
+#--- Internal sublistener classes ---#
+class _IKeyListener(IKeyListener):
+	def __init__(self, engine):
+		IKeyListener.__init__(self)
+		self.engine = engine
+		
+	# IKeyListener
+	def keyPressed(self, evt):
+		keyPressed.send(sender=self.engine, event=evt)
+
+	def keyReleased(self, evt):
 		keyReleased.send(sender=self.engine, event=evt)
 
+class _ICommandListener(ICommandListener):
+	def __init__(self, engine):
+		ICommandListener.__init__(self)
+		self.engine = engine
+		
 	# ICommandListener
 	def onCommand(self, command):
 		if command.getCommandType() == fife.CMD_QUIT_GAME:
@@ -167,7 +180,11 @@
 		else:
 			onCommand.send(sender=self.engine, command=command)
 		
-
+class _IMouseListener(IMouseListener):
+	def __init__(self, engine):
+		IMouseListener.__init__(self)
+		self.engine = engine
+		
 	# IMouseListener
 	def mouseEntered(self, evt):
 		mouseEntered.send(sender=self.engine, event=evt)
@@ -196,6 +213,26 @@
 	def mouseDragged(self, evt):
 		mouseDragged.send(sender=self.engine, event=evt)
 
+class _MapChangeListener(MapChangeListener):
+	def __init__(self, engine):
+		MapChangeListener.__init__(self)
+		self.engine = engine
+		
+	# MapChangeListener
+	def onMapChanged(self, map, changedLayers):
+		onMapChanged.send(sender=self.engine, map=map, changedLayers=changedLayers)
+		
+	def onLayerCreate(self, map, layer):
+		onLayerCreate.send(sender=self.engine, map=map, layer=layer)
+		
+	def onLayerDelete(self, map, layer):
+		onLayerDelete.send(sender=self.engine, map=map, layer=layer)
+		
+class _LayerChangeListener(LayerChangeListener):
+	def __init__(self, engine):
+		LayerChangeListener.__init__(self)
+		self.engine = engine
+		
 	# LayerChangeListener
 	def onLayerChanged(self, layer, changedInstances):
 		onLayerChanged.send(sender=self.engine, layer=layer, changedInstances=changedInstances)
@@ -205,20 +242,17 @@
 		
 	def onInstanceDelete(self, layer, instance):
 		onInstanceDelete.send(sender=self.engine, layer=layer, instance=instance)
-
-	# MapChangeListener
-	def onMapChanged(self, map, changedLayers):
-		onMapChanged.send(sender=self.engine, map=map, changedLayers=changedLayers)
 		
-	def onLayerCreate(self, map, layer):
-		onLayerCreate.send(sender=self.engine, map=map, layer=layer)
+class _ConsoleExecuter(ConsoleExecuter):
+	def __init__(self, engine):
+		ConsoleExecuter.__init__(self)
+		self.engine = engine
 		
-	def onLayerDelete(self, map, layer):
-		onLayerDelete.send(sender=self.engine, map=map, layer=layer)
-
 	# ConsoleExecuter
 	def onToolsClick(self):
 		onToolsClick.send(sender=self.engine)
 		
 	def onConsoleCommand(self, command):
 		onConsoleCommand.send(sender=self.engine, command=command)
+		
+		
\ No newline at end of file
--- a/clients/editor/scripts/mapview.py	Tue Aug 18 18:03:54 2009 +0000
+++ b/clients/editor/scripts/mapview.py	Fri Aug 21 14:39:54 2009 +0000
@@ -21,9 +21,19 @@
 			if not self._map.getLayers():
 				raise AttributeError('Editor error: map ' + self._map.getId() + ' has no layers. Cannot edit.')
 
+			map.addChangeListener(self._editor.getEventListener().mapchangelistener)
+			for layer in map.getLayers():
+				layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
+				
+			events.onLayerCreate.connect(self._layerCreated)
+
 		self.importlist = []
 		if hasattr(map, "importDirs"):
 			self.importlist.extend(map.importDirs)
+			
+	def _layerCreated(self, map, layer):
+		if map.getId() == self._map.getId():
+			layer.addChangeListener(self._editor.getEventListener().layerchangelistener)
 		
 	# Copied from mapeditor.py
 	def show(self):