Mercurial > skaapsteker
view skaapsteker/widgets/bubble.py @ 309:342e30c92a85
Check whether the new state has text after a state change. If not, close the dialogue.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 00:20:11 +0200 |
parents | 04be4219742b |
children | af021507a7e3 |
line wrap: on
line source
"""Widget for in-level dialogue / speech bubbles.""" from pygame.locals import (KEYDOWN, K_UP, K_p, K_q, K_x, K_z, K_RETURN, BLEND_ADD) import pygame.draw import pygame from ..engine import OpenDialog, CloseDialog from .text import Text, TextChoice class DialogueWidget(object): def __init__(self, npc): self.npc = npc self.dsm = npc.dsm self._text = None self._text_choice = None self._state_update() def _state_update(self): pos = pygame.Rect((0, 0), (300, 1)) state = self.dsm.get_state() if state.text: 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] if state.auto_next: text = state.auto_next_text if state.auto_next_text else "Next" options.append((text, "N")) else: 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: if data == "N": self.dsm.auto_next() else: self.dsm.choice(i) if self.dsm.has_text(): self._state_update() else: CloseDialog.post(self.npc) def draw(self, level_surface): if self._text: self._text.rect.center = level_surface.get_clip().center self._text_choice.rect.topleft = self._text.rect.bottomleft self._text_choice.rect.move_ip((0, 10)) bgrect = self._text.rect.union(self._text_choice.rect) bgrect.inflate_ip(10, 10) level_surface.fill((255, 255, 255), bgrect) pygame.draw.rect(level_surface, (0, 0, 0), bgrect, 1) self._text.draw(level_surface) self._text_choice.draw(level_surface) def close(self): pass def dispatch(self, ev): self._text_choice.dispatch(ev)