ModelInfo.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "ModelInfo.h"
  2. using namespace Framework;
  3. ModelInfo::ModelInfo(const char* modelPath,
  4. Framework::RCArray<Framework::Text> texturePaths,
  5. bool transparent, float size)
  6. : ReferenceCounter(),
  7. modelPath(modelPath),
  8. texturePaths(texturePaths),
  9. transparent(transparent),
  10. size(size)
  11. {}
  12. void ModelInfo::writeTo(Framework::StreamWriter* zWriter) const
  13. {
  14. char len = (char)modelPath.getLength();
  15. zWriter->schreibe(&len, 1);
  16. zWriter->schreibe(modelPath.getText(), (int)len);
  17. short count = (short)texturePaths.getEintragAnzahl();
  18. zWriter->schreibe((char*)&count, 2);
  19. for (Text* t : texturePaths)
  20. {
  21. len = (char)t->getLength();
  22. zWriter->schreibe(&len, 1);
  23. zWriter->schreibe(t->getText(), (int)len);
  24. }
  25. zWriter->schreibe((char*)&transparent, 1);
  26. zWriter->schreibe((char*)&size, 4);
  27. }
  28. Framework::Text ModelInfo::getModelPath() const
  29. {
  30. return modelPath;
  31. }
  32. Framework::RCArray<Framework::Text> ModelInfo::getTexturePaths() const
  33. {
  34. return texturePaths;
  35. }
  36. bool ModelInfo::isTransparent() const
  37. {
  38. return transparent;
  39. }
  40. float ModelInfo::getSize() const
  41. {
  42. return size;
  43. }
  44. ModelInfoFactory::ModelInfoFactory()
  45. : TypeFactory()
  46. {}
  47. ModelInfo* ModelInfoFactory::fromJson(Framework::JSON::JSONValue* zJson) const
  48. {
  49. Framework::RCArray<Framework::Text> texturePaths;
  50. for (Framework::JSON::JSONValue* v :
  51. *zJson->asObject()->zValue("texturePaths")->asArray())
  52. {
  53. texturePaths.add(new Framework::Text(v->asString()->getString()));
  54. }
  55. return new ModelInfo(
  56. zJson->asObject()->zValue("modelPath")->asString()->getString(),
  57. texturePaths,
  58. zJson->asObject()->zValue("transparent")->asBool()->getBool(),
  59. (float)zJson->asObject()->zValue("size")->asNumber()->getNumber());
  60. }
  61. Framework::JSON::JSONValue* ModelInfoFactory::toJson(ModelInfo* zObject) const
  62. {
  63. Framework::JSON::JSONObject* obj = new Framework::JSON::JSONObject();
  64. Framework::JSON::JSONArray* arr = new Framework::JSON::JSONArray();
  65. for (Framework::Text* t : zObject->texturePaths)
  66. {
  67. arr->addValue(new Framework::JSON::JSONString(t->getText()));
  68. }
  69. obj->addValue("texturePaths", arr);
  70. obj->addValue(
  71. "modelPath", new Framework::JSON::JSONString(zObject->modelPath));
  72. obj->addValue(
  73. "transparent", new Framework::JSON::JSONBool(zObject->transparent));
  74. obj->addValue(
  75. "size", new Framework::JSON::JSONNumber(zObject->size));
  76. return obj;
  77. }
  78. Framework::JSON::Validator::JSONValidator*
  79. ModelInfoFactory::getValidator() const
  80. {
  81. return Framework::JSON::Validator::JSONValidator::buildForObject()
  82. ->withRequiredArray("texturePaths")
  83. ->addAcceptedStringInArray()
  84. ->finishString()
  85. ->finishArray()
  86. ->withRequiredString("modelPath")
  87. ->finishString()
  88. ->withRequiredBool("transparent")
  89. ->withDefault(false)
  90. ->finishBool()
  91. ->withRequiredNumber("size")
  92. ->whichIsGreaterThen(0)
  93. ->withDefault(1.0)
  94. ->finishNumber()
  95. ->finishObject();
  96. }