GUISceneTreeView.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /// Creates a new scene tree view element.
  20. /// </summary>
  21. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  22. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  23. /// default element style is used.</param>
  24. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  25. /// override any similar options set by style.</param>
  26. public GUISceneTreeView(string style = "", params GUIOption[] options)
  27. {
  28. Internal_CreateInstance(this, style, options);
  29. }
  30. /// <summary>
  31. /// Creates a new scene tree view element.
  32. /// </summary>
  33. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  34. /// override any similar options set by style.</param>
  35. public GUISceneTreeView(params GUIOption[] options)
  36. {
  37. Internal_CreateInstance(this, "", options);
  38. }
  39. /// <summary>
  40. /// Updates the contents of the tree view with most recent scene data. Should be called once per frame.
  41. /// </summary>
  42. public void Update()
  43. {
  44. Internal_Update(mCachedPtr);
  45. }
  46. /// <summary>
  47. /// Cuts the currently selected scene object.
  48. /// </summary>
  49. public void CutSelection()
  50. {
  51. Internal_CutSelection(mCachedPtr);
  52. }
  53. /// <summary>
  54. /// Copies the currently selected scene object.
  55. /// </summary>
  56. public void CopySelection()
  57. {
  58. Internal_CopySelection(mCachedPtr);
  59. }
  60. /// <summary>
  61. /// Pastes the scene object(s) that were previously cut or copied.
  62. /// </summary>
  63. public void PasteToSelection()
  64. {
  65. Internal_PasteToSelection(mCachedPtr);
  66. }
  67. /// <summary>
  68. /// Deletes currently selected scene objects.
  69. /// </summary>
  70. public void DeleteSelection()
  71. {
  72. Internal_DeleteSelection(mCachedPtr);
  73. }
  74. /// <summary>
  75. /// Duplicates currently selected scene objects.
  76. /// </summary>
  77. public void DuplicateSelection()
  78. {
  79. Internal_DuplicateSelection(mCachedPtr);
  80. }
  81. /// <summary>
  82. /// Starts the rename operation for the currently selected scene objects.
  83. /// </summary>
  84. public void RenameSelection()
  85. {
  86. Internal_RenameSelection(mCachedPtr);
  87. }
  88. /// <summary>
  89. /// Triggered by the runtime when the scene is modified from the native scene tree view.
  90. /// </summary>
  91. private void Internal_DoOnModified()
  92. {
  93. EditorApplication.SetSceneDirty();
  94. }
  95. /// <summary>
  96. /// Triggered by the runtime when a resource is dropped on the scene tree view.
  97. /// </summary>
  98. private void Internal_DoOnResourceDropped(SceneObject parent, string[] resourcePaths)
  99. {
  100. if (resourcePaths == null)
  101. return;
  102. List<SceneObject> addedObjects = new List<SceneObject>();
  103. for (int i = 0; i < resourcePaths.Length; i++)
  104. {
  105. ResourceMeta meta = ProjectLibrary.GetMeta(resourcePaths[i]);
  106. if (meta == null)
  107. continue;
  108. if (meta.ResType == ResourceType.Mesh)
  109. {
  110. if (!string.IsNullOrEmpty(resourcePaths[i]))
  111. {
  112. string meshName = Path.GetFileNameWithoutExtension(resourcePaths[i]);
  113. Mesh mesh = ProjectLibrary.Load<Mesh>(resourcePaths[i]);
  114. if (mesh == null)
  115. continue;
  116. SceneObject so = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  117. so.Parent = parent;
  118. Renderable renderable = so.AddComponent<Renderable>();
  119. renderable.Mesh = mesh;
  120. addedObjects.Add(so);
  121. }
  122. }
  123. else if (meta.ResType == ResourceType.Prefab)
  124. {
  125. if (!string.IsNullOrEmpty(resourcePaths[i]))
  126. {
  127. Prefab prefab = ProjectLibrary.Load<Prefab>(resourcePaths[i]);
  128. SceneObject so = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  129. so.Parent = parent;
  130. addedObjects.Add(so);
  131. }
  132. }
  133. }
  134. if(addedObjects.Count > 0)
  135. EditorApplication.SetSceneDirty();
  136. Selection.SceneObjects = addedObjects.ToArray();
  137. }
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. private static extern void Internal_CreateInstance(GUISceneTreeView instance, string style, GUIOption[] options);
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. private static extern void Internal_Update(IntPtr thisPtr);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. private static extern void Internal_CutSelection(IntPtr thisPtr);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. private static extern void Internal_CopySelection(IntPtr thisPtr);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. private static extern void Internal_PasteToSelection(IntPtr thisPtr);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern void Internal_DeleteSelection(IntPtr thisPtr);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_DuplicateSelection(IntPtr thisPtr);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_RenameSelection(IntPtr thisPtr);
  154. }
  155. /** @} */
  156. }