Mercurial > parpg-source
annotate bGrease/impl/controls.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 |
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 """Control systems for binding controls to game logic""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
14 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
15 import bGrease |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
16 from pyglet.window import key |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
17 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
18 class KeyControls(grease.System): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
19 """System that maps subclass-defined action methods to keys. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
20 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
21 Keys may be mapped in the subclass definition using decorators |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
22 defined here as class methods or at runtime using the ``bind_key_*`` |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
23 instance methods. |
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 See :ref:`an example implementation in the tutorial <tut-controls-example>`. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
26 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
27 MODIFIER_MASK = ~(key.MOD_NUMLOCK | key.MOD_SCROLLLOCK | key.MOD_CAPSLOCK) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
28 """The MODIFIER_MASK allows you to filter out modifier keys that should be |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
29 ignored by the application. By default, capslock, numlock, and scrolllock |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
30 are ignored. |
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 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
33 world = None |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
34 """:class:`grease.World` object this system is bound to""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
35 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
36 def __init__(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
37 self._key_press_map = {} |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
38 self._key_release_map = {} |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
39 self._key_hold_map = {} |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
40 for name in self.__class__.__dict__: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
41 member = getattr(self, name) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
42 if hasattr(member, '_grease_hold_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
43 for binding in member._grease_hold_key_binding: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
44 self.bind_key_hold(member, *binding) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
45 if hasattr(member, '_grease_press_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
46 for binding in member._grease_press_key_binding: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
47 self.bind_key_press(member, *binding) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
48 if hasattr(member, '_grease_release_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
49 for binding in member._grease_release_key_binding: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
50 self.bind_key_release(member, *binding) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
51 self.held_keys = set() |
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 ## decorator methods for binding methods to key input events ## |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
54 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
55 @classmethod |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
56 def key_hold(cls, symbol, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
57 """Decorator to bind a method to be executed where a key is held down""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
58 def bind(f): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
59 if not hasattr(f, '_grease_hold_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
60 f._grease_hold_key_binding = [] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
61 f._grease_hold_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK)) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
62 return f |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
63 return bind |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
64 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
65 @classmethod |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
66 def key_press(cls, symbol, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
67 """Decorator to bind a method to be executed where a key is initially depressed""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
68 def bind(f): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
69 if not hasattr(f, '_grease_press_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
70 f._grease_press_key_binding = [] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
71 f._grease_press_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK)) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
72 return f |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
73 return bind |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
74 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
75 @classmethod |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
76 def key_release(cls, symbol, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
77 """Decorator to bind a method to be executed where a key is released""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
78 def bind(f): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
79 if not hasattr(f, '_grease_release_key_binding'): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
80 f._grease_release_key_binding = [] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
81 f._grease_release_key_binding.append((symbol, modifiers & cls.MODIFIER_MASK)) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
82 return f |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
83 return bind |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
84 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
85 ## runtime binding methods ## |
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 def bind_key_hold(self, method, key, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
88 """Bind a method to a key at runtime to be invoked when the key is |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
89 held down, this replaces any existing key hold binding for this key. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
90 To unbind the key entirely, pass ``None`` for method. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
91 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
92 if method is not None: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
93 self._key_hold_map[key, modifiers & self.MODIFIER_MASK] = method |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
94 else: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
95 try: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
96 del self._key_hold_map[key, modifiers & self.MODIFIER_MASK] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
97 except KeyError: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
98 pass |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
99 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
100 def bind_key_press(self, method, key, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
101 """Bind a method to a key at runtime to be invoked when the key is initially |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
102 pressed, this replaces any existing key hold binding for this key. To unbind |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
103 the key entirely, pass ``None`` for method. |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
104 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
105 if method is not None: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
106 self._key_press_map[key, modifiers & self.MODIFIER_MASK] = method |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
107 else: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
108 try: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
109 del self._key_press_map[key, modifiers & self.MODIFIER_MASK] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
110 except KeyError: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
111 pass |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
112 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
113 def bind_key_release(self, method, key, modifiers=0): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
114 """Bind a method to a key at runtime to be invoked when the key is releaseed, |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
115 this replaces any existing key hold binding for this key. To unbind |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
116 the key entirely, pass ``None`` for method. |
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 if method is not None: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
119 self._key_release_map[key, modifiers & self.MODIFIER_MASK] = method |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
120 else: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
121 try: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
122 del self._key_release_map[key, modifiers & self.MODIFIER_MASK] |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
123 except KeyError: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
124 pass |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
125 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
126 def step(self, dt): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
127 """invoke held key functions""" |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
128 already_run = set() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
129 for key in self.held_keys: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
130 func = self._key_hold_map.get(key) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
131 if func is not None and func not in already_run: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
132 already_run.add(func) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
133 func(dt) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
134 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
135 def on_key_press(self, key, modifiers): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
136 """Handle pyglet key press. Invoke key press methods and |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
137 activate key hold functions |
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 key_mod = (key, modifiers & self.MODIFIER_MASK) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
140 if key_mod in self._key_press_map: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
141 self._key_press_map[key_mod]() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
142 self.held_keys.add(key_mod) |
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 def on_key_release(self, key, modifiers): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
145 """Handle pyglet key release. Invoke key release methods and |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
146 deactivate key hold functions |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
147 """ |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
148 key_mod = (key, modifiers & self.MODIFIER_MASK) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
149 if key_mod in self._key_release_map: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
150 self._key_release_map[key_mod]() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
151 self.held_keys.discard(key_mod) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
152 |
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 if __name__ == '__main__': |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
155 import pyglet |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
156 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
157 class TestKeyControls(KeyControls): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
158 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
159 MODIFIER_MASK = ~(key.MOD_NUMLOCK | key.MOD_SCROLLLOCK | key.MOD_CTRL) |
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 remapped = False |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
162 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
163 @KeyControls.key_hold(key.UP) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
164 @KeyControls.key_hold(key.W) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
165 def up(self, dt): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
166 print 'UP!' |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
167 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
168 @KeyControls.key_hold(key.LEFT) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
169 @KeyControls.key_hold(key.A) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
170 def left(self, dt): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
171 print 'LEFT!' |
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 @KeyControls.key_hold(key.RIGHT) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
174 @KeyControls.key_hold(key.D) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
175 def right(self, dt): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
176 print 'RIGHT!' |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
177 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
178 @KeyControls.key_hold(key.DOWN) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
179 @KeyControls.key_hold(key.S) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
180 def down(self, dt): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
181 print 'DOWN!' |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
182 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
183 @KeyControls.key_press(key.SPACE) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
184 def fire(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
185 print 'FIRE!' |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
186 |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
187 @KeyControls.key_press(key.R) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
188 def remap_keys(self): |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
189 if not self.remapped: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
190 self.bind_key_hold(None, key.W) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
191 self.bind_key_hold(None, key.A) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
192 self.bind_key_hold(None, key.S) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
193 self.bind_key_hold(None, key.D) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
194 self.bind_key_hold(self.up, key.I) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
195 self.bind_key_hold(self.left, key.J) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
196 self.bind_key_hold(self.right, key.L) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
197 self.bind_key_hold(self.down, key.K) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
198 else: |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
199 self.bind_key_hold(None, key.I) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
200 self.bind_key_hold(None, key.J) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
201 self.bind_key_hold(None, key.K) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
202 self.bind_key_hold(None, key.L) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
203 self.bind_key_hold(self.up, key.W) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
204 self.bind_key_hold(self.left, key.A) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
205 self.bind_key_hold(self.right, key.D) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
206 self.bind_key_hold(self.down, key.S) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
207 self.remapped = not self.remapped |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
208 |
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 window = pyglet.window.Window() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
211 window.clear() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
212 controls = TestKeyControls() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
213 window.push_handlers(controls) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
214 pyglet.clock.schedule_interval(controls.step, 0.5) |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
215 pyglet.app.run() |
a6bbb732b27b
Added .hgeol file to automatically convert line endings.
KarstenBock@gmx.net
parents:
41
diff
changeset
|
216 |