CObject.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "CObject.h"
  2. #include <utility>
  3. #include "WallpaperEngine/Core/Objects/CImage.h"
  4. #include "WallpaperEngine/Core/Objects/CSound.h"
  5. #include "WallpaperEngine/Core/Objects/CParticle.h"
  6. using namespace WallpaperEngine::Core;
  7. CObject::CObject (
  8. bool visible,
  9. irr::u32 id,
  10. std::string name,
  11. std::string type,
  12. const irr::core::vector3df& origin,
  13. const irr::core::vector3df& scale,
  14. const irr::core::vector3df& angles) :
  15. m_visible (visible),
  16. m_id (id),
  17. m_name (std::move(name)),
  18. m_type (type),
  19. m_origin (origin),
  20. m_scale (scale),
  21. m_angles (angles)
  22. {
  23. }
  24. CObject* CObject::fromJSON (json data)
  25. {
  26. auto id_it = jsonFindRequired (data, "id", "Objects must have id");
  27. auto visible_it = data.find ("visible");
  28. auto origin_it = jsonFindRequired (data, "origin", "Objects must have origin point");
  29. auto scale_it = jsonFindRequired (data, "scale", "Objects must have scale");
  30. auto angles_it = jsonFindRequired (data, "angles", "Objects must have angles");
  31. auto name_it = jsonFindRequired (data, "name", "Objects must have name");
  32. auto effects_it = data.find ("effects");
  33. auto dependencies_it = data.find ("dependencies");
  34. bool visible = true;
  35. // visibility is optional
  36. if (visible_it != data.end ())
  37. {
  38. visible = *visible_it;
  39. }
  40. auto image_it = data.find ("image");
  41. auto sound_it = data.find ("sound");
  42. auto particle_it = data.find ("particle");
  43. CObject* object = nullptr;
  44. if (image_it != data.end () && (*image_it).is_null () == false)
  45. {
  46. object = Objects::CImage::fromJSON (
  47. data,
  48. visible,
  49. *id_it,
  50. *name_it,
  51. WallpaperEngine::Core::ato3vf (*origin_it),
  52. WallpaperEngine::Core::ato3vf (*scale_it),
  53. WallpaperEngine::Core::ato3vf (*angles_it)
  54. );
  55. }
  56. else if (sound_it != data.end ())
  57. {
  58. object = Objects::CSound::fromJSON (
  59. data,
  60. visible,
  61. *id_it,
  62. *name_it,
  63. WallpaperEngine::Core::ato3vf (*origin_it),
  64. WallpaperEngine::Core::ato3vf (*scale_it),
  65. WallpaperEngine::Core::ato3vf (*angles_it)
  66. );
  67. }
  68. else if (particle_it != data.end ())
  69. {
  70. object = Objects::CParticle::fromFile (
  71. (*particle_it).get <std::string> ().c_str (),
  72. *id_it,
  73. *name_it,
  74. WallpaperEngine::Core::ato3vf (*origin_it),
  75. WallpaperEngine::Core::ato3vf (*scale_it)
  76. );
  77. }
  78. else
  79. {
  80. throw std::runtime_error ("Unkonwn object type detected");
  81. }
  82. if (effects_it != data.end () && (*effects_it).is_array () == true)
  83. {
  84. auto cur = (*effects_it).begin ();
  85. auto end = (*effects_it).end ();
  86. for (; cur != end; cur ++)
  87. {
  88. object->insertEffect (
  89. Objects::CEffect::fromJSON (*cur, object)
  90. );
  91. }
  92. }
  93. if (dependencies_it != data.end () && (*dependencies_it).is_array () == true)
  94. {
  95. auto cur = (*dependencies_it).begin ();
  96. auto end = (*dependencies_it).end ();
  97. for (; cur != end; cur ++)
  98. {
  99. object->insertDependency (*cur);
  100. }
  101. }
  102. return object;
  103. }
  104. const irr::core::vector3df& CObject::getOrigin () const
  105. {
  106. return this->m_origin;
  107. }
  108. const irr::core::vector3df& CObject::getScale () const
  109. {
  110. return this->m_scale;
  111. }
  112. const irr::core::vector3df& CObject::getAngles () const
  113. {
  114. return this->m_angles;
  115. }
  116. const std::string& CObject::getName () const
  117. {
  118. return this->m_name;
  119. }
  120. const std::vector<Objects::CEffect*>& CObject::getEffects () const
  121. {
  122. return this->m_effects;
  123. }
  124. const std::vector<irr::u32>& CObject::getDependencies () const
  125. {
  126. return this->m_dependencies;
  127. }
  128. bool CObject::isVisible ()
  129. {
  130. return this->m_visible;
  131. }
  132. const int CObject::getId () const
  133. {
  134. return this->m_id;
  135. }
  136. void CObject::insertEffect (Objects::CEffect* effect)
  137. {
  138. this->m_effects.push_back (effect);
  139. }
  140. void CObject::insertDependency (irr::u32 dependency)
  141. {
  142. this->m_dependencies.push_back (dependency);
  143. }