LibraryGUIContent.cs 14 KB

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