Skip to content

Commit f0c5c86

Browse files
author
4b796c65
committed
Console objects now have __contains__, also new class tdl.event.App
1 parent 068d4b3 commit f0c5c86

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

tdl/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0r23
1+
1.0r24

tdl/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ def getCover(x, length):
348348
# not only would that have been a better idea. It would of been
349349
# faster too. (but only faster for Console's)
350350

351+
def __contains__(self, key):
352+
"""
353+
It's likely that you'll want to know if a point on the console can be
354+
drawn on. You can use ((x, y) in console) to check.
355+
"""
356+
x, y = key
357+
return (0 <= x < self.width) and (0 <= y < self.height)
358+
351359
def _drawable(self, x, y):
352360
"""Used internally
353361
Checks if a cell is part of the console.

tdl/event.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
use keyWait and isWindowClosed to control your entire program.
1414
"""
1515

16+
import time
1617
import ctypes
1718

1819
from .tcod import _lib, _Mouse, _Key
1920
from . import tcod as _tcod
21+
import tdl as _tdl
2022

2123
_mousel = 0
2224
_mousem = 0
@@ -107,6 +109,48 @@ def __init__(self, pos, cell, motion, cellmotion):
107109
self.motion = motion
108110
self.cellmotion = cellmotion
109111

112+
class App(object):
113+
__slots__ = ()
114+
115+
def ev_QUIT(self, event):
116+
raise SystemExit()
117+
118+
def ev_KEYDOWN(self, event):
119+
pass
120+
121+
def ev_KEYUP(self, event):
122+
pass
123+
124+
def ev_MOUSEDOWN(self, event):
125+
pass
126+
127+
def ev_MOUSEUP(self, event):
128+
pass
129+
130+
def ev_MOUSEMOTION(self, event):
131+
pass
132+
133+
def update(self, dt):
134+
pass
135+
136+
def run(self):
137+
prevTime = time.clock()
138+
while 1:
139+
for event in get():
140+
if event.type: # exclude custom events with a blank type variable
141+
# call the ev_* methods
142+
method = 'ev_%s' % event.type # ev_TYPE
143+
getattr(self, method)(event)
144+
if event.type == 'KEYDOWN':
145+
# call the key_* methods
146+
method = 'key_%s' % event.keyname # key_KEYNAME
147+
if hasattr(self, method): # silently exclude undefined methods
148+
getattr(self, method)(event)
149+
newTime = time.clock()
150+
self.update(newTime - prevTime)
151+
prevTime = newTime
152+
_tdl.flush()
153+
110154
def get():
111155
"""Flushes the event queue and returns the list of events.
112156

0 commit comments

Comments
 (0)