Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Merge shader and text editors

Simon Döhl65e5dfd4855d
diff --git a/editor/docks/editor_dock.cpp b/editor/docks/editor_dock.cpp
index f627e1ad28f..9a9a1198500 100644
--- a/editor/docks/editor_dock.cpp
+++ b/editor/docks/editor_dock.cpp
@@ -162,8 +162,16 @@ void EditorDock::make_visible() {
EditorDockManager::get_singleton()->open_dock(this, true);
}
-void EditorDock::make_floating() {
+void EditorDock::make_floating(int p_screen) {
EditorDockManager::get_singleton()->make_dock_floating(this);
+
+ if (p_screen < 0) {
+ return;
+ }
+ Window *current_window = get_window();
+ if (current_window) {
+ current_window->set_current_screen(p_screen);
+ }
}
void EditorDock::close() {
diff --git a/editor/docks/editor_dock.h b/editor/docks/editor_dock.h
index 753f2dcc398..f37104d1e12 100644
--- a/editor/docks/editor_dock.h
+++ b/editor/docks/editor_dock.h
@@ -114,7 +114,7 @@ protected:
public:
void open();
void make_visible();
- void make_floating();
+ void make_floating(int p_screen = -1);
void close();
void set_title(const String &p_title);
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp
index fbdfb0cedb0..a7f4a2c66fa 100644
--- a/editor/editor_node.cpp
+++ b/editor/editor_node.cpp
@@ -145,7 +145,7 @@
#include "editor/settings/editor_settings_dialog.h"
#include "editor/settings/project_settings_editor.h"
#include "editor/shader/editor_native_shader_source_visualizer.h"
-#include "editor/shader/text_shader_editor.h"
+#include "editor/shader/shader_text_editor.h"
#include "editor/themes/editor_color_map.h"
#include "editor/themes/editor_scale.h"
#include "editor/themes/editor_theme_manager.h"
@@ -9463,7 +9463,7 @@ EditorNode::EditorNode() {
ScriptTextEditor::register_editor(); // Register one for text scripts.
TextEditor::register_editor();
- TextShaderEditor::register_editor();
+ ShaderTextEditor::register_editor();
if (AssetLibraryEditorPlugin::is_available()) {
add_editor_plugin(memnew(AssetLibraryEditorPlugin));
diff --git a/editor/gui/code_editor.cpp b/editor/gui/code_editor.cpp
index 906520a0595..12ad4febc8b 100644
--- a/editor/gui/code_editor.cpp
+++ b/editor/gui/code_editor.cpp
@@ -1030,7 +1030,6 @@ void CodeTextEditor::_code_complete_timer_timeout() {
void CodeTextEditor::_complete_request() {
List<ScriptLanguage::CodeCompletionOption> entries;
String ctext = text_editor->get_text_for_code_completion();
- _code_complete_script(ctext, &entries);
bool forced = false;
if (code_complete_func) {
code_complete_func(code_complete_ud, ctext, &entries, forced);
@@ -1624,12 +1623,6 @@ Point2i CodeTextEditor::get_pos_for_display(Point2i p_internal_position) const {
return Point2(p_internal_position.x + 1, corrected_column + 1);
}
-void CodeTextEditor::goto_error() {
- if (!error->get_text().is_empty()) {
- goto_line_centered(error_line, error_column);
- }
-}
-
void CodeTextEditor::_update_text_editor_theme() {
emit_signal(SNAME("load_theme_settings"));
@@ -1716,7 +1709,6 @@ void CodeTextEditor::_update_font_ligatures() {
}
void CodeTextEditor::_text_changed_idle_timeout() {
- _validate_script();
emit_signal(SNAME("validate_script"));
}
@@ -1758,7 +1750,9 @@ void CodeTextEditor::_toggle_files_pressed() {
void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
- goto_error();
+ if (!error->get_text().is_empty()) {
+ goto_line_centered(error_line, error_column);
+ }
}
}
diff --git a/editor/gui/code_editor.h b/editor/gui/code_editor.h
index c147764ad86..1dfb95d7567 100644
--- a/editor/gui/code_editor.h
+++ b/editor/gui/code_editor.h
@@ -226,10 +226,6 @@ class CodeTextEditor : public VBoxContainer {
void _toggle_files_pressed();
protected:
- virtual void _load_theme_settings() {}
- virtual void _validate_script() {}
- virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {}
-
void _text_changed_idle_timeout();
void _code_complete_timer_timeout();
void _text_changed();
@@ -294,8 +290,6 @@ public:
FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
void set_find_replace_bar(FindReplaceBar *p_bar);
void remove_find_replace_bar();
- virtual void apply_code() {}
- virtual void goto_error();
void toggle_bookmark();
void goto_next_bookmark();
diff --git a/editor/script/find_in_files.cpp b/editor/script/find_in_files.cpp
index b7fa656cdd4..d13a39ba791 100644
--- a/editor/script/find_in_files.cpp
+++ b/editor/script/find_in_files.cpp
@@ -41,7 +41,6 @@
#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/shader/shader_editor_plugin.h"
-#include "editor/shader/text_shader_editor.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
@@ -293,18 +292,19 @@ CodeTextEditor *get_code_edit(const String &p_fpath) {
ShaderEditorPlugin *shader_editor_plugin = ShaderEditorPlugin::get_singleton();
ScriptEditorPlugin *script_editor_plugin = ScriptEditorPlugin::get_singleton();
+ ScriptEditor *script_editor = nullptr;
if (shader_editor_plugin && shader_editor_plugin->handles(res.ptr())) {
- TextShaderEditor *tse = Object::cast_to<TextShaderEditor>(shader_editor_plugin->get_shader_editor(res));
- if (tse) {
- return tse->get_code_editor();
- }
+ script_editor = ScriptEditor::get_bottom_script_editor();
} else if (script_editor_plugin && script_editor_plugin->handles(res.ptr())) {
- ScriptEditor *script_editor = ScriptEditor::get_singleton();
- TextEditorBase *teb = Object::cast_to<TextEditorBase>(script_editor->get_script_editor(res));
+ script_editor = ScriptEditor::get_singleton();
+ }
+ if (script_editor) {
+ TextEditorBase *teb = Object::cast_to<TextEditorBase>(script_editor->get_resource_editor(res));
if (teb) {
return teb->get_code_editor();
}
}
+
return nullptr;
}
diff --git a/editor/script/script_editor_base.cpp b/editor/script/script_editor_base.cpp
index 41b41437faf..d4158eaf9d8 100644
--- a/editor/script/script_editor_base.cpp
+++ b/editor/script/script_editor_base.cpp
@@ -41,6 +41,8 @@
#include "scene/gui/menu_button.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
+#include "scene/resources/shader.h"
+#include "scene/resources/shader_include.h"
#include "servers/display/display_server.h"
void ScriptEditorBase::_bind_methods() {
@@ -101,7 +103,10 @@ void ScriptEditorBase::tag_saved_version() {
//// TextEditorBase
TextEditorBase *TextEditorBase::EditMenus::_get_active_editor() {
- return Object::cast_to<TextEditorBase>(ScriptEditor::get_singleton()->get_current_editor());
+ if (se) {
+ return Object::cast_to<TextEditorBase>(se->get_current_editor());
+ }
+ return nullptr;
}
void TextEditorBase::EditMenus::_edit_option(int p_op) {
@@ -181,12 +186,14 @@ void TextEditorBase::EditMenus::_bookmark_item_pressed(int p_idx) {
script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));
} else {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
- script_text_editor->code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
+ script_text_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
ScriptEditorNavigationMarker::get_singleton()->locate_end();
}
}
-TextEditorBase::EditMenus::EditMenus() {
+TextEditorBase::EditMenus::EditMenus(ScriptEditor *p_se) {
+ se = p_se;
+
edit_menu = memnew(MenuButton);
edit_menu->set_flat(false);
edit_menu->set_theme_type_variation("FlatMenuButton");
@@ -390,30 +397,61 @@ bool TextEditorBase::_edit_option(int p_op) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
+ switch (p_op) {
+ case EDIT_UNDO:
+ case EDIT_REDO:
+ case EDIT_CUT:
+ case EDIT_COPY:
+ case EDIT_PASTE:
+ case EDIT_SELECT_ALL:
+ case EDIT_TRIM_TRAILING_WHITESAPCE:
+ case EDIT_TRIM_FINAL_NEWLINES:
+ case EDIT_CONVERT_INDENT_TO_SPACES:
+ case EDIT_CONVERT_INDENT_TO_TABS:
+ case EDIT_MOVE_LINE_UP:
+ case EDIT_MOVE_LINE_DOWN:
+ case EDIT_INDENT:
+ case EDIT_UNINDENT:
+ case EDIT_DELETE_LINE:
+ case EDIT_JOIN_LINES:
+ case EDIT_DUPLICATE_SELECTION:
+ case EDIT_DUPLICATE_LINES:
+ case EDIT_TO_UPPERCASE:
+ case EDIT_TO_LOWERCASE:
+ case EDIT_CAPITALIZE:
+ case EDIT_TOGGLE_FOLD_LINE:
+ case EDIT_FOLD_ALL_LINES:
+ case EDIT_TOGGLE_WORD_WRAP:
+ case EDIT_UNFOLD_ALL_LINES:
+ case EDIT_EMOJI_AND_SYMBOL:
+ case SEARCH_FIND_NEXT:
+ case SEARCH_FIND_PREV:
+ case BOOKMARK_TOGGLE:
+ case BOOKMARK_GOTO_NEXT:
+ case BOOKMARK_GOTO_PREV:
+ case BOOKMARK_REMOVE_ALL: {
+ callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
+ }
+ }
+
switch (p_op) {
case EDIT_UNDO: {
tx->undo();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_REDO: {
tx->redo();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_CUT: {
tx->cut();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_COPY: {
tx->copy();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_PASTE: {
tx->paste();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_SELECT_ALL: {
tx->select_all();
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
} break;
case EDIT_MOVE_LINE_UP: {
tx->move_lines_up();
@@ -480,14 +518,11 @@ bool TextEditorBase::_edit_option(int p_op) {
} break;
case SEARCH_IN_FILES: {
String selected_text = tx->get_selected_text();
-
- // Yep, because it doesn't make sense to instance this dialog for every single script open...
- // So this will be delegated to the ScriptEditor.
- emit_signal(SNAME("search_in_files_requested"), selected_text);
+ ScriptEditor::get_singleton()->open_find_in_files_dialog(selected_text);
} break;
case REPLACE_IN_FILES: {
String selected_text = tx->get_selected_text();
- emit_signal(SNAME("replace_in_files_requested"), selected_text);
+ ScriptEditor::get_singleton()->open_find_in_files_dialog(selected_text, true);
} break;
case SEARCH_GOTO_LINE: {
goto_line_popup->popup_find_line(code_editor);
@@ -622,6 +657,16 @@ void TextEditorBase::reload_text() {
te->set_text(script->get_source_code());
}
+ Ref<ShaderInclude> shader_inc = edited_res;
+ if (shader_inc.is_valid()) {
+ te->set_text(shader_inc->get_code());
+ }
+
+ Ref<Shader> shader = edited_res;
+ if (shader.is_valid()) {
+ te->set_text(shader->get_code());
+ }
+
te->set_caret_line(row);
te->set_caret_column(column);
te->set_h_scroll(h);
@@ -653,7 +698,7 @@ void TextEditorBase::set_tooltip_request_func(const Callable &p_toolip_callback)
code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
}
-void TextEditorBase::set_edit_state(const Variant &p_state) {
+void TextEditorBase::set_edit_state(const Variant &p_state, bool p_grab_focus) {
code_editor->set_edit_state(p_state);
Dictionary state = p_state;
@@ -669,7 +714,9 @@ void TextEditorBase::set_edit_state(const Variant &p_state) {
}
}
- ensure_focus();
+ if (p_grab_focus) {
+ ensure_focus();
+ }
}
TextEditorBase::TextEditorBase() {
@@ -709,6 +756,75 @@ TextEditorBase::~TextEditorBase() {
//// CodeEditorBase
+void CodeEditorBase::EditMenusCEB::_update_breakpoint_list() {
+ breakpoints_menu->clear();
+ breakpoints_menu->reset_size();
+
+ TextEditorBase *text_editor_base = _get_active_editor();
+ if (text_editor_base == nullptr) {
+ return;
+ }
+
+ PackedInt32Array breakpoint_list = text_editor_base->get_code_editor()->get_text_editor()->get_breakpointed_lines();
+ if (breakpoint_list.is_empty()) {
+ return;
+ }
+
+ breakpoints_menu->add_separator();
+
+ for (int i = 0; i < breakpoint_list.size(); i++) {
+ // Strip edges to remove spaces or tabs.
+ // Also replace any tabs by spaces, since we can't print tabs in the menu.
+ String line = text_editor_base->get_code_editor()->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
+
+ // Limit the size of the line if too big.
+ if (line.length() > 50) {
+ line = line.substr(0, 50);
+ }
+
+ breakpoints_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`");
+ breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]);
+ }
+}
+
+void CodeEditorBase::EditMenusCEB::_breakpoint_item_pressed(int p_idx) {
+ TextEditorBase *text_editor_base = _get_active_editor();
+ ERR_FAIL_NULL(text_editor_base);
+ if (p_idx < 4) { // Any item before the separator.
+ _edit_option(breakpoints_menu->get_item_id(p_idx));
+ } else {
+ text_editor_base->goto_line_centered(breakpoints_menu->get_item_metadata(p_idx));
+ }
+}
+
+void CodeEditorBase::EditMenusCEB::_notification(int p_what) {
+ switch (p_what) {
+ case NOTIFICATION_POSTINITIALIZE: {
+ // Update immediately for shortcuts.
+ _update_breakpoint_list();
+ }
+ }
+}
+
+CodeEditorBase::EditMenusCEB::EditMenusCEB(ScriptEditor *p_se, String p_breakpoint_menu_name) : EditMenus(p_se) {
+ edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
+ _popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);
+ edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
+
+ breakpoints_menu = memnew(PopupMenu);
+ goto_menu->get_popup()->add_submenu_node_item(TTRC(p_breakpoint_menu_name), breakpoints_menu);
+ breakpoints_menu->connect("about_to_popup", callable_mp(this, &EditMenusCEB::_update_breakpoint_list));
+ breakpoints_menu->connect("index_pressed", callable_mp(this, &EditMenusCEB::_breakpoint_item_pressed));
+}
+
+void CodeEditorBase::set_breakpoint(int p_line, bool p_enabled) {
+ code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
+}
+
+void CodeEditorBase::_show_warnings_panel(bool p_show) {
+ warnings_panel->set_visible(p_show);
+}
+
bool CodeEditorBase::_warning_clicked(const Variant &p_line) {
if (p_line.get_type() == Variant::INT) {
goto_line_centered(p_line.operator int64_t());
@@ -717,13 +833,123 @@ bool CodeEditorBase::_warning_clicked(const Variant &p_line) {
return false;
}
-CodeEditorBase::EditMenusCEB::EditMenusCEB() {
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
- _popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);
- edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
+void CodeEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {
+ TextEditorBase::_make_context_menu(p_selection, p_foldable, p_position, false);
+ context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
+ _popup_move_item(EDIT_UNINDENT, context_menu);
+
+ if (p_show) {
+ _show_context_menu(p_position);
+ }
+}
+
+void CodeEditorBase::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
+ CodeEditorBase *ste = (CodeEditorBase *)p_ud;
+ ste->_code_complete_script(p_code, r_options, r_force);
+}
+
+bool CodeEditorBase::_edit_option(int p_option) {
+ CodeEdit *tx = code_editor->get_text_editor();
+ tx->apply_ime();
+
+ switch (p_option) {
+ case DEBUG_TOGGLE_BREAKPOINT:
+ case DEBUG_REMOVE_ALL_BREAKPOINTS:
+ case DEBUG_GOTO_NEXT_BREAKPOINT:
+ case DEBUG_GOTO_PREV_BREAKPOINT: {
+ callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
+ }
+ }
+
+ switch (p_option) {
+ case DEBUG_TOGGLE_BREAKPOINT: {
+ Vector<int> sorted_carets = tx->get_sorted_carets();
+ int last_line = -1;
+
+ for (const int &c : sorted_carets) {
+ int from = tx->get_selection_from_line(c);
+ from += from == last_line ? 1 : 0;
+
+ int to = tx->get_selection_to_line(c);
+ if (to < from) {
+ continue;
+ }
+
+ // Check first if there's any lines with breakpoints in the selection.
+ bool selection_has_breakpoints = false;
+ for (int line = from; line <= to; line++) {
+ if (tx->is_line_breakpointed(line)) {
+ selection_has_breakpoints = true;
+ break;
+ }
+ }
+
+ // Set breakpoint on caret or remove all bookmarks from the selection.
+ if (!selection_has_breakpoints) {
+ if (tx->get_caret_line(c) != last_line) {
+ tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
+ }
+ } else {
+ for (int line = from; line <= to; line++) {
+ tx->set_line_as_breakpoint(line, false);
+ }
+ }
+
+ last_line = to;
+ }
+ } break;
+ case DEBUG_GOTO_NEXT_BREAKPOINT: {
+ PackedInt32Array bpoints = tx->get_breakpointed_lines();
+ if (bpoints.is_empty()) {
+ return true;
+ }
+
+ int current_line = tx->get_caret_line();
+ int bpoint_idx = 0;
+ if (current_line < (int)bpoints[bpoints.size() - 1]) {
+ while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
+ bpoint_idx++;
+ }
+ }
+ ScriptEditorNavigationMarker::get_singleton()->locate_begin();
+ goto_line_centered(bpoints[bpoint_idx]);
+ ScriptEditorNavigationMarker::get_singleton()->locate_end();
+ } break;
+ case DEBUG_GOTO_PREV_BREAKPOINT: {
+ PackedInt32Array bpoints = tx->get_breakpointed_lines();
+ if (bpoints.is_empty()) {
+ return true;
+ }
+
+ int current_line = tx->get_caret_line();
+ int bpoint_idx = bpoints.size() - 1;
+ if (current_line > (int)bpoints[0]) {
+ while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
+ bpoint_idx--;
+ }
+ }
+ ScriptEditorNavigationMarker::get_singleton()->locate_begin();
+ goto_line_centered(bpoints[bpoint_idx]);
+ ScriptEditorNavigationMarker::get_singleton()->locate_end();
+ } break;
+ case DEBUG_REMOVE_ALL_BREAKPOINTS: {
+ for (int line : tx->get_breakpointed_lines()) {
+ tx->set_line_as_breakpoint(line, false);
+ }
+ } break;
+ default: {
+ return TextEditorBase::_edit_option(p_option);
+ }
+ }
+ return true;
}
CodeEditorBase::CodeEditorBase() {
+ code_editor->set_code_complete_func(_code_complete_scripts, this);
+ code_editor->connect("show_warnings_panel", callable_mp(this, &CodeEditorBase::_show_warnings_panel));
+ code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
+ code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &CodeEditorBase::_breakpoint_toggled));
+
warnings_panel = memnew(RichTextLabel);
warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -738,6 +964,4 @@ CodeEditorBase::CodeEditorBase() {
add_child(editor_box);
editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
- editor_box->add_child(code_editor);
- editor_box->add_child(warnings_panel);
}
diff --git a/editor/script/script_editor_base.h b/editor/script/script_editor_base.h
index f0dc6ec846b..0bbee82336b 100644
--- a/editor/script/script_editor_base.h
+++ b/editor/script/script_editor_base.h
@@ -36,6 +36,7 @@
class EditorSyntaxHighlighter;
class MenuButton;
class VSplitContainer;
+class ScriptEditor;
class ScriptEditorBase : public VBoxContainer {
GDCLASS(ScriptEditorBase, VBoxContainer);
@@ -46,12 +47,23 @@ protected:
static void _bind_methods();
public:
+ class EditMenusBase : public HBoxContainer {
+ GDCLASS(EditMenusBase, HBoxContainer);
+
+ protected:
+ ScriptEditor *se = nullptr;
+
+ public:
+ virtual bool handles(ScriptEditorBase *p_seb) = 0;
+ };
+
struct EditedFileData {
String path;
uint64_t last_modified_time = -1;
} edited_file_data;
virtual String get_name();
+ virtual String get_doc_url_path() { return "/"; }
virtual Ref<Texture2D> get_theme_icon();
virtual void set_toggle_list_control(Control *p_toggle_list_control) = 0;
@@ -59,6 +71,8 @@ public:
virtual bool show_members_overview() { return false; }
+ virtual void ensure_focus() = 0;
+
virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
virtual Ref<Resource> get_edited_resource() const { return edited_res; }
@@ -76,8 +90,6 @@ typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resou
class TextEditorBase : public ScriptEditorBase {
GDCLASS(TextEditorBase, ScriptEditorBase);
- void _post_init();
-
protected:
enum {
EDIT_UNDO,
@@ -95,6 +107,7 @@ protected:
EDIT_INDENT,
EDIT_UNINDENT,
EDIT_DELETE_LINE,
+ EDIT_JOIN_LINES,
EDIT_DUPLICATE_SELECTION,
EDIT_DUPLICATE_LINES,
EDIT_TO_UPPERCASE,
@@ -120,13 +133,11 @@ protected:
BOOKMARK_GOTO_PREV,
BOOKMARK_REMOVE_ALL,
- EDIT_JOIN_LINES,
-
BASE_ENUM_COUNT,
};
- class EditMenus : public HBoxContainer {
- GDCLASS(EditMenus, HBoxContainer);
+ class EditMenus : public EditMenusBase {
+ GDCLASS(EditMenus, EditMenusBase);
protected:
MenuButton *edit_menu = nullptr;
@@ -149,7 +160,7 @@ protected:
void _bookmark_item_pressed(int p_idx);
public:
- EditMenus();
+ EditMenus(ScriptEditor *p_se);
};
static void _popup_move_item(int p_target_id, PopupMenu *r_popup, bool p_move_after = true, int p_idx = -1) {
@@ -159,7 +170,7 @@ protected:
}
}
- static inline EditMenus *edit_menus = nullptr;
+ bool validation_success = true;
bool editor_enabled = false;
CodeTextEditor *code_editor = nullptr;
@@ -169,9 +180,8 @@ protected:
LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
PopupMenu *context_menu = nullptr;
- MenuButton *search_menu = nullptr;
- void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true);
+ virtual void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true);
void _show_context_menu(const Vector2 &p_position);
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
@@ -198,14 +208,14 @@ public:
virtual void reload_text();
virtual void enable_editor();
- virtual Control *get_edit_menu() = 0;
+ virtual EditMenusBase *create_edit_menu(ScriptEditor *p_se) = 0;
virtual Control *get_base_editor() const override { return code_editor->get_text_editor(); }
virtual CodeTextEditor *get_code_editor() const { return code_editor; }
virtual void set_tooltip_request_func(const Callable &p_toolip_callback);
- virtual void ensure_focus() { code_editor->get_text_editor()->grab_focus(); }
+ virtual void ensure_focus() override { code_editor->get_text_editor()->grab_focus(); }
virtual void convert_indent() { code_editor->get_text_editor()->convert_indent(); }
virtual void trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); }
@@ -222,13 +232,14 @@ public:
virtual void clear_executing_line() { code_editor->clear_executing_line(); }
virtual Variant get_edit_state() { return code_editor->get_edit_state(); }
- virtual void set_edit_state(const Variant &p_state);
+ virtual void set_edit_state(const Variant &p_state, bool p_grab_focus = true);
virtual Variant get_navigation_state() { return code_editor->get_navigation_state(); }
virtual void update_settings() { code_editor->update_editor_settings(); }
virtual void set_find_replace_bar(FindReplaceBar *p_bar) { code_editor->set_find_replace_bar(p_bar); }
virtual void validate_script() override { code_editor->validate_script(); }
+ bool get_validation_success() { return validation_success; }
virtual void set_toggle_list_control(Control *p_toggle_list_control) override {
code_editor->set_toggle_list_control(p_toggle_list_control);
@@ -247,29 +258,51 @@ protected:
EDIT_COMPLETE = BASE_ENUM_COUNT,
EDIT_TOGGLE_COMMENT,
+ DEBUG_TOGGLE_BREAKPOINT,
+ DEBUG_REMOVE_ALL_BREAKPOINTS,
+ DEBUG_GOTO_NEXT_BREAKPOINT,
+ DEBUG_GOTO_PREV_BREAKPOINT,
+
CODE_ENUM_COUNT,
};
class EditMenusCEB : public EditMenus {
GDCLASS(EditMenusCEB, EditMenus);
+ protected:
+ PopupMenu *breakpoints_menu = nullptr;
+
+ virtual void _update_breakpoint_list();
+ void _breakpoint_item_pressed(int p_idx);
+
+ void _notification(int p_what);
+
public:
- EditMenusCEB();
+ EditMenusCEB(ScriptEditor *p_se, String p_breakpoint_menu_name);
};
VSplitContainer *editor_box = nullptr;
RichTextLabel *warnings_panel = nullptr;
+ static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) = 0;
+
+ void _show_warnings_panel(bool p_show);
virtual bool _warning_clicked(const Variant &p_line);
+ virtual void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true) override;
+
+ virtual bool _edit_option(int p_op) override;
+
+ virtual void _breakpoint_toggled(int p_row) = 0;
+
public:
virtual bool show_members_overview() override { return true; }
virtual Vector<String> get_functions() { return Vector<String>(); }
virtual PackedInt32Array get_breakpoints() { return PackedInt32Array(); }
- virtual void set_breakpoint(int p_line, bool p_enabled) {}
+ virtual void set_breakpoint(int p_line, bool p_enabled);
virtual void clear_breakpoints() {}
CodeEditorBase();
diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp
index e0299df04ee..d3bc40769ca 100644
--- a/editor/script/script_editor_plugin.cpp
+++ b/editor/script/script_editor_plugin.cpp
@@ -65,12 +65,14 @@
#include "editor/run/editor_run_bar.h"
#include "editor/scene/editor_scene_tabs.h"
#include "editor/script/find_in_files.h"
+#include "editor/script/script_editor_base.h"
#include "editor/script/script_editor_navigation_marker.h"
#include "editor/script/script_text_editor.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/script/text_editor.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/settings/editor_settings.h"
+#include "editor/shader/shader_create_dialog.h"
#include "editor/shader/shader_editor_plugin.h"
#include "editor/shader/shader_text_editor.h"
#include "editor/themes/editor_scale.h"
@@ -180,7 +182,7 @@ void DocumentOutline::_notification(int p_what) {
}
void DocumentOutline::_item_list_selected(int p_idx) {
- Control *active_editor = ScriptEditor::get_singleton()->get_active_editor();
+ Control *active_editor = script_editor->get_active_editor();
int line = item_list->get_item_metadata(p_idx);
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(active_editor)) {
@@ -202,7 +204,7 @@ void DocumentOutline::update_editor_settings() {
}
void DocumentOutline::update_outline() {
- Control *active_editor = ScriptEditor::get_singleton()->get_active_editor();
+ Control *active_editor = script_editor->get_active_editor();
item_list->clear();
if (CodeEditorBase *ceb = Object::cast_to<CodeEditorBase>(active_editor)) {
@@ -244,7 +246,7 @@ void DocumentOutline::update_outline() {
}
void DocumentOutline::update_visibility() {
- Control *active_editor = ScriptEditor::get_singleton()->get_active_editor();
+ Control *active_editor = script_editor->get_active_editor();
ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(active_editor);
EditorHelp *eh = Object::cast_to<EditorHelp>(active_editor);
@@ -265,7 +267,8 @@ void DocumentOutline::update_visibility() {
}
}
-DocumentOutline::DocumentOutline() {
+DocumentOutline::DocumentOutline(ScriptEditor *p_script_editor) {
+ script_editor = p_script_editor;
buttons_hbox = memnew(HBoxContainer);
filter = memnew(FilterLineEdit);
@@ -298,10 +301,6 @@ DocumentOutline::DocumentOutline() {
add_child(item_list);
}
-/////////////////////////////////
-
-ScriptEditor *ScriptEditor::script_editor = nullptr;
-
/*** SCRIPT EDITOR ******/
String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *p_se) {
@@ -322,8 +321,8 @@ String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *p_se) {
return debug_value;
}
-void ScriptEditor::_script_created(Ref<Script> p_script) {
- EditorNode::get_singleton()->push_item(p_script.operator->());
+void ScriptEditor::_resource_created(Ref<Resource> p_res) {
+ EditorNode::get_singleton()->push_item(p_res.ptr());
}
void ScriptEditor::_goto_script_line2(int p_line) {
@@ -428,12 +427,7 @@ Array ScriptEditor::_get_cached_breakpoints_for_script(const String &p_path) con
}
ScriptEditorBase *ScriptEditor::_get_current_editor() const {
- int selected = tab_container->get_current_tab();
- if (selected < 0 || selected >= tab_container->get_tab_count()) {
- return nullptr;
- }
-
- return Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected));
+ return Object::cast_to<ScriptEditorBase>(tab_container->get_current_tab_control());
}
void ScriptEditor::_update_history_arrows() {
@@ -575,12 +569,12 @@ void ScriptEditor::_go_to_tab(int p_idx, bool p_save_history) {
c = tab_container->get_current_tab_control();
if (ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(c)) {
- TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb);
- if (teb && is_visible_in_tree()) {
- teb->ensure_focus();
+ if (is_visible_in_tree() && !grab_focus_block) {
+ seb->ensure_focus();
}
- if (p_save_history) {
+ TextEditorBase *teb = Object::cast_to<TextEditorBase>(c);
+ if (p_save_history && teb) {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
teb->trigger_history_save_on_navigate();
ScriptEditorNavigationMarker::get_singleton()->locate_end();
@@ -623,7 +617,7 @@ void ScriptEditor::_add_recent_script(const String &p_path) {
return;
}
- Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
+ Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", config_section, Array());
if (rc.has(p_path)) {
rc.erase(p_path);
}
@@ -632,12 +626,12 @@ void ScriptEditor::_add_recent_script(const String &p_path) {
rc.resize(10);
}
- EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
+ EditorSettings::get_singleton()->set_project_metadata("recent_files", config_section, rc);
_update_recent_scripts();
}
void ScriptEditor::_update_recent_scripts() {
- Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
+ Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", config_section, Array());
recent_scripts->clear();
String path;
@@ -657,12 +651,12 @@ void ScriptEditor::_update_recent_scripts() {
void ScriptEditor::_open_recent_script(int p_idx) {
// clear button
if (p_idx == recent_scripts->get_item_count() - 1) {
- EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", Array());
+ EditorSettings::get_singleton()->set_project_metadata("recent_files", config_section, Array());
callable_mp(this, &ScriptEditor::_update_recent_scripts).call_deferred();
return;
}
- Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
+ Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", config_section, Array());
ERR_FAIL_INDEX(p_idx, rc.size());
String path = rc[p_idx];
@@ -699,7 +693,7 @@ void ScriptEditor::_open_recent_script(int p_idx) {
}
rc.remove_at(p_idx);
- EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
+ EditorSettings::get_singleton()->set_project_metadata("recent_files", config_section, rc);
_update_recent_scripts();
_show_error_dialog(path);
}
@@ -1021,7 +1015,7 @@ bool ScriptEditor::_test_script_times_on_disk(Ref<Resource> p_for_script) {
if (need_reload) {
if (!need_ask) {
- script_editor->reload_scripts();
+ reload_scripts();
need_reload = false;
} else {
callable_mp((Window *)disk_changed, &Window::popup_centered_ratio).call_deferred(0.3);
@@ -1190,14 +1184,16 @@ bool ScriptEditor::is_files_panel_toggled() {
List<String> ScriptEditor::_get_recognized_extensions() {
List<String> extensions;
- for (const String type : { "Script", "JSON" }) {
+ for (const String &type : handled_resource_types) {
ResourceLoader::get_recognized_extensions_for_type(type, &extensions);
}
return extensions;
}
void ScriptEditor::_menu_option(int p_option) {
- ScriptEditorBase *current = _get_current_editor();
+ Node *current_editor = tab_container->get_current_tab_control();
+ ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(current_editor);
+ EditorHelp *eh = Object::cast_to<EditorHelp>(current_editor);
switch (p_option) {
case FILE_MENU_NEW_SCRIPT: {
script_create_dialog->config("Node", "new_script", false, false);
@@ -1215,6 +1211,16 @@ void ScriptEditor::_menu_option(int p_option) {
file_dialog->set_title(TTRC("New Text File..."));
file_dialog->popup_file_dialog();
} break;
+ case FILE_MENU_NEW_SHADER: {
+ String base_path = FileSystemDock::get_singleton()->get_current_path().get_base_dir();
+ shader_create_dialog->config(base_path.path_join("new_shader"), false, false, "Shader");
+ shader_create_dialog->popup_centered();
+ } break;
+ case FILE_MENU_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, "ShaderInclude");
+ shader_create_dialog->popup_centered();
+ } break;
case FILE_MENU_OPEN: {
file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
@@ -1235,6 +1241,12 @@ void ScriptEditor::_menu_option(int p_option) {
file_dialog->popup_file_dialog();
return;
} break;
+ case FILE_MENU_OPEN_SHADER: {
+ InspectorDock::get_singleton()->open_resource("Shader");
+ } break;
+ case FILE_MENU_OPEN_INCLUDE: {
+ InspectorDock::get_singleton()->open_resource("ShaderInclude");
+ } break;
case FILE_MENU_REOPEN_CLOSED: {
if (previous_scripts.is_empty()) {
return;
@@ -1280,6 +1292,19 @@ void ScriptEditor::_menu_option(int p_option) {
save_all_scripts();
} break;
+ case FILE_MENU_INSPECT: {
+ if (seb && seb->get_edited_resource().is_valid()) {
+ EditorNode::get_singleton()->push_item(seb->get_edited_resource().ptr());
+ }
+ } break;
+ case FILE_MENU_INSPECT_NATIVE_SHADER_CODE: {
+ if (seb) {
+ Ref<Shader> shader = seb->get_edited_resource();
+ if (shader.is_valid()) {
+ shader->inspect_native_shader_code();
+ }
+ }
+ } break;
case SEARCH_IN_FILES: {
open_find_in_files_dialog("");
} break;
@@ -1287,12 +1312,9 @@ void ScriptEditor::_menu_option(int p_option) {
open_find_in_files_dialog("", true);
} break;
case SEARCH_HELP: {
- help_search_dialog->popup_dialog();
+ script_editor->help_search_dialog->popup_dialog();
} break;
case SEARCH_WEBSITE: {
- Control *tab = tab_container->get_current_tab_control();
-
- EditorHelp *eh = Object::cast_to<EditorHelp>(tab);
bool native_class_doc = false;
if (eh) {
const HashMap<String, DocData::ClassDoc>::ConstIterator E = EditorHelp::get_doc_data()->class_list.find(eh->get_class());
@@ -1302,6 +1324,9 @@ void ScriptEditor::_menu_option(int p_option) {
String name = eh->get_class().to_lower();
String doc_url = vformat(GODOT_VERSION_DOCS_URL "/classes/class_%s.html", name);
OS::get_singleton()->shell_open(doc_url);
+ } else if (seb) {
+ String doc_url = vformat(GODOT_VERSION_DOCS_URL "%s", seb->get_doc_url_path());
+ OS::get_singleton()->shell_open(doc_url);
} else {
OS::get_singleton()->shell_open(GODOT_VERSION_DOCS_URL "/");
}
@@ -1318,35 +1343,65 @@ void ScriptEditor::_menu_option(int p_option) {
} break;
case FILE_MENU_TOGGLE_FILES_PANEL: {
toggle_files_panel();
- if (current) {
- current->update_toggle_files_button();
+ if (seb) {
+ seb->update_toggle_files_button();
+ } else if (eh) {
+ eh->update_toggle_files_button();
+ }
+ } break;
+ case FILE_MENU_CLOSE: {
+ if (seb && seb->is_unsaved()) {
+ _ask_close_current_unsaved_tab(seb);
} else {
- Control *tab = tab_container->get_current_tab_control();
- if (EditorHelp *editor_help = Object::cast_to<EditorHelp>(tab)) {
- editor_help->update_toggle_files_button();
- }
+ _close_current_tab(false);
}
- }
+ } break;
+ case FILE_MENU_CLOSE_DOCS: {
+ _close_docs_tab();
+ } break;
+ case FILE_MENU_CLOSE_OTHER_TABS: {
+ _close_other_tabs();
+ } break;
+ case FILE_MENU_CLOSE_TABS_BELOW: {
+ _close_tabs_below();
+ } break;
+ case FILE_MENU_CLOSE_ALL: {
+ _close_all_tabs();
+ } break;
+ case FILE_MENU_MOVE_UP: {
+ if (tab_container->get_current_tab() > 0) {
+ tab_container->move_child(current_editor, tab_container->get_current_tab() - 1);
+ tab_container->set_current_tab(tab_container->get_current_tab());
+ _update_script_names();
+ }
+ } break;
+ case FILE_MENU_MOVE_DOWN: {
+ if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) {
+ tab_container->move_child(current_editor, tab_container->get_current_tab() + 1);
+ tab_container->set_current_tab(tab_container->get_current_tab());
+ _update_script_names();
+ }
+ } break;
}
if (p_option >= EditorContextMenuPlugin::BASE_ID) {
Ref<Resource> resource;
- if (current) {
- resource = current->get_edited_resource();
+ if (seb) {
+ resource = seb->get_edited_resource();
}
EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR, p_option, resource);
return;
}
- if (current) {
+ if (seb) {
switch (p_option) {
case FILE_MENU_SAVE: {
save_current_script();
} break;
case FILE_MENU_SAVE_AS: {
- _auto_format_text(current);
+ _auto_format_text(seb);
- Ref<Resource> resource = current->get_edited_resource();
+ Ref<Resource> resource = seb->get_edited_resource();
Ref<TextFile> text_file = resource;
Ref<Script> scr = resource;
@@ -1378,7 +1433,7 @@ void ScriptEditor::_menu_option(int p_option) {
} break;
case FILE_MENU_SOFT_RELOAD_TOOL: {
- Ref<Script> scr = current->get_edited_resource();
+ Ref<Script> scr = seb->get_edited_resource();
if (scr.is_null()) {
EditorNode::get_singleton()->show_warning(TTR("Can't obtain the script for reloading."));
break;
@@ -1392,25 +1447,16 @@ void ScriptEditor::_menu_option(int p_option) {
} break;
case FILE_MENU_RUN: {
- Ref<Script> scr = current->get_edited_resource();
+ Ref<Script> scr = seb->get_edited_resource();
if (scr.is_null()) {
EditorToaster::get_singleton()->popup_str(TTR("Cannot run the edited file because it's not a script."), EditorToaster::SEVERITY_WARNING);
break;
}
- current->apply_code();
+ seb->apply_code();
EditorNode::get_singleton()->run_editor_script(scr);
} break;
-
- case FILE_MENU_CLOSE: {
- ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(current);
- if (seb && seb->is_unsaved()) {
- _ask_close_current_unsaved_tab(seb);
- } else {
- _close_current_tab(false);
- }
- } break;
case FILE_MENU_COPY_PATH: {
_copy_script_path();
} break;
@@ -1418,86 +1464,29 @@ void ScriptEditor::_menu_option(int p_option) {
_copy_script_uid();
} break;
case FILE_MENU_SHOW_IN_FILE_SYSTEM: {
- const Ref<Resource> scr = current->get_edited_resource();
- String path = scr->get_path();
+ const Ref<Resource> res = seb->get_edited_resource();
+ String path = res->get_path();
if (!path.is_empty()) {
- if (scr->is_built_in()) {
+ if (res->is_built_in()) {
path = path.get_slice("::", 0); // Show the scene instead.
}
FileSystemDock::get_singleton()->navigate_to_path(path);
}
} break;
- case FILE_MENU_CLOSE_DOCS: {
- _close_docs_tab();
- } break;
- case FILE_MENU_CLOSE_OTHER_TABS: {
- _close_other_tabs();
- } break;
- case FILE_MENU_CLOSE_TABS_BELOW: {
- _close_tabs_below();
- } break;
- case FILE_MENU_CLOSE_ALL: {
- _close_all_tabs();
+ }
+ } else if (eh) {
+ switch (p_option) {
+ case HELP_SEARCH_FIND: {
+ eh->popup_search();
} break;
- case FILE_MENU_MOVE_UP: {
- if (tab_container->get_current_tab() > 0) {
- tab_container->move_child(current, tab_container->get_current_tab() - 1);
- tab_container->set_current_tab(tab_container->get_current_tab());
- _update_script_names();
- }
+ case HELP_SEARCH_FIND_NEXT: {
+ eh->search_again();
} break;
- case FILE_MENU_MOVE_DOWN: {
- if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) {
- tab_container->move_child(current, tab_container->get_current_tab() + 1);
- tab_container->set_current_tab(tab_container->get_current_tab());
- _update_script_names();
- }
+ case HELP_SEARCH_FIND_PREVIOUS: {
+ eh->search_again(true);
} break;
}
- } else {
- if (EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control())) {
- switch (p_option) {
- case HELP_SEARCH_FIND: {
- help->popup_search();
- } break;
- case HELP_SEARCH_FIND_NEXT: {
- help->search_again();
- } break;
- case HELP_SEARCH_FIND_PREVIOUS: {
- help->search_again(true);
- } break;
- case FILE_MENU_CLOSE: {
- _close_current_tab();
- } break;
- case FILE_MENU_CLOSE_DOCS: {
- _close_docs_tab();
- } break;
- case FILE_MENU_CLOSE_OTHER_TABS: {
- _close_other_tabs();
- } break;
- case FILE_MENU_CLOSE_TABS_BELOW: {
- _close_tabs_below();
- } break;
- case FILE_MENU_CLOSE_ALL: {
- _close_all_tabs();
- } break;
- case FILE_MENU_MOVE_UP: {
- if (tab_container->get_current_tab() > 0) {
- tab_container->move_child(help, tab_container->get_current_tab() - 1);
- tab_container->set_current_tab(tab_container->get_current_tab());
- _update_script_names();
- }
- } break;
- case FILE_MENU_MOVE_DOWN: {
- if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) {
- tab_container->move_child(help, tab_container->get_current_tab() + 1);
- tab_container->set_current_tab(tab_container->get_current_tab());
- _update_script_names();
- }
- } break;
- }
- }
}
}
@@ -1553,66 +1542,11 @@ bool ScriptEditor::_has_script_tab() const {
return false;
}
-void ScriptEditor::_prepare_file_menu() {
- PopupMenu *menu = file_menu->get_popup();
- ScriptEditorBase *editor = _get_current_editor();
- const Ref<Resource> res = editor ? editor->get_edited_resource() : Ref<Resource>();
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_REOPEN_CLOSED), previous_scripts.is_empty());
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE), res.is_null());
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE_AS), res.is_null());
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE_ALL), !_has_script_tab());
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SOFT_RELOAD_TOOL), res.is_null());
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_COPY_PATH), res.is_null() || res->get_path().is_empty());
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_COPY_UID), res.is_null() || ResourceLoader::get_resource_uid(res->get_path()) == ResourceUID::INVALID_ID);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SHOW_IN_FILE_SYSTEM), res.is_null());
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_HISTORY_PREV), history_pos <= 0);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_HISTORY_NEXT), history_pos >= history.size() - 1);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE), tab_container->get_tab_count() < 1);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_ALL), tab_container->get_tab_count() < 1);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_TABS_BELOW), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_DOCS), !_has_docs_tab());
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_RUN), res.is_null());
-}
-
-void ScriptEditor::_file_menu_closed() {
- PopupMenu *menu = file_menu->get_popup();
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_REOPEN_CLOSED), false);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE_AS), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SAVE_ALL), false);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SOFT_RELOAD_TOOL), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_COPY_PATH), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_SHOW_IN_FILE_SYSTEM), false);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_HISTORY_PREV), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_HISTORY_NEXT), false);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_ALL), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), false);
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_CLOSE_DOCS), false);
-
- menu->set_item_disabled(menu->get_item_index(FILE_MENU_RUN), false);
-}
-
-void ScriptEditor::_tab_changed(int p_which) {
- ensure_select_current();
-}
-
void ScriptEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_apply_editor_settings();
+ _floating_state_changed(get_window() != EditorNode::get_singleton()->get_window());
[[fallthrough]];
}
@@ -1620,7 +1554,7 @@ void ScriptEditor::_notification(int p_what) {
_update_online_doc();
if (!make_floating->is_disabled()) {
// Override default ScreenSelect tooltip if multi-window support is available.
- make_floating->set_tooltip_text(TTR("Make the script editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
+ make_floating->set_tooltip_text(TTR("Make the editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
}
[[fallthrough]];
}
@@ -1630,7 +1564,9 @@ void ScriptEditor::_notification(int p_what) {
_calculate_script_name_button_size();
- help_search->set_button_icon(get_editor_theme_icon(SNAME("HelpSearch")));
+ if (this == script_editor) {
+ help_search->set_button_icon(get_editor_theme_icon(SNAME("HelpSearch")));
+ }
site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
if (is_layout_rtl()) {
@@ -1664,10 +1600,12 @@ void ScriptEditor::_notification(int p_what) {
// Can't set own styles in NOTIFICATION_THEME_CHANGED, so for now this will do.
add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
- InspectorDock::get_singleton()->connect("request_help", callable_mp(this, &ScriptEditor::_help_class_open));
- EditorNode::get_singleton()->connect("request_help_search", callable_mp(this, &ScriptEditor::_help_search));
+ if (this == script_editor) {
+ InspectorDock::get_singleton()->connect("request_help", callable_mp(this, &ScriptEditor::_help_class_open));
+ EditorNode::get_singleton()->connect("request_help_search", callable_mp(this, &ScriptEditor::_help_search));
+ EditorNode::get_singleton()->connect("script_add_function_request", callable_mp(this, &ScriptEditor::_add_callback));
+ }
EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ScriptEditor::_close_builtin_scripts_from_scene));
- EditorNode::get_singleton()->connect("script_add_function_request", callable_mp(this, &ScriptEditor::_add_callback));
EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ScriptEditor::_res_saved_callback));
EditorNode::get_singleton()->connect("scene_saved", callable_mp(this, &ScriptEditor::_scene_saved_callback));
// Connect to scene_root child entered instead of scene_changed for new scenes.
@@ -1679,7 +1617,7 @@ void ScriptEditor::_notification(int p_what) {
script_split->connect("dragged", callable_mp(this, &ScriptEditor::_split_dragged));
list_split->connect("dragged", callable_mp(this, &ScriptEditor::_split_dragged));
- EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &ScriptEditor::_filesystem_changed));
+ EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &ScriptEditor::_update_script_names));
#ifdef ANDROID_ENABLED
set_process(true);
#endif
@@ -1739,12 +1677,8 @@ void ScriptEditor::_notification(int p_what) {
void ScriptEditor::_close_builtin_scripts_from_scene(const String &p_scene) {
for (int i = 0; i < tab_container->get_tab_count(); i++) {
if (ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i))) {
- Ref<Script> scr = seb->get_edited_resource();
- if (scr.is_null()) {
- continue;
- }
-
- if (scr->is_built_in() && scr->get_path().begins_with(p_scene)) { // Is an internal script and belongs to scene being closed.
+ Ref<Resource> res = seb->get_edited_resource();
+ if (res.is_valid() && res->is_built_in() && res->get_path().begins_with(p_scene)) { // Is an internal script and belongs to scene being closed.
_close_tab(i, false);
i--;
}
@@ -1828,9 +1762,9 @@ void ScriptEditor::ensure_select_current() {
if (tab_container->get_tab_count() && tab_container->get_current_tab() >= 0) {
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(_get_current_editor())) {
teb->enable_editor();
- if (teb && !grab_focus_block && is_visible_in_tree()) {
- teb->ensure_focus();
- }
+ }
+ if (_get_current_editor() && !grab_focus_block && is_visible_in_tree()) {
+ _get_current_editor()->ensure_focus();
}
}
_update_find_replace_bar();
@@ -1908,6 +1842,7 @@ struct _ScriptEditorItemData {
int category = 0;
Node *ref = nullptr;
String path;
+ bool validation_success = true;
bool operator<(const _ScriptEditorItemData &id) const {
if (category == id.category) {
@@ -1923,7 +1858,7 @@ struct _ScriptEditorItemData {
};
void ScriptEditor::_update_online_doc() {
- Node *current = tab_container->get_tab_control(tab_container->get_current_tab());
+ Node *current = tab_container->get_current_tab_control();
EditorHelp *eh = Object::cast_to<EditorHelp>(current);
bool native_class_doc = false;
@@ -2008,7 +1943,7 @@ void ScriptEditor::_update_script_names() {
sd.name = name;
sd.tooltip = saved ? path : TTR("Unsaved file.");
sd.index = i;
- sd.used = used.has(seb->get_edited_resource());
+ sd.used = used.has(scr);
sd.category = 0;
sd.ref = seb;
if (scr.is_valid()) {
@@ -2046,6 +1981,9 @@ void ScriptEditor::_update_script_names() {
sd.name = seb->get_name();
}
+ if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
+ sd.validation_success = teb->get_validation_success();
+ }
sedata.push_back(sd);
}
@@ -2149,13 +2087,16 @@ void ScriptEditor::_update_script_names() {
if (tab_container->get_current_tab() == sedata_filtered[i].index) {
script_list->select(index);
- if (ScriptEditorBase *seb = _get_current_editor()) {
- if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
+ if (Control *current = tab_container->get_current_tab_control()) {
+ if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(current)) {
teb->enable_editor();
}
_update_selected_editor_menu();
}
}
+ if (!sedata_filtered[i].validation_success) {
+ script_list->set_item_tag_icon(i, script_list->get_editor_theme_icon(SNAME("Error")));
+ }
}
bool has_active_tab = false;
@@ -2298,17 +2239,20 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
if ((scr.is_valid() && seb->get_edited_resource() == p_resource) || seb->get_edited_resource()->get_path() == p_resource->get_path()) {
if (should_open) {
- if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
+ TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb);
+ if (teb) {
teb->enable_editor();
+ }
- if (tab_container->get_current_tab() != i) {
- _go_to_tab(i);
- }
-
- if (is_visible_in_tree()) {
- teb->ensure_focus();
- }
+ if (tab_container->get_current_tab() != i) {
+ grab_focus_block = !p_grab_focus;
+ _go_to_tab(i, !teb);
+ grab_focus_block = false;
+ } else if (is_visible_in_tree() && p_grab_focus) {
+ seb->ensure_focus();
+ }
+ if (teb) {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
if (p_line >= 0) {
teb->goto_line_and_center_if_necessary(p_line, p_col);
@@ -2316,8 +2260,6 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
teb->trigger_history_save_on_navigate();
}
ScriptEditorNavigationMarker::get_singleton()->locate_end();
- } else if (tab_container->get_current_tab() != i) {
- _go_to_tab(i, true);
}
}
_update_script_names();
@@ -2340,15 +2282,26 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
seb->set_edited_resource(p_resource);
seb->set_toggle_list_control(get_left_list_split());
+ grab_focus_block = !p_grab_focus;
tab_container->add_child(seb);
+ grab_focus_block = false;
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
if (p_grab_focus) {
teb->enable_editor();
}
- Control *editor_edit_menu = teb->get_edit_menu();
- if (editor_edit_menu && !editor_edit_menu->get_parent()) {
+ ScriptEditorBase::EditMenusBase *editor_edit_menu = nullptr;
+ for (ScriptEditorBase::EditMenusBase *editor_menu : editor_menus) {
+ if (editor_menu->handles(teb)) {
+ editor_edit_menu = editor_menu;
+ break;
+ }
+ }
+ if (!editor_edit_menu) {
+ editor_edit_menu = teb->create_edit_menu(this);
+ }
+ if (!editor_edit_menu->get_parent()) {
editor_edit_menu->hide();
editor_menus.push_back(editor_edit_menu);
menu_hb->add_child(editor_edit_menu);
@@ -2362,10 +2315,10 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
}
}
- if (p_grab_focus) {
- _go_to_tab(tab_container->get_tab_count() - 1);
- _add_recent_script(p_resource->get_path());
- }
+ grab_focus_block = !p_grab_focus;
+ _go_to_tab(tab_container->get_tab_count() - 1);
+ grab_focus_block = false;
+ _add_recent_script(p_resource->get_path());
// If we delete a script within the filesystem, the original resource path
// is lost, so keep it as `edited_file_data` to figure out the exact tab to delete.
@@ -2373,7 +2326,9 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
seb->edited_file_data.last_modified_time = FileAccess::get_modified_time(p_resource->get_path());
seb->connect("name_changed", callable_mp(this, &ScriptEditor::_update_script_names));
- seb->connect("edited_script_changed", callable_mp(this, &ScriptEditor::_script_changed));
+ if (this == script_editor) {
+ seb->connect("edited_script_changed", callable_mp(this, &ScriptEditor::_script_changed));
+ }
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
// Syntax highlighting.
@@ -2402,6 +2357,14 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
if (languages.has(p_resource->get_path().get_extension())) {
teb->set_syntax_highlighter(highlighter);
highlighter_set = true;
+ continue;
+ }
+
+ // For built-in shaders, which do not use file extensions in their path.
+ ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(teb);
+ if (ste && languages.has("gdshader")) {
+ teb->set_syntax_highlighter(highlighter);
+ highlighter_set = true;
}
}
@@ -2417,7 +2380,7 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
teb->connect("go_to_method", callable_mp(this, &ScriptEditor::script_goto_method));
if (script_editor_cache->has_section(p_resource->get_path())) {
- teb->set_edit_state(script_editor_cache->get_value(p_resource->get_path(), "state"));
+ teb->set_edit_state(script_editor_cache->get_value(p_resource->get_path(), "state"), p_grab_focus);
if (ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(teb)) {
ste->store_previous_state();
}
@@ -2431,7 +2394,9 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
}
_sort_list_on_update = true;
+ grab_focus_block = !p_grab_focus;
_update_script_names();
+ grab_focus_block = false;
_save_layout();
//test for modification, maybe the script was not edited but was loaded
@@ -2647,6 +2612,23 @@ void ScriptEditor::_reload_scripts(bool p_refresh_only) {
if (text_file.is_valid()) {
text_file->reload_from_file();
}
+
+ ShaderTextEditor *ste = Object::cast_to<ShaderTextEditor>(seb);
+ if (ste) {
+ Ref<Shader> shader = edited_res;
+ Ref<ShaderInclude> shader_inc = edited_res;
+ String new_code;
+ if (shader.is_valid()) {
+ Ref<Shader> rel_sh = ResourceLoader::load(shader->get_path(), shader->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
+ ERR_CONTINUE(rel_sh.is_null());
+ new_code = rel_sh->get_code();
+ } else if (shader_inc.is_valid()) {
+ Ref<ShaderInclude> rel_sh_inc = ResourceLoader::load(shader_inc->get_path(), shader_inc->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
+ ERR_CONTINUE(rel_sh_inc.is_null());
+ new_code = rel_sh_inc->get_code();
+ }
+ ste->set_code_block_changed(new_code);
+ }
}
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
@@ -2675,7 +2657,7 @@ void ScriptEditor::_auto_format_text(ScriptEditorBase *p_seb) {
}
void ScriptEditor::open_find_in_files_dialog(const String &p_initial_text, bool p_replace) {
- find_in_files->open_dialog(p_initial_text, p_replace);
+ script_editor->find_in_files->open_dialog(p_initial_text, p_replace);
}
void ScriptEditor::open_script_create_dialog(const String &p_base_name, const String &p_base_path) {
@@ -2824,10 +2806,6 @@ void ScriptEditor::_apply_editor_settings() {
}
}
-void ScriptEditor::_filesystem_changed() {
- _update_script_names();
-}
-
void ScriptEditor::_files_moved(const String &p_old_file, const String &p_new_file) {
if (!script_editor_cache->has_section(p_old_file)) {
return;
@@ -2912,7 +2890,7 @@ Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
return Variant();
}
- Node *cur_node = tab_container->get_tab_control(tab_container->get_current_tab());
+ Node *cur_node = tab_container->get_current_tab_control();
HBoxContainer *drag_preview = memnew(HBoxContainer);
String preview_name = "";
@@ -2941,6 +2919,7 @@ Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
Dictionary drag_data;
drag_data["type"] = "script_list_element"; // using a custom type because node caused problems when dragging to scene tree
drag_data["script_list_element"] = cur_node;
+ drag_data["editor"] = config_section;
return drag_data;
}
@@ -2952,6 +2931,9 @@ bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data
}
if (String(d["type"]) == "script_list_element") {
+ if (d["editor"] != config_section) {
+ return false;
+ }
Node *node = Object::cast_to<Node>(d["script_list_element"]);
if (Object::cast_to<ScriptEditorBase>(node) || Object::cast_to<EditorHelp>(node)) {
@@ -2983,14 +2965,13 @@ bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data
if (file.is_empty() || !FileAccess::exists(file)) {
continue;
}
- if (ResourceLoader::exists(file, "Script") || ResourceLoader::exists(file, "JSON")) {
- Ref<Resource> scr = ResourceLoader::load(file);
- if (scr.is_valid()) {
- return true;
- }
+
+ const String &file_type = ResourceLoader::get_resource_type(file);
+ if (handled_resource_types.has(file_type)) {
+ return true;
}
- if (textfile_extensions.has(file.get_extension())) {
+ if (this == script_editor && textfile_extensions.has(file.get_extension())) {
Error err;
Ref<TextFile> text_file = _load_text_file(file, &err);
if (text_file.is_valid() && err == OK) {
@@ -3088,7 +3069,7 @@ void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Co
continue;
}
- if (!ResourceLoader::exists(file, "Script") && !ResourceLoader::exists(file, "JSON") && !textfile_extensions.has(file.get_extension())) {
+ if (!(handled_resource_types.has(ResourceLoader::get_resource_type(file)) || (this == script_editor && textfile_extensions.has(file.get_extension())))) {
continue;
}
@@ -3099,7 +3080,7 @@ void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Co
tab_container->move_child(tab_container->get_tab_control(tab_container->get_tab_count() - 1), new_index);
num_tabs_before = num_tabs;
} else if (num_tabs > 0) { /* Maybe script was already open */
- tab_container->move_child(tab_container->get_tab_control(tab_container->get_current_tab()), new_index);
+ tab_container->move_child(tab_container->get_current_tab_control(), new_index);
}
}
}
@@ -3111,8 +3092,6 @@ void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Co
}
void ScriptEditor::input(const Ref<InputEvent> &p_event) {
- // This is implemented in `input()` rather than `unhandled_input()` to allow
- // the shortcut to be used regardless of the click location.
// This feature can be disabled to avoid interfering with other uses of the additional
// mouse buttons, such as push-to-talk in a VoIP program.
if (EDITOR_GET("interface/editor/input/mouse_extra_buttons_navigate_history")) {
@@ -3121,13 +3100,16 @@ void ScriptEditor::input(const Ref<InputEvent> &p_event) {
// Navigate the script history using additional mouse buttons present on some mice.
// This must be hardcoded as the editor shortcuts dialog doesn't allow assigning
// more than one shortcut per action.
- if (mb.is_valid() && mb->is_pressed() && is_visible_in_tree()) {
- if (mb->get_button_index() == MouseButton::MB_XBUTTON1) {
- _history_back();
- }
+ Control *focus_owner = get_viewport()->gui_get_focus_owner();
+ if (focus_owner && (this == focus_owner || is_ancestor_of(focus_owner))) {
+ if (mb.is_valid() && mb->is_pressed() && is_visible_in_tree()) {
+ if (mb->get_button_index() == MouseButton::MB_XBUTTON1) {
+ _history_back();
+ }
- if (mb->get_button_index() == MouseButton::MB_XBUTTON2) {
- _history_forward();
+ if (mb->get_button_index() == MouseButton::MB_XBUTTON2) {
+ _history_forward();
+ }
}
}
}
@@ -3139,6 +3121,12 @@ void ScriptEditor::shortcut_input(const Ref<InputEvent> &p_event) {
if (!is_visible_in_tree() || !p_event->is_pressed()) {
return;
}
+
+ Control *focus_owner = get_viewport()->gui_get_focus_owner();
+ if (!(focus_owner && (this == focus_owner || is_ancestor_of(focus_owner)))) {
+ return;
+ }
+
if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
if (script_list->get_item_count() > 1) {
int next_tab = script_list->get_current() + 1;
@@ -3193,6 +3181,141 @@ void ScriptEditor::_script_list_clicked(int p_item, Vector2 p_local_mouse_pos, M
}
}
+void ScriptEditor::_setup_popup_menu(PopupMenu *p_menu, bool p_is_context_menu) {
+ ScriptEditorBase *seb = _get_current_editor();
+ const Ref<Resource> res = seb ? seb->get_edited_resource() : Ref<Resource>();
+ if (!p_is_context_menu) {
+ if (this == script_editor) {
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/new_script", TTRC("New Script..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW_SCRIPT);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTRC("New Text File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_TEXTFILE);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/open", TTRC("Open...")), FILE_MENU_OPEN);
+ } else {
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/new_shader", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW_SHADER);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/new_include", TTRC("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_INCLUDE);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/open_shader", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_MENU_OPEN_SHADER);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/open_include", TTRC("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_MENU_OPEN_INCLUDE);
+ }
+
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reopen_closed_script"), FILE_MENU_REOPEN_CLOSED);
+
+ p_menu->add_submenu_node_item(TTRC("Open Recent"), recent_scripts, FILE_MENU_OPEN_RECENT);
+ p_menu->add_separator();
+ }
+
+ if (!p_is_context_menu || res.is_valid()) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_MENU_SAVE);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_MENU_SAVE_AS);
+ }
+ if (!p_is_context_menu) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_all"), FILE_MENU_SAVE_ALL);
+ }
+ if (!p_is_context_menu || res.is_valid()) {
+ p_menu->add_separator();
+ }
+
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_MENU_CLOSE);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), FILE_MENU_CLOSE_OTHER_TABS);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_tabs_below"), FILE_MENU_CLOSE_TABS_BELOW);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), FILE_MENU_CLOSE_ALL);
+ if (this == script_editor) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_docs"), FILE_MENU_CLOSE_DOCS);
+ }
+ p_menu->add_separator();
+
+ Ref<Script> script = res;
+ if (this == script_editor && (!p_is_context_menu || (script.is_valid() && script->is_tool()))) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_MENU_SOFT_RELOAD_TOOL);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_MENU_RUN);
+ p_menu->add_separator();
+ }
+
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_MENU_COPY_PATH);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_uid"), FILE_MENU_COPY_UID);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), FILE_MENU_SHOW_IN_FILE_SYSTEM);
+ if (this == bottom_script_editor) {
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/open_in_inspector", TTRC("Open File in Inspector")), FILE_MENU_INSPECT);
+ p_menu->add_shortcut(ED_SHORTCUT("script_editor/inspect_native_code", TTRC("Inspect Native Shader Code...")), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);
+ }
+ p_menu->add_separator();
+
+ if (!p_is_context_menu) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/history_previous"), FILE_MENU_HISTORY_PREV);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/history_next"), FILE_MENU_HISTORY_NEXT);
+ p_menu->add_separator();
+
+ p_menu->add_submenu_node_item(TTRC("Theme"), theme_submenu, FILE_MENU_THEME_SUBMENU);
+ p_menu->add_separator();
+ }
+
+ if (p_is_context_menu) {
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), FILE_MENU_MOVE_UP);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), FILE_MENU_MOVE_DOWN);
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), FILE_MENU_SORT);
+ }
+
+ p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
+}
+
+void ScriptEditor::_prepare_popup_menu(PopupMenu *p_menu, bool p_is_context_menu) {
+ ScriptEditorBase *seb = _get_current_editor();
+ const Ref<Resource> res = seb ? seb->get_edited_resource() : Ref<Resource>();
+ if (!p_is_context_menu) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_REOPEN_CLOSED), previous_scripts.is_empty());
+ }
+
+ if (!p_is_context_menu || res.is_valid()) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SAVE), res.is_null());
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SAVE_AS), res.is_null());
+ }
+ if (!p_is_context_menu) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SAVE_ALL), !_has_script_tab());
+ }
+
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_CLOSE), tab_container->get_tab_count() < 1);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_CLOSE_TABS_BELOW), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_CLOSE_ALL), tab_container->get_tab_count() < 1);
+ if (this == script_editor) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_CLOSE_DOCS), !_has_docs_tab());
+ }
+
+ Ref<Script> script = res;
+ if (this == script_editor && (!p_is_context_menu || (script.is_valid() && script->is_tool()))) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SOFT_RELOAD_TOOL), script.is_null() || !script->is_tool());
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_RUN), script.is_null());
+ }
+
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_COPY_PATH), res.is_null() || res->get_path().is_empty());
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_COPY_UID), res.is_null() || ResourceLoader::get_resource_uid(res->get_path()) == ResourceUID::INVALID_ID);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SHOW_IN_FILE_SYSTEM), res.is_null() || res->get_path().is_empty());
+ if (this == bottom_script_editor) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_INSPECT), !seb);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), !seb);
+ }
+
+ if (!p_is_context_menu) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_HISTORY_PREV), history_pos <= 0);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_HISTORY_NEXT), history_pos >= history.size() - 1);
+ }
+
+ if (p_is_context_menu) {
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_MOVE_UP), tab_container->get_current_tab() <= 0);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_MOVE_DOWN), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
+ p_menu->set_item_disabled(p_menu->get_item_index(FILE_MENU_SORT), tab_container->get_tab_count() <= 1);
+ }
+}
+
+void ScriptEditor::_prepare_file_menu() {
+ _prepare_popup_menu(file_menu->get_popup(), false);
+}
+
+void ScriptEditor::_file_menu_closed() {
+ PopupMenu *menu = file_menu->get_popup();
+ for (int i = 0; i < menu->get_item_count(); i++) {
+ menu->set_item_disabled(i, false);
+ }
+}
+
void ScriptEditor::_make_script_list_context_menu() {
context_menu->clear();
@@ -3202,43 +3325,8 @@ void ScriptEditor::_make_script_list_context_menu() {
}
ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected));
- if (seb) {
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_MENU_SAVE);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_MENU_SAVE_AS);
- }
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_MENU_CLOSE);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), FILE_MENU_CLOSE_OTHER_TABS);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_tabs_below"), FILE_MENU_CLOSE_TABS_BELOW);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), FILE_MENU_CLOSE_ALL);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_docs"), FILE_MENU_CLOSE_DOCS);
- context_menu->add_separator();
- if (seb) {
- Ref<Script> scr = seb->get_edited_resource();
- if (scr.is_valid() && scr->is_tool()) {
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_MENU_SOFT_RELOAD_TOOL);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_MENU_RUN);
- context_menu->add_separator();
- }
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_MENU_COPY_PATH);
- context_menu->set_item_disabled(-1, seb->get_edited_resource()->get_path().is_empty());
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_uid"), FILE_MENU_COPY_UID);
- context_menu->set_item_disabled(-1, ResourceLoader::get_resource_uid(seb->get_edited_resource()->get_path()) == ResourceUID::INVALID_ID);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), FILE_MENU_SHOW_IN_FILE_SYSTEM);
- context_menu->add_separator();
- }
-
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), FILE_MENU_MOVE_UP);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), FILE_MENU_MOVE_DOWN);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), FILE_MENU_SORT);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
-
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), tab_container->get_tab_count() <= 0);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_DOCS), !_has_docs_tab());
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_TABS_BELOW), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_MOVE_UP), tab_container->get_current_tab() <= 0);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_MOVE_DOWN), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_SORT), tab_container->get_tab_count() <= 1);
+ _setup_popup_menu(context_menu, seb);
+ _prepare_popup_menu(context_menu, seb);
// Context menu plugin.
Vector<String> selected_paths;
@@ -3257,18 +3345,14 @@ void ScriptEditor::_make_script_list_context_menu() {
}
void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
- if (!bool(EDITOR_GET("text_editor/behavior/files/restore_scripts_on_load"))) {
- return;
- }
-
- if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help")) {
+ if (!p_layout->has_section_key(config_section, "open_scripts") && !p_layout->has_section_key(config_section, "open_help")) {
return;
}
- Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
+ Array scripts = p_layout->get_value(config_section, "open_scripts");
Array helps;
- if (p_layout->has_section_key("ScriptEditor", "open_help")) {
- helps = p_layout->get_value("ScriptEditor", "open_help");
+ if (p_layout->has_section_key(config_section, "open_help")) {
+ helps = p_layout->get_value(config_section, "open_help");
}
restoring_layout = true;
@@ -3278,7 +3362,7 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
ScriptEditorNavigationMarker::get_singleton()->init_begin();
- _set_script_zoom_factor(p_layout->get_value("ScriptEditor", "zoom_factor", 1.0f));
+ _set_script_zoom_factor(p_layout->get_value(config_section, "zoom_factor", 1.0f));
for (const Variant &v : scripts) {
String path = v;
@@ -3348,12 +3432,12 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
tab_container->get_tab_control(i)->set_meta("__editor_pass", Variant());
}
- if (p_layout->has_section_key("ScriptEditor", "script_split_offset")) {
- script_split->set_split_offset(p_layout->get_value("ScriptEditor", "script_split_offset"));
+ if (p_layout->has_section_key(config_section, "script_split_offset")) {
+ script_split->set_split_offset(p_layout->get_value(config_section, "script_split_offset"));
}
- if (p_layout->has_section_key("ScriptEditor", "list_split_offset")) {
- list_split->set_split_offset(p_layout->get_value("ScriptEditor", "list_split_offset"));
+ if (p_layout->has_section_key(config_section, "list_split_offset")) {
+ list_split->set_split_offset(p_layout->get_value(config_section, "list_split_offset"));
}
// Remove any deleted editors that have been removed between launches.
@@ -3381,8 +3465,8 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
ScriptEditorNavigationMarker::get_singleton()->init_end();
bool selected_saved_history = false;
- if (p_layout->has_section_key("ScriptEditor", "selected_script")) {
- String selected_script = p_layout->get_value("ScriptEditor", "selected_script");
+ if (p_layout->has_section_key(config_section, "selected_script")) {
+ String selected_script = p_layout->get_value(config_section, "selected_script");
// If the selected script is not in the list of open scripts, select nothing.
for (int i = 0; i < tab_container->get_tab_count() && !selected_script.is_empty(); i++) {
ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
@@ -3434,15 +3518,15 @@ void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
}
}
- p_layout->set_value("ScriptEditor", "open_scripts", scripts);
- p_layout->set_value("ScriptEditor", "selected_script", selected_script);
- p_layout->set_value("ScriptEditor", "open_help", helps);
- p_layout->set_value("ScriptEditor", "script_split_offset", script_split->get_split_offset());
- p_layout->set_value("ScriptEditor", "list_split_offset", list_split->get_split_offset());
- p_layout->set_value("ScriptEditor", "zoom_factor", zoom_factor);
+ p_layout->set_value(config_section, "open_scripts", scripts);
+ p_layout->set_value(config_section, "selected_script", selected_script);
+ p_layout->set_value(config_section, "open_help", helps);
+ p_layout->set_value(config_section, "script_split_offset", script_split->get_split_offset());
+ p_layout->set_value(config_section, "list_split_offset", list_split->get_split_offset());
+ p_layout->set_value(config_section, "zoom_factor", zoom_factor);
// Save the cache.
- script_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg"));
+ script_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join(cache_path));
}
void ScriptEditor::_help_class_open(const String &p_class) {
@@ -3545,9 +3629,8 @@ void ScriptEditor::update_docs_from_script(const Ref<Script> &p_script) {
}
void ScriptEditor::_update_selected_editor_menu() {
- TextEditorBase *current_editor = Object::cast_to<TextEditorBase>(_get_current_editor());
- for (Control *editor_menu : editor_menus) {
- editor_menu->set_visible(current_editor && editor_menu == current_editor->get_edit_menu());
+ for (ScriptEditorBase::EditMenusBase *editor_menu : editor_menus) {
+ editor_menu->set_visible(editor_menu->handles(_get_current_editor()));
}
PopupMenu *search_popup = script_search_menu->get_popup();
@@ -3578,12 +3661,11 @@ void ScriptEditor::_update_history_pos(int p_new_pos) {
n = history[history_pos].control;
- ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(n);
- if (seb) {
+ if (ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(n)) {
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(n)) {
teb->set_edit_state(history[history_pos].state);
- teb->ensure_focus();
}
+ seb->ensure_focus();
Ref<Script> scr = seb->get_edited_resource();
if (scr.is_valid()) {
@@ -3661,11 +3743,11 @@ Vector<Ref<Script>> ScriptEditor::get_open_scripts() const {
return out_scripts;
}
-ScriptEditorBase *ScriptEditor::get_script_editor(Ref<Resource> p_script) const {
+ScriptEditorBase *ScriptEditor::get_resource_editor(const Ref<Resource> &p_res) const {
for (int i = 0; i < tab_container->get_tab_count(); i++) {
- ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
- if (se && se->get_edited_resource() == p_script) {
- return se;
+ ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
+ if (seb && seb->get_edited_resource() == p_res) {
+ return seb;
}
}
return nullptr;
@@ -3739,7 +3821,7 @@ void ScriptEditor::_calculate_script_name_button_ratio() {
}
void ScriptEditor::_help_search(const String &p_text) {
- help_search_dialog->popup_dialog(p_text);
+ script_editor->help_search_dialog->popup_dialog(p_text);
}
void ScriptEditor::register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) {
@@ -3777,9 +3859,8 @@ void ScriptEditor::_on_find_in_files_result_selected(const String &fpath, int li
ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_editor_data().get_editor_by_name("Shader"));
shader_editor->edit(res.ptr());
shader_editor->make_visible(true);
- TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(shader_editor->get_shader_editor(res));
- if (text_shader_editor) {
- text_shader_editor->goto_line_selection(line_number - 1, begin, end);
+ if (ShaderTextEditor *shader_te = Object::cast_to<ShaderTextEditor>(ScriptEditor::get_bottom_script_editor()->get_resource_editor(res))) {
+ shader_te->goto_line_selection(line_number - 1, begin, end);
}
ScriptEditorNavigationMarker::get_singleton()->locate_end();
return;
@@ -3879,9 +3960,9 @@ void ScriptEditor::_update_code_editor_zoom_factor(CodeTextEditor *p_code_text_e
}
}
-void ScriptEditor::_window_changed(bool p_visible) {
- make_floating->set_visible(!p_visible);
- is_floating = p_visible;
+void ScriptEditor::_floating_state_changed(bool p_floating) {
+ make_floating->set_visible(!p_floating);
+ is_floating = p_floating;
}
void ScriptEditor::_filter_scripts_text_changed(const String &p_newtext) {
@@ -3916,11 +3997,27 @@ void ScriptEditor::_bind_methods() {
ADD_SIGNAL(MethodInfo("script_close", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, Script::get_class_static())));
}
-ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
- window_wrapper = p_wrapper;
+ScriptEditor::ScriptEditor(const String &p_config_section, const String &p_cache_path, WindowWrapper *p_wrapper, EditorDock *p_dock) {
+ config_section = p_config_section;
+ cache_path = p_cache_path;
+ // The main editor uses WindowWrapper while the shader editor uses EditorDock.
+ if (p_wrapper) {
+ script_editor = this;
+ } else {
+ bottom_script_editor = this;
+ }
+
+ make_floating = memnew(ScreenSelect);
+ make_floating->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
+
+ if (p_wrapper) {
+ make_floating->connect("request_open_in_screen", callable_mp(p_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
+ } else if (p_dock) {
+ make_floating->connect("request_open_in_screen", callable_mp(p_dock, &EditorDock::make_floating));
+ }
script_editor_cache.instantiate();
- script_editor_cache->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg"));
+ script_editor_cache->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join(cache_path));
restoring_layout = false;
pending_auto_reload = false;
@@ -3954,8 +4051,8 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
list_split->add_child(scripts_vbox);
filter_scripts = memnew(LineEdit);
- filter_scripts->set_placeholder(TTRC("Filter Scripts"));
- filter_scripts->set_accessibility_name(TTRC("Filter Scripts"));
+ filter_scripts->set_placeholder(TTRC("Filter Documents"));
+ filter_scripts->set_accessibility_name(TTRC("Filter Documents"));
filter_scripts->set_clear_button_enabled(true);
filter_scripts->connect(SceneStringName(text_changed), callable_mp(this, &ScriptEditor::_filter_scripts_text_changed));
scripts_vbox->add_child(filter_scripts);
@@ -3976,7 +4073,7 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
add_child(context_menu);
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_menu_option));
- document_outline = memnew(DocumentOutline);
+ document_outline = memnew(DocumentOutline(this));
document_outline->set_custom_minimum_size(Size2(0, 90));
document_outline->set_v_size_flags(SIZE_EXPAND_FILL);
@@ -4003,6 +4100,32 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
// FIXME: These should be `Key::GREATER` and `Key::LESS` but those don't work.
ED_SHORTCUT("script_editor/next_script", TTRC("Next Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::PERIOD);
ED_SHORTCUT("script_editor/prev_script", TTRC("Previous Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::COMMA);
+
+ ED_SHORTCUT("script_editor/save", TTRC("Save"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S);
+ ED_SHORTCUT("script_editor/save_as", TTRC("Save As..."));
+ ED_SHORTCUT("script_editor/save_all", TTRC("Save All"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::S);
+ ED_SHORTCUT_OVERRIDE("script_editor/save_all", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::S);
+
+ ED_SHORTCUT("script_editor/reload_script_soft", TTRC("Soft Reload Tool Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R);
+ ED_SHORTCUT("script_editor/run_file", TTRC("Run"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::X);
+
+ ED_SHORTCUT("script_editor/copy_path", TTRC("Copy File Path"));
+ ED_SHORTCUT("script_editor/copy_uid", TTRC("Copy File UID"));
+ ED_SHORTCUT("script_editor/show_in_file_system", TTRC("Show in FileSystem"));
+
+ ED_SHORTCUT_ARRAY("script_editor/history_previous", TTRC("History Previous"), { int32_t(KeyModifierMask::ALT | Key::LEFT), int32_t(Key::BACK) });
+ ED_SHORTCUT_ARRAY("script_editor/history_next", TTRC("History Next"), { int32_t(KeyModifierMask::ALT | Key::RIGHT), int32_t(Key::FORWARD) });
+ ED_SHORTCUT_OVERRIDE("script_editor/history_previous", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::LEFT);
+ ED_SHORTCUT_OVERRIDE("script_editor/history_next", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::RIGHT);
+
+ ED_SHORTCUT("script_editor/close_file", TTRC("Close"), KeyModifierMask::CMD_OR_CTRL | Key::W);
+ ED_SHORTCUT("script_editor/close_all", TTRC("Close All"));
+ ED_SHORTCUT("script_editor/close_other_tabs", TTRC("Close Other Tabs"));
+ ED_SHORTCUT("script_editor/close_tabs_below", TTRC("Close Tabs Below"));
+ ED_SHORTCUT("script_editor/close_docs", TTRC("Close Docs"));
+
+ ED_SHORTCUT("script_editor/toggle_files_panel", TTRC("Toggle Files Panel"), KeyModifierMask::CMD_OR_CTRL | Key::BACKSLASH);
+
set_process_input(true);
set_process_shortcut_input(true);
@@ -4012,70 +4135,23 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
file_menu->set_text(TTRC("File"));
file_menu->set_switch_on_hover(true);
file_menu->set_shortcut_context(this);
+ file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_menu_option));
+ file_menu->get_popup()->connect("about_to_popup", callable_mp(this, &ScriptEditor::_prepare_file_menu));
+ file_menu->get_popup()->connect("popup_hide", callable_mp(this, &ScriptEditor::_file_menu_closed));
menu_hb->add_child(file_menu);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTRC("New Script..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_MENU_NEW_SCRIPT);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTRC("New Text File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_MENU_NEW_TEXTFILE);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTRC("Open...")), FILE_MENU_OPEN);
- file_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_editor/reopen_closed_script"), FILE_MENU_REOPEN_CLOSED);
-
recent_scripts = memnew(PopupMenu);
recent_scripts->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
- file_menu->get_popup()->add_submenu_node_item(TTRC("Open Recent"), recent_scripts, FILE_MENU_OPEN_RECENT);
recent_scripts->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_open_recent_script));
-
_update_recent_scripts();
- file_menu->get_popup()->add_separator();
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTRC("Save"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_MENU_SAVE);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTRC("Save As...")), FILE_MENU_SAVE_AS);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTRC("Save All"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::S), FILE_MENU_SAVE_ALL);
- ED_SHORTCUT_OVERRIDE("script_editor/save_all", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::S);
- file_menu->get_popup()->add_separator();
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTRC("Soft Reload Tool Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R), FILE_MENU_SOFT_RELOAD_TOOL);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path", TTRC("Copy File Path")), FILE_MENU_COPY_PATH);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_uid", TTRC("Copy File UID")), FILE_MENU_COPY_UID);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system", TTRC("Show in FileSystem")), FILE_MENU_SHOW_IN_FILE_SYSTEM);
- file_menu->get_popup()->add_separator();
-
- file_menu->get_popup()->add_shortcut(
- ED_SHORTCUT_ARRAY("script_editor/history_previous", TTRC("History Previous"),
- { int32_t(KeyModifierMask::ALT | Key::LEFT), int32_t(Key::BACK) }),
- FILE_MENU_HISTORY_PREV);
- file_menu->get_popup()->add_shortcut(
- ED_SHORTCUT_ARRAY("script_editor/history_next", TTRC("History Next"),
- { int32_t(KeyModifierMask::ALT | Key::RIGHT), int32_t(Key::FORWARD) }),
- FILE_MENU_HISTORY_NEXT);
- ED_SHORTCUT_OVERRIDE("script_editor/history_previous", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::LEFT);
- ED_SHORTCUT_OVERRIDE("script_editor/history_next", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::RIGHT);
-
- file_menu->get_popup()->add_separator();
-
theme_submenu = memnew(PopupMenu);
+ theme_submenu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_theme_option));
theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTRC("Import Theme...")), THEME_IMPORT);
theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTRC("Reload Theme")), THEME_RELOAD);
- file_menu->get_popup()->add_submenu_node_item(TTRC("Theme"), theme_submenu, FILE_MENU_THEME_SUBMENU);
- theme_submenu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_theme_option));
-
theme_submenu->add_separator();
theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTRC("Save Theme As...")), THEME_SAVE_AS);
- file_menu->get_popup()->add_separator();
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTRC("Close"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_MENU_CLOSE);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTRC("Close All")), FILE_MENU_CLOSE_ALL);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs", TTRC("Close Other Tabs")), FILE_MENU_CLOSE_OTHER_TABS);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_tabs_below", TTRC("Close Tabs Below")), FILE_MENU_CLOSE_TABS_BELOW);
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTRC("Close Docs")), FILE_MENU_CLOSE_DOCS);
-
- file_menu->get_popup()->add_separator();
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file", TTRC("Run"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::X), FILE_MENU_RUN);
-
- file_menu->get_popup()->add_separator();
- file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_files_panel", TTRC("Toggle Files Panel"), KeyModifierMask::CMD_OR_CTRL | Key::BACKSLASH), FILE_MENU_TOGGLE_FILES_PANEL);
- file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_menu_option));
- file_menu->get_popup()->connect("about_to_popup", callable_mp(this, &ScriptEditor::_prepare_file_menu));
- file_menu->get_popup()->connect("popup_hide", callable_mp(this, &ScriptEditor::_file_menu_closed));
-
script_search_menu = memnew(MenuButton);
script_search_menu->set_flat(false);
script_search_menu->set_theme_type_variation("FlatMenuButton");
@@ -4085,19 +4161,23 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
script_search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_menu_option));
menu_hb->add_child(script_search_menu);
- MenuButton *debug_menu_btn = memnew(MenuButton);
- debug_menu_btn->set_flat(false);
- debug_menu_btn->set_theme_type_variation("FlatMenuButton");
- menu_hb->add_child(debug_menu_btn);
- debug_menu_btn->hide(); // Handled by EditorDebuggerNode below.
+ _setup_popup_menu(file_menu->get_popup(), false);
- EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
- debugger->set_script_debug_button(debug_menu_btn);
- debugger->connect("goto_script_line", callable_mp(this, &ScriptEditor::_goto_script_line));
- debugger->connect("set_execution", callable_mp(this, &ScriptEditor::_set_execution));
- debugger->connect("clear_execution", callable_mp(this, &ScriptEditor::_clear_execution));
- debugger->connect("breakpoint_set_in_tree", callable_mp(this, &ScriptEditor::_set_breakpoint));
- debugger->connect("breakpoints_cleared_in_tree", callable_mp(this, &ScriptEditor::_clear_breakpoints));
+ if (this == script_editor) {
+ MenuButton *debug_menu_btn = memnew(MenuButton);
+ debug_menu_btn->set_flat(false);
+ debug_menu_btn->set_theme_type_variation("FlatMenuButton");
+ menu_hb->add_child(debug_menu_btn);
+ debug_menu_btn->hide(); // Handled by EditorDebuggerNode below.
+
+ EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
+ debugger->set_script_debug_button(debug_menu_btn);
+ debugger->connect("goto_script_line", callable_mp(this, &ScriptEditor::_goto_script_line));
+ debugger->connect("set_execution", callable_mp(this, &ScriptEditor::_set_execution));
+ debugger->connect("clear_execution", callable_mp(this, &ScriptEditor::_clear_execution));
+ debugger->connect("breakpoint_set_in_tree", callable_mp(this, &ScriptEditor::_set_breakpoint));
+ debugger->connect("breakpoints_cleared_in_tree", callable_mp(this, &ScriptEditor::_clear_breakpoints));
+ }
script_name_button_hbox = memnew(HBoxContainer);
script_name_button_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
@@ -4127,12 +4207,14 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
site_search->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_WEBSITE));
menu_hb->add_child(site_search);
- help_search = memnew(Button);
- help_search->set_theme_type_variation(SceneStringName(FlatButton));
- help_search->set_text(TTRC("Search Help"));
- help_search->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_HELP));
- menu_hb->add_child(help_search);
- help_search->set_tooltip_text(TTRC("Search the reference documentation."));
+ if (this == script_editor) {
+ help_search = memnew(Button);
+ help_search->set_theme_type_variation(SceneStringName(FlatButton));
+ help_search->set_text(TTRC("Search Help"));
+ help_search->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_HELP));
+ menu_hb->add_child(help_search);
+ help_search->set_tooltip_text(TTRC("Search the reference documentation."));
+ }
menu_hb->add_child(memnew(VSeparator));
@@ -4153,15 +4235,9 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
script_forward->connect(SceneStringName(pressed), callable_mp(this, &ScriptEditor::_history_forward));
menu_hb->add_child(memnew(VSeparator));
-
- make_floating = memnew(ScreenSelect);
- make_floating->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
- make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
-
menu_hb->add_child(make_floating);
- p_wrapper->connect("window_visibility_changed", callable_mp(this, &ScriptEditor::_window_changed));
- tab_container->connect("tab_changed", callable_mp(this, &ScriptEditor::_tab_changed));
+ tab_container->connect("tab_changed", callable_mp(this, &ScriptEditor::ensure_select_current).unbind(1));
erase_tab_confirm = memnew(ConfirmationDialog);
erase_tab_confirm->set_flag(Window::FLAG_RESIZE_DISABLED, true);
@@ -4174,13 +4250,18 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
script_create_dialog = memnew(ScriptCreateDialog);
script_create_dialog->set_title(TTRC("Create Script"));
add_child(script_create_dialog);
- script_create_dialog->connect("script_created", callable_mp(this, &ScriptEditor::_script_created));
+ script_create_dialog->connect("script_created", callable_mp(this, &ScriptEditor::_resource_created));
file_dialog_option = -1;
file_dialog = memnew(EditorFileDialog);
add_child(file_dialog);
file_dialog->connect("file_selected", callable_mp(this, &ScriptEditor::_file_dialog_action));
+ shader_create_dialog = memnew(ShaderCreateDialog);
+ shader_create_dialog->connect("shader_created", callable_mp(this, &ScriptEditor::_resource_created));
+ shader_create_dialog->connect("shader_include_created", callable_mp(this, &ScriptEditor::_resource_created));
+ add_child(shader_create_dialog);
+
error_dialog = memnew(AcceptDialog);
add_child(error_dialog);
@@ -4215,8 +4296,6 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
add_child(disk_changed);
- script_editor = this;
-
autosave_timer = memnew(Timer);
autosave_timer->set_one_shot(false);
autosave_timer->connect(SceneStringName(tree_entered), callable_mp(this, &ScriptEditor::_update_autosave_timer));
@@ -4225,13 +4304,15 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
grab_focus_block = false;
- help_search_dialog = memnew(EditorHelpSearch);
- add_child(help_search_dialog);
- help_search_dialog->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
+ if (this == script_editor) {
+ help_search_dialog = memnew(EditorHelpSearch);
+ add_child(help_search_dialog);
+ help_search_dialog->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
- find_in_files = memnew(FindInFiles);
- find_in_files->connect("result_selected", callable_mp(this, &ScriptEditor::_on_find_in_files_result_selected));
- find_in_files->connect("files_modified", callable_mp(this, &ScriptEditor::_on_find_in_files_modified_files));
+ find_in_files = memnew(FindInFiles);
+ find_in_files->connect("result_selected", callable_mp(this, &ScriptEditor::_on_find_in_files_result_selected));
+ find_in_files->connect("files_modified", callable_mp(this, &ScriptEditor::_on_find_in_files_modified_files));
+ }
history_pos = -1;
@@ -4241,6 +4322,11 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
convert_indent_on_save = EDITOR_GET("text_editor/behavior/files/convert_indent_on_save");
highlight_scene_scripts = EDITOR_GET("text_editor/script_list/highlight_scene_scripts");
+ _update_online_doc();
+ _update_selected_editor_menu();
+
+ //// Highlighters ////
+
Ref<EditorJSONSyntaxHighlighter> json_syntax_highlighter;
json_syntax_highlighter.instantiate();
register_syntax_highlighter(json_syntax_highlighter);
@@ -4253,12 +4339,16 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
config_file_syntax_highlighter.instantiate();
register_syntax_highlighter(config_file_syntax_highlighter);
- _update_online_doc();
+ Ref<GDShaderSyntaxHighlighter> gdshader_syntax_highlighter;
+ gdshader_syntax_highlighter.instantiate();
+ register_syntax_highlighter(gdshader_syntax_highlighter);
}
ScriptEditor::~ScriptEditor() {
- memdelete(find_in_files);
ScriptEditorNavigationMarker::release_singleton();
+ if (this == script_editor) {
+ memdelete(find_in_files);
+ }
}
void ScriptEditorPlugin::_focus_another_editor() {
@@ -4423,7 +4513,7 @@ String ScriptEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
return String();
} else {
message.resize(unsaved_built_in_scripts.size() + 1);
- message.write[0] = TTR("There are unsaved changes in the following built-in script(s):");
+ message.write[0] = TTR("There are unsaved changes in the following built-in resource(s)");
int i = 1;
for (const String &E : unsaved_built_in_scripts) {
@@ -4435,7 +4525,7 @@ String ScriptEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
}
message.resize(unsaved_scripts.size() + 1);
- message.write[0] = TTR("Save changes to the following script(s) before quitting?");
+ message.write[0] = TTR("Save changes to the following file(s) before quitting?");
int i = 1;
for (const String &E : unsaved_scripts) {
@@ -4451,12 +4541,10 @@ void ScriptEditorPlugin::save_external_data() {
}
}
-void ScriptEditorPlugin::apply_changes() {
- script_editor->apply_scripts();
-}
-
void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
- script_editor->set_window_layout(p_layout);
+ if (bool(EDITOR_GET("text_editor/behavior/files/restore_scripts_on_load"))) {
+ script_editor->set_window_layout(p_layout);
+ }
if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ScriptEditor", "window_rect")) {
window_wrapper->restore_window_from_saved_position(
@@ -4469,6 +4557,15 @@ void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
}
void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
+#ifndef DISABLE_DEPRECATED
+ EditorSettings *es = EditorSettings::get_singleton();
+ if (es && es->get_project_metadata("recent_files", "scripts", "") != "") {
+ es->set_project_metadata("recent_files", "ScriptEditor", es->get_project_metadata("recent_files", "scripts", Array()));
+ es->set_project_metadata("recent_files", "scripts", Variant());
+ es->save_project_metadata();
+ }
+#endif
+
script_editor->get_window_layout(p_layout);
if (window_wrapper->get_window_enabled()) {
@@ -4490,14 +4587,6 @@ void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
}
}
-void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
- script_editor->get_breakpoints(p_breakpoints);
-}
-
-void ScriptEditorPlugin::edited_scene_changed() {
- script_editor->edited_scene_changed();
-}
-
ScriptEditorPlugin::ScriptEditorPlugin() {
script_editor_plugin = this;
@@ -4512,7 +4601,8 @@ ScriptEditorPlugin::ScriptEditorPlugin() {
window_wrapper = memnew(WindowWrapper);
window_wrapper->set_margins_enabled(true);
- script_editor = memnew(ScriptEditor(window_wrapper));
+ script_editor = memnew(ScriptEditor("ScriptEditor", "script_editor_cache.cfg", window_wrapper));
+ script_editor->set_handled_resource_types({ "GDScript", "Script", "JSON" });
Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("script_editor/make_floating", TTRC("Make Floating"));
window_wrapper->set_wrapped_control(script_editor, make_floating_shortcut);
diff --git a/editor/script/script_editor_plugin.h b/editor/script/script_editor_plugin.h
index b9ee2560292..f636210e7a6 100644
--- a/editor/script/script_editor_plugin.h
+++ b/editor/script/script_editor_plugin.h
@@ -34,7 +34,6 @@
#include "core/object/script_language.h"
#include "editor/plugins/editor_plugin.h"
#include "editor/script/script_editor_base.h"
-#include "scene/gui/dialogs.h"
#include "scene/gui/panel_container.h"
#include "scene/resources/text_file.h"
@@ -42,17 +41,11 @@ class CodeTextEditor;
class EditorFileDialog;
class EditorHelpSearch;
class FilterLineEdit;
-class FindReplaceBar;
class HSplitContainer;
class ItemList;
-class MenuButton;
class TabContainer;
-class TextureRect;
class Tree;
-class VSplitContainer;
class WindowWrapper;
-class EditorSyntaxHighlighter;
-class ScriptEditorBase;
class ScriptEditorQuickOpen : public ConfirmationDialog {
GDCLASS(ScriptEditorQuickOpen, ConfirmationDialog);
@@ -80,6 +73,7 @@ public:
class DocumentOutline : public VBoxContainer {
GDCLASS(DocumentOutline, VBoxContainer);
+ ScriptEditor *script_editor = nullptr;
ItemList *item_list = nullptr;
HBoxContainer *buttons_hbox = nullptr;
FilterLineEdit *filter = nullptr;
@@ -99,11 +93,13 @@ public:
void update_outline();
void update_visibility();
- DocumentOutline();
+ DocumentOutline(ScriptEditor *p_script_editor);
};
class EditorScriptCodeCompletionCache;
class FindInFiles;
+class TextFile;
+class ShaderCreateDialog;
class ScriptEditor : public PanelContainer {
GDCLASS(ScriptEditor, PanelContainer);
@@ -112,7 +108,12 @@ class ScriptEditor : public PanelContainer {
// File.
FILE_MENU_NEW_SCRIPT,
FILE_MENU_NEW_TEXTFILE,
+ FILE_MENU_NEW_SHADER,
+ FILE_MENU_NEW_INCLUDE,
+
FILE_MENU_OPEN,
+ FILE_MENU_OPEN_SHADER,
+ FILE_MENU_OPEN_INCLUDE,
FILE_MENU_REOPEN_CLOSED,
FILE_MENU_OPEN_RECENT,
@@ -120,6 +121,9 @@ class ScriptEditor : public PanelContainer {
FILE_MENU_SAVE_AS,
FILE_MENU_SAVE_ALL,
+ FILE_MENU_INSPECT,
+ FILE_MENU_INSPECT_NATIVE_SHADER_CODE,
+
FILE_MENU_SOFT_RELOAD_TOOL,
FILE_MENU_COPY_PATH,
FILE_MENU_COPY_UID,
@@ -179,7 +183,7 @@ class ScriptEditor : public PanelContainer {
MenuButton *debug_menu = nullptr;
PopupMenu *context_menu = nullptr;
Timer *autosave_timer = nullptr;
- LocalVector<Control *> editor_menus;
+ LocalVector<ScriptEditorBase::EditMenusBase *> editor_menus;
PopupMenu *recent_scripts = nullptr;
PopupMenu *theme_submenu = nullptr;
@@ -197,12 +201,14 @@ class ScriptEditor : public PanelContainer {
VBoxContainer *scripts_vbox = nullptr;
VSplitContainer *list_split = nullptr;
TabContainer *tab_container = nullptr;
- EditorFileDialog *file_dialog = nullptr;
AcceptDialog *error_dialog = nullptr;
ConfirmationDialog *erase_tab_confirm = nullptr;
- ScriptCreateDialog *script_create_dialog = nullptr;
FindReplaceBar *find_replace_bar = nullptr;
+ EditorFileDialog *file_dialog = nullptr;
+ ScriptCreateDialog *script_create_dialog = nullptr;
+ ShaderCreateDialog *shader_create_dialog = nullptr;
+
float zoom_factor = 1.0f;
HBoxContainer *script_name_button_hbox = nullptr;
@@ -243,16 +249,11 @@ class ScriptEditor : public PanelContainer {
List<String> previous_scripts;
List<int> script_close_queue;
- List<String> _get_recognized_extensions();
-
- void _tab_changed(int p_which);
void _menu_option(int p_option);
void _theme_option(int p_option);
void _show_save_theme_as_dialog();
bool _has_docs_tab() const;
bool _has_script_tab() const;
- void _prepare_file_menu();
- void _file_menu_closed();
Tree *disk_changed_list = nullptr;
ConfirmationDialog *disk_changed = nullptr;
@@ -314,7 +315,7 @@ class ScriptEditor : public PanelContainer {
void _set_execution(Ref<RefCounted> p_script, int p_line) { _change_execution(p_script, p_line, true); }
void _clear_execution(Ref<RefCounted> p_script) { _change_execution(p_script); }
String _get_debug_tooltip(const String &p_text, Node *p_se);
- void _script_created(Ref<Script> p_script);
+ void _resource_created(Ref<Resource> p_res);
void _set_breakpoint(Ref<RefCounted> p_script, int p_line, bool p_enabled);
void _clear_breakpoints();
Array _get_cached_breakpoints_for_script(const String &p_path) const;
@@ -323,10 +324,11 @@ class ScriptEditor : public PanelContainer {
TypedArray<ScriptEditorBase> _get_open_script_editors() const;
Ref<ConfigFile> script_editor_cache;
+ String cache_path;
+
void _save_editor_state(ScriptEditorBase *p_editor);
void _save_layout();
void _apply_editor_settings();
- void _filesystem_changed();
void _files_moved(const String &p_old_file, const String &p_new_file);
void _file_removed(const String &p_file);
void _autosave_scripts();
@@ -355,6 +357,10 @@ class ScriptEditor : public PanelContainer {
virtual void input(const Ref<InputEvent> &p_event) override;
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
+ void _setup_popup_menu(PopupMenu *p_menu, bool p_is_context_menu = true);
+ void _prepare_popup_menu(PopupMenu *p_menu, bool p_is_context_menu = true);
+ void _prepare_file_menu();
+ void _file_menu_closed();
void _script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
void _make_script_list_context_menu();
@@ -389,6 +395,10 @@ class ScriptEditor : public PanelContainer {
Ref<Script> _get_current_script();
TypedArray<Script> _get_open_scripts() const;
+ String config_section;
+
+ HashSet<String> handled_resource_types;
+ List<String> _get_recognized_extensions();
HashSet<String> textfile_extensions;
Ref<TextFile> _load_text_file(const String &p_path, Error *r_error) const;
Error _save_text_file(Ref<TextFile> p_text_file, const String &p_path);
@@ -399,11 +409,12 @@ class ScriptEditor : public PanelContainer {
void _set_script_zoom_factor(float p_zoom_factor);
void _update_code_editor_zoom_factor(CodeTextEditor *p_code_text_editor);
- void _window_changed(bool p_visible);
+ void _floating_state_changed(bool p_floating);
void _close_builtin_scripts_from_scene(const String &p_scene);
- static ScriptEditor *script_editor;
+ inline static ScriptEditor *script_editor = nullptr;
+ inline static ScriptEditor *bottom_script_editor = nullptr;
protected:
void _notification(int p_what);
@@ -411,6 +422,7 @@ protected:
public:
static ScriptEditor *get_singleton() { return script_editor; }
+ static ScriptEditor *get_bottom_script_editor() { return bottom_script_editor; }
bool toggle_files_panel();
bool is_files_panel_toggled();
@@ -426,6 +438,8 @@ public:
bool is_editor_floating();
+ void set_handled_resource_types(HashSet<String> p_file_types) { handled_resource_types = p_file_types; }
+
_FORCE_INLINE_ bool edit(const Ref<Resource> &p_resource, bool p_grab_focus = true) { return edit(p_resource, -1, 0, p_grab_focus); }
bool edit(const Ref<Resource> &p_resource, int p_line, int p_col, bool p_grab_focus = true);
@@ -446,7 +460,7 @@ public:
void set_scene_root_script(Ref<Script> p_script);
Vector<Ref<Script>> get_open_scripts() const;
- ScriptEditorBase *get_script_editor(Ref<Resource> p_script) const;
+ ScriptEditorBase *get_resource_editor(const Ref<Resource> &p_res) const;
ScriptEditorBase *get_current_editor() const { return _get_current_editor(); }
@@ -473,7 +487,7 @@ public:
static void register_create_script_editor_function(CreateScriptEditorFunc p_func);
- ScriptEditor(WindowWrapper *p_wrapper);
+ ScriptEditor(const String &p_config_section, const String &p_cache_path, WindowWrapper *p_wrapper = nullptr, EditorDock *p_dock = nullptr);
~ScriptEditor();
};
@@ -501,7 +515,7 @@ public:
static bool open_in_external_editor(const String &p_path, int p_line, int p_col, bool p_ignore_project = false);
virtual String get_plugin_name() const override { return TTRC("Script"); }
- bool has_main_screen() const override { return true; }
+ virtual bool has_main_screen() const override { return true; }
virtual void edit(Object *p_object) override;
virtual bool handles(Object *p_object) const override;
virtual void make_visible(bool p_visible) override;
@@ -509,14 +523,14 @@ public:
virtual String get_unsaved_status(const String &p_for_scene) const override;
virtual void save_external_data() override;
- virtual void apply_changes() override;
+ virtual void apply_changes() override { script_editor->apply_scripts(); }
virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
- virtual void get_breakpoints(List<String> *p_breakpoints) override;
+ virtual void get_breakpoints(List<String> *p_breakpoints) override { script_editor->get_breakpoints(p_breakpoints); }
- virtual void edited_scene_changed() override;
+ virtual void edited_scene_changed() override { script_editor->edited_scene_changed(); }
ScriptEditorPlugin();
};
diff --git a/editor/script/script_text_editor.cpp b/editor/script/script_text_editor.cpp
index f5d196c1c0f..64d97b0a050 100644
--- a/editor/script/script_text_editor.cpp
+++ b/editor/script/script_text_editor.cpp
@@ -137,55 +137,19 @@ ConnectionInfoDialog::ConnectionInfoDialog() {
////////////////////////////////////////////////////////////////////////////////
-void ScriptTextEditor::EditMenusSTE::_update_breakpoint_list() {
- breakpoints_menu->clear();
- breakpoints_menu->reset_size();
-
- breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
- breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
- breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
+void ScriptTextEditor::EditMenusScTE::EditMenusScTE::_update_breakpoint_list() {
+ EditMenusCEB::_update_breakpoint_list();
breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
-
- TextEditorBase *script_text_editor = _get_active_editor();
- if (script_text_editor == nullptr) {
- return;
- }
-
- PackedInt32Array breakpoint_list = script_text_editor->get_code_editor()->get_text_editor()->get_breakpointed_lines();
- if (breakpoint_list.is_empty()) {
- return;
- }
-
- breakpoints_menu->add_separator();
-
- for (int i = 0; i < breakpoint_list.size(); i++) {
- // Strip edges to remove spaces or tabs.
- // Also replace any tabs by spaces, since we can't print tabs in the menu.
- String line = script_text_editor->get_code_editor()->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
-
- // Limit the size of the line if too big.
- if (line.length() > 50) {
- line = line.substr(0, 50);
- }
-
- breakpoints_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`");
- breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]);
- }
-}
-
-void ScriptTextEditor::EditMenusSTE::_breakpoint_item_pressed(int p_idx) {
- TextEditorBase *script_text_editor = _get_active_editor();
- ERR_FAIL_NULL(script_text_editor);
- if (p_idx < 4) { // Any item before the separator.
- _edit_option(breakpoints_menu->get_item_id(p_idx));
- } else {
- ScriptEditorNavigationMarker::get_singleton()->locate_begin();
- script_text_editor->get_code_editor()->goto_line_centered(breakpoints_menu->get_item_metadata(p_idx));
- ScriptEditorNavigationMarker::get_singleton()->locate_end();
- }
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
+ breakpoints_menu->set_item_index(-1, 0);
}
-ScriptTextEditor::EditMenusSTE::EditMenusSTE() {
+ScriptTextEditor::EditMenusScTE::EditMenusScTE(ScriptEditor *p_se) : EditMenusCEB(p_se, "Breakpoints") {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
_popup_move_item(EDIT_DUPLICATE_LINES, edit_menu->get_popup());
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
@@ -199,14 +163,6 @@ ScriptTextEditor::EditMenusSTE::EditMenusSTE() {
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/show_tooltip"), SHOW_TOOLTIP_AT_CARET);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
-
- breakpoints_menu = memnew(PopupMenu);
- goto_menu->get_popup()->add_submenu_node_item(TTRC("Breakpoints"), breakpoints_menu);
- breakpoints_menu->connect("about_to_popup", callable_mp(this, &EditMenusSTE::_update_breakpoint_list));
- breakpoints_menu->connect("index_pressed", callable_mp(this, &EditMenusSTE::_breakpoint_item_pressed));
-
- // Update immediately for shortcuts.
- _update_breakpoint_list();
}
////////////////////////////////////////////////////////////////////////////////
@@ -377,10 +333,6 @@ void ScriptTextEditor::_show_errors_panel(bool p_show) {
errors_panel->set_visible(p_show);
}
-void ScriptTextEditor::_show_warnings_panel(bool p_show) {
- warnings_panel->set_visible(p_show);
-}
-
bool ScriptTextEditor::_warning_clicked(const Variant &p_line) {
if (CodeEditorBase::_warning_clicked(p_line)) {
return true;
@@ -771,7 +723,7 @@ Variant ScriptTextEditor::get_edit_state() {
return TextEditorBase::get_edit_state();
}
-void ScriptTextEditor::set_edit_state(const Variant &p_state) {
+void ScriptTextEditor::set_edit_state(const Variant &p_state, bool p_grab_focus) {
if (editor_enabled) {
code_editor->set_edit_state(p_state);
} else {
@@ -792,7 +744,7 @@ void ScriptTextEditor::set_edit_state(const Variant &p_state) {
}
}
- if (editor_enabled) {
+ if (editor_enabled && p_grab_focus) {
#ifndef ANDROID_ENABLED
ensure_focus();
#endif
@@ -878,7 +830,7 @@ void ScriptTextEditor::_validate_script() {
const String error_text = vformat(TTR("Error at ([hint=Line %d, column %d]%d, %d[/hint]):"), display_pos.x, display_pos.y, display_pos.x, display_pos.y) + " " + message;
code_editor->set_error(error_text);
}
- script_is_valid = false;
+ validation_success = false;
} else {
code_editor->set_error("");
if (!script->is_tool()) {
@@ -891,7 +843,7 @@ void ScriptTextEditor::_validate_script() {
for (const String &E : fnc) {
functions.push_back(E);
}
- script_is_valid = true;
+ validation_success = true;
}
_update_connected_methods();
_update_warnings();
@@ -1143,11 +1095,6 @@ void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_fo
}
}
-void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
- ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
- ste->_code_complete_script(p_code, r_options, r_force);
-}
-
void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
if (color_panel->is_visible()) {
return;
@@ -1485,7 +1432,7 @@ void ScriptTextEditor::_update_connected_methods() {
}
missing_connections.clear();
- if (!script_is_valid) {
+ if (!validation_success) {
return;
}
@@ -1688,6 +1635,18 @@ bool ScriptTextEditor::_edit_option(int p_op) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
+ switch (p_op) {
+ case EDIT_AUTO_INDENT:
+ case EDIT_EVALUATE:
+ case EDIT_CREATE_CODE_REGION:
+ case SHOW_TOOLTIP_AT_CARET:
+ case LOOKUP_SYMBOL:
+ case EDIT_COMPLETE:
+ case EDIT_TOGGLE_COMMENT: {
+ callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
+ }
+ }
+
switch (p_op) {
case EDIT_CREATE_CODE_REGION: {
tx->create_code_region();
@@ -1767,82 +1726,6 @@ bool ScriptTextEditor::_edit_option(int p_op) {
case SEARCH_LOCATE_FUNCTION: {
quick_open->popup_dialog(get_functions());
} break;
- case DEBUG_TOGGLE_BREAKPOINT: {
- Vector<int> sorted_carets = tx->get_sorted_carets();
- int last_line = -1;
- for (const int &c : sorted_carets) {
- int from = tx->get_selection_from_line(c);
- from += from == last_line ? 1 : 0;
- int to = tx->get_selection_to_line(c);
- if (to < from) {
- continue;
- }
- // Check first if there's any lines with breakpoints in the selection.
- bool selection_has_breakpoints = false;
- for (int line = from; line <= to; line++) {
- if (tx->is_line_breakpointed(line)) {
- selection_has_breakpoints = true;
- break;
- }
- }
-
- // Set breakpoint on caret or remove all bookmarks from the selection.
- if (!selection_has_breakpoints) {
- if (tx->get_caret_line(c) != last_line) {
- tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
- }
- } else {
- for (int line = from; line <= to; line++) {
- tx->set_line_as_breakpoint(line, false);
- }
- }
- last_line = to;
- }
- } break;
- case DEBUG_REMOVE_ALL_BREAKPOINTS: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
-
- for (int i = 0; i < bpoints.size(); i++) {
- int line = bpoints[i];
- bool dobreak = !tx->is_line_breakpointed(line);
- tx->set_line_as_breakpoint(line, dobreak);
- EditorDebuggerNode::get_singleton()->set_breakpoint(edited_res->get_path(), line + 1, dobreak);
- }
- } break;
- case DEBUG_GOTO_NEXT_BREAKPOINT: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
- if (bpoints.is_empty()) {
- return true;
- }
-
- int current_line = tx->get_caret_line();
- int bpoint_idx = 0;
- if (current_line < (int)bpoints[bpoints.size() - 1]) {
- while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
- bpoint_idx++;
- }
- }
- ScriptEditorNavigationMarker::get_singleton()->locate_begin();
- code_editor->goto_line_centered(bpoints[bpoint_idx]);
- ScriptEditorNavigationMarker::get_singleton()->locate_end();
- } break;
- case DEBUG_GOTO_PREV_BREAKPOINT: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
- if (bpoints.is_empty()) {
- return true;
- }
-
- int current_line = tx->get_caret_line();
- int bpoint_idx = bpoints.size() - 1;
- if (current_line > (int)bpoints[0]) {
- while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
- bpoint_idx--;
- }
- }
- ScriptEditorNavigationMarker::get_singleton()->locate_begin();
- code_editor->goto_line_centered(bpoints[bpoint_idx]);
- ScriptEditorNavigationMarker::get_singleton()->locate_end();
- } break;
case SHOW_TOOLTIP_AT_CARET: {
_show_symbol_tooltip(tx->get_word_under_caret(), tx->get_caret_line(), tx->get_caret_column(), true);
} break;
@@ -1865,7 +1748,7 @@ bool ScriptTextEditor::_edit_option(int p_op) {
}
} break;
default: {
- if (TextEditorBase::_edit_option(p_op)) {
+ if (CodeEditorBase::_edit_option(p_op)) {
return true;
}
if (p_op >= EditorContextMenuPlugin::BASE_ID) {
@@ -1925,21 +1808,10 @@ void ScriptTextEditor::_notification(int p_what) {
}
}
-Control *ScriptTextEditor::get_edit_menu() {
- if (!edit_menus) {
- edit_menus = memnew(EditMenusSTE);
- }
- return edit_menus;
-}
-
PackedInt32Array ScriptTextEditor::get_breakpoints() {
return code_editor->get_text_editor()->get_breakpointed_lines();
}
-void ScriptTextEditor::set_breakpoint(int p_line, bool p_enabled) {
- code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
-}
-
void ScriptTextEditor::clear_breakpoints() {
code_editor->get_text_editor()->clear_breakpointed_lines();
}
@@ -2567,7 +2439,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
color_position.z = end;
}
}
- _make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);
+ _make_ste_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);
}
}
@@ -2589,10 +2461,8 @@ void ScriptTextEditor::_color_changed(const Color &p_color) {
code_editor->get_text_editor()->end_complex_operation();
}
-void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, const Vector2 &p_position) {
- TextEditorBase::_make_context_menu(p_selection, p_foldable, p_position, false);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
- _popup_move_item(EDIT_UNINDENT, context_menu);
+void ScriptTextEditor::_make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, const Vector2 &p_position) {
+ CodeEditorBase::_make_context_menu(p_selection, p_foldable, p_position, false);
if (p_selection) {
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
@@ -2691,7 +2561,6 @@ void ScriptTextEditor::register_editor() {
void ScriptTextEditor::_enable_code_editor() {
code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel));
- code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel));
code_editor->get_text_editor()->connect("symbol_lookup", callable_mp(this, &ScriptTextEditor::_lookup_symbol));
code_editor->get_text_editor()->connect("symbol_hovered", callable_mp(this, &ScriptTextEditor::_show_symbol_tooltip).bind(false));
code_editor->get_text_editor()->connect("symbol_validate", callable_mp(this, &ScriptTextEditor::_validate_symbol));
@@ -2722,10 +2591,11 @@ void ScriptTextEditor::_enable_code_editor() {
}
ScriptTextEditor::ScriptTextEditor() {
- code_editor->set_code_complete_func(_code_complete_scripts, this);
+ editor_box->add_child(code_editor);
+ editor_box->add_child(warnings_panel);
+
code_editor->get_text_editor()->set_draw_breakpoints_gutter(true);
code_editor->get_text_editor()->set_draw_executing_lines_gutter(true);
- code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled));
code_editor->get_text_editor()->connect("caret_changed", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
code_editor->connect("navigation_preview_ended", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
@@ -2762,7 +2632,6 @@ ScriptTextEditor::ScriptTextEditor() {
code_editor->get_text_editor()->add_child(drag_info_label);
drag_info_label->hide();
- code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true);
color_panel = memnew(PopupPanel);
diff --git a/editor/script/script_text_editor.h b/editor/script/script_text_editor.h
index 58d866db9c6..0328b2cba81 100644
--- a/editor/script/script_text_editor.h
+++ b/editor/script/script_text_editor.h
@@ -58,7 +58,6 @@ class ScriptTextEditor : public CodeEditorBase {
GDCLASS(ScriptTextEditor, CodeEditorBase);
Variant pending_state;
- bool script_is_valid = false;
RichTextLabel *errors_panel = nullptr;
Label *drag_info_label = nullptr;
@@ -109,11 +108,6 @@ class ScriptTextEditor : public CodeEditorBase {
SEARCH_LOCATE_FUNCTION,
- DEBUG_TOGGLE_BREAKPOINT,
- DEBUG_REMOVE_ALL_BREAKPOINTS,
- DEBUG_GOTO_NEXT_BREAKPOINT,
- DEBUG_GOTO_PREV_BREAKPOINT,
-
SHOW_TOOLTIP_AT_CARET,
HELP_CONTEXTUAL,
LOOKUP_SYMBOL,
@@ -129,15 +123,16 @@ class ScriptTextEditor : public CodeEditorBase {
MODE_MAX
};
- class EditMenusSTE : public EditMenusCEB {
- GDCLASS(EditMenusSTE, EditMenusCEB);
- PopupMenu *breakpoints_menu = nullptr;
+ class EditMenusScTE : public EditMenusCEB {
+ GDCLASS(EditMenusScTE, EditMenusCEB);
- void _update_breakpoint_list();
- void _breakpoint_item_pressed(int p_idx);
+ protected:
+ void _update_breakpoint_list() override;
public:
- EditMenusSTE();
+ virtual bool handles(ScriptEditorBase *p_seb) override { return Object::cast_to<ScriptTextEditor>(p_seb); }
+
+ EditMenusScTE(ScriptEditor *p_se);
};
void _enable_code_editor();
@@ -157,19 +152,17 @@ class ScriptTextEditor : public CodeEditorBase {
static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);
protected:
- void _breakpoint_toggled(int p_row);
+ virtual void _breakpoint_toggled(int p_row) override;
void _on_caret_moved();
void _update_warnings();
void _update_errors();
- static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) override;
void _set_theme_for_script();
void _show_errors_panel(bool p_show);
- void _show_warnings_panel(bool p_show);
void _error_clicked(const Variant &p_line);
virtual bool _warning_clicked(const Variant &p_line) override;
void _on_mouse_exited();
@@ -204,7 +197,7 @@ protected:
void _goto_line(int p_line);
- void _make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, const Vector2 &p_pos);
+ void _make_ste_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, const Vector2 &p_pos);
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev) override;
virtual bool _edit_option(int p_op) override;
@@ -220,15 +213,14 @@ public:
virtual void enable_editor() override;
virtual Vector<String> get_functions() override;
- virtual Control *get_edit_menu() override;
+ virtual EditMenusBase *create_edit_menu(ScriptEditor *p_se) override { return memnew(EditMenusScTE(p_se)); }
virtual Ref<Texture2D> get_theme_icon() override;
virtual Variant get_edit_state() override;
- virtual void set_edit_state(const Variant &p_state) override;
+ virtual void set_edit_state(const Variant &p_state, bool p_grab_focus = true) override;
virtual PackedInt32Array get_breakpoints() override;
- virtual void set_breakpoint(int p_line, bool p_enabled) override;
virtual void clear_breakpoints() override;
virtual void add_callback(const String &p_function, const PackedStringArray &p_args);
diff --git a/editor/script/syntax_highlighters.cpp b/editor/script/syntax_highlighters.cpp
index 6de912ce1e4..fb0ee0388e2 100644
--- a/editor/script/syntax_highlighters.cpp
+++ b/editor/script/syntax_highlighters.cpp
@@ -34,6 +34,9 @@
#include "core/object/class_db.h"
#include "core/object/script_language.h"
#include "editor/settings/editor_settings.h"
+#include "scene/resources/shader.h"
+#include "servers/rendering/shader_preprocessor.h"
+#include "servers/rendering/shader_types.h"
String EditorSyntaxHighlighter::_get_name() const {
String ret = "Unnamed";
@@ -267,7 +270,7 @@ Ref<EditorSyntaxHighlighter> EditorMarkdownSyntaxHighlighter::_create() const {
return syntax_highlighter;
}
-///
+////
void EditorConfigFileSyntaxHighlighter::_update_cache() {
highlighter->set_text_edit(text_edit);
@@ -306,3 +309,153 @@ Ref<EditorSyntaxHighlighter> EditorConfigFileSyntaxHighlighter::_create() const
syntax_highlighter.instantiate();
return syntax_highlighter;
}
+
+////
+
+void GDShaderSyntaxHighlighter::_update_cache() {
+ highlighter->set_text_edit(text_edit);
+ highlighter->clear_keyword_colors();
+ highlighter->clear_member_keyword_colors();
+ highlighter->clear_color_regions();
+
+ highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
+ highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
+ highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));
+ highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));
+
+ const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
+ const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
+
+ List<String> keywords;
+ ShaderLanguage::get_keyword_list(&keywords);
+
+ for (const String &E : keywords) {
+ if (ShaderLanguage::is_control_flow_keyword(E)) {
+ highlighter->add_keyword_color(E, control_flow_keyword_color);
+ } else {
+ highlighter->add_keyword_color(E, keyword_color);
+ }
+ }
+
+ List<String> pp_keywords;
+ ShaderPreprocessor::get_keyword_list(&pp_keywords, false);
+
+ for (const String &E : pp_keywords) {
+ highlighter->add_keyword_color(E, control_flow_keyword_color);
+ }
+
+ // Colorize comments.
+ const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
+ highlighter->add_color_region("/*", "*/", comment_color, false);
+ highlighter->add_color_region("//", "", comment_color, true);
+
+ const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
+ highlighter->add_color_region("/**", "*/", doc_comment_color, false);
+ // "/**/" will be treated as the start of the "/**" region, this line is guaranteed to end the color_region.
+ highlighter->add_color_region("/**/", "", comment_color, true);
+
+ // Disabled preprocessor branches use translucent text color to be easier to distinguish from comments.
+ set_disabled_branch_color(Color(EDITOR_GET("text_editor/theme/highlighting/text_color")) * Color(1, 1, 1, 0.5));
+
+ // Colorize preprocessor include strings.
+ const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
+ highlighter->add_color_region("\"", "\"", string_color, false);
+ highlighter->set_uint_suffix_enabled(true);
+
+ // Colorize built-ins like `COLOR` differently to make them easier
+ // to distinguish from keywords at a quick glance.
+
+ Ref<ShaderInclude> shader_inc = _get_edited_resource();
+ Ref<Shader> shader = _get_edited_resource();
+
+ LocalVector<Vector<ShaderLanguage::ModeInfo>> modes_list;
+ LocalVector<String> built_ins;
+
+ ShaderTypes *st = ShaderTypes::get_singleton();
+ if (shader_inc.is_valid() || shader.is_valid()) {
+ for (int i = 0; i < RSE::SHADER_MAX; i++) {
+ if (shader_inc.is_null() && shader->get_mode() != i) {
+ continue;
+ }
+
+ for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : st->get_functions(RSE::ShaderMode(i))) {
+ for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {
+ built_ins.push_back(F.key);
+ }
+ }
+ modes_list.push_back(st->get_modes(RSE::ShaderMode(i)));
+ modes_list.push_back(st->get_stencil_modes(RSE::ShaderMode(i)));
+ }
+ }
+
+ for (const Vector<ShaderLanguage::ModeInfo> &modes : modes_list) {
+ for (const ShaderLanguage::ModeInfo &mode_info : modes) {
+ if (!mode_info.options.is_empty()) {
+ for (const StringName &option : mode_info.options) {
+ built_ins.push_back(String(mode_info.name) + "_" + String(option));
+ }
+ } else {
+ built_ins.push_back(String(mode_info.name));
+ }
+ }
+ }
+
+ const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
+
+ for (const String &E : built_ins) {
+ highlighter->add_keyword_color(E, user_type_color);
+ }
+}
+
+Dictionary GDShaderSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
+ Dictionary color_map;
+
+ for (const Point2i &region : disabled_branch_regions) {
+ if (p_line >= region.x && p_line <= region.y) {
+ // When "color_regions[0].p_start_key.length() > 2",
+ // disabled_branch_region causes color_region to break.
+ // This should be seen as a temporary solution.
+ highlighter->get_line_syntax_highlighting(p_line);
+
+ Dictionary highlighter_info;
+ highlighter_info["color"] = disabled_branch_color;
+
+ color_map[0] = highlighter_info;
+ return color_map;
+ }
+ }
+
+ return highlighter->get_line_syntax_highlighting(p_line);
+}
+
+void GDShaderSyntaxHighlighter::add_disabled_branch_region(const Point2i &p_region) {
+ ERR_FAIL_COND(p_region.x < 0);
+ ERR_FAIL_COND(p_region.y < 0);
+
+ for (const Point2i &disabled_branch_region : disabled_branch_regions) {
+ ERR_FAIL_COND_MSG(disabled_branch_region.x == p_region.x, "Branch region with a start line '" + itos(p_region.x) + "' already exists.");
+ }
+
+ Point2i disabled_branch_region;
+ disabled_branch_region.x = p_region.x;
+ disabled_branch_region.y = p_region.y;
+ disabled_branch_regions.push_back(disabled_branch_region);
+
+ clear_highlighting_cache();
+}
+
+void GDShaderSyntaxHighlighter::clear_disabled_branch_regions() {
+ disabled_branch_regions.clear();
+ clear_highlighting_cache();
+}
+
+void GDShaderSyntaxHighlighter::set_disabled_branch_color(const Color &p_color) {
+ disabled_branch_color = p_color;
+ clear_highlighting_cache();
+}
+
+Ref<EditorSyntaxHighlighter> GDShaderSyntaxHighlighter::_create() const {
+ Ref<GDShaderSyntaxHighlighter> syntax_highlighter;
+ syntax_highlighter.instantiate();
+ return syntax_highlighter;
+}
diff --git a/editor/script/syntax_highlighters.h b/editor/script/syntax_highlighters.h
index 41c33cceffa..09d2b586389 100644
--- a/editor/script/syntax_highlighters.h
+++ b/editor/script/syntax_highlighters.h
@@ -141,3 +141,28 @@ public:
EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }
};
+
+class GDShaderSyntaxHighlighter : public EditorSyntaxHighlighter {
+ GDCLASS(GDShaderSyntaxHighlighter, EditorSyntaxHighlighter)
+
+private:
+ Ref<CodeHighlighter> highlighter;
+
+ Vector<Point2i> disabled_branch_regions;
+ Color disabled_branch_color;
+
+public:
+ virtual void _update_cache() override;
+ virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
+
+ virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "gdshader", "gdshaderinc" }; }
+ virtual String _get_name() const override { return TTR("GDShader"); }
+
+ virtual Ref<EditorSyntaxHighlighter> _create() const override;
+
+ GDShaderSyntaxHighlighter() { highlighter.instantiate(); }
+
+ void add_disabled_branch_region(const Point2i &p_region);
+ void clear_disabled_branch_regions();
+ void set_disabled_branch_color(const Color &p_color);
+};
diff --git a/editor/script/text_editor.cpp b/editor/script/text_editor.cpp
index 34be3665bfd..3cccf1b0b61 100644
--- a/editor/script/text_editor.cpp
+++ b/editor/script/text_editor.cpp
@@ -72,13 +72,6 @@ ScriptEditorBase *TextEditor::create_editor(const Ref<Resource> &p_resource) {
return nullptr;
}
-Control *TextEditor::get_edit_menu() {
- if (!edit_menus) {
- edit_menus = memnew(EditMenus);
- }
- return edit_menus;
-}
-
void TextEditor::register_editor() {
ScriptEditor::register_create_script_editor_function(create_editor);
}
diff --git a/editor/script/text_editor.h b/editor/script/text_editor.h
index e41792c4c7e..b258e5a07d2 100644
--- a/editor/script/text_editor.h
+++ b/editor/script/text_editor.h
@@ -40,12 +40,21 @@ class TextEditor : public TextEditorBase {
static inline EditMenus *edit_menus = nullptr;
protected:
+ class EditMenusTEB : public EditMenus {
+ GDCLASS(EditMenusTEB, EditMenus);
+
+ public:
+ virtual bool handles(ScriptEditorBase *p_seb) override { return Object::cast_to<TextEditor>(p_seb); }
+
+ EditMenusTEB(ScriptEditor *p_se) : EditMenus(p_se) {}
+ };
+
virtual void _validate_script() override;
public:
virtual void apply_code() override;
- virtual Control *get_edit_menu() override;
+ virtual EditMenusBase *create_edit_menu(ScriptEditor *p_se) override { return memnew(EditMenusTEB(p_se)); }
static void register_editor();
diff --git a/editor/shader/editor_shader_language_plugin.h b/editor/shader/editor_shader_language_plugin.h
index 77a9e275cff..51fb8552051 100644
--- a/editor/shader/editor_shader_language_plugin.h
+++ b/editor/shader/editor_shader_language_plugin.h
@@ -30,7 +30,7 @@
#pragma once
-#include "editor/shader/shader_editor.h"
+#include "scene/resources/shader.h"
class EditorShaderLanguagePlugin : public RefCounted {
GDCLASS(EditorShaderLanguagePlugin, RefCounted);
@@ -46,10 +46,6 @@ public:
static Ref<EditorShaderLanguagePlugin> get_shader_language_for_index(int p_index);
static String get_file_extension_for_index(int p_index);
- virtual bool handles_shader(const Ref<Shader> &p_shader) const = 0;
- virtual bool handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const { return false; }
- virtual ShaderEditor *edit_shader(const Ref<Shader> &p_shader) = 0;
- virtual ShaderEditor *edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) { return nullptr; }
virtual Ref<Shader> create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) = 0;
virtual Ref<ShaderInclude> create_new_shader_include() { return Ref<ShaderInclude>(); }
virtual PackedStringArray get_language_variations() const = 0;
diff --git a/editor/shader/shader_editor.h b/editor/shader/shader_editor.h
deleted file mode 100644
index f77e36019ed..00000000000
--- a/editor/shader/shader_editor.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/**************************************************************************/
-/* shader_editor.h */
-/**************************************************************************/
-/* 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. */
-/**************************************************************************/
-
-#pragma once
-
-#include "scene/gui/margin_container.h"
-#include "scene/resources/shader.h"
-
-class Button;
-class MenuButton;
-
-class ShaderEditor : public MarginContainer {
- GDCLASS(ShaderEditor, MarginContainer);
-
-public:
- virtual void edit_shader(const Ref<Shader> &p_shader) = 0;
- virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {}
- virtual void use_menu_bar(MenuButton *p_file_menu) = 0;
-
- virtual void apply_shaders() = 0;
- virtual bool is_unsaved() const = 0;
- virtual void save_external_data(const String &p_str = "") = 0;
- virtual void set_toggle_list_control(Control *p_toggle_list_control) = 0;
- virtual void update_toggle_files_button() = 0;
- virtual void validate_script() = 0;
-};
diff --git a/editor/shader/shader_editor_plugin.cpp b/editor/shader/shader_editor_plugin.cpp
index ab02d16243b..d176d6245a7 100644
--- a/editor/shader/shader_editor_plugin.cpp
+++ b/editor/shader/shader_editor_plugin.cpp
@@ -30,184 +30,26 @@
#include "shader_editor_plugin.h"
-#include "core/io/resource_loader.h"
-#include "core/object/callable_mp.h"
+#include "core/variant/variant.h"
+#include "editor/docks/editor_dock.h"
#include "editor/docks/editor_dock_manager.h"
-#include "editor/docks/filesystem_dock.h"
-#include "editor/docks/inspector_dock.h"
#include "editor/editor_node.h"
-#include "editor/editor_string_names.h"
-#include "editor/editor_undo_redo_manager.h"
-#include "editor/gui/window_wrapper.h"
+#include "editor/script/script_editor_base.h"
#include "editor/settings/editor_command_palette.h"
#include "editor/settings/editor_settings.h"
-#include "editor/shader/shader_create_dialog.h"
-#include "editor/shader/text_shader_editor.h"
+#include "editor/shader/editor_shader_language_plugin.h"
#include "editor/shader/text_shader_language_plugin.h"
#include "editor/themes/editor_scale.h"
-#include "scene/gui/item_list.h"
-#include "scene/gui/tab_container.h"
-#include "scene/gui/texture_rect.h"
-#include "servers/display/display_server.h"
+#include "scene/resources/shader.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) {
+void ShaderEditorPlugin::shortcut_input(const Ref<InputEvent> &p_event) {
+ if (p_event.is_null() || !p_event->is_pressed() || p_event->is_echo()) {
return;
}
- EditedShader es;
- // First, check for ShaderInclude.
- ShaderInclude *shader_include = Object::cast_to<ShaderInclude>(p_object);
- if (shader_include != nullptr) {
- // Check if this shader include is already being edited.
- for (uint32_t i = 0; i < edited_shaders.size(); i++) {
- if (edited_shaders[i].shader_inc.ptr() == shader_include) {
- shader_tabs->set_current_tab(i);
- shader_list->select(i);
- _switch_to_editor(edited_shaders[i].shader_editor);
- return;
- }
- }
- es.shader_inc = Ref<ShaderInclude>(shader_include);
- for (Ref<EditorShaderLanguagePlugin> shader_lang : EditorShaderLanguagePlugin::get_shader_languages_read_only()) {
- if (shader_lang->handles_shader_include(es.shader_inc)) {
- es.shader_editor = shader_lang->edit_shader_include(es.shader_inc);
- break;
- }
- }
- } else {
- // If it's not a ShaderInclude, check for Shader.
- Shader *shader = Object::cast_to<Shader>(p_object);
- ERR_FAIL_NULL_MSG(shader, "ShaderEditorPlugin: Unable to edit object " + p_object->to_string() + " because it is not a Shader or ShaderInclude.");
- // Check if this shader is already being edited.
- for (uint32_t i = 0; i < edited_shaders.size(); i++) {
- if (edited_shaders[i].shader.ptr() == shader) {
- shader_tabs->set_current_tab(i);
- shader_list->select(i);
- _switch_to_editor(edited_shaders[i].shader_editor);
- return;
- }
- }
- // If we did not return, the shader needs to be opened in a new shader editor.
- es.shader = Ref<Shader>(shader);
- for (Ref<EditorShaderLanguagePlugin> shader_lang : EditorShaderLanguagePlugin::get_shader_languages_read_only()) {
- if (shader_lang->handles_shader(es.shader)) {
- es.shader_editor = shader_lang->edit_shader(es.shader);
- break;
- }
- }
- }
- ERR_FAIL_NULL_MSG(es.shader_editor, "ShaderEditorPlugin: Unable to edit shader because no suitable editor was found.");
- // TextShaderEditor-specific setup code.
- 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));
- cte->connect(SceneStringName(visibility_changed), callable_mp(this, &ShaderEditorPlugin::_update_shader_editor_zoom_factor).bind(cte));
- }
+ if (make_floating_shortcut.is_valid() && make_floating_shortcut->matches_event(p_event)) {
+ shader_dock->make_floating();
}
-
- // `set_toggle_list_control` must be called before adding the editor to the scene tree.
- es.shader_editor->set_toggle_list_control(shader_list);
- shader_tabs->add_child(es.shader_editor);
- shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
- edited_shaders.push_back(es);
- _update_shader_list();
- _switch_to_editor(es.shader_editor);
-}
-
-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) {
@@ -218,651 +60,107 @@ void ShaderEditorPlugin::make_visible(bool p_visible) {
void ShaderEditorPlugin::set_current() {
shader_dock->make_visible();
- TextShaderEditor *text_shader_editor = Object::cast_to<TextShaderEditor>(shader_tabs->get_current_tab_control());
+ TextEditorBase *text_shader_editor = Object::cast_to<TextEditorBase>(script_editor->get_current_editor());
if (text_shader_editor) {
- text_shader_editor->get_code_editor()->get_text_editor()->grab_focus();
- }
-}
-
-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;
- }
+ text_shader_editor->ensure_focus();
}
- return nullptr;
}
-void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
- if (!bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
+void ShaderEditorPlugin::edit(Object *p_object) {
+ if (!p_object) {
return;
}
- if (!p_layout->has_section("ShaderEditor")) {
- return;
+ if (Object::cast_to<Shader>(p_object) || Object::cast_to<ShaderInclude>(p_object)) {
+ script_editor->edit(Object::cast_to<Resource>(p_object), false);
}
+}
- if (!p_layout->has_section_key("ShaderEditor", "open_shaders") ||
- !p_layout->has_section_key("ShaderEditor", "selected_shader")) {
- return;
- }
+bool ShaderEditorPlugin::handles(Object *p_object) const {
+ return Object::cast_to<Shader>(p_object) || Object::cast_to<ShaderInclude>(p_object);
+}
- 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;
+void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
+#ifndef DISABLE_DEPRECATED
+ for (int i = 0; i < old_layout_keys.size(); i++) {
+ if (new_layout_keys[i] != "" && p_layout->has_section_key(config_section, old_layout_keys[i])) {
+ p_layout->set_value(config_section, new_layout_keys[i], p_layout->get_value(config_section, old_layout_keys[i]));
}
}
+#endif
- if (p_layout->has_section_key("ShaderEditor", "split_offset")) {
- files_split->set_split_offset(p_layout->get_value("ShaderEditor", "split_offset"));
+ if (bool(EDITOR_GET("editors/shader_editor/behavior/files/restore_shaders_on_load"))) {
+ script_editor->set_window_layout(p_layout);
}
-
- _update_shader_list();
- _shader_selected(selected_shader_idx, false);
-
- _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) {
#ifndef DISABLE_DEPRECATED
- 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");
+ for (int i = 0; i < old_layout_keys.size(); i++) {
+ if (p_layout->has_section_key(config_section, old_layout_keys[i])) {
+ p_layout->erase_section_key(config_section, old_layout_keys[i]);
+ }
}
#endif
- 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", files_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);
+ script_editor->get_window_layout(p_layout);
}
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("(*)"));
- }
- }
+ const PackedStringArray unsaved_scripts = script_editor->get_unsaved_scripts();
+ if (unsaved_scripts.is_empty()) {
+ return String();
}
+ PackedStringArray message;
if (!p_for_scene.is_empty()) {
- PackedStringArray unsaved_built_in_shaders;
+ PackedStringArray unsaved_built_in_scripts;
const String scene_file = p_for_scene.get_file();
- for (const String &E : unsaved_shaders) {
+ for (const String &E : unsaved_scripts) {
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);
+ unsaved_built_in_scripts.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->is_unsaved()) {
- 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, bool p_push_item) {
- if (p_index >= (int)edited_shaders.size()) {
- return;
- }
-
- if (edited_shaders[p_index].shader_editor) {
- _switch_to_editor(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);
-
- if (p_push_item) {
- // Avoid `Shader` being edited when editing `ShaderInclude` due to inspector refreshing.
- if (edited_shaders[p_index].shader.is_valid()) {
- EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader.ptr());
+ if (unsaved_built_in_scripts.is_empty()) {
+ return String();
} else {
- EditorNode::get_singleton()->push_item_no_inspector(edited_shaders[p_index].shader_inc.ptr());
- }
- }
-}
-
-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_GET_SHORTCUT("shader_editor/new"), FILE_MENU_NEW);
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/new_include"), FILE_MENU_NEW_INCLUDE);
- p_menu->add_separator();
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/open"), FILE_MENU_OPEN);
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/open_include"), FILE_MENU_OPEN_INCLUDE);
- }
-
- if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_MENU_SAVE);
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_MENU_SAVE_AS);
- }
-
- if (p_type == FILE) {
- p_menu->add_separator();
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/open_in_inspector"), FILE_MENU_INSPECT);
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/inspect_native_code"), FILE_MENU_INSPECT_NATIVE_SHADER_CODE);
- p_menu->add_separator();
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_MENU_CLOSE);
- p_menu->add_separator();
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_files_panel"), FILE_MENU_TOGGLE_FILES_PANEL);
- } else {
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_MENU_CLOSE);
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), FILE_MENU_CLOSE_ALL);
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), FILE_MENU_CLOSE_OTHER_TABS);
- if (p_type == CONTEXT_VALID_ITEM) {
- p_menu->add_separator();
- p_menu->add_shortcut(ED_GET_SHORTCUT("shader_editor/copy_path"), FILE_MENU_COPY_PATH);
- p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), FILE_MENU_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<ShaderEditor>(control) != nullptr;
-
- _setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
-
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
- context_menu->set_item_disabled(context_menu->get_item_index(FILE_MENU_CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
-
- context_menu->set_position(files_split->get_screen_position() + files_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());
- if (file_menu->get_parent() != nullptr) {
- file_menu->get_parent()->remove_child(file_menu);
- }
- ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(p_index));
- ERR_FAIL_NULL(shader_editor);
-
- memdelete(shader_editor);
- edited_shaders.remove_at(p_index);
- _update_shader_list();
- EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
+ message.resize(unsaved_built_in_scripts.size() + 1);
+ message.write[0] = TTR("There are unsaved changes in the following built-in resource(s)");
- if (shader_tabs->get_tab_count() == 0) {
- shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
- shader_tabs->hide();
- files_split->add_child(file_menu);
- file_menu->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
- } else {
- _switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);
- }
-}
-
-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_MENU_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, "Shader");
- shader_create_dialog->popup_centered();
- } break;
- case FILE_MENU_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, "ShaderInclude");
- shader_create_dialog->popup_centered();
- } break;
- case FILE_MENU_OPEN: {
- InspectorDock::get_singleton()->open_resource("Shader");
- } break;
- case FILE_MENU_OPEN_INCLUDE: {
- InspectorDock::get_singleton()->open_resource("ShaderInclude");
- } break;
- case FILE_MENU_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_MENU_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_MENU_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_MENU_INSPECT_NATIVE_SHADER_CODE: {
- int index = shader_tabs->get_current_tab();
- if (edited_shaders[index].shader.is_valid()) {
- edited_shaders[index].shader->inspect_native_shader_code();
- }
- } break;
- case FILE_MENU_CLOSE: {
- _close_shader(shader_tabs->get_current_tab());
- } break;
- case FILE_MENU_CLOSE_ALL: {
- while (shader_tabs->get_tab_count() > 0) {
- _close_shader(0);
- }
- } break;
- case FILE_MENU_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 FILE_MENU_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 FILE_MENU_COPY_PATH: {
- Ref<Resource> shader = _get_current_shader();
- DisplayServer::get_singleton()->clipboard_set(shader->get_path());
- } break;
- case FILE_MENU_TOGGLE_FILES_PANEL: {
- shader_list->set_visible(!shader_list->is_visible());
-
- int index = shader_tabs->get_current_tab();
- if (index != -1) {
- ERR_FAIL_INDEX(index, (int)edited_shaders.size());
- ShaderEditor *shader_editor = edited_shaders[index].shader_editor;
- ERR_FAIL_NULL(shader_editor);
- shader_editor->update_toggle_files_button();
- }
- } 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 = 0;
- if (p_point == Vector2(Math::INF, Math::INF)) {
- if (shader_list->is_anything_selected()) {
- idx = shader_list->get_selected_items()[0];
- }
- } else {
- 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_valid()) {
- 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));
- label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names.
- drag_preview->add_child(label);
- files_split->set_drag_preview(drag_preview);
-
- Dictionary drag_data;
- drag_data["type"] = "shader_list_element";
- drag_data["shader_list_element"] = idx;
-
- Ref<Resource> shader = edited_shaders[idx].shader;
- if (shader.is_null()) {
- shader = edited_shaders[idx].shader_inc;
- }
- drag_data["file_path"] = shader->get_path();
-
- 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.is_empty()) {
- 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 = 0;
- if (p_point == Vector2(Math::INF, Math::INF)) {
- if (shader_list->is_anything_selected()) {
- new_idx = shader_list->get_selected_items()[0];
+ int i = 1;
+ for (const String &E : unsaved_built_in_scripts) {
+ message.write[i] = E.trim_suffix("(*)");
+ i++;
}
- } else {
- new_idx = shader_list->get_item_at_position(p_point);
+ return String("\n").join(message);
}
- _move_shader_tab(idx, new_idx);
- return;
}
- if (String(d["type"]) == "files") {
- Vector<String> files = d["files"];
+ message.resize(unsaved_scripts.size() + 1);
+ message.write[0] = TTR("Save changes to the following file(s) before quitting?");
- 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::_set_text_shader_zoom_factor(float p_zoom_factor) {
- if (text_shader_zoom_factor == p_zoom_factor) {
- return;
- }
-
- text_shader_zoom_factor = p_zoom_factor;
-}
-
-void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const {
- if (p_shader_editor && p_shader_editor->is_visible_in_tree() && text_shader_zoom_factor != p_shader_editor->get_zoom_factor()) {
- p_shader_editor->set_zoom_factor(text_shader_zoom_factor);
- }
-}
-
-void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
- ERR_FAIL_NULL(p_editor);
- if (file_menu->get_parent() != nullptr) {
- file_menu->get_parent()->remove_child(file_menu);
- }
-
- shader_tabs->show();
- p_editor->use_menu_bar(file_menu);
- file_menu->set_v_size_flags(Control::SIZE_EXPAND_FILL);
-}
-
-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_MENU_SAVE), p_disabled);
- file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_SAVE_AS), p_disabled);
- file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT), p_disabled);
- file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_INSPECT_NATIVE_SHADER_CODE), p_disabled);
- file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_MENU_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));
- EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
- } break;
+ int i = 1;
+ for (const String &E : unsaved_scripts) {
+ message.write[i] = E.trim_suffix("(*)");
+ i++;
}
+ return String("\n").join(message);
}
-void ShaderEditorPlugin::shortcut_input(const Ref<InputEvent> &p_event) {
- if (p_event.is_null() || !p_event->is_pressed() || p_event->is_echo()) {
- return;
- }
-
- if (make_floating_shortcut.is_valid() && make_floating_shortcut->matches_event(p_event)) {
- shader_dock->make_floating();
+void ShaderEditorPlugin::save_external_data() {
+ if (!EditorNode::get_singleton()->is_exiting()) {
+ script_editor->save_all_scripts();
}
}
ShaderEditorPlugin::ShaderEditorPlugin() {
shader_editor_plugin = this;
- ED_SHORTCUT("shader_editor/new", TTRC("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N);
- ED_SHORTCUT("shader_editor/new_include", TTRC("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N);
- ED_SHORTCUT("shader_editor/open", TTRC("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O);
- ED_SHORTCUT("shader_editor/open_include", TTRC("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O);
- ED_SHORTCUT("shader_editor/open_in_inspector", TTRC("Open File in Inspector"));
- ED_SHORTCUT("shader_editor/inspect_native_code", TTRC("Inspect Native Shader Code..."));
- ED_SHORTCUT("shader_editor/copy_path", TTRC("Copy Shader Path"));
+ Ref<TextShaderLanguagePlugin> text_shader_lang;
+ text_shader_lang.instantiate();
+ EditorShaderLanguagePlugin::register_shader_language(text_shader_lang);
shader_dock = memnew(EditorDock);
shader_dock->set_name(TTRC("Shader Editor"));
@@ -877,61 +175,11 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("shader_editor/make_floating", TTRC("Make Floating"));
- files_split = memnew(HSplitContainer);
- files_split->set_split_offset(200 * EDSCALE);
- files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
- files_split->set_drag_nested_intersections(true);
- shader_dock->add_child(files_split);
-
- context_menu = memnew(PopupMenu);
- context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
- add_child(context_menu);
-
- shader_list = memnew(ItemList);
- shader_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
- shader_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
- shader_list->set_theme_type_variation("ItemListSecondary");
- shader_list->set_custom_minimum_size(Size2(100, 60) * EDSCALE);
- shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected).bind(true));
- 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);
- files_split->add_child(shader_list);
-
- shader_tabs = memnew(TabContainer);
- shader_tabs->set_tabs_visible(false);
- shader_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- shader_tabs->set_v_size_flags(Control::SIZE_EXPAND_FILL);
- Ref<StyleBoxEmpty> empty;
- empty.instantiate();
- shader_tabs->add_theme_style_override(SceneStringName(panel), empty);
- shader_tabs->hide();
- files_split->add_child(shader_tabs);
-
- file_menu = memnew(MenuButton);
- file_menu->set_flat(false);
- file_menu->set_theme_type_variation("FlatMenuButton");
- file_menu->set_text(TTRC("File"));
- file_menu->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
- file_menu->set_v_size_flags(Control::SIZE_SHRINK_BEGIN);
- file_menu->set_switch_on_hover(true);
- file_menu->set_shortcut_context(files_split);
- file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
- _setup_popup_menu(FILE, file_menu->get_popup());
- _set_file_specific_items_disabled(true);
- files_split->add_child(file_menu);
-
- shader_create_dialog = memnew(ShaderCreateDialog);
- 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));
- shader_dock->add_child(shader_create_dialog);
-
- Ref<TextShaderLanguagePlugin> text_shader_lang;
- text_shader_lang.instantiate();
- EditorShaderLanguagePlugin::register_shader_language(text_shader_lang);
+ script_editor = memnew(ScriptEditor(config_section, "shader_editor_cache.cfg", nullptr, shader_dock));
+ script_editor->set_handled_resource_types({ "Shader", "VisualShader", "ShaderInclude" });
+ shader_dock->add_child(script_editor);
}
ShaderEditorPlugin::~ShaderEditorPlugin() {
EditorShaderLanguagePlugin::clear_registered_shader_languages();
- memdelete(file_menu);
}
diff --git a/editor/shader/shader_editor_plugin.h b/editor/shader/shader_editor_plugin.h
index 18c0772efbb..f1946c8325d 100644
--- a/editor/shader/shader_editor_plugin.h
+++ b/editor/shader/shader_editor_plugin.h
@@ -30,104 +30,32 @@
#pragma once
-#include "editor/plugins/editor_plugin.h"
-#include "scene/resources/shader.h"
-
-class CodeTextEditor;
-class EditorDock;
-class HSplitContainer;
-class ItemList;
-class MenuButton;
-class ShaderCreateDialog;
-class ShaderEditor;
-class TabContainer;
-class VBoxContainer;
-class HBoxContainer;
-class WindowWrapper;
+#include "editor/script/script_editor_plugin.h"
class ShaderEditorPlugin : public EditorPlugin {
GDCLASS(ShaderEditorPlugin, EditorPlugin);
- struct EditedShader {
- Ref<Shader> shader;
- Ref<ShaderInclude> shader_inc;
- ShaderEditor *shader_editor = nullptr;
- String path;
- String name;
- };
-
- LocalVector<EditedShader> edited_shaders;
-
- enum FileMenu {
- FILE_MENU_NEW,
- FILE_MENU_NEW_INCLUDE,
- FILE_MENU_OPEN,
- FILE_MENU_OPEN_INCLUDE,
- FILE_MENU_SAVE,
- FILE_MENU_SAVE_AS,
- FILE_MENU_INSPECT,
- FILE_MENU_INSPECT_NATIVE_SHADER_CODE,
- FILE_MENU_CLOSE,
- FILE_MENU_CLOSE_ALL,
- FILE_MENU_CLOSE_OTHER_TABS,
- FILE_MENU_SHOW_IN_FILE_SYSTEM,
- FILE_MENU_COPY_PATH,
- FILE_MENU_TOGGLE_FILES_PANEL,
- };
-
- enum PopupMenuType {
- FILE,
- CONTEXT,
- CONTEXT_VALID_ITEM,
- };
- HSplitContainer *files_split = nullptr;
-
- ItemList *shader_list = nullptr;
- TabContainer *shader_tabs = nullptr;
-
- MenuButton *file_menu = nullptr;
- PopupMenu *context_menu = nullptr;
+ ScriptEditor *script_editor = nullptr;
EditorDock *shader_dock = nullptr;
Ref<Shortcut> make_floating_shortcut;
- ShaderCreateDialog *shader_create_dialog = nullptr;
+ const String config_section = "ShaderEditor";
- float text_shader_zoom_factor = 1.0f;
-
- Ref<Resource> _get_current_shader();
- void _update_shader_list();
- void _shader_selected(int p_index, bool p_push_item = true);
- void _shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
- void _setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu);
- void _make_script_list_context_menu();
- void _menu_item_pressed(int p_index);
- void _resource_saved(Object *obj);
- void _close_shader(int p_index);
- void _close_builtin_shaders_from_scene(const String &p_scene);
- void _file_removed(const String &p_removed_file);
- void _res_saved_callback(const Ref<Resource> &p_res);
- void _set_file_specific_items_disabled(bool p_disabled);
-
- void _shader_created(Ref<Shader> p_shader);
- void _shader_include_created(Ref<ShaderInclude> p_shader_inc);
- void _update_shader_list_status();
- void _move_shader_tab(int p_from, int p_to);
-
- Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
- bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
- void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
-
- void _set_text_shader_zoom_factor(float p_zoom_factor);
- void _update_shader_editor_zoom_factor(CodeTextEditor *p_shader_editor) const;
-
- void _switch_to_editor(ShaderEditor *p_editor);
+#ifndef DISABLE_DEPRECATED
+ const PackedStringArray old_layout_keys = {
+ "window_rect", "window_screen", "window_screen_rect",
+ "open_shaders", "split_offset", "selected_shader", "text_shader_zoom_factor"
+ };
+ const PackedStringArray new_layout_keys = {
+ "", "", "",
+ "open_scripts", "script_split_offset", "selected_script", "zoom_factor"
+ };
+#endif
static inline ShaderEditorPlugin *shader_editor_plugin = nullptr;
protected:
- void _notification(int p_what);
-
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
public:
@@ -139,14 +67,12 @@ public:
virtual void make_visible(bool p_visible) override;
virtual void set_current() override;
- ShaderEditor *get_shader_editor(const Ref<Shader> &p_for_shader);
-
virtual void set_window_layout(Ref<ConfigFile> p_layout) override;
virtual void get_window_layout(Ref<ConfigFile> p_layout) override;
virtual String get_unsaved_status(const String &p_for_scene) const override;
virtual void save_external_data() override;
- virtual void apply_changes() override;
+ virtual void apply_changes() override { script_editor->apply_scripts(); }
ShaderEditorPlugin();
~ShaderEditorPlugin();
diff --git a/editor/shader/shader_text_editor.cpp b/editor/shader/shader_text_editor.cpp
index 7bff3fc6e4d..423efc87cb6 100644
--- a/editor/shader/shader_text_editor.cpp
+++ b/editor/shader/shader_text_editor.cpp
@@ -31,24 +31,24 @@
#include "shader_text_editor.h"
#include "core/config/project_settings.h"
-#include "core/io/resource_loader.h"
-#include "core/io/resource_saver.h"
#include "core/object/callable_mp.h"
-#include "core/object/class_db.h"
-#include "core/os/os.h"
-#include "core/version_generated.gen.h"
#include "editor/docks/inspector_dock.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/scene/material_editor_plugin.h"
+#include "editor/script/script_editor_base.h"
+#include "editor/script/script_editor_plugin.h"
+#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
-#include "editor/themes/editor_theme_manager.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/gui/panel_container.h"
+#include "scene/gui/popup_menu.h"
+#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
#include "scene/gui/texture_rect.h"
+#include "scene/resources/shader.h"
#include "scene/resources/sky.h"
#include "scene/resources/style_box_flat.h"
#include "servers/display/display_server.h"
@@ -58,55 +58,6 @@
#include "modules/regex/regex.h"
-/*** SHADER SYNTAX HIGHLIGHTER ****/
-
-Dictionary GDShaderSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
- Dictionary color_map;
-
- for (const Point2i &region : disabled_branch_regions) {
- if (p_line >= region.x && p_line <= region.y) {
- // When "color_regions[0].p_start_key.length() > 2",
- // disabled_branch_region causes color_region to break.
- // This should be seen as a temporary solution.
- CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);
-
- Dictionary highlighter_info;
- highlighter_info["color"] = disabled_branch_color;
-
- color_map[0] = highlighter_info;
- return color_map;
- }
- }
-
- return CodeHighlighter::_get_line_syntax_highlighting_impl(p_line);
-}
-
-void GDShaderSyntaxHighlighter::add_disabled_branch_region(const Point2i &p_region) {
- ERR_FAIL_COND(p_region.x < 0);
- ERR_FAIL_COND(p_region.y < 0);
-
- for (int i = 0; i < disabled_branch_regions.size(); i++) {
- ERR_FAIL_COND_MSG(disabled_branch_regions[i].x == p_region.x, "Branch region with a start line '" + itos(p_region.x) + "' already exists.");
- }
-
- Point2i disabled_branch_region;
- disabled_branch_region.x = p_region.x;
- disabled_branch_region.y = p_region.y;
- disabled_branch_regions.push_back(disabled_branch_region);
-
- clear_highlighting_cache();
-}
-
-void GDShaderSyntaxHighlighter::clear_disabled_branch_regions() {
- disabled_branch_regions.clear();
- clear_highlighting_cache();
-}
-
-void GDShaderSyntaxHighlighter::set_disabled_branch_color(const Color &p_color) {
- disabled_branch_color = p_color;
- clear_highlighting_cache();
-}
-
/*** SHADER PREVIEW LINE LAYER ****/
void TextShaderPreviewLineLayer::_notification(int p_what) {
@@ -664,7 +615,7 @@ Ref<ShaderMaterial> TextShaderPreview::_get_source_material() const {
if (mesh.is_valid()) {
for (int i = 0; i < mesh->get_surface_count(); i++) {
- const Ref<ShaderMaterial> surface_material = Object::cast_to<ShaderMaterial>(mi->get_surface_override_material(i).ptr());
+ const Ref<ShaderMaterial> surface_material = mi->get_surface_override_material(i);
if (surface_material.is_valid() && _match_uniforms(surface_material, shader_material)) {
return surface_material;
@@ -781,6 +732,18 @@ void TextShaderPreview::set_shader_code(const String &p_code, int p_line, bool p
/*** SHADER SCRIPT EDITOR ****/
+void ShaderTextEditor::EditMenusShTE::EditMenusShTE::_update_breakpoint_list() {
+ EditMenusCEB::_update_breakpoint_list();
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/goto_previous_shader_preview"), DEBUG_GOTO_PREV_BREAKPOINT);
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/goto_next_shader_preview"), DEBUG_GOTO_NEXT_BREAKPOINT);
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/remove_all_shader_previews"), DEBUG_REMOVE_ALL_BREAKPOINTS);
+ breakpoints_menu->set_item_index(-1, 0);
+ breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/toggle_shader_preview"), DEBUG_TOGGLE_BREAKPOINT);
+ breakpoints_menu->set_item_index(-1, 0);
+}
+
static bool saved_warnings_enabled = false;
static bool saved_treat_warning_as_errors = false;
static HashMap<ShaderWarning::Code, bool> saved_warnings;
@@ -789,8 +752,8 @@ static uint32_t saved_warning_flags = 0U;
void ShaderTextEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
- get_text_editor()->add_theme_color_override("breakpoint_color", EditorNode::get_singleton()->get_editor_theme()->get_color(SceneStringName(font_color), EditorStringName(Editor)));
- get_text_editor()->add_theme_icon_override("breakpoint", get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
+ code_editor->get_text_editor()->add_theme_color_override("breakpoint_color", EditorNode::get_singleton()->get_editor_theme()->get_color(SceneStringName(font_color), EditorStringName(Editor)));
+ code_editor->get_text_editor()->add_theme_icon_override("breakpoint", get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
if (is_visible_in_tree()) {
_load_theme_settings();
@@ -799,122 +762,74 @@ void ShaderTextEditor::_notification(int p_what) {
_update_warning_panel();
}
}
- } break;
- }
-}
-Ref<Shader> ShaderTextEditor::get_edited_shader() const {
- return shader;
-}
-
-Ref<ShaderInclude> ShaderTextEditor::get_edited_shader_include() const {
- return shader_inc;
-}
-
-void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader) {
- set_edited_shader(p_shader, p_shader->get_code());
-}
+ Ref<StyleBoxFlat> tab_style = get_theme_stylebox(SNAME("tab_selected"), "TabBar");
+ Ref<StyleBoxFlat> preview_style = memnew(StyleBoxFlat);
+ preview_style->set_bg_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));
+ preview_style->set_corner_radius_all(tab_style->get_corner_radius(CORNER_TOP_LEFT));
+ preview_panel->add_theme_style_override(SceneStringName(panel), preview_style);
-void ShaderTextEditor::set_edited_shader(const Ref<Shader> &p_shader, const String &p_code) {
- if (shader == p_shader) {
- return;
- }
- if (shader.is_valid()) {
- shader->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
- }
- shader = p_shader;
- shader_inc = Ref<ShaderInclude>();
+ update_params_btn->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
+ remove_all_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
+ } break;
- set_edited_code(p_code);
+ case NOTIFICATION_VISIBILITY_CHANGED: {
+ if (is_visible_in_tree() && preview_timer->is_inside_tree()) {
+ preview_timer->start();
+ }
+ } break;
- if (shader.is_valid()) {
- shader->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
+ case NOTIFICATION_RESIZED: {
+ preview_timer->start();
+ } break;
}
}
-void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
- set_edited_shader_include(p_shader_inc, p_shader_inc->get_code());
-}
-
void ShaderTextEditor::_shader_changed() {
// This function is used for dependencies (include changing changes main shader and forces it to revalidate)
if (block_shader_changed) {
return;
}
- dependencies_version++;
- _validate_script();
-}
-
-void ShaderTextEditor::set_edited_shader_include(const Ref<ShaderInclude> &p_shader_inc, const String &p_code) {
- if (shader_inc == p_shader_inc) {
- return;
- }
- if (shader_inc.is_valid()) {
- shader_inc->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
- }
- shader_inc = p_shader_inc;
- shader = Ref<Shader>();
-
- set_edited_code(p_code);
-
- if (shader_inc.is_valid()) {
- shader_inc->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
- }
-}
-
-void ShaderTextEditor::set_edited_code(const String &p_code) {
- _load_theme_settings();
-
- get_text_editor()->set_text(p_code);
- get_text_editor()->clear_undo_history();
- callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_h_scroll).call_deferred(0);
- callable_mp((TextEdit *)get_text_editor(), &TextEdit::set_v_scroll).call_deferred(0);
- get_text_editor()->tag_saved_version();
-
+ dependencies_changed = true;
_validate_script();
- _line_col_changed();
}
-void ShaderTextEditor::goto_shader_preview(int p_line) {
- goto_line_centered(p_line);
-}
-
-void ShaderTextEditor::clear_previews() {
+void ShaderTextEditor::_clear_previews() {
for (KeyValue<int, TextShaderPreview *> pair : previews) {
pair.value->queue_free();
}
previews.clear();
}
-void ShaderTextEditor::redraw_preview_lines() {
+void ShaderTextEditor::_redraw_preview_lines() {
preview_line_layer->queue_redraw();
}
-void ShaderTextEditor::recompile_previews() {
+void ShaderTextEditor::_recompile_previews() {
for (KeyValue<int, TextShaderPreview *> &E : previews) {
- E.value->recompile(get_text_editor()->get_text());
+ E.value->recompile(code_editor->get_text_editor()->get_text());
}
}
-void ShaderTextEditor::update_parameters() {
+void ShaderTextEditor::_update_parameters() {
for (KeyValue<int, TextShaderPreview *> &E : previews) {
E.value->sync_shader_parameters();
}
}
-TextShaderPreviewLineLayer *ShaderTextEditor::get_preview_line_layer() const {
+TextShaderPreviewLineLayer *ShaderTextEditor::_get_preview_line_layer() const {
return preview_line_layer;
}
-TextShaderPreview *ShaderTextEditor::get_preview(int p_line) const {
+TextShaderPreview *ShaderTextEditor::_get_preview(int p_line) const {
if (previews.has(p_line)) {
return previews[p_line];
}
return nullptr;
}
-void ShaderTextEditor::toggle_shader_preview(int p_line) {
- CodeEdit *tx = get_text_editor();
+void ShaderTextEditor::_toggle_shader_preview(int p_line) {
+ CodeEdit *tx = code_editor->get_text_editor();
TextShaderPreview *preview = memnew(TextShaderPreview);
previews.insert(p_line, preview);
@@ -925,56 +840,21 @@ void ShaderTextEditor::toggle_shader_preview(int p_line) {
preview->set_shader_code(tx->get_text(), p_line, tx->is_in_comment(p_line) != -1);
}
- preview->connect("goto_btn_pressed", callable_mp(this, &ShaderTextEditor::goto_shader_preview).bind(p_line));
- preview->connect("remove_btn_pressed", callable_mp(this, &ShaderTextEditor::remove_shader_preview).bind(p_line));
+ preview->connect("goto_btn_pressed", callable_mp(this, &ShaderTextEditor::goto_line_centered).bind(p_line, 0));
+ preview->connect("remove_btn_pressed", callable_mp(this, &ShaderTextEditor::_remove_shader_preview).bind(p_line));
preview->get_panel_container()->connect(SceneStringName(mouse_entered), callable_mp((CanvasItem *)preview_line_layer, &CanvasItem::queue_redraw));
preview->get_panel_container()->connect(SceneStringName(mouse_exited), callable_mp((CanvasItem *)preview_line_layer, &CanvasItem::queue_redraw));
preview->get_hover_control()->connect(SceneStringName(mouse_entered), callable_mp((CanvasItem *)preview_line_layer, &CanvasItem::queue_redraw));
preview->get_hover_control()->connect(SceneStringName(mouse_exited), callable_mp((CanvasItem *)preview_line_layer, &CanvasItem::queue_redraw));
- preview_box->add_child(preview);
-}
-
-void ShaderTextEditor::remove_shader_preview(int p_line) {
- get_text_editor()->set_line_as_breakpoint(p_line, false);
-}
-
-void ShaderTextEditor::set_preview_box(Control *p_box) {
- preview_box = p_box;
-}
-
-void ShaderTextEditor::reload_text() {
- ERR_FAIL_COND(shader.is_null() && shader_inc.is_null());
-
- String code;
- if (shader.is_valid()) {
- code = shader->get_code();
- } else {
- code = shader_inc->get_code();
- }
-
- CodeEdit *te = get_text_editor();
- int column = te->get_caret_column();
- int row = te->get_caret_line();
- int h = te->get_h_scroll();
- int v = te->get_v_scroll();
-
- te->set_text(code);
- te->set_caret_line(row);
- te->set_caret_column(column);
- te->set_h_scroll(h);
- te->set_v_scroll(v);
-
- te->tag_saved_version();
-
- update_line_and_column();
+ preview_vbox->add_child(preview);
}
-void ShaderTextEditor::set_warnings_panel(RichTextLabel *p_warnings_panel) {
- warnings_panel = p_warnings_panel;
+void ShaderTextEditor::_remove_shader_preview(int p_line) {
+ set_breakpoint(p_line, false);
}
void ShaderTextEditor::_load_theme_settings() {
- CodeEdit *te = get_text_editor();
+ CodeEdit *te = code_editor->get_text_editor();
Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");
if (updated_marked_line_color != marked_line_color) {
for (int i = 0; i < te->get_line_count(); i++) {
@@ -985,131 +865,6 @@ void ShaderTextEditor::_load_theme_settings() {
marked_line_color = updated_marked_line_color;
}
- syntax_highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
- syntax_highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
- syntax_highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));
- syntax_highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));
-
- syntax_highlighter->clear_keyword_colors();
-
- const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
- const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
-
- List<String> keywords;
- ShaderLanguage::get_keyword_list(&keywords);
-
- for (const String &E : keywords) {
- if (ShaderLanguage::is_control_flow_keyword(E)) {
- syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);
- } else {
- syntax_highlighter->add_keyword_color(E, keyword_color);
- }
- }
-
- List<String> pp_keywords;
- ShaderPreprocessor::get_keyword_list(&pp_keywords, false);
-
- for (const String &E : pp_keywords) {
- syntax_highlighter->add_keyword_color(E, control_flow_keyword_color);
- }
-
- // Colorize built-ins like `COLOR` differently to make them easier
- // to distinguish from keywords at a quick glance.
-
- List<String> built_ins;
-
- if (shader_inc.is_valid()) {
- for (int i = 0; i < RSE::SHADER_MAX; i++) {
- for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RSE::ShaderMode(i))) {
- for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {
- built_ins.push_back(F.key);
- }
- }
-
- {
- const Vector<ShaderLanguage::ModeInfo> &render_modes = ShaderTypes::get_singleton()->get_modes(RSE::ShaderMode(i));
-
- for (const ShaderLanguage::ModeInfo &mode_info : render_modes) {
- if (!mode_info.options.is_empty()) {
- for (const StringName &option : mode_info.options) {
- built_ins.push_back(String(mode_info.name) + "_" + String(option));
- }
- } else {
- built_ins.push_back(String(mode_info.name));
- }
- }
- }
-
- {
- const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RSE::ShaderMode(i));
-
- for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {
- if (!mode_info.options.is_empty()) {
- for (const StringName &option : mode_info.options) {
- built_ins.push_back(String(mode_info.name) + "_" + String(option));
- }
- } else {
- built_ins.push_back(String(mode_info.name));
- }
- }
- }
- }
- } else if (shader.is_valid()) {
- for (const KeyValue<StringName, ShaderLanguage::FunctionInfo> &E : ShaderTypes::get_singleton()->get_functions(RSE::ShaderMode(shader->get_mode()))) {
- for (const KeyValue<StringName, ShaderLanguage::BuiltInInfo> &F : E.value.built_ins) {
- built_ins.push_back(F.key);
- }
- }
-
- {
- const Vector<ShaderLanguage::ModeInfo> &shader_modes = ShaderTypes::get_singleton()->get_modes(RSE::ShaderMode(shader->get_mode()));
-
- for (const ShaderLanguage::ModeInfo &mode_info : shader_modes) {
- if (!mode_info.options.is_empty()) {
- for (const StringName &option : mode_info.options) {
- built_ins.push_back(String(mode_info.name) + "_" + String(option));
- }
- } else {
- built_ins.push_back(String(mode_info.name));
- }
- }
- }
-
- {
- const Vector<ShaderLanguage::ModeInfo> &stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RSE::ShaderMode(shader->get_mode()));
-
- for (const ShaderLanguage::ModeInfo &mode_info : stencil_modes) {
- if (!mode_info.options.is_empty()) {
- for (const StringName &option : mode_info.options) {
- built_ins.push_back(String(mode_info.name) + "_" + String(option));
- }
- } else {
- built_ins.push_back(String(mode_info.name));
- }
- }
- }
- }
-
- const Color user_type_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
-
- for (const String &E : built_ins) {
- syntax_highlighter->add_keyword_color(E, user_type_color);
- }
-
- // Colorize comments.
- const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
- syntax_highlighter->clear_color_regions();
- syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
- syntax_highlighter->add_color_region("//", "", comment_color, true);
-
- const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
- syntax_highlighter->add_color_region("/**", "*/", doc_comment_color, false);
- // "/**/" will be treated as the start of the "/**" region, this line is guaranteed to end the color_region.
- syntax_highlighter->add_color_region("/**/", "", comment_color, true);
-
- // Disabled preprocessor branches use translucent text color to be easier to distinguish from comments.
- syntax_highlighter->set_disabled_branch_color(Color(EDITOR_GET("text_editor/theme/highlighting/text_color")) * Color(1, 1, 1, 0.5));
-
te->clear_comment_delimiters();
te->add_comment_delimiter("/*", "*/", false);
te->add_comment_delimiter("//", "", true);
@@ -1117,17 +872,17 @@ void ShaderTextEditor::_load_theme_settings() {
if (!te->has_auto_brace_completion_open_key("/*")) {
te->add_auto_brace_completion_pair("/*", "*/");
}
-
- // Colorize preprocessor include strings.
- const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
- syntax_highlighter->add_color_region("\"", "\"", string_color, false);
- syntax_highlighter->set_uint_suffix_enabled(true);
+ code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
void ShaderTextEditor::_check_shader_mode() {
- String type = ShaderLanguage::get_shader_type(get_text_editor()->get_text());
+ String type = ShaderLanguage::get_shader_type(code_editor->get_text_editor()->get_text());
Shader::Mode mode;
+ Ref<Shader> shader = edited_res;
+ if (shader.is_null()) {
+ return;
+ }
if (type == "canvas_item") {
mode = Shader::MODE_CANVAS_ITEM;
@@ -1144,9 +899,7 @@ void ShaderTextEditor::_check_shader_mode() {
}
if (shader->get_mode() != mode) {
- set_block_shader_changed(true);
- shader->set_code(get_text_editor()->get_text());
- set_block_shader_changed(false);
+ set_code_block_changed(code_editor->get_text_editor()->get_text());
_load_theme_settings();
}
}
@@ -1180,12 +933,12 @@ static void _complete_include_paths(List<ScriptLanguage::CodeCompletionOption> *
_complete_include_paths_search(EditorFileSystem::get_singleton()->get_filesystem(), r_options);
}
-void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {
+void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
List<ScriptLanguage::CodeCompletionOption> pp_options;
List<ScriptLanguage::CodeCompletionOption> pp_defines;
ShaderPreprocessor preprocessor;
String code;
- String resource_path = (shader.is_valid() ? shader->get_path() : shader_inc->get_path());
+ String resource_path = edited_res->get_path();
complete_from_path = resource_path.get_base_dir();
if (!complete_from_path.ends_with("/")) {
complete_from_path += "/";
@@ -1204,24 +957,16 @@ void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLa
ShaderLanguage::ShaderCompileInfo comp_info;
comp_info.global_shader_uniform_type_func = _get_global_shader_uniform_type;
- if (shader.is_null()) {
+ Ref<Shader> shader = edited_res;
+ if (shader.is_valid()) {
+ _check_shader_mode();
+ comp_info.functions = ShaderTypes::get_singleton()->get_functions(RSE::ShaderMode(shader->get_mode()));
+ comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RSE::ShaderMode(shader->get_mode()));
+ comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RSE::ShaderMode(shader->get_mode()));
+ comp_info.shader_types = ShaderTypes::get_singleton()->get_types();
+ } else {
comp_info.is_include = true;
-
- sl.complete(code, comp_info, r_options, calltip);
- if (sl.get_completion_type() == ShaderLanguage::COMPLETION_IDENTIFIER) {
- for (const ScriptLanguage::CodeCompletionOption &E : pp_defines) {
- r_options->push_back(E);
- }
- }
-
- get_text_editor()->set_code_hint(calltip);
- return;
}
- _check_shader_mode();
- comp_info.functions = ShaderTypes::get_singleton()->get_functions(RSE::ShaderMode(shader->get_mode()));
- comp_info.render_modes = ShaderTypes::get_singleton()->get_modes(RSE::ShaderMode(shader->get_mode()));
- comp_info.stencil_modes = ShaderTypes::get_singleton()->get_stencil_modes(RSE::ShaderMode(shader->get_mode()));
- comp_info.shader_types = ShaderTypes::get_singleton()->get_types();
sl.complete(code, comp_info, r_options, calltip);
if (sl.get_completion_type() == ShaderLanguage::COMPLETION_IDENTIFIER) {
@@ -1229,54 +974,118 @@ void ShaderTextEditor::_code_complete_script(const String &p_code, List<ScriptLa
r_options->push_back(E);
}
}
+ code_editor->get_text_editor()->set_code_hint(calltip);
+}
+
+void ShaderTextEditor::_update_warning_panel() {
+ int warning_count = 0;
+
+ warnings_panel->push_table(2);
+ for (const ShaderWarning &w : warnings) {
+ if (warning_count == 0) {
+ if (saved_treat_warning_as_errors) {
+ const String message = (w.get_message() + " " + TTR("Warnings should be fixed to prevent errors.")).replace("[", "[lb]");
+ const String error_text = vformat(TTR("Error at line %d:"), w.get_line()) + " " + message;
+
+ code_editor->set_error(error_text);
+ code_editor->set_error_pos(w.get_line() - 1, 0);
+
+ code_editor->get_text_editor()->set_line_background_color(w.get_line() - 1, marked_line_color);
+ }
+ }
+
+ warning_count++;
+ int line = w.get_line();
+
+ // First cell.
+ warnings_panel->push_cell();
+ warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
+ if (line != -1) {
+ warnings_panel->push_meta(line - 1);
+ warnings_panel->add_text(vformat(TTR("Line %d (%s):"), line, w.get_name()));
+ warnings_panel->pop(); // Meta goto.
+ } else {
+ warnings_panel->add_text(w.get_name() + ":");
+ }
+ warnings_panel->pop(); // Color.
+ warnings_panel->pop(); // Cell.
+
+ // Second cell.
+ warnings_panel->push_cell();
+ warnings_panel->add_text(w.get_message());
+ warnings_panel->pop(); // Cell.
+ }
+ warnings_panel->pop(); // Table.
- get_text_editor()->set_code_hint(calltip);
+ code_editor->set_warning_count(warning_count);
}
-void ShaderTextEditor::_validate_script() {
- emit_signal(CoreStringName(script_changed)); // Ensure to notify that it changed, so it is applied
+bool ShaderTextEditor::_edit_option(int p_option) {
+ CodeEdit *tx = code_editor->get_text_editor();
+ tx->apply_ime();
- String code;
+ switch (p_option) {
+ case EDIT_COMPLETE:
+ case EDIT_TOGGLE_COMMENT: {
+ callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
+ }
+ }
- if (shader.is_valid()) {
- _check_shader_mode();
- code = shader->get_code();
- } else {
- code = shader_inc->get_code();
+ switch (p_option) {
+ case EDIT_TOGGLE_COMMENT: {
+ if (edited_res.is_null()) {
+ return true;
+ }
+ code_editor->toggle_inline_comment("//");
+ } break;
+ case EDIT_COMPLETE: {
+ tx->request_code_completion();
+ } break;
+ default:
+ CodeEditorBase::_edit_option(p_option);
}
+ return true;
+}
+
+void ShaderTextEditor::_validate_script() {
+ apply_code();
+
+ CodeEdit *te = code_editor->get_text_editor();
+ String code = te->get_text();
ShaderPreprocessor preprocessor;
String code_pp;
String error_pp;
List<ShaderPreprocessor::FilePosition> err_positions;
List<ShaderPreprocessor::Region> regions;
- String filename;
+
+ Ref<Shader> shader = edited_res;
+ Ref<ShaderInclude> shader_inc = edited_res;
if (shader.is_valid()) {
- filename = shader->get_path();
- get_text_editor()->set_draw_breakpoints_gutter(true);
+ code_editor->get_text_editor()->set_draw_breakpoints_gutter(true);
} else if (shader_inc.is_valid()) {
- filename = shader_inc->get_path();
- get_text_editor()->set_draw_breakpoints_gutter(false);
+ code_editor->get_text_editor()->set_draw_breakpoints_gutter(false);
}
+ String filename = edited_res->get_path();
last_compile_result = preprocessor.preprocess(code, filename, code_pp, &error_pp, &err_positions, &regions);
- for (int i = 0; i < get_text_editor()->get_line_count(); i++) {
- get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
+ for (int i = 0; i < code_editor->get_text_editor()->get_line_count(); i++) {
+ code_editor->get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
}
- syntax_highlighter->clear_disabled_branch_regions();
- for (const ShaderPreprocessor::Region &region : regions) {
- if (!region.enabled) {
- if (filename != region.file) {
- continue;
+ Ref<GDShaderSyntaxHighlighter> sh = code_editor->get_text_editor()->get_syntax_highlighter();
+ if (sh.is_valid()) {
+ sh->clear_disabled_branch_regions();
+ for (const ShaderPreprocessor::Region &region : regions) {
+ if (!region.enabled && filename == region.file) {
+ sh->add_disabled_branch_region(Point2i(region.from_line, region.to_line));
}
- syntax_highlighter->add_disabled_branch_region(Point2i(region.from_line, region.to_line));
}
+ sh->emit_changed();
}
- syntax_highlighter->emit_changed();
- set_error("");
- set_error_count(0);
+ code_editor->set_error("");
+ code_editor->set_error_count(0);
if (last_compile_result != OK) {
// Preprocessor error.
@@ -1296,18 +1105,18 @@ void ShaderTextEditor::_validate_script() {
const String message = error_pp.replace("[", "[lb]");
err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;
- set_error_count(err_positions.size() - 1);
+ code_editor->set_error_count(err_positions.size() - 1);
}
- set_error(err_text);
- set_error_pos(err_line - 1, 0);
+ code_editor->set_error(err_text);
+ code_editor->set_error_pos(err_line - 1, 0);
- for (int i = 0; i < get_text_editor()->get_line_count(); i++) {
- get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
+ for (int i = 0; i < code_editor->get_text_editor()->get_line_count(); i++) {
+ code_editor->get_text_editor()->set_line_background_color(i, Color(0, 0, 0, 0));
}
- get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
+ code_editor->get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
- set_warning_count(0);
+ code_editor->set_warning_count(0);
for (KeyValue<int, TextShaderPreview *> pair : previews) {
pair.value->show_shader_compile_error();
@@ -1367,7 +1176,7 @@ void ShaderTextEditor::_validate_script() {
const String message = sl.get_error_text().replace("[", "[lb]");
err_text = vformat(TTR("Error at line %d in include %s:%d:"), err_line, inc_file, inc_line) + " " + message;
- set_error_count(include_positions.size() - 1);
+ code_editor->set_error_count(include_positions.size() - 1);
} else {
// Error in the main file.
err_line = sl.get_error_line();
@@ -1375,20 +1184,20 @@ void ShaderTextEditor::_validate_script() {
const String message = sl.get_error_text().replace("[", "[lb]");
err_text = vformat(TTR("Error at line %d:"), err_line) + " " + message;
- set_error_count(0);
+ code_editor->set_error_count(0);
}
- set_error(err_text);
- set_error_pos(err_line - 1, 0);
+ code_editor->set_error(err_text);
+ code_editor->set_error_pos(err_line - 1, 0);
- get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
+ code_editor->get_text_editor()->set_line_background_color(err_line - 1, marked_line_color);
for (KeyValue<int, TextShaderPreview *> pair : previews) {
pair.value->show_shader_compile_error();
}
} else {
for (KeyValue<int, TextShaderPreview *> pair : previews) {
- pair.value->set_shader_code(code, pair.key, get_text_editor()->is_in_comment(pair.key) != -1);
+ pair.value->set_shader_code(code, pair.key, code_editor->get_text_editor()->is_in_comment(pair.key) != -1);
}
}
@@ -1403,624 +1212,157 @@ void ShaderTextEditor::_validate_script() {
warnings.sort_custom<WarningsComparator>();
_update_warning_panel();
} else {
- set_warning_count(0);
+ code_editor->set_warning_count(0);
}
}
- emit_signal(SNAME("script_validated"), last_compile_result == OK); // Notify that validation finished, to update the list of scripts
+ validation_success = last_compile_result == OK;
+
+ TextEditorBase::_validate_script();
}
-void ShaderTextEditor::_update_warning_panel() {
- int warning_count = 0;
+void ShaderTextEditor::_update_warnings(bool p_validate) {
+ bool changed = false;
- warnings_panel->push_table(2);
- for (const ShaderWarning &w : warnings) {
- if (warning_count == 0) {
- if (saved_treat_warning_as_errors) {
- const String message = (w.get_message() + " " + TTR("Warnings should be fixed to prevent errors.")).replace("[", "[lb]");
- const String error_text = vformat(TTR("Error at line %d:"), w.get_line()) + " " + message;
+ bool warnings_enabled = GLOBAL_GET("debug/shader_language/warnings/enable").booleanize();
+ if (warnings_enabled != saved_warnings_enabled) {
+ saved_warnings_enabled = warnings_enabled;
+ changed = true;
+ }
+
+ bool treat_warning_as_errors = GLOBAL_GET("debug/shader_language/warnings/treat_warnings_as_errors").booleanize();
+ if (treat_warning_as_errors != saved_treat_warning_as_errors) {
+ saved_treat_warning_as_errors = treat_warning_as_errors;
+ changed = true;
+ }
- set_error(error_text);
- set_error_pos(w.get_line() - 1, 0);
+ bool update_flags = false;
- get_text_editor()->set_line_background_color(w.get_line() - 1, marked_line_color);
- }
+ for (int i = 0; i < ShaderWarning::WARNING_MAX; i++) {
+ ShaderWarning::Code code = (ShaderWarning::Code)i;
+ bool value = GLOBAL_GET("debug/shader_language/warnings/" + ShaderWarning::get_name_from_code(code).to_lower());
+
+ if (saved_warnings[code] != value) {
+ saved_warnings[code] = value;
+ update_flags = true;
+ changed = true;
}
+ }
- warning_count++;
- int line = w.get_line();
+ if (update_flags) {
+ saved_warning_flags = (uint32_t)ShaderWarning::get_flags_from_codemap(saved_warnings);
+ }
- // First cell.
- warnings_panel->push_cell();
- warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
- if (line != -1) {
- warnings_panel->push_meta(line - 1);
- warnings_panel->add_text(vformat(TTR("Line %d (%s):"), line, w.get_name()));
- warnings_panel->pop(); // Meta goto.
- } else {
- warnings_panel->add_text(w.get_name() + ":");
- }
- warnings_panel->pop(); // Color.
- warnings_panel->pop(); // Cell.
-
- // Second cell.
- warnings_panel->push_cell();
- warnings_panel->add_text(w.get_message());
- warnings_panel->pop(); // Cell.
- }
- warnings_panel->pop(); // Table.
-
- set_warning_count(warning_count);
-}
-
-void ShaderTextEditor::_bind_methods() {
- ADD_SIGNAL(MethodInfo("script_validated", PropertyInfo(Variant::BOOL, "valid")));
-}
-
-ShaderTextEditor::ShaderTextEditor() {
- syntax_highlighter.instantiate();
- get_text_editor()->set_syntax_highlighter(syntax_highlighter);
-
- preview_line_layer = memnew(TextShaderPreviewLineLayer);
- preview_line_layer->set_previews(previews);
- preview_line_layer->set_code_editor(get_text_editor());
- add_child(preview_line_layer);
-
- InspectorDock::get_inspector_singleton()->connect(SNAME("edited_object_changed"), callable_mp(this, &ShaderTextEditor::recompile_previews));
-}
-
-/*** SCRIPT EDITOR ******/
-
-void TextShaderEditor::_menu_option(int p_option) {
- CodeEdit *tx = code_editor->get_text_editor();
- tx->apply_ime();
-
- switch (p_option) {
- case EDIT_UNDO: {
- tx->undo();
- } break;
- case EDIT_REDO: {
- tx->redo();
- } break;
- case EDIT_CUT: {
- tx->cut();
- } break;
- case EDIT_COPY: {
- tx->copy();
- } break;
- case EDIT_PASTE: {
- tx->paste();
- } break;
- case EDIT_SELECT_ALL: {
- tx->select_all();
- } break;
- case EDIT_MOVE_LINE_UP: {
- tx->move_lines_up();
- } break;
- case EDIT_MOVE_LINE_DOWN: {
- tx->move_lines_down();
- } break;
- case EDIT_INDENT: {
- if (shader.is_null() && shader_inc.is_null()) {
- return;
- }
- tx->indent_lines();
- } break;
- case EDIT_UNINDENT: {
- if (shader.is_null() && shader_inc.is_null()) {
- return;
- }
- tx->unindent_lines();
- } break;
- case EDIT_DELETE_LINE: {
- tx->delete_lines();
- } break;
- case EDIT_DUPLICATE_SELECTION: {
- tx->duplicate_selection();
- } break;
- case EDIT_DUPLICATE_LINES: {
- tx->duplicate_lines();
- } break;
- case EDIT_TOGGLE_WORD_WRAP: {
- TextEdit::LineWrappingMode wrap = tx->get_line_wrapping_mode();
- tx->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
- } break;
- case EDIT_TOGGLE_COMMENT: {
- if (shader.is_null() && shader_inc.is_null()) {
- return;
- }
- code_editor->toggle_inline_comment("//");
- } break;
- case EDIT_COMPLETE: {
- tx->request_code_completion();
- } break;
- case SEARCH_FIND: {
- code_editor->get_find_replace_bar()->popup_search();
- } break;
- case SEARCH_FIND_NEXT: {
- code_editor->get_find_replace_bar()->search_next();
- } break;
- case SEARCH_FIND_PREV: {
- code_editor->get_find_replace_bar()->search_prev();
- } break;
- case SEARCH_REPLACE: {
- code_editor->get_find_replace_bar()->popup_replace();
- } break;
- case SEARCH_GOTO_LINE: {
- goto_line_popup->popup_find_line(code_editor);
- } break;
- case BOOKMARK_TOGGLE: {
- code_editor->toggle_bookmark();
- } break;
- case BOOKMARK_GOTO_NEXT: {
- code_editor->goto_next_bookmark();
- } break;
- case BOOKMARK_GOTO_PREV: {
- code_editor->goto_prev_bookmark();
- } break;
- case BOOKMARK_REMOVE_ALL: {
- code_editor->remove_all_bookmarks();
- } break;
- case PREVIEW_TOGGLE: {
- Vector<int> sorted_carets = tx->get_sorted_carets();
- int last_line = -1;
-
- for (const int &c : sorted_carets) {
- int from = tx->get_selection_from_line(c);
- from += from == last_line ? 1 : 0;
-
- int to = tx->get_selection_to_line(c);
- if (to < from) {
- continue;
- }
-
- // Check first if there's any lines with breakpoints in the selection.
- bool selection_has_breakpoints = false;
- for (int line = from; line <= to; line++) {
- if (tx->is_line_breakpointed(line)) {
- selection_has_breakpoints = true;
- break;
- }
- }
-
- // Set breakpoint on caret or remove all bookmarks from the selection.
- if (!selection_has_breakpoints) {
- if (tx->get_caret_line(c) != last_line) {
- tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
- }
- } else {
- for (int line = from; line <= to; line++) {
- tx->set_line_as_breakpoint(line, false);
- }
- }
-
- last_line = to;
- }
- } break;
- case PREVIEW_REMOVE_ALL: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
-
- for (int i = 0; i < bpoints.size(); i++) {
- int line = bpoints[i];
- bool dobreak = !tx->is_line_breakpointed(line);
-
- tx->set_line_as_breakpoint(line, dobreak);
- }
- } break;
- case PREVIEW_GOTO_NEXT: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
- if (bpoints.is_empty()) {
- return;
- }
-
- int current_line = tx->get_caret_line();
- int bpoint_idx = 0;
- if (current_line < (int)bpoints[bpoints.size() - 1]) {
- while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
- bpoint_idx++;
- }
- }
- _focus_preview_line(bpoints[bpoint_idx]);
- } break;
- case PREVIEW_GOTO_PREV: {
- PackedInt32Array bpoints = tx->get_breakpointed_lines();
- if (bpoints.is_empty()) {
- return;
- }
-
- int current_line = tx->get_caret_line();
- int bpoint_idx = bpoints.size() - 1;
- if (current_line > (int)bpoints[0]) {
- while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
- bpoint_idx--;
- }
- }
- _focus_preview_line(bpoints[bpoint_idx]);
- } break;
- case HELP_DOCS: {
- OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/shader_reference/index.html", GODOT_VERSION_DOCS_URL));
- } break;
- case EDIT_EMOJI_AND_SYMBOL: {
- tx->show_emoji_and_symbol_picker();
- } break;
- case EDIT_JOIN_LINES: {
- tx->join_lines();
- } break;
- }
- if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
- callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
- }
-}
-
-void TextShaderEditor::_prepare_edit_menu() {
- const CodeEdit *tx = code_editor->get_text_editor();
- PopupMenu *popup = edit_menu->get_popup();
- popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
- popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
-}
-
-void TextShaderEditor::_notification(int p_what) {
- switch (p_what) {
- case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
- if (EditorThemeManager::is_generated_theme_outdated() ||
- EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/fonts") ||
- EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor")) {
- _apply_editor_settings();
- }
- } break;
-
- case NOTIFICATION_VISIBILITY_CHANGED: {
- if (is_visible_in_tree() && preview_timer->is_inside_tree()) {
- preview_timer->start();
- }
- } break;
-
- case NOTIFICATION_RESIZED: {
- preview_timer->start();
- } break;
-
- case NOTIFICATION_THEME_CHANGED: {
- site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
-
- Ref<StyleBoxFlat> tab_style = get_theme_stylebox(SNAME("tab_selected"), "TabBar");
- Ref<StyleBoxFlat> preview_style = memnew(StyleBoxFlat);
- preview_style->set_bg_color(get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor)));
- preview_style->set_corner_radius_all(tab_style->get_corner_radius(CORNER_TOP_LEFT));
- preview_panel->add_theme_style_override(SceneStringName(panel), preview_style);
-
- update_params_btn->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
- remove_all_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
- } break;
-
- case NOTIFICATION_APPLICATION_FOCUS_IN: {
- _check_for_external_edit();
- } break;
- }
-}
-
-void TextShaderEditor::_apply_editor_settings() {
- code_editor->update_editor_settings();
-
- trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save");
- trim_final_newlines_on_save = EDITOR_GET("text_editor/behavior/files/trim_final_newlines_on_save");
-}
-
-void TextShaderEditor::_show_warnings_panel(bool p_show) {
- warnings_panel->set_visible(p_show);
-}
-
-void TextShaderEditor::_warning_clicked(const Variant &p_line) {
- if (p_line.get_type() == Variant::INT) {
- code_editor->goto_line_centered(p_line.operator int64_t());
- }
-}
-
-void TextShaderEditor::_bind_methods() {
- ClassDB::bind_method("_show_warnings_panel", &TextShaderEditor::_show_warnings_panel);
- ClassDB::bind_method("_warning_clicked", &TextShaderEditor::_warning_clicked);
-
- ADD_SIGNAL(MethodInfo("validation_changed"));
-}
-
-void TextShaderEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
- code_editor->goto_line_selection(p_line, p_begin, p_end);
-}
-
-void TextShaderEditor::_project_settings_changed() {
- _update_warnings(true);
-}
-
-void TextShaderEditor::_focus_preview_line(int p_line) {
- code_editor->goto_line_centered(p_line);
-
- TextShaderPreview *preview = code_editor->get_preview(p_line);
- if (preview) {
- preview_sbox->ensure_control_visible(preview);
- }
- preview_timer->start();
-}
-
-void TextShaderEditor::_update_warnings(bool p_validate) {
- bool changed = false;
-
- bool warnings_enabled = GLOBAL_GET("debug/shader_language/warnings/enable").booleanize();
- if (warnings_enabled != saved_warnings_enabled) {
- saved_warnings_enabled = warnings_enabled;
- changed = true;
- }
-
- bool treat_warning_as_errors = GLOBAL_GET("debug/shader_language/warnings/treat_warnings_as_errors").booleanize();
- if (treat_warning_as_errors != saved_treat_warning_as_errors) {
- saved_treat_warning_as_errors = treat_warning_as_errors;
- changed = true;
- }
-
- bool update_flags = false;
-
- for (int i = 0; i < ShaderWarning::WARNING_MAX; i++) {
- ShaderWarning::Code code = (ShaderWarning::Code)i;
- bool value = GLOBAL_GET("debug/shader_language/warnings/" + ShaderWarning::get_name_from_code(code).to_lower());
-
- if (saved_warnings[code] != value) {
- saved_warnings[code] = value;
- update_flags = true;
- changed = true;
- }
- }
-
- if (update_flags) {
- saved_warning_flags = (uint32_t)ShaderWarning::get_flags_from_codemap(saved_warnings);
- }
-
- if (p_validate && changed && code_editor && code_editor->get_edited_shader().is_valid()) {
+ Ref<Shader> shader = edited_res;
+ if (p_validate && changed && code_editor && shader.is_valid()) {
code_editor->validate_script();
}
}
-void TextShaderEditor::_check_for_external_edit() {
- bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change"));
-
- if (shader_inc.is_valid()) {
- if (shader_inc->get_last_modified_time() != FileAccess::get_modified_time(shader_inc->get_path())) {
- if (use_autoreload) {
- _reload_shader_include_from_disk();
- } else {
- callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());
- }
- }
+void ShaderTextEditor::set_edited_resource(const Ref<Resource> &p_res) {
+ if (p_res.is_null() || edited_res == p_res) {
return;
}
-
- if (shader.is_null() || shader->is_built_in()) {
+ Ref<Shader> shader = p_res;
+ Ref<ShaderInclude> shader_inc = p_res;
+ if (shader.is_null() && shader_inc.is_null()) {
return;
}
- if (shader->get_last_modified_time() != FileAccess::get_modified_time(shader->get_path())) {
- if (use_autoreload) {
- _reload_shader_from_disk();
- } else {
- callable_mp((Window *)disk_changed, &Window::popup_centered).call_deferred(Size2i());
- }
- }
-}
-
-void TextShaderEditor::_reload_shader_from_disk() {
- Ref<Shader> rel_shader = ResourceLoader::load(shader->get_path(), shader->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
- ERR_FAIL_COND(rel_shader.is_null());
-
- code_editor->set_block_shader_changed(true);
- shader->set_code(rel_shader->get_code());
- code_editor->set_block_shader_changed(false);
- shader->set_last_modified_time(rel_shader->get_last_modified_time());
- code_editor->reload_text();
-}
-
-void TextShaderEditor::_reload_shader_include_from_disk() {
- Ref<ShaderInclude> rel_shader_include = ResourceLoader::load(shader_inc->get_path(), shader_inc->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
- ERR_FAIL_COND(rel_shader_include.is_null());
+ p_res->disconnect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
- code_editor->set_block_shader_changed(true);
- shader_inc->set_code(rel_shader_include->get_code());
- code_editor->set_block_shader_changed(false);
- shader_inc->set_last_modified_time(rel_shader_include->get_last_modified_time());
- code_editor->reload_text();
-}
+ edited_res = p_res;
+ _load_theme_settings();
-void TextShaderEditor::_reload() {
+ String code;
if (shader.is_valid()) {
- _reload_shader_from_disk();
+ code = shader->get_code();
} else if (shader_inc.is_valid()) {
- _reload_shader_include_from_disk();
- }
-}
-
-void TextShaderEditor::edit_shader(const Ref<Shader> &p_shader) {
- if (p_shader.is_null() || !p_shader->is_text_shader()) {
- return;
- }
-
- if (shader == p_shader) {
- return;
+ code = shader_inc->get_code();
}
+ code_editor->get_text_editor()->set_text(code);
+ code_editor->get_text_editor()->clear_undo_history();
+ callable_mp((TextEdit *)code_editor->get_text_editor(), &TextEdit::set_h_scroll).call_deferred(0);
+ callable_mp((TextEdit *)code_editor->get_text_editor(), &TextEdit::set_v_scroll).call_deferred(0);
+ code_editor->get_text_editor()->tag_saved_version();
- shader = p_shader;
- shader_inc = Ref<ShaderInclude>();
+ _validate_script();
+ code_editor->update_line_and_column();
- code_editor->set_edited_shader(shader);
+ p_res->connect_changed(callable_mp(this, &ShaderTextEditor::_shader_changed));
}
-void TextShaderEditor::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
- if (p_shader_inc.is_null()) {
- return;
- }
+void ShaderTextEditor::goto_line_centered(int p_line, int p_column) {
+ TextEditorBase::goto_line_centered(p_line, p_column);
- if (shader_inc == p_shader_inc) {
- return;
+ TextShaderPreview *preview = _get_preview(p_line);
+ if (preview) {
+ preview_sbox->ensure_control_visible(preview);
}
-
- shader_inc = p_shader_inc;
- shader = Ref<Shader>();
-
- code_editor->set_edited_shader_include(p_shader_inc);
-}
-
-void TextShaderEditor::use_menu_bar(MenuButton *p_file_menu) {
- p_file_menu->set_switch_on_hover(true);
- menu_bar_hbox->add_child(p_file_menu);
- menu_bar_hbox->move_child(p_file_menu, 0);
+ preview_timer->start();
}
-void TextShaderEditor::save_external_data(const String &p_str) {
- if (shader.is_null() && shader_inc.is_null()) {
- disk_changed->hide();
- return;
- }
-
- if (trim_trailing_whitespace_on_save) {
- trim_trailing_whitespace();
- }
-
- if (trim_final_newlines_on_save) {
- trim_final_newlines();
- }
-
- apply_shaders();
-
- Ref<Shader> edited_shader = code_editor->get_edited_shader();
- if (edited_shader.is_valid()) {
- ResourceSaver::save(edited_shader);
- }
- if (shader.is_valid() && shader != edited_shader) {
- ResourceSaver::save(shader);
- }
-
- Ref<ShaderInclude> edited_shader_inc = code_editor->get_edited_shader_include();
- if (edited_shader_inc.is_valid()) {
- ResourceSaver::save(edited_shader_inc);
- }
- if (shader_inc.is_valid() && shader_inc != edited_shader_inc) {
- ResourceSaver::save(shader_inc);
+void ShaderTextEditor::set_code_block_changed(const String &p_code) {
+ Ref<Shader> shader = edited_res;
+ Ref<ShaderInclude> shader_inc = edited_res;
+ block_shader_changed = true;
+ if (shader.is_valid()) {
+ shader->set_code(p_code);
+ } else if (shader_inc.is_valid()) {
+ shader_inc->set_code(p_code);
}
- code_editor->get_text_editor()->tag_saved_version();
-
- disk_changed->hide();
-}
-
-void TextShaderEditor::trim_trailing_whitespace() {
- code_editor->trim_trailing_whitespace();
+ block_shader_changed = false;
}
-void TextShaderEditor::trim_final_newlines() {
- code_editor->trim_final_newlines();
-}
-
-void TextShaderEditor::set_toggle_list_control(Control *p_toggle_list_control) {
- code_editor->set_toggle_list_control(p_toggle_list_control);
-}
-
-void TextShaderEditor::update_toggle_files_button() {
- code_editor->update_toggle_files_button();
-}
-
-void TextShaderEditor::validate_script() {
- code_editor->_validate_script();
-}
-
-bool TextShaderEditor::is_unsaved() const {
- return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version();
-}
-
-void TextShaderEditor::tag_saved_version() {
- code_editor->get_text_editor()->tag_saved_version();
-}
-
-void TextShaderEditor::apply_shaders() {
+void ShaderTextEditor::apply_code() {
String editor_code = code_editor->get_text_editor()->get_text();
+ Ref<Shader> shader = edited_res;
if (shader.is_valid()) {
String shader_code = shader->get_code();
- if (shader_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {
- code_editor->set_block_shader_changed(true);
- shader->set_code(editor_code);
- code_editor->set_block_shader_changed(false);
+ if (shader_code != editor_code || dependencies_changed) {
+ set_code_block_changed(editor_code);
shader->set_edited(true);
}
}
+ Ref<ShaderInclude> shader_inc = edited_res;
if (shader_inc.is_valid()) {
String shader_inc_code = shader_inc->get_code();
- if (shader_inc_code != editor_code || dependencies_version != code_editor->get_dependencies_version()) {
- code_editor->set_block_shader_changed(true);
- shader_inc->set_code(editor_code);
- code_editor->set_block_shader_changed(false);
+ if (shader_inc_code != editor_code || dependencies_changed) {
+ set_code_block_changed(editor_code);
shader_inc->set_edited(true);
}
}
- dependencies_version = code_editor->get_dependencies_version();
+ dependencies_changed = false;
+ code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
-void TextShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
- Ref<InputEventMouseButton> mb = ev;
-
- if (mb.is_valid()) {
- if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
- CodeEdit *tx = code_editor->get_text_editor();
-
- tx->apply_ime();
-
- Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
- int row = pos.y;
- int col = pos.x;
- tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
-
- if (tx->is_move_caret_on_right_click_enabled()) {
- tx->remove_secondary_carets();
- if (tx->has_selection()) {
- int from_line = tx->get_selection_from_line();
- int to_line = tx->get_selection_to_line();
- int from_column = tx->get_selection_from_column();
- int to_column = tx->get_selection_to_column();
-
- if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
- // Right click is outside the selected text
- tx->deselect();
- }
- }
- if (!tx->has_selection()) {
- tx->set_caret_line(row, true, false, -1);
- tx->set_caret_column(col);
- }
- }
- _make_context_menu(tx->has_selection(), get_local_mouse_position());
- }
- }
-
- Ref<InputEventKey> k = ev;
- if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
- CodeEdit *tx = code_editor->get_text_editor();
- tx->adjust_viewport_to_caret();
- _make_context_menu(tx->has_selection(), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos()));
- context_menu->grab_focus();
+ScriptEditorBase *ShaderTextEditor::create_editor(const Ref<Resource> &p_resource) {
+ // Check class name for Shader to ensure it is not a VisualShader even when the visual shader module is disabled
+ if (p_resource->is_class("Shader") || Object::cast_to<ShaderInclude>(*p_resource)) {
+ return memnew(ShaderTextEditor);
}
+ return nullptr;
}
-void TextShaderEditor::_on_shader_preview_toggled(int p_line) {
+void ShaderTextEditor::_breakpoint_toggled(int p_line) {
if (!pending_update_shader_previews) {
pending_update_shader_previews = true;
- callable_mp(this, &TextShaderEditor::_update_shader_previews).call_deferred();
+ callable_mp(this, &ShaderTextEditor::_update_shader_previews).call_deferred();
}
}
-void TextShaderEditor::_update_shader_previews() {
+void ShaderTextEditor::_update_shader_previews() {
pending_update_shader_previews = false;
const CodeEdit *ce = code_editor->get_text_editor();
- code_editor->clear_previews();
+ _clear_previews();
bool found = false;
for (int i = 0; i < ce->get_line_count(); i++) {
if (ce->is_line_breakpointed(i)) {
found = true;
- code_editor->toggle_shader_preview(i);
+ _toggle_shader_preview(i);
}
}
@@ -2033,112 +1375,7 @@ void TextShaderEditor::_update_shader_previews() {
preview_timer->start();
}
-void TextShaderEditor::_update_bookmark_list() {
- bookmarks_menu->clear();
-
- bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
- bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
- bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
- bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
-
- PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
- if (bookmark_list.is_empty()) {
- return;
- }
-
- bookmarks_menu->add_separator();
-
- for (int i = 0; i < bookmark_list.size(); i++) {
- String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges();
- // Limit the size of the line if too big.
- if (line.length() > 50) {
- line = line.substr(0, 50);
- }
-
- bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - \"" + line + "\"");
- bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
- }
-}
-
-void TextShaderEditor::_bookmark_item_pressed(int p_idx) {
- if (p_idx < 4) { // Any item before the separator.
- _menu_option(bookmarks_menu->get_item_id(p_idx));
- } else {
- code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx));
- }
-}
-
-void TextShaderEditor::_update_shader_preview_list() {
- previews_menu->clear();
- previews_menu->reset_size();
-
- previews_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/toggle_shader_preview"), PREVIEW_TOGGLE);
- previews_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/remove_all_shader_previews"), PREVIEW_REMOVE_ALL);
- previews_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/goto_next_shader_preview"), PREVIEW_GOTO_NEXT);
- previews_menu->add_shortcut(ED_GET_SHORTCUT("shader_text_editor/goto_previous_shader_preview"), PREVIEW_GOTO_PREV);
-
- PackedInt32Array breakpoint_list = get_code_editor()->get_text_editor()->get_breakpointed_lines();
- if (breakpoint_list.is_empty()) {
- return;
- }
-
- previews_menu->add_separator();
-
- for (int i = 0; i < breakpoint_list.size(); i++) {
- // Strip edges to remove spaces or tabs.
- // Also replace any tabs by spaces, since we can't print tabs in the menu.
- String line = get_code_editor()->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
-
- // Limit the size of the line if too big.
- if (line.length() > 50) {
- line = line.substr(0, 50);
- }
-
- previews_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`");
- previews_menu->set_item_metadata(-1, breakpoint_list[i]);
- }
-}
-
-void TextShaderEditor::_shader_preview_item_pressed(int p_idx) {
- if (p_idx < 4) { // Any item before the separator.
- _menu_option(previews_menu->get_item_id(p_idx));
- } else {
- _focus_preview_line(previews_menu->get_item_metadata(p_idx));
- }
-}
-
-void TextShaderEditor::_make_context_menu(bool p_selection, Vector2 p_position) {
- context_menu->clear();
- if (DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
- context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
- context_menu->add_separator();
- }
- if (p_selection) {
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
- }
-
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
- context_menu->add_separator();
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
- context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
-
- context_menu->add_separator();
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
- context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
-
- context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !code_editor->get_text_editor()->has_undo());
- context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !code_editor->get_text_editor()->has_redo());
-
- context_menu->set_position(get_screen_position() + p_position);
- context_menu->reset_size();
- context_menu->popup();
-}
-
-void TextShaderEditor::register_editor() {
+void ShaderTextEditor::register_editor() {
ED_SHORTCUT("shader_text_editor/toggle_shader_preview", TTRC("Toggle Shader Preview"), Key::F9);
ED_SHORTCUT_OVERRIDE("shader_text_editor/toggle_shader_preview", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
@@ -2146,129 +1383,31 @@ void TextShaderEditor::register_editor() {
// Using Control for these shortcuts even on macOS because Command+Comma is taken for opening Editor Settings.
ED_SHORTCUT("shader_text_editor/goto_next_shader_preview", TTRC("Go to Next Shader Preview"), KeyModifierMask::CTRL | Key::PERIOD);
ED_SHORTCUT("shader_text_editor/goto_previous_shader_preview", TTRC("Go to Previous Shader Preview"), KeyModifierMask::CTRL | Key::COMMA);
+
+ ScriptEditor::register_create_script_editor_function(create_editor);
}
-TextShaderEditor::TextShaderEditor() {
+ShaderTextEditor::ShaderTextEditor() {
_update_warnings(false);
- code_editor = memnew(ShaderTextEditor);
-
- code_editor->connect("script_validated", callable_mp(this, &TextShaderEditor::_script_validated));
-
- code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
- code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
+ ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &ShaderTextEditor::_update_warnings).bind(true));
- code_editor->connect("show_warnings_panel", callable_mp(this, &TextShaderEditor::_show_warnings_panel));
- code_editor->connect(CoreStringName(script_changed), callable_mp(this, &TextShaderEditor::apply_shaders));
- ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextShaderEditor::_project_settings_changed));
+ code_editor->get_text_editor()->connect("_fold_line_updated", callable_mp(this, &ShaderTextEditor::_redraw_preview_lines), CONNECT_DEFERRED);
+ code_editor->get_text_editor()->connect(SceneStringName(theme_changed), callable_mp(this, &ShaderTextEditor::_redraw_preview_lines), CONNECT_DEFERRED);
+ code_editor->get_text_editor()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &ShaderTextEditor::_redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
- code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
- code_editor->get_text_editor()->set_context_menu_enabled(false);
code_editor->get_text_editor()->set_draw_executing_lines_gutter(false);
- code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextShaderEditor::_text_edit_gui_input));
- code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &TextShaderEditor::_on_shader_preview_toggled));
- code_editor->get_text_editor()->connect("_fold_line_updated", callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines), CONNECT_DEFERRED);
- code_editor->get_text_editor()->connect("theme_changed", callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines), CONNECT_DEFERRED);
- code_editor->get_text_editor()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
-
- code_editor->update_editor_settings();
-
- context_menu = memnew(PopupMenu);
- add_child(context_menu);
- context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
-
- VBoxContainer *main_container = memnew(VBoxContainer);
- main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
- menu_bar_hbox = memnew(HBoxContainer);
-
- edit_menu = memnew(MenuButton);
- edit_menu->set_flat(false);
- edit_menu->set_theme_type_variation("FlatMenuButton");
- edit_menu->set_shortcut_context(this);
- edit_menu->set_text(TTRC("Edit"));
- edit_menu->set_switch_on_hover(true);
- edit_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_prepare_edit_menu));
-
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
- edit_menu->get_popup()->add_separator();
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
- edit_menu->get_popup()->add_separator();
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
- edit_menu->get_popup()->add_separator();
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/join_lines"), EDIT_JOIN_LINES);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
- edit_menu->get_popup()->add_separator();
- edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
- edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
-
- search_menu = memnew(MenuButton);
- search_menu->set_flat(false);
- search_menu->set_theme_type_variation("FlatMenuButton");
- search_menu->set_shortcut_context(this);
- search_menu->set_text(TTRC("Search"));
- search_menu->set_switch_on_hover(true);
-
- search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
- search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
- search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
- search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
- search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
-
- MenuButton *goto_menu = memnew(MenuButton);
- goto_menu->set_flat(false);
- goto_menu->set_theme_type_variation("FlatMenuButton");
- goto_menu->set_shortcut_context(this);
- goto_menu->set_text(TTRC("Go To"));
- goto_menu->set_switch_on_hover(true);
- goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
-
- goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
- goto_menu->get_popup()->add_separator();
-
- bookmarks_menu = memnew(PopupMenu);
- goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);
- _update_bookmark_list();
- bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_bookmark_list));
- bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed));
-
- previews_menu = memnew(PopupMenu);
- goto_menu->get_popup()->add_submenu_node_item(TTRC("Shader Previews"), previews_menu);
- _update_shader_preview_list();
- previews_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_shader_preview_list));
- previews_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_shader_preview_item_pressed));
-
- add_child(main_container);
- main_container->add_child(menu_bar_hbox);
- menu_bar_hbox->add_child(edit_menu);
- menu_bar_hbox->add_child(search_menu);
- menu_bar_hbox->add_child(goto_menu);
- menu_bar_hbox->add_spacer();
-
- site_search = memnew(Button);
- site_search->set_theme_type_variation(SceneStringName(FlatButton));
- site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS));
- site_search->set_text(TTRC("Online Docs"));
- site_search->set_tooltip_text(TTRC("Open Godot online documentation."));
- menu_bar_hbox->add_child(site_search);
-
- menu_bar_hbox->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
+ preview_line_layer = memnew(TextShaderPreviewLineLayer);
+ preview_line_layer->set_previews(previews);
+ preview_line_layer->set_code_editor(code_editor->get_text_editor());
+
+ add_child(preview_line_layer);
HSplitContainer *main_box = memnew(HSplitContainer);
main_box->set_h_size_flags(SIZE_EXPAND_FILL);
main_box->set_v_size_flags(SIZE_EXPAND_FILL);
main_box->add_theme_constant_override("separation", 0);
- main_box->connect("dragged", callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
+ main_box->connect("dragged", callable_mp(this, &ShaderTextEditor::_redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
preview_box = memnew(VBoxContainer);
preview_box->hide();
@@ -2280,8 +1419,8 @@ TextShaderEditor::TextShaderEditor() {
preview_sbox = memnew(ScrollContainer);
preview_sbox->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
preview_sbox->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_AUTO);
- preview_sbox->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
- code_editor->get_preview_line_layer()->set_scroll_container(preview_sbox);
+ preview_sbox->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &ShaderTextEditor::_redraw_preview_lines).unbind(1), CONNECT_DEFERRED);
+ _get_preview_line_layer()->set_scroll_container(preview_sbox);
preview_panel->add_child(preview_sbox);
HSplitContainer *preview_hsplit = memnew(HSplitContainer);
@@ -2291,10 +1430,9 @@ TextShaderEditor::TextShaderEditor() {
preview_hsplit->add_theme_constant_override("minimum_grab_thickness", 8 * EDSCALE);
preview_sbox->add_child(preview_hsplit);
- VBoxContainer *preview_vbox = memnew(VBoxContainer);
+ preview_vbox = memnew(VBoxContainer);
preview_vbox->add_theme_constant_override("separation", 0);
- preview_vbox->connect(SceneStringName(sort_children), callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines));
- code_editor->set_preview_box(preview_vbox);
+ preview_vbox->connect(SceneStringName(sort_children), callable_mp(this, &ShaderTextEditor::_redraw_preview_lines));
preview_hsplit->add_child(preview_vbox);
Control *extended_gutter = memnew(Control);
@@ -2312,70 +1450,27 @@ TextShaderEditor::TextShaderEditor() {
update_params_btn->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD);
update_params_btn->set_h_size_flags(SIZE_EXPAND_FILL);
update_params_btn->set_theme_type_variation("FlatMenuButton");
- update_params_btn->connect(SceneStringName(pressed), callable_mp(code_editor, &ShaderTextEditor::update_parameters));
+ update_params_btn->connect(SceneStringName(pressed), callable_mp(this, &ShaderTextEditor::_update_parameters));
update_params_btn->set_tooltip_text(TTRC("Update shader parameters in previews to match the values in the current ShaderMaterial inspector."));
tools_row->add_child(update_params_btn);
remove_all_btn = memnew(Button);
remove_all_btn->set_theme_type_variation("FlatMenuButton");
- remove_all_btn->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(PREVIEW_REMOVE_ALL));
+ remove_all_btn->connect(SceneStringName(pressed), callable_mp(this, &ShaderTextEditor::_edit_option).bind(DEBUG_REMOVE_ALL_BREAKPOINTS));
remove_all_btn->set_tooltip_text(TTRC("Remove all line shader previews."));
tools_row->add_child(remove_all_btn);
main_box->add_child(preview_box);
main_box->add_child(code_editor);
- VSplitContainer *editor_box = memnew(VSplitContainer);
- main_container->add_child(editor_box);
- editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
- editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
editor_box->add_child(main_box);
-
- FindReplaceBar *bar = memnew(FindReplaceBar);
- main_container->add_child(bar);
- bar->hide();
- code_editor->set_find_replace_bar(bar);
-
- warnings_panel = memnew(RichTextLabel);
- warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
- warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
- warnings_panel->set_meta_underline(true);
- warnings_panel->set_selection_enabled(true);
- warnings_panel->set_context_menu_enabled(true);
- warnings_panel->set_focus_mode(FOCUS_CLICK);
- warnings_panel->hide();
- warnings_panel->connect("meta_clicked", callable_mp(this, &TextShaderEditor::_warning_clicked));
editor_box->add_child(warnings_panel);
- code_editor->set_warnings_panel(warnings_panel);
-
- goto_line_popup = memnew(GotoLinePopup);
- add_child(goto_line_popup);
- code_editor->connect("show_goto_popup", callable_mp(this, &TextShaderEditor::_menu_option).bind(SEARCH_GOTO_LINE));
-
- disk_changed = memnew(ConfirmationDialog);
-
- VBoxContainer *vbc = memnew(VBoxContainer);
- disk_changed->add_child(vbc);
-
- Label *dl = memnew(Label);
- dl->set_focus_mode(FOCUS_ACCESSIBILITY);
- dl->set_text(TTRC("This shader has been modified on disk.\nWhat action should be taken?"));
- vbc->add_child(dl);
-
- disk_changed->connect(SceneStringName(confirmed), callable_mp(this, &TextShaderEditor::_reload));
- disk_changed->set_ok_button_text(TTRC("Reload"));
-
- disk_changed->add_button(TTRC("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");
- disk_changed->connect("custom_action", callable_mp(this, &TextShaderEditor::save_external_data));
-
- add_child(disk_changed);
preview_timer = memnew(Timer);
add_child(preview_timer);
preview_timer->set_one_shot(true);
preview_timer->set_wait_time(0.001);
- preview_timer->connect("timeout", callable_mp(code_editor, &ShaderTextEditor::redraw_preview_lines));
+ preview_timer->connect("timeout", callable_mp(this, &ShaderTextEditor::_redraw_preview_lines));
- _apply_editor_settings();
- code_editor->show_toggle_files_button(); // TODO: Disabled for now, because it doesn't work properly.
+ InspectorDock::get_inspector_singleton()->connect(SNAME("edited_object_changed"), callable_mp(this, &ShaderTextEditor::_recompile_previews));
}
diff --git a/editor/shader/shader_text_editor.h b/editor/shader/shader_text_editor.h
index 70b8186af00..59f2cde00df 100644
--- a/editor/shader/shader_text_editor.h
+++ b/editor/shader/shader_text_editor.h
@@ -30,10 +30,7 @@
#pragma once
-#include "editor/gui/code_editor.h"
-#include "editor/shader/shader_editor.h"
-#include "scene/gui/menu_button.h"
-#include "scene/gui/rich_text_label.h"
+#include "editor/script/script_editor_base.h"
#include "servers/rendering/shader_warnings.h"
class MaterialEditor;
@@ -42,21 +39,8 @@ class ShaderMaterial;
class Timer;
class TextureRect;
class StyleBoxFlat;
-
-class GDShaderSyntaxHighlighter : public CodeHighlighter {
- GDCLASS(GDShaderSyntaxHighlighter, CodeHighlighter)
-
-private:
- Vector<Point2i> disabled_branch_regions;
- Color disabled_branch_color;
-
-public:
- virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override;
-
- void add_disabled_branch_region(const Point2i &p_region);
- void clear_disabled_branch_regions();
- void set_disabled_branch_color(const Color &p_color);
-};
+class Shader;
+class ShaderInclude;
class TextShaderPreview : public VBoxContainer {
GDCLASS(TextShaderPreview, VBoxContainer);
@@ -138,196 +122,95 @@ public:
TextShaderPreviewLineLayer();
};
-class ShaderTextEditor : public CodeTextEditor {
- GDCLASS(ShaderTextEditor, CodeTextEditor);
-
- Color marked_line_color = Color(1, 1, 1);
+class ShaderTextEditor : public CodeEditorBase {
+ GDCLASS(ShaderTextEditor, CodeEditorBase);
- struct WarningsComparator {
- _ALWAYS_INLINE_ bool operator()(const ShaderWarning &p_a, const ShaderWarning &p_b) const { return (p_a.get_line() < p_b.get_line()); }
- };
+ static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);
- Ref<GDShaderSyntaxHighlighter> syntax_highlighter;
- RichTextLabel *warnings_panel = nullptr;
- Ref<Shader> shader;
- Ref<ShaderInclude> shader_inc;
- List<ShaderWarning> warnings;
- Error last_compile_result = Error::OK;
HashMap<int, TextShaderPreview *> previews;
- Control *preview_box = nullptr;
TextShaderPreviewLineLayer *preview_line_layer = nullptr;
+ PanelContainer *preview_panel = nullptr;
+ VBoxContainer *preview_box = nullptr;
+ VBoxContainer *preview_vbox = nullptr;
+ Timer *preview_timer = nullptr;
+ ScrollContainer *preview_sbox = nullptr;
+ Button *update_params_btn = nullptr;
+ Button *remove_all_btn = nullptr;
+
+ bool pending_update_shader_previews = false;
+
+ Error last_compile_result = Error::OK;
void _check_shader_mode();
+
+ virtual bool _edit_option(int p_option) override;
+
void _update_warning_panel();
+ void _update_warnings(bool p_validate);
- bool block_shader_changed = false;
- void _shader_changed();
+ bool dependencies_changed = true;
- uint32_t dependencies_version = 0; // Incremented if deps changed
+ Color marked_line_color = Color(1, 1, 1);
-protected:
- void _notification(int p_what);
- static void _bind_methods();
- virtual void _load_theme_settings() override;
+ struct WarningsComparator {
+ _ALWAYS_INLINE_ bool operator()(const ShaderWarning &p_a, const ShaderWarning &p_b) const { return (p_a.get_line() < p_b.get_line()); }
+ };
- virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) override;
+ List<ShaderWarning> warnings;
-public:
- void set_block_shader_changed(bool p_block) { block_shader_changed = p_block; }
- uint32_t get_dependencies_version() const { return dependencies_version; }
+ bool block_shader_changed = false;
+ void _shader_changed();
- virtual void _validate_script() override;
+ void _focus_preview_line(int p_line);
+ void _toggle_shader_preview(int p_line);
+ void _remove_shader_preview(int p_line);
+ void _clear_previews();
+ void _redraw_preview_lines();
+ void _recompile_previews();
+ void _update_parameters();
- void reload_text();
- void set_warnings_panel(RichTextLabel *p_warnings_panel);
-
- Ref<Shader> get_edited_shader() const;
- Ref<ShaderInclude> get_edited_shader_include() const;
- TextShaderPreviewLineLayer *get_preview_line_layer() const;
- TextShaderPreview *get_preview(int p_line) const;
-
- void set_edited_shader(const Ref<Shader> &p_shader);
- void set_edited_shader(const Ref<Shader> &p_shader, const String &p_code);
- void set_edited_shader_include(const Ref<ShaderInclude> &p_include);
- void set_edited_shader_include(const Ref<ShaderInclude> &p_include, const String &p_code);
- void set_edited_code(const String &p_code);
-
- void toggle_shader_preview(int p_line);
- void remove_shader_preview(int p_line);
- void goto_shader_preview(int p_line);
- void set_preview_box(Control *p_box);
- void clear_previews();
- void redraw_preview_lines();
- void recompile_previews();
- void update_parameters();
+ TextShaderPreviewLineLayer *_get_preview_line_layer() const;
+ TextShaderPreview *_get_preview(int p_line) const;
- ShaderTextEditor();
-};
+protected:
+ class EditMenusShTE : public EditMenusCEB {
+ GDCLASS(EditMenusShTE, EditMenusCEB);
-class TextShaderEditor : public ShaderEditor {
- GDCLASS(TextShaderEditor, ShaderEditor);
-
- enum {
- EDIT_UNDO,
- EDIT_REDO,
- EDIT_CUT,
- EDIT_COPY,
- EDIT_PASTE,
- EDIT_SELECT_ALL,
- EDIT_MOVE_LINE_UP,
- EDIT_MOVE_LINE_DOWN,
- EDIT_INDENT,
- EDIT_UNINDENT,
- EDIT_DELETE_LINE,
- EDIT_DUPLICATE_SELECTION,
- EDIT_DUPLICATE_LINES,
- EDIT_TOGGLE_WORD_WRAP,
- EDIT_TOGGLE_COMMENT,
- EDIT_COMPLETE,
- SEARCH_FIND,
- SEARCH_FIND_NEXT,
- SEARCH_FIND_PREV,
- SEARCH_REPLACE,
- SEARCH_GOTO_LINE,
- BOOKMARK_TOGGLE,
- BOOKMARK_GOTO_NEXT,
- BOOKMARK_GOTO_PREV,
- BOOKMARK_REMOVE_ALL,
- PREVIEW_TOGGLE,
- PREVIEW_REMOVE_ALL,
- PREVIEW_GOTO_NEXT,
- PREVIEW_GOTO_PREV,
- HELP_DOCS,
- EDIT_EMOJI_AND_SYMBOL,
- EDIT_JOIN_LINES,
- };
+ protected:
+ void _update_breakpoint_list() override;
- HBoxContainer *menu_bar_hbox = nullptr;
- PanelContainer *preview_panel = nullptr;
- VBoxContainer *preview_box = nullptr;
- MenuButton *edit_menu = nullptr;
- MenuButton *search_menu = nullptr;
- PopupMenu *bookmarks_menu = nullptr;
- PopupMenu *previews_menu = nullptr;
- Button *site_search = nullptr;
- PopupMenu *context_menu = nullptr;
- RichTextLabel *warnings_panel = nullptr;
- Timer *preview_timer = nullptr;
- ScrollContainer *preview_sbox = nullptr;
+ public:
+ virtual bool handles(ScriptEditorBase *p_seb) override { return Object::cast_to<ShaderTextEditor>(p_seb); }
- GotoLinePopup *goto_line_popup = nullptr;
- ConfirmationDialog *disk_changed = nullptr;
+ EditMenusShTE(ScriptEditor *p_se) : EditMenusCEB(p_se, "Shader Previews") {}
+ };
- ShaderTextEditor *code_editor = nullptr;
- bool compilation_success = true;
+ void _notification(int p_what);
- void _menu_option(int p_option);
- void _prepare_edit_menu();
- mutable Ref<Shader> shader;
- mutable Ref<ShaderInclude> shader_inc;
+ virtual void _validate_script() override;
- void _apply_editor_settings();
- void _project_settings_changed();
+ virtual void _load_theme_settings() override;
- void _check_for_external_edit();
- void _reload_shader_from_disk();
- void _reload_shader_include_from_disk();
- void _reload();
- void _show_warnings_panel(bool p_show);
- void _warning_clicked(const Variant &p_line);
- void _update_warnings(bool p_validate);
- void _focus_preview_line(int p_line);
+ virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) override;
- void _script_validated(bool p_valid) {
- compilation_success = p_valid;
- emit_signal(SNAME("validation_changed"));
- }
+ virtual void _breakpoint_toggled(int p_row) override;
+ void _update_shader_previews();
- uint32_t dependencies_version = 0xFFFFFFFF;
+public:
+ virtual String get_doc_url_path() override { return "/tutorials/shaders/shader_reference/index.html"; }
- bool trim_trailing_whitespace_on_save = false;
- bool trim_final_newlines_on_save = false;
- bool pending_update_shader_previews = false;
+ virtual bool show_members_overview() override { return false; }
-private:
- Button *update_params_btn = nullptr;
- Button *remove_all_btn = nullptr;
+ virtual void set_edited_resource(const Ref<Resource> &p_res) override;
-protected:
- void _notification(int p_what);
- static void _bind_methods();
- void _make_context_menu(bool p_selection, Vector2 p_position);
- void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
- void _on_shader_preview_toggled(int p_line);
- void _update_shader_previews();
+ virtual void goto_line_centered(int p_line, int p_column = 0) override;
- void _update_bookmark_list();
- void _bookmark_item_pressed(int p_idx);
- void _update_shader_preview_list();
- void _shader_preview_item_pressed(int p_idx);
+ void set_code_block_changed(const String &p_code);
+ virtual void apply_code() override;
-public:
- virtual void edit_shader(const Ref<Shader> &p_shader) override;
- virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) override;
- virtual void use_menu_bar(MenuButton *p_file_menu) override;
-
- virtual void apply_shaders() override;
- virtual bool is_unsaved() const override;
- virtual void save_external_data(const String &p_str = "") override;
- virtual void set_toggle_list_control(Control *p_toggle_list_control) override;
- virtual void update_toggle_files_button() override;
- virtual void validate_script() override;
-
- bool was_compilation_successful() const { return compilation_success; }
- bool get_trim_trailing_whitespace_on_save() const { return trim_trailing_whitespace_on_save; }
- bool get_trim_final_newlines_on_save() const { return trim_final_newlines_on_save; }
- void goto_line_selection(int p_line, int p_begin, int p_end);
- void trim_trailing_whitespace();
- void trim_final_newlines();
- void tag_saved_version();
- ShaderTextEditor *get_code_editor() { return code_editor; }
+ virtual EditMenusBase *create_edit_menu(ScriptEditor *p_se) override { return memnew(EditMenusShTE(p_se)); }
static void register_editor();
- TextShaderEditor();
+ ShaderTextEditor();
};
diff --git a/editor/shader/text_shader_language_plugin.cpp b/editor/shader/text_shader_language_plugin.cpp
index fef40edb163..1d0fe539dcf 100644
--- a/editor/shader/text_shader_language_plugin.cpp
+++ b/editor/shader/text_shader_language_plugin.cpp
@@ -31,31 +31,9 @@
#include "text_shader_language_plugin.h"
#include "core/string/string_builder.h"
-#include "editor/shader/text_shader_editor.h"
+#include "editor/shader/shader_text_editor.h"
#include "servers/rendering/shader_types.h"
-bool TextShaderLanguagePlugin::handles_shader(const Ref<Shader> &p_shader) const {
- // The text shader editor only edits the base Shader class,
- // not classes that inherit from it like VisualShader.
- return p_shader->get_class_name() == Shader::get_class_static();
-}
-
-bool TextShaderLanguagePlugin::handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const {
- return p_shader_inc->get_class_static() == ShaderInclude::get_class_static();
-}
-
-ShaderEditor *TextShaderLanguagePlugin::edit_shader(const Ref<Shader> &p_shader) {
- TextShaderEditor *editor = memnew(TextShaderEditor);
- editor->edit_shader(p_shader);
- return editor;
-}
-
-ShaderEditor *TextShaderLanguagePlugin::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
- TextShaderEditor *editor = memnew(TextShaderEditor);
- editor->edit_shader_include(p_shader_inc);
- return editor;
-}
-
Ref<Shader> TextShaderLanguagePlugin::create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) {
Ref<Shader> shader;
shader.instantiate();
@@ -148,10 +126,6 @@ Ref<ShaderInclude> TextShaderLanguagePlugin::create_new_shader_include() {
return shader_inc;
}
-PackedStringArray TextShaderLanguagePlugin::get_language_variations() const {
- return PackedStringArray{ "Shader", "ShaderInclude" };
-}
-
String TextShaderLanguagePlugin::get_file_extension(int p_variation_index) const {
if (p_variation_index == 0) {
return "gdshader";
diff --git a/editor/shader/text_shader_language_plugin.h b/editor/shader/text_shader_language_plugin.h
index f5c08241fdb..15c85ae3e63 100644
--- a/editor/shader/text_shader_language_plugin.h
+++ b/editor/shader/text_shader_language_plugin.h
@@ -36,12 +36,8 @@ class TextShaderLanguagePlugin : public EditorShaderLanguagePlugin {
GDCLASS(TextShaderLanguagePlugin, EditorShaderLanguagePlugin);
public:
- virtual bool handles_shader(const Ref<Shader> &p_shader) const override;
- virtual bool handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const override;
- virtual ShaderEditor *edit_shader(const Ref<Shader> &p_shader) override;
- virtual ShaderEditor *edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) override;
virtual Ref<Shader> create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) override;
virtual Ref<ShaderInclude> create_new_shader_include() override;
- virtual PackedStringArray get_language_variations() const override;
+ virtual PackedStringArray get_language_variations() const override { return { "Shader", "ShaderInclude" }; }
virtual String get_file_extension(int p_variation_index) const override;
};
diff --git a/modules/visual_shader/editor/visual_shader_editor_plugin.cpp b/modules/visual_shader/editor/visual_shader_editor_plugin.cpp
index 04d42c5e7f2..d1e7572d1d9 100644
--- a/modules/visual_shader/editor/visual_shader_editor_plugin.cpp
+++ b/modules/visual_shader/editor/visual_shader_editor_plugin.cpp
@@ -38,8 +38,6 @@
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
#include "core/os/keyboard.h"
-#include "core/os/os.h"
-#include "core/version_generated.gen.h"
#include "editor/docks/filesystem_dock.h"
#include "editor/docks/inspector_dock.h"
#include "editor/editor_node.h"
@@ -51,20 +49,16 @@
#include "editor/inspector/editor_properties_vector.h"
#include "editor/scene/curve_editor_plugin.h"
#include "editor/scene/material_editor_plugin.h"
+#include "editor/script/script_editor_plugin.h"
#include "editor/settings/editor_settings.h"
-#include "editor/shader/shader_editor_plugin.h"
#include "editor/themes/editor_scale.h"
#include "editor/themes/editor_theme_manager.h"
#include "scene/animation/tween.h"
-#include "scene/gui/button.h"
#include "scene/gui/check_box.h"
#include "scene/gui/code_edit.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/graph_edit.h"
-#include "scene/gui/menu_button.h"
-#include "scene/gui/option_button.h"
-#include "scene/gui/popup.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/separator.h"
#include "scene/gui/split_container.h"
@@ -1535,19 +1529,21 @@ Vector2 VisualShaderEditor::selection_center;
List<VisualShaderEditor::CopyItem> VisualShaderEditor::copy_items_buffer;
List<VisualShader::Connection> VisualShaderEditor::copy_connections_buffer;
-void VisualShaderEditor::edit_shader(const Ref<Shader> &p_shader) {
+void VisualShaderEditor::set_edited_resource(const Ref<Resource> &p_shader) {
shader_fully_loaded = false;
bool changed = false;
- VisualShader *visual_shader_ptr = Object::cast_to<VisualShader>(p_shader.ptr());
- if (visual_shader_ptr) {
+ Ref<VisualShader> visual_shader = edited_res;
+ Ref<VisualShader> new_visual_shader = p_shader;
+ if (new_visual_shader.is_valid()) {
if (visual_shader.is_null()) {
changed = true;
} else {
- if (visual_shader.ptr() != visual_shader_ptr) {
+ if (visual_shader != new_visual_shader) {
changed = true;
}
}
visual_shader = p_shader;
+ edited_res = p_shader;
graph_plugin->register_shader(visual_shader.ptr());
visual_shader->connect_changed(callable_mp(this, &VisualShaderEditor::_update_preview));
@@ -1575,26 +1571,17 @@ void VisualShaderEditor::edit_shader(const Ref<Shader> &p_shader) {
}
}
-void VisualShaderEditor::use_menu_bar(MenuButton *p_file_menu) {
- p_file_menu->set_switch_on_hover(false);
- toolbar_hflow->add_child(p_file_menu);
- toolbar_hflow->move_child(p_file_menu, 2); // Toggle Files Panel button + separator.
+void VisualShaderEditor::apply_code() {
+ // Stub. TODO: Implement apply_code in visual shaders for parity with text shaders.
}
-void VisualShaderEditor::apply_shaders() {
- // Stub. TODO: Implement apply_shaders in visual shaders for parity with text shaders.
-}
-
-bool VisualShaderEditor::is_unsaved() const {
+bool VisualShaderEditor::is_unsaved() {
// Stub. TODO: Implement is_unsaved in visual shaders for parity with text shaders.
return false;
}
-void VisualShaderEditor::save_external_data(const String &p_str) {
- ResourceSaver::save(visual_shader, visual_shader->get_path());
-}
-
void VisualShaderEditor::validate_script() {
+ Ref<VisualShader> visual_shader = edited_res;
if (visual_shader.is_valid()) {
_update_nodes();
}
@@ -1724,6 +1711,7 @@ Dictionary VisualShaderEditor::get_custom_node_data(Ref<VisualShaderNodeCustom>
}
void VisualShaderEditor::_get_current_mode_limits(int &r_begin_type, int &r_end_type) const {
+ Ref<VisualShader> visual_shader = edited_res;
switch (visual_shader->get_mode()) {
case Shader::MODE_CANVAS_ITEM:
case Shader::MODE_SPATIAL: {
@@ -1773,6 +1761,7 @@ void VisualShaderEditor::_update_custom_script(const Ref<Script> &p_script) {
Ref<VisualShaderNodeCustom> ref;
ref.instantiate();
ref->set_script(p_script);
+ Ref<VisualShader> visual_shader = edited_res;
if (!ref->is_available(visual_shader->get_mode(), get_current_shader_type())) {
for (int i = 0; i < add_options.size(); i++) {
if (add_options[i].is_custom && add_options[i].script == p_script) {
@@ -1907,6 +1896,7 @@ void VisualShaderEditor::_resource_saved(const Ref<Resource> &p_resource) {
void VisualShaderEditor::_resources_removed() {
bool has_any_instance = false;
+ Ref<VisualShader> visual_shader = edited_res;
for (const Ref<Script> &scr : custom_scripts_to_delete) {
for (int i = custom_node_option_idx; i < add_options.size(); i++) {
@@ -1990,6 +1980,7 @@ void VisualShaderEditor::_update_options_menu_deferred() {
}
void VisualShaderEditor::_rebuild_shader_deferred() {
+ Ref<VisualShader> visual_shader = edited_res;
if (visual_shader.is_valid()) {
visual_shader->rebuild();
}
@@ -2069,7 +2060,7 @@ void VisualShaderEditor::_preview_tools_menu_option(int p_idx) {
src_mat2 = Object::cast_to<ShaderMaterial>(object);
}
- if (src_mat2 && src_mat2->get_shader().is_valid() && src_mat2->get_shader() == visual_shader) {
+ if (src_mat2 && src_mat2->get_shader().is_valid() && src_mat2->get_shader() == edited_res) {
src_mat = src_mat2;
break;
}
@@ -2085,8 +2076,8 @@ void VisualShaderEditor::_preview_tools_menu_option(int p_idx) {
List<PropertyInfo> params;
preview_material->get_shader()->get_shader_uniform_list(&params);
for (const PropertyInfo &E : params) {
- undo_redo->add_do_method(visual_shader.ptr(), "_set_preview_shader_parameter", E.name, src_mat->get_shader_parameter(E.name));
- undo_redo->add_undo_method(visual_shader.ptr(), "_set_preview_shader_parameter", E.name, preview_material->get_shader_parameter(E.name));
+ undo_redo->add_do_method(edited_res.ptr(), "_set_preview_shader_parameter", E.name, src_mat->get_shader_parameter(E.name));
+ undo_redo->add_undo_method(edited_res.ptr(), "_set_preview_shader_parameter", E.name, preview_material->get_shader_parameter(E.name));
}
undo_redo->commit_action();
@@ -2127,6 +2118,7 @@ void VisualShaderEditor::_update_preview_parameter_list() {
material_editor->edit(preview_material.ptr(), env);
List<PropertyInfo> properties;
+ Ref<VisualShader> visual_shader = edited_res;
RenderingServer::get_singleton()->get_shader_parameter_list(visual_shader->get_rid(), &properties);
HashSet<String> params_to_remove;
@@ -2173,6 +2165,7 @@ void VisualShaderEditor::_update_preview_parameter_list() {
void VisualShaderEditor::_update_nodes() {
clear_custom_types();
Dictionary added;
+ Ref<VisualShader> visual_shader = edited_res;
// Add GDScript classes.
{
@@ -2293,6 +2286,7 @@ void VisualShaderEditor::_update_options_menu() {
int current_func = -1;
+ Ref<VisualShader> visual_shader = edited_res;
if (visual_shader.is_valid()) {
current_func = visual_shader->get_mode();
}
@@ -2583,6 +2577,7 @@ void VisualShaderEditor::_draw_color_over_button(Object *p_obj, Color p_color) {
}
void VisualShaderEditor::_update_parameters(bool p_update_refs) {
+ Ref<VisualShader> visual_shader = edited_res;
VisualShaderNodeParameterRef::clear_parameters(visual_shader->get_rid());
for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
@@ -2635,6 +2630,7 @@ void VisualShaderEditor::_update_parameters(bool p_update_refs) {
void VisualShaderEditor::_update_parameter_refs(HashSet<String> &p_deleted_names) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
+ Ref<VisualShader> visual_shader = edited_res;
for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
VisualShader::Type type = VisualShader::Type(i);
@@ -2656,7 +2652,7 @@ void VisualShaderEditor::_update_parameter_refs(HashSet<String> &p_deleted_names
}
void VisualShaderEditor::_update_graph() {
- if (visual_shader.is_null()) {
+ if (edited_res.is_null()) {
return;
}
@@ -2678,6 +2674,7 @@ void VisualShaderEditor::_update_graph() {
}
List<VisualShader::Connection> node_connections;
+ Ref<VisualShader> visual_shader = edited_res;
visual_shader->get_node_connections(type, &node_connections);
graph_plugin->set_connections(node_connections);
@@ -2736,7 +2733,7 @@ void VisualShaderEditor::_restore_editor_state() {
}
String VisualShaderEditor::_get_cache_id_string() const {
- String id_string = visual_shader->get_path();
+ String id_string = edited_res->get_path();
const ResourceUID::ID uid = EditorFileSystem::get_singleton()->get_file_uid(id_string);
if (uid != ResourceUID::INVALID_ID) {
id_string = ResourceUID::get_singleton()->id_to_text(uid);
@@ -2751,6 +2748,7 @@ String VisualShaderEditor::_get_cache_key(const String &p_prop_name) const {
void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -2767,6 +2765,7 @@ void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type
void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -2783,6 +2782,7 @@ void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_typ
void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -2799,6 +2799,7 @@ void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_p
void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -2816,6 +2817,7 @@ void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_
void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *p_line_edit, int p_node_id, int p_port_id) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
ERR_FAIL_COND(node.is_null());
@@ -2843,6 +2845,7 @@ void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *p
void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *p_line_edit, int p_node_id, int p_port_id) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
ERR_FAIL_COND(node.is_null());
@@ -2870,6 +2873,7 @@ void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *
void VisualShaderEditor::_expand_output_port(int p_node, int p_port, bool p_expand) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
ERR_FAIL_COND(node.is_null());
@@ -2966,6 +2970,7 @@ void VisualShaderEditor::_expand_output_port(int p_node, int p_port, bool p_expa
void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3016,6 +3021,7 @@ void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3077,6 +3083,7 @@ void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
void VisualShaderEditor::_expression_focus_out(Object *p_code_edit, int p_node) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3099,6 +3106,7 @@ void VisualShaderEditor::_expression_focus_out(Object *p_code_edit, int p_node)
void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
VisualShader::Type type = VisualShader::Type(p_type);
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3137,6 +3145,7 @@ void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p
// Called once after the node was resized.
void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(VisualShader::Type(p_type), p_node);
if (node.is_null()) {
return;
@@ -3158,6 +3167,7 @@ void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, in
void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3177,6 +3187,7 @@ void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
void VisualShaderEditor::_frame_title_popup_show(const Point2 &p_position, int p_node_id) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, p_node_id);
if (node.is_null()) {
return;
@@ -3208,6 +3219,7 @@ void VisualShaderEditor::_frame_title_popup_hide() {
ERR_FAIL_COND(node_id == -1);
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, node_id);
ERR_FAIL_COND(node.is_null());
@@ -3239,6 +3251,7 @@ void VisualShaderEditor::_frame_color_enabled_changed(int p_node_id) {
}
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, p_node_id);
ERR_FAIL_COND(node.is_null());
@@ -3254,6 +3267,7 @@ void VisualShaderEditor::_frame_color_enabled_changed(int p_node_id) {
void VisualShaderEditor::_frame_color_popup_show(const Point2 &p_position, int p_node_id) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, p_node_id);
if (node.is_null()) {
return;
@@ -3273,6 +3287,7 @@ void VisualShaderEditor::_frame_color_changed(const Color &p_color) {
int node_id = (int)frame_tint_color_pick_popup->get_meta("id");
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, node_id);
ERR_FAIL_COND(node.is_null());
node->set_tint_color(p_color);
@@ -3284,6 +3299,7 @@ void VisualShaderEditor::_frame_color_popup_hide() {
int node_id = (int)frame_tint_color_pick_popup->get_meta("id");
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, node_id);
ERR_FAIL_COND(node.is_null());
@@ -3308,6 +3324,7 @@ void VisualShaderEditor::_frame_autoshrink_enabled_changed(int p_node_id) {
popup_menu->set_item_checked(item_index, !autoshrink_enabled);
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFrame> node = visual_shader->get_node(type, p_node_id);
ERR_FAIL_COND(node.is_null());
@@ -3326,6 +3343,7 @@ void VisualShaderEditor::_frame_autoshrink_enabled_changed(int p_node_id) {
void VisualShaderEditor::_parameter_line_edit_changed(const String &p_text, int p_node_id) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeParameter> node = visual_shader->get_node(type, p_node_id);
ERR_FAIL_COND(node.is_null());
@@ -3368,6 +3386,7 @@ void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id,
void VisualShaderEditor::_port_edited(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
ERR_FAIL_COND(vsn.is_null());
@@ -3389,6 +3408,7 @@ void VisualShaderEditor::_port_edited(const StringName &p_property, const Varian
void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> vs_node = visual_shader->get_node(type, p_node);
Variant value = vs_node->get_input_port_default_value(p_port);
@@ -3449,6 +3469,7 @@ void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node,
void VisualShaderEditor::_set_custom_node_option(int p_index, int p_node, int p_op) {
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeCustom> node = visual_shader->get_node(type, p_node);
if (node.is_null()) {
return;
@@ -3817,6 +3838,7 @@ void VisualShaderEditor::_add_node(int p_idx, const Vector<Variant> &p_ops, cons
bool is_custom = add_options[p_idx].is_custom;
+ Ref<VisualShader> visual_shader = edited_res;
if (!is_custom && !add_options[p_idx].type.is_empty()) {
VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instantiate(add_options[p_idx].type));
ERR_FAIL_NULL(vsn);
@@ -4104,12 +4126,13 @@ void VisualShaderEditor::_add_varying(const String &p_name, VisualShader::Varyin
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(vformat(TTR("Add Varying to Visual Shader: %s"), p_name));
- undo_redo->add_do_method(visual_shader.ptr(), "add_varying", p_name, p_mode, p_type);
- undo_redo->add_undo_method(visual_shader.ptr(), "remove_varying", p_name);
+ undo_redo->add_do_method(edited_res.ptr(), "add_varying", p_name, p_mode, p_type);
+ undo_redo->add_undo_method(edited_res.ptr(), "remove_varying", p_name);
undo_redo->add_do_method(this, "_update_varyings");
undo_redo->add_undo_method(this, "_update_varyings");
+ Ref<VisualShader> visual_shader = edited_res;
for (int i = 0; i <= VisualShader::TYPE_LIGHT; i++) {
if (p_mode == VisualShader::VARYING_MODE_FRAG_TO_LIGHT && i == 0) {
continue;
@@ -4139,6 +4162,7 @@ void VisualShaderEditor::_remove_varying(const String &p_name) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(vformat(TTR("Remove Varying from Visual Shader: %s"), p_name));
+ Ref<VisualShader> visual_shader = edited_res;
VisualShader::VaryingMode var_mode = visual_shader->get_varying_mode(p_name);
undo_redo->add_do_method(visual_shader.ptr(), "remove_varying", p_name);
@@ -4201,6 +4225,7 @@ void VisualShaderEditor::_remove_varying(const String &p_name) {
}
void VisualShaderEditor::_update_varyings() {
+ Ref<VisualShader> visual_shader = edited_res;
VisualShaderNodeVarying::clear_varyings(visual_shader->get_rid());
for (int i = 0; i < visual_shader->get_varyings_count(); i++) {
@@ -4231,6 +4256,7 @@ void VisualShaderEditor::_nodes_dragged() {
undo_redo->create_action(TTR("Move and Attach VisualShader Node(s) to parent frame"));
}
+ Ref<VisualShader> visual_shader = edited_res;
for (const DragOp &E : drag_buffer) {
undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E.type, E.node, E.to);
undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E.type, E.node, E.from);
@@ -4264,6 +4290,7 @@ void VisualShaderEditor::_connection_request(const String &p_from, int p_from_in
int to = p_to.to_int();
bool swap = last_to_node != -1 && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
+ Ref<VisualShader> visual_shader = edited_res;
if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
return;
}
@@ -4319,6 +4346,7 @@ void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from
info_label->show();
+ Ref<VisualShader> visual_shader = edited_res;
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Nodes Disconnected"));
undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
@@ -4339,6 +4367,7 @@ void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_s
from_slot = p_from_slot;
VisualShaderNode::PortType input_port_type = VisualShaderNode::PORT_TYPE_MAX;
VisualShaderNode::PortType output_port_type = VisualShaderNode::PORT_TYPE_MAX;
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node(get_current_shader_type(), from_node);
if (node.is_valid()) {
output_port_type = node->get_output_port_type(from_slot);
@@ -4351,6 +4380,7 @@ void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slo
to_slot = p_to_slot;
VisualShaderNode::PortType input_port_type = VisualShaderNode::PORT_TYPE_MAX;
VisualShaderNode::PortType output_port_type = VisualShaderNode::PORT_TYPE_MAX;
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node(get_current_shader_type(), to_node);
if (node.is_valid()) {
input_port_type = node->get_input_port_type(to_slot);
@@ -4367,6 +4397,7 @@ bool VisualShaderEditor::_check_node_drop_on_connection(const Vector2 &p_positio
int selected_node_count = 0;
Rect2 selected_node_rect;
+ Ref<VisualShader> visual_shader = edited_res;
for (int i = 0; i < graph->get_child_count(); i++) {
GraphNode *graph_node = Object::cast_to<GraphNode>(graph->get_child(i));
if (graph_node && graph_node->is_selected()) {
@@ -4461,6 +4492,7 @@ void VisualShaderEditor::_handle_node_drop_on_connection() {
int selected_node_id = drag_buffer.front()->get().node;
VisualShader::Type shader_type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> selected_vsnode = visual_shader->get_node(shader_type, selected_node_id);
// Delete the old connection.
@@ -4489,6 +4521,7 @@ void VisualShaderEditor::_handle_node_drop_on_connection() {
void VisualShaderEditor::_delete_nodes(int p_type, const List<int> &p_nodes) {
VisualShader::Type type = VisualShader::Type(p_type);
List<VisualShader::Connection> conns;
+ Ref<VisualShader> visual_shader = edited_res;
visual_shader->get_node_connections(type, &conns);
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
@@ -4587,11 +4620,12 @@ void VisualShaderEditor::_delete_nodes(int p_type, const List<int> &p_nodes) {
void VisualShaderEditor::_replace_node(VisualShader::Type p_type_id, int p_node_id, const StringName &p_from, const StringName &p_to) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
- undo_redo->add_do_method(visual_shader.ptr(), "replace_node", p_type_id, p_node_id, p_to);
- undo_redo->add_undo_method(visual_shader.ptr(), "replace_node", p_type_id, p_node_id, p_from);
+ undo_redo->add_do_method(edited_res.ptr(), "replace_node", p_type_id, p_node_id, p_to);
+ undo_redo->add_undo_method(edited_res.ptr(), "replace_node", p_type_id, p_node_id, p_from);
}
void VisualShaderEditor::_update_constant(VisualShader::Type p_type_id, int p_node_id, const Variant &p_var, int p_preview_port) {
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node(p_type_id, p_node_id);
ERR_FAIL_COND(node.is_null());
ERR_FAIL_COND(!node->has_method("set_constant"));
@@ -4602,6 +4636,7 @@ void VisualShaderEditor::_update_constant(VisualShader::Type p_type_id, int p_no
}
void VisualShaderEditor::_update_parameter(VisualShader::Type p_type_id, int p_node_id, const Variant &p_var, int p_preview_port) {
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeParameter> parameter = visual_shader->get_node(p_type_id, p_node_id);
ERR_FAIL_COND(parameter.is_null());
@@ -4631,6 +4666,7 @@ void VisualShaderEditor::_convert_constants_to_parameters(bool p_vice_versa) {
const HashSet<int> &current_set = p_vice_versa ? selected_parameters : selected_constants;
HashSet<String> deleted_names;
+ Ref<VisualShader> visual_shader = edited_res;
for (const int &E : current_set) {
int node_id = E;
Ref<VisualShaderNode> node = visual_shader->get_node(type_id, node_id);
@@ -4818,6 +4854,7 @@ void VisualShaderEditor::_convert_constants_to_parameters(bool p_vice_versa) {
void VisualShaderEditor::_detach_nodes_from_frame(int p_type, const List<int> &p_nodes) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
+ Ref<VisualShader> visual_shader = edited_res;
for (int node_id : p_nodes) {
Ref<VisualShaderNode> node = visual_shader->get_node((VisualShader::Type)p_type, node_id);
if (node.is_null()) {
@@ -4856,6 +4893,7 @@ void VisualShaderEditor::_detach_nodes_from_frame_request() {
}
void VisualShaderEditor::_delete_node_request(int p_type, int p_node) {
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node = visual_shader->get_node((VisualShader::Type)p_type, p_node);
if (!node->is_deletable()) {
return;
@@ -4873,6 +4911,7 @@ void VisualShaderEditor::_delete_node_request(int p_type, int p_node) {
void VisualShaderEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
List<int> to_erase;
+ Ref<VisualShader> visual_shader = edited_res;
if (p_nodes.is_empty()) {
// Called from context menu.
for (int i = 0; i < graph->get_child_count(); i++) {
@@ -4917,6 +4956,7 @@ void VisualShaderEditor::_node_selected(Object *p_node) {
int id = String(graph_element->get_name()).to_int();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
ERR_FAIL_COND(vsnode.is_null());
}
@@ -4945,6 +4985,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
List<int> selected_deletable_graph_elements;
List<GraphElement *> selected_graph_elements;
+ Ref<VisualShader> visual_shader = edited_res;
for (int i = 0; i < graph->get_child_count(); i++) {
GraphElement *graph_element = Object::cast_to<GraphElement>(graph->get_child(i));
if (!graph_element) {
@@ -5217,8 +5258,8 @@ void VisualShaderEditor::_param_property_changed(const String &p_property, const
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(vformat(TTR("Edit Preview Parameter: %s"), p_property));
- undo_redo->add_do_method(visual_shader.ptr(), "_set_preview_shader_parameter", raw_prop_name, p_value);
- undo_redo->add_undo_method(visual_shader.ptr(), "_set_preview_shader_parameter", raw_prop_name, preview_material->get(p_property));
+ undo_redo->add_do_method(edited_res.ptr(), "_set_preview_shader_parameter", raw_prop_name, p_value);
+ undo_redo->add_undo_method(edited_res.ptr(), "_set_preview_shader_parameter", raw_prop_name, preview_material->get(p_property));
undo_redo->add_do_method(this, "_update_current_param");
undo_redo->add_undo_method(this, "_update_current_param");
undo_redo->commit_action();
@@ -5227,6 +5268,7 @@ void VisualShaderEditor::_param_property_changed(const String &p_property, const
void VisualShaderEditor::_update_current_param() {
if (current_prop != nullptr) {
String name = current_prop->get_meta("id");
+ Ref<VisualShader> visual_shader = edited_res;
if (visual_shader->_has_preview_shader_parameter(name)) {
preview_material->set("shader_parameter/" + name, visual_shader->_get_preview_shader_parameter(name));
} else {
@@ -5271,10 +5313,6 @@ void VisualShaderEditor::_param_unselected() {
_clear_preview_param();
}
-void VisualShaderEditor::_help_open() {
- OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/visual_shaders.html", GODOT_VERSION_DOCS_URL));
-}
-
void VisualShaderEditor::_notification(int p_what) {
switch (p_what) {
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
@@ -5310,7 +5348,6 @@ void VisualShaderEditor::_notification(int p_what) {
} break;
case NOTIFICATION_THEME_CHANGED: {
- site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
highend_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
param_filter->set_right_icon(Control::get_editor_theme_icon(SNAME("Search")));
@@ -5429,6 +5466,7 @@ void VisualShaderEditor::_frame_rect_changed(const GraphFrame *p_frame, const Re
}
int node_id = String(p_frame->get_name()).to_int();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeResizableBase> vsnode = visual_shader->get_node(get_current_shader_type(), node_id);
if (vsnode.is_null()) {
return;
@@ -5444,6 +5482,7 @@ void VisualShaderEditor::_dup_copy_nodes(int p_type, List<CopyItem> &r_items, Li
HashSet<int> nodes;
+ Ref<VisualShader> visual_shader = edited_res;
for (int i = 0; i < graph->get_child_count(); i++) {
GraphElement *graph_element = Object::cast_to<GraphElement>(graph->get_child(i));
if (graph_element) {
@@ -5520,6 +5559,7 @@ void VisualShaderEditor::_dup_paste_nodes(int p_type, List<CopyItem> &r_items, c
VisualShader::Type type = (VisualShader::Type)p_type;
+ Ref<VisualShader> visual_shader = edited_res;
int base_id = visual_shader->get_valid_node_id(type);
int id_from = base_id;
HashMap<int, int> connection_remap; // Used for connections and frame attachments.
@@ -5741,6 +5781,7 @@ void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> p_input,
undo_redo_man->add_undo_method(p_input.ptr(), "set_input_name", prev_name);
if (type_changed) {
+ Ref<VisualShader> visual_shader = edited_res;
for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
VisualShader::Type type = VisualShader::Type(type_id);
@@ -5810,6 +5851,7 @@ void VisualShaderEditor::_parameter_ref_select_item(Ref<VisualShaderNodeParamete
undo_redo_man->add_undo_method(p_parameter_ref.ptr(), "set_parameter_name", prev_name);
// update output port
+ Ref<VisualShader> visual_shader = edited_res;
for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
VisualShader::Type type = VisualShader::Type(type_id);
int id = visual_shader->find_node_id(type, p_parameter_ref);
@@ -5866,6 +5908,7 @@ void VisualShaderEditor::_varying_select_item(Ref<VisualShaderNodeVarying> p_var
// update ports
for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
VisualShader::Type type = VisualShader::Type(type_id);
+ Ref<VisualShader> visual_shader = edited_res;
int id = visual_shader->find_node_id(type, p_varying);
if (id != VisualShader::NODE_ID_INVALID) {
@@ -5911,6 +5954,7 @@ void VisualShaderEditor::_float_constant_selected(int p_which) {
ERR_FAIL_INDEX(p_which, MAX_FLOAT_CONST_DEFS);
VisualShader::Type type = get_current_shader_type();
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNodeFloatConstant> node = visual_shader->get_node(type, selected_float_constant);
ERR_FAIL_COND(node.is_null());
@@ -5985,6 +6029,7 @@ void VisualShaderEditor::_update_varying_tree() {
varyings->clear();
TreeItem *root = varyings->create_item();
+ Ref<VisualShader> visual_shader = edited_res;
int count = visual_shader->get_varyings_count();
for (int i = 0; i < count; i++) {
@@ -6042,6 +6087,7 @@ void VisualShaderEditor::_varying_validate() {
String error;
String varname = varying_name->get_text();
+ Ref<VisualShader> visual_shader = edited_res;
if (!varname.is_valid_ascii_identifier()) {
error += TTR("Invalid name for varying.");
has_error = true;
@@ -6189,8 +6235,8 @@ void VisualShaderEditor::_connection_menu_id_pressed(int p_idx) {
case ConnectionMenuOptions::DISCONNECT: {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Disconnect"));
- undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
- undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
+ undo_redo->add_do_method(edited_res.ptr(), "disconnect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
+ undo_redo->add_undo_method(edited_res.ptr(), "connect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", get_current_shader_type(), String(clicked_connection->from_node).to_int(), clicked_connection->from_port, String(clicked_connection->to_node).to_int(), clicked_connection->to_port);
undo_redo->commit_action();
@@ -6198,6 +6244,7 @@ void VisualShaderEditor::_connection_menu_id_pressed(int p_idx) {
case ConnectionMenuOptions::INSERT_NEW_NODE: {
VisualShaderNode::PortType input_port_type = VisualShaderNode::PORT_TYPE_MAX;
VisualShaderNode::PortType output_port_type = VisualShaderNode::PORT_TYPE_MAX;
+ Ref<VisualShader> visual_shader = edited_res;
Ref<VisualShaderNode> node1 = visual_shader->get_node(get_current_shader_type(), String(clicked_connection->from_node).to_int());
if (node1.is_valid()) {
output_port_type = node1->get_output_port_type(from_slot);
@@ -6300,6 +6347,7 @@ void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add Node(s) to Visual Shader"));
+ Ref<VisualShader> visual_shader = edited_res;
if (d["files"].get_type() == Variant::PACKED_STRING_ARRAY) {
PackedStringArray arr = d["files"];
for (int i = 0; i < arr.size(); i++) {
@@ -6397,6 +6445,7 @@ void VisualShaderEditor::_update_preview() {
return;
}
+ Ref<VisualShader> visual_shader = edited_res;
String code = visual_shader->get_code();
preview_text->set_text(code);
@@ -6475,6 +6524,7 @@ void VisualShaderEditor::_update_next_previews(int p_node_id) {
}
void VisualShaderEditor::_get_next_nodes_recursively(VisualShader::Type p_type, int p_node_id, LocalVector<int> &r_nodes) const {
+ Ref<VisualShader> visual_shader = edited_res;
const LocalVector<int> &next_connections = visual_shader->get_next_connected_nodes(p_type, p_node_id);
for (int node_id : next_connections) {
@@ -6531,6 +6581,17 @@ void VisualShaderEditor::_bind_methods() {
ClassDB::bind_method("_update_current_param", &VisualShaderEditor::_update_current_param);
}
+ScriptEditorBase *VisualShaderEditor::create_editor(const Ref<Resource> &p_resource) {
+ if (Object::cast_to<VisualShader>(*p_resource)) {
+ return memnew(VisualShaderEditor);
+ }
+ return nullptr;
+}
+
+void VisualShaderEditor::register_editor() {
+ ScriptEditor::register_create_script_editor_function(create_editor);
+}
+
VisualShaderEditor::VisualShaderEditor() {
vs_editor_cache.instantiate();
vs_editor_cache->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("vs_editor_cache.cfg"));
@@ -6542,6 +6603,7 @@ VisualShaderEditor::VisualShaderEditor() {
HSplitContainer *main_box = memnew(HSplitContainer);
main_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
+ main_box->set_v_size_flags(SIZE_EXPAND_FILL);
add_child(main_box);
graph = memnew(GraphEdit);
@@ -6762,25 +6824,10 @@ VisualShaderEditor::VisualShaderEditor() {
toolbar_hflow->add_child(shader_preview_button);
shader_preview_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_show_shader_preview));
- Control *spacer = memnew(Control);
- spacer->set_h_size_flags(Control::SIZE_EXPAND);
- toolbar_hflow->add_child(spacer);
-
- site_search = memnew(Button);
- site_search->set_theme_type_variation(SceneStringName(FlatButton));
- site_search->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_help_open));
- site_search->set_text(TTR("Online Docs"));
- site_search->set_tooltip_text(TTR("Open Godot online documentation."));
- toolbar_hflow->add_child(site_search);
-
VSeparator *separator = memnew(VSeparator);
toolbar_hflow->add_child(separator);
toolbar_hflow->move_child(separator, 0);
- separator = memnew(VSeparator);
- toolbar_hflow->add_child(separator);
- toolbar_hflow->move_child(separator, 0);
-
toggle_files_button = memnew(Button);
toggle_files_button->set_theme_type_variation(SceneStringName(FlatButton));
toggle_files_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_toggle_files_pressed));
@@ -7829,6 +7876,9 @@ VisualShaderEditor::VisualShaderEditor() {
VisualShaderEditor::~VisualShaderEditor() {
save_editor_layout();
+ if (EditorNode::get_editor_data().get_edited_scene_count() > 0) { // Avoid crash on shutdown.
+ EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
+ }
}
class VisualShaderNodePluginInputEditor : public OptionButton {
@@ -8048,7 +8098,8 @@ public:
undo_redo->add_do_property(node.ptr(), p_property, p_value);
undo_redo->add_undo_property(node.ptr(), p_property, node->get(p_property));
- Ref<VisualShaderNode> vsnode = editor->get_visual_shader()->get_node(shader_type, node_id);
+ Ref<VisualShader> visual_shader = editor->get_edited_resource();
+ Ref<VisualShaderNode> vsnode = visual_shader->get_node(shader_type, node_id);
ERR_FAIL_COND(vsnode.is_null());
// Check for invalid connections due to removed ports.
@@ -8060,17 +8111,17 @@ public:
const int output_port_count = vsnode_new->get_expanded_output_port_count();
List<VisualShader::Connection> conns;
- editor->get_visual_shader()->get_node_connections(shader_type, &conns);
+ visual_shader->get_node_connections(shader_type, &conns);
VisualShaderGraphPlugin *graph_plugin = editor->get_graph_plugin();
bool undo_node_already_updated = false;
for (const VisualShader::Connection &c : conns) {
if ((c.from_node == node_id && c.from_port >= output_port_count) || (c.to_node == node_id && c.to_port >= input_port_count)) {
- undo_redo->add_do_method(editor->get_visual_shader().ptr(), "disconnect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
+ undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
undo_redo->add_do_method(graph_plugin, "disconnect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
// We need to update the node before reconnecting to avoid accessing a non-existing port.
undo_redo->add_undo_method(graph_plugin, "update_node_deferred", shader_type, node_id);
undo_node_already_updated = true;
- undo_redo->add_undo_method(editor->get_visual_shader().ptr(), "connect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
+ undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
undo_redo->add_undo_method(graph_plugin, "connect_nodes", shader_type, c.from_node, c.from_port, c.to_node, c.to_port);
}
}
@@ -8270,11 +8321,7 @@ void EditorPropertyVisualShaderMode::_option_selected(int p_which) {
return;
}
- ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_editor_data().get_editor_by_name("Shader"));
- if (!shader_editor) {
- return;
- }
- VisualShaderEditor *editor = Object::cast_to<VisualShaderEditor>(shader_editor->get_shader_editor(visual_shader));
+ VisualShaderEditor *editor = Object::cast_to<VisualShaderEditor>(ScriptEditor::get_bottom_script_editor()->get_resource_editor(visual_shader));
if (!editor) {
return;
}
diff --git a/modules/visual_shader/editor/visual_shader_editor_plugin.h b/modules/visual_shader/editor/visual_shader_editor_plugin.h
index 0be10630b43..0c1ffed9e18 100644
--- a/modules/visual_shader/editor/visual_shader_editor_plugin.h
+++ b/modules/visual_shader/editor/visual_shader_editor_plugin.h
@@ -33,7 +33,7 @@
#include "editor/inspector/editor_properties.h"
#include "editor/plugins/editor_plugin.h"
#include "editor/plugins/editor_resource_conversion_plugin.h"
-#include "editor/shader/shader_editor.h"
+#include "editor/script/script_editor_base.h"
#include "scene/gui/graph_edit.h"
#include "scene/resources/environment.h"
#include "scene/resources/material.h"
@@ -198,8 +198,8 @@ public:
Variant get_edited_property() const;
};
-class VisualShaderEditor : public ShaderEditor {
- GDCLASS(VisualShaderEditor, ShaderEditor);
+class VisualShaderEditor : public ScriptEditorBase {
+ GDCLASS(VisualShaderEditor, ScriptEditorBase);
friend class VisualShaderGraphPlugin;
Ref<ConfigFile> vs_editor_cache; // Keeps the graph offsets and zoom levels for each VisualShader that has been edited.
@@ -211,7 +211,6 @@ class VisualShaderEditor : public ShaderEditor {
Ref<VisualShaderEditedProperty> edited_property_holder;
MaterialEditor *material_editor = nullptr;
- Ref<VisualShader> visual_shader;
Ref<ShaderMaterial> preview_material;
Ref<Environment> env;
String param_filter_name;
@@ -655,20 +654,25 @@ class VisualShaderEditor : public ShaderEditor {
void _param_selected();
void _param_unselected();
- void _help_open();
+ static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
- virtual void edit_shader(const Ref<Shader> &p_shader) override;
- virtual void use_menu_bar(MenuButton *p_file_menu) override;
- virtual void apply_shaders() override;
- virtual bool is_unsaved() const override;
- virtual void save_external_data(const String &p_str = "") override;
+ virtual String get_doc_url_path() override { return "/tutorials/shaders/visual_shaders.html"; }
+
+ virtual void set_edited_resource(const Ref<Resource> &p_shader) override;
+ virtual void apply_code() override;
+ virtual bool is_unsaved() override;
virtual void validate_script() override;
+ virtual void ensure_focus() override { graph->grab_focus(true); }
+
+ virtual void set_toggle_list_control(Control *p_toggle_list_control) override;
+
+ virtual void update_toggle_files_button() override;
void save_editor_layout();
void set_current_shader_type(VisualShader::Type p_type);
@@ -681,14 +685,11 @@ public:
void clear_custom_types();
void add_custom_type(const String &p_name, const String &p_type, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, bool p_highend);
- virtual void set_toggle_list_control(Control *p_toggle_list_control) override;
Dictionary get_custom_node_data(Ref<VisualShaderNodeCustom> &p_custom_node);
void update_custom_type(const Ref<Resource> &p_resource);
- virtual void update_toggle_files_button() override;
-
- Ref<VisualShader> get_visual_shader() const { return visual_shader; }
+ static void register_editor();
VisualShaderEditor();
~VisualShaderEditor();
diff --git a/modules/visual_shader/editor/visual_shader_language_plugin.cpp b/modules/visual_shader/editor/visual_shader_language_plugin.cpp
index 2d4d86977c1..3402f91648b 100644
--- a/modules/visual_shader/editor/visual_shader_language_plugin.cpp
+++ b/modules/visual_shader/editor/visual_shader_language_plugin.cpp
@@ -30,19 +30,7 @@
#include "visual_shader_language_plugin.h"
-#include "visual_shader_editor_plugin.h"
-
-bool VisualShaderLanguagePlugin::handles_shader(const Ref<Shader> &p_shader) const {
- return Object::cast_to<VisualShader>(p_shader.ptr()) != nullptr;
-}
-
-ShaderEditor *VisualShaderLanguagePlugin::edit_shader(const Ref<Shader> &p_shader) {
- const Ref<VisualShader> shader = p_shader;
- ERR_FAIL_COND_V(shader.is_null(), nullptr);
- VisualShaderEditor *editor = memnew(VisualShaderEditor);
- editor->edit_shader(shader);
- return editor;
-}
+#include "modules/visual_shader/visual_shader.h"
Ref<Shader> VisualShaderLanguagePlugin::create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) {
Ref<VisualShader> shader;
@@ -50,9 +38,3 @@ Ref<Shader> VisualShaderLanguagePlugin::create_new_shader(int p_variation_index,
shader->set_mode(p_shader_mode);
return shader;
}
-
-PackedStringArray VisualShaderLanguagePlugin::get_language_variations() const {
- PackedStringArray variations;
- variations.push_back("VisualShader");
- return variations;
-}
diff --git a/modules/visual_shader/editor/visual_shader_language_plugin.h b/modules/visual_shader/editor/visual_shader_language_plugin.h
index fbff6bdc879..dfbdc6fe53f 100644
--- a/modules/visual_shader/editor/visual_shader_language_plugin.h
+++ b/modules/visual_shader/editor/visual_shader_language_plugin.h
@@ -36,8 +36,6 @@ class VisualShaderLanguagePlugin : public EditorShaderLanguagePlugin {
GDCLASS(VisualShaderLanguagePlugin, EditorShaderLanguagePlugin);
public:
- virtual bool handles_shader(const Ref<Shader> &p_shader) const override;
- virtual ShaderEditor *edit_shader(const Ref<Shader> &p_shader) override;
virtual Ref<Shader> create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) override;
- virtual PackedStringArray get_language_variations() const override;
+ virtual PackedStringArray get_language_variations() const override { return { "VisualShader" }; }
};
diff --git a/modules/visual_shader/register_types.cpp b/modules/visual_shader/register_types.cpp
index 6f26143e9ea..5b5950f70b4 100644
--- a/modules/visual_shader/register_types.cpp
+++ b/modules/visual_shader/register_types.cpp
@@ -54,6 +54,8 @@ static void _editor_init() {
Ref<VisualShaderLanguagePlugin> visual_shader_lang;
visual_shader_lang.instantiate();
EditorShaderLanguagePlugin::register_shader_language(visual_shader_lang);
+
+ VisualShaderEditor::register_editor();
}
#endif // TOOLS_ENABLED