Location.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. public TextAnchor runtimeTextAlignment { get { return m_TextAlignment; } }
  91. #if dUI_TextMeshPro
  92. public TextAlignmentOptions runtimeTMPTextAlignment { get { return m_TMPTextAlignment; } }
  93. #endif
  94. /// <summary>
  95. /// the minimum achor.
  96. /// ||Location对应的anchorMin。
  97. /// </summary>
  98. public Vector2 runtimeAnchorMin { get { return m_AnchorMin; } }
  99. /// <summary>
  100. /// the maximun achor.
  101. /// ||Location对应的anchorMax.
  102. /// ||</summary>
  103. public Vector2 runtimeAnchorMax { get { return m_AnchorMax; } }
  104. /// <summary>
  105. /// the povot.
  106. /// ||Loation对应的中心点。
  107. /// </summary>
  108. public Vector2 runtimePivot { get { return m_Pivot; } }
  109. public float runtimeLeft { get; private set; }
  110. public float runtimeRight { get; private set; }
  111. public float runtimeBottom { get; private set; }
  112. public float runtimeTop { get; private set; }
  113. public static Location defaultLeft
  114. {
  115. get
  116. {
  117. return new Location()
  118. {
  119. align = Align.CenterLeft,
  120. left = 0.03f,
  121. right = 0,
  122. top = 0,
  123. bottom = 0
  124. };
  125. }
  126. }
  127. public static Location defaultRight
  128. {
  129. get
  130. {
  131. return new Location()
  132. {
  133. align = Align.CenterRight,
  134. left = 0,
  135. right = 0.03f,
  136. top = 0,
  137. bottom = 0
  138. };
  139. }
  140. }
  141. public static Location defaultTop
  142. {
  143. get
  144. {
  145. return new Location()
  146. {
  147. align = Align.TopCenter,
  148. left = 0,
  149. right = 0,
  150. top = 0.03f,
  151. bottom = 0
  152. };
  153. }
  154. }
  155. public static Location defaultBottom
  156. {
  157. get
  158. {
  159. return new Location()
  160. {
  161. align = Align.BottomCenter,
  162. left = 0,
  163. right = 0,
  164. top = 0,
  165. bottom = 0.03f
  166. };
  167. }
  168. }
  169. private void UpdateAlign()
  170. {
  171. switch (m_Align)
  172. {
  173. case Align.BottomCenter:
  174. m_TextAlignment = TextAnchor.LowerCenter;
  175. #if dUI_TextMeshPro
  176. m_TMPTextAlignment = TextAlignmentOptions.Bottom;
  177. #endif
  178. m_AnchorMin = new Vector2(0.5f, 0);
  179. m_AnchorMax = new Vector2(0.5f, 0);
  180. m_Pivot = new Vector2(0.5f, 0);
  181. break;
  182. case Align.BottomLeft:
  183. m_TextAlignment = TextAnchor.LowerLeft;
  184. #if dUI_TextMeshPro
  185. m_TMPTextAlignment = TextAlignmentOptions.BottomLeft;
  186. #endif
  187. m_AnchorMin = new Vector2(0, 0);
  188. m_AnchorMax = new Vector2(0, 0);
  189. m_Pivot = new Vector2(0, 0);
  190. break;
  191. case Align.BottomRight:
  192. m_TextAlignment = TextAnchor.LowerRight;
  193. #if dUI_TextMeshPro
  194. m_TMPTextAlignment = TextAlignmentOptions.BottomRight;
  195. #endif
  196. m_AnchorMin = new Vector2(1, 0);
  197. m_AnchorMax = new Vector2(1, 0);
  198. m_Pivot = new Vector2(1, 0);
  199. break;
  200. case Align.Center:
  201. m_TextAlignment = TextAnchor.MiddleCenter;
  202. #if dUI_TextMeshPro
  203. m_TMPTextAlignment = TextAlignmentOptions.Center;
  204. #endif
  205. m_AnchorMin = new Vector2(0.5f, 0.5f);
  206. m_AnchorMax = new Vector2(0.5f, 0.5f);
  207. m_Pivot = new Vector2(0.5f, 0.5f);
  208. break;
  209. case Align.CenterLeft:
  210. m_TextAlignment = TextAnchor.MiddleLeft;
  211. #if dUI_TextMeshPro
  212. m_TMPTextAlignment = TextAlignmentOptions.Left;
  213. #endif
  214. m_AnchorMin = new Vector2(0, 0.5f);
  215. m_AnchorMax = new Vector2(0, 0.5f);
  216. m_Pivot = new Vector2(0, 0.5f);
  217. break;
  218. case Align.CenterRight:
  219. m_TextAlignment = TextAnchor.MiddleRight;
  220. #if dUI_TextMeshPro
  221. m_TMPTextAlignment = TextAlignmentOptions.Right;
  222. #endif
  223. m_AnchorMin = new Vector2(1, 0.5f);
  224. m_AnchorMax = new Vector2(1, 0.5f);
  225. m_Pivot = new Vector2(1, 0.5f);
  226. break;
  227. case Align.TopCenter:
  228. m_TextAlignment = TextAnchor.UpperCenter;
  229. #if dUI_TextMeshPro
  230. m_TMPTextAlignment = TextAlignmentOptions.Top;
  231. #endif
  232. m_AnchorMin = new Vector2(0.5f, 1);
  233. m_AnchorMax = new Vector2(0.5f, 1);
  234. m_Pivot = new Vector2(0.5f, 1);
  235. break;
  236. case Align.TopLeft:
  237. m_TextAlignment = TextAnchor.UpperLeft;
  238. #if dUI_TextMeshPro
  239. m_TMPTextAlignment = TextAlignmentOptions.TopLeft;
  240. #endif
  241. m_AnchorMin = new Vector2(0, 1);
  242. m_AnchorMax = new Vector2(0, 1);
  243. m_Pivot = new Vector2(0, 1);
  244. break;
  245. case Align.TopRight:
  246. m_TextAlignment = TextAnchor.UpperRight;
  247. #if dUI_TextMeshPro
  248. m_TMPTextAlignment = TextAlignmentOptions.TopRight;
  249. #endif
  250. m_AnchorMin = new Vector2(1, 1);
  251. m_AnchorMax = new Vector2(1, 1);
  252. m_Pivot = new Vector2(1, 1);
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. public bool IsBottom()
  259. {
  260. switch (m_Align)
  261. {
  262. case Align.BottomCenter:
  263. case Align.BottomLeft:
  264. case Align.BottomRight:
  265. return true;
  266. default:
  267. return false;
  268. }
  269. }
  270. public bool IsTop()
  271. {
  272. switch (m_Align)
  273. {
  274. case Align.TopCenter:
  275. case Align.TopLeft:
  276. case Align.TopRight:
  277. return true;
  278. default:
  279. return false;
  280. }
  281. }
  282. public bool IsCenter()
  283. {
  284. switch (m_Align)
  285. {
  286. case Align.Center:
  287. case Align.CenterLeft:
  288. case Align.CenterRight:
  289. return true;
  290. default:
  291. return false;
  292. }
  293. }
  294. public void UpdateRuntimeData(float chartWidth, float chartHeight)
  295. {
  296. runtimeLeft = left <= 1 ? left * chartWidth : left;
  297. runtimeRight = right <= 1 ? right * chartWidth : right;
  298. runtimeTop = top <= 1 ? top * chartHeight : top;
  299. runtimeBottom = bottom <= 1 ? bottom * chartHeight : bottom;
  300. }
  301. /// <summary>
  302. /// 返回在坐标系中的具体位置
  303. /// </summary>
  304. /// <param name="chartWidth"></param>
  305. /// <param name="chartHeight"></param>
  306. /// <returns></returns>
  307. public Vector3 GetPosition(float chartWidth, float chartHeight)
  308. {
  309. UpdateRuntimeData(chartWidth, chartHeight);
  310. switch (align)
  311. {
  312. case Align.BottomCenter:
  313. return new Vector3(chartWidth / 2, runtimeBottom);
  314. case Align.BottomLeft:
  315. return new Vector3(runtimeLeft, runtimeBottom);
  316. case Align.BottomRight:
  317. return new Vector3(chartWidth - runtimeRight, runtimeBottom);
  318. case Align.Center:
  319. return new Vector3(chartWidth / 2, chartHeight / 2);
  320. case Align.CenterLeft:
  321. return new Vector3(runtimeLeft, chartHeight / 2);
  322. case Align.CenterRight:
  323. return new Vector3(chartWidth - runtimeRight, chartHeight / 2);
  324. case Align.TopCenter:
  325. return new Vector3(chartWidth / 2, chartHeight - runtimeTop);
  326. case Align.TopLeft:
  327. return new Vector3(runtimeLeft, chartHeight - runtimeTop);
  328. case Align.TopRight:
  329. return new Vector3(chartWidth - runtimeRight, chartHeight - runtimeTop);
  330. default:
  331. return Vector2.zero;
  332. }
  333. }
  334. /// <summary>
  335. /// 属性变更时更新textAnchor,minAnchor,maxAnchor,pivot
  336. /// </summary>
  337. public void OnChanged()
  338. {
  339. UpdateAlign();
  340. }
  341. }
  342. }