Skip to content

Commit

Permalink
Merge pull request #88474 from MajorMcDoom/text-editor-zoom
Browse files Browse the repository at this point in the history
Improve text editor status bar and zooming UX
  • Loading branch information
akien-mga committed Feb 22, 2024
2 parents 7c3c90a + 9281c44 commit 0550abf
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 250 deletions.
265 changes: 126 additions & 139 deletions editor/code_editor.cpp

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "scene/gui/line_edit.h"
#include "scene/main/timer.h"

class MenuButton;

class GotoLineDialog : public ConfirmationDialog {
GDCLASS(GotoLineDialog, ConfirmationDialog);

Expand Down Expand Up @@ -156,37 +158,29 @@ class CodeTextEditor : public VBoxContainer {
Button *error_button = nullptr;
Button *warning_button = nullptr;

MenuButton *zoom_button = nullptr;
Label *line_and_col_txt = nullptr;
Label *indentation_txt = nullptr;

Label *info = nullptr;
Timer *idle = nullptr;
bool code_complete_enabled = true;
Timer *code_complete_timer = nullptr;
int code_complete_timer_line = 0;

Timer *font_resize_timer = nullptr;
int font_resize_val;
real_t font_size;
float zoom_factor = 1.0f;

Label *error = nullptr;
int error_line;
int error_column;

void _on_settings_change();
void _apply_settings_change();

void _update_text_editor_theme();
void _update_font_ligatures();
void _complete_request();
Ref<Texture2D> _get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option);
void _font_resize_timeout();
bool _add_font_size(int p_delta);

virtual void input(const Ref<InputEvent> &event) override;
void _text_editor_gui_input(const Ref<InputEvent> &p_event);
void _zoom_in();
void _zoom_out();
void _zoom_changed();
void _reset_zoom();

Color completion_font_color;
Color completion_string_color;
Expand All @@ -195,13 +189,17 @@ class CodeTextEditor : public VBoxContainer {
CodeTextEditorCodeCompleteFunc code_complete_func;
void *code_complete_ud = nullptr;

void _zoom_in();
void _zoom_out();
void _zoom_to(float p_zoom_factor);

void _error_button_pressed();
void _warning_button_pressed();
void _set_show_errors_panel(bool p_show);
void _set_show_warnings_panel(bool p_show);
void _error_pressed(const Ref<InputEvent> &p_event);

void _update_status_bar_theme();
void _zoom_popup_id_pressed(int p_idx);

void _toggle_scripts_pressed();

Expand Down Expand Up @@ -234,6 +232,8 @@ class CodeTextEditor : public VBoxContainer {
};
void convert_case(CaseStyle p_case);

void set_indent_using_spaces(bool p_use_spaces);

void move_lines_up();
void move_lines_down();
void delete_lines();
Expand Down Expand Up @@ -273,6 +273,9 @@ class CodeTextEditor : public VBoxContainer {
void goto_prev_bookmark();
void remove_all_bookmarks();

void set_zoom_factor(float p_zoom_factor);
float get_zoom_factor();

void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);

void validate_script();
Expand Down
46 changes: 40 additions & 6 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/version.h"
#include "editor/code_editor.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/debugger/script_editor_debugger.h"
#include "editor/editor_command_palette.h"
Expand Down Expand Up @@ -1639,7 +1640,7 @@ void ScriptEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &ScriptEditor::_editor_stop));
_editor_settings_changed();
_apply_editor_settings();
[[fallthrough]];
}

Expand Down Expand Up @@ -2481,6 +2482,12 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
se->connect("replace_in_files_requested", callable_mp(this, &ScriptEditor::_on_replace_in_files_requested));
se->connect("go_to_method", callable_mp(this, &ScriptEditor::script_goto_method));

CodeTextEditor *cte = se->get_code_editor();
if (cte) {
cte->set_zoom_factor(zoom_factor);
cte->connect("zoomed", callable_mp(this, &ScriptEditor::_set_zoom_factor));
}

//test for modification, maybe the script was not edited but was loaded

_test_script_times_on_disk(p_resource);
Expand Down Expand Up @@ -2779,11 +2786,16 @@ void ScriptEditor::_save_layout() {
}

void ScriptEditor::_editor_settings_changed() {
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor") &&
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor") &&
!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor") &&
!EditorSettings::get_singleton()->check_changed_settings_in_group("docks/filesystem")) {
return;
}

_apply_editor_settings();
}

void ScriptEditor::_apply_editor_settings() {
textfile_extensions.clear();
const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
for (const String &E : textfile_ext) {
Expand All @@ -2808,6 +2820,11 @@ void ScriptEditor::_editor_settings_changed() {
EditorSettings::get_singleton()->load_text_editor_theme();
}

_update_script_colors();
_update_script_names();

ScriptServer::set_reload_scripts_on_save(EDITOR_GET("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save"));

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) {
Expand All @@ -2816,10 +2833,6 @@ void ScriptEditor::_editor_settings_changed() {

se->update_settings();
}
_update_script_colors();
_update_script_names();

ScriptServer::set_reload_scripts_on_save(EDITOR_GET("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save"));
}

void ScriptEditor::_filesystem_changed() {
Expand Down Expand Up @@ -3322,6 +3335,8 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
}
}

_set_zoom_factor(p_layout->get_value("ScriptEditor", "zoom_factor", 1.0f));

restoring_layout = false;

_update_script_names();
Expand Down Expand Up @@ -3371,6 +3386,7 @@ void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
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);

// Save the cache.
script_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg"));
Expand Down Expand Up @@ -3806,6 +3822,24 @@ void ScriptEditor::_on_find_in_files_modified_files(PackedStringArray paths) {
_update_modified_scripts_for_external_editor();
}

void ScriptEditor::_set_zoom_factor(float p_zoom_factor) {
if (zoom_factor == p_zoom_factor) {
return;
}
zoom_factor = p_zoom_factor;
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) {
CodeTextEditor *cte = se->get_code_editor();
if (cte) {
if (zoom_factor != cte->get_zoom_factor()) {
cte->set_zoom_factor(zoom_factor);
}
}
}
}
}

void ScriptEditor::_window_changed(bool p_visible) {
make_floating->set_visible(!p_visible);
is_floating = p_visible;
Expand Down
7 changes: 7 additions & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "scene/resources/syntax_highlighter.h"
#include "scene/resources/text_file.h"

class CodeTextEditor;
class EditorFileDialog;
class EditorHelpSearch;
class FindReplaceBar;
Expand Down Expand Up @@ -192,6 +193,7 @@ class ScriptEditorBase : public VBoxContainer {
virtual void set_find_replace_bar(FindReplaceBar *p_bar) = 0;

virtual Control *get_base_editor() const = 0;
virtual CodeTextEditor *get_code_editor() const = 0;

virtual void validate() = 0;

Expand Down Expand Up @@ -306,6 +308,8 @@ class ScriptEditor : public PanelContainer {

String current_theme;

float zoom_factor = 1.0f;

TextureRect *script_icon = nullptr;
Label *script_name_label = nullptr;

Expand Down Expand Up @@ -420,6 +424,7 @@ class ScriptEditor : public PanelContainer {
void _save_editor_state(ScriptEditorBase *p_editor);
void _save_layout();
void _editor_settings_changed();
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);
Expand Down Expand Up @@ -491,6 +496,8 @@ class ScriptEditor : public PanelContainer {
void _start_find_in_files(bool with_replace);
void _on_find_in_files_modified_files(PackedStringArray paths);

void _set_zoom_factor(float p_zoom_factor);

void _window_changed(bool p_visible);

static void _open_script_request(const String &p_path);
Expand Down
10 changes: 6 additions & 4 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1393,11 +1393,11 @@ void ScriptTextEditor::_edit_option(int p_op) {
trim_trailing_whitespace();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
tx->set_indent_using_spaces(true);
code_editor->set_indent_using_spaces(true);
convert_indent();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
tx->set_indent_using_spaces(false);
code_editor->set_indent_using_spaces(false);
convert_indent();
} break;
case EDIT_PICK_COLOR: {
Expand Down Expand Up @@ -1691,6 +1691,10 @@ Control *ScriptTextEditor::get_base_editor() const {
return code_editor->get_text_editor();
}

CodeTextEditor *ScriptTextEditor::get_code_editor() const {
return code_editor;
}

Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
return Variant();
}
Expand Down Expand Up @@ -2374,8 +2378,6 @@ ScriptTextEditor::ScriptTextEditor() {

update_settings();

code_editor->get_text_editor()->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"));

code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
code_editor->get_text_editor()->set_context_menu_enabled(false);

Expand Down
1 change: 1 addition & 0 deletions editor/plugins/script_text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class ScriptTextEditor : public ScriptEditorBase {
static void register_editor();

virtual Control *get_base_editor() const override;
virtual CodeTextEditor *get_code_editor() const override;

virtual void validate() override;

Expand Down
29 changes: 27 additions & 2 deletions editor/plugins/shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ void ShaderEditorPlugin::edit(Object *p_object) {
es.shader_editor = memnew(TextShaderEditor);
es.shader_editor->edit(si);
shader_tabs->add_child(es.shader_editor);
es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
} else {
Shader *s = Object::cast_to<Shader>(p_object);
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
Expand All @@ -163,7 +162,16 @@ void ShaderEditorPlugin::edit(Object *p_object) {
es.shader_editor = memnew(TextShaderEditor);
shader_tabs->add_child(es.shader_editor);
es.shader_editor->edit(s);
es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));
}
}

if (es.shader_editor) {
es.shader_editor->connect("validation_changed", callable_mp(this, &ShaderEditorPlugin::_update_shader_list));

CodeTextEditor *cte = es.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));
}
}

Expand Down Expand Up @@ -244,6 +252,8 @@ void ShaderEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {

_update_shader_list();
_shader_selected(selected_shader_idx);

_set_text_shader_zoom_factor(p_layout->get_value("ShaderEditor", "text_shader_zoom_factor", 1.0f));
}

void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
Expand Down Expand Up @@ -290,6 +300,7 @@ void ShaderEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
p_layout->set_value("ShaderEditor", "open_shaders", shaders);
p_layout->set_value("ShaderEditor", "split_offset", main_split->get_split_offset());
p_layout->set_value("ShaderEditor", "selected_shader", selected_shader);
p_layout->set_value("ShaderEditor", "text_shader_zoom_factor", text_shader_zoom_factor);
}

String ShaderEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
Expand Down Expand Up @@ -590,6 +601,20 @@ void ShaderEditorPlugin::_window_changed(bool p_visible) {
make_floating->set_visible(!p_visible);
}

void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
if (text_shader_zoom_factor != p_zoom_factor) {
text_shader_zoom_factor = p_zoom_factor;
for (const EditedShader &edited_shader : edited_shaders) {
if (edited_shader.shader_editor) {
CodeTextEditor *cte = edited_shader.shader_editor->get_code_editor();
if (cte && cte->get_zoom_factor() != text_shader_zoom_factor) {
cte->set_zoom_factor(text_shader_zoom_factor);
}
}
}
}
}

void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
for (uint32_t i = 0; i < edited_shaders.size(); i++) {
if (edited_shaders[i].path == p_removed_file) {
Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class ShaderEditorPlugin : public EditorPlugin {

ShaderCreateDialog *shader_create_dialog = nullptr;

float text_shader_zoom_factor = 1.0f;

void _update_shader_list();
void _shader_selected(int p_index);
void _shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
Expand All @@ -106,6 +108,8 @@ class ShaderEditorPlugin : public EditorPlugin {

void _window_changed(bool p_visible);

void _set_text_shader_zoom_factor(float p_zoom_factor);

protected:
void _notification(int p_what);

Expand Down
8 changes: 6 additions & 2 deletions editor/plugins/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ Control *TextEditor::get_base_editor() const {
return code_editor->get_text_editor();
}

CodeTextEditor *TextEditor::get_code_editor() const {
return code_editor;
}

PackedInt32Array TextEditor::get_breakpoints() {
return PackedInt32Array();
}
Expand Down Expand Up @@ -419,11 +423,11 @@ void TextEditor::_edit_option(int p_op) {
trim_trailing_whitespace();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
tx->set_indent_using_spaces(true);
code_editor->set_indent_using_spaces(true);
convert_indent();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
tx->set_indent_using_spaces(false);
code_editor->set_indent_using_spaces(false);
convert_indent();
} break;
case EDIT_TO_UPPERCASE: {
Expand Down
Loading

0 comments on commit 0550abf

Please sign in to comment.