GUISceneTreeView.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using bs;
  8. namespace bs.Editor
  9. {
  10. /** @addtogroup GUI-Editor
  11. * @{
  12. */
  13. /// <summary>
  14. /// GUI element that displays all scene objects in the current scene as a tree view.
  15. /// </summary>
  16. public sealed class GUISceneTreeView : GUIElement
  17. {
  18. /// <summary>
  19. /// State of the tree view, containing the expand/collapse state of all the elements in the view.
  20. /// </summary>
  21. public SceneTreeViewState State
  22. {
  23. get { return Internal_GetState(mCachedPtr); }
  24. set { Internal_SetState(mCachedPtr, value);}
  25. }
  26. /// <summary>
  27. /// Creates a new scene tree view element.
  28. /// </summary>
  29. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  30. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  31. /// default element style is used.</param>
  32. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  33. /// override any similar options set by style.</param>
  34. public GUISceneTreeView(string style = "", params GUIOption[] options)
  35. {
  36. Internal_CreateInstance(this, style, options);
  37. }
  38. /// <summary>
  39. /// Creates a new scene tree view element.
  40. /// </summary>
  41. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  42. /// override any similar options set by style.</param>
  43. public GUISceneTreeView(params GUIOption[] options)
  44. {
  45. Internal_CreateInstance(this, "", options);
  46. }
  47. /// <summary>
  48. /// Updates the contents of the tree view with most recent scene data. Should be called once per frame.
  49. /// </summary>
  50. public void Update()
  51. {
  52. Internal_Update(mCachedPtr);
  53. }
  54. /// <summary>
  55. /// Cuts the currently selected scene object.
  56. /// </summary>
  57. public void CutSelection()
  58. {
  59. Internal_CutSelection(mCachedPtr);
  60. }
  61. /// <summary>
  62. /// Copies the currently selected scene object.
  63. /// </summary>
  64. public void CopySelection()
  65. {
  66. Internal_CopySelection(mCachedPtr);
  67. }
  68. /// <summary>
  69. /// Pastes the scene object(s) that were previously cut or copied.
  70. /// </summary>
  71. public void PasteToSelection()
  72. {
  73. Internal_PasteToSelection(mCachedPtr);
  74. }
  75. /// <summary>
  76. /// Deletes currently selected scene objects.
  77. /// </summary>
  78. public void DeleteSelection()
  79. {
  80. Internal_DeleteSelection(mCachedPtr);
  81. }
  82. /// <summary>
  83. /// Duplicates currently selected scene objects.
  84. /// </summary>
  85. public void DuplicateSelection()
  86. {
  87. Internal_DuplicateSelection(mCachedPtr);
  88. }
  89. /// <summary>
  90. /// Starts the rename operation for the currently selected scene objects.
  91. /// </summary>
  92. public void RenameSelection()
  93. {
  94. Internal_RenameSelection(mCachedPtr);
  95. }
  96. /// <summary>
  97. /// Triggered by the runtime when the scene is modified from the native scene tree view.
  98. /// </summary>
  99. private void Internal_DoOnModified()
  100. {
  101. EditorApplication.SetSceneDirty();
  102. }
  103. /// <summary>
  104. /// Triggered by the runtime when a resource is dropped on the scene tree view.
  105. /// </summary>
  106. private void Internal_DoOnResourceDropped(SceneObject parent, string[] resourcePaths)
  107. {
  108. if (resourcePaths == null)
  109. return;
  110. List<SceneObject> addedObjects = new List<SceneObject>();
  111. for (int i = 0; i < resourcePaths.Length; i++)
  112. {
  113. ResourceMeta meta = ProjectLibrary.GetMeta(resourcePaths[i]);
  114. if (meta == null)
  115. continue;
  116. if (meta.ResType == ResourceType.Mesh)
  117. {
  118. if (!string.IsNullOrEmpty(resourcePaths[i]))
  119. {
  120. string meshName = Path.GetFileNameWithoutExtension(resourcePaths[i]);
  121. Mesh mesh = ProjectLibrary.Load<Mesh>(resourcePaths[i]);
  122. if (mesh == null)
  123. continue;
  124. SceneObject so = new SceneObject(meshName);
  125. GameObjectUndo.RecordNewSceneObject(so);
  126. so.Parent = parent;
  127. Renderable renderable = so.AddComponent<Renderable>();
  128. renderable.Mesh = mesh;
  129. GameObjectUndo.ResolveDiffs();
  130. addedObjects.Add(so);
  131. }
  132. }
  133. else if (meta.ResType == ResourceType.Prefab)
  134. {
  135. if (!string.IsNullOrEmpty(resourcePaths[i]))
  136. {
  137. Prefab prefab = ProjectLibrary.Load<Prefab>(resourcePaths[i]);
  138. SceneObject so = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  139. so.Parent = parent;
  140. addedObjects.Add(so);
  141. }
  142. }
  143. }
  144. if(addedObjects.Count > 0)
  145. EditorApplication.SetSceneDirty();
  146. Selection.SceneObjects = addedObjects.ToArray();
  147. }
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern void Internal_CreateInstance(GUISceneTreeView instance, string style, GUIOption[] options);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_Update(IntPtr thisPtr);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_CutSelection(IntPtr thisPtr);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern void Internal_CopySelection(IntPtr thisPtr);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern void Internal_PasteToSelection(IntPtr thisPtr);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern void Internal_DeleteSelection(IntPtr thisPtr);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern void Internal_DuplicateSelection(IntPtr thisPtr);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern void Internal_RenameSelection(IntPtr thisPtr);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern SceneTreeViewState Internal_GetState(IntPtr thisPtr);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern void Internal_SetState(IntPtr thisPtr, SceneTreeViewState state);
  168. }
  169. /** @} */
  170. }