Mercurial > skaapsteker
view skaapsteker/widgets/bubble.py @ 285:71f15f6e9274
Hook up auto_next dialogue events.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Fri, 08 Apr 2011 20:52:47 +0200 |
parents | 030387133ace |
children | 04be4219742b |
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: options.append(("Next", "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) elif data == "N": self.dsm.auto_next() self._state_update() else: self.dsm.choice(i) self._state_update() 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)