StyleSheet.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. namespace SRF.UI
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Helpers;
  7. using UnityEngine;
  8. [Serializable]
  9. public class Style
  10. {
  11. public Color ActiveColor = Color.white;
  12. public Color DisabledColor = Color.white;
  13. public Color HoverColor = Color.white;
  14. public Sprite Image;
  15. public Color NormalColor = Color.white;
  16. public Style Copy()
  17. {
  18. var s = new Style();
  19. s.CopyFrom(this);
  20. return s;
  21. }
  22. public void CopyFrom(Style style)
  23. {
  24. Image = style.Image;
  25. NormalColor = style.NormalColor;
  26. HoverColor = style.HoverColor;
  27. ActiveColor = style.ActiveColor;
  28. DisabledColor = style.DisabledColor;
  29. }
  30. }
  31. [Serializable]
  32. public class StyleSheet : ScriptableObject
  33. {
  34. [SerializeField] private List<string> _keys = new List<string>();
  35. [SerializeField] private List<Style> _styles = new List<Style>();
  36. [SerializeField] public StyleSheet Parent;
  37. public Style GetStyle(string key, bool searchParent = true)
  38. {
  39. var i = _keys.IndexOf(key);
  40. if (i < 0)
  41. {
  42. if (searchParent && Parent != null)
  43. {
  44. return Parent.GetStyle(key);
  45. }
  46. return null;
  47. }
  48. return _styles[i];
  49. }
  50. #if UNITY_EDITOR
  51. public int AddStyle(string key)
  52. {
  53. if (_keys.Contains(key))
  54. {
  55. throw new ArgumentException("key already exists");
  56. }
  57. _keys.Add(key);
  58. _styles.Add(new Style());
  59. return _keys.Count - 1;
  60. }
  61. public bool DeleteStyle(string key)
  62. {
  63. var i = _keys.IndexOf(key);
  64. if (i < 0)
  65. {
  66. return false;
  67. }
  68. _keys.RemoveAt(i);
  69. _styles.RemoveAt(i);
  70. return true;
  71. }
  72. public IEnumerable<string> GetStyleKeys(bool includeParent = true)
  73. {
  74. if (Parent != null && includeParent)
  75. {
  76. return _keys.Union(Parent.GetStyleKeys());
  77. }
  78. return _keys.ToList();
  79. }
  80. [UnityEditor.MenuItem("Assets/Create/SRF/Style Sheet")]
  81. public static void CreateStyleSheet()
  82. {
  83. var o = AssetUtil.CreateAsset<StyleSheet>();
  84. AssetUtil.SelectAssetInProjectView(o);
  85. }
  86. #endif
  87. }
  88. }