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