ScriptComponentFile.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "ScriptComponentFile.h"
  23. namespace Atomic
  24. {
  25. FieldMap ScriptComponentFile::emptyFieldMap_;
  26. FieldTooltipMap ScriptComponentFile::emptyFieldTooltipMap_;
  27. EnumMap ScriptComponentFile::emptyEnumMap_;
  28. VariantMap ScriptComponentFile::emptyDefaultValueMap_;
  29. ScriptComponentFile::ScriptComponentFile(Context* context) : Resource(context)
  30. {
  31. }
  32. ScriptComponentFile::~ScriptComponentFile()
  33. {
  34. }
  35. void ScriptComponentFile::RegisterObject(Context* context)
  36. {
  37. //context->RegisterFactory<ScriptComponentFile>();
  38. }
  39. void ScriptComponentFile::AddEnum(const String& enumName, const EnumInfo& enumInfo, const String& classname)
  40. {
  41. EnumMap& enums = classEnums_[classname];
  42. Vector<EnumInfo>& enumValues = enums[enumName];
  43. enumValues.Push(enumInfo);
  44. }
  45. void ScriptComponentFile::AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName, bool isArray, unsigned fixedArraySize, const String &classname, const String& tooltip)
  46. {
  47. FieldMap& fields = classFields_[classname];
  48. FieldInfo finfo(fieldName, variantType, resourceTypeName, isArray, fixedArraySize);
  49. fields[fieldName] = finfo;
  50. if (tooltip.Length())
  51. {
  52. FieldTooltipMap& tooltips = classFieldTooltips_[classname];
  53. tooltips[fieldName] = tooltip;
  54. }
  55. }
  56. void ScriptComponentFile::AddDefaultValue(const String& fieldName, const Variant& value, const String& classname)
  57. {
  58. VariantMap& defaultValues = classDefaultFieldValues_[classname];
  59. defaultValues[fieldName] = value;
  60. }
  61. void ScriptComponentFile::Clear()
  62. {
  63. classFields_.Clear();
  64. classFieldTooltips_.Clear();
  65. classDefaultFieldValues_.Clear();
  66. classEnums_.Clear();
  67. }
  68. const FieldMap& ScriptComponentFile::GetFields(const String& classname) const
  69. {
  70. FieldMap* fieldMap = classFields_[classname];
  71. if (fieldMap)
  72. return *fieldMap;
  73. return emptyFieldMap_;
  74. }
  75. const FieldTooltipMap& ScriptComponentFile::GetFieldTooltips(const String& classname) const
  76. {
  77. FieldTooltipMap* fieldTooltipMap = classFieldTooltips_[classname];
  78. if (fieldTooltipMap)
  79. return *fieldTooltipMap;
  80. return emptyFieldTooltipMap_;
  81. }
  82. const VariantMap& ScriptComponentFile::GetDefaultFieldValues(const String& classname) const
  83. {
  84. VariantMap *vmap = classDefaultFieldValues_[classname];
  85. if (vmap)
  86. return *vmap;
  87. return emptyDefaultValueMap_;
  88. }
  89. const EnumMap& ScriptComponentFile::GetEnums(const String& classname) const
  90. {
  91. EnumMap* enumMap = classEnums_[classname];
  92. if (enumMap)
  93. return *enumMap;
  94. return emptyEnumMap_;
  95. }
  96. void ScriptComponentFile::GetDefaultFieldValue(const String& name, Variant& v,const String& classname) const
  97. {
  98. v = Variant::EMPTY;
  99. // first see if we have a default value for this field
  100. const VariantMap* defaultValues = classDefaultFieldValues_[classname];
  101. if (!defaultValues)
  102. return;
  103. const Variant* variant = (*defaultValues)[name];
  104. if (variant)
  105. {
  106. v = *variant;
  107. return;
  108. }
  109. // we don't have a default value, so we need to generate one based on the field type
  110. const FieldMap* fieldMap = classFields_[classname];
  111. if (!fieldMap)
  112. return;
  113. const FieldInfo* finfo = (*fieldMap)[name];
  114. if (!finfo || finfo->isArray_)
  115. return;
  116. VariantType variantType = finfo->variantType_;
  117. if (!variantType)
  118. return;
  119. switch (variantType)
  120. {
  121. case VAR_BOOL:
  122. v = false;
  123. break;
  124. case VAR_INT:
  125. v = 0;
  126. case VAR_DOUBLE:
  127. v = 0;
  128. case VAR_STRING:
  129. v = "";
  130. break;
  131. case VAR_FLOAT:
  132. v = 0.0f;
  133. break;
  134. case VAR_VECTOR2:
  135. v = Vector2::ZERO;
  136. break;
  137. case VAR_VECTOR3:
  138. v = Vector3::ZERO;
  139. break;
  140. case VAR_VECTOR4:
  141. v = Vector4::ZERO;
  142. break;
  143. case VAR_INTVECTOR2:
  144. v = IntVector2::ZERO;
  145. break;
  146. case VAR_COLOR:
  147. v = Color::WHITE;
  148. break;
  149. case VAR_QUATERNION:
  150. v = Quaternion::IDENTITY;
  151. break;
  152. case VAR_RESOURCEREF:
  153. v = Quaternion::IDENTITY;
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. }