LibraryGUIEntry.cs 16 KB

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