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

Surface editor - Refactor selection/movement code to match nodes

Device visualizer - Fixed exception when updating devices during render
This commit is contained in:
Robert 2022-04-19 23:11:44 +02:00
parent 52f2338154
commit e5ba48c7f4
13 changed files with 527 additions and 572 deletions

View File

@ -32,7 +32,7 @@ namespace Artemis.Core
/// <summary> /// <summary>
/// The full path to the Artemis data folder /// The full path to the Artemis data folder
/// </summary> /// </summary>
public static readonly string DataFolder = Path.Combine(BaseFolder, "Artemis.Avalonia"); public static readonly string DataFolder = Path.Combine(BaseFolder, "Artemis");
/// <summary> /// <summary>
/// The full path to the Artemis logs folder /// The full path to the Artemis logs folder

View File

@ -81,9 +81,12 @@ namespace Artemis.UI.Shared
if (!ShowColors) if (!ShowColors)
return; return;
lock (_deviceVisualizerLeds)
{
foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds) foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds)
deviceVisualizerLed.RenderGeometry(drawingContext, false); deviceVisualizerLed.RenderGeometry(drawingContext, false);
} }
}
finally finally
{ {
boundsPush?.Dispose(); boundsPush?.Dispose();
@ -244,10 +247,14 @@ namespace Artemis.UI.Shared
{ {
_deviceImage?.Dispose(); _deviceImage?.Dispose();
_deviceImage = null; _deviceImage = null;
_deviceVisualizerLeds.Clear();
_highlightedLeds = new List<DeviceVisualizerLed>(); _highlightedLeds = new List<DeviceVisualizerLed>();
_dimmedLeds = new List<DeviceVisualizerLed>(); _dimmedLeds = new List<DeviceVisualizerLed>();
lock (_deviceVisualizerLeds)
{
_deviceVisualizerLeds.Clear();
}
if (_oldDevice != null) if (_oldDevice != null)
{ {
_oldDevice.RgbDevice.PropertyChanged -= DevicePropertyChanged; _oldDevice.RgbDevice.PropertyChanged -= DevicePropertyChanged;
@ -264,8 +271,11 @@ namespace Artemis.UI.Shared
Device.DeviceUpdated += DeviceUpdated; Device.DeviceUpdated += DeviceUpdated;
// Create all the LEDs // Create all the LEDs
lock (_deviceVisualizerLeds)
{
foreach (ArtemisLed artemisLed in Device.Leds) foreach (ArtemisLed artemisLed in Device.Leds)
_deviceVisualizerLeds.Add(new DeviceVisualizerLed(artemisLed)); _deviceVisualizerLeds.Add(new DeviceVisualizerLed(artemisLed));
}
// Load the device main image on a background thread // Load the device main image on a background thread
ArtemisDevice? device = Device; ArtemisDevice? device = Device;
@ -282,8 +292,11 @@ namespace Artemis.UI.Shared
using IDrawingContextImpl context = renderTargetBitmap.CreateDrawingContext(new ImmediateRenderer(this)); using IDrawingContextImpl context = renderTargetBitmap.CreateDrawingContext(new ImmediateRenderer(this));
using Bitmap bitmap = new(device.Layout.Image.LocalPath); using Bitmap bitmap = new(device.Layout.Image.LocalPath);
context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality); context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality);
lock (_deviceVisualizerLeds)
{
foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds) foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds)
deviceVisualizerLed.DrawBitmap(context); deviceVisualizerLed.DrawBitmap(context);
}
_deviceImage = renderTargetBitmap; _deviceImage = renderTargetBitmap;

View File

@ -10,7 +10,6 @@ using Artemis.UI.Screens.ProfileEditor.ProfileTree;
using Artemis.UI.Screens.ProfileEditor.Properties; using Artemis.UI.Screens.ProfileEditor.Properties;
using Artemis.UI.Screens.ProfileEditor.Properties.DataBinding; using Artemis.UI.Screens.ProfileEditor.Properties.DataBinding;
using Artemis.UI.Screens.ProfileEditor.Properties.Timeline; using Artemis.UI.Screens.ProfileEditor.Properties.Timeline;
using Artemis.UI.Screens.ProfileEditor.Properties.Timeline.Segments;
using Artemis.UI.Screens.ProfileEditor.Properties.Tree; using Artemis.UI.Screens.ProfileEditor.Properties.Tree;
using Artemis.UI.Screens.ProfileEditor.VisualEditor.Visualizers; using Artemis.UI.Screens.ProfileEditor.VisualEditor.Visualizers;
using Artemis.UI.Screens.Settings; using Artemis.UI.Screens.Settings;
@ -18,18 +17,16 @@ using Artemis.UI.Screens.Sidebar;
using Artemis.UI.Screens.SurfaceEditor; using Artemis.UI.Screens.SurfaceEditor;
using Artemis.UI.Screens.VisualScripting; using Artemis.UI.Screens.VisualScripting;
using Artemis.UI.Screens.VisualScripting.Pins; using Artemis.UI.Screens.VisualScripting.Pins;
using Artemis.UI.Services;
using DynamicData.Binding;
using ReactiveUI; using ReactiveUI;
namespace Artemis.UI.Ninject.Factories namespace Artemis.UI.Ninject.Factories;
{
public interface IVmFactory
{
}
public interface IDeviceVmFactory : IVmFactory public interface IVmFactory
{ {
}
public interface IDeviceVmFactory : IVmFactory
{
DevicePropertiesViewModel DevicePropertiesViewModel(ArtemisDevice device); DevicePropertiesViewModel DevicePropertiesViewModel(ArtemisDevice device);
DeviceSettingsViewModel DeviceSettingsViewModel(ArtemisDevice device, DevicesTabViewModel devicesTabViewModel); DeviceSettingsViewModel DeviceSettingsViewModel(ArtemisDevice device, DevicesTabViewModel devicesTabViewModel);
DeviceDetectInputViewModel DeviceDetectInputViewModel(ArtemisDevice device); DeviceDetectInputViewModel DeviceDetectInputViewModel(ArtemisDevice device);
@ -37,44 +34,45 @@ namespace Artemis.UI.Ninject.Factories
DeviceInfoTabViewModel DeviceInfoTabViewModel(ArtemisDevice device); DeviceInfoTabViewModel DeviceInfoTabViewModel(ArtemisDevice device);
DeviceLedsTabViewModel DeviceLedsTabViewModel(ArtemisDevice device, ObservableCollection<ArtemisLed> selectedLeds); DeviceLedsTabViewModel DeviceLedsTabViewModel(ArtemisDevice device, ObservableCollection<ArtemisLed> selectedLeds);
InputMappingsTabViewModel InputMappingsTabViewModel(ArtemisDevice device, ObservableCollection<ArtemisLed> selectedLeds); InputMappingsTabViewModel InputMappingsTabViewModel(ArtemisDevice device, ObservableCollection<ArtemisLed> selectedLeds);
} }
public interface ISettingsVmFactory : IVmFactory public interface ISettingsVmFactory : IVmFactory
{ {
PluginSettingsViewModel CreatePluginSettingsViewModel(Plugin plugin); PluginSettingsViewModel CreatePluginSettingsViewModel(Plugin plugin);
PluginFeatureViewModel CreatePluginFeatureViewModel(PluginFeatureInfo pluginFeatureInfo, bool showShield); PluginFeatureViewModel CreatePluginFeatureViewModel(PluginFeatureInfo pluginFeatureInfo, bool showShield);
// DeviceSettingsViewModel CreateDeviceSettingsViewModel(ArtemisDevice device); // DeviceSettingsViewModel CreateDeviceSettingsViewModel(ArtemisDevice device);
} }
public interface ISidebarVmFactory : IVmFactory public interface ISidebarVmFactory : IVmFactory
{ {
SidebarViewModel? SidebarViewModel(IScreen hostScreen); SidebarViewModel? SidebarViewModel(IScreen hostScreen);
SidebarCategoryViewModel SidebarCategoryViewModel(SidebarViewModel sidebarViewModel, ProfileCategory profileCategory); SidebarCategoryViewModel SidebarCategoryViewModel(SidebarViewModel sidebarViewModel, ProfileCategory profileCategory);
SidebarProfileConfigurationViewModel SidebarProfileConfigurationViewModel(SidebarViewModel sidebarViewModel, ProfileConfiguration profileConfiguration); SidebarProfileConfigurationViewModel SidebarProfileConfigurationViewModel(SidebarViewModel sidebarViewModel, ProfileConfiguration profileConfiguration);
} }
public interface ISurfaceVmFactory : IVmFactory public interface ISurfaceVmFactory : IVmFactory
{ {
SurfaceDeviceViewModel SurfaceDeviceViewModel(ArtemisDevice device); SurfaceDeviceViewModel SurfaceDeviceViewModel(ArtemisDevice device, SurfaceEditorViewModel surfaceEditorViewModel);
ListDeviceViewModel ListDeviceViewModel(ArtemisDevice device); ListDeviceViewModel ListDeviceViewModel(ArtemisDevice device, SurfaceEditorViewModel surfaceEditorViewModel);
} }
public interface IPrerequisitesVmFactory : IVmFactory public interface IPrerequisitesVmFactory : IVmFactory
{ {
PluginPrerequisiteViewModel PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall); PluginPrerequisiteViewModel PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall);
} }
public interface IProfileEditorVmFactory : IVmFactory public interface IProfileEditorVmFactory : IVmFactory
{ {
ProfileEditorViewModel ProfileEditorViewModel(IScreen hostScreen); ProfileEditorViewModel ProfileEditorViewModel(IScreen hostScreen);
FolderTreeItemViewModel FolderTreeItemViewModel(TreeItemViewModel? parent, Folder folder); FolderTreeItemViewModel FolderTreeItemViewModel(TreeItemViewModel? parent, Folder folder);
LayerTreeItemViewModel LayerTreeItemViewModel(TreeItemViewModel? parent, Layer layer); LayerTreeItemViewModel LayerTreeItemViewModel(TreeItemViewModel? parent, Layer layer);
LayerShapeVisualizerViewModel LayerShapeVisualizerViewModel(Layer layer); LayerShapeVisualizerViewModel LayerShapeVisualizerViewModel(Layer layer);
LayerVisualizerViewModel LayerVisualizerViewModel(Layer layer); LayerVisualizerViewModel LayerVisualizerViewModel(Layer layer);
} }
public interface ILayerPropertyVmFactory : IVmFactory public interface ILayerPropertyVmFactory : IVmFactory
{ {
PropertyViewModel PropertyViewModel(ILayerProperty layerProperty); PropertyViewModel PropertyViewModel(ILayerProperty layerProperty);
PropertyGroupViewModel PropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup); PropertyGroupViewModel PropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup);
PropertyGroupViewModel PropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup, BaseLayerBrush layerBrush); PropertyGroupViewModel PropertyGroupViewModel(LayerPropertyGroup layerPropertyGroup, BaseLayerBrush layerBrush);
@ -84,21 +82,21 @@ namespace Artemis.UI.Ninject.Factories
TimelineViewModel TimelineViewModel(ObservableCollection<PropertyGroupViewModel> propertyGroupViewModels); TimelineViewModel TimelineViewModel(ObservableCollection<PropertyGroupViewModel> propertyGroupViewModels);
TimelineGroupViewModel TimelineGroupViewModel(PropertyGroupViewModel propertyGroupViewModel); TimelineGroupViewModel TimelineGroupViewModel(PropertyGroupViewModel propertyGroupViewModel);
} }
public interface IDataBindingVmFactory : IVmFactory public interface IDataBindingVmFactory : IVmFactory
{ {
DataBindingViewModel DataBindingViewModel(); DataBindingViewModel DataBindingViewModel();
} }
public interface IPropertyVmFactory public interface IPropertyVmFactory
{ {
ITreePropertyViewModel TreePropertyViewModel(ILayerProperty layerProperty, PropertyViewModel propertyViewModel); ITreePropertyViewModel TreePropertyViewModel(ILayerProperty layerProperty, PropertyViewModel propertyViewModel);
ITimelinePropertyViewModel TimelinePropertyViewModel(ILayerProperty layerProperty, PropertyViewModel propertyViewModel); ITimelinePropertyViewModel TimelinePropertyViewModel(ILayerProperty layerProperty, PropertyViewModel propertyViewModel);
} }
public interface INodeVmFactory : IVmFactory public interface INodeVmFactory : IVmFactory
{ {
NodeScriptViewModel NodeScriptViewModel(NodeScript nodeScript, bool isPreview); NodeScriptViewModel NodeScriptViewModel(NodeScript nodeScript, bool isPreview);
NodePickerViewModel NodePickerViewModel(NodeScript nodeScript); NodePickerViewModel NodePickerViewModel(NodeScript nodeScript);
NodeViewModel NodeViewModel(NodeScriptViewModel nodeScriptViewModel, INode node); NodeViewModel NodeViewModel(NodeScriptViewModel nodeScriptViewModel, INode node);
@ -108,13 +106,12 @@ namespace Artemis.UI.Ninject.Factories
OutputPinViewModel OutputPinViewModel(IPin outputPin); OutputPinViewModel OutputPinViewModel(IPin outputPin);
InputPinCollectionViewModel InputPinCollectionViewModel(IPinCollection inputPinCollection, NodeScriptViewModel nodeScriptViewModel); InputPinCollectionViewModel InputPinCollectionViewModel(IPinCollection inputPinCollection, NodeScriptViewModel nodeScriptViewModel);
OutputPinCollectionViewModel OutputPinCollectionViewModel(IPinCollection outputPinCollection, NodeScriptViewModel nodeScriptViewModel); OutputPinCollectionViewModel OutputPinCollectionViewModel(IPinCollection outputPinCollection, NodeScriptViewModel nodeScriptViewModel);
} }
public interface IConditionVmFactory : IVmFactory public interface IConditionVmFactory : IVmFactory
{ {
AlwaysOnConditionViewModel AlwaysOnConditionViewModel(AlwaysOnCondition alwaysOnCondition); AlwaysOnConditionViewModel AlwaysOnConditionViewModel(AlwaysOnCondition alwaysOnCondition);
PlayOnceConditionViewModel PlayOnceConditionViewModel(PlayOnceCondition playOnceCondition); PlayOnceConditionViewModel PlayOnceConditionViewModel(PlayOnceCondition playOnceCondition);
StaticConditionViewModel StaticConditionViewModel(StaticCondition staticCondition); StaticConditionViewModel StaticConditionViewModel(StaticCondition staticCondition);
EventConditionViewModel EventConditionViewModel(EventCondition eventCondition); EventConditionViewModel EventConditionViewModel(EventCondition eventCondition);
}
} }

View File

@ -1,10 +1,10 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class ListDeviceView : UserControl
{ {
public partial class ListDeviceView : UserControl
{
public ListDeviceView() public ListDeviceView()
{ {
InitializeComponent(); InitializeComponent();
@ -14,5 +14,4 @@ namespace Artemis.UI.Screens.SurfaceEditor
{ {
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
} }
}
} }

View File

@ -1,21 +1,22 @@
using Artemis.Core; using Artemis.Core;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using ReactiveUI;
using SkiaSharp; using SkiaSharp;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class ListDeviceViewModel : ViewModelBase
{ {
public class ListDeviceViewModel : ViewModelBase
{
private SKColor _color; private SKColor _color;
private bool _isSelected; private bool _isSelected;
public ListDeviceViewModel(ArtemisDevice device) public ListDeviceViewModel(ArtemisDevice device, SurfaceEditorViewModel surfaceEditorViewModel)
{ {
Device = device; Device = device;
SurfaceEditorViewModel = surfaceEditorViewModel;
} }
public ArtemisDevice Device { get; } public ArtemisDevice Device { get; }
public SurfaceEditorViewModel SurfaceEditorViewModel { get; }
public bool IsSelected public bool IsSelected
{ {
@ -28,5 +29,4 @@ namespace Artemis.UI.Screens.SurfaceEditor
get => _color; get => _color;
set => RaiseAndSetIfChanged(ref _color, value); set => RaiseAndSetIfChanged(ref _color, value);
} }
}
} }

View File

@ -3,9 +3,13 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared" xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared"
xmlns:surfaceEditor="clr-namespace:Artemis.UI.Screens.SurfaceEditor"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Screens.SurfaceEditor.SurfaceDeviceView"> x:Class="Artemis.UI.Screens.SurfaceEditor.SurfaceDeviceView"
<Grid> x:DataType="surfaceEditor:SurfaceDeviceViewModel">
<Grid PointerMoved="InputElement_OnPointerMoved"
PointerReleased="InputElement_OnPointerReleased"
Cursor="Hand">
<Grid.Styles> <Grid.Styles>
<Style Selector="Border.selection-border"> <Style Selector="Border.selection-border">
<Setter Property="Opacity" Value="0" /> <Setter Property="Opacity" Value="0" />
@ -15,21 +19,25 @@
</Transitions> </Transitions>
</Setter> </Setter>
</Style> </Style>
<Style Selector="Border.selection-border-selected"> <Style Selector="Border.deselected:pointerover">
<Setter Property="Opacity" Value="0.5" />
</Style>
<Style Selector="Border.selected">
<Setter Property="Opacity" Value="1" /> <Setter Property="Opacity" Value="1" />
</Style> </Style>
</Grid.Styles> </Grid.Styles>
<shared:DeviceVisualizer Device="{Binding Device}" ShowColors="True"/> <shared:DeviceVisualizer Device="{CompiledBinding Device}" ShowColors="True" />
<Border x:Name="SurfaceDeviceBorder" <Border x:Name="SurfaceDeviceBorder"
Classes="selection-border" Classes="selection-border"
Classes.selection-border-selected="{Binding Highlighted}" Classes.selected="{CompiledBinding IsSelected}"
Classes.deselected="{CompiledBinding !IsSelected}"
BorderThickness="1"> BorderThickness="1">
<Border.BorderBrush> <Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource SystemAccentColorLight2}"></SolidColorBrush> <SolidColorBrush Color="{DynamicResource SystemAccentColorLight2}" />
</Border.BorderBrush> </Border.BorderBrush>
<Border.Background> <Border.Background>
<SolidColorBrush Color="{DynamicResource SystemAccentColorLight2}" Opacity="0.2"></SolidColorBrush> <SolidColorBrush Color="{DynamicResource SystemAccentColorLight2}" Opacity="0.2" />
</Border.Background> </Border.Background>
</Border> </Border>
</Grid> </Grid>

View File

@ -1,43 +1,68 @@
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using Avalonia.VisualTree;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class SurfaceDeviceView : ReactiveUserControl<SurfaceDeviceViewModel>
{ {
public class SurfaceDeviceView : ReactiveUserControl<SurfaceDeviceViewModel> private bool _dragging;
{
public SurfaceDeviceView() public SurfaceDeviceView()
{ {
InitializeComponent(); InitializeComponent();
} }
/// <inheritdoc />
protected override void OnPointerEnter(PointerEventArgs e)
{
if (ViewModel?.SelectionStatus == SelectionStatus.None)
{
ViewModel.SelectionStatus = SelectionStatus.Hover;
Cursor = new Cursor(StandardCursorType.Hand);
}
base.OnPointerEnter(e);
}
/// <inheritdoc />
protected override void OnPointerLeave(PointerEventArgs e)
{
if (ViewModel?.SelectionStatus == SelectionStatus.Hover)
{
ViewModel.SelectionStatus = SelectionStatus.None;
Cursor = new Cursor(StandardCursorType.Arrow);
}
base.OnPointerLeave(e);
}
private void InitializeComponent() private void InitializeComponent()
{ {
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
} }
private void InputElement_OnPointerMoved(object? sender, PointerEventArgs e)
{
PointerPoint point = e.GetCurrentPoint(this.FindAncestorOfType<Canvas>());
if (ViewModel == null || !point.Properties.IsLeftButtonPressed)
return;
if (!_dragging)
{
_dragging = true;
if (!ViewModel.IsSelected)
{
ViewModel.SurfaceEditorViewModel.UpdateSelection(new List<SurfaceDeviceViewModel> {ViewModel}, false, false);
ViewModel.SurfaceEditorViewModel.FinishSelection();
}
ViewModel.SurfaceEditorViewModel.StartMouseDrag(point.Position);
e.Pointer.Capture((IInputElement?) sender);
}
ViewModel.SurfaceEditorViewModel.UpdateMouseDrag(point.Position, e.KeyModifiers.HasFlag(KeyModifiers.Shift), e.KeyModifiers.HasFlag(KeyModifiers.Alt));
e.Handled = true;
}
private void InputElement_OnPointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (ViewModel == null || e.InitialPressMouseButton != MouseButton.Left)
return;
if (_dragging)
{
_dragging = false;
ViewModel.SurfaceEditorViewModel.FinishSelection();
e.Pointer.Capture(null);
}
else
{
ViewModel.SurfaceEditorViewModel.UpdateSelection(new List<SurfaceDeviceViewModel> {ViewModel}, e.KeyModifiers.HasFlag(KeyModifiers.Shift), e.KeyModifiers.HasFlag(KeyModifiers.Control));
ViewModel.SurfaceEditorViewModel.FinishSelection();
}
e.Handled = true;
} }
} }

View File

@ -8,27 +8,26 @@ using Artemis.Core.Services;
using Artemis.UI.Ninject.Factories; using Artemis.UI.Ninject.Factories;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services;
using Avalonia.Input;
using ReactiveUI; using ReactiveUI;
using RGB.NET.Core; using RGB.NET.Core;
using SkiaSharp; using SkiaSharp;
using Point = Avalonia.Point; using Point = Avalonia.Point;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class SurfaceDeviceViewModel : ActivatableViewModelBase
{ {
public class SurfaceDeviceViewModel : ActivatableViewModelBase
{
private readonly IRgbService _rgbService;
private readonly IDeviceService _deviceService; private readonly IDeviceService _deviceService;
private readonly ISettingsService _settingsService;
private readonly IDeviceVmFactory _deviceVmFactory; private readonly IDeviceVmFactory _deviceVmFactory;
private readonly IRgbService _rgbService;
private readonly ISettingsService _settingsService;
private readonly IWindowService _windowService; private readonly IWindowService _windowService;
private Cursor _cursor;
private double _dragOffsetX; private double _dragOffsetX;
private double _dragOffsetY; private double _dragOffsetY;
private SelectionStatus _selectionStatus; private bool _isSelected;
public SurfaceDeviceViewModel(ArtemisDevice device, IRgbService rgbService, IDeviceService deviceService, ISettingsService settingsService, IDeviceVmFactory deviceVmFactory, public SurfaceDeviceViewModel(ArtemisDevice device, SurfaceEditorViewModel surfaceEditorViewModel, IRgbService rgbService, IDeviceService deviceService, ISettingsService settingsService,
IDeviceVmFactory deviceVmFactory,
IWindowService windowService) IWindowService windowService)
{ {
_rgbService = rgbService; _rgbService = rgbService;
@ -36,9 +35,9 @@ namespace Artemis.UI.Screens.SurfaceEditor
_settingsService = settingsService; _settingsService = settingsService;
_deviceVmFactory = deviceVmFactory; _deviceVmFactory = deviceVmFactory;
_windowService = windowService; _windowService = windowService;
_cursor = Cursor.Default;
Device = device; Device = device;
SurfaceEditorViewModel = surfaceEditorViewModel;
IdentifyDevice = ReactiveCommand.Create<ArtemisDevice>(ExecuteIdentifyDevice); IdentifyDevice = ReactiveCommand.Create<ArtemisDevice>(ExecuteIdentifyDevice);
ViewProperties = ReactiveCommand.CreateFromTask<ArtemisDevice>(ExecuteViewProperties); ViewProperties = ReactiveCommand.CreateFromTask<ArtemisDevice>(ExecuteViewProperties);
@ -48,30 +47,19 @@ namespace Artemis.UI.Screens.SurfaceEditor
public ReactiveCommand<ArtemisDevice, Unit> ViewProperties { get; } public ReactiveCommand<ArtemisDevice, Unit> ViewProperties { get; }
public ArtemisDevice Device { get; } public ArtemisDevice Device { get; }
public SurfaceEditorViewModel SurfaceEditorViewModel { get; }
public SelectionStatus SelectionStatus public bool IsSelected
{ {
get => _selectionStatus; get => _isSelected;
set set => RaiseAndSetIfChanged(ref _isSelected, value);
{
RaiseAndSetIfChanged(ref _selectionStatus, value);
this.RaisePropertyChanged(nameof(Highlighted));
} }
}
public bool Highlighted => SelectionStatus != SelectionStatus.None;
public bool CanDetectInput => Device.DeviceType == RGBDeviceType.Keyboard || Device.DeviceType == RGBDeviceType.Mouse; public bool CanDetectInput => Device.DeviceType == RGBDeviceType.Keyboard || Device.DeviceType == RGBDeviceType.Mouse;
public Cursor Cursor
{
get => _cursor;
set => RaiseAndSetIfChanged(ref _cursor, value);
}
public void StartMouseDrag(Point mouseStartPosition) public void StartMouseDrag(Point mouseStartPosition)
{ {
if (SelectionStatus != SelectionStatus.Selected) if (!IsSelected)
return; return;
_dragOffsetX = Device.X - mouseStartPosition.X; _dragOffsetX = Device.X - mouseStartPosition.X;
@ -80,7 +68,7 @@ namespace Artemis.UI.Screens.SurfaceEditor
public void UpdateMouseDrag(Point mousePosition, bool round, bool ignoreOverlap) public void UpdateMouseDrag(Point mousePosition, bool round, bool ignoreOverlap)
{ {
if (SelectionStatus != SelectionStatus.Selected) if (!IsSelected)
return; return;
float x = (float) (mousePosition.X + _dragOffsetX); float x = (float) (mousePosition.X + _dragOffsetX);
@ -139,12 +127,4 @@ namespace Artemis.UI.Screens.SurfaceEditor
return !own.Any(o => others.Any(l => l.IntersectsWith(o))); return !own.Any(o => others.Any(l => l.IntersectsWith(o)));
} }
}
public enum SelectionStatus
{
None,
Hover,
Selected
}
} }

View File

@ -28,16 +28,14 @@
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
Background="{StaticResource LargeCheckerboardBrush}" Background="{StaticResource LargeCheckerboardBrush}"
ZoomChanged="ZoomBorder_OnZoomChanged" ZoomChanged="ZoomBorder_OnZoomChanged"
PointerPressed="ZoomBorder_OnPointerPressed"
PointerMoved="ZoomBorder_OnPointerMoved"
PointerReleased="ZoomBorder_OnPointerReleased"> PointerReleased="ZoomBorder_OnPointerReleased">
<Grid Name="ContainerGrid" Background="Transparent"> <Grid Name="ContainerGrid" Background="Transparent">
<Grid.Transitions> <Grid.Transitions>
<Transitions> <Transitions>
<TransformOperationsTransition Property="RenderTransform" Duration="0:0:0.2" Easing="CubicEaseOut"/> <TransformOperationsTransition Property="RenderTransform" Duration="0:0:0.2" Easing="CubicEaseOut" />
</Transitions> </Transitions>
</Grid.Transitions> </Grid.Transitions>
<ItemsControl Items="{Binding SurfaceDeviceViewModels}" ClipToBounds="False"> <ItemsControl Name="DeviceContainer" Items="{Binding SurfaceDeviceViewModels}" ClipToBounds="False">
<ItemsControl.Styles> <ItemsControl.Styles>
<Style Selector="ContentPresenter"> <Style Selector="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Device.X}" /> <Setter Property="Canvas.Left" Value="{Binding Device.X}" />
@ -107,7 +105,7 @@
BorderBrush="{DynamicResource SystemAccentColor}" BorderBrush="{DynamicResource SystemAccentColor}"
BorderRadius="8"> BorderRadius="8">
<shared:SelectionRectangle.Background> <shared:SelectionRectangle.Background>
<SolidColorBrush Color="{DynamicResource SystemAccentColorLight1}" Opacity="0.2"></SolidColorBrush> <SolidColorBrush Color="{DynamicResource SystemAccentColorLight1}" Opacity="0.2" />
</shared:SelectionRectangle.Background> </shared:SelectionRectangle.Background>
</shared:SelectionRectangle> </shared:SelectionRectangle>

View File

@ -1,30 +1,31 @@
using System.Reactive; using System.Collections.Generic;
using System.Linq;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Events; using Artemis.UI.Shared.Events;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Generators;
using Avalonia.Controls.PanAndZoom; using Avalonia.Controls.PanAndZoom;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class SurfaceEditorView : ReactiveUserControl<SurfaceEditorViewModel>
{ {
public class SurfaceEditorView : ReactiveUserControl<SurfaceEditorViewModel> private readonly ItemsControl _deviceContainer;
{
private readonly Grid _containerGrid;
private readonly SelectionRectangle _selectionRectangle; private readonly SelectionRectangle _selectionRectangle;
private readonly Border _surfaceBounds; private readonly Border _surfaceBounds;
private readonly ZoomBorder _zoomBorder; private readonly ZoomBorder _zoomBorder;
private bool _dragging;
public SurfaceEditorView() public SurfaceEditorView()
{ {
InitializeComponent(); InitializeComponent();
_zoomBorder = this.Find<ZoomBorder>("ZoomBorder"); _zoomBorder = this.Find<ZoomBorder>("ZoomBorder");
_containerGrid = this.Find<Grid>("ContainerGrid"); _deviceContainer = this.Find<ItemsControl>("DeviceContainer");
_selectionRectangle = this.Find<SelectionRectangle>("SelectionRectangle"); _selectionRectangle = this.Find<SelectionRectangle>("SelectionRectangle");
_surfaceBounds = this.Find<Border>("SurfaceBounds"); _surfaceBounds = this.Find<Border>("SurfaceBounds");
@ -50,57 +51,17 @@ namespace Artemis.UI.Screens.SurfaceEditor
_surfaceBounds.BorderThickness = new Thickness(2 / _zoomBorder.ZoomX); _surfaceBounds.BorderThickness = new Thickness(2 / _zoomBorder.ZoomX);
} }
private void ZoomBorder_OnPointerPressed(object? sender, PointerPressedEventArgs e) private void SelectionRectangle_OnSelectionUpdated(object? sender, SelectionRectangleEventArgs e)
{ {
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) List<ItemContainerInfo> itemContainerInfos = _deviceContainer.ItemContainerGenerator.Containers.Where(c => c.ContainerControl.Bounds.Intersects(e.Rectangle)).ToList();
return; List<SurfaceDeviceViewModel> viewModels = itemContainerInfos.Where(c => c.Item is SurfaceDeviceViewModel).Select(c => (SurfaceDeviceViewModel) c.Item).ToList();
ViewModel?.UpdateSelection(viewModels, e.KeyModifiers.HasFlag(KeyModifiers.Shift), e.KeyModifiers.HasFlag(KeyModifiers.Control));
_dragging = false;
if (e.Source is Border {Name: "SurfaceDeviceBorder"})
{
e.Pointer.Capture(_zoomBorder);
e.Handled = true;
}
}
private void ZoomBorder_OnPointerMoved(object? sender, PointerEventArgs e)
{
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
return;
if (ReferenceEquals(e.Pointer.Captured, sender))
{
if (!_dragging)
ViewModel?.StartMouseDrag(e.GetPosition(_containerGrid));
ViewModel?.UpdateMouseDrag(e.GetPosition(_containerGrid), e.KeyModifiers.HasFlag(KeyModifiers.Shift), e.KeyModifiers.HasFlag(KeyModifiers.Alt));
}
_dragging = true;
} }
private void ZoomBorder_OnPointerReleased(object? sender, PointerReleasedEventArgs e) private void ZoomBorder_OnPointerReleased(object? sender, PointerReleasedEventArgs e)
{ {
if (e.InitialPressMouseButton != MouseButton.Left) if (!_selectionRectangle.IsSelecting && e.InitialPressMouseButton == MouseButton.Left)
return; ViewModel?.ClearSelection();
// If the mouse didn't move, apply selection
if (!_dragging)
{
ViewModel?.SelectFirstDeviceAtPoint(e.GetPosition(_containerGrid), e.KeyModifiers.HasFlag(KeyModifiers.Shift));
return;
}
if (ReferenceEquals(e.Pointer.Captured, sender))
{
ViewModel?.StopMouseDrag(e.GetPosition(_containerGrid), e.KeyModifiers.HasFlag(KeyModifiers.Shift), e.KeyModifiers.HasFlag(KeyModifiers.Alt));
e.Pointer.Capture(null);
}
}
private void SelectionRectangle_OnSelectionUpdated(object? sender, SelectionRectangleEventArgs e)
{
ViewModel?.UpdateSelection(e.Rectangle, e.KeyModifiers.HasFlag(KeyModifiers.Shift));
} }
private void UpdateZoomBorderBackground() private void UpdateZoomBorderBackground()
@ -108,5 +69,4 @@ namespace Artemis.UI.Screens.SurfaceEditor
if (_zoomBorder.Background is VisualBrush visualBrush) if (_zoomBorder.Background is VisualBrush visualBrush)
visualBrush.DestinationRect = new RelativeRect(_zoomBorder.OffsetX * -1, _zoomBorder.OffsetY * -1, 20, 20, RelativeUnit.Absolute); visualBrush.DestinationRect = new RelativeRect(_zoomBorder.OffsetX * -1, _zoomBorder.OffsetY * -1, 20, 20, RelativeUnit.Absolute);
} }
}
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
@ -8,16 +9,15 @@ using Artemis.Core.Services;
using Artemis.UI.Extensions; using Artemis.UI.Extensions;
using Artemis.UI.Ninject.Factories; using Artemis.UI.Ninject.Factories;
using Avalonia; using Avalonia;
using Avalonia.Skia;
using ReactiveUI; using ReactiveUI;
using SkiaSharp;
namespace Artemis.UI.Screens.SurfaceEditor namespace Artemis.UI.Screens.SurfaceEditor;
public class SurfaceEditorViewModel : MainScreenViewModel
{ {
public class SurfaceEditorViewModel : MainScreenViewModel
{
private readonly IRgbService _rgbService; private readonly IRgbService _rgbService;
private readonly ISettingsService _settingsService; private readonly ISettingsService _settingsService;
private List<SurfaceDeviceViewModel>? _initialSelection;
private bool _saving; private bool _saving;
public SurfaceEditorViewModel(IScreen hostScreen, public SurfaceEditorViewModel(IScreen hostScreen,
@ -28,8 +28,8 @@ namespace Artemis.UI.Screens.SurfaceEditor
_rgbService = rgbService; _rgbService = rgbService;
_settingsService = settingsService; _settingsService = settingsService;
DisplayName = "Surface Editor"; DisplayName = "Surface Editor";
SurfaceDeviceViewModels = new ObservableCollection<SurfaceDeviceViewModel>(rgbService.Devices.OrderBy(d => d.ZIndex).Select(surfaceVmFactory.SurfaceDeviceViewModel)); SurfaceDeviceViewModels = new ObservableCollection<SurfaceDeviceViewModel>(rgbService.EnabledDevices.OrderBy(d => d.ZIndex).Select(d => surfaceVmFactory.SurfaceDeviceViewModel(d, this)));
ListDeviceViewModels = new ObservableCollection<ListDeviceViewModel>(rgbService.Devices.OrderBy(d => d.ZIndex).Select(surfaceVmFactory.ListDeviceViewModel)); ListDeviceViewModels = new ObservableCollection<ListDeviceViewModel>(rgbService.EnabledDevices.OrderBy(d => d.ZIndex).Select(d => surfaceVmFactory.ListDeviceViewModel(d, this)));
BringToFront = ReactiveCommand.Create<ArtemisDevice>(ExecuteBringToFront); BringToFront = ReactiveCommand.Create<ArtemisDevice>(ExecuteBringToFront);
BringForward = ReactiveCommand.Create<ArtemisDevice>(ExecuteBringForward); BringForward = ReactiveCommand.Create<ArtemisDevice>(ExecuteBringForward);
@ -47,36 +47,32 @@ namespace Artemis.UI.Screens.SurfaceEditor
public double MaxTextureSize => 4096 / _settingsService.GetSetting("Core.RenderScale", 0.25).Value; public double MaxTextureSize => 4096 / _settingsService.GetSetting("Core.RenderScale", 0.25).Value;
public void ClearSelection() public void UpdateSelection(List<SurfaceDeviceViewModel> devices, bool expand, bool invert)
{ {
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels) _initialSelection ??= SurfaceDeviceViewModels.Where(d => d.IsSelected).ToList();
surfaceDeviceViewModel.SelectionStatus = SelectionStatus.None;
if (expand)
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in devices)
surfaceDeviceViewModel.IsSelected = true;
}
else if (invert)
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in devices)
surfaceDeviceViewModel.IsSelected = !_initialSelection.Contains(surfaceDeviceViewModel);
}
else
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in devices)
surfaceDeviceViewModel.IsSelected = true;
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels.Except(devices))
surfaceDeviceViewModel.IsSelected = false;
}
} }
public void StartMouseDrag(Point mousePosition) public void FinishSelection()
{ {
SurfaceDeviceViewModel? startedOn = GetViewModelAtPoint(mousePosition); _initialSelection = null;
if (startedOn != null && startedOn.SelectionStatus != SelectionStatus.Selected)
{
startedOn.SelectionStatus = SelectionStatus.Selected;
foreach (SurfaceDeviceViewModel device in SurfaceDeviceViewModels.Where(vm => vm != startedOn))
device.SelectionStatus = SelectionStatus.None;
}
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
surfaceDeviceViewModel.StartMouseDrag(mousePosition);
}
public void UpdateMouseDrag(Point mousePosition, bool round, bool ignoreOverlap)
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
surfaceDeviceViewModel.UpdateMouseDrag(mousePosition, round, ignoreOverlap);
}
public void StopMouseDrag(Point mousePosition, bool round, bool ignoreOverlap)
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
surfaceDeviceViewModel.UpdateMouseDrag(mousePosition, round, ignoreOverlap);
if (_saving) if (_saving)
return; return;
@ -95,45 +91,28 @@ namespace Artemis.UI.Screens.SurfaceEditor
}); });
} }
public void SelectFirstDeviceAtPoint(Point point, bool expand) public void ClearSelection()
{ {
SurfaceDeviceViewModel? toSelect = GetViewModelAtPoint(point); foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
if (toSelect != null) surfaceDeviceViewModel.IsSelected = false;
toSelect.SelectionStatus = SelectionStatus.Selected;
if (!expand)
{
foreach (SurfaceDeviceViewModel device in SurfaceDeviceViewModels.Where(vm => vm != toSelect))
device.SelectionStatus = SelectionStatus.None;
} }
ApplySurfaceSelection(); public void StartMouseDrag(Point mousePosition)
{
foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
surfaceDeviceViewModel.StartMouseDrag(mousePosition);
} }
private SurfaceDeviceViewModel? GetViewModelAtPoint(Point point) public void UpdateMouseDrag(Point mousePosition, bool round, bool ignoreOverlap)
{ {
SKPoint hitTestPoint = point.ToSKPoint(); foreach (SurfaceDeviceViewModel surfaceDeviceViewModel in SurfaceDeviceViewModels)
return SurfaceDeviceViewModels.OrderByDescending(vm => vm.Device.ZIndex).FirstOrDefault(d => d.Device.Rectangle.Contains(hitTestPoint)); surfaceDeviceViewModel.UpdateMouseDrag(mousePosition, round, ignoreOverlap);
}
public void UpdateSelection(Rect rect, bool expand)
{
SKRect hitTestRect = rect.ToSKRect();
foreach (SurfaceDeviceViewModel device in SurfaceDeviceViewModels)
{
if (device.Device.Rectangle.IntersectsWith(hitTestRect))
device.SelectionStatus = SelectionStatus.Selected;
else if (!expand)
device.SelectionStatus = SelectionStatus.None;
}
ApplySurfaceSelection();
} }
private void ApplySurfaceSelection() private void ApplySurfaceSelection()
{ {
foreach (ListDeviceViewModel viewModel in ListDeviceViewModels) foreach (ListDeviceViewModel viewModel in ListDeviceViewModels)
viewModel.IsSelected = SurfaceDeviceViewModels.Any(s => s.Device == viewModel.Device && s.SelectionStatus == SelectionStatus.Selected); viewModel.IsSelected = SurfaceDeviceViewModels.Any(s => s.Device == viewModel.Device && s.IsSelected);
} }
#region Context menu commands #region Context menu commands
@ -204,5 +183,4 @@ namespace Artemis.UI.Screens.SurfaceEditor
} }
#endregion #endregion
}
} }

View File

@ -2,10 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Presenters;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using Avalonia.VisualTree; using Avalonia.VisualTree;