Skip to content

Commit e5b078f

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
Apps can now be suspended, better KeyEvent variable names, event.get is now an interruptible generator.
1 parent 1c48298 commit e5b078f

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

tdl/event.py

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from . import __tcod as _tcod
2121
import tdl as _tdl
2222

23+
_eventQueue = []
24+
2325
_mousel = 0
2426
_mousem = 0
2527
_mouser = 0
@@ -44,9 +46,11 @@ class Event(object):
4446
type = None
4547

4648
def __repr__(self):
49+
"""List an events public attributes in print calls
50+
"""
4751
attrdict = {}
4852
for varname in dir(self):
49-
if '_' in varname:
53+
if '_' == varname[0]:
5054
continue
5155
attrdict[varname] = self.__getattribute__(varname)
5256
return '%s Event %s' % (self.__class__.__name__, repr(attrdict))
@@ -59,18 +63,18 @@ class Quit(Event):
5963
type = 'QUIT'
6064

6165
class KeyEvent(Event):
62-
__slots__ = ('key', 'keyname', 'char', 'lalt', 'lctrl', 'ralt', 'rctrl',
63-
'shift', 'alt', 'ctrl')
66+
__slots__ = ('key', 'keyname', 'char', 'shift', 'alt', 'ctrl',
67+
'leftAlt', 'leftCtrl', 'rightAlt', 'rightCtrl')
6468

6569
def __init__(self, key, char, lalt, lctrl, ralt, rctrl, shift):
6670
self.key = key
6771
self.keyname = _keynames[key]
6872
char = char if isinstance(char, str) else char.decode()
6973
self.char = char.replace('\x00', '') # change null to empty string
70-
self.lalt = bool(lalt)
71-
self.ralt = bool(ralt)
72-
self.lctrl = bool(lctrl)
73-
self.rctrl = bool(rctrl)
74+
self.leftAlt = bool(lalt)
75+
self.rightAlt = bool(ralt)
76+
self.leftCtrl = bool(lctrl)
77+
self.rightCtrl = bool(rctrl)
7478
self.shift = bool(shift)
7579
self.alt = bool(lalt or ralt)
7680
self.ctrl = bool(lctrl or rctrl)
@@ -110,7 +114,7 @@ def __init__(self, pos, cell, motion, cellmotion):
110114
self.cellmotion = cellmotion
111115

112116
class App(object):
113-
__slots__ = ()
117+
__slots__ = ('__running')
114118

115119
def ev_QUIT(self, event):
116120
raise SystemExit()
@@ -133,9 +137,13 @@ def ev_MOUSEMOTION(self, event):
133137
def update(self, dt):
134138
pass
135139

140+
def suspend(self):
141+
self._running = False
142+
136143
def run(self):
144+
self.__running = True
137145
prevTime = time.clock()
138-
while 1:
146+
while self._running:
139147
for event in get():
140148
if event.type: # exclude custom events with a blank type variable
141149
# call the ev_* methods
@@ -146,42 +154,28 @@ def run(self):
146154
method = 'key_%s' % event.keyname # key_KEYNAME
147155
if hasattr(self, method): # silently exclude undefined methods
148156
getattr(self, method)(event)
157+
if not __running:
158+
break # interupt event handing after suspend()
149159
newTime = time.clock()
150160
self.update(newTime - prevTime)
151161
prevTime = newTime
152162
_tdl.flush()
153-
154-
def get():
155-
"""Flushes the event queue and returns the list of events.
156-
157-
This function returns Event objects that can be ID'd and sorted with their type attribute:
158-
for event in tdl.event.get():
159-
if event.type == 'QUIT':
160-
raise SystemExit()
161-
elif event.type == 'MOUSEDOWN':
162-
print('Mouse button %i clicked at %i, %i' % (event.button, event.pos[0], event.pos[1]))
163-
elif event.type == 'KEYDOWN':
164-
print('Key #%i "%s" pressed' % (event.key, event.char))
165-
166-
Here is a list of events and their attributes:
167-
QUIT
168-
KEYDOWN: key char keyname alt ctrl shift lalt lctrl ralt rctrl
169-
KEYUP: key char keyname alt ctrl shift lalt lctrl ralt rctrl
170-
MOUSEDOWN: button pos cell
171-
MOUSEUP: button pos cell
172-
MOUSEMOTION: pos motion cell cellmotion
173-
"""
163+
164+
def _processEvents():
165+
"""Flushes the event queue from libtcod into the global list _eventQueue"""
174166
global _mousel, _mousem, _mouser, _eventsflushed
175167
_eventsflushed = True
176168
events = []
177169

178170
mouse = _Mouse()
179171
libkey = _Key()
180172
while 1:
181-
if not _lib.TCOD_sys_check_for_event(_tcod.TCOD_EVENT_ANY, libkey, mouse):
173+
libevent = _lib.TCOD_sys_check_for_event(_tcod.TCOD_EVENT_ANY, libkey, mouse)
174+
if not libevent: # no more events from libtcod
182175
break
183176

184-
if mouse.dx or mouse.dy:
177+
#if mouse.dx or mouse.dy:
178+
if libevent & _tcod.TCOD_EVENT_MOUSE_MOVE:
185179
events.append(MouseMotion(*mouse.motion))
186180

187181
mousepos = ((mouse.x, mouse.y), (mouse.cx, mouse.cy))
@@ -217,7 +211,32 @@ def get():
217211
if _lib.TCOD_console_is_window_closed():
218212
events.append(Quit())
219213

220-
return events
214+
_eventQueue.extend(events)
215+
216+
def get():
217+
"""Flushes the event queue and returns the list of events.
218+
219+
This function returns Event objects that can be ID'd and sorted with their type attribute:
220+
for event in tdl.event.get():
221+
if event.type == 'QUIT':
222+
raise SystemExit()
223+
elif event.type == 'MOUSEDOWN':
224+
print('Mouse button %i clicked at %i, %i' % (event.button, event.pos[0], event.pos[1]))
225+
elif event.type == 'KEYDOWN':
226+
print('Key #%i "%s" pressed' % (event.key, event.char))
227+
228+
Here is a list of events and their attributes:
229+
QUIT
230+
KEYDOWN: key char keyname alt ctrl shift lalt lctrl ralt rctrl
231+
KEYUP: key char keyname alt ctrl shift lalt lctrl ralt rctrl
232+
MOUSEDOWN: button pos cell
233+
MOUSEUP: button pos cell
234+
MOUSEMOTION: pos motion cell cellmotion
235+
"""
236+
_processEvents()
237+
while _eventQueue:
238+
yield(_eventQueue.pop(0))
239+
raise StopIteration()
221240

222241
def keyWait():
223242
"""Waits until the user presses a key. Then returns a KeyDown event.
@@ -229,18 +248,6 @@ def keyWait():
229248
_lib.TCOD_console_wait_for_keypress_wrapper(libkey, flush)
230249
return KeyDown(*libkey)
231250

232-
# tested this function recently, it did not work
233-
#def keyPressed(key):
234-
# """Returns True when key is currently pressed.
235-
#
236-
# key can be a number or single length string
237-
# """
238-
# assert isinstance(key, (str, int)), "key must be a single character string or int"
239-
# if not isinstance(key, int):
240-
# assert len(key) == 1, "key can not be a multi character string"
241-
# key = ord(key)
242-
# return _lib.TCOD_console_check_for_keypress(key).pressed # returns key object?
243-
244251
def isWindowClosed():
245252
"""Returns True if the exit button on the window has been clicked and
246253
stays True afterwards.

0 commit comments

Comments
 (0)