1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 21:38:38 +00:00

Nodes - Added hotkey press node

This commit is contained in:
Robert 2023-01-31 00:14:51 +01:00
parent 704bdce68b
commit 425485b059
5 changed files with 167 additions and 0 deletions

View File

@ -23,4 +23,11 @@
<ProjectReference Include="..\Artemis.UI.Shared\Artemis.UI.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Nodes\Input\Screens\HotkeyPressNodeCustomView.axaml.cs">
<DependentUpon>HotkeyPressNodeCustomView.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,69 @@
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.VisualScripting.Nodes.Input.Screens;
namespace Artemis.VisualScripting.Nodes.Input;
[Node("Hotkey press", "Outputs a boolean value for as long as a hotkey is pressed", "Input", OutputType = typeof(bool))]
public class HotkeyPressNode : Node<HotkeyEnableDisableNodeEntity, HotkeyPressNodeCustomViewModel>, IDisposable
{
private readonly IInputService _inputService;
private Hotkey? _toggleHotkey;
private bool _value;
public HotkeyPressNode(IInputService inputService)
{
_inputService = inputService;
_inputService.KeyboardKeyDown += InputServiceOnKeyboardKeyDown;
_inputService.KeyboardKeyUp += InputServiceOnKeyboardKeyUp;
Output = CreateOutputPin<bool>();
StorageModified += OnStorageModified;
}
public OutputPin<bool> Output { get; }
public override void Initialize(INodeScript script)
{
LoadHotkeys();
}
public override void Evaluate()
{
Output.Value = _value;
}
private void OnStorageModified(object? sender, EventArgs e)
{
LoadHotkeys();
}
private void LoadHotkeys()
{
if (Storage == null)
return;
_toggleHotkey = Storage.EnableHotkey != null ? new Hotkey(Storage.EnableHotkey) : null;
}
private void InputServiceOnKeyboardKeyDown(object? sender, ArtemisKeyboardKeyEventArgs e)
{
if (Storage == null)
return;
if (_toggleHotkey != null && _toggleHotkey.MatchesEventArgs(e))
_value = true;
}
private void InputServiceOnKeyboardKeyUp(object? sender, ArtemisKeyboardKeyEventArgs e)
{
if (Storage == null)
return;
if (_toggleHotkey != null && _toggleHotkey.MatchesEventArgs(e))
_value = false;
}
public void Dispose()
{
_inputService.KeyboardKeyUp -= InputServiceOnKeyboardKeyUp;
}
}

View File

@ -0,0 +1,11 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared"
xmlns:screens="clr-namespace:Artemis.VisualScripting.Nodes.Input.Screens"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.VisualScripting.Nodes.Input.Screens.HotkeyPressNodeCustomView"
x:DataType="screens:HotkeyPressNodeCustomViewModel">
<shared:HotkeyBox Classes="condensed" Hotkey="{CompiledBinding ToggleHotkey}" HotkeyChanged="HotkeyBox_OnHotkeyChanged" MinWidth="75" />
</UserControl>

View File

@ -0,0 +1,25 @@
using Artemis.UI.Shared;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
namespace Artemis.VisualScripting.Nodes.Input.Screens;
public partial class HotkeyPressNodeCustomView : ReactiveUserControl<HotkeyPressNodeCustomViewModel>
{
public HotkeyPressNodeCustomView()
{
InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void HotkeyBox_OnHotkeyChanged(HotkeyBox sender, EventArgs args)
{
ViewModel?.Save();
}
}

View File

@ -0,0 +1,55 @@
using System.Reactive.Linq;
using Artemis.Core;
using Artemis.UI.Shared.Services.NodeEditor;
using Artemis.UI.Shared.Services.NodeEditor.Commands;
using Artemis.UI.Shared.VisualScripting;
using Avalonia.Controls.Mixins;
using ReactiveUI;
namespace Artemis.VisualScripting.Nodes.Input.Screens;
public class HotkeyPressNodeCustomViewModel : CustomNodeViewModel
{
private readonly HotkeyPressNode _pressNode;
private readonly INodeEditorService _nodeEditorService;
private Hotkey? _toggleHotkey;
private bool _updating;
/// <inheritdoc />
public HotkeyPressNodeCustomViewModel(HotkeyPressNode pressNode, INodeScript script, INodeEditorService nodeEditorService) : base(pressNode, script)
{
_pressNode = pressNode;
_nodeEditorService = nodeEditorService;
this.WhenActivated(d =>
{
Observable.FromEventPattern(x => _pressNode.StorageModified += x, x => _pressNode.StorageModified -= x).Subscribe(_ => Update()).DisposeWith(d);
Update();
});
}
private void Update()
{
_updating = true;
ToggleHotkey = _pressNode.Storage?.EnableHotkey != null ? new Hotkey(_pressNode.Storage.EnableHotkey) : null;
_updating = false;
}
public Hotkey? ToggleHotkey
{
get => _toggleHotkey;
set => this.RaiseAndSetIfChanged(ref _toggleHotkey, value);
}
public void Save()
{
if (_updating)
return;
_nodeEditorService.ExecuteCommand(
Script,
new UpdateStorage<HotkeyEnableDisableNodeEntity>(_pressNode, new HotkeyEnableDisableNodeEntity(ToggleHotkey, null), "hotkey")
);
}
}