Component.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Component.h"
  25. #include "Context.h"
  26. #include "Node.h"
  27. #include "XMLElement.h"
  28. #include "DebugNew.h"
  29. OBJECTTYPESTATIC(Component);
  30. Component::Component(Context* context) :
  31. Serializable(context),
  32. id_(0),
  33. node_(0)
  34. {
  35. }
  36. Component::~Component()
  37. {
  38. }
  39. bool Component::Save(Serializer& dest)
  40. {
  41. // Write type and ID
  42. if (!dest.WriteShortStringHash(GetType()))
  43. return false;
  44. if (!dest.WriteUInt(id_))
  45. return false;
  46. // Write attributes
  47. return Serializable::Save(dest);
  48. }
  49. bool Component::SaveXML(XMLElement& dest)
  50. {
  51. // Write type and ID
  52. if (!dest.SetString("type", GetTypeName()))
  53. return false;
  54. if (!dest.SetInt("id", id_))
  55. return false;
  56. // Write attributes
  57. return Serializable::SaveXML(dest);
  58. }
  59. void Component::Remove()
  60. {
  61. if (node_)
  62. node_->RemoveComponent(this);
  63. }
  64. const Matrix3x4& Component::GetWorldTransform() const
  65. {
  66. return node_ ? node_->GetWorldTransform() : Matrix3x4::IDENTITY;
  67. }
  68. void Component::SetID(unsigned id)
  69. {
  70. id_ = id;
  71. }
  72. void Component::SetNode(Node* node)
  73. {
  74. node_ = node;
  75. OnNodeSet(node_);
  76. }
  77. Component* Component::GetComponent(ShortStringHash type) const
  78. {
  79. return node_ ? node_->GetComponent(type) : 0;
  80. }
  81. void Component::GetComponents(PODVector<Component*>& dest, ShortStringHash type) const
  82. {
  83. if (node_)
  84. node_->GetComponents(dest, type);
  85. else
  86. dest.Clear();
  87. }