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

Revision 736, 5.1 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

  • renamed scripts package to parpg
  • renamed parpg module to application
  • removed packaging and other related files (kept locally for reference, will reintroduce similar scripts to resolve bug #275
  • updated all import statements to respect changes above
  • 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
22
23class EventListener(fife.IKeyListener, 
24                   fife.ICommandListener, 
25                   fife.IMouseListener, 
26                   fife.ConsoleExecuter):
27    """Class that receives all events and distributes them to the listeners"""
28    def __init__(self, engine):
29        """Initialize the instance"""
30        self.event_manager = engine.getEventManager()
31
32        fife.IKeyListener.__init__(self)
33        self.event_manager.addKeyListener(self)
34        fife.ICommandListener.__init__(self)
35        self.event_manager.addCommandListener(self)
36        fife.IMouseListener.__init__(self)
37        self.event_manager.addMouseListener(self)
38        fife.ConsoleExecuter.__init__(self)
39        engine.getGuiManager().getConsole().setConsoleExecuter(self)
40       
41        self.listeners = {"Mouse" : [],                         
42                          "Key" : [],
43                          "Command" : [],
44                          "ConsoleCommand" : [],
45                          "Widget" : []}               
46               
47    def addListener(self, listener_type, listener):
48        """Adds a listener"""
49        if listener_type in self.listeners.iterkeys():
50            if not listener in self.listeners[listener_type]:
51                self.listeners[listener_type].append(listener)           
52        else:
53            print "Listener type \" " + listener_type + "\" not supported"
54   
55    def removeListener(self, listener_type, listener):
56        """Removes a listener"""
57        if listener_type in self.listeners.iterkeys():
58            self.listeners[listener_type].remove(listener)
59        else:
60            print "Listener type \" " + listener_type + "\" not supported"
61           
62    def mousePressed(self, evt):
63        """Called when a mouse button is pressed"""
64        for listeners in self.listeners["Mouse"]:
65            listeners.mousePressed(evt)
66
67    def mouseReleased(self, evt):
68        """Called when a mouse button is released"""
69        for listeners in self.listeners["Mouse"]:
70            listeners.mouseReleased(evt)
71
72    def mouseEntered(self, evt):
73        """Called when a mouse enters a region"""
74        for listeners in self.listeners["Mouse"]:
75            listeners.mouseEntered(evt)
76
77    def mouseExited(self, evt):
78        """Called when a mouse exits a region"""
79        for listeners in self.listeners["Mouse"]:
80            listeners.mouseExited(evt)
81
82    def mouseClicked(self, evt):
83        """Called after a mouse button is pressed and released"""
84        for listeners in self.listeners["Mouse"]:
85            listeners.mouseClicked(evt)
86
87    def mouseWheelMovedUp(self, evt):
88        """Called when the mouse wheel has been moved up"""
89        for listeners in self.listeners["Mouse"]:
90            listeners.mouseWheelMovedUp(evt)
91
92    def mouseWheelMovedDown(self, evt):
93        """Called when the mouse wheel has been moved down"""
94        for listener in self.listeners["Mouse"]:
95            listener.mouseWheelMovedDown(evt)
96
97    def mouseMoved(self, evt):
98        """Called when when the mouse has been moved"""
99        for listener in self.listeners["Mouse"]:
100            listener.mouseMoved(evt)
101
102    def mouseDragged(self, evt):
103        """Called when dragging the mouse"""
104        for listener in self.listeners["Mouse"]:
105            listener.mouseDragged(evt)
106
107    def keyPressed(self, evt):
108        """Called when a key is being pressed"""
109        for listener in self.listeners["Key"]:
110            listener.keyPressed(evt)
111
112    def keyReleased(self, evt):
113        """Called when a key is being released"""
114        for listener in self.listeners["Key"]:
115            listener.keyReleased(evt)
116
117    def onCommand(self, command):
118        """Called when a command is executed"""
119        for listener in self.listeners["Command"]:
120            listener.onCommand(command)
121
122    def onToolsClick(self):
123        """Called when the tools button has been clicked"""
124        for listener in self.listeners["ConsoleCommand"]:
125            listener.onToolsClick()
126
127    def onConsoleCommand(self, command):
128        """Called when a console command is executed"""
129        for listener in self.listeners["ConsoleCommand"]:
130            listener.onConsoleCommand(command)
131
132    def onWidgetAction(self, evt):
133        """Called when a widget action is executed"""
134        for listener in self.listeners["Widget"]:
135            listener.onWidgetAction(evt)
136
137
Note: See TracBrowser for help on using the repository browser.