Debian Codegraph @

modules/navigation_2d/editor/navigation_link_2d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_link_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 "navigation_link_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 "editor/settings/editor_settings.h"
37 #include "scene/main/viewport.h"
38
39 void NavigationLink2DEditor::_notification(int p_what) {
40 switch (p_what) {
41 case NOTIFICATION_ENTER_TREE: {
42 get_tree()->connect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
43 } break;
44
45 case NOTIFICATION_EXIT_TREE: {
46 get_tree()->disconnect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
47 } break;
48 }
49 }
50
51 void NavigationLink2DEditor::_node_removed(Node *p_node) {
52 if (p_node == node) {
53 node = nullptr;
54 }
55 }
56
57 bool NavigationLink2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
58 if (!node || !node->is_visible_in_tree()) {
59 return false;
60 }
61
62 Viewport *vp = node->get_viewport();
63 if (vp && !vp->is_visible_subviewport()) {
64 return false;
65 }
66
67 real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
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 if (mb->is_pressed()) {
73 // Start position
74 if (xform.xform(node->get_start_position()).distance_to(mb->get_position()) < grab_threshold) {
75 start_grabbed = true;
76 original_start_position = node->get_start_position();
77
78 return true;
79 } else {
80 start_grabbed = false;
81 }
82
83 // End position
84 if (xform.xform(node->get_end_position()).distance_to(mb->get_position()) < grab_threshold) {
85 end_grabbed = true;
86 original_end_position = node->get_end_position();
87
88 return true;
89 } else {
90 end_grabbed = false;
91 }
92 } else {
93 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
94 if (start_grabbed) {
95 undo_redo->create_action(TTR("Set start_position"));
96 undo_redo->add_do_method(node, "set_start_position", node->get_start_position());
97 undo_redo->add_do_method(canvas_item_editor, "update_viewport");
98 undo_redo->add_undo_method(node, "set_start_position", original_start_position);
99 undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
100 undo_redo->commit_action();
101
102 start_grabbed = false;
103
104 return true;
105 }
106
107 if (end_grabbed) {
108 undo_redo->create_action(TTR("Set end_position"));
109 undo_redo->add_do_method(node, "set_end_position", node->get_end_position());
110 undo_redo->add_do_method(canvas_item_editor, "update_viewport");
111 undo_redo->add_undo_method(node, "set_end_position", original_end_position);
112 undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
113 undo_redo->commit_action();
114
115 end_grabbed = false;
116
117 return true;
118 }
119 }
120 }
121
122 Ref<InputEventMouseMotion> mm = p_event;
123 if (mm.is_valid()) {
124 Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
125 point = node->get_screen_transform().affine_inverse().xform(point);
126
127 if (start_grabbed) {
128 node->set_start_position(point);
129 canvas_item_editor->update_viewport();
130
131 return true;
132 }
133
134 if (end_grabbed) {
135 node->set_end_position(point);
136 canvas_item_editor->update_viewport();
137
138 return true;
139 }
140 }
141
142 return false;
143 }
144
145 void NavigationLink2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
146 if (!node || !node->is_visible_in_tree()) {
147 return;
148 }
149
150 Viewport *vp = node->get_viewport();
151 if (vp && !vp->is_visible_subviewport()) {
152 return;
153 }
154
155 Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
156 Vector2 global_start_position = gt.xform(node->get_start_position());
157 Vector2 global_end_position = gt.xform(node->get_end_position());
158
159 // Only drawing the handles here, since the debug rendering will fill in the rest.
160 const Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorHandle"));
161 p_overlay->draw_texture(handle, global_start_position - handle->get_size() / 2);
162 p_overlay->draw_texture(handle, global_end_position - handle->get_size() / 2);
163 }
164
165 void NavigationLink2DEditor::edit(NavigationLink2D *p_node) {
166 if (!canvas_item_editor) {
167 canvas_item_editor = CanvasItemEditor::get_singleton();
168 }
169
170 if (p_node) {
171 node = p_node;
172 } else {
173 node = nullptr;
174 }
175
176 canvas_item_editor->update_viewport();
177 }
178
179 ///////////////////////
180
181 void NavigationLink2DEditorPlugin::edit(Object *p_object) {
182 editor->edit(Object::cast_to<NavigationLink2D>(p_object));
183 }
184
185 bool NavigationLink2DEditorPlugin::handles(Object *p_object) const {
186 return Object::cast_to<NavigationLink2D>(p_object) != nullptr;
187 }
188
189 void NavigationLink2DEditorPlugin::make_visible(bool p_visible) {
190 if (!p_visible) {
191 edit(nullptr);
192 }
193 }
194
195 NavigationLink2DEditorPlugin::NavigationLink2DEditorPlugin() {
196 editor = memnew(NavigationLink2DEditor);
197 EditorNode::get_singleton()->get_gui_base()->add_child(editor);
198 }