-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_editor.py
1795 lines (1429 loc) · 69.9 KB
/
data_editor.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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import widgets.tooltip_list as ttl
from PySide6 import QtCore, QtGui, QtWidgets
from collections import OrderedDict
from math import inf, degrees, radians
from lib.libbol import (EnemyPoint, EnemyPointGroup, CheckpointGroup, Checkpoint, Route, RoutePoint,
MapObject, KartStartPoint, Area, Camera, BOL, JugemPoint, MapObject,
LightParam, MGEntry, PositionedObject, OBJECTNAMES, REVERSEOBJECTNAMES, MUSIC_IDS, REVERSE_MUSIC_IDS,
SWERVE_IDS, REVERSE_SWERVE_IDS, REVERSE_AREA_TYPES,
KART_START_POINTS_PLAYER_IDS, REVERSE_KART_START_POINTS_PLAYER_IDS,
read_object_parameters, all_same_type, get_average_obj)
from lib.vectors import Vector3
from lib.model_rendering import Minimap
from lib.libbol import Rotation
def calc_width(fontmetric, minval, maxval, is_spinbox=False):
digits = max(len(str(minval)), len(str(maxval)))
if is_spinbox:
digits += 2
return fontmetric.horizontalAdvanceChar("8") * digits
def load_parameter_names(objectname):
try:
data = read_object_parameters(objectname)
parameter_names = data["Object Parameters"]
if len(parameter_names) != 8:
raise RuntimeError("Not enough or too many parameters: {0} (should be 8)".format(
len(parameter_names)))
assets = data["Assets"]
tooltips = data.get("Tooltips", [])
tooltips += [''] * (8 - len(tooltips))
tooltips = [
ttl.markdown_to_html(re.sub(r'[^\x00-\x7f]', r'', parameter_name).strip(), tool_tip)
if tool_tip else '' for parameter_name, tool_tip in zip(parameter_names, tooltips)
]
widget_types = data.get("Widgets", [])
widget_types += [None] * (8 - len(widget_types))
return tuple(parameter_names), tuple(assets), tuple(tooltips), tuple(widget_types)
except Exception:
return (
tuple(f'Obj Data {i + 1}' for i in range(8)),
tuple(),
tuple([''] * 8),
tuple([None] * 8),
)
def clear_layout(layout):
while layout.count():
child = layout.itemAt(0)
if child.widget():
child.widget().deleteLater()
if child.layout():
clear_layout(child.layout())
child.layout().deleteLater()
layout.takeAt(0)
def find_parent_layout(widget_or_layout) -> QtWidgets.QLayout:
"""
Finds the parent layout of the given widget or layout.
"""
parent_widget = widget_or_layout.parentWidget()
if parent_widget is None:
return None
for layout in parent_widget.findChildren(QtWidgets.QLayout):
for i in range(layout.count()):
layout_item = layout.itemAt(i)
if layout_item.widget() is widget_or_layout or layout_item.layout() is widget_or_layout:
return layout
return None
def set_tool_tip(widget: QtWidgets.QWidget, tool_tip: str):
"""
Sets the tool tip in the given widget, but also in all siblings of the widget.
"""
parent_layout = find_parent_layout(widget)
if parent_layout is not None:
for i in range(parent_layout.count()):
layout_item = parent_layout.itemAt(i)
layout_item_widget = layout_item.widget()
if layout_item_widget is not None:
layout_item_widget.setToolTip(tool_tip)
else:
widget.setToolTip(tool_tip)
class MaskBoxMenu(QtWidgets.QMenu):
def mouseReleaseEvent(self, event: QtGui.QMouseEvent):
action = self.activeAction()
if action is not None and action.isEnabled():
action.trigger()
event.accept()
return
super().mouseReleaseEvent(event)
class MaskBox(QtWidgets.QPushButton):
value_changed = QtCore.Signal(int)
def __init__(self, entries: 'dict[int, (str, str)]'):
super().__init__()
policy = self.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
self.setSizePolicy(policy)
self.entries = entries
self.actions = {}
self.menu = MaskBoxMenu()
for field, (char, label) in entries.items():
action = self.menu.addAction(f'{char} {label}')
action.setCheckable(True)
action.toggled.connect(self._on_action_toggled)
self.actions[field] = action
self.setMenu(self.menu)
def set_value(self, value: int):
for field, action in self.actions.items():
with QtCore.QSignalBlocker(action):
action.setChecked(bool(field & value))
button_label = ''
for field, (char, _label) in self.entries.items():
if field & value:
if button_label:
button_label += ' '
button_label += char
self.setText(button_label)
def _on_action_toggled(self, checked: bool):
_ = checked
value = 0
for field, action in self.actions.items():
if action.isChecked():
value |= field
self.set_value(value)
self.value_changed.emit(value)
class SpinBox(QtWidgets.QSpinBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
policy = self.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
self.setSizePolicy(policy)
def setValueQuiet(self, value: int):
with QtCore.QSignalBlocker(self):
self.setValue(value)
class DoubleSpinBox(QtWidgets.QDoubleSpinBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
policy = self.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
self.setSizePolicy(policy)
self.setDecimals(4)
def setValueQuiet(self, value: float):
with QtCore.QSignalBlocker(self):
self.setValue(value)
class ClickableLabel(QtWidgets.QLabel):
clicked = QtCore.Signal()
def mouseReleaseEvent(self, event):
if self.rect().contains(event.position().toPoint()):
event.accept()
self.clicked.emit()
class CompleterComboBox(QtWidgets.QComboBox):
item_changed = QtCore.Signal(str)
def __init__(self, bound_to, attribute, keyval_dict, parent: QtWidgets.QWidget = None):
super().__init__(parent=parent)
policy = self.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
self.setSizePolicy(policy)
self.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToMinimumContentsLengthWithIcon)
for val in keyval_dict:
self.addItem(val)
self.setInsertPolicy(QtWidgets.QComboBox.NoInsert)
self.setEditable(True)
lineedit = self.lineEdit()
def on_editingFinished():
key = lineedit.text()
if key not in keyval_dict:
# It may be a partial match in the completer. If not a valid key, revert back to
# the key based on the current value of the object.
obj = get_average_obj(bound_to)
value = getattr(obj, attribute)
for k, v in keyval_dict.items():
if v == value:
key = k
break
else:
return
lineedit.setText(key)
self.setCurrentText(key)
self.item_changed.emit(key)
lineedit.editingFinished.connect(on_editingFinished)
completer = QtWidgets.QCompleter(sorted(keyval_dict), self)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
completer.setFilterMode(QtCore.Qt.MatchContains)
max_visible_items = min(30, len(keyval_dict))
completer.setMaxVisibleItems(max_visible_items)
self.setMaxVisibleItems(max_visible_items)
completer.activated[str].connect(self.item_changed)
self.currentIndexChanged.connect(
lambda index: self.item_changed.emit(list(keyval_dict)[index]))
lineedit.setCompleter(completer)
class ColorPicker(ClickableLabel):
color_changed = QtCore.Signal(QtGui.QColor)
color_picked = QtCore.Signal(QtGui.QColor)
def __init__(self, with_alpha=False):
super().__init__()
height = int(self.fontMetrics().height() / 1.5)
pixmap = QtGui.QPixmap(height, height)
pixmap.fill(QtCore.Qt.black)
self.setPixmap(pixmap)
self.setFixedWidth(height)
self.color = QtGui.QColor(0, 0, 0, 0)
self.with_alpha = with_alpha
self.tmp_color = QtGui.QColor(0, 0, 0, 0)
self.clicked.connect(self.show_color_dialog)
def show_color_dialog(self):
dialog = QtWidgets.QColorDialog(self)
dialog.setOption(QtWidgets.QColorDialog.DontUseNativeDialog, True)
if self.with_alpha:
dialog.setOption(QtWidgets.QColorDialog.ShowAlphaChannel, True)
dialog.setCurrentColor(self.color)
dialog.currentColorChanged.connect(self.update_color)
dialog.currentColorChanged.connect(self.color_changed)
color = self.color
accepted = dialog.exec()
if accepted:
self.color = dialog.currentColor()
self.color_picked.emit(self.color)
else:
self.color = color
self.update_color(self.color)
self.color_changed.emit(self.color)
def update_color(self, color):
self.tmp_color = color
color = QtGui.QColor(color)
color.setAlpha(255)
pixmap = self.pixmap()
pixmap.fill(color)
self.setPixmap(pixmap)
class DataEditor(QtWidgets.QWidget):
emit_3d_update = QtCore.Signal()
def __init__(self, parent, bol, bound_to):
super().__init__(parent)
self.bol = bol
self.bound_to = bound_to
self.vbox = QtWidgets.QVBoxLayout(self)
self.vbox.setContentsMargins(0, 0, 0, 0)
self.vbox.setSpacing(3)
self.setup_widgets()
def catch_text_update(self):
self.emit_3d_update.emit()
def setup_widgets(self):
pass
def update_data(self):
pass
def get_bol_editor(self):
for window in QtWidgets.QApplication.topLevelWidgets():
if 'GenEditor' in str(type(window)):
return window
return None
def create_label(self, text):
label = QtWidgets.QLabel(self)
label.setText(text)
return label
def add_label(self, text):
label = self.create_label(text)
self.vbox.addWidget(label)
return label
def create_labeled_widget(self, parent, text, widget):
layout = QtWidgets.QHBoxLayout()
layout.setSpacing(5)
label = self.create_label(text)
label.setText(text)
layout.addWidget(label)
layout.addWidget(widget)
return layout
def create_labeled_widgets(self, parent, text, widgetlist):
layout = QtWidgets.QHBoxLayout()
layout.setSpacing(5)
label = self.create_label(text)
label.setText(text)
layout.addWidget(label)
if len(widgetlist) > 1:
child_layout = QtWidgets.QHBoxLayout()
child_layout.setSpacing(1)
child_layout.setContentsMargins(0, 0, 0, 0)
for widget in widgetlist:
child_layout.addWidget(widget)
layout.addLayout(child_layout)
elif widgetlist:
layout.addWidget(widgetlist[0])
return layout
def add_checkbox(self, text, attribute, off_value, on_value):
checkbox = QtWidgets.QCheckBox(self)
layout = self.create_labeled_widget(self, text, checkbox)
def checked(state):
for obj in self.bound_to:
setattr(obj, attribute, off_value if state == 0 else on_value)
checkbox.stateChanged.connect(checked)
self.vbox.addLayout(layout)
return checkbox
def add_maskbox(self, text, attribute, entries):
maskbox = MaskBox(entries)
layout = self.create_labeled_widget(self, text, maskbox)
def on_value_changed(value: int):
for obj in self.bound_to:
setattr(obj, attribute, value)
maskbox.value_changed.connect(on_value_changed)
self.vbox.addLayout(layout)
return maskbox
def add_integer_input(self, text, attribute, min_val, max_val):
spinbox = SpinBox(self)
spinbox.setRange(min_val, max_val)
if attribute is not None:
def on_spinbox_valueChanged(value):
for obj in self.bound_to:
setattr(obj, attribute, value)
spinbox.valueChanged.connect(on_spinbox_valueChanged)
layout = self.create_labeled_widget(self, text, spinbox)
self.vbox.addLayout(layout)
spinbox.setProperty('parent_layout', layout)
return spinbox
def add_decimal_input(self, text, attribute, min_val, max_val):
spinbox = DoubleSpinBox(self)
spinbox.setRange(min_val, max_val)
def on_spinbox_valueChanged(value):
self.catch_text_update()
for obj in self.bound_to:
setattr(obj, attribute, value)
spinbox.valueChanged.connect(on_spinbox_valueChanged)
layout = self.create_labeled_widget(self, text, spinbox)
self.vbox.addLayout(layout)
return spinbox
def add_text_input(self, text, attribute, maxlength):
line_edit = QtWidgets.QLineEdit(self)
layout = self.create_labeled_widget(self, text, line_edit)
line_edit.setMaxLength(maxlength)
def input_edited():
text = line_edit.text()
text = text.rjust(maxlength)
for obj in self.bound_to:
setattr(obj, attribute, text)
line_edit.editingFinished.connect(input_edited)
self.vbox.addLayout(layout)
return line_edit
def add_dropdown_input(self, text, attribute, keyval_dict):
combobox = QtWidgets.QComboBox(self)
for val in keyval_dict:
combobox.addItem(val)
policy = combobox.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
combobox.setSizePolicy(policy)
combobox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToMinimumContentsLengthWithIcon)
layout = self.create_labeled_widget(self, text, combobox)
def item_selected(item):
val = keyval_dict[item]
for obj in self.bound_to:
setattr(obj, attribute, val)
tt_dict = getattr(ttl, attribute, {})
if tt_dict:
combobox.setToolTip(
tt_dict.get(item) or ttl.markdown_to_html(item, 'Description not available.'))
combobox.currentTextChanged.connect(item_selected)
self.vbox.addLayout(layout)
return combobox
def add_completer_dropdown_input(self, text, attribute, keyval_dict):
combobox = CompleterComboBox(self.bound_to, attribute, keyval_dict, self)
layout = self.create_labeled_widget(self, text, combobox)
def on_item_selected(item):
val = keyval_dict[item]
for obj in self.bound_to:
setattr(obj, attribute, val)
tt_dict = getattr(ttl, attribute, {})
if tt_dict:
combobox.setToolTip(
tt_dict.get(item) or ttl.markdown_to_html(item, 'Description not available.'))
combobox.item_changed.connect(on_item_selected)
self.vbox.addLayout(layout)
return combobox
def add_button_input(self, labeltext, text, function):
button = QtWidgets.QPushButton(self)
button.setText(text)
button.clicked.connect(function)
policy = button.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
button.setSizePolicy(policy)
layout = self.create_labeled_widget(self, labeltext, button)
self.vbox.addLayout(layout)
return layout
def add_color_input(self, text, attribute, with_alpha=False):
spinboxes = []
input_edited_callbacks = []
for subattr in ["r", "g", "b", "a"] if with_alpha else ["r", "g", "b"]:
spinbox = SpinBox(self)
spinbox.setMaximumWidth(calc_width(self.fontMetrics(), 0, 255, is_spinbox=True))
spinbox.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
spinbox.setRange(0, 255)
input_edited = create_setter(self.bound_to,
attribute,
subattr,
self.catch_text_update)
input_edited_callbacks.append(input_edited)
spinbox.valueChanged.connect(input_edited)
spinboxes.append(spinbox)
color_picker = ColorPicker(with_alpha=with_alpha)
def on_spinbox_valueChanged(value: int):
_ = value
r = spinboxes[0].value()
g = spinboxes[1].value()
b = spinboxes[2].value()
a = spinboxes[3].value() if len(spinboxes) == 4 else 255
color_picker.color = QtGui.QColor(r, g, b, a)
color_picker.update_color(color_picker.color)
for spinbox in spinboxes:
spinbox.valueChanged.connect(on_spinbox_valueChanged)
def on_color_changed(color):
spinboxes[0].setValue(color.red())
spinboxes[1].setValue(color.green())
spinboxes[2].setValue(color.blue())
if len(spinboxes) == 4:
spinboxes[3].setValue(color.alpha())
def on_color_picked(color):
values = [color.red(), color.green(), color.blue()]
if len(spinboxes) == 4:
values.append(color.alpha())
for callback, value in zip(input_edited_callbacks, values):
callback(value)
color_picker.color_changed.connect(on_color_changed)
color_picker.color_picked.connect(on_color_picked)
layout = self.create_labeled_widgets(self, text, spinboxes + [color_picker])
self.vbox.addLayout(layout)
return spinboxes
def add_multiple_integer_input(self, text, attribute, subattributes, min_val, max_val):
spinboxes = []
for subattr in subattributes:
spinbox = SpinBox(self)
if max_val <= MAX_UNSIGNED_BYTE:
spinbox.setMaximumWidth(calc_width(self.fontMetrics(), min_val, max_val, is_spinbox=True))
spinbox.setRange(min_val, max_val)
input_edited = create_setter(self.bound_to,
attribute,
subattr,
self.catch_text_update)
spinbox.valueChanged.connect(input_edited)
spinboxes.append(spinbox)
layout = self.create_labeled_widgets(self, text, spinboxes)
self.vbox.addLayout(layout)
return spinboxes
def add_multiple_decimal_input(self, text, attribute, subattributes, min_val, max_val):
spinboxes = []
for subattr in subattributes:
spinbox = DoubleSpinBox(self)
if text in ('Position', 'Start', 'Light Position', 'Start Point', 'End Point'):
# Some fields can naturally get a greater step; no point in increasing position
# components by one unit in MKDD.
spinbox.setSingleStep(10)
spinbox.setRange(min_val, max_val)
input_edited = create_setter(self.bound_to,
attribute,
subattr,
self.catch_text_update)
spinbox.valueChanged.connect(input_edited)
spinboxes.append(spinbox)
layout = self.create_labeled_widgets(self, text, spinboxes)
self.vbox.addLayout(layout)
return spinboxes
def add_multiple_integer_input_list(self, text, attribute, min_val, max_val):
spinboxes = []
obj = get_average_obj(self.bound_to)
fieldlist = getattr(obj, attribute)
for i in range(len(fieldlist)):
spinbox = SpinBox(self)
spinbox.setMaximumWidth(calc_width(self.fontMetrics(), min_val, max_val, is_spinbox=True))
spinbox.setRange(min_val, max_val)
input_edited = create_setter_list(self.bound_to, attribute, i)
spinbox.valueChanged.connect(input_edited)
spinboxes.append(spinbox)
layout = self.create_labeled_widgets(self, text, spinboxes)
self.vbox.addLayout(layout)
return spinboxes
def add_types_widget_index(self, layout, text, attribute, index, widget_type):
# Certain widget types will be accompanied with arguments.
if isinstance(widget_type, (list, tuple)):
widget_type, *widget_type_args = widget_type
def set_value(value, index=index):
for obj in self.bound_to:
getattr(obj, attribute)[index] = value
if widget_type == "checkbox":
widget = QtWidgets.QCheckBox()
widget.stateChanged.connect(lambda state: set_value(int(bool(state))))
elif widget_type == "combobox":
widget = QtWidgets.QComboBox()
policy = widget.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
widget.setSizePolicy(policy)
widget.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToMinimumContentsLengthWithIcon)
for key, value in widget_type_args[0].items():
widget.addItem(key, value)
widget.currentIndexChanged.connect(
lambda index: set_value(widget.itemData(index)))
else:
widget = QtWidgets.QSpinBox()
policy = widget.sizePolicy()
policy.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
widget.setSizePolicy(policy)
widget.setRange(MIN_SIGNED_SHORT, MAX_SIGNED_SHORT)
widget.valueChanged.connect(set_value)
layout.addLayout(self.create_labeled_widget(None, text, widget))
return widget
def update_rotation(self, angle_x_edit, angle_y_edit, angle_z_edit):
rotation = get_average_obj(self.bound_to).rotation
"""forward, up, left = rotation.get_vectors()
for attr in ("x", "y", "z"):
if getattr(forward, attr) == 0.0:
setattr(forward, attr, 0.0)
for attr in ("x", "y", "z"):
if getattr(up, attr) == 0.0:
setattr(up, attr, 0.0)
for attr in ("x", "y", "z"):
if getattr(left, attr) == 0.0:
setattr(left, attr, 0.0)
forwardedits[0].setValueQuiet(forward.x)
forwardedits[1].setValueQuiet(forward.y)
forwardedits[2].setValueQuiet(forward.z)
upedits[0].setValueQuiet(up.x)
upedits[1].setValueQuiet(up.y)
upedits[2].setValueQuiet(up.z)
leftedits[0].setValueQuiet(left.x)
leftedits[1].setValueQuiet(left.y)
leftedits[2].setValueQuiet(left.z)"""
euler_angles = rotation.to_euler()
angle_x_edit.setValueQuiet(degrees(euler_angles[0]))
angle_y_edit.setValueQuiet(degrees(euler_angles[1]))
angle_z_edit.setValueQuiet(degrees(euler_angles[2]))
self.catch_text_update()
def add_rotation_input(self):
rotation = get_average_obj(self.bound_to).rotation
"""forward_spinboxes = []
up_spinboxes = []
left_spinboxes = []
for spinboxes in (forward_spinboxes, up_spinboxes, left_spinboxes):
for attr in ("x", "y", "z"):
spinbox = DoubleSpinBox(self)
spinbox.setDecimals(4)
spinbox.setRange(-1.0, 1.0)
spinboxes.append(spinbox)
def change_forward():
forward, up, left = rotation.get_vectors()
newforward = Vector3(*[v.value() for v in forward_spinboxes])
if newforward.norm() == 0.0:
newforward = left.cross(up)
newforward.normalize()
up = newforward.cross(left)
up.normalize()
left = up.cross(newforward)
left.normalize()
rotation.set_vectors(newforward, up, left)
for obj in self.bound_to:
obj.rotation.set_vectors(newforward, up, left)
self.update_rotation(forward_spinboxes, up_spinboxes, left_spinboxes)
def change_up():
forward, up, left = rotation.get_vectors()
newup = Vector3(*[v.value() for v in up_spinboxes])
if newup.norm() == 0.0:
newup = forward.cross(left)
newup.normalize()
forward = left.cross(newup)
forward.normalize()
left = newup.cross(forward)
left.normalize()
rotation.set_vectors(forward, newup, left)
for obj in self.bound_to:
obj.rotation.set_vectors(forward, newup, left)
self.update_rotation(forward_spinboxes, up_spinboxes, left_spinboxes)
def change_left():
forward, up, left = rotation.get_vectors()
newleft = Vector3(*[v.value() for v in left_spinboxes])
if newleft.norm() == 0.0:
newleft = up.cross(forward)
newleft.normalize()
forward = newleft.cross(up)
forward.normalize()
up = forward.cross(newleft)
up.normalize()
rotation.set_vectors(forward, up, newleft)
for obj in self.bound_to:
obj.rotation.set_vectors(forward, up, newleft)
self.update_rotation(forward_spinboxes, up_spinboxes, left_spinboxes)
for edit in forward_spinboxes:
edit.valueChanged.connect(lambda _value: change_forward())
for edit in up_spinboxes:
edit.valueChanged.connect(lambda _value: change_up())
for edit in left_spinboxes:
edit.valueChanged.connect(lambda _value: change_left())
layout = self.create_labeled_widgets(self, "Forward dir", forward_spinboxes)
self.vbox.addLayout(layout)
layout = self.create_labeled_widgets(self, "Up dir", up_spinboxes)
self.vbox.addLayout(layout)
layout = self.create_labeled_widgets(self, "Left dir", left_spinboxes)
self.vbox.addLayout(layout)
return forward_spinboxes, up_spinboxes, left_spinboxes"""
euler_angles = rotation.to_euler()
angle_edits = []
for angle in euler_angles:
spinbox = DoubleSpinBox(self)
spinbox.setWrapping(True)
spinbox.setRange(-360, 360)
spinbox.setDecimals(4)
angle_edits.append(spinbox)
def change_rotation():
angles = [radians(float(x.text())) for x in angle_edits]
for obj in self.bound_to:
obj.rotation.rotate_euler(*angles)
self.emit_3d_update.emit()
for edit in angle_edits:
edit.valueChanged.connect(lambda _value: change_rotation())
layout = self.create_labeled_widget(self, "Rotation X", angle_edits[0])
self.vbox.addLayout(layout)
layout = self.create_labeled_widget(self, "Rotation Y", angle_edits[1])
self.vbox.addLayout(layout)
layout = self.create_labeled_widget(self, "Rotation Z", angle_edits[2])
self.vbox.addLayout(layout)
return angle_edits
def create_setter_list(bound_to, attribute, index):
def on_spinbox_valueChanged(value):
for bound_to_object in bound_to:
mainattr = getattr(bound_to_object, attribute)
mainattr[index] = value
return on_spinbox_valueChanged
def create_setter(bound_to, attribute, subattr, update3dview):
def on_spinbox_valueChanged(value):
for bound_to_object in bound_to:
mainattr = getattr(bound_to_object, attribute)
setattr(mainattr, subattr, value)
update3dview()
return on_spinbox_valueChanged
MIN_SIGNED_BYTE = -128
MAX_SIGNED_BYTE = 127
MIN_SIGNED_SHORT = -2**15
MAX_SIGNED_SHORT = 2**15 - 1
MIN_SIGNED_INT = -2**31
MAX_SIGNED_INT = 2**31 - 1
MIN_UNSIGNED_BYTE = MIN_UNSIGNED_SHORT = MIN_UNSIGNED_INT = 0
MAX_UNSIGNED_BYTE = 255
MAX_UNSIGNED_SHORT = 2**16 - 1
MAX_UNSIGNED_INT = 2**32 - 1
def choose_data_editor(objs):
if not objs:
return None
if not all_same_type(objs):
if all(isinstance(obj, PositionedObject) for obj in objs):
return PositionedEdit
return None
obj = objs[0]
if isinstance(obj, EnemyPoint):
return EnemyPointEdit
elif isinstance(obj, EnemyPointGroup):
return EnemyPointGroupEdit
elif isinstance(obj, CheckpointGroup):
return CheckpointGroupEdit
elif isinstance(obj, MapObject):
return ObjectEdit
elif isinstance(obj, Checkpoint):
return CheckpointEdit
elif isinstance(obj, Route):
return ObjectRouteEdit
elif isinstance(obj, RoutePoint):
return ObjectRoutePointEdit
elif isinstance(obj, BOL):
return BOLEdit
elif isinstance(obj, KartStartPoint):
return KartStartPointEdit
elif isinstance(obj, Area):
return AreaEdit
elif isinstance(obj, Camera):
return CameraEdit
elif isinstance(obj, JugemPoint):
return RespawnPointEdit
elif isinstance(obj, LightParam):
return LightParamEdit
elif isinstance(obj, MGEntry):
return MGEntryEdit
elif isinstance(obj, Minimap):
return MinimapEdit
else:
return None
class EnemyPointGroupEdit(DataEditor):
def setup_widgets(self):
self.groupid = self.add_integer_input("Group ID", None, MIN_UNSIGNED_BYTE,
MAX_UNSIGNED_BYTE)
self.groupid.setEnabled(len(self.bound_to) == 1)
def on_valueChanged(value):
palette = self.groupid.palette()
obj = self.bound_to[0]
for i, group in enumerate(self.bol.enemypointgroups.groups):
if group is obj:
continue
if group.id == value:
print(f"Warning: Enemy path at index #{i} is already using ID {value}.")
palette.setColor(QtGui.QPalette.ColorRole.Highlight, QtCore.Qt.red)
self.groupid.setPalette(palette)
return
obj.id = value
for enemypathpoint in obj.points:
enemypathpoint.group = value
palette.setColor(QtGui.QPalette.ColorRole.Highlight, self.palette().highlight().color())
self.groupid.setPalette(palette)
self.update_name()
def on_editingFinished():
obj = self.bound_to[0]
if obj.id != self.groupid.value():
self.groupid.setValue(obj.id)
self.groupid.valueChanged.connect(on_valueChanged)
self.groupid.editingFinished.connect(on_editingFinished)
def update_data(self):
obj: EnemyPointGroup = get_average_obj(self.bound_to)
self.groupid.setValueQuiet(obj.id)
def update_name(self):
for obj in self.bound_to:
if obj.widget is None:
continue
obj.widget.update_name()
DRIFT_DIRECTION_OPTIONS = OrderedDict()
DRIFT_DIRECTION_OPTIONS[""] = 0
DRIFT_DIRECTION_OPTIONS["To the left"] = 1
DRIFT_DIRECTION_OPTIONS["To the right"] = 2
class EnemyPointEdit(DataEditor):
def setup_widgets(self, group_editable=False):
self.position = self.add_multiple_decimal_input("Position", "position", ["x", "y", "z"],
-inf, +inf)
self.link = self.add_integer_input("Link", "link",
MIN_SIGNED_SHORT, MAX_SIGNED_SHORT)
set_tool_tip(self.link, ttl.enemypoints['Link'])
self.scale = self.add_decimal_input("Scale", "scale", -inf, inf)
set_tool_tip(self.scale, ttl.enemypoints['Scale'])
self.itemsonly = self.add_checkbox("Items Only", "itemsonly", off_value=0, on_value=1)
set_tool_tip(self.itemsonly, ttl.enemypoints['Items Only'])
self.swerve = self.add_dropdown_input("Swerve", "swerve", REVERSE_SWERVE_IDS)
self.group = self.add_integer_input("Group", "group",
MIN_UNSIGNED_BYTE, MAX_UNSIGNED_BYTE)
if not group_editable:
self.group.setDisabled(True)
self.driftdirection = self.add_dropdown_input("Drift Direction", "driftdirection",
DRIFT_DIRECTION_OPTIONS)
self.driftacuteness = self.add_integer_input("Drift Acuteness", "driftacuteness",
MIN_UNSIGNED_BYTE, 250)
set_tool_tip(self.driftacuteness, ttl.enemypoints['Drift Acuteness'])
self.driftduration = self.add_integer_input("Drift Duration", "driftduration",
MIN_UNSIGNED_BYTE, MAX_UNSIGNED_BYTE)
set_tool_tip(self.driftduration, ttl.enemypoints['Drift Duration'])
self.driftsupplement = self.add_integer_input("Drift Supplement", "driftsupplement",
MIN_UNSIGNED_BYTE, MAX_UNSIGNED_BYTE)
set_tool_tip(self.driftsupplement, ttl.enemypoints['Drift Supplement'])
self.nomushroomzone = self.add_checkbox("No Mushroom Zone", "nomushroomzone",
off_value=0, on_value=1)
set_tool_tip(self.nomushroomzone, ttl.enemypoints['No Mushroom Zone'])
for widget in self.position:
widget.valueChanged.connect(lambda _value: self.catch_text_update())
for widget in (self.itemsonly, self.nomushroomzone):
widget.stateChanged.connect(lambda _state: self.catch_text_update())
for widget in (self.swerve, self.driftdirection):
widget.currentIndexChanged.connect(lambda _index: self.catch_text_update())
for widget in (self.link, self.driftacuteness, self.driftduration, self.driftsupplement):
widget.valueChanged.connect(lambda _value: self.catch_text_update())
self.link.valueChanged.connect(lambda _value: self.update_name())
def update_data(self):
obj: EnemyPoint = get_average_obj(self.bound_to)
self.position[0].setValueQuiet(obj.position.x)
self.position[1].setValueQuiet(obj.position.y)
self.position[2].setValueQuiet(obj.position.z)
self.driftdirection.setCurrentIndex(obj.driftdirection)
set_tool_tip(self.driftdirection, ttl.enemypoints['Drift Direction'])
self.link.setValueQuiet(obj.link)
self.scale.setValueQuiet(obj.scale)
self.itemsonly.setChecked(bool(obj.itemsonly))
self.group.setValueQuiet(obj.group)
self.driftacuteness.setValueQuiet(obj.driftacuteness)
self.driftduration.setValueQuiet(obj.driftduration)
self.driftsupplement.setValueQuiet(obj.driftsupplement)
self.nomushroomzone.setChecked(bool(obj.nomushroomzone))
if obj.swerve in SWERVE_IDS:
name = SWERVE_IDS[obj.swerve]
else:
name = SWERVE_IDS[0]
index = self.swerve.findText(name)
self.swerve.setCurrentIndex(index)
set_tool_tip(self.swerve, ttl.enemypoints['Swerve'])
def update_name(self):
for obj in self.bound_to:
if obj.widget is None:
continue
obj.widget.update_name()
obj.widget.parent().update_name()
class CheckpointGroupEdit(DataEditor):