-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
415 lines (351 loc) · 13.7 KB
/
gui.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
###############################################################
# by Crowfunder #
# Copyright my ass but also the GPL-3.0 License #
# Github: https://github.com/Crowfunder #
###############################################################
# External Imports
from webbrowser import open as OpenURL
import PySimpleGUI as sg
import threading
# Internal Imports
from main import RestoreFiles, CheckUpdates, ModuleData, Main, Settings
from main import VERSION_CURRENT, SEPARATOR, FILE_TYPES_LIST
# Define a few constants
URL_DISCORD = 'https://discord.gg/RAf499a'
URL_GITHUB = 'https://github.com/Crowfunder/Kozmadeus'
URL_MANUAL = 'https://github.com/Crowfunder/Kozmadeus/wiki'
URL_BUGS = 'https://github.com/Crowfunder/Kozmadeus/issues'
# noupdate_silent is a flag that disables
# error and "no updates available" popups
def WindowUpdates(noupdate_silent):
try:
update_data = CheckUpdates()
if update_data['current'] != update_data['fetched']:
sg.Popup('Updates available! \nDownload at: '
'https://github.com/Crowfunder/Kozmadeus/releases\n\n'
f'Current version: {update_data["current"]}\n'
f'New version: {update_data["fetched"]}',
icon='assets/kozmadeus.ico', title='Updates Found',
font=('Helvetica', 11))
else:
if not noupdate_silent:
sg.Popup('Kozmadeus is up to date!', icon='assets/kozmadeus.ico',
title='Up to date', font=('Helvetica', 11))
except Exception as e:
if not noupdate_silent:
sg.PopupError('Unable to fetch updates!\n'
'Check your internet connection.\n', e,
icon='assets/kozmadeus.ico', title='Error',
font=('Helvetica', 11))
def WindowModules():
module_data = ModuleData()
layout = [[sg.Text('Loaded Modules:', size=(16,1), font=('Helvetica', 14),
justification='c')], [sg.HorizontalSeparator()]]
layout_column = []
for data in module_data:
layout_column.append([sg.Text(data)])
layout_column.append([sg.HorizontalSeparator()])
layout.append([sg.Column(layout_column, scrollable=True, vertical_scroll_only=True,
expand_y=True, expand_x=True)])
window_modules = sg.Window('Module Data', layout, element_justification='c',
icon='assets/kozmadeus.ico', finalize=True, size=(350, 600))
window_modules.bind('<Escape>', 'Exit')
window_modules.bring_to_front()
while True:
event, values = window_modules.Read()
if event is None or event == 'Exit':
window_modules.close()
break
def WindowAbout():
layout_credits = [
[
sg.Text('Credits:', size=(16,1))
],
[
sg.Push(),
sg.Text('- Puzovoz (Author of Bootshuze)\n'
'- XanTheDragon (Substantive Support)\n'
'- Kirbeh (Current Logo)\n'
'- Whoever gave Kozma her name\n '
'(Enabled me to make that joke)',
font=('Helvetica', 11)),
sg.Push()
],
]
layout_about = [
[
sg.Text('About', size=(24,1),
justification='c', font=('Helvetica', 15))
],
[
sg.Push(),
sg.Text('Script written with love, by Crowfunder\n'
'Distributed under GPL-3.0 License',
font=('Helvetica', 11)),
sg.Push(),
sg.VPush()
]
]
layout = [
[
sg.Frame(title='', layout=layout_about)
],
[
sg.Frame(title='', layout=layout_credits)
],
[
sg.Button('Kozmadeus\n Github', size=(16,2), key='_URL-GITHUB_'),
sg.Push(),
sg.Button('Chat on\n Discord', size=(16,2), key='_URL-DISCORD_')
]
]
window_about = sg.Window('About', layout, element_justification='c',
icon='assets/kozmadeus.ico', finalize=True)
window_about.bind('<Escape>', 'Exit')
window_about.bring_to_front()
while True:
event, values = window_about.Read()
if event is None or event == 'Exit':
window_about.close()
break
elif event == '_URL-GITHUB_':
OpenURL(URL_GITHUB)
elif event == '_URL-DISCORD_':
OpenURL(URL_DISCORD)
def GuiMenu():
# Define the shortcut menubar buttons as strings
# to shorten the events-related code
menubar_open = 'Open Ctrl-O'
menubar_clear_q = 'Clear Queue Ctrl-Q'
menubar_clear_c = 'Clear Console Ctrl-R'
# Predefining file_names as empty list
# Necessary to make Listbox work
file_names = []
# Set the PySimpleGUI Theme
sg.theme('DarkGrey')
# Define all UI components
menubar = [
[
'&File',
[
f'&{menubar_open}',
f'&{menubar_clear_q}'
]
],
[
'&Edit',
[
f'&{menubar_clear_c}'
]
],
[
'&Options',
[
'&Modules',
'&Restore Files',
'&Check for Updates'
]
],
[
'&Help',
[
'&About',
'&Manual',
'&Report a Bug'
]
]
]
frame_import_opts = [
[
sg.Checkbox('Strip armature tree data', visible=True, key='_STRIP-ARMATURE-TREE_',
tooltip='Necessary for reimporting armors')
],
[
sg.Combo(['Opt1', 'Opt2'], readonly=True, visible=False, default_value='Opt1')
]
]
frame_output_type = [
[
sg.Radio('Articulated','OUTPUT_TYPE', default=True,
key='_ARTICULATED-MODE_',
tooltip='Output model as Articulated type\n'
'used for most cases'),
sg.Radio('Static', 'OUTPUT_TYPE', key='_STATIC-MODE_',
tooltip='Output model as Static type\n'
'used most notably for importing armors'),
sg.Radio('Animations\nOnly', 'OUTPUT_TYPE', key='_ANIMATION-MODE_',
tooltip='Output just the model animations (if they exist)',
visible=False)
]
]
frame_files_list = [
[
sg.Listbox(size=(30,10), horizontal_scroll=True,
values=[], key='_FILES_', enable_events=True,
tooltip='Click on model to remove it from list',
expand_x=True)
]
]
column_left = [
[
sg.Image('assets/kozmadeus_full.png', subsample=3)
],
[
sg.Text('Select models to process'),
sg.Input(key='_FILEBROWSE_', enable_events=True, visible=False),
sg.FilesBrowse(file_types=FILE_TYPES_LIST, target='_FILEBROWSE_', size=[10,1]),
],
[
sg.Frame(layout=frame_import_opts, title='Options', expand_x=True)
],
[
sg.Frame(layout=frame_output_type, title='Output Type', expand_x=True)
],
[
sg.Button('Submit', expand_x=True),
],
[
sg.Frame(layout=frame_files_list, title='Selected Files', expand_x=True)
]
]
column_right = [
[
sg.Multiline(expand_x=True, expand_y=True, key='_OUTPUT_',
auto_refresh=True, write_only=True, autoscroll=True,
reroute_stdout=True, reroute_stderr=True,
echo_stdout_stderr=True, disabled=True)
],
[
sg.Button('Clear Console', pad=((5,200),(0,0))),
sg.Text('', text_color='lawn green', key='_STATUS_', size=(5,1)),
]
]
layout = [
[
sg.Menu(menubar),
sg.Frame(layout = column_left, title=''),
sg.Frame(layout = column_right, title='Console', expand_x=True, expand_y=True)
]
]
window = sg.Window(f'Kozmadeus {VERSION_CURRENT}', layout,
element_justification='c', icon='assets/kozmadeus.ico', finalize=True)
# Define simpler to read shortcut events
window.bind('<Control-o>', 'Ctrl-O')
window.bind('<Control-q>', 'Ctrl-Q')
# Connect menubar events with existing ones
window.bind('<Control-r>', 'Clear Console')
window.bind('menubar_clear_c', 'Clear Console')
window.bind('<Return>', 'Submit')
# Bring window to front on start
window.bring_to_front()
# Create an Exception Hook for threads
def ThreadExceptHook(data):
nonlocal window
window.write_event_value('_THREAD-ERROR_', data.exc_value)
threading.excepthook = ThreadExceptHook
# Lock certain options when processing files to prevent
# absolute buffons from breaking the script
processing_lock = False
# Refresh the buttons and locks
# when the loop cycle is done
def RefreshWindowButtons():
nonlocal window
nonlocal processing_lock
window['Submit'].Update(disabled=False)
window.bind('<Return>', 'Submit')
processing_lock = False
window.ding()
# Check for updates with noupdate_silent flag
WindowUpdates(True)
# Main events and values loop
while True:
try:
event, values = window.Read()
window.refresh()
# Program exit event
if event is None:
break
# Files Queue related events
elif event == '_FILEBROWSE_':
file_names = values['_FILEBROWSE_'].split(';')
window['_FILES_'].Update(file_names)
elif values['_FILES_']:
file_names.remove(values['_FILES_'][0])
window['_FILES_'].Update(file_names)
# Menubar shortcut related events
elif event in (menubar_open , 'Ctrl-O'):
file_names = sg.PopupGetFile('file to open', no_window=True,
multiple_files=True,
file_types=FILE_TYPES_LIST)
# For some reason, PopupGetFile returns tuple
# Needs conversion to list
file_names = list(file_names)
window['_FILES_'].Update(file_names)
elif event in (menubar_clear_q, 'Ctrl-Q'):
file_names = []
window['_FILES_'].Update(file_names)
# Menubar remaining events
elif event == 'Modules':
if not processing_lock:
processing_lock = True
WindowModules()
processing_lock = False
elif event == 'Restore Files':
if not processing_lock:
processing_lock = True
print('Restoring template files...')
RestoreFiles()
print('Success!')
print(SEPARATOR)
elif event == 'Check for Updates':
WindowUpdates(False)
elif event == 'Manual':
OpenURL(URL_MANUAL)
elif event == 'About':
window.disable()
WindowAbout()
window.enable()
window.bring_to_front()
elif event == 'Report a Bug':
OpenURL(URL_BUGS)
# Thread Events
elif event == '_THREAD-ERROR_':
raise Exception(values['_THREAD-ERROR_'])
elif event == '_THREAD-COMPLETE_':
window['_STATUS_'].Update('Done!')
window['_STATUS_'].Update(text_color='lawn green')
RefreshWindowButtons()
# Button related events and others
elif event in (menubar_clear_c, 'Ctrl-R', 'Clear Console'):
window['_OUTPUT_'].Update('')
### BEGIN SUBMIT
elif event == 'Submit':
window['_STATUS_'].Update('')
# Get the user settings
if window['_ARTICULATED-MODE_'].Get():
mode = 'Articulated'
elif window['_STATIC-MODE_'].Get():
mode = 'Static'
strip_armature_tree = window['_STRIP-ARMATURE-TREE_'].Get()
# Start processing the files
if file_names:
# Lock the "Submit" button and certain functions
window['Submit'].Update(disabled=True)
window.bind('<Return>', 'null')
processing_lock = True
settings = Settings(file_names=file_names, model_mode=mode,
no_export_file=False, strip_armature_tree=strip_armature_tree)
# Long boi taken directly from PySimpleGUI Cookbook
# Creates a separate thread to prevent the program from freezing
window.start_thread(lambda: Main(settings), '_THREAD-COMPLETE_')
else:
print('Please select a file!')
### END SUBMIT
except Exception as exception:
window['_STATUS_'].Update('Error!')
window['_STATUS_'].Update(text_color='red')
print('Unhandled exception has occured:\n', exception)
print(SEPARATOR)
RefreshWindowButtons()
if __name__ == '__main__':
GuiMenu()