summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI
diff options
context:
space:
mode:
Diffstat (limited to 'Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI')
-rw-r--r--Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIEffect.cs57
-rw-r--r--Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIElement.cs186
-rw-r--r--Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/GamepadTemplateUI.cs417
3 files changed, 660 insertions, 0 deletions
diff --git a/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIEffect.cs b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIEffect.cs
new file mode 100644
index 0000000..695aa76
--- /dev/null
+++ b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIEffect.cs
@@ -0,0 +1,57 @@
+using UnityEngine;
+using UnityEngine.UI;
+
+namespace Rewired.Demos.GamepadTemplateUI;
+
+[RequireComponent(typeof(Image))]
+public class ControllerUIEffect : MonoBehaviour
+{
+ [SerializeField]
+ private Color _highlightColor = Color.white;
+
+ private Image _image;
+
+ private Color _color;
+
+ private Color _origColor;
+
+ private bool _isActive;
+
+ private float _highlightAmount;
+
+ private void Awake()
+ {
+ _image = GetComponent<Image>();
+ _origColor = _image.color;
+ _color = _origColor;
+ }
+
+ public void Activate(float amount)
+ {
+ amount = Mathf.Clamp01(amount);
+ if (!_isActive || amount != _highlightAmount)
+ {
+ _highlightAmount = amount;
+ _color = Color.Lerp(_origColor, _highlightColor, _highlightAmount);
+ _isActive = true;
+ RedrawImage();
+ }
+ }
+
+ public void Deactivate()
+ {
+ if (_isActive)
+ {
+ _color = _origColor;
+ _highlightAmount = 0f;
+ _isActive = false;
+ RedrawImage();
+ }
+ }
+
+ private void RedrawImage()
+ {
+ _image.color = _color;
+ _image.enabled = _isActive;
+ }
+}
diff --git a/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIElement.cs b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIElement.cs
new file mode 100644
index 0000000..b59e6f8
--- /dev/null
+++ b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIElement.cs
@@ -0,0 +1,186 @@
+using UnityEngine;
+using UnityEngine.UI;
+
+namespace Rewired.Demos.GamepadTemplateUI;
+
+[RequireComponent(typeof(Image))]
+public class ControllerUIElement : MonoBehaviour
+{
+ [SerializeField]
+ private Color _highlightColor = Color.white;
+
+ [SerializeField]
+ private ControllerUIEffect _positiveUIEffect;
+
+ [SerializeField]
+ private ControllerUIEffect _negativeUIEffect;
+
+ [SerializeField]
+ private Text _label;
+
+ [SerializeField]
+ private Text _positiveLabel;
+
+ [SerializeField]
+ private Text _negativeLabel;
+
+ [SerializeField]
+ private ControllerUIElement[] _childElements = new ControllerUIElement[0];
+
+ private Image _image;
+
+ private Color _color;
+
+ private Color _origColor;
+
+ private bool _isActive;
+
+ private float _highlightAmount;
+
+ private bool hasEffects
+ {
+ get
+ {
+ if (!(_positiveUIEffect != null))
+ {
+ return _negativeUIEffect != null;
+ }
+ return true;
+ }
+ }
+
+ private void Awake()
+ {
+ _image = GetComponent<Image>();
+ _origColor = _image.color;
+ _color = _origColor;
+ ClearLabels();
+ }
+
+ public void Activate(float amount)
+ {
+ amount = Mathf.Clamp(amount, -1f, 1f);
+ if (hasEffects)
+ {
+ if (amount < 0f && _negativeUIEffect != null)
+ {
+ _negativeUIEffect.Activate(Mathf.Abs(amount));
+ }
+ if (amount > 0f && _positiveUIEffect != null)
+ {
+ _positiveUIEffect.Activate(Mathf.Abs(amount));
+ }
+ }
+ else
+ {
+ if (_isActive && amount == _highlightAmount)
+ {
+ return;
+ }
+ _highlightAmount = amount;
+ _color = Color.Lerp(_origColor, _highlightColor, _highlightAmount);
+ }
+ _isActive = true;
+ RedrawImage();
+ if (_childElements.Length == 0)
+ {
+ return;
+ }
+ for (int i = 0; i < _childElements.Length; i++)
+ {
+ if (!(_childElements[i] == null))
+ {
+ _childElements[i].Activate(amount);
+ }
+ }
+ }
+
+ public void Deactivate()
+ {
+ if (!_isActive)
+ {
+ return;
+ }
+ _color = _origColor;
+ _highlightAmount = 0f;
+ if (_positiveUIEffect != null)
+ {
+ _positiveUIEffect.Deactivate();
+ }
+ if (_negativeUIEffect != null)
+ {
+ _negativeUIEffect.Deactivate();
+ }
+ _isActive = false;
+ RedrawImage();
+ if (_childElements.Length == 0)
+ {
+ return;
+ }
+ for (int i = 0; i < _childElements.Length; i++)
+ {
+ if (!(_childElements[i] == null))
+ {
+ _childElements[i].Deactivate();
+ }
+ }
+ }
+
+ public void SetLabel(string text, AxisRange labelType)
+ {
+ Text text2 = labelType switch
+ {
+ AxisRange.Full => _label,
+ AxisRange.Positive => _positiveLabel,
+ AxisRange.Negative => _negativeLabel,
+ _ => null,
+ };
+ if (text2 != null)
+ {
+ text2.text = text;
+ }
+ if (_childElements.Length == 0)
+ {
+ return;
+ }
+ for (int i = 0; i < _childElements.Length; i++)
+ {
+ if (!(_childElements[i] == null))
+ {
+ _childElements[i].SetLabel(text, labelType);
+ }
+ }
+ }
+
+ public void ClearLabels()
+ {
+ if (_label != null)
+ {
+ _label.text = string.Empty;
+ }
+ if (_positiveLabel != null)
+ {
+ _positiveLabel.text = string.Empty;
+ }
+ if (_negativeLabel != null)
+ {
+ _negativeLabel.text = string.Empty;
+ }
+ if (_childElements.Length == 0)
+ {
+ return;
+ }
+ for (int i = 0; i < _childElements.Length; i++)
+ {
+ if (!(_childElements[i] == null))
+ {
+ _childElements[i].ClearLabels();
+ }
+ }
+ }
+
+ private void RedrawImage()
+ {
+ _image.color = _color;
+ }
+}
diff --git a/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/GamepadTemplateUI.cs b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/GamepadTemplateUI.cs
new file mode 100644
index 0000000..e56617c
--- /dev/null
+++ b/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/GamepadTemplateUI.cs
@@ -0,0 +1,417 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace Rewired.Demos.GamepadTemplateUI;
+
+public class GamepadTemplateUI : MonoBehaviour
+{
+ private class Stick
+ {
+ private RectTransform _transform;
+
+ private Vector2 _origPosition;
+
+ private int _xAxisElementId = -1;
+
+ private int _yAxisElementId = -1;
+
+ public Vector2 position
+ {
+ get
+ {
+ if (!(_transform != null))
+ {
+ return Vector2.zero;
+ }
+ return _transform.anchoredPosition - _origPosition;
+ }
+ set
+ {
+ if (!(_transform == null))
+ {
+ _transform.anchoredPosition = _origPosition + value;
+ }
+ }
+ }
+
+ public Stick(RectTransform transform, int xAxisElementId, int yAxisElementId)
+ {
+ if (!(transform == null))
+ {
+ _transform = transform;
+ _origPosition = _transform.anchoredPosition;
+ _xAxisElementId = xAxisElementId;
+ _yAxisElementId = yAxisElementId;
+ }
+ }
+
+ public void Reset()
+ {
+ if (!(_transform == null))
+ {
+ _transform.anchoredPosition = _origPosition;
+ }
+ }
+
+ public bool ContainsElement(int elementId)
+ {
+ if (_transform == null)
+ {
+ return false;
+ }
+ if (elementId != _xAxisElementId)
+ {
+ return elementId == _yAxisElementId;
+ }
+ return true;
+ }
+
+ public void SetAxisPosition(int elementId, float value)
+ {
+ if (!(_transform == null))
+ {
+ Vector2 vector = position;
+ if (elementId == _xAxisElementId)
+ {
+ vector.x = value;
+ }
+ else if (elementId == _yAxisElementId)
+ {
+ vector.y = value;
+ }
+ position = vector;
+ }
+ }
+ }
+
+ private class UIElement
+ {
+ public int id;
+
+ public ControllerUIElement element;
+
+ public UIElement(int id, ControllerUIElement element)
+ {
+ this.id = id;
+ this.element = element;
+ }
+ }
+
+ private const float stickRadius = 20f;
+
+ public int playerId;
+
+ [SerializeField]
+ private RectTransform leftStick;
+
+ [SerializeField]
+ private RectTransform rightStick;
+
+ [SerializeField]
+ private ControllerUIElement leftStickX;
+
+ [SerializeField]
+ private ControllerUIElement leftStickY;
+
+ [SerializeField]
+ private ControllerUIElement leftStickButton;
+
+ [SerializeField]
+ private ControllerUIElement rightStickX;
+
+ [SerializeField]
+ private ControllerUIElement rightStickY;
+
+ [SerializeField]
+ private ControllerUIElement rightStickButton;
+
+ [SerializeField]
+ private ControllerUIElement actionBottomRow1;
+
+ [SerializeField]
+ private ControllerUIElement actionBottomRow2;
+
+ [SerializeField]
+ private ControllerUIElement actionBottomRow3;
+
+ [SerializeField]
+ private ControllerUIElement actionTopRow1;
+
+ [SerializeField]
+ private ControllerUIElement actionTopRow2;
+
+ [SerializeField]
+ private ControllerUIElement actionTopRow3;
+
+ [SerializeField]
+ private ControllerUIElement leftShoulder;
+
+ [SerializeField]
+ private ControllerUIElement leftTrigger;
+
+ [SerializeField]
+ private ControllerUIElement rightShoulder;
+
+ [SerializeField]
+ private ControllerUIElement rightTrigger;
+
+ [SerializeField]
+ private ControllerUIElement center1;
+
+ [SerializeField]
+ private ControllerUIElement center2;
+
+ [SerializeField]
+ private ControllerUIElement center3;
+
+ [SerializeField]
+ private ControllerUIElement dPadUp;
+
+ [SerializeField]
+ private ControllerUIElement dPadRight;
+
+ [SerializeField]
+ private ControllerUIElement dPadDown;
+
+ [SerializeField]
+ private ControllerUIElement dPadLeft;
+
+ private UIElement[] _uiElementsArray;
+
+ private Dictionary<int, ControllerUIElement> _uiElements = new Dictionary<int, ControllerUIElement>();
+
+ private IList<ControllerTemplateElementTarget> _tempTargetList = new List<ControllerTemplateElementTarget>(2);
+
+ private Stick[] _sticks;
+
+ private Player player => ReInput.players.GetPlayer(playerId);
+
+ private void Awake()
+ {
+ _uiElementsArray = new UIElement[23]
+ {
+ new UIElement(0, leftStickX),
+ new UIElement(1, leftStickY),
+ new UIElement(17, leftStickButton),
+ new UIElement(2, rightStickX),
+ new UIElement(3, rightStickY),
+ new UIElement(18, rightStickButton),
+ new UIElement(4, actionBottomRow1),
+ new UIElement(5, actionBottomRow2),
+ new UIElement(6, actionBottomRow3),
+ new UIElement(7, actionTopRow1),
+ new UIElement(8, actionTopRow2),
+ new UIElement(9, actionTopRow3),
+ new UIElement(14, center1),
+ new UIElement(15, center2),
+ new UIElement(16, center3),
+ new UIElement(19, dPadUp),
+ new UIElement(20, dPadRight),
+ new UIElement(21, dPadDown),
+ new UIElement(22, dPadLeft),
+ new UIElement(10, leftShoulder),
+ new UIElement(11, leftTrigger),
+ new UIElement(12, rightShoulder),
+ new UIElement(13, rightTrigger)
+ };
+ for (int i = 0; i < _uiElementsArray.Length; i++)
+ {
+ _uiElements.Add(_uiElementsArray[i].id, _uiElementsArray[i].element);
+ }
+ _sticks = new Stick[2]
+ {
+ new Stick(leftStick, 0, 1),
+ new Stick(rightStick, 2, 3)
+ };
+ ReInput.ControllerConnectedEvent += OnControllerConnected;
+ ReInput.ControllerDisconnectedEvent += OnControllerDisconnected;
+ }
+
+ private void Start()
+ {
+ if (ReInput.isReady)
+ {
+ DrawLabels();
+ }
+ }
+
+ private void OnDestroy()
+ {
+ ReInput.ControllerConnectedEvent -= OnControllerConnected;
+ ReInput.ControllerDisconnectedEvent -= OnControllerDisconnected;
+ }
+
+ private void Update()
+ {
+ if (ReInput.isReady)
+ {
+ DrawActiveElements();
+ }
+ }
+
+ private void DrawActiveElements()
+ {
+ for (int i = 0; i < _uiElementsArray.Length; i++)
+ {
+ _uiElementsArray[i].element.Deactivate();
+ }
+ for (int j = 0; j < _sticks.Length; j++)
+ {
+ _sticks[j].Reset();
+ }
+ IList<InputAction> actions = ReInput.mapping.Actions;
+ for (int k = 0; k < actions.Count; k++)
+ {
+ ActivateElements(player, actions[k].id);
+ }
+ }
+
+ private void ActivateElements(Player player, int actionId)
+ {
+ float axis = player.GetAxis(actionId);
+ if (axis == 0f)
+ {
+ return;
+ }
+ IList<InputActionSourceData> currentInputSources = player.GetCurrentInputSources(actionId);
+ for (int i = 0; i < currentInputSources.Count; i++)
+ {
+ InputActionSourceData inputActionSourceData = currentInputSources[i];
+ IGamepadTemplate template = inputActionSourceData.controller.GetTemplate<IGamepadTemplate>();
+ if (template == null)
+ {
+ continue;
+ }
+ template.GetElementTargets(inputActionSourceData.actionElementMap, _tempTargetList);
+ for (int j = 0; j < _tempTargetList.Count; j++)
+ {
+ ControllerTemplateElementTarget controllerTemplateElementTarget = _tempTargetList[j];
+ int id = controllerTemplateElementTarget.element.id;
+ ControllerUIElement controllerUIElement = _uiElements[id];
+ if (controllerTemplateElementTarget.elementType == ControllerTemplateElementType.Axis)
+ {
+ controllerUIElement.Activate(axis);
+ }
+ else if (controllerTemplateElementTarget.elementType == ControllerTemplateElementType.Button && (player.GetButton(actionId) || player.GetNegativeButton(actionId)))
+ {
+ controllerUIElement.Activate(1f);
+ }
+ GetStick(id)?.SetAxisPosition(id, axis * 20f);
+ }
+ }
+ }
+
+ private void DrawLabels()
+ {
+ for (int i = 0; i < _uiElementsArray.Length; i++)
+ {
+ _uiElementsArray[i].element.ClearLabels();
+ }
+ IList<InputAction> actions = ReInput.mapping.Actions;
+ for (int j = 0; j < actions.Count; j++)
+ {
+ DrawLabels(player, actions[j]);
+ }
+ }
+
+ private void DrawLabels(Player player, InputAction action)
+ {
+ Controller firstControllerWithTemplate = player.controllers.GetFirstControllerWithTemplate<IGamepadTemplate>();
+ if (firstControllerWithTemplate == null)
+ {
+ return;
+ }
+ IGamepadTemplate template = firstControllerWithTemplate.GetTemplate<IGamepadTemplate>();
+ ControllerMap map = player.controllers.maps.GetMap(firstControllerWithTemplate, "Default", "Default");
+ if (map != null)
+ {
+ for (int i = 0; i < _uiElementsArray.Length; i++)
+ {
+ ControllerUIElement element = _uiElementsArray[i].element;
+ int id = _uiElementsArray[i].id;
+ IControllerTemplateElement element2 = template.GetElement(id);
+ DrawLabel(element, action, map, template, element2);
+ }
+ }
+ }
+
+ private void DrawLabel(ControllerUIElement uiElement, InputAction action, ControllerMap controllerMap, IControllerTemplate template, IControllerTemplateElement element)
+ {
+ if (element.source == null)
+ {
+ return;
+ }
+ if (element.source.type == ControllerTemplateElementSourceType.Axis)
+ {
+ IControllerTemplateAxisSource controllerTemplateAxisSource = element.source as IControllerTemplateAxisSource;
+ ActionElementMap firstElementMapWithElementTarget;
+ if (controllerTemplateAxisSource.splitAxis)
+ {
+ firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.positiveTarget, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Positive);
+ }
+ firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.negativeTarget, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Negative);
+ }
+ return;
+ }
+ firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.fullTarget, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Full);
+ return;
+ }
+ firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(new ControllerElementTarget(controllerTemplateAxisSource.fullTarget)
+ {
+ axisRange = AxisRange.Positive
+ }, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Positive);
+ }
+ firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(new ControllerElementTarget(controllerTemplateAxisSource.fullTarget)
+ {
+ axisRange = AxisRange.Negative
+ }, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Negative);
+ }
+ }
+ else if (element.source.type == ControllerTemplateElementSourceType.Button)
+ {
+ IControllerTemplateButtonSource controllerTemplateButtonSource = element.source as IControllerTemplateButtonSource;
+ ActionElementMap firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateButtonSource.target, action.id, skipDisabledMaps: true);
+ if (firstElementMapWithElementTarget != null)
+ {
+ uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Full);
+ }
+ }
+ }
+
+ private Stick GetStick(int elementId)
+ {
+ for (int i = 0; i < _sticks.Length; i++)
+ {
+ if (_sticks[i].ContainsElement(elementId))
+ {
+ return _sticks[i];
+ }
+ }
+ return null;
+ }
+
+ private void OnControllerConnected(ControllerStatusChangedEventArgs args)
+ {
+ DrawLabels();
+ }
+
+ private void OnControllerDisconnected(ControllerStatusChangedEventArgs args)
+ {
+ DrawLabels();
+ }
+}