forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdlevent.py
More file actions
330 lines (257 loc) · 9.38 KB
/
Copy pathsdlevent.py
File metadata and controls
330 lines (257 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
"""
An alternative, more direct implementation of event handling using cffi
calls to SDL functions. The current code is incomplete, but can be
extended easily by following the official SDL documentation.
This module be run directly like any other event get example, but is meant
to be copied into your code base. Then you can use the sdlevent.get and
sdlevent.wait functions in your code.
Printing any event will tell you its attributes in a human readable format.
An events type attr is just the classes name with all letters upper-case.
Like in tdl, events use a type attribute to tell events apart. Unlike tdl
and tcod the names and values used are directly derived from SDL.
As a general guideline for turn-based rouge-likes, you should use
KeyDown.sym for commands, and TextInput.text for name entry.
This module may be included as a tcod.event module once it is more mature.
An absolute minimal example:
import tcod
import sdlevent
with tcod.console_init_root(80, 60, 'title') as console:
while True:
for event in sdlevent.wait():
print(event)
if event.type == 'QUIT':
raise SystemExit()
tcod.console_flush()
"""
# CC0 License: https://creativecommons.org/publicdomain/zero/1.0/
# To the extent possible under law, Kyle Stewart has waived all copyright and
# related or neighboring rights to this sdlevent.py module.
import tcod
def _copy_attrs(prefix):
"""Copy names and values from tcod.lib into this modules top-level scope.
This is a private function, used internally.
Args:
prefix (str): Used to filter out which names to copy.
Returns:
Dict[Any,str]: A reverse lookup table used in Event repr functions.
"""
g = globals() # dynamically add names to the global state
# removes the SDL prefix, this module already has SDL in the name
if prefix.startswith('SDL_'):
name_starts_at = 4
elif prefix.startswith('SDL'):
name_starts_at = 3
else:
name_starts_at = 0
revere_table = {}
for attr in dir(tcod.lib):
if attr.startswith(prefix):
name = attr[name_starts_at:]
value = getattr(tcod.lib, attr)
revere_table[value] = 'sdlevent.' + name
g[name] = value
return revere_table
def _describe_bitmask(bits, table, default='0'):
"""Returns a bitmask in human readable form.
This is a private function, used internally.
Args:
bits (int): The bitmask to be represented.
table (Dict[Any,str]): A reverse lookup table.
default (Any): A default return value when bits is 0.
Returns: str: A printable version of the bits variable.
"""
result = []
for bit, name in table.items():
if bit & bits:
result.append(name)
if not result:
return default
return '|'.join(result)
_REVSRSE_SCANCODE_TABLE = _copy_attrs('SDL_SCANCODE')
_REVSRSE_SYM_TABLE = _copy_attrs('SDLK')
_REVSRSE_MOD_TABLE = _copy_attrs('KMOD')
_REVSRSE_WHEEL_TABLE = _copy_attrs('SDL_MOUSEWHEEL')
# manually define names for SDL macros
BUTTON_LEFT = 1
BUTTON_MIDDLE = 2
BUTTON_RIGHT = 3
BUTTON_X1 = 4
BUTTON_X2 = 5
BUTTON_LMASK = 0x01
BUTTON_MMASK = 0x02
BUTTON_RMASK = 0x04
BUTTON_X1MASK = 0x08
BUTTON_X2MASK = 0x10
_REVSRSE_BUTTON_TABLE = {
BUTTON_LEFT: 'sdlevent.BUTTON_LEFT',
BUTTON_MIDDLE: 'sdlevent.BUTTON_MIDDLE',
BUTTON_RIGHT: 'sdlevent.BUTTON_RIGHT',
BUTTON_X1: 'sdlevent.BUTTON_X1',
BUTTON_X2: 'sdlevent.BUTTON_X2',
}
_REVSRSE_BUTTON_MASK_TABLE = {
BUTTON_LMASK: 'sdlevent.BUTTON_LMASK',
BUTTON_MMASK: 'sdlevent.BUTTON_MMASK',
BUTTON_RMASK: 'sdlevent.BUTTON_RMASK',
BUTTON_X1MASK: 'sdlevent.BUTTON_X1MASK',
BUTTON_X2MASK: 'sdlevent.BUTTON_X2MASK',
}
class Event(object):
"""The base event class."""
@classmethod
def from_sdl_event(cls, sdl_event):
"""Return a class instance from a cffi SDL_Event pointer."""
raise NotImplementedError()
@property
def type(self):
"""All event types are just the class name, but all upper-case."""
return self.__class__.__name__.upper()
class Quit(Event):
"""An application quit request event.
For more info on when this event is triggered see:
https://wiki.libsdl.org/SDL_EventType#SDL_QUIT
"""
@classmethod
def from_sdl_event(cls, sdl_event):
return cls()
def __repr__(self):
return 'sdlevent.%s()' % self.__class__.__name__
class KeyboardEvent(Event):
def __init__(self, scancode, sym, mod):
self.scancode = scancode
self.sym = sym
self.mod = mod
@classmethod
def from_sdl_event(cls, sdl_event):
keysym = sdl_event.key.keysym
return cls(keysym.scancode, keysym.sym, keysym.mod)
def __repr__(self):
return ('sdlevent.%s(scancode=%s, sym=%s, mod=%s)' %
(self.__class__.__name__,
_REVSRSE_SCANCODE_TABLE[self.scancode],
_REVSRSE_SYM_TABLE[self.sym],
_describe_bitmask(self.mod, _REVSRSE_MOD_TABLE),
)
)
class KeyDown(KeyboardEvent):
pass
class KeyUp(KeyboardEvent):
pass
class MouseMotion(Event):
def __init__(self, x, y, xrel, yrel, state):
self.x = x
self.y = y
self.xrel = xrel
self.yrel = yrel
self.state = state
@classmethod
def from_sdl_event(cls, sdl_event):
motion = sdl_event.motion
return cls(motion.x, motion.y, motion.xrel, motion.yrel, motion.state)
def __repr__(self):
return ('sdlevent.%s(x=%i, y=%i, xrel=%i, yrel=%i, state=%s)' %
(self.__class__.__name__,
self.x, self.y,
self.xrel, self.yrel,
_describe_bitmask(self.state, _REVSRSE_BUTTON_MASK_TABLE),
)
)
class MouseButtonEvent(Event):
def __init__(self, x, y, button):
self.x = x
self.y = y
self.button = button
@classmethod
def from_sdl_event(cls, sdl_event):
button = sdl_event.button
return cls(button.x, button.y, button.button)
def __repr__(self):
return ('sdlevent.%s(x=%i, y=%i, button=%s)' %
(self.__class__.__name__,
self.x, self.y, _REVSRSE_BUTTON_TABLE[self.button],
)
)
class MouseButtonDown(MouseButtonEvent):
pass
class MouseButtonUp(MouseButtonEvent):
pass
class MouseWheel(Event):
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
@classmethod
def from_sdl_event(cls, sdl_event):
wheel = sdl_event.wheel
return cls(wheel.x, wheel.y, wheel.direction)
def __repr__(self):
return ('sdlevent.%s(x=%i, y=%i, direction=%s)' %
(self.__class__.__name__,
self.x, self.y,
_REVSRSE_WHEEL_TABLE[self.direction],
)
)
class TextInput(Event):
def __init__(self, text):
self.text = text
@classmethod
def from_sdl_event(cls, sdl_event):
return cls(tcod.ffi.string(sdl_event.text.text, 32).decode('utf8'))
def __repr__(self):
return ('sdlevent.%s(text=%r)' % (self.__class__.__name__, self.text))
_SDL_TO_CLASS_TABLE = {
tcod.lib.SDL_QUIT: Quit,
tcod.lib.SDL_KEYDOWN: KeyDown,
tcod.lib.SDL_KEYUP: KeyUp,
tcod.lib.SDL_MOUSEMOTION: MouseMotion,
tcod.lib.SDL_MOUSEBUTTONDOWN: MouseButtonDown,
tcod.lib.SDL_MOUSEBUTTONUP: MouseButtonUp,
tcod.lib.SDL_MOUSEWHEEL: MouseWheel,
tcod.lib.SDL_TEXTINPUT: TextInput,
}
def get():
"""Iterate over all pending events.
Returns:
Iterator[sdlevent.Event]:
An iterator of Event subclasses.
"""
sdl_event = tcod.ffi.new('SDL_Event*')
while tcod.lib.SDL_PollEvent(sdl_event):
if sdl_event.type in _SDL_TO_CLASS_TABLE:
yield _SDL_TO_CLASS_TABLE[sdl_event.type].from_sdl_event(sdl_event)
def wait(timeout=None):
"""Block until an event exists, then iterate over all events.
Keep in mind that this function will wake even for events not handled by
this module.
Args:
timeout (Optional[int]):
Maximum number of milliseconds to wait, or None to wait forever.
Returns:
Iterator[sdlevent.Event]: Same iterator as a call to sdlevent.get
"""
if timeout is not None:
tcod.lib.SDL_WaitEventTimeout(tcod.ffi.NULL, timeout)
else:
tcod.lib.SDL_WaitEvent(tcod.ffi.NULL)
return get()
def _main():
"""An example program for when this module is run directly."""
WIDTH, HEIGHT = 120, 60
TITLE = 'sdlevent.py engine'
with tcod.console_init_root(WIDTH, HEIGHT, TITLE) as console:
tcod.sys_set_fps(24)
while True:
for event in wait():
print(event)
if event.type == 'QUIT':
raise SystemExit()
elif event.type == 'MOUSEMOTION':
console.rect(0, HEIGHT - 1, WIDTH, 1, True)
console.print_(0, HEIGHT - 1, repr(event))
else:
console.blit(console, 0, 0, 0, 1, WIDTH, HEIGHT - 2)
console.print_(0, HEIGHT - 3, repr(event))
tcod.console_flush()
if __name__ == '__main__':
_main()