1 /*************************************************************************/
2 /*  physical_bone_plugin.cpp                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 "physical_bone_plugin.h"
32 #include "editor/plugins/spatial_editor_plugin.h"
33 #include "scene/3d/physics_body.h"
34 
_bind_methods()35 void PhysicalBoneEditor::_bind_methods() {
36 	ClassDB::bind_method(D_METHOD("_on_toggle_button_transform_joint", "is_pressed"), &PhysicalBoneEditor::_on_toggle_button_transform_joint);
37 }
38 
_on_toggle_button_transform_joint(bool p_is_pressed)39 void PhysicalBoneEditor::_on_toggle_button_transform_joint(bool p_is_pressed) {
40 
41 	_set_move_joint();
42 }
43 
_set_move_joint()44 void PhysicalBoneEditor::_set_move_joint() {
45 	if (selected) {
46 		selected->_set_gizmo_move_joint(button_transform_joint->is_pressed());
47 	}
48 }
49 
PhysicalBoneEditor(EditorNode * p_editor)50 PhysicalBoneEditor::PhysicalBoneEditor(EditorNode *p_editor) :
51 		editor(p_editor),
52 		selected(NULL) {
53 
54 	spatial_editor_hb = memnew(HBoxContainer);
55 	spatial_editor_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
56 	spatial_editor_hb->set_alignment(BoxContainer::ALIGN_BEGIN);
57 	SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
58 
59 	spatial_editor_hb->add_child(memnew(VSeparator));
60 
61 	button_transform_joint = memnew(ToolButton);
62 	spatial_editor_hb->add_child(button_transform_joint);
63 
64 	button_transform_joint->set_text(TTR("Move Joint"));
65 	button_transform_joint->set_icon(SpatialEditor::get_singleton()->get_icon("PhysicalBone", "EditorIcons"));
66 	button_transform_joint->set_toggle_mode(true);
67 	button_transform_joint->connect("toggled", this, "_on_toggle_button_transform_joint");
68 
69 	hide();
70 }
71 
~PhysicalBoneEditor()72 PhysicalBoneEditor::~PhysicalBoneEditor() {}
73 
set_selected(PhysicalBone * p_pb)74 void PhysicalBoneEditor::set_selected(PhysicalBone *p_pb) {
75 
76 	button_transform_joint->set_pressed(false);
77 
78 	_set_move_joint();
79 	selected = p_pb;
80 	_set_move_joint();
81 }
82 
hide()83 void PhysicalBoneEditor::hide() {
84 	spatial_editor_hb->hide();
85 }
86 
show()87 void PhysicalBoneEditor::show() {
88 	spatial_editor_hb->show();
89 }
90 
PhysicalBonePlugin(EditorNode * p_editor)91 PhysicalBonePlugin::PhysicalBonePlugin(EditorNode *p_editor) :
92 		editor(p_editor),
93 		selected(NULL),
94 		physical_bone_editor(editor) {}
95 
make_visible(bool p_visible)96 void PhysicalBonePlugin::make_visible(bool p_visible) {
97 	if (p_visible) {
98 
99 		physical_bone_editor.show();
100 	} else {
101 
102 		physical_bone_editor.hide();
103 		physical_bone_editor.set_selected(NULL);
104 		selected = NULL;
105 	}
106 }
107 
edit(Object * p_node)108 void PhysicalBonePlugin::edit(Object *p_node) {
109 	selected = static_cast<PhysicalBone *>(p_node); // Trust it
110 	ERR_FAIL_COND(!selected);
111 
112 	physical_bone_editor.set_selected(selected);
113 }
114