重构Component相关代码,调整API接口

This commit is contained in:
monitor1394
2023-02-12 21:22:53 +08:00
parent 22b167981b
commit 4964bca341
57 changed files with 349 additions and 185 deletions

View File

@@ -3,36 +3,40 @@ using System.Collections.Generic;
namespace XCharts.Runtime
{
/// <summary>
/// The attribute for serie component.
/// |可添加到Serie的组件。
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class SerieExtraComponentAttribute : Attribute
public sealed class SerieComponentAttribute : Attribute
{
public readonly List<Type> types = new List<Type>();
public SerieExtraComponentAttribute()
public SerieComponentAttribute()
{ }
public SerieExtraComponentAttribute(Type type1)
public SerieComponentAttribute(Type type1)
{
AddType(type1);
}
public SerieExtraComponentAttribute(Type type1, Type type2)
public SerieComponentAttribute(Type type1, Type type2)
{
AddType(type1);
AddType(type2);
}
public SerieExtraComponentAttribute(Type type1, Type type2, Type type3)
public SerieComponentAttribute(Type type1, Type type2, Type type3)
{
AddType(type1);
AddType(type2);
AddType(type3);
}
public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4)
public SerieComponentAttribute(Type type1, Type type2, Type type3, Type type4)
{
AddType(type1);
AddType(type2);
AddType(type3);
AddType(type4);
}
public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5)
public SerieComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5)
{
AddType(type1);
AddType(type2);
@@ -40,7 +44,7 @@ namespace XCharts.Runtime
AddType(type4);
AddType(type5);
}
public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6)
public SerieComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6)
{
AddType(type1);
AddType(type2);
@@ -49,7 +53,7 @@ namespace XCharts.Runtime
AddType(type5);
AddType(type6);
}
public SerieExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7)
public SerieComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7)
{
AddType(type1);
AddType(type2);
@@ -67,7 +71,7 @@ namespace XCharts.Runtime
types.Add(type);
}
public bool Contains<T>() where T : ISerieExtraComponent
public bool Contains<T>() where T : ISerieComponent
{
return Contains(typeof(T));
}

View File

@@ -2,10 +2,11 @@ using System;
namespace XCharts.Runtime
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
/// <summary>
/// What serie can convert to me
/// The attribute for which serie types can be converted to.
/// |可转化为哪些Serie类型。
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class SerieConvertAttribute : Attribute
{
public readonly Type type0;

View File

@@ -3,36 +3,41 @@ using System.Collections.Generic;
namespace XCharts.Runtime
{
/// <summary>
/// The attribute for serie data component.
/// |可添加到SerieData的组件。
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class SerieDataExtraComponentAttribute : Attribute
public sealed class SerieDataComponentAttribute : Attribute
{
public readonly List<Type> types = new List<Type>();
public SerieDataExtraComponentAttribute()
{ }
public SerieDataExtraComponentAttribute(Type type1)
public SerieDataComponentAttribute()
{
}
public SerieDataComponentAttribute(Type type1)
{
AddType(type1);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2)
public SerieDataComponentAttribute(Type type1, Type type2)
{
AddType(type1);
AddType(type2);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2, Type type3)
public SerieDataComponentAttribute(Type type1, Type type2, Type type3)
{
AddType(type1);
AddType(type2);
AddType(type3);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4)
public SerieDataComponentAttribute(Type type1, Type type2, Type type3, Type type4)
{
AddType(type1);
AddType(type2);
AddType(type3);
AddType(type4);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5)
public SerieDataComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5)
{
AddType(type1);
AddType(type2);
@@ -40,7 +45,7 @@ namespace XCharts.Runtime
AddType(type4);
AddType(type5);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6)
public SerieDataComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6)
{
AddType(type1);
AddType(type2);
@@ -49,7 +54,7 @@ namespace XCharts.Runtime
AddType(type5);
AddType(type6);
}
public SerieDataExtraComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7)
public SerieDataComponentAttribute(Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7)
{
AddType(type1);
AddType(type2);
@@ -67,7 +72,7 @@ namespace XCharts.Runtime
types.Add(type);
}
public bool Contains<T>() where T : ISerieExtraComponent
public bool Contains<T>() where T : ISerieComponent
{
return Contains(typeof(T));
}

View File

@@ -69,7 +69,15 @@ namespace XCharts.Runtime
/// </summary>
public Vector3 chartPosition { get { return m_ChartPosition; } }
public Rect chartRect { get { return m_ChartRect; } }
/// <summary>
/// The callback function of chart init.
/// |图表的初始化完成回调。
/// </summary>
public Action onInit { set { m_OnInit = value; } }
/// <summary>
/// The callback function of chart update.
/// |图表的Update回调。
/// </summary>
public Action onUpdate { set { m_OnUpdate = value; } }
/// <summary>
/// 自定义绘制回调。在绘制Serie前调用。

View File

@@ -41,7 +41,7 @@ namespace XCharts.Runtime
public T AddChartComponent<T>() where T : MainComponent
{
return (T) AddChartComponent(typeof(T));
return (T)AddChartComponent(typeof(T));
}
public T AddChartComponentWhenNoExist<T>() where T : MainComponent
@@ -118,7 +118,7 @@ namespace XCharts.Runtime
if (attrubte.handler == null)
return;
var handler = (MainComponentHandler) Activator.CreateInstance(attrubte.handler);
var handler = (MainComponentHandler)Activator.CreateInstance(attrubte.handler);
handler.attribute = attrubte;
handler.chart = this;
handler.SetComponent(component);
@@ -221,9 +221,9 @@ namespace XCharts.Runtime
return GetChartComponentNum(typeof(T));
}
private static List<MainComponent> list;
public int GetChartComponentNum(Type type)
{
List<MainComponent> list;
if (m_ComponentMaps.TryGetValue(type, out list))
return list.Count;
else
@@ -245,6 +245,7 @@ namespace XCharts.Runtime
return m_ComponentMaps[typeof(T)];
}
[Obsolete("'GetOrAddChartComponent' is obsolete, Use 'EnsureChartComponent' instead.")]
public T GetOrAddChartComponent<T>() where T : MainComponent
{
var component = GetChartComponent<T>();
@@ -253,6 +254,15 @@ namespace XCharts.Runtime
else
return component;
}
/// <summary>
/// Ensure the chart has the component, if not, add it.
/// Note: it may fail to add.
/// |确保图表有该组件,如果没有则添加。注意:有可能添加不成功。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>component, or null if add failed.</returns>
[Since("v3.6.0")]
public T EnsureChartComponent<T>() where T : MainComponent
{
var component = GetChartComponent<T>();
@@ -261,7 +271,7 @@ namespace XCharts.Runtime
else
return component;
}
public bool TryGetChartComponent<T>(out T component, int index = 0)
where T : MainComponent
{
@@ -270,7 +280,7 @@ namespace XCharts.Runtime
{
if (com is T && com.index == index)
{
component = (T) com;
component = (T)com;
return true;
}
}
@@ -425,11 +435,11 @@ namespace XCharts.Runtime
var yAxis = GetChartComponent<YAxis>();
if (yAxis.IsCategory())
{
return yAxis.GetData((int) yAxis.context.pointerValue, dataZoom);
return yAxis.GetData((int)yAxis.context.pointerValue, dataZoom);
}
else if (xAxis.IsCategory())
{
return xAxis.GetData((int) xAxis.context.pointerValue, dataZoom);
return xAxis.GetData((int)xAxis.context.pointerValue, dataZoom);
}
return null;
}
@@ -439,11 +449,11 @@ namespace XCharts.Runtime
var yAxis = GetChartComponent<YAxis>(serie.yAxisIndex);
if (yAxis.IsCategory())
{
return yAxis.GetData((int) yAxis.context.pointerValue, dataZoom);
return yAxis.GetData((int)yAxis.context.pointerValue, dataZoom);
}
else if (xAxis.IsCategory())
{
return xAxis.GetData((int) xAxis.context.pointerValue, dataZoom);
return xAxis.GetData((int)xAxis.context.pointerValue, dataZoom);
}
return null;
}

View File

@@ -0,0 +1,11 @@
namespace XCharts.Runtime
{
/// <summary>
/// The interface for serie component.
/// |可用于Serie的组件。
/// </summary>
public interface ISerieComponent
{
bool show { get; set; }
}
}

View File

@@ -2,7 +2,6 @@ namespace XCharts.Runtime
{
public interface ISerieContainer
{
//bool runtimeIsPointerEnter { get; }
int index { get; }
bool IsPointerEnter();
}

View File

@@ -1,9 +1,10 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace XCharts.Runtime
{
/// <summary>
/// The interface for serie data component.
/// |可用于SerieData的组件。
/// </summary>
public interface ISerieDataComponent
{ }
{
}
}

View File

@@ -1,11 +0,0 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace XCharts.Runtime
{
public interface ISerieExtraComponent
{
bool show { get; set; }
}
}