WKLib 0.2.3
A modding library for White Knuckle
Loading...
Searching...
No Matches
InputTextReplace.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using Rewired;
4using TMPro;
5using UnityEngine;
6using UnityEngine.InputSystem;
7using UnityEngine.InputSystem.Users;
9
10namespace WKLib.API.UI;
11
20[RequireComponent(typeof(TMP_Text))]
21public class InputTextReplace : MonoBehaviour
22{
23 public string forceSchemeName = string.Empty;
24
25 private TMP_Text _text;
26 private string _originalText;
27 private bool _lastIsGamepad;
28
29 private void OnEnable()
30 {
31 _lastIsGamepad = InputManager.IsGamepad();
32
33 if (_text == null)
34 {
35 _text = GetComponent<TMP_Text>();
36 if (_text == null)
37 return;
38 _originalText = _text.text;
39 }
40
41 ReloadText();
42 }
43
44 private void Update()
45 {
46 if (_lastIsGamepad == InputManager.IsGamepad()) return;
47
48 ReloadText();
49 _lastIsGamepad = InputManager.IsGamepad();
50 }
51
52 public void ReloadText()
53 {
54 _text.text = _originalText;
55
56 int inputIndex = _text.text.IndexOf("{", StringComparison.Ordinal);
57 while (inputIndex != -1)
58 {
59 string keyTag = _text.text.Substring(inputIndex,
60 _text.text.IndexOf("}", inputIndex, StringComparison.Ordinal)-inputIndex+1);
61 string tagContent = keyTag.Replace("{", "").Replace("}", "");
62
63 if (tagContent.StartsWith("k:", StringComparison.Ordinal) ||
64 tagContent.StartsWith("c:", StringComparison.Ordinal) ||
65 tagContent.StartsWith("a:", StringComparison.Ordinal))
66 {
67 string[] keycodes = tagContent.Split(',');
68
69 string keycodeId = tagContent;
70 if (keycodes.Length == 2)
71 keycodeId = InputManager.IsGamepad() ? keycodes[1] : keycodes[0];
72
73 _text.text = _text.text.Replace(keyTag, GetInputName(keycodeId));
74 }
75
76 inputIndex = _text.text.IndexOf("{",inputIndex+1, StringComparison.Ordinal);
77 }
78 }
79
80 private string GetInputName(string tag)
81 {
82 if (string.IsNullOrEmpty(tag))
83 return "Unknown Key";
84
85 char prefix = tag[0];
86 tag = tag.Remove(0, 2);
87
88 switch (prefix)
89 {
90 case 'c':
91 var control = InputSystem.FindControl(tag);
92 if(control != null)
93 return control.displayName;
94 break;
95
96 case 'a':
97 var playerInput = InputManager.GetPlayerInput();
98 if (playerInput == null)
99 break;
100
101 int actionId = ReInput.mapping.GetActionId(tag);
102 if (actionId < 0)
103 break;
104
105 foreach (ControllerMap map in playerInput.controllers.maps.GetAllMaps())
106 {
107 if (!string.IsNullOrEmpty(forceSchemeName) &&
108 !string.Equals(map.name, forceSchemeName, StringComparison.OrdinalIgnoreCase))
109 continue;
110
111 foreach (ActionElementMap elementMap in map.AllMaps)
112 {
113 if (elementMap.actionId != actionId)
114 continue;
115
116 return elementMap.elementIdentifierName;
117 }
118 }
119
120 break;
121 case 'k':
122 if (Enum.TryParse(tag, out KeyCode keycode))
123 {
124 if (InputUtility.KeyCodeToControl(keycode) is InputControl keycodeControl)
125 return keycodeControl.displayName;
126 }
127 break;
128 }
129
130 return "Unknown Key";
131 }
132}
static ? object KeyCodeToControl(KeyCode key)
Once placed on a TextMeshPro text it will be able to replace special tags into proper input names/sym...