Debian Codegraph @

editor/scene/2d/physics/cast_2d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* cast_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 "cast_2d_editor_plugin.h"
32
33 #include "editor/editor_node.h"
34 #include "editor/editor_undo_redo_manager.h"
35 #include "editor/scene/canvas_item_editor_plugin.h"
36 #include "scene/2d/physics/ray_cast_2d.h"
37 #include "scene/2d/physics/shape_cast_2d.h"
38 #include "scene/main/viewport.h"
39
40 void Cast2DEditor::_notification(int p_what) {
41 switch (p_what) {
42 case NOTIFICATION_ENTER_TREE: {
43 get_tree()->connect("node_removed", callable_mp(this, &Cast2DEditor::_node_removed));
44 } break;
45
46 case NOTIFICATION_EXIT_TREE: {
47 get_tree()->disconnect("node_removed", callable_mp(this, &Cast2DEditor::_node_removed));
48 } break;
49 }
50 }
51
52 void Cast2DEditor::_node_removed(Node *p_node) {
53 if (p_node == node) {
54 node = nullptr;
55 }
56 }
57
58 bool Cast2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
59 if (!node || !node->is_visible_in_tree()) {
60 return false;
61 }
62
63 Viewport *vp = node->get_viewport();
64 if (vp && !vp->is_visible_subviewport()) {
65 return false;
66 }
67
68 Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
69
70 Ref<InputEventMouseButton> mb = p_event;
71 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
72 Vector2 target_position = node->get("target_position");
73
74 Vector2 gpoint = mb->get_position();
75
76 if (mb->is_pressed()) {
77 if (xform.xform(target_position).distance_to(gpoint) < 8) {
78 pressed = true;
79 original_target_position = target_position;
80 original_mouse_pos = gpoint;
81
82 return true;
83 } else {
84 pressed = false;
85
86 return false;
87 }
88 } else if (pressed) {
89 if (original_mouse_pos != gpoint) {
90 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
91 undo_redo->create_action(TTR("Set Target Position"));
92 undo_redo->add_do_property(node, "target_position", target_position);
93 undo_redo->add_do_method(canvas_item_editor, "update_viewport");
94 undo_redo->add_undo_property(node, "target_position", original_target_position);
95 undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
96 undo_redo->commit_action();
97 }
98
99 pressed = false;
100 return true;
101 }
102 }
103
104 Ref<InputEventMouseMotion> mm = p_event;
105 if (mm.is_valid() && pressed) {
106 Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
107 point = node->get_screen_transform().affine_inverse().xform(point);
108
109 node->set("target_position", point);
110 canvas_item_editor->update_viewport();
111
112 return true;
113 }
114
115 return false;
116 }
117
118 void Cast2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
119 if (!node || !node->is_visible_in_tree()) {
120 return;
121 }
122
123 Viewport *vp = node->get_viewport();
124 if (vp && !vp->is_visible_subviewport()) {
125 return;
126 }
127
128 Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
129
130 const Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorHandle"));
131 p_overlay->draw_texture(handle, gt.xform((Vector2)node->get("target_position")) - handle->get_size() / 2);
132 }
133
134 void Cast2DEditor::edit(Node2D *p_node) {
135 if (!canvas_item_editor) {
136 canvas_item_editor = CanvasItemEditor::get_singleton();
137 }
138
139 if (Object::cast_to<RayCast2D>(p_node) || Object::cast_to<ShapeCast2D>(p_node)) {
140 node = p_node;
141 } else {
142 node = nullptr;
143 }
144
145 canvas_item_editor->update_viewport();
146 }
147
148 ///////////////////////
149
150 void Cast2DEditorPlugin::edit(Object *p_object) {
151 cast_2d_editor->edit(Object::cast_to<Node2D>(p_object));
152 }
153
154 bool Cast2DEditorPlugin::handles(Object *p_object) const {
155 return Object::cast_to<RayCast2D>(p_object) != nullptr || Object::cast_to<ShapeCast2D>(p_object) != nullptr;
156 }
157
158 void Cast2DEditorPlugin::make_visible(bool p_visible) {
159 if (!p_visible) {
160 edit(nullptr);
161 }
162 }
163
164 Cast2DEditorPlugin::Cast2DEditorPlugin() {
165 cast_2d_editor = memnew(Cast2DEditor);
166 EditorNode::get_singleton()->get_gui_base()->add_child(cast_2d_editor);
167 }