Mercurial > boomslang
comparison pyntnclick/cursor.py @ 658:2703924c8c70 pyntnclick
Custom mouse cursors return
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 12 Feb 2012 14:25:27 +0200 |
parents | 2748d3afcae5 |
children | dbec226debe3 |
comparison
equal
deleted
inserted
replaced
657:0df0c81a3d8b | 658:2703924c8c70 |
---|---|
7 import pygame | 7 import pygame |
8 import pygame.color | 8 import pygame.color |
9 import pygame.cursors | 9 import pygame.cursors |
10 import pygame.mouse | 10 import pygame.mouse |
11 | 11 |
12 from pyntnclick.widgets.base import Widget | 12 from pyntnclick.engine import Screen |
13 | 13 |
14 # XXX: Need a way to get at the constants | 14 # XXX: Need a way to get at the constants |
15 from pyntnclick.constants import GameConstants | 15 from pyntnclick.constants import GameConstants |
16 SCENE_SIZE = GameConstants().scene_size | 16 SCENE_SIZE = GameConstants().scene_size |
17 # XXX: Needs a way to get at resource: | 17 # XXX: Needs a way to get at resource: |
60 | 60 |
61 | 61 |
62 HAND = CursorSprite('hand.png', 12, 0) | 62 HAND = CursorSprite('hand.png', 12, 0) |
63 | 63 |
64 | 64 |
65 class CursorWidget(Widget): | 65 class CursorScreen(Screen): |
66 """Mix-in widget to ensure that mouse_move is propogated to parents""" | 66 """A Screen with custom cursors""" |
67 | 67 |
68 cursor = HAND | 68 def setup(self): |
69 _cursor_group = RenderUpdates() | 69 self._cursor_group = RenderUpdates() |
70 _loaded_cursor = None | 70 self._loaded_cursor = None |
71 self.set_cursor(None) | |
71 | 72 |
72 def __init__(self, screen, *args, **kwargs): | 73 def on_enter(self): |
73 Widget.__init__(self, *args, **kwargs) | 74 super(CursorScreen, self).on_enter() |
74 self.screen = screen | |
75 | |
76 def enter_screen(self): | |
77 pygame.mouse.set_visible(0) | 75 pygame.mouse.set_visible(0) |
78 | 76 |
79 def leave_screen(self): | 77 def on_exit(self): |
78 super(CursorScreen, self).on_exit() | |
80 pygame.mouse.set_visible(1) | 79 pygame.mouse.set_visible(1) |
81 | 80 |
82 def draw_all(self, surface): | 81 def draw(self, surface): |
83 Widget.draw_all(self, surface) | 82 super(CursorScreen, self).draw(surface) |
84 self.draw_cursor(self.get_root().surface) | 83 self.set_cursor(self.game.tool) |
84 #XXX: self.cursor.set_highlight(self.cursor_highlight()) | |
85 self._cursor_group.update() | |
86 self._cursor_group.draw(surface) | |
85 | 87 |
86 def draw_cursor(self, surface): | 88 def set_cursor(self, item): |
87 self.set_cursor(self.screen.game.tool) | |
88 self.cursor.set_highlight(self.cursor_highlight()) | |
89 if self.cursor is not None: | |
90 self._cursor_group.update() | |
91 self._cursor_group.draw(surface) | |
92 | |
93 def mouse_delta(self, event): | |
94 self.invalidate() | |
95 | |
96 @classmethod | |
97 def set_cursor(cls, item): | |
98 if item is None or item.CURSOR is None: | 89 if item is None or item.CURSOR is None: |
99 cls.cursor = HAND | 90 cursor = HAND |
100 else: | 91 else: |
101 cls.cursor = item.CURSOR | 92 cursor = item.CURSOR |
102 if cls.cursor != cls._loaded_cursor: | 93 if cursor != self._loaded_cursor: |
103 cls._loaded_cursor = cls.cursor | 94 self._loaded_cursor = cursor |
104 if cls.cursor is None: | 95 self._loaded_cursor.load() |
105 pygame.mouse.set_visible(1) | 96 self._cursor_group.empty() |
106 cls._cursor_group.empty() | 97 self._cursor_group.add(self._loaded_cursor) |
107 else: | |
108 pygame.mouse.set_visible(0) | |
109 cls.cursor.load() | |
110 cls._cursor_group.empty() | |
111 cls._cursor_group.add(cls.cursor) | |
112 | 98 |
113 def cursor_highlight(self): | 99 def cursor_highlight(self): |
114 if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()): | 100 if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()): |
115 return False | 101 return False |
116 if self.screen.game.highlight_override: | 102 if self.screen.game.highlight_override: |