Mercurial > boomslang
comparison pyntnclick/cursor.py @ 548:ded4324b236e pyntnclick
Moved stuff around, broke everything.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 13:10:18 +0200 |
parents | gamelib/cursor.py@0e8487038834 |
children | 38fb04728ac5 |
comparison
equal
deleted
inserted
replaced
547:33ce7ff757c3 | 548:ded4324b236e |
---|---|
1 # cursor.py | |
2 # Copyright Boomslang team, 2010 (see COPYING File) | |
3 # Sprite Cursor | |
4 | |
5 from albow.resource import get_image | |
6 from albow.widget import Widget | |
7 from pygame.sprite import Sprite, RenderUpdates | |
8 from pygame.rect import Rect | |
9 import pygame | |
10 import pygame.color | |
11 import pygame.cursors | |
12 import pygame.mouse | |
13 | |
14 from gamelib.constants import SCENE_SIZE | |
15 | |
16 | |
17 class CursorSprite(Sprite): | |
18 "A Sprite that follows the Cursor" | |
19 | |
20 def __init__(self, filename, x=None, y=None): | |
21 Sprite.__init__(self) | |
22 self.filename = filename | |
23 self.pointer_x = x | |
24 self.pointer_y = y | |
25 self.highlighted = False | |
26 | |
27 def load(self): | |
28 if not hasattr(self, 'plain_image'): | |
29 self.plain_image = get_image('items', self.filename) | |
30 self.image = self.plain_image | |
31 self.rect = self.image.get_rect() | |
32 | |
33 if self.pointer_x is None: | |
34 self.pointer_x = self.rect.size[0] // 2 | |
35 if self.pointer_y is None: | |
36 self.pointer_y = self.rect.size[1] // 2 | |
37 | |
38 self.highlight = pygame.Surface(self.rect.size) | |
39 color = pygame.color.Color(255, 100, 100, 0) | |
40 self.highlight.fill(color) | |
41 | |
42 def update(self): | |
43 pos = pygame.mouse.get_pos() | |
44 self.rect.left = pos[0] - self.pointer_x | |
45 self.rect.top = pos[1] - self.pointer_y | |
46 | |
47 def set_highlight(self, enable): | |
48 if enable != self.highlighted: | |
49 self.load() | |
50 self.highlighted = enable | |
51 self.image = self.plain_image.copy() | |
52 if enable: | |
53 self.image.blit(self.highlight, self.highlight.get_rect(), | |
54 None, pygame.BLEND_MULT) | |
55 | |
56 | |
57 HAND = CursorSprite('hand.png', 12, 0) | |
58 | |
59 | |
60 class CursorWidget(Widget): | |
61 """Mix-in widget to ensure that mouse_move is propogated to parents""" | |
62 | |
63 cursor = HAND | |
64 _cursor_group = RenderUpdates() | |
65 _loaded_cursor = None | |
66 | |
67 def __init__(self, screen, *args, **kwargs): | |
68 Widget.__init__(self, *args, **kwargs) | |
69 self.screen = screen | |
70 | |
71 def enter_screen(self): | |
72 pygame.mouse.set_visible(0) | |
73 | |
74 def leave_screen(self): | |
75 pygame.mouse.set_visible(1) | |
76 | |
77 def draw_all(self, surface): | |
78 Widget.draw_all(self, surface) | |
79 self.draw_cursor(self.get_root().surface) | |
80 | |
81 def draw_cursor(self, surface): | |
82 self.set_cursor(self.screen.state.tool) | |
83 self.cursor.set_highlight(self.cursor_highlight()) | |
84 if self.cursor is not None: | |
85 self._cursor_group.update() | |
86 self._cursor_group.draw(surface) | |
87 | |
88 def mouse_delta(self, event): | |
89 self.invalidate() | |
90 | |
91 @classmethod | |
92 def set_cursor(cls, item): | |
93 if item is None or item.CURSOR is None: | |
94 cls.cursor = HAND | |
95 else: | |
96 cls.cursor = item.CURSOR | |
97 if cls.cursor != cls._loaded_cursor: | |
98 cls._loaded_cursor = cls.cursor | |
99 if cls.cursor is None: | |
100 pygame.mouse.set_visible(1) | |
101 cls._cursor_group.empty() | |
102 else: | |
103 pygame.mouse.set_visible(0) | |
104 cls.cursor.load() | |
105 cls._cursor_group.empty() | |
106 cls._cursor_group.add(cls.cursor) | |
107 | |
108 def cursor_highlight(self): | |
109 if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()): | |
110 return False | |
111 if self.screen.state.highlight_override: | |
112 return True | |
113 current_thing = self.screen.state.current_thing | |
114 if current_thing: | |
115 return current_thing.is_interactive() | |
116 return False |