BsPrefabDiff.h 4.7 KB

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