GUISceneTreeView.cs 6.8 KB

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