• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6599541470

21 Oct 2023 08:35PM UTC coverage: 44.926% (-7.6%) from 52.519%
6599541470

Pull #50

github

web-flow
Merge 3a933762e into 14b7ce9a7
Pull Request #50: Add XInput in preparation for gamepad triggers + add xmldoc

760 of 2323 branches covered (0.0%)

Branch coverage included in aggregate %.

2833 of 2833 new or added lines in 98 files covered. (100.0%)

3888 of 8023 relevant lines covered (48.46%)

22519.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.75
/Core/Key2Joy.Core/Mapping/Actions/Input/GamePadButtonAction.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Threading.Tasks;
5
using CommonServiceLocator;
6
using Key2Joy.Contracts.Mapping;
7
using Key2Joy.Contracts.Mapping.Actions;
8
using Key2Joy.Contracts.Mapping.Triggers;
9
using Key2Joy.LowLevelInput;
10
using Key2Joy.LowLevelInput.SimulatedGamePad;
11
using SimWinInput;
12

13
namespace Key2Joy.Mapping.Actions.Input;
14

15
[Action(
16
    Description = "GamePad/Controller Simulation",
17
    NameFormat = "{1} {0} on GamePad #{2}",
18
    GroupName = "GamePad Simulation",
19
    GroupImage = "joystick"
20
)]
21
public class GamePadButtonAction : CoreAction, IPressState, IProvideReverseAspect
22
{
23
    public GamePadControl Control { get; set; }
56✔
24
    public PressState PressState { get; set; }
56✔
25
    public int GamePadIndex { get; set; }
56✔
26

27
    public GamePadButtonAction(string name)
28
        : base(name)
63✔
29
    { }
63✔
30

31
    /// <inheritdoc/>
32
    public void MakeReverse(AbstractMappingAspect aspect)
33
        => CommonReverseAspect.MakeReversePressState(this, aspect);
×
34

35
    public static List<MappedOption> GetAllButtonActions(PressState pressState)
36
    {
37
        var actionFactory = ActionsRepository.GetAction(typeof(GamePadButtonAction));
×
38

39
        List<MappedOption> actions = new();
×
40
        foreach (var control in GetAllButtons())
×
41
        {
42
            var action = (GamePadButtonAction)MakeAction(actionFactory);
×
43
            action.Control = control;
×
44
            action.PressState = pressState;
×
45

46
            actions.Add(new MappedOption
×
47
            {
×
48
                Action = action
×
49
            });
×
50
        }
51
        return actions;
×
52
    }
53

54
    public static GamePadControl[] GetAllButtons()
55
    {
56
        var allEnums = Enum.GetValues(typeof(GamePadControl));
×
57

58
        var skip = new GamePadControl[]
×
59
        {
×
60
            GamePadControl.None,
×
61

×
62
            // Handled separately in GamePadTriggerAction
×
63
            GamePadControl.LeftTrigger,
×
64
            GamePadControl.RightTrigger,
×
65

×
66
            // Handled separately in GamePadStickAction
×
67
            GamePadControl.LeftStickLeft,
×
68
            GamePadControl.LeftStickRight,
×
69
            GamePadControl.LeftStickUp,
×
70
            GamePadControl.LeftStickDown,
×
71
            GamePadControl.RightStickLeft,
×
72
            GamePadControl.RightStickRight,
×
73
            GamePadControl.RightStickUp,
×
74
            GamePadControl.RightStickDown,
×
75
        };
×
76

77
        var buttons = allEnums
×
78
            .Cast<GamePadControl>()
×
79
            .Where(x => !skip.Contains(x))
×
80
            .ToArray();
×
81

82
        return buttons;
×
83
    }
84

85
    public override void OnStartListening(AbstractTriggerListener listener, ref IList<AbstractAction> otherActions)
86
    {
87
        base.OnStartListening(listener, ref otherActions);
×
88

89
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
90
        gamePadService.EnsurePluggedIn(this.GamePadIndex);
×
91
    }
×
92

93
    /// <markdown-doc>
94
    /// <parent-name>Input</parent-name>
95
    /// <path>Api/Input</path>
96
    /// </markdown-doc>
97
    /// <summary>
98
    /// Simulate pressing or releasing (or both) gamepad buttons.
99
    /// </summary>
100
    /// <markdown-example>
101
    /// Shows how to press "A" on the gamepad for 500ms, then release it.
102
    /// <code language="js">
103
    /// <![CDATA[
104
    /// GamePad.Simulate(GamePadControl.A, PressState.Press);
105
    /// setTimeout(function () {
106
    ///     GamePad.Simulate(GamePadControl.A, PressState.Release);
107
    /// }, 500);
108
    /// ]]>
109
    /// </code>
110
    /// </markdown-example>
111
    /// <param name="control">Button to simulate</param>
112
    /// <param name="pressState">Action to simulate</param>
113
    /// <param name="gamepadIndex">Which of 4 possible gamepads to simulate: 0 (default), 1, 2 or 3</param>
114
    /// <name>GamePad.Simulate</name>
115
    [ExposesScriptingMethod("GamePad.Simulate")]
116
    public void ExecuteForScript(GamePadControl control, PressState pressState, int gamepadIndex = 0)
117
    {
118
        this.Control = control;
×
119
        this.PressState = pressState;
×
120
        this.GamePadIndex = gamepadIndex;
×
121

122
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
123
        var gamePad = gamePadService.GetGamePad(this.GamePadIndex);
×
124

125
        if (!gamePad.GetIsPluggedIn())
×
126
        {
127
            gamePad.PlugIn();
×
128
        }
129

130
        if (this.PressState == PressState.Press)
×
131
        {
132
            gamePad.SetControl(this.Control);
×
133
        }
134

135
        if (this.PressState == PressState.Release)
×
136
        {
137
            gamePad.ReleaseControl(this.Control);
×
138
        }
139
    }
×
140

141
    public override async Task Execute(AbstractInputBag inputBag = null)
142
    {
143
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
144
        var gamePad = gamePadService.GetGamePad(this.GamePadIndex);
×
145

146
        if (this.PressState == PressState.Press)
×
147
        {
148
            gamePad.SetControl(this.Control);
×
149
        }
150
        else if (this.PressState == PressState.Release)
×
151
        {
152
            gamePad.ReleaseControl(this.Control);
×
153
        }
154
    }
×
155

156
    public override string GetNameDisplay() => this.Name.Replace("{0}", this.Control.ToString())
×
157
            .Replace("{1}", Enum.GetName(typeof(PressState), this.PressState))
×
158
            .Replace("{2}", this.GamePadIndex.ToString());
×
159

160
    public override bool Equals(object obj)
161
    {
162
        if (obj is not GamePadButtonAction action)
×
163
        {
164
            return false;
×
165
        }
166

167
        return action.Control == this.Control
×
168
            && action.PressState == this.PressState;
×
169
    }
170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc