Accelerometer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Accelerometer.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using System;
  12. #endregion
  13. namespace Platformer2D
  14. {
  15. /// <summary>
  16. /// A static encapsulation of accelerometer input to provide games with a polling-based
  17. /// accelerometer system.
  18. /// </summary>
  19. public static class Accelerometer
  20. {
  21. // we want to prevent the Accelerometer from being initialized twice.
  22. private static bool isInitialized = false;
  23. // whether or not the accelerometer is active
  24. private static bool isActive = false;
  25. /// <summary>
  26. /// Initializes the Accelerometer for the current game. This method can only be called once per game.
  27. /// </summary>
  28. public static void Initialize()
  29. {
  30. // make sure we don't initialize the Accelerometer twice
  31. if (isInitialized)
  32. {
  33. throw new InvalidOperationException("Initialize can only be called once");
  34. }
  35. // remember that we are initialized
  36. isInitialized = true;
  37. }
  38. /// <summary>
  39. /// Gets the current state of the accelerometer.
  40. /// </summary>
  41. /// <returns>A new AccelerometerState with the current state of the accelerometer.</returns>
  42. public static AccelerometerState GetState()
  43. {
  44. // make sure we've initialized the Accelerometer before we try to get the state
  45. if (!isInitialized)
  46. {
  47. throw new InvalidOperationException("You must Initialize before you can call GetState");
  48. }
  49. // create a new value for our state
  50. Vector3 stateValue = new Vector3();
  51. return new AccelerometerState(stateValue, isActive);
  52. }
  53. }
  54. /// <summary>
  55. /// An encapsulation of the accelerometer's current state.
  56. /// </summary>
  57. public struct AccelerometerState
  58. {
  59. /// <summary>
  60. /// Gets the accelerometer's current value in G-force.
  61. /// </summary>
  62. public Vector3 Acceleration { get; private set; }
  63. /// <summary>
  64. /// Gets whether or not the accelerometer is active and running.
  65. /// </summary>
  66. public bool IsActive { get; private set; }
  67. /// <summary>
  68. /// Initializes a new AccelerometerState.
  69. /// </summary>
  70. /// <param name="acceleration">The current acceleration (in G-force) of the accelerometer.</param>
  71. /// <param name="isActive">Whether or not the accelerometer is active.</param>
  72. public AccelerometerState(Vector3 acceleration, bool isActive)
  73. : this()
  74. {
  75. Acceleration = acceleration;
  76. IsActive = isActive;
  77. }
  78. /// <summary>
  79. /// Returns a string containing the values of the Acceleration and IsActive properties.
  80. /// </summary>
  81. /// <returns>A new string describing the state.</returns>
  82. public override string ToString()
  83. {
  84. return string.Format("Acceleration: {0}, IsActive: {1}", Acceleration, IsActive);
  85. }
  86. }
  87. }