暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Location.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using UnityEngine;
  3. #if dUI_TextMeshPro
  4. using TMPro;
  5. #endif
  6. namespace XCharts.Runtime
  7. {
  8. /// <summary>
  9. /// Location type. Quick to set the general location.
  10. /// |位置类型。通过Align快速设置大体位置,再通过left,right,top,bottom微调具体位置。
  11. /// </summary>
  12. [Serializable]
  13. public class Location : ChildComponent, IPropertyChanged
  14. {
  15. /// <summary>
  16. /// 对齐方式
  17. /// </summary>
  18. public enum Align
  19. {
  20. TopLeft,
  21. TopRight,
  22. TopCenter,
  23. BottomLeft,
  24. BottomRight,
  25. BottomCenter,
  26. Center,
  27. CenterLeft,
  28. CenterRight
  29. }
  30. [SerializeField] private Align m_Align = Align.TopCenter;
  31. [SerializeField] private float m_Left;
  32. [SerializeField] private float m_Right;
  33. [SerializeField] private float m_Top;
  34. [SerializeField] private float m_Bottom;
  35. private TextAnchor m_TextAlignment;
  36. #if dUI_TextMeshPro
  37. private TextAlignmentOptions m_TMPTextAlignment;
  38. #endif
  39. private Vector2 m_AnchorMin;
  40. private Vector2 m_AnchorMax;
  41. private Vector2 m_Pivot;
  42. /// <summary>
  43. /// 对齐方式。
  44. /// </summary>
  45. public Align align
  46. {
  47. get { return m_Align; }
  48. set { if (PropertyUtil.SetStruct(ref m_Align, value)) { SetComponentDirty(); UpdateAlign(); } }
  49. }
  50. /// <summary>
  51. /// Distance between component and the left side of the container.
  52. /// |离容器左侧的距离。
  53. /// </summary>
  54. public float left
  55. {
  56. get { return m_Left; }
  57. set { if (PropertyUtil.SetStruct(ref m_Left, value)) { SetComponentDirty(); UpdateAlign(); } }
  58. }
  59. /// <summary>
  60. /// Distance between component and the left side of the container.
  61. /// |离容器右侧的距离。
  62. /// </summary>
  63. public float right
  64. {
  65. get { return m_Right; }
  66. set { if (PropertyUtil.SetStruct(ref m_Right, value)) { SetComponentDirty(); UpdateAlign(); } }
  67. }
  68. /// <summary>
  69. /// Distance between component and the left side of the container.
  70. /// |离容器上侧的距离。
  71. /// </summary>
  72. public float top
  73. {
  74. get { return m_Top; }
  75. set { if (PropertyUtil.SetStruct(ref m_Top, value)) { SetComponentDirty(); UpdateAlign(); } }
  76. }
  77. /// <summary>
  78. /// Distance between component and the left side of the container.
  79. /// |离容器下侧的距离。
  80. /// </summary>
  81. public float bottom
  82. {
  83. get { return m_Bottom; }
  84. set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) { SetComponentDirty(); UpdateAlign(); } }
  85. }
  86. /// <summary>
  87. /// the anchor of text.
  88. /// |Location对应的Anchor锚点
  89. /// </summary>
  90. /// <value></value>
  91. public TextAnchor runtimeTextAlignment { get { return m_TextAlignment; } }
  92. #if dUI_TextMeshPro
  93. public TextAlignmentOptions runtimeTMPTextAlignment { get { return m_TMPTextAlignment; } }
  94. #endif
  95. /// <summary>
  96. /// the minimum achor.
  97. /// |Location对应的anchorMin。
  98. /// </summary>
  99. public Vector2 runtimeAnchorMin { get { return m_AnchorMin; } }
  100. /// <summary>
  101. /// the maximun achor.
  102. /// |Location对应的anchorMax.
  103. /// |</summary>
  104. public Vector2 runtimeAnchorMax { get { return m_AnchorMax; } }
  105. /// <summary>
  106. /// the povot.
  107. /// |Loation对应的中心点。
  108. /// </summary>
  109. public Vector2 runtimePivot { get { return m_Pivot; } }
  110. public float runtimeLeft { get; private set; }
  111. public float runtimeRight { get; private set; }
  112. public float runtimeBottom { get; private set; }
  113. public float runtimeTop { get; private set; }
  114. public static Location defaultLeft
  115. {
  116. get
  117. {
  118. return new Location()
  119. {
  120. align = Align.CenterLeft,
  121. left = 0.03f,
  122. right = 0,
  123. top = 0,
  124. bottom = 0
  125. };
  126. }
  127. }
  128. public static Location defaultRight
  129. {
  130. get
  131. {
  132. return new Location()
  133. {
  134. align = Align.CenterRight,
  135. left = 0,
  136. right = 0.03f,
  137. top = 0,
  138. bottom = 0
  139. };
  140. }
  141. }
  142. public static Location defaultTop
  143. {
  144. get
  145. {
  146. return new Location()
  147. {
  148. align = Align.TopCenter,
  149. left = 0,
  150. right = 0,
  151. top = 0.03f,
  152. bottom = 0
  153. };
  154. }
  155. }
  156. public static Location defaultBottom
  157. {
  158. get
  159. {
  160. return new Location()
  161. {
  162. align = Align.BottomCenter,
  163. left = 0,
  164. right = 0,
  165. top = 0,
  166. bottom = 0.03f
  167. };
  168. }
  169. }
  170. private void UpdateAlign()
  171. {
  172. switch (m_Align)
  173. {
  174. case Align.BottomCenter:
  175. m_TextAlignment = TextAnchor.LowerCenter;
  176. #if dUI_TextMeshPro
  177. m_TMPTextAlignment = TextAlignmentOptions.Bottom;
  178. #endif
  179. m_AnchorMin = new Vector2(0.5f, 0);
  180. m_AnchorMax = new Vector2(0.5f, 0);
  181. m_Pivot = new Vector2(0.5f, 0);
  182. break;
  183. case Align.BottomLeft:
  184. m_TextAlignment = TextAnchor.LowerLeft;
  185. #if dUI_TextMeshPro
  186. m_TMPTextAlignment = TextAlignmentOptions.BottomLeft;
  187. #endif
  188. m_AnchorMin = new Vector2(0, 0);
  189. m_AnchorMax = new Vector2(0, 0);
  190. m_Pivot = new Vector2(0, 0);
  191. break;
  192. case Align.BottomRight:
  193. m_TextAlignment = TextAnchor.LowerRight;
  194. #if dUI_TextMeshPro
  195. m_TMPTextAlignment = TextAlignmentOptions.BottomRight;
  196. #endif
  197. m_AnchorMin = new Vector2(1, 0);
  198. m_AnchorMax = new Vector2(1, 0);
  199. m_Pivot = new Vector2(1, 0);
  200. break;
  201. case Align.Center:
  202. m_TextAlignment = TextAnchor.MiddleCenter;
  203. #if dUI_TextMeshPro
  204. m_TMPTextAlignment = TextAlignmentOptions.Center;
  205. #endif
  206. m_AnchorMin = new Vector2(0.5f, 0.5f);
  207. m_AnchorMax = new Vector2(0.5f, 0.5f);
  208. m_Pivot = new Vector2(0.5f, 0.5f);
  209. break;
  210. case Align.CenterLeft:
  211. m_TextAlignment = TextAnchor.MiddleLeft;
  212. #if dUI_TextMeshPro
  213. m_TMPTextAlignment = TextAlignmentOptions.Left;
  214. #endif
  215. m_AnchorMin = new Vector2(0, 0.5f);
  216. m_AnchorMax = new Vector2(0, 0.5f);
  217. m_Pivot = new Vector2(0, 0.5f);
  218. break;
  219. case Align.CenterRight:
  220. m_TextAlignment = TextAnchor.MiddleRight;
  221. #if dUI_TextMeshPro
  222. m_TMPTextAlignment = TextAlignmentOptions.Right;
  223. #endif
  224. m_AnchorMin = new Vector2(1, 0.5f);
  225. m_AnchorMax = new Vector2(1, 0.5f);
  226. m_Pivot = new Vector2(1, 0.5f);
  227. break;
  228. case Align.TopCenter:
  229. m_TextAlignment = TextAnchor.UpperCenter;
  230. #if dUI_TextMeshPro
  231. m_TMPTextAlignment = TextAlignmentOptions.Top;
  232. #endif
  233. m_AnchorMin = new Vector2(0.5f, 1);
  234. m_AnchorMax = new Vector2(0.5f, 1);
  235. m_Pivot = new Vector2(0.5f, 1);
  236. break;
  237. case Align.TopLeft:
  238. m_TextAlignment = TextAnchor.UpperLeft;
  239. #if dUI_TextMeshPro
  240. m_TMPTextAlignment = TextAlignmentOptions.TopLeft;
  241. #endif
  242. m_AnchorMin = new Vector2(0, 1);
  243. m_AnchorMax = new Vector2(0, 1);
  244. m_Pivot = new Vector2(0, 1);
  245. break;
  246. case Align.TopRight:
  247. m_TextAlignment = TextAnchor.UpperRight;
  248. #if dUI_TextMeshPro
  249. m_TMPTextAlignment = TextAlignmentOptions.TopRight;
  250. #endif
  251. m_AnchorMin = new Vector2(1, 1);
  252. m_AnchorMax = new Vector2(1, 1);
  253. m_Pivot = new Vector2(1, 1);
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. public void UpdateRuntimeData(float chartWidth, float chartHeight)
  260. {
  261. runtimeLeft = left <= 1 ? left * chartWidth : left;
  262. runtimeRight = right <= 1 ? right * chartWidth : right;
  263. runtimeTop = top <= 1 ? top * chartHeight : top;
  264. runtimeBottom = bottom <= 1 ? bottom * chartHeight : bottom;
  265. }
  266. /// <summary>
  267. /// 返回在坐标系中的具体位置
  268. /// </summary>
  269. /// <param name="chartWidth"></param>
  270. /// <param name="chartHeight"></param>
  271. /// <returns></returns>
  272. public Vector3 GetPosition(float chartWidth, float chartHeight)
  273. {
  274. UpdateRuntimeData(chartWidth, chartHeight);
  275. switch (align)
  276. {
  277. case Align.BottomCenter:
  278. return new Vector3(chartWidth / 2, runtimeBottom);
  279. case Align.BottomLeft:
  280. return new Vector3(runtimeLeft, runtimeBottom);
  281. case Align.BottomRight:
  282. return new Vector3(chartWidth - runtimeRight, runtimeBottom);
  283. case Align.Center:
  284. return new Vector3(chartWidth / 2, chartHeight / 2);
  285. case Align.CenterLeft:
  286. return new Vector3(runtimeLeft, chartHeight / 2);
  287. case Align.CenterRight:
  288. return new Vector3(chartWidth - runtimeRight, chartHeight / 2);
  289. case Align.TopCenter:
  290. return new Vector3(chartWidth / 2, chartHeight - runtimeTop);
  291. case Align.TopLeft:
  292. return new Vector3(runtimeLeft, chartHeight - runtimeTop);
  293. case Align.TopRight:
  294. return new Vector3(chartWidth - runtimeRight, chartHeight - runtimeTop);
  295. default:
  296. return Vector2.zero;
  297. }
  298. }
  299. /// <summary>
  300. /// 属性变更时更新textAnchor,minAnchor,maxAnchor,pivot
  301. /// </summary>
  302. public void OnChanged()
  303. {
  304. UpdateAlign();
  305. }
  306. }
  307. }