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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. { { typeof(LabelStyle), "m_Labels" },
  11. { typeof(LabelLine), "m_LabelLines" },
  12. { typeof(EndLabelStyle), "m_EndLabels" },
  13. { typeof(LineArrow), "m_LineArrows" },
  14. { typeof(AreaStyle), "m_AreaStyles" },
  15. { typeof(TitleStyle), "m_TitleStyles" },
  16. { typeof(EmphasisStyle), "m_EmphasisStyles" },
  17. { typeof(BlurStyle), "m_BlurStyles" },
  18. { typeof(SelectStyle), "m_SelectStyles" },
  19. };
  20. [SerializeField][IgnoreDoc] private List<LabelStyle> m_Labels = new List<LabelStyle>();
  21. [SerializeField][IgnoreDoc] private List<LabelLine> m_LabelLines = new List<LabelLine>();
  22. [SerializeField][IgnoreDoc] private List<EndLabelStyle> m_EndLabels = new List<EndLabelStyle>();
  23. [SerializeField][IgnoreDoc] private List<LineArrow> m_LineArrows = new List<LineArrow>();
  24. [SerializeField][IgnoreDoc] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>();
  25. [SerializeField][IgnoreDoc] private List<TitleStyle> m_TitleStyles = new List<TitleStyle>();
  26. [SerializeField][IgnoreDoc] private List<EmphasisStyle> m_EmphasisStyles = new List<EmphasisStyle>();
  27. [SerializeField][IgnoreDoc] private List<BlurStyle> m_BlurStyles = new List<BlurStyle>();
  28. [SerializeField][IgnoreDoc] private List<SelectStyle> m_SelectStyles = new List<SelectStyle>();
  29. /// <summary>
  30. /// The style of area.
  31. /// |区域填充样式。
  32. /// </summary>
  33. public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
  34. /// <summary>
  35. /// Text label of graphic element,to explain some data information about graphic item like value, name and so on.
  36. /// |图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
  37. /// </summary>
  38. public LabelStyle label { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } }
  39. public LabelStyle endLabel { get { return m_EndLabels.Count > 0 ? m_EndLabels[0] : null; } }
  40. /// <summary>
  41. /// The line of label.
  42. /// |标签上的视觉引导线。
  43. /// </summary>
  44. public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } }
  45. /// <summary>
  46. /// The arrow of line.
  47. /// |折线图的箭头。
  48. /// </summary>
  49. public LineArrow lineArrow { get { return m_LineArrows.Count > 0 ? m_LineArrows[0] : null; } }
  50. /// <summary>
  51. /// the icon of data.
  52. /// |数据项标题样式。
  53. /// </summary>
  54. public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
  55. /// <summary>
  56. /// style of emphasis state.
  57. /// |高亮状态的样式。
  58. /// </summary>
  59. public EmphasisStyle emphasisStyle { get { return m_EmphasisStyles.Count > 0 ? m_EmphasisStyles[0] : null; } }
  60. /// <summary>
  61. /// style of blur state.
  62. /// |淡出状态的样式。
  63. /// </summary>
  64. public BlurStyle blurStyle { get { return m_BlurStyles.Count > 0 ? m_BlurStyles[0] : null; } }
  65. /// <summary>
  66. /// style of select state.
  67. /// |选中状态的样式。
  68. /// </summary>
  69. public SelectStyle selectStyle { get { return m_SelectStyles.Count > 0 ? m_SelectStyles[0] : null; } }
  70. public void RemoveAllExtraComponent()
  71. {
  72. var serieType = GetType();
  73. foreach (var kv in extraComponentMap)
  74. {
  75. ReflectionUtil.InvokeListClear(this, serieType.GetField(kv.Value));
  76. }
  77. SetAllDirty();
  78. }
  79. public T AddExtraComponent<T>() where T : ChildComponent, ISerieExtraComponent
  80. {
  81. return AddExtraComponent(typeof(T)) as T;
  82. }
  83. public ISerieExtraComponent AddExtraComponent(Type type)
  84. {
  85. if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false))
  86. {
  87. var attr = GetType().GetAttribute<SerieExtraComponentAttribute>();
  88. if (attr.Contains(type))
  89. {
  90. var fieldName = string.Empty;
  91. if (extraComponentMap.TryGetValue(type, out fieldName))
  92. {
  93. var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  94. if (ReflectionUtil.InvokeListCount(this, field) <= 0)
  95. {
  96. var extraComponent = Activator.CreateInstance(type) as ISerieExtraComponent;
  97. ReflectionUtil.InvokeListAdd(this, field, extraComponent);
  98. SetAllDirty();
  99. return extraComponent;
  100. }
  101. else
  102. {
  103. return ReflectionUtil.InvokeListGet<ISerieExtraComponent>(this, field, 0);
  104. }
  105. }
  106. }
  107. }
  108. throw new System.Exception(string.Format("Serie {0} not support extra component: {1}",
  109. GetType().Name, type.Name));
  110. }
  111. public void RemoveExtraComponent<T>() where T : ISerieExtraComponent
  112. {
  113. RemoveExtraComponent(typeof(T));
  114. }
  115. public void RemoveExtraComponent(Type type)
  116. {
  117. if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false))
  118. {
  119. var attr = GetType().GetAttribute<SerieExtraComponentAttribute>();
  120. if (attr.Contains(type))
  121. {
  122. var fieldName = string.Empty;
  123. if (extraComponentMap.TryGetValue(type, out fieldName))
  124. {
  125. var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  126. ReflectionUtil.InvokeListClear(this, field);
  127. SetAllDirty();
  128. return;
  129. }
  130. }
  131. }
  132. throw new System.Exception(string.Format("Serie {0} not support extra component: {1}",
  133. GetType().Name, type.Name));
  134. }
  135. private void RemoveExtraComponentList<T>(List<T> list) where T : ISerieExtraComponent
  136. {
  137. if (list.Count > 0)
  138. {
  139. list.Clear();
  140. SetAllDirty();
  141. }
  142. }
  143. }
  144. }