[optimize][painter] add upper and top painter layer

This commit is contained in:
monitor1394
2022-06-24 22:17:01 +08:00
parent 7c07499e44
commit 9a1c76c236
22 changed files with 215 additions and 80 deletions

View File

@@ -22,6 +22,7 @@ namespace XCharts.Editor
PropertyField(prop, "m_MaxPainter");
PropertyField(prop, "m_BasePainterMaterial");
PropertyField(prop, "m_SeriePainterMaterial");
PropertyField(prop, "m_UpperPainterMaterial");
PropertyField(prop, "m_TopPainterMaterial");
PropertyField(prop, "m_LineSmoothStyle");
PropertyField(prop, "m_LineSmoothness");

View File

@@ -4,8 +4,8 @@ using XCharts.Runtime;
namespace XCharts.Editor
{
[CustomPropertyDrawer(typeof(TextPadding), true)]
public class TextPaddingDrawer : BasePropertyDrawer
[CustomPropertyDrawer(typeof(Padding), true)]
public class PaddingDrawer : BasePropertyDrawer
{
public override string ClassName { get { return "Padding"; } }
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
@@ -22,4 +22,9 @@ namespace XCharts.Editor
}
}
}
[CustomPropertyDrawer(typeof(TextPadding), true)]
public class TextPaddingDrawer : PaddingDrawer
{
}
}

View File

@@ -20,6 +20,8 @@ namespace XCharts.Editor
PropertyField("m_Formatter");
PropertyField("m_Location");
PropertyField("m_LabelStyle");
PropertyField("m_Background");
PropertyField("m_Padding");
PropertyListField("m_Icons");
PropertyListField("m_Colors");
PropertyListField("m_Data");

View File

@@ -0,0 +1,79 @@
using System;
using UnityEngine;
namespace XCharts.Runtime
{
/// <summary>
/// padding setting of item or text.
/// |边距设置。
/// </summary>
[Serializable]
public class Padding : ChildComponent
{
[SerializeField] protected bool m_Show = true;
[SerializeField] protected float m_Top = 2;
[SerializeField] protected float m_Right = 4;
[SerializeField] protected float m_Left = 4;
[SerializeField] protected float m_Bottom = 2;
public Padding() { }
public Padding(float top, float right, float bottom, float left)
{
SetPadding(top, right, bottom, left);
}
public void SetPadding(float top, float right, float bottom, float left)
{
m_Top = top;;
m_Right = right;
m_Bottom = bottom;
m_Left = left;
}
/// <summary>
/// show padding.
/// 是否显示。
/// </summary>
public bool show
{
get { return m_Show; }
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of top.
/// |顶部间距。
/// </summary>
public float top
{
get { return m_Top; }
set { if (PropertyUtil.SetStruct(ref m_Top, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of right.
/// |右部间距。
/// </summary>
public float right
{
get { return m_Right; }
set { if (PropertyUtil.SetStruct(ref m_Right, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of bottom.
/// |底部间距。
/// </summary>
public float bottom
{
get { return m_Bottom; }
set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of left.
/// |左边间距。
/// </summary>
public float left
{
get { return m_Left; }
set { if (PropertyUtil.SetStruct(ref m_Left, value)) SetComponentDirty(); }
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c4249907274734533ba65edb14987472
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -8,72 +8,13 @@ namespace XCharts.Runtime
/// |文本的内边距设置。
/// </summary>
[Serializable]
public class TextPadding : ChildComponent
public class TextPadding : Padding
{
[SerializeField] private bool m_Show = true;
[SerializeField] private float m_Top = 2;
[SerializeField] private float m_Right = 4;
[SerializeField] private float m_Left = 4;
[SerializeField] private float m_Bottom = 2;
public TextPadding() { }
public TextPadding(float top, float right, float bottom, float left)
{
SetPadding(top, right, bottom, left);
}
public void SetPadding(float top, float right, float bottom, float left)
{
m_Top = top;;
m_Right = right;
m_Bottom = bottom;
m_Left = left;
}
/// <summary>
/// show padding.
/// 是否显示。
/// </summary>
public bool show
{
get { return m_Show; }
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of top.
/// |顶部间距。
/// </summary>
public float top
{
get { return m_Top; }
set { if (PropertyUtil.SetStruct(ref m_Top, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of right.
/// |右部间距。
/// </summary>
public float right
{
get { return m_Right; }
set { if (PropertyUtil.SetStruct(ref m_Right, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of bottom.
/// |底部间距。
/// </summary>
public float bottom
{
get { return m_Bottom; }
set { if (PropertyUtil.SetStruct(ref m_Bottom, value)) SetComponentDirty(); }
}
/// <summary>
/// padding of left.
/// |左边间距。
/// </summary>
public float left
{
get { return m_Left; }
set { if (PropertyUtil.SetStruct(ref m_Left, value)) SetComponentDirty(); }
}
}
}

View File

@@ -53,7 +53,7 @@ namespace XCharts.Runtime
}
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
for (int i = 0; i < component.items.Count; i++)
{

View File

@@ -19,7 +19,7 @@ namespace XCharts.Runtime
public override void InitComponent()
{
var dataZoom = component;
dataZoom.painter = chart.m_PainterTop;
dataZoom.painter = chart.m_PainterUpper;
dataZoom.refreshComponent = delegate()
{
var dataZoomObject = ChartHelper.AddObject(s_DefaultDataZoom + dataZoom.index, chart.transform,
@@ -62,7 +62,7 @@ namespace XCharts.Runtime
CheckDataZoomLabel(component);
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
if (chart == null)
return;

View File

@@ -39,7 +39,7 @@ namespace XCharts.Runtime
private void InitMarkArea(MarkArea markArea)
{
markArea.painter = chart.m_PainterTop;
markArea.painter = chart.m_PainterUpper;
markArea.refreshComponent = delegate()
{
var label = ChartHelper.AddChartLabel("label", m_MarkLineLabelRoot.transform, markArea.label, chart.theme.axis,

View File

@@ -19,7 +19,7 @@ namespace XCharts.Runtime
InitMarkLine(component);
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
DrawMarkLine(vh, component);
}
@@ -69,7 +69,7 @@ namespace XCharts.Runtime
private void InitMarkLineLabel(Serie serie, MarkLineData data, Color serieColor)
{
data.painter = chart.m_PainterTop;
data.painter = chart.m_PainterUpper;
data.refreshComponent = delegate()
{
var textName = string.Format("markLine_{0}_{1}", serie.index, data.index);

View File

@@ -15,6 +15,7 @@ namespace XCharts.Runtime
[SerializeField] protected bool m_ReversePainter = false;
[SerializeField] protected Material m_BasePainterMaterial;
[SerializeField] protected Material m_SeriePainterMaterial;
[SerializeField] protected Material m_UpperPainterMaterial;
[SerializeField] protected Material m_TopPainterMaterial;
[SerializeField][Range(1, 10)] protected float m_LineSmoothStyle = 3f;
[SerializeField][Range(1f, 20)] protected float m_LineSmoothness = 2f;
@@ -58,7 +59,7 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetClass(ref m_SeriePainterMaterial, value)) SetComponentDirty(); }
}
/// <summary>
/// Top Pointer 材质球设置后会影响Tooltip等
/// Top Pointer 材质球。
/// </summary>
public Material topPainterMaterial
{
@@ -66,6 +67,14 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetClass(ref m_TopPainterMaterial, value)) SetComponentDirty(); }
}
/// <summary>
/// Upper Pointer 材质球。
/// </summary>
public Material upperPainterMaterial
{
get { return m_UpperPainterMaterial; }
set { if (PropertyUtil.SetClass(ref m_UpperPainterMaterial, value)) SetComponentDirty(); }
}
/// <summary>
/// Curve smoothing factor. By adjusting the smoothing coefficient, the curvature of the curve can be changed,
/// and different curves with slightly different appearance can be obtained.
/// |曲线平滑系数。通过调整平滑系数可以改变曲线的曲率,得到外观稍微有变化的不同曲线。
@@ -134,6 +143,7 @@ namespace XCharts.Runtime
m_MaxPainter = settings.maxPainter;
m_BasePainterMaterial = settings.basePainterMaterial;
m_SeriePainterMaterial = settings.seriePainterMaterial;
m_UpperPainterMaterial = settings.upperPainterMaterial;
m_TopPainterMaterial = settings.topPainterMaterial;
m_LineSmoothStyle = settings.lineSmoothStyle;
m_LineSmoothness = settings.lineSmoothness;

View File

@@ -24,7 +24,7 @@ namespace XCharts.Runtime
var titleObject = ChartHelper.AddObject(objName, chart.transform, anchorMin, anchorMax,
pivot, chart.chartSizeDelta);
title.gameObject = titleObject;
title.gameObject.transform.SetSiblingIndex(chart.m_PainterTop.transform.GetSiblingIndex() + 1);
title.gameObject.transform.SetSiblingIndex(chart.m_PainterUpper.transform.GetSiblingIndex() + 1);
anchorMin = title.location.runtimeAnchorMin;
anchorMax = title.location.runtimeAnchorMax;
pivot = title.location.runtimePivot;

View File

@@ -26,14 +26,14 @@ namespace XCharts.Runtime
component.view.Update();
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
DrawTooltipIndicator(vh, component);
}
private void InitTooltip(Tooltip tooltip)
{
tooltip.painter = chart.m_PainterTop;
tooltip.painter = chart.m_PainterUpper;
tooltip.refreshComponent = delegate()
{
var objName = ChartCached.GetComponentObjectName(tooltip);

View File

@@ -57,7 +57,7 @@ namespace XCharts.Runtime
DrawCoord(vh, component);
}
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
if (SeriesHelper.IsAnyClipSerie(chart.series))
{

View File

@@ -56,7 +56,7 @@ namespace XCharts.Runtime
DrawCoord(vh);
}
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
if (SeriesHelper.IsAnyClipSerie(chart.series))
{

View File

@@ -84,7 +84,11 @@ namespace XCharts.Runtime
/// </summary>
public Action<VertexHelper, Serie> onDrawAfterSerie { set { m_OnDrawSerieAfter = value; } }
/// <summary>
/// 自定义Top绘制回调。在绘制Tooltip前调用。
/// 自定义Upper层绘制回调。在绘制Tooltip前调用。
/// </summary>
public Action<VertexHelper> onDrawUpper { set { m_OnDrawUpper = value; } }
/// <summary>
/// 自定义Top层绘制回调。在绘制Tooltip前调用。
/// </summary>
public Action<VertexHelper> onDrawTop { set { m_OnDrawTop = value; } }
/// <summary>
@@ -143,6 +147,7 @@ namespace XCharts.Runtime
m_RefreshChart = true;
if (m_Painter) m_Painter.Refresh();
foreach (var painter in m_PainterList) painter.Refresh();
if (m_PainterUpper) m_PainterUpper.Refresh();
if (m_PainterTop) m_PainterTop.Refresh();
}
@@ -507,6 +512,19 @@ namespace XCharts.Runtime
}
}
/// <summary>
/// 设置Upper Painter的材质球
/// </summary>
/// <param name="material"></param>
public void SetUpperPainterMaterial(Material material)
{
settings.upperPainterMaterial = material;
if (m_PainterUpper != null)
{
m_PainterUpper.material = material;
}
}
/// <summary>
/// 设置Top Painter的材质球
/// </summary>

View File

@@ -81,6 +81,7 @@ namespace XCharts.Runtime
protected Action m_OnInit;
protected Action m_OnUpdate;
protected Action<VertexHelper> m_OnDrawBase;
protected Action<VertexHelper> m_OnDrawUpper;
protected Action<VertexHelper> m_OnDrawTop;
protected Action<VertexHelper, Serie> m_OnDrawSerieBefore;
protected Action<VertexHelper, Serie> m_OnDrawSerieAfter;
@@ -96,8 +97,10 @@ namespace XCharts.Runtime
internal bool m_CheckAnimation = false;
internal protected List<string> m_LegendRealShowName = new List<string>();
protected List<Painter> m_PainterList = new List<Painter>();
internal Painter m_PainterUpper;
internal Painter m_PainterTop;
internal int m_BasePainterVertCount;
internal int m_UpperPainterVertCount;
internal int m_TopPainterVertCount;
private ThemeType m_CheckTheme = 0;
@@ -201,6 +204,11 @@ namespace XCharts.Runtime
m_PainterTop.Refresh();
}
public void RefreshUpperPainter()
{
m_PainterUpper.Refresh();
}
public void RefreshPainter(int index)
{
var painter = GetPainter(index);
@@ -218,7 +226,7 @@ namespace XCharts.Runtime
base.RefreshPainter(painter);
if (painter != null && painter.type == Painter.Type.Serie)
{
m_PainterTop.Refresh();
m_PainterUpper.Refresh();
}
}
@@ -306,6 +314,10 @@ namespace XCharts.Runtime
serie.index = i;
SetPainterActive(i, true);
}
if (transform.childCount - 3 != m_PainterTop.transform.GetSiblingIndex())
{
m_PainterTop.transform.SetSiblingIndex(transform.childCount - 3);
}
}
protected override void InitPainter()
@@ -328,6 +340,14 @@ namespace XCharts.Runtime
painter.transform.SetSiblingIndex(index + 1);
m_PainterList.Add(painter);
}
m_PainterUpper = ChartHelper.AddPainterObject("painter_u", transform, m_GraphMinAnchor,
m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
m_PainterUpper.type = Painter.Type.Top;
m_PainterUpper.onPopulateMesh = OnDrawPainterUpper;
m_PainterUpper.SetActive(true, m_DebugInfo.showAllChartObject);
m_PainterUpper.material = settings.topPainterMaterial;
m_PainterUpper.transform.SetSiblingIndex(settings.maxPainter + 1);
m_PainterTop = ChartHelper.AddPainterObject("painter_t", transform, m_GraphMinAnchor,
m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
m_PainterTop.type = Painter.Type.Top;
@@ -372,6 +392,7 @@ namespace XCharts.Runtime
if (m_Painter == null) return;
m_Painter.CheckRefresh();
foreach (var painter in m_PainterList) painter.CheckRefresh();
if (m_PainterUpper != null) m_PainterUpper.CheckRefresh();
if (m_PainterTop != null) m_PainterTop.CheckRefresh();
}
@@ -543,6 +564,7 @@ namespace XCharts.Runtime
var maxPainter = settings.maxPainter;
var maxSeries = m_Series.Count;
var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
m_PainterUpper.Refresh();
m_PainterTop.Refresh();
m_DebugInfo.refreshCount++;
for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++)
@@ -574,6 +596,18 @@ namespace XCharts.Runtime
}
}
protected virtual void OnDrawPainterUpper(VertexHelper vh, Painter painter)
{
vh.Clear();
DrawPainterUpper(vh);
foreach (var draw in m_ComponentHandlers) draw.DrawUpper(vh);
if (m_OnDrawUpper != null)
{
m_OnDrawUpper(vh);
}
m_UpperPainterVertCount = vh.currentVertCount;
}
protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter)
{
vh.Clear();
@@ -588,6 +622,12 @@ namespace XCharts.Runtime
protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie) { }
protected virtual void DrawPainterUpper(VertexHelper vh)
{
foreach (var handler in m_SerieHandlers)
handler.DrawUpper(vh);
}
protected virtual void DrawPainterTop(VertexHelper vh)
{
foreach (var handler in m_SerieHandlers)

View File

@@ -96,6 +96,7 @@ namespace XCharts.Runtime
public virtual void CheckComponent(StringBuilder sb) { }
public virtual void Update() { }
public virtual void DrawBase(VertexHelper vh) { }
public virtual void DrawUpper(VertexHelper vh) { }
public virtual void DrawTop(VertexHelper vh) { }
public virtual void OnSerieDataUpdate(int serieIndex) { }
public virtual void OnPointerClick(PointerEventData eventData) { }

View File

@@ -13,6 +13,7 @@ namespace XCharts.Runtime
private Button m_Button;
private Image m_Icon;
private ChartText m_Text;
private Image m_Background;
private Image m_TextBackground;
private RectTransform m_Rect;
private RectTransform m_IconRect;
@@ -53,7 +54,7 @@ namespace XCharts.Runtime
}
else
{
return 0;
return m_Text.GetPreferredHeight();
}
}
}
@@ -64,6 +65,7 @@ namespace XCharts.Runtime
m_Button = obj.GetComponent<Button>();
m_Rect = obj.GetComponent<RectTransform>();
m_Icon = obj.transform.Find("icon").gameObject.GetComponent<Image>();
m_Background = obj.GetComponent<Image>();
m_TextBackground = obj.transform.Find("content").gameObject.GetComponent<Image>();
m_Text = new ChartText(obj);
m_IconRect = m_Icon.gameObject.GetComponent<RectTransform>();
@@ -172,7 +174,8 @@ namespace XCharts.Runtime
public bool SetContent(string content)
{
if (m_Text != null && !m_Text.GetText().Equals(content))
if (m_Text == null) return false;
if (!m_Text.GetText().Equals(content))
{
m_Text.SetText(content);
if (m_LabelAutoSize)
@@ -186,11 +189,13 @@ namespace XCharts.Runtime
m_TextRect.anchoredPosition3D = new Vector3(m_LabelPaddingLeftRight, 0);
m_TextBackgroundRect.sizeDelta = new Vector2(m_Text.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
m_Text.GetPreferredHeight() + m_LabelPaddingTopBottom * 2 - 4);
m_Rect.sizeDelta = new Vector3(width, height);
}
m_Rect.sizeDelta = new Vector3(width, height);
return sizeChange;
}
}
m_Rect.sizeDelta = new Vector3(width, height);
return false;
}
@@ -209,5 +214,10 @@ namespace XCharts.Runtime
m_GameObject.SetActive(active);
}
}
public void SetBackground(ImageStyle imageStyle)
{
ChartHelper.SetBackground(m_Background, imageStyle);
}
}
}

View File

@@ -314,6 +314,22 @@ namespace XCharts.Runtime
return img;
}
public static void SetBackground(Image background, ImageStyle imageStyle)
{
if (background == null) return;
if (imageStyle.show)
{
background.sprite = imageStyle.sprite;
background.color = imageStyle.color;
background.type = imageStyle.type;
}
else
{
background.sprite = null;
background.color = Color.clear;
}
}
public static ChartLabel AddAxisLabelObject(int total, int index, string name, Transform parent,
Vector2 sizeDelta, Axis axis, ComponentTheme theme,
string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter)

View File

@@ -48,7 +48,7 @@ namespace XCharts.Runtime
}
}
public override void DrawTop(VertexHelper vh)
public override void DrawUpper(VertexHelper vh)
{
if (serie.IsUseCoord<GridCoord>())
{

View File

@@ -18,6 +18,7 @@ namespace XCharts.Runtime
public virtual void Update() { }
public virtual void DrawBase(VertexHelper vh) { }
public virtual void DrawSerie(VertexHelper vh) { }
public virtual void DrawUpper(VertexHelper vh) { }
public virtual void DrawTop(VertexHelper vh) { }
public virtual void OnPointerClick(PointerEventData eventData) { }
public virtual void OnPointerDown(PointerEventData eventData) { }