changeset 257:3f8ce3452cd6

Add support for selecting dialogue choices (and for moving TextChoice around the screen).
author Simon Cross <hodgestar@gmail.com>
date Fri, 08 Apr 2011 01:36:44 +0200
parents e43e66f3f647
children d7e6830fe051
files skaapsteker/widgets/bubble.py skaapsteker/widgets/text.py
diffstat 2 files changed, 38 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/widgets/bubble.py	Fri Apr 08 00:59:35 2011 +0200
+++ b/skaapsteker/widgets/bubble.py	Fri Apr 08 01:36:44 2011 +0200
@@ -4,7 +4,7 @@
 import pygame
 
 from ..engine import OpenDialog, CloseDialog
-from .text import Text
+from .text import Text, TextChoice
 
 
 class DialogueWidget(object):
@@ -13,30 +13,39 @@
         self.npc = npc
         self.dsm = npc.dsm
         self._text = None
-        self._update_text()
+        self._text_choice = None
+        self._state_update()
 
-    def _update_text(self):
+    def _state_update(self):
+        pos = pygame.Rect((0, 0), (300, 1))
         state = self.dsm.get_state()
         if state.text:
-            text = "\n".join([
-                state.text,
-                "",
-                "Press RETURN to continue."
-                ])
-            pos = pygame.Rect((0, 0), (300, 1))
-            self._text = Text(text, pos, wrap=True)
+            pos = pos.move(0, 0) # copy
+            self._text = Text(state.text, pos, wrap=True)
         else:
             self._text = None
+        options = [(text, i) for (i, text) in state.choices]
+        options.append(("Leave", "L"))
+        pos = pos.move(0, 0) # copy
+        self._text_choice = TextChoice(options, pos, wrap=True)
+        self._text_choice.callbacks.append(self._selected)
+
+    def _selected(self, i, data):
+        if data == "L":
+            CloseDialog.post(self.npc)
+        else:
+            print "Chose", self.dsm.get_state().choices[i][1]
 
     def draw(self, level_surface):
         if self._text:
             self._text.rect.center = level_surface.get_clip().center
             self._text.draw(level_surface)
+            self._text_choice.rect.topleft = self._text.rect.bottomleft
+            self._text_choice.rect.move_ip((0, 10))
+            self._text_choice.draw(level_surface)
 
     def close(self):
         pass
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
-            if ev.key is K_RETURN:
-                CloseDialog.post(self.npc)
+        self._text_choice.dispatch(ev)
--- a/skaapsteker/widgets/text.py	Fri Apr 08 00:59:35 2011 +0200
+++ b/skaapsteker/widgets/text.py	Fri Apr 08 01:36:44 2011 +0200
@@ -105,25 +105,35 @@
     def __init__(self, options, pos, **kwargs):
         self.options = []
         self.option_widgets = []
-        self.rect = pygame.Rect(pos, (0, 0))
-        self.selector = Text(u'» ', pos, **kwargs)
+        if isinstance(pos, pygame.Rect):
+            self.rect = pos
+        else:
+            self.rect = pygame.Rect(pos, (0, 0))
+            pos = self.rect
+        self.selector = Text(u'» ', pos.move(0, 0), **kwargs)
         self.selected = 0
         self.callbacks = []
 
-        pos = self.rect.move(self.selector.rect.width, 0)
         for option in options:
             if not isinstance(option, tuple):
                 option = (option, None)
             self.options.append(option)
             text, data = option
-            t = Text(text, pos, **kwargs)
-            pos = pos.move(0, t.rect.height)
+            t = Text(text, pos.move(0, 0), **kwargs)
             self.option_widgets.append(t)
 
         self.rect.width = max(line.rect.width for line in self.option_widgets
                              ) + self.selector.rect.width
         self.rect.height = sum(line.rect.height for line in self.option_widgets)
 
+    def _update_rects(self):
+        pos = self.rect.move(self.selector.rect.width, 0)
+        for t in self.option_widgets:
+            t.rect.topleft = pos.topleft
+            pos = pos.move(0, t.rect.height)
+        self.selector.rect.top = self.option_widgets[self.selected].rect.top
+        self.selector.rect.left = self.rect.left
+
     def dispatch(self, ev):
         if ev.type is KEYDOWN:
             if ev.key == K_UP:
@@ -135,9 +145,9 @@
                     callback(self.selected, self.options[self.selected][1])
 
         self.selected %= len(self.option_widgets)
-        self.selector.rect.top = self.option_widgets[self.selected].rect.top
 
     def draw(self, surface):
+        self._update_rects()
         for option in self.option_widgets:
             option.draw(surface)
         self.selector.draw(surface)