/
App.py
395 lines (292 loc) · 9.02 KB
/
App.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
385
386
387
388
389
390
391
392
393
394
395
import sys,pygame
from Cell import *
from Map import *
from Color import *
from Panel import *
from Button import *
from ImageLoader import *
from Utils import *
class App:
windowSize = None
size = None
screen = None
running = False
cellMap = []
editMode = 0
option = 1
buttonDown = False
FONT = None
GUIROOT = None
MODES = None
WIRE_OPTIONS = None
GATE_OPTIONS = None
FILE_OPTIONS = None
#mode buttons
wireMode = None
gateMode = None
fileMode = None
#wire mode button
drawOption = None
jonctionOption = None
inputOption = None
outputOption = None
clickOption = None
#gate mode button
notOption = None
andOption = None
orOption = None
xorOption = None
nandOption = None
norOption = None
nxorOption = None
deleteOption = None
#file mode button
saveFileOption = None
loadFileOption = None
CELL_SIZE = 20
ROWS = None
COLS = None
def init(self):
ImageLoader.loadImages()
#refactoring needed
self.windowSize = [1000,700]
self.size = [1000,640]
self.running = True
GUISize = (self.windowSize[0],self.windowSize[1]-self.size[1])
#CELLMAP
App.ROWS = int(self.size[1] / self.CELL_SIZE)
App.COLS = int(self.size[0] / self.CELL_SIZE)
cellMap = Map(0,GUISize[1],(App.ROWS,App.COLS),App.CELL_SIZE)
self.cellMap = cellMap
#writeToFile("hello.txt",cellMap)
#readFromFile("hello.txt")
#GUI
App.GUIROOT = Panel(0,0,GUISize[0],GUISize[1],Color.yellow)
App.MODES = Panel(0,0,200,GUISize[1],Color.darkGray)
def setWireMode():
App.editMode = 0
App.option = 1
App.gateMode.dePush()
App.fileMode.dePush()
if len(App.GUIROOT.children) >= 2:
App.GUIROOT.removeChild(1)
App.GUIROOT.addChild(App.WIRE_OPTIONS)
App.wireMode = Button(10,5,50,50,Color.green,Color.red,setWireMode,ImageLoader.WIRE)
App.MODES.addChild(App.wireMode)
def setGateMode():
App.editMode = 1
App.option = 1
App.wireMode.dePush()
App.fileMode.dePush()
if len(App.GUIROOT.children) >= 2:
App.GUIROOT.removeChild(1)
App.GUIROOT.addChild(App.GATE_OPTIONS)
App.gateMode = Button(70,5,50,50,Color.green,Color.red,setGateMode,ImageLoader.GATE)
App.MODES.addChild(App.gateMode)
def setFileMode():
App.editMode = 2
App.option = 1
App.wireMode.dePush()
App.gateMode.dePush()
if len(App.GUIROOT.children) >= 2:
App.GUIROOT.removeChild(1)
App.GUIROOT.addChild(App.FILE_OPTIONS)
App.fileMode = Button(130,5,50,50,Color.green,Color.red,setFileMode,ImageLoader.FILE)
App.MODES.addChild(App.fileMode)
App.WIRE_OPTIONS = Panel(200,0,800,GUISize[1],Color.blue)
def setDrawOption():
App.option = 1
App.jonctionOption.dePush()
App.inputOption.dePush()
App.outputOption.dePush()
App.clickOption.dePush()
App.drawOption = Button(10 ,5,50,50,Color.green,Color.red,setDrawOption,ImageLoader.DRAW)
def setJonctionOption():
App.option = 2
App.drawOption.dePush()
App.inputOption.dePush()
App.outputOption.dePush()
App.clickOption.dePush()
App.jonctionOption = Button(20 + 50,5,50,50,Color.green,Color.red,setJonctionOption,ImageLoader.JONCTION)
def setInputOption():
App.option = 3
App.jonctionOption.dePush()
App.drawOption.dePush()
App.outputOption.dePush()
App.clickOption.dePush()
App.inputOption = Button(30 + 100,5,50,50,Color.green,Color.red,setInputOption,ImageLoader.INPUT)
def setOutputOption():
App.option = 4
App.jonctionOption.dePush()
App.inputOption.dePush()
App.drawOption.dePush()
App.clickOption.dePush()
App.outputOption = Button(40 + 150,5,50,50,Color.green,Color.red,setOutputOption,ImageLoader.OUTPUT)
def setClickOption():
App.option = 5
App.jonctionOption.dePush()
App.inputOption.dePush()
App.outputOption.dePush()
App.drawOption.dePush()
App.clickOption = Button(50 + 200,5,50,50,Color.green,Color.red,setClickOption,ImageLoader.CLICK)
#add button
App.WIRE_OPTIONS.addChild(App.drawOption)
App.WIRE_OPTIONS.addChild(App.jonctionOption)
App.WIRE_OPTIONS.addChild(App.inputOption)
App.WIRE_OPTIONS.addChild(App.outputOption)
App.WIRE_OPTIONS.addChild(App.clickOption)
App.GATE_OPTIONS = Panel(200,0,800,GUISize[1],Color.lightGray)
def setNotOption():
App.option = 1
App.andOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.notOption = Button(10,5,50,50,Color.green,Color.red,setNotOption,ImageLoader.NOT)
def setAndOption():
App.option = 2
App.notOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.andOption = Button(20+50,5,50,50,Color.green,Color.red,setAndOption,ImageLoader.AND)
def setOrOption():
App.option = 3
App.notOption.dePush()
App.andOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.orOption = Button(30+100,5,50,50,Color.green,Color.red,setOrOption,ImageLoader.OR)
def setXorOption():
App.option = 4
App.notOption.dePush()
App.andOption.dePush()
App.orOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.xorOption = Button(40+150,5,50,50,Color.green,Color.red,setXorOption,ImageLoader.XOR)
def setNandOption():
App.option = 5
App.notOption.dePush()
App.andOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.norOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.nandOption = Button(50+200,5,50,50,Color.green,Color.red,setNandOption,ImageLoader.NAND)
def setNorOption():
App.option = 6
App.notOption.dePush()
App.andOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.nxorOption.dePush()
App.deleteOption.dePush()
App.norOption = Button(60+250,5,50,50,Color.green,Color.red,setNorOption,ImageLoader.NOR)
def setNxorOption():
App.option = 7
App.notOption.dePush()
App.andOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.deleteOption.dePush()
App.nxorOption = Button(70+300,5,50,50,Color.green,Color.red,setNxorOption,ImageLoader.NXOR)
def setDeleteOption():
App.option = 8
App.notOption.dePush()
App.andOption.dePush()
App.orOption.dePush()
App.xorOption.dePush()
App.nandOption.dePush()
App.norOption.dePush()
App.deleteOption = Button(70+300,5,50,50,Color.green,Color.red,setDeleteOption,ImageLoader.DELETE)
App.GATE_OPTIONS.addChild(App.notOption)
App.GATE_OPTIONS.addChild(App.andOption)
App.GATE_OPTIONS.addChild(App.orOption)
App.GATE_OPTIONS.addChild(App.xorOption)
App.GATE_OPTIONS.addChild(App.nandOption)
App.GATE_OPTIONS.addChild(App.norOption)
App.GATE_OPTIONS.addChild(App.nxorOption)
App.GATE_OPTIONS.addChild(App.deleteOption)
App.FILE_OPTIONS = Panel(200,0,800,GUISize[1],Color.lightGray)
def setSaveFileOption():
writeToFile("hello.txt",self.cellMap)
App.saveFileOption.dePush()
App.saveFileOption = Button(10,5,50,50,Color.green,Color.red,setSaveFileOption,ImageLoader.SAVE)
def setLoadFileOption():
readFromFile("hello.txt",self.cellMap)
App.loadFileOption.dePush()
App.loadFileOption = Button(20+50,5,50,50,Color.green,Color.red,setLoadFileOption,ImageLoader.LOAD)
App.FILE_OPTIONS.addChild(App.saveFileOption)
App.FILE_OPTIONS.addChild(App.loadFileOption)
App.GUIROOT.addChild(App.MODES)
#App.GUIROOT.addChild(OPTIONS)
#PYGAME
pygame.init()
pygame.font.init()
App.FONT = pygame.font.SysFont("arial", 16, bold=False, italic=False)
self.screen = pygame.display.set_mode(self.windowSize)
def render(self):
#background
self.screen.fill(Color.black)
#GUI
App.GUIROOT.render(self.screen)
#cellMap
self.cellMap.render(self.screen)
#refresh
pygame.display.flip()
def update(self):
#GUI
App.GUIROOT.update()
if pygame.key.get_pressed()[pygame.K_w]:
App.editMode = 0
if pygame.key.get_pressed()[pygame.K_g]:
App.editMode = 1
if pygame.key.get_pressed()[pygame.K_1]:
App.option = 1
if pygame.key.get_pressed()[pygame.K_2]:
App.option = 2
if pygame.key.get_pressed()[pygame.K_3]:
App.option = 3
if pygame.key.get_pressed()[pygame.K_4]:
App.option = 4
if pygame.key.get_pressed()[pygame.K_5]:
App.option = 5
if pygame.key.get_pressed()[pygame.K_6]:
App.option = 6
if pygame.key.get_pressed()[pygame.K_7]:
App.option = 7
if pygame.key.get_pressed()[pygame.K_8]:
App.option = 8
self.cellMap.update()
#print(pygame.mouse.get_pos())
def run(self):
self.init()
print("running...")
while self.running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
self.running = False
if e.type == pygame.MOUSEBUTTONUP:
buttonDown = True
if e.type == pygame.MOUSEBUTTONUP:
buttonDown = False
self.render()
self.update()
sys.exit()
print("App closed")