Skip to content

Commit cab0cf9

Browse files
committed
Adds interfaces for input-providers for better testing
1 parent 27e3a65 commit cab0cf9

17 files changed

+462
-33
lines changed

InputStateManager/InputManager.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,38 @@
2626
// ***************************************************************************
2727

2828
using InputStateManager.Inputs;
29+
using InputStateManager.Inputs.InputProviders.Implementations;
30+
using InputStateManager.Inputs.InputProviders.Interfaces;
2931
using JetBrains.Annotations;
3032

3133
namespace InputStateManager
3234
{
3335
[PublicAPI]
3436
public class InputManager
3537
{
36-
public Mouse Mouse { get; } = new Mouse();
37-
public Pad Pad { get; } = new Pad();
38-
public Key Key { get; } = new Key();
39-
public Touch Touch { get; } = new Touch();
38+
public Key Key { get; }
39+
public Mouse Mouse { get; }
40+
public Pad Pad { get; }
41+
public Touch Touch { get; }
42+
43+
public InputManager()
44+
{
45+
Key = new Key(new XnaKeyInputProvider());
46+
Mouse = new Mouse(new XnaMouseInputProvider());
47+
Pad = new Pad(new XnaPadInputProvider());
48+
Touch = new Touch(new XnaTouchInputProvider());
49+
}
50+
51+
/// <summary>
52+
/// Constructor that lets you inject input-providers for testing purposes.
53+
/// </summary>
54+
public InputManager(IKeyInputProvider keyInputProvider, IMouseInputProvider mouseInputProvider, IPadInputProvider padInputProvider, ITouchInputProvider touchInputProvider)
55+
{
56+
Key = new Key(keyInputProvider);
57+
Mouse = new Mouse(mouseInputProvider);
58+
Pad = new Pad(padInputProvider);
59+
Touch = new Touch(touchInputProvider);
60+
}
4061

4162
public void Update()
4263
{

InputStateManager/InputStateManager.csproj

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
<AppDesignerFolder>Properties</AppDesignerFolder>
1111
<RootNamespace>InputStateManager</RootNamespace>
1212
<AssemblyName>InputStateManager</AssemblyName>
13-
14-
<DefaultLanguage>en-US</DefaultLanguage>
13+
<DefaultLanguage>en-US</DefaultLanguage>
1514
<FileAlignment>512</FileAlignment>
16-
17-
18-
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
1916
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
2017
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
2118
</PropertyGroup>
@@ -48,6 +45,14 @@
4845
</ItemGroup>
4946
<ItemGroup>
5047
<Compile Include="Inputs\GamePad\GamePadStates.cs" />
48+
<Compile Include="Inputs\InputProviders\Implementations\XnaPadInputProvider.cs" />
49+
<Compile Include="Inputs\InputProviders\Implementations\XnaTouchInputProvider.cs" />
50+
<Compile Include="Inputs\InputProviders\Implementations\XnaMouseInputProvider.cs" />
51+
<Compile Include="Inputs\InputProviders\Implementations\XnaKeyInputProvider.cs" />
52+
<Compile Include="Inputs\InputProviders\Interfaces\ITouchInputProvider.cs" />
53+
<Compile Include="Inputs\InputProviders\Interfaces\IPadInputProvider.cs" />
54+
<Compile Include="Inputs\InputProviders\Interfaces\IKeyInputProvider.cs" />
55+
<Compile Include="Inputs\InputProviders\Interfaces\IMouseInputProvider.cs" />
5156
<Compile Include="Inputs\Pad.cs" />
5257
<Compile Include="InputManager.cs" />
5358
<Compile Include="Inputs\Key.cs" />
@@ -58,6 +63,7 @@
5863
<ItemGroup>
5964
<None Include="packages.config" />
6065
</ItemGroup>
66+
<ItemGroup />
6167
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
6268
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6369
Other similar extension points exist, see Microsoft.Common.targets.

InputStateManager/Inputs/GamePad/GamePadStates.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@
2525
// For more information, please refer to <http://unlicense.org>
2626
// ***************************************************************************
2727

28+
using InputStateManager.Inputs.InputProviders.Interfaces;
2829
using JetBrains.Annotations;
2930
using Microsoft.Xna.Framework;
3031
using Microsoft.Xna.Framework.Input;
3132

3233
namespace InputStateManager.Inputs.GamePad
3334
{
3435
[PublicAPI]
35-
class GamePadStates
36+
internal class GamePadStates
3637
{
38+
private IPadInputProvider provider;
3739
private GamePadState[] OldStates { get; set; } = new GamePadState[4];
3840
private GamePadState[] States { get; set; } = new GamePadState[4];
3941

42+
public GamePadStates(IPadInputProvider provider)
43+
{
44+
this.provider = provider;
45+
}
46+
4047
public GamePadState Get(PlayerIndex playerIndex = PlayerIndex.One)
4148
{
4249
return States[(int) playerIndex];
@@ -53,7 +60,7 @@ public void Update()
5360
OldStates[i] = States[i];
5461

5562
for (int i = 0; i < States.Length; i++)
56-
States[i] = Microsoft.Xna.Framework.Input.GamePad.GetState(i);
63+
States[i] = provider.GetState(i);
5764
}
5865
}
5966
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using InputStateManager.Inputs.InputProviders.Interfaces;
29+
using Microsoft.Xna.Framework.Input;
30+
31+
namespace InputStateManager.Inputs.InputProviders.Implementations
32+
{
33+
internal class XnaKeyInputProvider : IKeyInputProvider
34+
{
35+
public KeyboardState GetState()
36+
{
37+
return Keyboard.GetState();
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using InputStateManager.Inputs.InputProviders.Interfaces;
29+
using Microsoft.Xna.Framework.Input;
30+
31+
namespace InputStateManager.Inputs.InputProviders.Implementations
32+
{
33+
internal class XnaMouseInputProvider : IMouseInputProvider
34+
{
35+
public MouseState GetState()
36+
{
37+
return Microsoft.Xna.Framework.Input.Mouse.GetState();
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using InputStateManager.Inputs.InputProviders.Interfaces;
29+
using Microsoft.Xna.Framework.Input;
30+
31+
namespace InputStateManager.Inputs.InputProviders.Implementations
32+
{
33+
internal class XnaPadInputProvider : IPadInputProvider
34+
{
35+
public GamePadState GetState(int index)
36+
{
37+
return Microsoft.Xna.Framework.Input.GamePad.GetState(index);
38+
}
39+
}
40+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using System;
29+
using InputStateManager.Inputs.InputProviders.Interfaces;
30+
using Microsoft.Xna.Framework;
31+
using Microsoft.Xna.Framework.Input.Touch;
32+
33+
namespace InputStateManager.Inputs.InputProviders.Implementations
34+
{
35+
internal class XnaTouchInputProvider : ITouchInputProvider
36+
{
37+
public TouchCollection GetState()
38+
{
39+
return TouchPanel.GetState();
40+
}
41+
42+
public int GetDisplayHeight()
43+
{
44+
return TouchPanel.DisplayHeight;
45+
}
46+
47+
public int GetDisplayWidth()
48+
{
49+
return TouchPanel.DisplayWidth;
50+
}
51+
52+
public DisplayOrientation GetDisplayOrientation()
53+
{
54+
return TouchPanel.DisplayOrientation;
55+
}
56+
57+
public bool GetIsGestureAvailable()
58+
{
59+
return TouchPanel.IsGestureAvailable;
60+
}
61+
62+
public bool GetEnableMouseGestures()
63+
{
64+
return TouchPanel.EnableMouseGestures;
65+
}
66+
67+
public bool GetEnableMouseTouchPoint()
68+
{
69+
return TouchPanel.EnableMouseTouchPoint;
70+
}
71+
72+
public GestureType GetEnabledGestures()
73+
{
74+
return TouchPanel.EnabledGestures;
75+
}
76+
77+
public IntPtr GetWindowHandle()
78+
{
79+
return TouchPanel.WindowHandle;
80+
}
81+
82+
public TouchPanelCapabilities GetCapabilities()
83+
{
84+
return TouchPanel.GetCapabilities();
85+
}
86+
87+
public GestureSample ReadGesture()
88+
{
89+
return TouchPanel.ReadGesture();
90+
}
91+
}
92+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ***************************************************************************
2+
// This is free and unencumbered software released into the public domain.
3+
//
4+
// Anyone is free to copy, modify, publish, use, compile, sell, or
5+
// distribute this software, either in source code form or as a compiled
6+
// binary, for any purpose, commercial or non-commercial, and by any
7+
// means.
8+
//
9+
// In jurisdictions that recognize copyright laws, the author or authors
10+
// of this software dedicate any and all copyright interest in the
11+
// software to the public domain. We make this dedication for the benefit
12+
// of the public at large and to the detriment of our heirs and
13+
// successors. We intend this dedication to be an overt act of
14+
// relinquishment in perpetuity of all present and future rights to this
15+
// software under copyright law.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
// For more information, please refer to <http://unlicense.org>
26+
// ***************************************************************************
27+
28+
using JetBrains.Annotations;
29+
using Microsoft.Xna.Framework.Input;
30+
31+
namespace InputStateManager.Inputs.InputProviders.Interfaces
32+
{
33+
[PublicAPI]
34+
public interface IKeyInputProvider
35+
{
36+
KeyboardState GetState();
37+
}
38+
}

0 commit comments

Comments
 (0)