优化Comment的位置,用Location代替Position

This commit is contained in:
monitor1394
2022-11-27 17:28:37 +08:00
parent 906b6ed4c7
commit f2001fbc82
5 changed files with 30 additions and 9 deletions

View File

@@ -10,7 +10,7 @@ namespace XCharts.Runtime
/// </summary>
[Serializable]
[ComponentHandler(typeof(CommentHander), true)]
public class Comment : MainComponent
public class Comment : MainComponent, IPropertyChanged
{
[SerializeField] private bool m_Show = true;
[SerializeField] private LabelStyle m_LabelStyle = new LabelStyle();
@@ -65,5 +65,17 @@ namespace XCharts.Runtime
}
return m_MarkStyle;
}
/// <summary>
/// Callback handling when parameters change.
/// |参数变更时的回调处理。
/// </summary>
public void OnChanged()
{
foreach (var item in items)
{
item.location.OnChanged();
}
}
}
}

View File

@@ -12,6 +12,7 @@ namespace XCharts.Runtime
public override void InitComponent()
{
var comment = component;
comment.OnChanged();
comment.painter = null;
comment.refreshComponent = delegate()
{
@@ -30,10 +31,12 @@ namespace XCharts.Runtime
{
var item = comment.items[i];
var labelStyle = comment.GetLabelStyle(i);
var labelPos = chart.chartPosition + item.location.GetPosition(chart.chartWidth, chart.chartHeight);
var label = ChartHelper.AddChartLabel(s_CommentObjectName + i, commentObj.transform, labelStyle, chart.theme.common,
GetContent(item), Color.clear, TextAnchor.MiddleCenter);
label.SetActive(comment.show && item.show);
label.SetPosition(item.position + labelStyle.offset);
label.SetPosition(labelPos);
label.text.SetLocalPosition(labelStyle.offset);
}
};
comment.refreshComponent();

View File

@@ -12,10 +12,11 @@ namespace XCharts.Runtime
{
[SerializeField] private bool m_Show = true;
[SerializeField] private string m_Content = "comment";
[SerializeField] private Vector3 m_Position;
[SerializeField] private Rect m_MarkRect;
[SerializeField] private CommentMarkStyle m_MarkStyle = new CommentMarkStyle() { show = false };
[SerializeField] private LabelStyle m_LabelStyle = new LabelStyle() { show = false };
[SerializeField] [Since("v3.5.0")]private Location m_Location = new Location() { align = Location.Align.TopLeft, top = 0.125f };
/// <summary>
/// Set this to false to prevent this comment item from showing.
@@ -23,11 +24,6 @@ namespace XCharts.Runtime
/// </summary>
public bool show { get { return m_Show; } set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); } }
/// <summary>
/// position of comment.
/// |注解项的位置坐标。
/// </summary>
public Vector3 position { get { return m_Position; } set { if (PropertyUtil.SetStruct(ref m_Position, value)) SetComponentDirty(); } }
/// <summary>
/// content of comment.
/// |注解的文本内容。支持模板参数可以参考Tooltip的itemFormatter。
/// </summary>
@@ -51,5 +47,14 @@ namespace XCharts.Runtime
get { return m_LabelStyle; }
set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); }
}
/// <summary>
/// The location of comment.
/// |Comment显示的位置。
/// </summary>
public Location location
{
get { return m_Location; }
set { if (PropertyUtil.SetClass(ref m_Location, value)) SetComponentDirty(); }
}
}
}