mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-23 09:20:08 +00:00
v3.0.0-preivew8
This commit is contained in:
@@ -6,22 +6,13 @@ using UnityEngine.UI;
|
||||
namespace XCharts.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// The delegate function for AxisLabel's formatter.
|
||||
/// |AxisLabel的formatter自定义委托。
|
||||
/// </summary>
|
||||
/// <param name="labelIndex">label索引</param>
|
||||
/// <param name="value">当前label对应的数值数据,Value轴或Time轴有效</param>
|
||||
/// <param name="category">当前label对应的类目数据,Category轴有效</param>
|
||||
/// <returns>最终显示的文本内容</returns>
|
||||
public delegate string AxisLabelFormatterFunction(int labelIndex, double value, string category);
|
||||
/// <summary>
|
||||
/// The delegate function for SerieLabel‘s formatter.
|
||||
/// The delegate function for LabelStyle‘s formatter.
|
||||
/// |SerieLabel的formatter自定义委托。
|
||||
/// </summary>
|
||||
/// <param name="dataIndex">数据索引</param>
|
||||
/// <param name="value">数值</param>
|
||||
/// <returns>最终显示的文本内容</returns>
|
||||
public delegate string SerieLabelFormatterFunction(int dataIndex, double value);
|
||||
public delegate string LabelFormatterFunction(int dataIndex, double value, string category);
|
||||
public delegate float AnimationDelayFunction(int dataIndex);
|
||||
public delegate float AnimationDurationFunction(int dataIndex);
|
||||
/// <summary>
|
||||
|
||||
@@ -8,67 +8,87 @@ namespace XCharts.Runtime
|
||||
{
|
||||
[SerializeField] private ChartText m_LabelText;
|
||||
|
||||
private bool m_AutoHideIconWhenLabelEmpty = false;
|
||||
private bool m_LabelAutoSize = true;
|
||||
private float m_LabelPaddingLeftRight = 3f;
|
||||
private float m_LabelPaddingTopBottom = 3f;
|
||||
private RectTransform m_LabelRect;
|
||||
private RectTransform m_LabelBackgroundRect;
|
||||
private bool m_HideIconIfTextEmpty = false;
|
||||
private bool m_AutoSize = true;
|
||||
private float m_PaddingLeft = 0;
|
||||
private float m_PaddingRight = 0;
|
||||
private float m_PaddingTop = 0;
|
||||
private float m_PaddingBottom = 0;
|
||||
private float m_Width = 0;
|
||||
private float m_Height = 0;
|
||||
private RectTransform m_TextRect;
|
||||
private RectTransform m_IconRect;
|
||||
private RectTransform m_ObjectRect;
|
||||
private Vector3 m_IconOffest;
|
||||
private Align m_Align = Align.Left;
|
||||
private Image m_IconImage;
|
||||
private Image m_LabelBackgroundImage;
|
||||
|
||||
public Image icon
|
||||
{
|
||||
get { return m_IconImage; }
|
||||
set { SetIcon(value); }
|
||||
}
|
||||
public Image labelBackground
|
||||
{
|
||||
get { return m_LabelBackgroundImage; }
|
||||
set { SetLabelBackground(value); }
|
||||
}
|
||||
public ChartText label
|
||||
public ChartText text
|
||||
{
|
||||
get { return m_LabelText; }
|
||||
set
|
||||
{
|
||||
m_LabelText = value;
|
||||
if (value != null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
|
||||
if (value != null) m_TextRect = m_LabelText.gameObject.GetComponent<RectTransform>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
|
||||
public bool hideIconIfTextEmpty { set { m_HideIconIfTextEmpty = value; } }
|
||||
public bool isIconActive { get; private set; }
|
||||
public bool isAnimationEnd { get; internal set; }
|
||||
|
||||
internal RectTransform objectRect
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_ObjectRect == null)
|
||||
m_ObjectRect = gameObject.GetComponent<RectTransform>();
|
||||
return m_ObjectRect;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
m_ObjectRect = gameObject.GetComponent<RectTransform>();
|
||||
raycastTarget = false;
|
||||
}
|
||||
|
||||
public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom)
|
||||
public void SetTextPadding(TextPadding padding)
|
||||
{
|
||||
m_LabelAutoSize = autoSize;
|
||||
m_LabelPaddingLeftRight = paddingLeftRight;
|
||||
m_LabelPaddingTopBottom = paddingTopBottom;
|
||||
m_LabelText = new ChartText(labelObj);
|
||||
m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
|
||||
|
||||
m_Align = Align.Left;
|
||||
m_PaddingLeft = padding.left;
|
||||
m_PaddingRight = padding.right;
|
||||
m_PaddingTop = padding.top;
|
||||
m_PaddingBottom = padding.bottom;
|
||||
UpdatePadding();
|
||||
}
|
||||
|
||||
public void SetLabelBackground(Image image)
|
||||
public void SetPadding(float[] padding)
|
||||
{
|
||||
m_LabelBackgroundImage = image;
|
||||
if (image != null)
|
||||
if (padding.Length >= 4)
|
||||
{
|
||||
m_LabelBackgroundRect = m_LabelBackgroundImage.GetComponent<RectTransform>();
|
||||
m_PaddingLeft = padding[3];
|
||||
m_PaddingRight = padding[1];
|
||||
m_PaddingTop = padding[0];
|
||||
m_PaddingBottom = padding[2];
|
||||
}
|
||||
else if (padding.Length >= 2)
|
||||
{
|
||||
m_PaddingLeft = padding[1];
|
||||
m_PaddingRight = padding[1];
|
||||
m_PaddingTop = padding[0];
|
||||
m_PaddingBottom = padding[0];
|
||||
}
|
||||
else if (padding.Length == 1)
|
||||
{
|
||||
m_PaddingLeft = padding[0];
|
||||
m_PaddingRight = padding[0];
|
||||
m_PaddingTop = padding[0];
|
||||
m_PaddingBottom = padding[0];
|
||||
}
|
||||
UpdatePadding();
|
||||
}
|
||||
|
||||
public void SetIcon(Image image)
|
||||
@@ -80,9 +100,22 @@ namespace XCharts.Runtime
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAutoSize(bool flag)
|
||||
public float GetWidth()
|
||||
{
|
||||
m_LabelAutoSize = flag;
|
||||
return m_Width;
|
||||
}
|
||||
|
||||
public float GetHeight()
|
||||
{
|
||||
return m_Height;
|
||||
}
|
||||
|
||||
public void SetSize(float width, float height)
|
||||
{
|
||||
this.m_Width = width;
|
||||
this.m_Height = height;
|
||||
m_AutoSize = width == 0 && height == 0;
|
||||
objectRect.sizeDelta = new Vector2(width, height);
|
||||
}
|
||||
|
||||
public void SetIconSprite(Sprite sprite)
|
||||
@@ -108,24 +141,24 @@ namespace XCharts.Runtime
|
||||
m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
|
||||
m_IconOffest = iconStyle.offset;
|
||||
m_Align = iconStyle.align;
|
||||
m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
|
||||
m_HideIconIfTextEmpty = iconStyle.autoHideWhenLabelEmpty;
|
||||
AdjustIconPos();
|
||||
if (iconStyle.layer == IconStyle.Layer.UnderLabel)
|
||||
if (iconStyle.layer == IconStyle.Layer.UnderText)
|
||||
m_IconRect.SetSiblingIndex(0);
|
||||
else
|
||||
m_IconRect.SetSiblingIndex(transform.childCount - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetLabelWidth()
|
||||
public float GetTextWidth()
|
||||
{
|
||||
if (m_LabelRect) return m_LabelRect.sizeDelta.x;
|
||||
if (m_TextRect) return m_TextRect.sizeDelta.x;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public float GetLabelHeight()
|
||||
public float GetTextHeight()
|
||||
{
|
||||
if (m_LabelRect) return m_LabelRect.sizeDelta.y;
|
||||
if (m_TextRect) return m_TextRect.sizeDelta.y;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -134,7 +167,7 @@ namespace XCharts.Runtime
|
||||
if (m_LabelText != null) m_LabelText.SetColor(color);
|
||||
}
|
||||
|
||||
public void SetLabelRotate(float rotate)
|
||||
public void SetTextRotate(float rotate)
|
||||
{
|
||||
if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
|
||||
}
|
||||
@@ -144,21 +177,21 @@ namespace XCharts.Runtime
|
||||
transform.localPosition = position;
|
||||
}
|
||||
|
||||
public void SetRectPosition(Vector3 position)
|
||||
{
|
||||
objectRect.anchoredPosition3D = position;
|
||||
}
|
||||
|
||||
public Vector3 GetPosition()
|
||||
{
|
||||
return transform.localPosition;
|
||||
}
|
||||
|
||||
public void SetLabelPosition(Vector3 position)
|
||||
{
|
||||
if (m_LabelRect) m_LabelRect.localPosition = position;
|
||||
}
|
||||
|
||||
public void SetActive(bool flag)
|
||||
{
|
||||
ChartHelper.SetActive(gameObject, flag);
|
||||
}
|
||||
public void SetLabelActive(bool flag)
|
||||
public void SetTextActive(bool flag)
|
||||
{
|
||||
if (m_LabelText != null) m_LabelText.SetActive(flag);
|
||||
}
|
||||
@@ -170,7 +203,7 @@ namespace XCharts.Runtime
|
||||
|
||||
public bool SetText(string text)
|
||||
{
|
||||
if (m_LabelRect == null || m_LabelText == null)
|
||||
if (m_TextRect == null || m_LabelText == null)
|
||||
return false;
|
||||
|
||||
if (text == null)
|
||||
@@ -178,26 +211,25 @@ namespace XCharts.Runtime
|
||||
if (!m_LabelText.GetText().Equals(text))
|
||||
{
|
||||
m_LabelText.SetText(text);
|
||||
if (m_LabelAutoSize)
|
||||
if (m_AutoSize)
|
||||
{
|
||||
var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
|
||||
new Vector2(m_LabelText.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
|
||||
m_LabelText.GetPreferredHeight() + m_LabelPaddingTopBottom * 2);
|
||||
var sizeChange = newSize.x != m_LabelRect.sizeDelta.x || newSize.y != m_LabelRect.sizeDelta.y;
|
||||
new Vector2(m_LabelText.GetPreferredWidth(),
|
||||
m_LabelText.GetPreferredHeight());
|
||||
var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
|
||||
this.m_Width = newSize.x;
|
||||
this.m_Height = newSize.y;
|
||||
if (sizeChange)
|
||||
{
|
||||
m_LabelRect.sizeDelta = newSize;
|
||||
if (m_LabelBackgroundRect != null)
|
||||
m_LabelBackgroundRect.sizeDelta = newSize;
|
||||
if (!isIconActive && m_ObjectRect != null)
|
||||
m_ObjectRect.sizeDelta = newSize;
|
||||
m_TextRect.sizeDelta = newSize;
|
||||
UpdateSize();
|
||||
UpdatePadding();
|
||||
AdjustIconPos();
|
||||
|
||||
}
|
||||
return sizeChange;
|
||||
}
|
||||
AdjustIconPos();
|
||||
if (m_AutoHideIconWhenLabelEmpty && isIconActive)
|
||||
if (m_HideIconIfTextEmpty && isIconActive)
|
||||
{
|
||||
ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
|
||||
}
|
||||
@@ -205,9 +237,57 @@ namespace XCharts.Runtime
|
||||
return false;
|
||||
}
|
||||
|
||||
private void UpdateSize()
|
||||
{
|
||||
if (m_AutoSize)
|
||||
{
|
||||
var sizeDelta = m_TextRect.sizeDelta;
|
||||
m_Width = sizeDelta.x + m_PaddingLeft + m_PaddingRight;
|
||||
m_Height = sizeDelta.y + m_PaddingTop + m_PaddingBottom;
|
||||
objectRect.sizeDelta = new Vector2(m_Width, m_Height);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePadding()
|
||||
{
|
||||
if (m_TextRect == null) return;
|
||||
switch (text.alignment)
|
||||
{
|
||||
case TextAnchor.LowerLeft:
|
||||
m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_PaddingBottom);
|
||||
break;
|
||||
case TextAnchor.UpperLeft:
|
||||
m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, -m_PaddingTop);
|
||||
break;
|
||||
case TextAnchor.MiddleLeft:
|
||||
m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
|
||||
break;
|
||||
case TextAnchor.LowerRight:
|
||||
m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_PaddingBottom);
|
||||
break;
|
||||
case TextAnchor.UpperRight:
|
||||
m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, -m_PaddingTop);
|
||||
break;
|
||||
case TextAnchor.MiddleRight:
|
||||
m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
|
||||
break;
|
||||
case TextAnchor.LowerCenter:
|
||||
m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), m_PaddingBottom);
|
||||
break;
|
||||
case TextAnchor.UpperCenter:
|
||||
m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), -m_PaddingTop);
|
||||
break;
|
||||
case TextAnchor.MiddleCenter:
|
||||
m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void AdjustIconPos()
|
||||
{
|
||||
if (m_IconImage && m_IconRect && m_LabelText != null && m_ObjectRect != null)
|
||||
if (m_IconImage && m_IconRect && m_LabelText != null && m_TextRect != null)
|
||||
{
|
||||
var iconX = 0f;
|
||||
switch (m_Align)
|
||||
@@ -218,12 +298,12 @@ namespace XCharts.Runtime
|
||||
case TextAnchor.LowerLeft:
|
||||
case TextAnchor.UpperLeft:
|
||||
case TextAnchor.MiddleLeft:
|
||||
iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
|
||||
iconX = -m_TextRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
case TextAnchor.LowerRight:
|
||||
case TextAnchor.UpperRight:
|
||||
case TextAnchor.MiddleRight:
|
||||
iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
|
||||
iconX = m_TextRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
case TextAnchor.LowerCenter:
|
||||
case TextAnchor.UpperCenter:
|
||||
@@ -238,7 +318,7 @@ namespace XCharts.Runtime
|
||||
case TextAnchor.LowerLeft:
|
||||
case TextAnchor.UpperLeft:
|
||||
case TextAnchor.MiddleLeft:
|
||||
iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
|
||||
iconX = m_TextRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
case TextAnchor.LowerRight:
|
||||
case TextAnchor.UpperRight:
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace XCharts.Runtime
|
||||
[System.Serializable]
|
||||
public class ChartText
|
||||
{
|
||||
private float m_ExtraWidth;
|
||||
private Text m_Text;
|
||||
private TextAnchor m_TextAlignment;
|
||||
public Text text
|
||||
{
|
||||
get { return m_Text; }
|
||||
@@ -38,25 +38,7 @@ namespace XCharts.Runtime
|
||||
{
|
||||
get
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText == null) return TextAnchor.MiddleCenter;
|
||||
switch (m_TMPText.alignment)
|
||||
{
|
||||
case TextAlignmentOptions.Bottom: return TextAnchor.LowerCenter;
|
||||
case TextAlignmentOptions.BottomLeft: return TextAnchor.LowerLeft;
|
||||
case TextAlignmentOptions.BottomRight: return TextAnchor.LowerRight;
|
||||
case TextAlignmentOptions.Center: return TextAnchor.MiddleCenter;
|
||||
case TextAlignmentOptions.Left: return TextAnchor.MiddleLeft;
|
||||
case TextAlignmentOptions.Right: return TextAnchor.MiddleRight;
|
||||
case TextAlignmentOptions.Top: return TextAnchor.UpperCenter;
|
||||
case TextAlignmentOptions.TopLeft: return TextAnchor.UpperLeft;
|
||||
case TextAlignmentOptions.TopRight: return TextAnchor.UpperRight;
|
||||
default: return TextAnchor.MiddleCenter;
|
||||
}
|
||||
#else
|
||||
if (m_Text != null) return m_Text.alignment;
|
||||
else return TextAnchor.MiddleCenter;
|
||||
#endif
|
||||
return m_TextAlignment;
|
||||
}
|
||||
set
|
||||
{
|
||||
@@ -133,11 +115,6 @@ namespace XCharts.Runtime
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetExtraWidth(float width)
|
||||
{
|
||||
m_ExtraWidth = width;
|
||||
}
|
||||
|
||||
public void SetActive(bool flag)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
@@ -187,6 +164,7 @@ namespace XCharts.Runtime
|
||||
|
||||
public void SetAlignment(TextAnchor alignment)
|
||||
{
|
||||
m_TextAlignment = alignment;
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText == null) return;
|
||||
switch (alignment)
|
||||
@@ -200,6 +178,10 @@ namespace XCharts.Runtime
|
||||
case TextAnchor.UpperCenter: m_TMPText.alignment = TextAlignmentOptions.Top; break;
|
||||
case TextAnchor.UpperLeft: m_TMPText.alignment = TextAlignmentOptions.TopLeft; break;
|
||||
case TextAnchor.UpperRight: m_TMPText.alignment = TextAlignmentOptions.TopRight; break;
|
||||
default:
|
||||
m_TMPText.alignment = TextAlignmentOptions.Center;
|
||||
m_TextAlignment = TextAnchor.MiddleCenter;
|
||||
break;
|
||||
}
|
||||
#else
|
||||
if (m_Text != null) m_Text.alignment = alignment;
|
||||
@@ -262,9 +244,9 @@ namespace XCharts.Runtime
|
||||
public float GetPreferredWidth()
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return m_TMPText.preferredWidth + m_ExtraWidth;
|
||||
if (m_TMPText != null) return m_TMPText.preferredWidth;
|
||||
#else
|
||||
if (m_Text != null) return m_Text.preferredWidth + m_ExtraWidth;
|
||||
if (m_Text != null) return m_Text.preferredWidth;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -63,13 +63,13 @@ namespace XCharts.Runtime
|
||||
m_ReleaseDic.Clear();
|
||||
}
|
||||
|
||||
private static GameObject CreateSerieLabel(string name, Transform parent, LabelStyle label, Color color,
|
||||
private static GameObject CreateSerieLabel(string name, Transform parent, LabelStyle labelStyle, Color color,
|
||||
float iconWidth, float iconHeight, ThemeStyle theme)
|
||||
{
|
||||
var element = ChartHelper.AddSerieLabel(name, parent, label.backgroundWidth, label.backgroundHeight,
|
||||
color, label.textStyle, theme);
|
||||
ChartHelper.AddIcon("Icon", element.transform, iconWidth, iconHeight);
|
||||
return element;
|
||||
var label = ChartHelper.AddChartLabel(name, parent, labelStyle, theme.common,
|
||||
"", color, TextAnchor.MiddleCenter);
|
||||
label.SetActive(labelStyle.show);
|
||||
return label.gameObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,10 +232,11 @@ namespace XCharts.Runtime
|
||||
rect.pivot = pivot;
|
||||
}
|
||||
|
||||
public static ChartText AddTextObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
|
||||
Vector2 pivot, Vector2 sizeDelta, TextStyle textStyle, ComponentTheme theme, ChartText chartText = null)
|
||||
public static ChartText AddTextObject(string objectName, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
|
||||
Vector2 pivot, Vector2 sizeDelta, TextStyle textStyle, ComponentTheme theme, Color autoColor,
|
||||
TextAnchor autoAlignment, ChartText chartText = null)
|
||||
{
|
||||
GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
GameObject txtObj = AddObject(objectName, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
txtObj.transform.localEulerAngles = new Vector3(0, 0, textStyle.rotate);
|
||||
if (chartText == null)
|
||||
chartText = new ChartText();
|
||||
@@ -244,7 +245,6 @@ namespace XCharts.Runtime
|
||||
chartText.tmpText = GetOrAddComponent<TextMeshProUGUI>(txtObj);
|
||||
chartText.tmpText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont;
|
||||
chartText.tmpText.fontStyle = textStyle.tmpFontStyle;
|
||||
chartText.tmpText.alignment = textStyle.tmpAlignment;
|
||||
chartText.tmpText.richText = true;
|
||||
chartText.tmpText.raycastTarget = false;
|
||||
chartText.tmpText.enableWordWrapping = textStyle.autoWrap;
|
||||
@@ -252,17 +252,21 @@ namespace XCharts.Runtime
|
||||
chartText.text = GetOrAddComponent<Text>(txtObj);
|
||||
chartText.text.font = textStyle.font == null ? theme.font : textStyle.font;
|
||||
chartText.text.fontStyle = textStyle.fontStyle;
|
||||
chartText.text.alignment = textStyle.alignment;
|
||||
chartText.text.horizontalOverflow = textStyle.autoWrap ? HorizontalWrapMode.Wrap : HorizontalWrapMode.Overflow;
|
||||
chartText.text.verticalOverflow = VerticalWrapMode.Overflow;
|
||||
chartText.text.supportRichText = true;
|
||||
chartText.text.raycastTarget = false;
|
||||
#endif
|
||||
chartText.SetColor(textStyle.GetColor(theme.textColor));
|
||||
if (textStyle.autoColor && autoColor != Color.clear)
|
||||
chartText.SetColor(autoColor);
|
||||
else
|
||||
chartText.SetColor(textStyle.GetColor(theme.textColor));
|
||||
|
||||
chartText.SetAlignment(textStyle.autoAlign ? autoAlignment : textStyle.alignment);
|
||||
chartText.SetFontSize(textStyle.GetFontSize(theme));
|
||||
chartText.SetText("Text");
|
||||
chartText.SetLineSpacing(textStyle.lineSpacing);
|
||||
chartText.SetExtraWidth(textStyle.extraWidth);
|
||||
chartText.SetActive(textStyle.show);
|
||||
|
||||
RectTransform rect = GetOrAddComponent<RectTransform>(txtObj);
|
||||
rect.localPosition = Vector3.zero;
|
||||
@@ -273,93 +277,6 @@ namespace XCharts.Runtime
|
||||
return chartText;
|
||||
}
|
||||
|
||||
public static Text AddTextObject(string name, Transform parent, Font font, Color color,
|
||||
TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta,
|
||||
int fontSize = 14, float rotate = 0, FontStyle fontStyle = FontStyle.Normal, float lineSpacing = 1)
|
||||
{
|
||||
GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var txt = GetOrAddComponent<Text>(txtObj);
|
||||
txt.font = font;
|
||||
txt.fontSize = fontSize;
|
||||
txt.fontStyle = fontStyle;
|
||||
txt.text = "Text";
|
||||
txt.alignment = anchor;
|
||||
txt.horizontalOverflow = HorizontalWrapMode.Overflow;
|
||||
txt.verticalOverflow = VerticalWrapMode.Overflow;
|
||||
txt.color = color;
|
||||
txt.lineSpacing = lineSpacing;
|
||||
txt.raycastTarget = false;
|
||||
txtObj.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
|
||||
RectTransform rect = GetOrAddComponent<RectTransform>(txtObj);
|
||||
rect.localPosition = Vector3.zero;
|
||||
rect.sizeDelta = sizeDelta;
|
||||
rect.anchorMin = anchorMin;
|
||||
rect.anchorMax = anchorMax;
|
||||
rect.pivot = pivot;
|
||||
return txt;
|
||||
}
|
||||
|
||||
#if dUI_TextMeshPro
|
||||
public static TextMeshProUGUI AddTMPTextObject(string name, Transform parent, TMP_FontAsset font, Color color,
|
||||
TextAlignmentOptions anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta,
|
||||
int fontSize = 14, float rotate = 0, FontStyles fontStyle = FontStyles.Normal, float lineSpacing = 1)
|
||||
{
|
||||
GameObject txtObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var txt = GetOrAddComponent<TextMeshProUGUI>(txtObj);
|
||||
txt.font = font;
|
||||
txt.fontSize = fontSize;
|
||||
txt.fontStyle = fontStyle;
|
||||
txt.text = "Text";
|
||||
txt.alignment = anchor;
|
||||
txt.color = color;
|
||||
txt.lineSpacing = lineSpacing;
|
||||
txt.raycastTarget = false;
|
||||
txt.enableWordWrapping = false;
|
||||
txtObj.transform.localEulerAngles = new Vector3(0, 0, rotate);
|
||||
|
||||
RectTransform rect = GetOrAddComponent<RectTransform>(txtObj);
|
||||
rect.localPosition = Vector3.zero;
|
||||
rect.sizeDelta = sizeDelta;
|
||||
rect.anchorMin = anchorMin;
|
||||
rect.anchorMax = anchorMax;
|
||||
rect.pivot = pivot;
|
||||
return txt;
|
||||
}
|
||||
#endif
|
||||
|
||||
public static Button AddButtonObject(string name, Transform parent, Font font, int fontSize,
|
||||
Color color, TextAnchor anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot,
|
||||
Vector2 sizeDelta, float lineSpacing)
|
||||
{
|
||||
GameObject btnObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
GetOrAddComponent<Image>(btnObj);
|
||||
GetOrAddComponent<Button>(btnObj);
|
||||
Text txt = AddTextObject("Text", btnObj.transform, font, color, TextAnchor.MiddleCenter,
|
||||
new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f),
|
||||
sizeDelta, fontSize, lineSpacing);
|
||||
txt.rectTransform.offsetMin = Vector2.zero;
|
||||
txt.rectTransform.offsetMax = Vector2.zero;
|
||||
return btnObj.GetComponent<Button>();
|
||||
}
|
||||
|
||||
#if dUI_TextMeshPro
|
||||
public static Button AddTMPButtonObject(string name, Transform parent, TMP_FontAsset font, int fontSize,
|
||||
Color color, TextAlignmentOptions anchor, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot,
|
||||
Vector2 sizeDelta, float lineSpacing)
|
||||
{
|
||||
GameObject btnObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
GetOrAddComponent<Image>(btnObj);
|
||||
GetOrAddComponent<Button>(btnObj);
|
||||
var txt = AddTMPTextObject("Text", btnObj.transform, font, color, anchor,
|
||||
new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f),
|
||||
sizeDelta, fontSize, lineSpacing);
|
||||
txt.rectTransform.offsetMin = Vector2.zero;
|
||||
txt.rectTransform.offsetMax = Vector2.zero;
|
||||
return btnObj.GetComponent<Button>();
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static Painter AddPainterObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
|
||||
Vector2 pivot, Vector2 sizeDelta, HideFlags hideFlags, int siblingIndex)
|
||||
{
|
||||
@@ -369,6 +286,11 @@ namespace XCharts.Runtime
|
||||
return ChartHelper.GetOrAddComponent<Painter>(painterObj);
|
||||
}
|
||||
|
||||
public static Image AddIcon(string name, Transform parent, IconStyle iconStyle)
|
||||
{
|
||||
return AddIcon(name, parent, iconStyle.width, iconStyle.height, iconStyle.sprite, iconStyle.type);
|
||||
}
|
||||
|
||||
public static Image AddIcon(string name, Transform parent, float width, float height, Sprite sprite = null,
|
||||
Image.Type type = Image.Type.Simple)
|
||||
{
|
||||
@@ -394,105 +316,134 @@ namespace XCharts.Runtime
|
||||
}
|
||||
|
||||
public static ChartLabel AddAxisLabelObject(int total, int index, string name, Transform parent,
|
||||
Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme,
|
||||
string content)
|
||||
Vector2 sizeDelta, Axis axis, ComponentTheme theme,
|
||||
string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter)
|
||||
{
|
||||
var textStyle = axis.axisLabel.textStyle;
|
||||
var iconStyle = axis.iconStyle;
|
||||
var labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var label = GetOrAddComponent<ChartLabel>(labelObj);
|
||||
var label = AddChartLabel(name, parent, axis.axisLabel, theme, content, autoColor, autoAlignment);
|
||||
var labelShow = axis.axisLabel.show && (axis.axisLabel.interval == 0 || index % (axis.axisLabel.interval + 1) == 0);
|
||||
if (labelShow)
|
||||
{
|
||||
if (!axis.axisLabel.showStartLabel && index == 0) labelShow = false;
|
||||
else if (!axis.axisLabel.showEndLabel && index == total - 1) labelShow = false;
|
||||
}
|
||||
label.label = AddTextObject("Text", label.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme, label.label);
|
||||
label.icon = ChartHelper.AddIcon("Icon", label.transform, iconStyle.width, iconStyle.height);
|
||||
label.SetAutoSize(false);
|
||||
label.UpdateIcon(iconStyle, axis.GetIcon(index));
|
||||
label.label.SetActive(labelShow);
|
||||
label.SetText(content);
|
||||
label.color = textStyle.backgroundColor;
|
||||
label.UpdateIcon(axis.axisLabel.icon, axis.GetIcon(index));
|
||||
label.text.SetActive(labelShow);
|
||||
return label;
|
||||
}
|
||||
|
||||
public static ChartLabel AddDefaultChartLabel(string name, Transform parent,
|
||||
Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 sizeDelta, TextStyle textStyle, ComponentTheme theme,
|
||||
string content)
|
||||
public static ChartLabel AddChartLabel(string name, Transform parent, LabelStyle labelStyle,
|
||||
ComponentTheme theme, string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter)
|
||||
{
|
||||
Vector2 anchorMin, anchorMax, pivot;
|
||||
var sizeDelta = new Vector2(labelStyle.width, labelStyle.height);
|
||||
var textStyle = labelStyle.textStyle;
|
||||
var alignment = textStyle.GetAlignment(autoAlignment);
|
||||
UpdateAnchorAndPivotByTextAlignment(alignment, out anchorMin, out anchorMax, out pivot);
|
||||
var labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
// TODO: 为了兼容旧版本,这里后面版本可以去掉
|
||||
#region temp code
|
||||
var oldText = labelObj.GetComponent<Text>();
|
||||
if (oldText != null)
|
||||
{
|
||||
GameObject.DestroyImmediate(oldText);
|
||||
}
|
||||
var oldImage = labelObj.GetComponent<Image>();
|
||||
if (oldImage != null)
|
||||
{
|
||||
GameObject.DestroyImmediate(oldImage);
|
||||
}
|
||||
#endregion
|
||||
|
||||
var label = GetOrAddComponent<ChartLabel>(labelObj);
|
||||
label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme, label.label);
|
||||
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, 0, 0);
|
||||
label.SetAutoSize(true);
|
||||
label.label.SetActive(true);
|
||||
label.text = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot,
|
||||
sizeDelta, textStyle, theme, autoColor, autoAlignment, label.text);
|
||||
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, labelStyle.icon);
|
||||
label.SetSize(labelStyle.width, labelStyle.height);
|
||||
label.SetTextPadding(labelStyle.textPadding);
|
||||
label.SetText(content);
|
||||
label.color = textStyle.backgroundColor;
|
||||
label.UpdateIcon(labelStyle.icon);
|
||||
if (labelStyle.background.show)
|
||||
{
|
||||
label.color = (!labelStyle.background.autoColor || autoColor == Color.clear)
|
||||
? labelStyle.background.color : autoColor;
|
||||
label.sprite = labelStyle.background.sprite;
|
||||
label.type = labelStyle.background.type;
|
||||
}
|
||||
else
|
||||
{
|
||||
label.color = Color.clear;
|
||||
label.sprite = null;
|
||||
}
|
||||
label.transform.localEulerAngles = new Vector3(0, 0, labelStyle.rotate);
|
||||
label.transform.localPosition = labelStyle.offset;
|
||||
return label;
|
||||
}
|
||||
|
||||
internal static GameObject AddSerieLabel(string name, Transform parent, float width, float height,
|
||||
Color color, TextStyle textStyle, ThemeStyle theme)
|
||||
private static void UpdateAnchorAndPivotByTextAlignment(TextAnchor alignment, out Vector2 anchorMin, out Vector2 anchorMax,
|
||||
out Vector2 pivot)
|
||||
{
|
||||
Vector3 anchorMin, anchorMax, pivot;
|
||||
switch (textStyle.alignment)
|
||||
switch (alignment)
|
||||
{
|
||||
case TextAnchor.LowerLeft:
|
||||
anchorMin = new Vector2(0f, 0f);
|
||||
anchorMax = new Vector2(0f, 0f);
|
||||
pivot = new Vector2(0f, 0f);
|
||||
break;
|
||||
case TextAnchor.UpperLeft:
|
||||
anchorMin = new Vector2(0f, 1f);
|
||||
anchorMax = new Vector2(0f, 1f);
|
||||
pivot = new Vector2(0f, 1f);
|
||||
break;
|
||||
case TextAnchor.MiddleLeft:
|
||||
anchorMin = new Vector2(0f, 0.5f);
|
||||
anchorMax = new Vector2(0f, 0.5f);
|
||||
pivot = new Vector2(0f, 0.5f);
|
||||
break;
|
||||
case TextAnchor.LowerRight:
|
||||
anchorMin = new Vector2(1f, 0f);
|
||||
anchorMax = new Vector2(1f, 0f);
|
||||
pivot = new Vector2(1f, 0f);
|
||||
break;
|
||||
case TextAnchor.UpperRight:
|
||||
anchorMin = new Vector2(1f, 1f);
|
||||
anchorMax = new Vector2(1f, 1f);
|
||||
pivot = new Vector2(1f, 1f);
|
||||
break;
|
||||
case TextAnchor.MiddleRight:
|
||||
anchorMin = new Vector2(1, 0.5f);
|
||||
anchorMax = new Vector2(1, 0.5f);
|
||||
pivot = new Vector2(1, 0.5f);
|
||||
break;
|
||||
case TextAnchor.LowerCenter:
|
||||
anchorMin = new Vector2(0.5f, 0f);
|
||||
anchorMax = new Vector2(0.5f, 0f);
|
||||
pivot = new Vector2(0.5f, 0f);
|
||||
break;
|
||||
case TextAnchor.UpperCenter:
|
||||
anchorMin = new Vector2(0.5f, 1f);
|
||||
anchorMax = new Vector2(0.5f, 1f);
|
||||
pivot = new Vector2(0.5f, 1f);
|
||||
break;
|
||||
case TextAnchor.MiddleCenter:
|
||||
anchorMin = new Vector2(0.5f, 0.5f);
|
||||
anchorMax = new Vector2(0.5f, 0.5f);
|
||||
pivot = new Vector2(0.5f, 0.5f);
|
||||
break;
|
||||
default:
|
||||
anchorMin = new Vector2(0.5f, 0.5f);
|
||||
anchorMax = new Vector2(0.5f, 0.5f);
|
||||
pivot = new Vector2(0.5f, 0.5f);
|
||||
break;
|
||||
}
|
||||
var sizeDelta = (width != 0 && height != 0)
|
||||
? new Vector2(width, height)
|
||||
: new Vector2(50, textStyle.GetFontSize(theme.common) + 2);
|
||||
var labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var txt = AddTextObject("Text", labelObj.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle,
|
||||
theme.common);
|
||||
txt.SetColor(color);
|
||||
txt.SetAlignment(textStyle.alignment);
|
||||
txt.SetText("Text");
|
||||
txt.SetLocalPosition(new Vector2(0, 0));
|
||||
txt.SetLocalEulerAngles(Vector3.zero);
|
||||
labelObj.transform.localPosition = Vector3.zero;
|
||||
return labelObj;
|
||||
}
|
||||
|
||||
internal static ChartLabel AddTooltipLabel(Tooltip tooltip, string name, Transform parent, ThemeStyle theme, Vector2 pivot)
|
||||
internal static ChartLabel AddTooltipIndicatorLabel(Tooltip tooltip, string name, Transform parent,
|
||||
ThemeStyle theme, TextAnchor alignment)
|
||||
{
|
||||
var anchorMax = new Vector2(0.5f, 0.5f);
|
||||
var anchorMin = new Vector2(0.5f, 0.5f);
|
||||
var sizeDelta = new Vector2(100, 20);
|
||||
return AddTooltipLabel(tooltip, name, parent, theme, pivot, anchorMin, anchorMax, sizeDelta);
|
||||
}
|
||||
|
||||
internal static ChartLabel AddTooltipLabel(Tooltip tooltip, string name, Transform parent, ThemeStyle theme, Vector2 pivot,
|
||||
Vector2 anchorMin, Vector2 anchorMax, Vector2 sizeDelta)
|
||||
{
|
||||
var textStyle = tooltip.labelTextStyle;
|
||||
var labelGameObject = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
var label = GetOrAddComponent<ChartLabel>(labelGameObject);
|
||||
label.labelBackground = ChartHelper.AddIcon("Background", label.gameObject.transform, 50, 20);
|
||||
label.labelBackground.color = Color.black;
|
||||
label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme.tooltip, label.label);
|
||||
label.SetAutoSize(true);
|
||||
label.SetText("");
|
||||
label.color = textStyle.backgroundColor;
|
||||
var label = ChartHelper.AddChartLabel(name, parent, tooltip.indicatorLabelStyle, theme.tooltip,
|
||||
"", Color.clear, alignment);
|
||||
label.SetActive(tooltip.show && tooltip.indicatorLabelStyle.show);
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace XCharts.Runtime
|
||||
[ExecuteInEditMode]
|
||||
public static class XChartsMgr
|
||||
{
|
||||
public static readonly string version = "3.0.0-preview7";
|
||||
public static readonly int versionDate = 20220407;
|
||||
public static readonly string version = "3.0.0-preview8";
|
||||
public static readonly int versionDate = 20220426;
|
||||
public static string fullVersion { get { return version + "-" + versionDate; } }
|
||||
|
||||
internal static List<BaseChart> chartList = new List<BaseChart>();
|
||||
|
||||
Reference in New Issue
Block a user