File size: 2,512 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#if MLA_INPUT_SYSTEM
using System;
using Unity.Collections;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Unity.MLAgents.Extensions.Input
{
    /// <summary>
    /// This interface is passed to InputActionActuators to allow them to write to InputEvents.
    /// The way this interface should be used is to request the <see cref="InputEventPtr"/> by calling
    /// <see cref="GetEventForFrame"/> then call <see cref="EventProcessedInFrame"/> before returning from
    /// </summary>
    public class InputActuatorEventContext : IDisposable
    {
        /// <summary>
        /// The number of times to allow the use of an event before queuing it in the InputSystem.
        /// </summary>
        public readonly int NumTimesToProcess;
        public readonly InputDevice InputDevice;
        NativeArray<byte> m_EventBuffer;
        InputEventPtr m_Ptr;
        int m_Count;

#if UNITY_EDITOR
        public static InputActuatorEventContext s_EditorContext = new InputActuatorEventContext();
#endif

        public InputActuatorEventContext(int numTimesToProcess = 1, InputDevice device = null)
        {
            NumTimesToProcess = numTimesToProcess;
            InputDevice = device;
            m_Count = 0;
            m_Ptr = new InputEventPtr();
            m_EventBuffer = new NativeArray<byte>();
        }

        /// <summary>
        /// Returns the <see cref="InputEventPtr"/> to write to for the current frame.
        /// </summary>
        /// <returns>The <see cref="InputEventPtr"/> to write to for the current frame.</returns>
        public IDisposable GetEventForFrame(out InputEventPtr eventPtr)
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                eventPtr = new InputEventPtr();
            }
#endif
            if (m_Count % NumTimesToProcess == 0)
            {
                m_Count = 0;
                m_EventBuffer = StateEvent.From(InputDevice, out m_Ptr);
            }
            eventPtr = m_Ptr;
            return this;
        }

        public void Dispose()
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                return;
            }
#endif
            m_Count++;
            if (m_Count == NumTimesToProcess && m_Ptr.valid)
            {
                InputSystem.QueueEvent(m_Ptr);
                m_EventBuffer.Dispose();
            }
        }
    }
}
#endif // MLA_INPUT_SYSTEM