2
0

LibraryGUIContent.cs 13 KB

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