Mercurial > pyntnclick
comparison gamelib/scenewidgets.py @ 525:821b322e903b
Separate "scene widgets" from "game-specific widgets".
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Wed, 08 Sep 2010 14:02:11 +0200 |
parents | |
children | 42742a62f9c3 |
comparison
equal
deleted
inserted
replaced
524:a91cb4bffd5d | 525:821b322e903b |
---|---|
1 """Interactive elements within a Scene.""" | |
2 | |
3 | |
4 from pygame import Rect | |
5 from pygame.color import Color | |
6 from pygame.colordict import THECOLORS | |
7 from pygame.surface import Surface | |
8 from albow.resource import get_image | |
9 | |
10 from gamelib.state import Thing | |
11 from gamelib.constants import DEBUG | |
12 from gamelib.widgets import BoomLabel | |
13 | |
14 | |
15 class Interact(object): | |
16 | |
17 def __init__(self, image, rect, interact_rect): | |
18 self.image = image | |
19 self.rect = rect | |
20 self.interact_rect = interact_rect | |
21 | |
22 def set_thing(self, thing): | |
23 pass | |
24 | |
25 def draw(self, surface): | |
26 if self.image is not None: | |
27 surface.blit(self.image, self.rect, None) | |
28 | |
29 def animate(self): | |
30 return False | |
31 | |
32 | |
33 class InteractNoImage(Interact): | |
34 | |
35 def __init__(self, x, y, w, h): | |
36 super(InteractNoImage, self).__init__(None, None, Rect(x, y, w, h)) | |
37 | |
38 | |
39 class InteractText(Interact): | |
40 """Display box with text to interact with -- mostly for debugging.""" | |
41 | |
42 def __init__(self, x, y, text, bg_color=None): | |
43 if bg_color is None: | |
44 bg_color = (127, 127, 127) | |
45 label = BoomLabel(text) | |
46 label.set_margin(5) | |
47 label.border_width = 1 | |
48 label.border_color = (0, 0, 0) | |
49 label.bg_color = bg_color | |
50 label.fg_color = (0, 0, 0) | |
51 image = Surface(label.size) | |
52 rect = Rect((x, y), label.size) | |
53 label.draw_all(image) | |
54 super(InteractText, self).__init__(image, rect, rect) | |
55 | |
56 | |
57 class InteractRectUnion(Interact): | |
58 | |
59 def __init__(self, rect_list): | |
60 # pygame.rect.Rect.unionall should do this, but is broken | |
61 # in some pygame versions (including 1.8, it appears) | |
62 rect_list = [Rect(x) for x in rect_list] | |
63 union_rect = rect_list[0] | |
64 for rect in rect_list[1:]: | |
65 union_rect = union_rect.union(rect) | |
66 super(InteractRectUnion, self).__init__(None, None, union_rect) | |
67 self.interact_rect = rect_list | |
68 | |
69 | |
70 class InteractImage(Interact): | |
71 | |
72 def __init__(self, x, y, image_name): | |
73 super(InteractImage, self).__init__(None, None, None) | |
74 self._pos = (x, y) | |
75 self._image_name = image_name | |
76 | |
77 def set_thing(self, thing): | |
78 self.image = get_image(thing.folder, self._image_name) | |
79 self.rect = Rect(self._pos, self.image.get_size()) | |
80 self.interact_rect = self.rect | |
81 | |
82 | |
83 class InteractImageRect(InteractImage): | |
84 def __init__(self, x, y, image_name, r_x, r_y, r_w, r_h): | |
85 super(InteractImageRect, self).__init__(x, y, image_name) | |
86 self._r_pos = (r_x, r_y) | |
87 self._r_size = (r_w, r_h) | |
88 | |
89 def set_thing(self, thing): | |
90 super(InteractImageRect, self).set_thing(thing) | |
91 self.interact_rect = Rect(self._r_pos, self._r_size) | |
92 | |
93 | |
94 class InteractAnimated(Interact): | |
95 """Interactive with an animation rather than an image""" | |
96 | |
97 # FIXME: Assumes all images are the same size | |
98 # anim_seq - sequence of image names | |
99 # delay - number of frames to wait between changing images | |
100 | |
101 def __init__(self, x, y, anim_seq, delay): | |
102 self._pos = (x, y) | |
103 self._anim_pos = 0 | |
104 self._names = anim_seq | |
105 self._frame_count = 0 | |
106 self._anim_seq = None | |
107 self._delay = delay | |
108 | |
109 def set_thing(self, thing): | |
110 self._anim_seq = [get_image(thing.folder, x) for x in self._names] | |
111 self.image = self._anim_seq[0] | |
112 self.rect = Rect(self._pos, self.image.get_size()) | |
113 self.interact_rect = self.rect | |
114 | |
115 def animate(self): | |
116 if self._anim_seq: | |
117 self._frame_count += 1 | |
118 if self._frame_count > self._delay: | |
119 self._frame_count = 0 | |
120 self._anim_pos += 1 | |
121 if self._anim_pos >= len(self._anim_seq): | |
122 self._anim_pos = 0 | |
123 self.image = self._anim_seq[self._anim_pos] | |
124 # queue redraw | |
125 return True | |
126 return False | |
127 | |
128 | |
129 class GenericDescThing(Thing): | |
130 "Thing with an InteractiveUnionRect and a description" | |
131 | |
132 INITIAL = "description" | |
133 | |
134 def __init__(self, prefix, number, description, areas): | |
135 super(GenericDescThing, self).__init__() | |
136 self.description = description | |
137 self.name = '%s.%s' % (prefix, number) | |
138 self.interacts = { | |
139 'description' : InteractRectUnion(areas) | |
140 } | |
141 if DEBUG: | |
142 # Individual colors to make debugging easier | |
143 self._interact_hilight_color = Color(THECOLORS.keys()[number]) | |
144 | |
145 def get_description(self): | |
146 return self.description | |
147 | |
148 def is_interactive(self, tool=None): | |
149 return False | |
150 |