2
0

editor_zoom_widget.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*************************************************************************/
  2. /* editor_zoom_widget.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. // Don't show a decimal when the zoom level is higher than 1000 %.
  42. zoom_text = TS->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100))) + " " + TS->percent_sign();
  43. } else {
  44. zoom_text = TS->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, 0.1))) + " " + TS->percent_sign();
  45. }
  46. zoom_reset->set_text(zoom_text);
  47. }
  48. void EditorZoomWidget::_button_zoom_minus() {
  49. set_zoom_by_increments(-6, Input::get_singleton()->is_key_pressed(KEY_ALT));
  50. emit_signal(SNAME("zoom_changed"), zoom);
  51. }
  52. void EditorZoomWidget::_button_zoom_reset() {
  53. set_zoom(1.0 * MAX(1, EDSCALE));
  54. emit_signal(SNAME("zoom_changed"), zoom);
  55. }
  56. void EditorZoomWidget::_button_zoom_plus() {
  57. set_zoom_by_increments(6, Input::get_singleton()->is_key_pressed(KEY_ALT));
  58. emit_signal(SNAME("zoom_changed"), zoom);
  59. }
  60. float EditorZoomWidget::get_zoom() {
  61. return zoom;
  62. }
  63. void EditorZoomWidget::set_zoom(float p_zoom) {
  64. if (p_zoom > 0 && p_zoom != zoom) {
  65. zoom = p_zoom;
  66. _update_zoom_label();
  67. }
  68. }
  69. void EditorZoomWidget::set_zoom_by_increments(int p_increment_count, bool p_integer_only) {
  70. // Remove editor scale from the index computation.
  71. const float zoom_noscale = zoom / MAX(1, EDSCALE);
  72. if (p_integer_only) {
  73. // Only visit integer scaling factors above 100%, and fractions with an integer denominator below 100%
  74. // (1/2 = 50%, 1/3 = 33.33%, 1/4 = 25%, …).
  75. // This is useful when working on pixel art projects to avoid distortion.
  76. // This algorithm is designed to handle fractional start zoom values correctly
  77. // (e.g. 190% will zoom up to 200% and down to 100%).
  78. if (zoom_noscale + p_increment_count * 0.001 >= 1.0 - CMP_EPSILON) {
  79. // New zoom is certain to be above 100%.
  80. if (p_increment_count >= 1) {
  81. // Zooming.
  82. set_zoom(Math::floor(zoom_noscale + p_increment_count) * MAX(1, EDSCALE));
  83. } else {
  84. // Dezooming.
  85. set_zoom(Math::ceil(zoom_noscale + p_increment_count) * MAX(1, EDSCALE));
  86. }
  87. } else {
  88. if (p_increment_count >= 1) {
  89. // Zooming. Convert the current zoom into a denominator.
  90. float new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count);
  91. if (Math::is_equal_approx(zoom_noscale, new_zoom)) {
  92. // New zoom is identical to the old zoom, so try again.
  93. // This can happen due to floating-point precision issues.
  94. new_zoom = 1.0 / Math::ceil(1.0 / zoom_noscale - p_increment_count - 1);
  95. }
  96. set_zoom(new_zoom * MAX(1, EDSCALE));
  97. } else {
  98. // Dezooming. Convert the current zoom into a denominator.
  99. float new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count);
  100. if (Math::is_equal_approx(zoom_noscale, new_zoom)) {
  101. // New zoom is identical to the old zoom, so try again.
  102. // This can happen due to floating-point precision issues.
  103. new_zoom = 1.0 / Math::floor(1.0 / zoom_noscale - p_increment_count + 1);
  104. }
  105. set_zoom(new_zoom * MAX(1, EDSCALE));
  106. }
  107. }
  108. } else {
  109. // Base increment factor defined as the twelveth root of two.
  110. // This allow a smooth geometric evolution of the zoom, with the advantage of
  111. // visiting all integer power of two scale factors.
  112. // note: this is analogous to the 'semitones' interval in the music world
  113. // In order to avoid numerical imprecisions, we compute and edit a zoom index
  114. // with the following relation: zoom = 2 ^ (index / 12)
  115. if (zoom < CMP_EPSILON || p_increment_count == 0) {
  116. return;
  117. }
  118. // zoom = 2**(index/12) => log2(zoom) = index/12
  119. float closest_zoom_index = Math::round(Math::log(zoom_noscale) * 12.f / Math::log(2.f));
  120. float new_zoom_index = closest_zoom_index + p_increment_count;
  121. float new_zoom = Math::pow(2.f, new_zoom_index / 12.f);
  122. // Restore Editor scale transformation
  123. new_zoom *= MAX(1, EDSCALE);
  124. set_zoom(new_zoom);
  125. }
  126. }
  127. void EditorZoomWidget::_notification(int p_what) {
  128. switch (p_what) {
  129. case NOTIFICATION_ENTER_TREE:
  130. case NOTIFICATION_THEME_CHANGED:
  131. zoom_minus->set_icon(get_theme_icon(SNAME("ZoomLess"), SNAME("EditorIcons")));
  132. zoom_plus->set_icon(get_theme_icon(SNAME("ZoomMore"), SNAME("EditorIcons")));
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. void EditorZoomWidget::_bind_methods() {
  139. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &EditorZoomWidget::set_zoom);
  140. ClassDB::bind_method(D_METHOD("get_zoom"), &EditorZoomWidget::get_zoom);
  141. ClassDB::bind_method(D_METHOD("set_zoom_by_increments", "increment", "integer_only"), &EditorZoomWidget::set_zoom_by_increments);
  142. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  143. ADD_SIGNAL(MethodInfo("zoom_changed", PropertyInfo(Variant::FLOAT, "zoom")));
  144. }
  145. EditorZoomWidget::EditorZoomWidget() {
  146. // Zoom buttons
  147. zoom_minus = memnew(Button);
  148. zoom_minus->set_flat(true);
  149. add_child(zoom_minus);
  150. zoom_minus->connect("pressed", callable_mp(this, &EditorZoomWidget::_button_zoom_minus));
  151. zoom_minus->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_minus", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS));
  152. zoom_minus->set_shortcut_context(this);
  153. zoom_minus->set_focus_mode(FOCUS_NONE);
  154. zoom_reset = memnew(Button);
  155. zoom_reset->set_flat(true);
  156. add_child(zoom_reset);
  157. zoom_reset->add_theme_constant_override("outline_size", 1);
  158. zoom_reset->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  159. zoom_reset->add_theme_color_override("font_color", Color(1, 1, 1));
  160. zoom_reset->connect("pressed", callable_mp(this, &EditorZoomWidget::_button_zoom_reset));
  161. zoom_reset->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_reset", TTR("Zoom Reset"), KEY_MASK_CMD | KEY_0));
  162. zoom_reset->set_shortcut_context(this);
  163. zoom_reset->set_focus_mode(FOCUS_NONE);
  164. zoom_reset->set_text_align(Button::TextAlign::ALIGN_CENTER);
  165. // Prevent the button's size from changing when the text size changes
  166. zoom_reset->set_custom_minimum_size(Size2(75 * EDSCALE, 0));
  167. zoom_plus = memnew(Button);
  168. zoom_plus->set_flat(true);
  169. add_child(zoom_plus);
  170. zoom_plus->connect("pressed", callable_mp(this, &EditorZoomWidget::_button_zoom_plus));
  171. zoom_plus->set_shortcut(ED_SHORTCUT("canvas_item_editor/zoom_plus", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL)); // Usually direct access key for PLUS
  172. zoom_plus->set_shortcut_context(this);
  173. zoom_plus->set_focus_mode(FOCUS_NONE);
  174. _update_zoom_label();
  175. add_theme_constant_override("separation", Math::round(-8 * EDSCALE));
  176. }