Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,6 @@ FindReplaceBar::FindReplaceBar() {

/*** CODE EDITOR ****/

static constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };

// This function should be used to handle shortcuts that could otherwise
// be handled too late if they weren't handled here.
void CodeTextEditor::input(const Ref<InputEvent> &event) {
Expand Down
2 changes: 2 additions & 0 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class FindReplaceBar : public HBoxContainer {

typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced);

inline constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };

class CodeTextEditor : public VBoxContainer {
GDCLASS(CodeTextEditor, VBoxContainer);

Expand Down
155 changes: 145 additions & 10 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "editor/plugins/script_editor_plugin.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/separator.h"

#include "modules/modules_enabled.gen.h" // For gdscript, mono.

Expand Down Expand Up @@ -331,7 +332,111 @@ void EditorHelp::_class_desc_select(const String &p_select) {
}
}

void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;

if (mb.is_valid() && mb->is_command_or_control_pressed()) {
if (mb->is_pressed()) {
if (mb->get_button_index() == MouseButton::WHEEL_UP) {
_zoom_in();
accept_event();
return;
}
if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
_zoom_out();
accept_event();
return;
}
} else {
// Prevents of button's 'released' event propagation, causing RichTextLabel scroll while zooming.
if (mb->get_button_index() == MouseButton::WHEEL_UP || mb->get_button_index() == MouseButton::WHEEL_DOWN) {
accept_event();
return;
}
}
}

Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
if (magnify_gesture.is_valid()) {
_zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
accept_event();
return;
}

Ref<InputEventKey> k = p_event;
if (k.is_valid() && k->is_pressed()) {
if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
_zoom_in();
accept_event();
return;
}
if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
_zoom_out();
accept_event();
return;
}
if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
_zoom_to(1);
accept_event();
return;
}
}
}

void EditorHelp::_zoom_popup_id_pressed(int p_idx) {
_zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
}

void EditorHelp::_zoom_in() {
int s = class_desc->get_theme_font_size("normal_font_size");
_zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
}

void EditorHelp::_zoom_out() {
int s = class_desc->get_theme_font_size("normal_font_size");
_zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
}

void EditorHelp::_zoom_to(float p_zoom_factor) {
if (zoom_factor == p_zoom_factor) {
return;
}

float old_zoom_factor = zoom_factor;

set_zoom_factor(p_zoom_factor);

if (old_zoom_factor != zoom_factor) {
emit_signal(SNAME("zoomed"), zoom_factor);
}
}

void EditorHelp::set_zoom_factor(float p_zoom_factor) {
int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
zoom_factor = CLAMP(p_zoom_factor, ZOOM_FACTOR_PRESETS[0], ZOOM_FACTOR_PRESETS[preset_count - 1]);

int neutral_help_font_size = int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE;
int neutral_help_title_font_size = int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE;
int neutral_help_source_font_size = int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE;
int neutral_help_kb_font_size = (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE;

int new_help_font_size = Math::round(zoom_factor * neutral_help_font_size);
int new_help_title_font_size = Math::round(zoom_factor * neutral_help_title_font_size);
int new_help_source_font_size = Math::round(zoom_factor * neutral_help_source_font_size);
int new_help_kb_font_size = Math::round(zoom_factor * neutral_help_kb_font_size);

theme_cache.doc_font_size = new_help_font_size;
theme_cache.doc_title_font_size = new_help_title_font_size;
theme_cache.doc_code_font_size = new_help_source_font_size;
theme_cache.doc_kbd_font_size = new_help_kb_font_size;

_update_doc();
_class_desc_resized(true);
zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
}

float EditorHelp::get_zoom_factor() const {
return zoom_factor;
}

void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
Expand Down Expand Up @@ -926,8 +1031,6 @@ void EditorHelp::_update_doc() {
return;
}

scroll_locked = true;

class_desc->clear();
method_line.clear();
section_line.clear();
Expand Down Expand Up @@ -1027,11 +1130,13 @@ void EditorHelp::_update_doc() {

class_desc->push_indent(1);
class_desc->push_font(theme_cache.doc_bold_font);
class_desc->push_font_size(theme_cache.doc_code_font_size);
class_desc->push_color(theme_cache.text_color);

_add_text(brief_class_descr);

class_desc->pop(); // color
class_desc->pop(); // font_size
class_desc->pop(); // font
class_desc->pop(); // indent
}
Expand Down Expand Up @@ -2263,9 +2368,6 @@ void EditorHelp::_update_doc() {
class_desc->add_newline();
class_desc->add_newline();

// Free the scroll.
scroll_locked = false;

#undef HANDLE_DOC
}

Expand Down Expand Up @@ -2349,7 +2451,7 @@ void EditorHelp::_help_callback(const String &p_topic) {
}
}

static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class) {
static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class, int p_doc_code_font_size = 0, int p_doc_kbd_font_size = 0) {
const DocTools *doc = EditorHelp::get_doc_data();

bool is_native = false;
Expand All @@ -2368,8 +2470,15 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const C
const Ref<Font> doc_code_font = p_owner_node->get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
const Ref<Font> doc_kbd_font = p_owner_node->get_theme_font(SNAME("doc_keyboard"), EditorStringName(EditorFonts));

const int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
const int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
int doc_code_font_size = p_doc_code_font_size;
int doc_kbd_font_size = p_doc_kbd_font_size;

if (doc_code_font_size <= 0) {
doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
}
if (doc_kbd_font_size <= 0) {
doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
}

const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
const Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
Expand Down Expand Up @@ -2837,7 +2946,7 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const C
}

void EditorHelp::_add_text(const String &p_bbcode) {
_add_text_to_rt(p_bbcode, class_desc, this, edited_class);
_add_text_to_rt(p_bbcode, class_desc, this, edited_class, theme_cache.doc_code_font_size, theme_cache.doc_kbd_font_size);
}

int EditorHelp::doc_generation_count = 0;
Expand Down Expand Up @@ -3085,6 +3194,7 @@ void EditorHelp::_bind_methods() {

ADD_SIGNAL(MethodInfo("go_to_help"));
ADD_SIGNAL(MethodInfo("request_save_history"));
ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "zoom_factor")));
}

void EditorHelp::init_gdext_pointers() {
Expand All @@ -3097,6 +3207,10 @@ EditorHelp::EditorHelp() {

EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);

ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KeyModifierMask::CMD_OR_CTRL | Key::KEY_0);

class_desc = memnew(RichTextLabel);
class_desc->set_tab_size(8);
add_child(class_desc);
Expand All @@ -3123,6 +3237,27 @@ EditorHelp::EditorHelp() {
toggle_scripts_button->set_flat(true);
toggle_scripts_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
status_bar->add_child(toggle_scripts_button);
status_bar->add_spacer();
status_bar->add_child(memnew(VSeparator));

// Zoom
zoom_button = memnew(MenuButton);
status_bar->add_child(zoom_button);
zoom_button->set_flat(true);
zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
zoom_button->set_tooltip_text(TTR("Zoom factor"));
zoom_button->set_text("100 %");

PopupMenu *zoom_menu = zoom_button->get_popup();
int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);

for (int i = 0; i < preset_count; i++) {
float z = ZOOM_FACTOR_PRESETS[i];
zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
zoom_menu->set_item_metadata(i, z);
}

zoom_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorHelp::_zoom_popup_id_pressed));

class_desc->set_selection_enabled(true);
class_desc->set_context_menu_enabled(true);
Expand Down
12 changes: 11 additions & 1 deletion editor/editor_help.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class EditorHelp : public VBoxContainer {
HashMap<String, HashMap<String, int>> enum_values_line;
int description_line = 0;

MenuButton *zoom_button = nullptr;
float zoom_factor = 1.0f;

RichTextLabel *class_desc = nullptr;
HSplitContainer *h_split = nullptr;
static DocTools *doc;
Expand Down Expand Up @@ -156,7 +159,6 @@ class EditorHelp : public VBoxContainer {
void _help_callback(const String &p_topic);

void _add_text(const String &p_bbcode);
bool scroll_locked = false;

//void _button_pressed(int p_idx);
void _add_type(const String &p_type, const String &p_enum = String(), bool p_is_bitfield = false);
Expand All @@ -179,6 +181,11 @@ class EditorHelp : public VBoxContainer {
void _class_desc_resized(bool p_force_update_theme);
int display_margin = 0;

void _zoom_popup_id_pressed(int p_idx);
void _zoom_in();
void _zoom_out();
void _zoom_to(float p_zoom_factor);

Error _goto_desc(const String &p_class);
//void _update_history_buttons();
void _update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
Expand Down Expand Up @@ -243,6 +250,9 @@ class EditorHelp : public VBoxContainer {
int get_scroll() const;
void set_scroll(int p_scroll);

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

void update_toggle_scripts_button();

static void init_gdext_pointers();
Expand Down
9 changes: 9 additions & 0 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3503,11 +3503,13 @@ void ScriptEditor::_help_class_open(const String &p_class) {
EditorHelp *eh = memnew(EditorHelp);

eh->set_name(p_class);
eh->set_zoom_factor(zoom_factor);
tab_container->add_child(eh);
_go_to_tab(tab_container->get_tab_count() - 1);
eh->go_to_class(p_class);
eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
eh->connect("request_save_history", callable_mp(this, &ScriptEditor::_save_history));
eh->connect("zoomed", callable_mp(this, &ScriptEditor::_set_zoom_factor));
_add_recent_script(p_class);
_sort_list_on_update = true;
_update_script_names();
Expand All @@ -3524,9 +3526,11 @@ void ScriptEditor::_help_class_goto(const String &p_desc) {
EditorHelp *eh = memnew(EditorHelp);

eh->set_name(cname);
eh->set_zoom_factor(zoom_factor);
tab_container->add_child(eh);
eh->go_to_help(p_desc);
eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
eh->connect("zoomed", callable_mp(this, &ScriptEditor::_set_zoom_factor));
_add_recent_script(eh->get_class());
_sort_list_on_update = true;
_update_script_names();
Expand Down Expand Up @@ -3941,6 +3945,11 @@ void ScriptEditor::_set_zoom_factor(float p_zoom_factor) {
cte->set_zoom_factor(zoom_factor);
}
}
} else {
EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
if (help) {
help->set_zoom_factor(zoom_factor);
}
}
}
}
Expand Down