ModelInfo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "ModelInfo.h"
  2. using namespace Framework;
  3. ModelInfo::ModelInfo()
  4. : ReferenceCounter(),
  5. modelPath(),
  6. texturePaths(),
  7. transparent(false),
  8. size(1.0f)
  9. {}
  10. ModelInfo::ModelInfo(const char* modelPath,
  11. Framework::RCArray<Framework::Text> texturePaths,
  12. bool transparent,
  13. float size)
  14. : ReferenceCounter()
  15. {
  16. setModelPath(modelPath);
  17. for (Framework::Text* t : texturePaths)
  18. {
  19. addTexturePath(*t);
  20. }
  21. setTransparent(transparent);
  22. setSize(size);
  23. }
  24. void ModelInfo::writeTo(Framework::StreamWriter* zWriter) const
  25. {
  26. char len = (char)modelPath.getLength();
  27. zWriter->schreibe(&len, 1);
  28. zWriter->schreibe(modelPath.getText(), (int)len);
  29. short count = (short)texturePaths.getEintragAnzahl();
  30. zWriter->schreibe((char*)&count, 2);
  31. for (Text* t : texturePaths)
  32. {
  33. len = (char)t->getLength();
  34. zWriter->schreibe(&len, 1);
  35. zWriter->schreibe(t->getText(), (int)len);
  36. }
  37. zWriter->schreibe((char*)&transparent, 1);
  38. zWriter->schreibe((char*)&size, 4);
  39. }
  40. void ModelInfo::setModelPath(Framework::Text path)
  41. {
  42. modelPath = path;
  43. }
  44. void ModelInfo::addTexturePath(Framework::Text path)
  45. {
  46. texturePaths.add(new Framework::Text(path));
  47. }
  48. void ModelInfo::setTransparent(bool transparent)
  49. {
  50. this->transparent = transparent;
  51. }
  52. void ModelInfo::setSize(float size)
  53. {
  54. this->size = size;
  55. }
  56. Framework::Text ModelInfo::getModelPath() const
  57. {
  58. return modelPath;
  59. }
  60. Framework::RCArray<Framework::Text> ModelInfo::getTexturePaths() const
  61. {
  62. return texturePaths;
  63. }
  64. bool ModelInfo::isTransparent() const
  65. {
  66. return transparent;
  67. }
  68. float ModelInfo::getSize() const
  69. {
  70. return size;
  71. }
  72. ModelInfoFactory::ModelInfoFactory()
  73. : ObjectTypeFactory()
  74. {}
  75. ModelInfo* ModelInfoFactory::fromJson(Framework::JSON::JSONObject* zJson) const
  76. {
  77. ModelInfo* result = new ModelInfo();
  78. result->setModelPath(
  79. zJson->asObject()->zValue("modelPath")->asString()->getString());
  80. for (Framework::JSON::JSONValue* v :
  81. *zJson->asObject()->zValue("texturePaths")->asArray())
  82. {
  83. result->addTexturePath(v->asString()->getString());
  84. }
  85. result->setTransparent(
  86. zJson->asObject()->zValue("transparent")->asBool()->getBool());
  87. result->setSize(
  88. (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
  89. return result;
  90. }
  91. Framework::JSON::JSONObject* ModelInfoFactory::toJsonObject(
  92. ModelInfo* zObject) const
  93. {
  94. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  95. Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
  96. for (Framework::Text* t : zObject->texturePaths)
  97. {
  98. arr->addValue(new Framework::JSON::JSONString(t->getText()));
  99. }
  100. result->addValue("texturePaths", arr);
  101. result->addValue(
  102. "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
  103. result->addValue(
  104. "transparent", new Framework::JSON::JSONBool(zObject->transparent));
  105. result->addValue("size", new Framework::JSON::JSONNumber(zObject->size));
  106. return result;
  107. }
  108. JSONObjectValidationBuilder* ModelInfoFactory::addToValidator(
  109. JSONObjectValidationBuilder* builder) const
  110. {
  111. return builder->withRequiredArray("texturePaths")
  112. ->addAcceptedStringInArray()
  113. ->finishString()
  114. ->finishArray()
  115. ->withRequiredString("modelPath")
  116. ->finishString()
  117. ->withRequiredBool("transparent")
  118. ->withDefault(false)
  119. ->finishBool()
  120. ->withRequiredNumber("size")
  121. ->whichIsGreaterThen(0)
  122. ->withDefault(1.0)
  123. ->finishNumber();
  124. }