GUISceneTreeView.cs 6.4 KB

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