A game about forced loneliness, made by TACStudios
1using System;
2using System.Diagnostics;
3using UnityEngine.InputSystem.Controls;
4
5////TODO: API to get the control and device from the internal context
6
7////TODO: ToString()
8
9namespace UnityEngine.InputSystem
10{
11 /// <summary>
12 /// Wraps around values provided by input actions.
13 /// </summary>
14 /// <remarks>
15 /// This is a wrapper around <see cref="InputAction.CallbackContext"/> chiefly for use
16 /// with GameObject messages (i.e. <see cref="GameObject.SendMessage(string,object)"/>). It exists
17 /// so that action callback data can be represented as an object, can be reused, and shields
18 /// the receiver from having to know about action callback specifics.
19 /// </remarks>
20 /// <seealso cref="InputAction"/>
21 [DebuggerDisplay("Value = {Get()}")]
22 public class InputValue
23 {
24 /// <summary>
25 /// Read the value as an object.
26 /// </summary>
27 /// <remarks>
28 /// This method allocates GC memory and will thus created garbage. If used during gameplay,
29 /// it will lead to GC spikes.
30 /// </remarks>
31 /// <returns>The current value in the form of a boxed object.</returns>
32 public object Get()
33 {
34 return m_Context.Value.ReadValueAsObject();
35 }
36
37 ////TODO: add automatic conversions
38 public TValue Get<TValue>()
39 where TValue : struct
40 {
41 if (!m_Context.HasValue)
42 throw new InvalidOperationException($"Values can only be retrieved while in message callbacks");
43
44 return m_Context.Value.ReadValue<TValue>();
45 }
46
47 ////TODO: proper message if value type isn't right
48 public bool isPressed => Get<float>() >= ButtonControl.s_GlobalDefaultButtonPressPoint;
49
50 internal InputAction.CallbackContext? m_Context;
51 }
52}