LibraryGUIContent.cs 17 KB

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