Mercurial > pyntnclick
annotate gamelib/cursor.py @ 405:8ba59ae66188
raw images
author | Adrianna Pińska <adrianna.pinska@gmail.com> |
---|---|
date | Sat, 28 Aug 2010 21:37:28 +0200 |
parents | 92029c76b687 |
children | 0e8487038834 |
rev | line source |
---|---|
66 | 1 # cursor.py |
2 # Copyright Boomslang team, 2010 (see COPYING File) | |
3 # Sprite Cursor | |
4 | |
135 | 5 from albow.resource import get_image |
70 | 6 from albow.widget import Widget |
66 | 7 from pygame.sprite import Sprite, RenderUpdates |
401
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
8 from pygame.rect import Rect |
135 | 9 import pygame |
10 import pygame.color | |
70 | 11 import pygame.cursors |
12 import pygame.mouse | |
13 | |
401
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
14 from gamelib.constants import SCENE_SIZE |
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
15 |
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
16 |
66 | 17 class CursorSprite(Sprite): |
18 "A Sprite that follows the Cursor" | |
19 | |
302
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
20 def __init__(self, filename, x=None, y=None): |
66 | 21 Sprite.__init__(self) |
135 | 22 self.filename = filename |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
23 self.pointer_x = x |
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
24 self.pointer_y = y |
172
3b5cfb3f794e
Ensure cursor loaded when calling set_highlight
Stefano Rivera <stefano@rivera.za.net>
parents:
171
diff
changeset
|
25 self.highlighted = False |
66 | 26 |
135 | 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() | |
302
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
32 |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
33 if self.pointer_x is None: |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
34 self.pointer_x = self.rect.size[0] // 2 |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
35 if self.pointer_y is None: |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
36 self.pointer_y = self.rect.size[1] // 2 |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
37 |
135 | 38 self.highlight = pygame.Surface(self.rect.size) |
39 color = pygame.color.Color(255, 100, 100, 0) | |
40 self.highlight.fill(color) | |
41 | |
66 | 42 def update(self): |
135 | 43 pos = pygame.mouse.get_pos() |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
44 self.rect.left = pos[0] - self.pointer_x |
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
45 self.rect.top = pos[1] - self.pointer_y |
66 | 46 |
135 | 47 def set_highlight(self, enable): |
139
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
48 if enable != self.highlighted: |
172
3b5cfb3f794e
Ensure cursor loaded when calling set_highlight
Stefano Rivera <stefano@rivera.za.net>
parents:
171
diff
changeset
|
49 self.load() |
139
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
50 self.highlighted = enable |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
51 self.image = self.plain_image.copy() |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
52 if enable: |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
53 self.image.blit(self.highlight, self.highlight.get_rect(), |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
54 None, pygame.BLEND_MULT) |
135 | 55 |
56 | |
57 HAND = CursorSprite('hand.png', 12, 0) | |
58 | |
124 | 59 |
70 | 60 class CursorWidget(Widget): |
61 """Mix-in widget to ensure that mouse_move is propogated to parents""" | |
62 | |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
63 cursor = HAND |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
64 _cursor_group = RenderUpdates() |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
65 _loaded_cursor = None |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
66 |
187
fbfd8e748ac0
Remove screen reference from State.
Jeremy Thurgood <firxen@gmail.com>
parents:
172
diff
changeset
|
67 def __init__(self, screen, *args, **kwargs): |
84
c76f2fad2af5
Draw CursorWidget on top of StateWidget
Stefano Rivera <stefano@rivera.za.net>
parents:
70
diff
changeset
|
68 Widget.__init__(self, *args, **kwargs) |
187
fbfd8e748ac0
Remove screen reference from State.
Jeremy Thurgood <firxen@gmail.com>
parents:
172
diff
changeset
|
69 self.screen = screen |
66 | 70 |
139
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
71 def enter_screen(self): |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
72 pygame.mouse.set_visible(0) |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
73 |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
74 def leave_screen(self): |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
75 pygame.mouse.set_visible(1) |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
76 |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
77 def draw_all(self, surface): |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
78 Widget.draw_all(self, surface) |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
79 self.draw_cursor(self.get_root().surface) |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
80 |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
81 def draw_cursor(self, surface): |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
82 self.set_cursor(self.screen.state.tool) |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
83 self.cursor.set_highlight(self.cursor_highlight()) |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
84 if self.cursor is not None: |
115
5e704ee7b84b
Remove "in-widget" check and always draw cursor.
Jeremy Thurgood <firxen@gmail.com>
parents:
114
diff
changeset
|
85 self._cursor_group.update() |
5e704ee7b84b
Remove "in-widget" check and always draw cursor.
Jeremy Thurgood <firxen@gmail.com>
parents:
114
diff
changeset
|
86 self._cursor_group.draw(surface) |
66 | 87 |
114
13d8cb1d5962
Better cursor handling using draw_all() and mouse_delta().
Jeremy Thurgood <firxen@gmail.com>
parents:
108
diff
changeset
|
88 def mouse_delta(self, event): |
66 | 89 self.invalidate() |
70 | 90 |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
91 @classmethod |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
92 def set_cursor(cls, item): |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
93 if item is None: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
94 cls.cursor = HAND |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
95 else: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
96 cls.cursor = item.CURSOR |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
97 if cls.cursor != cls._loaded_cursor: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
98 cls._loaded_cursor = cls.cursor |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
99 if cls.cursor is None: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
100 pygame.mouse.set_visible(1) |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
101 cls._cursor_group.empty() |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
102 else: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
103 pygame.mouse.set_visible(0) |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
104 cls.cursor.load() |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
105 cls._cursor_group.empty() |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
106 cls._cursor_group.add(cls.cursor) |
135 | 107 |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
108 def cursor_highlight(self): |
401
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
109 if not Rect((0, 0), SCENE_SIZE).collidepoint(pygame.mouse.get_pos()): |
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
110 return False |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
111 if self.screen.state.highlight_override: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
112 return True |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
113 current_thing = self.screen.state.current_thing |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
114 if current_thing: |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
115 return current_thing.is_interactive() |
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
116 return False |