Mercurial > boomslang
comparison pyntnclick/cursor.py @ 854:79b5c1be9a5e default tip
Remove pyntnclick, it's its own library, now
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 21 Jun 2014 22:06:09 +0200 |
parents | f95830b58336 |
children |
comparison
equal
deleted
inserted
replaced
852:f95830b58336 | 854:79b5c1be9a5e |
---|---|
1 # cursor.py | |
2 # Copyright Boomslang team, 2010 (see COPYING File) | |
3 # Sprite Cursor | |
4 | |
5 from pygame.sprite import Sprite, RenderUpdates | |
6 import pygame | |
7 import pygame.color | |
8 import pygame.mouse | |
9 | |
10 from pyntnclick.engine import Screen | |
11 from pyntnclick.image_transforms import Colour | |
12 | |
13 | |
14 class CursorSprite(Sprite): | |
15 "A Sprite that follows the Cursor" | |
16 | |
17 def __init__(self, filename, x=None, y=None): | |
18 Sprite.__init__(self) | |
19 self.filename = filename | |
20 self.pointer_x = x | |
21 self.pointer_y = y | |
22 self.highlighted = False | |
23 self.highlight_colour = (255, 100, 100, 255) | |
24 | |
25 def load(self, resources): | |
26 if not hasattr(self, 'plain_image'): | |
27 self.highlight_transform = Colour(self.highlight_colour) | |
28 self.plain_image = resources.get_image('items', self.filename) | |
29 self.highlighted_image = resources.get_image('items', | |
30 self.filename, transforms=(self.highlight_transform,)) | |
31 self.image = self.plain_image | |
32 self.rect = self.image.get_rect() | |
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 def update(self): | |
39 pos = pygame.mouse.get_pos() | |
40 self.rect.left = pos[0] - self.pointer_x | |
41 self.rect.top = pos[1] - self.pointer_y | |
42 | |
43 def set_highlight(self, enable): | |
44 self.image = self.highlighted_image if enable else self.plain_image | |
45 | |
46 | |
47 HAND = CursorSprite('hand.png', 12, 0) | |
48 | |
49 | |
50 class CursorScreen(Screen): | |
51 """A Screen with custom cursors""" | |
52 | |
53 def setup(self): | |
54 self._cursor_group = RenderUpdates() | |
55 self._loaded_cursor = None | |
56 self.set_cursor(None) | |
57 | |
58 def on_enter(self): | |
59 super(CursorScreen, self).on_enter() | |
60 pygame.mouse.set_visible(0) | |
61 | |
62 def on_exit(self): | |
63 super(CursorScreen, self).on_exit() | |
64 pygame.mouse.set_visible(1) | |
65 | |
66 def draw(self, surface): | |
67 super(CursorScreen, self).draw(surface) | |
68 self.set_cursor(self.game.tool) | |
69 self._loaded_cursor.set_highlight(self.cursor_highlight()) | |
70 self._cursor_group.update() | |
71 self._cursor_group.draw(surface) | |
72 | |
73 def set_cursor(self, item): | |
74 if item is None or item.CURSOR is None: | |
75 cursor = HAND | |
76 else: | |
77 cursor = item.CURSOR | |
78 if cursor != self._loaded_cursor: | |
79 self._loaded_cursor = cursor | |
80 self._loaded_cursor.load(self.gd.resource) | |
81 self._cursor_group.empty() | |
82 self._cursor_group.add(self._loaded_cursor) | |
83 | |
84 def cursor_highlight(self): | |
85 return self.container.mouseover_widget.highlight_cursor |