Mercurial > boomslang
comparison gamelib/cursor.py @ 324:3476e8f3b100
Fixed mouse and cursor handling.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 28 Aug 2010 13:30:44 +0200 |
parents | 6d93e04036c9 |
children | 92029c76b687 |
comparison
equal
deleted
inserted
replaced
323:0630a37cb371 | 324:3476e8f3b100 |
---|---|
55 | 55 |
56 class CursorWidget(Widget): | 56 class CursorWidget(Widget): |
57 """Mix-in widget to ensure that mouse_move is propogated to parents""" | 57 """Mix-in widget to ensure that mouse_move is propogated to parents""" |
58 | 58 |
59 cursor = HAND | 59 cursor = HAND |
60 _cursor_group = RenderUpdates() | |
61 _loaded_cursor = None | |
60 | 62 |
61 def __init__(self, screen, *args, **kwargs): | 63 def __init__(self, screen, *args, **kwargs): |
62 Widget.__init__(self, *args, **kwargs) | 64 Widget.__init__(self, *args, **kwargs) |
63 self._cursor_group = RenderUpdates() | |
64 self._loaded_cursor = None | |
65 self.screen = screen | 65 self.screen = screen |
66 | 66 |
67 def enter_screen(self): | 67 def enter_screen(self): |
68 pygame.mouse.set_visible(0) | 68 pygame.mouse.set_visible(0) |
69 | 69 |
70 def leave_screen(self): | 70 def leave_screen(self): |
71 pygame.mouse.set_visible(1) | 71 pygame.mouse.set_visible(1) |
72 | 72 |
73 def draw_all(self, _surface): | 73 def draw_all(self, surface): |
74 Widget.draw_all(self, _surface) | 74 Widget.draw_all(self, surface) |
75 item = self.screen.state.tool | 75 self.draw_cursor(self.get_root().surface) |
76 if item is None: | 76 |
77 self.set_cursor(HAND) | 77 def draw_cursor(self, surface): |
78 else: | 78 self.set_cursor(self.screen.state.tool) |
79 self.set_cursor(item.CURSOR) | 79 self.cursor.set_highlight(self.cursor_highlight()) |
80 surface = self.get_root().surface | |
81 if self.cursor != self._loaded_cursor: | |
82 self._loaded_cursor = self.cursor | |
83 if self.cursor is None: | |
84 pygame.mouse.set_visible(1) | |
85 self._cursor_group.empty() | |
86 else: | |
87 pygame.mouse.set_visible(0) | |
88 self.cursor.load() | |
89 self._cursor_group.empty() | |
90 self._cursor_group.add(self.cursor) | |
91 if self.cursor is not None: | 80 if self.cursor is not None: |
92 self._cursor_group.update() | 81 self._cursor_group.update() |
93 self._cursor_group.draw(surface) | 82 self._cursor_group.draw(surface) |
94 | 83 |
95 def mouse_delta(self, event): | 84 def mouse_delta(self, event): |
96 self.invalidate() | 85 self.invalidate() |
97 | 86 |
98 def set_cursor(self, cursor): | 87 @classmethod |
99 CursorWidget.cursor = cursor | 88 def set_cursor(cls, item): |
89 if item is None: | |
90 cls.cursor = HAND | |
91 else: | |
92 cls.cursor = item.CURSOR | |
93 if cls.cursor != cls._loaded_cursor: | |
94 cls._loaded_cursor = cls.cursor | |
95 if cls.cursor is None: | |
96 pygame.mouse.set_visible(1) | |
97 cls._cursor_group.empty() | |
98 else: | |
99 pygame.mouse.set_visible(0) | |
100 cls.cursor.load() | |
101 cls._cursor_group.empty() | |
102 cls._cursor_group.add(cls.cursor) | |
100 | 103 |
101 def cursor_highlight(self, enable): | 104 def cursor_highlight(self): |
102 self.cursor.set_highlight(enable) | 105 if self.screen.state.highlight_override: |
106 return True | |
107 current_thing = self.screen.state.current_thing | |
108 if current_thing: | |
109 return current_thing.is_interactive() | |
110 return False |