Skip to content

Commit

Permalink
Merge pull request #815 from Artemis-RGB/development
Browse files Browse the repository at this point in the history
Adaption hints - Added more hints and sections
  • Loading branch information
RobertBeekman authored Sep 10, 2023
2 parents bbadef7 + d96581f commit 888ed37
Show file tree
Hide file tree
Showing 21 changed files with 433 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Artemis.Storage.Entities.Profile.AdaptionHints;
using RGB.NET.Core;
Expand All @@ -15,7 +16,12 @@ public class KeyboardSectionAdaptionHint : CorePropertyChanged, IAdaptionHint
{
{KeyboardSection.MacroKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_Programmable1 && l <= LedId.Keyboard_Programmable32).ToList()},
{KeyboardSection.LedStrips, Enum.GetValues<LedId>().Where(l => l >= LedId.LedStripe1 && l <= LedId.LedStripe128).ToList()},
{KeyboardSection.Extra, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_Custom1 && l <= LedId.Keyboard_Custom64).ToList()}
{KeyboardSection.Extra, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_Custom1 && l <= LedId.Keyboard_Custom64).ToList()},
{KeyboardSection.FunctionKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_F1 && l <= LedId.Keyboard_F12).ToList()},
{KeyboardSection.NumberKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_1 && l <= LedId.Keyboard_0).ToList()},
{KeyboardSection.NumPad, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_NumLock && l <= LedId.Keyboard_NumPeriodAndDelete).ToList()},
{KeyboardSection.ArrowKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_PageDown && l <= LedId.Keyboard_ArrowRight).ToList()},
{KeyboardSection.MediaKeys, Enum.GetValues<LedId>().Where(l => l >= LedId.Keyboard_MediaMute && l <= LedId.Keyboard_MediaNextTrack).ToList()},
};

private KeyboardSection _section;
Expand Down Expand Up @@ -46,6 +52,12 @@ public KeyboardSection Section
/// <inheritdoc />
public void Apply(Layer layer, List<ArtemisDevice> devices)
{
if (Section == KeyboardSection.Movement)
{
ApplyMovement(layer, devices);
return;
}

// Only keyboards should have the LEDs we care about
foreach (ArtemisDevice keyboard in devices.Where(d => d.DeviceType == RGBDeviceType.Keyboard))
{
Expand All @@ -54,6 +66,26 @@ public void Apply(Layer layer, List<ArtemisDevice> devices)
}
}

private void ApplyMovement(Layer layer, List<ArtemisDevice> devices)
{
// Only keyboards should have the LEDs we care about
foreach (ArtemisDevice keyboard in devices.Where(d => d.DeviceType == RGBDeviceType.Keyboard))
{
ArtemisLed? qLed = keyboard.Leds.FirstOrDefault(l => l.RgbLed.Id == LedId.Keyboard_Q);
ArtemisLed? aLed = keyboard.Leds.FirstOrDefault(l => l.RgbLed.Id == LedId.Keyboard_A);
if (qLed == null || aLed == null)
continue;

// AZERTY keyboards will have their A above their Q
bool isAzerty = aLed.Rectangle.MidX < qLed.Rectangle.MidX;

if (isAzerty)
layer.AddLeds(keyboard.Leds.Where(l => l.RgbLed.Id is LedId.Keyboard_Z or LedId.Keyboard_Q or LedId.Keyboard_S or LedId.Keyboard_D));
else
layer.AddLeds(keyboard.Leds.Where(l => l.RgbLed.Id is LedId.Keyboard_W or LedId.Keyboard_A or LedId.Keyboard_S or LedId.Keyboard_D));
}
}

/// <inheritdoc />
public IAdaptionHintEntity GetEntry()
{
Expand All @@ -77,15 +109,45 @@ public enum KeyboardSection
/// <summary>
/// A region containing the macro keys of a keyboard
/// </summary>
MacroKeys,
[Description("Macro Keys")] MacroKeys,

/// <summary>
/// A region containing the LED strips of a keyboard
/// </summary>
LedStrips,
[Description("LED Strips")] LedStrips,

/// <summary>
/// A region containing the extra non-standard LEDs of a keyboard
/// </summary>
[Description("Extra LEDs")] Extra,

/// <summary>
/// A region containing the movement keys of a keyboard (WASD for QWERTY and ZQSD for AZERTY)
/// </summary>
[Description("Movement (WASD/ZQSD)")] Movement,

/// <summary>
/// A region containing the F-keys of a keyboard
/// </summary>
[Description("F-keys")] FunctionKeys,

/// <summary>
/// A region containing the numeric keys of a keyboard
/// </summary>
[Description("Numeric keys")] NumberKeys,

/// <summary>
/// A region containing the Numpad of a keyboard
/// </summary>
[Description("Numpad")] NumPad,

/// <summary>
/// A region containing the arrow keys of a keyboard
/// </summary>
[Description("Arrow keys")] ArrowKeys,

/// <summary>
/// A region containing extra non-standard LEDs of a keyboard
/// A region containing the media keys of a keyboard
/// </summary>
Extra
[Description("Media keys")] MediaKeys
}
102 changes: 102 additions & 0 deletions src/Artemis.Core/Models/Profile/AdaptionHints/SingeLedAdaptionHint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using Artemis.Storage.Entities.Profile.AdaptionHints;
using RGB.NET.Core;

namespace Artemis.Core;

/// <summary>
/// Represents a hint that adapts layers to a single LED of one or more devices
/// </summary>
public class SingleLedAdaptionHint : CorePropertyChanged, IAdaptionHint
{
private LedId _ledId;
private int _skip;
private bool _limitAmount;
private int _amount;

/// <summary>
/// Creates a new instance of the <see cref="SingleLedAdaptionHint" /> class
/// </summary>
public SingleLedAdaptionHint()
{
}

internal SingleLedAdaptionHint(SingleLedAdaptionHintEntity entity)
{
LedId = (LedId) entity.LedId;
Skip = entity.Skip;
LimitAmount = entity.LimitAmount;
Amount = entity.Amount;
}

/// <summary>
/// Gets or sets the LED ID to apply to.
/// </summary>
public LedId LedId
{
get => _ledId;
set => SetAndNotify(ref _ledId, value);
}

/// <summary>
/// Gets or sets the amount of devices to skip
/// </summary>
public int Skip
{
get => _skip;
set => SetAndNotify(ref _skip, value);
}

/// <summary>
/// Gets or sets a boolean indicating whether a limited amount of devices should be used
/// </summary>
public bool LimitAmount
{
get => _limitAmount;
set => SetAndNotify(ref _limitAmount, value);
}

/// <summary>
/// Gets or sets the amount of devices to limit to if <see cref="LimitAmount" /> is <see langword="true" />
/// </summary>
public int Amount
{
get => _amount;
set => SetAndNotify(ref _amount, value);
}

#region Implementation of IAdaptionHint

/// <inheritdoc />
public void Apply(Layer layer, List<ArtemisDevice> devices)
{
IEnumerable<ArtemisDevice> matches = devices
.Where(d => d.Leds.Any(l => l.RgbLed.Id == LedId))
.OrderBy(d => d.Rectangle.Top)
.ThenBy(d => d.Rectangle.Left)
.Skip(Skip);
if (LimitAmount)
matches = matches.Take(Amount);

foreach (ArtemisDevice artemisDevice in matches)
{
ArtemisLed led = artemisDevice.Leds.First(l => l.RgbLed.Id == LedId);
layer.AddLed(led);
}
}

/// <inheritdoc />
public IAdaptionHintEntity GetEntry()
{
return new SingleLedAdaptionHintEntity {Amount = Amount, LimitAmount = LimitAmount, Skip = Skip, LedId = (int) LedId};
}

/// <inheritdoc />
public override string ToString()
{
return $"Single LED adaption - {nameof(LedId)}: {LedId}, {nameof(Skip)}: {Skip}, {nameof(LimitAmount)}: {LimitAmount}, {nameof(Amount)}: {Amount}";
}

#endregion
}
12 changes: 12 additions & 0 deletions src/Artemis.Core/Models/Profile/LayerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public List<IAdaptionHint> DetermineHints(IEnumerable<ArtemisDevice> devices)
newHints.Add(hint);
}
}

// A single LED assignment is turned into a hint for one matching LED ID on the same device type
if (Layer.Leds.Count == 1)
{
ArtemisLed led = Layer.Leds.Single();
SingleLedAdaptionHint hint = new() {LedId = led.RgbLed.Id, Amount = 1, LimitAmount = true};
Add(hint);
newHints.Add(hint);
}
}

return newHints;
Expand Down Expand Up @@ -184,6 +193,9 @@ public void Load()
case KeyboardSectionAdaptionHintEntity entity:
Add(new KeyboardSectionAdaptionHint(entity));
break;
case SingleLedAdaptionHintEntity entity:
Add(new SingleLedAdaptionHint(entity));
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Artemis.Storage.Entities.Profile.AdaptionHints;

public class SingleLedAdaptionHintEntity : IAdaptionHintEntity
{
public int LedId { get; set; }

public bool LimitAmount { get; set; }
public int Skip { get; set; }
public int Amount { get; set; }
}
2 changes: 1 addition & 1 deletion src/Artemis.UI.Shared/Controls/EnumComboBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ComboBox x:Name="ChildEnumComboBox" HorizontalAlignment="Stretch">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="local:EnumComboBoxItem">
<TextBlock Text="{CompiledBinding Value}" />
<TextBlock Text="{CompiledBinding Description}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using Artemis.Core;

namespace Artemis.UI.Shared.Services.ProfileEditor.Commands;

/// <summary>
/// Represents a profile editor command that can be used to apply adaption hints to a layer.
/// </summary>
public class ApplyAdaptionHints : IProfileEditorCommand
{
private readonly Layer _layer;
private readonly List<ArtemisDevice> _devices;
private readonly List<ArtemisLed> _originalLeds;

/// <summary>
/// Creates a new instance of the <see cref="ApplyAdaptionHints" /> class.
/// </summary>
public ApplyAdaptionHints(Layer layer, List<ArtemisDevice> devices)
{
_layer = layer;
_devices = devices;
_originalLeds = new List<ArtemisLed>(_layer.Leds);
}

#region Implementation of IProfileEditorCommand

/// <inheritdoc />
public string DisplayName => "Apply adaption hints";

/// <inheritdoc />
public void Execute()
{
_layer.ClearLeds();
_layer.Adapter.Adapt(_devices);
}

/// <inheritdoc />
public void Undo()
{
_layer.ClearLeds();
_layer.AddLeds(_originalLeds);
}

#endregion
}
19 changes: 0 additions & 19 deletions src/Artemis.UI/Artemis.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,4 @@
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<Compile Update="Screens\Workshop\Entries\Tabs\ProfileListView.axaml.cs">
<DependentUpon>ProfileListView.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="Screens\Workshop\Entries\Tabs\LayoutListView.axaml.cs">
<DependentUpon>LayoutListView.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="Screens\Workshop\Library\SubmissionDetailView.axaml.cs">
<DependentUpon>SubmissionsDetailView.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="Screens\Workshop\Entries\Windows\MarkdownPreviewView.axaml" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions src/Artemis.UI/DryIoc/Factories/IVMFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ public interface ILayerHintVmFactory : IVmFactory
CategoryAdaptionHintViewModel CategoryAdaptionHintViewModel(Layer layer, CategoryAdaptionHint adaptionHint);
DeviceAdaptionHintViewModel DeviceAdaptionHintViewModel(Layer layer, DeviceAdaptionHint adaptionHint);
KeyboardSectionAdaptionHintViewModel KeyboardSectionAdaptionHintViewModel(Layer layer, KeyboardSectionAdaptionHint adaptionHint);
SingleLedAdaptionHintViewModel SingleLedAdaptionHintViewModel(Layer layer, SingleLedAdaptionHint adaptionHint);
}
public class LayerHintVmFactory : ILayerHintVmFactory
{
Expand All @@ -454,6 +455,11 @@ public KeyboardSectionAdaptionHintViewModel KeyboardSectionAdaptionHintViewModel
{
return _container.Resolve<KeyboardSectionAdaptionHintViewModel>(new object[] { layer, adaptionHint });
}

public SingleLedAdaptionHintViewModel SingleLedAdaptionHintViewModel(Layer layer, SingleLedAdaptionHint adaptionHint)
{
return _container.Resolve<SingleLedAdaptionHintViewModel>(new object[] { layer, adaptionHint });
}
}

public interface IScriptVmFactory : IVmFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</Button>

<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<shared:EnumComboBox Value="{CompiledBinding CategoryAdaptionHint.Category}" Width="130" Margin="0 0 10 0" />
<shared:EnumComboBox Value="{CompiledBinding CategoryAdaptionHint.Category}" Width="200" Margin="0 0 10 0" />

<TextBlock VerticalAlignment="Center">Skip</TextBlock>
<controls:NumberBox HorizontalAlignment="Stretch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</Button>

<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<shared:EnumComboBox Value="{CompiledBinding DeviceAdaptionHint.DeviceType}" Width="130" Margin="0 0 10 0" />
<shared:EnumComboBox Value="{CompiledBinding DeviceAdaptionHint.DeviceType}" Width="200" Margin="0 0 10 0" />

<TextBlock VerticalAlignment="Center">Skip</TextBlock>
<controls:NumberBox HorizontalAlignment="Stretch"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</Button>

<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" Spacing="5">
<shared:EnumComboBox Value="{CompiledBinding KeyboardSectionAdaptionHint.Section}" Width="130" Margin="0 0 10 0" />
<shared:EnumComboBox Value="{CompiledBinding KeyboardSectionAdaptionHint.Section}" Width="200" Margin="0 0 10 0" />
</StackPanel>
</Grid>
</UserControl>
Loading

0 comments on commit 888ed37

Please sign in to comment.