-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
3464 lines (2959 loc) · 134 KB
/
gui.py
File metadata and controls
3464 lines (2959 loc) · 134 KB
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
from inspect import getmembers, isfunction
import os
import dearpygui.dearpygui as dpg
import textwrap
import logging
import mido
import re
import json
import socket
import clipboard
import model
import util
import functions
import fixtures
logger = logging.getLogger(__name__)
APP = None
SCREEN_WIDTH = 1940
SCREEN_HEIGHT = 1150
AXIS_MARGIN = 0.025
WINDOW_INFO = {}
PROJECT_EXTENSION = "ndmx"
NODE_EXTENSION = "ndmxc"
HUMAN_DELAY = 0.125
CLIP_INIT_CODE_VIEW = 0
CLIP_MAIN_CODE_VIEW = 1
DEFAULT_SEQUENCE_DURATION = 4 # beats
VARIABLE_NAME_PATTERN = r"[a-zA-Z_][a-zA-Z\d_]*$"
ICON = os.path.join(os.path.dirname(__file__), "assets", "icon.ico")
PLAY_BUTTON_COLOR = [0, 255, 0, 60]
def set_app(app):
global APP
APP = app
def action_callback(sender, app_data, user_data):
APP.action_callback(sender, app_data, user_data)
def valid(*objs):
return all([obj is not None and not getattr(obj, "deleted", False) for obj in objs])
class WindowManager:
"""Manages the positions and size of windows."""
# Clip window is always 41% x 45% of the top left region
CLIP_WINDOW_PERCENT = [0.41, 0.45]
# Clip param window starts off at 41% x 45% of the bottom left region
CLIP_PARAM_WINDOW_PERCENT = [0.41, 0.45]
def __init__(self, app):
self.app = app
self.state = app.state
self.window_info = {}
self.screen_width = SCREEN_WIDTH
self.screen_height = SCREEN_HEIGHT
self.update_window_size_info(self.screen_width, self.screen_height)
def resize_all(self):
self.resize_windows_callback(None, None, None)
def reset_all(self):
self.app.clip_preset_window.reset()
self.app.multi_clip_preset_window.reset()
self.app.clip_automation_presets_window.reset()
self.app.code_window.reset()
def update_window_size_info(self, new_width, new_height):
self.screen_width = new_width
self.screen_height = new_height
height_margin = 18
# Initial window settings for Edit mode.
clip_window_pos = (0, 18)
clip_window_size = (
int(self.CLIP_WINDOW_PERCENT[0] * new_width),
int(self.CLIP_WINDOW_PERCENT[1] * new_height),
)
code_window_pos = (clip_window_size[0], 18)
code_window_size = (new_width - clip_window_size[0], clip_window_size[1])
clip_parameters_pos = (0, 18 + clip_window_size[1])
clip_parameters_size = (
int(self.CLIP_PARAM_WINDOW_PERCENT[0] * new_width),
new_height - clip_window_size[1] - height_margin,
)
console_window_pos = (clip_parameters_size[0], 18 + code_window_size[1])
console_window_size = (
new_width - clip_parameters_size[0],
new_height - code_window_size[1] - height_margin,
)
self.window_info["edit"] = {
"clip_pos": clip_window_pos,
"clip_size": clip_window_size,
"code_pos": code_window_pos,
"code_size": code_window_size,
"console_pos": console_window_pos,
"console_size": console_window_size,
"clip_parameters_pos": clip_parameters_pos,
"clip_parameters_size": clip_parameters_size,
}
# Initial window settings for Performance mode.
clip_window_pos = (0, 18)
clip_window_size = (
int(self.CLIP_WINDOW_PERCENT[0] * new_width),
int(self.CLIP_WINDOW_PERCENT[1] * new_height),
)
clip_parameters_pos = (0, 18 + clip_window_size[1])
clip_parameters_size = (
int(self.CLIP_PARAM_WINDOW_PERCENT[0] * new_width),
new_height - clip_window_size[1] - height_margin,
)
right_pane_width = new_width - clip_window_size[0]
right_pane_bottom = int(new_height * 0.25)
clip_preset_percent = 0.75
clip_preset_pos = (clip_window_size[0], 18)
clip_preset_size = (
int(right_pane_width * clip_preset_percent),
new_height - right_pane_bottom - height_margin,
)
global_clip_preset_pos = (clip_preset_pos[0] + clip_preset_size[0], 18)
global_clip_preset_size = (
right_pane_width - clip_preset_size[0],
new_height - right_pane_bottom - height_margin,
)
clip_automation_pos = (
clip_window_size[0],
clip_preset_pos[1] + clip_preset_size[1],
)
clip_automation_size = (
right_pane_width,
right_pane_bottom,
)
self.window_info["performance"] = {
"clip_pos": clip_window_pos,
"clip_size": clip_window_size,
"clip_parameters_pos": clip_parameters_pos,
"clip_parameters_size": clip_parameters_size,
"clip_preset_pos": clip_preset_pos,
"clip_preset_size": clip_preset_size,
"global_clip_preset_pos": global_clip_preset_pos,
"global_clip_preset_size": global_clip_preset_size,
"clip_automation_pos": clip_automation_pos,
"clip_automation_size": clip_automation_size,
}
def resize_windows_callback(self, sender, app_data, user_data):
if app_data is None:
new_width = self.screen_width
new_height = self.screen_height
else:
new_width, new_height = app_data[2:4]
self.update_window_size_info(new_width, new_height)
if self.state.mode == "edit":
window_info = self.window_info["edit"]
# Clip window
dpg.set_item_pos("clip.gui.window", window_info["clip_pos"])
dpg.set_item_width("clip.gui.window", window_info["clip_size"][0])
dpg.set_item_height("clip.gui.window", window_info["clip_size"][1])
dpg.configure_item("clip.gui.window", show=True)
# Code windows
window_tag = "code_editor.gui.window"
dpg.set_item_pos(window_tag, window_info["code_pos"])
dpg.set_item_width(window_tag, window_info["code_size"][0])
dpg.set_item_height(window_tag, window_info["code_size"][1])
dpg.configure_item(window_tag, show=True)
#if APP._active_clip:
# text_tag = get_code_window_tag(APP._active_clip) + (
# ".main" if APP.code_view == CLIP_MAIN_CODE_VIEW else ".init"
# ) + ".text"
# dpg.set_item_width(text_tag, window_info["code_size"][0] * 0.98)
# dpg.set_item_height(
# text_tag, window_info["code_size"][1] * 0.91
# )
# Resize automation window
for input_channel in self.app.get_all_valid_clip_input_channels():
tag = get_source_node_window_tag(input_channel)
dpg.set_item_pos(tag, window_info["code_pos"])
dpg.set_item_width(tag, window_info["code_size"][0])
dpg.set_item_height(tag, window_info["code_size"][1])
# Console winodws
dpg.set_item_pos("console.gui.window", window_info["console_pos"])
dpg.set_item_width("console.gui.window", window_info["console_size"][0])
dpg.set_item_height("console.gui.window", window_info["console_size"][1])
dpg.configure_item("console.gui.window", show=True)
# Clip Parameters Windows
dpg.set_item_pos(
"clip_parameters.gui.window", window_info["clip_parameters_pos"]
)
dpg.set_item_width(
"clip_parameters.gui.window", window_info["clip_parameters_size"][0]
)
dpg.set_item_height(
"clip_parameters.gui.window", window_info["clip_parameters_size"][1]
)
dpg.configure_item("clip_parameters.gui.window", show=True)
elif self.state.mode == "performance":
window_info = self.window_info["performance"]
# Hide edit windows
windows = [
APP.code_window,
APP.console_window,
]
for window in windows:
window.hide()
# Clip window
dpg.set_item_pos("clip.gui.window", window_info["clip_pos"])
dpg.set_item_width("clip.gui.window", window_info["clip_size"][0])
dpg.set_item_height("clip.gui.window", window_info["clip_size"][1])
dpg.configure_item("clip.gui.window", show=True)
# Clip Preset Window
dpg.set_item_pos("clip_preset.gui.window", window_info["clip_preset_pos"])
dpg.set_item_width(
"clip_preset.gui.window", window_info["clip_preset_size"][0]
)
dpg.set_item_height(
"clip_preset.gui.window", window_info["clip_preset_size"][1]
)
dpg.configure_item("clip_preset.gui.window", show=True)
# Global Clip Preset Window
dpg.set_item_pos(
"global_preset_window.gui.window", window_info["global_clip_preset_pos"]
)
dpg.set_item_width(
"global_preset_window.gui.window",
window_info["global_clip_preset_size"][0],
)
dpg.set_item_height(
"global_preset_window.gui.window",
window_info["global_clip_preset_size"][1],
)
dpg.configure_item("global_preset_window.gui.window", show=True)
# Clip Parameters Windows
dpg.set_item_pos(
"clip_parameters.gui.window", window_info["clip_parameters_pos"]
)
dpg.set_item_width(
"clip_parameters.gui.window", window_info["clip_parameters_size"][0]
)
dpg.set_item_height(
"clip_parameters.gui.window", window_info["clip_parameters_size"][1]
)
dpg.configure_item("clip_parameters.gui.window", show=True)
# Clip Automation Windows
dpg.set_item_pos(
"clip_automation.gui.window", window_info["clip_automation_pos"]
)
dpg.set_item_width(
"clip_automation.gui.window", window_info["clip_automation_size"][0]
)
dpg.set_item_height(
"clip_automation.gui.window", window_info["clip_automation_size"][1]
)
dpg.configure_item("clip_automation.gui.window", show=True)
class GuiAction:
def __init__(self, params=None):
global APP
self.app = APP
self.state = self.app.state
self.params = params or {}
def execute(self):
raise NotImplementedError
def __call__(self, sender, app_data, user_data):
self.app.action(self)
class SelectTrack(GuiAction):
def execute(self):
# When user clicks on the track title, bring up the output configuration window.
track = self.params["track"]
if self.app._active_track == track:
return
self.app.save_last_active_clip()
self.last_track = self.app._active_track
self.last_clip = self.app._active_clip
# Unset activate clip
self.app._active_clip = None
self.app._active_clip_slot = None
for tag in self.app.tags["hide_on_clip_selection"]:
dpg.configure_item(tag, show=False)
self.app._active_track = track
last_active_clip_id = self.app.gui_state["track_last_active_clip"].get(
self.app._active_track.id
)
if last_active_clip_id is not None:
self.app._active_clip = self.app.state.get_obj(last_active_clip_id)
SelectClip(
{"track": self.app._active_track, "clip": self.app._active_clip}
).execute()
class SelectEmptyClipSlot(GuiAction):
def execute(self):
new_track_i = self.params["track_i"]
new_clip_i = self.params["clip_i"]
self.old_clip_slot = self.app._active_clip_slot
self.app._active_clip_slot = (new_track_i, new_clip_i)
self.app._active_track = self.state.tracks[new_track_i]
def undo(self):
if self.old_clip_slot is None:
return
self.app._active_clip_slot = self.old_clip_slot
self.app._active_track = self.state.tracks[self.old_clip_slot[0]]
class SelectClip(GuiAction):
def execute(self):
track = self.params["track"]
clip = self.params["clip"]
if self.app._active_clip == clip:
# Always reset code window even if the clip is the same.
if self.app.state.mode == "edit":
self.app.code_window.reset(show=True)
return
self.app.save_last_active_clip()
self.last_track = self.app._active_track
self.last_clip = self.app._active_clip
self.app._active_track = track
self.app._active_clip = clip
self.app._active_clip_slot = None
for tag in self.app.tags["hide_on_clip_selection"]:
dpg.configure_item(tag, show=False)
self.app.clip_automation_presets_window.reset(clip)
self.app.help_window.reset()
self.app.clip_params_window.reset()
if self.app.state.mode == "edit":
self.app.code_window.reset(show=True)
def undo(self):
self.app.save_last_active_clip()
self.app._active_track = self.last_track
self.app._active_clip = self.last_clip
for tag in self.app.tags["hide_on_clip_selection"]:
dpg.configure_item(tag, show=False)
class SelectInputChannel(GuiAction):
def execute(self):
clip = self.params["clip"]
input_channel = self.params["channel"]
for other_input_channel in clip.inputs:
if other_input_channel.deleted:
continue
dpg.configure_item(
get_source_node_window_tag(other_input_channel), show=False
)
# Set button color
if other_input_channel.input_type == "color":
dpg.bind_item_theme(
f"{other_input_channel.id}.name_button",
get_node_tag(other_input_channel) + ".theme",
)
else:
dpg.bind_item_theme(
f"{other_input_channel.id}.name_button", "not_selected_preset.theme"
)
if self.app.state.mode == "edit":
dpg.configure_item(get_source_node_window_tag(input_channel), show=True)
elif self.app.state.mode == "performance":
if input_channel.input_type == "color":
dpg.configure_item(get_source_node_window_tag(input_channel), show=True)
dpg.bind_item_theme(f"{input_channel.id}.name_button", "selected_preset.theme")
APP.reset_automation_plot(input_channel)
self.app._active_input_channel = input_channel
if self.app.state.mode == "edit":
self.app.code_window.hide()
class CreateNewClip(GuiAction):
def execute(self):
track_i = self.params["track_i"]
clip_i = self.params["clip_i"]
track = self.state.tracks[track_i]
action = self.params.get("action")
if action == "create":
result = self.app.execute_wrapper(f"new_clip {track.id},{clip_i}")
if not result.success:
raise RuntimeError("Failed to create clip")
# else restoring
clip = track.clips[clip_i]
# Create inputs
for input_channel in clip.inputs:
APP.add_input_channel_callback(
sender=None,
app_data=None,
user_data=(
"restore",
(clip, input_channel),
),
)
with dpg.value_registry():
dpg.add_string_value(
tag=get_code_window_tag(clip) + ".main.text",
default_value=clip.main_code.read(),
)
dpg.add_string_value(
tag=get_code_window_tag(clip) + ".init.text",
default_value=clip.init_code.read(),
)
# TODO: Can probably simplify this by making ClipWindow resettable
# Gui Updates
# Delete the double_click handler to create clips
dpg.delete_item(
get_clip_slot_group_tag(track_i, clip_i) + ".clip.item_handler_registry"
)
group_tag = get_clip_slot_group_tag(track_i, clip_i)
for slot, child_tags in dpg.get_item_children(group_tag).items():
for child_tag in child_tags:
dpg.delete_item(child_tag)
with dpg.group(parent=group_tag, horizontal=True, horizontal_spacing=5):
dpg.add_button(
arrow=True,
direction=dpg.mvDir_Right,
tag=f"{clip.id}.gui.play_button",
callback=self.app.toggle_clip_play_callback,
user_data=(track, clip),
)
clip_tag = group_tag + ".clip"
with dpg.group(
parent=group_tag, tag=clip_tag, horizontal=True, horizontal_spacing=5
):
text_tag = f"{clip.id}.name"
create_passive_button(
clip_tag,
text_tag,
clip.name,
SelectClip({"track": track, "clip": clip}),
text_theme="clip_text_theme",
)
def copy_clip_callback(sender, app_data, user_data):
self.app.copy_buffer = [user_data]
# TODO
def copy_clip_presets_callback(sender, app_data, user_data):
pass
for tag in [text_tag, text_tag + ".filler"]:
with dpg.popup(tag, mousebutton=1):
def show_properties_window(sender, app_data, user_data):
self.app._properties_buffer.clear()
dpg.configure_item(get_properties_window_tag(clip), show=True)
dpg.add_menu_item(label="Properties", callback=show_properties_window)
dpg.add_menu_item(
label="Copy", callback=copy_clip_callback, user_data=clip
)
dpg.add_menu_item(
label="Copy Presets", callback=copy_clip_presets_callback, user_data=clip
)
dpg.add_menu_item(
label="Paste",
callback=action_callback,
user_data=PasteClip({"track_i": track_i, "clip_i": clip_i}),
)
# End Gui updates
self.last_track = self.app._active_track
self.last_clip = self.app._active_clip
self.app.save_last_active_clip()
self.app._active_track = track
self.app._active_clip = clip
# Create the properties window
self.create_clip_properties_window(clip)
# Add the associated code editor
self.app.code_view = CLIP_INIT_CODE_VIEW
self.app.code_window.reset()
self.app.clip_params_window.reset()
def create_clip_properties_window(self, clip):
window_tag = get_properties_window_tag(clip)
with dpg.window(
tag=window_tag,
label="Properties",
width=500,
height=700,
pos=(SCREEN_WIDTH / 3, SCREEN_HEIGHT / 3),
no_move=True,
show=False,
modal=True,
popup=True,
no_title_bar=True,
):
properties_table_tag = f"{window_tag}.properties_table"
with dpg.table(
header_row=True,
tag=properties_table_tag,
policy=dpg.mvTable_SizingStretchProp,
):
dpg.add_table_column(label="Property")
dpg.add_table_column(label="Value")
def update_clip_buffer_callback(sender, app_data, user_data):
property_name = user_data
self.app._properties_buffer["clip"][property_name] = app_data
def save_clip_properties_callback(sender, app_data, user_data):
clip = user_data
for property_name, value in self.app._properties_buffer[
"clip"
].items():
setattr(clip, property_name, value)
dpg.configure_item(window_tag, show=False)
def cancel_properties_callback(sender, app_data, user_data):
clip = user_data
dpg.set_value(f"{clip.id}.name", clip.name)
dpg.configure_item(window_tag, show=False)
with dpg.table_row():
dpg.add_text(default_value="Name")
dpg.add_input_text(
source=f"{clip.id}.name",
callback=update_clip_buffer_callback,
user_data=("name"),
)
with dpg.table_row():
dpg.add_table_cell()
with dpg.group(horizontal=True):
dpg.add_button(
label="Save",
callback=save_clip_properties_callback,
user_data=clip,
)
dpg.add_button(
label="Cancel",
callback=cancel_properties_callback,
user_data=clip,
)
class PasteClip(GuiAction):
def execute(self):
track_i = self.params["track_i"]
clip_i = self.params["clip_i"]
with APP.lock:
APP.paste_clip(track_i, clip_i)
class ShowTrackProperties(GuiAction):
def execute(self):
# Hide all track config windows
for track in self.state.tracks:
dpg.configure_item(
get_output_configuration_window_tag(track),
show=False,
)
track = self.params["track"]
dpg.configure_item(
get_output_configuration_window_tag(track),
show=True,
)
dpg.focus_item(get_output_configuration_window_tag(track))
class ShowWindow(GuiAction):
def __init__(self, window):
if isinstance(window, str):
super().__init__({"window_tag": window})
else:
super().__init__({"window_tag": window.tag})
def execute(self):
window_tag = self.params["window_tag"]
dpg.configure_item(window_tag, show=False)
dpg.configure_item(window_tag, show=True)
dpg.focus_item(window_tag)
def get_clip_slot_group_tag(track_i, clip_i):
return f"track[{track_i}].clip[{clip_i}].gui.table_group"
def get_node_editor_tag(clip):
return f"{clip.id}.gui.node_window.node_editor"
def get_output_configuration_window_tag(track):
return f"{track.id}.gui.output_configuration_window"
def get_source_node_window_tag(input_channel, is_id=False):
return f"{input_channel if is_id else input_channel.id}.gui.source_node_window"
def get_properties_window_tag(obj):
return f"{obj.id}.gui.properties_window"
def get_plot_tag(input_channel):
return f"{input_channel.id}.plot"
def get_node_tag(obj):
return f"{obj.id}.node"
def get_node_window_tag(clip):
return f"{clip.id}.gui.node_window"
def get_code_window_tag(obj):
return f"{obj.id}.gui.code_editor.code_window"
def get_node_attribute_tag(clip, channel):
return f"{clip.id}.{channel.id}.node_attribute"
def get_output_node_value_tag(clip, output_channel):
return f"{clip.id}.{output_channel.id}.output.value"
def get_preset_menu_bar_tag(preset):
return f"{preset.id}.menu_bar"
def get_preset_sub_menu_tag(automation):
return f"{automation.id}.preset_menu"
def get_sequences_group_tag(track):
return f"{track.id}.sequences_group"
def get_sequence_button_tag(sequence):
return f"{sequence.id}.button"
def get_preset_button_tag(preset):
return f"{preset.id}.button"
def get_channel_preset_theme(preset):
return f"{preset.id}.theme"
def get_automation_button_tag(automation):
return f"{automation.id}.button"
def register_handler(add_item_handler_func, tag, function, user_data=None):
handler_registry_tag = f"{tag}.item_handler_registry"
if not dpg.does_item_exist(handler_registry_tag):
dpg.add_item_handler_registry(tag=handler_registry_tag)
add_item_handler_func(
parent=handler_registry_tag, callback=function, user_data=user_data
)
dpg.bind_item_handler_registry(tag, handler_registry_tag)
def create_passive_button(
group_tag,
text_tag,
text,
single_click_callback=None,
double_click_callback=None,
user_data=None,
double_click=False,
text_theme=None,
):
dpg.add_text(parent=group_tag, default_value=text, tag=text_tag)
if text_theme:
dpg.bind_item_theme(dpg.last_item(), text_theme)
dpg.add_text(parent=group_tag, default_value=" " * 1000, tag=f"{text_tag}.filler")
if single_click_callback is not None:
register_handler(
dpg.add_item_clicked_handler,
group_tag,
action_callback,
single_click_callback,
)
if double_click_callback is not None:
register_handler(
dpg.add_item_double_clicked_handler,
group_tag,
action_callback,
double_click_callback,
)
class Window:
state: model.ProgramState
def __init__(self, state: model.ProgramState, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.state = state
self.window = None
self.tag = kwargs.get("tag")
self._create()
def configure_window(self):
pass
def create(self):
"""Build the window."""
raise NotImplementedError
def on_close(self, sender, app_data, user_data):
pass
def _create(self):
"""Create and build the window."""
self.configure_window()
self.window = dpg.window(*self.args, **self.kwargs, on_close=self.on_close)
self.create()
@property
def shown(self):
return dpg.is_item_shown(self.tag)
def show(self):
dpg.configure_item(self.tag, show=True)
def hide(self):
dpg.configure_item(self.tag, show=False)
def focus(self):
dpg.focus_item(self.tag)
def unfix_location(self):
self.kwargs["no_move"] = False
self.kwargs["no_collapse"] = False
self.kwargs["no_close"] = False
self.kwargs["no_resize"] = False
def fix_location(self):
self.kwargs["no_move"] = True
self.kwargs["no_collapse"] = True
self.kwargs["no_close"] = True
self.kwargs["no_resize"] = True
class ResettableWindow(Window):
"""A Window that can be recreated.
Helpful for windows that depend on state from other items that frequently
change.
"""
def __init__(self, state, *args, **kwargs):
self.position = kwargs.get("pos", [1, 1])
self.width = kwargs.get("width", 1)
self.height = kwargs.get("height", 1)
super().__init__(state, *args, **kwargs)
def _create(self):
"""Create and build the window."""
self.kwargs["pos"] = self.position
self.kwargs["width"] = self.width
self.kwargs["height"] = self.height
self.kwargs["no_focus_on_appearing"] = True
super()._create()
def reset(self, show=None, focus=False):
self.position = dpg.get_item_pos(self.tag)
self.width = dpg.get_item_width(self.tag)
self.height = dpg.get_item_height(self.tag)
if show is None:
try:
show = self.shown
except:
logger.warning("Failed to get show status")
show = None
with APP.lock:
dpg.delete_item(self.tag)
self._create()
self.hide()
if show:
self.show()
else:
self.hide()
if focus:
self.focus()
def reset_callback(self, sender, app_data, user_data):
self.reset(show=True)
class TrackPropertiesWindow(ResettableWindow):
def __init__(self, state, track):
self.track = track
super().__init__(
state,
tag=get_output_configuration_window_tag(track),
label="Output Configuration",
width=400,
height=SCREEN_HEIGHT * 5 / 6,
pos=(799, 60),
show=False,
)
def create(self):
with self.window:
output_table_tag = f"{self.tag}.output_table"
with dpg.group(horizontal=True):
def set_track_title_button_text(sender, app_data, user_data):
if self.state.mode == "edit":
self.track.name = app_data
dpg.set_value(user_data, self.track.name)
track_title_tag = f"{self.track.id}.gui.button"
dpg.add_input_text(
tag=f"{self.track.id}.name",
default_value=self.track.name,
user_data=track_title_tag,
callback=set_track_title_button_text,
width=75,
)
dpg.add_button(
label="Add Output",
callback=APP.create_track_output,
user_data=("create", self.track),
)
dpg.add_button(label="Add Fixture")
with dpg.popup(dpg.last_item(), mousebutton=0):
for fixture in fixtures.FIXTURES:
dpg.add_menu_item(
label=fixture.name,
callback=APP.add_fixture,
user_data=(self.track, fixture),
)
def open_fixture_dialog():
dpg.configure_item("open_fixture_dialog", show=True)
dpg.add_menu_item(label="Custom", callback=open_fixture_dialog)
with dpg.table(
header_row=True,
tag=output_table_tag,
policy=dpg.mvTable_SizingStretchProp,
):
dpg.add_table_column(
label="DMX Ch.", tag=f"{output_table_tag}.column.dmx_address"
)
dpg.add_table_column(
label="Name", tag=f"{output_table_tag}.column.name"
)
dpg.add_table_column(tag=f"{output_table_tag}.column.delete", width=10)
###############
### Restore ###
###############
for output_index, output_channel in enumerate(self.track.outputs):
if output_channel.deleted:
continue
if isinstance(output_channel, model.DmxOutputGroup):
APP.create_track_output_group(
sender=None,
app_data=None,
user_data=("restore", self.track, output_channel),
)
else:
APP.create_track_output(
sender=None,
app_data=None,
user_data=("restore", self.track, output_channel),
)
def on_close(self):
APP.clip_params_window.reset()
# This is a good template to copy when you need to create a new window
class RenameWindow(Window):
def __init__(self, state):
super().__init__(
state,
tag="rename_node.popup",
label="Rename",
no_background=False,
modal=False,
show=False,
autosize=True,
pos=(2 * SCREEN_WIDTH / 5, SCREEN_HEIGHT / 3),
no_move=True,
no_collapse=True,
no_close=True,
)
def create(self):
with self.window:
def set_name_property(sender, app_data, user_data):
if not re.match(VARIABLE_NAME_PATTERN, app_data):
return
if APP._active_clip is not None and app_data:
node_editor_tag = get_node_editor_tag(APP._active_clip)
items = dpg.get_selected_nodes(node_editor_tag)
# Renaming a node
if items:
item = items[0]
alias = dpg.get_item_alias(item)
node_id = alias.replace(".node", "").rsplit(".", 1)[-1]
obj = self.state.get_obj(node_id)
obj.name = app_data
dpg.configure_item(
get_node_tag(APP._active_clip, obj), label=obj.name
)
dpg.set_value(f"{obj.id}.name", obj.name)
# Renaming a clip
else:
APP._active_clip.name = app_data
dpg.set_value(f"{APP._active_clip.id}.name", app_data)
dpg.configure_item(self.tag, show=False)
dpg.add_input_text(
tag="rename_node.text",
on_enter=True,
callback=set_name_property,
no_spaces=True,
)
class ReorderWindow(ResettableWindow):
def __init__(self, state):