forked from JackD83/PyMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMenu.py
486 lines (369 loc) · 18.7 KB
/
MainMenu.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import RenderObject, Configuration, SelectionMenu, FileChooser, Runner, Header, TextInput, ConfigMenu
import Footer, Keys, RenderControl, InfoOverlay, Common, NativeAppList,TaskHandler, ConfirmOverlay
import os, ResumeHandler, Suspend
import json, time
import pygame, sys, subprocess,platform
from pprint import pprint
from threading import Thread
class MainMenu(RenderObject.RenderObject):
config = Configuration.getConfiguration()
theme = Configuration.getTheme()
gear = Common.aspect_scale(Common.loadCachedImage("theme/gear.png"),20,20)
gearFinal = None
power = Common.aspect_scale(Common.loadCachedImage("theme/power.png"),18,18)
powerFinal = None
header = None
footer = None
systems = []
systembackgrounds = []
currentIndex = 0
inTransition = False
transitionOffset = 0
systemOffset = 160
systemIconOffet = (480 - config["screenWidth"]) / 2
overlay = None
subComponent = None
selection = "band"
def loadSystemImages(self):
self.systems = [None] * len(self.config["mainMenu"])
self.systembackgrounds = [None] * len(self.config["mainMenu"])
self.loadSystem(self.currentIndex)
self.loadSystem(self.getPrev())
self.loadSystem(self.getNext())
thread = Thread(target = self.loadImagesAsync, args = ())
thread.start()
def loadSystem(self, index):
if(index >= len(self.config["mainMenu"])):
return
entry = self.config["mainMenu"][index]
self.systems[index] = Common.aspect_scale(Common.loadCachedImage( entry["icon"]), 140, 70)
self.systembackgrounds[index] = pygame.transform.scale(Common.loadCachedImage( entry["background"]), ( self.config["screenWidth"],self.config["screenHeight"]) )
def loadImagesAsync(self):
for index, systemImage in enumerate(self.systems):
try:
if(systemImage is None):
self.loadSystem(index)
except ValueError:
pass
print("processed ", len(self.systems), " entries")
def handleEvents(self, events):
if(self.subComponent != None):
self.subComponent.handleEvents(events)
return
if(self.overlay != None):
self.overlay.handleEvents(events)
return
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == Keys.DINGOO_BUTTON_DOWN:
if(self.selection == "band"):
self.selection = "power"
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_UP:
if(self.selection == "power" or self.selection == "settings"):
self.selection = "band"
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_LEFT or event.key == Keys.DINGOO_BUTTON_L:
if(not self.inTransition and self.selection == "band"):
TaskHandler.addAnimation(0, 160, 200, self.transitionCallback)
self.inTransition = True
if( self.selection == "power"):
self.selection = "settings"
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_RIGHT or event.key == Keys.DINGOO_BUTTON_R:
if(not self.inTransition and self.selection == "band"):
TaskHandler.addAnimation(0, -160,200, self.transitionCallback)
self.inTransition = True
if( self.selection == "settings"):
self.selection = "power"
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_SELECT:
self.openOptions()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_A:
if(self.selection == "band"):
self.openSelection(self.config["mainMenu"][self.currentIndex])
if(self.selection == "power"):
self.openPowerMenu()
if(self.selection == "settings"):
self.openSettings()
RenderControl.setDirty()
def transitionCallback(self, start, target, current, finished):
if(finished):
self.inTransition = False
self.transitionOffset = 0
if(target < 0):
self.currentIndex = self.getNext()
else:
self.currentIndex = self.getPrev()
else:
self.transitionOffset = current
RenderControl.setDirty()
def openPowerMenu(self):
self.overlay = SelectionMenu.SelectionMenu(self.screen, ["Poweroff", "Reboot", "Mount USB"], self.contextMenuCallback)
def openOptions(self):
if("allowEdit" in self.config["options"] and self.config["options"]["allowEdit"] ):
self.overlay = SelectionMenu.SelectionMenu(self.screen, ["add new entry", "edit entry", "remove entry"], self.optionsCallback)
def openSettings(self):
self.subComponent = ConfigMenu.ConfigMenu(self.screen, "General Settings",{"textColor":(255,255,255), "backgroundColor":(0,0,0)}, \
Configuration.getPathData("options"), json.load(open('config/options.json')) ,self.configCallback)
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "change"), ("theme/start_button.png", "save")], (255,255,255))
self.subComponent.setFooter(footer)
RenderControl.setDirty()
def contextMenuCallback(self, selection, text):
self.overlay = None
if(text == "Mount USB"):
print("Mounting USB")
Common.mountUSB()
self.suspend.disableSuspend()
self.overlay = InfoOverlay.InfoOverlay("theme/usb.png", self.usbMountCallback)
if(text == "Poweroff"):
if(platform.processor() == ""):
subprocess.Popen(["sync"])
subprocess.Popen(["poweroff"])
else:
print("Poweroff")
if(text == "Reboot"):
if(platform.processor() == ""):
subprocess.Popen(["sync"])
subprocess.Popen(["reboot"])
else:
print("reboot")
def usbMountCallback(self, key):
print("Rebooting after usb mount")
if(platform.processor() == ""):
subprocess.Popen(["sync"])
subprocess.Popen(["reboot"])
else:
print("reboot")
self.overlay = None
def configCallback(self, select):
self.subComponent = None
Configuration.saveConfiguration()
RenderControl.setDirty()
def openSelection(self, current):
print("Opening selection")
ResumeHandler.setLastUsedMain(current, self.currentIndex)
if(current["type"] == "emulator"):
print()
if("useSelection" in current and current["useSelection"] == False):
print("Running emulator directly")
Runner.runNative(self.config["mainMenu"][self.currentIndex])
return
print("Opening emulator file selection")
options = {}
options["textColor"] = (55,55,55)
options["background"] = current["background"]
options["useSidebar"] = True
if("description" in current):
options["description"] = current["description"]
if("folderIcon" in current):
options["folderIcon"] = current["folderIcon"]
if("previews" in current):
options["previews"] = current["previews"]
if("fileFilter" in current):
options["fileFilter"] = current["fileFilter"]
self.subComponent = FileChooser.FileChooser(self.screen, current["name"], current["selectionPath"], False, options, self.emulatorCallback)
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "select")], (255,255,255))
self.subComponent.setFooter(footer)
if(current["type"] == "native"):
print("Opening native selection")
options = {}
options["background"] = current["background"]
options["useSidebar"] = True
self.subComponent = NativeAppList.NativeAppList(self.screen, current["name"], current["data"] , options, self.nativeCallback)
if("allowEdit" in self.config["options"] and self.config["options"]["allowEdit"] ):
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "select"), ("theme/select_button.png", "options")], (255,255,255))
else:
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "select")], (255,255,255))
self.subComponent.setFooter(footer)
def nativeCallback(self, selection):
self.subComponent = None
if(selection != None):
Runner.runNative(selection)
def optionsCallback(self, optionID, text):
print("Options came back with: ", optionID)
self.overlay = None
if(optionID == 0):
self.overlay = SelectionMenu.SelectionMenu(self.screen, ["Emulator", "Native"], self.typeAddCallback)
if(optionID == 1):
conf = None
if(self.config["mainMenu"][self.currentIndex]["type"] == "emulator"):
conf = json.load(open('config/entry.json'))
if(not Configuration.isRS97()):
conf = conf + json.load(open('config/pap.json'))
else:
conf = json.load(open('config/native.json'))
self.subComponent = ConfigMenu.ConfigMenu(self.screen, "Edit menu entry",{"textColor":(55,55,55), "backgroundColor":(221,221,221)}, \
self.config["mainMenu"][self.currentIndex] ,conf ,self.addEditCallback)
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "change"), ("theme/start_button.png", "save")], (255,255,255))
self.subComponent.setFooter(footer)
if(optionID == 2):
self.overlay = ConfirmOverlay.ConfirmOverlay("really delete?", (255,255,255), [("theme/b_button.png", "back"), ("theme/a_button.png", "delete")], self.deleteCallback)
RenderControl.setDirty()
def typeAddCallback(self, t, text):
self.overlay = None
data = None
conf = None
if(t==0):
data = self.getEmptyEmulatorData()
conf = json.load(open('config/entry.json'))
if(not Configuration.isRS97()):
conf = conf + json.load(open('config/pap.json'))
else:
data = self.getEmptyNativeData()
conf = json.load(open('config/native.json'))
self.subComponent = ConfigMenu.ConfigMenu(self.screen, "Add new menu entry",{"textColor":(255,255,255), "backgroundColor":(0,0,0)}, \
data ,conf ,self.addEditCallback)
footer = Footer.Footer([("theme/direction.png","select")], [("theme/b_button.png", "back"), ("theme/a_button.png", "change"), ("theme/start_button.png", "save")], (255,255,255))
self.subComponent.setFooter(footer)
def deleteCallback(self, res):
self.overlay = None
if(res == 1 and len(self.config["mainMenu"]) > 1):
del self.config["mainMenu"][self.currentIndex]
Configuration.saveConfiguration()
self.currentIndex = 0
self.loadSystemImages()
def getEmptyEmulatorData(self):
emptyEntry = {
"name": "Emulator",
"type": "emulator",
"background": "backgrounds",
"icon": "systems",
"cmd": ".",
"workingDir": None,
"useSelection" : True,
"selectionPath": ".",
"previews": None,
"legacy":False,
"screen":"default",
"overclock":"524"
}
return emptyEntry
def getEmptyNativeData(self):
emptyEntry = {
"name": "Native",
"type": "native",
"background": "backgrounds",
"icon": "systems",
"data": []
}
return emptyEntry
def addEditCallback(self, entry):
self.subComponent = None
print(entry)
if(entry == None):
self.reload()
return
if(entry["background"] == None or not os.path.isfile(entry["background"])):
print("Background does not exist! ")
self.reload()
return
if(entry["icon"] == None or not os.path.isfile(entry["icon"])):
print("Icon does not exist! ")
self.reload()
return
if(entry != None and not entry in self.config["mainMenu"]):
self.config["mainMenu"].insert(self.currentIndex, entry)
Configuration.saveConfiguration()
self.loadSystemImages()
RenderControl.setDirty()
def reload(self):
Configuration.reloadConfiguration()
self.config = Configuration.getConfiguration()
self.loadSystemImages()
def textCallback(self, text):
print("Text callback, got text: " + text)
self.subComponent = None
def emulatorCallback(self, selectedFile):
self.subComponent = None
if(selectedFile != None):
Runner.runEmu(self.config["mainMenu"][self.currentIndex], selectedFile)
def infoCallback(self, res):
self.overlay = None
self.config["firstStart"] = False
Configuration.saveConfiguration()
def getNext(self):
if(self.currentIndex + 1 >= len(self.systems)):
return 0
else:
return self.currentIndex + 1
def getPrev(self):
if(self.currentIndex - 1 < 0):
return len(self.systems) - 1
else:
return self.currentIndex -1
def render(self, screen):
if(self.subComponent != None):
self.subComponent.render(screen)
return
if(self.systembackgrounds[self.currentIndex] is not None):
screen.blit(self.systembackgrounds[self.currentIndex], (0, 0))
else:
RenderControl.setDirty()
if(self.selection=="band"):
self.banderole.fill((255,255,255, 160))
else:
self.banderole.fill((255,255,255, 90))
screen.blit(self.banderole, (0,80))
#current
if(self.systems[self.currentIndex] is not None):
screen.blit(self.systems[self.currentIndex], ( (self.config["screenWidth"] / 2 - self.systems[self.currentIndex].get_width() / 2) + self.transitionOffset , 40 + 80 -self.systems[self.currentIndex].get_height() / 2 ))
else:
RenderControl.setDirty()
#previous
if(self.systems[self.getPrev()] is not None and self.systems[self.getPrev()] is not None):
screen.blit(self.systems[self.getPrev()], (0 + self.transitionOffset - self.systemIconOffet, 40 + 80 -self.systems[self.getPrev()].get_height() / 2 ))
else:
RenderControl.setDirty()
#next
if(self.systems[self.getNext()] is not None and self.systems[self.currentIndex] is not None):
screen.blit(self.systems[self.getNext()], ( (self.config["screenWidth"] - self.systems[self.currentIndex].get_width()) + self.transitionOffset + self.systemIconOffet, 40 + 80 -self.systems[self.getNext()].get_height() / 2 ))
else:
RenderControl.setDirty()
if(self.footer != None):
self.footer.render(screen)
self.header.render(screen)
self.renderIcons(screen)
if(self.overlay != None):
self.overlay.render(screen)
def renderIcons(self, screen):
powerYPos = self.config["screenHeight"] - 30
powerXPos = self.config["screenWidth"] - 50
if(self.powerFinal == None):
self.powerFinal = self.power.convert_alpha().copy()
self.powerFinal.fill((255, 255, 255, 120), None, pygame.BLEND_RGBA_MULT)
if(self.selection == "power"):
screen.blit(self.power, (powerXPos,powerYPos))
else:
screen.blit(self.powerFinal, (powerXPos,powerYPos))
settingsYPos = self.config["screenHeight"] - 30
settingsXPos = 30
if(self.gearFinal == None):
self.gearFinal = self.gear.convert_alpha().copy()
self.gearFinal.fill((255, 255, 255, 120), None, pygame.BLEND_RGBA_MULT)
if(self.selection == "settings"):
screen.blit(self.gear, (settingsXPos,settingsYPos))
else:
screen.blit(self.gearFinal, (settingsXPos,settingsYPos))
def __init__(self, screen, suspend):
print("Main Menu Init")
self.screen = screen
self.suspend = suspend
self.banderole = pygame.Surface((self.config["screenWidth"],80),pygame.SRCALPHA)
self.header = Header.Header(24)
res = ResumeHandler.getResumeFile()
if(res != None and res["mainIndex"] != None and res["main"] != None):
self.currentIndex = res["mainIndex"]
if("useSelection" in self.config["mainMenu"][self.currentIndex] and self.config["mainMenu"][self.currentIndex]["useSelection"] != None):
if(self.config["mainMenu"][self.currentIndex]["useSelection"] == True):
self.openSelection(self.config["mainMenu"][self.currentIndex])
else:
ResumeHandler.clearResume()
else:
self.openSelection(self.config["mainMenu"][self.currentIndex])
else:
if("firstStart" in self.config and self.config["firstStart"]):
self.overlay = InfoOverlay.InfoOverlay("theme/info.png", self.infoCallback)
self.loadSystemImages()
#self.footer = Footer.Footer([("theme/direction.png","select")], [("theme/select_button.png", "options"), ("theme/a_button.png", "open")], (255,255,255))