-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
927 lines (832 loc) · 36.3 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from layout.layout import Ui_MainWindow
import core.main, core.convert, sys, os, random, math, inspect, re
version = core.main.version
keys = list(core.main.script().keys)
keys.pop(0)
keys = tuple(keys)
# Global variables for use
# -Qt Stuff
height = 0
width = 0
fileMenu = None
editMenu = None
viewMenu = None
textEdit = None
table = None
functionBox = None
addFrame = None
addFunction = None
themes = [{
'name':'Default',
'data':'',
},]
# -PyTAS stuff
frames = []
functions = []
default = """def roll_cancel():
script.input(1,('KEY_B',),0,0,0,0)
script.input(5,('KEY_ZL',),0,0,0,0)
script.input(35,('KEY_ZL','KEY_Y',),0,0,0,0)
script.input(2,('KEY_X','KEY_A',),30000,0,0,0)
script.input(1,('KEY_A',),30000,0,0,0)
script.input(1,('KEY_A',),30000,0,0,0)
script.input(1,('KEY_A',),30000,0,0,0)
script.input(1,('KEY_A',),30000,0,0,0)
# Put your inputs here!
def main():
for i in range(5):
roll_cancel()
script.wait(60)
# All Python syntax should work!
from core.main import script
script = script()
script.run(main)
"""
class GUI(Ui_MainWindow):
def __init__(self,MainWindow):
self.MainWindow = MainWindow
def notice(self):
notice = QMessageBox()
notice.setText('Warning!\nChanging files using the editor may result in unwanted changes in your file.\nPlease make sure you keep a backup before you save any changes.\nThank you for using my editor! -Delta/Mi460')
notice.setWindowTitle("Warning!")
notice.exec_()
def selfService(self):
# Assign variables for use
self.assign_variables()
# Setup GUI components
self.setup_window()
self.create_menu()
self.getTheme()
# Open file
while not self.openFile():
pass
def assign_variables(self):
global fileMenu, editMenu, viewMenu
fileMenu = self.menubar.addMenu('&File')
editMenu = self.menubar.addMenu('&Edit')
viewMenu = self.menubar.addMenu('&View')
global textEdit, table, functionBox, addFrame, addFunction
textEdit = self.tabWidget.findChild(QWidget,'tab2').findChild(QTextEdit,'textEdit')
table = self.tabWidget.findChild(QWidget,'tab1').findChild(QTableWidget,'table')
functionBox = self.tabWidget.findChild(QWidget,'tab1').findChild(QComboBox,'functionBox')
addFrame = self.tabWidget.findChild(QWidget,'tab1').findChild(QPushButton,'addFrame')
addFunction = self.tabWidget.findChild(QWidget,'tab1').findChild(QPushButton,'addFunction')
# Actions
self.MainWindow.closeEvent = self.closeEvent
def setup_window(self):
global height, width
# Window formatting
screen = app.primaryScreen()
size = screen.availableGeometry()
height = int(size.height())
width = int(size.width())
self.MainWindow.setWindowTitle(f'PyTAS Editor v{version}')
self.MainWindow.setFixedSize(width, height)
self.MainWindow.showMaximized()
# Function box formatting
functionBox.clear()
functionBox.addItem('main')
# Tabs formatting
self.tabWidget.setGeometry(QRect(0, 0, int(width), int(height - 20)))
self.tabWidget.currentChanged.connect(self.tabUpdate)
# Table formatting
table.setGeometry(QRect(int(width / 130), int(height / 23), int(width - width / 45), int(height - height / 7)))
self.row_count = 1
header = table.horizontalHeader()
header.setDefaultAlignment(Qt.AlignCenter)
header.setSectionResizeMode(QHeaderView.Stretch)
table.setRowCount(self.row_count)
table.setColumnCount(len(keys) + 4)
table.setSelectionBehavior(QAbstractItemView.SelectRows)
item = QTableWidgetItem("Frame")
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
table.setItem(0,0, item)
item = QTableWidgetItem("L-Stick")
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(0,1, item)
item = QTableWidgetItem("R-Stick")
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(0,2, item)
item = QTableWidgetItem("<>")
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(0,len(keys) + 3, item)
for i in range(len(keys)):
item = QTableWidgetItem(keys[i].replace('KEY_',''))
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(0,i + 3, item)
table.cellClicked.connect(self.tableUpdate)
# Buttons formatting
addFrame.setGeometry(QRect(int(width - width / 13), int(height - height / 10), 113, 32))
addFrame.clicked.connect(self.add_Frame)
addFunction.setGeometry(QRect(int(width - width / 7.5), int(height - height / 10), 113, 32))
addFunction.clicked.connect(self.add_Function)
# TextEdit formatting
textEdit.setGeometry(QRect(int(width / 130), int(width / 70), int(width - width / 45), int(height - height / 8)))
# FunctionBox formatting
functionBox.setGeometry(QRect(0, 0, int(width - width / 130), int(height / 35)))
functionBox.activated.connect(self.fill_table)
def create_menu(self):
# File menu
openFile = QAction('&Open File', self.MainWindow)
openFile.setShortcut('Ctrl+O')
openFile.triggered.connect(self.openFile)
fileMenu.addAction(openFile)
saveFile = QAction('&Save File', self.MainWindow)
saveFile.setShortcut('Ctrl+S')
saveFile.triggered.connect(self.askSave)
fileMenu.addAction(saveFile)
runFile = QAction('&Run Script', self.MainWindow)
runFile.setShortcut('F5')
runFile.triggered.connect(self.runFile)
fileMenu.addAction(runFile)
exit = QAction('&Quit', self.MainWindow)
exit.setShortcut('Ctrl+Q')
exit.triggered.connect(self.exit)
fileMenu.addAction(exit)
# Edit menu
newFrame = QAction('&New Frame', self.MainWindow)
newFrame.setShortcut('Ctrl+N')
newFrame.triggered.connect(self.add_Frame)
editMenu.addAction(newFrame)
# View menu
newTheme = QAction('&Change Theme', self.MainWindow)
newTheme.setShortcut('Ctrl+K')
newTheme.triggered.connect(self.themeUpdate)
viewMenu.addAction(newTheme)
def switch_tab1():
self.tabWidget.setCurrentIndex(0)
tab1 = QAction('&Switch To Visual', self.MainWindow)
tab1.setShortcut('Ctrl+1')
tab1.triggered.connect(switch_tab1)
def switch_tab2():
self.tabWidget.setCurrentIndex(1)
tab2 = QAction('&Switch To Programmatic', self.MainWindow)
tab2.setShortcut('Ctrl+2')
tab2.triggered.connect(switch_tab2)
viewMenu.addAction(tab1)
viewMenu.addAction(tab2)
def setupFileEnv(self):
buff = buffer
for line in buff.split('\n'):
if '.run(' in line:
buff = buff.replace(line,'')
buff = f'{buff}\n'
'from core.main import script\nscript = script()'
fileEnvironment = {}
exec(buff, fileEnvironment, None)
return fileEnvironment, buff
def fill_text(self):
textEdit.textChanged.connect(self.textUpdate)
textEdit.setPlainText(buffer)
def fill_table(self, new:bool = 'False'):
# Receive justified data of file from core.main
global buffer, frames, functions, table, functionBox
if 'from core.main' not in buffer:
raise Exception('Invalid PyTAS file!')
elif buffer.count('main()') > 1:
raise Exception('Cannot read, infinite recursion')
fileEnvironment, buff = self.setupFileEnv()
vals = fileEnvironment['script'].run(fileEnvironment['main'],output,True)
# Begin interpreting functions
functions = []
for i in fileEnvironment:
if i in ('__builtins__','script','main'):
continue
fileEnvironment['script'].__init__()
f_vals = fileEnvironment['script'].run(fileEnvironment[i],output,True)
if not f_vals:
continue
functions.append({
'name':i,
'data':f_vals,
'frames':f_vals[-1]['Frame'],
'length':len(f_vals),
})
# Get functionBox
func = functionBox.currentText()
if new == 'True':
func = 'main'
# Begin interpreting main
frames = []
if func != 'main':
if func == 'Create New Function':
func, response = QInputDialog.getText(self.MainWindow, 'Function Name', 'Enter the function name:')
func = func.replace(' ','')
if not response or not func or not func.isalpha():
functionBox.setCurrentText('main')
self.fill_table()
return
buffer = f"def {func}():\n script.input(1,('NONE',),0,0,0,0)\n\n{buffer}"
functionBox.addItem(func)
functionBox.setCurrentText(func)
self.fill_table()
return
vals = next(n for n in functions if n['name'] == func)['data']
n = 0
while n < len(vals):
i = vals[n]
if i['Caller'] not in (func,'wait') and func == 'main':
n += next(n for n in functions if n['name'] == i['Caller'])['length']
frames.append([True,i['Caller']])
else:
frames.append([False,i])
n += 1
# Clear functionBox
functionBox.clear()
# Begin filling functionBox
functionBox.addItem('main')
functionBox.addItems([ i['name'] for i in functions ])
functionBox.addItem('Create New Function')
if func != 'main':
functionBox.setCurrentText(func)
# Clear table
self.row_count = 1
table.setRowCount(self.row_count)
# Begin filling table
self.row_count += len(frames)
table.setRowCount(self.row_count)
for n in range(1,len(frames) + 1):
i = frames[n - 1]
# Frame/Function
if not i[0]:
item = QSpinBox()
item.setMinimum(0)
item.setMaximum(999999)
item.setValue(i[1]['Frame'])
item.valueChanged.connect(self.spinUpdate)
else:
item = QComboBox()
item.addItems([ i['name'] for i in functions if i['name'] != func and func not in self.getSource(i['name']) ])
item.setCurrentText(i[1])
item.currentIndexChanged.connect(self.comboUpdate)
table.setCellWidget(n, 0, item)
# Sticks
h = 1
for j in ['LeftStick','RightStick']:
item = QTableWidgetItem()
if i[0]:
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
else:
item.setText(i[1][j])
table.setItem(n, h, item)
h += 1
# Keys
for j in range(len(keys)):
item = QTableWidgetItem()
if not i[0]:
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
if keys[j] in i[1]['Key'].split(';'):
item.setCheckState(Qt.Checked)
else:
item.setCheckState(Qt.Unchecked)
else:
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
table.setItem(n, j + 3, item)
item = QTableWidgetItem("X")
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.table.setItem(n,len(keys) + 3, item)
# Fill programmatic tab
self.fill_text()
# Properly setup variables
self.sort_table()
def sort_table(self):
global table, frames
# Interpret table
frames = []
possible_frame = 0
func = functionBox.currentText()
for i in range(1,table.rowCount()):
val = table.cellWidget(i,0)
if isinstance(val,QSpinBox):
list = []
for n in range(3,len(keys) + 3):
if table.item(i,n).checkState() == Qt.Checked:
list.append(keys[n-3])
key = ''
for n in range(len(list)):
if n > 0:
key = f'{key};'
key = f'{key}{list[n]}'
if key == '':
key = 'NONE'
lstick = table.item(i,1)
rstick = table.item(i,2)
frame = val.value()
if possible_frame < frame:
possible_frame = frame
elif possible_frame == frame:
frame += 1
possible_frame = frame
else:
frame = possible_frame + 1
possible_frame = frame
frames.append([False,{
'Frame':frame,
'Key':f'{key}',
'LeftStick':f'{lstick.text()}',
'RightStick':f'{rstick.text()}',
'Caller':func,
}])
else:
for n in functions:
if val.currentText() == n['name']:
l = n.copy()
l['Frame'] = possible_frame
frames.append([True,l])
possible_frame += l['frames']
break
# Reorganize frames list
def ex(elem):
return int(elem[1]['Frame'])
frames = sorted(frames,key=ex)
last = 0
# Make sure functions that start on the same frame as an input always go after
for n in range(len(frames)):
i = frames[n]
if i[1]['Frame'] == last and not i[0]:
i.insert(n - 1, l.pop(n))
# Remove any frames that are placed inside of a function
for i in frames:
if i[0]:
for n in range(i[1]['Frame'] + 1,i[1]['Frame'] + i[1]['frames']):
for e in [ frame for frame in frames if frame[1]['Frame'] == n ]:
e[1]['Frame'] = i[1]['Frame'] + i[1]['frames'] + 1
while len([ frame for frame in frames if frame[1]['Frame'] == e[1]['Frame'] and frame != e ]) > 0:
e[1]['Frame'] += 1
return frames
def write_table(self, frames):
global buffer, functions
func = functionBox.currentText()
if func != 'main':
function = self.getSource(func)
# Interpret frames
char = "'"
text = f"def {func}():\n"
lastframe = 0
for i in frames:
if not i[0]:
i = i[1]
text = f"{text} script.input({i['Frame'] - lastframe},({','.join([ f'{char}{h}{char}' for h in i['Key'].split(';') ])},),{','.join(i['LeftStick'].split(';'))},{','.join(i['RightStick'].split(';'))})\n"
lastframe = i['Frame']
else:
i = i[1]
lastframe = 0
for n in i['data']:
text = f"{text} script.input({n['Frame'] - lastframe},({','.join([ f'{char}{h}{char}' for h in n['Key'].split(';') ])},),{','.join(n['LeftStick'].split(';'))},{','.join(n['RightStick'].split(';'))})\n"
lastframe = int(n['Frame'])
if len(frames) > 0:
text = text.rstrip()
buffer = buffer.replace(function,text)
fileEnvironment = self.setupFileEnv()[0]
fileEnvironment['script'].__init__()
f_vals = fileEnvironment['script'].run(fileEnvironment[func],output,True)
for i in functions:
if i['name'] == func:
i = {
'name':func,
'data':f_vals,
'frames':f_vals[-1]['Frame'],
'length':len(f_vals),
}
break
else:
text = ''
buffer = buffer.replace(function,text)
functionBox.setCurrentText('main')
list = [ i for i in functions if i['name'] == func ]
functions.remove(list[0])
buffer = buffer.replace(f'{func}()','pass')
self.fill_table()
return
# Interpret frames
text = "# This is an auto-generated PyTAS file"
char = "'"
for i in functions:
lastframe = 0
text = f"{text}\ndef {i['name']}():\n"
for n in i['data']:
if int(n['Frame']) <= lastframe:
continue
if n['Caller'] == i['name'] or n['Caller'] == 'wait':
text = f"{text} script.input({int(n['Frame']) - lastframe},({','.join([ f'{char}{h}{char}' for h in n['Key'].split(';') ])},),{','.join(n['LeftStick'].split(';'))},{','.join(n['RightStick'].split(';'))})\n"
lastframe = int(n['Frame'])
else:
for l in n['data']:
text = f"{text} script.input({l['Frame'] - lastframe},({','.join([ f'{char}{h}{char}' for h in l['Key'].split(';') ])},),{','.join(l['LeftStick'].split(';'))},{','.join(l['RightStick'].split(';'))})\n"
lastframe = int(l['Frame'])
# Next, let's create main
text = f"{text}\ndef main():\n"
lastframe = 0
for i in frames:
if not i[0]:
i = i[1]
if i['Key'] != 'NONE' or i['LeftStick'] != '0;0' or i['RightStick'] != '0;0':
text = f"{text} script.input({i['Frame'] - lastframe},({','.join([ f'{char}{h}{char}' for h in i['Key'].split(';') ])},),{','.join(i['LeftStick'].split(';'))},{','.join(i['RightStick'].split(';'))})\n"
else:
text = f"{text} script.wait({i['Frame'] - lastframe})\n"
lastframe = i['Frame']
else:
i = i[1]
text = f"{text} {i['name']}()\n"
lastframe += i['frames']
if len(frames) == 0:
text = f"{text} pass"
text = f"{text}\nfrom core.main import script\nscript = script()\nscript.run(main)\n"
# Write to buffer
buffer = text
def getSource(self, function):
fileEnvironment, buff = self.setupFileEnv()
lines = buff.split('\n')
# Some of this is sampled from the Python inspect module
# I did not fully author the code from here -
object = fileEnvironment[function].__code__
lnum = object.co_firstlineno - 1
pat = re.compile(r'^\s*def\s')
while lnum > 0:
if pat.match(lines[lnum]): break
lnum = lnum - 1
return '\n'.join(inspect.getblock(lines[lnum:])) # - to here
def add_Frame(self):
# Add row
self.row_count += 1
table.setRowCount(self.row_count)
# Set frame
item = QSpinBox()
item.setMinimum(0)
item.setMaximum(999999)
if len(frames) > 0:
if frames[-1]:
frame = frames[-1][1]['Frame'] + 1
else:
frame = frames[-1][1]['Frame'] + frames[-1][1]['frames'] + 1
else:
frame = 1
item.setValue(frame)
item.valueChanged.connect(self.spinUpdate)
table.setCellWidget(self.row_count - 1, 0, item)
# Sticks
h = 1
for j in ['LeftStick','RightStick']:
item = QTableWidgetItem()
item.setText('0;0')
table.setItem(self.row_count - 1, h, item)
h += 1
# Set keys
for j in range(len(keys)):
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setCheckState(Qt.Unchecked)
table.setItem(self.row_count - 1, j + 3, item)
# Update table
self.tableUpdate(0,0)
def add_Function(self):
funcs = [ i['name'] for i in functions if i['name'] != functionBox.currentText() and functionBox.currentText() not in self.getSource(i['name']) ]
if not len(funcs) > 0:
return
if len(funcs) > 1:
item, response = QInputDialog.getItem(self.MainWindow, "Select Function", "Which function would you like to insert?", funcs, 0, False)
if response and item:
func = item
else:
return
else:
func = funcs[0]
if table.rowCount() > 1:
frame, response = QInputDialog.getInt(self.MainWindow, 'Insert Function', 'After which frame would you like to insert the function?',0,0,frames[-1][1]['Frame'])
if not response:
return
index = self.row_count
for i in range(1,table.rowCount()):
cell = table.cellWidget(i,0)
if isinstance(cell,QSpinBox):
val = cell.value()
if val > frame:
index = i
break
else:
index = 1
# Add row
self.row_count += 1
table.insertRow(index)
# Set frame
item = QComboBox()
item.addItems(funcs)
item.setCurrentText(func)
item.currentIndexChanged.connect(self.comboUpdate)
table.setCellWidget(index, 0, item)
# Sticks
h = 1
for j in ['LeftStick','RightStick']:
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
table.setItem(index, h, item)
h += 1
# Set keys
for j in range(len(keys)):
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
table.setItem(index, j + 3, item)
# Update table
self.tableUpdate(0,0)
def changeStick(self, row, col):
global table
cell = table.item(row,col)
first = table.cellWidget(row,0)
if isinstance(first,QComboBox) or not cell:
return
if col == 1:
stick = 'LeftStick'
else:
stick = 'RightStick'
data = [ int(axis) for axis in cell.text().split(';') ]
x = data[0]
y = data[1]
# Begin creating window
class Ui_StickControl(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
self.MainWindow.setWindowTitle(f'Frame #{first.value()} - {stick}')
self.MainWindow.setStyleSheet('')
self.width = int(width / 5)
self.height = int(height / 2)
self.center = int(self.width / 2)
self.radii = int(self.center / 1.25)
if self.height < self.width:
self.height = int(self.width * 1.5)
self.MainWindow.setFixedSize(self.width, self.height)
self.MainWindow.paintEvent = self.paintEvent
self.MainWindow.mousePressEvent = self.mousePressEvent
self.MainWindow.mouseReleaseEvent = self.mouseReleaseEvent
self.MainWindow.mouseMoveEvent = self.mouseMoveEvent
self.MainWindow.setAutoFillBackground(True)
self.centralwidget = QWidget(self.MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.x = x
self.y = y
self.mouseClick = False
self.xSpin = QSpinBox(self.centralwidget)
self.xSpin.setGeometry(QRect(int(self.radii / 2), self.center + int(self.radii * 1.2), int(self.radii / 1.5), int(self.radii / 4)))
self.xSpin.setMinimum(-32767)
self.xSpin.setMaximum(32767)
self.xSpin.setValue(x)
self.xSpin.valueChanged.connect(self.xSpinUpdate)
self.ySpin = QSpinBox(self.centralwidget)
self.ySpin.setGeometry(QRect(int(self.radii * (4/3)), self.center + int(self.radii * 1.2), int(self.radii / 1.5), int(self.radii / 4)))
self.ySpin.setMinimum(-32767)
self.ySpin.setMaximum(32767)
self.ySpin.setValue(y)
self.ySpin.valueChanged.connect(self.ySpinUpdate)
self.angSpin = QSpinBox(self.centralwidget)
self.angSpin.setGeometry(QRect(int(self.radii / 2), self.center + int(self.radii * 1.2 + self.radii / 4), int(self.radii / 1.5), int(self.radii / 4)))
self.angSpin.setMinimum(0)
self.angSpin.setMaximum(359)
self.angSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[0])
self.angSpin.valueChanged.connect(self.angSpinUpdate)
self.magSpin = QSpinBox(self.centralwidget)
self.magSpin.setGeometry(QRect(int(self.radii * (4/3)), self.center + int(self.radii * 1.2 + self.radii / 4), int(self.radii / 1.5), int(self.radii / 4)))
self.magSpin.setMinimum(0)
self.magSpin.setMaximum(32767)
self.magSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[1])
self.magSpin.valueChanged.connect(self.magSpinUpdate)
self.button = QPushButton(self.centralwidget)
self.button.setText("Done")
self.button.setGeometry(QRect(int(self.width / 2 - self.width / 1.5 / 2), self.height - int(self.height / 7), int(self.width / 1.5), int(self.height / 8)))
def paintEvent(self, event):
self.centralwidget.painter = QPainter()
self.centralwidget.painter.begin(self.MainWindow)
self.centralwidget.painter.setPen(QPen(QColor(0,0,0), 3, Qt.SolidLine))
self.centralwidget.painter.setBrush(QColor(40, 40, 40))
self.centralwidget.painter.drawEllipse(QPoint(self.center,self.center), self.radii, self.radii)
self.centralwidget.painter.drawLine(self.center - self.radii,self.center,self.center + self.radii,self.center)
self.centralwidget.painter.drawLine(self.center,self.center - self.radii,self.center,self.center + self.radii)
self.centralwidget.painter.setPen(QPen(QColor(255,0,0), 1, Qt.SolidLine))
self.centralwidget.painter.setBrush(QColor(255, 0, 0))
self.centralwidget.painter.drawEllipse(QPoint(int(self.center + (self.x * self.radii / 32767)),int(self.center + (self.y * -1 * self.radii / 32767))), 5, 5)
self.centralwidget.painter.end()
def xSpinUpdate(self):
if math.sqrt( (int(self.center + (self.xSpin.value() * self.radii / 32767)) - self.center)**2 + (int(self.center + (self.ySpin.value() * -1 * self.radii / 32767)) - self.center)**2 ) <= self.radii:
self.x = self.xSpin.value()
ang,mag = core.convert.makeStickPolar(self.x,self.y)
self.blockSignals(True)
self.angSpin.setValue(ang)
self.magSpin.setValue(mag)
self.blockSignals(False)
else:
self.xSpin.setValue(self.x)
self.MainWindow.update()
def ySpinUpdate(self):
if math.sqrt( (int(self.center + (self.xSpin.value() * self.radii / 32767)) - self.center)**2 + (int(self.center + (self.ySpin.value() * -1 * self.radii / 32767)) - self.center)**2 ) <= self.radii:
self.y = self.ySpin.value()
ang,mag = core.convert.makeStickPolar(self.x,self.y)
self.blockSignals(True)
self.angSpin.setValue(ang)
self.magSpin.setValue(mag)
self.blockSignals(False)
else:
self.ySpin.setValue(self.y)
self.MainWindow.update()
def angSpinUpdate(self):
x,y = core.convert.makeStickCartesian(self.angSpin.value(),self.magSpin.value())
if math.sqrt( (int(self.center + (x * self.radii / 32767)) - self.center)**2 + (int(self.center + (y * -1 * self.radii / 32767)) - self.center)**2 ) <= self.radii:
self.x = x
self.y = y
self.blockSignals(True)
self.xSpin.setValue(self.x)
self.ySpin.setValue(self.y)
self.blockSignals(False)
else:
self.angSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[0])
self.MainWindow.update()
def magSpinUpdate(self):
x,y = core.convert.makeStickCartesian(self.angSpin.value(),self.magSpin.value())
if math.sqrt( (int(self.center + (x * self.radii / 32767)) - self.center)**2 + (int(self.center + (y * -1 * self.radii / 32767)) - self.center)**2 ) <= self.radii:
self.x = x
self.y = y
self.blockSignals(True)
self.xSpin.setValue(self.x)
self.ySpin.setValue(self.y)
self.blockSignals(False)
else:
self.angSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[1])
self.MainWindow.update()
def mousePressEvent(self, event):
self.mouseClick = True
self.mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.mouseClick = False
def blockSignals(self,toggle:bool):
self.xSpin.blockSignals(toggle)
self.ySpin.blockSignals(toggle)
self.angSpin.blockSignals(toggle)
self.magSpin.blockSignals(toggle)
def mouseMoveEvent(self, event):
if not self.mouseClick:
return
posx = event.pos().x()
posy = event.pos().y()
if math.sqrt( (posx - self.center)**2 + (posy - self.center)**2 ) <= self.radii:
self.x = int((posx - self.center) * 32767 / self.radii)
self.y = int((posy - self.center) * 32767 / self.radii) * -1
self.xSpin.setValue(self.x)
self.ySpin.setValue(self.y)
self.blockSignals(True)
self.angSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[0])
self.magSpin.setValue(core.convert.makeStickPolar(self.x,self.y)[1])
self.blockSignals(False)
global stickControl
stickControl = QWidget()
def closeEvent(event: QCloseEvent):
stickControl.close()
self.MainWindow.setEnabled(True)
def save():
x = Ui_StickControl.x
y = Ui_StickControl.y
item = QTableWidgetItem()
item.setText(f'{x};{y}')
table.setItem(row, col, item)
self.tableUpdate(0,0)
closeEvent('save')
stickControl.closeEvent = closeEvent
Ui_StickControl = Ui_StickControl()
Ui_StickControl.setupUi(stickControl)
Ui_StickControl.button.clicked.connect(save)
self.MainWindow.setEnabled(False)
stickControl.show()
def removeRow(self, row):
table.removeRow(row)
self.row_count -= 1
def getTheme(self):
with open('./options.txt','r') as file:
data = file.read().split('\n')
try:self.MainWindow.setStyleSheet(next(theme for theme in themes if theme['name'] == data[0])['data'])
except:print('Invalid options.txt')
def writeTheme(self,theme:str='Default'):
with open('./options.txt','w') as file:
file.write(theme)
# Event-based functions
def textUpdate(self):
global buffer
buffer = textEdit.toPlainText()
def tableUpdate(self, row, col):
if col == len(keys) + 3 and row != 0:
self.removeRow(row)
elif 3 > col > 0 and row != 0:
self.changeStick(row, col)
global table
# Sort table, write changes
self.write_table(self.sort_table())
# Fill table
self.fill_table()
def spinUpdate(self):
self.tableUpdate(0,0)
def comboUpdate(self):
self.tableUpdate(0,0)
def tabUpdate(self):
# Fill table
self.fill_table()
def themeUpdate(self):
item, response = QInputDialog.getItem(self.MainWindow, "Select Theme", "Which theme would you like to use?", [ theme['name'] for theme in themes ], 0, False)
if response and item:
self.MainWindow.setStyleSheet(next(theme for theme in themes if theme['name'] == item)['data'])
self.writeTheme(item)
else:
return
def closeEvent(self, event: QCloseEvent):
event.ignore()
self.exit()
# QActions
def openFile(self):
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.ExistingFiles)
dlg.setNameFilters(["PyTAS Script files (*.py)",'nx-TAS files (*.txt)','tiger.lua files (*.lua)'])
dlg.selectNameFilter("PyTAS Script files (*.py)")
if dlg.exec_() == QDialog.Accepted:
global buffer, filename
filename = dlg.selectedFiles()[0]
else:
return False
# Buffer
if filename.endswith('.txt') or filename.endswith('.lua'):
buffer = core.convert.convertFromFile(filename)
buffer = buffer + '\n\nfrom core.main import script\nscript = script()\nscript.run(main)\n'
else:
with open(filename,'r') as file:
buffer = file.read()
# A commonly-recurring function that pulls data from the buffer and writes it to the table
self.fill_table('True')
# Warn users
self.notice()
return True
def runFile(self):
exec(buffer, globals(), None)
notice = QMessageBox()
notice.setText('Process Success!')
notice.setWindowTitle('Process Success!')
notice.setDetailedText(f'File output written to \'{os.path.abspath("./output/" + output + ".txt")}\'')
notice.exec_()
def askSave(self):
with open(filename,'r') as file:
lines = file.read()
if lines == buffer:
return True
dlg = QMessageBox()
dlg.setWindowTitle("Save file")
dlg.setText("Would you like to save your current progress?")
dlg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
ans = dlg.exec()
if ans == QMessageBox.Yes:
# Save file
fileEnvironment = self.setupFileEnv()[0]
inputs = fileEnvironment['script'].run(fileEnvironment['main'],output,True)
if filename.endswith('.txt'):
with open(filename,'w') as file:
file.write(core.convert.nxTAS(inputs).justify())
elif filename.endswith('.lua'):
with open(filename,'w') as file:
file.write(core.convert.tigerlua(inputs).justify())
else:
with open(filename,'w') as file:
file.write(buffer)
return True
elif ans == QMessageBox.No:
return False
else:
return None
def exit(self):
if self.askSave() is None:
return
sys.exit("Closed app")
def getPath(path):
try:
root = sys._MEIPASS
except Exception:
root = os.path.abspath('.')
return os.path.join(root, path)
if __name__ == '__main__':
filename = './script.py'
output = 'script1'
buffer = None
if not os.path.isfile(filename):
with open(filename,'w') as file:
file.write(default)
if not os.path.isfile('./options.txt'):
with open('./options.txt','w') as file:
file.write('Default')
app = QApplication(sys.argv)
for theme in ('Fibers','SyNet','Toolery'):
file = QFile(getPath(f"layout/themes/{theme}.qss"))
file.open(QFile.ReadOnly | QFile.Text)
stream = QTextStream(file)
themes.append({
'name':theme,
'data':stream.readAll(),
})
MainWindow = QMainWindow()
window = GUI(MainWindow)
window.setupUi(MainWindow)
window.selfService()
MainWindow.show()
sys.exit(app.exec_())