Files
XCharts/Runtime/Internal/Utilities/UIHelper.cs

76 lines
3.3 KiB
C#
Raw Normal View History

2023-03-20 21:56:56 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XUGL;
namespace XCharts.Runtime
{
/// <summary>
/// UI帮助类。
/// </summary>
public static class UIHelper
{
2024-12-25 10:06:53 +08:00
public static void DrawBackground(VertexHelper vh, UIComponent component)
2023-03-20 21:56:56 +08:00
{
2024-01-16 22:29:15 +08:00
var background = component.background;
2024-12-17 22:19:22 +08:00
var rect = component.graphRect;
if (background.imageWidth > 0 || background.imageHeight > 0)
{
if (background.imageWidth > 0)
{
rect.width = background.imageWidth;
rect.x = component.graphX + (component.graphWidth - background.imageWidth) / 2;
}
if (background.imageHeight > 0)
{
rect.height = background.imageHeight;
rect.y = component.graphY + (component.graphHeight - background.imageHeight) / 2;
}
}
background.rect = rect;
2024-01-16 22:29:15 +08:00
if (!background.show)
return;
if (background.image != null)
return;
var backgroundColor = component.theme.GetBackgroundColor(background);
2024-12-25 10:06:53 +08:00
DrawBackground(vh, background, backgroundColor);
}
2024-12-27 22:13:35 +08:00
public static void DrawBackground(VertexHelper vh, Background background, Color32 color, float smoothness = 2)
2024-12-25 10:06:53 +08:00
{
if (!background.show)
return;
if (background.image != null)
return;
2024-01-16 22:29:15 +08:00
var borderWidth = background.borderStyle.GetRuntimeBorderWidth();
var borderColor = background.borderStyle.GetRuntimeBorderColor();
var cornerRadius = background.borderStyle.GetRuntimeCornerRadius();
2024-12-25 10:06:53 +08:00
UGL.DrawRoundRectangleWithBorder(vh, background.rect, color, color, cornerRadius,
2024-12-27 22:13:35 +08:00
borderWidth, borderColor, 0, smoothness);
2023-03-20 21:56:56 +08:00
}
2024-12-04 08:19:40 +08:00
internal static void InitBackground(UIComponent component)
2023-03-20 21:56:56 +08:00
{
2024-12-04 08:19:40 +08:00
if (component.background.show == false ||
(component.background.image == null && ChartHelper.IsClearColor(component.background.imageColor)))
2023-03-20 21:56:56 +08:00
{
2024-12-04 08:19:40 +08:00
ChartHelper.DestoryGameObject(component.transform, "Background");
2023-03-20 21:56:56 +08:00
return;
}
2024-12-04 08:19:40 +08:00
var sizeDelta = component.background.imageWidth > 0 && component.background.imageHeight > 0 ?
new Vector2(component.background.imageWidth, component.background.imageHeight) :
component.graphSizeDelta;
var backgroundObj = ChartHelper.AddObject("Background", component.transform, component.graphMinAnchor,
component.graphMaxAnchor, component.graphPivot, sizeDelta);
backgroundObj.hideFlags = component.chartHideFlags;
2023-03-20 21:56:56 +08:00
var backgroundImage = ChartHelper.EnsureComponent<Image>(backgroundObj);
2024-12-04 08:19:40 +08:00
ChartHelper.UpdateRectTransform(backgroundObj, component.graphMinAnchor,
component.graphMaxAnchor, component.graphPivot, sizeDelta);
ChartHelper.SetBackground(backgroundImage, component.background);
2023-03-20 21:56:56 +08:00
backgroundObj.transform.SetSiblingIndex(0);
2024-12-04 08:19:40 +08:00
backgroundObj.SetActive(component.background.show && component.background.image != null);
2023-03-20 21:56:56 +08:00
}
}
}