@classmethod
def post(cls):
super(InvalidateTheWorld, cls).post()
+
+
+class SelectEvent(NajaEvent):
+ TYPE = "SELECT"
+
+ @classmethod
+ def post(cls):
+ super(SelectEvent, cls).post()
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
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:
import pygame
from pygame import locals as pgl
-from naja.events import InvalidateTheWorld
+from naja.events import InvalidateTheWorld, SelectEvent
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
--- /dev/null
+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)