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