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