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

Merge pull request #797 from Artemis-RGB/feature/device-visualizer

Cleaned up the device visualizer
This commit is contained in:
RobertBeekman 2023-06-22 23:00:09 +02:00 committed by GitHub
commit d9c63d63e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared.Events; using Artemis.UI.Shared.Events;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
@ -13,6 +14,7 @@ using Avalonia.Media;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Threading; using Avalonia.Threading;
using RGB.NET.Core; using RGB.NET.Core;
using DryIoc;
using Color = RGB.NET.Core.Color; using Color = RGB.NET.Core.Color;
using Point = Avalonia.Point; using Point = Avalonia.Point;
using Size = Avalonia.Size; using Size = Avalonia.Size;
@ -24,20 +26,19 @@ namespace Artemis.UI.Shared;
/// </summary> /// </summary>
public class DeviceVisualizer : Control public class DeviceVisualizer : Control
{ {
private const double UPDATE_FRAME_RATE = 25.0; private readonly ICoreService _coreService;
private readonly List<DeviceVisualizerLed> _deviceVisualizerLeds; private readonly List<DeviceVisualizerLed> _deviceVisualizerLeds;
private readonly DispatcherTimer _timer;
private Rect _deviceBounds; private Rect _deviceBounds;
private RenderTargetBitmap? _deviceImage; private RenderTargetBitmap? _deviceImage;
private ArtemisDevice? _oldDevice; private ArtemisDevice? _oldDevice;
private bool _loading; private bool _loading;
private Color[] _previousState = Array.Empty<Color>(); private Color[] _previousState = Array.Empty<Color>();
/// <inheritdoc /> /// <inheritdoc />
public DeviceVisualizer() public DeviceVisualizer()
{ {
_timer = new DispatcherTimer(DispatcherPriority.Background) {Interval = TimeSpan.FromMilliseconds(1000.0 / UPDATE_FRAME_RATE)}; _coreService = UI.Locator.Resolve<ICoreService>();
_deviceVisualizerLeds = new List<DeviceVisualizerLed>(); _deviceVisualizerLeds = new List<DeviceVisualizerLed>();
PointerReleased += OnPointerReleased; PointerReleased += OnPointerReleased;
@ -120,23 +121,26 @@ public class DeviceVisualizer : Control
if (Device == null) if (Device == null)
return false; return false;
Color[] state = new Color[Device.RgbDevice.Count()]; bool difference = false;
bool difference = _previousState.Length != state.Length;
int newLedCount = Device.RgbDevice.Count();
if (_previousState.Length != newLedCount)
{
_previousState = new Color[newLedCount];
difference = true;
}
// Check all LEDs for differences and copy the colors to a new state // Check all LEDs for differences and copy the colors to a new state
int index = 0; int index = 0;
foreach (Led led in Device.RgbDevice) foreach (Led led in Device.RgbDevice)
{ {
if (!difference && !led.Color.Equals(_previousState[index])) if (_previousState[index] != led.Color)
difference = true; difference = true;
state[index] = led.Color; _previousState[index] = led.Color;
index++; index++;
} }
// Store the new state for next time
_previousState = state;
return difference; return difference;
} }
@ -156,11 +160,14 @@ public class DeviceVisualizer : Control
return geometry.Bounds; return geometry.Bounds;
} }
private void TimerOnTick(object? sender, EventArgs e) private void OnFrameRendered(object? sender, FrameRenderedEventArgs e)
{ {
if (IsDirty() && ShowColors && IsVisible && Opacity > 0) Dispatcher.UIThread.Post(() =>
Update(); {
if (ShowColors && IsVisible && Opacity > 0 && IsDirty())
Update();
}, DispatcherPriority.Background);
} }
private void OnPointerReleased(object? sender, PointerReleasedEventArgs e) private void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
@ -250,16 +257,16 @@ public class DeviceVisualizer : Control
/// <inheritdoc /> /// <inheritdoc />
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{ {
_timer.Start(); _coreService.FrameRendered += OnFrameRendered;
_timer.Tick += TimerOnTick;
base.OnAttachedToLogicalTree(e); base.OnAttachedToLogicalTree(e);
} }
/// <inheritdoc /> /// <inheritdoc />
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{ {
_timer.Stop(); _coreService.FrameRendered -= OnFrameRendered;
_timer.Tick -= TimerOnTick;
base.OnDetachedFromLogicalTree(e); base.OnDetachedFromLogicalTree(e);
} }