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 "editor/editor_scale.h" |
35 | #include "editor/editor_settings.h" |
36 | |
37 | void EditorZoomWidget::_update_zoom_label() { |
38 | String zoom_text; |
39 | // The zoom level displayed is relative to the editor scale |
40 | // (like in most image editors). Its lower bound is clamped to 1 as some people |
41 | // lower the editor scale to increase the available real estate, |
42 | // even if their display doesn't have a particularly low DPI. |
43 | if (zoom >= 10) { |
44 | zoom_text = TS->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100))); |
45 | } else { |
46 | // 2 decimal places if the zoom is below 10%, 1 decimal place if it's below 1000%. |
47 | zoom_text = TS->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, (zoom >= 0.1) ? 0.1 : 0.01))); |
48 | } |
49 | zoom_text += " " + TS->percent_sign(); |
50 | zoom_reset->set_text(zoom_text); |
51 | } |
52 | |
53 | void EditorZoomWidget::_button_zoom_minus() { |
54 | set_zoom_by_increments(-6, Input::get_singleton()->is_key_pressed(Key::ALT)); |
55 | emit_signal(SNAME("zoom_changed" ), zoom); |
56 | } |
57 | |
58 | void EditorZoomWidget::_button_zoom_reset() { |
59 | set_zoom(1.0 * MAX(1, EDSCALE)); |
60 | emit_signal(SNAME("zoom_changed" ), zoom); |
61 | } |
62 | |
63 | void EditorZoomWidget::_button_zoom_plus() { |
64 | set_zoom_by_increments(6, Input::get_singleton()->is_key_pressed(Key::ALT)); |
65 | emit_signal(SNAME("zoom_changed" ), zoom); |
66 | } |
67 | |
68 | float EditorZoomWidget::get_zoom() { |
69 | return zoom; |
70 | } |
71 | |
72 | void EditorZoomWidget::set_zoom(float p_zoom) { |
73 | if (p_zoom > 0 && p_zoom != zoom) { |
74 | zoom = p_zoom; |
75 | _update_zoom_label(); |
76 | } |
77 | } |
78 | |
79 | void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_integer_only) { |
80 | // Remove editor scale from the index computation. |
81 | const float zoom_noscale = zoom / MAX(1, EDSCALE); |
82 | |
83 | if (p_integer_only) { |
84 | // Only visit integer scaling factors above 100%, and fractions with an integer denominator below 100% |
85 | // (1/2 = 50%, 1/3 = 33.33%, 1/4 = 25%, …). |
86 | // This is useful when working on pixel art projects to avoid distortion. |
87 | // This algorithm is designed to handle fractional start zoom values correctly |
88 | // (e.g. 190% will zoom up to 200% and down to 100%). |
89 | if (zoom_noscale + p_increment_count * 0.001 >= 1.0 - CMP_EPSILON) { |
90 | // New zoom is certain to be above 100%. |
91 | if (p_increment_count >= 1) { |
92 | // Zooming. |
93 | set_zoom(Math::floor(zoom_noscale + p_increment_count) * MAX(1, EDSCALE)); |
94 | } else { |
95 | // Dezooming. |
96 | set_zoom(Math::ceil(zoom_noscale + p_increment_count) * MAX(1, EDSCALE)); |
97 | } |
98 | } else { |
99 | if (p_increment_count >= 1) { |
100 | // Zooming. Convert the current zoom into a denominator. |
101 | float new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count); |
102 | if (Math::is_equal_approx(zoom_noscale, new_zoom)) { |
103 | // New zoom is identical to the old zoom, so try again. |
104 | // This can happen due to floating-point precision issues. |
105 | new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count - 1); |
106 | } |
107 | set_zoom(new_zoom * MAX(1, EDSCALE)); |
108 | } else { |
109 | // Dezooming. Convert the current zoom into a denominator. |
110 | float new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count); |
111 | if (Math::is_equal_approx(zoom_noscale, new_zoom)) { |
112 | // New zoom is identical to the old zoom, so try again. |
113 | // This can happen due to floating-point precision issues. |
114 | new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count + 1); |
115 | } |
116 | set_zoom(new_zoom * MAX(1, EDSCALE)); |
117 | } |
118 | } |
119 | } else { |
120 | // Base increment factor defined as the twelveth root of two. |
121 | // This allow a smooth geometric evolution of the zoom, with the advantage of |
122 | // visiting all integer power of two scale factors. |
123 | // note: this is analogous to the 'semitones' interval in the music world |
124 | // In order to avoid numerical imprecisions, we compute and edit a zoom index |
125 | // with the following relation: zoom = 2 ^ (index / 12) |
126 | |
127 | if (zoom < CMP_EPSILON || p_increment_count == 0) { |
128 | return; |
129 | } |
130 | |
131 | // zoom = 2**(index/12) => log2(zoom) = index/12 |
132 | float closest_zoom_index = Math::round(Math::log(zoom_noscale) * 12.f / Math::log(2.f)); |
133 | |
134 | float new_zoom_index = closest_zoom_index + p_increment_count; |
135 | float new_zoom = Math::pow(2.f, new_zoom_index / 12.f); |
136 | |
137 | // Restore Editor scale transformation. |
138 | new_zoom *= MAX(1, EDSCALE); |
139 | |
140 | set_zoom(new_zoom); |
141 | } |
142 | } |
143 | |
144 | void EditorZoomWidget::_notification(int p_what) { |
145 | switch (p_what) { |
146 | case NOTIFICATION_ENTER_TREE: |
147 | case NOTIFICATION_THEME_CHANGED: { |
148 | zoom_minus->set_icon(get_editor_theme_icon(SNAME("ZoomLess" ))); |
149 | zoom_plus->set_icon(get_editor_theme_icon(SNAME("ZoomMore" ))); |
150 | } break; |
151 | } |
152 | } |
153 | |
154 | void EditorZoomWidget::_bind_methods() { |
155 | ClassDB::bind_method(D_METHOD("set_zoom" , "zoom" ), &EditorZoomWidget::set_zoom); |
156 | ClassDB::bind_method(D_METHOD("get_zoom" ), &EditorZoomWidget::get_zoom); |
157 | ClassDB::bind_method(D_METHOD("set_zoom_by_increments" , "increment" , "integer_only" ), &EditorZoomWidget::set_zoom_by_increments); |
158 | |
159 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom" ), "set_zoom" , "get_zoom" ); |
160 | |
161 | ADD_SIGNAL(MethodInfo("zoom_changed" , PropertyInfo(Variant::FLOAT, "zoom" ))); |
162 | } |
163 | |
164 | void EditorZoomWidget::set_shortcut_context(Node *p_node) const { |
165 | zoom_minus->set_shortcut_context(p_node); |
166 | zoom_plus->set_shortcut_context(p_node); |
167 | zoom_reset->set_shortcut_context(p_node); |
168 | } |
169 | |
170 | EditorZoomWidget::EditorZoomWidget() { |
171 | // Zoom buttons |
172 | zoom_minus = memnew(Button); |
173 | zoom_minus->set_flat(true); |
174 | add_child(zoom_minus); |
175 | zoom_minus->connect("pressed" , callable_mp(this, &EditorZoomWidget::_button_zoom_minus)); |
176 | zoom_minus->set_shortcut(ED_SHORTCUT_ARRAY("canvas_item_editor/zoom_minus" , TTR("Zoom Out" ), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::MINUS), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_SUBTRACT) })); |
177 | zoom_minus->set_shortcut_context(this); |
178 | zoom_minus->set_focus_mode(FOCUS_NONE); |
179 | |
180 | zoom_reset = memnew(Button); |
181 | zoom_reset->set_flat(true); |
182 | zoom_reset->add_theme_style_override("normal" , memnew(StyleBoxEmpty)); |
183 | zoom_reset->add_theme_style_override("hover" , memnew(StyleBoxEmpty)); |
184 | zoom_reset->add_theme_style_override("focus" , memnew(StyleBoxEmpty)); |
185 | zoom_reset->add_theme_style_override("pressed" , memnew(StyleBoxEmpty)); |
186 | add_child(zoom_reset); |
187 | zoom_reset->add_theme_constant_override("outline_size" , Math::ceil(2 * EDSCALE)); |
188 | zoom_reset->add_theme_color_override("font_outline_color" , Color(0, 0, 0)); |
189 | zoom_reset->add_theme_color_override("font_color" , Color(1, 1, 1)); |
190 | zoom_reset->connect("pressed" , callable_mp(this, &EditorZoomWidget::_button_zoom_reset)); |
191 | zoom_reset->set_shortcut(ED_GET_SHORTCUT("canvas_item_editor/zoom_100_percent" )); |
192 | zoom_reset->set_shortcut_context(this); |
193 | zoom_reset->set_focus_mode(FOCUS_NONE); |
194 | zoom_reset->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
195 | // Prevent the button's size from changing when the text size changes |
196 | zoom_reset->set_custom_minimum_size(Size2(56 * EDSCALE, 0)); |
197 | |
198 | zoom_plus = memnew(Button); |
199 | zoom_plus->set_flat(true); |
200 | add_child(zoom_plus); |
201 | zoom_plus->connect("pressed" , callable_mp(this, &EditorZoomWidget::_button_zoom_plus)); |
202 | zoom_plus->set_shortcut(ED_SHORTCUT_ARRAY("canvas_item_editor/zoom_plus" , TTR("Zoom In" ), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::EQUAL), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_ADD) })); |
203 | zoom_plus->set_shortcut_context(this); |
204 | zoom_plus->set_focus_mode(FOCUS_NONE); |
205 | |
206 | _update_zoom_label(); |
207 | |
208 | add_theme_constant_override("separation" , 0); |
209 | } |
210 | |