gltf_template_convert.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**************************************************************************/
  2. /* gltf_template_convert.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. #ifndef GLTF_TEMPLATE_CONVERT_H
  31. #define GLTF_TEMPLATE_CONVERT_H
  32. #include "core/templates/hash_set.h"
  33. #include "core/variant/array.h"
  34. #include "core/variant/dictionary.h"
  35. #include "core/variant/typed_array.h"
  36. namespace GLTFTemplateConvert {
  37. template <typename T>
  38. static Array to_array(const Vector<T> &p_inp) {
  39. Array ret;
  40. for (int i = 0; i < p_inp.size(); i++) {
  41. ret.push_back(p_inp[i]);
  42. }
  43. return ret;
  44. }
  45. template <typename T>
  46. static TypedArray<T> to_array(const HashSet<T> &p_inp) {
  47. TypedArray<T> ret;
  48. typename HashSet<T>::Iterator elem = p_inp.begin();
  49. while (elem) {
  50. ret.push_back(*elem);
  51. ++elem;
  52. }
  53. return ret;
  54. }
  55. template <typename T>
  56. static void set_from_array(Vector<T> &r_out, const Array &p_inp) {
  57. r_out.clear();
  58. for (int i = 0; i < p_inp.size(); i++) {
  59. r_out.push_back(p_inp[i]);
  60. }
  61. }
  62. template <typename T>
  63. static void set_from_array(HashSet<T> &r_out, const TypedArray<T> &p_inp) {
  64. r_out.clear();
  65. for (int i = 0; i < p_inp.size(); i++) {
  66. r_out.insert(p_inp[i]);
  67. }
  68. }
  69. template <typename K, typename V>
  70. static Dictionary to_dictionary(const HashMap<K, V> &p_inp) {
  71. Dictionary ret;
  72. for (const KeyValue<K, V> &E : p_inp) {
  73. ret[E.key] = E.value;
  74. }
  75. return ret;
  76. }
  77. template <typename K, typename V>
  78. static void set_from_dictionary(HashMap<K, V> &r_out, const Dictionary &p_inp) {
  79. r_out.clear();
  80. Array keys = p_inp.keys();
  81. for (int i = 0; i < keys.size(); i++) {
  82. r_out[keys[i]] = p_inp[keys[i]];
  83. }
  84. }
  85. } //namespace GLTFTemplateConvert
  86. #endif // GLTF_TEMPLATE_CONVERT_H