1/**************************************************************************/
2/* skeleton_2d.h */
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#ifndef SKELETON_2D_H
32#define SKELETON_2D_H
33
34#include "scene/2d/node_2d.h"
35#include "scene/resources/skeleton_modification_2d.h"
36
37class Skeleton2D;
38
39class Bone2D : public Node2D {
40 GDCLASS(Bone2D, Node2D);
41
42 friend class Skeleton2D;
43#ifdef TOOLS_ENABLED
44 friend class AnimatedValuesBackup;
45#endif
46
47 Bone2D *parent_bone = nullptr;
48 Skeleton2D *skeleton = nullptr;
49 Transform2D rest;
50
51 bool autocalculate_length_and_angle = true;
52 real_t length = 16;
53 real_t bone_angle = 0;
54
55 int skeleton_index = -1;
56
57 void calculate_length_and_rotation();
58
59#ifdef TOOLS_ENABLED
60 RID editor_gizmo_rid;
61 bool _editor_get_bone_shape(Vector<Vector2> *p_shape, Vector<Vector2> *p_outline_shape, Bone2D *p_other_bone);
62 bool _editor_show_bone_gizmo = true;
63#endif // TOOLS ENABLED
64
65protected:
66 void _notification(int p_what);
67 static void _bind_methods();
68 bool _set(const StringName &p_path, const Variant &p_value);
69 bool _get(const StringName &p_path, Variant &r_ret) const;
70 void _get_property_list(List<PropertyInfo> *p_list) const;
71
72public:
73 Transform2D cache_transform;
74 bool copy_transform_to_cache = true;
75
76 void set_rest(const Transform2D &p_rest);
77 Transform2D get_rest() const;
78 void apply_rest();
79 Transform2D get_skeleton_rest() const;
80
81 PackedStringArray get_configuration_warnings() const override;
82
83 void set_autocalculate_length_and_angle(bool p_autocalculate);
84 bool get_autocalculate_length_and_angle() const;
85 void set_length(real_t p_length);
86 real_t get_length() const;
87 void set_bone_angle(real_t p_angle);
88 real_t get_bone_angle() const;
89
90 int get_index_in_skeleton() const;
91
92#ifdef TOOLS_ENABLED
93 void _editor_set_show_bone_gizmo(bool p_show_gizmo);
94 bool _editor_get_show_bone_gizmo() const;
95#endif // TOOLS_ENABLED
96
97 Bone2D();
98 ~Bone2D();
99};
100
101class SkeletonModificationStack2D;
102
103class Skeleton2D : public Node2D {
104 GDCLASS(Skeleton2D, Node2D);
105
106 friend class Bone2D;
107#ifdef TOOLS_ENABLED
108 friend class AnimatedValuesBackup;
109#endif
110
111 struct Bone {
112 bool operator<(const Bone &p_bone) const {
113 return p_bone.bone->is_greater_than(bone);
114 }
115 Bone2D *bone = nullptr;
116 int parent_index = 0;
117 Transform2D accum_transform;
118 Transform2D rest_inverse;
119
120 //Transform2D local_pose_cache;
121 Transform2D local_pose_override;
122 real_t local_pose_override_amount = 0;
123 bool local_pose_override_persistent = false;
124 };
125
126 Vector<Bone> bones;
127
128 bool bone_setup_dirty = true;
129 void _make_bone_setup_dirty();
130 void _update_bone_setup();
131
132 bool transform_dirty = true;
133 void _make_transform_dirty();
134 void _update_transform();
135
136 RID skeleton;
137
138 Ref<SkeletonModificationStack2D> modification_stack;
139
140protected:
141 void _notification(int p_what);
142 static void _bind_methods();
143 bool _set(const StringName &p_path, const Variant &p_value);
144 bool _get(const StringName &p_path, Variant &r_ret) const;
145 void _get_property_list(List<PropertyInfo> *p_list) const;
146
147public:
148 int get_bone_count() const;
149 Bone2D *get_bone(int p_idx);
150
151 RID get_skeleton() const;
152
153 void set_bone_local_pose_override(int p_bone_idx, Transform2D p_override, real_t p_amount, bool p_persistent = true);
154 Transform2D get_bone_local_pose_override(int p_bone_idx);
155
156 Ref<SkeletonModificationStack2D> get_modification_stack() const;
157 void set_modification_stack(Ref<SkeletonModificationStack2D> p_stack);
158 void execute_modifications(real_t p_delta, int p_execution_mode);
159
160 Skeleton2D();
161 ~Skeleton2D();
162};
163
164#endif // SKELETON_2D_H
165