Files
grass-cutters/Assets/Scripts/UI/PhoneScreen.cs
Bonchellon 52619d8952 Hologram calls, HaulOS phone, Enhanced lighting, smoother camera; Blender remaster review files
Game:
- Assistant and Director portraits: GPT backgrounds and a holographic appear/disappear shader (Talk/TalkHolo) instead of on/off
- Phone redesigned (HaulOS home, address bar, dock, error/loading states) and the browser link made sturdier
- Settings > Graphics > Lighting: Classic / Enhanced (runtime volume, no project assets changed); Enhanced SSAO uses the
  smooth interleaved-gradient method at high samples so flat panels don't speckle
- Painted board textures (T_HullPanel, T_BarnRed) keep only edge wear: the big bare-wood patches read as dirty smudges
- The head leans into a sidestep smoothly instead of snapping with A / D
- New keys in all 11 languages

Review only (nothing swapped into the game yet): ArtSource/Blender/remaster (v1 paint pass of our own models, Synty-built
ones left out), remaster/crew (crew repaint with arm, colour-edge and face fixes), remaster_v2 (smooth-geometry pilot).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 15:28:47 +03:00

1058 lines
50 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace GrassCutter
{
/// <summary>
/// What's on the phone: HaulOS, a UI Toolkit panel drawn into a texture on the display.
/// Home: the wallpaper, a big clock, a widget with how much of the planet is mown and the company's money, a search
/// bar, app icons for the sites and a dock (map, notebook, browser).
/// Uplink, a page: the address bar on top (reload / stop in it), the page, a navigation bar under it; a splash with
/// the app's icon until the page's first frame is in, and a card with "try again" when it can't be reached.
/// Tapping goes through our own hit test, because the phone is a 3D object swinging about: a button is an element with
/// an Action in its userData. A button gives under the finger when pressed and acts when let go over it, like a phone's.
/// </summary>
public class PhoneScreen
{
public const int W = 720, H = 1578; // the same shape as Phone.ScreenW and ScreenH
const int StatusH = 62, TopH = 150, DockH = 132, WebH = H - TopH - DockH;
static readonly Color Ink = new Color(0.97f, 0.98f, 1f), Dim = new Color(0.8f, 0.86f, 0.92f, 0.78f);
static readonly Color Mint = new Color(0.45f, 1f, 0.72f), Hay = new Color(1f, 0.8f, 0.36f), Red = new Color(1f, 0.38f, 0.34f);
static readonly Color Bar = new Color(0.035f, 0.05f, 0.075f, 0.96f), Card = new Color(0.05f, 0.08f, 0.13f, 0.62f);
static readonly Color Line = new Color(1f, 1f, 1f, 0.1f), Deep = new Color(0.02f, 0.035f, 0.06f);
static readonly (string name, string url, Color colour, Action<Painter2D, float> glyph)[] Sites =
{
("YouTube", "https://m.youtube.com/", new Color(0.93f, 0.2f, 0.2f), Play),
("Shorts", "https://m.youtube.com/shorts", new Color(1f, 0.42f, 0.2f), Feed),
("Twitch", "https://m.twitch.tv/", new Color(0.55f, 0.34f, 0.95f), Live),
("TikTok", "https://www.tiktok.com/foryou", new Color(0.1f, 0.12f, 0.16f), Note2),
("Instagram", "https://www.instagram.com/reels/", new Color(0.88f, 0.28f, 0.6f), Lens),
("Reddit", "https://www.reddit.com/", new Color(1f, 0.45f, 0.12f), Talk),
("Google", "https://www.google.com/", new Color(0.26f, 0.52f, 0.96f), Find),
("Wikipedia", "https://www.wikipedia.org/", new Color(0.5f, 0.55f, 0.6f), Book),
};
public RenderTexture Tex { get; }
/// <summary>The display's root, for whatever takes the whole screen over (a call).</summary>
public VisualElement Root => _root;
public PhoneWeb Web { get; }
/// <summary>A page is up, rather than the home screen.</summary>
public bool ShowingPage => _page;
public bool Wide => _wide;
/// <summary>A text box has the keyboard.</summary>
public bool Typing => _addressing || _editing;
readonly VisualElement _root, _home, _top, _dock, _view, _progress, _status, _unturn, _splash, _error, _chip, _spinner, _splashIcon;
readonly VisualElement _ring, _reload, _signal, _homeGrid;
readonly Label _clock, _homeClock, _homeDate, _address, _search, _note, _mown, _money, _shipped, _link, _splashName, _errorText, _chipText, _toast;
readonly Label _mownTitle, _fundsTitle, _retryLabel, _homeLabel;
readonly List<Label> _appLabels = new List<Label>();
readonly VisualElement[] _bars = new VisualElement[4];
VisualElement _hover, _pressed;
Label _typeInto;
// every label with whether it's in the display face: a new language may bring new letters
readonly List<(Label label, bool display)> _labels = new List<(Label, bool)>();
string _lang;
bool _page, _wide, _visible, _focus, _touching, _addressing, _editing, _selectAll, _awaiting;
string _typed = "", _shownUrl, _openedUrl;
Vector2 _touchAt;
float _nextStats, _mown01, _progressAt, _openT = 1f, _homeT = 1f, _toastUntil;
int _fieldAsk;
public PhoneScreen(GameObject host)
{
Tex = new RenderTexture(W, H, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB) { name = "PhoneScreen", useMipMap = true, autoGenerateMips = true, anisoLevel = 4 };
Tex.Create();
Web = new PhoneWeb((float)WebH / W, (float)H / W);
var main = UnityEngine.Object.FindAnyObjectByType<UIDocument>(FindObjectsInactive.Include);
var ps = UnityEngine.Object.Instantiate(main.panelSettings);
ps.targetTexture = Tex;
ps.scaleMode = PanelScaleMode.ConstantPixelSize;
ps.scale = 1f;
ps.clearColor = true;
ps.colorClearValue = Color.black;
ps.sortingOrder = -10;
var go = new GameObject("PhoneUI");
go.SetActive(false);
go.transform.SetParent(host.transform, false);
var doc = go.AddComponent<UIDocument>();
doc.panelSettings = ps;
go.SetActive(true);
_root = doc.rootVisualElement;
_root.style.width = W; _root.style.height = H;
_root.style.backgroundColor = Deep;
UIK.SetFont(_root, false);
// ── home ──
_home = Abs(_root);
_home.pickingMode = PickingMode.Ignore;
var wall = AssetBank.I.Tex("phone_wallpaper");
if (wall) { _home.style.backgroundImage = new StyleBackground(wall); _home.style.unityBackgroundScaleMode = ScaleMode.ScaleAndCrop; }
var body = El(_home);
body.pickingMode = PickingMode.Ignore;
body.style.flexGrow = 1;
body.style.paddingLeft = 40; body.style.paddingRight = 40; body.style.paddingTop = 104;
body.style.alignItems = Align.Center;
_homeClock = T("0:00", 172, Ink, body, true);
Shadow(_homeClock);
_homeDate = T("HERBARIA-7", 26, Ink, body, true);
_homeDate.style.letterSpacing = 3f;
_homeDate.style.marginBottom = 40;
Shadow(_homeDate);
// the widget: the planet's ring and the company's purse
var widget = Row(body);
widget.style.width = Length.Percent(100);
widget.style.height = 214;
widget.style.marginBottom = 26;
widget.style.paddingLeft = 26; widget.style.paddingRight = 26;
widget.style.alignItems = Align.Center;
Glass(widget, 38);
_ring = El(widget);
_ring.pickingMode = PickingMode.Ignore;
_ring.style.width = 156; _ring.style.height = 156;
_ring.style.alignItems = Align.Center; _ring.style.justifyContent = Justify.Center;
_ring.generateVisualContent += DrawRing;
_mown = T("0%", 25, Ink, _ring, true);
var words = El(widget);
words.pickingMode = PickingMode.Ignore;
words.style.flexGrow = 1;
words.style.marginLeft = 26;
_mownTitle = T(Loc.T("PLANET MOWN"), 20, Mint, words, true);
_mownTitle.style.letterSpacing = 2f;
_fundsTitle = T(Loc.T("COMPANY FUNDS"), 20, Hay, words, true);
_fundsTitle.style.letterSpacing = 2f;
_fundsTitle.style.marginTop = 6;
_money = T("$0", 54, Ink, words, true);
_shipped = T("", 22, Dim, words, false);
var search = El(body);
search.style.width = Length.Percent(100);
search.style.height = 88;
search.style.marginBottom = 34;
search.style.paddingLeft = 30;
search.style.flexDirection = FlexDirection.Row;
search.style.alignItems = Align.Center;
Glass(search, 44);
search.userData = (Action)(() => StartAddress(_search, false));
Glyph(search, 40, Ink, Find).style.marginRight = 18;
_search = T(Loc.T("Search or type a site"), 30, Dim, search, false);
_homeGrid = Row(body);
_homeGrid.style.width = Length.Percent(100);
_homeGrid.style.flexWrap = Wrap.Wrap;
foreach (var (name, url, colour, glyph) in Sites) App(_homeGrid, name, colour, glyph, () => Open(url, name, colour, glyph));
_note = T(Note, 22, Dim, body, false);
_note.style.whiteSpace = WhiteSpace.Normal;
_note.style.unityTextAlign = TextAnchor.MiddleCenter;
_note.style.marginTop = 4;
_note.style.maxWidth = 560;
Shadow(_note);
// the dock, clear of the cut corner at the bottom left
var dock = Row(_home);
dock.style.position = Position.Absolute;
dock.style.left = 74; dock.style.right = 34; dock.style.bottom = 34; dock.style.height = 176;
dock.style.justifyContent = Justify.SpaceAround;
dock.style.alignItems = Align.Center;
Glass(dock, 52);
dock.style.backgroundColor = new Color(0.05f, 0.08f, 0.13f, 0.5f);
App(dock, Loc.T("Map"), new Color(0.16f, 0.62f, 0.42f), Hex, () => { Phone.I.Show(PhoneMode.Side); UIRoot.I.OpenMap(); }, true);
App(dock, Loc.T("Notebook"), new Color(0.86f, 0.62f, 0.18f), Book, () => { Phone.I.Show(PhoneMode.Side); UIRoot.I.OpenNotebook(); }, true);
App(dock, Loc.T("Browser"), new Color(0.2f, 0.62f, 0.86f), Globe, () => StartAddress(_search, false), true);
// ── a page: the address on top, the navigation under it, and the page between them ──
_view = Abs(_root);
_view.pickingMode = PickingMode.Ignore;
_view.style.top = TopH; _view.style.bottom = StyleKeyword.Auto; _view.style.height = WebH;
_view.style.backgroundColor = Color.black;
_view.style.backgroundImage = new StyleBackground(Background.FromRenderTexture((RenderTexture)Web.Picture));
_view.style.unityBackgroundScaleMode = ScaleMode.StretchToFill;
// the app's splash, until the page's first frame is in
_splash = Abs(_view);
_splash.pickingMode = PickingMode.Ignore;
_splash.style.backgroundColor = Deep;
_splash.style.alignItems = Align.Center; _splash.style.justifyContent = Justify.Center;
_splashIcon = El(_splash);
_splashIcon.pickingMode = PickingMode.Ignore;
_splashIcon.style.width = 200; _splashIcon.style.height = 200;
_splashIcon.style.alignItems = Align.Center; _splashIcon.style.justifyContent = Justify.Center;
Round(_splashIcon, 52);
_splashName = T("", 40, Ink, _splash, true);
_splashName.style.marginTop = 26;
_spinner = El(_splash);
_spinner.pickingMode = PickingMode.Ignore;
_spinner.style.width = 70; _spinner.style.height = 70; _spinner.style.marginTop = 60;
_spinner.generateVisualContent += DrawSpinner;
// the page couldn't be had
_error = Abs(_view);
_error.pickingMode = PickingMode.Ignore;
_error.style.backgroundColor = new Color(0.02f, 0.035f, 0.06f, 0.94f);
_error.style.alignItems = Align.Center; _error.style.justifyContent = Justify.Center;
_error.style.paddingLeft = 60; _error.style.paddingRight = 60;
Glyph(_error, 120, Red, Broken).style.marginBottom = 30;
_errorText = T("", 34, Ink, _error, false);
_errorText.style.whiteSpace = WhiteSpace.Normal;
_errorText.style.unityTextAlign = TextAnchor.MiddleCenter;
_errorText.style.marginBottom = 44;
var choices = Row(_error);
_retryLabel = Pill(choices, Loc.T("Try again"), Mint, new Color(0.02f, 0.12f, 0.08f), () => { if (_openedUrl != null) Open(_openedUrl, _splashName.text, _splashIcon.resolvedStyle.backgroundColor, null); });
_retryLabel.parent.style.marginRight = 20;
_homeLabel = Pill(choices, Loc.T("Home"), new Color(1f, 1f, 1f, 0.12f), Ink, Home);
_unturn = El(_view);
_unturn.style.position = Position.Absolute;
_unturn.style.right = 28; _unturn.style.top = 28; _unturn.style.width = 92; _unturn.style.height = 92;
_unturn.style.alignItems = Align.Center; _unturn.style.justifyContent = Justify.Center;
Round(_unturn, 46);
_unturn.style.backgroundColor = new Color(0f, 0f, 0f, 0.55f);
_unturn.userData = (Action)Turn;
Glyph(_unturn, 52, Ink, Rotate);
_top = Abs(_root);
_top.pickingMode = PickingMode.Ignore;
_top.style.bottom = StyleKeyword.Auto; _top.style.height = TopH;
_top.style.backgroundColor = Bar;
_top.style.justifyContent = Justify.FlexEnd;
_top.style.paddingLeft = 18; _top.style.paddingRight = 18; _top.style.paddingBottom = 12;
var pill = El(_top);
pill.style.height = 70;
pill.style.paddingLeft = 24; pill.style.paddingRight = 6;
pill.style.flexDirection = FlexDirection.Row;
pill.style.alignItems = Align.Center;
Round(pill, 35);
pill.style.backgroundColor = new Color(1f, 1f, 1f, 0.09f);
pill.userData = (Action)(() => StartAddress(_address, true));
var lockGlyph = Glyph(pill, 28, Mint, Lock);
lockGlyph.style.marginRight = 14;
lockGlyph.style.flexShrink = 0;
_address = T("", 28, Ink, pill, false);
_address.style.flexGrow = 1;
// a long address gives way, not the lock or the reload button
_address.style.flexShrink = 1;
_address.style.minWidth = 0;
_address.style.overflow = Overflow.Hidden;
_address.style.textOverflow = TextOverflow.Ellipsis;
_reload = El(pill);
_reload.style.width = 60; _reload.style.height = 60;
_reload.style.flexShrink = 0;
_reload.style.alignItems = Align.Center; _reload.style.justifyContent = Justify.Center;
Round(_reload, 30);
_reload.userData = (Action)(() => { if (Web.Loading) Web.Stop(); else Web.Reload(); });
_reload.generateVisualContent += ctx =>
{
var p = ctx.painter2D;
float s = _reload.contentRect.width / 100f * 0.62f;
p.strokeColor = Ink; p.fillColor = Ink; p.lineWidth = 9f * s;
p.lineCap = LineCap.Round; p.lineJoin = LineJoin.Round;
var o = new Vector2(_reload.contentRect.width * 0.19f, _reload.contentRect.height * 0.19f);
if (Web.Loading) { Seg(p, o, s, 22, 22, 78, 78); Seg(p, o, s, 78, 22, 22, 78); }
else ReloadAt(p, o, s);
};
_progress = El(_root);
_progress.pickingMode = PickingMode.Ignore;
_progress.style.position = Position.Absolute;
_progress.style.top = TopH - 4; _progress.style.height = 4; _progress.style.left = 0;
_progress.style.width = Length.Percent(0);
_progress.style.backgroundColor = Mint;
_dock = Abs(_root);
_dock.pickingMode = PickingMode.Ignore;
_dock.style.top = StyleKeyword.Auto; _dock.style.height = DockH;
_dock.style.backgroundColor = Bar;
_dock.style.flexDirection = FlexDirection.Row;
_dock.style.justifyContent = Justify.SpaceAround;
_dock.style.alignItems = Align.Center;
_dock.style.paddingLeft = 60; _dock.style.paddingRight = 10; _dock.style.paddingBottom = 8;
Nav(Back, () => Web.Back(), false);
Nav(Forward, () => Web.Forward(), false);
Nav(HomeGlyph, Home, true);
Nav(Rotate, Turn, false);
// what the keyboard is doing
_chip = El(_root);
_chip.pickingMode = PickingMode.Ignore;
_chip.style.position = Position.Absolute;
_chip.style.left = 70; _chip.style.right = 40; _chip.style.bottom = DockH + 24;
_chip.style.height = 64;
_chip.style.alignItems = Align.Center; _chip.style.justifyContent = Justify.Center;
Round(_chip, 32);
_chip.style.backgroundColor = new Color(0.08f, 0.32f, 0.22f, 0.94f);
_chipText = T("", 24, Ink, _chip, false);
// a short note over everything (no browser, a site lost)
_toast = T("", 26, Ink, _root, false);
_toast.style.position = Position.Absolute;
_toast.style.left = 70; _toast.style.right = 40; _toast.style.top = 92;
_toast.style.paddingLeft = 26; _toast.style.paddingRight = 26; _toast.style.paddingTop = 18; _toast.style.paddingBottom = 18;
_toast.style.whiteSpace = WhiteSpace.Normal;
_toast.style.unityTextAlign = TextAnchor.MiddleCenter;
_toast.style.backgroundColor = new Color(0.16f, 0.05f, 0.05f, 0.94f);
Round(_toast, 30);
// ── the strip over everything ──
_status = Abs(_root);
_status.pickingMode = PickingMode.Ignore;
_status.style.bottom = StyleKeyword.Auto; _status.style.height = StatusH;
_status.style.flexDirection = FlexDirection.Row;
_status.style.justifyContent = Justify.SpaceBetween;
_status.style.alignItems = Align.Center;
_status.style.paddingLeft = 44; _status.style.paddingRight = 112; _status.style.paddingTop = 10;
_clock = T("0:00", 28, Ink, _status, true);
var right = Row(_status);
right.style.alignItems = Align.Center;
_link = T(Loc.T("RELAY"), 20, Mint, right, true);
_link.style.letterSpacing = 3f;
_link.style.marginRight = 10;
_signal = Row(right);
_signal.style.alignItems = Align.FlexEnd;
_signal.style.height = 26;
_signal.style.marginRight = 14;
for (int i = 0; i < 4; i++)
{
var bar = El(_signal);
bar.pickingMode = PickingMode.Ignore;
bar.style.width = 6; bar.style.height = 8 + i * 6; bar.style.marginLeft = 3;
bar.style.backgroundColor = Ink;
Round(bar, 2);
_bars[i] = bar;
}
Glyph(right, 44, Ink, Battery);
_lang = Loc.Code;
ShowPage(false);
_homeT = 1f;
Tick();
}
static string Note => PhoneWeb.Available ? Loc.T("The whole internet, relayed from Earth. Log in once and it stays.") : Loc.T("The relay needs Microsoft Edge or Google Chrome on this PC.");
/// <summary>The phone outlives a change of language: its fixed words and letters are put right here.</summary>
void Relabel()
{
_lang = Loc.Code;
UIK.SetFont(_root, false);
foreach (var (l, display) in _labels) UIK.SetFont(l, display);
_mownTitle.text = Loc.T("PLANET MOWN");
_fundsTitle.text = Loc.T("COMPANY FUNDS");
_appLabels[Sites.Length].text = Loc.T("Map");
_appLabels[Sites.Length + 1].text = Loc.T("Notebook");
_appLabels[Sites.Length + 2].text = Loc.T("Browser");
_retryLabel.text = Loc.T("Try again");
_homeLabel.text = Loc.T("Home");
_note.text = Note;
if (!_addressing) _search.text = Loc.T("Search or type a site");
_nextStats = 0f;
}
// ───────────────────────── little helpers ─────────────────────────
static VisualElement El(VisualElement parent)
{
var e = new VisualElement();
parent?.Add(e);
return e;
}
static VisualElement Abs(VisualElement parent)
{
var e = El(parent);
e.style.position = Position.Absolute;
e.style.left = 0; e.style.right = 0; e.style.top = 0; e.style.bottom = 0;
return e;
}
static VisualElement Row(VisualElement parent)
{
var e = El(parent);
e.pickingMode = PickingMode.Ignore;
e.style.flexDirection = FlexDirection.Row;
return e;
}
static void Round(VisualElement e, float r)
{
e.style.borderTopLeftRadius = r; e.style.borderTopRightRadius = r;
e.style.borderBottomLeftRadius = r; e.style.borderBottomRightRadius = r;
}
static void Border(VisualElement e, float w, Color c)
{
e.style.borderTopWidth = w; e.style.borderBottomWidth = w; e.style.borderLeftWidth = w; e.style.borderRightWidth = w;
e.style.borderTopColor = c; e.style.borderBottomColor = c; e.style.borderLeftColor = c; e.style.borderRightColor = c;
}
/// <summary>Frosted glass over the wallpaper.</summary>
static void Glass(VisualElement e, float r)
{
Round(e, r);
e.style.backgroundColor = Card;
Border(e, 2, Line);
}
static void Shadow(Label l)
{
l.style.textShadow = new TextShadow { offset = new Vector2(0f, 3f), blurRadius = 8f, color = new Color(0f, 0f, 0f, 0.55f) };
}
Label T(string text, int size, Color c, VisualElement parent, bool display)
{
var l = new Label(text);
UIK.SetFont(l, display);
_labels.Add((l, display));
l.style.fontSize = size;
l.style.color = c;
l.style.marginBottom = 0; l.style.marginTop = 0; l.style.marginLeft = 0; l.style.marginRight = 0;
l.style.paddingBottom = 0; l.style.paddingTop = 0; l.style.paddingLeft = 0; l.style.paddingRight = 0;
l.pickingMode = PickingMode.Ignore;
parent?.Add(l);
return l;
}
/// <summary>An app: a rounded square in its colour, a sheen on its top half, its glyph in white, its name under it.</summary>
void App(VisualElement parent, string name, Color colour, Action<Painter2D, float> glyph, Action open, bool docked = false)
{
var cell = El(parent);
cell.style.width = docked ? 150 : Length.Percent(25);
cell.style.alignItems = Align.Center;
cell.style.marginBottom = docked ? 0 : 30;
cell.style.paddingTop = 6;
cell.userData = open;
var icon = El(cell);
icon.pickingMode = PickingMode.Ignore;
icon.style.width = 116; icon.style.height = 116;
icon.style.alignItems = Align.Center; icon.style.justifyContent = Justify.Center;
icon.style.backgroundColor = colour;
// no overflow:hidden for the round corners: clipping wants a stencil buffer, which the phone's texture hasn't
// got (the icons came out as white squares), so the sheen rounds its own top corners instead
Round(icon, 32);
Border(icon, 2, new Color(1f, 1f, 1f, 0.16f));
var sheen = Abs(icon);
sheen.pickingMode = PickingMode.Ignore;
sheen.style.bottom = Length.Percent(50);
sheen.style.backgroundColor = new Color(1f, 1f, 1f, 0.1f);
sheen.style.borderTopLeftRadius = sheen.style.borderTopRightRadius = 30;
Glyph(icon, 62, Color.white, glyph);
var l = T(name, 23, Ink, cell, false);
l.style.marginTop = 10;
Shadow(l);
_appLabels.Add(l);
}
void Nav(Action<Painter2D, float> glyph, Action click, bool main)
{
var b = El(_dock);
b.style.width = main ? 104 : 96; b.style.height = main ? 104 : 92;
b.style.alignItems = Align.Center; b.style.justifyContent = Justify.Center;
Round(b, main ? 52 : 46);
if (main) b.style.backgroundColor = new Color(1f, 1f, 1f, 0.1f);
b.userData = click;
Glyph(b, main ? 50 : 52, Ink, glyph);
}
/// <summary>A rounded button with words on it; returns the words, for the language.</summary>
Label Pill(VisualElement parent, string text, Color back, Color ink, Action click)
{
var b = El(parent);
b.style.height = 86;
b.style.paddingLeft = 44; b.style.paddingRight = 44;
b.style.alignItems = Align.Center; b.style.justifyContent = Justify.Center;
Round(b, 43);
b.style.backgroundColor = back;
b.userData = click;
return T(text, 30, ink, b, true);
}
// ───────────────────────── drawn bits ─────────────────────────
void DrawRing(MeshGenerationContext ctx)
{
var p = ctx.painter2D;
var r = _ring.contentRect;
var c = r.center;
float rad = r.width * 0.5f - 10f;
p.lineWidth = 14f;
p.lineCap = LineCap.Round;
p.strokeColor = new Color(1f, 1f, 1f, 0.12f);
p.BeginPath(); p.Arc(c, rad, Angle.Degrees(0f), Angle.Degrees(360f)); p.Stroke();
// a sliver at least, so the ring says where it starts
float to = -90f + 360f * Mathf.Clamp(_mown01, 0.015f, 1f);
p.strokeColor = Mint;
p.BeginPath(); p.Arc(c, rad, Angle.Degrees(-90f), Angle.Degrees(to)); p.Stroke();
}
void DrawSpinner(MeshGenerationContext ctx)
{
var p = ctx.painter2D;
var r = _spinner.contentRect;
float a = Time.unscaledTime * 320f;
p.lineWidth = 8f;
p.lineCap = LineCap.Round;
p.strokeColor = new Color(1f, 1f, 1f, 0.12f);
p.BeginPath(); p.Arc(r.center, r.width * 0.5f - 6f, Angle.Degrees(0f), Angle.Degrees(360f)); p.Stroke();
p.strokeColor = Mint;
p.BeginPath(); p.Arc(r.center, r.width * 0.5f - 6f, Angle.Degrees(a), Angle.Degrees(a + 100f)); p.Stroke();
}
// ───────────────────────── glyphs: our own, and drawn, so they stay sharp ─────────────────────────
static VisualElement Glyph(VisualElement parent, float size, Color c, Action<Painter2D, float> draw)
{
var e = El(parent);
e.pickingMode = PickingMode.Ignore;
e.style.width = size; e.style.height = size;
e.generateVisualContent += ctx =>
{
if (draw == null) return;
var p = ctx.painter2D;
float s = e.contentRect.width / 100f;
p.strokeColor = c; p.fillColor = c;
p.lineWidth = 9f * s;
p.lineCap = LineCap.Round; p.lineJoin = LineJoin.Round;
draw(p, s);
};
return e;
}
static void Path(Painter2D p, float s, bool closed, bool fill, params float[] xy)
{
p.BeginPath();
p.MoveTo(new Vector2(xy[0], xy[1]) * s);
for (int i = 2; i < xy.Length; i += 2) p.LineTo(new Vector2(xy[i], xy[i + 1]) * s);
if (closed) p.ClosePath();
if (fill) p.Fill();
else p.Stroke();
}
static void Seg(Painter2D p, Vector2 o, float s, float x0, float y0, float x1, float y1)
{
p.BeginPath(); p.MoveTo(o + new Vector2(x0, y0) * s); p.LineTo(o + new Vector2(x1, y1) * s); p.Stroke();
}
static void Ring(Painter2D p, float s, float x, float y, float r, float from = 0f, float to = 360f)
{
p.BeginPath();
p.Arc(new Vector2(x, y) * s, r * s, Angle.Degrees(from), Angle.Degrees(to));
p.Stroke();
}
static void ReloadAt(Painter2D p, Vector2 o, float s)
{
p.BeginPath(); p.Arc(o + new Vector2(50, 52) * s, 34 * s, Angle.Degrees(-50f), Angle.Degrees(250f)); p.Stroke();
p.BeginPath(); p.MoveTo(o + new Vector2(70, 6) * s); p.LineTo(o + new Vector2(94, 26) * s); p.LineTo(o + new Vector2(62, 34) * s); p.ClosePath(); p.Fill();
}
static void Play(Painter2D p, float s) => Path(p, s, true, true, 30, 18, 84, 50, 30, 82);
static void Back(Painter2D p, float s) => Path(p, s, false, false, 62, 18, 30, 50, 62, 82);
static void Forward(Painter2D p, float s) => Path(p, s, false, false, 38, 18, 70, 50, 38, 82);
static void Hex(Painter2D p, float s) { Path(p, s, true, false, 50, 8, 88, 29, 88, 71, 50, 92, 12, 71, 12, 29); Ring(p, s, 50, 50, 12); }
static void Find(Painter2D p, float s) { Ring(p, s, 42, 42, 28); Path(p, s, false, false, 63, 63, 88, 88); }
static void Feed(Painter2D p, float s) { Path(p, s, true, false, 26, 8, 74, 8, 74, 92, 26, 92); Path(p, s, true, true, 42, 34, 64, 50, 42, 66); }
static void Note2(Painter2D p, float s) { Ring(p, s, 36, 72, 16); Path(p, s, false, false, 52, 72, 52, 14, 80, 28); }
static void Lens(Painter2D p, float s) { Path(p, s, true, false, 14, 14, 86, 14, 86, 86, 14, 86); Ring(p, s, 50, 50, 20); Ring(p, s, 70, 30, 2); }
static void Talk(Painter2D p, float s) => Path(p, s, true, false, 12, 16, 88, 16, 88, 68, 46, 68, 26, 88, 26, 68, 12, 68);
static void Book(Painter2D p, float s) { Path(p, s, true, false, 18, 14, 82, 14, 82, 86, 18, 86); Path(p, s, false, false, 36, 36, 64, 36); Path(p, s, false, false, 36, 56, 64, 56); }
static void Live(Painter2D p, float s) { Ring(p, s, 50, 50, 11); Ring(p, s, 50, 50, 28, -40f, 40f); Ring(p, s, 50, 50, 28, 140f, 220f); Ring(p, s, 50, 50, 44, -40f, 40f); Ring(p, s, 50, 50, 44, 140f, 220f); }
static void Globe(Painter2D p, float s) { Ring(p, s, 50, 50, 38); Path(p, s, false, false, 12, 50, 88, 50); Ring(p, s, 50, 50, 38, -90f, 90f); p.BeginPath(); p.Arc(new Vector2(50, 50) * s, 38 * s, Angle.Degrees(90f), Angle.Degrees(270f)); p.Stroke(); Path(p, s, false, false, 50, 12, 50, 88); }
static void Rotate(Painter2D p, float s) { Path(p, s, true, false, 10, 52, 90, 52, 90, 90, 10, 90); Ring(p, s, 50, 48, 36, 200f, 320f); Path(p, s, true, true, 92, 16, 86, 44, 64, 28); }
static void HomeGlyph(Painter2D p, float s) { Path(p, s, false, false, 14, 50, 50, 16, 86, 50); Path(p, s, false, false, 26, 42, 26, 86, 74, 86, 74, 42); }
static void Lock(Painter2D p, float s) { Path(p, s, true, true, 20, 44, 80, 44, 80, 92, 20, 92); Ring(p, s, 50, 44, 22, 180f, 360f); }
static void Broken(Painter2D p, float s) { Ring(p, s, 50, 50, 42); Path(p, s, false, false, 50, 26, 50, 58); Path(p, s, true, true, 45, 70, 55, 70, 55, 80, 45, 80); }
static void Battery(Painter2D p, float s)
{
p.lineWidth = 5f * s;
Path(p, s, true, false, 8, 30, 84, 30, 84, 70, 8, 70);
Path(p, s, true, true, 88, 42, 94, 42, 94, 58, 88, 58);
Path(p, s, true, true, 16, 38, 66, 38, 66, 62, 16, 62);
}
// ───────────────────────── pages ─────────────────────────
public void Open(string url)
{
string name = Short(url);
Open(url, name.Length > 26 ? name.Substring(0, 24) + "…" : name, new Color(0.2f, 0.62f, 0.86f), Globe);
}
void Open(string url, string name, Color colour, Action<Painter2D, float> glyph)
{
if (!PhoneWeb.Available) { Toast(Note); return; }
EndTyping();
bool was = _page;
ShowPage(true);
if (!was) _openT = 0f;
_openedUrl = url;
_awaiting = true;
_error.style.display = DisplayStyle.None;
_splash.style.display = DisplayStyle.Flex;
_splashName.text = name;
_splashIcon.style.backgroundColor = colour;
if (glyph != null)
{
_splashIcon.Clear();
Glyph(_splashIcon, 112, Color.white, glyph);
}
_address.text = Loc.T("Linking...");
_address.style.color = Dim;
_progressAt = 0.08f;
_shownUrl = null;
Web.Go(url);
}
public void Home()
{
EndTyping();
if (_wide) Turn();
if (_page) _homeT = 0f;
ShowPage(false);
_shownUrl = _openedUrl = null;
_awaiting = false;
Web.Leave();
}
void ShowPage(bool on)
{
_page = on;
_home.style.display = on ? DisplayStyle.None : DisplayStyle.Flex;
_view.style.display = on ? DisplayStyle.Flex : DisplayStyle.None;
if (!on) { _error.style.display = DisplayStyle.None; _splash.style.display = DisplayStyle.None; }
Dress();
}
/// <summary>Sideways the page takes the whole display: the view is laid along the long side and turned onto it.</summary>
void Dress()
{
bool full = _page && _wide;
_top.style.display = _dock.style.display = _page && !_wide ? DisplayStyle.Flex : DisplayStyle.None;
_status.style.display = full ? DisplayStyle.None : DisplayStyle.Flex;
_unturn.style.display = full && _focus ? DisplayStyle.Flex : DisplayStyle.None;
_view.style.left = full ? W : 0;
_view.style.top = full ? 0 : TopH;
_view.style.right = StyleKeyword.Auto;
_view.style.width = full ? H : W;
_view.style.height = full ? W : WebH;
_view.style.transformOrigin = new TransformOrigin(0, 0);
_view.style.rotate = new Rotate(full ? 90f : 0f);
// the status strip reads light over a page's dark bar and over the wallpaper alike
_status.style.backgroundColor = _page && !_wide ? Color.clear : new Color(0f, 0f, 0f, 0.18f);
}
/// <summary>The phone onto its side and back. Only a page has anything to gain from it.</summary>
public void Turn()
{
if (!_page) return;
EndTyping();
_wide = !_wide;
Web.Wide(_wide);
Dress();
Phone.I?.Turned(_wide);
}
public void ModeChanged(PhoneMode m)
{
_visible = m != PhoneMode.Away;
_focus = m == PhoneMode.Up;
if (!_focus) { EndTyping(); Release(); }
Web.Show(_visible);
Dress();
if (_hover != null && !_focus) Hover(new Vector2(-1f, -1f));
}
/// <summary>The wheel, wherever the phone is held; over the page, it scrolls what's under the pointer.</summary>
public void Wheel(int dir) => Wheel(dir, new Vector2(-1f, -1f));
public void Wheel(int dir, Vector2 uv)
{
if (!_page) return;
var css = ToPage(uv);
bool inside = uv.x >= 0f && uv.x <= 1f && css.y >= 0f && css.y <= Web.CssH && css.x >= 0f && css.x <= Web.CssW;
if (inside) Web.Wheel(dir, css.x, css.y);
else Web.Wheel(dir);
}
void Toast(string text)
{
_toast.text = text;
_toastUntil = Time.unscaledTime + 5f;
}
// ───────────────────────── fingers ─────────────────────────
VisualElement Under(Vector2 uv)
{
if (uv.x < 0f || uv.x > 1f || uv.y < 0f || uv.y > 1f || _root.panel == null) return null;
var e = _root.panel.Pick(new Vector2(uv.x * W, (1f - uv.y) * H));
while (e != null && !(e.userData is Action)) e = e.parent;
return e;
}
public void Hover(Vector2 uv)
{
var e = DirectorCall.Busy ? null : Under(uv);
if (e == _hover) return;
if (_hover != null && _hover != _pressed) Look(_hover, 1f, 1f);
_hover = e;
if (_hover != null && _hover != _pressed) Look(_hover, 1.04f, 1f);
}
static void Look(VisualElement e, float scale, float opacity)
{
e.style.scale = new Scale(Vector3.one * scale);
e.style.opacity = opacity;
}
void Release()
{
if (_pressed == null) return;
Look(_pressed, _pressed == _hover ? 1.04f : 1f, 1f);
_pressed = null;
}
/// <summary>Panel pixels to the page's own: on its side the view is turned a quarter, so its x runs down the panel.</summary>
Vector2 ToPage(Vector2 uv)
{
var at = new Vector2(uv.x * W, (1f - uv.y) * H);
if (_wide) return new Vector2(at.y / H * Web.CssW, (W - at.x) / W * Web.CssH);
return new Vector2(at.x / W * Web.CssW, (at.y - TopH) / WebH * Web.CssH);
}
/// <summary>The mouse on the phone: a button gives when pressed and acts when let go over it; the page takes presses and drags itself.</summary>
public void Pointer(Vector2 uv, bool down, bool held, bool up)
{
// the Director's call has the screen: nothing under it is to be touched
if (DirectorCall.Busy) { Release(); return; }
if (down)
{
var b = Under(uv);
if (b != null)
{
_pressed = b;
Look(b, 0.93f, 0.82f);
Sfx.Play("ui_click", 0.3f, 1.25f);
if (_addressing && b.userData is Action && !IsAddressPill(b)) EndTyping();
return;
}
}
if (_pressed != null)
{
if (up || !held)
{
var b = _pressed;
Release();
if (Under(uv) == b && b.userData is Action a) a();
}
return;
}
if (!_page || _error.style.display.value == DisplayStyle.Flex) return;
var css = ToPage(uv);
bool inside = uv.x >= 0f && uv.x <= 1f && css.y >= 0f && css.y <= Web.CssH && css.x >= 0f && css.x <= Web.CssW;
if (down && inside)
{
if (_addressing) EndTyping();
_touching = true;
_touchAt = css;
Web.Touch("touchStart", css.x, css.y);
return;
}
if (!_touching) return;
if (held && (css - _touchAt).sqrMagnitude > 1f)
{
_touchAt = css;
Web.Touch("touchMove", css.x, css.y);
}
if (up || !held)
{
_touching = false;
Web.TouchEnd();
CheckField();
}
}
bool IsAddressPill(VisualElement b) => _address.parent == b || _search.parent == b;
// ───────────────────────── typing ─────────────────────────
/// <summary>A tap may have landed in a text box: the page is asked a moment later, once it has reacted.</summary>
async void CheckField()
{
int ask = ++_fieldAsk;
await System.Threading.Tasks.Task.Delay(180);
bool field = await Web.FieldActive();
// a newer tap has asked since: its answer is the one that counts
if (ask != _fieldAsk || field == _editing || _addressing || !_page) return;
if (field) { _editing = true; Listen(true); Ctl.Typing = true; }
else EndTyping();
}
void StartAddress(Label into, bool fromPage)
{
EndTyping();
_addressing = true;
_typeInto = into;
// on a page the address is there to edit, all of it chosen, so typing replaces it
_typed = fromPage && Web.Url != null && Web.Url != "about:blank" ? Web.Url : "";
_selectAll = _typed.Length > 0;
into.style.color = Ink;
ShowTyped();
Listen(true);
Ctl.Typing = true;
}
void ShowTyped()
{
if (_typeInto == null) return;
bool caret = Mathf.Repeat(Time.unscaledTime, 1f) < 0.55f;
_typeInto.text = _typed + (_selectAll ? "" : caret ? "|" : " ");
_typeInto.style.backgroundColor = _selectAll ? new Color(0.45f, 1f, 0.72f, 0.3f) : Color.clear;
}
void EndTyping()
{
if (Typing) { Listen(false); Ctl.Typing = false; }
_editing = false;
if (_addressing)
{
_addressing = false;
_selectAll = false;
if (_typeInto != null) _typeInto.style.backgroundColor = Color.clear;
_typeInto = null;
_shownUrl = null;
_search.text = Loc.T("Search or type a site");
_search.style.color = Dim;
}
}
public void CancelTyping() => EndTyping();
void Listen(bool on)
{
var k = Keyboard.current;
if (k == null) return;
k.onTextInput -= Char;
if (on) k.onTextInput += Char;
}
/// <summary>One character from the keyboard, into the address or into the page.</summary>
public void Char(char c)
{
if (_addressing)
{
if (c == '\r' || c == '\n')
{
var t = _typed.Trim();
EndTyping();
if (t.Length > 0) Open(ToUrl(t));
return;
}
if (c == '\b' || c == 127)
{
if (_selectAll) _typed = "";
else if (_typed.Length > 0) _typed = c == 127 ? TrimWord(_typed) : _typed.Substring(0, _typed.Length - 1);
}
else if (c == 22) _typed = (_selectAll ? "" : _typed) + GUIUtility.systemCopyBuffer;
else if (c >= 32) _typed = (_selectAll ? "" : _typed) + c;
else return;
_selectAll = false;
ShowTyped();
return;
}
if (c == '\b') Web.Key("Backspace", 8, null);
else if (c == 127) Web.Key("Backspace", 8, null, 2);
else if (c == '\r' || c == '\n') Web.Key("Enter", 13, "\\r");
else if (c == '\t') Web.Key("Tab", 9, null);
else if (c == 22) Web.Insert(GUIUtility.systemCopyBuffer);
else if (c >= 32) Web.Insert(c.ToString());
}
static string TrimWord(string s)
{
int i = s.Length - 1;
while (i >= 0 && s[i] == ' ') i--;
while (i >= 0 && s[i] != ' ' && s[i] != '/' && s[i] != '.') i--;
return s.Substring(0, i + 1);
}
/// <summary>The keys that make no characters, for a text box on the page: arrows, Home/End, Delete, select-all, undo.</summary>
void PageKeys()
{
var k = Keyboard.current;
if (k == null || !_editing) return;
bool ctrl = k.ctrlKey.isPressed;
if (k.leftArrowKey.wasPressedThisFrame) Web.Key("ArrowLeft", 37, null, ctrl ? 2 : 0);
if (k.rightArrowKey.wasPressedThisFrame) Web.Key("ArrowRight", 39, null, ctrl ? 2 : 0);
if (k.upArrowKey.wasPressedThisFrame) Web.Key("ArrowUp", 38, null);
if (k.downArrowKey.wasPressedThisFrame) Web.Key("ArrowDown", 40, null);
if (k.homeKey.wasPressedThisFrame) Web.Key("Home", 36, null);
if (k.endKey.wasPressedThisFrame) Web.Key("End", 35, null);
if (k.deleteKey.wasPressedThisFrame) Web.Key("Delete", 46, null);
if (ctrl && k.aKey.wasPressedThisFrame) Web.Command("selectAll");
if (ctrl && k.zKey.wasPressedThisFrame) Web.Command("undo");
}
static string ToUrl(string t)
{
if (t.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || t.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) return t;
if (t.IndexOf(' ') < 0 && t.IndexOf('.') > 0) return "https://" + t;
return "https://duckduckgo.com/?q=" + Uri.EscapeDataString(t);
}
// ───────────────────────── every frame ─────────────────────────
static string Short(string url)
{
var m = Regex.Match(url, @"^https?://(?:www\.|m\.)?([^/?#]+)");
return m.Success ? m.Groups[1].Value : url;
}
public void Tick()
{
if (_lang != Loc.Code) Relabel();
Web.Hold = DirectorCall.Busy;
Web.Tick(_focus);
float dt = Time.unscaledDeltaTime;
if (Web.Fresh)
{
_view.MarkDirtyRepaint();
if (_awaiting && Web.Url != null && Web.Url != "about:blank") _awaiting = false;
}
var problem = Web.TakeProblem();
if (problem != null)
{
problem = Loc.T(problem);
if (_page)
{
_errorText.text = problem;
_error.style.display = DisplayStyle.Flex;
_splash.style.display = DisplayStyle.None;
_awaiting = false;
_address.text = problem;
}
else Toast(problem);
}
string url = Web.Url;
if (url == "about:blank")
{
// backed out past the first page
if (_page && !Web.Loading && !_awaiting) Home();
url = null;
}
if (url != null && url != _shownUrl && !_addressing && _page)
{
_shownUrl = url;
bool bad = url.StartsWith("chrome-error", StringComparison.Ordinal);
_address.text = bad ? Loc.T("Can't reach that site") : Short(url);
_address.style.color = bad ? Red : Ink;
if (bad)
{
_errorText.text = Loc.T("Can't reach that site");
_error.style.display = DisplayStyle.Flex;
}
else _error.style.display = DisplayStyle.None;
}
// the splash goes once the page's own picture is in
if (_page) _splash.style.display = _awaiting ? DisplayStyle.Flex : DisplayStyle.None;
if (_awaiting) _spinner.MarkDirtyRepaint();
// a progress line that creeps on while the page loads, fills when it's done, and goes
bool loading = Web.Loading && _page;
if (loading) _progressAt = Mathf.Lerp(_progressAt, 0.92f, dt * 1.2f);
else if (_progressAt > 0f) _progressAt = _progressAt >= 1f ? (_progressAt < 1.25f ? _progressAt + dt : 0f) : Mathf.MoveTowards(_progressAt, 1f, dt * 4f);
bool showBar = _page && !_wide && _progressAt > 0f;
_progress.style.display = showBar ? DisplayStyle.Flex : DisplayStyle.None;
if (showBar)
{
_progress.style.width = Length.Percent(Mathf.Min(_progressAt, 1f) * 100f);
_progress.style.opacity = _progressAt > 1f ? 1f - (_progressAt - 1f) / 0.25f : 1f;
}
_reload.MarkDirtyRepaint();
// an app opening grows up out of the home screen; home comes back the same way
if (_openT < 1f)
{
_openT = Mathf.Min(1f, _openT + dt / 0.22f);
float e = 1f - (1f - _openT) * (1f - _openT);
foreach (var el in new[] { _top, _dock }) el.style.opacity = e;
_view.style.opacity = e;
_view.style.translate = new Translate(0, (1f - e) * 60f);
}
if (_homeT < 1f)
{
_homeT = Mathf.Min(1f, _homeT + dt / 0.25f);
float e = 1f - (1f - _homeT) * (1f - _homeT);
_home.style.opacity = e;
_home.style.scale = new Scale(Vector3.one * (1.06f - 0.06f * e));
}
// the keyboard's chip and the caret
bool chip = Typing && _focus;
_chip.style.display = chip ? DisplayStyle.Flex : DisplayStyle.None;
if (chip) _chipText.text = _addressing ? Loc.T("Enter to go · Esc to cancel") : Loc.T("Typing on the page · Esc to stop");
if (_addressing) ShowTyped();
PageKeys();
_toast.style.display = Time.unscaledTime < _toastUntil ? DisplayStyle.Flex : DisplayStyle.None;
if (!_visible && _nextStats > 0f) return;
if (Time.unscaledTime < _nextStats) return;
_nextStats = Time.unscaledTime + 0.5f;
string now = DateTime.Now.ToString("H:mm", Loc.Culture);
if (_clock.text != now) { _clock.text = now; _homeClock.text = now; }
var ts = TeamState.Instance;
if (ts)
{
float m = (float)ts.Mown01;
_mown.text = Loc.F("{0:0.0000}%", ts.Mown01 * 100.0);
if (!Mathf.Approximately(m, _mown01)) { _mown01 = m; _ring.MarkDirtyRepaint(); }
_money.text = UIK.Money(ts.Money.Value);
_shipped.text = Loc.F("{0} shipped", GameDefs.Kilos(ts.Shipped.Value));
}
var me = PlayerAgent.Local;
string day = DateTime.Now.ToString("ddd d MMM", Loc.Culture).ToUpperInvariant();
_homeDate.text = me ? day + " · " + Loc.F("HERBARIA-7 · SECTOR {0}", HexGrid.SectorAt(Planet.Up(me.transform.position)) + 1) : day + " · HERBARIA-7";
// the relay's bars: all four with a browser running, one while it starts, none without one
int bars = Web.Ready ? 4 : Web.Loading ? 1 + (int)(Time.unscaledTime * 2f) % 3 : PhoneWeb.Available ? 2 : 0;
for (int i = 0; i < 4; i++) _bars[i].style.opacity = i < bars ? 1f : 0.25f;
_link.text = Web.Ready ? Loc.T("RELAY") : Loc.T("RELAY IDLE");
}
public void Close(bool quitting = false)
{
EndTyping();
Release();
if (_wide) { _wide = false; Web.Wide(false); Dress(); }
ShowPage(false);
_shownUrl = _openedUrl = null;
_awaiting = false;
Web.Close(quitting);
}
/// <summary>Test hook: where an app is, in display coordinates.</summary>
public Vector2 TileAt(int i)
{
var c = _homeGrid[i].worldBound.center;
return new Vector2(c.x / W, 1f - c.y / H);
}
/// <summary>Test hook: a point of the page, in display coordinates.</summary>
public Vector2 PageAt(float x, float y) => _wide
? new Vector2(1f - y, 1f - x)
: new Vector2(x, 1f - (TopH + y * WebH) / H);
}
}