ModelInfo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. : TypeFactory()
  74. {}
  75. ModelInfo* ModelInfoFactory::createValue(
  76. Framework::JSON::JSONObject* zJson) const
  77. {
  78. return new ModelInfo();
  79. }
  80. void ModelInfoFactory::fromJson(
  81. ModelInfo* zResult, Framework::JSON::JSONObject* zJson) const
  82. {
  83. zResult->setModelPath(
  84. zJson->asObject()->zValue("modelPath")->asString()->getString());
  85. for (Framework::JSON::JSONValue* v :
  86. *zJson->asObject()->zValue("texturePaths")->asArray())
  87. {
  88. zResult->addTexturePath(v->asString()->getString());
  89. }
  90. zResult->setTransparent(
  91. zJson->asObject()->zValue("transparent")->asBool()->getBool());
  92. zResult->setSize(
  93. (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
  94. }
  95. void ModelInfoFactory::toJson(
  96. ModelInfo* zObject, Framework::JSON::JSONObject* zResult) const
  97. {
  98. Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
  99. for (Framework::Text* t : zObject->texturePaths)
  100. {
  101. arr->addValue(new Framework::JSON::JSONString(t->getText()));
  102. }
  103. zResult->addValue("texturePaths", arr);
  104. zResult->addValue(
  105. "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
  106. zResult->addValue(
  107. "transparent", new Framework::JSON::JSONBool(zObject->transparent));
  108. zResult->addValue("size", new Framework::JSON::JSONNumber(zObject->size));
  109. }
  110. JSONObjectValidationBuilder* ModelInfoFactory::addToValidator(
  111. JSONObjectValidationBuilder* builder) const
  112. {
  113. return builder->withRequiredArray("texturePaths")
  114. ->addAcceptedStringInArray()
  115. ->finishString()
  116. ->finishArray()
  117. ->withRequiredString("modelPath")
  118. ->finishString()
  119. ->withRequiredBool("transparent")
  120. ->withDefault(false)
  121. ->finishBool()
  122. ->withRequiredNumber("size")
  123. ->whichIsGreaterThen(0)
  124. ->withDefault(1.0)
  125. ->finishNumber();
  126. }