Changeset 257:3f8ce3452cd6


Ignore:
Timestamp:
04/07/11 23:36:44 (12 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Phase:
public
Message:

Add support for selecting dialogue choices (and for moving TextChoice around the screen).

Location:
skaapsteker/widgets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/widgets/bubble.py

    r256 r257  
    55
    66from ..engine import OpenDialog, CloseDialog
    7 from .text import Text
     7from .text import Text, TextChoice
    88
    99
     
    1414        self.dsm = npc.dsm
    1515        self._text = None
    16         self._update_text()
     16        self._text_choice = None
     17        self._state_update()
    1718
    18     def _update_text(self):
     19    def _state_update(self):
     20        pos = pygame.Rect((0, 0), (300, 1))
    1921        state = self.dsm.get_state()
    2022        if state.text:
    21             text = "\n".join([
    22                 state.text,
    23                 "",
    24                 "Press RETURN to continue."
    25                 ])
    26             pos = pygame.Rect((0, 0), (300, 1))
    27             self._text = Text(text, pos, wrap=True)
     23            pos = pos.move(0, 0) # copy
     24            self._text = Text(state.text, pos, wrap=True)
    2825        else:
    2926            self._text = None
     27        options = [(text, i) for (i, text) in state.choices]
     28        options.append(("Leave", "L"))
     29        pos = pos.move(0, 0) # copy
     30        self._text_choice = TextChoice(options, pos, wrap=True)
     31        self._text_choice.callbacks.append(self._selected)
     32
     33    def _selected(self, i, data):
     34        if data == "L":
     35            CloseDialog.post(self.npc)
     36        else:
     37            print "Chose", self.dsm.get_state().choices[i][1]
    3038
    3139    def draw(self, level_surface):
     
    3341            self._text.rect.center = level_surface.get_clip().center
    3442            self._text.draw(level_surface)
     43            self._text_choice.rect.topleft = self._text.rect.bottomleft
     44            self._text_choice.rect.move_ip((0, 10))
     45            self._text_choice.draw(level_surface)
    3546
    3647    def close(self):
     
    3849
    3950    def dispatch(self, ev):
    40         if ev.type is KEYDOWN:
    41             if ev.key is K_RETURN:
    42                 CloseDialog.post(self.npc)
     51        self._text_choice.dispatch(ev)
  • skaapsteker/widgets/text.py

    r249 r257  
    106106        self.options = []
    107107        self.option_widgets = []
    108         self.rect = pygame.Rect(pos, (0, 0))
    109         self.selector = Text(u'» ', pos, **kwargs)
     108        if isinstance(pos, pygame.Rect):
     109            self.rect = pos
     110        else:
     111            self.rect = pygame.Rect(pos, (0, 0))
     112            pos = self.rect
     113        self.selector = Text(u'» ', pos.move(0, 0), **kwargs)
    110114        self.selected = 0
    111115        self.callbacks = []
    112116
    113         pos = self.rect.move(self.selector.rect.width, 0)
    114117        for option in options:
    115118            if not isinstance(option, tuple):
     
    117120            self.options.append(option)
    118121            text, data = option
    119             t = Text(text, pos, **kwargs)
    120             pos = pos.move(0, t.rect.height)
     122            t = Text(text, pos.move(0, 0), **kwargs)
    121123            self.option_widgets.append(t)
    122124
     
    124126                             ) + self.selector.rect.width
    125127        self.rect.height = sum(line.rect.height for line in self.option_widgets)
     128
     129    def _update_rects(self):
     130        pos = self.rect.move(self.selector.rect.width, 0)
     131        for t in self.option_widgets:
     132            t.rect.topleft = pos.topleft
     133            pos = pos.move(0, t.rect.height)
     134        self.selector.rect.top = self.option_widgets[self.selected].rect.top
     135        self.selector.rect.left = self.rect.left
    126136
    127137    def dispatch(self, ev):
     
    136146
    137147        self.selected %= len(self.option_widgets)
    138         self.selector.rect.top = self.option_widgets[self.selected].rect.top
    139148
    140149    def draw(self, surface):
     150        self._update_rects()
    141151        for option in self.option_widgets:
    142152            option.draw(surface)
Note: See TracChangeset for help on using the changeset viewer.