mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-19 15:00:08 +00:00
3.0 - unitypackage
This commit is contained in:
257
Runtime/Internal/Object/ChartLabel.cs
Normal file
257
Runtime/Internal/Object/ChartLabel.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public class ChartLabel : Image
|
||||
{
|
||||
private bool m_AutoHideIconWhenLabelEmpty = false;
|
||||
private bool m_LabelAutoSize = true;
|
||||
private float m_LabelPaddingLeftRight = 3f;
|
||||
private float m_LabelPaddingTopBottom = 3f;
|
||||
|
||||
private ChartText m_LabelText;
|
||||
private RectTransform m_LabelRect;
|
||||
private RectTransform m_LabelBackgroundRect;
|
||||
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
|
||||
{
|
||||
get { return m_LabelText; }
|
||||
set
|
||||
{
|
||||
m_LabelText = value;
|
||||
if (value != null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
|
||||
public bool isIconActive { get; private set; }
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
m_ObjectRect = gameObject.GetComponent<RectTransform>();
|
||||
raycastTarget = false;
|
||||
}
|
||||
|
||||
public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public void SetLabelBackground(Image image)
|
||||
{
|
||||
m_LabelBackgroundImage = image;
|
||||
if (image != null)
|
||||
{
|
||||
m_LabelBackgroundRect = m_LabelBackgroundImage.GetComponent<RectTransform>();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIcon(Image image)
|
||||
{
|
||||
m_IconImage = image;
|
||||
if (image != null)
|
||||
{
|
||||
m_IconRect = m_IconImage.GetComponent<RectTransform>();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAutoSize(bool flag)
|
||||
{
|
||||
m_LabelAutoSize = flag;
|
||||
}
|
||||
|
||||
public void SetIconSprite(Sprite sprite)
|
||||
{
|
||||
if (m_IconImage != null) m_IconImage.sprite = sprite;
|
||||
}
|
||||
|
||||
public void SetIconSize(float width, float height)
|
||||
{
|
||||
if (m_IconRect != null) m_IconRect.sizeDelta = new Vector3(width, height);
|
||||
}
|
||||
|
||||
public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
|
||||
{
|
||||
if (m_IconImage == null || iconStyle == null)
|
||||
return;
|
||||
|
||||
SetIconActive(iconStyle.show);
|
||||
if (iconStyle.show)
|
||||
{
|
||||
m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
|
||||
m_IconImage.color = iconStyle.color;
|
||||
m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
|
||||
m_IconOffest = iconStyle.offset;
|
||||
m_Align = iconStyle.align;
|
||||
m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
|
||||
AdjustIconPos();
|
||||
if (iconStyle.layer == IconStyle.Layer.UnderLabel)
|
||||
m_IconRect.SetSiblingIndex(0);
|
||||
else
|
||||
m_IconRect.SetSiblingIndex(transform.childCount - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetLabelWidth()
|
||||
{
|
||||
if (m_LabelRect) return m_LabelRect.sizeDelta.x;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public float GetLabelHeight()
|
||||
{
|
||||
if (m_LabelRect) return m_LabelRect.sizeDelta.y;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void SetTextColor(Color color)
|
||||
{
|
||||
if (m_LabelText != null) m_LabelText.SetColor(color);
|
||||
}
|
||||
|
||||
public void SetLabelRotate(float rotate)
|
||||
{
|
||||
if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
|
||||
}
|
||||
|
||||
public void SetPosition(Vector3 position)
|
||||
{
|
||||
transform.localPosition = 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)
|
||||
{
|
||||
if (m_LabelText != null) m_LabelText.SetActive(flag);
|
||||
}
|
||||
public void SetIconActive(bool flag)
|
||||
{
|
||||
isIconActive = flag;
|
||||
if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
|
||||
}
|
||||
|
||||
public bool SetText(string text)
|
||||
{
|
||||
if (m_LabelRect == null || m_LabelText == null)
|
||||
return false;
|
||||
|
||||
if (text == null)
|
||||
text = "";
|
||||
if (!m_LabelText.GetText().Equals(text))
|
||||
{
|
||||
m_LabelText.SetText(text);
|
||||
if (m_LabelAutoSize)
|
||||
{
|
||||
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;
|
||||
if (sizeChange)
|
||||
{
|
||||
m_LabelRect.sizeDelta = newSize;
|
||||
if (m_LabelBackgroundRect != null)
|
||||
m_LabelBackgroundRect.sizeDelta = newSize;
|
||||
|
||||
AdjustIconPos();
|
||||
}
|
||||
return sizeChange;
|
||||
}
|
||||
AdjustIconPos();
|
||||
if (m_AutoHideIconWhenLabelEmpty && isIconActive)
|
||||
{
|
||||
ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void AdjustIconPos()
|
||||
{
|
||||
if (m_IconImage && m_IconRect && m_LabelText != null && m_ObjectRect != null)
|
||||
{
|
||||
var iconX = 0f;
|
||||
switch (m_Align)
|
||||
{
|
||||
case Align.Left:
|
||||
switch (m_LabelText.alignment)
|
||||
{
|
||||
case TextAnchor.LowerLeft:
|
||||
case TextAnchor.UpperLeft:
|
||||
case TextAnchor.MiddleLeft:
|
||||
iconX = -m_ObjectRect.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;
|
||||
break;
|
||||
case TextAnchor.LowerCenter:
|
||||
case TextAnchor.UpperCenter:
|
||||
case TextAnchor.MiddleCenter:
|
||||
iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Align.Right:
|
||||
switch (m_LabelText.alignment)
|
||||
{
|
||||
case TextAnchor.LowerLeft:
|
||||
case TextAnchor.UpperLeft:
|
||||
case TextAnchor.MiddleLeft:
|
||||
iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
case TextAnchor.LowerRight:
|
||||
case TextAnchor.UpperRight:
|
||||
case TextAnchor.MiddleRight:
|
||||
iconX = m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
case TextAnchor.LowerCenter:
|
||||
case TextAnchor.UpperCenter:
|
||||
case TextAnchor.MiddleCenter:
|
||||
iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Object/ChartLabel.cs.meta
Normal file
11
Runtime/Internal/Object/ChartLabel.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61287841bdc4142caba8e77985cd8715
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
15
Runtime/Internal/Object/ChartObject.cs
Normal file
15
Runtime/Internal/Object/ChartObject.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public class ChartObject
|
||||
{
|
||||
protected GameObject m_GameObject;
|
||||
|
||||
public virtual void Destroy()
|
||||
{
|
||||
GameObject.Destroy(m_GameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Object/ChartObject.cs.meta
Normal file
11
Runtime/Internal/Object/ChartObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fe3102b0eea042938d30af910ca86d6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
312
Runtime/Internal/Object/ChartText.cs
Normal file
312
Runtime/Internal/Object/ChartText.cs
Normal file
@@ -0,0 +1,312 @@
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
#if dUI_TextMeshPro
|
||||
using TMPro;
|
||||
#endif
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public class ChartText
|
||||
{
|
||||
private float m_ExtraWidth;
|
||||
private Text m_Text;
|
||||
public Text text
|
||||
{
|
||||
get { return m_Text; }
|
||||
set { m_Text = value; }
|
||||
}
|
||||
#if dUI_TextMeshPro
|
||||
private TextMeshProUGUI m_TMPText;
|
||||
public TextMeshProUGUI tmpText { get { return m_TMPText; } set { m_TMPText = value; } }
|
||||
#endif
|
||||
public GameObject gameObject
|
||||
{
|
||||
get
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return m_TMPText.gameObject;
|
||||
#else
|
||||
if (m_Text != null) return m_Text.gameObject;
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public TextAnchor alignment
|
||||
{
|
||||
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
|
||||
}
|
||||
set
|
||||
{
|
||||
SetAlignment(alignment);
|
||||
}
|
||||
}
|
||||
|
||||
public ChartText()
|
||||
{
|
||||
}
|
||||
|
||||
public ChartText(GameObject textParent)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
m_TMPText = textParent.GetComponentInChildren<TextMeshProUGUI>();
|
||||
if (m_TMPText == null)
|
||||
{
|
||||
Debug.LogError("can't find TextMeshProUGUI component:" + textParent);
|
||||
}
|
||||
#else
|
||||
m_Text = textParent.GetComponentInChildren<Text>();
|
||||
if (m_Text == null)
|
||||
{
|
||||
Debug.LogError("can't find Text component:" + textParent);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetFontSize(float fontSize)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.fontSize = fontSize;
|
||||
#else
|
||||
if (m_Text != null) m_Text.fontSize = (int)fontSize;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetText(string text)
|
||||
{
|
||||
if (text == null) text = string.Empty;
|
||||
else text = text.Replace("\\n", "\n");
|
||||
#if dUI_TextMeshPro
|
||||
if(m_TMPText != null) m_TMPText.text = text;
|
||||
#else
|
||||
if (m_Text != null) m_Text.text = text;
|
||||
#endif
|
||||
}
|
||||
|
||||
public string GetText()
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return m_TMPText.text;
|
||||
#else
|
||||
if (m_Text != null) return m_Text.text;
|
||||
#endif
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public void SetColor(Color color)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.color = color;
|
||||
#else
|
||||
if (m_Text != null) m_Text.color = color;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetLineSpacing(float lineSpacing)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.lineSpacing = lineSpacing;
|
||||
#else
|
||||
if (m_Text != null) m_Text.lineSpacing = lineSpacing;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetExtraWidth(float width)
|
||||
{
|
||||
m_ExtraWidth = width;
|
||||
}
|
||||
|
||||
public void SetActive(bool flag)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
//m_TMPText.gameObject.SetActive(flag);
|
||||
if (m_TMPText != null) ChartHelper.SetActive(m_TMPText.gameObject, flag);
|
||||
#else
|
||||
//m_Text.gameObject.SetActive(flag);
|
||||
if (m_Text != null) ChartHelper.SetActive(m_Text.gameObject, flag);
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetLocalPosition(Vector3 position)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.transform.localPosition = position;
|
||||
#else
|
||||
if (m_Text != null) m_Text.transform.localPosition = position;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetRectPosition(Vector3 position)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().anchoredPosition3D = position;
|
||||
#else
|
||||
if (m_Text != null) m_Text.GetComponent<RectTransform>().anchoredPosition3D = position;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetSizeDelta(Vector2 sizeDelta)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().sizeDelta = sizeDelta;
|
||||
#else
|
||||
if (m_Text != null) m_Text.GetComponent<RectTransform>().sizeDelta = sizeDelta;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetLocalEulerAngles(Vector3 position)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) m_TMPText.transform.localEulerAngles = position;
|
||||
#else
|
||||
if (m_Text != null) m_Text.transform.localEulerAngles = position;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetAlignment(TextAnchor alignment)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText == null) return;
|
||||
switch (alignment)
|
||||
{
|
||||
case TextAnchor.LowerCenter: m_TMPText.alignment = TextAlignmentOptions.Bottom; break;
|
||||
case TextAnchor.LowerLeft: m_TMPText.alignment = TextAlignmentOptions.BottomLeft; break;
|
||||
case TextAnchor.LowerRight: m_TMPText.alignment = TextAlignmentOptions.BottomRight; break;
|
||||
case TextAnchor.MiddleCenter: m_TMPText.alignment = TextAlignmentOptions.Center; break;
|
||||
case TextAnchor.MiddleLeft: m_TMPText.alignment = TextAlignmentOptions.Left; break;
|
||||
case TextAnchor.MiddleRight: m_TMPText.alignment = TextAlignmentOptions.Right; break;
|
||||
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;
|
||||
}
|
||||
#else
|
||||
if (m_Text != null) m_Text.alignment = alignment;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetFont(Font font)
|
||||
{
|
||||
if (m_Text) m_Text.font = font;
|
||||
}
|
||||
|
||||
public void SetFontStyle(FontStyle fontStyle)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText == null) return;
|
||||
switch (fontStyle)
|
||||
{
|
||||
case FontStyle.Normal: m_TMPText.fontStyle = FontStyles.Normal; break;
|
||||
case FontStyle.Bold: m_TMPText.fontStyle = FontStyles.Bold; break;
|
||||
case FontStyle.BoldAndItalic: m_TMPText.fontStyle = FontStyles.Bold | FontStyles.Italic; break;
|
||||
case FontStyle.Italic: m_TMPText.fontStyle = FontStyles.Italic; break;
|
||||
}
|
||||
#else
|
||||
if (m_Text != null) m_Text.fontStyle = fontStyle;
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetFontAndSizeAndStyle(TextStyle textStyle, ComponentTheme theme)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText == null) return;
|
||||
m_TMPText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont;
|
||||
m_TMPText.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
|
||||
m_TMPText.fontStyle = textStyle.tmpFontStyle;
|
||||
#else
|
||||
if (m_Text != null)
|
||||
{
|
||||
m_Text.font = textStyle.font == null ? theme.font : textStyle.font;
|
||||
m_Text.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
|
||||
m_Text.fontStyle = textStyle.fontStyle;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public float GetPreferredWidth(string content)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return 0; // TODO:
|
||||
#else
|
||||
if (m_Text != null)
|
||||
{
|
||||
var tg = m_Text.cachedTextGeneratorForLayout;
|
||||
var setting = m_Text.GetGenerationSettings(Vector2.zero);
|
||||
return tg.GetPreferredWidth(content, setting) / m_Text.pixelsPerUnit;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float GetPreferredWidth()
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return m_TMPText.preferredWidth + m_ExtraWidth;
|
||||
#else
|
||||
if (m_Text != null) return m_Text.preferredWidth + m_ExtraWidth;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
public float GetPreferredHeight()
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return m_TMPText.preferredHeight;
|
||||
#else
|
||||
if (m_Text != null) return m_Text.preferredHeight;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
public string GetPreferredText(string content, string suffix, float maxWidth)
|
||||
{
|
||||
#if dUI_TextMeshPro
|
||||
if (m_TMPText != null) return content; // TODO:
|
||||
#else
|
||||
if (m_Text != null)
|
||||
{
|
||||
var sourWid = GetPreferredWidth(content);
|
||||
if (sourWid < maxWidth) return content;
|
||||
var suffixWid = GetPreferredWidth(suffix);
|
||||
var textWid = maxWidth - 1.3f * suffixWid;
|
||||
for (int i = content.Length; i > 0; i--)
|
||||
{
|
||||
var temp = content.Substring(0, i);
|
||||
if (GetPreferredWidth(temp) < textWid)
|
||||
{
|
||||
return temp + suffix;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
#if dUI_TextMeshPro
|
||||
|
||||
public void SetFont(TMP_FontAsset font)
|
||||
{
|
||||
if (m_TMPText != null) m_TMPText.font = font;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Object/ChartText.cs.meta
Normal file
11
Runtime/Internal/Object/ChartText.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e2466c1fe5874bea8373b071405a930
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
214
Runtime/Internal/Object/LegendItem.cs
Normal file
214
Runtime/Internal/Object/LegendItem.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public class LegendItem
|
||||
{
|
||||
private int m_Index;
|
||||
private string m_Name;
|
||||
private string m_LegendName;
|
||||
private GameObject m_GameObject;
|
||||
private Button m_Button;
|
||||
private Image m_Icon;
|
||||
private ChartText m_Text;
|
||||
private Image m_TextBackground;
|
||||
private RectTransform m_Rect;
|
||||
private RectTransform m_IconRect;
|
||||
private RectTransform m_TextRect;
|
||||
private RectTransform m_TextBackgroundRect;
|
||||
private float m_Gap = 0f;
|
||||
private float m_LabelPaddingLeftRight = 0f;
|
||||
private float m_LabelPaddingTopBottom = 0f;
|
||||
private bool m_LabelAutoSize = true;
|
||||
|
||||
public int index { get { return m_Index; } set { m_Index = value; } }
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
public string legendName { get { return m_LegendName; } set { m_LegendName = value; } }
|
||||
public GameObject gameObject { get { return m_GameObject; } }
|
||||
public Button button { get { return m_Button; } }
|
||||
public float width
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_IconRect && m_TextBackgroundRect)
|
||||
{
|
||||
return m_IconRect.sizeDelta.x + m_Gap + m_TextBackgroundRect.sizeDelta.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float height
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_IconRect && m_TextBackgroundRect)
|
||||
{
|
||||
return Mathf.Max(m_IconRect.sizeDelta.y, m_TextBackgroundRect.sizeDelta.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetObject(GameObject obj)
|
||||
{
|
||||
m_GameObject = obj;
|
||||
m_Button = obj.GetComponent<Button>();
|
||||
m_Rect = obj.GetComponent<RectTransform>();
|
||||
m_Icon = obj.transform.Find("icon").gameObject.GetComponent<Image>();
|
||||
m_TextBackground = obj.transform.Find("content").gameObject.GetComponent<Image>();
|
||||
m_Text = new ChartText(obj);
|
||||
m_IconRect = m_Icon.gameObject.GetComponent<RectTransform>();
|
||||
m_TextRect = m_Text.gameObject.GetComponent<RectTransform>();
|
||||
m_TextBackgroundRect = m_TextBackground.gameObject.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
public void SetButton(Button button)
|
||||
{
|
||||
m_Button = button;
|
||||
}
|
||||
|
||||
public void SetIcon(Image icon)
|
||||
{
|
||||
m_Icon = icon;
|
||||
}
|
||||
|
||||
public void SetText(ChartText text)
|
||||
{
|
||||
m_Text = text;
|
||||
}
|
||||
|
||||
public void SetTextBackground(Image image)
|
||||
{
|
||||
m_TextBackground = image;
|
||||
}
|
||||
|
||||
public void SetIconSize(float width, float height)
|
||||
{
|
||||
if (m_IconRect)
|
||||
{
|
||||
m_IconRect.sizeDelta = new Vector2(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
public Rect GetIconRect()
|
||||
{
|
||||
if (m_GameObject && m_IconRect)
|
||||
{
|
||||
var pos = m_GameObject.transform.localPosition;
|
||||
var sizeDelta = m_IconRect.sizeDelta;
|
||||
var y = pos.y - (m_Rect.sizeDelta.y - sizeDelta.y) / 2 - sizeDelta.y;
|
||||
return new Rect(pos.x, y, m_IconRect.sizeDelta.x, m_IconRect.sizeDelta.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Rect.zero;
|
||||
}
|
||||
}
|
||||
|
||||
public Color GetIconColor()
|
||||
{
|
||||
if (m_Icon) return m_Icon.color;
|
||||
else return Color.clear;
|
||||
}
|
||||
|
||||
|
||||
public void SetIconColor(Color color)
|
||||
{
|
||||
if (m_Icon)
|
||||
{
|
||||
m_Icon.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIconImage(Sprite image)
|
||||
{
|
||||
if (m_Icon)
|
||||
{
|
||||
m_Icon.sprite = image;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIconActive(bool active)
|
||||
{
|
||||
if (m_Icon)
|
||||
{
|
||||
m_Icon.gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetContentColor(Color color)
|
||||
{
|
||||
if (m_Text != null)
|
||||
{
|
||||
m_Text.SetColor(color);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetContentBackgroundColor(Color color)
|
||||
{
|
||||
if (m_TextBackground)
|
||||
{
|
||||
m_TextBackground.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetContentPosition(Vector3 offset)
|
||||
{
|
||||
m_Gap = offset.x;
|
||||
if (m_TextBackgroundRect)
|
||||
{
|
||||
var posX = m_IconRect.sizeDelta.x + offset.x;
|
||||
m_TextBackgroundRect.anchoredPosition3D = new Vector3(posX, offset.y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SetContent(string content)
|
||||
{
|
||||
if (m_Text != null && !m_Text.GetText().Equals(content))
|
||||
{
|
||||
m_Text.SetText(content);
|
||||
if (m_LabelAutoSize)
|
||||
{
|
||||
var newSize = string.IsNullOrEmpty(content) ? Vector2.zero :
|
||||
new Vector2(m_Text.GetPreferredWidth(), m_Text.GetPreferredHeight());
|
||||
var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
|
||||
if (sizeChange)
|
||||
{
|
||||
m_TextRect.sizeDelta = newSize;
|
||||
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);
|
||||
}
|
||||
return sizeChange;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetPosition(Vector3 position)
|
||||
{
|
||||
if (m_GameObject)
|
||||
{
|
||||
m_GameObject.transform.localPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetActive(bool active)
|
||||
{
|
||||
if (m_GameObject)
|
||||
{
|
||||
m_GameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Object/LegendItem.cs.meta
Normal file
11
Runtime/Internal/Object/LegendItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e5abcb8f339f41f5b3680ecdab67509
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user