comparison gamelib/gui_base.py @ 38:7e18a67995f6

fixed pep8 issues
author Rizmari Versfeld <rizziepit@gmail.com>
date Mon, 07 May 2012 00:13:11 +0200
parents 9c4bf1f15431
children d82d3e54a4ef
comparison
equal deleted inserted replaced
37:9c4bf1f15431 38:7e18a67995f6
12 font_medium = Font(data.filepath('fonts/DejaVuSans.ttf'), 14) 12 font_medium = Font(data.filepath('fonts/DejaVuSans.ttf'), 14)
13 font_large = Font(data.filepath('fonts/DejaVuSans.ttf'), 18) 13 font_large = Font(data.filepath('fonts/DejaVuSans.ttf'), 18)
14 14
15 15
16 class Drawable(object): 16 class Drawable(object):
17 17
18 def __init__(self, rect): 18 def __init__(self, rect):
19 if isinstance(rect, Rect): 19 if isinstance(rect, Rect):
20 self.rect = rect 20 self.rect = rect
21 else: 21 else:
22 self.rect = Rect(rect[0],rect[1],rect[2],rect[3]) 22 self.rect = Rect(rect[0], rect[1], rect[2], rect[3])
23 23
24 def draw(self, surface): 24 def draw(self, surface):
25 pass 25 pass
26 26
27 27
28 class Clickable(object): 28 class Clickable(object):
29 29
30 def on_mouse_down(self, pos): 30 def on_mouse_down(self, pos):
31 pass 31 pass
32 32
33 def on_mouse_up(self, pos): 33 def on_mouse_up(self, pos):
34 pass 34 pass
35 35
36 def on_mouse_move(self, pos): 36 def on_mouse_move(self, pos):
37 pass 37 pass
38 38
39 def on_mouse_cancel(self): 39 def on_mouse_cancel(self):
40 pass 40 pass
41 41
42 def on_click(self): 42 def on_click(self):
43 pass 43 pass
44 44
45 45
46 class ContainerView(Drawable): 46 class ContainerView(Drawable):
47 47
48 def __init__(self): 48 def __init__(self):
49 self.children = [] 49 self.children = []
50 50
51 def add_child(self, child): 51 def add_child(self, child):
52 self.children.append(child) 52 self.children.append(child)
53 53
54 def remove_child(self, child): 54 def remove_child(self, child):
55 self.children.remove(child) 55 self.children.remove(child)
56 56
57 def get_child_by_pos(self, pos): 57 def get_child_by_pos(self, pos):
58 for child in self.children: 58 for child in self.children:
59 if child.rect.collidepoint(pos): 59 if child.rect.collidepoint(pos):
60 if isinstance(child, ContainerView): 60 if isinstance(child, ContainerView):
61 # calculates position relative to child 61 # calculates position relative to child
62 return child.get_child_by_pos((pos[0] - child.rect[0], pos[1] - child.rect[1])) 62 x = pos[0] - child.rect[0]
63 else: 63 y = pos[1] - child.rect[1]
64 return child 64 return child.get_child_by_pos((x, y))
65 65 else:
66 def draw_children(self, surface): 66 return child
67 for child in self.children: 67
68 child.draw(surface) 68 def draw_children(self, surface):
69 69 for child in self.children:
70 70 child.draw(surface)
71
72
71 class Window(Clickable, ContainerView): 73 class Window(Clickable, ContainerView):
72 74
73 def __init__(self, screen): 75 def __init__(self, screen):
74 super(Window, self).__init__() 76 super(Window, self).__init__()
75 self.surface = Surface((screen.get_width(), screen.get_height())) 77 self.surface = Surface((screen.get_width(), screen.get_height()))
76 self.background_colour = None 78 self.background_colour = None
77 self.background_image = None 79 self.background_image = None
78 self.pressed_child = None 80 self.pressed_child = None
79 81
80 def on_mouse_down(self, pos): 82 def on_mouse_down(self, pos):
81 child = self.get_child_by_pos(pos) 83 child = self.get_child_by_pos(pos)
82 if isinstance(child, Clickable): 84 if isinstance(child, Clickable):
83 # calculates position relative to child 85 # calculates position relative to child
84 child.on_mouse_down((pos[0] - child.rect[0], pos[1] - child.rect[1])) 86 x = pos[0] - child.rect[0]
85 self.pressed_child = child 87 y = pos[1] - child.rect[1]
86 88 child.on_mouse_down((x, y))
87 def on_mouse_up(self, pos): 89 self.pressed_child = child
88 if self.pressed_child: 90
89 child = self.pressed_child 91 def on_mouse_up(self, pos):
90 child.on_mouse_up((pos[0] - child.rect[0], pos[1] - child.rect[1])); 92 if self.pressed_child:
91 self.pressed_child = None 93 child = self.pressed_child
92 94 x = pos[0] - child.rect[0]
93 def on_mouse_move(self, pos): 95 y = pos[1] - child.rect[1]
94 if self.pressed_child and self.pressed_child != self.get_child_by_pos(pos): 96 child.on_mouse_up((x, y))
95 self.pressed_child.on_mouse_cancel() 97 self.pressed_child = None
96 self.pressed_child = None 98
97 99 def on_mouse_move(self, pos):
98 def draw(self, screen): 100 if self.pressed_child:
99 if self.background_colour: 101 if self.pressed_child != self.get_child_by_pos(pos):
100 self.surface.fill(self.background_colour) 102 self.pressed_child.on_mouse_cancel()
101 if self.background_image: 103 self.pressed_child = None
102 self.surface.blit(self.background_image, (0,0)) 104
103 self.draw_children(self.surface) 105 def draw(self, screen):
104 screen.blit(self.surface, (0,0)) 106 if self.background_colour:
105 107 self.surface.fill(self.background_colour)
108 if self.background_image:
109 self.surface.blit(self.background_image, (0, 0))
110 self.draw_children(self.surface)
111 screen.blit(self.surface, (0, 0))
112
106 113
107 class StateDrawable(Drawable): 114 class StateDrawable(Drawable):
108 115
109 def __init__(self, rect): 116 def __init__(self, rect):
110 super(StateDrawable, self).__init__(rect) 117 super(StateDrawable, self).__init__(rect)
111 self.state = -1 118 self.state = -1
112 self.states = {} 119 self.states = {}
113 self.drawables = [] 120 self.drawables = []
114 self.surface = Surface((self.rect[2], self.rect[3])) 121 self.surface = Surface((self.rect[2], self.rect[3]))
115 122
116 def draw(self, surface): 123 def draw(self, surface):
117 if self.state != -1 and self.drawables[self.state]: 124 if self.state != -1 and self.drawables[self.state]:
118 self.drawables[self.state].draw(surface) 125 self.drawables[self.state].draw(surface)
119 126
120 def add_state(self, state_name, drawable): 127 def add_state(self, state_name, drawable):
121 self.states[state_name] = len(self.drawables) 128 self.states[state_name] = len(self.drawables)
122 self.drawables.append(drawable) 129 self.drawables.append(drawable)
123 130
124 def set_state(self, state_name): 131 def set_state(self, state_name):
125 self.state = self.states[state_name] 132 self.state = self.states[state_name]
126 133
127 134
128 class Button(Clickable, StateDrawable): 135 class Button(Clickable, StateDrawable):
129 136
130 def __init__(self, rect, normal_drawable, down_drawable): 137 def __init__(self, rect, normal_drawable, down_drawable):
131 super(Button, self).__init__(rect) 138 super(Button, self).__init__(rect)
132 self.add_state('NORMAL', normal_drawable) 139 self.add_state('NORMAL', normal_drawable)
133 self.add_state('DOWN', down_drawable) 140 self.add_state('DOWN', down_drawable)
134 self.set_state('NORMAL') 141 self.set_state('NORMAL')
135 142
136 def on_mouse_down(self, pos): 143 def on_mouse_down(self, pos):
137 self.set_state('DOWN') 144 self.set_state('DOWN')
138 145
139 def on_mouse_up(self, pos): 146 def on_mouse_up(self, pos):
140 self.set_state('NORMAL') 147 self.set_state('NORMAL')
141 self.on_click() 148 self.on_click()
142 149
143 def on_mouse_cancel(self): 150 def on_mouse_cancel(self):
144 self.set_state('NORMAL') 151 self.set_state('NORMAL')
145 152
146 153
147 class TextButton(Button): 154 class TextButton(Button):
148 155
149 def __init__(self, rect, normal_drawable, down_drawable, text): 156 def __init__(self, rect, normal_drawable, down_drawable, text):
150 super(TextButton, self).__init__(rect, normal_drawable, down_drawable) 157 super(TextButton, self).__init__(rect, normal_drawable, down_drawable)
151 self.surface = Surface((rect[2],rect[3])) 158 self.surface = Surface((rect[2], rect[3]))
152 self.text = text 159 self.text = text
153 font_large.set_bold(True) 160 font_large.set_bold(True)
154 self.text_surface = font_large.render(self.text, True, (128,128,128,128)) 161 self.text_surface = font_large.render(self.text, True, (128, 128, 128))
155 shadow = font_large.render(self.text, True, (0,0,0,255)) 162 shadow = font_large.render(self.text, True, (0, 0, 0))
156 font_large.set_bold(False) 163 font_large.set_bold(False)
157 self.text_surface.blit(shadow, (-2, -2)) 164 self.text_surface.blit(shadow, (-2, -2))
158 size = font_large.size(self.text) 165 size = font_large.size(self.text)
159 self.text_offset = ((rect[2]-size[0])/2, (rect[3]-size[1])/2) 166 self.text_offset = ((rect[2] - size[0]) / 2, (rect[3] - size[1]) / 2)
160
161 def draw(self, surface):
162 self.surface.fill((0,0,0,0))
163 super(TextButton, self).draw(self.surface)
164 self.surface.blit(self.text_surface, self.text_offset)
165 surface.blit(self.surface, self.rect)
166 167
167 168 def draw(self, surface):
168 169 self.surface.fill((0, 0, 0, 0))
169 170 super(TextButton, self).draw(self.surface)
170 171 self.surface.blit(self.text_surface, self.text_offset)
171 172 surface.blit(self.surface, self.rect)