changeset 267:1e8dca95c48a

Hook up hot-keys in editor for jerith
author Neil Muller <drnlmuller@gmail.com>
date Thu, 15 Sep 2011 12:54:16 +0200
parents 190aa1481908
children 01c21e4f2997
files mamba/habitats/editor.py mamba/widgets/toollist.py
diffstat 2 files changed, 43 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/mamba/habitats/editor.py	Thu Sep 15 12:53:52 2011 +0200
+++ b/mamba/habitats/editor.py	Thu Sep 15 12:54:16 2011 +0200
@@ -1,7 +1,7 @@
 """Habitat for editing levels."""
 
 import pygame.display
-from pygame.locals import SWSURFACE, KEYDOWN
+from pygame.locals import SWSURFACE, KEYDOWN, K_1, K_2
 
 from mamba.engine import Habitat, NewHabitatEvent
 from mamba.widgets.level import EditLevelWidget
@@ -49,6 +49,9 @@
         if ev.key in ESCAPE_KEYS:
             from mamba.habitats.mainmenu import MainMenu
             NewHabitatEvent.post(MainMenu())
+        elif ev.key == K_1:
+            # Activate floor button
+            self.floor_button.forced_click()
 
     def setup_toolbar(self):
         """Draw the editor toolbar"""
@@ -82,14 +85,15 @@
         self.container.add(self.current_tool)
         button_height += self.current_tool.surface.get_height()
         button_height += button_padding
-        floor_button = ImageButtonWidget(
+        self.floor_button = ImageButtonWidget(
                 (button_left, button_height), self.level.tileset.floor,
                 'Floor', color='white')
-        self.container.add(floor_button)
-        floor_button.add_callback('clicked', self.change_tool,
+        self.container.add(self.floor_button)
+        self.floor_button.add_callback('clicked', self.change_tool,
                 '.', 'Floor')
         self.edit_widget.set_tool('.')
-        button_height += floor_button.surface.get_height() + button_padding
+        button_height += (self.floor_button.surface.get_height()
+                + button_padding)
         if self.mode == 'Tile':
             tile_map = TILE_MAP
             change_mode_text = 'Switch to Things'
@@ -114,10 +118,10 @@
             tile_button.add_callback('clicked', self.change_tool,
                     tile_char, text)
             tool_list.append(tile_button)
-        tool_widget = ToolListWidget((button_left, button_height),
-                tool_list, MAX_TOOLS)
-        self.container.add(tool_widget)
-        button_height += tool_widget.rect.height + 2
+        self.tool_widget = ToolListWidget((button_left, button_height),
+                tool_list, MAX_TOOLS, start_key=K_2)
+        self.container.add(self.tool_widget)
+        button_height += self.tool_widget.rect.height + 2
 
         mode_button = TextButton((button_left, button_height),
                 change_mode_text)
--- a/mamba/widgets/toollist.py	Thu Sep 15 12:53:52 2011 +0200
+++ b/mamba/widgets/toollist.py	Thu Sep 15 12:54:16 2011 +0200
@@ -1,3 +1,5 @@
+from pygame.constants import KEYUP, K_1, K_PAGEDOWN, K_PAGEUP
+
 from mamba.widgets.base import Container
 from mamba.widgets.text import TextButton
 
@@ -5,31 +7,52 @@
 class ToolListWidget(Container):
     """List of other widgets, with some paging trickery"""
 
-    def __init__(self, rect, widget_list, page_length, padding=2):
+    def __init__(self, rect, widget_list, page_length, start_key=K_1,
+            padding=2):
         self.widget_list = widget_list
         self.page_length = page_length
         self.padding = padding
         self.page = 0
+        self.start_key = start_key
         super(ToolListWidget, self).__init__(rect)
         self.fill_page()
+        # We do this to avoid needing to worry about focus too much
+        self.add_callback(KEYUP, self.handle_key)
+        self.focussable = True
 
     def fill_page(self):
+        self.hot_keys = {}
+        self.prev_but = self.next_but = None
         start_page = self.page * self.page_length
         end_page = start_page + self.page_length
         button_height = self.rect.top + self.padding
         button_left = self.rect.left + self.padding
+        key = self.start_key
         for widget in self.widget_list[start_page:end_page]:
             widget.rect.topleft = (button_left, button_height)
             self.add(widget)
+            if key:
+                self.hot_keys[key] = widget
+                key += 1
             button_height += widget.rect.height + self.padding
         if start_page > 0:
-            prev_but = TextButton((button_left, button_height), 'Prev')
-            prev_but.add_callback('clicked', self.change_page, -1)
-            self.add(prev_but)
+            self.prev_but = TextButton((button_left, button_height), 'Prev')
+            self.prev_but.add_callback('clicked', self.change_page, -1)
+            self.add(self.prev_but)
         if end_page < len(self.widget_list):
-            next_but = TextButton((button_left + 100, button_height), 'Next')
-            next_but.add_callback('clicked', self.change_page, 1)
-            self.add(next_but)
+            self.next_but = TextButton((button_left + 100, button_height),
+                    'Next')
+            self.next_but.add_callback('clicked', self.change_page, 1)
+            self.add(self.next_but)
+
+    def handle_key(self, ev, widget):
+        if ev.key in self.hot_keys:
+            widget = self.hot_keys[ev.key]
+            return widget.forced_click()
+        elif ev.key == K_PAGEDOWN and self.prev_but:
+            self.prev_but.forced_click()
+        elif ev.key == K_PAGEUP and self.next_but:
+            self.next_but.forced_click()
 
     def change_page(self, ev, widget, change):
         self.page += change