LibraryGUIEntry.cs 21 KB

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