Skip to content

Commit

Permalink
Force renew listener instances on arming mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Jul 8, 2024
1 parent 4ddc31b commit 4a5aff0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Core/Key2Joy.Core/Key2JoyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ public void ArmMappings(MappingProfile profile, bool withExplicitTriggerListener

if (withExplicitTriggerListeners)
{
allListeners.AddRange(new List<AbstractTriggerListener> {
allListeners.AddRange([
// Trigger listeners that should explicitly loaded. This ensures that they're available for scripts
// even if no mapping option is mapped to be triggered by it.
// Always add these listeners so scripts can ask them if stuff has happened.
KeyboardTriggerListener.Instance,
MouseButtonTriggerListener.Instance,
MouseMoveTriggerListener.Instance,
GamePadButtonTriggerListener.Instance,
GamePadStickTriggerListener.Instance,
GamePadTriggerTriggerListener.Instance,
});
KeyboardTriggerListener.NewInstance(),
MouseButtonTriggerListener.NewInstance(),
MouseMoveTriggerListener.NewInstance(),
GamePadButtonTriggerListener.NewInstance(),
GamePadStickTriggerListener.NewInstance(),
GamePadTriggerTriggerListener.NewInstance(),
]);
}

var allActions = (IList<AbstractAction>)profile.MappedOptions.Select(m => m.Action).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Triggers;
using Key2Joy.LowLevelInput.XInput;
using Key2Joy.Mapping.Triggers.Mouse;

namespace Key2Joy.Mapping.Triggers.GamePad;

Expand All @@ -20,6 +21,13 @@ public static GamePadButtonTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static GamePadButtonTriggerListener NewInstance() => instance = new GamePadButtonTriggerListener();

private readonly IXInputService xInputService;
private readonly Dictionary<GamePadButton, bool> currentKeysDown = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public static GamePadStickTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static GamePadStickTriggerListener NewInstance() => instance = new GamePadStickTriggerListener();

private readonly Dictionary<GamePadSide, GamePadStickLookup> stickAxisLookups;
private readonly IXInputService xInputService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public static GamePadTriggerTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static GamePadTriggerTriggerListener NewInstance() => instance = new GamePadTriggerTriggerListener();

private readonly Dictionary<GamePadSide, List<AbstractMappedOption>> triggerLookup;
private readonly IXInputService xInputService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public static KeyboardTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static KeyboardTriggerListener NewInstance() => instance = new KeyboardTriggerListener();

private GlobalInputHook globalKeyboardHook;
private readonly VirtualKeyConverter virtualKeyConverter = new();
private readonly Dictionary<Keys, bool> currentKeysPressed = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Key2Joy.Config;
using Key2Joy.Contracts.Mapping.Triggers;
using Key2Joy.LowLevelInput;
using Key2Joy.Mapping.Triggers.Keyboard;

namespace Key2Joy.Mapping.Triggers.Mouse;

Expand All @@ -20,6 +21,13 @@ public static MouseButtonTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static MouseButtonTriggerListener NewInstance() => instance = new MouseButtonTriggerListener();

private GlobalInputHook globalMouseButtonHook;
private readonly Dictionary<LowLevelInput.Mouse.Buttons, bool> currentButtonsDown = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public static MouseMoveTriggerListener Instance
}
}

/// <summary>
/// Force a new instance to be created, used to reset the listener when mappings
/// are re-armed.
/// </summary>
/// <returns></returns>
public static MouseMoveTriggerListener NewInstance() => instance = new MouseMoveTriggerListener();

private static readonly TimeSpan IS_MOVING_TOLERANCE = TimeSpan.FromMilliseconds(10);

private readonly Dictionary<int, List<AbstractMappedOption>> lookupAxis;
Expand Down

0 comments on commit 4a5aff0

Please sign in to comment.