Mercurial > boomslang
annotate pyntnclick/cursor.py @ 692:d6ded808cc33 pyntnclick
Much scene management refactoring.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Tue, 14 Feb 2012 13:39:05 +0200 |
parents | 6d75895477ac |
children | c8b683dd56d3 |
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 | |
135 | 6 import pygame |
7 import pygame.color | |
70 | 8 import pygame.cursors |
9 import pygame.mouse | |
10 | |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
11 from pyntnclick.engine import Screen |
636
2748d3afcae5
Excise albow. Really.
Stefano Rivera <stefano@rivera.za.net>
parents:
603
diff
changeset
|
12 |
401
92029c76b687
Different toolbar colour and remove highlighted cursor outside scene widget.
Jeremy Thurgood <firxen@gmail.com>
parents:
324
diff
changeset
|
13 |
66 | 14 class CursorSprite(Sprite): |
15 "A Sprite that follows the Cursor" | |
16 | |
302
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
17 def __init__(self, filename, x=None, y=None): |
66 | 18 Sprite.__init__(self) |
135 | 19 self.filename = filename |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
20 self.pointer_x = x |
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
21 self.pointer_y = y |
172
3b5cfb3f794e
Ensure cursor loaded when calling set_highlight
Stefano Rivera <stefano@rivera.za.net>
parents:
171
diff
changeset
|
22 self.highlighted = False |
66 | 23 |
660
dbec226debe3
We don't need no resources hacks
Stefano Rivera <stefano@rivera.za.net>
parents:
658
diff
changeset
|
24 def load(self, resources): |
135 | 25 if not hasattr(self, 'plain_image'): |
660
dbec226debe3
We don't need no resources hacks
Stefano Rivera <stefano@rivera.za.net>
parents:
658
diff
changeset
|
26 self.plain_image = resources.get_image('items', self.filename) |
135 | 27 self.image = self.plain_image |
28 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
|
29 |
6d93e04036c9
CursorSprite: Default pointer-position to the centre of the cursor
Stefano Rivera <stefano@rivera.za.net>
parents:
187
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 |
135 | 35 self.highlight = pygame.Surface(self.rect.size) |
36 color = pygame.color.Color(255, 100, 100, 0) | |
37 self.highlight.fill(color) | |
38 | |
66 | 39 def update(self): |
135 | 40 pos = pygame.mouse.get_pos() |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
41 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
|
42 self.rect.top = pos[1] - self.pointer_y |
66 | 43 |
135 | 44 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
|
45 if enable != self.highlighted: |
687
6d75895477ac
Comment out highlight_override, not used by our widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
675
diff
changeset
|
46 #XXX: Do we need this? self.load() |
139
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
47 self.highlighted = enable |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
48 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
|
49 if enable: |
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
50 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
|
51 None, pygame.BLEND_MULT) |
135 | 52 |
53 | |
54 HAND = CursorSprite('hand.png', 12, 0) | |
55 | |
124 | 56 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
57 class CursorScreen(Screen): |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
58 """A Screen with custom cursors""" |
70 | 59 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
60 def setup(self): |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
61 self._cursor_group = RenderUpdates() |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
62 self._loaded_cursor = None |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
63 self.set_cursor(None) |
129
4223d66d88b4
Cursor change when you select a Tool
Stefano Rivera <stefano@rivera.za.net>
parents:
124
diff
changeset
|
64 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
65 def on_enter(self): |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
66 super(CursorScreen, self).on_enter() |
139
a5972444ad1e
Improve cursor state handling, disable highlights on modal dialogs
Stefano Rivera <stefano@rivera.za.net>
parents:
135
diff
changeset
|
67 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
|
68 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
69 def on_exit(self): |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
70 super(CursorScreen, self).on_exit() |
139
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 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
73 def draw(self, surface): |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
74 super(CursorScreen, self).draw(surface) |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
75 self.set_cursor(self.game.tool) |
675
67373f9d2d20
Renable cursor highlights. NAFC how it's working, but wth
Stefano Rivera <stefano@rivera.za.net>
parents:
660
diff
changeset
|
76 self._loaded_cursor.set_highlight(self.cursor_highlight()) |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
77 self._cursor_group.update() |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
78 self._cursor_group.draw(surface) |
70 | 79 |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
80 def set_cursor(self, item): |
513
0e8487038834
fixed crash caused by the manual item's special snowflakeness
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
401
diff
changeset
|
81 if item is None or item.CURSOR is None: |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
82 cursor = HAND |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
83 else: |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
84 cursor = item.CURSOR |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
85 if cursor != self._loaded_cursor: |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
86 self._loaded_cursor = cursor |
660
dbec226debe3
We don't need no resources hacks
Stefano Rivera <stefano@rivera.za.net>
parents:
658
diff
changeset
|
87 self._loaded_cursor.load(self.gd.resource) |
658
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
88 self._cursor_group.empty() |
2703924c8c70
Custom mouse cursors return
Stefano Rivera <stefano@rivera.za.net>
parents:
636
diff
changeset
|
89 self._cursor_group.add(self._loaded_cursor) |
135 | 90 |
324
3476e8f3b100
Fixed mouse and cursor handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
302
diff
changeset
|
91 def cursor_highlight(self): |
692
d6ded808cc33
Much scene management refactoring.
Jeremy Thurgood <firxen@gmail.com>
parents:
687
diff
changeset
|
92 return self.container.mouseover_widget.highlight_cursor |