Callbacks for widget events
[naja.git] / naja / widgets / base.py
index 6b86a870f5adadbb518ed4fc4caae837ccce7650..81f11b649df9734371017f34891fa3ec6b360505 100644 (file)
@@ -1,4 +1,9 @@
+from collections import defaultdict
+
 import pygame
+from pygame import locals as pgl
+
+from naja.events import InvalidateTheWorld
 
 
 class Widget(object):
@@ -6,26 +11,44 @@ class Widget(object):
         self.pos = pos
         self.size = size or (0, 0)
         self._prepared = False
+        self.callbacks = defaultdict(list)
 
     @property
     def rect(self):
         return pygame.Rect(self.pos, self.size)
 
     def render(self, surface):
+        '''Draw the widget onto surface'''
         if not self._prepared:
             self.prepare()
             self._prepared = True
         self.draw(surface)
 
     def draw(self, surface):
+        '''The overrideable bit of widget drawing'''
         raise NotImplemented()
 
     def prepare(self):
-        raise NotImplemented()
+        '''Prepare the widget for drawing. This usually caches a surface.'''
 
     def handle_event(self, ev):
+        '''Return True if the event has been handled'''
+        if InvalidateTheWorld.matches(ev):
+            # Invalidate has special handling. Widgets should never return
+            # True for for this event
+            self._prepared = False
+            return False
+        if ev.type == pgl.MOUSEBUTTONDOWN:
+            self.callback('click')
         return False
 
+    def add_callback(self, event, callback):
+        self.callbacks[event].append(callback)
+
+    def callback(self, event):
+        for callback in self.callbacks[event]:
+            callback(event)
+
 
 class Container(object):
     def __init__(self, *widgets):
@@ -41,7 +64,16 @@ class Container(object):
             widget.render(surface)
 
     def handle_event(self, ev):
-        for widget in self.widgets:
-            if widget.handle_event(ev):
-                return True
+        if hasattr(ev, 'pos'):
+            for widget in self.widgets:
+                if isinstance(widget, Container):
+                    if widget.handle_event(ev):
+                        return True
+                elif widget.rect.collidepoint(ev.pos):
+                    if widget.handle_event(ev):
+                        return True
+        else:
+            for widget in self.widgets:
+                if widget.handle_event(ev):
+                    return True
         return False