Sender.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Unity.WebRTC;
  5. using UnityEngine;
  6. using UnityEngine.InputSystem;
  7. using UnityEngine.InputSystem.LowLevel;
  8. using UnityEngine.InputSystem.Utilities;
  9. namespace Unity.RenderStreaming.InputSystem
  10. {
  11. using InputSystem = UnityEngine.InputSystem.InputSystem;
  12. class Sender : InputManager, IDisposable
  13. {
  14. public override event Action<InputEventPtr, InputDevice> onEvent;
  15. public override event Action<InputDevice, InputDeviceChange> onDeviceChange;
  16. public override event Action<string, InputControlLayoutChange> onLayoutChange;
  17. private InputPositionCorrector _corrector;
  18. private Action<InputEventPtr, InputDevice> _onEvent;
  19. public Sender()
  20. {
  21. InputSystem.onEvent += OnEvent;
  22. InputSystem.onDeviceChange += OnDeviceChange;
  23. InputSystem.onLayoutChange += OnLayoutChange;
  24. _onEvent = (InputEventPtr ptr, InputDevice device) => { onEvent?.Invoke(ptr, device); };
  25. _corrector = new InputPositionCorrector(_onEvent);
  26. }
  27. ~Sender()
  28. {
  29. this.Dispose();
  30. }
  31. public void Dispose()
  32. {
  33. InputSystem.onEvent -= OnEvent;
  34. InputSystem.onDeviceChange -= OnDeviceChange;
  35. InputSystem.onLayoutChange -= OnLayoutChange;
  36. }
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public override ReadOnlyArray<InputDevice> devices
  41. {
  42. get
  43. {
  44. return InputSystem.devices;
  45. }
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. public override IEnumerable<string> layouts
  51. {
  52. get
  53. {
  54. // todo(kazuki):: filter layout
  55. return InputSystem.ListLayouts();
  56. }
  57. }
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. public bool EnableInputPositionCorrection { set; get; }
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. /// <param name="inputRegion"></param>
  66. /// <param name="outputRegion"></param>
  67. public void CalculateInputRegion(Rect inputRegion, Rect outputRegion)
  68. {
  69. _corrector.inputRegion = inputRegion;
  70. _corrector.outputRegion = outputRegion;
  71. }
  72. private void OnEvent(InputEventPtr ptr, InputDevice device)
  73. {
  74. // mapping sender coordinate system to receiver one.
  75. if (EnableInputPositionCorrection && device is Pointer && ptr.IsA<StateEvent>())
  76. {
  77. _corrector.Invoke(ptr, device);
  78. }
  79. else
  80. {
  81. onEvent?.Invoke(ptr, device);
  82. }
  83. }
  84. private void OnDeviceChange(InputDevice device, InputDeviceChange change)
  85. {
  86. onDeviceChange?.Invoke(device, change);
  87. }
  88. private void OnLayoutChange(string name, InputControlLayoutChange change)
  89. {
  90. onLayoutChange?.Invoke(name, change);
  91. }
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. class Observer : IObserver<InputRemoting.Message>
  97. {
  98. private RTCDataChannel _channel;
  99. public Observer(RTCDataChannel channel)
  100. {
  101. _channel = channel ?? throw new ArgumentNullException("channel is null");
  102. }
  103. public void OnNext(InputRemoting.Message value)
  104. {
  105. if (_channel.ReadyState != RTCDataChannelState.Open)
  106. return;
  107. byte[] bytes = MessageSerializer.Serialize(ref value);
  108. _channel.Send(bytes);
  109. }
  110. [Serializable]
  111. public struct DataTest
  112. {
  113. public string name;
  114. public string layoutJson;
  115. public bool isOverride;
  116. }
  117. private static TData DeserializeData<TData>(byte[] data)
  118. {
  119. var json = Encoding.UTF8.GetString(data);
  120. return JsonUtility.FromJson<TData>(json);
  121. }
  122. public void OnCompleted()
  123. {
  124. }
  125. public void OnError(Exception error)
  126. {
  127. }
  128. }
  129. }
  130. // #endif