view_panner.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**************************************************************************/
  2. /* view_panner.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 "view_panner.h"
  31. #include "core/input/input.h"
  32. #include "core/input/shortcut.h"
  33. #include "core/os/keyboard.h"
  34. bool ViewPanner::gui_input(const Ref<InputEvent> &p_event, Rect2 p_canvas_rect) {
  35. Ref<InputEventMouseButton> mb = p_event;
  36. if (mb.is_valid()) {
  37. Vector2 scroll_vec = Vector2((mb->get_button_index() == MouseButton::WHEEL_RIGHT) - (mb->get_button_index() == MouseButton::WHEEL_LEFT), (mb->get_button_index() == MouseButton::WHEEL_DOWN) - (mb->get_button_index() == MouseButton::WHEEL_UP));
  38. // Moving the scroll wheel sends two events: one with pressed as true,
  39. // and one with pressed as false. Make sure we only process one of them.
  40. if (scroll_vec != Vector2() && mb->is_pressed()) {
  41. if (control_scheme == SCROLL_PANS) {
  42. if (mb->is_ctrl_pressed()) {
  43. // Compute the zoom factor.
  44. float zoom_factor = mb->get_factor() <= 0 ? 1.0 : mb->get_factor();
  45. zoom_factor = ((scroll_zoom_factor - 1.0) * zoom_factor) + 1.0;
  46. float zoom = (scroll_vec.x + scroll_vec.y) > 0 ? 1.0 / scroll_zoom_factor : scroll_zoom_factor;
  47. zoom_callback.call(zoom, mb->get_position(), p_event);
  48. return true;
  49. } else {
  50. Vector2 panning = scroll_vec * mb->get_factor();
  51. if (pan_axis == PAN_AXIS_HORIZONTAL) {
  52. panning = Vector2(panning.x + panning.y, 0);
  53. } else if (pan_axis == PAN_AXIS_VERTICAL) {
  54. panning = Vector2(0, panning.x + panning.y);
  55. } else if (mb->is_shift_pressed()) {
  56. panning = Vector2(panning.y, panning.x);
  57. }
  58. pan_callback.call(-panning * scroll_speed, p_event);
  59. return true;
  60. }
  61. } else {
  62. if (mb->is_ctrl_pressed()) {
  63. Vector2 panning = scroll_vec * mb->get_factor();
  64. if (pan_axis == PAN_AXIS_HORIZONTAL) {
  65. panning = Vector2(panning.x + panning.y, 0);
  66. } else if (pan_axis == PAN_AXIS_VERTICAL) {
  67. panning = Vector2(0, panning.x + panning.y);
  68. } else if (mb->is_shift_pressed()) {
  69. panning = Vector2(panning.y, panning.x);
  70. }
  71. pan_callback.call(-panning * scroll_speed, p_event);
  72. return true;
  73. } else if (!mb->is_shift_pressed()) {
  74. // Compute the zoom factor.
  75. float zoom_factor = mb->get_factor() <= 0 ? 1.0 : mb->get_factor();
  76. zoom_factor = ((scroll_zoom_factor - 1.0) * zoom_factor) + 1.0;
  77. float zoom = (scroll_vec.x + scroll_vec.y) > 0 ? 1.0 / scroll_zoom_factor : scroll_zoom_factor;
  78. zoom_callback.call(zoom, mb->get_position(), p_event);
  79. return true;
  80. }
  81. }
  82. }
  83. // Alt is not used for button presses, so ignore it.
  84. if (mb->is_alt_pressed()) {
  85. return false;
  86. }
  87. bool is_drag_event = mb->get_button_index() == MouseButton::MIDDLE ||
  88. (enable_rmb && mb->get_button_index() == MouseButton::RIGHT) ||
  89. (!simple_panning_enabled && mb->get_button_index() == MouseButton::LEFT && is_panning()) ||
  90. (force_drag && mb->get_button_index() == MouseButton::LEFT);
  91. if (is_drag_event) {
  92. if (mb->is_pressed()) {
  93. is_dragging = true;
  94. } else {
  95. is_dragging = false;
  96. }
  97. return mb->get_button_index() != MouseButton::LEFT || mb->is_pressed(); // Don't consume LMB release events (it fixes some selection problems).
  98. }
  99. }
  100. Ref<InputEventMouseMotion> mm = p_event;
  101. if (mm.is_valid()) {
  102. if (is_dragging) {
  103. if (p_canvas_rect != Rect2()) {
  104. pan_callback.call(Input::get_singleton()->warp_mouse_motion(mm, p_canvas_rect), p_event);
  105. } else {
  106. pan_callback.call(mm->get_relative(), p_event);
  107. }
  108. return true;
  109. }
  110. }
  111. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  112. if (magnify_gesture.is_valid()) {
  113. // Zoom gesture
  114. zoom_callback.call(magnify_gesture->get_factor(), magnify_gesture->get_position(), p_event);
  115. return true;
  116. }
  117. Ref<InputEventPanGesture> pan_gesture = p_event;
  118. if (pan_gesture.is_valid()) {
  119. if (pan_gesture->is_ctrl_pressed()) {
  120. // Zoom gesture.
  121. float pan_zoom_factor = 1.02f;
  122. float zoom_direction = pan_gesture->get_delta().x - pan_gesture->get_delta().y;
  123. if (zoom_direction == 0.f) {
  124. return true;
  125. }
  126. float zoom = zoom_direction < 0 ? 1.0 / pan_zoom_factor : pan_zoom_factor;
  127. zoom_callback.call(zoom, pan_gesture->get_position(), p_event);
  128. return true;
  129. }
  130. pan_callback.call(-pan_gesture->get_delta() * scroll_speed, p_event);
  131. }
  132. Ref<InputEventScreenDrag> screen_drag = p_event;
  133. if (screen_drag.is_valid()) {
  134. if (Input::get_singleton()->is_emulating_mouse_from_touch() || Input::get_singleton()->is_emulating_touch_from_mouse()) {
  135. // This set of events also generates/is generated by
  136. // InputEventMouseButton/InputEventMouseMotion events which will be processed instead.
  137. } else {
  138. pan_callback.call(screen_drag->get_relative(), p_event);
  139. }
  140. }
  141. Ref<InputEventKey> k = p_event;
  142. if (k.is_valid()) {
  143. if (pan_view_shortcut.is_valid() && pan_view_shortcut->matches_event(k)) {
  144. pan_key_pressed = k->is_pressed();
  145. if (simple_panning_enabled || Input::get_singleton()->get_mouse_button_mask().has_flag(MouseButtonMask::LEFT)) {
  146. is_dragging = pan_key_pressed;
  147. }
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. void ViewPanner::release_pan_key() {
  154. pan_key_pressed = false;
  155. is_dragging = false;
  156. }
  157. void ViewPanner::set_callbacks(Callable p_pan_callback, Callable p_zoom_callback) {
  158. pan_callback = p_pan_callback;
  159. zoom_callback = p_zoom_callback;
  160. }
  161. void ViewPanner::set_control_scheme(ControlScheme p_scheme) {
  162. control_scheme = p_scheme;
  163. }
  164. void ViewPanner::set_enable_rmb(bool p_enable) {
  165. enable_rmb = p_enable;
  166. }
  167. void ViewPanner::set_pan_shortcut(Ref<Shortcut> p_shortcut) {
  168. pan_view_shortcut = p_shortcut;
  169. pan_key_pressed = false;
  170. }
  171. void ViewPanner::set_simple_panning_enabled(bool p_enabled) {
  172. simple_panning_enabled = p_enabled;
  173. }
  174. void ViewPanner::set_scroll_speed(int p_scroll_speed) {
  175. ERR_FAIL_COND(p_scroll_speed <= 0);
  176. scroll_speed = p_scroll_speed;
  177. }
  178. void ViewPanner::set_scroll_zoom_factor(float p_scroll_zoom_factor) {
  179. ERR_FAIL_COND(p_scroll_zoom_factor <= 1.0);
  180. scroll_zoom_factor = p_scroll_zoom_factor;
  181. }
  182. void ViewPanner::set_pan_axis(PanAxis p_pan_axis) {
  183. pan_axis = p_pan_axis;
  184. }
  185. void ViewPanner::setup(ControlScheme p_scheme, Ref<Shortcut> p_shortcut, bool p_simple_panning) {
  186. set_control_scheme(p_scheme);
  187. set_pan_shortcut(p_shortcut);
  188. set_simple_panning_enabled(p_simple_panning);
  189. }
  190. bool ViewPanner::is_panning() const {
  191. return is_dragging || pan_key_pressed;
  192. }
  193. void ViewPanner::set_force_drag(bool p_force) {
  194. force_drag = p_force;
  195. }
  196. ViewPanner::ViewPanner() {
  197. Array inputs;
  198. inputs.append(InputEventKey::create_reference(Key::SPACE));
  199. pan_view_shortcut.instantiate();
  200. pan_view_shortcut->set_events(inputs);
  201. }