CObject.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "CObject.h"
  2. #include <utility>
  3. #include "Objects/CImage.h"
  4. #include "Objects/CSound.h"
  5. #include "Objects/Particles/CParticle.h"
  6. #include "Core.h"
  7. using namespace WallpaperEngine::Core;
  8. CObject::CObject (
  9. bool visible,
  10. irr::u32 id,
  11. std::string name,
  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_origin (origin),
  19. m_scale (scale),
  20. m_angles (angles)
  21. {
  22. }
  23. CObject* CObject::fromJSON (json data)
  24. {
  25. json::const_iterator id_it = data.find ("id");
  26. json::const_iterator visible_it = data.find ("visible");
  27. json::const_iterator origin_it = data.find ("origin");
  28. json::const_iterator scale_it = data.find ("scale");
  29. json::const_iterator size_it = data.find ("size");
  30. json::const_iterator angles_it = data.find ("angles");
  31. json::const_iterator name_it = data.find ("name");
  32. json::const_iterator effects_it = data.find ("effects");
  33. bool visible = true;
  34. if (id_it == data.end ())
  35. {
  36. throw std::runtime_error ("Objects must have id");
  37. }
  38. if (origin_it == data.end ())
  39. {
  40. throw std::runtime_error ("Objects must have origin point");
  41. }
  42. if (scale_it == data.end ())
  43. {
  44. throw std::runtime_error ("Objects must have scale");
  45. }
  46. if (angles_it == data.end ())
  47. {
  48. throw std::runtime_error ("Objects must have angles");
  49. }
  50. if (name_it == data.end ())
  51. {
  52. throw std::runtime_error ("Objects must have name");
  53. }
  54. // visibility is optional
  55. if (visible_it != data.end ())
  56. {
  57. visible = *visible_it;
  58. }
  59. json::const_iterator image_it = data.find ("image");
  60. json::const_iterator sound_it = data.find ("sound");
  61. json::const_iterator particle_it = data.find ("particle");
  62. CObject* object = nullptr;
  63. if (image_it != data.end () && (*image_it).is_null () == false)
  64. {
  65. object = Objects::CImage::fromJSON (
  66. data,
  67. visible,
  68. *id_it,
  69. *name_it,
  70. WallpaperEngine::Core::ato3vf (*origin_it),
  71. WallpaperEngine::Core::ato3vf (*scale_it),
  72. WallpaperEngine::Core::ato3vf (*angles_it)
  73. );
  74. }
  75. else if (sound_it != data.end ())
  76. {
  77. object = Objects::CSound::fromJSON (
  78. data,
  79. visible,
  80. *id_it,
  81. *name_it,
  82. WallpaperEngine::Core::ato3vf (*origin_it),
  83. WallpaperEngine::Core::ato3vf (*scale_it),
  84. WallpaperEngine::Core::ato3vf (*angles_it)
  85. );
  86. }
  87. else if (particle_it != data.end ())
  88. {
  89. object = Objects::Particles::CParticle::fromFile (
  90. (*particle_it).get <std::string> ().c_str (),
  91. *id_it,
  92. *name_it,
  93. WallpaperEngine::Core::ato3vf (*origin_it),
  94. WallpaperEngine::Core::ato3vf (*scale_it)
  95. );
  96. }
  97. else
  98. {
  99. throw std::runtime_error ("Unkonwn object type detected");
  100. }
  101. if (effects_it != data.end () && (*effects_it).is_array () == true && object != nullptr)
  102. {
  103. json::const_iterator cur = (*effects_it).begin ();
  104. json::const_iterator end = (*effects_it).end ();
  105. for (; cur != end; cur ++)
  106. {
  107. object->insertEffect (
  108. Objects::CEffect::fromJSON (*cur)
  109. );
  110. }
  111. }
  112. return object;
  113. }
  114. std::vector<Objects::CEffect*>* CObject::getEffects ()
  115. {
  116. return &this->m_effects;
  117. }
  118. void CObject::insertEffect (Objects::CEffect* effect)
  119. {
  120. this->m_effects.push_back (effect);
  121. }