Controls.cs 3.0 KB

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