summaryrefslogtreecommitdiff
path: root/scene/2d
diff options
context:
space:
mode:
Diffstat (limited to 'scene/2d')
-rw-r--r--scene/2d/canvas_item.cpp6
-rw-r--r--scene/2d/canvas_item.h2
-rw-r--r--scene/2d/mesh_instance_2d.cpp2
-rw-r--r--scene/2d/skeleton_2d.cpp210
-rw-r--r--scene/2d/skeleton_2d.h68
5 files changed, 283 insertions, 5 deletions
diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp
index 8d7fce8cc4..5cca5705a0 100644
--- a/scene/2d/canvas_item.cpp
+++ b/scene/2d/canvas_item.cpp
@@ -779,13 +779,13 @@ void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Colo
VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid, rid_normal, p_antialiased);
}
-void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, RID p_skeleton) {
+void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {
ERR_FAIL_COND(p_mesh.is_null());
RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();
- VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), texture_rid, normal_map_rid, p_skeleton);
+ VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), texture_rid, normal_map_rid);
}
void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {
@@ -1032,7 +1032,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_colored_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("draw_string", "font", "position", "text", "modulate", "clip_w"), &CanvasItem::draw_string, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
ClassDB::bind_method(D_METHOD("draw_char", "font", "position", "char", "next", "modulate"), &CanvasItem::draw_char, DEFVAL(Color(1, 1, 1)));
- ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map", "skeleton"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()), DEFVAL(RID()));
+ ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()));
ClassDB::bind_method(D_METHOD("draw_multimesh", "mesh", "texture", "normal_map"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()));
ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform);
diff --git a/scene/2d/canvas_item.h b/scene/2d/canvas_item.h
index d0bf584b38..980fcb4109 100644
--- a/scene/2d/canvas_item.h
+++ b/scene/2d/canvas_item.h
@@ -283,7 +283,7 @@ public:
void draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);
void draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);
- void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, RID p_skeleton);
+ void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
void draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
void draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, const Color &p_modulate = Color(1, 1, 1), int p_clip_w = -1);
diff --git a/scene/2d/mesh_instance_2d.cpp b/scene/2d/mesh_instance_2d.cpp
index 184942663f..adbb227d0c 100644
--- a/scene/2d/mesh_instance_2d.cpp
+++ b/scene/2d/mesh_instance_2d.cpp
@@ -4,7 +4,7 @@ void MeshInstance2D::_notification(int p_what) {
if (p_what == NOTIFICATION_DRAW) {
if (mesh.is_valid()) {
- draw_mesh(mesh, texture, normal_map, RID());
+ draw_mesh(mesh, texture, normal_map);
}
}
}
diff --git a/scene/2d/skeleton_2d.cpp b/scene/2d/skeleton_2d.cpp
new file mode 100644
index 0000000000..705e82bcbb
--- /dev/null
+++ b/scene/2d/skeleton_2d.cpp
@@ -0,0 +1,210 @@
+#include "skeleton_2d.h"
+
+void Bone2D::_notification(int p_what) {
+
+ if (p_what == NOTIFICATION_ENTER_TREE) {
+ Node *parent = get_parent();
+ parent_bone = Object::cast_to<Bone2D>(parent);
+ skeleton = NULL;
+ while (parent) {
+ skeleton = Object::cast_to<Skeleton2D>(parent);
+ if (skeleton)
+ break;
+ if (!Object::cast_to<Bone2D>(parent))
+ break; //skeletons must be chained to Bone2Ds.
+ }
+
+ if (skeleton) {
+ Skeleton2D::Bone bone;
+ bone.bone = this;
+ skeleton->bones.push_back(bone);
+ skeleton->_make_bone_setup_dirty();
+ }
+ }
+ if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
+ if (skeleton) {
+ skeleton->_make_transform_dirty();
+ }
+ }
+
+ if (p_what == NOTIFICATION_EXIT_TREE) {
+ if (skeleton) {
+ for (int i = 0; i < skeleton->bones.size(); i++) {
+ if (skeleton->bones[i].bone == this) {
+ skeleton->bones.remove(i);
+ break;
+ }
+ }
+ skeleton->_make_bone_setup_dirty();
+ skeleton = NULL;
+ }
+ parent_bone = NULL;
+ }
+}
+void Bone2D::_bind_methods() {
+
+ ClassDB::bind_method(D_METHOD("set_rest", "rest"), &Bone2D::set_rest);
+ ClassDB::bind_method(D_METHOD("get_rest"), &Bone2D::get_rest);
+ ClassDB::bind_method(D_METHOD("apply_rest"), &Bone2D::apply_rest);
+}
+
+void Bone2D::set_rest(const Transform2D &p_rest) {
+ rest = p_rest;
+ if (skeleton)
+ skeleton->_make_bone_setup_dirty();
+}
+
+Transform2D Bone2D::get_rest() const {
+ return rest;
+}
+
+Transform2D Bone2D::get_skeleton_rest() const {
+
+ if (parent_bone) {
+ return parent_bone->get_skeleton_rest() * rest;
+ } else {
+ return rest;
+ }
+}
+
+void Bone2D::apply_rest() {
+ set_transform(rest);
+}
+
+String Bone2D::get_configuration_warning() const {
+ if (!skeleton) {
+ if (parent_bone) {
+ return TTR("This Bone2D chain should end at a Skeleton2D node.");
+ } else {
+ return TTR("A Bone2D only works with a Skeleton2D or another Bone2D as parent node.");
+ }
+ }
+
+ return Node2D::get_configuration_warning();
+}
+
+Bone2D::Bone2D() {
+ skeleton = NULL;
+ parent_bone = NULL;
+ set_notify_local_transform(true);
+}
+
+//////////////////////////////////////
+
+void Skeleton2D::_make_bone_setup_dirty() {
+
+ if (bone_setup_dirty)
+ return;
+ bone_setup_dirty = true;
+ if (is_inside_tree()) {
+ call_deferred("_update_bone_setup");
+ }
+}
+
+void Skeleton2D::_update_bone_setup() {
+
+ if (!bone_setup_dirty)
+ return;
+
+ bone_setup_dirty = false;
+ VS::get_singleton()->skeleton_allocate(skeleton, bones.size(), true);
+
+ bones.sort(); //sorty so they are always in the same order/index
+
+ for (int i = 0; i < bones.size(); i++) {
+ bones[i].rest_inverse = bones[i].bone->get_skeleton_rest(); //bind pose
+ }
+
+ transform_dirty = true;
+ _update_transform();
+}
+
+void Skeleton2D::_make_transform_dirty() {
+
+ if (transform_dirty)
+ return;
+ transform_dirty = true;
+ if (is_inside_tree()) {
+ call_deferred("_update_transform");
+ }
+}
+
+void Skeleton2D::_update_transform() {
+
+ if (bone_setup_dirty) {
+ _update_bone_setup();
+ return; //above will update transform anyway
+ }
+ if (!transform_dirty)
+ return;
+
+ transform_dirty = false;
+
+ Transform2D global_xform = get_global_transform();
+ Transform2D global_xform_inverse = global_xform.affine_inverse();
+
+ for (int i = 0; i < bones.size(); i++) {
+
+ Transform2D final_xform = bones[i].rest_inverse * bones[i].bone->get_relative_transform_to_parent(this);
+ VS::get_singleton()->skeleton_bone_set_transform_2d(skeleton, i, global_xform * (final_xform * global_xform_inverse));
+ }
+}
+
+int Skeleton2D::get_bone_count() const {
+
+ ERR_FAIL_COND_V(!is_inside_tree(), 0);
+
+ if (bone_setup_dirty) {
+ const_cast<Skeleton2D *>(this)->_update_bone_setup();
+ }
+
+ return bones.size();
+}
+
+Bone2D *Skeleton2D::get_bone(int p_idx) {
+
+ ERR_FAIL_COND_V(!is_inside_tree(), NULL);
+ ERR_FAIL_INDEX_V(p_idx, bones.size(), NULL);
+
+ return bones[p_idx].bone;
+}
+
+void Skeleton2D::_notification(int p_what) {
+
+ if (p_what == NOTIFICATION_READY) {
+
+ if (bone_setup_dirty)
+ _update_bone_setup();
+ if (transform_dirty)
+ _update_transform();
+ }
+
+ if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
+ _make_transform_dirty();
+ }
+}
+
+RID Skeleton2D::get_skeleton() const {
+ return skeleton;
+}
+void Skeleton2D::_bind_methods() {
+
+ ClassDB::bind_method(D_METHOD("_update_bone_setup"), &Skeleton2D::_update_bone_setup);
+ ClassDB::bind_method(D_METHOD("_update_transform"), &Skeleton2D::_update_transform);
+
+ ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton2D::get_bone_count);
+ ClassDB::bind_method(D_METHOD("get_bone"), &Skeleton2D::get_bone);
+
+ ClassDB::bind_method(D_METHOD("get_skeleton"), &Skeleton2D::get_skeleton);
+}
+
+Skeleton2D::Skeleton2D() {
+ bone_setup_dirty = true;
+ transform_dirty = true;
+ skeleton = VS::get_singleton()->skeleton_create();
+}
+
+Skeleton2D::~Skeleton2D() {
+
+ VS::get_singleton()->free(skeleton);
+}
diff --git a/scene/2d/skeleton_2d.h b/scene/2d/skeleton_2d.h
new file mode 100644
index 0000000000..791af57bdf
--- /dev/null
+++ b/scene/2d/skeleton_2d.h
@@ -0,0 +1,68 @@
+#ifndef SKELETON_2D_H
+#define SKELETON_2D_H
+
+#include "scene/2d/node_2d.h"
+
+class Skeleton2D;
+
+class Bone2D : public Node2D {
+ GDCLASS(Bone2D, Node2D)
+
+ Bone2D *parent_bone;
+ Skeleton2D *skeleton;
+ Transform2D rest;
+
+protected:
+ void _notification(int p_what);
+ static void _bind_methods();
+
+public:
+ void set_rest(const Transform2D &p_rest);
+ Transform2D get_rest() const;
+ void apply_rest();
+ Transform2D get_skeleton_rest() const;
+
+ String get_configuration_warning() const;
+
+ Bone2D();
+};
+
+class Skeleton2D : public Node2D {
+ GDCLASS(Skeleton2D, Node2D);
+
+ friend class Bone2D;
+
+ struct Bone {
+ bool operator<(const Bone &p_bone) const {
+ p_bone.bone->is_greater_than(bone);
+ }
+ Bone2D *bone;
+ Transform2D rest_inverse;
+ };
+
+ Vector<Bone> bones;
+
+ bool bone_setup_dirty;
+ void _make_bone_setup_dirty();
+ void _update_bone_setup();
+
+ bool transform_dirty;
+ void _make_transform_dirty();
+ void _update_transform();
+
+ RID skeleton;
+
+protected:
+ void _notification(int p_what);
+ static void _bind_methods();
+
+public:
+ int get_bone_count() const;
+ Bone2D *get_bone(int p_idx);
+
+ RID get_skeleton() const;
+ Skeleton2D();
+ ~Skeleton2D();
+};
+
+#endif // SKELETON_2D_H