XGridLayout.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. /// <summary>
  6. /// Grid layout component. Used to manage the layout of multiple `GridCoord`, and the number of rows and columns of the grid can be controlled by `row` and `column`.
  7. /// ||网格布局组件。用于管理多个`GridCoord`的布局,可以通过`row`和`column`来控制网格的行列数。
  8. /// </summary>
  9. [Since("v3.8.0")]
  10. [Serializable]
  11. [ComponentHandler(typeof(GridLayoutHandler), true)]
  12. public class GridLayout : MainComponent, IUpdateRuntimeData
  13. {
  14. [SerializeField] private bool m_Show = true;
  15. [SerializeField] private float m_Left = 0.1f;
  16. [SerializeField] private float m_Right = 0.08f;
  17. [SerializeField] private float m_Top = 0.22f;
  18. [SerializeField] private float m_Bottom = 0.12f;
  19. [SerializeField] private int m_Row = 2;
  20. [SerializeField] private int m_Column = 2;
  21. [SerializeField] private Vector2 m_Spacing = Vector2.zero;
  22. [SerializeField] protected bool m_Inverse = false;
  23. public GridLayoutContext context = new GridLayoutContext();
  24. /// <summary>
  25. /// Whether to show the grid in rectangular coordinate.
  26. /// ||是否显示直角坐标系网格。
  27. /// </summary>
  28. public bool show
  29. {
  30. get { return m_Show; }
  31. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetVerticesDirty(); }
  32. }
  33. /// <summary>
  34. /// Distance between grid component and the left side of the container.
  35. /// ||grid 组件离容器左侧的距离。
  36. /// </summary>
  37. public float left
  38. {
  39. get { return m_Left; }
  40. set { if (PropertyUtil.SetStruct(ref m_Left, value)) SetAllDirty(); }
  41. }
  42. /// <summary>
  43. /// Distance between grid component and the right side of the container.
  44. /// ||grid 组件离容器右侧的距离。
  45. /// </summary>
  46. public float right
  47. {
  48. get { return m_Right; }
  49. set { if (PropertyUtil.SetStruct(ref m_Right, value)) SetAllDirty(); }
  50. }
  51. /// <summary>
  52. /// Distance between grid component and the top side of the container.
  53. /// ||grid 组件离容器上侧的距离。
  54. /// </summary>
  55. public float top
  56. {
  57. get { return m_Top; }
  58. set { if (PropertyUtil.SetStruct(ref m_Top, value)) SetAllDirty(); }
  59. }
  60. /// <summary>
  61. /// Distance between grid component and the bottom side of the container.
  62. /// ||grid 组件离容器下侧的距离。
  63. /// </summary>
  64. public float bottom
  65. {
  66. get { return m_Bottom; }
  67. set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) SetAllDirty(); }
  68. }
  69. /// <summary>
  70. /// the row count of grid layout.
  71. /// ||网格布局的行数。
  72. /// </summary>
  73. public int row
  74. {
  75. get { return m_Row; }
  76. set { if (PropertyUtil.SetStruct(ref m_Row, value)) SetAllDirty(); }
  77. }
  78. /// <summary>
  79. /// the column count of grid layout.
  80. /// ||网格布局的列数。
  81. /// </summary>
  82. public int column
  83. {
  84. get { return m_Column; }
  85. set { if (PropertyUtil.SetStruct(ref m_Column, value)) SetAllDirty(); }
  86. }
  87. /// <summary>
  88. /// the spacing of grid layout.
  89. /// ||网格布局的间距。
  90. /// </summary>
  91. public Vector2 spacing
  92. {
  93. get { return m_Spacing; }
  94. set { if (PropertyUtil.SetStruct(ref m_Spacing, value)) SetAllDirty(); }
  95. }
  96. /// <summary>
  97. /// Whether to inverse the grid layout.
  98. /// ||是否反转网格布局。
  99. /// </summary>
  100. public bool inverse
  101. {
  102. get { return m_Inverse; }
  103. set { if (PropertyUtil.SetStruct(ref m_Inverse, value)) SetAllDirty(); }
  104. }
  105. public void UpdateRuntimeData(BaseChart chart)
  106. {
  107. var chartX = chart.chartX;
  108. var chartY = chart.chartY;
  109. var chartWidth = chart.chartWidth;
  110. var chartHeight = chart.chartHeight;
  111. var actualLeft = left <= 1 ? left * chartWidth : left;
  112. var actualBottom = bottom <= 1 ? bottom * chartHeight : bottom;
  113. var actualTop = top <= 1 ? top * chartHeight : top;
  114. var actualRight = right <= 1 ? right * chartWidth : right;
  115. context.x = chartX + actualLeft;
  116. context.y = chartY + actualBottom;
  117. context.width = chartWidth - actualLeft - actualRight;
  118. context.height = chartHeight - actualTop - actualBottom;
  119. context.eachWidth = (context.width - spacing.x * (column - 1)) / column;
  120. context.eachHeight = (context.height - spacing.y * (row - 1)) / row;
  121. }
  122. internal void UpdateGridContext(int index, ref float x, ref float y, ref float width, ref float height)
  123. {
  124. var row = index / m_Column;
  125. var column = index % m_Column;
  126. x = context.x + column * (context.eachWidth + spacing.x);
  127. if(m_Inverse)
  128. y = context.y + row * (context.eachHeight + spacing.y);
  129. else
  130. y = context.y + context.height - (row + 1) * context.eachHeight - row * spacing.y;
  131. width = context.eachWidth;
  132. height = context.eachHeight;
  133. }
  134. internal void UpdateGridContext(int index, ref Vector3 position, ref float width, ref float height)
  135. {
  136. float x = 0, y = 0;
  137. UpdateGridContext(index, ref x, ref y, ref width, ref height);
  138. position = new Vector3(x, y);
  139. }
  140. }
  141. }