working on multiplayer
This commit is contained in:
83
Runtime/Engine/Code/ConsoleUI/ConsoleUIService.cs
Normal file
83
Runtime/Engine/Code/ConsoleUI/ConsoleUIService.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System.Text;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Main;
|
||||
using RebootKit.Engine.Services.Input;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Services.ConsoleUI {
|
||||
public class ConsoleUIService : ServiceMonoBehaviour {
|
||||
static readonly Logger s_logger = new Logger(nameof(ConsoleUIService));
|
||||
|
||||
[SerializeField] ConsoleVC m_ConsoleVC;
|
||||
[SerializeField] ScriptableInputAction m_ToggleAction;
|
||||
|
||||
readonly StringBuilder m_Content = new StringBuilder();
|
||||
|
||||
public bool IsVisible => m_ConsoleVC.gameObject.activeSelf;
|
||||
|
||||
void Awake() {
|
||||
SetVisibility(false);
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
s_logger.Info("OnEnable console");
|
||||
|
||||
m_ToggleAction.Action.Enable();
|
||||
m_ToggleAction.Action.performed += OnToggleAction;
|
||||
|
||||
m_ConsoleVC.InputSubmitted += OnUserInput;
|
||||
m_ConsoleVC.ClearRequested += Clear;
|
||||
RR.Console.OnOutputMessage += Write;
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
m_ToggleAction.Action.performed -= OnToggleAction;
|
||||
|
||||
m_ConsoleVC.InputSubmitted -= OnUserInput;
|
||||
m_ConsoleVC.ClearRequested -= Clear;
|
||||
RR.Console.OnOutputMessage -= Write;
|
||||
}
|
||||
|
||||
public override void Dispose() {
|
||||
}
|
||||
|
||||
public void ToggleVisibility() {
|
||||
SetVisibility(!IsVisible);
|
||||
|
||||
if (IsVisible) {
|
||||
RR.Input.DisableControls();
|
||||
RR.Input.UnlockCursor();
|
||||
|
||||
m_ConsoleVC.SetMessageContent(m_Content.ToString());
|
||||
} else {
|
||||
RR.Input.EnableControls();
|
||||
RR.Input.LockCursor();
|
||||
}
|
||||
}
|
||||
|
||||
void OnUserInput(string input) {
|
||||
RR.Console.WriteToOutput("> " + input);
|
||||
RR.Console.Execute(input);
|
||||
}
|
||||
|
||||
public void SetVisibility(bool visible) {
|
||||
m_ConsoleVC.gameObject.SetActive(visible);
|
||||
}
|
||||
|
||||
public void Write(string message) {
|
||||
m_Content.AppendLine(message);
|
||||
m_ConsoleVC.SetMessageContent(m_Content.ToString());
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
m_Content.Clear();
|
||||
m_ConsoleVC.SetMessageContent(string.Empty);
|
||||
}
|
||||
|
||||
void OnToggleAction(InputAction.CallbackContext obj) {
|
||||
ToggleVisibility();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/ConsoleUI/ConsoleUIService.cs.meta
Normal file
3
Runtime/Engine/Code/ConsoleUI/ConsoleUIService.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca844a0157054677b2f129fdbf6ddc45
|
||||
timeCreated: 1740780314
|
||||
66
Runtime/Engine/Code/ConsoleUI/ConsoleVC.cs
Normal file
66
Runtime/Engine/Code/ConsoleUI/ConsoleVC.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Services.ConsoleUI {
|
||||
public class ConsoleVC : MonoBehaviour {
|
||||
static readonly Logger s_logger = new Logger(nameof(ConsoleVC));
|
||||
|
||||
[SerializeField] UIDocument m_Document;
|
||||
|
||||
Label m_LabelMessage;
|
||||
|
||||
VisualElement m_Root;
|
||||
|
||||
ScrollView m_ScrollView;
|
||||
TextField m_TextField;
|
||||
|
||||
void OnEnable() {
|
||||
m_Root = m_Document.rootVisualElement;
|
||||
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(); });
|
||||
|
||||
Button clearButton = m_Root.Q<Button>("console-btn-clear");
|
||||
clearButton.RegisterCallback<ClickEvent>(_ => { Clear(); });
|
||||
|
||||
m_ScrollView = m_Root.Q<ScrollView>("console-scrollview");
|
||||
|
||||
m_TextField.schedule.Execute(() => m_TextField.Focus()).StartingIn(1);
|
||||
}
|
||||
|
||||
public event Action<string> InputSubmitted = _ => { };
|
||||
public event Action ClearRequested = () => { };
|
||||
|
||||
public void SetMessageContent(string message) {
|
||||
if (m_LabelMessage == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_LabelMessage.text = message;
|
||||
m_ScrollView.schedule.Execute(() => { m_ScrollView.scrollOffset = new Vector2(0, m_ScrollView.contentContainer.contentRect.height); }).StartingIn(16);
|
||||
}
|
||||
|
||||
void Submit() {
|
||||
InputSubmitted.Invoke(m_TextField.value);
|
||||
m_TextField.value = string.Empty;
|
||||
m_TextField.Focus();
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
ClearRequested.Invoke();
|
||||
m_TextField.value = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/ConsoleUI/ConsoleVC.cs.meta
Normal file
3
Runtime/Engine/Code/ConsoleUI/ConsoleVC.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6131b8af74b4894ba7b95b12286bc66
|
||||
timeCreated: 1742461272
|
||||
Reference in New Issue
Block a user