Controls.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace AtomicEngine
  4. {
  5. public partial class Controls
  6. {
  7. // If null, we own the unmanaged value
  8. // This is not-null when this is created from a pointer to another object, so we use this to keep the other object alive.
  9. Connection connection;
  10. internal IntPtr handle;
  11. internal Controls(Connection connection, IntPtr handle)
  12. {
  13. this.connection = connection;
  14. this.handle = handle;
  15. }
  16. public Controls()
  17. {
  18. handle = csi_Atomic_Controls_Create();
  19. connection = null;
  20. }
  21. static void ReleaseControl(IntPtr h)
  22. {
  23. csi_Atomic_Controls_Destroy(h);
  24. }
  25. ~Controls()
  26. {
  27. if (connection == null)
  28. {
  29. ReleaseControl(handle);
  30. handle = IntPtr.Zero;
  31. }
  32. }
  33. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  34. extern static void csi_Atomic_Controls_Destroy(IntPtr handle);
  35. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  36. extern static IntPtr csi_Atomic_Controls_Create();
  37. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  38. extern static uint csi_Atomic_Controls_GetButtons(IntPtr handle);
  39. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  40. extern static void csi_Atomic_Controls_SetButtons(IntPtr handle, uint value);
  41. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  42. extern static float csi_Atomic_Controls_GetYaw(IntPtr handle);
  43. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  44. extern static void csi_Atomic_Controls_SetYaw(IntPtr handle, float yaw);
  45. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  46. extern static float csi_Atomic_Controls_GetPitch(IntPtr handle);
  47. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  48. extern static void csi_Atomic_Controls_SetPitch(IntPtr handle, float yaw);
  49. public uint Buttons
  50. {
  51. get { return csi_Atomic_Controls_GetButtons(handle); }
  52. set { csi_Atomic_Controls_SetButtons(handle, value); }
  53. }
  54. public float Yaw
  55. {
  56. get { return csi_Atomic_Controls_GetYaw(handle); }
  57. set { csi_Atomic_Controls_SetYaw(handle, value); }
  58. }
  59. public float Pitch
  60. {
  61. get { return csi_Atomic_Controls_GetPitch(handle); }
  62. set { csi_Atomic_Controls_SetPitch(handle, value); }
  63. }
  64. public bool IsPressed(uint button, ref Controls previousControls)
  65. {
  66. return ((Buttons & button) != 0) && ((previousControls.Buttons & button) != 0);
  67. }
  68. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  69. internal static extern void csi_Atomic_Controls_Reset(IntPtr handle);
  70. public void Reset()
  71. {
  72. csi_Atomic_Controls_Reset(handle);
  73. }
  74. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  75. internal static extern void csi_Atomic_Controls_Set(IntPtr handle, uint buttons, bool down);
  76. public void Set(uint buttons, bool down)
  77. {
  78. csi_Atomic_Controls_Set(handle, buttons, down);
  79. }
  80. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl)]
  81. internal static extern bool csi_Atomic_Controls_IsDown(IntPtr handle, uint button);
  82. public bool IsDown(uint button)
  83. {
  84. return csi_Atomic_Controls_IsDown(handle, button);
  85. }
  86. }
  87. }