Skip to content

Commit

Permalink
Group options
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Apr 19, 2024
1 parent 56acf30 commit 552f647
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Installer/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Section "Install" SecInstall

SetOutPath "$INSTDIR"

File /r /x *.log /x *.pdb "..\ShockOsc\bin\Release\net8.0-windows10.0.19041.0\win10-x64\*.*"
File /r /x *.log /x *.pdb /x *.mui "..\ShockOsc\bin\Release\net8.0-windows10.0.19041.0\win10-x64\*.*"

WriteRegStr HKLM "Software\ShockOSC" "InstallDir" $INSTDIR
WriteUninstaller "$INSTDIR\Uninstall.exe"
Expand Down
22 changes: 22 additions & 0 deletions ShockOsc/Config/Group.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace OpenShock.ShockOsc.Config;

public sealed class Group
{
public required string Name { get; set; }
public IList<Guid> Shockers { get; set; } = new List<Guid>();

public bool OverrideIntensity { get; set; }

public bool RandomIntensity { get; set; }
public JsonRange IntensityRange { get; set; } = new JsonRange { Min = 1, Max = 30 };
public byte FixedIntensity { get; set; } = 50;

public bool OverrideDuration { get; set; }
public bool RandomDuration { get; set; }
public uint RandomDurationStep { get; set; } = 1000;
public JsonRange DurationRange { get; set; } = new JsonRange { Min = 1000, Max = 5000 };
public uint FixedDuration { get; set; } = 2000;

public bool OverrideCooldownTime { get; set; }
public uint CooldownTime { get; set; } = 5000;
}
6 changes: 1 addition & 5 deletions ShockOsc/Config/ShockOscConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ public sealed class ShockOscConfig
public IDictionary<Guid, Group> Groups { get; set; } = new Dictionary<Guid, Group>();
public Version? LastIgnoredVersion { get; set; } = null;

public sealed class Group
{
public required string Name { get; set; }
public IList<Guid> Shockers { get; set; } = new List<Guid>();
}

}
6 changes: 5 additions & 1 deletion ShockOsc/Models/ProgramGroup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using OpenShock.ShockOsc.Config;
using OpenShock.ShockOsc.OscChangeTracker;
using OpenShock.ShockOsc.Services;

Expand All @@ -23,10 +24,13 @@ public sealed class ProgramGroup
public string Name { get; }
public TriggerMethod TriggerMethod { get; set; }

public ProgramGroup(Guid id, string name, OscClient oscClient)
public Group? ConfigGroup { get; }

public ProgramGroup(Guid id, string name, OscClient oscClient, Group? group)
{
Id = id;
Name = name;
ConfigGroup = group;

ParamActive = new ChangeTrackedOscParam<bool>(Name, "_Active", false, oscClient);
ParamCooldown = new ChangeTrackedOscParam<bool>(Name, "_Cooldown", false, oscClient);
Expand Down
2 changes: 1 addition & 1 deletion ShockOsc/Platforms/Windows/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</Dependencies>

<Resources>
<Resource Language="x-generate" />
<Resource Language="en-US"/>
</Resources>

<Applications>
Expand Down
6 changes: 4 additions & 2 deletions ShockOsc/Platforms/Windows/WindowsTrayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OpenShock.SDK.CSharp.Hub;
using OpenShock.ShockOsc.Services;
using Application = Microsoft.Maui.Controls.Application;
using Color = System.Drawing.Color;
using Image = System.Drawing.Image;

namespace OpenShock.ShockOsc;
Expand All @@ -20,13 +21,14 @@ public WindowsTrayService(OpenShockHubClient apiHubClient)
_apiHubClient.Reconnecting += _ => HubStateChanged();
_apiHubClient.Reconnected += _ => HubStateChanged();
_apiHubClient.Closed += _ => HubStateChanged();
_apiHubClient.Connected += _ => HubStateChanged();
}

private ToolStripLabel _stateLabel;

private Task HubStateChanged()
{
_stateLabel.Text = "State: " + _apiHubClient.State;
_stateLabel.Text = $"State: {_apiHubClient.State}";
return Task.CompletedTask;
}

Expand All @@ -40,7 +42,7 @@ public void Initialize()

menu.Items.Add("ShockOSC", Image.FromFile(@"Resources\openshock-icon.ico"), OnMainClick);
menu.Items.Add(new ToolStripSeparator());
_stateLabel = new ToolStripLabel("State: " + _apiHubClient.State);
_stateLabel = new ToolStripLabel($"State: {_apiHubClient.State}");
menu.Items.Add(_stateLabel);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Restart", null, Restart);
Expand Down
74 changes: 52 additions & 22 deletions ShockOsc/Services/ShockOsc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public ShockOsc(ILogger<ShockOsc> logger,
private async Task SetupGroups()
{
_dataLayer.ProgramGroups.Clear();
_dataLayer.ProgramGroups[Guid.Empty] = new ProgramGroup(Guid.Empty, "_All", _oscClient);
foreach (var (id, group) in _configManager.Config.Groups) _dataLayer.ProgramGroups[id] = new ProgramGroup(id, group.Name, _oscClient);
_dataLayer.ProgramGroups[Guid.Empty] = new ProgramGroup(Guid.Empty, "_All", _oscClient, null);
foreach (var (id, group) in _configManager.Config.Groups) _dataLayer.ProgramGroups[id] = new ProgramGroup(id, group.Name, _oscClient, group);
}

public Task RaiseOnGroupsChanged() => OnGroupsChanged.Raise();
Expand Down Expand Up @@ -309,7 +309,7 @@ private async Task ReceiveLogic()
return;
}

OsTask.Run(() => InstantShock(programGroup, GetDuration(), GetIntensity()));
OsTask.Run(() => InstantShock(programGroup, GetDuration(programGroup), GetIntensity(programGroup)));

return;
case "Stretch":
Expand Down Expand Up @@ -430,23 +430,39 @@ private async Task CheckLoop()
}
}

private byte GetIntensity()
private byte GetIntensity(ProgramGroup programGroup)
{
var config = _configManager.Config.Behaviour;
if (programGroup.ConfigGroup is not { OverrideDuration: true })
{
// Use global config
var config = _configManager.Config.Behaviour;

if (!config.RandomIntensity) return config.FixedIntensity;
var rir = config.IntensityRange;
var intensityValue = Random.Next((int)rir.Min, (int)rir.Max);
return (byte)intensityValue;
}

// Use groupConfig
var groupConfig = programGroup.ConfigGroup;

if (!config.RandomIntensity) return config.FixedIntensity;
var rir = config.IntensityRange;
var intensityValue = Random.Next((int)rir.Min, (int)rir.Max);
return (byte)intensityValue;
if (!groupConfig.RandomIntensity) return groupConfig.FixedIntensity;
var groupRir = groupConfig.IntensityRange;
var groupIntensityValue = Random.Next((int)groupRir.Min, (int)groupRir.Max);
return (byte)groupIntensityValue;
}

private async Task CheckLogic()
{
var config = _configManager.Config.Behaviour;
foreach (var (pos, programGroup) in _dataLayer.ProgramGroups)
{
var cooldownTime = _configManager.Config.Behaviour.CooldownTime;
if(programGroup.ConfigGroup is { OverrideCooldownTime: true })
cooldownTime = programGroup.ConfigGroup.CooldownTime;

var isActiveOrOnCooldown =
programGroup.LastExecuted.AddMilliseconds(_configManager.Config.Behaviour.CooldownTime)
programGroup.LastExecuted.AddMilliseconds(cooldownTime)
.AddMilliseconds(programGroup.LastDuration) > DateTime.UtcNow;

if (programGroup.TriggerMethod == TriggerMethod.None &&
Expand Down Expand Up @@ -498,26 +514,40 @@ private async Task CheckLogic()

if (programGroup.TriggerMethod == TriggerMethod.PhysBoneRelease)
{
intensity = (byte)MathUtils.LerpFloat(config.IntensityRange.Min, config.IntensityRange.Max,
programGroup.LastStretchValue);
intensity = programGroup.ConfigGroup is { OverrideIntensity: true }
? (byte)MathUtils.LerpFloat(programGroup.ConfigGroup.IntensityRange.Min,
programGroup.ConfigGroup.IntensityRange.Max,
programGroup.LastStretchValue)
: (byte)MathUtils.LerpFloat(config.IntensityRange.Min, config.IntensityRange.Max,
programGroup.LastStretchValue);
programGroup.LastStretchValue = 0;
}
else intensity = GetIntensity();
else intensity = GetIntensity(programGroup);

InstantShock(programGroup, GetDuration(), intensity);
InstantShock(programGroup, GetDuration(programGroup), intensity);
}
}



private uint GetDuration()
private uint GetDuration(ProgramGroup programGroup)
{
var config = _configManager.Config.Behaviour;
if (programGroup.ConfigGroup is not { OverrideDuration: true })
{
// Use global config
var config = _configManager.Config.Behaviour;

if (!config.RandomDuration) return config.FixedDuration;
var rdr = config.DurationRange;
return (uint)(Random.Next((int)(rdr.Min / config.RandomDurationStep),
(int)(rdr.Max / config.RandomDurationStep)) * config.RandomDurationStep);
}

// Use group config
var groupConfig = programGroup.ConfigGroup;

if (!config.RandomDuration) return config.FixedDuration;
var rdr = config.DurationRange;
return (uint)(Random.Next((int)(rdr.Min / config.RandomDurationStep),
(int)(rdr.Max / config.RandomDurationStep)) * config.RandomDurationStep);
if (!groupConfig.RandomDuration) return groupConfig.FixedDuration;
var groupRdr = groupConfig.DurationRange;
return (uint)(Random.Next((int)(groupRdr.Min / groupConfig.RandomDurationStep),
(int)(groupRdr.Max / groupConfig.RandomDurationStep)) * groupConfig.RandomDurationStep);
}

}
11 changes: 7 additions & 4 deletions ShockOsc/ShockOsc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

<WindowsPackageType Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">None</WindowsPackageType>
<PackageIcon>Resources\Icon512.png</PackageIcon>

<ResourceLanguages>en</ResourceLanguages>
<SatelliteResourceLanguages>en-US;en</SatelliteResourceLanguages>
</PropertyGroup>

<ItemGroup>
Expand All @@ -54,9 +57,9 @@
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="LucHeart.CoreOSC" Version="1.2.1" />
<PackageReference Include="MeaMod.DNS" Version="1.0.70" />
<PackageReference Include="OpenShock.SDK.CSharp" Version="0.0.13" />
<PackageReference Include="OpenShock.SDK.CSharp.Hub" Version="0.0.13" />
<PackageReference Include="OpenShock.SDK.CSharp.Live" Version="0.0.13" />
<PackageReference Include="OpenShock.SDK.CSharp" Version="0.0.15" />
<PackageReference Include="OpenShock.SDK.CSharp.Hub" Version="0.0.15" />
<PackageReference Include="OpenShock.SDK.CSharp.Live" Version="0.0.15" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
Expand All @@ -66,7 +69,7 @@
<PackageReference Include="SmartFormat.NET" Version="3.3.2" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\Icon512.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
7 changes: 7 additions & 0 deletions ShockOsc/Ui/Components/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@
ApiHubClient.Reconnecting += ApiHubClientOnReconnecting;
ApiHubClient.Reconnected += ApiHubClientOnReconnected;
ApiHubClient.Closed += ApiHubClientOnClosed;
ApiHubClient.Connected += AbiHubClientOnConnected;

LiveControlManager.OnStateUpdated += LiveControlManagerOnOnStateUpdated;
}

private Task AbiHubClientOnConnected(string? arg)=> InvokeAsync(() =>
{
StateHasChanged();
Snackbar.Add("Connected to hub", Severity.Warning);
});

private Task LiveControlManagerOnOnStateUpdated()
{
return InvokeAsync(StateHasChanged);
Expand Down
2 changes: 1 addition & 1 deletion ShockOsc/Ui/Components/Parts/DebouncedSlider.razor
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@typeparam T

<MudSlider Size="@Size" Style="@Style" @bind-Value="ValueProp">@ChildContent</MudSlider>
<MudSlider Size="@Size" Class="@Class" Style="@Style" Min="@Min" Max="@Max" Step="@Step" @bind-Value="ValueProp">@ChildContent</MudSlider>
25 changes: 20 additions & 5 deletions ShockOsc/Ui/Components/Parts/DebouncedSlider.razor.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using Size = MudBlazor.Size;

namespace OpenShock.ShockOsc.Ui.Components.Parts;

public partial class DebouncedSlider<T> : ComponentBase
{

private BehaviorSubject<T> _subject = null!;
private BehaviorSubject<T>? _subject;

private T ValueProp
{
get => _subject.Value;
get => _subject!.Value;
set
{
_subject.OnNext(value);
SliderValue = value;
OnValueChanged?.Invoke(value);
}
}

Expand All @@ -31,6 +30,9 @@ protected override void OnInitialized()
[Parameter] public TimeSpan DebounceTime { get; set; } = TimeSpan.FromMilliseconds(500);

[Parameter] public EventCallback<T> SliderValueChanged { get; set; }

[Parameter]
public Action<T>? OnValueChanged { get; set; }

private T _sliderValue = default!;

Expand All @@ -40,7 +42,8 @@ public T SliderValue
get => _sliderValue;
set
{
if(_sliderValue.Equals(value)) return;
_subject?.OnNext(value);
if(_sliderValue != null && _sliderValue.Equals(value)) return;

SliderValueChanged.InvokeAsync(value);
_sliderValue = value!;
Expand All @@ -55,6 +58,18 @@ public T SliderValue
[Parameter]
public string? Style { get; set; }

[Parameter]
public string? Class { get; set; }

[Parameter]
public RenderFragment? ChildContent { get; set; }

[Parameter]
public T? Min { get; set; }

[Parameter]
public T? Max { get; set; }

[Parameter]
public T? Step { get; set; }
}
Loading

0 comments on commit 552f647

Please sign in to comment.