123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<InputEventPtr, InputDevice> onEvent;
- public override event Action<InputDevice, InputDeviceChange> onDeviceChange;
- public override event Action<string, InputControlLayoutChange> onLayoutChange;
- private InputPositionCorrector _corrector;
- private Action<InputEventPtr, InputDevice> _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;
- }
- /// <summary>
- ///
- /// </summary>
- public override ReadOnlyArray<InputDevice> devices
- {
- get
- {
- return InputSystem.devices;
- }
- }
- /// <summary>
- ///
- /// </summary>
- public override IEnumerable<string> layouts
- {
- get
- {
- // todo(kazuki):: filter layout
- return InputSystem.ListLayouts();
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool EnableInputPositionCorrection { set; get; }
- /// <summary>
- ///
- /// </summary>
- /// <param name="inputRegion"></param>
- /// <param name="outputRegion"></param>
- 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<StateEvent>())
- {
- _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);
- }
- }
- /// <summary>
- ///
- /// </summary>
- class Observer : IObserver<InputRemoting.Message>
- {
- 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<TData>(byte[] data)
- {
- var json = Encoding.UTF8.GetString(data);
- return JsonUtility.FromJson<TData>(json);
- }
- public void OnCompleted()
- {
- }
- public void OnError(Exception error)
- {
- }
- }
- }
- // #endif
|