CopyPreferredSizes.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. namespace SRF.UI
  3. {
  4. using Internal;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Copies the preferred size of another layout element (useful for a parent object basing its sizing from a child
  9. /// element).
  10. /// This does have very quirky behaviour, though.
  11. /// </summary>
  12. [RequireComponent(typeof(RectTransform))]
  13. [ExecuteInEditMode]
  14. [AddComponentMenu(ComponentMenuPaths.CopyPreferredSizes)]
  15. public class CopyPreferredSizes : LayoutElement
  16. {
  17. public enum Operations
  18. {
  19. Max,
  20. Min
  21. }
  22. [Serializable]
  23. public class CopySource
  24. {
  25. public RectTransform Rect;
  26. public float PaddingHeight;
  27. public float PaddingWidth;
  28. }
  29. public CopySource[] CopySources;
  30. public Operations Operation;
  31. public override float preferredWidth
  32. {
  33. get
  34. {
  35. if (CopySources == null || CopySources.Length == 0 || !IsActive())
  36. {
  37. return -1f;
  38. }
  39. float current = Operation == Operations.Max ? float.MinValue : float.MaxValue;
  40. for (var i = 0; i < CopySources.Length; i++)
  41. {
  42. if (CopySources[i].Rect == null)
  43. continue;
  44. float width = LayoutUtility.GetPreferredWidth(CopySources[i].Rect) + CopySources[i].PaddingWidth;
  45. if (Operation == Operations.Max && width > current)
  46. current = width;
  47. else if (Operation == Operations.Min && width < current)
  48. current = width;
  49. }
  50. // ReSharper disable CompareOfFloatsByEqualityOperator
  51. if (Operation == Operations.Max && current == float.MinValue) return -1;
  52. if (Operation == Operations.Min && current == float.MaxValue) return -1;
  53. // ReSharper restore CompareOfFloatsByEqualityOperator
  54. return current;
  55. }
  56. }
  57. public override float preferredHeight
  58. {
  59. get
  60. {
  61. if (CopySources == null || CopySources.Length == 0 || !IsActive())
  62. {
  63. return -1f;
  64. }
  65. float current = Operation == Operations.Max ? float.MinValue : float.MaxValue;
  66. for (var i = 0; i < CopySources.Length; i++)
  67. {
  68. if (CopySources[i].Rect == null)
  69. continue;
  70. float height = LayoutUtility.GetPreferredHeight(CopySources[i].Rect) + CopySources[i].PaddingHeight;
  71. if (Operation == Operations.Max && height > current)
  72. current = height;
  73. else if (Operation == Operations.Min && height < current)
  74. current = height;
  75. }
  76. // ReSharper disable CompareOfFloatsByEqualityOperator
  77. if (Operation == Operations.Max && current == float.MinValue) return -1;
  78. if (Operation == Operations.Min && current == float.MaxValue) return -1;
  79. // ReSharper restore CompareOfFloatsByEqualityOperator
  80. return current;
  81. }
  82. }
  83. public override int layoutPriority
  84. {
  85. get { return 2; }
  86. }
  87. }
  88. }