resource_preloader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* resource_preloader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "resource_preloader.h"
  30. void ResourcePreloader::_set_resources(const Array& p_data) {
  31. resources.clear();
  32. ERR_FAIL_COND(p_data.size()!=2);
  33. DVector<String> names=p_data[0];
  34. Array resdata=p_data[1];
  35. ERR_FAIL_COND(names.size()!=resdata.size());
  36. for(int i=0;i<resdata.size();i++) {
  37. String name=names[i];
  38. RES resource = resdata[i];
  39. ERR_CONTINUE( !resource.is_valid() );
  40. resources[name]=resource;
  41. //add_resource(name,resource);
  42. }
  43. }
  44. Array ResourcePreloader::_get_resources() const {
  45. DVector<String> names;
  46. Array arr;
  47. arr.resize(resources.size());
  48. names.resize(resources.size());
  49. Set<String> sorted_names;
  50. for(Map<StringName,RES >::Element *E=resources.front();E;E=E->next()) {
  51. sorted_names.insert(E->key());
  52. }
  53. int i=0;
  54. for(Set<String>::Element *E=sorted_names.front();E;E=E->next()) {
  55. names.set(i,E->get());
  56. arr[i]=resources[E->get()];
  57. i++;
  58. }
  59. Array res;
  60. res.push_back(names);
  61. res.push_back(arr);
  62. return res;
  63. }
  64. void ResourcePreloader::add_resource(const StringName& p_name,const RES& p_resource) {
  65. ERR_FAIL_COND(p_resource.is_null());
  66. if (resources.has(p_name)) {
  67. StringName new_name;
  68. int idx=2;
  69. while(true) {
  70. new_name=p_name.operator String()+" "+itos(idx);
  71. if (resources.has(new_name)) {
  72. idx++;
  73. continue;
  74. }
  75. break;
  76. }
  77. add_resource(new_name,p_resource);
  78. } else {
  79. resources[p_name]=p_resource;
  80. }
  81. }
  82. void ResourcePreloader::remove_resource(const StringName& p_name) {
  83. ERR_FAIL_COND( !resources.has(p_name) );
  84. resources.erase(p_name);
  85. }
  86. void ResourcePreloader::rename_resource(const StringName& p_from_name,const StringName& p_to_name) {
  87. ERR_FAIL_COND( !resources.has(p_from_name) );
  88. RES res = resources[p_from_name];
  89. resources.erase(p_from_name);
  90. add_resource(p_to_name,res);
  91. }
  92. bool ResourcePreloader::has_resource(const StringName& p_name) const {
  93. return resources.has(p_name);
  94. }
  95. RES ResourcePreloader::get_resource(const StringName& p_name) const {
  96. ERR_FAIL_COND_V(!resources.has(p_name),RES());
  97. return resources[p_name];
  98. }
  99. DVector<String> ResourcePreloader::_get_resource_list() const {
  100. DVector<String> res;
  101. res.resize(resources.size());
  102. int i=0;
  103. for(Map<StringName,RES >::Element *E=resources.front();E;E=E->next(),i++) {
  104. res.set(i,E->key());
  105. }
  106. return res;
  107. }
  108. void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
  109. for(Map<StringName,RES >::Element *E=resources.front();E;E=E->next()) {
  110. p_list->push_back(E->key());
  111. }
  112. }
  113. void ResourcePreloader::_bind_methods() {
  114. ObjectTypeDB::bind_method(_MD("_set_resources"),&ResourcePreloader::_set_resources);
  115. ObjectTypeDB::bind_method(_MD("_get_resources"),&ResourcePreloader::_get_resources);
  116. ObjectTypeDB::bind_method(_MD("add_resource","name","resource"),&ResourcePreloader::add_resource);
  117. ObjectTypeDB::bind_method(_MD("remove_resource","name"),&ResourcePreloader::remove_resource);
  118. ObjectTypeDB::bind_method(_MD("rename_resource","name","newname"),&ResourcePreloader::rename_resource);
  119. ObjectTypeDB::bind_method(_MD("has_resource","name"),&ResourcePreloader::has_resource);
  120. ObjectTypeDB::bind_method(_MD("get_resource","name"),&ResourcePreloader::get_resource);
  121. ObjectTypeDB::bind_method(_MD("get_resource_list"),&ResourcePreloader::_get_resource_list);
  122. ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"resources",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_resources"), _SCS("_get_resources"));
  123. }
  124. ResourcePreloader::ResourcePreloader()
  125. {
  126. }