1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Support custom LED shape drawing in editor

This commit is contained in:
SpoinkyNL 2019-08-11 22:48:33 +02:00
parent 090c726880
commit 6ea6f57ee8
2 changed files with 33 additions and 28 deletions

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes;
using RGB.NET.Core; using RGB.NET.Core;
using SharpVectors.Converters; using SharpVectors.Converters;
using SharpVectors.Renderers.Wpf; using SharpVectors.Renderers.Wpf;
using Stylet; using Stylet;
using Color = System.Windows.Media.Color; using Color = System.Windows.Media.Color;
using Shape = RGB.NET.Core.Shape;
namespace Artemis.UI.ViewModels.Controls.RgbDevice namespace Artemis.UI.ViewModels.Controls.RgbDevice
{ {
@ -37,35 +39,33 @@ namespace Artemis.UI.ViewModels.Controls.RgbDevice
private void CreateLedGeometry() private void CreateLedGeometry()
{ {
// Prepare the SVG for this LED var relativeRectangle = new Rect(0, 0, Led.LedRectangle.Width, Led.LedRectangle.Height);
var converter = new FileSvgReader(new WpfDrawingSettings {OptimizePath = true}); Geometry geometry;
if (Led.Image != null) switch (Led.Shape)
{ {
DisplayDrawing = new DrawingImage(converter.Read(Led.Image)); case Shape.Custom:
geometry = Geometry.Parse(Led.ShapeData);
break;
case Shape.Rectangle:
geometry = new RectangleGeometry(relativeRectangle, 2, 2);
break;
case Shape.Circle:
geometry = new EllipseGeometry(relativeRectangle);
break;
default:
throw new ArgumentOutOfRangeException();
} }
else var drawing = new GeometryDrawing(null, new Pen(null, 2), geometry);
{
var relativeRectangle = new Rect(0, 0, Led.LedRectangle.Width, Led.LedRectangle.Height);
Geometry geometry;
switch (Led.Shape)
{
case Shape.Custom:
geometry = Geometry.Parse(Led.ShapeData);
break;
case Shape.Rectangle:
geometry = new RectangleGeometry(relativeRectangle, 2, 2);
break;
case Shape.Circle:
geometry = new EllipseGeometry(relativeRectangle);
break;
default:
throw new ArgumentOutOfRangeException();
}
var drawing = new GeometryDrawing(null, new Pen(null, 2), geometry); // The pen needs some adjustments when drawing custom shapes, a thickness of 2 just means you get a very thick pen that covers the
DisplayDrawing = new DrawingImage(drawing); // entire shape.. I'm not sure why to be honest sssh don't tell
if (Led.Shape == Shape.Custom)
{
drawing.Pen.Thickness = 0.075;
drawing.Pen.LineJoin = PenLineJoin.Round;
} }
DisplayDrawing = new DrawingImage(drawing);
NotifyOfPropertyChange(() => DisplayDrawing); NotifyOfPropertyChange(() => DisplayDrawing);
} }
@ -80,21 +80,25 @@ namespace Artemis.UI.ViewModels.Controls.RgbDevice
FillColor = newFillColor; FillColor = newFillColor;
changed = true; changed = true;
} }
if (Math.Abs(Led.LedRectangle.X - X) > 0.1) if (Math.Abs(Led.LedRectangle.X - X) > 0.1)
{ {
X = Led.LedRectangle.X; X = Led.LedRectangle.X;
changed = true; changed = true;
} }
if (Math.Abs(Led.LedRectangle.Y - Y) > 0.1) if (Math.Abs(Led.LedRectangle.Y - Y) > 0.1)
{ {
Y = Led.LedRectangle.Y; Y = Led.LedRectangle.Y;
changed = true; changed = true;
} }
if (Math.Abs(Led.LedRectangle.Width - Width) > 0.1) if (Math.Abs(Led.LedRectangle.Width - Width) > 0.1)
{ {
Width = Led.LedRectangle.Width; Width = Led.LedRectangle.Width;
changed = true; changed = true;
} }
if (Math.Abs(Led.LedRectangle.Height - Height) > 0.1) if (Math.Abs(Led.LedRectangle.Height - Height) > 0.1)
{ {
Height = Led.LedRectangle.Height; Height = Led.LedRectangle.Height;
@ -107,13 +111,12 @@ namespace Artemis.UI.ViewModels.Controls.RgbDevice
{ {
if (DisplayDrawing.Drawing is GeometryDrawing geometryDrawing) if (DisplayDrawing.Drawing is GeometryDrawing geometryDrawing)
{ {
geometryDrawing.Brush = new SolidColorBrush(FillColor) {Opacity = 0.6}; geometryDrawing.Brush = new SolidColorBrush(FillColor) {Opacity = 0.3};
geometryDrawing.Pen.Brush = new SolidColorBrush(FillColor); geometryDrawing.Pen.Brush = new SolidColorBrush(FillColor);
} }
else if (DisplayDrawing.Drawing is DrawingGroup drawingGroup)
drawingGroup.OpacityMask = new SolidColorBrush(FillColor); NotifyOfPropertyChange(() => DisplayDrawing);
}); });
NotifyOfPropertyChange(() => DisplayDrawing);
} }
} }
} }

View File

@ -9,6 +9,7 @@
d:DataContext="{d:DesignInstance rgbDevice:RgbDeviceViewModel}" d:DataContext="{d:DesignInstance rgbDevice:RgbDeviceViewModel}"
d:DesignHeight="450" d:DesignWidth="800"> d:DesignHeight="450" d:DesignWidth="800">
<Grid> <Grid>
<Image Source="{Binding Device.DeviceInfo.Image}"></Image>
<ItemsControl ItemsSource="{Binding Leds}"> <ItemsControl ItemsSource="{Binding Leds}">
<ItemsControl.ItemsPanel> <ItemsControl.ItemsPanel>
<ItemsPanelTemplate> <ItemsPanelTemplate>
@ -27,5 +28,6 @@
</DataTemplate> </DataTemplate>
</ItemsControl.ItemTemplate> </ItemsControl.ItemTemplate>
</ItemsControl> </ItemsControl>
</Grid> </Grid>
</UserControl> </UserControl>