mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-26 02:40:13 +00:00
增加UI组件支持
This commit is contained in:
92
Runtime/Internal/UIComponent.cs
Normal file
92
Runtime/Internal/UIComponent.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// UI组件基类。
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
[DisallowMultipleComponent]
|
||||
public class UIComponent : BaseGraph
|
||||
{
|
||||
[SerializeField] private bool m_DebugModel = false;
|
||||
[SerializeField] protected UIComponentTheme m_Theme = new UIComponentTheme();
|
||||
[SerializeField] private ImageStyle m_Background = new ImageStyle() { show = false };
|
||||
protected bool m_DataDirty;
|
||||
public override HideFlags chartHideFlags { get { return m_DebugModel ? HideFlags.None : HideFlags.HideInHierarchy; } }
|
||||
public UIComponentTheme theme { get { return m_Theme; } set { m_Theme = value; } }
|
||||
/// <summary>
|
||||
/// 背景样式。
|
||||
/// </summary>
|
||||
public ImageStyle background { get { return m_Background; } set { m_Background = value; color = Color.white; } }
|
||||
/// <summary>
|
||||
/// Update chart theme.
|
||||
/// |切换内置主题。
|
||||
/// </summary>
|
||||
/// <param name="theme">theme</param>
|
||||
public bool UpdateTheme(ThemeType theme)
|
||||
{
|
||||
if (theme == ThemeType.Custom)
|
||||
{
|
||||
Debug.LogError("UpdateTheme: not support switch to Custom theme.");
|
||||
return false;
|
||||
}
|
||||
if (m_Theme.sharedTheme == null)
|
||||
m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
|
||||
m_Theme.sharedTheme.CopyTheme(theme);
|
||||
m_Theme.SetAllDirty();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void InitComponent()
|
||||
{
|
||||
base.InitComponent();
|
||||
if (m_Theme.sharedTheme == null)
|
||||
m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
|
||||
UIHelper.InitBackground(this);
|
||||
}
|
||||
|
||||
protected override void CheckComponent()
|
||||
{
|
||||
base.CheckComponent();
|
||||
if (m_Theme.anyDirty)
|
||||
{
|
||||
if (m_Theme.componentDirty)
|
||||
{
|
||||
SetAllComponentDirty();
|
||||
}
|
||||
if (m_Theme.vertsDirty) RefreshGraph();
|
||||
m_Theme.ClearDirty();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void SetAllComponentDirty()
|
||||
{
|
||||
base.SetAllComponentDirty();
|
||||
InitComponent();
|
||||
}
|
||||
|
||||
protected override void OnDrawPainterBase(VertexHelper vh, Painter painter)
|
||||
{
|
||||
vh.Clear();
|
||||
UIHelper.DrawBackground(vh, this);
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
if (m_DataDirty)
|
||||
{
|
||||
m_DataDirty = false;
|
||||
DataDirty();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void DataDirty()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/UIComponent.cs.meta
Normal file
11
Runtime/Internal/UIComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb30814b19a9d4c1d800ae89e4537a8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
53
Runtime/Internal/UIComponentTheme.cs
Normal file
53
Runtime/Internal/UIComponentTheme.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace XCharts.Runtime
|
||||
{
|
||||
[Serializable]
|
||||
public class UIComponentTheme : ChildComponent
|
||||
{
|
||||
[SerializeField] private bool m_Show = true;
|
||||
[SerializeField] private Theme m_SharedTheme;
|
||||
[SerializeField] private bool m_TransparentBackground = false;
|
||||
|
||||
public bool show { get { return m_Show; } }
|
||||
/// <summary>
|
||||
/// the theme of chart.
|
||||
/// |主题类型。
|
||||
/// </summary>
|
||||
public ThemeType themeType
|
||||
{
|
||||
get { return sharedTheme.themeType; }
|
||||
}
|
||||
/// <summary>
|
||||
/// theme name.
|
||||
/// |主题名字。
|
||||
/// </summary>
|
||||
public string themeName
|
||||
{
|
||||
get { return sharedTheme.themeName; }
|
||||
}
|
||||
/// <summary>
|
||||
/// the asset of theme.
|
||||
/// |主题配置。
|
||||
/// </summary>
|
||||
public Theme sharedTheme
|
||||
{
|
||||
get { return m_SharedTheme; }
|
||||
set { m_SharedTheme = value; SetAllDirty(); }
|
||||
}
|
||||
/// <summary>
|
||||
/// the background color of chart.
|
||||
/// |背景颜色。
|
||||
/// </summary>
|
||||
public Color32 backgroundColor
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_TransparentBackground) return ColorUtil.clearColor32;
|
||||
else return sharedTheme.backgroundColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/UIComponentTheme.cs.meta
Normal file
11
Runtime/Internal/UIComponentTheme.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 259f5ef4039524e15a7f88578635e907
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
57
Runtime/Internal/Utilities/UIHelper.cs
Normal file
57
Runtime/Internal/Utilities/UIHelper.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XUGL;
|
||||
|
||||
namespace XCharts.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// UI帮助类。
|
||||
/// </summary>
|
||||
public static class UIHelper
|
||||
{
|
||||
internal static void DrawBackground(VertexHelper vh, UIComponent component)
|
||||
{
|
||||
if (component.background.show == false ||
|
||||
(component.background.sprite == null && ChartHelper.IsClearColor(component.background.color)))
|
||||
{
|
||||
var p1 = new Vector3(component.graphX, component.graphY);
|
||||
var p2 = new Vector3(component.graphX + component.graphWidth, component.graphY);
|
||||
var p3 = new Vector3(component.graphX + component.graphWidth, component.graphY + component.graphHeight);
|
||||
var p4 = new Vector3(component.graphX, component.graphY + component.graphHeight);
|
||||
UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, GetBackgroundColor(component));
|
||||
}
|
||||
}
|
||||
|
||||
internal static void InitBackground(UIComponent table)
|
||||
{
|
||||
if (table.background.show == false ||
|
||||
(table.background.sprite == null && ChartHelper.IsClearColor(table.background.color)))
|
||||
{
|
||||
ChartHelper.DestoryGameObject(table.transform, "Background");
|
||||
return;
|
||||
}
|
||||
var sizeDelta = table.background.width > 0 && table.background.height > 0 ?
|
||||
new Vector2(table.background.width, table.background.height) :
|
||||
table.graphSizeDelta;
|
||||
var backgroundObj = ChartHelper.AddObject("Background", table.transform, table.graphMinAnchor,
|
||||
table.graphMaxAnchor, table.graphPivot, sizeDelta);
|
||||
backgroundObj.hideFlags = table.chartHideFlags;
|
||||
|
||||
var backgroundImage = ChartHelper.EnsureComponent<Image>(backgroundObj);
|
||||
ChartHelper.UpdateRectTransform(backgroundObj, table.graphMinAnchor,
|
||||
table.graphMaxAnchor, table.graphPivot, sizeDelta);
|
||||
ChartHelper.SetBackground(backgroundImage, table.background);
|
||||
backgroundObj.transform.SetSiblingIndex(0);
|
||||
}
|
||||
|
||||
public static Color32 GetBackgroundColor(UIComponent component)
|
||||
{
|
||||
if (component.background.show && !ChartHelper.IsClearColor(component.background.color))
|
||||
return component.background.color;
|
||||
else
|
||||
return component.theme.backgroundColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Runtime/Internal/Utilities/UIHelper.cs.meta
Normal file
11
Runtime/Internal/Utilities/UIHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3be0399ecf6194793aa056e45ebfe20a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user