No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Serie.ExtraComponent.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace XCharts.Runtime
  6. {
  7. public partial class Serie
  8. {
  9. public static Dictionary<Type, string> extraComponentMap = new Dictionary<Type, string>
  10. {
  11. { typeof(LabelStyle), "m_Labels" },
  12. { typeof(LabelLine), "m_LabelLines" },
  13. { typeof(EndLabelStyle), "m_EndLabels" },
  14. { typeof(LineArrow), "m_LineArrows" },
  15. { typeof(AreaStyle), "m_AreaStyles" },
  16. { typeof(TitleStyle), "m_TitleStyles" },
  17. { typeof(EmphasisStyle), "m_EmphasisStyles" },
  18. { typeof(BlurStyle), "m_BlurStyles" },
  19. { typeof(SelectStyle), "m_SelectStyles" },
  20. };
  21. [SerializeField][IgnoreDoc] private List<LabelStyle> m_Labels = new List<LabelStyle>();
  22. [SerializeField][IgnoreDoc] private List<LabelLine> m_LabelLines = new List<LabelLine>();
  23. [SerializeField][IgnoreDoc] private List<EndLabelStyle> m_EndLabels = new List<EndLabelStyle>();
  24. [SerializeField][IgnoreDoc] private List<LineArrow> m_LineArrows = new List<LineArrow>();
  25. [SerializeField][IgnoreDoc] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>();
  26. [SerializeField][IgnoreDoc] private List<TitleStyle> m_TitleStyles = new List<TitleStyle>();
  27. [SerializeField][IgnoreDoc] private List<EmphasisStyle> m_EmphasisStyles = new List<EmphasisStyle>();
  28. [SerializeField][IgnoreDoc] private List<BlurStyle> m_BlurStyles = new List<BlurStyle>();
  29. [SerializeField][IgnoreDoc] private List<SelectStyle> m_SelectStyles = new List<SelectStyle>();
  30. /// <summary>
  31. /// The style of area.
  32. /// |区域填充样式。
  33. /// </summary>
  34. public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
  35. /// <summary>
  36. /// Text label of graphic element,to explain some data information about graphic item like value, name and so on.
  37. /// |图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
  38. /// </summary>
  39. public LabelStyle label { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } }
  40. public LabelStyle endLabel { get { return m_EndLabels.Count > 0 ? m_EndLabels[0] : null; } }
  41. /// <summary>
  42. /// The line of label.
  43. /// |标签上的视觉引导线。
  44. /// </summary>
  45. public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } }
  46. /// <summary>
  47. /// The arrow of line.
  48. /// |折线图的箭头。
  49. /// </summary>
  50. public LineArrow lineArrow { get { return m_LineArrows.Count > 0 ? m_LineArrows[0] : null; } }
  51. /// <summary>
  52. /// the icon of data.
  53. /// |数据项标题样式。
  54. /// </summary>
  55. public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
  56. /// <summary>
  57. /// style of emphasis state.
  58. /// |高亮状态的样式。
  59. /// </summary>
  60. public EmphasisStyle emphasisStyle { get { return m_EmphasisStyles.Count > 0 ? m_EmphasisStyles[0] : null; } }
  61. /// <summary>
  62. /// style of blur state.
  63. /// |淡出状态的样式。
  64. /// </summary>
  65. public BlurStyle blurStyle { get { return m_BlurStyles.Count > 0 ? m_BlurStyles[0] : null; } }
  66. /// <summary>
  67. /// style of select state.
  68. /// |选中状态的样式。
  69. /// </summary>
  70. public SelectStyle selectStyle { get { return m_SelectStyles.Count > 0 ? m_SelectStyles[0] : null; } }
  71. /// <summary>
  72. /// Remove all extra components.
  73. /// |移除所有额外组件。
  74. /// </summary>
  75. public void RemoveAllComponents()
  76. {
  77. var serieType = GetType();
  78. foreach (var kv in extraComponentMap)
  79. {
  80. ReflectionUtil.InvokeListClear(this, serieType.GetField(kv.Value));
  81. }
  82. SetAllDirty();
  83. }
  84. [Obsolete("Use EnsureComponent<T>() instead.")]
  85. public T AddExtraComponent<T>() where T : ChildComponent, ISerieComponent
  86. {
  87. return EnsureComponent<T>();
  88. }
  89. public T GetComponent<T>() where T : ChildComponent, ISerieComponent
  90. {
  91. return GetComponent(typeof(T)) as T;
  92. }
  93. /// <summary>
  94. /// Ensure the serie has the component. If not, add it.
  95. /// |确保系列有该组件。如果没有,则添加。
  96. /// </summary>
  97. /// <typeparam name="T"></typeparam>
  98. /// <returns>component or null</returns>
  99. public T EnsureComponent<T>() where T : ChildComponent, ISerieComponent
  100. {
  101. return EnsureComponent(typeof(T)) as T;
  102. }
  103. public bool CanAddComponent<T>() where T : ChildComponent, ISerieComponent
  104. {
  105. return CanAddComponent(typeof(T));
  106. }
  107. public bool CanAddComponent(Type type)
  108. {
  109. if (GetType().IsDefined(typeof(SerieComponentAttribute), false))
  110. {
  111. var attr = GetType().GetAttribute<SerieComponentAttribute>();
  112. if (attr.Contains(type))
  113. {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. public ISerieComponent GetComponent(Type type)
  120. {
  121. if (GetType().IsDefined(typeof(SerieComponentAttribute), false))
  122. {
  123. var attr = GetType().GetAttribute<SerieComponentAttribute>();
  124. if (attr.Contains(type))
  125. {
  126. var fieldName = string.Empty;
  127. if (extraComponentMap.TryGetValue(type, out fieldName))
  128. {
  129. var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  130. if (ReflectionUtil.InvokeListCount(this, field) > 0)
  131. {
  132. return ReflectionUtil.InvokeListGet<ISerieComponent>(this, field, 0);
  133. }
  134. }
  135. }
  136. }
  137. return null;
  138. }
  139. public ISerieComponent EnsureComponent(Type type)
  140. {
  141. if (GetType().IsDefined(typeof(SerieComponentAttribute), false))
  142. {
  143. var attr = GetType().GetAttribute<SerieComponentAttribute>();
  144. if (attr.Contains(type))
  145. {
  146. var fieldName = string.Empty;
  147. if (extraComponentMap.TryGetValue(type, out fieldName))
  148. {
  149. var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  150. if (ReflectionUtil.InvokeListCount(this, field) <= 0)
  151. {
  152. var extraComponent = Activator.CreateInstance(type) as ISerieComponent;
  153. ReflectionUtil.InvokeListAdd(this, field, extraComponent);
  154. SetAllDirty();
  155. return extraComponent;
  156. }
  157. else
  158. {
  159. return ReflectionUtil.InvokeListGet<ISerieComponent>(this, field, 0);
  160. }
  161. }
  162. }
  163. }
  164. throw new System.Exception(string.Format("Serie {0} not support component: {1}",
  165. GetType().Name, type.Name));
  166. }
  167. public void RemoveComponent<T>() where T : ISerieComponent
  168. {
  169. RemoveComponent(typeof(T));
  170. }
  171. public void RemoveComponent(Type type)
  172. {
  173. if (GetType().IsDefined(typeof(SerieComponentAttribute), false))
  174. {
  175. var attr = GetType().GetAttribute<SerieComponentAttribute>();
  176. if (attr.Contains(type))
  177. {
  178. var fieldName = string.Empty;
  179. if (extraComponentMap.TryGetValue(type, out fieldName))
  180. {
  181. var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  182. ReflectionUtil.InvokeListClear(this, field);
  183. SetAllDirty();
  184. return;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }