editor_zoom_widget.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_settings.h"
  33. #include "editor/themes/editor_scale.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. float new_zoom = CLAMP(p_zoom, min_zoom, max_zoom);
  66. if (zoom != new_zoom) {
  67. zoom = new_zoom;
  68. _update_zoom_label();
  69. }
  70. }
  71. float EditorZoomWidget::get_min_zoom() {
  72. return min_zoom;
  73. }
  74. float EditorZoomWidget::get_max_zoom() {
  75. return max_zoom;
  76. }
  77. void EditorZoomWidget::setup_zoom_limits(float p_min, float p_max) {
  78. ERR_FAIL_COND(p_min < 0 || p_min > p_max);
  79. min_zoom = p_min;
  80. max_zoom = p_max;
  81. if (zoom > max_zoom) {
  82. set_zoom(max_zoom);
  83. emit_signal(SNAME("zoom_changed"), zoom);
  84. } else if (zoom < min_zoom) {
  85. set_zoom(min_zoom);
  86. emit_signal(SNAME("zoom_changed"), zoom);
  87. }
  88. }
  89. void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_integer_only) {
  90. // Remove editor scale from the index computation.
  91. const float zoom_noscale = zoom / MAX(1, EDSCALE);
  92. if (p_integer_only) {
  93. // Only visit integer scaling factors above 100%, and fractions with an integer denominator below 100%
  94. // (1/2 = 50%, 1/3 = 33.33%, 1/4 = 25%, …).
  95. // This is useful when working on pixel art projects to avoid distortion.
  96. // This algorithm is designed to handle fractional start zoom values correctly
  97. // (e.g. 190% will zoom up to 200% and down to 100%).
  98. if (zoom_noscale + p_increment_count * 0.001 >= 1.0 - CMP_EPSILON) {
  99. // New zoom is certain to be above 100%.
  100. if (p_increment_count >= 1) {
  101. // Zooming.
  102. set_zoom(Math::floor(zoom_noscale + p_increment_count) * MAX(1, EDSCALE));
  103. } else {
  104. // Dezooming.
  105. set_zoom(Math::ceil(zoom_noscale + p_increment_count) * MAX(1, EDSCALE));
  106. }
  107. } else {
  108. if (p_increment_count >= 1) {
  109. // Zooming in. Convert the current zoom into a denominator.
  110. float new_zoom = 1.0 / Math::ceil(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::ceil(1.0 / zoom_noscale - p_increment_count - 1);
  115. }
  116. set_zoom(new_zoom * MAX(1, EDSCALE));
  117. } else {
  118. // Zooming out. Convert the current zoom into a denominator.
  119. float new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count);
  120. if (Math::is_equal_approx(zoom_noscale, new_zoom)) {
  121. // New zoom is identical to the old zoom, so try again.
  122. // This can happen due to floating-point precision issues.
  123. new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count + 1);
  124. }
  125. set_zoom(new_zoom * MAX(1, EDSCALE));
  126. }
  127. }
  128. } else {
  129. if (zoom < CMP_EPSILON || p_increment_count == 0) {
  130. return;
  131. }
  132. // Zoom is calculated as pow(zoom_factor, zoom_step).
  133. // This ensures the zoom will always equal 100% when zoom_step is 0.
  134. float zoom_factor = EDITOR_GET("editors/2d/zoom_speed_factor");
  135. float current_zoom_step = Math::round(Math::log(zoom_noscale) / Math::log(zoom_factor));
  136. float new_zoom = Math::pow(zoom_factor, current_zoom_step + p_increment_count);
  137. // Restore Editor scale transformation.
  138. new_zoom *= MAX(1, EDSCALE);
  139. set_zoom(new_zoom);
  140. }
  141. }
  142. void EditorZoomWidget::_notification(int p_what) {
  143. switch (p_what) {
  144. case NOTIFICATION_ENTER_TREE:
  145. case NOTIFICATION_THEME_CHANGED: {
  146. zoom_minus->set_button_icon(get_editor_theme_icon(SNAME("ZoomLess")));
  147. zoom_plus->set_button_icon(get_editor_theme_icon(SNAME("ZoomMore")));
  148. } break;
  149. }
  150. }
  151. void EditorZoomWidget::_bind_methods() {
  152. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &EditorZoomWidget::set_zoom);
  153. ClassDB::bind_method(D_METHOD("get_zoom"), &EditorZoomWidget::get_zoom);
  154. ClassDB::bind_method(D_METHOD("set_zoom_by_increments", "increment", "integer_only"), &EditorZoomWidget::set_zoom_by_increments);
  155. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  156. ADD_SIGNAL(MethodInfo("zoom_changed", PropertyInfo(Variant::FLOAT, "zoom")));
  157. }
  158. void EditorZoomWidget::set_shortcut_context(Node *p_node) const {
  159. zoom_minus->set_shortcut_context(p_node);
  160. zoom_plus->set_shortcut_context(p_node);
  161. zoom_reset->set_shortcut_context(p_node);
  162. }
  163. EditorZoomWidget::EditorZoomWidget() {
  164. // Zoom buttons
  165. zoom_minus = memnew(Button);
  166. zoom_minus->set_flat(true);
  167. 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) }));
  168. zoom_minus->set_shortcut_context(this);
  169. zoom_minus->set_focus_mode(FOCUS_NONE);
  170. add_child(zoom_minus);
  171. zoom_minus->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_minus));
  172. zoom_reset = memnew(Button);
  173. zoom_reset->set_flat(true);
  174. Ref<StyleBoxEmpty> empty_stylebox = memnew(StyleBoxEmpty);
  175. zoom_reset->add_theme_style_override(CoreStringName(normal), empty_stylebox);
  176. zoom_reset->add_theme_style_override(SceneStringName(hover), empty_stylebox);
  177. zoom_reset->add_theme_style_override("focus", empty_stylebox);
  178. zoom_reset->add_theme_style_override(SceneStringName(pressed), empty_stylebox);
  179. zoom_reset->add_theme_constant_override("outline_size", Math::ceil(2 * EDSCALE));
  180. zoom_reset->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  181. zoom_reset->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  182. zoom_reset->set_shortcut(ED_GET_SHORTCUT("canvas_item_editor/zoom_100_percent"));
  183. zoom_reset->set_shortcut_context(this);
  184. zoom_reset->set_focus_mode(FOCUS_NONE);
  185. zoom_reset->set_text_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  186. // Prevent the button's size from changing when the text size changes
  187. zoom_reset->set_custom_minimum_size(Size2(56 * EDSCALE, 0));
  188. add_child(zoom_reset);
  189. zoom_reset->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_reset));
  190. zoom_plus = memnew(Button);
  191. zoom_plus->set_flat(true);
  192. 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) }));
  193. zoom_plus->set_shortcut_context(this);
  194. zoom_plus->set_focus_mode(FOCUS_NONE);
  195. add_child(zoom_plus);
  196. zoom_plus->connect(SceneStringName(pressed), callable_mp(this, &EditorZoomWidget::_button_zoom_plus));
  197. _update_zoom_label();
  198. add_theme_constant_override("separation", 0);
  199. }