add solidcolorbrush

This commit is contained in:
bluepilledgreat 2024-10-20 20:47:52 +01:00
parent b566dc5452
commit 3ecea91985
2 changed files with 59 additions and 3 deletions

View File

@ -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": {

View File

@ -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<string, HandleXmlBrushElementDelegate> _brushHandlerMap = new Dictionary<string, HandleXmlBrushElementDelegate>()
{
["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<double>(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<AlignmentX>(xmlElement, "AlignmentX", AlignmentX.Center);
imageBrush.AlignmentY = ParseXmlAttribute<AlignmentY>(xmlElement, "AlignmentY", AlignmentY.Center);
imageBrush.Opacity = ParseXmlAttribute<double>(xmlElement, "Opacity", 1.0);
imageBrush.Stretch = ParseXmlAttribute<Stretch>(xmlElement, "Stretch", Stretch.Fill);
imageBrush.TileMode = ParseXmlAttribute<TileMode>(xmlElement, "TileMode", TileMode.None);