DriverPygame: add mouse events and key press autorepeat.
--- a/tuikit/common.py Mon Oct 10 22:20:59 2011 +0200
+++ b/tuikit/common.py Tue Oct 11 10:09:58 2011 +0200
@@ -176,3 +176,26 @@
LTEE = '├'
RTEE = '┤'
+
+class MouseEvent:
+ def __init__(self, x=0, y=0):
+ self.x = x # global coordinates
+ self.y = y
+ self.wx = x # local widget coordinates
+ self.wy = y
+ self.px = 0 # parent coordinates
+ self.py = 0
+ self.button = 0
+
+
+ def childevent(self, child):
+ ev = MouseEvent(self.x, self.y)
+ # original local coordinates are new parent coordinates
+ ev.px = self.wx
+ ev.py = self.wy
+ # update local coordinates
+ ev.wx = self.wx - child.x
+ ev.wy = self.wy - child.y
+
+ return ev
+
--- a/tuikit/driver_curses.py Mon Oct 10 22:20:59 2011 +0200
+++ b/tuikit/driver_curses.py Tue Oct 11 10:09:58 2011 +0200
@@ -6,29 +6,7 @@
import logging
from tuikit.driver import Driver
-
-
-class MouseEvent:
- def __init__(self, x=0, y=0):
- self.x = x # global coordinates
- self.y = y
- self.wx = x # local widget coordinates
- self.wy = y
- self.px = 0 # parent coordinates
- self.py = 0
- self.button = 0
-
-
- def childevent(self, child):
- ev = MouseEvent(self.x, self.y)
- # original local coordinates are new parent coordinates
- ev.px = self.wx
- ev.py = self.wy
- # update local coordinates
- ev.wx = self.wx - child.x
- ev.wy = self.wy - child.y
-
- return ev
+from tuikit.common import MouseEvent
class DriverCurses(Driver):
--- a/tuikit/driver_pygame.py Mon Oct 10 22:20:59 2011 +0200
+++ b/tuikit/driver_pygame.py Tue Oct 11 10:09:58 2011 +0200
@@ -5,7 +5,7 @@
import logging
from tuikit.driver import Driver
-from tuikit.common import Coords, Size
+from tuikit.common import Coords, Size, MouseEvent
class CharCache:
@@ -164,26 +164,49 @@
'''Process input, return list of events.'''
events = []
for ev in pygame.event.get():
+ # mouse
if ev.type == pygame.MOUSEMOTION:
- pass
- elif ev.type == pygame.MOUSEBUTTONUP:
- pass
- elif ev.type == pygame.MOUSEBUTTONDOWN:
- pass
+ evdata = MouseEvent(ev.pos[0] // self.charsize.w, ev.pos[1] // self.charsize.h)
+ events.append(('mousemove', evdata))
+ elif ev.type in (pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP):
+ evdata = MouseEvent(ev.pos[0] // self.charsize.w, ev.pos[1] // self.charsize.h)
+ evdata.button = ev.button
+ evname = {pygame.MOUSEBUTTONDOWN: 'mousedown', pygame.MOUSEBUTTONUP: 'mouseup'}
+ events.append((evname[ev.type], evdata))
+
+ # keyboard
elif ev.type == pygame.KEYDOWN:
- if ev.key in self.key_map:
- events.append(('keypress', self.key_map[ev.key], None))
- elif ev.unicode:
- events.append(('keypress', None, ev.unicode))
+ keypress = self.keypress_from_pygame_event(ev)
+ events.append(keypress)
+ self.last_keypress = keypress
+ self.last_key = ev.key
+ pygame.time.set_timer(pygame.USEREVENT, 200)
+ elif ev.type == pygame.USEREVENT: # repeat last key press
+ events.append(self.last_keypress)
+ pygame.time.set_timer(pygame.USEREVENT, 50)
+ elif ev.type == pygame.KEYUP:
+ if ev.key == self.last_key:
+ pygame.time.set_timer(pygame.USEREVENT, 0)
+
+ # window resize
elif ev.type == pygame.VIDEORESIZE:
#self.size.h, self.size.w = self.screen.getmaxyx()
events.append(('resize',))
+
+ # window close
elif ev.type == pygame.QUIT:
events.append(('quit',))
+
else:
self.log.warning('Unknown PyGame event: %r', ev.type)
return events
+ def keypress_from_pygame_event(self, ev):
+ if ev.key in self.key_map:
+ keypress = ('keypress', self.key_map[ev.key], None)
+ elif ev.unicode:
+ keypress = ('keypress', None, ev.unicode)
+ return keypress
## drawing ##
--- a/tuikit/menu.py Mon Oct 10 22:20:59 2011 +0200
+++ b/tuikit/menu.py Tue Oct 11 10:09:58 2011 +0200
@@ -30,7 +30,8 @@
i = 1
for item in self.items:
if item is None:
- screen.puts(x, y + i, screen.LTEE + screen.HLINE * (self.width - 2) + screen.RTEE)
+ screen.puts(x, y + i, screen.unigraph.LTEE + \
+ screen.unigraph.HLINE * (self.width - 2) + screen.unigraph.RTEE)
else:
if self.selected == item:
screen.pushcolor(self.highlight)