annotate src/parpg/bGrease/impl/mode.py @ 108:2f928c913c78

Fixed item_type not being set when creating items.
author KarstenBock@gmx.net
date Fri, 23 Sep 2011 13:30:41 +0200
parents 0f659c7675f6
children
rev   line source
92
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
1 #############################################################################
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
2 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
4 # All Rights Reserved.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
5 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
6 # This software is subject to the provisions of the MIT License
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
7 # A copy of the license should accompany this distribution.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
11 #
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
12 #############################################################################
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
13 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
14 Modes manage the state and transition between different application modes.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
15 Typically such modes are presented as different screens that the user can
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
16 navigate between, similar to the way a browser navigates web pages. Individual
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
17 modes may be things like:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
18
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
19 - Title screen
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
20 - Options dialog
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
21 - About screen
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
22 - In-progress game
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
23 - Inventory interface
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
24
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
25 The modal framework provides a simple mechanism to ensure that modes are
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
26 activated and deactivated properly. An activated mode is running and receives
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
27 events. A deactivated mode is paused and does not receive events.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
28
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
29 Modes may be managed as a *last-in-first-out* stack, or as a list, or ring
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
30 of modes in sequence, or some combination of all.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
31
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
32 For example usage see: :ref:`the mode section of the tutorial <tut-mode-section>`.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
33 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
34
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
35 __version__ = '$Id$'
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
36
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
37 import abc
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
38 import pyglet
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
39 from parpg.bGrease.mode import *
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
40
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
41 class PygletManager(BaseManager):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
42 """Mode manager abstract base class using pyglet.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
43
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
44 The mode manager keeps a stack of modes where a single mode
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
45 is active at one time. As modes are pushed on and popped from
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
46 the stack, the mode at the top is always active. The current
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
47 active mode receives events from the manager's event dispatcher.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
48 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
49
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
50 event_dispatcher = None
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
51 """:class:`pyglet.event.EventDispatcher` object that the
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
52 active mode receive events from.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
53 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
54
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
55 def activate_mode(self, mode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
56 """Perform actions to activate a node
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
57
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
58 :param mode: The :class: 'Mode' object to activate
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
59 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
60 BaseManager.activate_mode(self, mode)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
61 self.event_dispatcher.push_handlers(mode)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
62
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
63 def deactivate_mode(self, mode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
64 """Perform actions to deactivate a node
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
65
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
66 :param mode: The :class: 'Mode' object to deactivate
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
67 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
68 BaseManager.deactivate_mode(self, mode)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
69 self.event_dispatcher.remove_handlers(mode)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
70
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
71 class Manager(PygletManager):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
72 """A basic mode manager that wraps a single
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
73 :class:`pyglet.event.EventDispatcher` object for use by its modes.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
74 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
75
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
76 def __init__(self, event_dispatcher):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
77 self.modes = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
78 self.event_dispatcher = event_dispatcher
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
79
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
80
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
81 class ManagerWindow(PygletManager, pyglet.window.Window):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
82 """An integrated mode manager and pyglet window for convenience.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
83 The window is the event dispatcher used by modes pushed to
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
84 this manager.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
85
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
86 Constructor arguments are identical to :class:`pyglet.window.Window`
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
87 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
88
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
89 def __init__(self, *args, **kw):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
90 super(ManagerWindow, self).__init__(*args, **kw)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
91 self.modes = []
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
92 self.event_dispatcher = self
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
93
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
94 def on_key_press(self, symbol, modifiers):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
95 """Default :meth:`on_key_press handler`, pops the current mode on ``ESC``"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
96 if symbol == pyglet.window.key.ESCAPE:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
97 self.pop_mode()
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
98
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
99 def on_last_mode_pop(self, mode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
100 """Hook executed when the last mode is popped from the manager.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
101 When the last mode is popped from a window, an :meth:`on_close` event
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
102 is dispatched.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
103
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
104 :param mode: The :class:`Mode` object just popped from the manager
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
105 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
106 self.dispatch_event('on_close')
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
107
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
108
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
109 class Mode(BaseMode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
110 """Application mode abstract base class using pyglet
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
111
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
112 Subclasses must implement the :meth:`step` method
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
113
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
114 :param step_rate: The rate of :meth:`step()` calls per second.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
115
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
116 :param master_clock: The :class:`pyglet.clock.Clock` interface used
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
117 as the master clock that ticks the world's clock. This
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
118 defaults to the main pyglet clock.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
119 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
120 clock = None
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
121 """The :class:`pyglet.clock.Clock` instance used as this mode's clock.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
122 You should use this clock to schedule tasks for this mode, so they
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
123 properly respect when the mode is active or inactive
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
124
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
125 Example::
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
126
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
127 my_mode.clock.schedule_once(my_cool_function, 4)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
128 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
129
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
130 def __init__(self, step_rate=60, master_clock=pyglet.clock,
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
131 clock_factory=pyglet.clock.Clock):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
132 BaseMode.__init__(self)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
133 self.step_rate = step_rate
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
134 self.time = 0.0
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
135 self.master_clock = master_clock
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
136 self.clock = clock_factory(time_function=lambda: self.time)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
137 self.clock.schedule_interval(self.step, 1.0 / step_rate)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
138
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
139 def on_activate(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
140 """Being called when the Mode is activated"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
141 self.master_clock.schedule(self.tick)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
142
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
143 def on_deactivate(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
144 """Being called when the Mode is deactivated"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
145 self.master_clock.unschedule(self.tick)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
146
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
147 def tick(self, dt):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
148 """Tick the mode's clock.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
149
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
150 :param dt: The time delta since the last tick
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
151 :type dt: float
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
152 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
153 self.time += dt
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
154 self.clock.tick(poll=False)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
155
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
156 @abc.abstractmethod
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
157 def step(self, dt):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
158 """Execute a timestep for this mode. Must be defined by subclasses.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
159
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
160 :param dt: The time delta since the last time step
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
161 :type dt: float
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
162 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
163
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
164 class Multi(BaseMulti, Mode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
165 """A mode with multiple submodes. One submode is active at one time.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
166 Submodes can be switched to directly or switched in sequence. If
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
167 the Multi is active, then one submode is always active.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
168
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
169 Multis are useful when modes can switch in an order other than
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
170 a LIFO stack, such as in "hotseat" multiplayer games, a
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
171 "wizard" style ui, or a sequence of slides.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
172
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
173 Note unlike a normal :class:`Mode`, a :class:`Multi` doesn't have it's own
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
174 :attr:`clock` and :attr:`step_rate`. The active submode's are used
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
175 instead.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
176 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
177
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
178 def __init__(self, submodes):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
179 BaseMulti.__init__(self, submodes)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
180 self.time = 0.0
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
181
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
182
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
183 def _set_active_submode(self, submode):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
184 BaseMulti._set_active_submode(self, submode)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
185 self.master_clock = submode.master_clock
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
186 self.clock = submode.clock
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
187
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
188 def clear_subnode(self):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
189 """Clear any subnmode data"""
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
190 BaseMulti.clear_subnode(self)
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
191 self.master_clock = None
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
192 self.clock = None
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
193
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
194 def tick(self, dt):
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
195 """Tick the active submode's clock.
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
196
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
197 :param dt: The time delta since the last tick
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
198 :type dt: float
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
199 """
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
200 self.time += dt
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
201 if self.active_submode is not None:
0f659c7675f6 Changed "import bGrease" to "import parpg.bGrease".
KarstenBock@gmx.net
parents: 66
diff changeset
202 self.active_submode.clock.tick(poll=False)