LibraryGUIEntry.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.IO;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Represents GUI for a single resource tile used in <see cref="LibraryGUIContent"/>.
  9. /// </summary>
  10. internal class LibraryGUIEntry
  11. {
  12. private static readonly Color PING_COLOR = Color.BansheeOrange;
  13. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  14. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  15. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  16. private const int VERT_PADDING = 3;
  17. private const int BG_HORZ_PADDING = 2;
  18. private const int BG_VERT_PADDING = 2;
  19. private const string LibraryEntryFirstBg = "LibraryEntryFirstBg";
  20. private const string LibraryEntryBg = "LibraryEntryBg";
  21. private const string LibraryEntryLastBg = "LibraryEntryLastBg";
  22. private const string LibraryEntryVertFirstBg = "LibraryEntryVertFirstBg";
  23. private const string LibraryEntryVertBg = "LibraryEntryVertBg";
  24. private const string LibraryEntryVertLastBg = "LibraryEntryVertLastBg";
  25. /// <summary>
  26. /// Possible visual states for the resource tile.
  27. /// </summary>
  28. enum UnderlayState // Note: Order of these is relevant
  29. {
  30. None, Hovered, Selected, Pinged
  31. }
  32. public int index;
  33. public string path;
  34. public GUITexture icon;
  35. public GUILabel label;
  36. public Rect2I bounds;
  37. private GUITexture underlay;
  38. private GUITexture groupUnderlay;
  39. private LibraryGUIContent owner;
  40. private UnderlayState underlayState;
  41. private GUITextBox renameTextBox;
  42. private int width, height;
  43. private LibraryGUIEntryType type;
  44. private bool delayedSelect;
  45. private float delayedSelectTime;
  46. private ulong delayedOpenCodeEditorFrame = ulong.MaxValue;
  47. /// <summary>
  48. /// Bounds of the entry relative to part content area.
  49. /// </summary>
  50. public Rect2I Bounds
  51. {
  52. get { return bounds; }
  53. }
  54. /// <summary>
  55. /// Constructs a new resource tile entry.
  56. /// </summary>
  57. /// <param name="owner">Content area this entry is part of.</param>
  58. /// <param name="parent">Parent layout to add this entry's GUI elements to.</param>
  59. /// <param name="path">Path to the project library entry to display data for.</param>
  60. /// <param name="index">Sequential index of the entry in the conent area.</param>
  61. /// <param name="width">Width of the GUI entry.</param>
  62. /// <param name="height">Maximum allowed height for the label.</param>"
  63. /// <param name="type">Type of the entry, which controls its style and/or behaviour.</param>
  64. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, string path, int index, int width, int height,
  65. LibraryGUIEntryType type)
  66. {
  67. GUILayout entryLayout;
  68. if (owner.GridLayout)
  69. entryLayout = parent.AddLayoutY();
  70. else
  71. entryLayout = parent.AddLayoutX();
  72. SpriteTexture iconTexture = GetIcon(path, owner.TileSize);
  73. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  74. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  75. label = null;
  76. string name = PathEx.GetTail(path);
  77. if (owner.GridLayout)
  78. {
  79. int labelHeight = height - owner.TileSize;
  80. label = new GUILabel(name, EditorStyles.MultiLineLabelCentered,
  81. GUIOption.FixedWidth(width), GUIOption.FixedHeight(labelHeight));
  82. switch (type)
  83. {
  84. case LibraryGUIEntryType.Single:
  85. break;
  86. case LibraryGUIEntryType.MultiFirst:
  87. groupUnderlay = new GUITexture(null, LibraryEntryFirstBg);
  88. break;
  89. case LibraryGUIEntryType.MultiElement:
  90. groupUnderlay = new GUITexture(null, LibraryEntryBg);
  91. break;
  92. case LibraryGUIEntryType.MultiLast:
  93. groupUnderlay = new GUITexture(null, LibraryEntryLastBg);
  94. break;
  95. }
  96. }
  97. else
  98. {
  99. label = new GUILabel(name, GUIOption.FixedWidth(width - owner.TileSize), GUIOption.FixedHeight(height));
  100. switch (type)
  101. {
  102. case LibraryGUIEntryType.Single:
  103. break;
  104. case LibraryGUIEntryType.MultiFirst:
  105. groupUnderlay = new GUITexture(null, LibraryEntryVertFirstBg);
  106. break;
  107. case LibraryGUIEntryType.MultiElement:
  108. groupUnderlay = new GUITexture(null, LibraryEntryVertBg);
  109. break;
  110. case LibraryGUIEntryType.MultiLast:
  111. groupUnderlay = new GUITexture(null, LibraryEntryVertLastBg);
  112. break;
  113. }
  114. }
  115. entryLayout.AddElement(icon);
  116. entryLayout.AddElement(label);
  117. if (groupUnderlay != null)
  118. owner.DeepUnderlay.AddElement(groupUnderlay);
  119. this.owner = owner;
  120. this.index = index;
  121. this.path = path;
  122. this.bounds = new Rect2I();
  123. this.underlay = null;
  124. this.type = type;
  125. this.width = width;
  126. this.height = height;
  127. }
  128. /// <summary>
  129. /// Positions the GUI elements. Must be called after construction, but only after all content area entries have
  130. /// been constructed so that entry's final bounds are known.
  131. /// </summary>
  132. public void Initialize()
  133. {
  134. Rect2I iconBounds = icon.Bounds;
  135. bounds = iconBounds;
  136. Rect2I labelBounds = label.Bounds;
  137. if (owner.GridLayout)
  138. {
  139. bounds.x = labelBounds.x;
  140. bounds.y -= VERT_PADDING;
  141. bounds.width = labelBounds.width;
  142. bounds.height = (labelBounds.y + labelBounds.height + VERT_PADDING) - bounds.y;
  143. }
  144. else
  145. {
  146. bounds.y -= VERT_PADDING;
  147. bounds.width = width;
  148. bounds.height += VERT_PADDING;
  149. }
  150. string hoistedPath = path;
  151. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  152. overlayBtn.Bounds = bounds;
  153. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  154. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  155. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  156. owner.Overlay.AddElement(overlayBtn);
  157. if (groupUnderlay != null)
  158. {
  159. if (owner.GridLayout)
  160. {
  161. int offsetToNext = BG_HORZ_PADDING + owner.HorzElementSpacing;
  162. if (type == LibraryGUIEntryType.MultiLast)
  163. offsetToNext = BG_HORZ_PADDING * 2;
  164. Rect2I bgBounds = new Rect2I(bounds.x - BG_HORZ_PADDING, bounds.y,
  165. bounds.width + offsetToNext, bounds.height);
  166. groupUnderlay.Bounds = bgBounds;
  167. }
  168. else
  169. {
  170. int offsetToNext = BG_VERT_PADDING + LibraryGUIContent.LIST_ENTRY_SPACING;
  171. if (type == LibraryGUIEntryType.MultiLast)
  172. offsetToNext = BG_VERT_PADDING * 2;
  173. Rect2I bgBounds = new Rect2I(bounds.x, bounds.y - BG_VERT_PADDING,
  174. bounds.width, bounds.height + offsetToNext);
  175. groupUnderlay.Bounds = bgBounds;
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// Called every frame.
  181. /// </summary>
  182. public void Update()
  183. {
  184. if (delayedSelect && Time.RealElapsed > delayedSelectTime)
  185. {
  186. owner.Window.Select(path);
  187. delayedSelect = false;
  188. }
  189. if (delayedOpenCodeEditorFrame == Time.FrameIdx)
  190. {
  191. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  192. if (entry != null && entry.Type == LibraryEntryType.File)
  193. {
  194. FileEntry resEntry = (FileEntry) entry;
  195. CodeEditor.OpenFile(resEntry.Path, 0);
  196. }
  197. ProgressBar.Hide();
  198. }
  199. }
  200. /// <summary>
  201. /// Changes the visual representation of the element as being cut.
  202. /// </summary>
  203. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  204. public void MarkAsCut(bool enable)
  205. {
  206. if (enable)
  207. icon.SetTint(CUT_COLOR);
  208. else
  209. icon.SetTint(Color.White);
  210. }
  211. /// <summary>
  212. /// Changes the visual representation of the element as being selected.
  213. /// </summary>
  214. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  215. public void MarkAsSelected(bool enable)
  216. {
  217. if ((int)underlayState > (int)UnderlayState.Selected)
  218. return;
  219. if (enable)
  220. {
  221. CreateUnderlay();
  222. underlay.SetTint(SELECTION_COLOR);
  223. underlayState = UnderlayState.Selected;
  224. }
  225. else
  226. {
  227. ClearUnderlay();
  228. underlayState = UnderlayState.None;
  229. }
  230. }
  231. /// <summary>
  232. /// Changes the visual representation of the element as being pinged.
  233. /// </summary>
  234. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  235. public void MarkAsPinged(bool enable)
  236. {
  237. if ((int)underlayState > (int)UnderlayState.Pinged)
  238. return;
  239. if (enable)
  240. {
  241. CreateUnderlay();
  242. underlay.SetTint(PING_COLOR);
  243. underlayState = UnderlayState.Pinged;
  244. }
  245. else
  246. {
  247. ClearUnderlay();
  248. underlayState = UnderlayState.None;
  249. }
  250. }
  251. /// <summary>
  252. /// Changes the visual representation of the element as being hovered over.
  253. /// </summary>
  254. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  255. public void MarkAsHovered(bool enable)
  256. {
  257. if ((int)underlayState > (int)UnderlayState.Hovered)
  258. return;
  259. if (enable)
  260. {
  261. CreateUnderlay();
  262. underlay.SetTint(HOVER_COLOR);
  263. underlayState = UnderlayState.Hovered;
  264. }
  265. else
  266. {
  267. ClearUnderlay();
  268. underlayState = UnderlayState.None;
  269. }
  270. }
  271. /// <summary>
  272. /// Starts a rename operation over the entry, displaying the rename input box.
  273. /// </summary>
  274. public void StartRename()
  275. {
  276. if (renameTextBox != null)
  277. return;
  278. renameTextBox = new GUITextBox(true);
  279. Rect2I renameBounds = label.Bounds;
  280. // Rename box allows for less space for text than label, so adjust it slightly so it's more likely to be able
  281. // to display all visible text.
  282. renameBounds.x -= 4;
  283. renameBounds.width += 8;
  284. renameBounds.height += 8;
  285. renameTextBox.Bounds = renameBounds;
  286. owner.RenameOverlay.AddElement(renameTextBox);
  287. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  288. renameTextBox.Text = name;
  289. renameTextBox.Focus = true;
  290. }
  291. /// <summary>
  292. /// Stops a rename operation over the entry, hiding the rename input box.
  293. /// </summary>
  294. public void StopRename()
  295. {
  296. if (renameTextBox != null)
  297. {
  298. renameTextBox.Destroy();
  299. renameTextBox = null;
  300. }
  301. }
  302. /// <summary>
  303. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  304. /// </summary>
  305. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  306. public string GetRenamedName()
  307. {
  308. if (renameTextBox != null)
  309. return renameTextBox.Text;
  310. return "";
  311. }
  312. /// <summary>
  313. /// Clears the underlay GUI element (for example ping, hover, select).
  314. /// </summary>
  315. private void ClearUnderlay()
  316. {
  317. if (underlay != null)
  318. {
  319. underlay.Destroy();
  320. underlay = null;
  321. }
  322. underlayState = UnderlayState.None;
  323. }
  324. /// <summary>
  325. /// Creates a GUI elements that may be used for underlay effects (for example ping, hover, select).
  326. /// </summary>
  327. private void CreateUnderlay()
  328. {
  329. if (underlay == null)
  330. {
  331. underlay = new GUITexture(Builtin.WhiteTexture);
  332. underlay.Bounds = Bounds;
  333. owner.Underlay.AddElement(underlay);
  334. }
  335. }
  336. /// <summary>
  337. /// Triggered when the user clicks on the entry.
  338. /// </summary>
  339. /// <param name="path">Project library path of the clicked entry.</param>
  340. private void OnEntryClicked(string path)
  341. {
  342. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  343. if (entry != null && entry.Type == LibraryEntryType.Directory)
  344. {
  345. // If entry is a directory delay selection as it might be a double-click, in which case we want to keep
  346. // whatever selection is active currently so that user can perform drag and drop with its inspector
  347. // from the folder he is browsing to.
  348. delayedSelect = true;
  349. delayedSelectTime = Time.RealElapsed + 0.5f;
  350. }
  351. else
  352. owner.Window.Select(path);
  353. }
  354. /// <summary>
  355. /// Triggered when the user double-clicked on the entry.
  356. /// </summary>
  357. /// <param name="path">Project library path of the double-clicked entry.</param>
  358. private void OnEntryDoubleClicked(string path)
  359. {
  360. delayedSelect = false;
  361. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  362. if (entry != null)
  363. {
  364. if (entry.Type == LibraryEntryType.Directory)
  365. owner.Window.EnterDirectory(path);
  366. else
  367. {
  368. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  369. FileEntry fileEntry = (FileEntry)entry;
  370. if (meta.ResType == ResourceType.Prefab)
  371. {
  372. EditorApplication.LoadScene(fileEntry.Path);
  373. }
  374. else if (meta.ResType == ResourceType.ScriptCode)
  375. {
  376. ProgressBar.Show("Opening external code editor...", 1.0f);
  377. delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
  378. }
  379. }
  380. }
  381. }
  382. /// <summary>
  383. /// Returns an icon that can be used for displaying a resource of the specified type.
  384. /// </summary>
  385. /// <param name="path">Path to the project library entry to display data for.</param>
  386. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  387. /// <returns>Icon to display for the specified entry.</returns>
  388. private static SpriteTexture GetIcon(string path, int size)
  389. {
  390. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  391. if (entry.Type == LibraryEntryType.Directory)
  392. {
  393. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Folder, size);
  394. }
  395. else
  396. {
  397. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  398. switch (meta.ResType)
  399. {
  400. case ResourceType.Font:
  401. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Font, size);
  402. case ResourceType.Mesh:
  403. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Mesh, size);
  404. case ResourceType.Texture:
  405. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Texture, size);
  406. case ResourceType.PlainText:
  407. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PlainText, size);
  408. case ResourceType.ScriptCode:
  409. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.ScriptCode, size);
  410. case ResourceType.SpriteTexture:
  411. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.SpriteTexture, size);
  412. case ResourceType.Shader:
  413. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  414. case ResourceType.ShaderInclude:
  415. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  416. case ResourceType.Material:
  417. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Material, size);
  418. case ResourceType.Prefab:
  419. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Prefab, size);
  420. case ResourceType.GUISkin:
  421. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.GUISkin, size);
  422. case ResourceType.PhysicsMaterial:
  423. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMaterial, size);
  424. case ResourceType.PhysicsMesh:
  425. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMesh, size);
  426. }
  427. }
  428. return null;
  429. }
  430. }
  431. /// <summary>
  432. /// Type of <see cref="LibraryGUIEntry"/> that controls its look.
  433. /// </summary>
  434. internal enum LibraryGUIEntryType
  435. {
  436. /// <summary>
  437. /// Represents a single resource.
  438. /// </summary>
  439. Single,
  440. /// <summary>
  441. /// Represents the first entry in a multi-resource group.
  442. /// </summary>
  443. MultiFirst,
  444. /// <summary>
  445. /// Represents one of the mid entries in a multi-resource group.
  446. /// </summary>
  447. MultiElement,
  448. /// <summary>
  449. /// Represents the last entry in a multi-resource group.
  450. /// </summary>
  451. MultiLast
  452. }
  453. }