Mercurial > pyntnclick
comparison gamelib/cursor.py @ 84:c76f2fad2af5
Draw CursorWidget on top of StateWidget
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Mon, 23 Aug 2010 23:58:11 +0200 |
parents | 213e47dea4d0 |
children | da547e148532 |
comparison
equal
deleted
inserted
replaced
83:4fd56ee2af61 | 84:c76f2fad2af5 |
---|---|
1 # cursor.py | 1 # cursor.py |
2 # Copyright Boomslang team, 2010 (see COPYING File) | 2 # Copyright Boomslang team, 2010 (see COPYING File) |
3 # Sprite Cursor | 3 # Sprite Cursor |
4 | 4 |
5 from albow.screen import Screen | |
6 from albow.widget import Widget | 5 from albow.widget import Widget |
7 from albow.resource import get_image | 6 from albow.resource import get_image |
8 import pygame.mouse as mouse | 7 import pygame.mouse as mouse |
9 from pygame.sprite import Sprite, RenderUpdates | 8 from pygame.sprite import Sprite, RenderUpdates |
10 import pygame.cursors | 9 import pygame.cursors |
11 import pygame.mouse | 10 import pygame.mouse |
12 | |
13 # FIXME: make this a all transparent cursor | |
14 BLANK_CURSOR=pygame.cursors.broken_x | |
15 | 11 |
16 class CursorSprite(Sprite): | 12 class CursorSprite(Sprite): |
17 "A Sprite that follows the Cursor" | 13 "A Sprite that follows the Cursor" |
18 | 14 |
19 def __init__(self, filename): | 15 def __init__(self, filename): |
25 self.rect.midtop = mouse.get_pos() | 21 self.rect.midtop = mouse.get_pos() |
26 | 22 |
27 class CursorWidget(Widget): | 23 class CursorWidget(Widget): |
28 """Mix-in widget to ensure that mouse_move is propogated to parents""" | 24 """Mix-in widget to ensure that mouse_move is propogated to parents""" |
29 | 25 |
30 def mouse_move(self, event): | 26 def __init__(self, *args, **kwargs): |
31 self.call_parent_handler('mouse_move', event) | 27 Widget.__init__(self, *args, **kwargs) |
32 | 28 self._cursor_group = RenderUpdates() |
33 def get_cursor(self, event): | 29 self._cursor_name = '' |
34 return BLANK_CURSOR | |
35 | |
36 | |
37 class CursorSpriteScreen(Screen): | |
38 "A Screen with a CursorSprite" | |
39 | |
40 def __init__(self, shell): | |
41 Screen.__init__(self, shell) | |
42 | |
43 sprite = CursorSprite('hand') | |
44 self.cursor_group = RenderUpdates(sprite) | |
45 | 30 |
46 def draw(self, surface): | 31 def draw(self, surface): |
47 if self.get_cursor(None) == pygame.mouse.get_cursor(): | 32 if self.rect.collidepoint(mouse.get_pos()): |
48 self.cursor_group.update() | 33 cursor = self.get_sprite_cursor() |
49 self.cursor_group.draw(surface) | 34 if cursor != self._cursor_name: |
35 if self.get_sprite_cursor() is None: | |
36 pygame.mouse.set_visible(1) | |
37 self._cursor_group.empty() | |
38 else: | |
39 pygame.mouse.set_visible(0) | |
40 self._cursor_group.empty() | |
41 self._cursor_group.add(CursorSprite(cursor)) | |
42 if cursor is not None: | |
43 self._cursor_group.update() | |
44 self._cursor_group.draw(surface) | |
50 | 45 |
51 def mouse_move(self, event): | 46 def mouse_move(self, event): |
52 self.invalidate() | 47 self.invalidate() |
53 | 48 |
54 def get_cursor(self, event): | 49 def get_sprite_cursor(self): |
55 return BLANK_CURSOR | 50 return 'hand' |