Skip to content

Commit

Permalink
Add SOCD modes (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles authored Mar 21, 2024
1 parent a5c7857 commit d8859fc
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 43 deletions.
1 change: 1 addition & 0 deletions InputDisplay.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RT/@EntryIndexedValue">RT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UI/@EntryIndexedValue">UI</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Interactable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=SOCD/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tekken/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Binary file modified assets/img/config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/InputDisplay/Config/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public record SelectedTheme(
public bool InvertHistory { get; set; } = false;
public bool FramesAfter { get; set; } = false;
public bool HideButtonRelease { get; set; }
public SOCDMode SOCD { get; set; } = SOCDMode.Neutral;
public bool ShortcutsEnabled { get; set; } = true;
public int IconSize { get; set; } = 40;
public int SpaceBetweenInputs { get; set; } = 2;
Expand Down Expand Up @@ -100,6 +101,7 @@ public void CopyFrom(Settings config)
InputMap = config.InputMap;
DirectionSpace = config.DirectionSpace;
AutoCorrectMultiple = config.AutoCorrectMultiple;
SOCD = config.SOCD;
InvertHistory = config.InvertHistory;
FramesAfter = config.FramesAfter;
EnabledDirections = config.EnabledDirections;
Expand Down
100 changes: 83 additions & 17 deletions src/InputDisplay/Config/Window/SettingsControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ void SetMacroButton(ButtonName name, (int Row, int Col) pos)
}
}


Widget BuildThemes()
{
Grid grid = new()
Expand Down Expand Up @@ -440,11 +439,11 @@ Widget BuildThemes()
{
var newTheme = (string)btnCombo.SelectedItem.Tag;


config.CurrentTheme = config.CurrentTheme with
{
Buttons = newTheme,
};

SaveConfig();
RebuildMacroButtons();
};
Expand All @@ -456,7 +455,7 @@ Widget BuildSettings()
{
Grid grid = new()
{
Margin = new(20),
Margin = new(20, 10, 20, 15),
VerticalAlignment = VerticalAlignment.Center,
ColumnSpacing = 8,
RowSpacing = 4,
Expand Down Expand Up @@ -490,19 +489,12 @@ Widget BuildSettings()
AddCheck(1, 3, "Use Shortcuts", config.ShortcutsEnabled, check => config.ShortcutsEnabled = check);
CreateDirectionsSourceBox(1, 4, "Dir.Sources");

AddNumeric(2, 0, "Input space", config.SpaceBetweenInputs, v => config.SpaceBetweenInputs = v);
AddNumeric(2, 1, "Command space", config.SpaceBetweenCommands, v => config.SpaceBetweenCommands = v);
AddNumeric(2, 2, "Direction space", config.DirectionSpace, v => config.DirectionSpace = v);
AddNumeric(2, 3, "Icon size", config.IconSize, v => config.IconSize = v);

var backgroundColor = ColorPicker("Background color", config.ClearColor, c =>
{
config.ClearColor = c;
SaveConfig();
});
Grid.SetColumn(backgroundColor, 2);
Grid.SetRow(backgroundColor, 4);
grid.Widgets.Add(backgroundColor);
AddEnumCombo(2, 0, "SOCD", config.SOCD, item => config.SOCD = item);
AddNumeric(2, 1, "Input space", config.SpaceBetweenInputs, v => config.SpaceBetweenInputs = v);
AddNumeric(2, 2, "Command space", config.SpaceBetweenCommands, v => config.SpaceBetweenCommands = v);
AddNumeric(2, 3, "Direction space", config.DirectionSpace, v => config.DirectionSpace = v);
AddNumeric(2, 4, "Icon size", config.IconSize, v => config.IconSize = v);
AddColorPicker(2, 5, "Background color", config.ClearColor, c => config.ClearColor = c);

return grid;

Expand Down Expand Up @@ -563,6 +555,80 @@ void AddNumeric(int col, int row, string labelText, int value, Action<int> onCha
Grid.SetRow(chk, row);
grid.Widgets.Add(chk);
}

void AddEnumCombo<T>(int col, int row, string labelText, T value, Action<T> onChange) where T : struct, Enum
{
var panel = EnumComboView(value, labelText, onChange);
Grid.SetColumn(panel, col);
Grid.SetRow(panel, row);
grid.Widgets.Add(panel);
}

void AddColorPicker(int col, int row, string labelText, Color value, Action<Color> onChange)
{
var backgroundColor = ColorPicker(labelText.ToFieldLabel(), value, newColor =>
{
onChange(newColor);
SaveConfig();
});
Grid.SetColumn(backgroundColor, col);
Grid.SetRow(backgroundColor, row);

grid.Widgets.Add(backgroundColor);
}
}

HorizontalStackPanel EnumComboView<T>(T value, string labelText, Action<T> onChange) where T : struct, Enum
{
ComboView enumCombo = new()
{
VerticalAlignment = VerticalAlignment.Center,
};

var enums = Enum.GetValues<T>();
for (var index = 0; index < enums.Length; index++)
{
var enumValue = enums[index];

enumCombo.Widgets.Add(
new Label
{
Text = enumValue.ToString(),
Tag = enumValue,
VerticalAlignment = VerticalAlignment.Center,
Margin = new(5, 0),
}
);

if (enumValue.Equals(value))
enumCombo.SelectedIndex = index;
}

HorizontalStackPanel panel = new()
{
Padding = new(4),
};

Label label = new()
{
Text = labelText.ToFieldLabel(),
VerticalAlignment = VerticalAlignment.Center,
Margin = new(0, 0, 5, 0),
};

panel.Widgets.Add(label);

enumCombo.SelectedIndexChanged += (_, _) =>
{
if (enumCombo is not { SelectedItem.Tag: T newValue })
return;

onChange(newValue);
SaveConfig();
};

panel.Widgets.Add(enumCombo);
return panel;
}

CheckButton InputCheck(string labelText, bool isChecked, Action<bool> onClick)
Expand Down Expand Up @@ -672,7 +738,7 @@ HorizontalStackPanel ColorPicker(string labelText, Color color, Action<Color> on
Color = current,
};

dialog.Closed += (s, a) =>
dialog.Closed += (_, _) =>
{
if (!dialog.Result) return;
image.Tag = dialog.Color;
Expand Down
2 changes: 1 addition & 1 deletion src/InputDisplay/Config/Window/SettingsGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected override void Update(GameTime gameTime)
}

player.Update();
gameInput.Update(player, Config.InputMap, Config.EnabledDirections);
gameInput.Update(player, Config);

if (controls.MappingButton is null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/InputDisplay/Inputs/Drawable/InputBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class InputBuffer(Settings config)

public void Update(PlayerPad pad)
{
gameInput.Update(pad, config.InputMap, config.EnabledDirections);
gameInput.Update(pad, config);
var controllerState = gameInput.CurrentState;

if (Last is { } last)
Expand Down
46 changes: 36 additions & 10 deletions src/InputDisplay/Inputs/Drawable/InputEntry.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using InputDisplay.Config;
using InputDisplay.Themes;

Expand All @@ -13,10 +14,11 @@ public class InputEntry

public void IncrementFrame() => HoldingFrames = Math.Min(HoldingFrames + 1, MaxHoldingFrames);

readonly HashSet<ButtonName> holding = new();
readonly HashSet<ButtonName> pressed = new();
readonly HashSet<ButtonName> fallback = new();
readonly SortedSet<ButtonName> currentButtons = new();
readonly HashSet<ButtonName> holding = [];
readonly HashSet<ButtonName> pressed = [];
readonly HashSet<ButtonName> fallback = [];
readonly SortedSet<ButtonName> currentButtons = [];


public void Draw(
Settings config,
Expand Down Expand Up @@ -66,15 +68,29 @@ void DrawDirection()
return;
}

var step = commandDir * (config.IconSize + config.SpaceBetweenInputs + config.DirectionSpace);
var step = commandDir * (config.IconSize + config.SpaceBetweenInputs);

if (
(
(config.ShowNeutralIcon && State.Stick.Direction is Direction.Neutral)
|| State.Stick.Direction is not Direction.Neutral
)
&& theme.GetTexture(State.Stick.Direction) is { } dirTexture
(config.ShowNeutralIcon && State.Stick.Direction is Direction.Neutral)
|| State.Stick.Direction is not Direction.Neutral
)
{
if (SOCD.IsSingle(config.SOCD, State.Stick.Direction))
DrawDirectionComponent(State.Stick.Direction);
else
{
foreach (var stickDir in SOCD.GetComponents(config.SOCD, State.Stick))
DrawDirectionComponent(stickDir);
}

offset += commandDir * config.DirectionSpace;
}

void DrawDirectionComponent(Direction stickDirection)
{
if (theme.GetTexture(stickDirection) is not { } dirTexture)
return;

var dirColor = State.Stick.Holding && config.ShadowHolding ? Color.LightGray : Color.White;
Rectangle dirRect = new(
(int)offset.X, (int)offset.Y,
Expand Down Expand Up @@ -181,3 +197,13 @@ void CheckButton(GameInput.Button button, ButtonName name)
}
}
}

sealed class TimeEntry<T>(T value) : IComparable<TimeEntry<T>>
{
public T Value { get; } = value;
public long Time { get; set; }

public void Update() => Time = Stopwatch.GetTimestamp();

public int CompareTo(TimeEntry<T>? other) => Time.CompareTo(other?.Time);
}
22 changes: 13 additions & 9 deletions src/InputDisplay/Inputs/GameInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public readonly record struct Button(ButtonStatus Status)
);
}

public record struct Stick(Direction Direction, bool Holding);
public record struct Stick(
Direction Direction,
Direction Raw,
bool Holding
);

public record struct State
{
Expand Down Expand Up @@ -169,11 +173,12 @@ void UpdateButton(GamePadState state, Buttons padButton, ref Button button)
}
}

public void UpdateDirections(GamePadState state, Settings.DirectionSources sources)
public void UpdateDirections(GamePadState state, SOCDMode socd, Settings.DirectionSources sources)
{
if (sources is Settings.DirectionSources.None)
{
currentState.Stick.Direction = Direction.Neutral;
currentState.Stick.Raw = Direction.Neutral;
currentState.Stick.Holding = false;
return;
}
Expand Down Expand Up @@ -209,21 +214,20 @@ public void UpdateDirections(GamePadState state, Settings.DirectionSources sourc
state.IsButtonDown(Buttons.RightThumbstickRight)))
direction |= Direction.Forward;

currentState.Stick.Holding = currentState.Stick.Direction == direction
&& direction != Direction.Neutral;
currentState.Stick.Direction = direction;
currentState.Stick.Holding = currentState.Stick.Raw == direction && direction != Direction.Neutral;
currentState.Stick.Direction = SOCD.Clean(socd, direction, currentState.Stick);
currentState.Stick.Raw = direction;
}

public void Update(
PlayerPad pad,
InputMap mapper,
Settings.DirectionSources directionSources = Settings.DirectionSources.Default
Settings config
)
{
var state = pad.State;
UpdateDirections(state, directionSources);
UpdateDirections(state, config.SOCD, config.EnabledDirections);

var map = mapper.GetMappingOrDefault(pad.Identifier);
var map = config.InputMap.GetMappingOrDefault(pad.Identifier);
UpdateButton(state, map.LP, ref currentState.LP);
UpdateButton(state, map.MP, ref currentState.MP);
UpdateButton(state, map.HP, ref currentState.HP);
Expand Down
Loading

0 comments on commit d8859fc

Please sign in to comment.