BsPrefabDiff.h 4.9 KB

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