SceneObjectSet.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _SCENE_OBJECT_SET_H_
  23. #define _SCENE_OBJECT_SET_H_
  24. #ifndef _SCENE_OBJECT_LIST_H_
  25. #include "2d/sceneobject/SceneObjectList.h"
  26. #endif
  27. #ifndef _FIND_ITERATOR_H_
  28. #include "collection/findIterator.h"
  29. #endif
  30. #ifndef _SIMDICTIONARY_H_
  31. #include "sim/simDictionary.h"
  32. #endif
  33. #ifndef _SIM_OBJECT_H_
  34. #include "sim/simObject.h"
  35. #endif
  36. #ifndef _TAML_CHILDREN_H_
  37. #include "persistence/taml/tamlChildren.h"
  38. #endif
  39. //-----------------------------------------------------------------------------
  40. class SceneObject;
  41. //-----------------------------------------------------------------------------
  42. class SceneObjectSet : public SimObject, public TamlChildren
  43. {
  44. typedef SimObject Parent;
  45. protected:
  46. SceneObjectList mObjectList;
  47. public:
  48. SceneObjectSet()
  49. {
  50. VECTOR_SET_ASSOCIATION(mObjectList);
  51. }
  52. ~SceneObjectSet()
  53. {
  54. }
  55. typedef SceneObjectList::iterator iterator;
  56. typedef SceneObjectList::value_type value;
  57. inline SceneObject* front( void ) { return mObjectList.front(); }
  58. inline SceneObject* first( void ) { return mObjectList.first(); }
  59. inline SceneObject* last( void ) { return mObjectList.last(); }
  60. inline bool empty( void ) { return mObjectList.empty(); }
  61. inline S32 size( void ) const { return mObjectList.size(); }
  62. inline iterator begin( void ) { return mObjectList.begin(); }
  63. inline iterator end( void ) { return mObjectList.end(); }
  64. value operator[] (S32 index) { return mObjectList[U32(index)]; }
  65. inline iterator find( iterator first, iterator last, SceneObject *obj ) { return ::find(first, last, obj); }
  66. inline iterator find( SceneObject *obj ) { return ::find(begin(), end(), obj); }
  67. template <typename T> inline bool containsType( void )
  68. {
  69. for( iterator itr = begin(); itr != end(); ++itr )
  70. {
  71. if ( dynamic_cast<T*>(*itr) != NULL )
  72. return true;
  73. }
  74. return false;
  75. }
  76. virtual bool reOrder( SceneObject *obj, SceneObject *target=0 );
  77. SceneObject* at(S32 index) const { return mObjectList.at(index); }
  78. void deleteObjects( void );
  79. void clear();
  80. virtual void onRemove();
  81. virtual void onDeleteNotify(SimObject *object);
  82. virtual void addObject(SceneObject*);
  83. virtual void removeObject(SceneObject*);
  84. virtual void pushObject(SceneObject*);
  85. virtual void popObject();
  86. void bringObjectToFront(SceneObject* obj) { reOrder(obj, front()); }
  87. void pushObjectToBack(SceneObject* obj) { reOrder(obj, NULL); }
  88. virtual U32 getTamlChildCount( void ) const { return (U32)size(); }
  89. virtual SimObject* getTamlChild( const U32 childIndex ) const
  90. {
  91. // Sanity!
  92. AssertFatal( childIndex < (U32)size(), "SceneObjectSet::getTamlChild() - Child index is out of range." );
  93. // For when the assert is not used.
  94. if ( childIndex >= (U32)size() )
  95. return NULL;
  96. return (SimObject*)at( childIndex );
  97. }
  98. virtual void addTamlChild( SimObject* pSimObject );
  99. void callOnChildren( const char * method, S32 argc, const char *argv[], bool executeOnChildGroups = true );
  100. virtual SimObject *findObject(const char *name);
  101. SceneObject* findObjectByInternalName(const char* internalName, bool searchChildren = false);
  102. DECLARE_CONOBJECT(SceneObjectSet);
  103. #ifdef TORQUE_DEBUG
  104. inline void _setVectorAssoc( const char *file, const U32 line ) { mObjectList.setFileAssociation( file, line ); }
  105. #endif
  106. };
  107. #ifdef TORQUE_DEBUG
  108. # define SCENEOBJECT_SET_ASSOCIATION( x ) x._setVectorAssoc( __FILE__, __LINE__ )
  109. #else
  110. # define SCENEOBJECT_SET_ASSOCIATION( x )
  111. #endif
  112. //-----------------------------------------------------------------------------
  113. class SceneObjectSetIterator
  114. {
  115. protected:
  116. struct Entry
  117. {
  118. SceneObjectSet* set;
  119. SceneObjectSet::iterator itr;
  120. };
  121. class Stack : public Vector<Entry> {
  122. public:
  123. void push_back(SceneObjectSet*);
  124. };
  125. Stack stack;
  126. public:
  127. SceneObjectSetIterator(SceneObjectSet*);
  128. SceneObject* operator++();
  129. SceneObject* operator*() {
  130. return stack.empty()? 0: *stack.last().itr;
  131. }
  132. };
  133. #endif // _SCENE_OBJECT_SET_H_