Component.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #pragma once
  24. #include "Matrix3x4.h"
  25. #include "Serializable.h"
  26. class Node;
  27. class Scene;
  28. struct ComponentReplicationState;
  29. /// Base class for components. Components can be created to scene nodes.
  30. class Component : public Serializable
  31. {
  32. OBJECT(Component);
  33. friend class Node;
  34. friend class Scene;
  35. public:
  36. /// Construct.
  37. Component(Context* context);
  38. /// Destruct.
  39. virtual ~Component();
  40. /// Handle attribute write access.
  41. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  42. /// Save as binary data. Return true if successful.
  43. virtual bool Save(Serializer& dest);
  44. /// Save as XML data. Return true if successful.
  45. virtual bool SaveXML(XMLElement& dest);
  46. /// Return the depended on nodes to order network updates.
  47. virtual void GetDependencyNodes(PODVector<Node*>& dest) {};
  48. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  49. void Remove();
  50. /// Return ID.
  51. unsigned GetID() const { return id_; }
  52. /// Return scene node.
  53. Node* GetNode() const { return node_; }
  54. /// Return the scene the node belongs to.
  55. Scene* GetScene() const;
  56. /// Return components in the same scene node by type.
  57. void GetComponents(PODVector<Component*>& dest, ShortStringHash type) const;
  58. /// Return component in the same scene node by type. If there are several, returns the first.
  59. Component* GetComponent(ShortStringHash type) const;
  60. /// Template version of returning a component in the same scene node by type.
  61. template <class T> T* GetComponent() const;
  62. /// Template version of returning components in the same scene node by type.
  63. template <class T> void GetComponents(PODVector<T*>& dest) const;
  64. /// Add a replication state that is tracking this component.
  65. void AddReplicationState(ComponentReplicationState* state);
  66. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  67. void PrepareNetworkUpdate();
  68. /// Clean up all references to a network connection that is about to be removed.
  69. void CleanupConnection(Connection* connection);
  70. /// Mark for attribute check on the next network update.
  71. void MarkNetworkUpdate();
  72. protected:
  73. /// Handle scene node being assigned at creation.
  74. virtual void OnNodeSet(Node* node) {};
  75. /// Handle scene node transform dirtied.
  76. virtual void OnMarkedDirty(Node* node) {};
  77. /// %Set ID. Called by Scene.
  78. void SetID(unsigned id);
  79. /// %Set scene node. Called by Node when creating the component.
  80. void SetNode(Node* node);
  81. /// Scene node.
  82. Node* node_;
  83. /// Unique ID within the scene.
  84. unsigned id_;
  85. /// Network update queued flag.
  86. bool networkUpdate_;
  87. };
  88. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  89. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }