StyleComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.UI;
  5. namespace SRF.UI
  6. {
  7. [AddComponentMenu("SRF/UI/Style Component")]
  8. [ExecuteInEditMode]
  9. public class StyleComponent : SRMonoBehaviour
  10. {
  11. public string StyleKey
  12. {
  13. get
  14. {
  15. return this._styleKey;
  16. }
  17. set
  18. {
  19. this._styleKey = value;
  20. this.Refresh(false);
  21. }
  22. }
  23. private void Start()
  24. {
  25. this.Refresh(true);
  26. this._hasStarted = true;
  27. }
  28. private void OnEnable()
  29. {
  30. if (this._hasStarted)
  31. {
  32. this.Refresh(false);
  33. }
  34. }
  35. public void Refresh(bool invalidateCache)
  36. {
  37. if (string.IsNullOrEmpty(this.StyleKey))
  38. {
  39. this._activeStyle = null;
  40. return;
  41. }
  42. if (this._cachedRoot == null || invalidateCache)
  43. {
  44. this._cachedRoot = this.GetStyleRoot();
  45. }
  46. if (this._cachedRoot == null)
  47. {
  48. UnityEngine.Debug.LogWarning("[StyleComponent] No active StyleRoot object found in parents.", this);
  49. this._activeStyle = null;
  50. return;
  51. }
  52. Style style = this._cachedRoot.GetStyle(this.StyleKey);
  53. if (style == null)
  54. {
  55. UnityEngine.Debug.LogWarning("[StyleComponent] Style not found ({0})".Fmt(new object[]
  56. {
  57. this.StyleKey
  58. }), this);
  59. this._activeStyle = null;
  60. return;
  61. }
  62. this._activeStyle = style;
  63. this.ApplyStyle();
  64. }
  65. private StyleRoot GetStyleRoot()
  66. {
  67. Transform transform = base.CachedTransform;
  68. int num = 0;
  69. StyleRoot componentInParent;
  70. for (;;)
  71. {
  72. componentInParent = transform.GetComponentInParent<StyleRoot>();
  73. if (componentInParent != null)
  74. {
  75. transform = componentInParent.transform.parent;
  76. }
  77. num++;
  78. if (num > 100)
  79. {
  80. break;
  81. }
  82. if (!(componentInParent != null) || componentInParent.enabled || !(transform != null))
  83. {
  84. return componentInParent;
  85. }
  86. }
  87. UnityEngine.Debug.LogWarning("Breaking Loop");
  88. return componentInParent;
  89. }
  90. private void ApplyStyle()
  91. {
  92. if (this._activeStyle == null)
  93. {
  94. return;
  95. }
  96. if (this._graphic == null)
  97. {
  98. this._graphic = base.GetComponent<Graphic>();
  99. }
  100. if (this._selectable == null)
  101. {
  102. this._selectable = base.GetComponent<Selectable>();
  103. }
  104. if (this._image == null)
  105. {
  106. this._image = base.GetComponent<Image>();
  107. }
  108. if (!this.IgnoreImage && this._image != null)
  109. {
  110. this._image.sprite = this._activeStyle.Image;
  111. }
  112. if (this._selectable != null)
  113. {
  114. ColorBlock colors = this._selectable.colors;
  115. colors.normalColor = this._activeStyle.NormalColor;
  116. colors.highlightedColor = this._activeStyle.HoverColor;
  117. colors.pressedColor = this._activeStyle.ActiveColor;
  118. colors.disabledColor = this._activeStyle.DisabledColor;
  119. colors.colorMultiplier = 1f;
  120. this._selectable.colors = colors;
  121. if (this._graphic != null)
  122. {
  123. this._graphic.color = Color.white;
  124. }
  125. }
  126. else if (this._graphic != null)
  127. {
  128. this._graphic.color = this._activeStyle.NormalColor;
  129. }
  130. }
  131. private void SRStyleDirty()
  132. {
  133. if (!base.CachedGameObject.activeInHierarchy)
  134. {
  135. this._cachedRoot = null;
  136. return;
  137. }
  138. this.Refresh(true);
  139. }
  140. private Style _activeStyle;
  141. private StyleRoot _cachedRoot;
  142. private Graphic _graphic;
  143. private bool _hasStarted;
  144. private Image _image;
  145. private Selectable _selectable;
  146. [HideInInspector]
  147. [FormerlySerializedAs("StyleKey")]
  148. [SerializeField]
  149. private string _styleKey;
  150. public bool IgnoreImage;
  151. }
  152. }