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