using System; using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// Small UI Toolkit construction kit: fonts, buttons with sound, sliders, toggles and juicy animations. public static class UIK { public static Font Display, Body; public static VisualElement El(string classes = null, VisualElement parent = null) { var e = new VisualElement { pickingMode = PickingMode.Ignore }; AddClasses(e, classes); parent?.Add(e); return e; } public static Label Text(string text, string classes = null, VisualElement parent = null, bool display = true) { var l = new Label(text) { pickingMode = PickingMode.Ignore }; AddClasses(l, classes); SetFont(l, display); parent?.Add(l); return l; } public static void SetFont(VisualElement e, bool display) { var f = display ? Display : Body; if (f) e.style.unityFontDefinition = FontDefinition.FromFont(f); } public static Button Btn(string text, Action onClick, string classes = "btn", VisualElement parent = null, bool sound = true) { var b = new Button(() => { if (sound) Sfx.Play("ui_click", 0.8f, 1f, 0.04f); onClick?.Invoke(); }) { text = text }; b.focusable = false; AddClasses(b, classes); SetFont(b, true); b.RegisterCallback(_ => { if (b.enabledSelf) Sfx.Play("ui_hover", 0.35f, 1f, 0.08f, 0.03f); }); parent?.Add(b); return b; } public static VisualElement Img(Texture2D tex, string classes = null, VisualElement parent = null) { var e = El(classes, parent); if (tex) e.style.backgroundImage = new StyleBackground(tex); e.style.unityBackgroundScaleMode = ScaleMode.ScaleToFit; return e; } public static void AddClasses(VisualElement e, string classes) { if (string.IsNullOrEmpty(classes)) return; foreach (var c in classes.Split(' ')) if (c.Length > 0) e.AddToClassList(c); } // ───────────────────────── widgets ───────────────────────── /// Custom chunky slider. Returns a setter to update the value from code. public static Action Slider(VisualElement parent, string label, float value, float min, float max, Func fmt, Action changed) { var row = El("setting", parent); Text(label, "setting-label", row, false); var track = El("slider", row); track.pickingMode = PickingMode.Position; var fill = El("slider-fill", track); var knob = El("slider-knob", track); var val = Text("", "setting-value", row, false); float current = value; void Set(float v, bool notify) { current = Mathf.Clamp(v, min, max); float k = Mathf.InverseLerp(min, max, current); fill.style.width = Length.Percent(k * 100f); knob.style.left = Length.Percent(k * 100f); val.text = fmt(current); if (notify) changed?.Invoke(current); } bool dragging = false; void FromPointer(Vector2 local) { float w = track.resolvedStyle.width; if (w <= 1f) return; Set(Mathf.Lerp(min, max, Mathf.Clamp01(local.x / w)), true); } track.RegisterCallback(e => { dragging = true; track.CapturePointer(e.pointerId); FromPointer(e.localPosition); Sfx.Play("ui_hover", 0.3f); }); track.RegisterCallback(e => { if (dragging) FromPointer(e.localPosition); }); track.RegisterCallback(e => { dragging = false; track.ReleasePointer(e.pointerId); Sfx.Play("ui_click", 0.4f, 1.2f); }); Set(value, false); return v => Set(v, false); } public static void Toggle(VisualElement parent, string label, bool value, Action changed) { var row = El("setting", parent); Text(label, "setting-label", row, false); var t = El("toggle", row); t.pickingMode = PickingMode.Position; El("toggle-knob", t); bool on = value; t.EnableInClassList("toggle-on", on); t.RegisterCallback(_ => { on = !on; t.EnableInClassList("toggle-on", on); Sfx.Play("ui_click", 0.7f, on ? 1.15f : 0.9f); changed?.Invoke(on); }); } public static void Choice(VisualElement parent, string label, string[] options, int value, Action changed) { var row = El("setting", parent); Text(label, "setting-label", row, false); var box = El(null, row); box.style.flexDirection = FlexDirection.Row; var buttons = new Button[options.Length]; for (int i = 0; i < options.Length; i++) { int idx = i; buttons[i] = Btn(options[i], () => { for (int k = 0; k < buttons.Length; k++) buttons[k].EnableInClassList("tab-on", k == idx); changed?.Invoke(idx); }, "tab", box); buttons[i].EnableInClassList("tab-on", i == value); } } // ───────────────────────── animation ───────────────────────── public static void PopIn(VisualElement e, float delay = 0f, float duration = 0.42f) { e.style.scale = new Scale(Vector3.zero); e.style.opacity = 0f; Tween.Run(e, duration, k => { e.style.scale = new Scale(new Vector3(k, k, 1)); e.style.opacity = Mathf.Clamp01(k * 2f); }, Ease.OutBack, delay); } public static void PopOut(VisualElement e, Action done = null, float duration = 0.18f) { Tween.Kill(e); Tween.Run(e, duration, k => { float s = 1f - k; e.style.scale = new Scale(new Vector3(s, s, 1)); e.style.opacity = 1f - k; }, Ease.InBack, 0f, done); } public static void SlideIn(VisualElement e, Vector2 from, float delay = 0f, float duration = 0.5f) { e.style.translate = new Translate(from.x, from.y); e.style.opacity = 0f; Tween.Run(e, duration, k => { e.style.translate = new Translate(Mathf.LerpUnclamped(from.x, 0, k), Mathf.LerpUnclamped(from.y, 0, k)); e.style.opacity = Mathf.Clamp01(k * 1.5f); }, Ease.OutBack, delay); } public static void Punch(VisualElement e, float amount = 0.25f, float duration = 0.35f) { Tween.Kill(e); // it may have cut a pop-in short: never leave the element see-through e.style.opacity = 1f; Tween.Run(e, duration, k => { float s = 1f + Mathf.Sin(k * Mathf.PI * 2.5f) * (1f - k) * amount; e.style.scale = new Scale(new Vector3(s, s, 1)); }, Ease.Linear, 0f, () => e.style.scale = new Scale(Vector3.one)); } public static void Fade(VisualElement e, float to, float duration, Action done = null) { float from = e.resolvedStyle.opacity; Tween.Kill(e); Tween.Run(e, duration, k => e.style.opacity = Mathf.Lerp(from, to, k), Ease.InOutQuad, 0f, done); } /// Grass pays in cents, so small sums show them. Separators follow the interface language. public static string Money(double v) { if (v >= 100_000d) return Money((long)v); return "$" + v.ToString(System.Math.Abs(v - System.Math.Round(v)) < 0.005d ? "N0" : "N2", Loc.Culture); } public static string Money(long v) { var c = Loc.Culture; if (v >= 1_000_000) return Loc.F("${0}M", (v / 1_000_000f).ToString(v >= 10_000_000 ? "0.#" : "0.##", c)); if (v >= 100_000) return Loc.F("${0}K", (v / 1000f).ToString("0", c)); return "$" + v.ToString("N0", c); } /// A percentage the way the language writes it: "45%", "45 %", "%45". public static string Pct(float v01) => Loc.F("{0}%", Mathf.RoundToInt(v01 * 100f)); } }