using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
namespace GrassCutter
{
/// What a key can be set to do. The order is the order on the Keys tab.
public enum Act
{
Forward, Back, Left, Right, Jump, Sprint,
Use, Alt, Interact, Pack, Boots, Lamp,
Build, Category, Rotate, Free, MovePiece, Copy,
View, Cruise,
Stats, Map, Notebook, Phone, PhoneTap, PhoneTurn,
}
///
/// The player's keys: two per action (main and spare), kept as "k_<Act>_0/1". A code is a keyboard ,
/// or 1000 + a mouse button (1 left, 2 right, 3 middle, 4 back, 5 forward), 0 for nothing. The gamepad isn't rebound.
///
public static class Binds
{
public const int Mouse = 1000;
public static readonly int Count = Enum.GetValues(typeof(Act)).Length;
public struct Group { public string title; public Act[] acts; }
/// The Keys tab, a heading at a time.
public static readonly Group[] Groups =
{
new Group { title = Loc.N("MOVING"), acts = new[] { Act.Forward, Act.Back, Act.Left, Act.Right, Act.Jump, Act.Sprint } },
new Group { title = Loc.N("WORK"), acts = new[] { Act.Use, Act.Alt, Act.Interact, Act.Pack, Act.Boots, Act.Lamp } },
new Group { title = Loc.N("BUILDING"), acts = new[] { Act.Build, Act.Category, Act.Rotate, Act.Free, Act.MovePiece, Act.Copy } },
new Group { title = Loc.N("DRIVING"), acts = new[] { Act.View, Act.Cruise } },
new Group { title = Loc.N("SCREENS"), acts = new[] { Act.Stats, Act.Map, Act.Notebook, Act.Phone, Act.PhoneTap, Act.PhoneTurn } },
};
public static string Name(Act a) => Loc.T(a switch
{
Act.Forward => Loc.N("Forward"),
Act.Back => Loc.N("Back"),
Act.Left => Loc.N("Left"),
Act.Right => Loc.N("Right"),
Act.Jump => Loc.N("Jump"),
Act.Sprint => Loc.N("Run · machine boost"),
Act.Use => Loc.N("Use the tool · place"),
Act.Alt => Loc.N("Second action · remove · throw"),
Act.Interact => Loc.N("Interact"),
Act.Pack => Loc.N("Tie a bundle"),
Act.Boots => Loc.N("Gravity boots · garage"),
Act.Lamp => Loc.N("Lamp"),
Act.Build => Loc.N("Build"),
Act.Category => Loc.N("Next category"),
Act.Rotate => Loc.N("Turn"),
Act.Free => Loc.N("Free placement"),
Act.MovePiece => Loc.N("Move a piece · tip the bed"),
Act.Copy => Loc.N("Copy a piece"),
Act.View => Loc.N("Driving view"),
Act.Cruise => Loc.N("Cruise control"),
Act.Stats => Loc.N("Stats"),
Act.Map => Loc.N("Map"),
Act.Notebook => Loc.N("Notebook"),
Act.Phone => Loc.N("Phone"),
Act.PhoneTap => Loc.N("Tap the phone"),
Act.PhoneTurn => Loc.N("Turn the phone"),
_ => a.ToString(),
});
static int[] Default(Act a) => a switch
{
Act.Forward => new[] { (int)Key.W, (int)Key.UpArrow },
Act.Back => new[] { (int)Key.S, (int)Key.DownArrow },
Act.Left => new[] { (int)Key.A, (int)Key.LeftArrow },
Act.Right => new[] { (int)Key.D, (int)Key.RightArrow },
Act.Jump => new[] { (int)Key.Space, 0 },
Act.Sprint => new[] { (int)Key.LeftShift, 0 },
Act.Use => new[] { Mouse + 1, 0 },
Act.Alt => new[] { Mouse + 2, 0 },
Act.Interact => new[] { (int)Key.E, 0 },
Act.Pack => new[] { (int)Key.Q, 0 },
Act.Boots => new[] { (int)Key.G, 0 },
Act.Lamp => new[] { (int)Key.N, 0 },
Act.Build => new[] { (int)Key.B, 0 },
Act.Category => new[] { (int)Key.Q, 0 },
Act.Rotate => new[] { (int)Key.R, 0 },
Act.Free => new[] { (int)Key.F, 0 },
Act.MovePiece => new[] { (int)Key.T, 0 },
Act.Copy => new[] { Mouse + 3, 0 },
Act.View => new[] { (int)Key.R, 0 },
Act.Cruise => new[] { (int)Key.C, 0 },
Act.Stats => new[] { (int)Key.Tab, 0 },
Act.Map => new[] { (int)Key.M, 0 },
Act.Notebook => new[] { (int)Key.H, 0 },
Act.Phone => new[] { (int)Key.V, 0 },
Act.PhoneTap => new[] { (int)Key.X, 0 },
Act.PhoneTurn => new[] { (int)Key.Z, 0 },
_ => new[] { 0, 0 },
};
static int[,] s_codes;
/// A key has been set or the defaults are back: whatever shows a key name redraws.
public static event Action Changed;
static void Load()
{
if (s_codes != null) return;
s_codes = new int[Count, 2];
for (int i = 0; i < Count; i++)
{
var d = Default((Act)i);
for (int s = 0; s < 2; s++)
{
int c = PlayerPrefs.GetInt(Pref((Act)i, s), d[s]);
s_codes[i, s] = Valid(c) ? c : d[s];
}
}
}
static string Pref(Act a, int slot) => "k_" + a + "_" + slot;
static bool Valid(int c) => c == 0 || (c > Mouse && c <= Mouse + 5) || (c > 0 && c < Mouse && Enum.IsDefined(typeof(Key), c) && (Key)c != Key.None);
public static int Get(Act a, int slot) { Load(); return s_codes[(int)a, slot]; }
public static void Set(Act a, int slot, int code)
{
Load();
if (!Valid(code)) return;
s_codes[(int)a, slot] = code;
PlayerPrefs.SetInt(Pref(a, slot), code);
Changed?.Invoke();
}
public static void ResetAll()
{
Load();
for (int i = 0; i < Count; i++)
for (int s = 0; s < 2; s++)
{
PlayerPrefs.DeleteKey(Pref((Act)i, s));
s_codes[i, s] = Default((Act)i)[s];
}
Changed?.Invoke();
}
// ───────────────────────── reading ─────────────────────────
static ButtonControl Control(int code)
{
if (code <= 0) return null;
if (code > Mouse)
{
var m = UnityEngine.InputSystem.Mouse.current;
if (m == null) return null;
return (code - Mouse) switch { 1 => m.leftButton, 2 => m.rightButton, 3 => m.middleButton, 4 => m.backButton, 5 => m.forwardButton, _ => null };
}
var k = Keyboard.current;
return k != null ? k[(Key)code] : null;
}
public static bool Held(Act a)
{
Load();
for (int s = 0; s < 2; s++) { var c = Control(s_codes[(int)a, s]); if (c != null && c.isPressed) return true; }
return false;
}
public static bool Pressed(Act a)
{
Load();
for (int s = 0; s < 2; s++) { var c = Control(s_codes[(int)a, s]); if (c != null && c.wasPressedThisFrame) return true; }
return false;
}
// ───────────────────────── names ─────────────────────────
/// The main key of an action, as a key cap shows it (the spare if the main is empty).
public static string Cap(Act a)
{
int c = Get(a, 0);
if (c == 0) c = Get(a, 1);
return KeyName(c);
}
/// A key as printed on it: a letter as this keyboard's layout puts it there when that's a Latin letter
/// (AZERTY shows its A where QWERTY has Q), otherwise the US name, so a Russian layout still reads W, not Ц.
public static string KeyName(int code)
{
if (code <= 0) return "—";
if (code > Mouse)
return Loc.T((code - Mouse) switch { 1 => Loc.N("LMB"), 2 => Loc.N("RMB"), 3 => Loc.N("MMB"), 4 => Loc.N("Mouse 4"), _ => Loc.N("Mouse 5") });
var key = (Key)code;
if (key >= Key.A && key <= Key.Z)
{
var kc = Keyboard.current?[key];
string shown = kc?.displayName;
if (!string.IsNullOrEmpty(shown) && shown.Length == 1 && shown[0] < 128 && char.IsLetter(shown[0])) return shown.ToUpperInvariant();
return key.ToString();
}
if (key >= Key.Digit1 && key <= Key.Digit0) return key == Key.Digit0 ? "0" : ((int)(key - Key.Digit1) + 1).ToString();
if (key >= Key.Numpad0 && key <= Key.Numpad9) return "Num " + (int)(key - Key.Numpad0);
if (key >= Key.F1 && key <= Key.F12) return key.ToString();
return key switch
{
Key.Space => Loc.T("Space"),
Key.LeftShift => "Shift",
Key.RightShift => "R Shift",
Key.LeftCtrl => "Ctrl",
Key.RightCtrl => "R Ctrl",
Key.LeftAlt => "Alt",
Key.RightAlt => "R Alt",
Key.Tab => "Tab",
Key.Enter => "Enter",
Key.NumpadEnter => "Num Enter",
Key.Backspace => "Backspace",
Key.CapsLock => "Caps Lock",
Key.UpArrow => Loc.T("Arrow up"),
Key.DownArrow => Loc.T("Arrow down"),
Key.LeftArrow => Loc.T("Arrow left"),
Key.RightArrow => Loc.T("Arrow right"),
Key.Backquote => "`",
Key.Minus => "-",
Key.Equals => "=",
Key.LeftBracket => "[",
Key.RightBracket => "]",
Key.Semicolon => ";",
Key.Quote => "'",
Key.Backslash => "\\",
Key.Comma => ",",
Key.Period => ".",
Key.Slash => "/",
Key.Insert => "Insert",
Key.Delete => "Delete",
Key.Home => "Home",
Key.End => "End",
Key.PageUp => "Page Up",
Key.PageDown => "Page Down",
_ => key.ToString(),
};
}
/// Whatever key or mouse button went down this frame (not Esc), or 0.
public static int Pressed()
{
var m = UnityEngine.InputSystem.Mouse.current;
if (m != null)
{
if (m.leftButton.wasPressedThisFrame) return Mouse + 1;
if (m.rightButton.wasPressedThisFrame) return Mouse + 2;
if (m.middleButton.wasPressedThisFrame) return Mouse + 3;
if (m.backButton.wasPressedThisFrame) return Mouse + 4;
if (m.forwardButton.wasPressedThisFrame) return Mouse + 5;
}
var k = Keyboard.current;
if (k == null) return 0;
foreach (var kc in k.allKeys)
if (kc != null && kc.wasPressedThisFrame && kc.keyCode != Key.Escape && kc.keyCode != Key.None) return (int)kc.keyCode;
return 0;
}
}
}