view skaapsteker/widgets/bubble.py @ 291:04be4219742b

Add support for auto_next_text to give the auto_next effect but with a different prompt.
author Simon Cross <hodgestar@gmail.com>
date Fri, 08 Apr 2011 21:58:35 +0200
parents 71f15f6e9274
children 342e30c92a85
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)
        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)