LibraryGUIEntry.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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. // Note: Order of these is relevant
  19. enum UnderlayState
  20. {
  21. None, Hovered, Selected, Pinged
  22. }
  23. public int index;
  24. public string path;
  25. public GUITexture icon;
  26. public GUILabel label;
  27. public Rect2I bounds;
  28. private GUITexture underlay;
  29. private LibraryGUIContent owner;
  30. private UnderlayState underlayState;
  31. private GUITextBox renameTextBox;
  32. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, LibraryEntry entry, int index)
  33. {
  34. GUILayout entryLayout;
  35. if (owner.GridLayout)
  36. entryLayout = parent.AddLayoutY();
  37. else
  38. entryLayout = parent.AddLayoutX();
  39. SpriteTexture iconTexture = GetIcon(entry);
  40. icon = new GUITexture(iconTexture, GUIImageScaleMode.ScaleToFit,
  41. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  42. label = null;
  43. if (owner.GridLayout)
  44. {
  45. label = new GUILabel(entry.Name, EditorStyles.MultiLineLabelCentered,
  46. GUIOption.FixedWidth(owner.LabelWidth), GUIOption.FlexibleHeight(0, MAX_LABEL_HEIGHT));
  47. }
  48. else
  49. {
  50. label = new GUILabel(entry.Name);
  51. }
  52. entryLayout.AddElement(icon);
  53. entryLayout.AddElement(label);
  54. this.owner = owner;
  55. this.index = index;
  56. this.path = entry.Path;
  57. this.bounds = new Rect2I();
  58. this.underlay = null;
  59. }
  60. public void Initialize()
  61. {
  62. bounds = icon.Bounds;
  63. Rect2I labelBounds = label.Bounds;
  64. bounds.x = MathEx.Min(bounds.x, labelBounds.x - SELECTION_EXTRA_WIDTH);
  65. bounds.y = MathEx.Min(bounds.y, labelBounds.y) - 5; // 5 - Just padding for better look
  66. bounds.width = MathEx.Max(bounds.x + bounds.width,
  67. labelBounds.x + labelBounds.width) - bounds.x + SELECTION_EXTRA_WIDTH;
  68. bounds.height = MathEx.Max(bounds.y + bounds.height,
  69. labelBounds.y + labelBounds.height) - bounds.y;
  70. string hoistedPath = path;
  71. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  72. overlayBtn.Bounds = bounds;
  73. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  74. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  75. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  76. owner.Overlay.AddElement(overlayBtn);
  77. }
  78. public Rect2I Bounds
  79. {
  80. get { return bounds; }
  81. }
  82. public void MarkAsCut(bool enable)
  83. {
  84. if (enable)
  85. icon.SetTint(CUT_COLOR);
  86. else
  87. icon.SetTint(Color.White);
  88. }
  89. public void MarkAsSelected(bool enable)
  90. {
  91. if ((int)underlayState > (int)UnderlayState.Selected)
  92. return;
  93. if (enable)
  94. {
  95. CreateUnderlay();
  96. underlay.SetTint(SELECTION_COLOR);
  97. }
  98. else
  99. ClearUnderlay();
  100. underlayState = UnderlayState.Selected;
  101. }
  102. public void MarkAsPinged(bool enable)
  103. {
  104. if ((int)underlayState > (int)UnderlayState.Pinged)
  105. return;
  106. if (enable)
  107. {
  108. CreateUnderlay();
  109. underlay.SetTint(PING_COLOR);
  110. }
  111. else
  112. ClearUnderlay();
  113. underlayState = UnderlayState.Pinged;
  114. }
  115. public void MarkAsHovered(bool enable)
  116. {
  117. if ((int)underlayState > (int)UnderlayState.Hovered)
  118. return;
  119. if (enable)
  120. {
  121. CreateUnderlay();
  122. underlay.SetTint(HOVER_COLOR);
  123. }
  124. else
  125. ClearUnderlay();
  126. underlayState = UnderlayState.Hovered;
  127. }
  128. public void StartRename()
  129. {
  130. if (renameTextBox != null)
  131. return;
  132. renameTextBox = new GUITextBox(false);
  133. renameTextBox.Bounds = label.Bounds;
  134. owner.InputOverlay.AddElement(renameTextBox);
  135. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  136. renameTextBox.Text = name;
  137. renameTextBox.Focus = true;
  138. label.Visible = false;
  139. }
  140. public void StopRename()
  141. {
  142. if (renameTextBox != null)
  143. {
  144. renameTextBox.Destroy();
  145. renameTextBox = null;
  146. }
  147. label.Visible = true;
  148. }
  149. public string GetRenamedName()
  150. {
  151. if (renameTextBox != null)
  152. return renameTextBox.Text;
  153. return "";
  154. }
  155. private void ClearUnderlay()
  156. {
  157. if (underlay != null)
  158. {
  159. underlay.Destroy();
  160. underlay = null;
  161. }
  162. underlayState = UnderlayState.None;
  163. }
  164. private void CreateUnderlay()
  165. {
  166. if (underlay == null)
  167. {
  168. underlay = new GUITexture(Builtin.WhiteTexture);
  169. underlay.Bounds = Bounds;
  170. owner.Underlay.AddElement(underlay);
  171. }
  172. }
  173. private void OnEntryClicked(string path)
  174. {
  175. owner.Window.Select(path);
  176. }
  177. private void OnEntryDoubleClicked(string path)
  178. {
  179. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  180. if (entry != null)
  181. {
  182. if (entry.Type == LibraryEntryType.Directory)
  183. owner.Window.EnterDirectory(path);
  184. else
  185. {
  186. FileEntry resEntry = (FileEntry)entry;
  187. if (resEntry.ResType == ResourceType.Prefab)
  188. {
  189. EditorApplication.LoadScene(resEntry.Path);
  190. }
  191. }
  192. }
  193. }
  194. private static SpriteTexture GetIcon(LibraryEntry entry)
  195. {
  196. if (entry.Type == LibraryEntryType.Directory)
  197. {
  198. return EditorBuiltin.FolderIcon;
  199. }
  200. else
  201. {
  202. FileEntry fileEntry = (FileEntry)entry;
  203. switch (fileEntry.ResType)
  204. {
  205. case ResourceType.Font:
  206. return EditorBuiltin.FontIcon;
  207. case ResourceType.Mesh:
  208. return EditorBuiltin.MeshIcon;
  209. case ResourceType.Texture:
  210. return EditorBuiltin.TextureIcon;
  211. case ResourceType.PlainText:
  212. return EditorBuiltin.PlainTextIcon;
  213. case ResourceType.ScriptCode:
  214. return EditorBuiltin.ScriptCodeIcon;
  215. case ResourceType.SpriteTexture:
  216. return EditorBuiltin.SpriteTextureIcon;
  217. case ResourceType.Shader:
  218. return EditorBuiltin.ShaderIcon;
  219. case ResourceType.Material:
  220. return EditorBuiltin.MaterialIcon;
  221. case ResourceType.Prefab:
  222. return EditorBuiltin.PrefabIcon;
  223. }
  224. }
  225. return null;
  226. }
  227. }
  228. }