ScriptComponentFile.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "ScriptComponentFile.h"
  2. namespace Atomic
  3. {
  4. FieldMap ScriptComponentFile::emptyFieldMap_;
  5. EnumMap ScriptComponentFile::emptyEnumMap_;
  6. VariantMap ScriptComponentFile::emptyDefaultValueMap_;
  7. ScriptComponentFile::ScriptComponentFile(Context* context) : Resource(context)
  8. {
  9. }
  10. ScriptComponentFile::~ScriptComponentFile()
  11. {
  12. }
  13. void ScriptComponentFile::RegisterObject(Context* context)
  14. {
  15. //context->RegisterFactory<ScriptComponentFile>();
  16. }
  17. void ScriptComponentFile::AddEnum(const String& enumName, const EnumInfo& enumInfo, const String& classname)
  18. {
  19. EnumMap& enums = classEnums_[classname];
  20. Vector<EnumInfo>& enumValues = enums[enumName];
  21. enumValues.Push(enumInfo);
  22. }
  23. void ScriptComponentFile::AddField(const String& fieldName, VariantType variantType, const String &classname)
  24. {
  25. FieldMap& fields = classFields_[classname];
  26. fields[fieldName] = variantType;
  27. }
  28. void ScriptComponentFile::AddDefaultValue(const String& fieldName, const Variant& value, const String& classname)
  29. {
  30. VariantMap& defaultValues = classDefaultFieldValues_[classname];
  31. defaultValues[fieldName] = value;
  32. }
  33. void ScriptComponentFile::Clear()
  34. {
  35. classFields_.Clear();
  36. classDefaultFieldValues_.Clear();
  37. classEnums_.Clear();
  38. }
  39. const FieldMap& ScriptComponentFile::GetFields(const String& classname) const
  40. {
  41. FieldMap* fieldMap = classFields_[classname];
  42. if (fieldMap)
  43. return *fieldMap;
  44. return emptyFieldMap_;
  45. }
  46. const VariantMap& ScriptComponentFile::GetDefaultFieldValues(const String& classname) const
  47. {
  48. VariantMap *vmap = classDefaultFieldValues_[classname];
  49. if (vmap)
  50. return *vmap;
  51. return emptyDefaultValueMap_;
  52. }
  53. const EnumMap& ScriptComponentFile::GetEnums(const String& classname) const
  54. {
  55. EnumMap* enumMap = classEnums_[classname];
  56. if (enumMap)
  57. return *enumMap;
  58. return emptyEnumMap_;
  59. }
  60. void ScriptComponentFile::GetDefaultFieldValue(const String& name, Variant& v,const String& classname) const
  61. {
  62. v = Variant::EMPTY;
  63. // first see if we have a default value for this field
  64. const VariantMap* defaultValues = classDefaultFieldValues_[classname];
  65. if (!defaultValues)
  66. return;
  67. const Variant* variant = (*defaultValues)[name];
  68. if (variant)
  69. {
  70. v = *variant;
  71. return;
  72. }
  73. // we don't have a default value, so we need to generate one based on the field type
  74. const FieldMap* fieldMap = classFields_[classname];
  75. if (!fieldMap)
  76. return;
  77. const VariantType* variantType = (*fieldMap)[name];
  78. if (!variantType)
  79. return;
  80. switch (*variantType)
  81. {
  82. case VAR_BOOL:
  83. v = false;
  84. break;
  85. case VAR_STRING:
  86. v = "";
  87. break;
  88. case VAR_FLOAT:
  89. v = 0.0f;
  90. break;
  91. case VAR_VECTOR3:
  92. v = Vector3::ZERO;
  93. break;
  94. default:
  95. break;
  96. }
  97. }
  98. }