Optimized ScriptEditor initialization when many scripts are loaded

This change avoids the editor to freeze for several seconds when a
project with lots of scripts is loaded in the editor.

It focuses on a few heavy operations previously executed on all
previously loaded scripts:
- Initialize script resource (script validation/parsing) only
on focus
- ScriptTextEditor: code editor and edit menu are added to the
scene only on focus
- Add to recent scripts only when opening new scripts
(load/save scene metadata)
This commit is contained in:
PouleyKetchoupp 2020-07-15 16:15:29 +02:00
parent 89f57ae122
commit e3765e97de
9 changed files with 226 additions and 124 deletions

View file

@ -1462,8 +1462,6 @@ void CodeTextEditor::set_edit_state(const Variant &p_state) {
text_editor->set_line_as_bookmark(bookmarks[i], true); text_editor->set_line_as_bookmark(bookmarks[i], true);
} }
} }
text_editor->grab_focus();
} }
void CodeTextEditor::set_error(const String &p_error) { void CodeTextEditor::set_error(const String &p_error) {

View file

@ -580,7 +580,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
Node *tselected = tab_container->get_child(selected); Node *tselected = tab_container->get_child(selected);
ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tab_container->get_child(selected)); ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tselected);
if (current) { if (current) {
Ref<Script> script = current->get_edited_resource(); Ref<Script> script = current->get_edited_resource();
if (p_save) { if (p_save) {
@ -666,8 +666,10 @@ void ScriptEditor::_close_docs_tab() {
void ScriptEditor::_copy_script_path() { void ScriptEditor::_copy_script_path() {
ScriptEditorBase *se = _get_current_editor(); ScriptEditorBase *se = _get_current_editor();
RES script = se->get_edited_resource(); if (se) {
OS::get_singleton()->set_clipboard(script->get_path()); RES script = se->get_edited_resource();
OS::get_singleton()->set_clipboard(script->get_path());
}
} }
void ScriptEditor::_close_other_tabs() { void ScriptEditor::_close_other_tabs() {
@ -943,17 +945,19 @@ void ScriptEditor::_file_dialog_action(String p_file) {
} break; } break;
case FILE_SAVE_AS: { case FILE_SAVE_AS: {
ScriptEditorBase *current = _get_current_editor(); ScriptEditorBase *current = _get_current_editor();
if (current) {
RES resource = current->get_edited_resource();
String path = ProjectSettings::get_singleton()->localize_path(p_file);
Error err = _save_text_file(resource, path);
String path = ProjectSettings::get_singleton()->localize_path(p_file); if (err != OK) {
Error err = _save_text_file(current->get_edited_resource(), path); editor->show_accept(TTR("Error saving file!"), TTR("OK"));
return;
}
if (err != OK) { resource->set_path(path);
editor->show_accept(TTR("Error saving file!"), TTR("OK")); _update_script_names();
return;
} }
((Resource *)current->get_edited_resource().ptr())->set_path(path);
_update_script_names();
} break; } break;
case THEME_SAVE_AS: { case THEME_SAVE_AS: {
if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) { if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
@ -1170,13 +1174,14 @@ void ScriptEditor::_menu_option(int p_option) {
} }
} }
Ref<TextFile> text_file = current->get_edited_resource(); RES resource = current->get_edited_resource();
Ref<TextFile> text_file = resource;
if (text_file != NULL) { if (text_file != NULL) {
current->apply_code(); current->apply_code();
_save_text_file(text_file, text_file->get_path()); _save_text_file(text_file, text_file->get_path());
break; break;
} }
editor->save_resource(current->get_edited_resource()); editor->save_resource(resource);
} break; } break;
case FILE_SAVE_AS: { case FILE_SAVE_AS: {
@ -1194,7 +1199,8 @@ void ScriptEditor::_menu_option(int p_option) {
} }
} }
Ref<TextFile> text_file = current->get_edited_resource(); RES resource = current->get_edited_resource();
Ref<TextFile> text_file = resource;
if (text_file != NULL) { if (text_file != NULL) {
file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE); file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
@ -1210,8 +1216,8 @@ void ScriptEditor::_menu_option(int p_option) {
break; break;
} }
editor->push_item(Object::cast_to<Object>(current->get_edited_resource().ptr())); editor->push_item(resource.ptr());
editor->save_resource_as(current->get_edited_resource()); editor->save_resource_as(resource);
} break; } break;
@ -1615,9 +1621,11 @@ void ScriptEditor::ensure_select_current() {
ScriptEditorBase *se = _get_current_editor(); ScriptEditorBase *se = _get_current_editor();
if (se) { if (se) {
se->enable_editor();
if (!grab_focus_block && is_visible_in_tree()) if (!grab_focus_block && is_visible_in_tree()) {
se->ensure_focus(); se->ensure_focus();
}
} }
} }
@ -1954,6 +1962,11 @@ void ScriptEditor::_update_script_names() {
script_list->select(index); script_list->select(index);
script_name_label->set_text(sedata_filtered[i].name); script_name_label->set_text(sedata_filtered[i].name);
script_icon->set_texture(sedata_filtered[i].icon); script_icon->set_texture(sedata_filtered[i].icon);
ScriptEditorBase *se = _get_current_editor();
if (se) {
se->enable_editor();
_update_selected_editor_menu();
}
} }
} }
@ -2131,6 +2144,8 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
if ((script != NULL && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) { if ((script != NULL && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) {
if (should_open) { if (should_open) {
se->enable_editor();
if (tab_container->get_current_tab() != i) { if (tab_container->get_current_tab() != i) {
_go_to_tab(i); _go_to_tab(i);
_update_script_names(); _update_script_names();
@ -2159,6 +2174,8 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
} }
ERR_FAIL_COND_V(!se, false); ERR_FAIL_COND_V(!se, false);
se->set_edited_resource(p_resource);
if (p_resource->get_class_name() != StringName("VisualScript")) { if (p_resource->get_class_name() != StringName("VisualScript")) {
bool highlighter_set = false; bool highlighter_set = false;
for (int i = 0; i < syntax_highlighters_func_count; i++) { for (int i = 0; i < syntax_highlighters_func_count; i++) {
@ -2176,7 +2193,11 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
} }
tab_container->add_child(se); tab_container->add_child(se);
se->set_edited_resource(p_resource);
if (p_grab_focus) {
se->enable_editor();
}
se->set_tooltip_request_func("_get_debug_tooltip", this); se->set_tooltip_request_func("_get_debug_tooltip", this);
if (se->get_edit_menu()) { if (se->get_edit_menu()) {
se->get_edit_menu()->hide(); se->get_edit_menu()->hide();
@ -2186,6 +2207,7 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
if (p_grab_focus) { if (p_grab_focus) {
_go_to_tab(tab_container->get_tab_count() - 1); _go_to_tab(tab_container->get_tab_count() - 1);
_add_recent_script(p_resource->get_path());
} }
_sort_list_on_update = true; _sort_list_on_update = true;
@ -2209,7 +2231,6 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
} }
notify_script_changed(p_resource); notify_script_changed(p_resource);
_add_recent_script(p_resource->get_path());
return true; return true;
} }
@ -2708,7 +2729,7 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
if (!scr.is_valid()) { if (!scr.is_valid()) {
continue; continue;
} }
if (!edit(scr)) { if (!edit(scr, false)) {
continue; continue;
} }
} else { } else {
@ -2717,7 +2738,7 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
if (error != OK || !text_file.is_valid()) { if (error != OK || !text_file.is_valid()) {
continue; continue;
} }
if (!edit(text_file)) { if (!edit(text_file, false)) {
continue; continue;
} }
} }

View file

@ -90,6 +90,7 @@ public:
virtual RES get_edited_resource() const = 0; virtual RES get_edited_resource() const = 0;
virtual Vector<String> get_functions() = 0; virtual Vector<String> get_functions() = 0;
virtual void set_edited_resource(const RES &p_res) = 0; virtual void set_edited_resource(const RES &p_res) = 0;
virtual void enable_editor() = 0;
virtual void reload_text() = 0; virtual void reload_text() = 0;
virtual String get_name() = 0; virtual String get_name() = 0;
virtual Ref<Texture> get_icon() = 0; virtual Ref<Texture> get_icon() = 0;

View file

@ -142,10 +142,10 @@ RES ScriptTextEditor::get_edited_resource() const {
} }
void ScriptTextEditor::set_edited_resource(const RES &p_res) { void ScriptTextEditor::set_edited_resource(const RES &p_res) {
ERR_FAIL_COND(!script.is_null()); ERR_FAIL_COND(script.is_valid());
ERR_FAIL_COND(p_res.is_null());
script = p_res; script = p_res;
_set_theme_for_script();
code_editor->get_text_edit()->set_text(script->get_source_code()); code_editor->get_text_edit()->set_text(script->get_source_code());
code_editor->get_text_edit()->clear_undo_history(); code_editor->get_text_edit()->clear_undo_history();
@ -153,6 +153,17 @@ void ScriptTextEditor::set_edited_resource(const RES &p_res) {
emit_signal("name_changed"); emit_signal("name_changed");
code_editor->update_line_and_column(); code_editor->update_line_and_column();
}
void ScriptTextEditor::enable_editor() {
if (editor_enabled) {
return;
}
editor_enabled = true;
_enable_code_editor();
_set_theme_for_script();
_validate_script(); _validate_script();
} }
@ -411,15 +422,6 @@ void ScriptTextEditor::reload_text() {
code_editor->update_line_and_column(); code_editor->update_line_and_column();
} }
void ScriptTextEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY:
_load_theme_settings();
break;
}
}
void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) { void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
String code = code_editor->get_text_edit()->get_text(); String code = code_editor->get_text_edit()->get_text();
@ -468,6 +470,10 @@ void ScriptTextEditor::set_edit_state(const Variant &p_state) {
_change_syntax_highlighter(idx); _change_syntax_highlighter(idx);
} }
} }
if (editor_enabled) {
ensure_focus();
}
} }
void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) { void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
@ -1778,65 +1784,41 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p
context_menu->popup(); context_menu->popup();
} }
ScriptTextEditor::ScriptTextEditor() { void ScriptTextEditor::_enable_code_editor() {
ERR_FAIL_COND(code_editor->get_parent());
theme_loaded = false;
script_is_valid = false;
VSplitContainer *editor_box = memnew(VSplitContainer); VSplitContainer *editor_box = memnew(VSplitContainer);
add_child(editor_box); add_child(editor_box);
editor_box->set_anchors_and_margins_preset(Control::PRESET_WIDE); editor_box->set_anchors_and_margins_preset(Control::PRESET_WIDE);
editor_box->set_v_size_flags(SIZE_EXPAND_FILL); editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
code_editor = memnew(CodeTextEditor);
editor_box->add_child(code_editor); editor_box->add_child(code_editor);
code_editor->add_constant_override("separation", 2); code_editor->connect("show_warnings_panel", this, "_show_warnings_panel");
code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
code_editor->connect("validate_script", this, "_validate_script"); code_editor->connect("validate_script", this, "_validate_script");
code_editor->connect("load_theme_settings", this, "_load_theme_settings"); code_editor->connect("load_theme_settings", this, "_load_theme_settings");
code_editor->set_code_complete_func(_code_complete_scripts, this);
code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled"); code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol"); code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
code_editor->get_text_edit()->connect("info_clicked", this, "_lookup_connections"); code_editor->get_text_edit()->connect("info_clicked", this, "_lookup_connections");
code_editor->set_v_size_flags(SIZE_EXPAND_FILL); code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
code_editor->show_toggle_scripts_button(); code_editor->show_toggle_scripts_button();
warnings_panel = memnew(RichTextLabel);
editor_box->add_child(warnings_panel); editor_box->add_child(warnings_panel);
warnings_panel->add_font_override( warnings_panel->add_font_override(
"normal_font", EditorNode::get_singleton()->get_gui_base()->get_font("main", "EditorFonts")); "normal_font", EditorNode::get_singleton()->get_gui_base()->get_font("main", "EditorFonts"));
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_focus_mode(FOCUS_CLICK);
warnings_panel->hide();
code_editor->connect("show_warnings_panel", this, "_show_warnings_panel");
warnings_panel->connect("meta_clicked", this, "_warning_clicked"); warnings_panel->connect("meta_clicked", this, "_warning_clicked");
update_settings();
code_editor->get_text_edit()->set_callhint_settings(
EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
code_editor->get_text_edit()->set_context_menu_enabled(false);
code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
context_menu = memnew(PopupMenu);
add_child(context_menu); add_child(context_menu);
context_menu->connect("id_pressed", this, "_edit_option"); context_menu->connect("id_pressed", this, "_edit_option");
context_menu->set_hide_on_window_lose_focus(true); context_menu->set_hide_on_window_lose_focus(true);
color_panel = memnew(PopupPanel);
add_child(color_panel); add_child(color_panel);
color_picker = memnew(ColorPicker); color_picker = memnew(ColorPicker);
color_picker->set_deferred_mode(true); color_picker->set_deferred_mode(true);
color_panel->add_child(color_picker);
color_picker->connect("color_changed", this, "_color_changed"); color_picker->connect("color_changed", this, "_color_changed");
color_panel->add_child(color_picker);
// get default color picker mode from editor settings // get default color picker mode from editor settings
int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode"); int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
if (default_color_mode == 1) if (default_color_mode == 1)
@ -1844,12 +1826,51 @@ ScriptTextEditor::ScriptTextEditor() {
else if (default_color_mode == 2) else if (default_color_mode == 2)
color_picker->set_raw_mode(true); color_picker->set_raw_mode(true);
edit_hb = memnew(HBoxContainer); quick_open = memnew(ScriptEditorQuickOpen);
quick_open->connect("goto_line", this, "_goto_line");
add_child(quick_open);
edit_menu = memnew(MenuButton); goto_line_dialog = memnew(GotoLineDialog);
edit_menu->set_text(TTR("Edit")); add_child(goto_line_dialog);
edit_menu->set_switch_on_hover(true);
edit_menu->get_popup()->set_hide_on_window_lose_focus(true); add_child(connection_info_dialog);
edit_hb->add_child(search_menu);
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()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
edit_hb->add_child(edit_menu);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/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_left"), EDIT_INDENT_LEFT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
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/toggle_comment"), EDIT_TOGGLE_COMMENT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO); edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO); edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
edit_menu->get_popup()->add_separator(); edit_menu->get_popup()->add_separator();
@ -1879,8 +1900,6 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->connect("id_pressed", this, "_edit_option"); edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
edit_menu->get_popup()->add_separator(); edit_menu->get_popup()->add_separator();
PopupMenu *convert_case = memnew(PopupMenu);
convert_case->set_name("convert_case");
edit_menu->get_popup()->add_child(convert_case); edit_menu->get_popup()->add_child(convert_case);
edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case"); edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase"), KEY_MASK_SHIFT | KEY_F4), EDIT_TO_UPPERCASE); convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase"), KEY_MASK_SHIFT | KEY_F4), EDIT_TO_UPPERCASE);
@ -1888,66 +1907,99 @@ ScriptTextEditor::ScriptTextEditor() {
convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F6), EDIT_CAPITALIZE); convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F6), EDIT_CAPITALIZE);
convert_case->connect("id_pressed", this, "_edit_option"); convert_case->connect("id_pressed", this, "_edit_option");
highlighters[TTR("Standard")] = NULL;
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
edit_menu->get_popup()->add_child(highlighter_menu); edit_menu->get_popup()->add_child(highlighter_menu);
edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu"); edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
highlighter_menu->add_radio_check_item(TTR("Standard"));
highlighter_menu->connect("id_pressed", this, "_change_syntax_highlighter"); highlighter_menu->connect("id_pressed", this, "_change_syntax_highlighter");
search_menu = memnew(MenuButton); _load_theme_settings();
edit_hb->add_child(search_menu);
search_menu->set_text(TTR("Search"));
search_menu->set_switch_on_hover(true);
search_menu->get_popup()->set_hide_on_window_lose_focus(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()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
edit_hb->add_child(edit_menu);
MenuButton *goto_menu = memnew(MenuButton);
edit_hb->add_child(goto_menu); edit_hb->add_child(goto_menu);
goto_menu->set_text(TTR("Go To"));
goto_menu->set_switch_on_hover(true);
goto_menu->get_popup()->connect("id_pressed", this, "_edit_option");
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION); goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE); goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
goto_menu->get_popup()->add_separator(); goto_menu->get_popup()->add_separator();
bookmarks_menu = memnew(PopupMenu);
bookmarks_menu->set_name("Bookmarks");
goto_menu->get_popup()->add_child(bookmarks_menu); goto_menu->get_popup()->add_child(bookmarks_menu);
goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks"); goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
_update_bookmark_list(); _update_bookmark_list();
bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list"); bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed"); bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed");
breakpoints_menu = memnew(PopupMenu);
breakpoints_menu->set_name("Breakpoints");
goto_menu->get_popup()->add_child(breakpoints_menu); goto_menu->get_popup()->add_child(breakpoints_menu);
goto_menu->get_popup()->add_submenu_item(TTR("Breakpoints"), "Breakpoints"); goto_menu->get_popup()->add_submenu_item(TTR("Breakpoints"), "Breakpoints");
_update_breakpoint_list(); _update_breakpoint_list();
breakpoints_menu->connect("about_to_show", this, "_update_breakpoint_list"); breakpoints_menu->connect("about_to_show", this, "_update_breakpoint_list");
breakpoints_menu->connect("index_pressed", this, "_breakpoint_item_pressed"); breakpoints_menu->connect("index_pressed", this, "_breakpoint_item_pressed");
quick_open = memnew(ScriptEditorQuickOpen); goto_menu->get_popup()->connect("id_pressed", this, "_edit_option");
add_child(quick_open); }
quick_open->connect("goto_line", this, "_goto_line");
goto_line_dialog = memnew(GotoLineDialog); ScriptTextEditor::ScriptTextEditor() {
add_child(goto_line_dialog); theme_loaded = false;
script_is_valid = false;
editor_enabled = false;
code_editor = memnew(CodeTextEditor);
code_editor->add_constant_override("separation", 2);
code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
code_editor->set_code_complete_func(_code_complete_scripts, this);
code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
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_focus_mode(FOCUS_CLICK);
warnings_panel->hide();
update_settings();
code_editor->get_text_edit()->set_callhint_settings(
EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
code_editor->get_text_edit()->set_context_menu_enabled(false);
context_menu = memnew(PopupMenu);
color_panel = memnew(PopupPanel);
color_picker = NULL;
edit_hb = memnew(HBoxContainer);
edit_menu = memnew(MenuButton);
edit_menu->set_text(TTR("Edit"));
edit_menu->set_switch_on_hover(true);
edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
convert_case = memnew(PopupMenu);
convert_case->set_name("convert_case");
highlighters[TTR("Standard")] = NULL;
highlighter_menu = memnew(PopupMenu);
highlighter_menu->set_name("highlighter_menu");
highlighter_menu->add_radio_check_item(TTR("Standard"));
search_menu = memnew(MenuButton);
search_menu->set_text(TTR("Search"));
search_menu->set_switch_on_hover(true);
search_menu->get_popup()->set_hide_on_window_lose_focus(true);
goto_menu = memnew(MenuButton);
goto_menu->set_text(TTR("Go To"));
goto_menu->set_switch_on_hover(true);
bookmarks_menu = memnew(PopupMenu);
bookmarks_menu->set_name("Bookmarks");
breakpoints_menu = memnew(PopupMenu);
breakpoints_menu->set_name("Breakpoints");
quick_open = NULL;
goto_line_dialog = NULL;
connection_info_dialog = memnew(ConnectionInfoDialog); connection_info_dialog = memnew(ConnectionInfoDialog);
add_child(connection_info_dialog);
code_editor->get_text_edit()->set_drag_forwarding(this); code_editor->get_text_edit()->set_drag_forwarding(this);
} }
@ -1959,6 +2011,22 @@ ScriptTextEditor::~ScriptTextEditor() {
} }
} }
highlighters.clear(); highlighters.clear();
if (!editor_enabled) {
memdelete(code_editor);
memdelete(warnings_panel);
memdelete(context_menu);
memdelete(color_panel);
memdelete(edit_hb);
memdelete(edit_menu);
memdelete(convert_case);
memdelete(highlighter_menu);
memdelete(search_menu);
memdelete(goto_menu);
memdelete(bookmarks_menu);
memdelete(breakpoints_menu);
memdelete(connection_info_dialog);
}
} }
static ScriptEditorBase *create_editor(const RES &p_resource) { static ScriptEditorBase *create_editor(const RES &p_resource) {

View file

@ -60,6 +60,7 @@ class ScriptTextEditor : public ScriptEditorBase {
Ref<Script> script; Ref<Script> script;
bool script_is_valid; bool script_is_valid;
bool editor_enabled;
Vector<String> functions; Vector<String> functions;
@ -71,10 +72,12 @@ class ScriptTextEditor : public ScriptEditorBase {
MenuButton *edit_menu; MenuButton *edit_menu;
MenuButton *search_menu; MenuButton *search_menu;
MenuButton *goto_menu;
PopupMenu *bookmarks_menu; PopupMenu *bookmarks_menu;
PopupMenu *breakpoints_menu; PopupMenu *breakpoints_menu;
PopupMenu *highlighter_menu; PopupMenu *highlighter_menu;
PopupMenu *context_menu; PopupMenu *context_menu;
PopupMenu *convert_case;
GotoLineDialog *goto_line_dialog; GotoLineDialog *goto_line_dialog;
ScriptEditorQuickOpen *quick_open; ScriptEditorQuickOpen *quick_open;
@ -145,6 +148,8 @@ class ScriptTextEditor : public ScriptEditorBase {
LOOKUP_SYMBOL, LOOKUP_SYMBOL,
}; };
void _enable_code_editor();
protected: protected:
void _update_breakpoint_list(); void _update_breakpoint_list();
void _breakpoint_item_pressed(int p_idx); void _breakpoint_item_pressed(int p_idx);
@ -162,7 +167,6 @@ protected:
void _show_warnings_panel(bool p_show); void _show_warnings_panel(bool p_show);
void _warning_clicked(Variant p_line); void _warning_clicked(Variant p_line);
void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
Map<String, SyntaxHighlighter *> highlighters; Map<String, SyntaxHighlighter *> highlighters;
@ -197,6 +201,7 @@ public:
virtual void apply_code(); virtual void apply_code();
virtual RES get_edited_resource() const; virtual RES get_edited_resource() const;
virtual void set_edited_resource(const RES &p_res); virtual void set_edited_resource(const RES &p_res);
virtual void enable_editor();
virtual Vector<String> get_functions(); virtual Vector<String> get_functions();
virtual void reload_text(); virtual void reload_text();
virtual String get_name(); virtual String get_name();

View file

@ -164,8 +164,7 @@ String TextEditor::get_name() {
} }
Ref<Texture> TextEditor::get_icon() { Ref<Texture> TextEditor::get_icon() {
return EditorNode::get_singleton()->get_object_icon(text_file.ptr(), "");
return EditorNode::get_singleton()->get_object_icon(text_file.operator->(), "");
} }
RES TextEditor::get_edited_resource() const { RES TextEditor::get_edited_resource() const {
@ -173,7 +172,8 @@ RES TextEditor::get_edited_resource() const {
} }
void TextEditor::set_edited_resource(const RES &p_res) { void TextEditor::set_edited_resource(const RES &p_res) {
ERR_FAIL_COND(!text_file.is_null()); ERR_FAIL_COND(text_file.is_valid());
ERR_FAIL_COND(p_res.is_null());
text_file = p_res; text_file = p_res;
@ -185,6 +185,16 @@ void TextEditor::set_edited_resource(const RES &p_res) {
code_editor->update_line_and_column(); code_editor->update_line_and_column();
} }
void TextEditor::enable_editor() {
if (editor_enabled) {
return;
}
editor_enabled = true;
_load_theme_settings();
}
void TextEditor::add_callback(const String &p_function, PoolStringArray p_args) { void TextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
} }
@ -282,6 +292,8 @@ void TextEditor::set_edit_state(const Variant &p_state) {
_change_syntax_highlighter(idx); _change_syntax_highlighter(idx);
} }
} }
ensure_focus();
} }
void TextEditor::trim_trailing_whitespace() { void TextEditor::trim_trailing_whitespace() {
@ -361,15 +373,6 @@ void TextEditor::clear_edit_menu() {
memdelete(edit_hb); memdelete(edit_hb);
} }
void TextEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY:
_load_theme_settings();
break;
}
}
void TextEditor::_edit_option(int p_op) { void TextEditor::_edit_option(int p_op) {
TextEdit *tx = code_editor->get_text_edit(); TextEdit *tx = code_editor->get_text_edit();
@ -624,6 +627,8 @@ void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is
} }
TextEditor::TextEditor() { TextEditor::TextEditor() {
editor_enabled = false;
code_editor = memnew(CodeTextEditor); code_editor = memnew(CodeTextEditor);
add_child(code_editor); add_child(code_editor);
code_editor->add_constant_override("separation", 0); code_editor->add_constant_override("separation", 0);

View file

@ -41,6 +41,7 @@ private:
CodeTextEditor *code_editor; CodeTextEditor *code_editor;
Ref<TextFile> text_file; Ref<TextFile> text_file;
bool editor_enabled;
HBoxContainer *edit_hb; HBoxContainer *edit_hb;
MenuButton *edit_menu; MenuButton *edit_menu;
@ -98,8 +99,6 @@ private:
protected: protected:
static void _bind_methods(); static void _bind_methods();
void _notification(int p_what);
void _edit_option(int p_op); void _edit_option(int p_op);
void _make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position); void _make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position);
void _text_edit_gui_input(const Ref<InputEvent> &ev); void _text_edit_gui_input(const Ref<InputEvent> &ev);
@ -123,7 +122,7 @@ public:
virtual Ref<Texture> get_icon(); virtual Ref<Texture> get_icon();
virtual RES get_edited_resource() const; virtual RES get_edited_resource() const;
virtual void set_edited_resource(const RES &p_res); virtual void set_edited_resource(const RES &p_res);
void set_edited_file(const Ref<TextFile> &p_file); virtual void enable_editor();
virtual void reload_text(); virtual void reload_text();
virtual void apply_code(); virtual void apply_code();
virtual bool is_unsaved(); virtual bool is_unsaved();

View file

@ -2401,7 +2401,8 @@ RES VisualScriptEditor::get_edited_resource() const {
} }
void VisualScriptEditor::set_edited_resource(const RES &p_res) { void VisualScriptEditor::set_edited_resource(const RES &p_res) {
ERR_FAIL_COND(script.is_valid());
ERR_FAIL_COND(p_res.is_null());
script = p_res; script = p_res;
signal_editor->script = script; signal_editor->script = script;
signal_editor->undo_redo = undo_redo; signal_editor->undo_redo = undo_redo;
@ -2422,6 +2423,9 @@ void VisualScriptEditor::set_edited_resource(const RES &p_res) {
_update_members(); _update_members();
} }
void VisualScriptEditor::enable_editor() {
}
Vector<String> VisualScriptEditor::get_functions() { Vector<String> VisualScriptEditor::get_functions() {
return Vector<String>(); return Vector<String>();

View file

@ -295,6 +295,7 @@ public:
virtual void apply_code(); virtual void apply_code();
virtual RES get_edited_resource() const; virtual RES get_edited_resource() const;
virtual void set_edited_resource(const RES &p_res); virtual void set_edited_resource(const RES &p_res);
virtual void enable_editor();
virtual Vector<String> get_functions(); virtual Vector<String> get_functions();
virtual void reload_text(); virtual void reload_text();
virtual String get_name(); virtual String get_name();