LibraryGUIEntry.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using BansheeEngine;
  10. namespace BansheeEditor
  11. {
  12. /// <summary>
  13. /// Represents GUI for a single resource tile used in <see cref="LibraryGUIContent"/>.
  14. /// </summary>
  15. internal class LibraryGUIEntry
  16. {
  17. private const int MAX_LABEL_HEIGHT = 50;
  18. private static readonly Color PING_COLOR = Color.BansheeOrange;
  19. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  20. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  21. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  22. private const int SELECTION_EXTRA_WIDTH = 3;
  23. /// <summary>
  24. /// Possible visual states for the resource tile.
  25. /// </summary>
  26. enum UnderlayState // Note: Order of these is relevant
  27. {
  28. None, Hovered, Selected, Pinged
  29. }
  30. public int index;
  31. public string path;
  32. public GUITexture icon;
  33. public GUILabel label;
  34. public Rect2I bounds;
  35. private GUITexture underlay;
  36. private LibraryGUIContent owner;
  37. private UnderlayState underlayState;
  38. private GUITextBox renameTextBox;
  39. private bool delayedSelect;
  40. private float delayedSelectTime;
  41. private ulong delayedOpenCodeEditorFrame = ulong.MaxValue;
  42. /// <summary>
  43. /// Bounds of the entry relative to part content area.
  44. /// </summary>
  45. public Rect2I Bounds
  46. {
  47. get { return bounds; }
  48. }
  49. /// <summary>
  50. /// Constructs a new resource tile entry.
  51. /// </summary>
  52. /// <param name="owner">Content area this entry is part of.</param>
  53. /// <param name="parent">Parent layout to add this entry's GUI elements to.</param>
  54. /// <param name="entry">Project library entry this entry displays data for.</param>
  55. /// <param name="index">Sequential index of the entry in the conent area.</param>
  56. /// <param name="labelWidth">Width of the GUI labels that display the elements.</param>
  57. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, LibraryEntry entry, int index, int labelWidth)
  58. {
  59. GUILayout entryLayout;
  60. if (owner.GridLayout)
  61. entryLayout = parent.AddLayoutY();
  62. else
  63. entryLayout = parent.AddLayoutX();
  64. SpriteTexture iconTexture = GetIcon(entry, owner.TileSize);
  65. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  66. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  67. label = null;
  68. if (owner.GridLayout)
  69. {
  70. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabelCentered,
  71. GUIOption.FixedWidth(labelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  72. }
  73. else
  74. {
  75. label = new GUILabel(entry.Name);
  76. }
  77. entryLayout.AddElement(icon);
  78. entryLayout.AddElement(label);
  79. this.owner = owner;
  80. this.index = index;
  81. this.path = entry.Path;
  82. this.bounds = new Rect2I();
  83. this.underlay = null;
  84. }
  85. /// <summary>
  86. /// Positions the GUI elements. Must be called after construction, but only after all content area entries have
  87. /// been constructed so that entry's final bounds are known.
  88. /// </summary>
  89. public void Initialize()
  90. {
  91. bounds = icon.Bounds;
  92. Rect2I labelBounds = label.Bounds;
  93. bounds.x = MathEx.Min(bounds.x, labelBounds.x - SELECTION_EXTRA_WIDTH);
  94. bounds.y = MathEx.Min(bounds.y, labelBounds.y) - 5; // 5 - Just padding for better look
  95. bounds.width = MathEx.Max(bounds.x + bounds.width,
  96. labelBounds.x + labelBounds.width) - bounds.x + SELECTION_EXTRA_WIDTH;
  97. bounds.height = MathEx.Max(bounds.y + bounds.height,
  98. labelBounds.y + labelBounds.height) - bounds.y;
  99. string hoistedPath = path;
  100. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  101. overlayBtn.Bounds = bounds;
  102. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  103. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  104. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  105. owner.Overlay.AddElement(overlayBtn);
  106. }
  107. /// <summary>
  108. /// Called every frame.
  109. /// </summary>
  110. public void Update()
  111. {
  112. if (delayedSelect && Time.RealElapsed > delayedSelectTime)
  113. {
  114. owner.Window.Select(path);
  115. delayedSelect = false;
  116. }
  117. if (delayedOpenCodeEditorFrame == Time.FrameIdx)
  118. {
  119. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  120. if (entry != null && entry.Type == LibraryEntryType.File)
  121. {
  122. FileEntry resEntry = (FileEntry) entry;
  123. CodeEditor.OpenFile(resEntry.Path, 0);
  124. }
  125. ProgressBar.Hide();
  126. }
  127. }
  128. /// <summary>
  129. /// Changes the visual representation of the element as being cut.
  130. /// </summary>
  131. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  132. public void MarkAsCut(bool enable)
  133. {
  134. if (enable)
  135. icon.SetTint(CUT_COLOR);
  136. else
  137. icon.SetTint(Color.White);
  138. }
  139. /// <summary>
  140. /// Changes the visual representation of the element as being selected.
  141. /// </summary>
  142. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  143. public void MarkAsSelected(bool enable)
  144. {
  145. if ((int)underlayState > (int)UnderlayState.Selected)
  146. return;
  147. if (enable)
  148. {
  149. CreateUnderlay();
  150. underlay.SetTint(SELECTION_COLOR);
  151. underlayState = UnderlayState.Selected;
  152. }
  153. else
  154. {
  155. ClearUnderlay();
  156. underlayState = UnderlayState.None;
  157. }
  158. }
  159. /// <summary>
  160. /// Changes the visual representation of the element as being pinged.
  161. /// </summary>
  162. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  163. public void MarkAsPinged(bool enable)
  164. {
  165. if ((int)underlayState > (int)UnderlayState.Pinged)
  166. return;
  167. if (enable)
  168. {
  169. CreateUnderlay();
  170. underlay.SetTint(PING_COLOR);
  171. underlayState = UnderlayState.Pinged;
  172. }
  173. else
  174. {
  175. ClearUnderlay();
  176. underlayState = UnderlayState.None;
  177. }
  178. }
  179. /// <summary>
  180. /// Changes the visual representation of the element as being hovered over.
  181. /// </summary>
  182. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  183. public void MarkAsHovered(bool enable)
  184. {
  185. if ((int)underlayState > (int)UnderlayState.Hovered)
  186. return;
  187. if (enable)
  188. {
  189. CreateUnderlay();
  190. underlay.SetTint(HOVER_COLOR);
  191. underlayState = UnderlayState.Hovered;
  192. }
  193. else
  194. {
  195. ClearUnderlay();
  196. underlayState = UnderlayState.None;
  197. }
  198. }
  199. /// <summary>
  200. /// Starts a rename operation over the entry, displaying the rename input box.
  201. /// </summary>
  202. public void StartRename()
  203. {
  204. if (renameTextBox != null)
  205. return;
  206. renameTextBox = new GUITextBox(true);
  207. Rect2I renameBounds = label.Bounds;
  208. // Rename box allows for less space for text than label, so adjust it slightly so it's more likely to be able
  209. // to display all visible text.
  210. renameBounds.x -= 4;
  211. renameBounds.width += 8;
  212. renameBounds.height += 8;
  213. renameTextBox.Bounds = renameBounds;
  214. owner.RenameOverlay.AddElement(renameTextBox);
  215. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  216. renameTextBox.Text = name;
  217. renameTextBox.Focus = true;
  218. }
  219. /// <summary>
  220. /// Stops a rename operation over the entry, hiding the rename input box.
  221. /// </summary>
  222. public void StopRename()
  223. {
  224. if (renameTextBox != null)
  225. {
  226. renameTextBox.Destroy();
  227. renameTextBox = null;
  228. }
  229. }
  230. /// <summary>
  231. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  232. /// </summary>
  233. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  234. public string GetRenamedName()
  235. {
  236. if (renameTextBox != null)
  237. return renameTextBox.Text;
  238. return "";
  239. }
  240. /// <summary>
  241. /// Clears the underlay GUI element (e.g. ping, hover, select).
  242. /// </summary>
  243. private void ClearUnderlay()
  244. {
  245. if (underlay != null)
  246. {
  247. underlay.Destroy();
  248. underlay = null;
  249. }
  250. underlayState = UnderlayState.None;
  251. }
  252. /// <summary>
  253. /// Creates a GUI elements that may be used for underlay effects (e.g. ping, hover, select).
  254. /// </summary>
  255. private void CreateUnderlay()
  256. {
  257. if (underlay == null)
  258. {
  259. underlay = new GUITexture(Builtin.WhiteTexture);
  260. underlay.Bounds = Bounds;
  261. owner.Underlay.AddElement(underlay);
  262. }
  263. }
  264. /// <summary>
  265. /// Triggered when the user clicks on the entry.
  266. /// </summary>
  267. /// <param name="path">Project library path of the clicked entry.</param>
  268. private void OnEntryClicked(string path)
  269. {
  270. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  271. if (entry != null && entry.Type == LibraryEntryType.Directory)
  272. {
  273. // If entry is a directory delay selection as it might be a double-click, in which case we want to keep
  274. // whatever selection is active currently so that user can perform drag and drop with its inspector
  275. // from the folder he is browsing to.
  276. delayedSelect = true;
  277. delayedSelectTime = Time.RealElapsed + 0.5f;
  278. }
  279. else
  280. owner.Window.Select(path);
  281. }
  282. /// <summary>
  283. /// Triggered when the user double-clicked on the entry.
  284. /// </summary>
  285. /// <param name="path">Project library path of the double-clicked entry.</param>
  286. private void OnEntryDoubleClicked(string path)
  287. {
  288. delayedSelect = false;
  289. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  290. if (entry != null)
  291. {
  292. if (entry.Type == LibraryEntryType.Directory)
  293. owner.Window.EnterDirectory(path);
  294. else
  295. {
  296. FileEntry resEntry = (FileEntry)entry;
  297. if (resEntry.ResType == ResourceType.Prefab)
  298. {
  299. EditorApplication.LoadScene(resEntry.Path);
  300. }
  301. else if (resEntry.ResType == ResourceType.ScriptCode)
  302. {
  303. ProgressBar.Show("Opening external code editor...", 1.0f);
  304. delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
  305. }
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// Returns an icon that can be used for displaying a resource of the specified type.
  311. /// </summary>
  312. /// <param name="entry">Project library entry of the resource to retrieve icon for.</param>
  313. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  314. /// <returns>Icon to display for the specified entry.</returns>
  315. private static SpriteTexture GetIcon(LibraryEntry entry, int size)
  316. {
  317. if (entry.Type == LibraryEntryType.Directory)
  318. {
  319. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Folder, size);
  320. }
  321. else
  322. {
  323. FileEntry fileEntry = (FileEntry)entry;
  324. switch (fileEntry.ResType)
  325. {
  326. case ResourceType.Font:
  327. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Font, size);
  328. case ResourceType.Mesh:
  329. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Mesh, size);
  330. case ResourceType.Texture:
  331. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Texture, size);
  332. case ResourceType.PlainText:
  333. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PlainText, size);
  334. case ResourceType.ScriptCode:
  335. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.ScriptCode, size);
  336. case ResourceType.SpriteTexture:
  337. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.SpriteTexture, size);
  338. case ResourceType.Shader:
  339. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  340. case ResourceType.ShaderInclude:
  341. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  342. case ResourceType.Material:
  343. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Material, size);
  344. case ResourceType.Prefab:
  345. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Prefab, size);
  346. case ResourceType.GUISkin:
  347. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.GUISkin, size);
  348. }
  349. }
  350. return null;
  351. }
  352. }
  353. }