LibraryGUIContent.cs 17 KB

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