sceneTracker.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SCENETRACKER_H_
  23. #define _SCENETRACKER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. /// @file
  28. /// This file contains an abstract framework for tracking SceneObjects.
  29. class SceneTracker;
  30. //-----------------------------------------------------------------------------
  31. // SceneObjectLink.
  32. //-----------------------------------------------------------------------------
  33. /// A SceneObjectLink represents the link between a SceneObject and a SceneTracker.
  34. class SceneObjectLink
  35. {
  36. public:
  37. typedef void Parent;
  38. friend class SceneTracker; // Administers our link fields.
  39. protected:
  40. /// SceneObject being linked to; always set and never changes.
  41. SceneObject* mObject;
  42. /// The scene tracker to which this link belongs.
  43. SceneTracker* mTracker;
  44. /// Next scope link on this SceneObject; NULL if last.
  45. SceneObjectLink* mNextLink;
  46. /// Previous scope link on this SceneObject; NULL if first.
  47. SceneObjectLink* mPrevLink;
  48. public:
  49. ///
  50. SceneObjectLink( SceneTracker* tracker, SceneObject* object );
  51. virtual ~SceneObjectLink();
  52. /// @return The SceneScopeTracker managing this link.
  53. SceneTracker* getTracker() const { return mTracker; }
  54. /// @return The object being linked to.
  55. SceneObject* getObject() const { return mObject; }
  56. /// @return The next link in this link chain.
  57. SceneObjectLink* getNextLink() const { return mNextLink; }
  58. /// @return The previous link in this link chain.
  59. SceneObjectLink* getPrevLink() const { return mPrevLink; }
  60. /// Notify the associated tracker that the transform state of the
  61. /// scene object represented by this link has changed.
  62. void update();
  63. ///
  64. static SceneObjectLink* getLinkForTracker( SceneTracker* tracker, SceneObject* fromObject );
  65. };
  66. //-----------------------------------------------------------------------------
  67. // SceneTracker.
  68. //-----------------------------------------------------------------------------
  69. /// A SceneTracker tracks SceneObjects.
  70. ///
  71. /// This is an abstract base class.
  72. class SceneTracker
  73. {
  74. public:
  75. typedef void Parent;
  76. friend class SceneObjectLink; // SceneObjectLink::update() notifies us on SceneObject state changes.
  77. protected:
  78. /// If true, only client SceneObjects will be tracked; otherwise it's only server SceneObjects.
  79. bool mIsClientTracker;
  80. /// Type mask that SceneObjects must match in order to be allowed to register.
  81. U32 mObjectTypeMask;
  82. /// Return true if the given object qualifies for being managed by this SceneTracker.
  83. virtual bool _isTrackableObject( SceneObject* object ) const
  84. {
  85. return ( object->isClientObject() == mIsClientTracker
  86. && ( object->getTypeMask() & getObjectTypeMask() ) );
  87. }
  88. /// Callback used for the initial scan of objects in init().
  89. static void _containerFindCallback( SceneObject* object, SceneTracker* tracker );
  90. public:
  91. ///
  92. SceneTracker( bool isClientTracker, U32 typeMask = 0xFFFFFFFF );
  93. virtual ~SceneTracker();
  94. /// Initialize the tracker from the current scene.
  95. virtual void init();
  96. /// @return The type mask that must be matched by objects in order to be allowed to register.
  97. U32 getObjectTypeMask() const { return mObjectTypeMask; }
  98. /// Set the type mask that objects must match in order to be allowed to register.
  99. void setObjectTypeMask( U32 typeMask ) { mObjectTypeMask = typeMask; }
  100. /// @return True if this tracker only deals with client objects; false if only server objects.
  101. bool isClientTracker() const { return mIsClientTracker; }
  102. /// Register a SceneObject for being tracked by this tracker.
  103. ///
  104. /// Only objects that fit the tracker's client/server state and
  105. /// object type mask will actually get registered. For other objects,
  106. /// this is a NOP.
  107. ///
  108. /// @param object Scene object.
  109. virtual void registerObject( SceneObject* object ) = 0;
  110. /// Unregister the given object from the tracker.
  111. /// @param object Scene object.
  112. virtual void unregisterObject( SceneObject* object ) = 0;
  113. /// Notify the tracker that the transform state of the given scene object has changed.
  114. /// @param object Scene object.
  115. virtual void updateObject( SceneObjectLink* object ) = 0;
  116. };
  117. #endif // !_SCENETRACKER_H_