Skip to content

Commit 4ab457b

Browse files
committed
Make Editor Help text possible to be zoomed in/out
1 parent 88f3b5f commit 4ab457b

File tree

5 files changed

+167
-13
lines changed

5 files changed

+167
-13
lines changed

editor/code_editor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,6 @@ FindReplaceBar::FindReplaceBar() {
798798

799799
/*** CODE EDITOR ****/
800800

801-
static constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };
802-
803801
// This function should be used to handle shortcuts that could otherwise
804802
// be handled too late if they weren't handled here.
805803
void CodeTextEditor::input(const Ref<InputEvent> &event) {

editor/code_editor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class FindReplaceBar : public HBoxContainer {
153153

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

156+
inline constexpr float ZOOM_FACTOR_PRESETS[7] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f };
157+
156158
class CodeTextEditor : public VBoxContainer {
157159
GDCLASS(CodeTextEditor, VBoxContainer);
158160

editor/editor_help.cpp

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "editor/plugins/script_editor_plugin.h"
4848
#include "editor/themes/editor_scale.h"
4949
#include "scene/gui/line_edit.h"
50+
#include "scene/gui/separator.h"
5051

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

@@ -331,7 +332,111 @@ void EditorHelp::_class_desc_select(const String &p_select) {
331332
}
332333
}
333334

334-
void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
335+
void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_event) {
336+
Ref<InputEventMouseButton> mb = p_event;
337+
338+
if (mb.is_valid() && mb->is_command_or_control_pressed()) {
339+
if (mb->is_pressed()) {
340+
if (mb->get_button_index() == MouseButton::WHEEL_UP) {
341+
_zoom_in();
342+
accept_event();
343+
return;
344+
}
345+
if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
346+
_zoom_out();
347+
accept_event();
348+
return;
349+
}
350+
} else {
351+
// Prevents of button's 'released' event propagation, causing RichTextLabel scroll while zooming.
352+
if (mb->get_button_index() == MouseButton::WHEEL_UP || mb->get_button_index() == MouseButton::WHEEL_DOWN) {
353+
accept_event();
354+
return;
355+
}
356+
}
357+
}
358+
359+
Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
360+
if (magnify_gesture.is_valid()) {
361+
_zoom_to(zoom_factor * powf(magnify_gesture->get_factor(), 0.25f));
362+
accept_event();
363+
return;
364+
}
365+
366+
Ref<InputEventKey> k = p_event;
367+
if (k.is_valid() && k->is_pressed()) {
368+
if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
369+
_zoom_in();
370+
accept_event();
371+
return;
372+
}
373+
if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
374+
_zoom_out();
375+
accept_event();
376+
return;
377+
}
378+
if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
379+
_zoom_to(1);
380+
accept_event();
381+
return;
382+
}
383+
}
384+
}
385+
386+
void EditorHelp::_zoom_popup_id_pressed(int p_idx) {
387+
_zoom_to(zoom_button->get_popup()->get_item_metadata(p_idx));
388+
}
389+
390+
void EditorHelp::_zoom_in() {
391+
int s = class_desc->get_theme_font_size("normal_font_size");
392+
_zoom_to(zoom_factor * (s + MAX(1.0f, EDSCALE)) / s);
393+
}
394+
395+
void EditorHelp::_zoom_out() {
396+
int s = class_desc->get_theme_font_size("normal_font_size");
397+
_zoom_to(zoom_factor * (s - MAX(1.0f, EDSCALE)) / s);
398+
}
399+
400+
void EditorHelp::_zoom_to(float p_zoom_factor) {
401+
if (zoom_factor == p_zoom_factor) {
402+
return;
403+
}
404+
405+
float old_zoom_factor = zoom_factor;
406+
407+
set_zoom_factor(p_zoom_factor);
408+
409+
if (old_zoom_factor != zoom_factor) {
410+
emit_signal(SNAME("zoomed"), zoom_factor);
411+
}
412+
}
413+
414+
void EditorHelp::set_zoom_factor(float p_zoom_factor) {
415+
int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
416+
zoom_factor = CLAMP(p_zoom_factor, ZOOM_FACTOR_PRESETS[0], ZOOM_FACTOR_PRESETS[preset_count - 1]);
417+
418+
int neutral_help_font_size = int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE;
419+
int neutral_help_title_font_size = int(EDITOR_GET("text_editor/help/help_title_font_size")) * EDSCALE;
420+
int neutral_help_source_font_size = int(EDITOR_GET("text_editor/help/help_source_font_size")) * EDSCALE;
421+
int neutral_help_kb_font_size = (int(EDITOR_GET("text_editor/help/help_source_font_size")) - 1) * EDSCALE;
422+
423+
int new_help_font_size = Math::round(zoom_factor * neutral_help_font_size);
424+
int new_help_title_font_size = Math::round(zoom_factor * neutral_help_title_font_size);
425+
int new_help_source_font_size = Math::round(zoom_factor * neutral_help_source_font_size);
426+
int new_help_kb_font_size = Math::round(zoom_factor * neutral_help_kb_font_size);
427+
428+
theme_cache.doc_font_size = new_help_font_size;
429+
theme_cache.doc_title_font_size = new_help_title_font_size;
430+
theme_cache.doc_code_font_size = new_help_source_font_size;
431+
theme_cache.doc_kbd_font_size = new_help_kb_font_size;
432+
433+
_update_doc();
434+
_class_desc_resized(true);
435+
zoom_button->set_text(itos(Math::round(zoom_factor * 100)) + " %");
436+
}
437+
438+
float EditorHelp::get_zoom_factor() const {
439+
return zoom_factor;
335440
}
336441

337442
void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
@@ -926,8 +1031,6 @@ void EditorHelp::_update_doc() {
9261031
return;
9271032
}
9281033

929-
scroll_locked = true;
930-
9311034
class_desc->clear();
9321035
method_line.clear();
9331036
section_line.clear();
@@ -1027,11 +1130,13 @@ void EditorHelp::_update_doc() {
10271130

10281131
class_desc->push_indent(1);
10291132
class_desc->push_font(theme_cache.doc_bold_font);
1133+
class_desc->push_font_size(theme_cache.doc_code_font_size);
10301134
class_desc->push_color(theme_cache.text_color);
10311135

10321136
_add_text(brief_class_descr);
10331137

10341138
class_desc->pop(); // color
1139+
class_desc->pop(); // font_size
10351140
class_desc->pop(); // font
10361141
class_desc->pop(); // indent
10371142
}
@@ -2263,9 +2368,6 @@ void EditorHelp::_update_doc() {
22632368
class_desc->add_newline();
22642369
class_desc->add_newline();
22652370

2266-
// Free the scroll.
2267-
scroll_locked = false;
2268-
22692371
#undef HANDLE_DOC
22702372
}
22712373

@@ -2349,7 +2451,7 @@ void EditorHelp::_help_callback(const String &p_topic) {
23492451
}
23502452
}
23512453

2352-
static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class) {
2454+
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) {
23532455
const DocTools *doc = EditorHelp::get_doc_data();
23542456

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

2371-
const int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
2372-
const int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
2473+
int doc_code_font_size = p_doc_code_font_size;
2474+
int doc_kbd_font_size = p_doc_kbd_font_size;
2475+
2476+
if (doc_code_font_size <= 0) {
2477+
doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
2478+
}
2479+
if (doc_kbd_font_size <= 0) {
2480+
doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
2481+
}
23732482

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

28392948
void EditorHelp::_add_text(const String &p_bbcode) {
2840-
_add_text_to_rt(p_bbcode, class_desc, this, edited_class);
2949+
_add_text_to_rt(p_bbcode, class_desc, this, edited_class, theme_cache.doc_code_font_size, theme_cache.doc_kbd_font_size);
28412950
}
28422951

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

30863195
ADD_SIGNAL(MethodInfo("go_to_help"));
30873196
ADD_SIGNAL(MethodInfo("request_save_history"));
3197+
ADD_SIGNAL(MethodInfo("zoomed", PropertyInfo(Variant::FLOAT, "zoom_factor")));
30883198
}
30893199

30903200
void EditorHelp::init_gdext_pointers() {
@@ -3097,6 +3207,10 @@ EditorHelp::EditorHelp() {
30973207

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

3210+
ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KeyModifierMask::CMD_OR_CTRL | Key::EQUAL);
3211+
ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KeyModifierMask::CMD_OR_CTRL | Key::MINUS);
3212+
ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KeyModifierMask::CMD_OR_CTRL | Key::KEY_0);
3213+
31003214
class_desc = memnew(RichTextLabel);
31013215
class_desc->set_tab_size(8);
31023216
add_child(class_desc);
@@ -3123,6 +3237,27 @@ EditorHelp::EditorHelp() {
31233237
toggle_scripts_button->set_flat(true);
31243238
toggle_scripts_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
31253239
status_bar->add_child(toggle_scripts_button);
3240+
status_bar->add_spacer();
3241+
status_bar->add_child(memnew(VSeparator));
3242+
3243+
// Zoom
3244+
zoom_button = memnew(MenuButton);
3245+
status_bar->add_child(zoom_button);
3246+
zoom_button->set_flat(true);
3247+
zoom_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
3248+
zoom_button->set_tooltip_text(TTR("Zoom factor"));
3249+
zoom_button->set_text("100 %");
3250+
3251+
PopupMenu *zoom_menu = zoom_button->get_popup();
3252+
int preset_count = sizeof(ZOOM_FACTOR_PRESETS) / sizeof(float);
3253+
3254+
for (int i = 0; i < preset_count; i++) {
3255+
float z = ZOOM_FACTOR_PRESETS[i];
3256+
zoom_menu->add_item(itos(Math::round(z * 100)) + " %");
3257+
zoom_menu->set_item_metadata(i, z);
3258+
}
3259+
3260+
zoom_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorHelp::_zoom_popup_id_pressed));
31263261

31273262
class_desc->set_selection_enabled(true);
31283263
class_desc->set_context_menu_enabled(true);

editor/editor_help.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class EditorHelp : public VBoxContainer {
112112
HashMap<String, HashMap<String, int>> enum_values_line;
113113
int description_line = 0;
114114

115+
MenuButton *zoom_button = nullptr;
116+
float zoom_factor = 1.0f;
117+
115118
RichTextLabel *class_desc = nullptr;
116119
HSplitContainer *h_split = nullptr;
117120
static DocTools *doc;
@@ -156,7 +159,6 @@ class EditorHelp : public VBoxContainer {
156159
void _help_callback(const String &p_topic);
157160

158161
void _add_text(const String &p_bbcode);
159-
bool scroll_locked = false;
160162

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

184+
void _zoom_popup_id_pressed(int p_idx);
185+
void _zoom_in();
186+
void _zoom_out();
187+
void _zoom_to(float p_zoom_factor);
188+
182189
Error _goto_desc(const String &p_class);
183190
//void _update_history_buttons();
184191
void _update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
@@ -243,6 +250,9 @@ class EditorHelp : public VBoxContainer {
243250
int get_scroll() const;
244251
void set_scroll(int p_scroll);
245252

253+
void set_zoom_factor(float p_zoom_factor);
254+
float get_zoom_factor() const;
255+
246256
void update_toggle_scripts_button();
247257

248258
static void init_gdext_pointers();

editor/plugins/script_editor_plugin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3503,11 +3503,13 @@ void ScriptEditor::_help_class_open(const String &p_class) {
35033503
EditorHelp *eh = memnew(EditorHelp);
35043504

35053505
eh->set_name(p_class);
3506+
eh->set_zoom_factor(zoom_factor);
35063507
tab_container->add_child(eh);
35073508
_go_to_tab(tab_container->get_tab_count() - 1);
35083509
eh->go_to_class(p_class);
35093510
eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
35103511
eh->connect("request_save_history", callable_mp(this, &ScriptEditor::_save_history));
3512+
eh->connect("zoomed", callable_mp(this, &ScriptEditor::_set_zoom_factor));
35113513
_add_recent_script(p_class);
35123514
_sort_list_on_update = true;
35133515
_update_script_names();
@@ -3524,9 +3526,11 @@ void ScriptEditor::_help_class_goto(const String &p_desc) {
35243526
EditorHelp *eh = memnew(EditorHelp);
35253527

35263528
eh->set_name(cname);
3529+
eh->set_zoom_factor(zoom_factor);
35273530
tab_container->add_child(eh);
35283531
eh->go_to_help(p_desc);
35293532
eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
3533+
eh->connect("zoomed", callable_mp(this, &ScriptEditor::_set_zoom_factor));
35303534
_add_recent_script(eh->get_class());
35313535
_sort_list_on_update = true;
35323536
_update_script_names();
@@ -3941,6 +3945,11 @@ void ScriptEditor::_set_zoom_factor(float p_zoom_factor) {
39413945
cte->set_zoom_factor(zoom_factor);
39423946
}
39433947
}
3948+
} else {
3949+
EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
3950+
if (help) {
3951+
help->set_zoom_factor(zoom_factor);
3952+
}
39443953
}
39453954
}
39463955
}

0 commit comments

Comments
 (0)