| 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 | |
| 31 | #include "view_panner.h" |
| 32 | |
| 33 | #include "core/input/input.h" |
| 34 | #include "core/input/shortcut.h" |
| 35 | #include "core/os/keyboard.h" |
| 36 | |
| 37 | bool ViewPanner::gui_input(const Ref<InputEvent> &p_event, Rect2 p_canvas_rect) { |
| 38 | Ref<InputEventMouseButton> mb = p_event; |
| 39 | if (mb.is_valid()) { |
| 40 | 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)); |
| 41 | // Moving the scroll wheel sends two events: one with pressed as true, |
| 42 | // and one with pressed as false. Make sure we only process one of them. |
| 43 | if (scroll_vec != Vector2() && mb->is_pressed()) { |
| 44 | if (control_scheme == SCROLL_PANS) { |
| 45 | if (mb->is_ctrl_pressed()) { |
| 46 | // Compute the zoom factor. |
| 47 | float zoom_factor = mb->get_factor() <= 0 ? 1.0 : mb->get_factor(); |
| 48 | zoom_factor = ((scroll_zoom_factor - 1.0) * zoom_factor) + 1.0; |
| 49 | float zoom = (scroll_vec.x + scroll_vec.y) > 0 ? 1.0 / scroll_zoom_factor : scroll_zoom_factor; |
| 50 | callback_helper(zoom_callback, varray(zoom, mb->get_position(), p_event)); |
| 51 | return true; |
| 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 | callback_helper(pan_callback, varray(-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 | callback_helper(pan_callback, varray(-panning * scroll_speed, p_event)); |
| 75 | return true; |
| 76 | } else if (!mb->is_shift_pressed()) { |
| 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.x + scroll_vec.y) > 0 ? 1.0 / scroll_zoom_factor : scroll_zoom_factor; |
| 81 | callback_helper(zoom_callback, varray(zoom, mb->get_position(), p_event)); |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Alt is not used for button presses, so ignore it. |
| 88 | if (mb->is_alt_pressed()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | bool is_drag_event = mb->get_button_index() == MouseButton::MIDDLE || |
| 93 | (enable_rmb && mb->get_button_index() == MouseButton::RIGHT) || |
| 94 | (!simple_panning_enabled && mb->get_button_index() == MouseButton::LEFT && is_panning()) || |
| 95 | (force_drag && mb->get_button_index() == MouseButton::LEFT); |
| 96 | |
| 97 | if (is_drag_event) { |
| 98 | if (mb->is_pressed()) { |
| 99 | is_dragging = true; |
| 100 | } else { |
| 101 | is_dragging = false; |
| 102 | } |
| 103 | return mb->get_button_index() != MouseButton::LEFT || mb->is_pressed(); // Don't consume LMB release events (it fixes some selection problems). |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | Ref<InputEventMouseMotion> mm = p_event; |
| 108 | if (mm.is_valid()) { |
| 109 | if (is_dragging) { |
| 110 | if (p_canvas_rect != Rect2()) { |
| 111 | callback_helper(pan_callback, varray(Input::get_singleton()->warp_mouse_motion(mm, p_canvas_rect), p_event)); |
| 112 | } else { |
| 113 | callback_helper(pan_callback, varray(mm->get_relative(), p_event)); |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | Ref<InputEventMagnifyGesture> magnify_gesture = p_event; |
| 120 | if (magnify_gesture.is_valid()) { |
| 121 | // Zoom gesture |
| 122 | callback_helper(zoom_callback, varray(magnify_gesture->get_factor(), magnify_gesture->get_position(), p_event)); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | Ref<InputEventPanGesture> pan_gesture = p_event; |
| 127 | if (pan_gesture.is_valid()) { |
| 128 | callback_helper(pan_callback, varray(-pan_gesture->get_delta() * scroll_speed, p_event)); |
| 129 | } |
| 130 | |
| 131 | Ref<InputEventScreenDrag> screen_drag = p_event; |
| 132 | if (screen_drag.is_valid()) { |
| 133 | if (Input::get_singleton()->is_emulating_mouse_from_touch() || Input::get_singleton()->is_emulating_touch_from_mouse()) { |
| 134 | // This set of events also generates/is generated by |
| 135 | // InputEventMouseButton/InputEventMouseMotion events which will be processed instead. |
| 136 | } else { |
| 137 | callback_helper(pan_callback, varray(screen_drag->get_relative(), p_event)); |
| 138 | } |
| 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 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | void ViewPanner::release_pan_key() { |
| 156 | pan_key_pressed = false; |
| 157 | is_dragging = false; |
| 158 | } |
| 159 | |
| 160 | void ViewPanner::callback_helper(Callable p_callback, Vector<Variant> p_args) { |
| 161 | const Variant **argptr = (const Variant **)alloca(sizeof(Variant *) * p_args.size()); |
| 162 | for (int i = 0; i < p_args.size(); i++) { |
| 163 | argptr[i] = &p_args[i]; |
| 164 | } |
| 165 | |
| 166 | Variant result; |
| 167 | Callable::CallError ce; |
| 168 | p_callback.callp(argptr, p_args.size(), result, ce); |
| 169 | } |
| 170 | |
| 171 | void ViewPanner::set_callbacks(Callable p_pan_callback, Callable p_zoom_callback) { |
| 172 | pan_callback = p_pan_callback; |
| 173 | zoom_callback = p_zoom_callback; |
| 174 | } |
| 175 | |
| 176 | void ViewPanner::set_control_scheme(ControlScheme p_scheme) { |
| 177 | control_scheme = p_scheme; |
| 178 | } |
| 179 | |
| 180 | void ViewPanner::set_enable_rmb(bool p_enable) { |
| 181 | enable_rmb = p_enable; |
| 182 | } |
| 183 | |
| 184 | void ViewPanner::set_pan_shortcut(Ref<Shortcut> p_shortcut) { |
| 185 | pan_view_shortcut = p_shortcut; |
| 186 | pan_key_pressed = false; |
| 187 | } |
| 188 | |
| 189 | void ViewPanner::set_simple_panning_enabled(bool p_enabled) { |
| 190 | simple_panning_enabled = p_enabled; |
| 191 | } |
| 192 | |
| 193 | void ViewPanner::set_scroll_speed(int p_scroll_speed) { |
| 194 | ERR_FAIL_COND(p_scroll_speed <= 0); |
| 195 | scroll_speed = p_scroll_speed; |
| 196 | } |
| 197 | |
| 198 | void ViewPanner::set_scroll_zoom_factor(float p_scroll_zoom_factor) { |
| 199 | ERR_FAIL_COND(p_scroll_zoom_factor <= 1.0); |
| 200 | scroll_zoom_factor = p_scroll_zoom_factor; |
| 201 | } |
| 202 | |
| 203 | void ViewPanner::set_pan_axis(PanAxis p_pan_axis) { |
| 204 | pan_axis = p_pan_axis; |
| 205 | } |
| 206 | |
| 207 | void ViewPanner::setup(ControlScheme p_scheme, Ref<Shortcut> p_shortcut, bool p_simple_panning) { |
| 208 | set_control_scheme(p_scheme); |
| 209 | set_pan_shortcut(p_shortcut); |
| 210 | set_simple_panning_enabled(p_simple_panning); |
| 211 | } |
| 212 | |
| 213 | bool ViewPanner::is_panning() const { |
| 214 | return is_dragging || pan_key_pressed; |
| 215 | } |
| 216 | |
| 217 | void ViewPanner::set_force_drag(bool p_force) { |
| 218 | force_drag = p_force; |
| 219 | } |
| 220 | |
| 221 | ViewPanner::ViewPanner() { |
| 222 | Array inputs; |
| 223 | inputs.append(InputEventKey::create_reference(Key::SPACE)); |
| 224 | |
| 225 | pan_view_shortcut.instantiate(); |
| 226 | pan_view_shortcut->set_events(inputs); |
| 227 | } |
| 228 | |