GUISceneTreeView.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /// Triggered by the runtime when the scene is modified from the native scene tree view.
  43. /// </summary>
  44. private void Internal_DoOnModified()
  45. {
  46. EditorApplication.SetSceneDirty();
  47. }
  48. /// <summary>
  49. /// Triggered by the runtime when a resource is dropped on the scene tree view.
  50. /// </summary>
  51. private void Internal_DoOnResourceDropped(SceneObject parent, string[] resourcePaths)
  52. {
  53. if (resourcePaths == null)
  54. return;
  55. List<SceneObject> addedObjects = new List<SceneObject>();
  56. for (int i = 0; i < resourcePaths.Length; i++)
  57. {
  58. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePaths[i]);
  59. if (entry != null && entry.Type == LibraryEntryType.File)
  60. {
  61. FileEntry fileEntry = (FileEntry)entry;
  62. if (fileEntry.ResType == ResourceType.Mesh)
  63. {
  64. if (!string.IsNullOrEmpty(resourcePaths[i]))
  65. {
  66. string meshName = Path.GetFileNameWithoutExtension(resourcePaths[i]);
  67. Mesh mesh = ProjectLibrary.Load<Mesh>(resourcePaths[i]);
  68. if (mesh == null)
  69. continue;
  70. SceneObject so = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
  71. so.Parent = parent;
  72. Renderable renderable = so.AddComponent<Renderable>();
  73. renderable.Mesh = mesh;
  74. addedObjects.Add(so);
  75. }
  76. }
  77. else if (fileEntry.ResType == ResourceType.Prefab)
  78. {
  79. if (!string.IsNullOrEmpty(resourcePaths[i]))
  80. {
  81. Prefab prefab = ProjectLibrary.Load<Prefab>(resourcePaths[i]);
  82. SceneObject so = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
  83. so.Parent = parent;
  84. addedObjects.Add(so);
  85. }
  86. }
  87. }
  88. }
  89. if(addedObjects.Count > 0)
  90. EditorApplication.SetSceneDirty();
  91. Selection.SceneObjects = addedObjects.ToArray();
  92. }
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern void Internal_CreateInstance(GUISceneTreeView instance, string style, GUIOption[] options);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_Update(IntPtr thisPtr);
  97. }
  98. }