Component.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "ReplicationState.h"
  27. #include "Scene.h"
  28. #include "XMLElement.h"
  29. #include "DebugNew.h"
  30. OBJECTTYPESTATIC(Component);
  31. Component::Component(Context* context) :
  32. Serializable(context),
  33. node_(0),
  34. id_(0),
  35. networkUpdate_(false)
  36. {
  37. }
  38. Component::~Component()
  39. {
  40. }
  41. void Component::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  42. {
  43. Serializable::OnSetAttribute(attr, src);
  44. MarkNetworkUpdate();
  45. }
  46. bool Component::Save(Serializer& dest)
  47. {
  48. // Write type and ID
  49. if (!dest.WriteShortStringHash(GetType()))
  50. return false;
  51. if (!dest.WriteUInt(id_))
  52. return false;
  53. // Write attributes
  54. return Serializable::Save(dest);
  55. }
  56. bool Component::SaveXML(XMLElement& dest)
  57. {
  58. // Write type and ID
  59. if (!dest.SetString("type", GetTypeName()))
  60. return false;
  61. if (!dest.SetInt("id", id_))
  62. return false;
  63. // Write attributes
  64. return Serializable::SaveXML(dest);
  65. }
  66. void Component::Remove()
  67. {
  68. if (node_)
  69. node_->RemoveComponent(this);
  70. }
  71. Scene* Component::GetScene() const
  72. {
  73. return node_ ? node_->GetScene() : 0;
  74. }
  75. void Component::AddReplicationState(ComponentReplicationState* state)
  76. {
  77. if (!networkState_)
  78. networkState_ = new NetworkState();
  79. networkState_->replicationStates_.Push(state);
  80. }
  81. void Component::PrepareNetworkUpdate()
  82. {
  83. const Vector<AttributeInfo>* attributes = GetNetworkAttributes();
  84. if (!attributes)
  85. return;
  86. unsigned numAttributes = attributes->Size();
  87. if (!networkState_)
  88. networkState_ = new NetworkState();
  89. if (networkState_->attributes_.Size() != numAttributes)
  90. {
  91. networkState_->attributes_.Resize(numAttributes);
  92. networkState_->previousAttributes_.Resize(numAttributes);
  93. // Copy the default attribute values to the previous state as a starting point
  94. for (unsigned i = 0; i < numAttributes; ++i)
  95. networkState_->previousAttributes_[i] = attributes->At(i).defaultValue_;
  96. }
  97. // Check for attribute changes
  98. for (unsigned i = 0; i < numAttributes; ++i)
  99. {
  100. const AttributeInfo& attr = attributes->At(i);
  101. OnGetAttribute(attr, networkState_->attributes_[i]);
  102. if (networkState_->attributes_[i] != networkState_->previousAttributes_[i])
  103. {
  104. networkState_->previousAttributes_[i] = networkState_->attributes_[i];
  105. // Mark the attribute dirty in all replication states that are tracking this component
  106. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  107. networkState_->replicationStates_.End(); ++j)
  108. {
  109. ComponentReplicationState* compState = static_cast<ComponentReplicationState*>(*j);
  110. compState->dirtyAttributes_.Set(i);
  111. // Add component's parent node to the dirty set if not added yet
  112. NodeReplicationState* nodeState = compState->nodeState_;
  113. if (!nodeState->markedDirty_)
  114. {
  115. nodeState->markedDirty_ = true;
  116. nodeState->sceneState_->dirtyNodes_.Insert(node_->GetID());
  117. }
  118. }
  119. }
  120. }
  121. networkUpdate_ = false;
  122. }
  123. void Component::CleanupConnection(Connection* connection)
  124. {
  125. if (networkState_)
  126. {
  127. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  128. {
  129. if (networkState_->replicationStates_[i]->connection_ == connection)
  130. networkState_->replicationStates_.Erase(i);
  131. }
  132. }
  133. }
  134. void Component::MarkNetworkUpdate()
  135. {
  136. if (id_ < FIRST_LOCAL_ID && !networkUpdate_)
  137. {
  138. Scene* scene = GetScene();
  139. if (scene)
  140. {
  141. scene->MarkNetworkUpdate(this);
  142. networkUpdate_ = true;
  143. }
  144. }
  145. }
  146. void Component::SetID(unsigned id)
  147. {
  148. id_ = id;
  149. }
  150. void Component::SetNode(Node* node)
  151. {
  152. node_ = node;
  153. OnNodeSet(node_);
  154. }
  155. Component* Component::GetComponent(ShortStringHash type) const
  156. {
  157. return node_ ? node_->GetComponent(type) : 0;
  158. }
  159. void Component::GetComponents(PODVector<Component*>& dest, ShortStringHash type) const
  160. {
  161. if (node_)
  162. node_->GetComponents(dest, type);
  163. else
  164. dest.Clear();
  165. }