Skip to content

Commit

Permalink
Some code cleanup, missing icons and actions for the axis dial action.
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianh committed Apr 6, 2024
1 parent 456769e commit d6b9835
Show file tree
Hide file tree
Showing 20 changed files with 441 additions and 248 deletions.
132 changes: 88 additions & 44 deletions streamdeck-vjoy-w4rl0ck/Actions/AxisDialButtonAction.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.DirectoryServices.ActiveDirectory;
using BarRaider.SdTools;
using BarRaider.SdTools;
using BarRaider.SdTools.Events;
using BarRaider.SdTools.Payloads;
using BarRaider.SdTools.Wrappers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Timer = System.Timers.Timer;

namespace streamdeck_vjoy_w4rl0ck.Actions;

Expand All @@ -18,15 +18,12 @@ public AxisDialButtonAction(SDConnection connection, InitialPayload payload) : b
Connection.OnSendToPlugin += Connection_OnSendToPlugin;
SimpleVJoyInterface.VJoyStatusUpdateSignal += SimpleVJoyInterface_OnVJoyStatusUpdate;
SimpleVJoyInterface.AxisSignal += SimpleVJoyInterface_OnAxisSignal;

_timer.AutoReset = true;
_timer.Elapsed += (sender, e) => TimerTick();

if (payload.Controller == "Encoder")
{
_isEncoder = true;
}

if (payload.Controller == "Encoder") _isEncoder = true;

if (payload.Settings == null || payload.Settings.Count == 0)
{
_settings = PluginSettings.CreateDefaultSettings();
Expand All @@ -36,10 +33,12 @@ public AxisDialButtonAction(SDConnection connection, InitialPayload payload) : b
{
_settings = payload.Settings.ToObject<PluginSettings>();
}

ConvertSettings();
#pragma warning disable 4014
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
SendAxisUpdate(_simpleVJoyInterface.GetCurrentAxisValue(_settings.Axis));
#pragma warning enable 4014
#pragma warning restore 4014
}

public override void Dispose()
Expand All @@ -58,7 +57,7 @@ private void Connection_OnSendToPlugin(object sender, SDEventReceivedEventArgs<S
var action = e.Event.Payload["action"]?.ToString();
if (action == "showconfig") Configuration.ShowConfiguration();
}

private async void SimpleVJoyInterface_OnAxisSignal(uint axis, float value)
{
if (axis != _settings.Axis) return;
Expand All @@ -70,8 +69,8 @@ private async Task SendAxisUpdate(float value)
if (!_isEncoder && !_settings.SetTitleValue) return;
var indicator = (int)(value * 100);
if (_configuration.GlobalSettings.AxisConfiguration[_settings.Axis] == 1) value = (value - 0.5f) * 2;
if (Math.Abs(value - (-0f)) < 0.001) value = Math.Abs(value);
var valueString = value.ToString("P0").Replace(" ", string.Empty);;
if (Math.Abs(value - -0f) < 0.001) value = Math.Abs(value);
var valueString = value.ToString("P0").Replace(" ", string.Empty);
if (_isEncoder)
{
var dict = new JObject
Expand All @@ -81,6 +80,7 @@ private async Task SendAxisUpdate(float value)
};
await Connection.SetFeedbackAsync(dict);
}

if (_settings.SetTitleValue) await Connection.SetTitleAsync(valueString);
}

Expand Down Expand Up @@ -110,16 +110,27 @@ public override void DialRotate(DialRotatePayload payload)

public override void DialDown(DialPayload payload)
{
ResetAxis();
if (_settings.DialResetAxis) ResetAxis();
if (_settings.DialButtonAction)
SimpleVJoyInterface.Instance.ButtonState(_dialButtonId, SimpleVJoyInterface.ButtonAction.Down);
}

public override void DialUp(DialPayload payload)
{
if (_settings.DialButtonAction)
SimpleVJoyInterface.Instance.ButtonState(_dialButtonId, SimpleVJoyInterface.ButtonAction.Up);
}

public override void TouchPress(TouchpadPressPayload payload)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "TouchScreen Pressed");
if (_settings.TouchResetAxis) ResetAxis();
if (_settings.TouchButtonAction)
{
_touchButtonIsDown = true;
_timer.Start();
SimpleVJoyInterface.Instance.ButtonState(_dialButtonId, SimpleVJoyInterface.ButtonAction.Down);
}
}

public override void OnTick()
Expand All @@ -129,15 +140,11 @@ public override void OnTick()
private void ResetAxis()
{
if (_configuration.GlobalSettings.AxisConfiguration[_settings.Axis] == 1)
{
_simpleVJoyInterface.SetAxis(_settings.Axis, 50);
}
_simpleVJoyInterface.SetAxis(_settings.Axis, 50);
else
{
_simpleVJoyInterface.SetAxis(_settings.Axis, 0);
}
}

private void TimerTick()
{
switch (_settings.ButtonAction)
Expand All @@ -146,62 +153,96 @@ private void TimerTick()
_simpleVJoyInterface.MoveAxis(_settings.Axis, _settings.Sensitivity / 100.0);
break;
case 2:
_simpleVJoyInterface.MoveAxis(_settings.Axis, - _settings.Sensitivity / 100.0);
_simpleVJoyInterface.MoveAxis(_settings.Axis, -_settings.Sensitivity / 100.0);
break;
}

if (_touchButtonIsDown)
{
SimpleVJoyInterface.Instance.ButtonState(_touchButtonId, SimpleVJoyInterface.ButtonAction.Up);
_timer.Stop();
}

if (_settings.ButtonAction == 0) _timer.Stop();
}

public override void ReceivedSettings(ReceivedSettingsPayload payload)
public override async void ReceivedSettings(ReceivedSettingsPayload payload)
{
Tools.AutoPopulateSettings(_settings, payload.Settings);
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
SendAxisUpdate(_simpleVJoyInterface.GetCurrentAxisValue(_settings.Axis));
if (!_settings.SetTitleValue) Connection.SetTitleAsync("");
ConvertSettings();
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
await SendAxisUpdate(_simpleVJoyInterface.GetCurrentAxisValue(_settings.Axis));
if (!_settings.SetTitleValue) await Connection.SetTitleAsync("");
}

public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload)
{

}

#region Private Methods

private Task SaveSettings()
{
return Connection.SetSettingsAsync(JObject.FromObject(_settings));
}

#endregion

private class PluginSettings
{
[JsonProperty(PropertyName = "axis")]
public ushort Axis { get; set; }
[JsonProperty(PropertyName = "axis")] public ushort Axis { get; set; }

[JsonProperty(PropertyName = "title_value")]
[JsonProperty(PropertyName = "title_value")]
public bool SetTitleValue { get; set; }

[JsonProperty(PropertyName = "sensitivity")]
public ushort Sensitivity { get; set; }

[JsonProperty(PropertyName = "button_action")]
public ushort ButtonAction { get; set; }



[JsonProperty(PropertyName = "dial_reset_axis")]
public bool DialResetAxis { get; set; }

[JsonProperty(PropertyName = "dial_button_action")]
public bool DialButtonAction { get; set; }

[JsonProperty(PropertyName = "dial_button_id")]
public string DialButtonId { get; set; }

[JsonProperty(PropertyName = "touch_reset_axis")]
public bool TouchResetAxis { get; set; }

[JsonProperty(PropertyName = "touch_action")]
public bool TouchButtonAction { get; set; }

[JsonProperty(PropertyName = "touch_button_id")]
public string TouchButtonId { get; set; }

public static PluginSettings CreateDefaultSettings()
{
var instance = new PluginSettings
{
Axis = 0,
Sensitivity = 100,
ButtonAction = 0
ButtonAction = 0,
DialResetAxis = true,
DialButtonAction = false,
DialButtonId = string.Empty,
TouchResetAxis = false,
TouchButtonAction = false,
TouchButtonId = string.Empty
};
return instance;
}
}

#region Private Methods

private Task SaveSettings()
{
return Connection.SetSettingsAsync(JObject.FromObject(_settings));
}

private void ConvertSettings()
{
ushort.TryParse(_settings.TouchButtonId, out _touchButtonId);
ushort.TryParse(_settings.DialButtonId, out _dialButtonId);
}

#endregion

#region Property Inspector

private async void Connection_OnPropertyInspectorDidAppear(object sender,
Expand All @@ -219,7 +260,7 @@ private void Connection_OnPropertyInspectorDidDisappear(object sender,

private async void SimpleVJoyInterface_OnVJoyStatusUpdate()
{
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
if (_simpleVJoyInterface.Status == SimpleVJoyInterface.VJoyStatus.Connected)
await SendAxisUpdate(_simpleVJoyInterface.GetCurrentAxisValue(_settings.Axis));
if (_propertyInspectorIsOpen) await SendPropertyInspectorData();
}
Expand All @@ -235,10 +276,13 @@ private async Task SendPropertyInspectorData()

private readonly PluginSettings _settings;
private bool _propertyInspectorIsOpen;
private readonly System.Timers.Timer _timer = new System.Timers.Timer(100);
private bool _touchButtonIsDown;
private readonly Timer _timer = new(100);
private readonly SimpleVJoyInterface _simpleVJoyInterface = SimpleVJoyInterface.Instance;
private readonly Configuration _configuration = Configuration.Instance;
private readonly bool _isEncoder;
private ushort _touchButtonId;
private ushort _dialButtonId;

#endregion
}
13 changes: 7 additions & 6 deletions streamdeck-vjoy-w4rl0ck/Actions/SimpleButtonAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BarRaider.SdTools.Wrappers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Timer = System.Timers.Timer;

namespace streamdeck_vjoy_w4rl0ck.Actions;

Expand All @@ -18,7 +19,7 @@ public SimpleButtonAction(SDConnection connection, InitialPayload payload) : bas

_timer.AutoReset = false;
_timer.Elapsed += (sender, e) => TimerTick();

if (payload.Settings == null || payload.Settings.Count == 0)
{
_settings = PluginSettings.CreateDefaultSettings();
Expand Down Expand Up @@ -47,7 +48,7 @@ public override void Dispose()
}

public override void KeyPressed(KeyPayload payload)
{
{
Logger.Instance.LogMessage(TracingLevel.INFO, $"Key Pressed '{payload.IsInMultiAction}'");
SimpleVJoyInterface.Instance.ButtonState(_settings.ButtonId, SimpleVJoyInterface.ButtonAction.Down);
if (payload.IsInMultiAction) _timer.Start();
Expand All @@ -59,12 +60,12 @@ public override void KeyReleased(KeyPayload payload)
Logger.Instance.LogMessage(TracingLevel.INFO, $"Key Released '{payload.IsInMultiAction}'");
SimpleVJoyInterface.Instance.ButtonState(_settings.ButtonId, SimpleVJoyInterface.ButtonAction.Up);
}

private void TimerTick()
{
Logger.Instance.LogMessage(TracingLevel.INFO, $"Timer Released");
Logger.Instance.LogMessage(TracingLevel.INFO, "Timer Released");
SimpleVJoyInterface.Instance.ButtonState(_settings.ButtonId, SimpleVJoyInterface.ButtonAction.Up);
_timer.Stop();
_timer.Stop();
}

public override void OnTick()
Expand Down Expand Up @@ -134,7 +135,7 @@ private async Task SendPropertyInspectorData()
#region Private Members

private readonly PluginSettings _settings;
private readonly System.Timers.Timer _timer = new System.Timers.Timer(100);
private readonly Timer _timer = new(100);
private bool _propertyInspectorIsOpen;

#endregion
Expand Down
60 changes: 30 additions & 30 deletions streamdeck-vjoy-w4rl0ck/Actions/ToggleButtonAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void Connection_OnSendToPlugin(object sender, SDEventReceivedEventArgs<S
var action = e.Event.Payload["action"]?.ToString();
if (action == "showconfig") Configuration.ShowConfiguration();
}

private void SimpleVJoyInterface_OnUpdateButtonSignal(uint buttonId, bool state)
{
if (buttonId != _settings.ButtonId) return;
Expand All @@ -64,34 +64,6 @@ public override void KeyReleased(KeyPayload payload)
public override void OnTick()
{
}


#region Property Inspector

private async void Connection_OnPropertyInspectorDidAppear(object sender,
SDEventReceivedEventArgs<PropertyInspectorDidAppear> e)
{
await SendPropertyInspectorData();
_propertyInspectorIsOpen = true;
}

private void Connection_OnPropertyInspectorDidDisappear(object sender,
SDEventReceivedEventArgs<PropertyInspectorDidDisappear> e)
{
_propertyInspectorIsOpen = false;
}

private async void SimpleVJoyInterface_OnVJoyStatusUpdate()
{
if (_propertyInspectorIsOpen) await SendPropertyInspectorData();
}

private async Task SendPropertyInspectorData()
{
await Connection.SendToPropertyInspectorAsync(Configuration.Instance.GetPropertyInspectorData());
}

#endregion

public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
Expand Down Expand Up @@ -125,7 +97,35 @@ public static PluginSettings CreateDefaultSettings()
return instance;
}
}



#region Property Inspector

private async void Connection_OnPropertyInspectorDidAppear(object sender,
SDEventReceivedEventArgs<PropertyInspectorDidAppear> e)
{
await SendPropertyInspectorData();
_propertyInspectorIsOpen = true;
}

private void Connection_OnPropertyInspectorDidDisappear(object sender,
SDEventReceivedEventArgs<PropertyInspectorDidDisappear> e)
{
_propertyInspectorIsOpen = false;
}

private async void SimpleVJoyInterface_OnVJoyStatusUpdate()
{
if (_propertyInspectorIsOpen) await SendPropertyInspectorData();
}

private async Task SendPropertyInspectorData()
{
await Connection.SendToPropertyInspectorAsync(Configuration.Instance.GetPropertyInspectorData());
}

#endregion

#region Private Members

private readonly PluginSettings _settings;
Expand Down
Loading

0 comments on commit d6b9835

Please sign in to comment.