changeset 187:fbfd8e748ac0

Remove screen reference from State.
author Jeremy Thurgood <firxen@gmail.com>
date Wed, 25 Aug 2010 16:45:06 +0200
parents f15c4f7c24e6
children aebd4fd5b561
files gamelib/cursor.py gamelib/gamescreen.py gamelib/popupmenu.py gamelib/state.py gamelib/widgets.py
diffstat 5 files changed, 56 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/cursor.py	Wed Aug 25 16:24:55 2010 +0200
+++ b/gamelib/cursor.py	Wed Aug 25 16:45:06 2010 +0200
@@ -52,10 +52,11 @@
 
     cursor = HAND
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, screen, *args, **kwargs):
         Widget.__init__(self, *args, **kwargs)
         self._cursor_group = RenderUpdates()
         self._loaded_cursor = None
+        self.screen = screen
 
     def enter_screen(self):
         pygame.mouse.set_visible(0)
@@ -65,6 +66,11 @@
 
     def draw_all(self, _surface):
         Widget.draw_all(self, _surface)
+        item = self.screen.state.tool
+        if item is None:
+            self.set_cursor(HAND)
+        else:
+            self.set_cursor(item.CURSOR)
         surface = self.get_root().surface
         if self.cursor != self._loaded_cursor:
             self._loaded_cursor = self.cursor
--- a/gamelib/gamescreen.py	Wed Aug 25 16:24:55 2010 +0200
+++ b/gamelib/gamescreen.py	Wed Aug 25 16:45:06 2010 +0200
@@ -22,10 +22,10 @@
     sel_color = Color("yellow")
     sel_width = 2
 
-    def __init__(self, state, scene_widget):
+    def __init__(self, screen):
         PaletteView.__init__(self, (BUTTON_SIZE, BUTTON_SIZE), 1, 6, scrolling=True)
-        self.state = state
-        self.scene_widget = scene_widget
+        self.state = screen.state
+        self.state_widget = screen.state_widget
 
     def num_items(self):
         return len(self.state.inventory)
@@ -41,7 +41,7 @@
             if self.state.tool:
                 result = self.state.inventory[item_no].interact(self.state.tool, self.state)
                 if result:
-                    result.process(self.scene_widget)
+                    result.process(self.state_widget)
             else:
                 self.state.set_tool(self.state.inventory[item_no])
 
@@ -54,13 +54,14 @@
 
 class StateWidget(Widget):
 
-    def __init__(self, state):
+    def __init__(self, screen):
         Widget.__init__(self, Rect(0, 0, SCENE_SIZE[0], SCENE_SIZE[1]))
-        self.state = state
-        self.detail = DetailWindow(state)
+        self.screen = screen
+        self.state = screen.state
+        self.detail = DetailWindow(screen)
 
     def draw(self, surface):
-        self.state.draw(surface)
+        self.state.draw(surface, self.screen)
 
     def mouse_down(self, event):
         if self.subwidgets:
@@ -78,7 +79,7 @@
             self.invalidate()
         # We do this here so we can get enter and leave events regardless
         # of what happens
-        result = self.state.check_enter_leave()
+        result = self.state.check_enter_leave(self.screen)
         if result:
             result.process(self)
 
@@ -87,12 +88,12 @@
             self._mouse_move(event.pos)
 
     def _mouse_move(self, pos):
-        self.state.mouse_move(pos)
+        self.state.mouse_move(pos, self.screen)
 
     def show_message(self, message):
         self.parent.cursor_highlight(False)
         # Display the message as a modal dialog
-        MessageDialog(message, 60).present()
+        MessageDialog(self.screen, message, 60).present()
         # queue a redraw to show updated state
         self.invalidate()
         # The cursor could have gone anywhere
@@ -109,9 +110,10 @@
 
 
 class DetailWindow(Widget):
-    def __init__(self, state):
+    def __init__(self, screen):
         Widget.__init__(self)
-        self.state = state
+        self.screen = screen
+        self.state = screen.state
         self.border_width = 5
         self.border_color = (0, 0, 0)
 
@@ -122,7 +124,7 @@
         self.set_rect(rect.inflate(bw*2, bw*2))
 
     def draw(self, surface):
-        self.state.draw_detail(surface.subsurface(self.image_rect))
+        self.state.draw_detail(surface.subsurface(self.image_rect), self.screen)
 
     def mouse_down(self, event):
         result = self.state.interact_detail(self.global_to_local(event.pos))
@@ -133,7 +135,7 @@
         self._mouse_move(event.pos)
 
     def _mouse_move(self, pos):
-        self.state.mouse_move_detail(self.global_to_local(pos))
+        self.state.mouse_move_detail(self.global_to_local(pos), self.screen)
 
     def show_message(self, message):
         self.parent.show_message(message)
@@ -149,7 +151,7 @@
 
 class GameScreen(Screen, CursorWidget):
     def __init__(self, shell):
-        CursorWidget.__init__(self)
+        CursorWidget.__init__(self, self)
         Screen.__init__(self, shell)
         self.running = False
 
@@ -160,17 +162,17 @@
     def start_game(self):
         self._clear_all()
         # TODO: Randomly plonk the state here for now
-        self.state = initial_state(self)
-        self.state_widget = StateWidget(self.state)
+        self.state = initial_state()
+        self.state_widget = StateWidget(self)
         self.add(self.state_widget)
 
-        self.popup_menu = PopupMenu(self.shell)
+        self.popup_menu = PopupMenu(self)
         self.menubutton = PopupMenuButton('Menu',
                 action=self.popup_menu.show_menu)
 
         self.handbutton = HandButton(action=self.hand_pressed)
 
-        self.inventory = InventoryView(self.state, self.state_widget)
+        self.inventory = InventoryView(self)
 
         self.toolbar = ToolBar([
                 self.menubutton,
--- a/gamelib/popupmenu.py	Wed Aug 25 16:24:55 2010 +0200
+++ b/gamelib/popupmenu.py	Wed Aug 25 16:45:06 2010 +0200
@@ -24,9 +24,10 @@
 
 class PopupMenu(Menu, CursorWidget):
 
-    def __init__(self, shell):
-        CursorWidget.__init__(self)
-        self.shell = shell
+    def __init__(self, screen):
+        CursorWidget.__init__(self, screen)
+        self.screen = screen
+        self.shell = screen.shell
         items = [
                 ('Resume Game', 'hide'),
                 ('Quit Game', 'quit'),
--- a/gamelib/state.py	Wed Aug 25 16:24:55 2010 +0200
+++ b/gamelib/state.py	Wed Aug 25 16:45:06 2010 +0200
@@ -34,9 +34,9 @@
         if self.detail_view:
             scene_widget.show_detail(self.detail_view)
 
-def initial_state(screen):
+def initial_state():
     """Load the initial state."""
-    state = State(screen)
+    state = State()
     state.load_scenes("cryo")
     state.load_scenes("bridge")
     state.load_scenes("mess")
@@ -58,7 +58,7 @@
     * scenes
     """
 
-    def __init__(self, screen):
+    def __init__(self):
         # map of scene name -> Scene object
         self.scenes = {}
         # map of detail view name -> DetailView object
@@ -79,8 +79,6 @@
         self.do_check = None
         self.old_pos = None
 
-        self.screen = screen
-
     def add_scene(self, scene):
         self.scenes[scene.name] = scene
 
@@ -136,20 +134,16 @@
 
     def set_tool(self, item):
         self.tool = item
-        if item is None:
-            self.screen.set_cursor(HAND)
-        else:
-            self.screen.set_cursor(item.CURSOR)
 
-    def draw(self, surface):
+    def draw(self, surface, screen):
         if self.do_check and self.previous_scene and self.do_check == constants.LEAVE:
             # We still need to handle leave events, so still display the scene
-            self.previous_scene.draw(surface)
+            self.previous_scene.draw(surface, screen)
         else:
-            self.current_scene.draw(surface)
+            self.current_scene.draw(surface, screen)
 
-    def draw_detail(self, surface):
-        self.current_detail.draw(surface)
+    def draw_detail(self, surface, screen):
+        self.current_detail.draw(surface, screen)
 
     def interact(self, pos):
         return self.current_scene.interact(self.tool, pos)
@@ -161,7 +155,7 @@
         if not self.do_check:
             return self.current_scene.animate()
 
-    def check_enter_leave(self):
+    def check_enter_leave(self, screen):
         if not self.do_check:
             return None
         if self.do_check == constants.LEAVE:
@@ -173,12 +167,12 @@
             self.do_check = None
             # Fix descriptions, etc.
             if self.old_pos:
-                self.current_scene.mouse_move(self.tool, self.old_pos)
+                self.current_scene.mouse_move(self.tool, self.old_pos, screen)
             return self.current_scene.enter()
         raise RuntimeError('invalid do_check value %s' % self.do_check)
 
-    def mouse_move(self, pos):
-        self.current_scene.mouse_move(self.tool, pos)
+    def mouse_move(self, pos, screen):
+        self.current_scene.mouse_move(self.tool, pos, screen)
         # So we can do sensible things on enter and leave
         self.old_pos = pos
 
@@ -186,8 +180,8 @@
         """Flag that we need to run the enter loop"""
         self.do_check = constants.LEAVE
 
-    def mouse_move_detail(self, pos):
-        self.current_detail.mouse_move(self.tool, pos)
+    def mouse_move_detail(self, pos, screen):
+        self.current_detail.mouse_move(self.tool, pos, screen)
 
 
 class StatefulGizmo(object):
@@ -258,9 +252,9 @@
         label.fg_color = (0, 0, 0)
         return label
 
-    def draw_description(self, surface):
+    def draw_description(self, surface, screen):
         if self._current_description is not None:
-            sub = self.state.screen.get_root().surface.subsurface(
+            sub = screen.get_root().surface.subsurface(
                 Rect(5, 5, *self._current_description.size))
             self._current_description.draw_all(sub)
 
@@ -274,10 +268,10 @@
         for thing in self.things.itervalues():
             thing.draw(surface)
 
-    def draw(self, surface):
+    def draw(self, surface, screen):
         self.draw_background(surface)
         self.draw_things(surface)
-        self.draw_description(surface)
+        self.draw_description(surface, screen)
 
     def interact(self, item, pos):
         """Interact with a particular position.
@@ -313,14 +307,14 @@
         self._current_description = None
         return None
 
-    def mouse_move(self, item, pos):
+    def mouse_move(self, item, pos, screen):
         """Call to check whether the cursor has entered / exited a thing.
 
         Item may be an item in the list of items or None for the hand.
         """
         if self._current_thing is not None:
             if self._current_thing.contains(pos):
-                self.state.screen.cursor_highlight(True)
+                screen.cursor_highlight(True)
                 return
             else:
                 self._current_thing.leave()
@@ -333,7 +327,7 @@
                 self._current_description = self._make_description(
                     thing.get_description())
                 break
-        self.state.screen.cursor_highlight(self._current_thing is not None)
+        screen.cursor_highlight(self._current_thing is not None)
 
     def get_detail_size(self):
         return self._background.get_size()
--- a/gamelib/widgets.py	Wed Aug 25 16:24:55 2010 +0200
+++ b/gamelib/widgets.py	Wed Aug 25 16:45:06 2010 +0200
@@ -23,8 +23,8 @@
 
 class MessageDialog(BoomLabel, CursorWidget):
 
-    def __init__(self, text, wrap_width, **kwds):
-        CursorWidget.__init__(self)
+    def __init__(self, screen, text, wrap_width, **kwds):
+        CursorWidget.__init__(self, screen)
         paras = text.split("\n\n")
         text = "\n".join([textwrap.fill(para, wrap_width) for para in paras])
         albow.controls.Label.__init__(self, text, **kwds)