# HG changeset patch # User Simon Cross # Date 1282590283 -7200 # Node ID 99c5506de7eab41d463d1a27aed834fefacd2cc8 # Parent 213e47dea4d07f26a45c24e3682e7f78c4459e45 Start of interact handling. diff -r 213e47dea4d0 -r 99c5506de7ea gamelib/gamescreen.py --- a/gamelib/gamescreen.py Mon Aug 23 20:54:04 2010 +0200 +++ b/gamelib/gamescreen.py Mon Aug 23 21:04:43 2010 +0200 @@ -65,7 +65,13 @@ if desc: print desc + def mouse_down(self, event): + # TODO: replace None with the correct item + self.state.interact(None, event.pos) + def mouse_move(self, event): + # TODO: replace None with the correct item + self.state.mouse_move(None, event.pos) if self.state.check_for_new_description(event.pos): # queue a redraw self.invalidate() diff -r 213e47dea4d0 -r 99c5506de7ea gamelib/state.py --- a/gamelib/state.py Mon Aug 23 20:54:04 2010 +0200 +++ b/gamelib/state.py Mon Aug 23 21:04:43 2010 +0200 @@ -67,6 +67,12 @@ def draw(self, surface): self.current_scene.draw(surface) + def interact(self, item, pos): + self.current_scene.iteract(item, pos) + + def mouse_move(self, item, pos): + self.current_scene.mouse_move(item, pos) + def get_message(self): return self.msg @@ -82,6 +88,9 @@ return old_desc != self.description def get_description(self): + # + # DEPRECATED + # return self.description def message(self, msg): @@ -126,6 +135,7 @@ # map of thing names -> Thing objects self.things = {} self._background = get_image(self.FOLDER, self.BACKGROUND) + self._current_thing = None def add_item(self, item): self.state.add_item(item) @@ -148,7 +158,37 @@ self.draw_background(surface) self.draw_things(surface) + def interact(self, item, pos): + """Interact with a particular position. + + Item may be an item in the list of items or None for the hand. + """ + for thing in self.things.itervalues(): + if thing.rect.collidepoint(pos): + thing.interact(item) + break + + def mouse_move(self, item, pos): + """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.rect.collidepoint(pos): + return + else: + self._current_thing.leave() + self._current_thing = None + for thing in self.things.itervalues(): + if thing.rect.collidepoint(pos): + thing.enter(item) + self._current_thing = thing + break + def check_description(self, pos): + # + # DEPRECATED + # desc = None for thing in self.things.itervalues(): # Last thing in the list that matches wins @@ -200,6 +240,14 @@ def is_interactive(self): return True + def enter(self, item): + """Called when the cursor enters the Thing.""" + print "Enter %r -> %r" % (item, self) + + def leave(self): + """Called when the cursr leaves the Thing.""" + print "Leaves %r" % self + def interact(self, item): if not self.is_interactive(): return