1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Added first working version of a WPF-RGBSurface

(needs tons of work though, performance is bad and it doesn't look too good so far)
This commit is contained in:
Darth Affe 2017-01-29 20:14:57 +01:00
parent 2d42bd85fc
commit dcaebd2b3b
8 changed files with 349 additions and 0 deletions

View File

@ -0,0 +1,33 @@
using System.Windows;
using System.Windows.Controls;
using RGB.NET.Core;
namespace RGB.NET.WPF.Controls
{
/// <summary>
/// Visualizes a <see cref="Core.Led"/> in an wpf-application.
/// </summary>
public class LedVisualizer : Control
{
#region DependencyProperties
// ReSharper disable InconsistentNaming
/// <summary>
/// Backing-property for the <see cref="Led"/>-property.
/// </summary>
public static readonly DependencyProperty LedProperty = DependencyProperty.Register(
"Led", typeof(Led), typeof(LedVisualizer), new PropertyMetadata(default(Led)));
/// <summary>
/// Gets or sets the <see cref="Core.Led"/> to visualize.
/// </summary>
public Led Led
{
get { return (Led)GetValue(LedProperty); }
set { SetValue(LedProperty, value); }
}
// ReSharper restore InconsistentNaming
#endregion
}
}

View File

@ -0,0 +1,76 @@
using System.Windows;
using System.Windows.Controls;
using RGB.NET.Core;
namespace RGB.NET.WPF.Controls
{
/// <summary>
/// Visualizes a <see cref="IRGBDevice"/> in an wpf-application.
/// </summary>
[TemplatePart(Name = PART_CANVAS, Type = typeof(Canvas))]
public class RGBDeviceVisualizer : Control
{
#region Constants
private const string PART_CANVAS = "PART_Canvas";
#endregion
#region Properties & Fields
private Canvas _canvas;
#endregion
#region DependencyProperties
// ReSharper disable InconsistentNaming
/// <summary>
/// Backing-property for the <see cref="Device"/>-property.
/// </summary>
public static readonly DependencyProperty DeviceProperty = DependencyProperty.Register(
"Device", typeof(IRGBDevice), typeof(RGBDeviceVisualizer), new PropertyMetadata(default(IRGBDevice), DeviceChanged));
/// <summary>
/// Gets or sets the <see cref="IRGBDevice"/> to visualize.
/// </summary>
public IRGBDevice Device
{
get { return (IRGBDevice)GetValue(DeviceProperty); }
set { SetValue(DeviceProperty, value); }
}
// ReSharper restore InconsistentNaming
#endregion
#region Methods
/// <inheritdoc />
public override void OnApplyTemplate()
{
_canvas = (Canvas)GetTemplateChild(PART_CANVAS);
LayoutLeds();
}
private static void DeviceChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((RGBDeviceVisualizer)dependencyObject).LayoutLeds();
}
private void LayoutLeds()
{
if (_canvas == null) return;
_canvas.Children.Clear();
if (Device == null) return;
foreach (Led led in Device)
_canvas.Children.Add(new LedVisualizer { Led = led });
}
#endregion
}
}

View File

@ -0,0 +1,64 @@
using System.Windows;
using System.Windows.Controls;
using RGB.NET.Core;
namespace RGB.NET.WPF.Controls
{
/// <summary>
/// Visualizes the <see cref="RGBSurface"/> in an wpf-application.
/// </summary>
[TemplatePart(Name = PART_CANVAS, Type = typeof(Canvas))]
public class RGBSurfaceVisualizer : Control
{
#region Constants
private const string PART_CANVAS = "PART_Canvas";
#endregion
#region Properties & Fields
private Canvas _canvas;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RGBSurfaceVisualizer"/> class.
/// </summary>
public RGBSurfaceVisualizer()
{
RGBSurface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
foreach (IRGBDevice device in RGBSurface.Devices)
AddDevice(device);
}
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args)
{
if (args.DeviceAdded)
foreach (IRGBDevice device in args.Devices)
AddDevice(device);
_canvas.Width = RGBSurface.SurfaceRectangle.Size.Width;
_canvas.Height = RGBSurface.SurfaceRectangle.Size.Height;
}
#endregion
#region Methods
/// <inheritdoc />
public override void OnApplyTemplate()
{
_canvas = (Canvas)GetTemplateChild(PART_CANVAS);
}
private void AddDevice(IRGBDevice device)
{
_canvas.Children.Add(new RGBDeviceVisualizer { Device = device });
}
#endregion
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace RGB.NET.WPF.Converter
{
/// <summary>
/// Converts <see cref="Core.Color"/> into <see cref="SolidColorBrush"/>.
/// </summary>
[ValueConversion(typeof(Core.Color), typeof(SolidColorBrush))]
public class ColorToSolidColorBrushConverter : IValueConverter
{
/// <inheritdoc />
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Core.Color color = value as Core.Color;
return new SolidColorBrush(color == null
? Color.FromArgb(0, 0, 0, 0)
: Color.FromArgb(color.A, color.R, color.G, color.B));
}
/// <inheritdoc />
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush brush = value as SolidColorBrush;
return brush == null
? Core.Color.Transparent
: new Core.Color(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
}
}
}

View File

@ -32,25 +32,48 @@
<DocumentationFile>..\bin\RGB.NET.WPF.XML</DocumentationFile> <DocumentationFile>..\bin\RGB.NET.WPF.XML</DocumentationFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="RGB.NET.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="RGB.NET.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RGB.NET.Core.1.0.0\lib\net45\RGB.NET.Core.dll</HintPath> <HintPath>..\packages\RGB.NET.Core.1.0.0\lib\net45\RGB.NET.Core.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Controls\LedVisualizer.cs" />
<Compile Include="Controls\RGBDeviceVisualizer.cs" />
<Compile Include="Controls\RGBSurfaceVisualizer.cs" />
<Compile Include="Converter\ColorToSolidColorBrushConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<ItemGroup>
<Page Include="Styles\LedVisualizer.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Styles\RGBDeviceVisualizer.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Styles\RGBSurfaceVisualizer.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -0,0 +1,41 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:RGB.NET.WPF.Controls"
xmlns:converter="clr-namespace:RGB.NET.WPF.Converter">
<converter:ColorToSolidColorBrushConverter x:Key="ConverterColorToSolidColorBrush" />
<Style x:Key="StyleLedVisualizer"
TargetType="{x:Type controls:LedVisualizer}">
<Setter Property="Width" Value="{Binding Led.LedRectangle.Size.Width, RelativeSource={RelativeSource Self}}" />
<Setter Property="Height" Value="{Binding Led.LedRectangle.Size.Height, RelativeSource={RelativeSource Self}}" />
<Setter Property="Canvas.Left" Value="{Binding Led.LedRectangle.Location.X, RelativeSource={RelativeSource Self}}" />
<Setter Property="Canvas.Top" Value="{Binding Led.LedRectangle.Location.Y, RelativeSource={RelativeSource Self}}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="#202020" />
<Setter Property="Background" Value="{Binding Led.Color, RelativeSource={RelativeSource Self},
Converter={StaticResource ConverterColorToSolidColorBrush}}" />
<Setter Property="Opacity" Value="0.66" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:LedVisualizer}">
<Rectangle VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="{TemplateBinding BorderThickness}"
Fill="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="#FFFFFF" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type controls:LedVisualizer}" BasedOn="{StaticResource StyleLedVisualizer}" />
</ResourceDictionary>

View File

@ -0,0 +1,46 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:RGB.NET.WPF.Controls">
<Style x:Key="StyleRGBDeviceVisualizer"
TargetType="{x:Type controls:RGBDeviceVisualizer}">
<Setter Property="Width" Value="{Binding Device.Size.Width, RelativeSource={RelativeSource Self}}" />
<Setter Property="Height" Value="{Binding Device.Size.Height, RelativeSource={RelativeSource Self}}" />
<Setter Property="Canvas.Left" Value="{Binding Device.Location.X, RelativeSource={RelativeSource Self}}" />
<Setter Property="Canvas.Top" Value="{Binding Device.Location.Y, RelativeSource={RelativeSource Self}}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="#202020" />
<!--<Setter Property="Background" Value="#3A3A3A" />-->
<Setter Property="Background">
<Setter.Value>
<ImageBrush AlignmentX="Left" AlignmentY="Top"
Stretch="Uniform"
ImageSource="{Binding Device.DeviceInfo.Image, RelativeSource={RelativeSource AncestorType=controls:RGBDeviceVisualizer}}" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:RGBDeviceVisualizer}">
<Grid VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Background="{TemplateBinding Background}">
<Canvas x:Name="PART_Canvas"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" />
<Border VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{x:Null}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type controls:RGBDeviceVisualizer}" BasedOn="{StaticResource StyleRGBDeviceVisualizer}" />
</ResourceDictionary>

View File

@ -0,0 +1,34 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:RGB.NET.WPF.Controls">
<Style x:Key="StyleRGBSurfaceVisualizer" TargetType="{x:Type controls:RGBSurfaceVisualizer}">
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:RGBSurfaceVisualizer}">
<ScrollViewer VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
<Border VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<Canvas x:Name="PART_Canvas" Background="Transparent" />
</Border>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type controls:RGBSurfaceVisualizer}" BasedOn="{StaticResource StyleRGBSurfaceVisualizer}" />
</ResourceDictionary>