BsPrefabUtility.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsGameObject.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Scene
  9. * @{
  10. */
  11. /** Performs various prefab specific operations. */
  12. class BS_CORE_EXPORT PrefabUtility
  13. {
  14. private:
  15. /** Contains saved Component instance data. */
  16. struct ComponentProxy
  17. {
  18. GameObjectInstanceDataPtr instanceData;
  19. UINT32 linkId;
  20. };
  21. /** Contains saved SceneObject instance data, as well as saved instance data for all its children and components. */
  22. struct SceneObjectProxy
  23. {
  24. GameObjectInstanceDataPtr instanceData;
  25. UINT32 linkId;
  26. Vector<ComponentProxy> components;
  27. Vector<SceneObjectProxy> children;
  28. };
  29. public:
  30. /**
  31. * Remove any instance specific changes to the object or its hierarchy from the provided prefab instance and
  32. * restore it to the exact copy of the linked prefab.
  33. *
  34. * @param[in] so Object to revert.
  35. */
  36. static void revertToPrefab(const HSceneObject& so);
  37. /**
  38. * Updates all of the objects belonging to the same prefab instance as the provided object (if any). The update
  39. * will apply any changes from the linked prefab to the hierarchy (if any).
  40. *
  41. * @param[in] so Object to update.
  42. */
  43. static void updateFromPrefab(const HSceneObject& so);
  44. /**
  45. * Generates prefab "link" ID that can be used for tracking which game object in a prefab instance corresponds to
  46. * an object in the prefab.
  47. *
  48. * @note If any children of the provided object belong to another prefab they will not have IDs generated.
  49. */
  50. static UINT32 generatePrefabIds(const HSceneObject& sceneObject, UINT32 startingId);
  51. /**
  52. * Clears all prefab "link" IDs in the provided object and its children.
  53. *
  54. * @note If any of its children belong to another prefab they will not be cleared.
  55. */
  56. static void clearPrefabIds(const HSceneObject& sceneObject, bool recursive = true);
  57. /**
  58. * Updates the internal prefab diff data by recording the difference between the current values in the provided
  59. * prefab instance and its prefab.
  60. *
  61. * @note
  62. * If the provided object contains any child prefab instances, this will be done recursively for them as well.
  63. */
  64. static void recordPrefabDiff(const HSceneObject& sceneObject);
  65. private:
  66. /**
  67. * Traverses the object hierarchy, finds all child objects and components and records their instance data, as well
  68. * as their original place in the hierarchy. Instance data essentially holds the object's "identity" and by
  69. * restoring it we ensure any handles pointing to the object earlier will still point to the new version.
  70. *
  71. * @param[in] so Object to traverse and record.
  72. * @param[out] output Contains the output hierarchy of instance data.
  73. * @param[out] linkedInstanceData A map of link IDs to instance data. Objects without link IDs will not be
  74. * included here.
  75. *
  76. * @note Does not recurse into child prefab instances.
  77. */
  78. static void recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  79. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  80. /**
  81. * Restores instance data in the provided hierarchy, using link ids to determine what data maps to which objects.
  82. *
  83. * @param[in] so Object to traverse and restore the instance data.
  84. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  85. * recordInstanceData() method.
  86. * @param[in] linkedInstanceData A map of link IDs to instance data, returned by recordInstanceData() method.
  87. *
  88. * @note Does not recurse into child prefab instances.
  89. */
  90. static void restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  91. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData);
  92. /**
  93. * Restores instance data in the provided hierarchy, but only for objects without a link id. Since the objects do
  94. * not have a link ID we rely on their sequential order to find out which instance data belongs to which object.
  95. *
  96. * @param[in] so Object to traverse and restore the instance data.
  97. * @param[in] proxy Hierarchy containing instance data for all objects and components, returned by
  98. * recordInstanceData() method.
  99. *
  100. * @note Does not recurse into child prefab instances.
  101. */
  102. static void restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy);
  103. };
  104. /** @} */
  105. }