ProjectWindow.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. internal sealed class ProjectWindow : EditorWindow
  5. {
  6. private enum ViewType
  7. {
  8. Grid64, Grid48, Grid32, List16
  9. }
  10. private const int GRID_ENTRY_SPACING = 15;
  11. private const int LIST_ENTRY_SPACING = 7;
  12. private const int MAX_LABEL_HEIGHT = 50;
  13. private bool hasContentFocus = false;
  14. private bool HasContentFocus { get { return HasFocus && hasContentFocus; } }
  15. private ViewType viewType = ViewType.Grid32;
  16. private string currentDirectory;
  17. private string currentSelection;
  18. private string currentPing;
  19. private GUIScrollArea contentScrollArea;
  20. private GUILayout contentLayout;
  21. [MenuItem("Windows/Project", ButtonModifier.Ctrl, ButtonCode.P)]
  22. private static void OpenProjectWindow()
  23. {
  24. OpenWindow<ProjectWindow>();
  25. }
  26. private void OnInitialize()
  27. {
  28. ProjectLibrary.OnEntryAdded += OnEntryChanged;
  29. ProjectLibrary.OnEntryRemoved += OnEntryChanged;
  30. contentScrollArea = new GUIScrollArea(GUIOption.FlexibleWidth(), GUIOption.FlexibleHeight());
  31. GUI.AddElement(contentScrollArea);
  32. Reset();
  33. }
  34. public void Ping(Resource resource)
  35. {
  36. currentPing = ProjectLibrary.GetPath(resource);
  37. Refresh();
  38. }
  39. private void Select(Resource resource)
  40. {
  41. currentSelection = ProjectLibrary.GetPath(resource);
  42. currentPing = "";
  43. Refresh();
  44. }
  45. private void EnterDirectory(string directory)
  46. {
  47. currentDirectory = directory;
  48. currentPing = "";
  49. currentSelection = "";
  50. Refresh();
  51. }
  52. private void SetView(ViewType type)
  53. {
  54. viewType = type;
  55. Refresh();
  56. }
  57. private void EditorUpdate()
  58. {
  59. // TODO - Handle input, drag and drop and whatever else might be needed
  60. }
  61. private void OnEntryChanged(string entry)
  62. {
  63. Refresh();
  64. }
  65. private SpriteTexture GetIcon(LibraryEntry entry)
  66. {
  67. if (entry.Type == LibraryEntryType.Directory)
  68. {
  69. return EditorBuiltin.FolderIcon;
  70. }
  71. else
  72. {
  73. FileEntry fileEntry = (FileEntry)entry;
  74. switch (fileEntry.ResType)
  75. {
  76. case ResourceType.Font:
  77. return EditorBuiltin.FontIcon;
  78. case ResourceType.Mesh:
  79. return EditorBuiltin.MeshIcon;
  80. case ResourceType.Texture:
  81. return EditorBuiltin.TextureIcon;
  82. case ResourceType.PlainText:
  83. return null; // TODO
  84. case ResourceType.ScriptCode:
  85. return null; // TODO
  86. case ResourceType.SpriteTexture:
  87. return null; // TODO
  88. case ResourceType.Shader:
  89. return null; // TODO
  90. case ResourceType.Material:
  91. return null; // TODO
  92. }
  93. }
  94. return null;
  95. }
  96. private void Refresh()
  97. {
  98. DirectoryEntry entry = ProjectLibrary.GetEntry(currentDirectory) as DirectoryEntry;
  99. if (entry == null)
  100. {
  101. Reset();
  102. return;
  103. }
  104. if (contentLayout != null)
  105. contentLayout.Destroy();
  106. contentLayout = contentScrollArea.layout.AddLayoutY();
  107. Rect2I contentBounds = contentScrollArea.Bounds;
  108. LibraryEntry[] childEntries = entry.Children;
  109. if (childEntries.Length == 0)
  110. return;
  111. if (viewType == ViewType.List16)
  112. {
  113. int tileSize = 16;
  114. for (int i = 0; i < childEntries.Length; i++)
  115. {
  116. // TODO
  117. }
  118. }
  119. else
  120. {
  121. int tileSize = 64;
  122. switch (viewType)
  123. {
  124. case ViewType.Grid64: tileSize = 64; break;
  125. case ViewType.Grid48: tileSize = 48; break;
  126. case ViewType.Grid32: tileSize = 32; break;
  127. }
  128. GUILayoutX rowLayout = contentLayout.AddLayoutX();
  129. contentLayout.AddFlexibleSpace();
  130. rowLayout.AddFlexibleSpace();
  131. int currentWidth = GRID_ENTRY_SPACING * 2;
  132. bool addedAny = false;
  133. for (int i = 0; i < childEntries.Length; i++)
  134. {
  135. if (currentWidth >= contentBounds.width && addedAny) // We force at least one entry per row, even if it doesn't fit
  136. {
  137. rowLayout = contentLayout.AddLayoutX();
  138. contentLayout.AddFlexibleSpace();
  139. rowLayout.AddFlexibleSpace();
  140. currentWidth = GRID_ENTRY_SPACING * 2;
  141. }
  142. LibraryEntry currentEntry = childEntries[i];
  143. GUILayoutY entryLayout = rowLayout.AddLayoutY();
  144. rowLayout.AddFlexibleSpace();
  145. SpriteTexture iconTexture = GetIcon(currentEntry);
  146. GUITexture icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  147. true, GUIOption.FixedHeight(tileSize), GUIOption.FixedWidth(tileSize));
  148. GUILabel label = new GUILabel(currentEntry.Name, EditorStyles.MultiLineLabel,
  149. GUIOption.FixedWidth(tileSize), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  150. entryLayout.AddElement(icon);
  151. entryLayout.AddElement(label);
  152. addedAny = true;
  153. currentWidth += tileSize + GRID_ENTRY_SPACING;
  154. }
  155. }
  156. // TODO - Handle ping/selection and whatever else
  157. }
  158. private void Reset()
  159. {
  160. currentDirectory = ProjectLibrary.Root.Path;
  161. Refresh();
  162. }
  163. protected override void WindowResized(int width, int height)
  164. {
  165. base.WindowResized(width, height);
  166. Refresh();
  167. }
  168. }
  169. }