- Robot docks and drone pads with no belt ship straight to Earth; robots widen their rings up to 4x once everything in reach is mown, travel 2.5x faster empty, hoppers 60/150 kg. Drone reach 70 m + 25 m per upgrade. - New Sky Mower: a big mowing drone on a pad, 91 sectors, flies over the grass. - Cut paint follows the blade as a strip, no disc ahead of the machine. - Sector toasts aggregate, chimes and till sounds throttled, toasts moved and narrowed. - Menus no longer rebuild on every money change (price tint follows the wallet), scroll position kept. - Map drawn as one mesh, opens near the player, ME / + / - / GLOBE, scale bar. - Key-art still (x_key) and -shotsize for the store stills; -demobots test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
262 lines
11 KiB
C#
262 lines
11 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>Small UI Toolkit construction kit: fonts, buttons with sound, sliders, toggles and juicy animations.</summary>
|
|
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<PointerEnterEvent>(_ => { if (b.enabledSelf) Sfx.Play("ui_hover", 0.35f, 1f, 0.08f, 0.03f); });
|
|
parent?.Add(b);
|
|
return b;
|
|
}
|
|
|
|
// ───────────────────────── prices that follow the wallet ─────────────────────────
|
|
|
|
/// <summary>A price on something: the wallet only recolours it (<see cref="FollowWallet"/>), it never rebuilds the card.</summary>
|
|
sealed class Price { public double cost; public string yes, no; }
|
|
|
|
/// <summary>
|
|
/// Mark a button (green when you can pay, grey when you can't) or a tag (with <paramref name="yes"/> when you can) as
|
|
/// costing this much. The money changes every second once robots and belts run; rebuilding a card for that threw
|
|
/// away the scroll and the button under the cursor, so a click landed one time in two.
|
|
/// </summary>
|
|
public static T Costs<T>(T e, double cost, string yes = "btn-green", string no = "btn-grey") where T : VisualElement
|
|
{
|
|
e.AddToClassList("costs");
|
|
e.userData = new Price { cost = cost, yes = yes, no = no };
|
|
Tint(e, TeamState.Instance ? TeamState.Instance.Money.Value : 0d);
|
|
return e;
|
|
}
|
|
|
|
static void Tint(VisualElement e, double money)
|
|
{
|
|
if (!(e.userData is Price p)) return;
|
|
bool can = money >= p.cost;
|
|
if (!string.IsNullOrEmpty(p.yes)) e.EnableInClassList(p.yes, can);
|
|
if (!string.IsNullOrEmpty(p.no)) e.EnableInClassList(p.no, !can);
|
|
}
|
|
|
|
/// <summary>Recolour every price on the card a few times a second as the wallet moves.</summary>
|
|
public static void FollowWallet(VisualElement card, long everyMs = 250)
|
|
{
|
|
card.schedule.Execute(() =>
|
|
{
|
|
var ts = TeamState.Instance;
|
|
if (!ts) return;
|
|
double money = ts.Money.Value;
|
|
card.Query(className: "costs").ForEach(e => Tint(e, money));
|
|
}).Every(everyMs);
|
|
}
|
|
|
|
/// <summary>Can the team pay this right now (checked when the button is pressed, not when it was drawn).</summary>
|
|
public static bool CanPay(double cost) => TeamState.Instance && TeamState.Instance.Money.Value >= cost;
|
|
|
|
/// <summary>Rebuild what is inside a scroll view without it jumping back to the top.</summary>
|
|
public static void KeepScroll(ScrollView list, Vector2 offset)
|
|
{
|
|
if (list == null) return;
|
|
list.scrollOffset = offset;
|
|
list.schedule.Execute(() => list.scrollOffset = offset);
|
|
}
|
|
|
|
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 ─────────────────────────
|
|
|
|
/// <summary>Custom chunky slider. Returns a setter to update the value from code.</summary>
|
|
public static Action<float> Slider(VisualElement parent, string label, float value, float min, float max, Func<float, string> fmt, Action<float> 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<PointerDownEvent>(e => { dragging = true; track.CapturePointer(e.pointerId); FromPointer(e.localPosition); Sfx.Play("ui_hover", 0.3f); });
|
|
track.RegisterCallback<PointerMoveEvent>(e => { if (dragging) FromPointer(e.localPosition); });
|
|
track.RegisterCallback<PointerUpEvent>(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<bool> 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<ClickEvent>(_ =>
|
|
{
|
|
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<int> 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);
|
|
}
|
|
|
|
/// <summary>Grass pays in cents, so small sums show them. Separators follow the interface language.</summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>A percentage the way the language writes it: "45%", "45 %", "%45".</summary>
|
|
public static string Pct(float v01) => Loc.F("{0}%", Mathf.RoundToInt(v01 * 100f));
|
|
}
|
|
}
|