skeleton_modification_2d_lookat.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /**************************************************************************/
  2. /* skeleton_modification_2d_lookat.cpp */
  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. #include "skeleton_modification_2d_lookat.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif // TOOLS_ENABLED
  35. bool SkeletonModification2DLookAt::_set(const StringName &p_path, const Variant &p_value) {
  36. String path = p_path;
  37. if (path.begins_with("enable_constraint")) {
  38. set_enable_constraint(p_value);
  39. } else if (path.begins_with("constraint_angle_min")) {
  40. set_constraint_angle_min(Math::deg_to_rad(float(p_value)));
  41. } else if (path.begins_with("constraint_angle_max")) {
  42. set_constraint_angle_max(Math::deg_to_rad(float(p_value)));
  43. } else if (path.begins_with("constraint_angle_invert")) {
  44. set_constraint_angle_invert(p_value);
  45. } else if (path.begins_with("constraint_in_localspace")) {
  46. set_constraint_in_localspace(p_value);
  47. } else if (path.begins_with("additional_rotation")) {
  48. set_additional_rotation(Math::deg_to_rad(float(p_value)));
  49. }
  50. #ifdef TOOLS_ENABLED
  51. if (path.begins_with("editor/draw_gizmo")) {
  52. set_editor_draw_gizmo(p_value);
  53. }
  54. #endif // TOOLS_ENABLED
  55. return true;
  56. }
  57. bool SkeletonModification2DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
  58. String path = p_path;
  59. if (path.begins_with("enable_constraint")) {
  60. r_ret = get_enable_constraint();
  61. } else if (path.begins_with("constraint_angle_min")) {
  62. r_ret = Math::rad_to_deg(get_constraint_angle_min());
  63. } else if (path.begins_with("constraint_angle_max")) {
  64. r_ret = Math::rad_to_deg(get_constraint_angle_max());
  65. } else if (path.begins_with("constraint_angle_invert")) {
  66. r_ret = get_constraint_angle_invert();
  67. } else if (path.begins_with("constraint_in_localspace")) {
  68. r_ret = get_constraint_in_localspace();
  69. } else if (path.begins_with("additional_rotation")) {
  70. r_ret = Math::rad_to_deg(get_additional_rotation());
  71. }
  72. #ifdef TOOLS_ENABLED
  73. if (path.begins_with("editor/draw_gizmo")) {
  74. r_ret = get_editor_draw_gizmo();
  75. }
  76. #endif // TOOLS_ENABLED
  77. return true;
  78. }
  79. void SkeletonModification2DLookAt::_get_property_list(List<PropertyInfo> *p_list) const {
  80. p_list->push_back(PropertyInfo(Variant::BOOL, "enable_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  81. if (enable_constraint) {
  82. p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_min", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
  83. p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_max", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
  84. p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_angle_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  85. p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_in_localspace", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  86. }
  87. p_list->push_back(PropertyInfo(Variant::FLOAT, "additional_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  88. #ifdef TOOLS_ENABLED
  89. if (Engine::get_singleton()->is_editor_hint()) {
  90. p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  91. }
  92. #endif // TOOLS_ENABLED
  93. }
  94. void SkeletonModification2DLookAt::_execute(float p_delta) {
  95. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  96. "Modification is not setup and therefore cannot execute!");
  97. if (!enabled) {
  98. return;
  99. }
  100. if (target_node_cache.is_null()) {
  101. WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
  102. update_target_cache();
  103. return;
  104. }
  105. if (bone2d_node_cache.is_null() && !bone2d_node.is_empty()) {
  106. update_bone2d_cache();
  107. WARN_PRINT_ONCE("Bone2D node cache is out of date. Attempting to update...");
  108. return;
  109. }
  110. if (target_node_reference == nullptr) {
  111. target_node_reference = Object::cast_to<Node2D>(ObjectDB::get_instance(target_node_cache));
  112. }
  113. if (!target_node_reference || !target_node_reference->is_inside_tree()) {
  114. ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
  115. return;
  116. }
  117. if (bone_idx <= -1) {
  118. ERR_PRINT_ONCE("Bone index is invalid. Cannot execute modification!");
  119. return;
  120. }
  121. Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
  122. if (operation_bone == nullptr) {
  123. ERR_PRINT_ONCE("bone_idx for modification does not point to a valid bone! Cannot execute modification");
  124. return;
  125. }
  126. Transform2D operation_transform = operation_bone->get_global_transform();
  127. Transform2D target_trans = target_node_reference->get_global_transform();
  128. // Look at the target!
  129. operation_transform = operation_transform.looking_at(target_trans.get_origin());
  130. // Apply whatever scale it had prior to looking_at
  131. operation_transform.set_scale(operation_bone->get_global_scale());
  132. // Account for the direction the bone faces in:
  133. operation_transform.set_rotation(operation_transform.get_rotation() - operation_bone->get_bone_angle());
  134. // Apply additional rotation
  135. operation_transform.set_rotation(operation_transform.get_rotation() + additional_rotation);
  136. // Apply constraints in globalspace:
  137. if (enable_constraint && !constraint_in_localspace) {
  138. operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
  139. }
  140. // Convert from a global transform to a local transform via the Bone2D node
  141. operation_bone->set_global_transform(operation_transform);
  142. operation_transform = operation_bone->get_transform();
  143. // Apply constraints in localspace:
  144. if (enable_constraint && constraint_in_localspace) {
  145. operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
  146. }
  147. // Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
  148. stack->skeleton->set_bone_local_pose_override(bone_idx, operation_transform, stack->strength, true);
  149. operation_bone->set_transform(operation_transform);
  150. }
  151. void SkeletonModification2DLookAt::_setup_modification(SkeletonModificationStack2D *p_stack) {
  152. stack = p_stack;
  153. if (stack != nullptr) {
  154. is_setup = true;
  155. update_target_cache();
  156. update_bone2d_cache();
  157. }
  158. }
  159. void SkeletonModification2DLookAt::_draw_editor_gizmo() {
  160. if (!enabled || !is_setup) {
  161. return;
  162. }
  163. Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
  164. editor_draw_angle_constraints(operation_bone, constraint_angle_min, constraint_angle_max,
  165. enable_constraint, constraint_in_localspace, constraint_angle_invert);
  166. }
  167. void SkeletonModification2DLookAt::update_bone2d_cache() {
  168. if (!is_setup || !stack) {
  169. ERR_PRINT_ONCE("Cannot update Bone2D cache: modification is not properly setup!");
  170. return;
  171. }
  172. bone2d_node_cache = ObjectID();
  173. if (stack->skeleton) {
  174. if (stack->skeleton->is_inside_tree()) {
  175. if (stack->skeleton->has_node(bone2d_node)) {
  176. Node *node = stack->skeleton->get_node(bone2d_node);
  177. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  178. "Cannot update Bone2D cache: node is this modification's skeleton or cannot be found!");
  179. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  180. "Cannot update Bone2D cache: node is not in the scene tree!");
  181. bone2d_node_cache = node->get_instance_id();
  182. Bone2D *bone = Object::cast_to<Bone2D>(node);
  183. if (bone) {
  184. bone_idx = bone->get_index_in_skeleton();
  185. } else {
  186. ERR_FAIL_MSG("Error Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
  187. }
  188. // Set this to null so we update it
  189. target_node_reference = nullptr;
  190. }
  191. }
  192. }
  193. }
  194. void SkeletonModification2DLookAt::set_bone2d_node(const NodePath &p_target_node) {
  195. bone2d_node = p_target_node;
  196. update_bone2d_cache();
  197. }
  198. NodePath SkeletonModification2DLookAt::get_bone2d_node() const {
  199. return bone2d_node;
  200. }
  201. int SkeletonModification2DLookAt::get_bone_index() const {
  202. return bone_idx;
  203. }
  204. void SkeletonModification2DLookAt::set_bone_index(int p_bone_idx) {
  205. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  206. if (is_setup && stack) {
  207. if (stack->skeleton) {
  208. ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
  209. bone_idx = p_bone_idx;
  210. bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
  211. bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
  212. } else {
  213. WARN_PRINT("Cannot verify the bone index for this modification...");
  214. bone_idx = p_bone_idx;
  215. }
  216. } else {
  217. WARN_PRINT("Cannot verify the bone index for this modification...");
  218. bone_idx = p_bone_idx;
  219. }
  220. notify_property_list_changed();
  221. }
  222. void SkeletonModification2DLookAt::update_target_cache() {
  223. if (!is_setup || !stack) {
  224. ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
  225. return;
  226. }
  227. target_node_cache = ObjectID();
  228. if (stack->skeleton) {
  229. if (stack->skeleton->is_inside_tree()) {
  230. if (stack->skeleton->has_node(target_node)) {
  231. Node *node = stack->skeleton->get_node(target_node);
  232. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  233. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  234. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  235. "Cannot update target cache: node is not in the scene tree!");
  236. target_node_cache = node->get_instance_id();
  237. }
  238. }
  239. }
  240. }
  241. void SkeletonModification2DLookAt::set_target_node(const NodePath &p_target_node) {
  242. target_node = p_target_node;
  243. update_target_cache();
  244. }
  245. NodePath SkeletonModification2DLookAt::get_target_node() const {
  246. return target_node;
  247. }
  248. float SkeletonModification2DLookAt::get_additional_rotation() const {
  249. return additional_rotation;
  250. }
  251. void SkeletonModification2DLookAt::set_additional_rotation(float p_rotation) {
  252. additional_rotation = p_rotation;
  253. }
  254. void SkeletonModification2DLookAt::set_enable_constraint(bool p_constraint) {
  255. enable_constraint = p_constraint;
  256. notify_property_list_changed();
  257. #ifdef TOOLS_ENABLED
  258. if (stack && is_setup) {
  259. stack->set_editor_gizmos_dirty(true);
  260. }
  261. #endif // TOOLS_ENABLED
  262. }
  263. bool SkeletonModification2DLookAt::get_enable_constraint() const {
  264. return enable_constraint;
  265. }
  266. void SkeletonModification2DLookAt::set_constraint_angle_min(float p_angle_min) {
  267. constraint_angle_min = p_angle_min;
  268. #ifdef TOOLS_ENABLED
  269. if (stack && is_setup) {
  270. stack->set_editor_gizmos_dirty(true);
  271. }
  272. #endif // TOOLS_ENABLED
  273. }
  274. float SkeletonModification2DLookAt::get_constraint_angle_min() const {
  275. return constraint_angle_min;
  276. }
  277. void SkeletonModification2DLookAt::set_constraint_angle_max(float p_angle_max) {
  278. constraint_angle_max = p_angle_max;
  279. #ifdef TOOLS_ENABLED
  280. if (stack && is_setup) {
  281. stack->set_editor_gizmos_dirty(true);
  282. }
  283. #endif // TOOLS_ENABLED
  284. }
  285. float SkeletonModification2DLookAt::get_constraint_angle_max() const {
  286. return constraint_angle_max;
  287. }
  288. void SkeletonModification2DLookAt::set_constraint_angle_invert(bool p_invert) {
  289. constraint_angle_invert = p_invert;
  290. #ifdef TOOLS_ENABLED
  291. if (stack && is_setup) {
  292. stack->set_editor_gizmos_dirty(true);
  293. }
  294. #endif // TOOLS_ENABLED
  295. }
  296. bool SkeletonModification2DLookAt::get_constraint_angle_invert() const {
  297. return constraint_angle_invert;
  298. }
  299. void SkeletonModification2DLookAt::set_constraint_in_localspace(bool p_constraint_in_localspace) {
  300. constraint_in_localspace = p_constraint_in_localspace;
  301. #ifdef TOOLS_ENABLED
  302. if (stack && is_setup) {
  303. stack->set_editor_gizmos_dirty(true);
  304. }
  305. #endif // TOOLS_ENABLED
  306. }
  307. bool SkeletonModification2DLookAt::get_constraint_in_localspace() const {
  308. return constraint_in_localspace;
  309. }
  310. void SkeletonModification2DLookAt::_bind_methods() {
  311. ClassDB::bind_method(D_METHOD("set_bone2d_node", "bone2d_nodepath"), &SkeletonModification2DLookAt::set_bone2d_node);
  312. ClassDB::bind_method(D_METHOD("get_bone2d_node"), &SkeletonModification2DLookAt::get_bone2d_node);
  313. ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification2DLookAt::set_bone_index);
  314. ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification2DLookAt::get_bone_index);
  315. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DLookAt::set_target_node);
  316. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DLookAt::get_target_node);
  317. ClassDB::bind_method(D_METHOD("set_additional_rotation", "rotation"), &SkeletonModification2DLookAt::set_additional_rotation);
  318. ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification2DLookAt::get_additional_rotation);
  319. ClassDB::bind_method(D_METHOD("set_enable_constraint", "enable_constraint"), &SkeletonModification2DLookAt::set_enable_constraint);
  320. ClassDB::bind_method(D_METHOD("get_enable_constraint"), &SkeletonModification2DLookAt::get_enable_constraint);
  321. ClassDB::bind_method(D_METHOD("set_constraint_angle_min", "angle_min"), &SkeletonModification2DLookAt::set_constraint_angle_min);
  322. ClassDB::bind_method(D_METHOD("get_constraint_angle_min"), &SkeletonModification2DLookAt::get_constraint_angle_min);
  323. ClassDB::bind_method(D_METHOD("set_constraint_angle_max", "angle_max"), &SkeletonModification2DLookAt::set_constraint_angle_max);
  324. ClassDB::bind_method(D_METHOD("get_constraint_angle_max"), &SkeletonModification2DLookAt::get_constraint_angle_max);
  325. ClassDB::bind_method(D_METHOD("set_constraint_angle_invert", "invert"), &SkeletonModification2DLookAt::set_constraint_angle_invert);
  326. ClassDB::bind_method(D_METHOD("get_constraint_angle_invert"), &SkeletonModification2DLookAt::get_constraint_angle_invert);
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index");
  328. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D"), "set_bone2d_node", "get_bone2d_node");
  329. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
  330. }
  331. SkeletonModification2DLookAt::SkeletonModification2DLookAt() {
  332. stack = nullptr;
  333. is_setup = false;
  334. bone_idx = -1;
  335. additional_rotation = 0;
  336. enable_constraint = false;
  337. constraint_angle_min = 0;
  338. constraint_angle_max = Math_PI * 2;
  339. constraint_angle_invert = false;
  340. enabled = true;
  341. editor_draw_gizmo = true;
  342. }
  343. SkeletonModification2DLookAt::~SkeletonModification2DLookAt() {
  344. }