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

luttje / Key2Joy / 6598306412

21 Oct 2023 03:54PM UTC coverage: 45.09% (-7.4%) from 52.519%
6598306412

Pull #50

github

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

751 of 2289 branches covered (0.0%)

Branch coverage included in aggregate %.

2384 of 2384 new or added lines in 80 files covered. (100.0%)

3845 of 7904 relevant lines covered (48.65%)

15964.61 hits per line

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

5.81
/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.Actions;
7
using Key2Joy.Contracts.Mapping.Triggers;
8
using Key2Joy.LowLevelInput;
9
using Key2Joy.LowLevelInput.SimulatedGamePad;
10
using SimWinInput;
11

12
namespace Key2Joy.Mapping.Actions.Input;
13

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

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

30
    public static List<MappedOption> GetAllButtonActions(PressState pressState)
31
    {
32
        var actionFactory = ActionsRepository.GetAction(typeof(GamePadButtonAction));
×
33

34
        List<MappedOption> actions = new();
×
35
        foreach (var control in GetAllButtons())
×
36
        {
37
            var action = (GamePadButtonAction)MakeAction(actionFactory);
×
38
            action.Control = control;
×
39
            action.PressState = pressState;
×
40

41
            actions.Add(new MappedOption
×
42
            {
×
43
                Action = action
×
44
            });
×
45
        }
46
        return actions;
×
47
    }
48

49
    public static GamePadControl[] GetAllButtons()
50
    {
51
        var allEnums = Enum.GetValues(typeof(GamePadControl));
×
52

53
        var skip = new GamePadControl[]
×
54
        {
×
55
            GamePadControl.None,
×
56

×
57
            // Handled separately in GamePadTriggerAction
×
58
            GamePadControl.LeftTrigger,
×
59
            GamePadControl.RightTrigger,
×
60

×
61
            // Handled separately in GamePadStickAction
×
62
            GamePadControl.LeftStickLeft,
×
63
            GamePadControl.LeftStickRight,
×
64
            GamePadControl.LeftStickUp,
×
65
            GamePadControl.LeftStickDown,
×
66
            GamePadControl.RightStickLeft,
×
67
            GamePadControl.RightStickRight,
×
68
            GamePadControl.RightStickUp,
×
69
            GamePadControl.RightStickDown,
×
70
        };
×
71

72
        var buttons = allEnums
×
73
            .Cast<GamePadControl>()
×
74
            .Where(x => !skip.Contains(x))
×
75
            .ToArray();
×
76

77
        return buttons;
×
78
    }
79

80
    public override void OnStartListening(AbstractTriggerListener listener, ref IList<AbstractAction> otherActions)
81
    {
82
        base.OnStartListening(listener, ref otherActions);
×
83

84
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
85
        gamePadService.EnsurePluggedIn(this.GamePadIndex);
×
86
    }
×
87

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

117
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
118
        var gamePad = gamePadService.GetGamePad(this.GamePadIndex);
×
119

120
        if (!gamePad.GetIsPluggedIn())
×
121
        {
122
            gamePad.PlugIn();
×
123
        }
124

125
        if (this.PressState == PressState.Press)
×
126
        {
127
            gamePad.SetControl(this.Control);
×
128
        }
129

130
        if (this.PressState == PressState.Release)
×
131
        {
132
            gamePad.ReleaseControl(this.Control);
×
133
        }
134
    }
×
135

136
    public override async Task Execute(AbstractInputBag inputBag = null)
137
    {
138
        var gamePadService = ServiceLocator.Current.GetInstance<ISimulatedGamePadService>();
×
139
        var gamePad = gamePadService.GetGamePad(this.GamePadIndex);
×
140

141
        if (this.PressState == PressState.Press)
×
142
        {
143
            gamePad.SetControl(this.Control);
×
144
        }
145
        else if (this.PressState == PressState.Release)
×
146
        {
147
            gamePad.ReleaseControl(this.Control);
×
148
        }
149
    }
×
150

151
    public override string GetNameDisplay() => this.Name.Replace("{0}", this.Control.ToString())
×
152
            .Replace("{1}", Enum.GetName(typeof(PressState), this.PressState))
×
153
            .Replace("{2}", this.GamePadIndex.ToString());
×
154

155
    public override bool Equals(object obj)
156
    {
157
        if (obj is not GamePadButtonAction action)
×
158
        {
159
            return false;
×
160
        }
161

162
        return action.Control == this.Control
×
163
            && action.PressState == this.PressState;
×
164
    }
165
}
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