Changeset 79:fe8652a060df


Ignore:
Timestamp:
Apr 3, 2011, 10:46:13 PM (9 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

Simple menu screen and TextChoice? widget

Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • scripts/level-editor

    r75 r79  
    6262        text = Text("Level: %s.\nOffset: %r Tile: %r\nFPS: %.1f"
    6363                    % (levelname, position, tilepos, sum(fpss)/len(fpss)),
     64                    (10, 10),
    6465                    color='white')
    65         text.draw(screen_surface, (10, 10))
     66        text.draw(screen_surface)
    6667
    6768        pygame.display.flip()
  • skaapsteker/__init__.py

    r0 r79  
    1 if __name__ == "__main__":
    2     import main
    3     main.main()
  • skaapsteker/__main__.py

    r56 r79  
    22   """
    33
    4 from constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
    5 from engine import Engine
    6 from levelscene import LevelScene
     4from .constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
     5from .engine import Engine
     6from .levelscene import LevelScene
     7from .menuscene import MenuScene
    78
    89import pygame
     
    4950
    5051    level = getattr(opts, 'level', None)
    51     level = "level1" if level is None else level
    5252
    5353    engine = Engine()
    54     engine.change_scene(LevelScene(level))
     54    if level is not None:
     55        engine.change_scene(LevelScene(level))
     56    else:
     57        engine.change_scene(MenuScene())
    5558    try:
    5659        engine.run()
     
    5861        pass
    5962
    60  
     63
    6164if __name__ == '__main__':
    6265    main()
  • skaapsteker/engine.py

    r14 r79  
    4040
    4141    def __init__(self):
    42         pass
     42        self.widgets = []
    4343
    4444    def post(self, ev):
     
    5656    def dispatch(self, ev):
    5757        """Dispatch an event."""
    58         pass
     58        for widget in self.widgets:
     59            widget.dispatch(ev)
    5960
    6061    def draw(self, surface):
    6162        """Update the scene surface."""
    62         pass
     63        for widget in self.widgets:
     64            widget.draw(surface)
    6365
    6466
  • skaapsteker/widgets/__init__.py

    r45 r79  
     1class Widget(object):
     2    def __init__(self):
     3        pass
     4
     5    def dispatch(self, event):
     6        pass
     7
     8    def draw(self, surface):
     9        pass
  • skaapsteker/widgets/text.py

    r46 r79  
     1# -*- coding: utf-8 -*-
     2
    13import os.path
    24
    35import pygame
     6from pygame.locals import KEYDOWN, K_UP, K_DOWN, K_RETURN
    47
    5 import skaapsteker.data
     8from ..widgets import Widget
     9from ..data import filepath
    610
    711fonts = {
     
    1115loaded_fonts = {}
    1216
    13 class Text(object):
    14     def __init__(self, text, font='sans', size=16, color='black'):
     17def load_font(name, size):
     18    if (name, size) not in loaded_fonts:
     19        fontfn = filepath('fonts/' + fonts[name])
     20        fonts[(name, size)] = pygame.font.Font(fontfn, size)
     21    return fonts[(name, size)]
     22
     23
     24class Text(Widget):
     25    def __init__(self, text, pos, font='sans', size=16, color='black'):
    1526        self.text = text
     27        if isinstance(pos, pygame.Rect):
     28            self.rect = pos
     29        else:
     30            self.rect = pygame.Rect(pos, (0, 0))
    1631        self.font = load_font(font, size)
    1732        self.color = pygame.Color(color)
     33        # TODO: Wrapping
    1834        self.surfaces = [self.font.render(line, True, self.color)
    1935                         for line in self.text.split('\n')]
     36        self.rect.width = max(line.get_width() for line in self.surfaces)
     37        self.rect.height = self.font.get_linesize() * len(self.surfaces)
    2038
    21     def draw(self, surface, pos):
    22         if isinstance(pos, pygame.Rect):
    23             pos = pos.copy()
    24         else:
    25             pos = pygame.Rect(pos, (0, 0))
    26 
     39    def draw(self, surface):
     40        pos = self.rect.copy()
    2741        for line in self.surfaces:
    2842            surface.blit(line, pos)
    2943            pos.move_ip(0, self.font.get_linesize())
    3044
    31 def load_font(name, size):
    32     if (name, size) not in loaded_fonts:
    33         fontfn = skaapsteker.data.filepath('fonts/' + fonts[name])
    34         fonts[(name, size)] = pygame.font.Font(fontfn, size)
    35     return fonts[(name, size)]
     45
     46class TextChoice(Widget):
     47    def __init__(self, options, pos, **kwargs):
     48        self.options = []
     49        self.rect = pygame.Rect(pos, (0, 0))
     50        self.selector = Text(u'» ', self.rect.copy(), **kwargs)
     51        self.selected = 0
     52        self.callbacks = []
     53
     54        pos = self.rect.move(self.selector.rect.width, 0)
     55        for option in options:
     56            t = Text(option, pos, **kwargs)
     57            pos = pos.move(0, t.rect.height)
     58            self.options.append(t)
     59
     60    def dispatch(self, ev):
     61        if ev.type is KEYDOWN:
     62            if ev.key == K_UP:
     63                self.selected -= 1
     64            elif ev.key == K_DOWN:
     65                self.selected += 1
     66            elif ev.key == K_RETURN:
     67                all(callback(self.selected) for callback in self.callbacks)
     68
     69        self.selected %= len(self.options)
     70        self.selector.rect.top = self.options[self.selected].rect.top
     71
     72    def draw(self, surface):
     73        for option in self.options:
     74            option.draw(surface)
     75        self.selector.draw(surface)
Note: See TracChangeset for help on using the changeset viewer.