SizeConfigCollection.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace TheraBytes.BetterUi
  7. {
  8. public interface ISizeConfigCollection
  9. {
  10. string GetCurrentConfigName();
  11. void MarkDirty();
  12. bool IsDirty { get; }
  13. void Sort();
  14. }
  15. [Serializable]
  16. public class SizeConfigCollection<T> : ISizeConfigCollection
  17. where T : class, IScreenConfigConnection
  18. {
  19. [SerializeField]
  20. List<T> items = new List<T>();
  21. public IReadOnlyList<T> Items { get { return items; } }
  22. bool isDirty = true;
  23. public bool IsDirty { get { return isDirty; } }
  24. public void AddItem(T item)
  25. {
  26. items.Add(item);
  27. MarkDirty();
  28. }
  29. public void Sort()
  30. {
  31. if (!isDirty)
  32. return;
  33. List<string> order = ResolutionMonitor.Instance.OptimizedScreens.Select(o => o.Name).ToList();
  34. items.Sort((a, b) => order.IndexOf(a.ScreenConfigName).CompareTo(order.IndexOf(b.ScreenConfigName)));
  35. isDirty = false;
  36. }
  37. public string GetCurrentConfigName()
  38. {
  39. T result = GetCurrentItem(null);
  40. if (result != null)
  41. return result.ScreenConfigName;
  42. return null;
  43. }
  44. public T GetItemForConfig(string configName, T fallback)
  45. {
  46. foreach(var itm in items)
  47. {
  48. if (itm.ScreenConfigName == configName)
  49. return itm;
  50. }
  51. return fallback;
  52. }
  53. public T GetCurrentItem(T fallback)
  54. {
  55. // if there is no config matching the screen
  56. if (ResolutionMonitor.CurrentScreenConfiguration == null)
  57. return fallback;
  58. Sort();
  59. #if UNITY_EDITOR
  60. // simulation
  61. var config = ResolutionMonitor.SimulatedScreenConfig;
  62. if (config != null)
  63. {
  64. if (Items.Any(o => o.ScreenConfigName == config.Name))
  65. {
  66. return Items.First(o => o.ScreenConfigName == config.Name);
  67. }
  68. }
  69. #endif
  70. // search for screen config
  71. foreach (T item in items)
  72. {
  73. if (string.IsNullOrEmpty(item.ScreenConfigName))
  74. return fallback;
  75. var c = ResolutionMonitor.GetConfig(item.ScreenConfigName);
  76. if(c != null && c.IsActive)
  77. {
  78. return item;
  79. }
  80. }
  81. // fallback logic
  82. foreach (var conf in ResolutionMonitor.GetCurrentScreenConfigurations())
  83. {
  84. foreach (var c in conf.Fallbacks)
  85. {
  86. var matchingItem = items.FirstOrDefault(o => o.ScreenConfigName == c);
  87. if (matchingItem != null)
  88. return matchingItem;
  89. }
  90. }
  91. // final fallback
  92. return fallback;
  93. }
  94. public void MarkDirty()
  95. {
  96. isDirty = true;
  97. }
  98. }
  99. }