forked from RodZill4/material-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.gd
More file actions
1564 lines (1346 loc) · 57.7 KB
/
Copy pathmain_window.gd
File metadata and controls
1564 lines (1346 loc) · 57.7 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
extends Control
var quitting : bool = false
var recent_files = []
var current_tab = null
var updating : bool = false
var need_update : bool = false
# The resolution scale to use for 3D previews.
# Values above 1.0 enable supersampling. This has a significant performance cost
# but greatly improves texture rendering quality, especially when using
# specular/parallax mapping and when viewed at oblique angles.
var preview_rendering_scale_factor : float = 2.0
# The number of subdivisions to use for tesselated 3D previews. Higher values
# result in more detailed bumps but are more demanding to render.
# This doesn't apply to non-tesselated 3D previews which use parallax occlusion mapping.
var preview_tesselation_detail : int = 256
@onready var node_library_manager = $NodeLibraryManager
@onready var brush_library_manager = $BrushLibraryManager
@onready var projects_panel = $VBoxContainer/Layout/FlexibleLayout/Main
@onready var layout = $VBoxContainer/Layout
var library
var preview_2d : Array
var histogram
var preview_3d
var hierarchy
var brushes
var current_mesh : Mesh = null
const FPS_LIMIT_MIN = 20
const FPS_LIMIT_MAX = 500
const IDLE_FPS_LIMIT_MIN = 1
const IDLE_FPS_LIMIT_MAX = 100
const RECENT_FILES_COUNT = 15
const MENU_QUICK_EXPORT : int = 1000
const RECENTS_MENU_CLEAR = 1001
const MENU_SAVE_PRESET : int = 1002
const MENU_MANAGE_PRESETS : int = 1003
const THEMES = ["Default Dark", "Default Light", "Classic"]
const MENU : Array[Dictionary] = [
{ menu="File/New material", command="new_material", shortcut="Control+N" },
{ menu="File/New paint project (Experimental)", command="new_paint_project", shortcut="Control+Shift+N", not_in_ports=["HTML5"] },
{ menu="File/Load", command="load_project", shortcut="Control+O" },
{ menu="File/Load material from website", command="load_material_from_website" },
{ menu="File/Load recent", submenu="load_recent", standalone_only=true, not_in_ports=["HTML5"] },
{ menu="File/-" },
{ menu="File/Save", command="save_project", shortcut="Control+S" },
{ menu="File/Save as...", command="save_project_as", shortcut="Control+Shift+S" },
{ menu="File/Save all...", command="save_all_projects", not_in_ports=["HTML5"] },
{ menu="File/-" },
{ menu="File/Export again", command="export_again", shortcut="Control+E", not_in_ports=["HTML5"] },
{ menu="File/Export material", submenu="export_material", not_in_ports=["HTML5"] },
{ menu="File/-" },
{ menu="File/Close", command="close_project", shortcut="Control+Shift+Q" },
{ menu="File/Quit", command="quit", shortcut="Control+Q", not_in_ports=["HTML5"] },
{ menu="Edit/Undo", command="edit_undo", shortcut="Control+Z" },
{ menu="Edit/Redo", command="edit_redo", shortcut="Control+Shift+Z" },
{ menu="Edit/-" },
{ menu="Edit/Cut", command="edit_cut", shortcut="Control+X" },
{ menu="Edit/Copy", command="edit_copy", shortcut="Control+C" },
{ menu="Edit/Paste", command="edit_paste", shortcut="Control+V" },
{ menu="Edit/Duplicate", command="edit_duplicate", shortcut="Control+D" },
{ menu="Edit/Duplicate with inputs", command="edit_duplicate_with_inputs", shortcut="Control+Shift+D" },
{ menu="Edit/Swap node inputs", command="edit_swap_node_inputs", shortcut="Alt+S"},
{ menu="Edit/-" },
{ menu="Edit/Frame selected nodes", command="frame_nodes", shortcut="Control+Shift+F" },
{ menu="Edit/-" },
{ menu="Edit/Select All", command="edit_select_all", shortcut="Control+A" },
{ menu="Edit/Select None", command="edit_select_none", shortcut="Control+Shift+A" },
{ menu="Edit/Invert Selection", command="edit_select_invert", shortcut="Control+I" },
{ menu="Edit/Select Sources", command="edit_select_sources", shortcut="Control+L" },
{ menu="Edit/Select Targets", command="edit_select_targets", shortcut="Control+Shift+L" },
{ menu="Edit/-" },
{ menu="Edit/Align Start", command="edit_align_start", shortcut="Control+BRACKETLEFT" },
{ menu="Edit/Align Center", command="edit_align_center", shortcut="Control+BACKSLASH" },
{ menu="Edit/Align End", command="edit_align_end", shortcut="Control+BRACKETRIGHT" },
{ menu="Edit/-" },
{ menu="Edit/Load Selection", command="edit_load_selection", not_in_ports=["HTML5"] },
{ menu="Edit/Save Selection", command="edit_save_selection", not_in_ports=["HTML5"] },
{ menu="Edit/-" },
{ menu="Edit/Set theme", submenu="set_theme" },
{ menu="Edit/Preferences", command="edit_preferences", shortcut="Control+Comma" },
{ menu="View/Center view", command="view_center", shortcut="C" },
{ menu="View/Reset zoom", command="view_reset_zoom", shortcut="Control+0" },
{ menu="View/-" },
{ menu="View/Show or Hide side panels", command="toggle_side_panels", shortcut="Alt+Control+Space" },
{ menu="View/Panels", submenu="show_panels" },
{ menu="View/Presets", submenu="panels_preset" },
{ menu="View/Reset Panels", command="view_reset_panels" },
{ menu="Tools/Create", submenu="create" },
{ menu="Tools/Create group", command="create_subgraph", shortcut="Control+G" },
{ menu="Tools/Make selected nodes editable", command="make_selected_nodes_editable", shortcut="Control+W" },
{ menu="Tools/-" },
{ menu="Tools/Add selection to library", submenu="add_selection_to_library", mode="material" },
{ menu="Tools/Add current brush to library", submenu="add_brush_to_library", mode="paint", not_in_ports=["HTML5"] },
{ menu="Tools/Create a screenshot of the current graph", command="generate_graph_screenshot", mode="material" },
{ menu="Tools/Paint project settings", command="paint_project_settings", mode="paint", not_in_ports=["HTML5"] },
{ menu="Tools/Set painting environment", submenu="paint_environment", mode="paint", not_in_ports=["HTML5"] },
{ menu="Tools/-" },
{ menu="Tools/Environment editor", command="environment_editor", not_in_ports=["HTML5"] },
#{ menu="Tools/Generate screenshots for the library nodes", command="generate_screenshots", mode="material" },
{ menu="Help/User manual", command="show_doc", shortcut="F1" },
{ menu="Help/Example projects", command="show_example_projects"},
{ menu="Help/Show selected library item documentation", command="show_library_item_doc", shortcut="Control+F1" },
{ menu="Help/Report a bug", command="bug_report" },
{ menu="Help/" },
{ menu="Help/About", command="about" }
]
enum WinTabletDriver { WININK, WINTAB, DISABLED }
func _enter_tree() -> void:
mm_globals.main_window = self
func _ready() -> void:
get_window().borderless = false
get_window().transparent = false
get_window().grab_focus()
get_window().close_requested.connect(self.on_close_requested)
get_tree().set_auto_accept_quit(false)
if mm_globals.get_config("locale") == "":
mm_globals.set_config("locale", TranslationServer.get_locale())
on_config_changed()
# Set a minimum window size to prevent UI elements from collapsing on each other.
get_window().min_size = Vector2(1024, 600)
# Restore the window position/size if values are present in the configuration cache
if mm_globals.config.has_section_key("window", "screen"):
get_window().current_screen = mm_globals.config.get_value("window", "screen")
if mm_globals.config.has_section_key("window", "maximized"):
get_window().mode = Window.MODE_MAXIMIZED if (mm_globals.config.get_value("window", "maximized")) else Window.MODE_WINDOWED
if get_window().mode != Window.MODE_MAXIMIZED:
if mm_globals.config.has_section_key("window", "position"):
get_window().position = mm_globals.config.get_value("window", "position")
else:
get_window().min_size *= get_window().content_scale_factor
get_window().move_to_center()
if mm_globals.config.has_section_key("window", "size"):
get_window().size = mm_globals.config.get_value("window", "size")
# Restore the theme
var theme_name: String = "default dark"
if mm_globals.config.has_section_key("window", "theme"):
theme_name = mm_globals.config.get_value("window", "theme")
change_theme(theme_name)
# In HTML5 export, copy all examples to the filesystem
if OS.get_name() == "HTML5":
print("Copying samples")
DirAccess.open("res://").make_dir("/examples")
var dir : DirAccess = DirAccess.open("res://material_maker/examples/")
dir.list_dir_begin() # TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
while true:
var f = dir.get_next()
if f == "":
break
if f.ends_with(".ptex"):
print(f)
dir.copy("res://material_maker/examples/"+f, "/examples/"+f)
# Set window title
get_window().set_title(ProjectSettings.get_setting("application/config/name")+" v"+ProjectSettings.get_setting("application/config/actual_release"))
layout.load_panels()
library = get_panel("Library")
preview_2d = [ get_panel("Preview2D"), get_panel("Preview2D (2)") ]
histogram = get_panel("Histogram")
preview_3d = get_panel("Preview3D")
preview_3d.connect("need_update", self.update_preview_3d)
hierarchy = get_panel("Hierarchy")
hierarchy.connect("group_selected", self.on_group_selected)
brushes = get_panel("Brushes")
# Load recent projects
load_recents()
get_window().connect("files_dropped", self.on_files_dropped)
var args : PackedStringArray = OS.get_cmdline_args()
for a in args:
if a.get_extension().to_lower() in [ "ptex", "mmpp" ]:
do_load_project(get_file_absolute_path(a))
elif a.get_extension().to_lower() in [ "obj", "glb", "gltf", "fbx" ]:
var mesh_filename : String = get_file_absolute_path(a)
if mesh_filename == "":
push_error("Cannot load mesh from '%s' (no such file or directory)" % a)
continue
var mesh : Mesh = MMMeshLoader.load_mesh(mesh_filename)
if mesh == null:
push_error("Cannot load mesh from '%s'" % mesh_filename)
continue
var project_filename : String = mesh_filename.get_basename()+".mmpp"
create_paint_project(mesh, mesh_filename, 1024, project_filename)
# Rescue unsaved projects
if true:
var dir : DirAccess = DirAccess.open("user://unsaved_projects")
if dir != null:
var files : Array = []
dir.list_dir_begin() # TODOGODOT4 fill missing arguments https://github.com/godotengine/godot/pull/40547
var file_name = dir.get_next()
while file_name != "":
if !dir.current_is_dir() and file_name.get_extension() == "mmcr":
files.append("user://unsaved_projects".path_join(file_name))
file_name = dir.get_next()
if ! files.is_empty():
var dialog_text : String = "Oops, it seems Material Maker crashed and rescued unsaved work\nLoad %d unsaved projects?" % files.size()
var result = await accept_dialog(dialog_text, true, false, [ { label="Delete them!", action="delete" } ])
match result:
"ok":
for f in files:
var graph_edit = new_graph_panel()
graph_edit.load_from_recovery(f)
graph_edit.update_tab_title()
hierarchy.update_from_graph_edit(get_current_graph_edit())
"delete":
for f in files:
DirAccess.remove_absolute(f)
if get_current_graph_edit() == null:
await get_tree().process_frame
new_material()
size = get_window().size
position = Vector2.ZERO
set_anchors_preset(Control.PRESET_FULL_RECT)
update_menus()
mm_logger.message("Material Maker "+ProjectSettings.get_setting("application/config/actual_release"))
size = get_viewport().size/get_viewport().content_scale_factor
position = Vector2i(0, 0)
var menu_update_requested : bool = false
func update_menus() -> void:
if ! menu_update_requested:
menu_update_requested = true
do_update_menus.call_deferred()
func do_update_menus() -> void:
# Create menus
var menu_bar_class
if DisplayServer.has_feature(DisplayServer.FEATURE_GLOBAL_MENU):
menu_bar_class = mm_globals.menu_manager.MenuBarDisplayServer
else:
menu_bar_class = mm_globals.menu_manager.MenuBarGodot
var menu_bar = menu_bar_class.new($VBoxContainer/TopBar/Menu)
mm_globals.menu_manager.create_menus(MENU, self, menu_bar)
menu_update_requested = false
func _exit_tree() -> void:
# Save the window position and size to remember it when restarting the application
mm_globals.config.set_value("window", "screen", get_window().current_screen)
mm_globals.config.set_value("window", "maximized", (get_window().mode == Window.MODE_MAXIMIZED) || ((get_window().mode == Window.MODE_EXCLUSIVE_FULLSCREEN) or (get_window().mode == Window.MODE_FULLSCREEN)))
mm_globals.config.set_value("window", "position", get_window().position)
mm_globals.config.set_value("window", "size", get_window().size)
layout.save_config()
func _input(event: InputEvent) -> void:
if event.is_action_pressed("toggle_fullscreen"):
match get_window().mode:
Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN, Window.MODE_MAXIMIZED:
get_window().mode = Window.MODE_WINDOWED
_:
get_window().mode = Window.MODE_MAXIMIZED
func on_config_changed() -> void:
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED if (mm_globals.get_config("vsync")) else DisplayServer.VSYNC_DISABLED)
# Convert FPS to microseconds per frame.
# Clamp the FPS to reasonable values to avoid locking up the UI.
@warning_ignore("narrowing_conversion")
OS.low_processor_usage_mode_sleep_usec = (1.0 / clamp(mm_globals.get_config("fps_limit"), FPS_LIMIT_MIN, FPS_LIMIT_MAX)) * 1_000_000
# locale
var locale = mm_globals.get_config("locale")
if locale != "" and locale != TranslationServer.get_locale():
TranslationServer.set_locale(locale)
get_tree().call_group("updated_from_locale", "update_from_locale")
if OS.get_name() == "macOS":
mm_globals.main_window.update_menus()
var ui_scale = mm_globals.get_config("ui_scale")
if ui_scale <= 0:
# If scale is set to 0 (auto), scale everything if the display requires it (crude hiDPI support).
# This prevents UI elements from being too small on hiDPI displays.
ui_scale = 2 if DisplayServer.screen_get_dpi() >= 192 and DisplayServer.screen_get_size().x >= 2048 else 1
get_viewport().content_scale_factor = ui_scale
size = get_viewport().size/get_viewport().content_scale_factor
position = Vector2i(0, 0)
#ProjectSettings.set_setting("display/window/stretch/scale", scale)
# Clamp to reasonable values to avoid crashes on startup.
preview_rendering_scale_factor = clamp(mm_globals.get_config("ui_3d_preview_resolution"), 1.0, 2.0)
update_preview_3d([ preview_3d, projects_panel.preview_3d_background ])
@warning_ignore("narrowing_conversion")
preview_tesselation_detail = clamp(mm_globals.get_config("ui_3d_preview_tesselation_detail"), 16, 1024)
if OS.get_name() == "Windows":
match mm_globals.get_config("win_tablet_driver"):
WinTabletDriver.WININK:
DisplayServer.tablet_set_current_driver("winink")
WinTabletDriver.WINTAB:
DisplayServer.tablet_set_current_driver("wintab")
WinTabletDriver.DISABLED:
DisplayServer.tablet_set_current_driver("dummy")
# update minimize/close button visibility
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
for c in graph_edit.get_children():
if c.has_method("update_node"):
c.update_node()
if c.has_method("update"):
c.update()
if not get_window().gui_embed_subwindows:
get_window().gui_embed_subwindows = mm_globals.get_config("ui_single_window_mode")
func get_panel(panel_name : String) -> Control:
return layout.get_panel(panel_name)
func get_current_project() -> Control:
return projects_panel.get_projects().get_current_tab_control()
func get_current_graph_edit() -> MMGraphEdit:
if projects_panel == null:
return null
var graph_edit = projects_panel.get_projects().get_current_tab_control()
if graph_edit != null and graph_edit.has_method("get_graph_edit"):
return graph_edit.get_graph_edit()
return null
func get_share_button():
return %Share
# Modes
var current_mode : String = ""
func get_current_mode() -> String:
return current_mode
func set_current_mode(mode : String) -> void:
current_mode = mode
layout.change_mode(current_mode)
# Menus
func create_menu_load_recent(menu) -> void:
menu.clear()
if recent_files.is_empty():
menu.add_item("No items found", 0)
menu.set_item_disabled(0, true)
else:
for i in recent_files.size():
menu.add_item(recent_files[i], i)
menu.connect_id_pressed(self._on_LoadRecent_id_pressed)
menu.add_separator()
menu.add_item("Clear recent files", RECENTS_MENU_CLEAR)
func _on_LoadRecent_id_pressed(id) -> void:
match id:
RECENTS_MENU_CLEAR:
clear_recents()
_:
do_load_project(recent_files[id])
func load_recents() -> void:
var f : FileAccess = FileAccess.open("user://recent_files.bin", FileAccess.READ)
if f != null:
var test_json_conv = JSON.new()
test_json_conv.parse(f.get_as_text())
recent_files = test_json_conv.get_data()
func clear_recents() -> void:
recent_files.clear()
save_recents()
func save_recents() -> void:
var f : FileAccess = FileAccess.open("user://recent_files.bin", FileAccess.WRITE)
if f != null:
f.store_string(JSON.stringify(recent_files))
update_menus()
func add_recent(path, save = true) -> void:
remove_recent(path, false)
recent_files.push_front(path)
while recent_files.size() > RECENT_FILES_COUNT:
recent_files.pop_back()
if save:
save_recents()
func remove_recent(path, save = true) -> void:
while true:
var index = recent_files.find(path)
if index >= 0:
recent_files.remove_at(index)
else:
break
if save:
save_recents()
func export_profile_config_key(profile : String) -> String:
var key = "export_"+profile.to_lower().replace(" ", "_")
return key
func quick_export() -> void:
var project = get_current_project()
if project == null:
return
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit == null:
return
# get project filename
var project_file : String
if not graph_edit.save_path.is_empty():
project_file = graph_edit.save_path.right(-(graph_edit.save_path.rfind("/")+1))
project_file = project_file.trim_suffix(".ptex")
else:
project_file = "unnamed"
var export_prefix : String
var exports : Array
var has_unconnected_exports : bool = false
var stack : Array[MMGenBase] = [graph_edit.top_generator]
while stack.size():
var node : MMGenBase = stack.pop_back()
if node.has_method("export_material") and not node.has_method("get_export_profiles"):
if node.get_source(0) != null:
exports.append(node)
else:
has_unconnected_exports = true
stack.append_array(node.get_children())
# No export nodes
if not exports.size():
var dialog : AcceptDialog = load("res://material_maker/windows/accept_dialog/accept_dialog.tscn").instantiate()
var error_text = "Quick export requires at least one export node"
if has_unconnected_exports:
error_text += " with connected input"
dialog.dialog_text = TranslationServer.translate(error_text)
add_child(dialog)
await dialog.ask()
return
var file_dialog := preload("res://material_maker/windows/file_dialog/file_dialog.tscn").instantiate()
file_dialog.access = FileDialog.ACCESS_FILESYSTEM
file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_DIR
file_dialog.title = "Quick Export"
if mm_globals.config.has_section_key("path", "quick_export"):
file_dialog.current_dir = mm_globals.config.get_value("path", "quick_export")
var files = await file_dialog.select_files()
if files.size() == 1:
export_prefix = files[0]
else:
return
var progress_dialog = null
var progress_dialog_scene = load("res://material_maker/windows/progress_window/progress_window.tscn")
if progress_dialog_scene != null:
progress_dialog = progress_dialog_scene.instantiate()
var dim_color_rect = ColorRect.new()
dim_color_rect.modulate = Color(0.05, 0.05, 0.05, 0.5)
add_child(dim_color_rect)
get_tree().get_root().add_child(progress_dialog)
progress_dialog.set_text("Quick Export")
progress_dialog.set_progress(0)
var export_count = 0.0
for export_node in exports:
await export_node.export_material(
"%s/%s" % [export_prefix, project_file], "Quick Export")
export_count += 1.0
progress_dialog.set_progress(export_count / len(exports))
if progress_dialog != null:
# Wait a little to allow progress bar to complete
await get_tree().create_timer(0.25).timeout
dim_color_rect.queue_free()
progress_dialog.queue_free()
mm_globals.config.set_value("path", "quick_export", export_prefix)
mm_globals.set_tip_text(
"Quick exported %s file(s) to %s" % [len(exports), export_prefix], 3, 1)
func export_material(file_path : String, profile : String) -> void:
var project = get_current_project()
if project == null:
return
mm_globals.config.set_value("path", export_profile_config_key(profile), file_path.get_base_dir())
var export_prefix = file_path.trim_suffix("."+file_path.get_extension())
project.export_material(export_prefix, profile)
mm_steam.unlock_achievement("ACH_MATERIALIZED")
func export_again_is_disabled() -> bool:
var project = get_current_project()
if project == null:
return true
var material_node = project.get_material_node()
if material_node == null or material_node.get_last_export_target() == "":
return true
return false
func export_again() -> void:
var project = get_current_project()
if project == null:
return
var material_node = project.get_material_node()
if material_node == null:
return
var export_target : String = material_node.get_last_export_target()
if export_target == "":
return
var export_path : String = material_node.get_export_path(export_target)
export_material(export_path, export_target)
func create_menu_export_material(menu : MMMenuManager.MenuBase, prefix : String = "",
export_profiles = null, add_quick_export : bool = true) -> void:
if prefix == "":
menu.clear()
var project = get_current_project()
if project == null:
return
var material_node = project.get_material_node()
if material_node == null:
return
var prefix_len = prefix.length()
var submenus : Array[String] = []
if export_profiles == null:
export_profiles = material_node.get_export_profiles()
for id in range(export_profiles.size()):
var p : String = export_profiles[id]
if prefix_len > 0:
if p.left(prefix_len) != prefix:
continue
p = p.right(-prefix_len)
var slash_position = p.find("/")
if slash_position == -1:
menu.add_item(p, id)
else:
var submenu_name : String = p.left(slash_position)
if submenus.find(submenu_name) == -1:
var submenu : MMMenuManager.MenuBase = menu.add_submenu(submenu_name)
create_menu_export_material(submenu, p.left(slash_position+1), export_profiles, false)
submenus.append(submenu_name)
menu.connect_id_pressed(self._on_ExportMaterial_id_pressed)
if add_quick_export:
# This work as stated in godot docs, but still shows a warning
# https://github.com/godotengine/godot/issues/101320
@warning_ignore("int_as_enum_without_cast")
@warning_ignore("int_as_enum_without_match")
menu.add_item("Quick Export", MENU_QUICK_EXPORT, KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_E )
func _on_ExportMaterial_id_pressed(id) -> void:
if id == MENU_QUICK_EXPORT:
quick_export()
return
var project = get_current_project()
if project == null:
return
var material_node = project.get_material_node()
if material_node == null:
return
var profile = material_node.get_export_profiles()[id]
var export_extension : String = material_node.get_export_extension(profile)
if export_extension == "":
export_material("", profile)
else:
var dialog = preload("res://material_maker/windows/file_dialog/file_dialog.tscn").instantiate()
dialog.min_size = Vector2(500, 500)
dialog.access = FileDialog.ACCESS_FILESYSTEM
dialog.file_mode = FileDialog.FILE_MODE_SAVE_FILE
var profile_name : String = profile
var last_profile_name_slash : int = profile_name.rfind("/")
if last_profile_name_slash != -1:
profile_name = profile_name.right(-(last_profile_name_slash+1))
dialog.add_filter("*."+export_extension+";"+profile_name+" Material")
var last_export_path = material_node.get_export_path(profile)
if last_export_path != "":
dialog.current_path = last_export_path
else:
var config_key = export_profile_config_key(profile)
dialog.current_dir = mm_globals.config.get_value("path", config_key, mm_globals.get_home_directory())
add_child(dialog)
var files = await dialog.select_files()
if files.size() > 0:
export_material(files[0], profile)
func create_menu_set_theme(menu : MMMenuManager.MenuBase) -> void:
menu.clear()
for t in THEMES:
menu.add_item(t)
menu.connect_id_pressed(self._on_SetTheme_id_pressed)
func change_theme(theme_name) -> void:
if not ResourceLoader.exists("res://material_maker/theme/"+theme_name+".tres"):
theme_name = "default dark"
var _theme = load("res://material_maker/theme/"+theme_name+".tres")
if _theme == theme:
return
if _theme is EnhancedTheme:
_theme.update()
await get_tree().process_frame
theme = _theme
if "classic" in theme_name:
RenderingServer.set_default_clear_color(Color(0.14, 0.17,0.23))
else:
RenderingServer.set_default_clear_color(
Color("4d4d4d") if "light" in theme_name else Color("1f1f1f"))
$NodeFactory.on_theme_changed()
func _on_SetTheme_id_pressed(id) -> void:
var theme_name : String = THEMES[id].to_lower()
change_theme(theme_name)
mm_globals.config.set_value("window", "theme", theme_name)
func create_menu_show_panels(menu : MMMenuManager.MenuBase) -> void:
menu.clear()
var panels = layout.get_panel_list()
for i in range(panels.size()):
menu.add_check_item(panels[i], i)
menu.set_item_checked(i, layout.is_panel_visible(panels[i]))
if current_mode:
menu.set_item_disabled(i, panels[i] in layout.HIDE_PANELS[current_mode])
menu.connect_id_pressed(self._on_ShowPanels_id_pressed)
func create_menu_panels_preset(menu : MMMenuManager.MenuBase) -> void:
menu.clear()
menu.add_item("Save Preset", MENU_SAVE_PRESET)
menu.add_item("Manage Presets", MENU_MANAGE_PRESETS)
if not layout.presets.is_empty():
menu.add_separator()
for id in layout.presets.size():
menu.add_item(layout.presets[id].name, id)
menu.connect_id_pressed(self._on_PanelsPreset_id_pressed)
func _on_ShowPanels_id_pressed(id) -> void:
var panel : String = layout.get_panel_list()[id]
layout.set_panel_visible(panel, not layout.is_panel_visible(panel))
update_menus()
func _on_PanelsPreset_id_pressed(id : int) -> void:
match id:
MENU_MANAGE_PRESETS:
if get_node_or_null("PanelPresetsDialog"):
return
var dialog : Window = preload("res://material_maker/windows/panels_presets_dialog/panels_presets_dialog.tscn").instantiate()
add_child(dialog)
await dialog.edit_presets(layout.presets)
MENU_SAVE_PRESET:
var dialog : Window = preload("res://material_maker/windows/line_dialog/line_dialog.tscn").instantiate()
add_child(dialog)
var status : Dictionary = await dialog.enter_text("Save Preset",
"Enter a name for the new preset", "")
if status.ok:
var preset_name : String = status.text.strip_edges()
if preset_name.is_empty():
accept_dialog("Preset name cannot be empty.")
else:
var is_unique_preset : bool = true
var existing_preset : Dictionary
if not layout.presets.is_empty():
for preset in layout.presets:
if preset.name.to_lower() == preset_name.to_lower():
is_unique_preset = false
existing_preset = preset
break
if not is_unique_preset:
var replace_status : String = await accept_dialog(
"Preset \"%s\" already exists. Do you want to replace it?" % [preset_name], true)
if replace_status == "ok":
existing_preset.preset = $VBoxContainer/Layout/FlexibleLayout.serialize()
else:
var new_preset : Dictionary = {
"name": preset_name,
"preset": $VBoxContainer/Layout/FlexibleLayout.serialize()
}
layout.presets.push_back(new_preset)
_:
$VBoxContainer/Layout/FlexibleLayout.init(layout.presets[id].preset)
update_menus()
func create_menu_create(menu : MMMenuManager.MenuBase) -> void:
var gens = mm_loader.get_generator_list()
menu.clear()
for i in gens.size():
menu.add_item(gens[i], i)
menu.connect_id_pressed(self._on_Create_id_pressed)
func _on_Create_id_pressed(id) -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
var gens = mm_loader.get_generator_list()
await graph_edit.create_gen_from_type(gens[id])
func new_graph_panel() -> GraphEdit:
var graph_edit = preload("res://material_maker/panels/graph_edit/graph_edit.tscn").instantiate()
graph_edit.node_factory = $NodeFactory
projects_panel.get_projects().add_tab(graph_edit)
projects_panel.get_projects().current_tab = graph_edit.get_index()
return graph_edit
func new_material() -> void:
var graph_edit = new_graph_panel()
graph_edit.new_material()
graph_edit.top_generator.set_current_mesh(current_mesh)
graph_edit.update_tab_title()
hierarchy.update_from_graph_edit(get_current_graph_edit())
func new_paint_project(obj_file_name = null) -> void:
# Prevent opening the New Paint Project dialog several times by pressing the keyboard shortcut.
if get_node_or_null("NewPainterWindow") != null:
return
var new_painter_dialog = preload("res://material_maker/windows/new_painter/new_painter.tscn").instantiate()
var result = await new_painter_dialog.ask(obj_file_name)
if ! result.has("mesh"):
return
create_paint_project(result.mesh, result.mesh_filename, result.size, result.project_filename)
func create_paint_project(mesh, mesh_filename, texture_size, project_filename):
var paint_panel = load("res://material_maker/panels/paint/paint.tscn").instantiate()
projects_panel.get_projects().add_tab(paint_panel)
paint_panel.init_project(mesh, mesh_filename, texture_size, project_filename)
projects_panel.get_projects().current_tab = paint_panel.get_index()
func load_project() -> void:
if OS.get_name() == "HTML5":
if ! Html5.is_connected("file_loaded", Callable(self, "on_html5_load_file")):
Html5.connect("file_loaded", Callable(self, "on_html5_load_file"))
Html5.load_file(".ptex")
else:
var dialog = preload("res://material_maker/windows/file_dialog/file_dialog.tscn").instantiate()
dialog.min_size = Vector2(500, 500)
dialog.access = FileDialog.ACCESS_FILESYSTEM
dialog.file_mode = FileDialog.FILE_MODE_OPEN_FILES
dialog.add_filter("*.ptex;Procedural Textures File")
dialog.add_filter("*.mmpp;Model Painting File")
dialog.current_dir = mm_globals.config.get_value("path", "project", mm_globals.get_home_directory())
var files = await dialog.select_files()
if files.size() > 0:
do_load_projects(files)
func on_html5_load_file(file_name, _file_type, file_data):
match file_name.get_extension():
"ptex":
if do_load_material_from_data(file_name, file_data, false):
hierarchy.update_from_graph_edit(get_current_graph_edit())
func get_file_absolute_path(filename : String) -> String:
var file : FileAccess = FileAccess.open(filename, FileAccess.READ)
if file == null:
return ""
return file.get_path_absolute()
func do_load_projects(filenames) -> void:
var file_name : String = ""
for f in filenames:
f = get_file_absolute_path(f)
if f != "":
file_name = f
do_load_project(file_name)
if file_name != "":
mm_globals.config.set_value("path", "project", file_name.get_base_dir())
func do_load_project(file_name : String) -> bool:
var status : bool = false
match file_name.get_extension():
"ptex":
status = await do_load_material(file_name, false)
hierarchy.update_from_graph_edit(get_current_graph_edit())
"mmpp":
status = do_load_painting(file_name)
if ! FileAccess.file_exists(file_name):
status = false
if status:
add_recent(file_name)
else:
remove_recent(file_name)
return status
func create_new_graph_edit_if_needed() -> MMGraphEdit:
var graph_edit : MMGraphEdit = get_current_graph_edit()
var node_count = 2 # So test below succeeds if graph_edit is null...
if graph_edit != null:
node_count = 0
for c in graph_edit.get_children():
if c is GraphNode:
node_count += 1
if node_count > 1:
break
if node_count > 1:
graph_edit = new_graph_panel()
return graph_edit
func do_load_material(filename : String, update_hierarchy : bool = true) -> bool:
var graph_edit : MMGraphEdit = create_new_graph_edit_if_needed()
await graph_edit.load_file(filename)
if update_hierarchy:
hierarchy.update_from_graph_edit(get_current_graph_edit())
print("Current mesh: ", current_mesh)
print("Top generator: ", graph_edit.top_generator)
if current_mesh and graph_edit.top_generator:
graph_edit.top_generator.set_current_mesh(current_mesh)
return true
func do_load_material_from_data(filename : String, data : String, update_hierarchy : bool = true) -> bool:
var graph_edit : MMGraphEdit = create_new_graph_edit_if_needed()
graph_edit.load_from_data(filename, data)
if update_hierarchy:
hierarchy.update_from_graph_edit(get_current_graph_edit())
return true
func do_load_painting(filename : String) -> bool:
var paint_panel = load("res://material_maker/panels/paint/paint.tscn").instantiate()
projects_panel.get_projects().add_tab(paint_panel)
var status : bool = paint_panel.load_project(filename)
projects_panel.get_projects().current_tab = paint_panel.get_index()
return status
func load_material_from_website() -> void:
var dialog = load("res://material_maker/windows/load_from_website/load_from_website.tscn").instantiate()
var result = await dialog.select_asset()
if result == {}:
return
new_material()
var graph_edit = get_current_graph_edit()
var new_generator = await mm_loader.create_gen(result)
graph_edit.set_new_generator(new_generator)
hierarchy.update_from_graph_edit(graph_edit)
mm_steam.unlock_achievement("ACH_COMMUNITY_CHEST")
func save_project(project : Control = null) -> bool:
if project == null:
project = get_current_project()
if project != null:
return await project.save()
return false
func save_project_as(project : Control = null) -> bool:
if project == null:
project = get_current_project()
if project != null:
return await project.save_as()
return false
func save_all_projects() -> void:
for i in range(projects_panel.get_projects().get_tab_count()):
await projects_panel.get_projects().get_tab(i).save()
func close_project() -> void:
projects_panel.get_projects().close_tab()
func quit() -> void:
if quitting:
return
quitting = true
if mm_globals.get_config("confirm_quit"):
var result = await accept_dialog("Quit Material Maker?", true)
if result == "cancel":
quitting = false
return
if mm_globals.get_config("confirm_close_project"):
var result = await $VBoxContainer/Layout/FlexibleLayout/Main/Projects.check_save_tabs()
if !result:
quitting = false
return
await mm_renderer.stop_rendering_thread()
dim_window()
get_tree().quit()
quitting = false
func edit_cut() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.cut()
func edit_undo() -> void:
var project = get_current_project()
if project != null and project.get("undoredo") != null:
project.undoredo.undo()
func edit_undo_is_disabled() -> bool:
var project = get_current_project()
if project != null and project.get("undoredo") != null:
return !project.undoredo.can_undo()
return true
func edit_redo() -> void:
var project = get_current_project()
if project != null and project.get("undoredo") != null:
project.undoredo.redo()
func edit_redo_is_disabled() -> bool:
var project = get_current_project()
if project != null and project.get("undoredo") != null:
return !project.undoredo.can_redo()
return true
func edit_cut_is_disabled() -> bool:
var graph_edit : MMGraphEdit = get_current_graph_edit()
return graph_edit == null or !graph_edit.can_copy()
func edit_copy() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.copy()
func edit_copy_is_disabled() -> bool:
return edit_cut_is_disabled()
func edit_paste() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.paste()
func edit_paste_is_disabled() -> bool:
return false # todo validate_json(DisplayServer.clipboard_get()) != ""
func edit_duplicate() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.duplicate_selected()
func edit_duplicate_with_inputs() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.duplicate_selected_with_inputs()
func edit_duplicate_with_inputs_is_disabled() -> bool:
return edit_cut_is_disabled()
func edit_swap_node_inputs() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.swap_node_inputs()
func edit_select_all() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.select_all()
func edit_select_none() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.select_none()
func edit_select_invert() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
graph_edit.select_invert()
func edit_select_connected(end1 : String, end2 : String) -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
var node_list : Array = []
for n in graph_edit.get_selected_nodes():
node_list.push_back(n.name)
while !node_list.is_empty():
var new_node_list = []
for c in graph_edit.get_connection_list():