LibraryGUIContent.cs 13 KB

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