From: Stefano Rivera Date: Sun, 11 May 2014 19:00:58 +0000 (+0200) Subject: Scenes get a Container for widgets X-Git-Tag: 0.1~392^2~4 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=5331699e9ab927608ad9fa6841de85a30b1c42b1;p=naja.git Scenes get a Container for widgets --- diff --git a/naja/scenes/credits.py b/naja/scenes/credits.py index a1c7d8f..747dd01 100644 --- a/naja/scenes/credits.py +++ b/naja/scenes/credits.py @@ -15,14 +15,11 @@ class CreditsScene(Scene): 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) diff --git a/naja/scenes/game.py b/naja/scenes/game.py index 9d8b290..32285cd 100644 --- a/naja/scenes/game.py +++ b/naja/scenes/game.py @@ -19,12 +19,12 @@ class GameScene(Scene): 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) diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index a73c2ec..46b2b3b 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -16,15 +16,12 @@ class MenuScene(Scene): 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() diff --git a/naja/scenes/scene.py b/naja/scenes/scene.py index ce0b684..4841ab5 100644 --- a/naja/scenes/scene.py +++ b/naja/scenes/scene.py @@ -2,6 +2,8 @@ Base Scene class. """ +from naja.widgets.base import Container + class Scene(object): """ @@ -11,7 +13,10 @@ 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 @@ -19,12 +24,17 @@ class Scene(object): 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 diff --git a/naja/widgets/base.py b/naja/widgets/base.py index 32ff46f..6b86a87 100644 --- a/naja/widgets/base.py +++ b/naja/widgets/base.py @@ -22,3 +22,26 @@ class Widget(object): 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