| 1 | /**************************************************************************/ |
| 2 | /* editor_zoom_widget.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "editor_zoom_widget.h" |
| 32 | |
| 33 | #include "core/os/keyboard.h" |
| 34 | #include "core/string/translation_server.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "editor/themes/editor_scale.h" |
| 37 | |
| 38 | void EditorZoomWidget::_update_zoom_label() { |
| 39 | const String &lang = _get_locale(); |
| 40 | |
| 41 | String zoom_text; |
| 42 | // The zoom level displayed is relative to the editor scale |
| 43 | // (like in most image editors). Its lower bound is clamped to 1 as some people |
| 44 | // lower the editor scale to increase the available real estate, |
| 45 | // even if their display doesn't have a particularly low DPI. |
| 46 | if (zoom >= 10) { |
| 47 | zoom_text = TranslationServer::get_singleton()->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100)), lang); |
| 48 | } else { |
| 49 | // 2 decimal places if the zoom is below 10%, 1 decimal place if it's below 1000%. |
| 50 | zoom_text = TranslationServer::get_singleton()->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, (zoom >= 0.1) ? 0.1 : 0.01)), lang); |
| 51 | } |
| 52 | zoom_text += " " + TranslationServer::get_singleton()->get_percent_sign(lang); |
| 53 | zoom_reset->set_text(zoom_text); |
| 54 | } |
| 55 | |
| 56 | void EditorZoomWidget::_button_zoom_minus() { |
| 57 | set_zoom_by_increments(-6, Input::get_singleton()->is_key_pressed(Key::ALT)); |
| 58 | emit_signal(SNAME("zoom_changed"), zoom); |
| 59 | } |
| 60 | |
| 61 | void EditorZoomWidget::_button_zoom_reset() { |
| 62 | set_zoom(1.0 * MAX(1, EDSCALE)); |
| 63 | emit_signal(SNAME("zoom_changed"), zoom); |
| 64 | } |
| 65 | |
| 66 | void EditorZoomWidget::_button_zoom_plus() { |
| 67 | set_zoom_by_increments(6, Input::get_singleton()->is_key_pressed(Key::ALT)); |
| 68 | emit_signal(SNAME("zoom_changed"), zoom); |
| 69 | } |
| 70 | |
| 71 | float EditorZoomWidget::get_zoom() { |
| 72 | return zoom; |
| 73 | } |
| 74 | |
| 75 | void EditorZoomWidget::set_zoom(float p_zoom) { |
| 76 | float new_zoom = CLAMP(p_zoom, min_zoom, max_zoom); |
| 77 | if (zoom != new_zoom) { |
| 78 | zoom = new_zoom; |
| 79 | _update_zoom_label(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | float EditorZoomWidget::get_min_zoom() { |
| 84 | return min_zoom; |
| 85 | } |
| 86 | |
| 87 | float EditorZoomWidget::get_max_zoom() { |
| 88 | return max_zoom; |
| 89 | } |
| 90 | |
| 91 | void EditorZoomWidget::setup_zoom_limits(float p_min, float p_max) { |
| 92 | ERR_FAIL_COND(p_min < 0 || p_min > p_max); |
| 93 | |
| 94 | min_zoom = p_min; |
| 95 | max_zoom = p_max; |
| 96 | |
| 97 | if (zoom > max_zoom) { |
| 98 | set_zoom(max_zoom); |
| 99 | emit_signal(SNAME("zoom_changed"), zoom); |
| 100 | } else if (zoom < min_zoom) { |
| 101 | set_zoom(min_zoom); |
| 102 | emit_signal(SNAME("zoom_changed"), zoom); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_integer_only) { |
| 107 | // Remove editor scale from the index computation. |
| 108 | const float zoom_noscale = zoom / MAX(1, EDSCALE); |
| 109 | |
| 110 | if (p_integer_only) { |
| 111 | // Only visit integer scaling factors above 100%, and fractions with an integer denominator below 100% |
| 112 | // (1/2 = 50%, 1/3 = 33.33%, 1/4 = 25%, …). |
| 113 | // This is useful when working on pixel art projects to avoid distortion. |
| 114 | // This algorithm is designed to handle fractional start zoom values correctly |
| 115 | // (e.g. 190% will zoom up to 200% and down to 100%). |
| 116 | if (zoom_noscale + p_increment_count * 0.001 >= 1.0 - CMP_EPSILON) { |
| 117 | // New zoom is certain to be above 100%. |
| 118 | if (p_increment_count >= 1) { |
| 119 | // Zooming. |
| 120 | set_zoom(Math::floor(zoom_noscale + p_increment_count) * MAX(1, EDSCALE)); |
| 121 | } else { |
| 122 | // Dezooming. |
| 123 | set_zoom(Math::ceil(zoom_noscale + p_increment_count) * MAX(1, EDSCALE)); |
| 124 | } |
| 125 | } else { |
| 126 | if (p_increment_count >= 1) { |
| 127 | // Zooming in. Convert the current zoom into a denominator. |
| 128 | float new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count); |
| 129 | if (Math::is_equal_approx(zoom_noscale, new_zoom)) { |
| 130 | // New zoom is identical to the old zoom, so try again. |
| 131 | // This can happen due to floating-point precision issues. |
| 132 | new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count - 1); |
| 133 | } |
| 134 | set_zoom(new_zoom * MAX(1, EDSCALE)); |
| 135 | } else { |
| 136 | // Zooming out. Convert the current zoom into a denominator. |
| 137 | float new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count); |
| 138 | if (Math::is_equal_approx(zoom_noscale, new_zoom)) { |
| 139 | // New zoom is identical to the old zoom, so try again. |
| 140 | // This can happen due to floating-point precision issues. |
| 141 | new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count + 1); |
| 142 | } |
| 143 | set_zoom(new_zoom * MAX(1, EDSCALE)); |
| 144 | } |
| 145 | } |
| 146 | } else { |
| 147 | if (zoom < CMP_EPSILON || p_increment_count == 0) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Zoom is calculated as pow(zoom_factor, zoom_step). |
| 152 | // This ensures the zoom will always equal 100% when zoom_step is 0. |
| 153 | float zoom_factor = EDITOR_GET("editors/2d/zoom_speed_factor"); |
| 154 | float current_zoom_step = Math::round(Math::log(zoom_noscale) / Math::log(zoom_factor)); |
| 155 | float new_zoom = Math::pow(zoom_factor, current_zoom_step + p_increment_count); |
| 156 | |
| 157 | // Restore Editor scale transformation. |
| 158 | new_zoom *= MAX(1, EDSCALE); |
| 159 | |
| 160 | set_zoom(new_zoom); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void EditorZoomWidget::_notification(int p_what) { |
| 165 | switch (p_what) { |
| 166 | case NOTIFICATION_THEME_CHANGED: { |
| 167 | zoom_minus->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess"))); |
| 168 | zoom_plus->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore"))); |
| 169 | } break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void EditorZoomWidget::_bind_methods() { |
| 174 | ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &EditorZoomWidget::set_zoom); |
| 175 | ClassDB::bind_method(D_METHOD("get_zoom"), &EditorZoomWidget::get_zoom); |
| 176 | ClassDB::bind_method(D_METHOD("set_zoom_by_increments", "increment", "integer_only"), &EditorZoomWidget::set_zoom_by_increments); |
| 177 | |
| 178 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom"); |
| 179 | |
| 180 | ADD_SIGNAL(MethodInfo("zoom_changed", PropertyInfo(Variant::FLOAT, "zoom"))); |
| 181 | } |
| 182 | |
| 183 | void EditorZoomWidget::set_shortcut_context(Node *p_node) const { |
| 184 | zoom_minus->set_shortcut_context(p_node); |
| 185 | zoom_plus->set_shortcut_context(p_node); |
| 186 | zoom_reset->set_shortcut_context(p_node); |
| 187 | } |
| 188 | |
| 189 | EditorZoomWidget::EditorZoomWidget() { |
| 190 | // Zoom buttons |
| 191 | zoom_minus = memnew(Button); |
| 192 | zoom_minus->set_accessibility_name(TTRC("Zoom Out")); |
| 193 | zoom_minus->set_flat(true); |
| 194 | zoom_minus->set_shortcut(ED_SHORTCUT_ARRAY("canvas_item_editor/zoom_minus", TTRC("Zoom Out"), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) })); |
| 195 | zoom_minus->set_shortcut_context(this); |
| 196 | zoom_minus->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 197 | add_child(zoom_minus); |
| 198 | zoom_minus->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_minus)); |
| 199 | |
| 200 | zoom_reset = memnew(Button); |
| 201 | zoom_reset->set_flat(true); |
| 202 | zoom_reset->set_accessibility_name(TTRC("Reset Zoom")); |
| 203 | |
| 204 | Ref<StyleBoxEmpty> empty_stylebox = memnew(StyleBoxEmpty); |
| 205 | zoom_reset->add_theme_style_override(CoreStringName(normal), empty_stylebox); |
| 206 | zoom_reset->add_theme_style_override(SceneStringName(hover), empty_stylebox); |
| 207 | zoom_reset->add_theme_style_override("focus", empty_stylebox); |
| 208 | zoom_reset->add_theme_style_override(SceneStringName(pressed), empty_stylebox); |
| 209 | zoom_reset->add_theme_constant_override("outline_size", Math::ceil(2 * EDSCALE)); |
| 210 | zoom_reset->add_theme_color_override("font_outline_color", Color(0, 0, 0)); |
| 211 | zoom_reset->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1)); |
| 212 | |
| 213 | zoom_reset->set_shortcut(ED_GET_SHORTCUT("canvas_item_editor/zoom_100_percent")); |
| 214 | zoom_reset->set_shortcut_context(this); |
| 215 | zoom_reset->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 216 | zoom_reset->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 217 | // Prevent the button's size from changing when the text size changes |
| 218 | zoom_reset->set_custom_minimum_size(Size2(56 * EDSCALE, 0)); |
| 219 | add_child(zoom_reset); |
| 220 | zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_reset)); |
| 221 | |
| 222 | zoom_plus = memnew(Button); |
| 223 | zoom_plus->set_accessibility_name(TTRC("Zoom In")); |
| 224 | zoom_plus->set_flat(true); |
| 225 | zoom_plus->set_shortcut(ED_SHORTCUT_ARRAY("canvas_item_editor/zoom_plus", TTRC("Zoom In"), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) })); |
| 226 | zoom_plus->set_shortcut_context(this); |
| 227 | zoom_plus->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 228 | add_child(zoom_plus); |
| 229 | zoom_plus->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_plus)); |
| 230 | |
| 231 | _update_zoom_label(); |
| 232 | |
| 233 | add_theme_constant_override("separation", 0); |
| 234 | } |