-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
124 lines (100 loc) · 3.2 KB
/
main.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
import imgui
import imguihelper
import os
import _nx
import runpy
import sys
import time
from imgui.integrations.nx import NXRenderer
from nx.utils import clear_terminal
import traceback
sys.argv = [""] # workaround needed for runpy
def colorToFloat(t):
nt = ()
for v in t:
nt += ((1/255) * v, )
return nt
# (r, g, b)
FOLDER_COLOR = colorToFloat((230, 126, 34))
PYFILE_COLOR = colorToFloat((46, 204, 113))
FILE_COLOR = colorToFloat((41, 128, 185))
ERROR = ""
TILED_DOUBLE = 1
def run_python_module(path: str):
global ERROR
# clear both buffers
imguihelper.clear()
imguihelper.clear()
_nx.gfx_set_mode(TILED_DOUBLE)
clear_terminal()
try:
runpy.run_path(path, run_name='__main__')
except Exception as e:
ERROR = traceback.format_exc()
imguihelper.initialize()
def main():
global ERROR
renderer = NXRenderer()
currentDir = os.getcwd()
while True:
renderer.handleinputs()
imgui.new_frame()
width, height = renderer.io.display_size
imgui.set_next_window_size(width, height)
imgui.set_next_window_position(0, 0)
imgui.begin("",
flags=imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_SAVED_SETTINGS
)
imgui.text("Welcome to PyNX!")
imgui.text("Touch is supported")
imgui.text("Current dir: " + os.getcwd())
if os.getcwd() != "sdmc:/":
imgui.push_style_color(imgui.COLOR_BUTTON, *FOLDER_COLOR)
if imgui.button("../", width=200, height=60):
os.chdir("..")
imgui.pop_style_color(1)
dirs = []
files = []
for e in os.listdir():
if os.path.isdir(e):
dirs.append(e)
else:
files.append(e)
dirs = sorted(dirs)
files = sorted(files)
for e in dirs:
imgui.push_style_color(imgui.COLOR_BUTTON, *FOLDER_COLOR)
if imgui.button(e + "/", width=200, height=60):
os.chdir(e)
imgui.pop_style_color(1)
for e in files:
if e.endswith(".py"):
imgui.push_style_color(imgui.COLOR_BUTTON, *PYFILE_COLOR)
else:
imgui.push_style_color(imgui.COLOR_BUTTON, *FILE_COLOR)
if imgui.button(e, width=200, height=60) and e.endswith(".py"):
run_python_module(e)
imgui.pop_style_color(1)
imgui.end()
if ERROR:
imgui.set_next_window_size(width, height)
imgui.set_next_window_position(0, 0)
imgui.begin("ERROR",
flags=imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_SAVED_SETTINGS
)
imgui.text(str(ERROR))
if imgui.button("OK", width=200, height=60):
ERROR = ""
imgui.end()
imgui.render()
renderer.render()
renderer.shutdown()
if __name__ == "__main__":
try:
main()
except Exception:
imguihelper.clear()
imguihelper.clear()
_nx.gfx_set_mode(TILED_DOUBLE)
clear_terminal()
traceback.print_exc()