LibraryGUIEntry.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. /** @addtogroup Library
  8. * @{
  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 static readonly Color PING_COLOR = Color.BansheeOrange;
  16. private static readonly Color SELECTION_COLOR = Color.DarkCyan;
  17. private static readonly Color HOVER_COLOR = new Color(Color.DarkCyan.r, Color.DarkCyan.g, Color.DarkCyan.b, 0.5f);
  18. private static readonly Color CUT_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.5f);
  19. private const int VERT_PADDING = 3;
  20. private const int BG_HORZ_PADDING = 2;
  21. private const int BG_VERT_PADDING = 2;
  22. private const string LibraryEntryFirstBg = "LibraryEntryFirstBg";
  23. private const string LibraryEntryBg = "LibraryEntryBg";
  24. private const string LibraryEntryLastBg = "LibraryEntryLastBg";
  25. private const string LibraryEntryVertFirstBg = "LibraryEntryVertFirstBg";
  26. private const string LibraryEntryVertBg = "LibraryEntryVertBg";
  27. private const string LibraryEntryVertLastBg = "LibraryEntryVertLastBg";
  28. /// <summary>
  29. /// Possible visual states for the resource tile.
  30. /// </summary>
  31. enum UnderlayState // Note: Order of these is relevant
  32. {
  33. None, Hovered, Selected, Pinged
  34. }
  35. public int index;
  36. public string path;
  37. public GUITexture icon;
  38. public GUILabel label;
  39. public Rect2I bounds;
  40. private GUITexture underlay;
  41. private GUITexture groupUnderlay;
  42. private LibraryGUIContent owner;
  43. private UnderlayState underlayState;
  44. private GUITextBox renameTextBox;
  45. private int width;
  46. private LibraryGUIEntryType type;
  47. private bool delayedSelect;
  48. private float delayedSelectTime;
  49. private ulong delayedOpenCodeEditorFrame = ulong.MaxValue;
  50. /// <summary>
  51. /// Bounds of the entry relative to part content area.
  52. /// </summary>
  53. public Rect2I Bounds
  54. {
  55. get { return bounds; }
  56. }
  57. /// <summary>
  58. /// Constructs a new resource tile entry.
  59. /// </summary>
  60. /// <param name="owner">Content area this entry is part of.</param>
  61. /// <param name="parent">Parent layout to add this entry's GUI elements to.</param>
  62. /// <param name="path">Path to the project library entry to display data for.</param>
  63. /// <param name="index">Sequential index of the entry in the conent area.</param>
  64. /// <param name="width">Width of the GUI entry.</param>
  65. /// <param name="height">Maximum allowed height for the label.</param>"
  66. /// <param name="type">Type of the entry, which controls its style and/or behaviour.</param>
  67. public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, string path, int index, int width, int height,
  68. LibraryGUIEntryType type)
  69. {
  70. GUILayout entryLayout;
  71. if (owner.GridLayout)
  72. entryLayout = parent.AddLayoutY();
  73. else
  74. entryLayout = parent.AddLayoutX();
  75. SpriteTexture iconTexture = GetIcon(path, owner.TileSize);
  76. icon = new GUITexture(iconTexture, GUITextureScaleMode.ScaleToFit,
  77. true, GUIOption.FixedHeight(owner.TileSize), GUIOption.FixedWidth(owner.TileSize));
  78. label = null;
  79. string name = PathEx.GetTail(path);
  80. if (owner.GridLayout)
  81. {
  82. int labelHeight = height - owner.TileSize;
  83. label = new GUILabel(name, EditorStyles.MultiLineLabelCentered,
  84. GUIOption.FixedWidth(width), GUIOption.FixedHeight(labelHeight));
  85. switch (type)
  86. {
  87. case LibraryGUIEntryType.Single:
  88. break;
  89. case LibraryGUIEntryType.MultiFirst:
  90. groupUnderlay = new GUITexture(null, LibraryEntryFirstBg);
  91. break;
  92. case LibraryGUIEntryType.MultiElement:
  93. groupUnderlay = new GUITexture(null, LibraryEntryBg);
  94. break;
  95. case LibraryGUIEntryType.MultiLast:
  96. groupUnderlay = new GUITexture(null, LibraryEntryLastBg);
  97. break;
  98. }
  99. }
  100. else
  101. {
  102. label = new GUILabel(name, GUIOption.FixedWidth(width - owner.TileSize), GUIOption.FixedHeight(height));
  103. switch (type)
  104. {
  105. case LibraryGUIEntryType.Single:
  106. break;
  107. case LibraryGUIEntryType.MultiFirst:
  108. groupUnderlay = new GUITexture(null, LibraryEntryVertFirstBg);
  109. break;
  110. case LibraryGUIEntryType.MultiElement:
  111. groupUnderlay = new GUITexture(null, LibraryEntryVertBg);
  112. break;
  113. case LibraryGUIEntryType.MultiLast:
  114. groupUnderlay = new GUITexture(null, LibraryEntryVertLastBg);
  115. break;
  116. }
  117. }
  118. entryLayout.AddElement(icon);
  119. entryLayout.AddElement(label);
  120. if (groupUnderlay != null)
  121. owner.DeepUnderlay.AddElement(groupUnderlay);
  122. this.owner = owner;
  123. this.index = index;
  124. this.path = path;
  125. this.bounds = new Rect2I();
  126. this.underlay = null;
  127. this.type = type;
  128. this.width = width;
  129. }
  130. /// <summary>
  131. /// Positions the GUI elements. Must be called after construction, but only after all content area entries have
  132. /// been constructed so that entry's final bounds are known.
  133. /// </summary>
  134. public void Initialize()
  135. {
  136. Rect2I iconBounds = icon.Bounds;
  137. bounds = iconBounds;
  138. Rect2I labelBounds = label.Bounds;
  139. if (owner.GridLayout)
  140. {
  141. bounds.x = labelBounds.x;
  142. bounds.y -= VERT_PADDING;
  143. bounds.width = labelBounds.width;
  144. bounds.height = (labelBounds.y + labelBounds.height + VERT_PADDING) - bounds.y;
  145. }
  146. else
  147. {
  148. bounds.y -= VERT_PADDING;
  149. bounds.width = width;
  150. bounds.height += VERT_PADDING;
  151. }
  152. string hoistedPath = path;
  153. GUIButton overlayBtn = new GUIButton("", EditorStyles.Blank);
  154. overlayBtn.Bounds = bounds;
  155. overlayBtn.OnClick += () => OnEntryClicked(hoistedPath);
  156. overlayBtn.OnDoubleClick += () => OnEntryDoubleClicked(hoistedPath);
  157. overlayBtn.SetContextMenu(owner.Window.ContextMenu);
  158. owner.Overlay.AddElement(overlayBtn);
  159. if (groupUnderlay != null)
  160. {
  161. if (owner.GridLayout)
  162. {
  163. int offsetToNext = BG_HORZ_PADDING + owner.HorzElementSpacing;
  164. if (type == LibraryGUIEntryType.MultiLast)
  165. offsetToNext = BG_HORZ_PADDING * 2;
  166. Rect2I bgBounds = new Rect2I(bounds.x - BG_HORZ_PADDING, bounds.y,
  167. bounds.width + offsetToNext, bounds.height);
  168. groupUnderlay.Bounds = bgBounds;
  169. }
  170. else
  171. {
  172. int offsetToNext = BG_VERT_PADDING + LibraryGUIContent.LIST_ENTRY_SPACING;
  173. if (type == LibraryGUIEntryType.MultiLast)
  174. offsetToNext = BG_VERT_PADDING * 2;
  175. Rect2I bgBounds = new Rect2I(bounds.x, bounds.y - BG_VERT_PADDING,
  176. bounds.width, bounds.height + offsetToNext);
  177. groupUnderlay.Bounds = bgBounds;
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Called every frame.
  183. /// </summary>
  184. public void Update()
  185. {
  186. if (delayedSelect && Time.RealElapsed > delayedSelectTime)
  187. {
  188. owner.Window.Select(path);
  189. delayedSelect = false;
  190. }
  191. if (delayedOpenCodeEditorFrame == Time.FrameIdx)
  192. {
  193. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  194. if (entry != null && entry.Type == LibraryEntryType.File)
  195. {
  196. FileEntry resEntry = (FileEntry) entry;
  197. CodeEditor.OpenFile(resEntry.Path, 0);
  198. }
  199. ProgressBar.Hide();
  200. }
  201. }
  202. /// <summary>
  203. /// Changes the visual representation of the element as being cut.
  204. /// </summary>
  205. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  206. public void MarkAsCut(bool enable)
  207. {
  208. if (enable)
  209. icon.SetTint(CUT_COLOR);
  210. else
  211. icon.SetTint(Color.White);
  212. }
  213. /// <summary>
  214. /// Changes the visual representation of the element as being selected.
  215. /// </summary>
  216. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  217. public void MarkAsSelected(bool enable)
  218. {
  219. if ((int)underlayState > (int)UnderlayState.Selected)
  220. return;
  221. if (enable)
  222. {
  223. CreateUnderlay();
  224. underlay.SetTint(SELECTION_COLOR);
  225. underlayState = UnderlayState.Selected;
  226. }
  227. else
  228. {
  229. ClearUnderlay();
  230. underlayState = UnderlayState.None;
  231. }
  232. }
  233. /// <summary>
  234. /// Changes the visual representation of the element as being pinged.
  235. /// </summary>
  236. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  237. public void MarkAsPinged(bool enable)
  238. {
  239. if ((int)underlayState > (int)UnderlayState.Pinged)
  240. return;
  241. if (enable)
  242. {
  243. CreateUnderlay();
  244. underlay.SetTint(PING_COLOR);
  245. underlayState = UnderlayState.Pinged;
  246. }
  247. else
  248. {
  249. ClearUnderlay();
  250. underlayState = UnderlayState.None;
  251. }
  252. }
  253. /// <summary>
  254. /// Changes the visual representation of the element as being hovered over.
  255. /// </summary>
  256. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  257. public void MarkAsHovered(bool enable)
  258. {
  259. if ((int)underlayState > (int)UnderlayState.Hovered)
  260. return;
  261. if (enable)
  262. {
  263. CreateUnderlay();
  264. underlay.SetTint(HOVER_COLOR);
  265. underlayState = UnderlayState.Hovered;
  266. }
  267. else
  268. {
  269. ClearUnderlay();
  270. underlayState = UnderlayState.None;
  271. }
  272. }
  273. /// <summary>
  274. /// Starts a rename operation over the entry, displaying the rename input box.
  275. /// </summary>
  276. public void StartRename()
  277. {
  278. if (renameTextBox != null)
  279. return;
  280. renameTextBox = new GUITextBox(true);
  281. Rect2I renameBounds = label.Bounds;
  282. // Rename box allows for less space for text than label, so adjust it slightly so it's more likely to be able
  283. // to display all visible text.
  284. renameBounds.x -= 4;
  285. renameBounds.width += 8;
  286. renameBounds.height += 8;
  287. renameTextBox.Bounds = renameBounds;
  288. owner.RenameOverlay.AddElement(renameTextBox);
  289. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  290. renameTextBox.Text = name;
  291. renameTextBox.Focus = true;
  292. }
  293. /// <summary>
  294. /// Stops a rename operation over the entry, hiding the rename input box.
  295. /// </summary>
  296. public void StopRename()
  297. {
  298. if (renameTextBox != null)
  299. {
  300. renameTextBox.Destroy();
  301. renameTextBox = null;
  302. }
  303. }
  304. /// <summary>
  305. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  306. /// </summary>
  307. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  308. public string GetRenamedName()
  309. {
  310. if (renameTextBox != null)
  311. return renameTextBox.Text;
  312. return "";
  313. }
  314. /// <summary>
  315. /// Clears the underlay GUI element (for example ping, hover, select).
  316. /// </summary>
  317. private void ClearUnderlay()
  318. {
  319. if (underlay != null)
  320. {
  321. underlay.Destroy();
  322. underlay = null;
  323. }
  324. underlayState = UnderlayState.None;
  325. }
  326. /// <summary>
  327. /// Creates a GUI elements that may be used for underlay effects (for example ping, hover, select).
  328. /// </summary>
  329. private void CreateUnderlay()
  330. {
  331. if (underlay == null)
  332. {
  333. underlay = new GUITexture(Builtin.WhiteTexture);
  334. underlay.Bounds = Bounds;
  335. owner.Underlay.AddElement(underlay);
  336. }
  337. }
  338. /// <summary>
  339. /// Triggered when the user clicks on the entry.
  340. /// </summary>
  341. /// <param name="path">Project library path of the clicked entry.</param>
  342. private void OnEntryClicked(string path)
  343. {
  344. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  345. if (entry != null && entry.Type == LibraryEntryType.Directory)
  346. {
  347. // If entry is a directory delay selection as it might be a double-click, in which case we want to keep
  348. // whatever selection is active currently so that user can perform drag and drop with its inspector
  349. // from the folder he is browsing to.
  350. delayedSelect = true;
  351. delayedSelectTime = Time.RealElapsed + 0.5f;
  352. }
  353. else
  354. owner.Window.Select(path);
  355. }
  356. /// <summary>
  357. /// Triggered when the user double-clicked on the entry.
  358. /// </summary>
  359. /// <param name="path">Project library path of the double-clicked entry.</param>
  360. private void OnEntryDoubleClicked(string path)
  361. {
  362. delayedSelect = false;
  363. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  364. if (entry != null)
  365. {
  366. if (entry.Type == LibraryEntryType.Directory)
  367. owner.Window.EnterDirectory(path);
  368. else
  369. {
  370. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  371. FileEntry fileEntry = (FileEntry)entry;
  372. if (meta.ResType == ResourceType.Prefab)
  373. {
  374. EditorApplication.LoadScene(fileEntry.Path);
  375. }
  376. else if (meta.ResType == ResourceType.ScriptCode)
  377. {
  378. ProgressBar.Show("Opening external code editor...", 1.0f);
  379. delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
  380. }
  381. }
  382. }
  383. }
  384. /// <summary>
  385. /// Returns an icon that can be used for displaying a resource of the specified type.
  386. /// </summary>
  387. /// <param name="path">Path to the project library entry to display data for.</param>
  388. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  389. /// <returns>Icon to display for the specified entry.</returns>
  390. private static SpriteTexture GetIcon(string path, int size)
  391. {
  392. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  393. if (entry.Type == LibraryEntryType.Directory)
  394. {
  395. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Folder, size);
  396. }
  397. else
  398. {
  399. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  400. switch (meta.ResType)
  401. {
  402. case ResourceType.Font:
  403. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Font, size);
  404. case ResourceType.Mesh:
  405. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Mesh, size);
  406. case ResourceType.Texture:
  407. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Texture, size);
  408. case ResourceType.PlainText:
  409. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PlainText, size);
  410. case ResourceType.ScriptCode:
  411. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.ScriptCode, size);
  412. case ResourceType.SpriteTexture:
  413. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.SpriteTexture, size);
  414. case ResourceType.Shader:
  415. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  416. case ResourceType.ShaderInclude:
  417. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  418. case ResourceType.Material:
  419. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Material, size);
  420. case ResourceType.Prefab:
  421. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Prefab, size);
  422. case ResourceType.GUISkin:
  423. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.GUISkin, size);
  424. case ResourceType.PhysicsMaterial:
  425. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMaterial, size);
  426. case ResourceType.PhysicsMesh:
  427. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMesh, size);
  428. case ResourceType.AudioClip:
  429. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.AudioClip, size);
  430. case ResourceType.AnimationClip:
  431. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.AnimationClip, size);
  432. }
  433. }
  434. return null;
  435. }
  436. }
  437. /// <summary>
  438. /// Type of <see cref="LibraryGUIEntry"/> that controls its look.
  439. /// </summary>
  440. internal enum LibraryGUIEntryType
  441. {
  442. /// <summary>
  443. /// Represents a single resource.
  444. /// </summary>
  445. Single,
  446. /// <summary>
  447. /// Represents the first entry in a multi-resource group.
  448. /// </summary>
  449. MultiFirst,
  450. /// <summary>
  451. /// Represents one of the mid entries in a multi-resource group.
  452. /// </summary>
  453. MultiElement,
  454. /// <summary>
  455. /// Represents the last entry in a multi-resource group.
  456. /// </summary>
  457. MultiLast
  458. }
  459. /** @} */
  460. }