Controls.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Runtime.ValidateObject(this);
  18. handle = Controls_Create ();
  19. connection = null;
  20. }
  21. static void ReleaseControl (IntPtr h)
  22. {
  23. Controls_Destroy (h);
  24. }
  25. ~Controls ()
  26. {
  27. if (connection == null){
  28. ReleaseControl (handle);
  29. handle = IntPtr.Zero;
  30. }
  31. }
  32. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  33. extern static void Controls_Destroy (IntPtr handle);
  34. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  35. extern static IntPtr Controls_Create ();
  36. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  37. extern static uint Controls_GetButtons (IntPtr handle);
  38. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  39. extern static void Controls_SetButtons (IntPtr handle, uint value);
  40. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  41. extern static float Controls_GetYaw (IntPtr handle);
  42. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  43. extern static void Controls_SetYaw (IntPtr handle, float yaw);
  44. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  45. extern static float Controls_GetPitch (IntPtr handle);
  46. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  47. extern static void Controls_SetPitch (IntPtr handle, float yaw);
  48. public uint Buttons {
  49. get { return Controls_GetButtons (handle); }
  50. set { Controls_SetButtons (handle, value); }
  51. }
  52. public float Yaw {
  53. get { return Controls_GetYaw (handle); }
  54. set { Controls_SetYaw (handle, value); }
  55. }
  56. public float Pitch {
  57. get { return Controls_GetPitch (handle); }
  58. set { Controls_SetPitch (handle, value); }
  59. }
  60. public bool IsPressed (uint button, ref Controls previousControls)
  61. {
  62. Runtime.ValidateObject(this);
  63. return ((Buttons & button) != 0) && ((previousControls.Buttons & button) != 0);
  64. }
  65. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  66. internal static extern void Controls_Reset (IntPtr handle);
  67. public void Reset ()
  68. {
  69. Runtime.ValidateObject(this);
  70. Controls_Reset (handle);
  71. }
  72. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  73. internal static extern void Controls_Set (IntPtr handle, uint buttons, bool down);
  74. public void Set (uint buttons, bool down)
  75. {
  76. Runtime.ValidateObject(this);
  77. Controls_Set (handle, buttons, down);
  78. }
  79. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  80. internal static extern bool Controls_IsDown (IntPtr handle, uint button);
  81. public bool IsDown (uint button)
  82. {
  83. Runtime.ValidateObject(this);
  84. return Controls_IsDown (handle, button);
  85. }
  86. }
  87. }