annotate grease/mode.py @ 5:bc88f7d5ca8b

Added base files for grease
author KarstenBock@gmx.net
date Tue, 12 Jul 2011 10:16:48 +0200
parents
children ce33d344e202
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 Modes manage the state and transition between different application modes.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 Typically such modes are presented as different screens that the user can
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16 navigate between, similar to the way a browser navigates web pages. Individual
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17 modes may be things like:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 - Title screen
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20 - Options dialog
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 - About screen
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 - In-progress game
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23 - Inventory interface
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 The modal framework provides a simple mechanism to ensure that modes are
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 activated and deactivated properly. An activated mode is running and receives
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 events. A deactivated mode is paused and does not receive events.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 Modes may be managed as a *last-in-first-out* stack, or as a list, or ring
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 of modes in sequence, or some combination of all.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 For example usage see: :ref:`the mode section of the tutorial <tut-mode-section>`.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 __version__ = '$Id$'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 import abc
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 class BaseManager(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 """Mode manager abstract base class.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 The mode manager keeps a stack of modes where a single mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 is active at one time. As modes are pushed on and popped from
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 the stack, the mode at the top is always active. The current
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 active mode receives events from the manager's event dispatcher.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 modes = ()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 """The mode stack sequence. The last mode in the stack is
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 the current active mode. Read-only.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54 @property
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 def current_mode(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56 """The current active mode or ``None``. Read-only"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 return self.modes[-1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 except IndexError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 return None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 def on_last_mode_pop(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 """Hook executed when the last mode is popped from the manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 Implementing this method is optional for subclasses.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 :param mode: The :class:`Mode` object just popped from the manager
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 def activate_mode(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 """Perform actions to activate a node
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 :param mode: The :class: 'Mode' object to activate
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74 mode.activate(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 def deactivate_mode(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 """Perform actions to deactivate a node
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 :param mode: The :class: 'Mode' object to deactivate
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81 mode.deactivate(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84 def push_mode(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 """Push a mode to the top of the mode stack and make it active
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87 :param mode: The :class:`Mode` object to make active
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 current = self.current_mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 if current is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 self.deactivate_mode(current)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92 self.modes.append(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 self.activate_mode(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 def pop_mode(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 """Pop the current mode off the top of the stack and deactivate it.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 The mode now at the top of the stack, if any is then activated.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 :param mode: The :class:`Mode` object popped from the stack
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101 mode = self.modes.pop()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 mode.deactivate(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 self.event_dispatcher.remove_handlers(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 current = self.current_mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105 if current is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 self.activate_mode(current)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 self.on_last_mode_pop(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 return mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111 def swap_modes(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 """Exchange the specified mode with the mode at the top of the stack.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 This is similar to popping the current mode and pushing the specified
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114 one, but without activating the previous mode on the stack or
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 executing :meth:`on_last_mode_pop()` if there is no previous mode.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 :param mode: The :class:`Mode` object that was deactivated and replaced.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119 old_mode = self.modes.pop()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 self.deactivate_mode(old_mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121 self.modes.append(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122 self.activate_mode(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 return old_mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 def remove_mode(self, mode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 """Remove the specified mode. If the mode is at the top of the stack,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 this is equivilent to :meth:`pop_mode()`. If not, no other modes
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 are affected. If the mode is not in the manager, do nothing.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 :param mode: The :class:`Mode` object to remove from the manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132 if self.current_mode is mode:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 self.pop_mode()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 try:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 self.modes.remove(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 except ValueError:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 class BaseMode(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141 """Application mode very abstract base class
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143 __metaclass__ = abc.ABCMeta
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
144
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
145 manager = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
146 """The :class:`BaseManager` that manages this mode"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
147
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
148 def __init__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
149 self.active = False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
150
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
151 def on_activate(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
152 """Being called when the Mode is activated"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
153 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
154
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
155 def activate(self, mode_manager):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
156 """Activate the mode for the given mode manager, if the mode is already active,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
157 do nothing
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
158
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
159 The default implementation schedules time steps at :attr:`step_rate` per
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
160 second, sets the :attr:`manager` and sets the :attr:`active` flag to True.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
161 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
162 if not self.active:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
163 self.on_activate()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
164 self.manager = mode_manager
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
165 self.active = True
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
166
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
167 def on_deactivate(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
168 """Being called when the Mode is deactivated"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
169 pass
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
170
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
171 def deactivate(self, mode_manager):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
172 """Deactivate the mode, if the mode is not active, do nothing
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
173
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
174 The default implementation unschedules time steps for the mode and
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
175 sets the :attr:`active` flag to False.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
176 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
177 self.on_deactivate()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
178 self.active = False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
179
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
180
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
181 class BaseMulti(BaseMode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
182 """A mode with multiple submodes. One submode is active at one time.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
183 Submodes can be switched to directly or switched in sequence. If
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
184 the Multi is active, then one submode is always active.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
185
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
186 Multis are useful when modes can switch in an order other than
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
187 a LIFO stack, such as in "hotseat" multiplayer games, a
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
188 "wizard" style ui, or a sequence of slides.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
189
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
190 Note unlike a normal :class:`Mode`, a :class:`Multi` doesn't have it's own
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
191 :attr:`clock` and :attr:`step_rate`. The active submode's are used
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
192 instead.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
193 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
194 active_submode = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
195 """The currently active submode"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
196
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
197 def __init__(self, *submodes):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
198 # We do not invoke the superclass __init__ intentionally
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
199 self.active = False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
200 self.submodes = list(submodes)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
201
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
202 def add_submode(self, mode, before=None, index=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
203 """Add the submode, but do not make it active.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
204
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
205 :param mode: The :class:`Mode` object to add.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
206
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
207 :param before: The existing mode to insert the mode before.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
208 If the mode specified is not a submode, raise
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
209 ValueError.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
210
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
211 :param index: The place to insert the mode in the mode list.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
212 Only one of ``before`` or ``index`` may be specified.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
213
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
214 If neither ``before`` or ``index`` are specified, the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
215 mode is appended to the end of the list.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
216 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
217 assert before is None or index is None, (
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
218 "Cannot specify both 'before' and 'index' arguments")
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
219 if before is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
220 index = self.submodes.index(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
221 if index is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
222 self.submodes.insert(index, mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
223 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
224 self.submodes.append(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
225
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
226 def remove_submode(self, mode=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
227 """Remove the submode.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
228
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
229 :param mode: The submode to remove, if omitted the active submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
230 is removed. If the mode is not present, do nothing. If the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
231 mode is active, it is deactivated, and the next mode, if any
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
232 is activated. If the last mode is removed, the :class:`Multi`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
233 is removed from its manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
234 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
235 # TODO handle multiple instances of the same subnode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
236 if mode is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
237 mode = self.active_submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
238 elif mode not in self.submodes:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
239 return
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
240 next_mode = self.activate_next()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
241 self.submodes.remove(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
242 if next_mode is mode:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
243 if self.manager is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
244 self.manager.remove_mode(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
245 self._deactivate_submode()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
246
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
247 def activate_subnode(self, mode, before=None, index=None):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
248 """Activate the specified mode, adding it as a subnode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
249 if it is not already. If the mode is already the active
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
250 submode, do nothing.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
251
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
252 :param mode: The mode to activate, and add as necesary.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
253
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
254 :param before: The existing mode to insert the mode before
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
255 if it is not already a submode. If the mode specified is not
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
256 a submode, raise ValueError.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
257
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
258 :param index: The place to insert the mode in the mode list
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
259 if it is not already a submode. Only one of ``before`` or
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
260 ``index`` may be specified.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
261
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
262 If the mode is already a submode, the ``before`` and ``index``
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
263 arguments are ignored.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
264 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
265 if mode not in self.submodes:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
266 self.add_submode(mode, before, index)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
267 if self.active_submode is not mode:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
268 self._activate_submode(mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
269
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
270 def activate_next(self, loop=True):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
271 """Activate the submode after the current submode in order. If there
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
272 is no current submode, the first submode is activated.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
273
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
274 Note if there is only one submode, it's active, and `loop` is True
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
275 (the default), then this method does nothing and the subnode remains
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
276 active.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
277
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
278 :param loop: When :meth:`activate_next` is called
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
279 when the last submode is active, a True value for ``loop`` will
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
280 cause the first submode to be activated. Otherwise the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
281 :class:`Multi` is removed from its manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
282 :type loop: bool
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
283
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
284 :return:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
285 The submode that was activated or None if there is no
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
286 other submode to activate.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
287 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
288 assert self.submodes, "No submode to activate"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
289 next_mode = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
290 if self.active_submode is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
291 next_mode = self.submodes[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
292 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
293 last_mode = self.active_submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
294 index = self.submodes.index(last_mode) + 1
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
295 if index < len(self.submodes):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
296 next_mode = self.submodes[index]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
297 elif loop:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
298 next_mode = self.submodes[0]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
299 self._activate_submode(next_mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
300 return next_mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
301
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
302 def activate_previous(self, loop=True):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
303 """Activate the submode before the current submode in order. If there
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
304 is no current submode, the last submode is activated.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
305
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
306 Note if there is only one submode, it's active, and `loop` is True
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
307 (the default), then this method does nothing and the subnode remains
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
308 active.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
309
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
310 :param loop: When :meth:`activate_previous` is called
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
311 when the first submode is active, a True value for ``loop`` will
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
312 cause the last submode to be activated. Otherwise the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
313 :class:`Multi` is removed from its manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
314 :type loop: bool
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
315
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
316 :return:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
317 The submode that was activated or None if there is no
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
318 other submode to activate.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
319 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
320 assert self.submodes, "No submode to activate"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
321 prev_mode = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
322 if self.active_submode is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
323 prev_mode = self.submodes[-1]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
324 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
325 last_mode = self.active_submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
326 index = self.submodes.index(last_mode) - 1
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
327 if loop or index >= 0:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
328 prev_mode = self.submodes[index]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
329 self._activate_submode(prev_mode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
330 return prev_mode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
331
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
332 def _set_active_submode(self, submode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
333 self.active_submode = submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
334 self.step_rate = submode.step_rate
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
335
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
336 def _activate_submode(self, submode):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
337 """Activate a submode deactivating any current submode. If the Multi
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
338 itself is active, this happens immediately, otherwise the actual
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
339 activation is deferred until the Multi is activated. If the submode
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
340 is None, the Mulitmode is removed from its manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
341
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
342 If submode is already the active submode, do nothing.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
343 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
344 if self.active_submode is submode:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
345 return
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
346 assert submode in self.submodes, "Unknown submode"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
347 self._deactivate_submode()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
348 self._set_active_submode(submode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
349 if submode is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
350 if self.active:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
351 self.manager.activate_mode(submode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
352 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
353 if self.manager is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
354 self.manager.remove_mode(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
355
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
356 def clear_subnode(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
357 """Clear any subnmode data"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
358 self.active_submode = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
359 self.step_rate = None
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
360
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
361 def _deactivate_submode(self, clear_subnode=True):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
362 """Deactivate the current submode, if any. if `clear_subnode` is
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
363 True, `active_submode` is always None when this method returns
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
364 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
365 if self.active_submode is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
366 if self.active:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
367 self.manager.deactivate_mode(self.active_submode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
368 if clear_subnode:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
369 self.clear_subnode()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
370
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
371 def activate(self, mode_manager):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
372 """Activate the :class:`Multi` for the specified manager. The
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
373 previously active submode of the :class:`Multi` is activated. If there
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
374 is no previously active submode, then the first submode is made active.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
375 A :class:`Multi` with no submodes cannot be activated
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
376 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
377 assert self.submodes, "No submode to activate"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
378 self.manager = mode_manager
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
379 if self.active_submode is None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
380 self._set_active_submode(self.submodes[0])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
381 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
382 self._set_active_submode(self.active_submode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
383 self.manager.activate_mode(self.active_submode)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
384 super(BaseMulti, self).activate(mode_manager)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
385
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
386 def deactivate(self, mode_manager):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
387 """Deactivate the :class:`Multi` for the specified manager.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
388 The `active_submode`, if any, is deactivated.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
389 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
390 self._deactivate_submode(clear_subnode=False)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
391 super(BaseMulti, self).deactivate(mode_manager)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
392