From 2a9f5c76556f00f631ea10a6cba44a92bdd27512 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sun, 11 May 2014 22:45:31 +0200 Subject: [PATCH] SelectorWidget --- naja/events.py | 8 ++++++++ naja/scenes/menu.py | 9 ++++++--- naja/widgets/base.py | 4 ++-- naja/widgets/selector.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 naja/widgets/selector.py diff --git a/naja/events.py b/naja/events.py index 0e102ed..e34f740 100644 --- a/naja/events.py +++ b/naja/events.py @@ -45,3 +45,11 @@ class InvalidateTheWorld(NajaEvent): @classmethod def post(cls): super(InvalidateTheWorld, cls).post() + + +class SelectEvent(NajaEvent): + TYPE = "SELECT" + + @classmethod + def post(cls): + super(SelectEvent, cls).post() diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index 317e50f..6b4152d 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -6,6 +6,7 @@ import pygame.locals as pgl from naja.scenes.scene import Scene from naja.widgets.text import TextWidget +from naja.widgets.selector import SelectorWidget from naja.events import QuitGameEvent from naja.scenes.credits import CreditsScene from naja.scenes.game import GameScene @@ -15,17 +16,19 @@ from naja.events import SceneChangeEvent class MenuScene(Scene): def __init__(self, state): super(MenuScene, self).__init__(state) + selector = SelectorWidget() + self.add(selector) run_game = TextWidget((100, 100), 'Game', fontsize=32, colour='white') run_game.add_callback('click', lambda event: SceneChangeEvent.post(GameScene)) - self.add(run_game) + selector.add(run_game) credits = TextWidget((100, 200), 'Credits', fontsize=32, colour='white') credits.add_callback('click', lambda event: SceneChangeEvent.post(CreditsScene)) - self.add(credits) + selector.add(credits) quit = TextWidget((100, 300), 'Quit', fontsize=32, colour='white') quit.add_callback('click', lambda event: QuitGameEvent.post()) - self.add(quit) + selector.add(quit) def handle_scene_event(self, ev): if ev.type == pgl.KEYDOWN: diff --git a/naja/widgets/base.py b/naja/widgets/base.py index 81f11b6..c820261 100644 --- a/naja/widgets/base.py +++ b/naja/widgets/base.py @@ -3,7 +3,7 @@ from collections import defaultdict import pygame from pygame import locals as pgl -from naja.events import InvalidateTheWorld +from naja.events import InvalidateTheWorld, SelectEvent class Widget(object): @@ -38,7 +38,7 @@ class Widget(object): # True for for this event self._prepared = False return False - if ev.type == pgl.MOUSEBUTTONDOWN: + if SelectEvent.matches(ev) or ev.type == pgl.MOUSEBUTTONDOWN: self.callback('click') return False diff --git a/naja/widgets/selector.py b/naja/widgets/selector.py new file mode 100644 index 0000000..c0c91b9 --- /dev/null +++ b/naja/widgets/selector.py @@ -0,0 +1,35 @@ +import pygame.locals as pgl + +from naja.widgets.base import Container +from naja.resources import resources +from naja.resources.mutators import R270 + + +class SelectorWidget(Container): + def __init__(self, *args, **kwargs): + super(SelectorWidget, self).__init__(*args, **kwargs) + self.position = 0 + self.selector = resources.get_image('bits', 'arrow_on.png', + transforms=(R270,)) + + def render(self, surface): + super(SelectorWidget, self).render(surface) + pos = self.selector.get_rect() + selected = self.widgets[self.position] + pos = pos.move(selected.pos) + pos = pos.move(-pos.width * 1.5, (selected.size[1] - pos.height) / 2) + surface.blit(self.selector, pos) + + def handle_event(self, ev): + if ev.type == pgl.KEYDOWN: + if ev.key in (pgl.K_DOWN, pgl.K_UP): + if ev.key == pgl.K_DOWN: + self.position += 1 + else: + self.position -= 1 + self.position %= len(self.widgets) + return True + elif ev.key == pgl.K_RETURN: + return self.widgets[self.position].callback('click') + + return super(SelectorWidget, self).handle_event(ev) -- 2.34.1