From: pin Date: Mon, 22 Feb 2010 17:37:28 +0000 (+0000) Subject: création d'un dispatcher d'événement central. X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/commitdiff_plain/7d014e3dc9f077f1b0402aed8f957146bf317ea7?ds=sidebyside création d'un dispatcher d'événement central. git-svn-id: https://svn.cri.ensmp.fr/svn/minwii/trunk@51 fe552daf-6dbe-4428-90eb-1537e0879342 --- diff --git a/src/app/eventutils.py b/src/app/eventutils.py index 10ddbad..1741579 100755 --- a/src/app/eventutils.py +++ b/src/app/eventutils.py @@ -27,28 +27,61 @@ $Id$ $URL$ """ import types +import pygame + +class _EventDispatcher : + def __init__(self) : + self.registry = {} + + def addEventListener(self, eventType, listener) : + if self.registry.has_key(eventType) : + self.registry[eventType].append(listener) + else : + self.registry[eventType] = [listener] + + def dispatchEvents(self) : + events = pygame.event.get() + for event in events : + listeners = self.registry.get(event.type, []) + for listener in listeners : + listener(event) + +EventDispatcher = _EventDispatcher() def event_handler(eventType) : - def doRename(m) : - m.__name__ = 'eventHandler%s' % eventType + def markFunctionAsListener(m) : + m.__islistener__ = True + m.__eventtype__ = eventType return m - return doRename + return markFunctionAsListener -class MetaRenamer(type): - def __new__(mcs, name, bases, dict) : - for k, v in dict.items() : - if isinstance(v, types.FunctionType) : - if k != v.__name__ : - print 'renommage de %s en %s' % (k, v.__name__) - dict[v.__name__] = v - del dict[k] - return type.__new__(mcs, name, bases, dict) +class EventInitializer(type): + + def __init__(cls, name, bases, dict) : + def init_listeners(self) : + for k, v in dict.items() : + if isinstance(v, types.FunctionType) and hasattr(v, '__islistener__') : + listener = getattr(self, k) + EventDispatcher.addEventListener(v.__eventtype__, listener) + del v.__islistener__ + del v.__eventtype__ + + def ctor(self, *args, **kw) : + init_listeners(self) + default_ctor = dict.get('__init__') + if not default_ctor : + super(cls, self).__init__(*args, **kw) + else : + default_ctor(self, *args, **kw) + + cls.__init__ = ctor class EventHandlerMixin(object) : - __metaclass__ = MetaRenamer + __metaclass__ = EventInitializer - def input(self, event) : - handler = getattr(self, 'eventHandler%s' % event.type, lambda e:None) - handler(event) + #def input(self) : + # event = pygame.event.wait() + # handler = getattr(self, 'eventHandler%s' % event.type, lambda e:None) + # handler(event) diff --git a/src/app/minwii.py b/src/app/minwii.py index 3757df1..a3292f0 100755 --- a/src/app/minwii.py +++ b/src/app/minwii.py @@ -10,26 +10,11 @@ from pgu.gui import Desktop from pgu.gui import QUIT from widgets.home import Home from widgets.playingscreen import _PlayingScreenBase, SongPlayingScreenTest -import time class MinWii(Desktop): def __init__(self) : Desktop.__init__(self) - - # home = Home() - # home.connect(QUIT, self.quit) - # self.run(home) - - # class C:pass - # o = C() - # o.midi=1 - # playingScreen = _PlayingScreenBase([o]) - import pygame playingScreen = SongPlayingScreenTest() - #playingScreen.draw(pygame.display.get_surface()) playingScreen.run() - - #pygame.display.flip() - #time.sleep(2) diff --git a/src/app/start.py b/src/app/start.py index 11f2905..02db7ce 100755 --- a/src/app/start.py +++ b/src/app/start.py @@ -13,7 +13,7 @@ def main() : pygame.init() modeResolution = (1024,768) - window = pygame.display.set_mode(modeResolution) + window = pygame.display.set_mode(modeResolution, pygame.FULLSCREEN) MinWii() # from gui.PGUConfiguration import PGUConfiguration # pygame.init() diff --git a/src/app/widgets/cursors.py b/src/app/widgets/cursors.py index 6d81a67..b52bfb3 100755 --- a/src/app/widgets/cursors.py +++ b/src/app/widgets/cursors.py @@ -8,9 +8,12 @@ $URL$ import pygame import os +from eventutils import EventHandlerMixin, event_handler +from itertools import cycle +from pygame.locals import USEREVENT +TIMEOUT = USEREVENT + 1 - -class WarpingCursor(pygame.sprite.Sprite): +class WarpingCursor(pygame.sprite.Sprite, EventHandlerMixin): ''' The class for animating the warping cursor @@ -63,6 +66,7 @@ class WarpingCursor(pygame.sprite.Sprite): surface = pygame.display.get_surface() surface.blit(self.image, self.rect) + self._startBlink() #self.flashImagePath = flashImage #self.durations = durations @@ -73,6 +77,20 @@ class WarpingCursor(pygame.sprite.Sprite): #self._imagePointer = 0 #self._animationOffset = 0 #self._flashTimer = 0 + + def _startBlink(self) : + pygame.time.set_timer(TIMEOUT, self.duration) + self.iterator = self.iterImages() + + def iterImages(self) : + for img in cycle(self.images) : + yield img + + @event_handler(TIMEOUT) + def loadNext(self, event) : + self.image = self.iterator.next() + surface = pygame.display.get_surface() + surface.blit(self.image, self.rect) def update(self) : print 'cursor update' diff --git a/src/app/widgets/playingscreen.py b/src/app/widgets/playingscreen.py index 23f2643..248f0bb 100755 --- a/src/app/widgets/playingscreen.py +++ b/src/app/widgets/playingscreen.py @@ -10,7 +10,7 @@ import pygame from colorsys import hls_to_rgb from gradients import gradients from cursors import WarpingCursor -from eventutils import event_handler, EventHandlerMixin +from eventutils import event_handler, EventDispatcher, EventHandlerMixin from math import floor import types # TODO : positionner cette constance en fonction de la résolution d'affichage @@ -32,6 +32,7 @@ class _PlayingScreenBase(pygame.sprite.OrderedUpdates, EventHandlerMixin) : distinctNotes : notes disctinctes présentes dans la chanson triées du plus grave au plus aigu. """ + print '__init__ _PlayingScreenBase' super(_PlayingScreenBase, self).__init__() self.distinctNotes = distinctNotes self.keyboardLength = 0 @@ -76,7 +77,6 @@ class _PlayingScreenBase(pygame.sprite.OrderedUpdates, EventHandlerMixin) : hueStep = FIRST_HUE / (self.keyboardLength - 1) for i, rect in enumerate(self.keyboardRects) : hue = FIRST_HUE - hueStep * i - print hue c = Column(hue, rect) self.add(c) @@ -92,15 +92,11 @@ class _PlayingScreenBase(pygame.sprite.OrderedUpdates, EventHandlerMixin) : def run(self): self._running = True + clock = pygame.time.Clock() while self._running : pygame.display.flip() - events = pygame.event.get() - for event in events: - self.input(event) - - def input(self, event) : - handler = getattr(self, 'eventHandler%s' % event.type, lambda e:None) - handler(event) + EventDispatcher.dispatchEvents() + clock.tick(50) @event_handler(pygame.KEYDOWN) def handleKeyDown(self, event) : @@ -126,6 +122,7 @@ class SongPlayingScreen(_PlayingScreenBase) : class SongPlayingScreenTest(_PlayingScreenBase) : def __init__(self) : + print '__init__ SongPlayingScreenTest' class C:pass o = C() o.midi=1