Skip to content

Commit 86401b2

Browse files
authored
Refactor to make accelerometer and compass classes available in cross-platform apps. (MonoGame#8663)
Fixes MonoGame#8664 ### Description of Change Moved the SensorData classes up to the top level (still don't like that the namespaces are `Microsoft.Devices.*`), then refactored the Platform classes to remove the iOS IFDEF, following more consistent patterns in the codebase. Then added the classes to the non supported classes (the throw `PlatformNotSupported` exceptions if called), but this now allows game devs to use Accelerometer and Compass classes in a more cross-platform way. Since Microsoft has pulled the WinPhone, in MG 4, we should consider renaming the namespace to `MonoGame.Devices.*`, IMHO.
1 parent 352dda3 commit 86401b2

25 files changed

+453
-151
lines changed

MonoGame.Framework/MonoGame.Framework.Android.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Compile Remove="Platform\**\*" />
2424
<Compile Remove="Properties\**\*" />
2525
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
26+
<Compile Remove="MonoGame\**\*" />
2627
</ItemGroup>
2728

2829
<ItemGroup>
@@ -85,5 +86,5 @@
8586

8687
<Import Project="Platform\OpenGL.targets" />
8788
<Import Project="Platform\OpenAL.targets" />
88-
<Import Project="Platform\Microsoft.Devices.Sensors.targets" />
89+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
8990
</Project>

MonoGame.Framework/MonoGame.Framework.ConsoleCheck.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Remove="Platform\**\*" />
1717
<Compile Remove="Properties\**\*" />
1818
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
19+
<Compile Remove="MonoGame\**\*" />
1920
</ItemGroup>
2021

2122
<ItemGroup>
@@ -60,6 +61,8 @@
6061
<Compile Include="Platform\Windows\WinFormsGameForm.cs" />
6162
<Compile Include="Platform\Windows\WinFormsGamePlatform.cs" />
6263
<Compile Include="Platform\Windows\WinFormsGameWindow.cs" />
64+
<Compile Include="Platform\Desktop\Devices\Sensors\Accelerometer.cs" />
65+
<Compile Include="Platform\Desktop\Devices\Sensors\Compass.cs" />
6366
</ItemGroup>
6467

6568
<ItemGroup>
@@ -91,4 +94,5 @@
9194

9295
<Import Project="Platform\DirectX.targets" />
9396
<Import Project="Platform\XAudio.targets" />
97+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
9498
</Project>

MonoGame.Framework/MonoGame.Framework.DesktopGL.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Compile Remove="Platform\**\*" />
3030
<Compile Remove="Properties\**\*" />
3131
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
32+
<Compile Remove="MonoGame\**\*" />
3233
</ItemGroup>
3334

3435
<ItemGroup>
@@ -75,6 +76,8 @@
7576
<Compile Include="Platform\Graphics\GraphicsAdapter.Legacy.cs" />
7677
<Compile Include="Platform\GamePlatform.Desktop.cs" />
7778
<Compile Include="Platform\GraphicsDeviceManager.SDL.cs" />
79+
<Compile Include="Platform\Desktop\Devices\Sensors\Accelerometer.cs" />
80+
<Compile Include="Platform\Desktop\Devices\Sensors\Compass.cs" />
7881

7982
<Compile Include="..\ThirdParty\StbImageSharp\src\**\*.cs" LinkBase="Utilities\StbImageSharp" />
8083
<Compile Include="..\ThirdParty\StbImageWriteSharp\src\**\*.cs" LinkBase="Utilities\StbImageWriteSharp" />
@@ -92,4 +95,5 @@
9295

9396
<Import Project="Platform\OpenGL.targets" />
9497
<Import Project="Platform\OpenAL.targets" />
98+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
9599
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project>
2+
<ItemGroup>
3+
<Compile Include="MonoGame\Framework\Devices\Sensors\AccelerometerFailedException.cs" />
4+
<Compile Include="MonoGame\Framework\Devices\Sensors\AccelerometerReading.cs" />
5+
<Compile Include="MonoGame\Framework\Devices\Sensors\CalibrationEventArgs.cs" />
6+
<Compile Include="MonoGame\Framework\Devices\Sensors\CompassReading.cs" />
7+
<Compile Include="MonoGame\Framework\Devices\Sensors\ISensorReading.cs" />
8+
<Compile Include="MonoGame\Framework\Devices\Sensors\SensorBase.cs" />
9+
<Compile Include="MonoGame\Framework\Devices\Sensors\SensorFailedException.cs" />
10+
<Compile Include="MonoGame\Framework\Devices\Sensors\SensorReadingEventArgs.cs" />
11+
<Compile Include="MonoGame\Framework\Devices\Sensors\SensorState.cs" />
12+
<Compile Include="MonoGame\Framework\Devices\Sensors\Accelerometer.cs" />
13+
<Compile Include="MonoGame\Framework\Devices\Sensors\Compass.cs" />
14+
</ItemGroup>
15+
</Project>

MonoGame.Framework/MonoGame.Framework.Native.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
<Compile Remove="Properties\**\*" />
2222
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
2323
<Compile Include="Platform\Native\**\*" />
24-
<Compile Include="Platform\Utilities\ReflectionHelpers.Default.cs" />
24+
<Compile Include="Platform\Utilities\ReflectionHelpers.Default.cs" />
25+
<Compile Remove="MonoGame\**\*" />
2526
</ItemGroup>
2627

2728
<ItemGroup>
@@ -42,10 +43,13 @@
4243
<Compile Include="Platform\Input\InputKeyEventArgs.cs" />
4344
<Compile Include="Platform\Utilities\AssemblyHelper.cs" />
4445
<Compile Include="Platform\Threading.cs" />
46+
<Compile Include="Platform\Desktop\Devices\Sensors\Accelerometer.cs" />
47+
<Compile Include="Platform\Desktop\Devices\Sensors\Compass.cs" />
4548
</ItemGroup>
4649

4750
<ItemGroup>
4851
<Folder Include="Platform\Graphics\Vertices\" />
4952
</ItemGroup>
50-
53+
54+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
5155
</Project>

MonoGame.Framework/MonoGame.Framework.WindowsDX.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Compile Remove="Platform\**\*" />
2020
<Compile Remove="Properties\**\*" />
2121
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
22+
<Compile Remove="MonoGame\**\*" />
2223
</ItemGroup>
2324

2425
<ItemGroup>
@@ -63,6 +64,8 @@
6364
<Compile Include="Platform\Windows\WinFormsGameForm.cs" />
6465
<Compile Include="Platform\Windows\WinFormsGamePlatform.cs" />
6566
<Compile Include="Platform\Windows\WinFormsGameWindow.cs" />
67+
<Compile Include="Platform\Desktop\Devices\Sensors\Accelerometer.cs" />
68+
<Compile Include="Platform\Desktop\Devices\Sensors\Compass.cs" />
6669
</ItemGroup>
6770

6871
<ItemGroup>
@@ -94,4 +97,5 @@
9497

9598
<Import Project="Platform\DirectX.targets" />
9699
<Import Project="Platform\XAudio.targets" />
100+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
97101
</Project>

MonoGame.Framework/MonoGame.Framework.iOS.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<IsAotCompatible>true</IsAotCompatible>
1414
</PropertyGroup>
1515

16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<CreatePackage>false</CreatePackage>
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
20+
<CreatePackage>false</CreatePackage>
21+
</PropertyGroup>
1622
<ItemGroup>
1723
<PackageReference Include="MonoGame.Library.OpenAL" Version="1.23.1.10" />
1824
</ItemGroup>
@@ -23,6 +29,7 @@
2329
<Compile Remove="Platform\**\*" />
2430
<Compile Remove="Properties\**\*" />
2531
<Compile Remove="Utilities\System.Numerics.Vectors\**\*" />
32+
<Compile Remove="MonoGame\**\*" />
2633
</ItemGroup>
2734

2835
<ItemGroup>
@@ -80,5 +87,5 @@
8087

8188
<Import Project="Platform\OpenGL.targets" />
8289
<Import Project="Platform\OpenAL.targets" />
83-
<Import Project="Platform\Microsoft.Devices.Sensors.targets" />
90+
<Import Project="MonoGame.Framework.Devices.Sensors.targets" />
8491
</Project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// MonoGame - Copyright (C) MonoGame Foundation, Inc
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE.txt', which is part of this source code package.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using Microsoft.Xna.Framework;
8+
9+
namespace MonoGame.Framework.Devices.Sensors
10+
{
11+
/// <summary>
12+
/// Provides Desktop applications access to the device's accelerometer sensor.
13+
/// Currently stubs, for cross-platform development.
14+
/// </summary>
15+
public sealed partial class Accelerometer : SensorBase<AccelerometerReading>
16+
{
17+
/// <summary>
18+
/// Gets or sets whether the device on which the application is running supports the accelerometer sensor.
19+
/// </summary>
20+
public static bool IsSupported
21+
{
22+
get
23+
{
24+
return PlatformIsSupported();
25+
}
26+
}
27+
28+
/// <summary>
29+
/// Gets the current state of the accelerometer. The value is a member of the SensorState enumeration.
30+
/// </summary>
31+
public SensorState State
32+
{
33+
get
34+
{
35+
return PlatformSensorState();
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Creates a new instance of the Accelerometer object.
41+
/// </summary>
42+
public Accelerometer()
43+
{
44+
PlatformAccelerometer();
45+
}
46+
47+
/// <summary>
48+
/// Initializes the platform resources required for the accelerometer sensor.
49+
/// </summary>
50+
static void Initialize()
51+
{
52+
PlatformInitialize();
53+
}
54+
55+
/// <summary>
56+
/// Starts data acquisition from the accelerometer.
57+
/// </summary>
58+
public override void Start()
59+
{
60+
PlatformStart();
61+
}
62+
63+
/// <summary>
64+
/// Stops data acquisition from the accelerometer.
65+
/// </summary>
66+
public override void Stop()
67+
{
68+
PlatformStop();
69+
}
70+
71+
protected override void Dispose(bool disposing)
72+
{
73+
PlatformDispose(disposing);
74+
75+
base.Dispose(disposing);
76+
}
77+
}
78+
}

MonoGame.Framework/Platform/Microsoft/Devices/Sensors/AccelerometerFailedException.cs renamed to MonoGame.Framework/MonoGame/Framework/Devices/Sensors/AccelerometerFailedException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This file is subject to the terms and conditions defined in
33
// file 'LICENSE.txt', which is part of this source code package.
44

5-
namespace Microsoft.Devices.Sensors
5+
namespace MonoGame.Framework.Devices.Sensors
66
{
77
/// <summary>
88
/// The exception that may be thrown during a call to Start() or Stop(). The Message field describes the reason for the exception and the ErrorId field contains the error code from the underlying native code implementation of the accelerometer framework.

MonoGame.Framework/Platform/Microsoft/Devices/Sensors/AccelerometerReading.cs renamed to MonoGame.Framework/MonoGame/Framework/Devices/Sensors/AccelerometerReading.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System;
66
using Microsoft.Xna.Framework;
77

8-
namespace Microsoft.Devices.Sensors
8+
namespace MonoGame.Framework.Devices.Sensors
99
{
1010
/// <summary>
1111
/// Stores values from an accelerometer event

0 commit comments

Comments
 (0)