editor_zoom_widget.cpp 9.4 KB

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