From 49e7724f02578107dec80112ff34b4601863e28c Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Tue, 22 Oct 2024 19:11:17 +0100 Subject: [PATCH] add effects --- .../Resources/CustomBootstrapperSchema.json | 31 ++++++++++ .../Bootstrapper/CustomDialog.Creator.cs | 62 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/Bloxstrap/Resources/CustomBootstrapperSchema.json b/Bloxstrap/Resources/CustomBootstrapperSchema.json index cb323e2..b790d7c 100644 --- a/Bloxstrap/Resources/CustomBootstrapperSchema.json +++ b/Bloxstrap/Resources/CustomBootstrapperSchema.json @@ -212,6 +212,25 @@ "RadiusX": "double", "RadiusY": "double" } + }, + "BlurEffect": { + "IsCreatable": true, + "Attributes": { + "KernelType": "KernelType", + "Radius": "double", + "RenderingBias": "RenderingBias" + } + }, + "DropShadowEffect": { + "IsCreatable": true, + "Attributes": { + "BlurRadius": "double", + "Direction": "double", + "Opacity": "double", + "ShadowDepth": "double", + "RenderingBias": "RenderingBias", + "Color": "Color" + } } }, "Types": { @@ -395,6 +414,18 @@ "Bevel", "Round" ] + }, + "KernelType": { + "Values": [ + "Gaussian", + "Box" + ] + }, + "RenderingBias": { + "Values": [ + "Performance", + "Quality" + ] } } } \ No newline at end of file diff --git a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs index a812e44..2f8f7ab 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/CustomDialog.Creator.cs @@ -3,6 +3,7 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Imaging; +using System.Windows.Media.Effects; using System.Windows.Shapes; using System.Xml.Linq; @@ -48,6 +49,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper ["RotateTransform"] = HandleXml_RotateTransform, ["TranslateTransform"] = HandleXml_TranslateTransform, + ["BlurEffect"] = HandleXmlElement_BlurEffect, + ["DropShadowEffect"] = HandleXmlElement_DropShadowEffect, + ["Ellipse"] = HandleXmlElement_Ellipse, ["Line"] = HandleXmlElement_Line, ["Rectangle"] = HandleXmlElement_Rectangle @@ -300,6 +304,55 @@ namespace Bloxstrap.UI.Elements.Bootstrapper } #endregion + #region Effects + private static BlurEffect HandleXmlElement_BlurEffect(CustomDialog dialog, XElement xmlElement) + { + var effect = new BlurEffect(); + + effect.KernelType = ParseXmlAttribute(xmlElement, "KernelType", KernelType.Gaussian); + effect.Radius = ParseXmlAttribute(xmlElement, "Radius", 5); + effect.RenderingBias = ParseXmlAttribute(xmlElement, "RenderingBias", RenderingBias.Performance); + + return effect; + } + + private static DropShadowEffect HandleXmlElement_DropShadowEffect(CustomDialog dialog, XElement xmlElement) + { + var effect = new DropShadowEffect(); + + effect.BlurRadius = ParseXmlAttribute(xmlElement, "BlurRadius", 5); + effect.Direction = ParseXmlAttribute(xmlElement, "Direction", 315); + effect.Opacity = ParseXmlAttribute(xmlElement, "Opacity", 1); + effect.ShadowDepth = ParseXmlAttribute(xmlElement, "ShadowDepth", 5); + effect.RenderingBias = ParseXmlAttribute(xmlElement, "RenderingBias", RenderingBias.Performance); + + var color = GetColorFromXElement(xmlElement, "Color"); + if (color is Color) + effect.Color = (Color)color; + + return effect; + } + + + private static void ApplyEffects_UIElement(CustomDialog dialog, UIElement uiElement, XElement xmlElement) + { + var effectElement = xmlElement.Element($"{xmlElement.Name}.Effect"); + if (effectElement == null) + return; + + var children = effectElement.Elements(); + if (children.Count() > 1) + throw new Exception($"{xmlElement.Name}.Effect can only have one child"); + + var child = children.FirstOrDefault(); + if (child == null) + return; + + Effect effect = HandleXml(dialog, child); + uiElement.Effect = effect; + } + #endregion + #region Brushes private static void HandleXml_Brush(Brush brush, XElement xmlElement) { @@ -524,6 +577,8 @@ namespace Bloxstrap.UI.Elements.Bootstrapper int zIndex = ParseXmlAttributeClamped(xmlElement, "ZIndex", defaultValue: 0, min: 0, max: 1000); Panel.SetZIndex(uiElement, zIndex); + + ApplyEffects_UIElement(dialog, uiElement, xmlElement); } private static void HandleXmlElement_Control(CustomDialog dialog, Control uiElement, XElement xmlElement) @@ -551,6 +606,10 @@ namespace Bloxstrap.UI.Elements.Bootstrapper xmlElement.SetAttributeValue("IsEnabled", "True"); HandleXmlElement_Control(dialog, dialog, xmlElement); + // transfer effect to element grid + dialog.ElementGrid.Effect = dialog.Effect; + dialog.Effect = null; + var theme = ParseXmlAttribute(xmlElement, "Theme", Theme.Default); dialog.Resources.MergedDictionaries.Clear(); dialog.Resources.MergedDictionaries.Add(new ThemesDictionary() { Theme = theme.GetFinal() == Theme.Dark ? Wpf.Ui.Appearance.ThemeType.Dark : Wpf.Ui.Appearance.ThemeType.Light }); @@ -580,6 +639,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper xmlElement.SetAttributeValue("IsEnabled", "True"); HandleXmlElement_Control(dialog, dialog.RootTitleBar, xmlElement); + // get rid of all effects + dialog.Effect = null; + Panel.SetZIndex(dialog.RootTitleBar, 1001); // always show above others // properties we dont want modifiable