From: Simon Cross Date: Sat, 10 Sep 2016 21:26:03 +0000 (+0200) Subject: Allow for centring text buttons and display levels centred. X-Git-Tag: tabakrolletjie-v1.0.0~36 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=30bbdf89429d8669ff1b595171a2a956bed7d377;p=tabakrolletjie.git Allow for centring text buttons and display levels centred. --- diff --git a/tabakrolletjie/scenes/load_level.py b/tabakrolletjie/scenes/load_level.py index 6c219a0..7f3223f 100644 --- a/tabakrolletjie/scenes/load_level.py +++ b/tabakrolletjie/scenes/load_level.py @@ -5,6 +5,7 @@ import os import pygame.locals as pgl from .base import BaseScene +from ..constants import SCREEN_SIZE from ..events import SceneChangeEvent from ..widgets import TextButton from ..loader import loader @@ -20,23 +21,23 @@ class LoadLevelScene(BaseScene): def enter(self, gamestate): """Construct list of stations""" - height = 50 - width = 30 + width = SCREEN_SIZE[0] / 2 + self._title = TextButton( + 'Select a planet:', (255, 255, 255), pos=(width, 75), + size=32, font='bold', centre=True) + height = 150 self._buttons = [] for station in self._list_stations(): title = station["config"]["name"] pos = (width, height) - button = TextButton(title, (255, 255, 255), None, pos) + button = TextButton(title, (255, 255, 255), pos=pos, centre=True) button.station = station - if width < 400: - width += 350 - else: - width = 30 - height += button.get_height() + 20 + height += button.get_height() + 20 self._buttons.append(button) def render(self, surface, gamestate): surface.fill((0, 128, 128)) + self._title.render(surface) for button in self._buttons: button.render(surface) diff --git a/tabakrolletjie/widgets.py b/tabakrolletjie/widgets.py index c1bbc94..043c22e 100644 --- a/tabakrolletjie/widgets.py +++ b/tabakrolletjie/widgets.py @@ -69,9 +69,14 @@ class SpacerButton(Button): class TextButton(Button): - def __init__(self, text, colour, name=None, pos=None, padding=10): - font = loader.load_font(FONTS['sans'], size=24) + def __init__( + self, text, colour, name=None, pos=None, padding=10, size=24, + font='sans', centre=False): + font = loader.load_font(FONTS[font], size=size) self._text = font.render(text, True, colour) + if centre: + w, h = self._text.get_size() + pos = pos[0] - (w / 2), pos[1] - (h / 2) self._disabled_text = self._text.copy() Multiply(colour=(80, 80, 80)).apply(self._disabled_text) super(TextButton, self).__init__(self._text.get_size(), name,