LibraryGUIContent.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. for (int i = 0; i < resourcesToDisplay.Count; i++)
  173. {
  174. ResourceToDisplay entry = resourcesToDisplay[i];
  175. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entry.path, i, 0, 0, entry.type);
  176. entries.Add(guiEntry);
  177. entryLookup[guiEntry.path] = guiEntry;
  178. if (i != resourcesToDisplay.Count - 1)
  179. main.AddSpace(LIST_ENTRY_SPACING);
  180. }
  181. main.AddFlexibleSpace();
  182. }
  183. else
  184. {
  185. int elemWidth = 0;
  186. int elemHeight = 0;
  187. int vertElemSpacing = 0;
  188. switch (viewType)
  189. {
  190. case ProjectViewType.Grid64:
  191. tileSize = 64;
  192. elemWidth = tileSize;
  193. elemHeight = tileSize + 32;
  194. horzElementSpacing = 10;
  195. vertElemSpacing = 12;
  196. break;
  197. case ProjectViewType.Grid48:
  198. tileSize = 48;
  199. elemWidth = tileSize;
  200. elemHeight = tileSize + 32;
  201. horzElementSpacing = 8;
  202. vertElemSpacing = 10;
  203. break;
  204. case ProjectViewType.Grid32:
  205. tileSize = 32;
  206. elemWidth = tileSize + 16;
  207. elemHeight = tileSize + 48;
  208. horzElementSpacing = 6;
  209. vertElemSpacing = 10;
  210. break;
  211. }
  212. gridLayout = true;
  213. int availableWidth = bounds.width;
  214. elementsPerRow = MathEx.FloorToInt((availableWidth - horzElementSpacing) / (float)(elemWidth + horzElementSpacing));
  215. elementsPerRow = Math.Max(elementsPerRow, 1);
  216. int numRows = MathEx.CeilToInt(resourcesToDisplay.Count / (float)elementsPerRow);
  217. int neededHeight = numRows * elemHeight + TOP_MARGIN;
  218. if (numRows > 0)
  219. neededHeight += (numRows - 1)* vertElemSpacing;
  220. bool requiresScrollbar = neededHeight > bounds.height;
  221. if (requiresScrollbar)
  222. {
  223. availableWidth -= parent.ScrollBarWidth;
  224. elementsPerRow = MathEx.FloorToInt((availableWidth - horzElementSpacing) / (float)(elemWidth + horzElementSpacing));
  225. elementsPerRow = Math.Max(elementsPerRow, 1);
  226. }
  227. int extraRowSpace = availableWidth - (elementsPerRow * (elemWidth + horzElementSpacing) + horzElementSpacing);
  228. main.AddSpace(TOP_MARGIN);
  229. GUILayoutX rowLayout = main.AddLayoutX();
  230. rowLayout.AddSpace(horzElementSpacing);
  231. int elemsInRow = 0;
  232. for (int i = 0; i < resourcesToDisplay.Count; i++)
  233. {
  234. if (elemsInRow == elementsPerRow && elemsInRow > 0)
  235. {
  236. main.AddSpace(vertElemSpacing);
  237. rowLayout.AddSpace(extraRowSpace);
  238. rowLayout = main.AddLayoutX();
  239. rowLayout.AddSpace(horzElementSpacing);
  240. elemsInRow = 0;
  241. }
  242. ResourceToDisplay entry = resourcesToDisplay[i];
  243. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, rowLayout, entry.path, i, elemWidth, elemHeight,
  244. entry.type);
  245. entries.Add(guiEntry);
  246. entryLookup[guiEntry.path] = guiEntry;
  247. rowLayout.AddSpace(horzElementSpacing);
  248. elemsInRow++;
  249. }
  250. int extraElements = elementsPerRow - elemsInRow;
  251. rowLayout.AddSpace((elemWidth + horzElementSpacing) * extraElements + extraRowSpace);
  252. main.AddFlexibleSpace();
  253. }
  254. for (int i = 0; i < entries.Count; i++)
  255. {
  256. LibraryGUIEntry guiEntry = entries[i];
  257. guiEntry.Initialize();
  258. }
  259. }
  260. /// <summary>
  261. /// Called every frame.
  262. /// </summary>
  263. public void Update()
  264. {
  265. for (int i = 0; i < entries.Count; i++)
  266. entries[i].Update();
  267. }
  268. /// <summary>
  269. /// Changes the visual representation of an element at the specified path as being hovered over.
  270. /// </summary>
  271. /// <param name="path">Project library path to the element to mark.</param>
  272. /// <param name="hovered">True if mark as hovered, false to reset to normal.</param>
  273. public void MarkAsHovered(string path, bool hovered)
  274. {
  275. if (!string.IsNullOrEmpty(path))
  276. {
  277. LibraryGUIEntry previousUnderCursorElem;
  278. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  279. previousUnderCursorElem.MarkAsHovered(hovered);
  280. }
  281. }
  282. /// <summary>
  283. /// Changes the visual representation of an element at the specified path as being pinged.
  284. /// </summary>
  285. /// <param name="path">Project library path to the element to mark.</param>
  286. /// <param name="pinged">True if mark as pinged, false to reset to normal.</param>
  287. public void MarkAsPinged(string path, bool pinged)
  288. {
  289. if (!string.IsNullOrEmpty(path))
  290. {
  291. LibraryGUIEntry previousUnderCursorElem;
  292. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  293. previousUnderCursorElem.MarkAsPinged(pinged);
  294. }
  295. }
  296. /// <summary>
  297. /// Changes the visual representation of an element at the specified path as being cut.
  298. /// </summary>
  299. /// <param name="path">Project library path to the element to mark.</param>
  300. /// <param name="cut">True if mark as cut, false to reset to normal.</param>
  301. public void MarkAsCut(string path, bool cut)
  302. {
  303. if (!string.IsNullOrEmpty(path))
  304. {
  305. LibraryGUIEntry previousUnderCursorElem;
  306. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  307. previousUnderCursorElem.MarkAsCut(cut);
  308. }
  309. }
  310. /// <summary>
  311. /// Changes the visual representation of an element at the specified path as being selected.
  312. /// </summary>
  313. /// <param name="path">Project library path to the element to mark.</param>
  314. /// <param name="selected">True if mark as selected, false to reset to normal.</param>
  315. public void MarkAsSelected(string path, bool selected)
  316. {
  317. if (!string.IsNullOrEmpty(path))
  318. {
  319. LibraryGUIEntry previousUnderCursorElem;
  320. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  321. previousUnderCursorElem.MarkAsSelected(selected);
  322. }
  323. }
  324. /// <summary>
  325. /// Attempts to find a resource tile element at the specified coordinates.
  326. /// </summary>
  327. /// <param name="scrollPos">Coordinates relative to the scroll area the content area is part of.</param>
  328. /// <returns>True if found an entry, false otherwise.</returns>
  329. public LibraryGUIEntry FindElementAt(Vector2I scrollPos)
  330. {
  331. foreach (var element in entries)
  332. {
  333. if (element.bounds.Contains(scrollPos))
  334. return element;
  335. }
  336. return null;
  337. }
  338. /// <summary>
  339. /// Attempts to find all resource tile elements overlapping the specified bounds.
  340. /// </summary>
  341. /// <param name="scrollBounds">Bounds to check for overlap, specified relative to the scroll area the content area
  342. /// is part of.</param>
  343. /// <returns>A list of found entries.</returns>
  344. public LibraryGUIEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  345. {
  346. List<LibraryGUIEntry> elements = new List<LibraryGUIEntry>();
  347. foreach (var element in entries)
  348. {
  349. if (element.Bounds.Overlaps(scrollBounds))
  350. elements.Add(element);
  351. }
  352. return elements.ToArray();
  353. }
  354. /// <summary>
  355. /// Attempts to find a resource tile element with the specified path.
  356. /// </summary>
  357. /// <param name="path">Project library path to the element.</param>
  358. /// <param name="entry">Found element, or null if none found.</param>
  359. /// <returns>True if an element was found, false otherwise.</returns>
  360. public bool TryGetEntry(string path, out LibraryGUIEntry entry)
  361. {
  362. return entryLookup.TryGetValue(path, out entry);
  363. }
  364. /// <summary>
  365. /// Helper structure containing information about a single entry to display in the library.
  366. /// </summary>
  367. private struct ResourceToDisplay
  368. {
  369. public ResourceToDisplay(string path, LibraryGUIEntryType type)
  370. {
  371. this.path = path;
  372. this.type = type;
  373. }
  374. public string path;
  375. public LibraryGUIEntryType type;
  376. }
  377. }
  378. }