-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjects.py
More file actions
336 lines (283 loc) · 10.8 KB
/
objects.py
File metadata and controls
336 lines (283 loc) · 10.8 KB
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
environment = []
# Set properties
# Get properties
# Convert % and px to correct format
# Format multiple values to work in practice
# Gemaakt door Dylan Noorland
palette = {
'white' : color(238, 239, 240),
'black' : color(50, 50, 50),
'gray' : color(213, 216, 219),
'gray_hover' : color(203, 206, 209),
'dark_gray' : color(193, 196, 199),
'light_blue' : color(204, 216, 223),
'blue' : color(77, 107, 164),
'red' : color(229, 56, 59),
'transparent' : color(220, 220, 220, 100),
'solid_white' : color(255, 255, 255),
'player_colors' : [
color(248, 249, 250), # white
color(20, 23, 26), # black
color(204, 42, 66), # red
color(63, 84, 174) # blue
]
}
screen = {
'w': [1280],
'h': [720],
'x': [0],
'y': [0],
'rectMode': [CORNER]
}
class Property:
global screen
def __init__(self, parent = None):
if not parent:
self.parent = screen
else:
self.parent = parent
self.default = {
'w': 50,
'h': 50,
'x': 0,
'y': 0,
'radius': 0,
'fill': '255 255 255 255',
'stroke': '0 0 0',
'strokeWeight': 1,
'strokeCap': SQUARE,
'rectMode': CORNER,
'textSize': 16,
'placeholder': '',
'textColor': '0 0 0 255',
'textAlign': [LEFT, BOTTOM],
'textMargin': '0 0',
'selectType': 'hold',
'font': 'OpenSans-48.vlw'
}
self.storage = {}
self.setDefault()
def setDefault(self, copyValues = True):
"""Default item duplication to property storage
Parameters:
bool copyValues: If false, setDefault will only export the keys to storage.
"""
if copyValues == True:
for propertyType in self.default.keys():
self[propertyType] = self.default[propertyType]
else:
for propertyType in self.default.keys():
self.storage.update({propertyType: ''})
def __setitem__(self, propertyType, propertyValue):
if propertyType in self.default.keys():
if not isinstance(propertyValue, str):
if isinstance(propertyValue, list):
self.storage[propertyType] = propertyValue
else:
self.storage[propertyType] = self.setMultipleValues(propertyType, self.toNormal(propertyValue))
else:
self.storage[propertyType] = self.setMultipleValues(propertyType, propertyValue)
else:
print("'" + str(propertyType) + "' is not a valid property key!")
def __getitem__(self, propertyType):
return self.storage[propertyType]
def setItems(self, properties):
for propertyKey, propertyValue in properties.items():
self[propertyKey] = propertyValue
def getItems(self):
temp = {}
for propertyKey in self.storage.keys():
temp[propertyKey] = self[propertyKey]
return temp
def setMultipleValues(self, propertyType, propertyValue):
if propertyType != 'placeholder':
splitValues = propertyValue.split()
for index in range(len(splitValues)):
splitValues[index] = self.toReal(propertyType, splitValues[index])
return splitValues
return [propertyValue]
def toReal(self, propertyType, normalValue):
if '%'in normalValue:
percent = float(normalValue.replace('%', '')) / 100
if propertyType in 'wh':
return self.parent[propertyType][0] * percent
if self.parent['rectMode'][0] == CORNER:
if propertyType == 'x':
return self.parent['x'][0] + self.parent['w'][0] * percent
elif propertyType == 'y':
return self.parent['y'][0] + self.parent['h'][0] * percent
elif self.parent['rectMode'][0] == CENTER:
if propertyType in 'xy':
return self.parent[propertyType][0] + self.parent['w'][0] * percent - self.parent['w'][0] / 2
try:
if str(int(normalValue)).isnumeric():
if propertyType == 'x' or propertyType == 'y':
return self.parent[propertyType][0] + int(normalValue)
else:
return int(normalValue)
except:
return normalValue
def toNormal(self, realValue):
if isinstance(realValue, list):
temp = []
for i in range(len(realValue)):
temp.append(self.toNormal(realValue[i]))
return temp
else:
return str(realValue)
class Screen(object):
def __init__(self, name, properties = None):
self.name = name
self.properties = Property(None)
self.properties.setItems(properties)
self.active = False
self.content = []
self.data = {}
def start(self):
global environment
if self in environment:
print("Error for " + str(self.name) + ": start() has already been initialized.")
else:
self.active = True
environment.append(self)
def stop(self):
if self.active == False:
print("Error for " + str(self.name) + ": you cannot initialize stop() before start().")
else:
self.active = False
def addData(self, key, value):
self.data[key] = value
def delData(self, key):
del self.data[key]
def getData(self, key = False):
if key:
return self.data[key]
else:
return self.data
def addContent(self, objectToAdd):
self.content.append(objectToAdd)
def drawContent(self):
for i in range(len(self.content)):
content.draw()
class Instance(object):
def __init__(self, parent, properties = None):
global environment
self.parent = parent
self.default = properties
self.properties = Property(parent)
self.hover = Property(parent)
self.selected = Property(parent)
self.properties.setItems(properties)
self.hover.setItems(self.properties.getItems())
self.selected.setItems(self.properties.getItems())
self.isSelected = False
for i in range(len(environment)):
if environment[i].active == True:
self.screen = environment[i]
try:
self.screen.content.append(self)
except:
print("Could not find an active screen for " + str(self))
def __setitem__(self, propertyType, propertyValue):
self.properties[propertyType] = propertyValue
def __getitem__(self, propertyType):
return self.properties[propertyType]
def drawInstance(self):
if mousePressed and self.isHover():
self.mousePressedEvent()
elif self.isHover() and not self.isSelected:
self.hoverEvent()
elif not self.isSelected:
self.properties.setItems(self.default)
self.setting()
def hoverEvent(self):
self.properties.setItems(self.hover.getItems())
def mousePressedEvent(self):
if not self.isSelected:
self.isSelected = True
self.properties.setItems(self.selected.getItems())
def mouseReleasedEvent(self):
self.isSelected = False
self.properties.setItems(self.default)
def isHover(self):
a = [mouseX,mouseY]
if self['rectMode'][0] == CORNER:
b = [self['x'][0], self['y'][0], self['w'][0], self['h'][0]]
else:
b = [self['x'][0] - self['w'][0] / 2, self['y'][0] - self['h'][0] / 2, self['w'][0], self['h'][0]]
isBetweenX = a[0] >= b[0] and a[0] <= b[0]+b[2]
isBetweenY = a[1] >= b[1] and a[1] <= b[1]+b[3]
if (isBetweenY and isBetweenX):
return True
return False
def setting(self):
if self['fill'][0] == 'None':
noFill()
else:
fill(
self['fill'][0],
self['fill'][1],
self['fill'][2],
self['fill'][3]
)
if self['stroke'][0] == 'None':
noStroke()
else:
stroke(
self['stroke'][0],
self['stroke'][1],
self['stroke'][2]
)
class Rectangle(Instance):
def __init__(self, parent, properties = None):
super(Rectangle, self).__init__(parent, properties)
def shape(self):
rectMode(int(self['rectMode'][0]))
rect(
self['x'][0],
self['y'][0],
self['w'][0],
self['h'][0],
self['radius'][0]
)
def draw(self):
self.drawInstance()
self.shape()
class Button(Rectangle):
def __init__(self, parent, properties = None):
super(Button, self).__init__(parent, properties)
self.goTo = self.screen
self.text = ''
self.forbidden = [ENTER, TAB, BACKSPACE]
def draw(self):
self.drawInstance()
self.shape()
self.drawText()
def drawText(self):
font = loadFont(self['font'][0])
textFont(font, 48)
fill(self['textColor'][0], self['textColor'][1], self['textColor'][2], self['textColor'][3])
textAlign(self['textAlign'][0], self['textAlign'][1])
textSize(self['textSize'][0])
text(self['placeholder'][0], self['x'][0] + self['textMargin'][0], self['y'][0] + self['textMargin'][1], self['textMargin'][1])
class TextField(Rectangle):
def __init__(self, parent, properties = None):
super(TextField, self).__init__(parent, properties)
self.text = ''
self.forbidden = [ENTER, TAB, BACKSPACE]
def draw(self):
self.drawInstance()
self.shape()
self.drawText()
def drawText(self):
font = loadFont(self['font'][0])
textFont(font, 48)
fill(self['textColor'][0], self['textColor'][1], self['textColor'][2], self['textColor'][3])
textAlign(self['textAlign'][0], self['textAlign'][1])
textSize(self['textSize'][0])
if not self.isSelected and self.text == '':
text(self['placeholder'][0], self['x'][0] + self['textMargin'][0], self['y'][0] + self['textMargin'][1] + self['h'][0] / 2, self['textMargin'][1])
else:
text(self.text, self['x'][0] + self['textMargin'][0], self['y'][0], self['textMargin'][1])
def keyTypedEvent(self):
pass