BetterAxisAlignedLayoutGroup.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Serialization;
  8. using UnityEngine.UI;
  9. namespace TheraBytes.BetterUi
  10. {
  11. #if UNITY_2018_3_OR_NEWER
  12. [ExecuteAlways]
  13. #else
  14. [ExecuteInEditMode]
  15. #endif
  16. [HelpURL("https://documentation.therabytes.de/better-ui/BetterAxisAlignedLayoutGroup.html")]
  17. [AddComponentMenu("Better UI/Layout/Better Axis Aligned Layout Group", 30)]
  18. public class BetterAxisAlignedLayoutGroup
  19. : HorizontalOrVerticalLayoutGroup, IBetterHorizontalOrVerticalLayoutGroup, IResolutionDependency
  20. {
  21. [Serializable]
  22. public class Settings : IScreenConfigConnection
  23. {
  24. public TextAnchor ChildAlignment;
  25. public bool ReverseArrangement = false;
  26. public bool ChildForceExpandHeight = false;
  27. public bool ChildForceExpandWidth = false;
  28. public bool ChildScaleWidth = false;
  29. public bool ChildScaleHeight = false;
  30. public bool ChildControlWidth = true;
  31. public bool ChildControlHeight = true;
  32. public Axis Orientation;
  33. [SerializeField]
  34. string screenConfigName;
  35. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  36. public Settings(TextAnchor childAlignment, bool expandWidth, bool expandHeight, Axis orientation)
  37. {
  38. this.ChildAlignment = childAlignment;
  39. this.ChildForceExpandWidth = expandWidth;
  40. this.ChildForceExpandHeight = expandHeight;
  41. this.Orientation = orientation;
  42. }
  43. }
  44. [Serializable]
  45. public class SettingsConfigCollection : SizeConfigCollection<Settings> { }
  46. public enum Axis
  47. {
  48. Horizontal,
  49. Vertical,
  50. }
  51. public MarginSizeModifier PaddingSizer { get { return customPaddingSizers.GetCurrentItem(paddingSizerFallback); } }
  52. public FloatSizeModifier SpacingSizer { get { return customSpacingSizers.GetCurrentItem(spacingSizerFallback); } }
  53. public Settings CurrentSettings { get { return customSettings.GetCurrentItem(settingsFallback); } }
  54. public Axis Orientation { get { return orientation; } set { orientation = value; } }
  55. bool isVertical { get { return orientation == Axis.Vertical; } }
  56. [SerializeField]
  57. MarginSizeModifier paddingSizerFallback =
  58. new MarginSizeModifier(new Margin(), new Margin(), new Margin(1000, 1000, 1000, 1000));
  59. [SerializeField]
  60. MarginSizeConfigCollection customPaddingSizers = new MarginSizeConfigCollection();
  61. [SerializeField]
  62. FloatSizeModifier spacingSizerFallback =
  63. new FloatSizeModifier(0, 0, 300);
  64. [SerializeField]
  65. FloatSizeConfigCollection customSpacingSizers = new FloatSizeConfigCollection();
  66. [SerializeField]
  67. Settings settingsFallback;
  68. [SerializeField]
  69. SettingsConfigCollection customSettings = new SettingsConfigCollection();
  70. [SerializeField]
  71. Axis orientation;
  72. #region new base setters
  73. public new RectOffset padding
  74. {
  75. get { return base.padding; }
  76. set { Config.Set(value, (o) => base.padding = value, (o) => PaddingSizer.SetSize(this, new Margin(o))); }
  77. }
  78. public new float spacing
  79. {
  80. get { return base.spacing; }
  81. set { Config.Set(value, (o) => base.spacing = value, (o) => SpacingSizer.SetSize(this, o)); }
  82. }
  83. public new TextAnchor childAlignment
  84. {
  85. get { return base.childAlignment; }
  86. set { Config.Set(value, (o) => base.childAlignment = o, (o) => CurrentSettings.ChildAlignment = o); }
  87. }
  88. public new bool childForceExpandHeight
  89. {
  90. get { return base.childForceExpandHeight; }
  91. set { Config.Set(value, (o) => base.childForceExpandHeight = o, (o) => CurrentSettings.ChildForceExpandHeight = o); }
  92. }
  93. public new bool childForceExpandWidth
  94. {
  95. get { return base.childForceExpandWidth; }
  96. set { Config.Set(value, (o) => base.childForceExpandWidth = o, (o) => CurrentSettings.ChildForceExpandWidth = o); }
  97. }
  98. #if UNITY_2020_1_OR_NEWER
  99. public new bool reverseArrangement
  100. {
  101. get { return base.reverseArrangement; }
  102. set { Config.Set(value, (o) => base.reverseArrangement = o, (o) => CurrentSettings.ReverseArrangement = o); }
  103. }
  104. #endif
  105. #if UNITY_2019_1_OR_NEWER
  106. public new bool childScaleWidth
  107. {
  108. get { return base.childScaleWidth; }
  109. set { Config.Set(value, (o) => base.childScaleWidth = o, (o) => CurrentSettings.ChildScaleWidth = o); }
  110. }
  111. public new bool childScaleHeight
  112. {
  113. get { return base.childScaleHeight; }
  114. set { Config.Set(value, (o) => base.childScaleHeight = o, (o) => CurrentSettings.ChildScaleHeight = o); }
  115. }
  116. #endif
  117. #if !(UNITY_5_4) && !(UNITY_5_3)
  118. public new bool childControlWidth
  119. {
  120. get { return base.childControlWidth; }
  121. set { Config.Set(value, (o) => base.childControlWidth = o, (o) => CurrentSettings.ChildControlWidth = o); }
  122. }
  123. public new bool childControlHeight
  124. {
  125. get { return base.childControlHeight; }
  126. set { Config.Set(value, (o) => base.childControlHeight = o, (o) => CurrentSettings.ChildControlHeight = o); }
  127. }
  128. #endif
  129. #endregion
  130. protected override void OnEnable()
  131. {
  132. base.OnEnable();
  133. if (settingsFallback == null || string.IsNullOrEmpty(settingsFallback.ScreenConfigName))
  134. {
  135. StartCoroutine(InitDelayed());
  136. }
  137. else
  138. {
  139. CalculateCellSize();
  140. }
  141. }
  142. protected override void OnTransformChildrenChanged()
  143. {
  144. base.OnTransformChildrenChanged();
  145. if(isActiveAndEnabled)
  146. {
  147. StartCoroutine(SetDirtyDelayed());
  148. }
  149. }
  150. private IEnumerator SetDirtyDelayed()
  151. {
  152. yield return null;
  153. base.SetDirty();
  154. }
  155. protected override void OnRectTransformDimensionsChange()
  156. {
  157. base.OnRectTransformDimensionsChange();
  158. base.SetDirty();
  159. }
  160. IEnumerator InitDelayed()
  161. {
  162. yield return null;
  163. settingsFallback = new Settings(this.childAlignment, this.childForceExpandWidth, this.childForceExpandHeight, this.orientation)
  164. {
  165. #if !(UNITY_5_4) && !(UNITY_5_3)
  166. ChildControlWidth = this.childControlWidth,
  167. ChildControlHeight = this.childControlHeight,
  168. #endif
  169. #if UNITY_2019_1_OR_NEWER
  170. ChildScaleWidth = this.childScaleWidth,
  171. ChildScaleHeight = this.childScaleHeight,
  172. #endif
  173. #if UNITY_2020_1_OR_NEWER
  174. ReverseArrangement = this.reverseArrangement,
  175. #endif
  176. ScreenConfigName = "Fallback",
  177. };
  178. CalculateCellSize();
  179. }
  180. public override void CalculateLayoutInputHorizontal()
  181. {
  182. base.CalculateLayoutInputHorizontal();
  183. base.CalcAlongAxis(0, isVertical);
  184. }
  185. public override void CalculateLayoutInputVertical()
  186. {
  187. base.CalcAlongAxis(1, isVertical);
  188. }
  189. public override void SetLayoutHorizontal()
  190. {
  191. base.SetChildrenAlongAxis(0, isVertical);
  192. }
  193. public override void SetLayoutVertical()
  194. {
  195. base.SetChildrenAlongAxis(1, isVertical);
  196. }
  197. public void OnResolutionChanged()
  198. {
  199. CalculateCellSize();
  200. }
  201. public void CalculateCellSize()
  202. {
  203. Rect r = this.rectTransform.rect;
  204. if (r.width == float.NaN || r.height == float.NaN)
  205. return;
  206. ApplySettings(CurrentSettings);
  207. base.m_Spacing = SpacingSizer.CalculateSize(this);
  208. Margin pad = PaddingSizer.CalculateSize(this);
  209. pad.CopyValuesTo(base.m_Padding);
  210. }
  211. void ApplySettings(Settings settings)
  212. {
  213. if (settingsFallback == null)
  214. return;
  215. this.m_ChildAlignment = settings.ChildAlignment;
  216. this.orientation = settings.Orientation;
  217. this.m_ChildForceExpandWidth = settings.ChildForceExpandWidth;
  218. this.m_ChildForceExpandHeight = settings.ChildForceExpandHeight;
  219. #if !(UNITY_5_4) && !(UNITY_5_3)
  220. this.m_ChildControlWidth = settings.ChildControlWidth;
  221. this.m_ChildControlHeight = settings.ChildControlHeight;
  222. #endif
  223. #if UNITY_2019_1_OR_NEWER
  224. this.childScaleWidth = settings.ChildScaleWidth;
  225. this.childScaleHeight = settings.ChildScaleHeight;
  226. #endif
  227. #if UNITY_2020_1_OR_NEWER
  228. this.reverseArrangement = settings.ReverseArrangement;
  229. #endif
  230. }
  231. #if UNITY_EDITOR
  232. protected override void OnValidate()
  233. {
  234. CalculateCellSize();
  235. base.OnValidate();
  236. }
  237. #endif
  238. }
  239. }