forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_editor_plugin.cpp
840 lines (735 loc) · 29.5 KB
/
shader_editor_plugin.cpp
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
/**************************************************************************/
/* shader_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "shader_editor_plugin.h"
#include "editor/editor_command_palette.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/filesystem_dock.h"
#include "editor/gui/editor_bottom_panel.h"
#include "editor/inspector_dock.h"
#include "editor/plugins/text_shader_editor.h"
#include "editor/plugins/visual_shader_editor_plugin.h"
#include "editor/shader_create_dialog.h"
#include "editor/themes/editor_scale.h"
#include "editor/window_wrapper.h"
#include "scene/gui/item_list.h"
#include "scene/gui/texture_rect.h"
Ref<Resource> ShaderEditorPlugin::_get_current_shader() {
int index = shader_tabs->get_current_tab();
ERR_FAIL_INDEX_V(index, shader_tabs->get_tab_count(), Ref<Resource>());
if (edited_shaders[index].shader.is_valid()) {
return edited_shaders[index].shader;
} else {
return edited_shaders[index].shader_inc;
}
}
void ShaderEditorPlugin::_update_shader_list() {
shader_list->clear();
for (EditedShader &edited_shader : edited_shaders) {
Ref<Resource> shader = edited_shader.shader;
if (shader.is_null()) {
shader = edited_shader.shader_inc;
}
String path = shader->get_path();
String text = path.get_file();
if (text.is_empty()) {
// This appears for newly created built-in shaders before saving the scene.
text = TTR("[unsaved]");
} else if (shader->is_built_in()) {
const String &shader_name = shader->get_name();
if (!shader_name.is_empty()) {
text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));
}
}
// When shader is deleted in filesystem dock, need this to correctly close shader editor.
edited_shader.path = path;
bool unsaved = false;
if (edited_shader.shader_editor) {
unsaved = edited_shader.shader_editor->is_unsaved();
}
// TODO: Handle visual shaders too.
if (unsaved) {
text += "(*)";
}
String _class = shader->get_class();
if (!shader_list->has_theme_icon(_class, EditorStringName(EditorIcons))) {
_class = "TextFile";
}
Ref<Texture2D> icon = shader_list->get_editor_theme_icon(_class);
shader_list->add_item(text, icon);
shader_list->set_item_tooltip(-1, path);
edited_shader.name = text;
}
if (shader_tabs->get_tab_count()) {
shader_list->select(shader_tabs->get_current_tab());
}
_set_file_specific_items_disabled(edited_shaders.is_empty());
_update_shader_list_status();
}
void ShaderEditorPlugin::_update_shader_list_status() {
for (int i = 0; i < shader_list->get_item_count(); i++) {
TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));
if (se) {
if (se->was_compilation_successful()) {
shader_list->set_item_tag_icon(i, Ref<Texture2D>());
} else {
shader_list->set_item_tag_icon(i, shader_list->get_editor_theme_icon(SNAME("Error")));
}
}
}
}
void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {
if (p_from == p_to) {
return;
}
EditedShader es = edited_shaders[p_from];
edited_shaders.remove_at(p_from);
edited_shaders.insert(p_to, es);
shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);
_update_shader_list();
}
void ShaderEditorPlugin::edit(Object *p_object) {
if (!p_object) {
return;
}
EditedShader es;
ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
if (si != nullptr) {
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].shader_inc.ptr() == si) {
shader_tabs->set_current_tab(i);
shader_list->select(i);
return;
}
}
es.shader_inc = Ref<ShaderInclude>(si);
TextShaderEditor *text_shader = memnew(TextShaderEditor);
text_shader->get_code_editor()->set_toggle_list_control(left_panel);
es.shader_editor = text_shader;
es.shader_editor->edit_shader_include(si);
shader_tabs->add_child(es.shader_editor);
} else {
Shader *s = Object::cast_to<Shader>(p_object);
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].shader.ptr() == s) {
shader_tabs->set_current_tab(i);
shader_list->select(i);
return;
}
}
es.shader = Ref<Shader>(s);
Ref<VisualShader> vs = es.shader;
if (vs.is_valid()) {
es.shader_editor = memnew(VisualShaderEditor);
} else {
TextShaderEditor *text_shader = memnew(TextShaderEditor);
text_shader->get_code_editor()->set_toggle_list_control(left_panel);
es.shader_editor = text_shader;
}
shader_tabs->add_child(es.shader_editor);
es.shader_editor->edit_shader(es.shader);
}
TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(es.shader_editor);
if (text_shader_editor) {
text_shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
CodeTextEditor *cte = text_shader_editor->get_code_editor();
if (cte) {
cte->set_zoom_factor(text_shader_zoom_factor);
cte->connect("zoomed", callable_mp(this, &ShaderEditorPlugin::_set_text_shader_zoom_factor));
}
}
shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
edited_shaders.push_back(es);
_update_shader_list();
}
bool ShaderEditorPlugin::handles(Object *p_object) const {
return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
}
void ShaderEditorPlugin::make_visible(bool p_visible) {
if (p_visible) {
EditorNode::get_bottom_panel()->make_item_visible(window_wrapper);
}
}
void ShaderEditorPlugin::selected_notify() {
}
ShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
for (EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader == p_for_shader) {
return edited_shader.shader_editor;
}
}
return nullptr;
}
void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) {
window_wrapper->restore_window_from_saved_position(
p_layout->get_value("ShaderEditor", "window_rect", Rect2i()),
p_layout->get_value("ShaderEditor", "window_screen", -1),
p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i()));
} else {
window_wrapper->set_window_enabled(false);
}
if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
return;
}
if (!p_layout->has_section("ShaderEditor")) {
return;
}
if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||
!p_layout->has_section_key("ShaderEditor", "selected_shader")) {
return;
}
Array shaders = p_layout->get_value("ShaderEditor", "open_shaders");
int selected_shader_idx = 0;
String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader");
for (int i = 0; i < shaders.size(); i++) {
String path = shaders[i];
Ref<Resource> res = ResourceLoader::load(path);
if (res.is_valid()) {
edit(res.ptr());
}
if (selected_shader == path) {
selected_shader_idx = i;
}
}
if (p_layout->has_section_key("ShaderEditor", "split_offset")) {
main_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));
}
_update_shader_list();
_shader_selected(selected_shader_idx);
_set_text_shader_zoom_factor(p_layout->get_value("ShaderEditor", "text_shader_zoom_factor", 1.0f));
}
void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
if (window_wrapper->get_window_enabled()) {
p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect());
int screen = window_wrapper->get_window_screen();
p_layout->set_value("ShaderEditor", "window_screen", screen);
p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));
} else {
if (p_layout->has_section_key("ShaderEditor", "window_rect")) {
p_layout->erase_section_key("ShaderEditor", "window_rect");
}
if (p_layout->has_section_key("ShaderEditor", "window_screen")) {
p_layout->erase_section_key("ShaderEditor", "window_screen");
}
if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) {
p_layout->erase_section_key("ShaderEditor", "window_screen_rect");
}
}
Array shaders;
String selected_shader;
for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
EditedShader edited_shader = edited_shaders[i];
if (edited_shader.shader_editor) {
String shader_path;
if (edited_shader.shader.is_valid()) {
shader_path = edited_shader.shader->get_path();
} else {
DEV_ASSERT(edited_shader.shader_inc.is_valid());
shader_path = edited_shader.shader_inc->get_path();
}
shaders.push_back(shader_path);
ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_current_tab_control());
if (shader_editor && edited_shader.shader_editor == shader_editor) {
selected_shader = shader_path;
}
}
}
p_layout->set_value("ShaderEditor", "open_shaders", shaders);
p_layout->set_value("ShaderEditor", "split_offset", main_split->get_split_offset());
p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
p_layout->set_value("ShaderEditor", "text_shader_zoom_factor", text_shader_zoom_factor);
}
String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
// TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...
PackedStringArray unsaved_shaders;
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].shader_editor) {
if (edited_shaders[i].shader_editor->is_unsaved()) {
if (unsaved_shaders.is_empty()) {
unsaved_shaders.append(TTR("Save changes to the following shaders(s) before quitting?"));
}
unsaved_shaders.append(edited_shaders[i].name.trim_suffix("(*)"));
}
}
}
if (!p_for_scene.is_empty()) {
PackedStringArray unsaved_built_in_shaders;
const String scene_file = p_for_scene.get_file();
for (const String &E : unsaved_shaders) {
if (!E.is_resource_file() && E.contains(scene_file)) {
if (unsaved_built_in_shaders.is_empty()) {
unsaved_built_in_shaders.append(TTR("There are unsaved changes in the following built-in shaders(s):"));
}
unsaved_built_in_shaders.append(E);
}
}
if (!unsaved_built_in_shaders.is_empty()) {
return String("\n").join(unsaved_built_in_shaders);
}
return String();
}
return String("\n").join(unsaved_shaders);
}
void ShaderEditorPlugin::save_external_data() {
for (EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader_editor) {
edited_shader.shader_editor->save_external_data();
}
}
_update_shader_list();
}
void ShaderEditorPlugin::apply_changes() {
for (EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader_editor) {
edited_shader.shader_editor->apply_shaders();
}
}
}
void ShaderEditorPlugin::_shader_selected(int p_index) {
if (p_index >= (int)edited_shaders.size()) {
return;
}
if (edited_shaders[p_index].shader_editor) {
edited_shaders[p_index].shader_editor->validate_script();
}
shader_tabs->set_current_tab(p_index);
shader_list->select(p_index);
}
void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
if (p_mouse_button_index == MouseButton::MIDDLE) {
_close_shader(p_item);
}
if (p_mouse_button_index == MouseButton::RIGHT) {
_make_script_list_context_menu();
}
}
void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {
if (p_type == FILE) {
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTR("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_NEW);
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTR("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_NEW_INCLUDE);
p_menu->add_separator();
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTR("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_OPEN);
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTR("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_OPEN_INCLUDE);
}
if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTR("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE);
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTR("Save File As...")), FILE_SAVE_AS);
}
if (p_type == FILE) {
p_menu->add_separator();
p_menu->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
p_menu->add_separator();
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTR("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
} else {
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTR("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
p_menu->add_item(TTR("Close All"), CLOSE_ALL);
p_menu->add_item(TTR("Close Other Tabs"), CLOSE_OTHER_TABS);
if (p_type == CONTEXT_VALID_ITEM) {
p_menu->add_separator();
p_menu->add_item(TTR("Copy Script Path"), COPY_PATH);
p_menu->add_item(TTR("Show in File System"), SHOW_IN_FILE_SYSTEM);
}
}
}
void ShaderEditorPlugin::_make_script_list_context_menu() {
context_menu->clear();
int selected = shader_tabs->get_current_tab();
if (selected < 0 || selected >= shader_tabs->get_tab_count()) {
return;
}
Control *control = shader_tabs->get_tab_control(selected);
bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);
_setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
context_menu->set_position(main_split->get_screen_position() + main_split->get_local_mouse_position());
context_menu->reset_size();
context_menu->popup();
}
void ShaderEditorPlugin::_close_shader(int p_index) {
ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
Control *c = shader_tabs->get_tab_control(p_index);
memdelete(c);
edited_shaders.remove_at(p_index);
_update_shader_list();
EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
if (shader_tabs->get_tab_count() == 0) {
left_panel->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
}
}
void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
for (uint32_t i = 0; i < edited_shaders.size();) {
Ref<Shader> &shader = edited_shaders[i].shader;
if (shader.is_valid()) {
if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
_close_shader(i);
continue;
}
}
Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
if (include.is_valid()) {
if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
_close_shader(i);
continue;
}
}
i++;
}
}
void ShaderEditorPlugin::_resource_saved(Object *obj) {
// May have been renamed on save.
for (EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader.ptr() == obj || edited_shader.shader_inc.ptr() == obj) {
_update_shader_list();
return;
}
}
}
void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
switch (p_index) {
case FILE_NEW: {
String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
shader_create_dialog->popup_centered();
} break;
case FILE_NEW_INCLUDE: {
String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
shader_create_dialog->popup_centered();
} break;
case FILE_OPEN: {
InspectorDock::get_singleton()->open_resource("Shader");
} break;
case FILE_OPEN_INCLUDE: {
InspectorDock::get_singleton()->open_resource("ShaderInclude");
} break;
case FILE_SAVE: {
int index = shader_tabs->get_current_tab();
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
if (editor) {
if (editor->get_trim_trailing_whitespace_on_save()) {
editor->trim_trailing_whitespace();
}
if (editor->get_trim_final_newlines_on_save()) {
editor->trim_final_newlines();
}
}
if (edited_shaders[index].shader.is_valid()) {
EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
} else {
EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
}
if (editor) {
editor->tag_saved_version();
}
} break;
case FILE_SAVE_AS: {
int index = shader_tabs->get_current_tab();
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
if (editor) {
if (editor->get_trim_trailing_whitespace_on_save()) {
editor->trim_trailing_whitespace();
}
if (editor->get_trim_final_newlines_on_save()) {
editor->trim_final_newlines();
}
}
String path;
if (edited_shaders[index].shader.is_valid()) {
path = edited_shaders[index].shader->get_path();
if (!path.is_resource_file()) {
path = "";
}
EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
} else {
path = edited_shaders[index].shader_inc->get_path();
if (!path.is_resource_file()) {
path = "";
}
EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
}
if (editor) {
editor->tag_saved_version();
}
} break;
case FILE_INSPECT: {
int index = shader_tabs->get_current_tab();
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
if (edited_shaders[index].shader.is_valid()) {
EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
} else {
EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
}
} break;
case FILE_CLOSE: {
_close_shader(shader_tabs->get_current_tab());
} break;
case CLOSE_ALL: {
while (shader_tabs->get_tab_count() > 0) {
_close_shader(0);
}
} break;
case CLOSE_OTHER_TABS: {
int index = shader_tabs->get_current_tab();
for (int i = 0; i < index; i++) {
_close_shader(0);
}
while (shader_tabs->get_tab_count() > 1) {
_close_shader(1);
}
} break;
case SHOW_IN_FILE_SYSTEM: {
Ref<Resource> shader = _get_current_shader();
String path = shader->get_path();
if (!path.is_empty()) {
FileSystemDock::get_singleton()->navigate_to_path(path);
}
} break;
case COPY_PATH: {
Ref<Resource> shader = _get_current_shader();
DisplayServer::get_singleton()->clipboard_set(shader->get_path());
} break;
}
}
void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
EditorNode::get_singleton()->push_item(p_shader.ptr());
}
void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
}
Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
if (shader_list->get_item_count() == 0) {
return Variant();
}
int idx = shader_list->get_item_at_position(p_point);
if (idx < 0) {
return Variant();
}
HBoxContainer *drag_preview = memnew(HBoxContainer);
String preview_name = shader_list->get_item_text(idx);
Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
if (!preview_icon.is_null()) {
TextureRect *tf = memnew(TextureRect);
tf->set_texture(preview_icon);
tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
drag_preview->add_child(tf);
}
Label *label = memnew(Label(preview_name));
drag_preview->add_child(label);
main_split->set_drag_preview(drag_preview);
Dictionary drag_data;
drag_data["type"] = "shader_list_element";
drag_data["shader_list_element"] = idx;
return drag_data;
}
bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
Dictionary d = p_data;
if (!d.has("type")) {
return false;
}
if (String(d["type"]) == "shader_list_element") {
return true;
}
if (String(d["type"]) == "files") {
Vector<String> files = d["files"];
if (files.size() == 0) {
return false;
}
for (int i = 0; i < files.size(); i++) {
const String &file = files[i];
if (ResourceLoader::exists(file, "Shader")) {
Ref<Shader> shader = ResourceLoader::load(file);
if (shader.is_valid()) {
return true;
}
}
if (ResourceLoader::exists(file, "ShaderInclude")) {
Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
if (sinclude.is_valid()) {
return true;
}
}
}
return false;
}
return false;
}
void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
if (!can_drop_data_fw(p_point, p_data, p_from)) {
return;
}
Dictionary d = p_data;
if (!d.has("type")) {
return;
}
if (String(d["type"]) == "shader_list_element") {
int idx = d["shader_list_element"];
int new_idx = shader_list->get_item_at_position(p_point);
_move_shader_tab(idx, new_idx);
return;
}
if (String(d["type"]) == "files") {
Vector<String> files = d["files"];
for (int i = 0; i < files.size(); i++) {
const String &file = files[i];
Ref<Resource> res;
if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
res = ResourceLoader::load(file);
}
if (res.is_valid()) {
edit(res.ptr());
}
}
}
}
void ShaderEditorPlugin::_window_changed(bool p_visible) {
make_floating->set_visible(!p_visible);
}
void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
if (text_shader_zoom_factor != p_zoom_factor) {
text_shader_zoom_factor = p_zoom_factor;
for (const EditedShader &edited_shader : edited_shaders) {
TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited_shader.shader_editor);
if (text_shader_editor) {
CodeTextEditor *cte = text_shader_editor->get_code_editor();
if (cte && cte->get_zoom_factor() != text_shader_zoom_factor) {
cte->set_zoom_factor(text_shader_zoom_factor);
}
}
}
}
}
void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].path == p_removed_file) {
_close_shader(i);
break;
}
}
}
void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {
if (p_res.is_null()) {
return;
}
const String &path = p_res->get_path();
for (EditedShader &edited : edited_shaders) {
Ref<Resource> shader_res = edited.shader;
if (shader_res.is_null()) {
shader_res = edited.shader_inc;
}
ERR_FAIL_COND(shader_res.is_null());
TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited.shader_editor);
if (!text_shader_editor || !shader_res->is_built_in()) {
continue;
}
if (shader_res->get_path().get_slice("::", 0) == path) {
text_shader_editor->tag_saved_version();
_update_shader_list();
}
}
}
void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {
PopupMenu *file_popup_menu = file_menu->get_popup();
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_SAVE), p_disabled);
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_SAVE_AS), p_disabled);
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_INSPECT), p_disabled);
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_CLOSE), p_disabled);
}
void ShaderEditorPlugin::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_res_saved_callback));
} break;
}
}
ShaderEditorPlugin::ShaderEditorPlugin() {
window_wrapper = memnew(WindowWrapper);
window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));
window_wrapper->set_margins_enabled(true);
main_split = memnew(HSplitContainer);
Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTR("Make Floating"));
window_wrapper->set_wrapped_control(main_split, make_floating_shortcut);
left_panel = memnew(VBoxContainer);
HBoxContainer *menu_hb = memnew(HBoxContainer);
left_panel->add_child(menu_hb);
file_menu = memnew(MenuButton);
file_menu->set_text(TTR("File"));
file_menu->set_shortcut_context(main_split);
_setup_popup_menu(FILE, file_menu->get_popup());
file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
menu_hb->add_child(file_menu);
_set_file_specific_items_disabled(true);
context_menu = memnew(PopupMenu);
add_child(context_menu);
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
Control *padding = memnew(Control);
padding->set_h_size_flags(Control::SIZE_EXPAND_FILL);
menu_hb->add_child(padding);
make_floating = memnew(ScreenSelect);
make_floating->set_flat(true);
make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
if (!make_floating->is_disabled()) {
// Override default ScreenSelect tooltip if multi-window support is available.
make_floating->set_tooltip_text(TTR("Make the shader editor floating.\nRight-click to open the screen selector."));
}
menu_hb->add_child(make_floating);
window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
shader_list = memnew(ItemList);
shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
left_panel->add_child(shader_list);
shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected));
shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
shader_list->set_allow_rmb_select(true);
SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
main_split->add_child(left_panel);
left_panel->set_custom_minimum_size(Size2(200, 300) * EDSCALE);
shader_tabs = memnew(TabContainer);
shader_tabs->set_tabs_visible(false);
shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
main_split->add_child(shader_tabs);
Ref<StyleBoxEmpty> empty;
empty.instantiate();
shader_tabs->add_theme_style_override(SceneStringName(panel), empty);
button = EditorNode::get_bottom_panel()->add_item(TTR("Shader Editor"), window_wrapper, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_editor_bottom_panel", TTR("Toggle Shader Editor Bottom Panel"), KeyModifierMask::ALT | Key::S));
shader_create_dialog = memnew(ShaderCreateDialog);
main_split->add_child(shader_create_dialog);
shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
}
ShaderEditorPlugin::~ShaderEditorPlugin() {
}