1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
from color import Color
from rectangle import Rectangle
class VBoxContainer:
def __init__(self, x, y, background_color=Color.WHITE, border_color=Color.BLACK, border_width=0,
border_radius=0, padding=10, separation_distance=20, centered=False, children=None):
if children is None:
children = []
self.x = x
self.y = y
self.width = 0
self.height = 0
self.background_color = background_color
self.border_color = border_color
self.border_width = border_width
self.border_radius = border_radius
self.padding = padding
self.separation_distance = separation_distance
self.centered = centered
self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height,
self.background_color, self.border_color, self.border_width, self.border_radius)
self.children = children
self.set_size()
self.update_children_position()
def get_children(self):
return self.children
def get_child(self, index):
return self.children[index]
def get_child_count(self):
return len(self.children)
def get_child_index(self, child):
return self.children.index(child)
def center(self, x=None, y=None):
if x is None:
x = self.x
if y is None:
y = self.y
self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2)
def set_position(self, x, y):
self.x = x
self.y = y
self.rect.set_position(x - self.padding, y - self.padding)
self.update_children_position()
def update_children_position(self):
child_y = self.y
for i, child in enumerate(self.children):
child_position_x = self.x + (
self.get_width() - 2 * self.padding - child.get_width()) // 2 if self.centered else self.x
child_position_y = child_y
if hasattr(child, 'padding'):
child_position_x += child.padding
child_position_y += child.padding
child.set_position(child_position_x, child_position_y)
child_y += child.get_height() + self.separation_distance
self.rect.set_size(self.get_width(), self.get_height())
def update(self):
for child in self.children:
if hasattr(child, 'update'):
child.update()
self.update_children_position()
def get_width(self):
if self.get_child_count() == 0:
return 0
return max([child.get_width() for child in self.children]) + self.padding * 2
def get_height(self):
return sum([child.get_height() for child in self.children]) + self.padding * 2 + (
self.get_child_count() - 1) * self.separation_distance
def move(self, dx, dy):
self.x += dx
self.y += dy
self.rect.move(dx, dy)
for child in self.children:
child.move(dx, dy)
def set_size(self):
if self.get_child_count() == 0:
self.width = 0
self.height = 0
else:
self.width = max([child.get_width() for child in self.children]) + self.padding * 2
self.height = sum([child.get_height() for child in self.children]) + self.padding * 2 + (
self.get_child_count() - 1) * self.separation_distance
self.rect.set_size(self.width, self.height)
def add_child(self, child):
self.children.append(child)
self.set_size()
self.update_children_position()
def remove_child(self, child):
self.children.remove(child)
self.set_size()
self.update_children_position()
def draw(self, screen):
self.rect.draw(screen)
for child in self.children:
child.draw(screen)
class HBoxContainer:
def __init__(self, x, y, background_color=Color.WHITE, border_color=Color.BLACK, border_width=0,
border_radius=0, padding=10, separation_distance=20, centered=True, children=None):
if children is None:
children = []
self.x = x
self.y = y
self.width = 0
self.height = 0
self.background_color = background_color
self.border_color = border_color
self.border_width = border_width
self.border_radius = border_radius
self.padding = padding
self.separation_distance = separation_distance
self.centered = centered
self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height,
self.background_color, self.border_color, self.border_width, self.border_radius)
self.children = children
self.set_size()
self.update_children_position()
def get_children(self):
return self.children
def get_child(self, index):
return self.children[index]
def get_child_count(self):
return len(self.children)
def get_child_index(self, child):
return self.children.index(child)
def center(self, x=None, y=None):
if x is None:
x = self.x
if y is None:
y = self.y
self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2)
def set_position(self, x, y):
self.x = x
self.y = y
self.rect.set_position(x - self.padding, y - self.padding)
self.update_children_position()
def update_children_position(self):
child_x = self.x
for i, child in enumerate(self.children):
child_position_x = child_x
child_position_y = self.y + (
self.get_height() - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x
if hasattr(child, 'padding'):
child_position_x += child.padding
child_position_y += child.padding
child.set_position(child_position_x, child_position_y)
child_x += child.get_width() + self.separation_distance
self.rect.set_size(self.get_width(), self.get_height())
def update(self):
for child in self.children:
if hasattr(child, 'update'):
child.update()
self.update_children_position()
def get_width(self):
return sum([child.get_width() for child in self.children]) + self.padding * 2 + (
self.get_child_count() - 1) * self.separation_distance
def get_height(self):
if self.get_child_count() == 0:
return 0
return max([child.get_height() for child in self.children]) + self.padding * 2
def move(self, dx, dy):
self.x += dx
self.y += dy
self.rect.move(dx, dy)
for child in self.children:
child.move(dx, dy)
def set_size(self):
if self.get_child_count() == 0:
self.width = 0
self.height = 0
else:
self.width = sum([child.get_width() for child in self.children]) + self.padding * 2 + (
self.get_child_count() - 1) * self.separation_distance
self.height = max([child.get_height() for child in self.children]) + self.padding * 2
self.rect.set_size(self.width, self.height)
def add_child(self, child):
self.children.append(child)
self.set_size()
self.update_children_position()
def remove_child(self, child):
self.children.remove(child)
self.set_size()
self.update_children_position()
def draw(self, screen):
self.rect.draw(screen)
for child in self.children:
child.draw(screen)
class GridContainer:
def __init__(self, x, y, rows, columns, background_color=Color.WHITE, border_color=Color.BLACK, border_width=0,
border_radius=0, padding=10, separation_distance=20, children=None):
if children is None:
children = [[None for _ in range(columns)] for _ in range(rows)]
else:
if len(children) != rows:
raise ValueError('Children must be a 2D array of size rows x columns')
for row in children:
if len(row) != columns:
raise ValueError('Children must be a 2D array of size rows x columns')
self.x = x
self.y = y
self.width = 0
self.height = 0
self.rows = rows
self.columns = columns
self.grid = VBoxContainer(x, y, background_color, border_color, border_width, border_radius, padding,
separation_distance, True)
for row in children:
self.grid.add_child(
HBoxContainer(x, y, background_color, border_color=background_color, border_width=0, border_radius=0,
padding=0, separation_distance=separation_distance, centered=True, children=row))
self.children = children
def get_children(self):
return self.children
def get_child(self, row, col):
return self.children[row][col]
def get_child_count(self):
counter = 0
for row in self.children:
counter += len(row)
return counter
def get_child_index(self, child):
for row in self.children:
if child in row:
return self.children.index(child)
return -1
def center(self, x=None, y=None):
if x is None:
x = self.x
if y is None:
y = self.y
self.set_position(x - (self.width - 2 * self.grid.padding) // 2, y - (self.height - 2 * self.grid.padding) // 2)
self.grid.center(x, y)
def set_position(self, x, y):
self.x = x
self.y = y
self.grid.set_position(x, y)
def update(self):
self.grid.update()
def get_width(self):
return sum([child.get_width() for child in self.children]) + self.grid.padding * 2 + (
self.get_child_count() - 1) * self.grid.separation_distance
def get_height(self):
return sum([child.get_height() for child in self.children]) + self.grid.padding * 2 + (
self.get_child_count() - 1) * self.grid.separation_distance
def move(self, dx, dy):
self.x += dx
self.y += dy
self.grid.move(dx, dy)
def set_size(self):
if self.get_child_count() == 0:
self.width = 0
self.height = 0
else:
self.width = sum([child.get_width() for child in self.children]) + self.grid.padding * 2 + (
self.get_child_count() - 1) * self.grid.separation_distance
self.height = sum([child.get_height() for child in self.children]) + self.grid.padding * 2 + (
self.get_child_count() - 1) * self.grid.separation_distance
self.grid.set_size()
def add_child(self, child, row, col):
self.children[row][col] = child
self.grid.set_size()
self.grid.update_children_position()
def remove_child(self, child):
self.children.remove(child)
self.grid.set_size()
self.grid.update_children_position()
def draw(self, screen):
self.grid.draw(screen)
|