-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysimpleGUI.py
384 lines (313 loc) · 12.9 KB
/
pysimpleGUI.py
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import pygame as pg
pg.init()
largeFont = pg.font.SysFont('calibri', 42)
smallFont = pg.font.SysFont('calibri', 32)
tinyFont = pg.font.SysFont('calibri', 18)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
LIGHTRED = (255, 128, 128)
GREEN = (0, 255, 0)
LIGHTGREEN = (128, 255, 128)
BGCOLOR = (0, 0, 128)
LIGHTGREY = (200, 200, 200)
DARKGREEN = (0, 128, 0)
DARKRED = (128, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
PURPLE = (128, 0, 128)
def empty(_):
pass
def euclidDis(p1, p2):
#Finner avstanden mellom to punkt i 2d rom ved hjelp av pytagoras
return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)**(1/2)
class Button():
def __init__(self, shape, func=empty, textBox = None, params=[]):
self.func, self.params = func, params
self.textBox = textBox
self.shape = shape
self.isactive = True
try:
if not self.shape.isshape:
raise Exception('Not a valid shape object')
except:
raise Exception('Not a valid shape object')
def collide(self, point):
if self.isactive:
if self.shape.collide(point):
self.func(self.params)
def draw(self):
if self.isactive:
self.shape.draw()
if self.textBox != None:
self.textBox.draw()
class Selector():
def __init__(self, buttons, selectorColor=WHITE, startSelected=0):
self.buttons = buttons
self.selectorColor = selectorColor
self.selected = startSelected
self.isactive = True
def getOutput(self):
return self.selected
def draw(self):
if self.isactive:
#Draw the outline (a bit convoluted)
self.buttons[self.selected].shape.center[0] -= 2
self.buttons[self.selected].shape.center[1] -= 2
self.buttons[self.selected].shape.update()
orgColor = self.buttons[self.selected].shape.color
self.buttons[self.selected].shape.color = self.selectorColor
self.buttons[self.selected].shape.draw()
self.buttons[self.selected].shape.color = orgColor
self.buttons[self.selected].shape.center[0] += 2
self.buttons[self.selected].shape.center[1] += 2
self.buttons[self.selected].shape.update()
#Draw the buttons
[button.draw() for button in self.buttons]
def collide(self, point):
if self.isactive:
for i, button in enumerate(self.buttons):
if button.shape.collide(point):
self.selected = i
class GUICircle():
def __init__(self, center, radius, color, surface=None):
self.center, self.radius, self.color, self.surface = center, radius, color ,surface
self.isshape = True
self.isactive = True
def collide(self, point):
if self.isactive:
return euclidDis(point, self.center) <= self.radius
return False
def draw(self):
if self.isactive:
pg.draw.circle(self.surface, self.color, self.center, self.radius)
def update(self):
pass
class GUIRect():
def __init__(self, center, size, color, surface=None, topleft=False):
self.center, self.size, self.color, self.surface = center, size, color, surface
self.topleft = topleft
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not topleft:
self.rect.center = center[0], center[1]
else:
self.rect.topleft = center[0], center[1]
self.isshape = True
self.isactive = True
def collide(self, point):
if self.isactive:
newRect = pg.Rect(point[0], point[1], 1, 1)
return bool(newRect.colliderect(self.rect))
return False
def draw(self):
if self.isactive:
pg.draw.rect(self.surface, self.color, self.rect)
def update(self):
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not self.topleft:
self.rect.center = self.center[0], self.center[1]
else:
self.rect.topleft = self.center[0], self.center[1]
class Bar():
def __init__(self, center, size, color, surface=None, topleft=False, borderColor=BLACK):
##Automatically change direction of bar based on longest dimension
self.center, self.size, self.color, self.surface, self.borderColor = center, size, color, surface, borderColor
self.topleft = topleft
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not topleft:
self.rect.center = center[0], center[1]
else:
self.rect.topleft = center[0], center[1]
self.isactive = True
self.fill = 1
def collide(self, point):
pass
def draw(self):
if self.isactive:
pg.draw.rect(self.surface, self.color, self.rect)
# Horisontal lines
self.rect.width = self.size[0]
self.rect.height = 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.rect.top += self.size[1]
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.update() # Reset the rect for more mutilation
# Vertical lines
self.rect.width = 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.rect.left += self.size[0]
self.rect.height += 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.update() #Make sure the rect is in good condition after we are done
def setFill(self, fill):
self.fill = fill
self.update()
def update(self):
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not self.topleft:
self.rect.center = self.center[0], self.center[1]
else:
self.rect.topleft = self.center[0], self.center[1]
self.rect.width *= self.fill
class Slider():
def __init__(self, center, size, color, surface=None, topleft=False, borderColor=BLACK, sliderColor=BLACK):
##Automatically change direction of bar based on longest dimension
self.center, self.size, self.color, self.surface, self.borderColor = center, size, color, surface, borderColor
self.sliderColor = sliderColor
self.topleft = topleft
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not topleft:
self.rect.center = center[0], center[1]
else:
self.rect.topleft = center[0], center[1]
self.isactive = True
self.fill = 1
def collide(self, point):
if not self.isactive:
return False
rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not self.topleft:
rect.center = self.center[0], self.center[1]
else:
rect.topleft = self.center[0], self.center[1]
newRect = pg.Rect(point[0], point[1], 1, 1)
if rect.colliderect(newRect):
self.setFill((point[0]-rect.top-12)/self.size[0])
def draw(self):
if self.isactive:
pg.draw.rect(self.surface, self.color, self.rect)
#Horisontal lines
self.rect.width = self.size[0]
self.rect.height = 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.rect.top += self.size[1]
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.update() #Reset the rect for more mutilation
#Vertical lines
self.rect.width = 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.rect.left += self.size[0]
self.rect.height += 2
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.rect.left += (self.size[0]*self.fill) - (self.size[0])
#Making the slider
self.rect.width = 6
self.rect.height += 6
self.rect.top -= 3
pg.draw.rect(self.surface, self.borderColor, self.rect)
self.update() #Make sure the rect is in good condition after we are done
def setFill(self, fill):
self.fill = fill
self.update()
def update(self):
self.rect = pg.Rect(0, 0, self.size[0], self.size[1])
if not self.topleft:
self.rect.center = self.center[0], self.center[1]
else:
self.rect.topleft = self.center[0], self.center[1]
self.rect.width *= self.fill
class TextBox():
def __init__(self, center, color, text, surface=None, font=largeFont, maxWidth=float('inf'), lineDis=1):
self.center, self.color, self.text, self.font = center, color, text, font
self.maxWidth = maxWidth
self.surface = surface
self.lineDis = lineDis*self.font.size(self.text)[1]
self.getDescs()
self.isactive = True
def draw(self):
if self.isactive:
for i, desc in enumerate(self.descs):
rect = desc.get_rect()
rect.center = self.center[0], self.center[1]+i*self.lineDis
self.surface.blit(desc, rect)
def newText(self, text, color=None):
self.text = text
if color == None:
color = self.color
self.color = color
self.getDescs()
def getWordLen(self, word):
return self.font.size(word)[0]
def getDescs(self):
words = [word for word in self.text.split(' ') if word != '']
wordLens = [self.getWordLen(word) for word in words]
self.descs = []
newLine = 0
for i, word in enumerate(words):
if sum(wordLens[newLine:i+1]) >= self.maxWidth:
self.descs += [self.font.render(' '.join(words[newLine:i]), True, self.color)]
newLine = i
elif word == '\\n':
self.descs += [self.font.render(' '.join(words[newLine:i]), True, self.color)]
newLine = i+1
remaining = [word for word in words[newLine:i+1] if word != '\\n']
self.descs += [self.font.render(' '.join(remaining), True, self.color)]
class Layout():
def __init__(self):
self.buttons = []
self.textBoxes = []
self.shapes = []
self.selectors = []
self.bars = []
def draw(self):
if self.checkSurface():
[button.draw() for button in self.buttons]
[text.draw() for text in self.textBoxes]
[shape.draw() for shape in self.shapes]
[selector.draw() for selector in self.selectors]
[bar.draw() for bar in self.bars]
else:
raise Exception('Some elements have undeffined surfaces. Set a surface for all elements in this layout by using the setSurface(surface) method of the layout object')
def collide(self, pos):
[button.collide(pos) for button in self.buttons]
[selector.collide(pos) for selector in self.selectors]
[bar.collide(pos) for bar in self.bars]
def addBar(self, bar):
self.bars += [bar]
def addButton(self, button):
self.buttons += [button]
def addTextBox(self, textBox):
self.textBoxes += [textBox]
def addShape(self, shape):
self.textBoxes += [shape]
def addSelector(self, selector):
self.selectors += [selector]
def setSurface(self, surface):
for textBox in self.textBoxes:
textBox.surface = surface
for button in self.buttons:
button.shape.surface = surface
if button.textBox != None:
button.textBox.surface = surface
for shape in self.shapes:
shape.surface = surface
for selector in self.selectors:
for button in selector.buttons:
button.shape.surface = surface
button.textBox.surface = surface
for bar in self.bars:
bar.surface = surface
def checkSurface(self):
#Checks if all contents have a defined surface
for textBox in self.textBoxes:
if textBox.surface == None:
return False
for button in self.buttons:
if button.shape.surface == None:
return False
if button.textBox != None:
if button.textBox.surface == None:
return False
for shape in self.shapes:
if shape.surface == None:
return False
for selector in self.selectors:
for button in selector.buttons:
if button.shape.surface == None:
return False
if button.textBox.surface == None:
return False
for bar in self.bars:
if bar.surface == None:
return False
return True