LibraryGUIEntry.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /// <summary>
  38. /// Constructs a new resource tile entry.
  39. /// </summary>
  40. /// <param name="owner">Content area this entry is part of.</param>
  41. /// <param name="parent">Parent layout to add this entry's GUI elements to.</param>
  42. /// <param name="entry">Project library entry this entry displays data for.</param>
  43. /// <param name="index">Sequential index of the entry in the conent area.</param>
  44. /// <param name="labelWidth">Width of the GUI labels that display the elements.</param>
  45. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, LibraryEntry entry, int index, int labelWidth)
  46. {
  47. GUILayout entryLayout;
  48. if (owner.GridLayout)
  49. entryLayout = parent.AddLayoutY();
  50. else
  51. entryLayout = parent.AddLayoutX();
  52. SpriteTexture iconTexture = GetIcon(entry, owner.TileSize);
  53. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  54. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  55. label = null;
  56. if (owner.GridLayout)
  57. {
  58. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabelCentered,
  59. GUIOption.FixedWidth(labelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  60. }
  61. else
  62. {
  63. label = new GUILabel(entry.Name);
  64. }
  65. entryLayout.AddElement(icon);
  66. entryLayout.AddElement(label);
  67. this.owner = owner;
  68. this.index = index;
  69. this.path = entry.Path;
  70. this.bounds = new Rect2I();
  71. this.underlay = null;
  72. }
  73. /// <summary>
  74. /// Positions the GUI elements. Must be called after construction, but only after all content area entries have
  75. /// been constructed so that entry's final bounds are known.
  76. /// </summary>
  77. public void Initialize()
  78. {
  79. bounds = icon.Bounds;
  80. Rect2I labelBounds = label.Bounds;
  81. bounds.x = MathEx.Min(bounds.x, labelBounds.x - SELECTION_EXTRA_WIDTH);
  82. bounds.y = MathEx.Min(bounds.y, labelBounds.y) - 5; // 5 - Just padding for better look
  83. bounds.width = MathEx.Max(bounds.x + bounds.width,
  84. labelBounds.x + labelBounds.width) - bounds.x + SELECTION_EXTRA_WIDTH;
  85. bounds.height = MathEx.Max(bounds.y + bounds.height,
  86. labelBounds.y + labelBounds.height) - bounds.y;
  87. string hoistedPath = path;
  88. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  89. overlayBtn.Bounds = bounds;
  90. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  91. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  92. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  93. owner.Overlay.AddElement(overlayBtn);
  94. }
  95. /// <summary>
  96. /// Bounds of the entry relative to part content area.
  97. /// </summary>
  98. public Rect2I Bounds
  99. {
  100. get { return bounds; }
  101. }
  102. /// <summary>
  103. /// Changes the visual representation of the element as being cut.
  104. /// </summary>
  105. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  106. public void MarkAsCut(bool enable)
  107. {
  108. if (enable)
  109. icon.SetTint(CUT_COLOR);
  110. else
  111. icon.SetTint(Color.White);
  112. }
  113. /// <summary>
  114. /// Changes the visual representation of the element as being selected.
  115. /// </summary>
  116. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  117. public void MarkAsSelected(bool enable)
  118. {
  119. if ((int)underlayState > (int)UnderlayState.Selected)
  120. return;
  121. if (enable)
  122. {
  123. CreateUnderlay();
  124. underlay.SetTint(SELECTION_COLOR);
  125. }
  126. else
  127. ClearUnderlay();
  128. underlayState = UnderlayState.Selected;
  129. }
  130. /// <summary>
  131. /// Changes the visual representation of the element as being pinged.
  132. /// </summary>
  133. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  134. public void MarkAsPinged(bool enable)
  135. {
  136. if ((int)underlayState > (int)UnderlayState.Pinged)
  137. return;
  138. if (enable)
  139. {
  140. CreateUnderlay();
  141. underlay.SetTint(PING_COLOR);
  142. }
  143. else
  144. ClearUnderlay();
  145. underlayState = UnderlayState.Pinged;
  146. }
  147. /// <summary>
  148. /// Changes the visual representation of the element as being hovered over.
  149. /// </summary>
  150. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  151. public void MarkAsHovered(bool enable)
  152. {
  153. if ((int)underlayState > (int)UnderlayState.Hovered)
  154. return;
  155. if (enable)
  156. {
  157. CreateUnderlay();
  158. underlay.SetTint(HOVER_COLOR);
  159. }
  160. else
  161. ClearUnderlay();
  162. underlayState = UnderlayState.Hovered;
  163. }
  164. /// <summary>
  165. /// Starts a rename operation over the entry, displaying the rename input box.
  166. /// </summary>
  167. public void StartRename()
  168. {
  169. if (renameTextBox != null)
  170. return;
  171. renameTextBox = new GUITextBox(true);
  172. renameTextBox.Bounds = label.Bounds;
  173. owner.RenameOverlay.AddElement(renameTextBox);
  174. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  175. renameTextBox.Text = name;
  176. renameTextBox.Focus = true;
  177. label.Active = false;
  178. }
  179. /// <summary>
  180. /// Stops a rename operation over the entry, hiding the rename input box.
  181. /// </summary>
  182. public void StopRename()
  183. {
  184. if (renameTextBox != null)
  185. {
  186. renameTextBox.Destroy();
  187. renameTextBox = null;
  188. }
  189. label.Active = true;
  190. }
  191. /// <summary>
  192. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  193. /// </summary>
  194. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  195. public string GetRenamedName()
  196. {
  197. if (renameTextBox != null)
  198. return renameTextBox.Text;
  199. return "";
  200. }
  201. /// <summary>
  202. /// Clears the underlay GUI element (e.g. ping, hover, select).
  203. /// </summary>
  204. private void ClearUnderlay()
  205. {
  206. if (underlay != null)
  207. {
  208. underlay.Destroy();
  209. underlay = null;
  210. }
  211. underlayState = UnderlayState.None;
  212. }
  213. /// <summary>
  214. /// Creates a GUI elements that may be used for underlay effects (e.g. ping, hover, select).
  215. /// </summary>
  216. private void CreateUnderlay()
  217. {
  218. if (underlay == null)
  219. {
  220. underlay = new GUITexture(Builtin.WhiteTexture);
  221. underlay.Bounds = Bounds;
  222. owner.Underlay.AddElement(underlay);
  223. }
  224. }
  225. /// <summary>
  226. /// Triggered when the user clicks on the entry.
  227. /// </summary>
  228. /// <param name="path">Project library path of the clicked entry.</param>
  229. private void OnEntryClicked(string path)
  230. {
  231. owner.Window.Select(path);
  232. }
  233. /// <summary>
  234. /// Triggered when the user double-clicked on the entry.
  235. /// </summary>
  236. /// <param name="path">Project library path of the double-clicked entry.</param>
  237. private void OnEntryDoubleClicked(string path)
  238. {
  239. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  240. if (entry != null)
  241. {
  242. if (entry.Type == LibraryEntryType.Directory)
  243. owner.Window.EnterDirectory(path);
  244. else
  245. {
  246. FileEntry resEntry = (FileEntry)entry;
  247. if (resEntry.ResType == ResourceType.Prefab)
  248. {
  249. EditorApplication.LoadScene(resEntry.Path);
  250. }
  251. else if (resEntry.ResType == ResourceType.ScriptCode)
  252. {
  253. CodeEditor.OpenFile(resEntry.Path, 0);
  254. }
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Returns an icon that can be used for displaying a resource of the specified type.
  260. /// </summary>
  261. /// <param name="entry">Project library entry of the resource to retrieve icon for.</param>
  262. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  263. /// <returns>Icon to display for the specified entry.</returns>
  264. private static SpriteTexture GetIcon(LibraryEntry entry, int size)
  265. {
  266. if (entry.Type == LibraryEntryType.Directory)
  267. {
  268. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Folder, size);
  269. }
  270. else
  271. {
  272. FileEntry fileEntry = (FileEntry)entry;
  273. switch (fileEntry.ResType)
  274. {
  275. case ResourceType.Font:
  276. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Font, size);
  277. case ResourceType.Mesh:
  278. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Mesh, size);
  279. case ResourceType.Texture:
  280. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Texture, size);
  281. case ResourceType.PlainText:
  282. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PlainText, size);
  283. case ResourceType.ScriptCode:
  284. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.ScriptCode, size);
  285. case ResourceType.SpriteTexture:
  286. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.SpriteTexture, size);
  287. case ResourceType.Shader:
  288. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  289. case ResourceType.ShaderInclude:
  290. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  291. case ResourceType.Material:
  292. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Material, size);
  293. case ResourceType.Prefab:
  294. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Prefab, size);
  295. case ResourceType.GUISkin:
  296. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.GUISkin, size);
  297. }
  298. }
  299. return null;
  300. }
  301. }
  302. }