comparison gamelib/scenes/scene_widgets.py @ 263:3b4a78422201

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