EditorSceneData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. namespace bs.Editor
  5. {
  6. /// <summary>
  7. /// Contains per-scene data relevant to the editor.
  8. /// </summary>
  9. [SerializeObject]
  10. internal class EditorSceneData
  11. {
  12. /// <summary>
  13. /// Creates new editor scene data.
  14. /// </summary>
  15. /// <param name="root">Root object of the scene to create editor scene data for.</param>
  16. /// <returns>New editor scene data object, initialized with the hierarchy of the provided scene.</returns>
  17. public static EditorSceneData FromScene(SceneObject root)
  18. {
  19. EditorSceneData output = new EditorSceneData();
  20. output.Root = EditorSceneObject.FromSceneObject(root);
  21. return output;
  22. }
  23. /// <summary>
  24. /// Updates the editor scene object hierarchy from the current scene hierarchy.
  25. /// </summary>
  26. /// <param name="root">Root of the hierarchy to update from.</param>
  27. public void UpdateFromScene(SceneObject root)
  28. {
  29. Dictionary<UUID, EditorSceneObject> lookup = GetLookup();
  30. Root = EditorSceneObject.FromSceneObject(root);
  31. void UpdateFromOldData(EditorSceneObject so)
  32. {
  33. if (lookup.TryGetValue(so.UUID, out EditorSceneObject data))
  34. so.CopyData(data);
  35. foreach(var entry in so.Children)
  36. UpdateFromOldData(entry);
  37. }
  38. UpdateFromOldData(Root);
  39. }
  40. /// <summary>
  41. /// Builds a lookup table of scene object UUID -> editor scene object data, for all scene objects.
  42. /// </summary>
  43. /// <returns>Lookup table.</returns>
  44. public Dictionary<UUID, EditorSceneObject> GetLookup()
  45. {
  46. Dictionary<UUID, EditorSceneObject> lookup = new Dictionary<UUID, EditorSceneObject>();
  47. void AddToLookup(EditorSceneObject so)
  48. {
  49. lookup[so.UUID] = so;
  50. foreach(var entry in so.Children)
  51. AddToLookup(entry);
  52. }
  53. if(Root != null)
  54. AddToLookup(Root);
  55. return lookup;
  56. }
  57. /// <summary>
  58. /// Root object of the scene hierarchy.
  59. /// </summary>
  60. public EditorSceneObject Root;
  61. }
  62. /// <summary>
  63. /// Contains editor-relevant data about a scene object.
  64. /// </summary>
  65. [SerializeObject]
  66. internal class EditorSceneObject
  67. {
  68. /// <summary>
  69. /// Unique identifier of the scene object.
  70. /// </summary>
  71. public UUID UUID;
  72. /// <summary>
  73. /// True if the object is expanded in the hierarchy view.
  74. /// </summary>
  75. public bool IsExpanded;
  76. /// <summary>
  77. /// Child scene objects, if any.
  78. /// </summary>
  79. public EditorSceneObject[] Children;
  80. /// <summary>
  81. /// Initializes the object from a corresponding scene object.
  82. /// </summary>
  83. /// <param name="so">Scene object that this object contains additional data for.</param>
  84. /// <returns>New editor scene object.</returns>
  85. public static EditorSceneObject FromSceneObject(SceneObject so)
  86. {
  87. EditorSceneObject output = new EditorSceneObject();
  88. output.UUID = so.UUID;
  89. int numChildren = so.GetNumChildren();
  90. output.Children = new EditorSceneObject[numChildren];
  91. for (int i = 0; i < numChildren; i++)
  92. output.Children[i] = FromSceneObject(so.GetChild(i));
  93. return output;
  94. }
  95. /// <summary>
  96. /// Copies the stored data from one object instance to another. Does not copy object UUID or child list.
  97. /// </summary>
  98. /// <param name="other">Object from which to copy the data.</param>
  99. public void CopyData(EditorSceneObject other)
  100. {
  101. IsExpanded = other.IsExpanded;
  102. }
  103. }
  104. }