ScriptComponentFile.cpp 5.1 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. }
  38. void ScriptComponentFile::AddEnum(const String& enumName, const EnumInfo& enumInfo, const String& classname)
  39. {
  40. EnumMap& enums = classEnums_[classname];
  41. Vector<EnumInfo>& enumValues = enums[enumName];
  42. enumValues.Push(enumInfo);
  43. }
  44. void ScriptComponentFile::AddField(const String& fieldName, VariantType variantType, const String& resourceTypeName, bool isArray, bool isEnum, unsigned fixedArraySize, const String &classname, const String& tooltip)
  45. {
  46. FieldMap& fields = classFields_[classname];
  47. FieldInfo finfo(fieldName, variantType, resourceTypeName, isArray, isEnum, fixedArraySize);
  48. fields[fieldName] = finfo;
  49. if (tooltip.Length())
  50. {
  51. FieldTooltipMap& tooltips = classFieldTooltips_[classname];
  52. tooltips[fieldName] = tooltip;
  53. }
  54. }
  55. void ScriptComponentFile::AddDefaultValue(const String& fieldName, const Variant& value, const String& classname)
  56. {
  57. VariantMap& defaultValues = classDefaultFieldValues_[classname];
  58. defaultValues[fieldName] = value;
  59. }
  60. void ScriptComponentFile::Clear()
  61. {
  62. classFields_.Clear();
  63. classFieldTooltips_.Clear();
  64. classDefaultFieldValues_.Clear();
  65. classEnums_.Clear();
  66. }
  67. const FieldMap& ScriptComponentFile::GetFields(const String& classname) const
  68. {
  69. FieldMap* fieldMap = classFields_[classname];
  70. if (fieldMap)
  71. return *fieldMap;
  72. return emptyFieldMap_;
  73. }
  74. const FieldTooltipMap& ScriptComponentFile::GetFieldTooltips(const String& classname) const
  75. {
  76. FieldTooltipMap* fieldTooltipMap = classFieldTooltips_[classname];
  77. if (fieldTooltipMap)
  78. return *fieldTooltipMap;
  79. return emptyFieldTooltipMap_;
  80. }
  81. const VariantMap& ScriptComponentFile::GetDefaultFieldValues(const String& classname) const
  82. {
  83. VariantMap *vmap = classDefaultFieldValues_[classname];
  84. if (vmap)
  85. return *vmap;
  86. return emptyDefaultValueMap_;
  87. }
  88. const EnumMap& ScriptComponentFile::GetEnums(const String& classname) const
  89. {
  90. EnumMap* enumMap = classEnums_[classname];
  91. if (enumMap)
  92. return *enumMap;
  93. return emptyEnumMap_;
  94. }
  95. void ScriptComponentFile::GetDefaultFieldValue(const String& name, Variant& v,const String& classname) const
  96. {
  97. v = Variant::EMPTY;
  98. // first see if we have a default value for this field
  99. const VariantMap* defaultValues = classDefaultFieldValues_[classname];
  100. if (!defaultValues)
  101. return;
  102. const Variant* variant = (*defaultValues)[name];
  103. if (variant)
  104. {
  105. v = *variant;
  106. return;
  107. }
  108. // we don't have a default value, so we need to generate one based on the field type
  109. const FieldMap* fieldMap = classFields_[classname];
  110. if (!fieldMap)
  111. return;
  112. const FieldInfo* finfo = (*fieldMap)[name];
  113. if (!finfo || finfo->isArray_)
  114. return;
  115. VariantType variantType = finfo->variantType_;
  116. if (!variantType)
  117. return;
  118. switch (variantType)
  119. {
  120. case VAR_BOOL:
  121. v = false;
  122. break;
  123. case VAR_INT:
  124. v = 0;
  125. case VAR_DOUBLE:
  126. v = 0;
  127. case VAR_STRING:
  128. v = "";
  129. break;
  130. case VAR_FLOAT:
  131. v = 0.0f;
  132. break;
  133. case VAR_VECTOR2:
  134. v = Vector2::ZERO;
  135. break;
  136. case VAR_VECTOR3:
  137. v = Vector3::ZERO;
  138. break;
  139. case VAR_VECTOR4:
  140. v = Vector4::ZERO;
  141. break;
  142. case VAR_INTVECTOR2:
  143. v = IntVector2::ZERO;
  144. break;
  145. case VAR_COLOR:
  146. v = Color::WHITE;
  147. break;
  148. case VAR_QUATERNION:
  149. v = Quaternion::IDENTITY;
  150. break;
  151. case VAR_RESOURCEREF:
  152. v = Quaternion::IDENTITY;
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. }