From 3ecea91985b0c2e0cb97b01707e55c1dffff41ba Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Sun, 20 Oct 2024 20:47:52 +0100 Subject: [PATCH] add solidcolorbrush --- .../Resources/CustomBootstrapperSchema.json | 16 ++++++- .../Bootstrapper/CustomDialog.Creator.cs | 46 ++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Bloxstrap/Resources/CustomBootstrapperSchema.json b/Bloxstrap/Resources/CustomBootstrapperSchema.json index e8df14e..a2d9713 100644 --- a/Bloxstrap/Resources/CustomBootstrapperSchema.json +++ b/Bloxstrap/Resources/CustomBootstrapperSchema.json @@ -126,12 +126,25 @@ "Y": "double" } }, + "Brush": { + "IsCreatable": false, + "Attributes": { + "Opacity": "double" + } + }, + "SolidColorBrush": { + "SuperClass": "Brush", + "IsCreatable": true, + "Attributes": { + "Color": "Color" + } + }, "ImageBrush": { + "SuperClass": "Brush", "IsCreatable": true, "Attributes": { "AlignmentX": "AlignmentX", "AlignmentY": "AlignmentY", - "Opacity": "double", "Stretch": "Stretch", "TileMode": "TileMode", "ViewboxUnits": "BrushMappingMode", @@ -155,6 +168,7 @@ "Thickness": {}, "Rect": {}, "Brush": {}, + "Color": {}, "Content": {}, "ImageSource": {}, "Visibility": { diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs index 05def2b..b98e90b 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs @@ -25,6 +25,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper private static RectConverter? _rectConverter = null; public static RectConverter RectConverter { get => _rectConverter ??= new RectConverter(); } + private static ColorConverter? _colorConverter = null; + public static ColorConverter ColorConverter { get => _colorConverter ??= new ColorConverter(); } + private bool _initialised = false; // prevent users from creating elements with the same name multiple times @@ -49,6 +52,7 @@ namespace Bloxstrap.UI.Elements.Bootstrapper private static Dictionary _brushHandlerMap = new Dictionary() { + ["SolidColorBrush"] = HandleXmlBrush_SolidColorBrush, ["ImageBrush"] = HandleXmlBrush_ImageBrush }; @@ -207,6 +211,28 @@ namespace Bloxstrap.UI.Elements.Bootstrapper return rect; } + private static object? GetColorFromXElement(XElement xmlElement, string attributeName) + { + string? attributeValue = xmlElement.Attribute(attributeName)?.Value?.ToString(); + if (attributeValue == null) + return null; + + object? color; + try + { + color = ColorConverter.ConvertFromInvariantString(attributeValue); + } + catch (Exception ex) + { + throw new Exception($"{xmlElement.Name} has invalid {attributeName}: {ex.Message}", ex); + } + + if (color == null) + throw new Exception($"{xmlElement.Name} has invalid {attributeName}"); + + return color; + } + private static FontWeight GetFontWeightFromXElement(XElement element) { string? value = element.Attribute("FontWeight")?.Value?.ToString(); @@ -399,15 +425,31 @@ namespace Bloxstrap.UI.Elements.Bootstrapper #endregion #region Brushes + private static void HandleXmlBrush_Brush(Brush brush, XElement xmlElement) + { + brush.Opacity = ParseXmlAttribute(xmlElement, "Opacity", 1.0); + } + + private static Brush HandleXmlBrush_SolidColorBrush(CustomDialog dialog, XElement xmlElement) + { + var brush = new SolidColorBrush(); + HandleXmlBrush_Brush(brush, xmlElement); + + object? color = GetColorFromXElement(xmlElement, "Color"); + if (color is Color) + brush.Color = (Color)color; + + return brush; + } + private static Brush HandleXmlBrush_ImageBrush(CustomDialog dialog, XElement xmlElement) { var imageBrush = new ImageBrush(); + HandleXmlBrush_Brush(imageBrush, xmlElement); imageBrush.AlignmentX = ParseXmlAttribute(xmlElement, "AlignmentX", AlignmentX.Center); imageBrush.AlignmentY = ParseXmlAttribute(xmlElement, "AlignmentY", AlignmentY.Center); - imageBrush.Opacity = ParseXmlAttribute(xmlElement, "Opacity", 1.0); - imageBrush.Stretch = ParseXmlAttribute(xmlElement, "Stretch", Stretch.Fill); imageBrush.TileMode = ParseXmlAttribute(xmlElement, "TileMode", TileMode.None);