animation_library.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* animation_library.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "animation_library.h"
  31. bool AnimationLibrary::is_valid_animation_name(const String &p_name) {
  32. return !(p_name.is_empty() || p_name.contains("/") || p_name.contains(":") || p_name.contains(",") || p_name.contains("["));
  33. }
  34. bool AnimationLibrary::is_valid_library_name(const String &p_name) {
  35. return !(p_name.contains("/") || p_name.contains(":") || p_name.contains(",") || p_name.contains("["));
  36. }
  37. String AnimationLibrary::validate_library_name(const String &p_name) {
  38. String name = p_name;
  39. const char *characters = "/:,[";
  40. for (const char *p = characters; *p; p++) {
  41. name = name.replace(String::chr(*p), "_");
  42. }
  43. return name;
  44. }
  45. Error AnimationLibrary::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) {
  46. ERR_FAIL_COND_V_MSG(!is_valid_animation_name(p_name), ERR_INVALID_PARAMETER, "Invalid animation name: '" + String(p_name) + "'.");
  47. ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER);
  48. if (animations.has(p_name)) {
  49. animations.erase(p_name);
  50. emit_signal(SNAME("animation_removed"), p_name);
  51. }
  52. animations.insert(p_name, p_animation);
  53. emit_signal(SNAME("animation_added"), p_name);
  54. notify_property_list_changed();
  55. return OK;
  56. }
  57. void AnimationLibrary::remove_animation(const StringName &p_name) {
  58. ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name));
  59. animations.erase(p_name);
  60. emit_signal(SNAME("animation_removed"), p_name);
  61. notify_property_list_changed();
  62. }
  63. void AnimationLibrary::rename_animation(const StringName &p_name, const StringName &p_new_name) {
  64. ERR_FAIL_COND_MSG(!animations.has(p_name), vformat("Animation not found: %s.", p_name));
  65. ERR_FAIL_COND_MSG(!is_valid_animation_name(p_new_name), "Invalid animation name: '" + String(p_new_name) + "'.");
  66. ERR_FAIL_COND_MSG(animations.has(p_new_name), vformat("Animation name \"%s\" already exists in library.", p_new_name));
  67. animations.insert(p_new_name, animations[p_name]);
  68. animations.erase(p_name);
  69. emit_signal(SNAME("animation_renamed"), p_name, p_new_name);
  70. }
  71. bool AnimationLibrary::has_animation(const StringName &p_name) const {
  72. return animations.has(p_name);
  73. }
  74. Ref<Animation> AnimationLibrary::get_animation(const StringName &p_name) const {
  75. ERR_FAIL_COND_V_MSG(!animations.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name));
  76. return animations[p_name];
  77. }
  78. TypedArray<StringName> AnimationLibrary::_get_animation_list() const {
  79. TypedArray<StringName> ret;
  80. List<StringName> names;
  81. get_animation_list(&names);
  82. for (const StringName &K : names) {
  83. ret.push_back(K);
  84. }
  85. return ret;
  86. }
  87. void AnimationLibrary::get_animation_list(List<StringName> *p_animations) const {
  88. List<StringName> anims;
  89. for (const KeyValue<StringName, Ref<Animation>> &E : animations) {
  90. anims.push_back(E.key);
  91. }
  92. anims.sort_custom<StringName::AlphCompare>();
  93. for (const StringName &E : anims) {
  94. p_animations->push_back(E);
  95. }
  96. }
  97. void AnimationLibrary::_set_data(const Dictionary &p_data) {
  98. animations.clear();
  99. List<Variant> keys;
  100. p_data.get_key_list(&keys);
  101. for (const Variant &K : keys) {
  102. add_animation(K, p_data[K]);
  103. }
  104. }
  105. Dictionary AnimationLibrary::_get_data() const {
  106. Dictionary ret;
  107. for (const KeyValue<StringName, Ref<Animation>> &K : animations) {
  108. ret[K.key] = K.value;
  109. }
  110. return ret;
  111. }
  112. void AnimationLibrary::_bind_methods() {
  113. ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &AnimationLibrary::add_animation);
  114. ClassDB::bind_method(D_METHOD("remove_animation", "name"), &AnimationLibrary::remove_animation);
  115. ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &AnimationLibrary::rename_animation);
  116. ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationLibrary::has_animation);
  117. ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationLibrary::get_animation);
  118. ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationLibrary::_get_animation_list);
  119. ClassDB::bind_method(D_METHOD("_set_data", "data"), &AnimationLibrary::_set_data);
  120. ClassDB::bind_method(D_METHOD("_get_data"), &AnimationLibrary::_get_data);
  121. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "_set_data", "_get_data");
  122. ADD_SIGNAL(MethodInfo("animation_added", PropertyInfo(Variant::STRING_NAME, "name")));
  123. ADD_SIGNAL(MethodInfo("animation_removed", PropertyInfo(Variant::STRING_NAME, "name")));
  124. ADD_SIGNAL(MethodInfo("animation_renamed", PropertyInfo(Variant::STRING_NAME, "name"), PropertyInfo(Variant::STRING_NAME, "to_name")));
  125. }
  126. AnimationLibrary::AnimationLibrary() {
  127. }