CObject.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if (visible_it->is_boolean () == false)
  39. {
  40. // TODO: SUPPORT CONFIGURATION VALUES ON ATTRIBUTES LIKE VISIBLE
  41. // TODO: FOR NOW JUST DEFAULT TO FALSE
  42. visible = false;
  43. }
  44. else
  45. {
  46. visible = *visible_it;
  47. }
  48. }
  49. auto image_it = data.find ("image");
  50. auto sound_it = data.find ("sound");
  51. auto particle_it = data.find ("particle");
  52. auto text_it = data.find ("text");
  53. CObject* object = nullptr;
  54. if (image_it != data.end () && (*image_it).is_null () == false)
  55. {
  56. object = Objects::CImage::fromJSON (
  57. data,
  58. visible,
  59. *id_it,
  60. *name_it,
  61. WallpaperEngine::Core::ato3vf (*origin_it),
  62. WallpaperEngine::Core::ato3vf (*scale_it),
  63. WallpaperEngine::Core::ato3vf (*angles_it)
  64. );
  65. }
  66. else if (sound_it != data.end ())
  67. {
  68. object = Objects::CSound::fromJSON (
  69. data,
  70. visible,
  71. *id_it,
  72. *name_it,
  73. WallpaperEngine::Core::ato3vf (*origin_it),
  74. WallpaperEngine::Core::ato3vf (*scale_it),
  75. WallpaperEngine::Core::ato3vf (*angles_it)
  76. );
  77. }
  78. else if (particle_it != data.end ())
  79. {
  80. /// TODO: XXXHACK -- TO REMOVE WHEN PARTICLE SUPPORT IS PROPERLY IMPLEMENTED
  81. try
  82. {
  83. object = Objects::CParticle::fromFile (
  84. (*particle_it).get <std::string> ().c_str (),
  85. *id_it,
  86. *name_it,
  87. WallpaperEngine::Core::ato3vf (*origin_it),
  88. WallpaperEngine::Core::ato3vf (*scale_it)
  89. );
  90. }
  91. catch (std::runtime_error ex)
  92. {
  93. return nullptr;
  94. }
  95. }
  96. else if (text_it != data.end ())
  97. {
  98. /// TODO: XXXHACK -- TO REMOVE WHEN TEXT SUPPORT IS IMPLEMENTED
  99. return nullptr;
  100. }
  101. else
  102. {
  103. throw std::runtime_error (std::string ("Unkonwn object type detected ").append (*name_it));
  104. }
  105. if (effects_it != data.end () && (*effects_it).is_array () == true)
  106. {
  107. auto cur = (*effects_it).begin ();
  108. auto end = (*effects_it).end ();
  109. for (; cur != end; cur ++)
  110. {
  111. object->insertEffect (
  112. Objects::CEffect::fromJSON (*cur, object)
  113. );
  114. }
  115. }
  116. if (dependencies_it != data.end () && (*dependencies_it).is_array () == true)
  117. {
  118. auto cur = (*dependencies_it).begin ();
  119. auto end = (*dependencies_it).end ();
  120. for (; cur != end; cur ++)
  121. {
  122. object->insertDependency (*cur);
  123. }
  124. }
  125. return object;
  126. }
  127. const irr::core::vector3df& CObject::getOrigin () const
  128. {
  129. return this->m_origin;
  130. }
  131. const irr::core::vector3df& CObject::getScale () const
  132. {
  133. return this->m_scale;
  134. }
  135. const irr::core::vector3df& CObject::getAngles () const
  136. {
  137. return this->m_angles;
  138. }
  139. const std::string& CObject::getName () const
  140. {
  141. return this->m_name;
  142. }
  143. const std::vector<Objects::CEffect*>& CObject::getEffects () const
  144. {
  145. return this->m_effects;
  146. }
  147. const std::vector<irr::u32>& CObject::getDependencies () const
  148. {
  149. return this->m_dependencies;
  150. }
  151. bool CObject::isVisible ()
  152. {
  153. return this->m_visible;
  154. }
  155. const int CObject::getId () const
  156. {
  157. return this->m_id;
  158. }
  159. void CObject::insertEffect (Objects::CEffect* effect)
  160. {
  161. this->m_effects.push_back (effect);
  162. }
  163. void CObject::insertDependency (irr::u32 dependency)
  164. {
  165. this->m_dependencies.push_back (dependency);
  166. }