Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/shader/shader_editor_plugin.cpp
9898 views
1
/**************************************************************************/
2
/* shader_editor_plugin.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "shader_editor_plugin.h"
32
33
#include "editor/docks/filesystem_dock.h"
34
#include "editor/docks/inspector_dock.h"
35
#include "editor/editor_node.h"
36
#include "editor/editor_string_names.h"
37
#include "editor/editor_undo_redo_manager.h"
38
#include "editor/gui/editor_bottom_panel.h"
39
#include "editor/gui/window_wrapper.h"
40
#include "editor/settings/editor_command_palette.h"
41
#include "editor/shader/shader_create_dialog.h"
42
#include "editor/shader/text_shader_editor.h"
43
#include "editor/shader/visual_shader_editor_plugin.h"
44
#include "editor/themes/editor_scale.h"
45
#include "scene/gui/item_list.h"
46
#include "scene/gui/tab_container.h"
47
#include "scene/gui/texture_rect.h"
48
49
Ref<Resource> ShaderEditorPlugin::_get_current_shader() {
50
int index = shader_tabs->get_current_tab();
51
ERR_FAIL_INDEX_V(index, shader_tabs->get_tab_count(), Ref<Resource>());
52
if (edited_shaders[index].shader.is_valid()) {
53
return edited_shaders[index].shader;
54
} else {
55
return edited_shaders[index].shader_inc;
56
}
57
}
58
59
void ShaderEditorPlugin::_update_shader_list() {
60
shader_list->clear();
61
for (EditedShader &edited_shader : edited_shaders) {
62
Ref<Resource> shader = edited_shader.shader;
63
if (shader.is_null()) {
64
shader = edited_shader.shader_inc;
65
}
66
67
String path = shader->get_path();
68
String text = path.get_file();
69
if (text.is_empty()) {
70
// This appears for newly created built-in shaders before saving the scene.
71
text = TTR("[unsaved]");
72
} else if (shader->is_built_in()) {
73
const String &shader_name = shader->get_name();
74
if (!shader_name.is_empty()) {
75
text = vformat("%s (%s)", shader_name, text.get_slice("::", 0));
76
}
77
}
78
79
// When shader is deleted in filesystem dock, need this to correctly close shader editor.
80
edited_shader.path = path;
81
82
bool unsaved = false;
83
if (edited_shader.shader_editor) {
84
unsaved = edited_shader.shader_editor->is_unsaved();
85
}
86
// TODO: Handle visual shaders too.
87
88
if (unsaved) {
89
text += "(*)";
90
}
91
92
String _class = shader->get_class();
93
if (!shader_list->has_theme_icon(_class, EditorStringName(EditorIcons))) {
94
_class = "TextFile";
95
}
96
Ref<Texture2D> icon = shader_list->get_editor_theme_icon(_class);
97
98
shader_list->add_item(text, icon);
99
shader_list->set_item_tooltip(-1, path);
100
edited_shader.name = text;
101
}
102
103
if (shader_tabs->get_tab_count()) {
104
shader_list->select(shader_tabs->get_current_tab());
105
}
106
107
_set_file_specific_items_disabled(edited_shaders.is_empty());
108
109
_update_shader_list_status();
110
}
111
112
void ShaderEditorPlugin::_update_shader_list_status() {
113
for (int i = 0; i < shader_list->get_item_count(); i++) {
114
TextShaderEditor *se = Object::cast_to<TextShaderEditor>(shader_tabs->get_tab_control(i));
115
if (se) {
116
if (se->was_compilation_successful()) {
117
shader_list->set_item_tag_icon(i, Ref<Texture2D>());
118
} else {
119
shader_list->set_item_tag_icon(i, shader_list->get_editor_theme_icon(SNAME("Error")));
120
}
121
}
122
}
123
}
124
125
void ShaderEditorPlugin::_move_shader_tab(int p_from, int p_to) {
126
if (p_from == p_to) {
127
return;
128
}
129
EditedShader es = edited_shaders[p_from];
130
edited_shaders.remove_at(p_from);
131
edited_shaders.insert(p_to, es);
132
shader_tabs->move_child(shader_tabs->get_tab_control(p_from), p_to);
133
_update_shader_list();
134
}
135
136
void ShaderEditorPlugin::edit(Object *p_object) {
137
if (!p_object) {
138
return;
139
}
140
141
EditedShader es;
142
143
ShaderInclude *si = Object::cast_to<ShaderInclude>(p_object);
144
if (si != nullptr) {
145
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
146
if (edited_shaders[i].shader_inc.ptr() == si) {
147
shader_tabs->set_current_tab(i);
148
shader_list->select(i);
149
_switch_to_editor(edited_shaders[i].shader_editor);
150
return;
151
}
152
}
153
es.shader_inc = Ref<ShaderInclude>(si);
154
TextShaderEditor *text_shader = memnew(TextShaderEditor);
155
text_shader->get_code_editor()->set_toggle_list_control(shader_list);
156
es.shader_editor = text_shader;
157
es.shader_editor->edit_shader_include(si);
158
shader_tabs->add_child(es.shader_editor);
159
} else {
160
Shader *s = Object::cast_to<Shader>(p_object);
161
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
162
if (edited_shaders[i].shader.ptr() == s) {
163
shader_tabs->set_current_tab(i);
164
shader_list->select(i);
165
_switch_to_editor(edited_shaders[i].shader_editor);
166
return;
167
}
168
}
169
es.shader = Ref<Shader>(s);
170
Ref<VisualShader> vs = es.shader;
171
if (vs.is_valid()) {
172
VisualShaderEditor *vs_editor = memnew(VisualShaderEditor);
173
vs_editor->set_toggle_list_control(shader_list);
174
es.shader_editor = vs_editor;
175
} else {
176
TextShaderEditor *text_shader = memnew(TextShaderEditor);
177
text_shader->get_code_editor()->set_toggle_list_control(shader_list);
178
es.shader_editor = text_shader;
179
}
180
shader_tabs->add_child(es.shader_editor);
181
es.shader_editor->edit_shader(es.shader);
182
}
183
184
TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(es.shader_editor);
185
if (text_shader_editor) {
186
text_shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
187
CodeTextEditor *cte = text_shader_editor->get_code_editor();
188
if (cte) {
189
cte->set_zoom_factor(text_shader_zoom_factor);
190
cte->connect("zoomed", callable_mp(this, &ShaderEditorPlugin::_set_text_shader_zoom_factor));
191
cte->connect(SceneStringName(visibility_changed), callable_mp(this, &ShaderEditorPlugin::_update_shader_editor_zoom_factor).bind(cte));
192
}
193
}
194
195
shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
196
edited_shaders.push_back(es);
197
_update_shader_list();
198
_switch_to_editor(es.shader_editor);
199
}
200
201
bool ShaderEditorPlugin::handles(Object *p_object) const {
202
return Object::cast_to<Shader>(p_object) != nullptr || Object::cast_to<ShaderInclude>(p_object) != nullptr;
203
}
204
205
void ShaderEditorPlugin::make_visible(bool p_visible) {
206
if (p_visible) {
207
EditorNode::get_bottom_panel()->make_item_visible(window_wrapper);
208
}
209
}
210
211
void ShaderEditorPlugin::selected_notify() {
212
}
213
214
ShaderEditor *ShaderEditorPlugin::get_shader_editor(const Ref<Shader> &p_for_shader) {
215
for (EditedShader &edited_shader : edited_shaders) {
216
if (edited_shader.shader == p_for_shader) {
217
return edited_shader.shader_editor;
218
}
219
}
220
return nullptr;
221
}
222
223
void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
224
if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ShaderEditor", "window_rect")) {
225
window_wrapper->restore_window_from_saved_position(
226
p_layout->get_value("ShaderEditor", "window_rect", Rect2i()),
227
p_layout->get_value("ShaderEditor", "window_screen", -1),
228
p_layout->get_value("ShaderEditor", "window_screen_rect", Rect2i()));
229
} else {
230
window_wrapper->set_window_enabled(false);
231
}
232
233
if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
234
return;
235
}
236
if (!p_layout->has_section("ShaderEditor")) {
237
return;
238
}
239
if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||
240
!p_layout->has_section_key("ShaderEditor", "selected_shader")) {
241
return;
242
}
243
244
Array shaders = p_layout->get_value("ShaderEditor", "open_shaders");
245
int selected_shader_idx = 0;
246
String selected_shader = p_layout->get_value("ShaderEditor", "selected_shader");
247
for (int i = 0; i < shaders.size(); i++) {
248
String path = shaders[i];
249
Ref<Resource> res = ResourceLoader::load(path);
250
if (res.is_valid()) {
251
edit(res.ptr());
252
}
253
if (selected_shader == path) {
254
selected_shader_idx = i;
255
}
256
}
257
258
if (p_layout->has_section_key("ShaderEditor", "split_offset")) {
259
files_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));
260
}
261
262
_update_shader_list();
263
_shader_selected(selected_shader_idx, false);
264
265
_set_text_shader_zoom_factor(p_layout->get_value("ShaderEditor", "text_shader_zoom_factor", 1.0f));
266
}
267
268
void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
269
if (window_wrapper->get_window_enabled()) {
270
p_layout->set_value("ShaderEditor", "window_rect", window_wrapper->get_window_rect());
271
int screen = window_wrapper->get_window_screen();
272
p_layout->set_value("ShaderEditor", "window_screen", screen);
273
p_layout->set_value("ShaderEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));
274
275
} else {
276
if (p_layout->has_section_key("ShaderEditor", "window_rect")) {
277
p_layout->erase_section_key("ShaderEditor", "window_rect");
278
}
279
if (p_layout->has_section_key("ShaderEditor", "window_screen")) {
280
p_layout->erase_section_key("ShaderEditor", "window_screen");
281
}
282
if (p_layout->has_section_key("ShaderEditor", "window_screen_rect")) {
283
p_layout->erase_section_key("ShaderEditor", "window_screen_rect");
284
}
285
}
286
287
Array shaders;
288
String selected_shader;
289
for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
290
EditedShader edited_shader = edited_shaders[i];
291
if (edited_shader.shader_editor) {
292
String shader_path;
293
if (edited_shader.shader.is_valid()) {
294
shader_path = edited_shader.shader->get_path();
295
} else {
296
DEV_ASSERT(edited_shader.shader_inc.is_valid());
297
shader_path = edited_shader.shader_inc->get_path();
298
}
299
shaders.push_back(shader_path);
300
301
ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_current_tab_control());
302
303
if (shader_editor && edited_shader.shader_editor == shader_editor) {
304
selected_shader = shader_path;
305
}
306
}
307
}
308
p_layout->set_value("ShaderEditor", "open_shaders", shaders);
309
p_layout->set_value("ShaderEditor", "split_offset", files_split->get_split_offset());
310
p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
311
p_layout->set_value("ShaderEditor", "text_shader_zoom_factor", text_shader_zoom_factor);
312
}
313
314
String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
315
// TODO: This should also include visual shaders and shader includes, but save_external_data() doesn't seem to save them...
316
PackedStringArray unsaved_shaders;
317
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
318
if (edited_shaders[i].shader_editor) {
319
if (edited_shaders[i].shader_editor->is_unsaved()) {
320
if (unsaved_shaders.is_empty()) {
321
unsaved_shaders.append(TTR("Save changes to the following shaders(s) before quitting?"));
322
}
323
unsaved_shaders.append(edited_shaders[i].name.trim_suffix("(*)"));
324
}
325
}
326
}
327
328
if (!p_for_scene.is_empty()) {
329
PackedStringArray unsaved_built_in_shaders;
330
331
const String scene_file = p_for_scene.get_file();
332
for (const String &E : unsaved_shaders) {
333
if (!E.is_resource_file() && E.contains(scene_file)) {
334
if (unsaved_built_in_shaders.is_empty()) {
335
unsaved_built_in_shaders.append(TTR("There are unsaved changes in the following built-in shaders(s):"));
336
}
337
unsaved_built_in_shaders.append(E);
338
}
339
}
340
341
if (!unsaved_built_in_shaders.is_empty()) {
342
return String("\n").join(unsaved_built_in_shaders);
343
}
344
return String();
345
}
346
347
return String("\n").join(unsaved_shaders);
348
}
349
350
void ShaderEditorPlugin::save_external_data() {
351
for (EditedShader &edited_shader : edited_shaders) {
352
if (edited_shader.shader_editor && edited_shader.shader_editor->is_unsaved()) {
353
edited_shader.shader_editor->save_external_data();
354
}
355
}
356
_update_shader_list();
357
}
358
359
void ShaderEditorPlugin::apply_changes() {
360
for (EditedShader &edited_shader : edited_shaders) {
361
if (edited_shader.shader_editor) {
362
edited_shader.shader_editor->apply_shaders();
363
}
364
}
365
}
366
367
void ShaderEditorPlugin::_shader_selected(int p_index, bool p_push_item) {
368
if (p_index >= (int)edited_shaders.size()) {
369
return;
370
}
371
372
if (edited_shaders[p_index].shader_editor) {
373
_switch_to_editor(edited_shaders[p_index].shader_editor);
374
edited_shaders[p_index].shader_editor->validate_script();
375
}
376
377
shader_tabs->set_current_tab(p_index);
378
shader_list->select(p_index);
379
380
if (p_push_item) {
381
// Avoid `Shader` being edited when editing `ShaderInclude` due to inspector refreshing.
382
if (edited_shaders[p_index].shader.is_valid()) {
383
EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader.ptr());
384
} else {
385
EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader_inc.ptr());
386
}
387
}
388
}
389
390
void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
391
if (p_mouse_button_index == MouseButton::MIDDLE) {
392
_close_shader(p_item);
393
}
394
if (p_mouse_button_index == MouseButton::RIGHT) {
395
_make_script_list_context_menu();
396
}
397
}
398
399
void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {
400
if (p_type == FILE) {
401
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW);
402
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTRC("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_INCLUDE);
403
p_menu->add_separator();
404
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_MENU_OPEN);
405
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTRC("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_MENU_OPEN_INCLUDE);
406
}
407
408
if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
409
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTRC("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_MENU_SAVE);
410
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTRC("Save File As...")), FILE_MENU_SAVE_AS);
411
}
412
413
if (p_type == FILE) {
414
p_menu->add_separator();
415
p_menu->add_item(TTR("Open File in Inspector"), FILE_MENU_INSPECT);
416
p_menu->add_item(TTR("Inspect Native Shader Code..."), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);
417
p_menu->add_separator();
418
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
419
p_menu->add_separator();
420
p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
421
} else {
422
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
423
p_menu->add_item(TTR("Close All"), FILE_MENU_CLOSE_ALL);
424
p_menu->add_item(TTR("Close Other Tabs"), FILE_MENU_CLOSE_OTHER_TABS);
425
if (p_type == CONTEXT_VALID_ITEM) {
426
p_menu->add_separator();
427
p_menu->add_item(TTR("Copy Script Path"), FILE_MENU_COPY_PATH);
428
p_menu->add_item(TTR("Show in FileSystem"), FILE_MENU_SHOW_IN_FILE_SYSTEM);
429
}
430
}
431
}
432
433
void ShaderEditorPlugin::_make_script_list_context_menu() {
434
context_menu->clear();
435
436
int selected = shader_tabs->get_current_tab();
437
if (selected < 0 || selected >= shader_tabs->get_tab_count()) {
438
return;
439
}
440
441
Control *control = shader_tabs->get_tab_control(selected);
442
bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);
443
444
_setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
445
446
context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
447
context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
448
449
context_menu->set_position(files_split->get_screen_position() + files_split->get_local_mouse_position());
450
context_menu->reset_size();
451
context_menu->popup();
452
}
453
454
void ShaderEditorPlugin::_close_shader(int p_index) {
455
ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
456
if (file_menu->get_parent() != nullptr) {
457
file_menu->get_parent()->remove_child(file_menu);
458
}
459
if (make_floating->get_parent()) {
460
make_floating->get_parent()->remove_child(make_floating);
461
}
462
ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(p_index));
463
ERR_FAIL_NULL(shader_editor);
464
465
memdelete(shader_editor);
466
edited_shaders.remove_at(p_index);
467
_update_shader_list();
468
EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
469
470
if (shader_tabs->get_tab_count() == 0) {
471
shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
472
} else {
473
_switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);
474
}
475
}
476
477
void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
478
for (uint32_t i = 0; i < edited_shaders.size();) {
479
Ref<Shader> &shader = edited_shaders[i].shader;
480
if (shader.is_valid()) {
481
if (shader->is_built_in() && shader->get_path().begins_with(p_scene)) {
482
_close_shader(i);
483
continue;
484
}
485
}
486
Ref<ShaderInclude> &include = edited_shaders[i].shader_inc;
487
if (include.is_valid()) {
488
if (include->is_built_in() && include->get_path().begins_with(p_scene)) {
489
_close_shader(i);
490
continue;
491
}
492
}
493
i++;
494
}
495
}
496
497
void ShaderEditorPlugin::_resource_saved(Object *obj) {
498
// May have been renamed on save.
499
for (EditedShader &edited_shader : edited_shaders) {
500
if (edited_shader.shader.ptr() == obj || edited_shader.shader_inc.ptr() == obj) {
501
_update_shader_list();
502
return;
503
}
504
}
505
}
506
507
void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
508
switch (p_index) {
509
case FILE_MENU_NEW: {
510
String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
511
shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 0);
512
shader_create_dialog->popup_centered();
513
} break;
514
case FILE_MENU_NEW_INCLUDE: {
515
String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
516
shader_create_dialog->config(base_path.path_join("new_shader"), false, false, 2);
517
shader_create_dialog->popup_centered();
518
} break;
519
case FILE_MENU_OPEN: {
520
InspectorDock::get_singleton()->open_resource("Shader");
521
} break;
522
case FILE_MENU_OPEN_INCLUDE: {
523
InspectorDock::get_singleton()->open_resource("ShaderInclude");
524
} break;
525
case FILE_MENU_SAVE: {
526
int index = shader_tabs->get_current_tab();
527
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
528
TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
529
if (editor) {
530
if (editor->get_trim_trailing_whitespace_on_save()) {
531
editor->trim_trailing_whitespace();
532
}
533
534
if (editor->get_trim_final_newlines_on_save()) {
535
editor->trim_final_newlines();
536
}
537
}
538
if (edited_shaders[index].shader.is_valid()) {
539
EditorNode::get_singleton()->save_resource(edited_shaders[index].shader);
540
} else {
541
EditorNode::get_singleton()->save_resource(edited_shaders[index].shader_inc);
542
}
543
if (editor) {
544
editor->tag_saved_version();
545
}
546
} break;
547
case FILE_MENU_SAVE_AS: {
548
int index = shader_tabs->get_current_tab();
549
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
550
TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
551
if (editor) {
552
if (editor->get_trim_trailing_whitespace_on_save()) {
553
editor->trim_trailing_whitespace();
554
}
555
556
if (editor->get_trim_final_newlines_on_save()) {
557
editor->trim_final_newlines();
558
}
559
}
560
String path;
561
if (edited_shaders[index].shader.is_valid()) {
562
path = edited_shaders[index].shader->get_path();
563
if (!path.is_resource_file()) {
564
path = "";
565
}
566
EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader, path);
567
} else {
568
path = edited_shaders[index].shader_inc->get_path();
569
if (!path.is_resource_file()) {
570
path = "";
571
}
572
EditorNode::get_singleton()->save_resource_as(edited_shaders[index].shader_inc, path);
573
}
574
if (editor) {
575
editor->tag_saved_version();
576
}
577
} break;
578
case FILE_MENU_INSPECT: {
579
int index = shader_tabs->get_current_tab();
580
ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
581
if (edited_shaders[index].shader.is_valid()) {
582
EditorNode::get_singleton()->push_item(edited_shaders[index].shader.ptr());
583
} else {
584
EditorNode::get_singleton()->push_item(edited_shaders[index].shader_inc.ptr());
585
}
586
} break;
587
case FILE_MENU_INSPECT_NATIVE_SHADER_CODE: {
588
int index = shader_tabs->get_current_tab();
589
if (edited_shaders[index].shader.is_valid()) {
590
edited_shaders[index].shader->inspect_native_shader_code();
591
}
592
} break;
593
case FILE_MENU_CLOSE: {
594
_close_shader(shader_tabs->get_current_tab());
595
} break;
596
case FILE_MENU_CLOSE_ALL: {
597
while (shader_tabs->get_tab_count() > 0) {
598
_close_shader(0);
599
}
600
} break;
601
case FILE_MENU_CLOSE_OTHER_TABS: {
602
int index = shader_tabs->get_current_tab();
603
for (int i = 0; i < index; i++) {
604
_close_shader(0);
605
}
606
while (shader_tabs->get_tab_count() > 1) {
607
_close_shader(1);
608
}
609
} break;
610
case FILE_MENU_SHOW_IN_FILE_SYSTEM: {
611
Ref<Resource> shader = _get_current_shader();
612
String path = shader->get_path();
613
if (!path.is_empty()) {
614
FileSystemDock::get_singleton()->navigate_to_path(path);
615
}
616
} break;
617
case FILE_MENU_COPY_PATH: {
618
Ref<Resource> shader = _get_current_shader();
619
DisplayServer::get_singleton()->clipboard_set(shader->get_path());
620
} break;
621
case FILE_MENU_TOGGLE_FILES_PANEL: {
622
shader_list->set_visible(!shader_list->is_visible());
623
624
int index = shader_tabs->get_current_tab();
625
if (index != -1) {
626
ERR_FAIL_INDEX(index, (int)edited_shaders.size());
627
TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
628
if (editor) {
629
editor->get_code_editor()->update_toggle_files_button();
630
} else {
631
VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(edited_shaders[index].shader_editor);
632
if (vs_editor) {
633
vs_editor->update_toggle_files_button();
634
}
635
}
636
}
637
} break;
638
}
639
}
640
641
void ShaderEditorPlugin::_shader_created(Ref<Shader> p_shader) {
642
EditorNode::get_singleton()->push_item(p_shader.ptr());
643
}
644
645
void ShaderEditorPlugin::_shader_include_created(Ref<ShaderInclude> p_shader_inc) {
646
EditorNode::get_singleton()->push_item(p_shader_inc.ptr());
647
}
648
649
Variant ShaderEditorPlugin::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
650
if (shader_list->get_item_count() == 0) {
651
return Variant();
652
}
653
654
int idx = 0;
655
if (p_point == Vector2(Math::INF, Math::INF)) {
656
if (shader_list->is_anything_selected()) {
657
idx = shader_list->get_selected_items()[0];
658
}
659
} else {
660
idx = shader_list->get_item_at_position(p_point);
661
}
662
if (idx < 0) {
663
return Variant();
664
}
665
666
HBoxContainer *drag_preview = memnew(HBoxContainer);
667
String preview_name = shader_list->get_item_text(idx);
668
Ref<Texture2D> preview_icon = shader_list->get_item_icon(idx);
669
670
if (preview_icon.is_valid()) {
671
TextureRect *tf = memnew(TextureRect);
672
tf->set_texture(preview_icon);
673
tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
674
drag_preview->add_child(tf);
675
}
676
Label *label = memnew(Label(preview_name));
677
label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names.
678
drag_preview->add_child(label);
679
files_split->set_drag_preview(drag_preview);
680
681
Dictionary drag_data;
682
drag_data["type"] = "shader_list_element";
683
drag_data["shader_list_element"] = idx;
684
685
return drag_data;
686
}
687
688
bool ShaderEditorPlugin::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
689
Dictionary d = p_data;
690
if (!d.has("type")) {
691
return false;
692
}
693
694
if (String(d["type"]) == "shader_list_element") {
695
return true;
696
}
697
698
if (String(d["type"]) == "files") {
699
Vector<String> files = d["files"];
700
701
if (files.is_empty()) {
702
return false;
703
}
704
705
for (int i = 0; i < files.size(); i++) {
706
const String &file = files[i];
707
if (ResourceLoader::exists(file, "Shader")) {
708
Ref<Shader> shader = ResourceLoader::load(file);
709
if (shader.is_valid()) {
710
return true;
711
}
712
}
713
if (ResourceLoader::exists(file, "ShaderInclude")) {
714
Ref<ShaderInclude> sinclude = ResourceLoader::load(file);
715
if (sinclude.is_valid()) {
716
return true;
717
}
718
}
719
}
720
return false;
721
}
722
723
return false;
724
}
725
726
void ShaderEditorPlugin::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
727
if (!can_drop_data_fw(p_point, p_data, p_from)) {
728
return;
729
}
730
731
Dictionary d = p_data;
732
if (!d.has("type")) {
733
return;
734
}
735
736
if (String(d["type"]) == "shader_list_element") {
737
int idx = d["shader_list_element"];
738
int new_idx = 0;
739
if (p_point == Vector2(Math::INF, Math::INF)) {
740
if (shader_list->is_anything_selected()) {
741
new_idx = shader_list->get_selected_items()[0];
742
}
743
} else {
744
new_idx = shader_list->get_item_at_position(p_point);
745
}
746
_move_shader_tab(idx, new_idx);
747
return;
748
}
749
750
if (String(d["type"]) == "files") {
751
Vector<String> files = d["files"];
752
753
for (int i = 0; i < files.size(); i++) {
754
const String &file = files[i];
755
Ref<Resource> res;
756
if (ResourceLoader::exists(file, "Shader") || ResourceLoader::exists(file, "ShaderInclude")) {
757
res = ResourceLoader::load(file);
758
}
759
if (res.is_valid()) {
760
edit(res.ptr());
761
}
762
}
763
}
764
}
765
766
void ShaderEditorPlugin::_window_changed(bool p_visible) {
767
make_floating->set_visible(!p_visible);
768
}
769
770
void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
771
if (text_shader_zoom_factor == p_zoom_factor) {
772
return;
773
}
774
775
text_shader_zoom_factor = p_zoom_factor;
776
}
777
778
void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const {
779
if (p_shader_editor && p_shader_editor->is_visible_in_tree() && text_shader_zoom_factor != p_shader_editor->get_zoom_factor()) {
780
p_shader_editor->set_zoom_factor(text_shader_zoom_factor);
781
}
782
}
783
784
void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
785
if (file_menu->get_parent() != nullptr) {
786
file_menu->get_parent()->remove_child(file_menu);
787
}
788
if (make_floating->get_parent()) {
789
make_floating->get_parent()->remove_child(make_floating);
790
}
791
p_editor->use_menu_bar_items(file_menu, make_floating);
792
}
793
794
void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
795
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
796
if (edited_shaders[i].path == p_removed_file) {
797
_close_shader(i);
798
break;
799
}
800
}
801
}
802
803
void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {
804
if (p_res.is_null()) {
805
return;
806
}
807
const String &path = p_res->get_path();
808
809
for (EditedShader &edited : edited_shaders) {
810
Ref<Resource> shader_res = edited.shader;
811
if (shader_res.is_null()) {
812
shader_res = edited.shader_inc;
813
}
814
ERR_FAIL_COND(shader_res.is_null());
815
816
TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(edited.shader_editor);
817
if (!text_shader_editor || !shader_res->is_built_in()) {
818
continue;
819
}
820
821
if (shader_res->get_path().get_slice("::", 0) == path) {
822
text_shader_editor->tag_saved_version();
823
_update_shader_list();
824
}
825
}
826
}
827
828
void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {
829
PopupMenu *file_popup_menu = file_menu->get_popup();
830
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE), p_disabled);
831
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE_AS), p_disabled);
832
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT), p_disabled);
833
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), p_disabled);
834
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_CLOSE), p_disabled);
835
}
836
837
void ShaderEditorPlugin::_notification(int p_what) {
838
switch (p_what) {
839
case NOTIFICATION_READY: {
840
EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_resource_saved), CONNECT_DEFERRED);
841
EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ShaderEditorPlugin::_close_builtin_shaders_from_scene));
842
FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ShaderEditorPlugin::_file_removed));
843
EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ShaderEditorPlugin::_res_saved_callback));
844
} break;
845
}
846
}
847
848
ShaderEditorPlugin::ShaderEditorPlugin() {
849
window_wrapper = memnew(WindowWrapper);
850
window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Shader Editor")));
851
window_wrapper->set_margins_enabled(true);
852
853
main_container = memnew(VBoxContainer);
854
Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTRC("Make Floating"));
855
window_wrapper->set_wrapped_control(main_container, make_floating_shortcut);
856
857
files_split = memnew(HSplitContainer);
858
files_split->set_split_offset(200 * EDSCALE);
859
files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
860
861
file_menu = memnew(MenuButton);
862
file_menu->set_flat(false);
863
file_menu->set_theme_type_variation("FlatMenuButton");
864
file_menu->set_text(TTR("File"));
865
file_menu->set_switch_on_hover(true);
866
file_menu->set_shortcut_context(files_split);
867
_setup_popup_menu(FILE, file_menu->get_popup());
868
file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
869
870
_set_file_specific_items_disabled(true);
871
872
context_menu = memnew(PopupMenu);
873
add_child(context_menu);
874
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
875
876
make_floating = memnew(ScreenSelect);
877
make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
878
if (!make_floating->is_disabled()) {
879
// Override default ScreenSelect tooltip if multi-window support is available.
880
make_floating->set_tooltip_text(TTR("Make the shader editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
881
}
882
883
window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
884
885
shader_list = memnew(ItemList);
886
shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
887
shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
888
shader_list->set_theme_type_variation("ItemListSecondary");
889
shader_list->set_custom_minimum_size(Size2(100, 60) * EDSCALE);
890
files_split->add_child(shader_list);
891
shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected).bind(true));
892
shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
893
shader_list->set_allow_rmb_select(true);
894
SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
895
896
main_container->add_child(files_split);
897
main_container->set_custom_minimum_size(Size2(100, 300) * EDSCALE);
898
899
shader_tabs = memnew(TabContainer);
900
shader_tabs->set_custom_minimum_size(Size2(460, 300) * EDSCALE);
901
shader_tabs->set_tabs_visible(false);
902
shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
903
files_split->add_child(shader_tabs);
904
Ref<StyleBoxEmpty> empty;
905
empty.instantiate();
906
shader_tabs->add_theme_style_override(SceneStringName(panel), empty);
907
908
button = EditorNode::get_bottom_panel()->add_item(TTRC("Shader Editor"), window_wrapper, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_shader_editor_bottom_panel", TTRC("Toggle Shader Editor Bottom Panel"), KeyModifierMask::ALT | Key::S));
909
910
shader_create_dialog = memnew(ShaderCreateDialog);
911
files_split->add_child(shader_create_dialog);
912
shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
913
shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
914
}
915
916
ShaderEditorPlugin::~ShaderEditorPlugin() {
917
memdelete(file_menu);
918
memdelete(make_floating);
919
}
920
921