def __init__(self, state):
super(CreditsScene, self).__init__(state)
- self.widgets.append(TextWidget((60, 10), 'Credits', fontsize=32,
- colour='white'))
- self.widgets.append(TextWidget((60, 30),
- 'Your mom\n'
- 'A stranger', fontsize=32,
- colour='white'))
+ self.add(TextWidget((60, 10), 'Credits', fontsize=32, colour='white'))
+ self.add(TextWidget((60, 30), 'Your mom\nA stranger', fontsize=32,
+ colour='white'))
- def handle_event(self, ev):
+ def handle_scene_event(self, ev):
if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
from naja.scenes.menu import MenuScene
SceneChangeEvent.post(MenuScene)
def __init__(self, state):
super(GameScene, self).__init__(state)
- self.widgets.append(PlayerBitsWidget((0, 0)))
- self.widgets.append(BoardWidget((0, 60)))
- self.widgets.append(GameBitsWidget((0, 540)))
- self.widgets.append(InfoAreaWidget((480, 0)))
+ self.add(PlayerBitsWidget((0, 0)))
+ self.add(BoardWidget((0, 60)))
+ self.add(GameBitsWidget((0, 540)))
+ self.add(InfoAreaWidget((480, 0)))
- def handle_event(self, ev):
+ def handle_scene_event(self, ev):
from naja.scenes.menu import MenuScene
if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE):
SceneChangeEvent.post(MenuScene)
def __init__(self, state):
super(MenuScene, self).__init__(state)
- self.widgets.append(TextWidget((10, 10), 'Game', fontsize=32,
- colour='white'))
- self.widgets.append(TextWidget((10, 40), 'Credits', fontsize=32,
- colour='white'))
- self.widgets.append(TextWidget((10, 70), 'Quit', fontsize=32,
- colour='white'))
+ self.add(TextWidget((10, 10), 'Game', fontsize=32, colour='white'))
+ self.add(TextWidget((10, 40), 'Credits', fontsize=32, colour='white'))
+ self.add(TextWidget((10, 70), 'Quit', fontsize=32, colour='white'))
self.pos = 0
- def handle_event(self, ev):
+ def handle_scene_event(self, ev):
if ev.type == pgl.KEYDOWN:
if ev.key in (pgl.K_q, pgl.K_ESCAPE):
QuitGameEvent.post()
Base Scene class.
"""
+from naja.widgets.base import Container
+
class Scene(object):
"""
"""
def __init__(self, state):
self.state = state
- self.widgets = []
+ self.container = Container()
+
+ def add(self, widget):
+ self.container.add(widget)
def enter(self):
pass
def exit(self):
pass
- def render(self, surface):
- self.render_widgets(surface)
+ def render_scene(self, surface):
+ pass
- def render_widgets(self, surface):
- for widget in self.widgets:
- widget.render(surface)
+ def render(self, surface):
+ self.render_scene(surface)
+ self.container.render(surface)
def handle_event(self, ev):
+ if self.container.handle_event(ev):
+ return
+ self.handle_scene_event(ev)
+
+ def handle_scene_event(ev):
pass
def prepare(self):
raise NotImplemented()
+
+ def handle_event(self, ev):
+ return False
+
+
+class Container(object):
+ def __init__(self, *widgets):
+ self.widgets = []
+ for widget in widgets:
+ self.add(widget)
+
+ def add(self, widget):
+ self.widgets.append(widget)
+
+ def render(self, surface):
+ for widget in self.widgets:
+ widget.render(surface)
+
+ def handle_event(self, ev):
+ for widget in self.widgets:
+ if widget.handle_event(ev):
+ return True
+ return False