LibraryGUIContent.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Manages GUI for the content area of the library window. Content area displays resources as a grid or list of
  11. /// resource icons.
  12. /// </summary>
  13. internal class LibraryGUIContent
  14. {
  15. private const int MIN_HORZ_SPACING = 8;
  16. private const int GRID_ENTRY_SPACING = 15;
  17. private const int LIST_ENTRY_SPACING = 7;
  18. private GUIPanel mainPanel;
  19. private GUILayout main;
  20. private GUIPanel overlay;
  21. private GUIPanel underlay;
  22. private GUIPanel deepUnderlay;
  23. private GUIPanel renameOverlay;
  24. private LibraryWindow window;
  25. private GUIScrollArea parent;
  26. private int tileSize;
  27. private bool gridLayout;
  28. private int elementsPerRow;
  29. private int labelWidth;
  30. private int elementWidth;
  31. private List<LibraryGUIEntry> entries = new List<LibraryGUIEntry>();
  32. private Dictionary<string, LibraryGUIEntry> entryLookup = new Dictionary<string, LibraryGUIEntry>();
  33. /// <summary>
  34. /// Area of the content area relative to the parent window.
  35. /// </summary>
  36. public Rect2I Bounds
  37. {
  38. get { return main.Bounds; }
  39. }
  40. /// <summary>
  41. /// Number of elements per row. Only relevant for grid layouts.
  42. /// </summary>
  43. public int ElementsPerRow
  44. {
  45. get { return elementsPerRow; }
  46. }
  47. /// <summary>
  48. /// Width of a single element in a row, including any margins. Only relevant for grid layouts.
  49. /// </summary>
  50. public int ElementWidth
  51. {
  52. get { return elementWidth; }
  53. }
  54. /// <summary>
  55. /// Determines is the content display in a grid (true) or list (false) layout.
  56. /// </summary>
  57. public bool GridLayout
  58. {
  59. get { return gridLayout; }
  60. }
  61. /// <summary>
  62. /// Sizes of a single resource tile in grid layout. Size in list layout is fixed.
  63. /// </summary>
  64. public int TileSize
  65. {
  66. get { return tileSize; }
  67. }
  68. /// <summary>
  69. /// Returns objects representing each of the displayed resource icons.
  70. /// </summary>
  71. public List<LibraryGUIEntry> Entries
  72. {
  73. get { return entries; }
  74. }
  75. /// <summary>
  76. /// Returns parent window the content area is part of.
  77. /// </summary>
  78. public LibraryWindow Window
  79. {
  80. get { return window; }
  81. }
  82. /// <summary>
  83. /// Returns a GUI panel that can be used for displaying elements underneath the resource tiles.
  84. /// </summary>
  85. public GUIPanel Underlay
  86. {
  87. get { return underlay; }
  88. }
  89. /// <summary>
  90. /// Returns a GUI panel that can be used for displaying elements underneath the resource tiles. Displays under
  91. /// <see cref="Underlay"/>.
  92. /// </summary>
  93. public GUIPanel DeepUnderlay
  94. {
  95. get { return deepUnderlay; }
  96. }
  97. /// <summary>
  98. /// Returns a GUI panel that can be used for displaying elements above the resource tiles.
  99. /// </summary>
  100. public GUIPanel Overlay
  101. {
  102. get { return overlay; }
  103. }
  104. /// <summary>
  105. /// Returns a GUI panel that can be used for displaying rename input box. Displayed on top of the resource tiles
  106. /// and the standard overlay.
  107. /// </summary>
  108. public GUIPanel RenameOverlay
  109. {
  110. get { return renameOverlay; }
  111. }
  112. /// <summary>
  113. /// Constructs a new GUI library content object.
  114. /// </summary>
  115. /// <param name="window">Parent window the content area is part of.</param>
  116. /// <param name="parent">Scroll area the content area is part of.</param>
  117. public LibraryGUIContent(LibraryWindow window, GUIScrollArea parent)
  118. {
  119. this.window = window;
  120. this.parent = parent;
  121. }
  122. /// <summary>
  123. /// Refreshes the contents of the content area. Must be called at least once after construction.
  124. /// </summary>
  125. /// <param name="viewType">Determines how to display the resource tiles.</param>
  126. /// <param name="entriesToDisplay">Project library entries to display.</param>
  127. public void Refresh(ProjectViewType viewType, LibraryEntry[] entriesToDisplay)
  128. {
  129. if (mainPanel != null)
  130. mainPanel.Destroy();
  131. entries.Clear();
  132. entryLookup.Clear();
  133. mainPanel = parent.Layout.AddPanel();
  134. GUIPanel contentPanel = mainPanel.AddPanel(1);
  135. overlay = mainPanel.AddPanel(0);
  136. underlay = mainPanel.AddPanel(2);
  137. deepUnderlay = mainPanel.AddPanel(3);
  138. renameOverlay = mainPanel.AddPanel(-1);
  139. main = contentPanel.AddLayoutY();
  140. List<ResourceToDisplay> resourcesToDisplay = new List<ResourceToDisplay>();
  141. foreach (var entry in entriesToDisplay)
  142. {
  143. if (entry.Type == LibraryEntryType.Directory)
  144. resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.Single));
  145. else
  146. {
  147. FileEntry fileEntry = (FileEntry)entry;
  148. ResourceMeta[] metas = fileEntry.ResourceMetas;
  149. if (metas.Length > 0)
  150. {
  151. if (metas.Length == 1)
  152. resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.Single));
  153. else
  154. {
  155. resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.MultiFirst));
  156. for (int i = 1; i < metas.Length - 1; i++)
  157. {
  158. string path = Path.Combine(entry.Path, metas[i].SubresourceName);
  159. resourcesToDisplay.Add(new ResourceToDisplay(path, LibraryGUIEntryType.MultiElement));
  160. }
  161. string lastPath = Path.Combine(entry.Path, metas[metas.Length - 1].SubresourceName);
  162. resourcesToDisplay.Add(new ResourceToDisplay(lastPath, LibraryGUIEntryType.MultiLast));
  163. }
  164. }
  165. }
  166. }
  167. if (viewType == ProjectViewType.List16)
  168. {
  169. tileSize = 16;
  170. gridLayout = false;
  171. elementsPerRow = 1;
  172. }
  173. else
  174. {
  175. switch (viewType)
  176. {
  177. case ProjectViewType.Grid64:
  178. tileSize = 64;
  179. break;
  180. case ProjectViewType.Grid48:
  181. tileSize = 48;
  182. break;
  183. case ProjectViewType.Grid32:
  184. tileSize = 32;
  185. break;
  186. }
  187. gridLayout = true;
  188. Rect2I scrollBounds = parent.Bounds;
  189. int availableWidth = scrollBounds.width;
  190. int elemSize = tileSize + GRID_ENTRY_SPACING;
  191. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  192. elementsPerRow = Math.Max(elementsPerRow, 1);
  193. int numRows = MathEx.CeilToInt(resourcesToDisplay.Count / (float)elementsPerRow);
  194. int neededHeight = numRows * (elemSize);
  195. bool requiresScrollbar = neededHeight > scrollBounds.height;
  196. if (requiresScrollbar)
  197. {
  198. availableWidth -= parent.ScrollBarWidth;
  199. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  200. }
  201. if (elementsPerRow > 0)
  202. {
  203. labelWidth = (availableWidth - (elementsPerRow + 1)*MIN_HORZ_SPACING)/elementsPerRow;
  204. elementWidth = availableWidth/elementsPerRow;
  205. }
  206. else
  207. {
  208. labelWidth = 0;
  209. elementWidth = 0;
  210. }
  211. }
  212. if (viewType == ProjectViewType.List16)
  213. {
  214. for (int i = 0; i < resourcesToDisplay.Count; i++)
  215. {
  216. ResourceToDisplay entry = resourcesToDisplay[i];
  217. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entry.path, i, labelWidth, entry.type);
  218. entries.Add(guiEntry);
  219. entryLookup[guiEntry.path] = guiEntry;
  220. if (i != resourcesToDisplay.Count - 1)
  221. main.AddSpace(LIST_ENTRY_SPACING);
  222. }
  223. main.AddFlexibleSpace();
  224. }
  225. else
  226. {
  227. main.AddSpace(GRID_ENTRY_SPACING / 2);
  228. GUILayoutX rowLayout = main.AddLayoutX();
  229. main.AddSpace(GRID_ENTRY_SPACING);
  230. rowLayout.AddFlexibleSpace();
  231. int elemsInRow = 0;
  232. for (int i = 0; i < resourcesToDisplay.Count; i++)
  233. {
  234. if (elemsInRow == elementsPerRow && elemsInRow > 0)
  235. {
  236. rowLayout = main.AddLayoutX();
  237. main.AddSpace(GRID_ENTRY_SPACING);
  238. rowLayout.AddFlexibleSpace();
  239. elemsInRow = 0;
  240. }
  241. ResourceToDisplay entry = resourcesToDisplay[i];
  242. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, rowLayout, entry.path, i, labelWidth, entry.type);
  243. entries.Add(guiEntry);
  244. entryLookup[guiEntry.path] = guiEntry;
  245. rowLayout.AddFlexibleSpace();
  246. elemsInRow++;
  247. }
  248. int extraElements = elementsPerRow - elemsInRow;
  249. for (int i = 0; i < extraElements; i++)
  250. {
  251. rowLayout.AddSpace(labelWidth);
  252. rowLayout.AddFlexibleSpace();
  253. }
  254. main.AddFlexibleSpace();
  255. }
  256. for (int i = 0; i < entries.Count; i++)
  257. {
  258. LibraryGUIEntry guiEntry = entries[i];
  259. guiEntry.Initialize();
  260. }
  261. }
  262. /// <summary>
  263. /// Called every frame.
  264. /// </summary>
  265. public void Update()
  266. {
  267. for (int i = 0; i < entries.Count; i++)
  268. entries[i].Update();
  269. }
  270. /// <summary>
  271. /// Changes the visual representation of an element at the specified path as being hovered over.
  272. /// </summary>
  273. /// <param name="path">Project library path to the element to mark.</param>
  274. /// <param name="hovered">True if mark as hovered, false to reset to normal.</param>
  275. public void MarkAsHovered(string path, bool hovered)
  276. {
  277. if (!string.IsNullOrEmpty(path))
  278. {
  279. LibraryGUIEntry previousUnderCursorElem;
  280. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  281. previousUnderCursorElem.MarkAsHovered(hovered);
  282. }
  283. }
  284. /// <summary>
  285. /// Changes the visual representation of an element at the specified path as being pinged.
  286. /// </summary>
  287. /// <param name="path">Project library path to the element to mark.</param>
  288. /// <param name="pinged">True if mark as pinged, false to reset to normal.</param>
  289. public void MarkAsPinged(string path, bool pinged)
  290. {
  291. if (!string.IsNullOrEmpty(path))
  292. {
  293. LibraryGUIEntry previousUnderCursorElem;
  294. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  295. previousUnderCursorElem.MarkAsPinged(pinged);
  296. }
  297. }
  298. /// <summary>
  299. /// Changes the visual representation of an element at the specified path as being cut.
  300. /// </summary>
  301. /// <param name="path">Project library path to the element to mark.</param>
  302. /// <param name="cut">True if mark as cut, false to reset to normal.</param>
  303. public void MarkAsCut(string path, bool cut)
  304. {
  305. if (!string.IsNullOrEmpty(path))
  306. {
  307. LibraryGUIEntry previousUnderCursorElem;
  308. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  309. previousUnderCursorElem.MarkAsCut(cut);
  310. }
  311. }
  312. /// <summary>
  313. /// Changes the visual representation of an element at the specified path as being selected.
  314. /// </summary>
  315. /// <param name="path">Project library path to the element to mark.</param>
  316. /// <param name="selected">True if mark as selected, false to reset to normal.</param>
  317. public void MarkAsSelected(string path, bool selected)
  318. {
  319. if (!string.IsNullOrEmpty(path))
  320. {
  321. LibraryGUIEntry previousUnderCursorElem;
  322. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  323. previousUnderCursorElem.MarkAsSelected(selected);
  324. }
  325. }
  326. /// <summary>
  327. /// Attempts to find a resource tile element at the specified coordinates.
  328. /// </summary>
  329. /// <param name="scrollPos">Coordinates relative to the scroll area the content area is part of.</param>
  330. /// <returns>True if found an entry, false otherwise.</returns>
  331. public LibraryGUIEntry FindElementAt(Vector2I scrollPos)
  332. {
  333. foreach (var element in entries)
  334. {
  335. if (element.bounds.Contains(scrollPos))
  336. return element;
  337. }
  338. return null;
  339. }
  340. /// <summary>
  341. /// Attempts to find all resource tile elements overlapping the specified bounds.
  342. /// </summary>
  343. /// <param name="scrollBounds">Bounds to check for overlap, specified relative to the scroll area the content area
  344. /// is part of.</param>
  345. /// <returns>A list of found entries.</returns>
  346. public LibraryGUIEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  347. {
  348. List<LibraryGUIEntry> elements = new List<LibraryGUIEntry>();
  349. foreach (var element in entries)
  350. {
  351. if (element.Bounds.Overlaps(scrollBounds))
  352. elements.Add(element);
  353. }
  354. return elements.ToArray();
  355. }
  356. /// <summary>
  357. /// Attempts to find a resource tile element with the specified path.
  358. /// </summary>
  359. /// <param name="path">Project library path to the element.</param>
  360. /// <param name="entry">Found element, or null if none found.</param>
  361. /// <returns>True if an element was found, false otherwise.</returns>
  362. public bool TryGetEntry(string path, out LibraryGUIEntry entry)
  363. {
  364. return entryLookup.TryGetValue(path, out entry);
  365. }
  366. /// <summary>
  367. /// Helper structure containing information about a single entry to display in the library.
  368. /// </summary>
  369. private struct ResourceToDisplay
  370. {
  371. public ResourceToDisplay(string path, LibraryGUIEntryType type)
  372. {
  373. this.path = path;
  374. this.type = type;
  375. }
  376. public string path;
  377. public LibraryGUIEntryType type;
  378. }
  379. }
  380. }