1 /*************************************************************************/
2 /* shader_editor_plugin.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "core/io/resource_loader.h"
34 #include "core/io/resource_saver.h"
35 #include "core/os/keyboard.h"
36 #include "core/os/os.h"
37 #include "editor/editor_node.h"
38 #include "editor/editor_scale.h"
39 #include "editor/editor_settings.h"
40 #include "editor/property_editor.h"
41 #include "servers/visual/shader_types.h"
42
43 /*** SHADER SCRIPT EDITOR ****/
44
get_edited_shader() const45 Ref<Shader> ShaderTextEditor::get_edited_shader() const {
46
47 return shader;
48 }
set_edited_shader(const Ref<Shader> & p_shader)49 void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) {
50
51 if (shader == p_shader) {
52 return;
53 }
54 shader = p_shader;
55
56 _load_theme_settings();
57
58 get_text_edit()->set_text(p_shader->get_code());
59 get_text_edit()->clear_undo_history();
60
61 _validate_script();
62 _line_col_changed();
63 }
64
reload_text()65 void ShaderTextEditor::reload_text() {
66 ERR_FAIL_COND(shader.is_null());
67
68 TextEdit *te = get_text_edit();
69 int column = te->cursor_get_column();
70 int row = te->cursor_get_line();
71 int h = te->get_h_scroll();
72 int v = te->get_v_scroll();
73
74 te->set_text(shader->get_code());
75 te->cursor_set_line(row);
76 te->cursor_set_column(column);
77 te->set_h_scroll(h);
78 te->set_v_scroll(v);
79
80 te->tag_saved_version();
81
82 update_line_and_column();
83 }
84
_load_theme_settings()85 void ShaderTextEditor::_load_theme_settings() {
86
87 get_text_edit()->clear_colors();
88
89 Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
90 Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
91 Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
92 Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color");
93 Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color");
94 Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
95 Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
96 Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color");
97 Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color");
98 Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color");
99 Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color");
100 Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color");
101 Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color");
102 Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
103 Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
104 Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
105 Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
106 Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
107 Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
108 Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
109 Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
110 Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
111 Color executing_line_color = EDITOR_GET("text_editor/highlighting/executing_line_color");
112 Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
113 Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
114 Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
115 Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
116 Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
117 Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
118
119 get_text_edit()->add_color_override("background_color", background_color);
120 get_text_edit()->add_color_override("completion_background_color", completion_background_color);
121 get_text_edit()->add_color_override("completion_selected_color", completion_selected_color);
122 get_text_edit()->add_color_override("completion_existing_color", completion_existing_color);
123 get_text_edit()->add_color_override("completion_scroll_color", completion_scroll_color);
124 get_text_edit()->add_color_override("completion_font_color", completion_font_color);
125 get_text_edit()->add_color_override("font_color", text_color);
126 get_text_edit()->add_color_override("line_number_color", line_number_color);
127 get_text_edit()->add_color_override("caret_color", caret_color);
128 get_text_edit()->add_color_override("caret_background_color", caret_background_color);
129 get_text_edit()->add_color_override("font_color_selected", text_selected_color);
130 get_text_edit()->add_color_override("selection_color", selection_color);
131 get_text_edit()->add_color_override("brace_mismatch_color", brace_mismatch_color);
132 get_text_edit()->add_color_override("current_line_color", current_line_color);
133 get_text_edit()->add_color_override("line_length_guideline_color", line_length_guideline_color);
134 get_text_edit()->add_color_override("word_highlighted_color", word_highlighted_color);
135 get_text_edit()->add_color_override("number_color", number_color);
136 get_text_edit()->add_color_override("function_color", function_color);
137 get_text_edit()->add_color_override("member_variable_color", member_variable_color);
138 get_text_edit()->add_color_override("mark_color", mark_color);
139 get_text_edit()->add_color_override("bookmark_color", bookmark_color);
140 get_text_edit()->add_color_override("breakpoint_color", breakpoint_color);
141 get_text_edit()->add_color_override("executing_line_color", executing_line_color);
142 get_text_edit()->add_color_override("code_folding_color", code_folding_color);
143 get_text_edit()->add_color_override("search_result_color", search_result_color);
144 get_text_edit()->add_color_override("search_result_border_color", search_result_border_color);
145 get_text_edit()->add_color_override("symbol_color", symbol_color);
146
147 List<String> keywords;
148 ShaderLanguage::get_keyword_list(&keywords);
149
150 if (shader.is_valid()) {
151
152 for (const Map<StringName, ShaderLanguage::FunctionInfo>::Element *E = ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())).front(); E; E = E->next()) {
153
154 for (const Map<StringName, ShaderLanguage::BuiltInInfo>::Element *F = E->get().built_ins.front(); F; F = F->next()) {
155 keywords.push_back(F->key());
156 }
157 }
158
159 for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())).size(); i++) {
160
161 keywords.push_back(ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode()))[i]);
162 }
163 }
164
165 for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
166
167 get_text_edit()->add_keyword_color(E->get(), keyword_color);
168 }
169
170 //colorize comments
171 get_text_edit()->add_color_region("/*", "*/", comment_color, false);
172 get_text_edit()->add_color_region("//", "", comment_color, false);
173 }
174
_check_shader_mode()175 void ShaderTextEditor::_check_shader_mode() {
176
177 String type = ShaderLanguage::get_shader_type(get_text_edit()->get_text());
178
179 Shader::Mode mode;
180
181 if (type == "canvas_item") {
182 mode = Shader::MODE_CANVAS_ITEM;
183 } else if (type == "particles") {
184 mode = Shader::MODE_PARTICLES;
185 } else {
186 mode = Shader::MODE_SPATIAL;
187 }
188
189 if (shader->get_mode() != mode) {
190 shader->set_code(get_text_edit()->get_text());
191 _load_theme_settings();
192 }
193 }
194
_code_complete_script(const String & p_code,List<ScriptCodeCompletionOption> * r_options)195 void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptCodeCompletionOption> *r_options) {
196
197 _check_shader_mode();
198
199 ShaderLanguage sl;
200 String calltip;
201
202 sl.complete(p_code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_types(), r_options, calltip);
203
204 get_text_edit()->set_code_hint(calltip);
205 }
206
_validate_script()207 void ShaderTextEditor::_validate_script() {
208
209 _check_shader_mode();
210
211 String code = get_text_edit()->get_text();
212 //List<StringName> params;
213 //shader->get_param_list(¶ms);
214
215 ShaderLanguage sl;
216
217 Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader->get_mode())), ShaderTypes::get_singleton()->get_types());
218
219 if (err != OK) {
220 String error_text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
221 set_error(error_text);
222 set_error_pos(sl.get_error_line() - 1, 0);
223 for (int i = 0; i < get_text_edit()->get_line_count(); i++)
224 get_text_edit()->set_line_as_marked(i, false);
225 get_text_edit()->set_line_as_marked(sl.get_error_line() - 1, true);
226
227 } else {
228 for (int i = 0; i < get_text_edit()->get_line_count(); i++)
229 get_text_edit()->set_line_as_marked(i, false);
230 set_error("");
231 }
232
233 emit_signal("script_changed");
234 }
235
_bind_methods()236 void ShaderTextEditor::_bind_methods() {
237 }
238
ShaderTextEditor()239 ShaderTextEditor::ShaderTextEditor() {
240 }
241
242 /*** SCRIPT EDITOR ******/
243
_menu_option(int p_option)244 void ShaderEditor::_menu_option(int p_option) {
245
246 switch (p_option) {
247 case EDIT_UNDO: {
248 shader_editor->get_text_edit()->undo();
249 } break;
250 case EDIT_REDO: {
251 shader_editor->get_text_edit()->redo();
252 } break;
253 case EDIT_CUT: {
254 shader_editor->get_text_edit()->cut();
255 } break;
256 case EDIT_COPY: {
257 shader_editor->get_text_edit()->copy();
258 } break;
259 case EDIT_PASTE: {
260 shader_editor->get_text_edit()->paste();
261 } break;
262 case EDIT_SELECT_ALL: {
263 shader_editor->get_text_edit()->select_all();
264 } break;
265 case EDIT_MOVE_LINE_UP: {
266 shader_editor->move_lines_up();
267 } break;
268 case EDIT_MOVE_LINE_DOWN: {
269 shader_editor->move_lines_down();
270 } break;
271 case EDIT_INDENT_LEFT: {
272
273 if (shader.is_null())
274 return;
275
276 TextEdit *tx = shader_editor->get_text_edit();
277 tx->indent_left();
278
279 } break;
280 case EDIT_INDENT_RIGHT: {
281
282 if (shader.is_null())
283 return;
284
285 TextEdit *tx = shader_editor->get_text_edit();
286 tx->indent_right();
287
288 } break;
289 case EDIT_DELETE_LINE: {
290 shader_editor->delete_lines();
291 } break;
292 case EDIT_CLONE_DOWN: {
293 shader_editor->clone_lines_down();
294 } break;
295 case EDIT_TOGGLE_COMMENT: {
296
297 if (shader.is_null())
298 return;
299
300 shader_editor->toggle_inline_comment("//");
301
302 } break;
303 case EDIT_COMPLETE: {
304
305 shader_editor->get_text_edit()->query_code_comple();
306 } break;
307 case SEARCH_FIND: {
308
309 shader_editor->get_find_replace_bar()->popup_search();
310 } break;
311 case SEARCH_FIND_NEXT: {
312
313 shader_editor->get_find_replace_bar()->search_next();
314 } break;
315 case SEARCH_FIND_PREV: {
316
317 shader_editor->get_find_replace_bar()->search_prev();
318 } break;
319 case SEARCH_REPLACE: {
320
321 shader_editor->get_find_replace_bar()->popup_replace();
322 } break;
323 case SEARCH_GOTO_LINE: {
324
325 goto_line_dialog->popup_find_line(shader_editor->get_text_edit());
326 } break;
327 case BOOKMARK_TOGGLE: {
328
329 shader_editor->toggle_bookmark();
330 } break;
331 case BOOKMARK_GOTO_NEXT: {
332
333 shader_editor->goto_next_bookmark();
334 } break;
335 case BOOKMARK_GOTO_PREV: {
336
337 shader_editor->goto_prev_bookmark();
338 } break;
339 case BOOKMARK_REMOVE_ALL: {
340
341 shader_editor->remove_all_bookmarks();
342 } break;
343 case HELP_DOCS: {
344 OS::get_singleton()->shell_open("https://docs.godotengine.org/en/stable/tutorials/shading/shading_reference/index.html");
345 } break;
346 }
347 if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
348 shader_editor->get_text_edit()->call_deferred("grab_focus");
349 }
350 }
351
_notification(int p_what)352 void ShaderEditor::_notification(int p_what) {
353
354 if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN) {
355 _check_for_external_edit();
356 }
357 }
358
_params_changed()359 void ShaderEditor::_params_changed() {
360
361 shader_editor->_validate_script();
362 }
363
_editor_settings_changed()364 void ShaderEditor::_editor_settings_changed() {
365
366 shader_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
367 shader_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
368 shader_editor->get_text_edit()->set_indent_size(EditorSettings::get_singleton()->get("text_editor/indent/size"));
369 shader_editor->get_text_edit()->set_indent_using_spaces(EditorSettings::get_singleton()->get("text_editor/indent/type"));
370 shader_editor->get_text_edit()->set_auto_indent(EditorSettings::get_singleton()->get("text_editor/indent/auto_indent"));
371 shader_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/indent/draw_tabs"));
372 shader_editor->get_text_edit()->set_draw_spaces(EditorSettings::get_singleton()->get("text_editor/indent/draw_spaces"));
373 shader_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
374 shader_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting"));
375 shader_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences"));
376 shader_editor->get_text_edit()->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line"));
377 shader_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
378 shader_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
379 shader_editor->get_text_edit()->add_constant_override("line_spacing", EditorSettings::get_singleton()->get("text_editor/theme/line_spacing"));
380 shader_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
381 shader_editor->get_text_edit()->set_smooth_scroll_enabled(EditorSettings::get_singleton()->get("text_editor/navigation/smooth_scrolling"));
382 shader_editor->get_text_edit()->set_v_scroll_speed(EditorSettings::get_singleton()->get("text_editor/navigation/v_scroll_speed"));
383 shader_editor->get_text_edit()->set_draw_minimap(EditorSettings::get_singleton()->get("text_editor/navigation/show_minimap"));
384 shader_editor->get_text_edit()->set_minimap_width((int)EditorSettings::get_singleton()->get("text_editor/navigation/minimap_width") * EDSCALE);
385 shader_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guideline"));
386 shader_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_column"));
387 shader_editor->get_text_edit()->set_breakpoint_gutter_enabled(false);
388 }
389
_bind_methods()390 void ShaderEditor::_bind_methods() {
391
392 ClassDB::bind_method("_reload_shader_from_disk", &ShaderEditor::_reload_shader_from_disk);
393 ClassDB::bind_method("_editor_settings_changed", &ShaderEditor::_editor_settings_changed);
394 ClassDB::bind_method("_text_edit_gui_input", &ShaderEditor::_text_edit_gui_input);
395
396 ClassDB::bind_method("_update_bookmark_list", &ShaderEditor::_update_bookmark_list);
397 ClassDB::bind_method("_bookmark_item_pressed", &ShaderEditor::_bookmark_item_pressed);
398
399 ClassDB::bind_method("_menu_option", &ShaderEditor::_menu_option);
400 ClassDB::bind_method("_params_changed", &ShaderEditor::_params_changed);
401 ClassDB::bind_method("apply_shaders", &ShaderEditor::apply_shaders);
402 ClassDB::bind_method("save_external_data", &ShaderEditor::save_external_data);
403 }
404
ensure_select_current()405 void ShaderEditor::ensure_select_current() {
406
407 /*
408 if (tab_container->get_child_count() && tab_container->get_current_tab()>=0) {
409
410 ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(tab_container->get_child(tab_container->get_current_tab()));
411 if (!ste)
412 return;
413 Ref<Shader> shader = ste->get_edited_shader();
414 get_scene()->get_root_node()->call("_resource_selected",shader);
415 }*/
416 }
417
goto_line_selection(int p_line,int p_begin,int p_end)418 void ShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
419
420 shader_editor->goto_line_selection(p_line, p_begin, p_end);
421 }
422
_check_for_external_edit()423 void ShaderEditor::_check_for_external_edit() {
424
425 if (shader.is_null() || !shader.is_valid()) {
426 return;
427 }
428
429 // internal shader.
430 if (shader->get_path() == "" || shader->get_path().find("local://") != -1 || shader->get_path().find("::") != -1) {
431 return;
432 }
433
434 bool use_autoreload = bool(EDITOR_DEF("text_editor/files/auto_reload_scripts_on_external_change", false));
435 if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) {
436 if (use_autoreload) {
437 _reload_shader_from_disk();
438 } else {
439 disk_changed->call_deferred("popup_centered");
440 }
441 }
442 }
443
_reload_shader_from_disk()444 void ShaderEditor::_reload_shader_from_disk() {
445
446 Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), true);
447 ERR_FAIL_COND(!rel_shader.is_valid());
448
449 shader->set_code(rel_shader->get_code());
450 shader->set_last_modified_time(rel_shader->get_last_modified_time());
451 shader_editor->reload_text();
452 }
453
edit(const Ref<Shader> & p_shader)454 void ShaderEditor::edit(const Ref<Shader> &p_shader) {
455
456 if (p_shader.is_null() || !p_shader->is_text_shader())
457 return;
458
459 if (shader == p_shader)
460 return;
461
462 shader = p_shader;
463
464 shader_editor->set_edited_shader(p_shader);
465
466 //vertex_editor->set_edited_shader(shader,ShaderLanguage::SHADER_MATERIAL_VERTEX);
467 // see if already has it
468 }
469
save_external_data(const String & p_str)470 void ShaderEditor::save_external_data(const String &p_str) {
471
472 if (shader.is_null()) {
473 disk_changed->hide();
474 return;
475 }
476
477 apply_shaders();
478 if (shader->get_path() != "" && shader->get_path().find("local://") == -1 && shader->get_path().find("::") == -1) {
479 //external shader, save it
480 ResourceSaver::save(shader->get_path(), shader);
481 }
482
483 disk_changed->hide();
484 }
485
apply_shaders()486 void ShaderEditor::apply_shaders() {
487
488 if (shader.is_valid()) {
489 String shader_code = shader->get_code();
490 String editor_code = shader_editor->get_text_edit()->get_text();
491 if (shader_code != editor_code) {
492 shader->set_code(editor_code);
493 shader->set_edited(true);
494 }
495 }
496 }
497
_text_edit_gui_input(const Ref<InputEvent> & ev)498 void ShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
499
500 Ref<InputEventMouseButton> mb = ev;
501
502 if (mb.is_valid()) {
503
504 if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
505
506 int col, row;
507 TextEdit *tx = shader_editor->get_text_edit();
508 tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
509 tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
510
511 if (tx->is_right_click_moving_caret()) {
512 if (tx->is_selection_active()) {
513
514 int from_line = tx->get_selection_from_line();
515 int to_line = tx->get_selection_to_line();
516 int from_column = tx->get_selection_from_column();
517 int to_column = tx->get_selection_to_column();
518
519 if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
520 // Right click is outside the selected text
521 tx->deselect();
522 }
523 }
524 if (!tx->is_selection_active()) {
525 tx->cursor_set_line(row, true, false);
526 tx->cursor_set_column(col);
527 }
528 }
529 _make_context_menu(tx->is_selection_active(), get_local_mouse_position());
530 }
531 }
532
533 Ref<InputEventKey> k = ev;
534 if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_MENU) {
535 TextEdit *tx = shader_editor->get_text_edit();
536 _make_context_menu(tx->is_selection_active(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->_get_cursor_pixel_pos()));
537 context_menu->grab_focus();
538 }
539 }
540
_update_bookmark_list()541 void ShaderEditor::_update_bookmark_list() {
542
543 bookmarks_menu->clear();
544
545 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
546 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
547 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
548 bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
549
550 Array bookmark_list = shader_editor->get_text_edit()->get_bookmarks_array();
551 if (bookmark_list.size() == 0) {
552 return;
553 }
554
555 bookmarks_menu->add_separator();
556
557 for (int i = 0; i < bookmark_list.size(); i++) {
558 String line = shader_editor->get_text_edit()->get_line(bookmark_list[i]).strip_edges();
559 // Limit the size of the line if too big.
560 if (line.length() > 50) {
561 line = line.substr(0, 50);
562 }
563
564 bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\"");
565 bookmarks_menu->set_item_metadata(bookmarks_menu->get_item_count() - 1, bookmark_list[i]);
566 }
567 }
568
_bookmark_item_pressed(int p_idx)569 void ShaderEditor::_bookmark_item_pressed(int p_idx) {
570
571 if (p_idx < 4) { // Any item before the separator.
572 _menu_option(bookmarks_menu->get_item_id(p_idx));
573 } else {
574 shader_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
575 }
576 }
577
_make_context_menu(bool p_selection,Vector2 p_position)578 void ShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) {
579
580 context_menu->clear();
581 if (p_selection) {
582 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
583 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
584 }
585
586 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
587 context_menu->add_separator();
588 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
589 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
590 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
591
592 context_menu->add_separator();
593 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
594 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
595 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
596 context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
597
598 context_menu->set_position(get_global_transform().xform(p_position));
599 context_menu->set_size(Vector2(1, 1));
600 context_menu->popup();
601 }
602
ShaderEditor(EditorNode * p_node)603 ShaderEditor::ShaderEditor(EditorNode *p_node) {
604
605 shader_editor = memnew(ShaderTextEditor);
606 shader_editor->set_v_size_flags(SIZE_EXPAND_FILL);
607 shader_editor->add_constant_override("separation", 0);
608 shader_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
609
610 shader_editor->connect("script_changed", this, "apply_shaders");
611 EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
612
613 shader_editor->get_text_edit()->set_callhint_settings(
614 EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
615 EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
616
617 shader_editor->get_text_edit()->set_select_identifiers_on_hover(true);
618 shader_editor->get_text_edit()->set_context_menu_enabled(false);
619 shader_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
620
621 shader_editor->update_editor_settings();
622
623 context_menu = memnew(PopupMenu);
624 add_child(context_menu);
625 context_menu->connect("id_pressed", this, "_menu_option");
626 context_menu->set_hide_on_window_lose_focus(true);
627
628 VBoxContainer *main_container = memnew(VBoxContainer);
629 HBoxContainer *hbc = memnew(HBoxContainer);
630
631 edit_menu = memnew(MenuButton);
632 edit_menu->set_text(TTR("Edit"));
633 edit_menu->set_switch_on_hover(true);
634 edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
635 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
636 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
637 edit_menu->get_popup()->add_separator();
638 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
639 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
640 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
641 edit_menu->get_popup()->add_separator();
642 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
643 edit_menu->get_popup()->add_separator();
644 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
645 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
646 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
647 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
648 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
649 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
650 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
651 edit_menu->get_popup()->add_separator();
652 edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
653 edit_menu->get_popup()->connect("id_pressed", this, "_menu_option");
654
655 search_menu = memnew(MenuButton);
656 search_menu->set_text(TTR("Search"));
657 search_menu->set_switch_on_hover(true);
658 search_menu->get_popup()->set_hide_on_window_lose_focus(true);
659 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
660 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
661 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
662 search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
663 search_menu->get_popup()->connect("id_pressed", this, "_menu_option");
664
665 MenuButton *goto_menu = memnew(MenuButton);
666 goto_menu->set_text(TTR("Go To"));
667 goto_menu->set_switch_on_hover(true);
668 goto_menu->get_popup()->connect("id_pressed", this, "_menu_option");
669
670 goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
671 goto_menu->get_popup()->add_separator();
672
673 bookmarks_menu = memnew(PopupMenu);
674 bookmarks_menu->set_name("Bookmarks");
675 goto_menu->get_popup()->add_child(bookmarks_menu);
676 goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
677 _update_bookmark_list();
678 bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
679 bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed");
680
681 help_menu = memnew(MenuButton);
682 help_menu->set_text(TTR("Help"));
683 help_menu->set_switch_on_hover(true);
684 help_menu->get_popup()->add_icon_item(p_node->get_gui_base()->get_icon("Instance", "EditorIcons"), TTR("Online Docs"), HELP_DOCS);
685 help_menu->get_popup()->connect("id_pressed", this, "_menu_option");
686
687 add_child(main_container);
688 main_container->add_child(hbc);
689 hbc->add_child(search_menu);
690 hbc->add_child(edit_menu);
691 hbc->add_child(goto_menu);
692 hbc->add_child(help_menu);
693 hbc->add_style_override("panel", p_node->get_gui_base()->get_stylebox("ScriptEditorPanel", "EditorStyles"));
694 main_container->add_child(shader_editor);
695
696 goto_line_dialog = memnew(GotoLineDialog);
697 add_child(goto_line_dialog);
698
699 disk_changed = memnew(ConfirmationDialog);
700
701 VBoxContainer *vbc = memnew(VBoxContainer);
702 disk_changed->add_child(vbc);
703
704 Label *dl = memnew(Label);
705 dl->set_text(TTR("This shader has been modified on on disk.\nWhat action should be taken?"));
706 vbc->add_child(dl);
707
708 disk_changed->connect("confirmed", this, "_reload_shader_from_disk");
709 disk_changed->get_ok()->set_text(TTR("Reload"));
710
711 disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
712 disk_changed->connect("custom_action", this, "save_external_data");
713
714 add_child(disk_changed);
715
716 _editor_settings_changed();
717 }
718
edit(Object * p_object)719 void ShaderEditorPlugin::edit(Object *p_object) {
720
721 Shader *s = Object::cast_to<Shader>(p_object);
722 shader_editor->edit(s);
723 }
724
handles(Object * p_object) const725 bool ShaderEditorPlugin::handles(Object *p_object) const {
726
727 Shader *shader = Object::cast_to<Shader>(p_object);
728 return shader != NULL && shader->is_text_shader();
729 }
730
make_visible(bool p_visible)731 void ShaderEditorPlugin::make_visible(bool p_visible) {
732
733 if (p_visible) {
734 button->show();
735 editor->make_bottom_panel_item_visible(shader_editor);
736
737 } else {
738
739 button->hide();
740 if (shader_editor->is_visible_in_tree())
741 editor->hide_bottom_panel();
742 shader_editor->apply_shaders();
743 }
744 }
745
selected_notify()746 void ShaderEditorPlugin::selected_notify() {
747
748 shader_editor->ensure_select_current();
749 }
750
save_external_data()751 void ShaderEditorPlugin::save_external_data() {
752
753 shader_editor->save_external_data();
754 }
755
apply_changes()756 void ShaderEditorPlugin::apply_changes() {
757
758 shader_editor->apply_shaders();
759 }
760
ShaderEditorPlugin(EditorNode * p_node)761 ShaderEditorPlugin::ShaderEditorPlugin(EditorNode *p_node) {
762
763 editor = p_node;
764 shader_editor = memnew(ShaderEditor(p_node));
765
766 shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
767 button = editor->add_bottom_panel_item(TTR("Shader"), shader_editor);
768 button->hide();
769 }
770
~ShaderEditorPlugin()771 ShaderEditorPlugin::~ShaderEditorPlugin() {
772 }
773