| 1 | /**************************************************************************/ |
| 2 | /* camera_2d_editor_plugin.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 "camera_2d_editor_plugin.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "editor/editor_undo_redo_manager.h" |
| 36 | #include "editor/scene/canvas_item_editor_plugin.h" |
| 37 | #include "editor/themes/editor_scale.h" |
| 38 | #include "scene/2d/camera_2d.h" |
| 39 | #include "scene/gui/label.h" |
| 40 | #include "scene/gui/menu_button.h" |
| 41 | |
| 42 | void Camera2DEditor::edit(Camera2D *p_camera) { |
| 43 | if (p_camera == selected_camera) { |
| 44 | return; |
| 45 | } |
| 46 | const Callable update_overlays = callable_mp(plugin, &EditorPlugin::update_overlays); |
| 47 | |
| 48 | if (selected_camera) { |
| 49 | selected_camera->disconnect(SceneStringName(draw), update_overlays); |
| 50 | if (drag_type != Drag::NONE) { |
| 51 | selected_camera->set_limit_rect(drag_revert); |
| 52 | } |
| 53 | drag_type = Drag::NONE; |
| 54 | hover_type = Drag::NONE; |
| 55 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_ARROW); |
| 56 | } |
| 57 | selected_camera = p_camera; |
| 58 | |
| 59 | if (selected_camera) { |
| 60 | selected_camera->connect(SceneStringName(draw), update_overlays); |
| 61 | } |
| 62 | plugin->update_overlays(); |
| 63 | } |
| 64 | |
| 65 | bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) { |
| 66 | if (!selected_camera || !selected_camera->is_limit_enabled()) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | Ref<InputEventMouseButton> mb = p_event; |
| 71 | if (mb.is_valid()) { |
| 72 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 73 | if (mb->is_pressed()) { |
| 74 | if (hover_type != Drag::NONE) { |
| 75 | Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position()); |
| 76 | const Rect2 limit_rect = selected_camera->get_limit_rect(); |
| 77 | |
| 78 | drag_type = hover_type; |
| 79 | drag_revert = selected_camera->get_limit_rect(); |
| 80 | center_drag_point = pos - limit_rect.position; |
| 81 | return true; |
| 82 | } |
| 83 | } else if (drag_type != Drag::NONE) { |
| 84 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 85 | ur->create_action(TTR("Edit Camera2D Limits")); |
| 86 | ur->add_do_method(selected_camera, "_set_limit_rect", selected_camera->get_limit_rect()); |
| 87 | ur->add_do_method(this, "_update_overlays_if_needed", selected_camera); |
| 88 | ur->add_undo_method(selected_camera, "_set_limit_rect", drag_revert); |
| 89 | ur->add_undo_method(this, "_update_overlays_if_needed", selected_camera); |
| 90 | ur->commit_action(false); |
| 91 | |
| 92 | drag_type = Drag::NONE; |
| 93 | return true; |
| 94 | } |
| 95 | } else if (drag_type != Drag::NONE && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 96 | selected_camera->set_limit_rect(drag_revert); |
| 97 | drag_type = Drag::NONE; |
| 98 | plugin->update_overlays(); |
| 99 | _update_hover(mb->get_position()); |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | Ref<InputEventMouseMotion> mm = p_event; |
| 106 | if (mm.is_valid()) { |
| 107 | Vector2 pos = mm->get_position(); |
| 108 | if (drag_type == Drag::NONE) { |
| 109 | _update_hover(pos); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos); |
| 114 | pos = CanvasItemEditor::get_singleton()->snap_point(pos); |
| 115 | |
| 116 | switch (drag_type) { |
| 117 | case Drag::LEFT: { |
| 118 | selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x)); |
| 119 | plugin->update_overlays(); |
| 120 | } break; |
| 121 | |
| 122 | case Drag::RIGHT: { |
| 123 | selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x)); |
| 124 | plugin->update_overlays(); |
| 125 | } break; |
| 126 | |
| 127 | case Drag::TOP: { |
| 128 | selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y)); |
| 129 | plugin->update_overlays(); |
| 130 | } break; |
| 131 | |
| 132 | case Drag::BOTTOM: { |
| 133 | selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y)); |
| 134 | plugin->update_overlays(); |
| 135 | } break; |
| 136 | |
| 137 | case Drag::TOP_LEFT: { |
| 138 | selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x)); |
| 139 | selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y)); |
| 140 | plugin->update_overlays(); |
| 141 | } break; |
| 142 | |
| 143 | case Drag::TOP_RIGHT: { |
| 144 | selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x)); |
| 145 | selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y)); |
| 146 | plugin->update_overlays(); |
| 147 | } break; |
| 148 | |
| 149 | case Drag::BOTTOM_LEFT: { |
| 150 | selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x)); |
| 151 | selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y)); |
| 152 | plugin->update_overlays(); |
| 153 | } break; |
| 154 | |
| 155 | case Drag::BOTTOM_RIGHT: { |
| 156 | selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x)); |
| 157 | selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y)); |
| 158 | plugin->update_overlays(); |
| 159 | } break; |
| 160 | |
| 161 | case Drag::CENTER: { |
| 162 | Rect2 target_rect = selected_camera->get_limit_rect(); |
| 163 | target_rect.position = pos - center_drag_point; |
| 164 | selected_camera->set_limit_rect(target_rect); |
| 165 | plugin->update_overlays(); |
| 166 | } break; |
| 167 | |
| 168 | case Drag::NONE: { |
| 169 | } break; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | void Camera2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) { |
| 178 | if (!selected_camera || !selected_camera->is_limit_enabled()) { |
| 179 | return; |
| 180 | } |
| 181 | Rect2 limit_rect = selected_camera->get_limit_rect(); |
| 182 | limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(limit_rect); |
| 183 | p_overlay->draw_rect(limit_rect, Color(1, 1, 0.25, 0.63), false, 3); |
| 184 | } |
| 185 | |
| 186 | void Camera2DEditor::_menu_option(int p_option) { |
| 187 | switch (p_option) { |
| 188 | case MENU_SNAP_LIMITS_TO_VIEWPORT: { |
| 189 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 190 | ur->create_action(TTR("Snap Camera2D Limits to the Viewport"), UndoRedo::MERGE_DISABLE, selected_camera); |
| 191 | ur->add_do_method(this, "_snap_limits_to_viewport", selected_camera); |
| 192 | ur->add_undo_method(selected_camera, "_set_limit_rect", selected_camera->get_limit_rect()); |
| 193 | ur->add_undo_method(this, "_update_overlays_if_needed", selected_camera); |
| 194 | ur->commit_action(); |
| 195 | } break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void Camera2DEditor::_snap_limits_to_viewport(Camera2D *p_camera) { |
| 200 | p_camera->set_limit(SIDE_LEFT, 0); |
| 201 | p_camera->set_limit(SIDE_TOP, 0); |
| 202 | p_camera->set_limit(SIDE_RIGHT, GLOBAL_GET("display/window/size/viewport_width")); |
| 203 | p_camera->set_limit(SIDE_BOTTOM, GLOBAL_GET("display/window/size/viewport_height")); |
| 204 | _update_overlays_if_needed(p_camera); |
| 205 | } |
| 206 | |
| 207 | void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) { |
| 208 | if (p_camera == selected_camera) { |
| 209 | plugin->update_overlays(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) { |
| 214 | if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) { |
| 215 | hover_type = Drag::NONE; |
| 216 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | const Rect2 limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(selected_camera->get_limit_rect()); |
| 221 | const float drag_tolerance = 8.0; |
| 222 | const Vector2 tolerance_vector = Vector2(1, 1) * drag_tolerance; |
| 223 | |
| 224 | hover_type = Drag::NONE; |
| 225 | if (Rect2(limit_rect.position - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) { |
| 226 | hover_type = Drag::TOP_LEFT; |
| 227 | } else if (Rect2(Vector2(limit_rect.get_end().x, limit_rect.position.y) - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) { |
| 228 | hover_type = Drag::TOP_RIGHT; |
| 229 | } else if (Rect2(Vector2(limit_rect.position.x, limit_rect.get_end().y) - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) { |
| 230 | hover_type = Drag::BOTTOM_LEFT; |
| 231 | } else if (Rect2(limit_rect.get_end() - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) { |
| 232 | hover_type = Drag::BOTTOM_RIGHT; |
| 233 | } else if (p_mouse_pos.y > limit_rect.position.y && p_mouse_pos.y < limit_rect.get_end().y) { |
| 234 | if (Math::abs(p_mouse_pos.x - limit_rect.position.x) < drag_tolerance) { |
| 235 | hover_type = Drag::LEFT; |
| 236 | } else if (Math::abs(p_mouse_pos.x - limit_rect.get_end().x) < drag_tolerance) { |
| 237 | hover_type = Drag::RIGHT; |
| 238 | } |
| 239 | } else if (p_mouse_pos.x > limit_rect.position.x && p_mouse_pos.x < limit_rect.get_end().x) { |
| 240 | if (Math::abs(p_mouse_pos.y - limit_rect.position.y) < drag_tolerance) { |
| 241 | hover_type = Drag::TOP; |
| 242 | } else if (Math::abs(p_mouse_pos.y - limit_rect.get_end().y) < drag_tolerance) { |
| 243 | hover_type = Drag::BOTTOM; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (hover_type == Drag::NONE && limit_rect.has_point(p_mouse_pos)) { |
| 248 | const Rect2 editor_rect = Rect2(Vector2(), CanvasItemEditor::get_singleton()->get_viewport_control()->get_size()); |
| 249 | const Rect2 transformed_rect = selected_camera->get_viewport()->get_canvas_transform().xform_inv(limit_rect); |
| 250 | |
| 251 | // Only allow center drag if any limit edge is visible on screen. |
| 252 | bool edge_visible = false; |
| 253 | edge_visible = edge_visible || (transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y && transformed_rect.position.x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x); |
| 254 | edge_visible = edge_visible || (transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y && transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.get_end().x < editor_rect.get_end().x); |
| 255 | edge_visible = edge_visible || (transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x && transformed_rect.position.y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y); |
| 256 | edge_visible = edge_visible || (transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x && transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.get_end().y < editor_rect.get_end().y); |
| 257 | |
| 258 | if (edge_visible) { |
| 259 | hover_type = Drag::CENTER; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | switch (hover_type) { |
| 264 | case Drag::NONE: { |
| 265 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(); |
| 266 | } break; |
| 267 | case Drag::LEFT: |
| 268 | case Drag::RIGHT: { |
| 269 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_HSIZE); |
| 270 | } break; |
| 271 | case Drag::TOP: |
| 272 | case Drag::BOTTOM: { |
| 273 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_VSIZE); |
| 274 | } break; |
| 275 | case Drag::TOP_LEFT: |
| 276 | case Drag::BOTTOM_RIGHT: { |
| 277 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_FDIAGSIZE); |
| 278 | } break; |
| 279 | case Drag::TOP_RIGHT: |
| 280 | case Drag::BOTTOM_LEFT: { |
| 281 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_BDIAGSIZE); |
| 282 | } break; |
| 283 | case Drag::CENTER: { |
| 284 | CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_MOVE); |
| 285 | } break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void Camera2DEditor::_notification(int p_what) { |
| 290 | switch (p_what) { |
| 291 | case NOTIFICATION_THEME_CHANGED: { |
| 292 | options->set_button_icon(get_editor_theme_icon(SNAME("Camera2D"))); |
| 293 | } break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void Camera2DEditor::_bind_methods() { |
| 298 | ClassDB::bind_method(D_METHOD("_snap_limits_to_viewport", "camera"), &Camera2DEditor::_snap_limits_to_viewport); |
| 299 | ClassDB::bind_method(D_METHOD("_update_overlays_if_needed", "camera"), &Camera2DEditor::_update_overlays_if_needed); |
| 300 | } |
| 301 | |
| 302 | Camera2DEditor::Camera2DEditor(EditorPlugin *p_plugin) { |
| 303 | plugin = p_plugin; |
| 304 | |
| 305 | options = memnew(MenuButton); |
| 306 | options->set_text(TTRC("Camera2D")); |
| 307 | options->set_flat(false); |
| 308 | options->set_theme_type_variation("FlatMenuButtonNoIconTint"); |
| 309 | options->get_popup()->add_item(TTRC("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT); |
| 310 | options->set_switch_on_hover(true); |
| 311 | options->hide(); |
| 312 | CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options); |
| 313 | options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Camera2DEditor::_menu_option)); |
| 314 | } |
| 315 | |
| 316 | void Camera2DEditorPlugin::edit(Object *p_object) { |
| 317 | camera_2d_editor->edit(Object::cast_to<Camera2D>(p_object)); |
| 318 | } |
| 319 | |
| 320 | bool Camera2DEditorPlugin::handles(Object *p_object) const { |
| 321 | return p_object->is_class("Camera2D"); |
| 322 | } |
| 323 | |
| 324 | void Camera2DEditorPlugin::make_visible(bool p_visible) { |
| 325 | if (p_visible) { |
| 326 | camera_2d_editor->options->show(); |
| 327 | } else { |
| 328 | camera_2d_editor->options->hide(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | Camera2DEditorPlugin::Camera2DEditorPlugin() { |
| 333 | camera_2d_editor = memnew(Camera2DEditor(this)); |
| 334 | EditorNode::get_singleton()->get_gui_base()->add_child(camera_2d_editor); |
| 335 | } |