mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 00:20:18 +00:00
3.0 - unitypackage
This commit is contained in:
106
Runtime/Component/Title/Title.cs
Normal file
106
Runtime/Component/Title/Title.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// Title component, including main title and subtitle.
|
||||
/// 标题组件,包含主标题和副标题。
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[ComponentHandler(typeof(TitleHander), true)]
|
||||
public class Title : MainComponent, IPropertyChanged
|
||||
{
|
||||
[SerializeField] private bool m_Show = true;
|
||||
[SerializeField] private string m_Text = "Chart Title";
|
||||
[SerializeField] private TextStyle m_TextStyle = new TextStyle();
|
||||
[SerializeField] private string m_SubText = "";
|
||||
[SerializeField] private TextStyle m_SubTextStyle = new TextStyle();
|
||||
[SerializeField] private float m_ItemGap = 0;
|
||||
[SerializeField] private Location m_Location = Location.defaultTop;
|
||||
|
||||
/// <summary>
|
||||
/// [default:true]
|
||||
/// Set this to false to prevent the title from showing.
|
||||
/// 是否显示标题组件。
|
||||
/// </summary>
|
||||
public bool show { get { return m_Show; } set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); } }
|
||||
/// <summary>
|
||||
/// The main title text, supporting \n for newlines.
|
||||
/// 主标题文本,支持使用 \n 换行。
|
||||
/// </summary>
|
||||
public string text { get { return m_Text; } set { if (PropertyUtil.SetClass(ref m_Text, value)) SetComponentDirty(); } }
|
||||
/// <summary>
|
||||
/// The text style of main title.
|
||||
/// 主标题文本样式。
|
||||
/// </summary>
|
||||
public TextStyle textStyle
|
||||
{
|
||||
get { return m_TextStyle; }
|
||||
set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetComponentDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Subtitle text, supporting for \n for newlines.
|
||||
/// 副标题文本,支持使用 \n 换行。
|
||||
/// </summary>
|
||||
public string subText
|
||||
{
|
||||
get { return m_SubText; }
|
||||
set { if (PropertyUtil.SetClass(ref m_SubText, value)) SetComponentDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// The text style of sub title.
|
||||
/// 副标题文本样式。
|
||||
/// </summary>
|
||||
public TextStyle subTextStyle
|
||||
{
|
||||
get { return m_SubTextStyle; }
|
||||
set { if (PropertyUtil.SetClass(ref m_SubTextStyle, value)) SetComponentDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// [default:8]
|
||||
/// The gap between the main title and subtitle.
|
||||
/// 主副标题之间的间距。
|
||||
/// </summary>
|
||||
public float itemGap
|
||||
{
|
||||
get { return m_ItemGap; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_ItemGap, value)) SetComponentDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// The location of title component.
|
||||
/// 标题显示位置。
|
||||
/// </summary>
|
||||
public Location location
|
||||
{
|
||||
get { return m_Location; }
|
||||
set { if (PropertyUtil.SetClass(ref m_Location, value)) SetComponentDirty(); }
|
||||
}
|
||||
|
||||
public override bool vertsDirty { get { return false; } }
|
||||
public override bool componentDirty
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_ComponentDirty
|
||||
|| location.componentDirty
|
||||
|| textStyle.componentDirty
|
||||
|| subTextStyle.componentDirty;
|
||||
}
|
||||
}
|
||||
|
||||
public override void ClearComponentDirty()
|
||||
{
|
||||
base.ClearComponentDirty();
|
||||
location.ClearComponentDirty();
|
||||
textStyle.ClearComponentDirty();
|
||||
subTextStyle.ClearComponentDirty();
|
||||
}
|
||||
|
||||
public void OnChanged()
|
||||
{
|
||||
m_Location.OnChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Component/Title/Title.cs.meta
Normal file
11
Runtime/Component/Title/Title.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c4f5a39710624b94a3d015eb552f53a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
58
Runtime/Component/Title/TitleHandler.cs
Normal file
58
Runtime/Component/Title/TitleHandler.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
internal sealed class TitleHander : MainComponentHandler<Title>
|
||||
{
|
||||
private static readonly string s_TitleObjectName = "title";
|
||||
private static readonly string s_SubTitleObjectName = "title_sub";
|
||||
|
||||
public override void InitComponent()
|
||||
{
|
||||
var title = component;
|
||||
title.painter = null;
|
||||
title.refreshComponent = delegate ()
|
||||
{
|
||||
title.OnChanged();
|
||||
var anchorMin = title.location.runtimeAnchorMin;
|
||||
var anchorMax = title.location.runtimeAnchorMax;
|
||||
var pivot = title.location.runtimePivot;
|
||||
var objName = ChartCached.GetComponentObjectName(title);
|
||||
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);
|
||||
anchorMin = title.location.runtimeAnchorMin;
|
||||
anchorMax = title.location.runtimeAnchorMax;
|
||||
pivot = title.location.runtimePivot;
|
||||
title.textStyle.UpdateAlignmentByLocation(title.location);
|
||||
title.subTextStyle.UpdateAlignmentByLocation(title.location);
|
||||
var fontSize = title.textStyle.GetFontSize(chart.theme.title);
|
||||
ChartHelper.UpdateRectTransform(titleObject, anchorMin, anchorMax, pivot, new Vector2(chart.chartWidth, chart.chartHeight));
|
||||
var titlePosition = chart.GetTitlePosition(title);
|
||||
var subTitlePosition = -new Vector3(0, fontSize + title.itemGap, 0);
|
||||
var titleWid = chart.chartWidth;
|
||||
|
||||
titleObject.transform.localPosition = titlePosition;
|
||||
titleObject.hideFlags = chart.chartHideFlags;
|
||||
ChartHelper.HideAllObject(titleObject);
|
||||
|
||||
var titleText = ChartHelper.AddTextObject(s_TitleObjectName, titleObject.transform, anchorMin, anchorMax,
|
||||
pivot, new Vector2(titleWid, fontSize), title.textStyle, chart.theme.title);
|
||||
titleText.SetActive(title.show);
|
||||
titleText.SetLocalPosition(Vector3.zero + title.textStyle.offsetv3);
|
||||
titleText.SetText(title.text);
|
||||
|
||||
var subText = ChartHelper.AddTextObject(s_SubTitleObjectName, titleObject.transform, anchorMin, anchorMax,
|
||||
pivot, new Vector2(titleWid, title.subTextStyle.GetFontSize(chart.theme.subTitle)), title.subTextStyle,
|
||||
chart.theme.subTitle);
|
||||
subText.SetActive(title.show && !string.IsNullOrEmpty(title.subText));
|
||||
subText.SetLocalPosition(subTitlePosition + title.subTextStyle.offsetv3);
|
||||
subText.SetText(title.subText);
|
||||
};
|
||||
title.refreshComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Component/Title/TitleHandler.cs.meta
Normal file
11
Runtime/Component/Title/TitleHandler.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbe3062b7770040e6b4a98026f0ad044
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
89
Runtime/Component/Title/TitleStyle.cs
Normal file
89
Runtime/Component/Title/TitleStyle.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
/// <summary>
|
||||
/// the title of serie.
|
||||
/// 标题相关设置。
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class TitleStyle : ChildComponent
|
||||
{
|
||||
[SerializeField] private bool m_Show;
|
||||
[FormerlySerializedAs("m_textStyle")]
|
||||
[SerializeField] private TextStyle m_TextStyle = new TextStyle();
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show title.
|
||||
/// 是否显示标题。
|
||||
/// </summary>
|
||||
public bool show
|
||||
{
|
||||
get { return m_Show; }
|
||||
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// the color of text.
|
||||
/// 文本的颜色。
|
||||
/// </summary>
|
||||
public TextStyle textStyle
|
||||
{
|
||||
get { return m_TextStyle; }
|
||||
set { if (PropertyUtil.SetClass(ref m_TextStyle, value, true)) SetComponentDirty(); }
|
||||
}
|
||||
|
||||
public override bool componentDirty { get { return m_ComponentDirty || textStyle.componentDirty; } }
|
||||
|
||||
public override void ClearComponentDirty()
|
||||
{
|
||||
base.ClearComponentDirty();
|
||||
textStyle.ClearComponentDirty();
|
||||
}
|
||||
|
||||
public ChartText runtimeText { get; set; }
|
||||
|
||||
public bool IsInited()
|
||||
{
|
||||
return runtimeText != null;
|
||||
}
|
||||
|
||||
public void SetActive(bool active)
|
||||
{
|
||||
if (runtimeText != null)
|
||||
{
|
||||
runtimeText.SetActive(active);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePosition(Vector3 pos)
|
||||
{
|
||||
if (runtimeText != null)
|
||||
{
|
||||
runtimeText.SetLocalPosition(pos + new Vector3(m_TextStyle.offset.x, m_TextStyle.offset.y));
|
||||
}
|
||||
}
|
||||
|
||||
public void SetText(string text)
|
||||
{
|
||||
if (runtimeText == null) return;
|
||||
var oldText = runtimeText.GetText();
|
||||
if (oldText != null && !oldText.Equals(text))
|
||||
{
|
||||
if (!ChartHelper.IsClearColor(textStyle.color)) runtimeText.SetColor(textStyle.color);
|
||||
runtimeText.SetText(text);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetColor(Color color)
|
||||
{
|
||||
if (runtimeText != null)
|
||||
{
|
||||
runtimeText.SetColor(color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Component/Title/TitleStyle.cs.meta
Normal file
11
Runtime/Component/Title/TitleStyle.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cd97375f7d84f4fd18dab048c465cdd8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user