RecentItems.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProVideo.Editor
  8. {
  9. /// <summary>
  10. /// </summary>
  11. public static class RecentItems
  12. {
  13. private const int MaxRecentItems = 16;
  14. private static List<string> _recentFiles = new List<string>(MaxRecentItems);
  15. private static List<string> _recentUrls = new List<string>(MaxRecentItems);
  16. // TODO: add a list for favourites to allow user to create their own list?
  17. public static List<string> Files { get { return _recentFiles; } }
  18. public static List<string> Urls { get { return _recentUrls; } }
  19. static RecentItems()
  20. {
  21. MediaPlayer.InternalMediaLoadedEvent.RemoveListener(Add);
  22. MediaPlayer.InternalMediaLoadedEvent.AddListener(Add);
  23. }
  24. public static void Load()
  25. {
  26. _recentFiles = EditorHelper.GetEditorPrefsToStringList(MediaPlayerEditor.SettingsPrefix + "RecentFiles");
  27. _recentUrls = EditorHelper.GetEditorPrefsToStringList(MediaPlayerEditor.SettingsPrefix + "RecentUrls");
  28. }
  29. public static void Save()
  30. {
  31. EditorHelper.SetEditorPrefsFromStringList(MediaPlayerEditor.SettingsPrefix + "RecentFiles", _recentFiles);
  32. EditorHelper.SetEditorPrefsFromStringList(MediaPlayerEditor.SettingsPrefix + "RecentUrls", _recentUrls);
  33. }
  34. public static void Add(string path)
  35. {
  36. if (path.Contains("://"))
  37. {
  38. Add(path, _recentUrls);
  39. }
  40. else
  41. {
  42. Add(path, _recentFiles);
  43. }
  44. }
  45. private static void Add(string path, List<string> list)
  46. {
  47. if (!list.Contains(path))
  48. {
  49. list.Insert(0, path);
  50. if (list.Count > MaxRecentItems)
  51. {
  52. // Remove the oldest item from the list
  53. list.RemoveAt(list.Count - 1);
  54. }
  55. }
  56. else
  57. {
  58. // If it already contains the item, then move it to the top
  59. list.Remove(path);
  60. list.Insert(0, path);
  61. }
  62. Save();
  63. }
  64. public static void ClearMissingFiles()
  65. {
  66. if (_recentFiles != null && _recentFiles.Count > 0)
  67. {
  68. List<string> newList = new List<string>(_recentFiles.Count);
  69. for (int i = 0; i < _recentFiles.Count; i++)
  70. {
  71. string path = _recentFiles[i];
  72. if (System.IO.File.Exists(path))
  73. {
  74. newList.Add(path);
  75. }
  76. }
  77. _recentFiles = newList;
  78. }
  79. }
  80. }
  81. }