增加IconStyle子组件,优化SerieData的图标配置

This commit is contained in:
monitor1394
2019-11-12 07:38:02 +08:00
parent 6d8e6992f1
commit 6e8d65cb34
13 changed files with 250 additions and 107 deletions

View File

@@ -0,0 +1,105 @@
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
using UnityEngine;
using UnityEngine.UI;
namespace XCharts
{
/// <summary>
/// 系列数据项的图标
/// </summary>
[System.Serializable]
public class IconStyle : SubComponent
{
public enum Layer
{
UnderLabel,
AboveLabel
}
[SerializeField] private bool m_Show;
[SerializeField] private Layer m_Layer;
[SerializeField] private Sprite m_Sprite;
[SerializeField] private Color m_Color = Color.white;
[SerializeField] private float m_Width = 40;
[SerializeField] private float m_Height = 40;
[SerializeField] private Vector3 m_Offset;
/// <summary>
/// Whether the data icon is show.
/// 是否显示图标。
/// </summary>
public bool show { get { return m_Show; } set { m_Show = value; UpdateIcon(); } }
/// <summary>
/// 显示在上层还是在下层。
/// </summary>
public Layer layer { get { return m_Layer; } set { m_Layer = value; } }
/// <summary>
/// The image of icon.
/// 图标的图片。
/// </summary>
public Sprite sprite { get { return m_Sprite; } set { m_Sprite = value; } }
/// <summary>
/// 图标颜色。
/// </summary>
public Color color { get { return m_Color; } set { m_Color = value; } }
/// <summary>
/// 图标宽。
/// </summary>
public float width { get { return m_Width; } set { m_Width = value; } }
/// <summary>
/// 图标高。
/// </summary>
public float height { get { return m_Height; } set { m_Height = value; } }
/// <summary>
/// 图标偏移。
/// </summary>
public Vector3 offset { get { return m_Offset; } set { m_Offset = value; } }
public Image image { get; private set; }
public RectTransform rect { get; private set; }
public void SetImage(Image image)
{
this.image = image;
if (image)
{
rect = image.GetComponent<RectTransform>();
if (m_Layer == Layer.UnderLabel)
rect.SetSiblingIndex(0);
else
rect.SetSiblingIndex(image.transform.childCount - 1);
UpdateIcon();
}
}
public void SetActive(bool flag)
{
if (image)
{
ChartHelper.SetActive(image.gameObject, flag);
}
}
public void UpdateIcon()
{
if (image == null) return;
if (show)
{
ChartHelper.SetActive(image.gameObject, true);
image.sprite = m_Sprite;
image.color = m_Color;
rect.sizeDelta = new Vector2(m_Width, m_Height);
image.transform.localPosition = m_Offset;
}
else
{
ChartHelper.SetActive(image.gameObject, false);
}
}
}
}

View File

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

View File

@@ -9,6 +9,7 @@ using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
namespace XCharts
{
@@ -22,13 +23,7 @@ namespace XCharts
[SerializeField] private string m_Name;
[SerializeField] private bool m_Selected;
[SerializeField] private float m_Radius;
[SerializeField] private bool m_ShowIcon;
[SerializeField] private Sprite m_IconImage;
[SerializeField] private Color m_IconColor = Color.white;
[SerializeField] private float m_IconWidth = 40;
[SerializeField] private float m_IconHeight = 40;
[SerializeField] private Vector3 m_IconOffset;
[SerializeField] private IconStyle m_IconStyle = new IconStyle();
[SerializeField] private List<float> m_Data = new List<float>();
private bool m_Show = true;
@@ -58,32 +53,10 @@ namespace XCharts
/// </summary>
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
/// <summary>
/// Whether the data icon is show.
/// 是否显示图标
/// the icon of data.
/// 数据项图标样式
/// </summary>
public bool showIcon { get { return m_ShowIcon; } set { m_ShowIcon = value; } }
/// <summary>
/// The image of icon.
/// 图标的图片。
/// </summary>
public Sprite iconImage { get { return m_IconImage; } set { m_IconImage = value; } }
/// <summary>
/// 图标颜色。
/// </summary>
public Color iconColor { get { return m_IconColor; } set { m_IconColor = value; } }
/// <summary>
/// 图标宽。
/// </summary>
public float iconWidth { get { return m_IconWidth; } set { m_IconWidth = value; } }
/// <summary>
/// 图标高。
/// </summary>
public float iconHeight { get { return m_IconHeight; } set { m_IconHeight = value; } }
/// <summary>
/// 图标偏移。
/// </summary>
public Vector3 iconOffset { get { return m_IconOffset; } set { m_IconOffset = value; } }
public IconStyle iconStyle { get { return m_IconStyle; } set { m_IconStyle = value; } }
/// <summary>
/// An arbitrary dimension data list of data item.
/// 可指定任意维数的数值列表。
@@ -124,8 +97,7 @@ namespace XCharts
/// 最小值。
/// </summary>
public float min { get { return m_Data.Min(); } }
public Image icon { get; private set; }
public RectTransform iconRect { get; private set; }
/// <summary>
/// 关联的gameObject
/// </summary>
@@ -238,28 +210,19 @@ namespace XCharts
if (labelRect) labelRect.localPosition = position;
}
public void SetIconObj(GameObject iconObj)
[Obsolete("Use SerieData.SetIconImage() instead.", true)]
public void SetIconObj(GameObject iconObj) { }
public void SetIconImage(Image image)
{
icon = iconObj.GetComponent<Image>();
iconRect = iconObj.GetComponent<RectTransform>();
UpdateIcon();
if (iconStyle == null) return;
iconStyle.SetImage(image);
}
public void UpdateIcon()
{
if (icon == null) return;
if (m_ShowIcon)
{
ChartHelper.SetActive(icon.gameObject, true);
icon.sprite = m_IconImage;
icon.color = m_IconColor;
iconRect.sizeDelta = new Vector2(m_IconWidth, m_IconHeight);
icon.transform.localPosition = m_IconOffset;
}
else
{
ChartHelper.SetActive(icon.gameObject, false);
}
if (iconStyle == null) return;
iconStyle.UpdateIcon();
}
}
}