view_panner.cpp 8.6 KB

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