From: Neil Date: Thu, 8 Sep 2016 10:40:24 +0000 (+0200) Subject: widgets start arriving X-Git-Tag: tabakrolletjie-v1.0.0~186 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=64b201d5de529398659af1022cabb985acd52ecc;p=tabakrolletjie.git widgets start arriving --- diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index 0492ae9..e870054 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -6,6 +6,7 @@ from .base import BaseScene from ..events import QuitEvent, SceneChangeEvent from ..loader import loader from ..constants import FONTS +from ..widgets import TextButton class MenuScene(BaseScene): @@ -16,11 +17,11 @@ class MenuScene(BaseScene): font_title = loader.load_font(FONTS['bold'], size=32) self._title = font_title.render('A Game with a title', True, (255, 255, 255)) - font_menu = loader.load_font(FONTS['sans'], size=24) - self._menu = [ - font_menu.render("Load Level", True, (255, 255, 255)), - font_menu.render("Load Saved Game", True, (255, 255, 255)), - ] + self._menu = { + 'load level': TextButton("Load Level", (255, 255, 255)), + 'saved game': TextButton("Load Saved Game", (255, 255, 255)), + 'start game': TextButton("Start Game (Day)", (255, 255, 255)), + } def render(self, surface, gamestate): surface.fill((0, 128, 0)) @@ -29,10 +30,23 @@ class MenuScene(BaseScene): surface.blit(self._title, pos, None) height = 150 - for item in self._menu: - pos = ((surface.get_width() - item.get_width()) / 2, height) - surface.blit(item, pos, None) - height += 50 + for label in sorted(self._menu): + item = self._menu[label] + if not item.position: + item.position = ((surface.get_width() - item.get_width()) / 2, + height) + item.render(surface) + height += item.get_height() + 30 + + def _get_pressed(self, ev): + for label, button in self._menu.items(): + if button.pressed(ev): + return label + return None + + def _do_day(self): + from .day import DayScene + SceneChangeEvent.post(scene=DayScene()) def event(self, ev, gamestate): if ev.type == pgl.KEYDOWN: @@ -42,8 +56,16 @@ class MenuScene(BaseScene): from .night import NightScene SceneChangeEvent.post(scene=NightScene()) elif ev.key == pgl.K_d: - from .day import DayScene - SceneChangeEvent.post(scene=DayScene()) + self._do_day() elif ev.key == pgl.K_l: print "Loading Station Alpha ..." gamestate.load_station("station-alpha.json") + elif ev.type == pgl.MOUSEBUTTONDOWN: + pressed = self._get_pressed(ev) + if pressed == 'load level': + print "Loading Station Alpha ..." + gamestate.load_station("station-alpha.json") + elif pressed == 'start game': + self._do_day() + elif pressed: + print 'Pressed', pressed diff --git a/tabakrolletjie/widgets.py b/tabakrolletjie/widgets.py new file mode 100644 index 0000000..ff854de --- /dev/null +++ b/tabakrolletjie/widgets.py @@ -0,0 +1,59 @@ +# Simple widgets +# This just unifies some logic. +# There is no implied container / window system (yet) + +import pygame.locals as pgl + +from .loader import loader +from .constants import FONTS + + +class Button(object): + + def __init__(self, size, pos=None, padding=10): + self._size = size + self._padding = padding + self.position = pos + + @property + def position(self): + return self._pos + + @position.setter + def position(self, pos): + self._pos = pos + if pos is not None: + self._min_x = pos[0] - self._padding + self._max_x = pos[0] + self._size[0] + self._padding + self._min_y = pos[1] - self._padding + self._max_y = pos[1] + self._size[1] + self._padding + + def get_width(self): + return self._size[0] + + def get_height(self): + return self._size[1] + + def render(self, surface): + pass + + def pressed(self, ev): + if self._pos is None: + # Unplaced buttons can't be pressed + return False + if ev.type == pgl.MOUSEBUTTONDOWN and ev.button == 1: + if self._min_x < ev.pos[0] < self._max_x: + if self._min_y < ev.pos[1] < self._max_y: + return True + return False + + +class TextButton(Button): + + def __init__(self, text, colour, pos=None, padding=10): + font = loader.load_font(FONTS['sans'], size=24) + self._text = font.render(text, True, colour) + super(TextButton, self).__init__(self._text.get_size(), pos, padding) + + def render(self, surface): + surface.blit(self._text, self._pos, None)