summaryrefslogtreecommitdiff
path: root/scene/3d/skeleton.h
blob: 033f212a58ff42c606e9b88b3b553a9bde499264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*************************************************************************/
/*  skeleton.h                                                           */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* 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_H
#define SKELETON_H

#include "scene/3d/spatial.h"
#include "rid.h"

/**
	@author Juan Linietsky <reduzio@gmail.com>
*/
class Skeleton : public Spatial {

	OBJ_TYPE( Skeleton, Spatial );

	struct Bone {

		String name;

		bool enabled;
		int parent;

		bool disable_rest;
		Transform rest;
		Transform rest_global_inverse;
		 
		Transform pose;
		Transform pose_global;

		bool custom_pose_enable;
		Transform custom_pose;
		
		List<uint32_t> nodes_bound;
		
		Bone() { parent=-1; enabled=true; custom_pose_enable=false; disable_rest=false; }
	};

	bool rest_global_inverse_dirty;

	Vector<Bone> bones;
	
	RID skeleton;
	
	void _make_dirty();
	bool dirty;
	
//bind helpers
	Array _get_bound_child_nodes_to_bone(int p_bone) const {
	
		Array bound;
		List<Node*> childs;
		get_bound_child_nodes_to_bone(p_bone,&childs);
		
		for (int i=0;i<childs.size();i++) {
		
			bound.push_back( childs[i] );
		}
		return bound;
	}

	virtual RES _get_gizmo_geometry() const;

protected:

	bool _get(const StringName& p_name,Variant &r_ret) const;
	bool _set(const StringName& p_name, const Variant& p_value);
	void _get_property_list( List<PropertyInfo>* p_list ) const;
	void _notification(int p_what);
	static void _bind_methods();
	
public:

	enum {
	
		NOTIFICATION_UPDATE_SKELETON=50
	};


	RID get_skeleton() const;

	// skeleton creation api
	void add_bone(const String&p_name);
	int find_bone(String p_name) const;
	String get_bone_name(int p_bone) const;
	
	void set_bone_parent(int p_bone, int p_parent);
	int get_bone_parent(int p_bone) const;

	void unparent_bone_and_rest(int p_idx);

	void set_bone_disable_rest(int p_bone, bool p_disable);
	bool is_bone_rest_disabled(int p_bone) const;

	int get_bone_count() const;
	
	void set_bone_rest(int p_bone, const Transform& p_rest);
	Transform get_bone_rest(int p_bone) const;
	Transform get_bone_transform(int p_bone) const;
	Transform get_bone_global_pose(int p_bone) const;

	void set_bone_global_pose(int p_bone,const Transform& p_pose);

	void set_bone_enabled(int p_bone, bool p_enabled);
	bool is_bone_enabled(int p_bone) const;
	
	void bind_child_node_to_bone(int p_bone,Node *p_node);
	void unbind_child_node_from_bone(int p_bone,Node *p_node);
	void get_bound_child_nodes_to_bone(int p_bone,List<Node*> *p_bound) const;
	
	void clear_bones();
	
	// posing api
	
	void set_bone_pose(int p_bone, const Transform& p_pose);
	Transform get_bone_pose(int p_bone) const;

	void set_bone_custom_pose(int p_bone, const Transform& p_custom_pose);
	Transform get_bone_custom_pose(int p_bone) const;

	void localize_rests(); // used for loaders and tools
	
	Skeleton();	
	~Skeleton();

};

#endif