Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GridCoord.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using UnityEngine;
  3. using XUGL;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// Grid component.
  8. /// |Drawing grid in rectangular coordinate. Line chart, bar chart, and scatter chart can be drawn in grid.
  9. /// |网格组件。
  10. /// 直角坐标系内绘图网格。可以在网格上绘制折线图,柱状图,散点图。
  11. /// </summary>
  12. [Serializable]
  13. [ComponentHandler(typeof(GridCoordHandler), true)]
  14. public class GridCoord : CoordSystem, IUpdateRuntimeData, ISerieContainer
  15. {
  16. [SerializeField] private bool m_Show = true;
  17. [SerializeField] private float m_Left = 0.1f;
  18. [SerializeField] private float m_Right = 0.08f;
  19. [SerializeField] private float m_Top = 0.22f;
  20. [SerializeField] private float m_Bottom = 0.12f;
  21. [SerializeField] private Color32 m_BackgroundColor;
  22. [SerializeField] private bool m_ShowBorder = false;
  23. [SerializeField] private float m_BorderWidth = 0f;
  24. [SerializeField] private Color32 m_BorderColor;
  25. public GridCoordContext context = new GridCoordContext();
  26. /// <summary>
  27. /// Whether to show the grid in rectangular coordinate.
  28. /// |是否显示直角坐标系网格。
  29. /// </summary>
  30. public bool show
  31. {
  32. get { return m_Show; }
  33. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetVerticesDirty(); }
  34. }
  35. /// <summary>
  36. /// Distance between grid component and the left side of the container.
  37. /// |grid 组件离容器左侧的距离。
  38. /// </summary>
  39. public float left
  40. {
  41. get { return m_Left; }
  42. set { if (PropertyUtil.SetStruct(ref m_Left, value)) SetAllDirty(); }
  43. }
  44. /// <summary>
  45. /// Distance between grid component and the right side of the container.
  46. /// |grid 组件离容器右侧的距离。
  47. /// </summary>
  48. public float right
  49. {
  50. get { return m_Right; }
  51. set { if (PropertyUtil.SetStruct(ref m_Right, value)) SetAllDirty(); }
  52. }
  53. /// <summary>
  54. /// Distance between grid component and the top side of the container.
  55. /// |grid 组件离容器上侧的距离。
  56. /// </summary>
  57. public float top
  58. {
  59. get { return m_Top; }
  60. set { if (PropertyUtil.SetStruct(ref m_Top, value)) SetAllDirty(); }
  61. }
  62. /// <summary>
  63. /// Distance between grid component and the bottom side of the container.
  64. /// |grid 组件离容器下侧的距离。
  65. /// </summary>
  66. public float bottom
  67. {
  68. get { return m_Bottom; }
  69. set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) SetAllDirty(); }
  70. }
  71. /// <summary>
  72. /// Background color of grid, which is transparent by default.
  73. /// |网格背景色,默认透明。
  74. /// </summary>
  75. public Color32 backgroundColor
  76. {
  77. get { return m_BackgroundColor; }
  78. set { if (PropertyUtil.SetColor(ref m_BackgroundColor, value)) SetVerticesDirty(); }
  79. }
  80. /// <summary>
  81. /// Whether to show the grid border.
  82. /// |是否显示网格边框。
  83. /// </summary>
  84. public bool showBorder
  85. {
  86. get { return m_ShowBorder; }
  87. set { if (PropertyUtil.SetStruct(ref m_ShowBorder, value)) SetVerticesDirty(); }
  88. }
  89. /// <summary>
  90. /// Border width of grid.
  91. /// |网格边框宽。
  92. /// </summary>
  93. public float borderWidth
  94. {
  95. get { return m_BorderWidth; }
  96. set { if (PropertyUtil.SetStruct(ref m_BorderWidth, value)) SetVerticesDirty(); }
  97. }
  98. /// <summary>
  99. /// The color of grid border.
  100. /// |网格边框颜色。
  101. /// </summary>
  102. public Color32 borderColor
  103. {
  104. get { return m_BorderColor; }
  105. set { if (PropertyUtil.SetColor(ref m_BorderColor, value)) SetVerticesDirty(); }
  106. }
  107. public bool IsPointerEnter()
  108. {
  109. return context.isPointerEnter;
  110. }
  111. public void UpdateRuntimeData(float chartX, float chartY, float chartWidth, float chartHeight)
  112. {
  113. context.left = left <= 1 ? left * chartWidth : left;
  114. context.bottom = bottom <= 1 ? bottom * chartHeight : bottom;
  115. context.top = top <= 1 ? top * chartHeight : top;
  116. context.right = right <= 1 ? right * chartWidth : right;
  117. context.x = chartX + context.left;
  118. context.y = chartY + context.bottom;
  119. context.width = chartWidth - context.left - context.right;
  120. context.height = chartHeight - context.top - context.bottom;
  121. context.position = new Vector3(context.x, context.y);
  122. context.center = new Vector3(context.x + context.width / 2, context.y + context.height / 2);
  123. }
  124. public bool Contains(Vector3 pos)
  125. {
  126. return Contains(pos.x, pos.y);
  127. }
  128. public bool Contains(float x, float y)
  129. {
  130. if (x < context.x - 1 || x > context.x + context.width + 1 ||
  131. y < context.y - 1 || y > context.y + context.height + 1)
  132. {
  133. return false;
  134. }
  135. return true;
  136. }
  137. /// <summary>
  138. /// 给定的线段和Grid边界的交点
  139. /// </summary>
  140. /// <param name="sp"></param>
  141. /// <param name="ep"></param>
  142. /// <returns></returns>
  143. public bool BoundaryPoint(Vector3 sp, Vector3 ep, ref Vector3 point)
  144. {
  145. if (Contains(sp) && Contains(ep))
  146. {
  147. point = ep;
  148. return false;
  149. }
  150. var lb = new Vector3(context.x, context.y);
  151. var lt = new Vector3(context.x, context.y + context.height);
  152. var rt = new Vector3(context.x + context.width, context.y + context.height);
  153. var rb = new Vector3(context.x + context.width, context.y);
  154. if (UGLHelper.GetIntersection(sp, ep, rb, rt, ref point))
  155. return true;
  156. if (UGLHelper.GetIntersection(sp, ep, lt, rt, ref point))
  157. return true;
  158. if (UGLHelper.GetIntersection(sp, ep, lb, rb, ref point))
  159. return true;
  160. if (UGLHelper.GetIntersection(sp, ep, lb, lt, ref point))
  161. return true;
  162. return false;
  163. }
  164. }
  165. }