using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// /// Drop-down cheat console (~ / Ё / F1). Enter runs a line, Tab completes, ↑↓ walk the history, ~ or Esc closes. /// The log and the history survive closing it. /// public class CheatConsole { static readonly List s_log = new List(); static readonly List s_history = new List(); const int MaxLog = 200; public static IReadOnlyList Log => s_log; public readonly VisualElement Panel; readonly ScrollView _scroll; readonly TextField _input; readonly Label _hint; int _historyAt; public CheatConsole(VisualElement layer) { layer.style.justifyContent = Justify.FlexStart; Panel = UIK.El("console", layer); Panel.pickingMode = PickingMode.Position; var head = UIK.El("console-head", Panel); UIK.Text("CHEAT CONSOLE", "console-title", head); UIK.Text("help · Tab completes · ↑↓ history · ~ closes", "console-sub", head, false); _scroll = new ScrollView(ScrollViewMode.Vertical); _scroll.AddToClassList("console-log"); _scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden; Panel.Add(_scroll); var row = UIK.El("console-row", Panel); UIK.Text(">", "console-prompt", row); _input = new TextField(); _input.AddToClassList("console-input"); UIK.SetFont(_input, false); row.Add(_input); _hint = UIK.Text("", "console-hint", Panel, false); if (s_log.Count == 0) Print("Type help for the list of cheats. Most of them only work for the host."); foreach (var l in s_log) AddLine(l); _input.RegisterCallback(OnKey, TrickleDown.TrickleDown); // Tab would move focus away: keep it in the field _input.RegisterCallback(e => e.StopPropagation(), TrickleDown.TrickleDown); _input.RegisterValueChangedCallback(e => { // the toggle key types itself into the field on the way in/out string clean = e.newValue.Replace("`", "").Replace("~", "").Replace("ё", "").Replace("Ё", ""); if (clean != e.newValue) _input.SetValueWithoutNotify(clean); UpdateHint(clean); }); _historyAt = s_history.Count; Focus(); ScrollToEnd(); } public void Focus() => _input.schedule.Execute(() => { _input.Focus(); _input.SelectRange(_input.value.Length, _input.value.Length); }).ExecuteLater(30); void OnKey(KeyDownEvent e) { switch (e.keyCode) { case KeyCode.Return: case KeyCode.KeypadEnter: Submit(_input.value); e.StopImmediatePropagation(); Focus(); break; case KeyCode.Tab: { var done = Cheats.Complete(_input.value, out var options); _input.SetValueWithoutNotify(done); _input.SelectRange(done.Length, done.Length); _hint.text = options.Count > 1 ? string.Join(" ", options) : ""; e.StopImmediatePropagation(); Focus(); break; } case KeyCode.UpArrow: if (s_history.Count == 0) break; _historyAt = Mathf.Max(0, _historyAt - 1); SetInput(s_history[_historyAt]); e.StopImmediatePropagation(); break; case KeyCode.DownArrow: if (s_history.Count == 0) break; _historyAt = Mathf.Min(s_history.Count, _historyAt + 1); SetInput(_historyAt < s_history.Count ? s_history[_historyAt] : ""); e.StopImmediatePropagation(); break; } } void SetInput(string s) { _input.SetValueWithoutNotify(s); _input.SelectRange(s.Length, s.Length); UpdateHint(s); } void UpdateHint(string line) { var parts = line.Trim().Split(' '); var cmd = parts.Length > 0 ? Cheats.All.Find(c => c.name == parts[0].ToLowerInvariant()) : null; _hint.text = cmd != null ? $"{cmd.name} {cmd.args} — {cmd.desc}" : ""; } /// Put text in the input line, as if typed (demo bot). public void Type(string s) => _input.value = s; /// Run a line as if typed (also used by the demo bot). public void Submit(string line) { line = (line ?? "").Trim(); _input.SetValueWithoutNotify(""); _hint.text = ""; if (line.Length == 0) return; if (s_history.Count == 0 || s_history[s_history.Count - 1] != line) s_history.Add(line); _historyAt = s_history.Count; Print($"> {line}"); string name = line.Split(' ')[0].ToLowerInvariant(); if (name == "clear") { s_log.Clear(); _scroll.Clear(); return; } string result = Cheats.Run(line); if (!string.IsNullOrEmpty(result)) Print(result); Sfx.Play(result != null && result.Contains("#FF8A7A") ? "error" : "ui_click", 0.5f); } void Print(string text) { s_log.Add(text); if (s_log.Count > MaxLog) s_log.RemoveAt(0); AddLine(text); ScrollToEnd(); } void AddLine(string text) { var l = UIK.Text(text, "console-line", null, false); l.enableRichText = true; _scroll.Add(l); while (_scroll.contentContainer.childCount > MaxLog) _scroll.contentContainer.RemoveAt(0); } void ScrollToEnd() => _scroll.schedule.Execute(() => _scroll.scrollOffset = new Vector2(0, float.MaxValue)).ExecuteLater(20); } }