调整UI组件相关代码

This commit is contained in:
monitor1394
2024-01-16 22:29:15 +08:00
parent 6aa11249c5
commit daec83c4a5
12 changed files with 223 additions and 65 deletions

View File

@@ -14,7 +14,7 @@ namespace XCharts.Runtime
{
[SerializeField] private bool m_DebugModel = false;
[SerializeField] protected UIComponentTheme m_Theme = new UIComponentTheme();
[SerializeField] private ImageStyle m_Background = new ImageStyle() { show = false };
[SerializeField] private Background m_Background = new Background() { show = true };
protected bool m_DataDirty;
private ThemeType m_CheckTheme = 0;
@@ -24,7 +24,7 @@ namespace XCharts.Runtime
/// <summary>
/// 背景样式。
/// </summary>
public ImageStyle background { get { return m_Background; } set { m_Background = value; color = Color.white; } }
public Background background { get { return m_Background; } set { m_Background = value; color = Color.white; } }
/// <summary>
/// Update chart theme.
/// ||切换内置主题。

View File

@@ -50,5 +50,13 @@ namespace XCharts.Runtime
else return ColorUtil.clearColor32;
}
}
public Color32 GetBackgroundColor(Background background)
{
if (background != null && background.show && !background.autoColor)
return background.imageColor;
else
return backgroundColor;
}
}
}

View File

@@ -368,6 +368,28 @@ namespace XCharts.Runtime
}
}
public static void SetBackground(Image background, Background imageStyle)
{
if (background == null) return;
if (imageStyle.show)
{
background.gameObject.SetActive(true);
background.sprite = imageStyle.image;
background.color = imageStyle.imageColor;
background.type = imageStyle.imageType;
if (imageStyle.imageWidth > 0 && imageStyle.imageHeight > 0)
{
background.rectTransform.sizeDelta = new Vector2(imageStyle.imageWidth, imageStyle.imageHeight);
}
}
else
{
background.sprite = null;
background.color = Color.clear;
background.gameObject.SetActive(false);
}
}
public static ChartLabel AddAxisLabelObject(int total, int index, string name, Transform parent,
Vector2 sizeDelta, Axis axis, ComponentTheme theme,
string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter, Color32 iconDefaultColor = default(Color32))

View File

@@ -13,27 +13,30 @@ namespace XCharts.Runtime
{
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));
}
var background = component.background;
if (!background.show)
return;
if (background.image != null)
return;
var backgroundColor = component.theme.GetBackgroundColor(background);
var borderWidth = background.borderStyle.GetRuntimeBorderWidth();
var borderColor = background.borderStyle.GetRuntimeBorderColor();
var cornerRadius = background.borderStyle.GetRuntimeCornerRadius();
UGL.DrawRoundRectangleWithBorder(vh, component.graphRect, backgroundColor, backgroundColor, cornerRadius,
borderWidth, borderColor);
}
internal static void InitBackground(UIComponent table)
{
if (table.background.show == false ||
(table.background.sprite == null && ChartHelper.IsClearColor(table.background.color)))
(table.background.image == null && ChartHelper.IsClearColor(table.background.imageColor)))
{
ChartHelper.DestoryGameObject(table.transform, "Background");
return;
}
var sizeDelta = table.background.width > 0 && table.background.height > 0 ?
new Vector2(table.background.width, table.background.height) :
var sizeDelta = table.background.imageWidth > 0 && table.background.imageHeight > 0 ?
new Vector2(table.background.imageWidth, table.background.imageHeight) :
table.graphSizeDelta;
var backgroundObj = ChartHelper.AddObject("Background", table.transform, table.graphMinAnchor,
table.graphMaxAnchor, table.graphPivot, sizeDelta);
@@ -44,14 +47,7 @@ namespace XCharts.Runtime
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;
backgroundObj.SetActive(table.background.show && table.background.image != null);
}
}
}