LibraryGUIContent.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. internal class LibraryGUIContent
  7. {
  8. private const int MIN_HORZ_SPACING = 8;
  9. private const int GRID_ENTRY_SPACING = 15;
  10. private const int LIST_ENTRY_SPACING = 7;
  11. private GUIPanel mainPanel;
  12. private GUILayout main;
  13. private GUIPanel overlay;
  14. private GUIPanel underlay;
  15. private GUIPanel inputOverlay;
  16. private LibraryWindow window;
  17. private GUIScrollArea parent;
  18. private int tileSize;
  19. private bool gridLayout;
  20. private int elementsPerRow;
  21. private int labelWidth;
  22. private LibraryGUIEntry[] entries = new LibraryGUIEntry[0];
  23. private Dictionary<string, LibraryGUIEntry> entryLookup = new Dictionary<string, LibraryGUIEntry>();
  24. public Rect2I Bounds
  25. {
  26. get { return main.Bounds; }
  27. }
  28. public int ElementsPerRow
  29. {
  30. get { return elementsPerRow; }
  31. }
  32. public int LabelWidth
  33. {
  34. get { return labelWidth; }
  35. }
  36. public bool GridLayout
  37. {
  38. get { return gridLayout; }
  39. }
  40. public int TileSize
  41. {
  42. get { return tileSize; }
  43. }
  44. public LibraryGUIEntry[] Entries
  45. {
  46. get { return entries; }
  47. }
  48. // TODO: This doesn't feel like it should be public
  49. public LibraryWindow Window
  50. {
  51. get { return window; }
  52. }
  53. // TODO: This doesn't feel like it should be public
  54. public GUIPanel Underlay
  55. {
  56. get { return underlay; }
  57. }
  58. // TODO: This doesn't feel like it should be public
  59. public GUIPanel Overlay
  60. {
  61. get { return overlay; }
  62. }
  63. // TODO: This doesn't feel like it should be public
  64. public GUIPanel InputOverlay
  65. {
  66. get { return inputOverlay; }
  67. }
  68. public LibraryGUIContent(LibraryWindow window, GUIScrollArea parent)
  69. {
  70. this.window = window;
  71. this.parent = parent;
  72. }
  73. public void Refresh(ProjectViewType viewType, LibraryEntry[] entriesToDisplay)
  74. {
  75. if (mainPanel != null)
  76. mainPanel.Destroy();
  77. entries = new LibraryGUIEntry[entriesToDisplay.Length];
  78. entryLookup.Clear();
  79. mainPanel = parent.Layout.AddPanel();
  80. GUIPanel contentPanel = mainPanel.AddPanel(1);
  81. overlay = mainPanel.AddPanel(0);
  82. underlay = mainPanel.AddPanel(2);
  83. inputOverlay = mainPanel.AddPanel(-1);
  84. main = contentPanel.AddLayoutY();
  85. if (viewType == ProjectViewType.List16)
  86. {
  87. tileSize = 16;
  88. gridLayout = false;
  89. elementsPerRow = 1;
  90. }
  91. else
  92. {
  93. switch (viewType)
  94. {
  95. case ProjectViewType.Grid64:
  96. tileSize = 64;
  97. break;
  98. case ProjectViewType.Grid48:
  99. tileSize = 48;
  100. break;
  101. case ProjectViewType.Grid32:
  102. tileSize = 32;
  103. break;
  104. }
  105. gridLayout = true;
  106. Rect2I scrollBounds = parent.Bounds;
  107. int availableWidth = scrollBounds.width;
  108. int elemSize = tileSize + GRID_ENTRY_SPACING;
  109. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  110. elementsPerRow = Math.Max(elementsPerRow, 1);
  111. int numRows = MathEx.CeilToInt(entriesToDisplay.Length / (float)elementsPerRow);
  112. int neededHeight = numRows * (elemSize);
  113. bool requiresScrollbar = neededHeight > scrollBounds.height;
  114. if (requiresScrollbar)
  115. {
  116. availableWidth -= parent.ScrollBarWidth;
  117. elementsPerRow = (availableWidth - GRID_ENTRY_SPACING * 2) / elemSize;
  118. }
  119. if (elementsPerRow > 0)
  120. labelWidth = (availableWidth - (elementsPerRow + 1) * MIN_HORZ_SPACING) / elementsPerRow;
  121. else
  122. labelWidth = 0;
  123. }
  124. if (viewType == ProjectViewType.List16)
  125. {
  126. for (int i = 0; i < entriesToDisplay.Length; i++)
  127. {
  128. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entriesToDisplay[i], i);
  129. entries[i] = guiEntry;
  130. entryLookup[guiEntry.path] = guiEntry;
  131. if (i != entriesToDisplay.Length - 1)
  132. main.AddSpace(LIST_ENTRY_SPACING);
  133. }
  134. main.AddFlexibleSpace();
  135. }
  136. else
  137. {
  138. main.AddSpace(GRID_ENTRY_SPACING / 2);
  139. GUILayoutX rowLayout = main.AddLayoutX();
  140. main.AddSpace(GRID_ENTRY_SPACING);
  141. rowLayout.AddFlexibleSpace();
  142. int elemsInRow = 0;
  143. for (int i = 0; i < entriesToDisplay.Length; i++)
  144. {
  145. if (elemsInRow == elementsPerRow && elemsInRow > 0)
  146. {
  147. rowLayout = main.AddLayoutX();
  148. main.AddSpace(GRID_ENTRY_SPACING);
  149. rowLayout.AddFlexibleSpace();
  150. elemsInRow = 0;
  151. }
  152. LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, rowLayout, entriesToDisplay[i], i);
  153. entries[i] = guiEntry;
  154. entryLookup[guiEntry.path] = guiEntry;
  155. rowLayout.AddFlexibleSpace();
  156. elemsInRow++;
  157. }
  158. int extraElements = elementsPerRow - elemsInRow;
  159. for (int i = 0; i < extraElements; i++)
  160. {
  161. rowLayout.AddSpace(labelWidth);
  162. rowLayout.AddFlexibleSpace();
  163. }
  164. main.AddFlexibleSpace();
  165. }
  166. for (int i = 0; i < entries.Length; i++)
  167. {
  168. LibraryGUIEntry guiEntry = entries[i];
  169. guiEntry.Initialize();
  170. }
  171. }
  172. public void MarkAsHovered(string path, bool hovered)
  173. {
  174. if (!string.IsNullOrEmpty(path))
  175. {
  176. LibraryGUIEntry previousUnderCursorElem;
  177. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  178. previousUnderCursorElem.MarkAsHovered(hovered);
  179. }
  180. }
  181. public void MarkAsPinged(string path, bool pinged)
  182. {
  183. if (!string.IsNullOrEmpty(path))
  184. {
  185. LibraryGUIEntry previousUnderCursorElem;
  186. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  187. previousUnderCursorElem.MarkAsPinged(pinged);
  188. }
  189. }
  190. public void MarkAsCut(string path, bool cut)
  191. {
  192. if (!string.IsNullOrEmpty(path))
  193. {
  194. LibraryGUIEntry previousUnderCursorElem;
  195. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  196. previousUnderCursorElem.MarkAsCut(cut);
  197. }
  198. }
  199. public void MarkAsSelected(string path, bool selected)
  200. {
  201. if (!string.IsNullOrEmpty(path))
  202. {
  203. LibraryGUIEntry previousUnderCursorElem;
  204. if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
  205. previousUnderCursorElem.MarkAsSelected(selected);
  206. }
  207. }
  208. public LibraryGUIEntry FindElementAt(Vector2I scrollPos)
  209. {
  210. foreach (var element in entries)
  211. {
  212. if (element.bounds.Contains(scrollPos))
  213. return element;
  214. }
  215. return null;
  216. }
  217. public LibraryGUIEntry[] FindElementsOverlapping(Rect2I scrollBounds)
  218. {
  219. List<LibraryGUIEntry> elements = new List<LibraryGUIEntry>();
  220. foreach (var element in entries)
  221. {
  222. if (element.Bounds.Overlaps(scrollBounds))
  223. elements.Add(element);
  224. }
  225. return elements.ToArray();
  226. }
  227. public bool TryGetEntry(string path, out LibraryGUIEntry entry)
  228. {
  229. return entryLookup.TryGetValue(path, out entry);
  230. }
  231. }
  232. }