XCharts 2.0

This commit is contained in:
monitor1394
2021-01-11 08:54:28 +08:00
parent ed8d0687f7
commit 489095865d
304 changed files with 14799 additions and 12503 deletions

View File

@@ -1,21 +1,21 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
public class LabelObject : ChartObject
public class ChartLabel : ChartObject
{
private bool m_LabelAutoSize = true;
private float m_LabelPaddingLeftRight = 3f;
private float m_LabelPaddingTopBottom = 3f;
private Text m_LabelText;
private ChartText m_LabelText;
private RectTransform m_LabelRect;
private RectTransform m_IconRect;
private RectTransform m_ObjectRect;
@@ -24,9 +24,9 @@ namespace XCharts
public GameObject gameObject { get { return m_GameObject; } }
public Image icon { get { return m_IconImage; } }
public Text label { get { return m_LabelText; } }
public ChartText label { get { return m_LabelText; } }
public LabelObject()
public ChartLabel()
{
}
@@ -36,8 +36,8 @@ namespace XCharts
m_LabelAutoSize = autoSize;
m_LabelPaddingLeftRight = paddingLeftRight;
m_LabelPaddingTopBottom = paddingTopBottom;
m_LabelText = labelObj.GetComponentInChildren<Text>();
m_LabelRect = m_LabelText.GetComponent<RectTransform>();
m_LabelText = new ChartText(labelObj);
m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
m_ObjectRect = labelObj.GetComponent<RectTransform>();
}
@@ -95,12 +95,12 @@ namespace XCharts
public void SetLabelColor(Color color)
{
if (m_LabelText) m_LabelText.color = color;
if (m_LabelText != null) m_LabelText.SetColor(color);
}
public void SetLabelRotate(float rotate)
{
if (m_LabelText) m_LabelText.transform.localEulerAngles = new Vector3(0, 0, rotate);
if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
}
public void SetPosition(Vector3 position)
@@ -122,7 +122,7 @@ namespace XCharts
}
public void SetLabelActive(bool flag)
{
if (m_LabelText) ChartHelper.SetActive(m_LabelText, flag);
if (m_LabelText != null) m_LabelText.SetActive(flag);
}
public void SetIconActive(bool flag)
{
@@ -131,14 +131,15 @@ namespace XCharts
public bool SetText(string text)
{
if (m_LabelText && !m_LabelText.text.Equals(text))
if (m_LabelRect == null) return false;
if (m_LabelText != null && !m_LabelText.GetText().Equals(text))
{
m_LabelText.text = text;
m_LabelText.SetText(text);
if (m_LabelAutoSize)
{
var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
new Vector2(m_LabelText.preferredWidth + m_LabelPaddingLeftRight * 2,
m_LabelText.preferredHeight + m_LabelPaddingTopBottom * 2);
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)
{

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1277b7528331b42cfb61da7a2c762bee
guid: 61287841bdc4142caba8e77985cd8715
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,9 +1,9 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using System;
using UnityEngine;

View File

@@ -0,0 +1,255 @@
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using UnityEngine;
using UnityEngine.UI;
#if dUI_TextMeshPro
using TMPro;
#endif
namespace XCharts
{
public class ChartText
{
private Text m_Text;
private TextGenerationSettings m_RelatedTextSettings;
public Text text
{
get { return m_Text; }
set
{
m_Text = value;
if (value != null)
{
m_RelatedTextSettings = m_Text.GetGenerationSettings(Vector2.zero);
}
}
}
#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 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 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 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) return m_Text.cachedTextGenerator.GetPreferredWidth(content, m_RelatedTextSettings);
#endif
return 0;
}
public float GetPreferredWidth()
{
#if dUI_TextMeshPro
if (m_TMPText != null) return m_TMPText.preferredWidth;
#else
if (m_Text != null) return m_Text.preferredWidth;
#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;
}
#if dUI_TextMeshPro
public void SetAlignment(TextAlignmentOptions alignment)
{
if (m_TMPText != null) m_TMPText.alignment = alignment;
}
public void SetFont(TMP_FontAsset font)
{
if (m_TMPText != null) m_TMPText.font = font;
}
#endif
}
}

View File

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

View File

@@ -1,9 +1,9 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
using System;
using UnityEngine;
using UnityEngine.UI;
@@ -18,7 +18,7 @@ namespace XCharts
private GameObject m_GameObject;
private Button m_Button;
private Image m_Icon;
private Text m_Text;
private ChartText m_Text;
private Image m_TextBackground;
private RectTransform m_Rect;
private RectTransform m_IconRect;
@@ -71,7 +71,7 @@ namespace XCharts
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 = obj.transform.Find("content/Text").gameObject.GetComponent<Text>();
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>();
@@ -87,7 +87,7 @@ namespace XCharts
m_Icon = icon;
}
public void SetText(Text text)
public void SetText(ChartText text)
{
m_Text = text;
}
@@ -123,9 +123,9 @@ namespace XCharts
public void SetContentColor(Color color)
{
if (m_Text)
if (m_Text != null)
{
m_Text.color = color;
m_Text.SetColor(color);
}
}
@@ -149,20 +149,20 @@ namespace XCharts
public bool SetContent(string content)
{
if (m_Text && !m_Text.text.Equals(content))
if (m_Text != null && !m_Text.GetText().Equals(content))
{
m_Text.text = content;
m_Text.SetText(content);
if (m_LabelAutoSize)
{
var newSize = string.IsNullOrEmpty(content) ? Vector2.zero :
new Vector2(m_Text.preferredWidth, m_Text.preferredHeight);
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.preferredWidth + m_LabelPaddingLeftRight * 2,
m_Text.preferredHeight + m_LabelPaddingTopBottom * 2 - 4);
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;