File: twidget_manager.py

package info (click to toggle)
teg 0.11.2%2Bdebian-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,340 kB
  • sloc: ansic: 17,600; sh: 8,546; python: 1,442; xml: 1,310; makefile: 357
file content (295 lines) | stat: -rw-r--r-- 10,237 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# $Id: twidget_manager.py,v 1.1 2003/09/14 02:01:14 riq Exp $
###############################################################################################
# $Civil: widget_manager.py,v 1.6 2003/06/29 09:54:52 chakie Exp $
# This file belongs to http://civil.sf.net
# License: GNU GPL v2
# Copyright: Civil team
###############################################################################################

import pygame
import pygame.display
from pygame.locals import *
from twidget import TWidget
import twidget

class TWidgetManager( object ):

    sdl_surface = None

    """
    This class defines a simple framework for managing widgets and their events. All created widgets
    should be added to a framework before they are of any use or can receive events. All SDL events
    should be sent to this framework for possible dispatching to widgets. If an event does not
    consern any widget the other parts of the application are free to do whatever they wish with the
    event.

    Every event should be sent to handle() as the raw SDL event as received from pygame.event.poll ()
    or pygame.event.wait().
    """

    def __init__ (self, surface, dialog=None):
        """Initializes the instance"""

        # clear the list of registered widgets
        self.widgets = []

        # no keyboard shortcuts yet
        self.shortcuts = {}
        
        # the previous mouse position, for enter/leave events
        self.last_pos = (-1, -1)

        # the surface widgets are painted on
        self.surface = surface

        # store the dialog too
        self.dialog = dialog
        

    def register (self, widget, shortcut=None):
        """Register a widget within the widget manager. The widget will after it has been registered
        be able to receive various events. If the shortcut is a valid Pygame key id (of of the K_*
        constants), then the shortcut will be stored. The shortcut is used for activating a left
        mouse press on the widget when the given shortcut key is pressed."""
        # is the widget already in the list?
        if not widget in self.widgets:
            # nope, add it
            self.widgets.append ( widget )

            # do we have a shortcut?
            if shortcut != None:
                # yeah, so add it
                self.shortcuts [shortcut] = widget
            

    def deRegister (self, widget):
        """Removes the passed widget from the management."""
        # do we have the widget in the list?
        if widget in self.widgets:
            # yep, remove it
            self.widgets.remove ( widget )

            # TODO: delete from shortcuts too


    def paint (self, force = 0, clear = 0):
        """Repaints the surface if needed."""
        paintneeded = 0

        # should we clear the disply first?
        if clear:
            # yep, do that first
            self.surface.fill ( (0,0,0) ) 
            
        # loop over all widgets
        for widget in self.widgets:
            # paint widget 
            if widget.paint (self.surface, force):
                # it did something, so set the flag
                paintneeded = 1
                
        # is a paint needed?
        if paintneeded:
            # update the whole screen
#            TWidgetManager.sdl_surface.update ()
            pygame.display.flip()
         

    def handle (self, event):
        """Handles the passed event. If the event is of any interest to any of the registred widgets
        the widgets are allowed to act on the event. If any widget handled the event then this
        method will return 1 to indicate that it should not be processed any further. A value of 0
        indicates that the event was not processed."""

        # what type of event do we have?
        if event.type == twidget.KEYDOWN:
            # keyboard key pressed 
            return self.handleKeyDown (event)
            
        elif event.type == twidget.KEYUP:
            # keyboard key released
            return self.handleKeyUp (event)

        elif event.type == twidget.MOUSEMOTION:
            # mouse moved 
            return self.handleMouseMotion (event)

        elif event.type == twidget.MOUSEBUTTONDOWN:
            # mouse button pressed
            return self.handleMousePressed (event)

        elif event.type == twidget.MOUSEBUTTONUP:
            # mouse button released 
            return self.handleMouseReleased (event)

        elif event.type == twidget.TIMER:
            # timer event
            return self.handleTimer (event)

        elif event.type == twidget.QUIT:
            # player wants to quit
            return self.handleQuit (event)
            
        # it was none of our events
        return 0
    

    def handleKeyDown (self, event):
        """Handles an event when a key was pressed but not yet released. Uses the last known
        position of the mouse to determine which widget has focus."""

        # by default we've not handled it
        handled = twidget.UNHANDLED

        # loop over all widgets in the list
        for w in self.widgets:
            # is the mouse inside the widget?
            if w.isInside ( self.last_pos ) and w.isVisible ():
                # have the widget handle the event
                handled = w.handle ( twidget.KEYDOWN, event )

        # was it handled?
        if handled != twidget.UNHANDLED:
            # it was handled somehow, go away
            return handled

        # not handled, so see if we have any keyboard shortcuts for itm get the key 
        key = event.key

        # any shortcut for it?
        if self.shortcuts.has_key ( key ):
            # yeah, so get the widget
            w = self.shortcuts[key]

            #execute create an left mouse click event that's inside the widget
            newevent = pygame.event.Event ( MOUSEBUTTONUP, pos=w.getPosition (), button=1 )

            return self.handleMouseReleased ( newevent )

        # no shortcut either, it was not handled
        return twidget.UNHANDLED
    

    def handleKeyUp (self, event):
        """Handles an event when a pressed key released. Uses the last known
        position of the mouse to determine which widget has focus."""
        # by default we've not handled it
        handled = twidget.UNHANDLED

        # loop over all widgets in the list
        for w in self.widgets:
            # is the mouse inside the widget?
            if w.isInside ( self.last_pos ) and w.isVisible ():
                # have the widget handle the event
                w.handle ( twidget.KEYUP, event )
                handled = twidget.HANDLED

        return handled
     

    def handleMouseMotion (self, event):
        """Handles an event when the mouse pointer was moved within a widget."""

        # get the position of the mouse
        position = event.pos

        # by default we've not handled it
        handled = twidget.UNHANDLED
        
        # loop over all widgets in the list
        for w in self.widgets:

            # is the mouse inside the widget?
            if w.isInside ( position ) and not w.isInside ( self.last_pos ) and w.isVisible ():
                # have the widget handle the event
                w.handle ( twidget.MOUSEENTEREVENT, event )
                handled = twidget.HANDLED

            # is the mouse outside the widget
            elif not w.isInside ( position ) and w.isInside ( self.last_pos ) and w.isVisible ():
                # have the widget handle the event
                w.handle ( twidget.MOUSELEAVEEVENT, event )
                handled = twidget.HANDLED

            # is the mouse the widget and the last move was too inside?
            elif w.isInside ( position ) and w.isInside ( self.last_pos ) and w.isVisible ():
                # have the widget handle the event
                w.handle ( twidget.MOUSEMOTION, event )
                handled = twidget.HANDLED

        # store new last position
        self.last_pos = position

        return handled
            

    def handleMousePressed (self, event):
        """Handles an event when a mouse key was pressed but not yet released."""

        # get the position of the press
        position = event.pos
       
        # loop over all widgets in the list
        for w in self.widgets:
            # is the mouse inside the widget?
            if w.isInside ( position ) and w.isVisible ():
                # have the widget handle the event
                status = w.handle ( twidget.MOUSEBUTTONDOWN, event )

                # did it handle it?
                if status != twidget.UNHANDLED:
                    # it handled it, we're done here
                    return status

        return twidget.UNHANDLED
       

    def handleMouseReleased (self, event):
        """Handles an event when a pressed mouse button was released."""

        # get the position of the press
        position = event.pos
        
        # loop over all widgets in the list
        for w in self.widgets:
            # is the mouse inside the widget?
            if w.isInside ( position ) and w.isVisible ():
                # have the widget handle the event
                status = w.handle ( twidget.MOUSEBUTTONUP, event )

                # did it handle it?
                if status != twidget.UNHANDLED:
                    # it handled it, we're done here
                    return status

        return twidget.UNHANDLED


    def handleTimer (self, event):
        """Handles a timer event."""

        # do we have a dialog?
        if self.dialog != None:
            # yep, call it
            return self.dialog.timer ()
         
        # it was not handled
        return twidget.UNHANDLED


    def handleQuit (self, event):
        """Handles a quit event. This is called when the player has done something that should cause
        the game to quit."""
        
        # do we have a dialog?
        if self.dialog != None:
            # yep, call it. this may never actually return if the dialog decides to quit
            return self.dialog.quit ()
         
        # it was not handled
        return twidget.UNHANDLED
       
#  Local Variables:
#  mode: auto-fill
#  fill-column: 100
#  End: