Changeset 247:8c0c132b422f


Ignore:
Timestamp:
04/07/11 17:25:48 (12 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Phase:
public
Message:

Button widget for cutscene

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/cutscene.py

    r243 r247  
    77from . import data
    88from .engine import ChangeScene, Scene
    9 from .widgets.text import Text, unindent_text
     9from .widgets.text import Text, ButtonSet, TextButton, unindent_text
    1010
    1111class CutScene(Scene):
    1212    def __init__(self, game_state, text, background, music=None):
    1313        super(CutScene, self).__init__(game_state)
    14         self.text = text
    15         self.text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40),
    16                                 size=24, shadow='gray', wrap=True)
    1714        self.background = data.load_image('backgrounds/' + background)
    1815        self.start_time = pygame.time.get_ticks()
    1916        self.run_time = 60000 # ms
     17
    2018        self._background_music = None
    2119        if music and pygame.mixer.get_init():
    2220            self._background_music = data.filepath(music)
     21
     22        text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40),
     23                           size=24, shadow='gray', wrap=True)
     24        self.widgets.append(text_widget)
     25
     26        button_set = ButtonSet()
     27        button_set.append(TextButton("Continue", (300, 500), size=24, color='yellow'))
     28        button_set.callbacks.append(self.done)
     29        self.widgets.append(button_set)
     30
     31    def done(self, selected=None, data=None):
     32        # Avoid circular import...
     33        from .menuscene import MenuScene
     34        ChangeScene.post(MenuScene(self.game_state))
    2335
    2436    def draw(self, surface, engine):
     
    3547                     pygame.Rect((pos, bottom), viewport.size))
    3648
    37         self.text_widget.draw(surface)
    3849        super(CutScene, self).draw(surface, engine)
    3950
     
    4152        if ev.type is KEYDOWN:
    4253            if ev.key in(K_q, K_ESCAPE):
    43                 # Avoid circular import...
    44                 from .menuscene import MenuScene
    45                 ChangeScene.post(MenuScene(self.game_state))
     54                self.done()
    4655        super(CutScene, self).dispatch(ev)
    4756
  • skaapsteker/widgets/text.py

    r183 r247  
    11# -*- coding: utf-8 -*-
    22
     3import collections
    34import os.path
    45
     
    144145            option.draw(surface)
    145146        self.selector.draw(surface)
     147
     148
     149class TextButton(Widget):
     150    def __init__(self, text, pos, margin=10, **kwargs):
     151        textpos = pygame.Rect((pos[0] + margin, pos[1] + margin), (0, 0))
     152        self.text = Text(text, textpos, **kwargs)
     153        self.rect = pygame.Rect(pos, (0, 0))
     154        self.rect.width = self.text.rect.width + 2 * margin
     155        self.rect.height = self.text.rect.height + 2 * margin
     156
     157    def draw(self, surface, selected=False):
     158        self.text.draw(surface)
     159        pygame.draw.rect(surface, self.text.color, self.rect, 1)
     160
     161
     162class ButtonSet(Widget):
     163    def __init__(self):
     164        self.options = []
     165        self.selected = 0
     166        self.callbacks = []
     167
     168    def append(self, widget, data=None):
     169        self.options.append((widget, data))
     170
     171    def dispatch(self, ev):
     172        if ev.type is KEYDOWN:
     173            if ev.key == K_UP:
     174                self.selected -= 1
     175            elif ev.key == K_DOWN:
     176                self.selected += 1
     177            elif ev.key == K_RETURN:
     178                for callback in self.callbacks:
     179                    callback(self.selected, self.options[self.selected][1])
     180
     181    def draw(self, surface):
     182        for i, widget in enumerate(self.options):
     183            widget[0].draw(surface, i == self.selected)
Note: See TracChangeset for help on using the changeset viewer.