improvements to input service

This commit is contained in:
2025-10-21 15:24:31 +02:00
parent 00b33af98b
commit 40e2236f12
4 changed files with 151 additions and 59 deletions

View File

@@ -1,13 +1,6 @@
# RebootKit
# Installationa (WIP)
### nuget
Install nuget package manager for unity from the unity package manager.
And install the following packages:
- **R3** by Cysharp
- **ZLinq** by Cysharp
# Installation (WIP)
### manifest.json
Ensure you have the following dependencies in your `Packages/manifest.json` file:
@@ -15,17 +8,8 @@ Ensure you have the following dependencies in your `Packages/manifest.json` file
```json
{
"dependencies": {
"com.cysharp.r3": "https://github.com/Cysharp/R3.git?path=src/R3.Unity/Assets/R3.Unity",
"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"com.cysharp.zlinq": "https://github.com/Cysharp/ZLinq.git?path=src/ZLinq.Unity/Assets/ZLinq.Unity",
"com.github-glitchenzo.nugetforunity": "https://github.com/GlitchEnzo/NuGetForUnity.git?path=/src/NuGetForUnity",
}
}
```
# TODO
- [ ] Save system
- [ ] Built in main menu
- [ ] Generic settings ui based on Config Variables
- [ ] Extendable FPP Controller
- [ ] UI System

View File

@@ -12,15 +12,28 @@ namespace RebootKit.Engine.ConsoleUI {
[SerializeField] ConsoleVC m_ConsoleVC;
[SerializeField] ScriptableInputAction m_ToggleAction;
InputState m_InputState;
readonly StringBuilder m_Content = new StringBuilder();
public bool IsVisible => m_ConsoleVC.gameObject.activeSelf;
void Awake() {
m_InputState = RR.Input.NewInputState("ConsoleUI");
m_InputState.Priority = int.MaxValue;
m_InputState.LockCursor = false;
m_InputState.HideCursor = false;
m_InputState.IsActive = IsVisible;
SetVisibility(false);
}
void OnDestroy() {
m_InputState?.Dispose();
m_InputState = null;
}
void OnEnable() {
s_Logger.Info("OnEnable console");
@@ -47,13 +60,7 @@ namespace RebootKit.Engine.ConsoleUI {
SetVisibility(!IsVisible);
if (IsVisible) {
RR.Input.DisableControls();
RR.Input.UnlockCursor();
m_ConsoleVC.SetMessageContent(m_Content.ToString());
} else {
RR.Input.EnableControls();
RR.Input.LockCursor();
}
}
@@ -64,6 +71,7 @@ namespace RebootKit.Engine.ConsoleUI {
public void SetVisibility(bool visible) {
m_ConsoleVC.gameObject.SetActive(visible);
m_InputState.IsActive = visible;
}
public void Write(string message) {

View File

@@ -5,7 +5,7 @@ using Logger = RebootKit.Engine.Foundation.Logger;
namespace RebootKit.Engine.ConsoleUI {
public class ConsoleVC : MonoBehaviour {
static readonly Logger s_logger = new Logger(nameof(ConsoleVC));
static readonly Logger s_Logger = new Logger(nameof(ConsoleVC));
[SerializeField] UIDocument m_Document;
@@ -21,14 +21,13 @@ namespace RebootKit.Engine.ConsoleUI {
m_LabelMessage = m_Root.Q<Label>("console-window-message");
m_TextField = m_Root.Q<TextField>("console-text-field");
m_TextField.value = string.Empty;
m_TextField.RegisterCallback<KeyUpEvent>(ev => {
if (ev.keyCode == KeyCode.Return) {
Submit();
ev.StopPropagation();
}
});
Button submitButton = m_Root.Q<Button>("console-btn-submit");
submitButton.RegisterCallback<ClickEvent>(_ => { Submit(); });
@@ -37,7 +36,13 @@ namespace RebootKit.Engine.ConsoleUI {
m_ScrollView = m_Root.Q<ScrollView>("console-scrollview");
m_TextField.schedule.Execute(() => m_TextField.Focus()).StartingIn(1);
m_TextField.schedule.Execute(() => {
m_TextField.Focus();
m_TextField.value = "";
}).StartingIn(16);
}
void OnDisable() {
}
public event Action<string> InputSubmitted = _ => { };

View File

@@ -5,18 +5,95 @@ using RebootKit.Engine.Foundation;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.InputSystem;
using UnityEngine.Profiling;
using Logger = RebootKit.Engine.Foundation.Logger;
namespace RebootKit.Engine.Input {
public class InputState {
public string Name { get; private set; }
public bool HideCursor { get; private set; }
public bool LockCursor { get; private set; }
public class InputState : IDisposable {
static readonly Logger s_Logger = new Logger(nameof(InputState));
public InputState(string name, bool hideCursor = false, bool lockCursor = false) {
public string Name { get; private set; }
bool m_IsActive;
public bool IsActive {
get {
return m_IsActive;
}
set {
m_IsActive = value;
Service?.UpdateUnityInputState();
}
}
int m_Priority;
public int Priority {
get {
return m_Priority;
}
set {
m_Priority = value;
Service?.UpdateUnityInputState();
}
}
bool m_HideCursor;
public bool HideCursor {
get {
return m_HideCursor;
}
set {
m_HideCursor = value;
Service?.UpdateUnityInputState();
}
}
bool m_LockCursor;
public bool LockCursor {
get {
return m_LockCursor;
}
set {
m_LockCursor = value;
Service?.UpdateUnityInputState();
}
}
internal List<string> ActiveMaps = new List<string>(4);
internal InputService Service;
internal InputState(string name, InputService service) {
IsActive = true;
Name = name;
HideCursor = hideCursor;
LockCursor = lockCursor;
Service = service;
}
public void Dispose() {
Service.UnregisterInputState(this);
Service = null;
}
public void AddActiveMap(string mapName) {
if (ActiveMaps.Contains(mapName)) {
s_Logger.Warning($"Action Map '{mapName}' is already active in InputState '{Name}'");
return;
}
ActiveMaps.Add(mapName);
Service.UpdateUnityInputState();
}
public void RemoveActiveMap(string mapName) {
ActiveMaps.Remove(mapName);
Service.UpdateUnityInputState();
}
public bool IsMapActive(string mapName) {
return ActiveMaps.Contains(mapName);
}
}
@@ -27,7 +104,7 @@ namespace RebootKit.Engine.Input {
InputActionAsset m_InputActionAsset;
List<InputState> m_States = new List<InputState>(16);
readonly List<InputState> m_States = new List<InputState>(16);
public InputService(Config config) {
m_Config = config;
@@ -39,41 +116,59 @@ namespace RebootKit.Engine.Input {
m_InputActionAsset = null;
}
public void AddState(InputState state) {
Assert.IsFalse(m_States.Contains(state), "State already exists in the input service.");
Assert.IsNotNull(state, "Input state cannot be null.");
Assert.IsNull(m_States.FirstOrDefault(t => t.Name.Equals(state.Name, StringComparison.Ordinal)),
$"State with name '{state.Name}' already exists in the input service.");
public InputState NewInputState(string name) {
Assert.IsNull(m_States.FirstOrDefault(t => t.Name.Equals(name, StringComparison.Ordinal)),
$"State with name '{name}' already exists in the input service.");
m_States.Add(state);
var inputState = new InputState(name, this);
m_States.Add(inputState);
return inputState;
}
internal void UnregisterInputState(InputState state) {
m_States.Remove(state);
}
internal void UpdateUnityInputState() {
Profiler.BeginSample("Find Active State");
InputState activeState = m_States.Where(t => t.IsActive)
.OrderByDescending(t => t.Priority)
.FirstOrDefault();
Profiler.EndSample();
if (activeState == null) {
s_Logger.Warning("No Active input state found");
return;
}
Cursor.visible = !activeState.HideCursor;
Cursor.lockState = activeState.LockCursor ? CursorLockMode.Locked : CursorLockMode.None;
foreach (InputActionMap inputActionMap in m_InputActionAsset.actionMaps) {
if (activeState.IsMapActive(inputActionMap.name)) {
inputActionMap.Enable();
} else {
inputActionMap.Disable();
}
}
}
public void EnableControls() {
if (m_InputActionAsset == null) {
return;
}
m_InputActionAsset.Enable();
}
public void DisableControls() {
if (m_InputActionAsset == null) {
return;
}
m_InputActionAsset.Disable();
}
public bool AreControlsEnabled() {
return m_InputActionAsset.enabled;
}
public InputAction FindInputAction(string path) {
return m_InputActionAsset.FindAction(path);
}
public void LockCursor() {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
public void UnlockCursor() {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
[Serializable]
public class Config {
public InputActionAsset inputActionAsset;