CObject.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "CObject.h"
  2. #include <utility>
  3. #include "WallpaperEngine/Core/CProject.h"
  4. #include "WallpaperEngine/Core/Objects/CImage.h"
  5. #include "WallpaperEngine/Core/Objects/CParticle.h"
  6. #include "WallpaperEngine/Core/Objects/CSound.h"
  7. #include "WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h"
  8. #include "WallpaperEngine/Core/Wallpapers/CScene.h"
  9. #include "WallpaperEngine/Assets/CContainer.h"
  10. #include "WallpaperEngine/Logging/CLog.h"
  11. using namespace WallpaperEngine::Core;
  12. using namespace WallpaperEngine::Assets;
  13. using namespace WallpaperEngine::Core::UserSettings;
  14. CObject::CObject (
  15. std::shared_ptr <const Core::CProject> project, const CUserSettingBoolean* visible, int id, std::string name,
  16. const CUserSettingVector3* origin, const CUserSettingVector3* scale, const CUserSettingVector3* angles,
  17. std::vector<int> dependencies
  18. ) :
  19. m_visible (visible),
  20. m_id (id),
  21. m_name (std::move(name)),
  22. m_origin (origin),
  23. m_scale (scale),
  24. m_angles (angles),
  25. m_project (project),
  26. m_dependencies (std::move(dependencies)) {}
  27. const CObject* CObject::fromJSON (
  28. const json& data, std::shared_ptr <const Core::CProject> project, const std::shared_ptr<const CContainer>& container
  29. ) {
  30. const auto id = jsonFindRequired <int> (data, "id", "Objects must have id");
  31. const auto visible = jsonFindUserConfig<CUserSettingBoolean> (data, *project, "visible", true);
  32. const auto origin = jsonFindUserConfig<CUserSettingVector3> (data, *project, "origin", {0, 0, 0});
  33. const auto scale = jsonFindUserConfig<CUserSettingVector3> (data, *project, "scale", {1, 1, 1});
  34. const auto angles_val = jsonFindUserConfig<CUserSettingVector3> (data, *project, "angles", glm::vec3 (0, 0, 0));
  35. const auto name = jsonFindRequired <std::string> (data, "name", "Objects must have name");
  36. const auto effects_it = data.find ("effects");
  37. const auto dependencies_it = data.find ("dependencies");
  38. const auto image_it = data.find ("image");
  39. const auto sound_it = data.find ("sound");
  40. const auto particle_it = data.find ("particle");
  41. const auto text_it = data.find ("text");
  42. const auto light_it = data.find ("light");
  43. std::vector<int> dependencies;
  44. std::vector<const Objects::CEffect*> effects;
  45. const CObject* object;
  46. if (dependencies_it != data.end () && dependencies_it->is_array ())
  47. for (const auto& cur : *dependencies_it)
  48. dependencies.push_back (cur);
  49. if (image_it != data.end () && !image_it->is_null ()) {
  50. object = Objects::CImage::fromJSON (
  51. project, data, container, visible, id, name, origin, scale, angles_val, effects_it, dependencies);
  52. } else if (sound_it != data.end () && !sound_it->is_null ()) {
  53. object = Objects::CSound::fromJSON (project, data, visible, id, name, origin, scale, angles_val, dependencies);
  54. } else if (particle_it != data.end () && !particle_it->is_null ()) {
  55. /// TODO: XXXHACK -- TO REMOVE WHEN PARTICLE SUPPORT IS PROPERLY IMPLEMENTED
  56. try {
  57. object = Objects::CParticle::fromFile (
  58. project, particle_it->get<std::string> (), container, visible, id, name, origin, angles_val, scale, dependencies);
  59. } catch (std::runtime_error&) {
  60. return nullptr;
  61. }
  62. } else if (text_it != data.end () && !text_it->is_null ()) {
  63. /// TODO: XXXHACK -- TO REMOVE WHEN TEXT SUPPORT IS IMPLEMENTED
  64. return nullptr;
  65. } else if (light_it != data.end () && !light_it->is_null ()) {
  66. /// TODO: XXXHACK -- TO REMOVE WHEN LIGHT SUPPORT IS IMPLEMENTED
  67. return nullptr;
  68. } else {
  69. sLog.exception ("Unknown object type detected: ", name);
  70. }
  71. return object;
  72. }
  73. const glm::vec3& CObject::getOrigin () const {
  74. return this->m_origin->getVec3 ();
  75. }
  76. const glm::vec3& CObject::getScale () const {
  77. return this->m_scale->getVec3 ();
  78. }
  79. const glm::vec3& CObject::getAngles () const {
  80. return this->m_angles->getVec3 ();
  81. }
  82. const std::string& CObject::getName () const {
  83. return this->m_name;
  84. }
  85. const std::vector<int>& CObject::getDependencies () const {
  86. return this->m_dependencies;
  87. }
  88. bool CObject::isVisible () const {
  89. return this->m_visible->getBool ();
  90. }
  91. std::shared_ptr <const CProject> CObject::getProject () const {
  92. return this->m_project;
  93. }
  94. int CObject::getId () const {
  95. return this->m_id;
  96. }