diff gamelib/widgets.py @ 358:760f6a318d2e

Moved some widgets around.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 28 Aug 2010 16:50:37 +0200
parents 92cc50d7ce7a
children 277a7a0c2cea
line wrap: on
line diff
--- a/gamelib/widgets.py	Sat Aug 28 16:46:46 2010 +0200
+++ b/gamelib/widgets.py	Sat Aug 28 16:50:37 2010 +0200
@@ -6,9 +6,12 @@
 import textwrap
 
 import albow.controls
-from albow.resource import get_font
+import albow.menu
+from albow.resource import get_font, get_image
 from pygame.color import Color
+from pygame.rect import Rect
 
+from constants import BUTTON_SIZE
 from cursor import CursorWidget
 
 
@@ -36,6 +39,7 @@
     def _draw_all_no_bg(self, surface):
         pass
 
+
 class BoomButton(BoomLabel):
 
     def __init__(self, text, action, screen):
@@ -84,3 +88,53 @@
 
     def cursor_highlight(self):
         return False
+
+
+class HandButton(albow.controls.Image):
+    """The fancy hand button for the widget"""
+
+    def __init__(self, action):
+        # FIXME: Yes, please.
+        this_image = get_image('items', 'hand.png')
+        albow.controls.Image.__init__(self, image=this_image)
+        self.action = action
+        self.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE))
+
+    def mouse_down(self, event):
+        self.action()
+
+
+class PopupMenuButton(albow.controls.Button):
+
+    def __init__(self, text, action):
+        albow.controls.Button.__init__(self, text, action)
+
+        self.font = get_font(16, 'Vera.ttf')
+        self.set_rect(Rect(0, 0, BUTTON_SIZE, BUTTON_SIZE))
+        self.margin = (BUTTON_SIZE - self.font.get_linesize()) / 2
+
+
+class PopupMenu(albow.menu.Menu, CursorWidget):
+
+    def __init__(self, screen):
+        CursorWidget.__init__(self, screen)
+        self.screen = screen
+        self.shell = screen.shell
+        items = [
+                ('Resume Game', 'hide'),
+                ('Quit Game', 'quit'),
+                ('Exit to Main Menu', 'main_menu'),
+                ]
+        # albow.menu.Menu ignores title string
+        albow.menu.Menu.__init__(self, None, items)
+        self.font = get_font(16, 'Vera.ttf')
+
+    def show_menu(self):
+        """Call present, with the correct position"""
+        item_height = self.font.get_linesize()
+        menu_top = 600 - (len(self.items) * item_height + BUTTON_SIZE)
+        item = self.present(self.shell, (0, menu_top))
+        if item > -1:
+            # A menu item needs to be invoked
+            self.invoke_item(item)
+