LibraryGUIEntry.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. overlayBtn.AcceptsKeyFocus = false;
  159. owner.Overlay.AddElement(overlayBtn);
  160. if (groupUnderlay != null)
  161. {
  162. if (owner.GridLayout)
  163. {
  164. int offsetToNext = BG_HORZ_PADDING + owner.HorzElementSpacing;
  165. if (type == LibraryGUIEntryType.MultiLast)
  166. offsetToNext = BG_HORZ_PADDING * 2;
  167. Rect2I bgBounds = new Rect2I(bounds.x - BG_HORZ_PADDING, bounds.y,
  168. bounds.width + offsetToNext, bounds.height);
  169. groupUnderlay.Bounds = bgBounds;
  170. }
  171. else
  172. {
  173. int offsetToNext = BG_VERT_PADDING + LibraryGUIContent.LIST_ENTRY_SPACING;
  174. if (type == LibraryGUIEntryType.MultiLast)
  175. offsetToNext = BG_VERT_PADDING * 2;
  176. Rect2I bgBounds = new Rect2I(bounds.x, bounds.y - BG_VERT_PADDING,
  177. bounds.width, bounds.height + offsetToNext);
  178. groupUnderlay.Bounds = bgBounds;
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// Called every frame.
  184. /// </summary>
  185. public void Update()
  186. {
  187. if (delayedSelect && Time.RealElapsed > delayedSelectTime)
  188. {
  189. owner.Window.Select(path);
  190. delayedSelect = false;
  191. }
  192. if (delayedOpenCodeEditorFrame == Time.FrameIdx)
  193. {
  194. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  195. if (entry != null && entry.Type == LibraryEntryType.File)
  196. {
  197. FileEntry resEntry = (FileEntry) entry;
  198. CodeEditor.OpenFile(resEntry.Path, 0);
  199. }
  200. ProgressBar.Hide();
  201. }
  202. }
  203. /// <summary>
  204. /// Changes the visual representation of the element as being cut.
  205. /// </summary>
  206. /// <param name="enable">True if mark as cut, false to reset to normal.</param>
  207. public void MarkAsCut(bool enable)
  208. {
  209. if (enable)
  210. icon.SetTint(CUT_COLOR);
  211. else
  212. icon.SetTint(Color.White);
  213. }
  214. /// <summary>
  215. /// Changes the visual representation of the element as being selected.
  216. /// </summary>
  217. /// <param name="enable">True if mark as selected, false to reset to normal.</param>
  218. public void MarkAsSelected(bool enable)
  219. {
  220. if ((int)underlayState > (int)UnderlayState.Selected)
  221. return;
  222. if (enable)
  223. {
  224. CreateUnderlay();
  225. underlay.SetTint(SELECTION_COLOR);
  226. underlayState = UnderlayState.Selected;
  227. }
  228. else
  229. {
  230. ClearUnderlay();
  231. underlayState = UnderlayState.None;
  232. }
  233. }
  234. /// <summary>
  235. /// Changes the visual representation of the element as being pinged.
  236. /// </summary>
  237. /// <param name="enable">True if mark as pinged, false to reset to normal.</param>
  238. public void MarkAsPinged(bool enable)
  239. {
  240. if ((int)underlayState > (int)UnderlayState.Pinged)
  241. return;
  242. if (enable)
  243. {
  244. CreateUnderlay();
  245. underlay.SetTint(PING_COLOR);
  246. underlayState = UnderlayState.Pinged;
  247. }
  248. else
  249. {
  250. ClearUnderlay();
  251. underlayState = UnderlayState.None;
  252. }
  253. }
  254. /// <summary>
  255. /// Changes the visual representation of the element as being hovered over.
  256. /// </summary>
  257. /// <param name="enable">True if mark as hovered, false to reset to normal.</param>
  258. public void MarkAsHovered(bool enable)
  259. {
  260. if ((int)underlayState > (int)UnderlayState.Hovered)
  261. return;
  262. if (enable)
  263. {
  264. CreateUnderlay();
  265. underlay.SetTint(HOVER_COLOR);
  266. underlayState = UnderlayState.Hovered;
  267. }
  268. else
  269. {
  270. ClearUnderlay();
  271. underlayState = UnderlayState.None;
  272. }
  273. }
  274. /// <summary>
  275. /// Starts a rename operation over the entry, displaying the rename input box.
  276. /// </summary>
  277. public void StartRename()
  278. {
  279. if (renameTextBox != null)
  280. return;
  281. renameTextBox = new GUITextBox(true);
  282. Rect2I renameBounds = label.Bounds;
  283. // Rename box allows for less space for text than label, so adjust it slightly so it's more likely to be able
  284. // to display all visible text.
  285. renameBounds.x -= 4;
  286. renameBounds.width += 8;
  287. renameBounds.height += 8;
  288. renameTextBox.Bounds = renameBounds;
  289. owner.RenameOverlay.AddElement(renameTextBox);
  290. string name = Path.GetFileNameWithoutExtension(PathEx.GetTail(path));
  291. renameTextBox.Text = name;
  292. renameTextBox.Focus = true;
  293. }
  294. /// <summary>
  295. /// Stops a rename operation over the entry, hiding the rename input box.
  296. /// </summary>
  297. public void StopRename()
  298. {
  299. if (renameTextBox != null)
  300. {
  301. renameTextBox.Destroy();
  302. renameTextBox = null;
  303. }
  304. }
  305. /// <summary>
  306. /// Gets the new name of the entry. Only valid while a rename operation is in progress.
  307. /// </summary>
  308. /// <returns>New name of the entry currently entered in the rename input box.</returns>
  309. public string GetRenamedName()
  310. {
  311. if (renameTextBox != null)
  312. return renameTextBox.Text;
  313. return "";
  314. }
  315. /// <summary>
  316. /// Clears the underlay GUI element (for example ping, hover, select).
  317. /// </summary>
  318. private void ClearUnderlay()
  319. {
  320. if (underlay != null)
  321. {
  322. underlay.Destroy();
  323. underlay = null;
  324. }
  325. underlayState = UnderlayState.None;
  326. }
  327. /// <summary>
  328. /// Creates a GUI elements that may be used for underlay effects (for example ping, hover, select).
  329. /// </summary>
  330. private void CreateUnderlay()
  331. {
  332. if (underlay == null)
  333. {
  334. underlay = new GUITexture(Builtin.WhiteTexture);
  335. underlay.Bounds = Bounds;
  336. owner.Underlay.AddElement(underlay);
  337. }
  338. }
  339. /// <summary>
  340. /// Triggered when the user clicks on the entry.
  341. /// </summary>
  342. /// <param name="path">Project library path of the clicked entry.</param>
  343. private void OnEntryClicked(string path)
  344. {
  345. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  346. if (entry != null && entry.Type == LibraryEntryType.Directory)
  347. {
  348. // If entry is a directory delay selection as it might be a double-click, in which case we want to keep
  349. // whatever selection is active currently so that user can perform drag and drop with its inspector
  350. // from the folder he is browsing to.
  351. delayedSelect = true;
  352. delayedSelectTime = Time.RealElapsed + 0.5f;
  353. }
  354. else
  355. owner.Window.Select(path);
  356. }
  357. /// <summary>
  358. /// Triggered when the user double-clicked on the entry.
  359. /// </summary>
  360. /// <param name="path">Project library path of the double-clicked entry.</param>
  361. private void OnEntryDoubleClicked(string path)
  362. {
  363. delayedSelect = false;
  364. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  365. if (entry != null)
  366. {
  367. if (entry.Type == LibraryEntryType.Directory)
  368. owner.Window.EnterDirectory(path);
  369. else
  370. {
  371. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  372. FileEntry fileEntry = (FileEntry)entry;
  373. if (meta.ResType == ResourceType.Prefab)
  374. {
  375. EditorApplication.LoadScene(fileEntry.Path);
  376. }
  377. else if (meta.ResType == ResourceType.ScriptCode)
  378. {
  379. ProgressBar.Show("Opening external code editor...", 1.0f);
  380. delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
  381. }
  382. }
  383. }
  384. }
  385. /// <summary>
  386. /// Returns an icon that can be used for displaying a resource of the specified type.
  387. /// </summary>
  388. /// <param name="path">Path to the project library entry to display data for.</param>
  389. /// <param name="size">Size of the icon to retrieve, in pixels.</param>
  390. /// <returns>Icon to display for the specified entry.</returns>
  391. private static SpriteTexture GetIcon(string path, int size)
  392. {
  393. LibraryEntry entry = ProjectLibrary.GetEntry(path);
  394. if (entry.Type == LibraryEntryType.Directory)
  395. {
  396. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Folder, size);
  397. }
  398. else
  399. {
  400. ResourceMeta meta = ProjectLibrary.GetMeta(path);
  401. ProjectResourceIcons icons = meta.Icons;
  402. Texture icon;
  403. if (size <= 16)
  404. icon = icons.icon16;
  405. else if (size <= 32)
  406. icon = icons.icon32;
  407. else if (size <= 48)
  408. icon = icons.icon48;
  409. else
  410. icon = icons.icon64;
  411. if(icon != null)
  412. return new SpriteTexture(icon);
  413. switch (meta.ResType)
  414. {
  415. case ResourceType.Font:
  416. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Font, size);
  417. case ResourceType.Mesh:
  418. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Mesh, size);
  419. case ResourceType.Texture:
  420. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Texture, size);
  421. case ResourceType.PlainText:
  422. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PlainText, size);
  423. case ResourceType.ScriptCode:
  424. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.ScriptCode, size);
  425. case ResourceType.SpriteTexture:
  426. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.SpriteTexture, size);
  427. case ResourceType.Shader:
  428. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  429. case ResourceType.ShaderInclude:
  430. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Shader, size);
  431. case ResourceType.Material:
  432. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Material, size);
  433. case ResourceType.Prefab:
  434. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.Prefab, size);
  435. case ResourceType.GUISkin:
  436. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.GUISkin, size);
  437. case ResourceType.PhysicsMaterial:
  438. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMaterial, size);
  439. case ResourceType.PhysicsMesh:
  440. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.PhysicsMesh, size);
  441. case ResourceType.AudioClip:
  442. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.AudioClip, size);
  443. case ResourceType.AnimationClip:
  444. return EditorBuiltin.GetLibraryItemIcon(LibraryItemIcon.AnimationClip, size);
  445. }
  446. }
  447. return null;
  448. }
  449. }
  450. /// <summary>
  451. /// Type of <see cref="LibraryGUIEntry"/> that controls its look.
  452. /// </summary>
  453. internal enum LibraryGUIEntryType
  454. {
  455. /// <summary>
  456. /// Represents a single resource.
  457. /// </summary>
  458. Single,
  459. /// <summary>
  460. /// Represents the first entry in a multi-resource group.
  461. /// </summary>
  462. MultiFirst,
  463. /// <summary>
  464. /// Represents one of the mid entries in a multi-resource group.
  465. /// </summary>
  466. MultiElement,
  467. /// <summary>
  468. /// Represents the last entry in a multi-resource group.
  469. /// </summary>
  470. MultiLast
  471. }
  472. /** @} */
  473. }