Component.cpp 5.7 KB

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