diff options
Diffstat (limited to 'editor/plugins')
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.cpp | 106 | ||||
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.h | 2 | ||||
-rw-r--r-- | editor/plugins/physical_bone_plugin.cpp | 123 | ||||
-rw-r--r-- | editor/plugins/physical_bone_plugin.h | 78 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 6 | ||||
-rw-r--r-- | editor/plugins/skeleton_editor_plugin.cpp | 189 | ||||
-rw-r--r-- | editor/plugins/skeleton_editor_plugin.h | 95 | ||||
-rw-r--r-- | editor/plugins/spatial_editor_plugin.cpp | 52 |
8 files changed, 569 insertions, 82 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 8cad40c9ce..93aeca6632 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -467,32 +467,28 @@ void CanvasItemEditor::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_no const real_t grab_distance = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8); CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node); - bool locked = p_node->has_meta("_edit_lock_") && p_node->get_meta("_edit_lock_"); - - if (!locked) { - for (int i = p_node->get_child_count() - 1; i >= 0; i--) { - if (canvas_item && !canvas_item->is_set_as_toplevel()) { - _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_limit, p_parent_xform * canvas_item->get_transform(), p_canvas_xform); - } else { - CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node); - _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_limit, Transform2D(), cl ? cl->get_transform() : p_canvas_xform); - } - if (p_limit != 0 && r_items.size() >= p_limit) - return; + for (int i = p_node->get_child_count() - 1; i >= 0; i--) { + if (canvas_item && !canvas_item->is_set_as_toplevel()) { + _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_limit, p_parent_xform * canvas_item->get_transform(), p_canvas_xform); + } else { + CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node); + _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_limit, Transform2D(), cl ? cl->get_transform() : p_canvas_xform); } + if (p_limit != 0 && r_items.size() >= p_limit) + return; + } - if (canvas_item && canvas_item->is_visible_in_tree()) { - Transform2D xform = (p_parent_xform * p_canvas_xform * canvas_item->get_transform()).affine_inverse(); - const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length(); - if (canvas_item->_edit_is_selected_on_click(xform.xform(p_pos), local_grab_distance)) { - Node2D *node = Object::cast_to<Node2D>(canvas_item); + if (canvas_item && canvas_item->is_visible_in_tree()) { + Transform2D xform = (p_parent_xform * p_canvas_xform * canvas_item->get_transform()).affine_inverse(); + const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length(); + if (canvas_item->_edit_is_selected_on_click(xform.xform(p_pos), local_grab_distance)) { + Node2D *node = Object::cast_to<Node2D>(canvas_item); - _SelectResult res; - res.item = canvas_item; - res.z_index = node ? node->get_z_index() : 0; - res.has_z = node; - r_items.push_back(res); - } + _SelectResult res; + res.item = canvas_item; + res.z_index = node ? node->get_z_index() : 0; + res.has_z = node; + r_items.push_back(res); } } @@ -509,14 +505,14 @@ void CanvasItemEditor::_get_canvas_items_at_pos(const Point2 &p_pos, Vector<_Sel for (int i = 0; i < r_items.size(); i++) { Node *node = r_items[i].item; - // Make sure the selected node is in the current scene - while (node && node != scene && node->get_owner() != scene) { + // Make sure the selected node is in the current scene, or editable + while (node && node != get_tree()->get_edited_scene_root() && node->get_owner() != scene && !scene->is_editable_instance(node->get_owner())) { node = node->get_parent(); }; // Replace the node by the group if grouped CanvasItem *canvas_item = Object::cast_to<CanvasItem>(node); - while (node && node != scene) { + while (node && node != scene->get_parent()) { CanvasItem *canvas_item_tmp = Object::cast_to<CanvasItem>(node); if (canvas_item_tmp && node->has_meta("_edit_group_")) { canvas_item = canvas_item_tmp; @@ -525,7 +521,7 @@ void CanvasItemEditor::_get_canvas_items_at_pos(const Point2 &p_pos, Vector<_Sel } //Remove the item if invalid - if (!canvas_item || (canvas_item != scene && canvas_item->get_owner() != scene && !scene->is_editable_instance(canvas_item->get_owner()))) { + if (!canvas_item || (canvas_item != scene && canvas_item->get_owner() != scene && !scene->is_editable_instance(canvas_item->get_owner())) || (canvas_item->has_meta("_edit_lock_") && canvas_item->get_meta("_edit_lock_"))) { r_items.remove(i); i--; } else { @@ -541,13 +537,13 @@ void CanvasItemEditor::_find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_n return; CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node); + Node *scene = editor->get_edited_scene(); - bool inherited = p_node != get_tree()->get_edited_scene_root() && p_node->get_filename() != ""; - bool editable = !inherited || EditorNode::get_singleton()->get_edited_scene()->is_editable_instance(p_node); + bool editable = p_node == scene || p_node->get_owner() == scene || scene->is_editable_instance(p_node->get_owner()); bool lock_children = p_node->has_meta("_edit_group_") && p_node->get_meta("_edit_group_"); bool locked = p_node->has_meta("_edit_lock_") && p_node->get_meta("_edit_lock_"); - if (!lock_children && !locked && editable) { + if (!lock_children || !editable) { for (int i = p_node->get_child_count() - 1; i >= 0; i--) { if (canvas_item && !canvas_item->is_set_as_toplevel()) { _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * canvas_item->get_transform(), p_canvas_xform); @@ -558,7 +554,7 @@ void CanvasItemEditor::_find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_n } } - if (canvas_item && canvas_item->is_visible_in_tree() && !canvas_item->has_meta("_edit_lock_")) { + if (canvas_item && canvas_item->is_visible_in_tree() && !locked && editable) { Transform2D xform = p_parent_xform * p_canvas_xform * canvas_item->get_transform(); if (canvas_item->_edit_use_rect()) { @@ -2593,9 +2589,6 @@ void CanvasItemEditor::_draw_bones() { for (Map<BoneKey, BoneList>::Element *E = bone_list.front(); E; E = E->next()) { - E->get().from = Vector2(); - E->get().to = Vector2(); - Node2D *from_node = Object::cast_to<Node2D>(ObjectDB::get_instance(E->key().from)); Node2D *to_node = Object::cast_to<Node2D>(ObjectDB::get_instance(E->key().to)); @@ -2615,9 +2608,6 @@ void CanvasItemEditor::_draw_bones() { else to = transform.xform(from_node->get_global_transform().xform(Vector2(E->get().length, 0))); - E->get().from = from; - E->get().to = to; - Vector2 rel = to - from; Vector2 relt = rel.tangent().normalized() * bone_width; Vector2 reln = rel.normalized(); @@ -2679,7 +2669,7 @@ void CanvasItemEditor::_draw_invisible_nodes_positions(Node *p_node, const Trans ERR_FAIL_COND(!p_node); Node *scene = editor->get_edited_scene(); - if (p_node != scene && p_node->get_owner() != scene && !scene->is_editable_instance(p_node)) + if (p_node != scene && p_node->get_owner() != scene && !scene->is_editable_instance(p_node->get_owner())) return; CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node); if (canvas_item && !canvas_item->is_visible()) @@ -2805,27 +2795,20 @@ bool CanvasItemEditor::_build_bones_list(Node *p_node) { } } - CanvasItem *c = Object::cast_to<CanvasItem>(p_node); - if (!c) { - return false; - } - - Node *p = c->get_parent(); - if (!p) { - return false; - } - - if (!c->is_visible()) { + CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node); + Node *scene = editor->get_edited_scene(); + if (!canvas_item || !canvas_item->is_visible() || (canvas_item != scene && canvas_item->get_owner() != scene && !scene->is_editable_instance(canvas_item->get_owner()))) { return false; } - if (Object::cast_to<Bone2D>(c)) { + Node *parent = canvas_item->get_parent(); - if (Object::cast_to<Bone2D>(p)) { - //add as bone->parent relationship + if (Object::cast_to<Bone2D>(canvas_item)) { + if (Object::cast_to<Bone2D>(parent)) { + // Add as bone->parent relationship BoneKey bk; - bk.from = p->get_instance_id(); - bk.to = c->get_instance_id(); + bk.from = parent->get_instance_id(); + bk.to = canvas_item->get_instance_id(); if (!bone_list.has(bk)) { BoneList b; b.length = 0; @@ -2836,8 +2819,9 @@ bool CanvasItemEditor::_build_bones_list(Node *p_node) { } if (!has_child_bones) { + // Add a last bone if the Bone2D has no Bone2D child BoneKey bk; - bk.from = c->get_instance_id(); + bk.from = canvas_item->get_instance_id(); bk.to = 0; if (!bone_list.has(bk)) { BoneList b; @@ -2849,11 +2833,12 @@ bool CanvasItemEditor::_build_bones_list(Node *p_node) { return true; } - if (c->has_meta("_edit_bone_")) { + if (canvas_item->has_meta("_edit_bone_")) { + // Add a "custom bone" BoneKey bk; - bk.from = c->get_parent()->get_instance_id(); - bk.to = c->get_instance_id(); + bk.from = parent->get_instance_id(); + bk.to = canvas_item->get_instance_id(); if (!bone_list.has(bk)) { BoneList b; b.length = 0; @@ -2938,6 +2923,7 @@ void CanvasItemEditor::_notification(int p_what) { int nb_control = 0; int nb_having_pivot = 0; + // Update the viewport if the canvas_item changes List<CanvasItem *> selection = _get_edited_canvas_items(); for (List<CanvasItem *>::Element *E = selection.front(); E; E = E->next()) { CanvasItem *canvas_item = E->get(); @@ -2983,12 +2969,14 @@ void CanvasItemEditor::_notification(int p_what) { nb_having_pivot++; } } + // Activate / Deactivate the pivot tool pivot_button->set_disabled(nb_having_pivot == 0); // Show / Hide the layout button presets_menu->set_visible(nb_control > 0 && nb_control == selection.size()); + // Update the viewport if bones changes for (Map<BoneKey, BoneList>::Element *E = bone_list.front(); E; E = E->next()) { Object *b = ObjectDB::get_instance(E->key().from); @@ -2999,7 +2987,7 @@ void CanvasItemEditor::_notification(int p_what) { } Node2D *b2 = Object::cast_to<Node2D>(b); - if (!b2) { + if (!b2 || !b2->is_inside_tree()) { continue; } diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index eb3595cae6..a1957b892e 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -266,8 +266,6 @@ class CanvasItemEditor : public VBoxContainer { Transform2D xform; float length; uint64_t last_pass; - Vector2 from; - Vector2 to; }; uint64_t bone_last_frame; diff --git a/editor/plugins/physical_bone_plugin.cpp b/editor/plugins/physical_bone_plugin.cpp new file mode 100644 index 0000000000..42f1adcadf --- /dev/null +++ b/editor/plugins/physical_bone_plugin.cpp @@ -0,0 +1,123 @@ +/*************************************************************************/ +/* physical_bone_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "physical_bone_plugin.h" +#include "editor/plugins/spatial_editor_plugin.h" +#include "scene/3d/physics_body.h" + +void PhysicalBoneEditor::_bind_methods() { + ClassDB::bind_method(D_METHOD("_on_toggle_button_transform_joint", "is_pressed"), &PhysicalBoneEditor::_on_toggle_button_transform_joint); +} + +void PhysicalBoneEditor::_on_toggle_button_transform_joint(bool p_is_pressed) { + + _set_move_joint(); +} + +void PhysicalBoneEditor::_set_move_joint() { + if (selected) { + selected->_set_gizmo_move_joint(button_transform_joint->is_pressed()); + } +} + +PhysicalBoneEditor::PhysicalBoneEditor(EditorNode *p_editor) : + editor(p_editor), + selected(NULL) { + + spatial_editor_hb = memnew(HBoxContainer); + spatial_editor_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); + spatial_editor_hb->set_alignment(BoxContainer::ALIGN_BEGIN); + SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb); + + spatial_editor_hb->add_child(memnew(VSeparator)); + + button_transform_joint = memnew(ToolButton); + spatial_editor_hb->add_child(button_transform_joint); + + button_transform_joint->set_text(TTR("Move joint")); + button_transform_joint->set_icon(SpatialEditor::get_singleton()->get_icon("PhysicalBone", "EditorIcons")); + button_transform_joint->set_toggle_mode(true); + button_transform_joint->connect("toggled", this, "_on_toggle_button_transform_joint"); + + hide(); +} + +PhysicalBoneEditor::~PhysicalBoneEditor() { + // TODO the spatial_editor_hb should be removed from SpatialEditor, but in this moment it's not possible + for (int i = spatial_editor_hb->get_child_count() - 1; 0 <= i; --i) { + Node *n = spatial_editor_hb->get_child(i); + spatial_editor_hb->remove_child(n); + memdelete(n); + } + memdelete(spatial_editor_hb); +} + +void PhysicalBoneEditor::set_selected(PhysicalBone *p_pb) { + + button_transform_joint->set_pressed(false); + + _set_move_joint(); + selected = p_pb; + _set_move_joint(); +} + +void PhysicalBoneEditor::hide() { + spatial_editor_hb->hide(); +} + +void PhysicalBoneEditor::show() { + spatial_editor_hb->show(); +} + +PhysicalBonePlugin::PhysicalBonePlugin(EditorNode *p_editor) : + editor(p_editor), + selected(NULL) { + + physical_bone_editor = memnew(PhysicalBoneEditor(editor)); +} + +void PhysicalBonePlugin::make_visible(bool p_visible) { + if (p_visible) { + + physical_bone_editor->show(); + } else { + + physical_bone_editor->hide(); + physical_bone_editor->set_selected(NULL); + selected = NULL; + } +} + +void PhysicalBonePlugin::edit(Object *p_node) { + selected = static_cast<PhysicalBone *>(p_node); // Trust it + ERR_FAIL_COND(!selected); + + physical_bone_editor->set_selected(selected); +} diff --git a/editor/plugins/physical_bone_plugin.h b/editor/plugins/physical_bone_plugin.h new file mode 100644 index 0000000000..9e7a50307a --- /dev/null +++ b/editor/plugins/physical_bone_plugin.h @@ -0,0 +1,78 @@ +/*************************************************************************/ +/* physical_bone_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef PHYSICAL_BONE_PLUGIN_H +#define PHYSICAL_BONE_PLUGIN_H + +#include "editor/editor_node.h" + +class PhysicalBoneEditor : public Object { + GDCLASS(PhysicalBoneEditor, Object); + + EditorNode *editor; + HBoxContainer *spatial_editor_hb; + ToolButton *button_transform_joint; + + PhysicalBone *selected; + +protected: + static void _bind_methods(); + +private: + void _on_toggle_button_transform_joint(bool p_is_pressed); + void _set_move_joint(); + +public: + PhysicalBoneEditor(EditorNode *p_editor); + ~PhysicalBoneEditor(); + + void set_selected(PhysicalBone *p_pb); + + void hide(); + void show(); +}; + +class PhysicalBonePlugin : public EditorPlugin { + GDCLASS(PhysicalBonePlugin, EditorPlugin); + + EditorNode *editor; + PhysicalBone *selected; + PhysicalBoneEditor *physical_bone_editor; + +public: + virtual String get_name() const { return "PhysicalBone"; } + virtual bool handles(Object *p_object) const { return p_object->is_class("PhysicalBone"); } + virtual void make_visible(bool p_visible); + virtual void edit(Object *p_node); + + PhysicalBonePlugin(EditorNode *p_editor); +}; + +#endif diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index c872a6f28b..45f5e667fa 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -746,6 +746,8 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c _goto_line(p_row); + result.class_name = result.class_name.trim_prefix("_"); + switch (result.type) { case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: { @@ -1007,6 +1009,10 @@ void ScriptTextEditor::_edit_option(int p_op) { } int next_line = to_line + 1; + if (to_line >= tx->get_line_count() - 1) { + tx->set_line(to_line, tx->get_line(to_line) + "\n"); + } + tx->begin_complex_operation(); for (int i = from_line; i <= to_line; i++) { diff --git a/editor/plugins/skeleton_editor_plugin.cpp b/editor/plugins/skeleton_editor_plugin.cpp new file mode 100644 index 0000000000..c41e3b546f --- /dev/null +++ b/editor/plugins/skeleton_editor_plugin.cpp @@ -0,0 +1,189 @@ +/*************************************************************************/ +/* skeleton_editor_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "skeleton_editor_plugin.h" +#include "scene/3d/collision_shape.h" +#include "scene/3d/physics_body.h" +#include "scene/3d/physics_joint.h"; +#include "scene/resources/capsule_shape.h" +#include "scene/resources/sphere_shape.h" +#include "spatial_editor_plugin.h" + +void SkeletonEditor::_on_click_option(int p_option) { + if (!skeleton) { + return; + } + + switch (p_option) { + case MENU_OPTION_CREATE_PHYSICAL_SKELETON: { + create_physical_skeleton(); + } break; + } +} + +void SkeletonEditor::create_physical_skeleton() { + UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); + Node *owner = skeleton == get_tree()->get_edited_scene_root() ? skeleton : skeleton->get_owner(); + + const int bc = skeleton->get_bone_count(); + + if (!bc) { + return; + } + + Vector<BoneInfo> bones_infos; + bones_infos.resize(bc); + + for (int bone_id = 0; bc > bone_id; ++bone_id) { + + const int parent = skeleton->get_bone_parent(bone_id); + const int parent_parent = skeleton->get_bone_parent(parent); + + if (parent < 0) { + + bones_infos[bone_id].relative_rest = skeleton->get_bone_rest(bone_id); + + } else { + + bones_infos[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id); + + /// create physical bone on parent + if (!bones_infos[parent].physical_bone) { + + bones_infos[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos); + + ur->create_action(TTR("Create physical bones")); + ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone); + ur->add_do_reference(bones_infos[parent].physical_bone); + ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone); + ur->commit_action(); + + bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent)); + bones_infos[parent].physical_bone->set_owner(owner); + bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner + + /// Create joint between parent of parent + if (-1 != parent_parent) { + + bones_infos[parent].physical_bone->set_joint_type(PhysicalBone::JOINT_TYPE_PIN); + } + } + } + } +} + +PhysicalBone *SkeletonEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) { + + real_t half_height(skeleton->get_bone_rest(bone_child_id).origin.length() * 0.5); + real_t radius(half_height * 0.2); + + CapsuleShape *bone_shape_capsule = memnew(CapsuleShape); + bone_shape_capsule->set_height((half_height - radius) * 2); + bone_shape_capsule->set_radius(radius); + + CollisionShape *bone_shape = memnew(CollisionShape); + bone_shape->set_shape(bone_shape_capsule); + + Transform body_transform; + body_transform.origin = Vector3(0, 0, -half_height); + + Transform joint_transform; + joint_transform.origin = Vector3(0, 0, half_height); + + PhysicalBone *physical_bone = memnew(PhysicalBone); + physical_bone->add_child(bone_shape); + physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id)); + physical_bone->set_body_offset(body_transform); + physical_bone->set_joint_offset(joint_transform); + return physical_bone; +} + +void SkeletonEditor::edit(Skeleton *p_node) { + skeleton = p_node; +} + +void SkeletonEditor::_node_removed(Node *p_node) { + + if (p_node == skeleton) { + skeleton = NULL; + options->hide(); + } +} + +void SkeletonEditor::_bind_methods() { + ClassDB::bind_method("_on_click_option", &SkeletonEditor::_on_click_option); +} + +SkeletonEditor::SkeletonEditor() { + options = memnew(MenuButton); + SpatialEditor::get_singleton()->add_control_to_menu_panel(options); + + options->set_text(TTR("Skeleton")); + options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton", "EditorIcons")); + + options->get_popup()->add_item(TTR("Create physical skeleton"), MENU_OPTION_CREATE_PHYSICAL_SKELETON); + + options->get_popup()->connect("id_pressed", this, "_on_click_option"); + options->hide(); +} + +SkeletonEditor::~SkeletonEditor() { + SpatialEditor::get_singleton()->remove_child(options); + memdelete(options); +} + +void SkeletonEditorPlugin::edit(Object *p_object) { + skeleton_editor->edit(Object::cast_to<Skeleton>(p_object)); +} + +bool SkeletonEditorPlugin::handles(Object *p_object) const { + return p_object->is_class("Skeleton"); +} + +void SkeletonEditorPlugin::make_visible(bool p_visible) { + if (p_visible) { + skeleton_editor->options->show(); + } else { + + skeleton_editor->options->hide(); + skeleton_editor->edit(NULL); + } +} + +SkeletonEditorPlugin::SkeletonEditorPlugin(EditorNode *p_node) { + editor = p_node; + skeleton_editor = memnew(SkeletonEditor); + editor->get_viewport()->add_child(skeleton_editor); +} + +SkeletonEditorPlugin::~SkeletonEditorPlugin() { + editor->get_viewport()->remove_child(skeleton_editor); + memdelete(skeleton_editor); +} diff --git a/editor/plugins/skeleton_editor_plugin.h b/editor/plugins/skeleton_editor_plugin.h new file mode 100644 index 0000000000..b9bdf91902 --- /dev/null +++ b/editor/plugins/skeleton_editor_plugin.h @@ -0,0 +1,95 @@ +/*************************************************************************/ +/* skeleton_editor_plugin.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef SKELETON_EDITOR_PLUGIN_H +#define SKELETON_EDITOR_PLUGIN_H + +#include "editor/editor_node.h" +#include "editor/editor_plugin.h" +#include "scene/3d/skeleton.h" + +class PhysicalBone; +class Joint; + +class SkeletonEditor : public Node { + GDCLASS(SkeletonEditor, Node); + + enum Menu { + MENU_OPTION_CREATE_PHYSICAL_SKELETON + }; + + struct BoneInfo { + PhysicalBone *physical_bone; + Transform relative_rest; // Relative to skeleton node + BoneInfo() : + physical_bone(NULL) {} + }; + + Skeleton *skeleton; + + MenuButton *options; + + void _on_click_option(int p_option); + + friend class SkeletonEditorPlugin; + +protected: + void _node_removed(Node *p_node); + static void _bind_methods(); + + void create_physical_skeleton(); + PhysicalBone *create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos); + +public: + void edit(Skeleton *p_mesh); + + SkeletonEditor(); + ~SkeletonEditor(); +}; + +class SkeletonEditorPlugin : public EditorPlugin { + + GDCLASS(SkeletonEditorPlugin, EditorPlugin); + + EditorNode *editor; + SkeletonEditor *skeleton_editor; + +public: + virtual String get_name() const { return "Skeleton"; } + virtual bool has_main_screen() const { return false; } + virtual void edit(Object *p_object); + virtual bool handles(Object *p_object) const; + virtual void make_visible(bool p_visible); + + SkeletonEditorPlugin(EditorNode *p_node); + ~SkeletonEditorPlugin(); +}; + +#endif // SKELETON_EDITOR_PLUGIN_H diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 1443f22c8f..5b713ef3c4 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -72,6 +72,14 @@ #define MIN_FOV 0.01 #define MAX_FOV 179 +#ifdef TOOLS_ENABLED +#define get_global_gizmo_transform get_global_gizmo_transform +#define get_local_gizmo_transform get_local_gizmo_transform +#else +#define get_global_gizmo_transform get_global_transform +#define get_local_gizmo_transform get_transform +#endif + void SpatialEditorViewport::_update_camera(float p_interp_delta) { bool is_orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL; @@ -584,8 +592,8 @@ void SpatialEditorViewport::_compute_edit(const Point2 &p_point) { if (!se) continue; - se->original = se->sp->get_global_transform(); - se->original_local = se->sp->get_transform(); + se->original = se->sp->get_global_gizmo_transform(); + se->original_local = se->sp->get_local_gizmo_transform(); } } @@ -1184,7 +1192,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) { if (!se) continue; - undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_transform()); + undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform()); undo_redo->add_undo_method(sp, "set_global_transform", se->original); } undo_redo->commit_action(); @@ -2150,7 +2158,7 @@ void SpatialEditorViewport::_notification(int p_what) { se->aabb = vi ? vi->get_aabb() : AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4)); } - Transform t = sp->get_global_transform(); + Transform t = sp->get_global_gizmo_transform(); t.translate(se->aabb.position); // apply AABB scaling before item's global transform @@ -2503,7 +2511,7 @@ void SpatialEditorViewport::_menu_option(int p_option) { xform.scale_basis(sp->get_scale()); undo_redo->add_do_method(sp, "set_global_transform", xform); - undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform()); + undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform()); } undo_redo->commit_action(); } break; @@ -2961,7 +2969,7 @@ void SpatialEditorViewport::focus_selection() { if (!se) continue; - center += sp->get_global_transform().origin; + center += sp->get_global_gizmo_transform().origin; count++; } @@ -3043,7 +3051,7 @@ AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, c MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(child); if (mesh_instance) { AABB mesh_instance_bounds = mesh_instance->get_aabb(); - mesh_instance_bounds.position += mesh_instance->get_global_transform().origin - p_parent->get_global_transform().origin; + mesh_instance_bounds.position += mesh_instance->get_global_gizmo_transform().origin - p_parent->get_global_gizmo_transform().origin; bounds.merge_with(mesh_instance_bounds); } bounds = _calculate_spatial_bounds(child, bounds); @@ -3121,7 +3129,7 @@ bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const P if (!scene.is_valid()) { // invalid scene return false; } else { - instanced_scene = scene->instance(); + instanced_scene = scene->instance(PackedScene::GEN_EDIT_STATE_INSTANCE); } } } @@ -3154,7 +3162,7 @@ bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const P Transform global_transform; Spatial *parent_spatial = Object::cast_to<Spatial>(parent); if (parent_spatial) - global_transform = parent_spatial->get_global_transform(); + global_transform = parent_spatial->get_global_gizmo_transform(); global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point)); @@ -3787,7 +3795,8 @@ void SpatialEditor::update_transform_gizmo() { if (!se) continue; - Transform xf = se->sp->get_global_transform(); + Transform xf = se->sp->get_global_gizmo_transform(); + if (first) { center.position = xf.origin; first = false; @@ -4054,7 +4063,7 @@ void SpatialEditor::_xform_dialog_action() { bool post = xform_type->get_selected() > 0; - Transform tr = sp->get_global_transform(); + Transform tr = sp->get_global_gizmo_transform(); if (post) tr = tr * t; else { @@ -4064,7 +4073,7 @@ void SpatialEditor::_xform_dialog_action() { } undo_redo->add_do_method(sp, "set_global_transform", tr); - undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform()); + undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform()); } undo_redo->commit_action(); } @@ -4598,7 +4607,10 @@ void SpatialEditor::_init_grid() { PoolVector<Color> grid_colors[3]; PoolVector<Vector3> grid_points[3]; - Color grid_color = EditorSettings::get_singleton()->get("editors/3d/grid_color"); + Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color"); + Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color"); + int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size"); + int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps"); for (int i = 0; i < 3; i++) { Vector3 axis; @@ -4608,19 +4620,17 @@ void SpatialEditor::_init_grid() { Vector3 axis_n2; axis_n2[(i + 2) % 3] = 1; -#define ORIGIN_GRID_SIZE 50 - - for (int j = -ORIGIN_GRID_SIZE; j <= ORIGIN_GRID_SIZE; j++) { - Vector3 p1 = axis_n1 * j + axis_n2 * -ORIGIN_GRID_SIZE; + for (int j = -grid_size; j <= grid_size; j++) { + Vector3 p1 = axis_n1 * j + axis_n2 * -grid_size; Vector3 p1_dest = p1 * (-axis_n2 + axis_n1); - Vector3 p2 = axis_n2 * j + axis_n1 * -ORIGIN_GRID_SIZE; + Vector3 p2 = axis_n2 * j + axis_n1 * -grid_size; Vector3 p2_dest = p2 * (-axis_n1 + axis_n2); - Color line_color = grid_color; + Color line_color = secondary_grid_color; if (j == 0) { continue; - } else if (j % 10 == 0) { - line_color *= 1.5; + } else if (j % primary_grid_steps == 0) { + line_color = primary_grid_color; } grid_points[i].push_back(p1); |