source: branches/active/character_customization/game/parpg/common/listeners/event_listener.py @ 833

Revision 797, 5.3 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

  • converted print statements to logging messages.
  • all of parpg's log messages go to the same file, but are differenciated by their class names
  • need to find a way to manipulate fife's log level
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18"""This module contains the EventListener that receives events and distributes
19them to PARPG listeners"""
20
21from fife import fife
22import logging
23
24logger = logging.getLogger('event_listener')
25
26class EventListener(fife.IKeyListener, 
27                   fife.ICommandListener, 
28                   fife.IMouseListener, 
29                   fife.ConsoleExecuter):
30    """Class that receives all events and distributes them to the listeners"""
31    def __init__(self, engine):
32        """Initialize the instance"""
33        self.event_manager = engine.getEventManager()
34
35        fife.IKeyListener.__init__(self)
36        self.event_manager.addKeyListener(self)
37        fife.ICommandListener.__init__(self)
38        self.event_manager.addCommandListener(self)
39        fife.IMouseListener.__init__(self)
40        self.event_manager.addMouseListener(self)
41        fife.ConsoleExecuter.__init__(self)
42        engine.getGuiManager().getConsole().setConsoleExecuter(self)
43       
44        self.listeners = {"Mouse" : [],                         
45                          "Key" : [],
46                          "Command" : [],
47                          "ConsoleCommand" : [],
48                          "Widget" : []}               
49               
50    def addListener(self, listener_type, listener):
51        """Adds a listener"""
52        if listener_type in self.listeners.iterkeys():
53            if not listener in self.listeners[listener_type]:
54                self.listeners[listener_type].append(listener)           
55        else:
56            logger.warning("Listener type "
57                                  "'{0}' not supported".format(listener_type))
58   
59    def removeListener(self, listener_type, listener):
60        """Removes a listener"""
61        if listener_type in self.listeners.iterkeys():
62            self.listeners[listener_type].remove(listener)
63        else:
64            logger.warning("Listener type "
65                                  "'{0}' not supported".format(listener_type))
66           
67    def mousePressed(self, evt):
68        """Called when a mouse button is pressed"""
69        for listeners in self.listeners["Mouse"]:
70            listeners.mousePressed(evt)
71
72    def mouseReleased(self, evt):
73        """Called when a mouse button is released"""
74        for listeners in self.listeners["Mouse"]:
75            listeners.mouseReleased(evt)
76
77    def mouseEntered(self, evt):
78        """Called when a mouse enters a region"""
79        for listeners in self.listeners["Mouse"]:
80            listeners.mouseEntered(evt)
81
82    def mouseExited(self, evt):
83        """Called when a mouse exits a region"""
84        for listeners in self.listeners["Mouse"]:
85            listeners.mouseExited(evt)
86
87    def mouseClicked(self, evt):
88        """Called after a mouse button is pressed and released"""
89        for listeners in self.listeners["Mouse"]:
90            listeners.mouseClicked(evt)
91
92    def mouseWheelMovedUp(self, evt):
93        """Called when the mouse wheel has been moved up"""
94        for listeners in self.listeners["Mouse"]:
95            listeners.mouseWheelMovedUp(evt)
96
97    def mouseWheelMovedDown(self, evt):
98        """Called when the mouse wheel has been moved down"""
99        for listener in self.listeners["Mouse"]:
100            listener.mouseWheelMovedDown(evt)
101
102    def mouseMoved(self, evt):
103        """Called when when the mouse has been moved"""
104        for listener in self.listeners["Mouse"]:
105            listener.mouseMoved(evt)
106
107    def mouseDragged(self, evt):
108        """Called when dragging the mouse"""
109        for listener in self.listeners["Mouse"]:
110            listener.mouseDragged(evt)
111
112    def keyPressed(self, evt):
113        """Called when a key is being pressed"""
114        for listener in self.listeners["Key"]:
115            listener.keyPressed(evt)
116
117    def keyReleased(self, evt):
118        """Called when a key is being released"""
119        for listener in self.listeners["Key"]:
120            listener.keyReleased(evt)
121
122    def onCommand(self, command):
123        """Called when a command is executed"""
124        for listener in self.listeners["Command"]:
125            listener.onCommand(command)
126
127    def onToolsClick(self):
128        """Called when the tools button has been clicked"""
129        for listener in self.listeners["ConsoleCommand"]:
130            listener.onToolsClick()
131
132    def onConsoleCommand(self, command):
133        """Called when a console command is executed"""
134        for listener in self.listeners["ConsoleCommand"]:
135            listener.onConsoleCommand(command)
136
137    def onWidgetAction(self, evt):
138        """Called when a widget action is executed"""
139        for listener in self.listeners["Widget"]:
140            listener.onWidgetAction(evt)
141
142
Note: See TracBrowser for help on using the repository browser.