BsPrefabDiff.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsGameObject.h"
  5. #include "BsVector3.h"
  6. #include "BsQuaternion.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Contains differences between two components of the same type.
  11. *
  12. * @see PrefabDiff
  13. */
  14. struct BS_CORE_EXPORT PrefabComponentDiff : public IReflectable
  15. {
  16. INT32 id;
  17. SPtr<SerializedObject> data;
  18. /************************************************************************/
  19. /* RTTI */
  20. /************************************************************************/
  21. public:
  22. friend class PrefabComponentDiffRTTI;
  23. static RTTITypeBase* getRTTIStatic();
  24. virtual RTTITypeBase* getRTTI() const override;
  25. };
  26. /**
  27. * @brief Flags that mark which portion of a scene-object is modified.
  28. */
  29. enum class SceneObjectDiffFlags
  30. {
  31. Name = 0x01,
  32. Position = 0x02,
  33. Rotation = 0x04,
  34. Scale = 0x08,
  35. Active = 0x10
  36. };
  37. /**
  38. * @brief Contains a set of prefab differences for a single scene object.
  39. *
  40. * @see PrefabDiff
  41. */
  42. struct BS_CORE_EXPORT PrefabObjectDiff : public IReflectable
  43. {
  44. UINT32 id = 0;
  45. String name;
  46. Vector3 position;
  47. Quaternion rotation;
  48. Vector3 scale;
  49. bool isActive = false;
  50. UINT32 soFlags = 0;
  51. Vector<SPtr<PrefabComponentDiff>> componentDiffs;
  52. Vector<UINT32> removedComponents;
  53. Vector<SPtr<SerializedObject>> addedComponents;
  54. Vector<SPtr<PrefabObjectDiff>> childDiffs;
  55. Vector<UINT32> removedChildren;
  56. Vector<SPtr<SerializedObject>> addedChildren;
  57. /************************************************************************/
  58. /* RTTI */
  59. /************************************************************************/
  60. public:
  61. friend class PrefabObjectDiffRTTI;
  62. static RTTITypeBase* getRTTIStatic();
  63. virtual RTTITypeBase* getRTTI() const override;
  64. };
  65. /**
  66. * @brief Contains modifications between an prefab and its instance. The modifications are a set of
  67. * added/removed children or components and per-field "diffs" of their components.
  68. */
  69. class BS_CORE_EXPORT PrefabDiff : public IReflectable
  70. {
  71. public:
  72. /**
  73. * @brief Creates a new prefab diff by comparing the provided instanced scene object hierarchy
  74. * with the prefab scene object hierarchy.
  75. */
  76. static SPtr<PrefabDiff> create(const HSceneObject& prefab, const HSceneObject& instance);
  77. /**
  78. * @brief Applies the internal prefab diff to the provided object. The object should have
  79. * similar hierarchy as the prefab the diff was created for, otherwise the results are
  80. * undefined.
  81. */
  82. void apply(const HSceneObject& object);
  83. private:
  84. /**
  85. * @brief A reference to a renamed game object instance data, and its original ID
  86. * so it may be restored later.
  87. */
  88. struct RenamedGameObject
  89. {
  90. GameObjectInstanceDataPtr instanceData;
  91. UINT64 originalId;
  92. };
  93. /**
  94. * @brief Recurses over every scene object in the prefab a generates differences between itself
  95. * and the instanced version.
  96. *
  97. * @see create
  98. */
  99. static SPtr<PrefabObjectDiff> generateDiff(const HSceneObject& prefab, const HSceneObject& instance);
  100. /**
  101. * @brief Recursively applies a per-object set of prefab differences to a specific object.
  102. *
  103. * @see apply
  104. */
  105. static void applyDiff(const SPtr<PrefabObjectDiff>& diff, const HSceneObject& object);
  106. /**
  107. * @brief Renames all game objects in the provided instance so that IDs of the objects will match
  108. * the IDs of their counterparts in the prefab.
  109. *
  110. * @note This is a temporary action and should be undone by calling "restoreInstanceIds" and providing
  111. * it with the output of this method.
  112. * @par By doing this before calling ::generateDiff we ensure that any game object handles pointing to objects
  113. * within the prefab instance hierarchy aren't recorded by the diff system, since we want those to
  114. * remain as they are after applying the diff.
  115. */
  116. static void renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output);
  117. /**
  118. * @brief Restores any instance IDs that were modified by the "renameInstanceIds" method.
  119. *
  120. * @see renameInstanceIds;
  121. */
  122. static void restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects);
  123. SPtr<PrefabObjectDiff> mRoot;
  124. /************************************************************************/
  125. /* RTTI */
  126. /************************************************************************/
  127. public:
  128. friend class PrefabDiffRTTI;
  129. static RTTITypeBase* getRTTIStatic();
  130. virtual RTTITypeBase* getRTTI() const override;
  131. };
  132. }