From c99b694a2a9e1237a402c0989d9f55e394c56cfe Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 8 Sep 2016 14:07:29 +0200 Subject: [PATCH] Add name to buttons to simplify logic --- tabakrolletjie/scenes/day.py | 5 +++-- tabakrolletjie/scenes/menu.py | 17 ++++++++--------- tabakrolletjie/widgets.py | 12 ++++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tabakrolletjie/scenes/day.py b/tabakrolletjie/scenes/day.py index 184c301..7702fc7 100644 --- a/tabakrolletjie/scenes/day.py +++ b/tabakrolletjie/scenes/day.py @@ -22,7 +22,8 @@ class DayScene(BaseScene): self._lights = LightManager(self._space, gamestate) # Toolbar self._tools = [ - ImageButton('32', 'seed.png', pos=(50, SCREEN_SIZE[1] - 40)), + ImageButton('32', 'seed.png', name='seed', + pos=(50, SCREEN_SIZE[1] - 40)), ] @debug_timer("day.render") @@ -44,7 +45,7 @@ class DayScene(BaseScene): # Check tools for tool in self._tools: if tool.pressed(ev): - print 'tool' + print 'tool', tool.name return # Not tool, so check lights self._lights.toggle_nearest(ev.pos, surfpos=True) diff --git a/tabakrolletjie/scenes/menu.py b/tabakrolletjie/scenes/menu.py index e870054..1d14abc 100644 --- a/tabakrolletjie/scenes/menu.py +++ b/tabakrolletjie/scenes/menu.py @@ -17,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)) - 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)), - } + self._menu = [ + TextButton("Load Level", (255, 255, 255), name='load level'), + TextButton("Start Game (Day)", (255, 255, 255), name='start game'), + TextButton("Load Saved Game", (255, 255, 255), name='load game'), + ] def render(self, surface, gamestate): surface.fill((0, 128, 0)) @@ -30,8 +30,7 @@ class MenuScene(BaseScene): surface.blit(self._title, pos, None) height = 150 - for label in sorted(self._menu): - item = self._menu[label] + for item in self._menu: if not item.position: item.position = ((surface.get_width() - item.get_width()) / 2, height) @@ -39,9 +38,9 @@ class MenuScene(BaseScene): height += item.get_height() + 30 def _get_pressed(self, ev): - for label, button in self._menu.items(): + for button in self._menu: if button.pressed(ev): - return label + return button.name return None def _do_day(self): diff --git a/tabakrolletjie/widgets.py b/tabakrolletjie/widgets.py index 3f37b52..f34fa35 100644 --- a/tabakrolletjie/widgets.py +++ b/tabakrolletjie/widgets.py @@ -10,10 +10,11 @@ from .constants import FONTS class Button(object): - def __init__(self, size, pos=None, padding=10): + def __init__(self, size, name=None, pos=None, padding=10): self._size = size self._padding = padding self.position = pos + self.name = name @property def position(self): @@ -50,10 +51,11 @@ class Button(object): class TextButton(Button): - def __init__(self, text, colour, pos=None, padding=10): + def __init__(self, text, colour, name=None, 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) + super(TextButton, self).__init__(self._text.get_size(), name, + pos, padding) def render(self, surface): surface.blit(self._text, self._pos, None) @@ -63,9 +65,11 @@ class ImageButton(Button): def __init__(self, *imgparts, **kwargs): self._img = loader.load_image(*imgparts) + name = kwargs.get('name') pos = kwargs.get('pos') padding = kwargs.get('padding', 0) - super(ImageButton, self).__init__(self._img.get_size(), pos, padding) + super(ImageButton, self).__init__(self._img.get_size(), name, + pos, padding) def render(self, surface): surface.blit(self._img, self._pos, None) -- 2.34.1