LibraryGUIEntry.cs 14 KB

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