using System; using System.Collections.Generic; using System.Text; using Unity.WebRTC; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.LowLevel; using UnityEngine.InputSystem.Utilities; namespace Unity.RenderStreaming.InputSystem { using InputSystem = UnityEngine.InputSystem.InputSystem; class Sender : InputManager, IDisposable { public override event Action onEvent; public override event Action onDeviceChange; public override event Action onLayoutChange; private InputPositionCorrector _corrector; private Action _onEvent; public Sender() { InputSystem.onEvent += OnEvent; InputSystem.onDeviceChange += OnDeviceChange; InputSystem.onLayoutChange += OnLayoutChange; _onEvent = (InputEventPtr ptr, InputDevice device) => { onEvent?.Invoke(ptr, device); }; _corrector = new InputPositionCorrector(_onEvent); } ~Sender() { this.Dispose(); } public void Dispose() { InputSystem.onEvent -= OnEvent; InputSystem.onDeviceChange -= OnDeviceChange; InputSystem.onLayoutChange -= OnLayoutChange; } /// /// /// public override ReadOnlyArray devices { get { return InputSystem.devices; } } /// /// /// public override IEnumerable layouts { get { // todo(kazuki):: filter layout return InputSystem.ListLayouts(); } } /// /// /// public bool EnableInputPositionCorrection { set; get; } /// /// /// /// /// public void CalculateInputRegion(Rect inputRegion, Rect outputRegion) { _corrector.inputRegion = inputRegion; _corrector.outputRegion = outputRegion; } private void OnEvent(InputEventPtr ptr, InputDevice device) { // mapping sender coordinate system to receiver one. if (EnableInputPositionCorrection && device is Pointer && ptr.IsA()) { _corrector.Invoke(ptr, device); } else { onEvent?.Invoke(ptr, device); } } private void OnDeviceChange(InputDevice device, InputDeviceChange change) { onDeviceChange?.Invoke(device, change); } private void OnLayoutChange(string name, InputControlLayoutChange change) { onLayoutChange?.Invoke(name, change); } } /// /// /// class Observer : IObserver { private RTCDataChannel _channel; public Observer(RTCDataChannel channel) { _channel = channel ?? throw new ArgumentNullException("channel is null"); } public void OnNext(InputRemoting.Message value) { if (_channel.ReadyState != RTCDataChannelState.Open) return; byte[] bytes = MessageSerializer.Serialize(ref value); _channel.Send(bytes); } [Serializable] public struct DataTest { public string name; public string layoutJson; public bool isOverride; } private static TData DeserializeData(byte[] data) { var json = Encoding.UTF8.GetString(data); return JsonUtility.FromJson(json); } public void OnCompleted() { } public void OnError(Exception error) { } } } // #endif