BsPrefabUtility.h 4.0 KB

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