view_panner.cpp 8.8 KB

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