LibraryGUIContent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /// Changes the visual representation of an element at the specified path as being hovered over.
  208. /// </summary>
  209. /// <param name="path">Project library path to the element to mark.</param>
  210. /// <param name="hovered">True if mark as hovered, false to reset to normal.</param>
  211. public void MarkAsHovered(string path, bool hovered)
  212. {
  213. if (!string.IsNullOrEmpty(path))
  214. {
  215. LibraryGUIEntry previousUnderCursorElem;
  216. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  217. previousUnderCursorElem.MarkAsHovered(hovered);
  218. }
  219. }
  220. /// <summary>
  221. /// Changes the visual representation of an element at the specified path as being pinged.
  222. /// </summary>
  223. /// <param name="path">Project library path to the element to mark.</param>
  224. /// <param name="pinged">True if mark as pinged, false to reset to normal.</param>
  225. public void MarkAsPinged(string path, bool pinged)
  226. {
  227. if (!string.IsNullOrEmpty(path))
  228. {
  229. LibraryGUIEntry previousUnderCursorElem;
  230. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  231. previousUnderCursorElem.MarkAsPinged(pinged);
  232. }
  233. }
  234. /// <summary>
  235. /// Changes the visual representation of an element at the specified path as being cut.
  236. /// </summary>
  237. /// <param name="path">Project library path to the element to mark.</param>
  238. /// <param name="cut">True if mark as cut, false to reset to normal.</param>
  239. public void MarkAsCut(string path, bool cut)
  240. {
  241. if (!string.IsNullOrEmpty(path))
  242. {
  243. LibraryGUIEntry previousUnderCursorElem;
  244. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  245. previousUnderCursorElem.MarkAsCut(cut);
  246. }
  247. }
  248. /// <summary>
  249. /// Changes the visual representation of an element at the specified path as being selected.
  250. /// </summary>
  251. /// <param name="path">Project library path to the element to mark.</param>
  252. /// <param name="selected">True if mark as selected, false to reset to normal.</param>
  253. public void MarkAsSelected(string path, bool selected)
  254. {
  255. if (!string.IsNullOrEmpty(path))
  256. {
  257. LibraryGUIEntry previousUnderCursorElem;
  258. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  259. previousUnderCursorElem.MarkAsSelected(selected);
  260. }
  261. }
  262. /// <summary>
  263. /// Attempts to find a resource tile element at the specified coordinates.
  264. /// </summary>
  265. /// <param name="scrollPos">Coordinates relative to the scroll area the content area is part of.</param>
  266. /// <returns>True if found an entry, false otherwise.</returns>
  267. public LibraryGUIEntry FindElementAt(Vector2I scrollPos)
  268. {
  269. foreach (var element in entries)
  270. {
  271. if (element.bounds.Contains(scrollPos))
  272. return element;
  273. }
  274. return null;
  275. }
  276. /// <summary>
  277. /// Attempts to find all resource tile elements overlapping the specified bounds.
  278. /// </summary>
  279. /// <param name="scrollBounds">Bounds to check for overlap, specified relative to the scroll area the content area
  280. /// is part of.</param>
  281. /// <returns>A list of found entries.</returns>
  282. public LibraryGUIEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  283. {
  284. List<LibraryGUIEntry> elements = new List<LibraryGUIEntry>();
  285. foreach (var element in entries)
  286. {
  287. if (element.Bounds.Overlaps(scrollBounds))
  288. elements.Add(element);
  289. }
  290. return elements.ToArray();
  291. }
  292. /// <summary>
  293. /// Attempts to find a resource tile element with the specified path.
  294. /// </summary>
  295. /// <param name="path">Project library path to the element.</param>
  296. /// <param name="entry">Found element, or null if none found.</param>
  297. /// <returns>True if an element was found, false otherwise.</returns>
  298. public bool TryGetEntry(string path, out LibraryGUIEntry entry)
  299. {
  300. return entryLookup.TryGetValue(path, out entry);
  301. }
  302. }
  303. }