ScriptComponentFile.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. EnumMap ScriptComponentFile::emptyEnumMap_;
  27. VariantMap ScriptComponentFile::emptyDefaultValueMap_;
  28. ScriptComponentFile::ScriptComponentFile(Context* context) : Resource(context)
  29. {
  30. }
  31. ScriptComponentFile::~ScriptComponentFile()
  32. {
  33. }
  34. void ScriptComponentFile::RegisterObject(Context* context)
  35. {
  36. //context->RegisterFactory<ScriptComponentFile>();
  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 &classname)
  45. {
  46. FieldMap& fields = classFields_[classname];
  47. fields[fieldName] = variantType;
  48. }
  49. void ScriptComponentFile::AddDefaultValue(const String& fieldName, const Variant& value, const String& classname)
  50. {
  51. VariantMap& defaultValues = classDefaultFieldValues_[classname];
  52. defaultValues[fieldName] = value;
  53. }
  54. void ScriptComponentFile::Clear()
  55. {
  56. classFields_.Clear();
  57. classDefaultFieldValues_.Clear();
  58. classEnums_.Clear();
  59. }
  60. const FieldMap& ScriptComponentFile::GetFields(const String& classname) const
  61. {
  62. FieldMap* fieldMap = classFields_[classname];
  63. if (fieldMap)
  64. return *fieldMap;
  65. return emptyFieldMap_;
  66. }
  67. const VariantMap& ScriptComponentFile::GetDefaultFieldValues(const String& classname) const
  68. {
  69. VariantMap *vmap = classDefaultFieldValues_[classname];
  70. if (vmap)
  71. return *vmap;
  72. return emptyDefaultValueMap_;
  73. }
  74. const EnumMap& ScriptComponentFile::GetEnums(const String& classname) const
  75. {
  76. EnumMap* enumMap = classEnums_[classname];
  77. if (enumMap)
  78. return *enumMap;
  79. return emptyEnumMap_;
  80. }
  81. void ScriptComponentFile::GetDefaultFieldValue(const String& name, Variant& v,const String& classname) const
  82. {
  83. v = Variant::EMPTY;
  84. // first see if we have a default value for this field
  85. const VariantMap* defaultValues = classDefaultFieldValues_[classname];
  86. if (!defaultValues)
  87. return;
  88. const Variant* variant = (*defaultValues)[name];
  89. if (variant)
  90. {
  91. v = *variant;
  92. return;
  93. }
  94. // we don't have a default value, so we need to generate one based on the field type
  95. const FieldMap* fieldMap = classFields_[classname];
  96. if (!fieldMap)
  97. return;
  98. const VariantType* variantType = (*fieldMap)[name];
  99. if (!variantType)
  100. return;
  101. switch (*variantType)
  102. {
  103. case VAR_BOOL:
  104. v = false;
  105. break;
  106. case VAR_STRING:
  107. v = "";
  108. break;
  109. case VAR_FLOAT:
  110. v = 0.0f;
  111. break;
  112. case VAR_VECTOR3:
  113. v = Vector3::ZERO;
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. }