# HG changeset patch # User Jeremy Thurgood # Date 1329219545 -7200 # Node ID d6ded808cc339696bf8f0f7daa5c14a3763e2a27 # Parent 60bf20849231a34dc6d9305ad10cc6018a0a7677 Much scene management refactoring. diff -r 60bf20849231 -r d6ded808cc33 gamelib/scenes/cryo.py --- a/gamelib/scenes/cryo.py Tue Feb 14 13:05:10 2012 +0200 +++ b/gamelib/scenes/cryo.py Tue Feb 14 13:39:05 2012 +0200 @@ -361,7 +361,7 @@ " other side is preventing it from opening completely.", soundfile='chain.ogg') else: - self.game.set_current_scene('map') + self.game.change_scene('map') return None def interact_default(self, item): diff -r 60bf20849231 -r d6ded808cc33 gamelib/scenes/game_widgets.py --- a/gamelib/scenes/game_widgets.py Tue Feb 14 13:05:10 2012 +0200 +++ b/gamelib/scenes/game_widgets.py Tue Feb 14 13:39:05 2012 +0200 @@ -21,7 +21,7 @@ def interact_without(self): """Go to map.""" - self.game.set_current_scene("map") + self.game.change_scene("map") def get_description(self): return 'An open doorway leads to the rest of the ship.' diff -r 60bf20849231 -r d6ded808cc33 gamelib/scenes/map.py --- a/gamelib/scenes/map.py Tue Feb 14 13:05:10 2012 +0200 +++ b/gamelib/scenes/map.py Tue Feb 14 13:39:05 2012 +0200 @@ -57,7 +57,7 @@ def interact(self, _item): """Go to destination.""" if self.DEST in self.game.scenes: - self.game.set_current_scene(self.DEST) + self.game.change_scene(self.DEST) class ToCryo(DoorThing): diff -r 60bf20849231 -r d6ded808cc33 gamelib/tests/game_logic_utils.py --- a/gamelib/tests/game_logic_utils.py Tue Feb 14 13:05:10 2012 +0200 +++ b/gamelib/tests/game_logic_utils.py Tue Feb 14 13:39:05 2012 +0200 @@ -21,7 +21,7 @@ def setUp(self): self.state = state.initial_state() - self.state.set_current_scene(self.CURRENT_SCENE) + self.state.change_scene(self.CURRENT_SCENE) def tearDown(self): for item in self.state.items.values(): diff -r 60bf20849231 -r d6ded808cc33 pyntnclick/cursor.py --- a/pyntnclick/cursor.py Tue Feb 14 13:05:10 2012 +0200 +++ b/pyntnclick/cursor.py Tue Feb 14 13:39:05 2012 +0200 @@ -3,7 +3,6 @@ # Sprite Cursor from pygame.sprite import Sprite, RenderUpdates -from pygame.rect import Rect import pygame import pygame.color import pygame.cursors @@ -90,11 +89,4 @@ self._cursor_group.add(self._loaded_cursor) def cursor_highlight(self): - #XXX: if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()): - #XXX: return False - #XXX: if self.game.highlight_override: - #XXX: return True - current_thing = self.game.current_thing - if current_thing: - return current_thing.is_interactive() - return False + return self.container.mouseover_widget.highlight_cursor diff -r 60bf20849231 -r d6ded808cc33 pyntnclick/gamescreen.py --- a/pyntnclick/gamescreen.py Tue Feb 14 13:05:10 2012 +0200 +++ b/pyntnclick/gamescreen.py Tue Feb 14 13:39:05 2012 +0200 @@ -78,7 +78,6 @@ super(InventoryView, self).__init__(rect, gd) self.screen = screen self.game = screen.game - self.scene_widget = screen.scene_widget slots = (self.rect.width - self.MIN_UPDOWN_WIDTH) / self.bsize self.slots = [self.add(self.make_slot(i)) for i in range(slots)] @@ -148,6 +147,7 @@ def __init__(self, rect, gd, scene, screen, is_detail=False): super(SceneWidget, self).__init__(rect, gd) + self.name = scene.NAME self.scene = scene self.screen = screen self.game = screen.game @@ -163,13 +163,19 @@ def draw(self, surface): self.scene.draw(surface.subsurface(self.rect)) - self.scene.draw_description(surface) + if self.parent.is_top(self): + self.scene.draw_description(surface) super(SceneWidget, self).draw(surface) if self.is_detail: border = self.rect.inflate(self.DETAIL_BORDER, self.DETAIL_BORDER) pygame.draw.rect( surface, self.DETAIL_BORDER_COLOR, border, self.DETAIL_BORDER) + @property + def highlight_cursor(self): + return (self.scene.current_thing + and self.scene.current_thing.is_interactive()) + def queue_widget(self, widget): self._message_queue.append(widget) @@ -179,7 +185,7 @@ self.game.cancel_doodah(self.screen) else: pos = self.global_to_local(event.pos) - result = self.game.interact(pos) + result = self.scene.interact(self.game.tool, pos) handle_result(result, self) def animate(self): @@ -188,8 +194,8 @@ # self.invalidate() # We do this here so we can get enter and leave events regardless # of what happens - result = self.game.check_enter_leave(self.screen) - handle_result(result, self) + # result = self.game.check_enter_leave(self.screen) + # handle_result(result, self) if self._message_queue: # Only add a message if we're at the top if self.screen.screen_modal.is_top(self.screen.inner_container): @@ -220,15 +226,9 @@ # according self.queue_widget(widget) - def show_detail(self, detail): - self.screen.show_detail(detail) - def close(self, event, widget): self.screen.close_detail(self) - def end_game(self): - self.screen.change_screen('end') - class ToolBar(Container): def __init__(self, rect, gd, screen): @@ -284,21 +284,15 @@ self.gd.running = False self.create_initial_state = self.gd.initial_state self.container.add_callback(KEYDOWN, self.key_pressed) - self.scene_widget = None def _clear_all(self): for widget in self.container.children[:]: self.container.remove(widget) def process_event(self, event_name, data): - if event_name == 'restart': - self.start_game() - elif event_name == 'inventory': - self.inventory.update_slots() - elif event_name == 'change_scene': - self.change_scene(data) + getattr(self, 'game_event_%s' % event_name, lambda d: None)(data) - def start_game(self): + def game_event_restart(self, data): self._clear_all() self.game = self.create_initial_state() @@ -313,29 +307,48 @@ self.scene_modal = self.inner_container.add( ModalStackContainer(rect, self.gd)) - self.change_scene(None) - self.toolbar = self.inner_container.add( ToolBar((0, rect.height), self.gd, self)) self.inventory = self.toolbar.inventory + self.game.change_scene self.gd.running = True + def game_event_inventory(self, data): + self.inventory.update_slots() + + def game_event_change_scene(self, data): + scene_name = data['name'] + if data['detail']: + self.show_detail(scene_name) + else: + self.change_scene(scene_name) + def change_scene(self, scene_name): - self.scene_modal.remove_all() - self.scene_widget = self.scene_modal.add( - SceneWidget(self.scene_modal.rect.copy(), self.gd, - self.game.current_scene, self)) + for scene_widget in reversed(self.scene_modal.children[:]): + self.scene_modal.remove(scene_widget) + scene_widget.scene.leave() + scene = self.game.scenes[scene_name] + self.game.current_scene = scene + self._add_scene(scene) def show_detail(self, detail_name): - detail = self.game.set_current_detail(detail_name) - detail_rect = Rect((0, 0), detail.get_detail_size()) - detail_rect.center = self.scene_modal.rect.center - self.scene_modal.add( - SceneWidget(detail_rect, self.gd, detail, self, is_detail=True)) + self._add_scene(self.game.detail_views[detail_name], True) + + def _add_scene(self, scene, detail=False): + rect = self.scene_modal.rect.copy() + if detail: + rect = Rect((0, 0), scene.get_detail_size()) + rect.center = self.scene_modal.rect.center + + scene_widget = self.scene_modal.add( + SceneWidget(rect, self.gd, scene, self, detail)) + + handle_result(scene.enter(), scene_widget) def close_detail(self, detail): self.scene_modal.remove(detail) + detail.scene.leave() def animate(self): """Animate the scene widgets""" diff -r 60bf20849231 -r d6ded808cc33 pyntnclick/main.py --- a/pyntnclick/main.py Tue Feb 14 13:05:10 2012 +0200 +++ b/pyntnclick/main.py Tue Feb 14 13:39:05 2012 +0200 @@ -80,8 +80,7 @@ initial_state.set_debug_rects(self._debug_rects) for scene in self._scene_list: initial_state.load_scenes(scene) - initial_state.set_current_scene(self._initial_scene) - initial_state.set_do_enter_leave() + initial_state.change_scene(self._initial_scene) return initial_state def game_constants(self): diff -r 60bf20849231 -r d6ded808cc33 pyntnclick/state.py --- a/pyntnclick/state.py Tue Feb 14 13:05:10 2012 +0200 +++ b/pyntnclick/state.py Tue Feb 14 13:39:05 2012 +0200 @@ -41,11 +41,7 @@ if self.message: scene_widget.show_message(self.message) if self.detail_view: - scene_widget.show_detail(self.detail_view) - if (self.close_detail - and hasattr(scene_widget, 'parent') - and hasattr(scene_widget.parent, 'clear_detail')): - scene_widget.parent.clear_detail() + scene_widget.game.show_detail(self.detail_view) if self.end_game: scene_widget.end_game() @@ -116,16 +112,6 @@ self.data = GameState() # current scene self.current_scene = None - # current detail view - self.current_detail = None - # scene we came from, for enter and leave processing - self.previous_scene = None - # scene transion helpers - self.do_check = None - self.old_pos = None - # current thing - self.current_thing = None - self.highlight_override = False # debug rects self.debug_rects = False @@ -157,22 +143,13 @@ scene = scene_cls(self) self.add_detail_view(scene) - def set_current_scene(self, name): - old_scene = self.current_scene - self.current_scene = self.scenes[name] - self.current_thing = None - if old_scene and old_scene != self.current_scene: - self.previous_scene = old_scene - self.set_do_enter_leave() - ScreenEvent.post('game', 'change_scene', name) + def change_scene(self, name): + ScreenEvent.post('game', 'change_scene', + {'name': name, 'detail': False}) - def set_current_detail(self, name): - self.current_thing = None - if name is None: - self.current_detail = None - else: - self.current_detail = self.detail_views[name] - return self.current_detail + def show_detail(self, name): + ScreenEvent.post('game', 'change_scene', + {'name': name, 'detail': True}) def _update_inventory(self): ScreenEvent.post('game', 'inventory', None) @@ -208,12 +185,6 @@ def set_tool(self, item): self.tool = item - def interact(self, pos): - return self.current_scene.interact(self.tool, pos) - - def interact_detail(self, pos): - return self.current_detail.interact(self.tool, pos) - def cancel_doodah(self, screen): if self.tool: self.set_tool(None) @@ -222,38 +193,10 @@ #elif self.current_detail: # screen.state_widget.clear_detail() - def do_enter_detail(self): - if self.current_detail: - self.current_detail.enter() - - def do_leave_detail(self): - if self.current_detail: - self.current_detail.leave() - def animate(self): if not self.do_check: return self.current_scene.animate() - def check_enter_leave(self, screen): - if not self.do_check: - return None - if self.do_check == self.gd.constants.leave: - self.do_check = self.gd.constants.enter - if self.previous_scene: - return self.previous_scene.leave() - return None - elif self.do_check == self.gd.constants.enter: - self.do_check = None - # Fix descriptions, etc. - if self.old_pos: - self.current_scene.update_current_thing(self.old_pos) - return self.current_scene.enter() - raise RuntimeError('invalid do_check value %s' % self.do_check) - - def set_do_enter_leave(self): - """Flag that we need to run the enter loop""" - self.do_check = self.gd.constants.leave - class GameDeveloperGizmo(object): """Base class for objects game developers see.""" @@ -328,6 +271,7 @@ self.state_key = self.name # map of thing names -> Thing objects self.things = {} + self.current_thing = None self._background = None def set_game(self, game): @@ -344,13 +288,13 @@ def remove_thing(self, thing): del self.things[thing.name] - if thing is self.game.current_thing: - self.game.current_thing.leave() - self.game.current_thing = None + if thing is self.current_thing: + self.current_thing.leave() + self.current_thing = None def _get_description(self, dest_rect): - text = (self.game.current_thing and - self.game.current_thing.get_description()) + text = (self.current_thing and + self.current_thing.get_description()) if text is None: return None label = LabelWidget((0, 10), self.gd, text) @@ -389,8 +333,8 @@ Returns a Result object to provide feedback to the player. """ - if self.game.current_thing is not None: - return self.game.current_thing.interact(item) + if self.current_thing is not None: + return self.current_thing.interact(item) def animate(self): """Animate all the things in the scene. @@ -409,14 +353,14 @@ return None def update_current_thing(self, pos): - if self.game.current_thing is not None: - if not self.game.current_thing.contains(pos): - self.game.current_thing.leave() - self.game.current_thing = None + if self.current_thing is not None: + if not self.current_thing.contains(pos): + self.current_thing.leave() + self.current_thing = None for thing in self.things.itervalues(): if thing.contains(pos): thing.enter(self.game.tool) - self.game.current_thing = thing + self.current_thing = thing break def mouse_move(self, pos): diff -r 60bf20849231 -r d6ded808cc33 pyntnclick/widgets/base.py --- a/pyntnclick/widgets/base.py Tue Feb 14 13:05:10 2012 +0200 +++ b/pyntnclick/widgets/base.py Tue Feb 14 13:39:05 2012 +0200 @@ -9,6 +9,8 @@ class Widget(object): + highlight_cursor = False + def __init__(self, rect, gd): if not isinstance(rect, pygame.Rect): rect = pygame.Rect(rect, (0, 0)) @@ -19,6 +21,8 @@ self.parent = None self.disabled = False self.callbacks = collections.defaultdict(list) + # To track which widget the mouse is over + self.mouseover_widget = self def add_callback(self, eventtype, callback, *args): self.callbacks[eventtype].append((callback, args)) @@ -64,6 +68,8 @@ class Button(Widget): + highlight_cursor = True + def event(self, ev): if super(Button, self).event(ev): return True @@ -93,10 +99,13 @@ """Push an event down through the tree, and fire our own event as a last resort """ + self.mouseover_widget = self if ev.type in (MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN): for child in self.children[:]: if child.rect.collidepoint(ev.pos): - if child.event(ev): + result = child.event(ev) + self.mouseover_widget = child.mouseover_widget + if result: return True else: @@ -137,6 +146,7 @@ def event(self, ev): """Only the topmost child gets events. """ + self.mouseover_widget = self.children[-1].mouseover_widget if self.children[-1].event(ev): return True