2
0

LibraryGUIEntry.cs 15 KB

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