annotate bGrease/mode.py @ 178:c706963ed0c3

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