using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace GrassCutter
{
///
/// What's on the phone: HaulOS, a UI Toolkit panel drawn into a texture on the display. A home screen with the state of
/// the job and the sites, and Uplink, the page itself with a dock under it. Tapping goes through our own hit test, because
/// the phone is a 3D object swinging about, so a button is just an element with an Action in its userData.
///
public class PhoneScreen
{
public const int W = 720, H = 1578; // the same shape as Phone.ScreenW and ScreenH
const int TopH = 138, DockH = 124, WebH = H - TopH - DockH;
static readonly Color Mint = new Color(0.45f, 1f, 0.72f), Hay = new Color(1f, 0.8f, 0.36f), Ink = new Color(0.9f, 1f, 0.94f);
static readonly Color Dim = new Color(0.72f, 0.9f, 0.8f, 0.72f), Glass = new Color(0.56f, 1f, 0.76f, 0.09f), Edge = new Color(0.56f, 1f, 0.76f, 0.26f);
static readonly Color Deep = new Color(0.016f, 0.05f, 0.04f);
static readonly (string name, string url, Color colour, Action glyph)[] Sites =
{
("YouTube", "https://m.youtube.com/", new Color(1f, 0.38f, 0.32f), Play),
("Shorts", "https://m.youtube.com/shorts", new Color(1f, 0.56f, 0.34f), Feed),
("Twitch", "https://m.twitch.tv/", new Color(0.72f, 0.56f, 1f), Live),
("TikTok", "https://www.tiktok.com/foryou", new Color(0.36f, 0.94f, 0.94f), Feed),
("Instagram", "https://www.instagram.com/reels/", new Color(1f, 0.52f, 0.76f), Lens),
("Reddit", "https://www.reddit.com/", new Color(1f, 0.62f, 0.3f), Talk),
("Google", "https://www.google.com/", new Color(0.5f, 0.74f, 1f), Find),
("Wikipedia", "https://www.wikipedia.org/", new Color(0.82f, 0.88f, 0.86f), Book),
};
public RenderTexture Tex { get; }
/// The display's root, for whatever takes the whole screen over (a call).
public VisualElement Root => _root;
public PhoneWeb Web { get; }
/// A page is up, rather than the home screen.
public bool ShowingPage => _page;
public bool Wide => _wide;
/// A text box has the keyboard.
public bool Typing => _addressing || _editing;
readonly VisualElement _root, _home, _top, _dock, _view, _progress, _status, _unturn, _relay;
readonly Label _clock, _homeClock, _homeDate, _address, _search, _opening, _note, _mown, _money, _shipped, _link;
readonly VisualElement _mownBar;
VisualElement _hover, _homeGrid;
Label _typeInto;
readonly Label _mownTitle, _fundsTitle, _mapTile, _bookTile;
// 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;
string _typed = "", _shownUrl;
Vector2 _touchAt;
float _nextStats;
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(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();
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);
var wall = AssetBank.I.Tex("phone_wallpaper");
var back = Abs(_root);
back.pickingMode = PickingMode.Ignore;
if (wall) { back.style.backgroundImage = new StyleBackground(wall); back.style.unityBackgroundScaleMode = ScaleMode.ScaleAndCrop; }
// ── home ──
_home = Abs(_root);
_home.pickingMode = PickingMode.Ignore;
_home.style.paddingLeft = 40; _home.style.paddingRight = 40; _home.style.paddingTop = 84;
_homeClock = T("0:00", 168, Ink, _home, true);
_homeDate = T("HERBARIA-7", 25, Mint, _home, true);
_homeDate.style.letterSpacing = 4f;
_homeDate.style.marginBottom = 28;
var cards = Row(_home);
cards.style.justifyContent = Justify.SpaceBetween;
cards.style.marginBottom = 24;
var left = Panel(cards, 312, 146);
_mownTitle = T(Loc.T("PLANET MOWN"), 21, Dim, left, true);
_mownTitle.style.letterSpacing = 2f;
_mown = T(Loc.F("{0:0.0000}%", 0d), 44, Ink, left, true);
var track = El(left);
track.pickingMode = PickingMode.Ignore;
track.style.height = 8; track.style.marginTop = 8;
track.style.backgroundColor = Edge;
Round(track, 4);
_mownBar = El(track);
_mownBar.pickingMode = PickingMode.Ignore;
_mownBar.style.height = 8; _mownBar.style.width = Length.Percent(1);
_mownBar.style.backgroundColor = Mint;
Round(_mownBar, 4);
var right = Panel(cards, 312, 146);
_fundsTitle = T(Loc.T("COMPANY FUNDS"), 21, Dim, right, true);
_fundsTitle.style.letterSpacing = 2f;
_money = T("$0", 44, Hay, right, true);
_shipped = T("", 23, Dim, right, false);
var search = El(_home);
search.style.height = 92;
search.style.marginBottom = 24;
search.style.paddingLeft = 30;
search.style.flexDirection = FlexDirection.Row;
search.style.alignItems = Align.Center;
Skin(search, 46);
search.userData = (Action)(() => StartAddress(_search));
Glyph(search, 44, Mint, Find).style.marginRight = 18;
_search = T(Loc.T("Search or type a site"), 32, Dim, search, false);
_homeGrid = El(_home);
_homeGrid.pickingMode = PickingMode.Ignore;
_homeGrid.style.flexDirection = FlexDirection.Row;
_homeGrid.style.flexWrap = Wrap.Wrap;
_homeGrid.style.justifyContent = Justify.SpaceBetween;
foreach (var (name, url, colour, glyph) in Sites) Tile(_homeGrid, name, colour, glyph, () => Open(url));
_mapTile = Tile(_homeGrid, Loc.T("Map"), Mint, Hex, () => { Phone.I.Show(PhoneMode.Side); UIRoot.I.OpenMap(); });
_bookTile = Tile(_homeGrid, Loc.T("Notebook"), Hay, Book, () => { Phone.I.Show(PhoneMode.Side); UIRoot.I.OpenNotebook(); });
_note = T(Note, 24, Dim, _home, false);
_note.style.whiteSpace = WhiteSpace.Normal;
_note.style.unityTextAlign = TextAnchor.MiddleCenter;
_note.style.marginTop = 6;
// ── a page: the address on top, the dock 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;
_view.style.alignItems = Align.Center;
_view.style.justifyContent = Justify.Center;
_opening = T(Loc.T("Linking..."), 36, Mint, _view, true);
_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;
Skin(_unturn, 46);
_unturn.style.backgroundColor = new Color(0f, 0f, 0f, 0.55f);
_unturn.userData = (Action)Turn;
Glyph(_unturn, 52, Mint, Rotate);
_top = Abs(_root);
_top.pickingMode = PickingMode.Ignore;
_top.style.bottom = StyleKeyword.Auto; _top.style.height = TopH;
_top.style.backgroundColor = new Color(0.016f, 0.05f, 0.04f, 0.97f);
_top.style.justifyContent = Justify.FlexEnd;
_top.style.paddingLeft = 16; _top.style.paddingRight = 16; _top.style.paddingBottom = 10;
var pill = El(_top);
pill.style.height = 62;
pill.style.paddingLeft = 24; pill.style.paddingRight = 18;
pill.style.flexDirection = FlexDirection.Row;
pill.style.alignItems = Align.Center;
Skin(pill, 31);
pill.userData = (Action)(() => StartAddress(_address));
Glyph(pill, 30, Mint, Find).style.marginRight = 14;
_address = T("", 28, Ink, pill, false);
_dock = Abs(_root);
_dock.pickingMode = PickingMode.Ignore;
_dock.style.top = StyleKeyword.Auto; _dock.style.height = DockH;
_dock.style.backgroundColor = new Color(0.016f, 0.05f, 0.04f, 0.97f);
_dock.style.flexDirection = FlexDirection.Row;
_dock.style.justifyContent = Justify.SpaceAround;
_dock.style.alignItems = Align.Center;
_dock.style.paddingLeft = 56; _dock.style.paddingBottom = 12;
Dock(Back, () => Web.Back());
Dock(Forward, () => Web.Forward());
Dock(Reload, () => Web.Reload());
Dock(Rotate, Turn);
Dock(Hex, Home);
_progress = El(_root);
_progress.pickingMode = PickingMode.Ignore;
_progress.style.position = Position.Absolute;
_progress.style.top = TopH - 5; _progress.style.height = 5;
_progress.style.width = Length.Percent(30);
_progress.style.backgroundColor = Mint;
// ── the strip over everything ──
_status = Abs(_root);
_status.pickingMode = PickingMode.Ignore;
_status.style.bottom = StyleKeyword.Auto; _status.style.height = 62;
_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);
_relay = Row(_status);
_relay.style.alignItems = Align.Center;
_link = T(Loc.T("RELAY"), 21, Mint, _relay, true);
_link.style.letterSpacing = 3f;
_link.style.marginRight = 10;
for (int i = 0; i < 4; i++)
{
var bar = El(_relay);
bar.pickingMode = PickingMode.Ignore;
bar.style.width = 6; bar.style.height = 8 + i * 5; bar.style.marginLeft = 3;
bar.style.alignSelf = Align.FlexEnd;
bar.style.backgroundColor = Mint;
Round(bar, 2);
}
_lang = Loc.Code;
ShowPage(false);
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.");
/// The phone outlives a change of language: its fixed words and letters are put right here.
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");
_mapTile.text = Loc.T("Map");
_bookTile.text = Loc.T("Notebook");
_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 Skin(VisualElement e, float r)
{
Round(e, r);
e.style.backgroundColor = Glass;
e.style.borderTopWidth = 2; e.style.borderBottomWidth = 2; e.style.borderLeftWidth = 2; e.style.borderRightWidth = 2;
e.style.borderTopColor = Edge; e.style.borderBottomColor = Edge; e.style.borderLeftColor = Edge; e.style.borderRightColor = Edge;
}
static VisualElement Panel(VisualElement parent, float w, float h)
{
var e = El(parent);
e.pickingMode = PickingMode.Ignore;
e.style.width = w; e.style.height = h;
e.style.paddingLeft = 22; e.style.paddingRight = 18; e.style.paddingTop = 14;
Skin(e, 26);
e.style.backgroundColor = new Color(0.02f, 0.07f, 0.055f, 0.74f);
return e;
}
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;
}
Label Tile(VisualElement grid, string name, Color colour, Action glyph, Action open)
{
var tile = El(grid);
tile.style.width = 312; tile.style.height = 112;
tile.style.marginBottom = 16;
tile.style.flexDirection = FlexDirection.Row;
tile.style.alignItems = Align.Center;
tile.style.paddingLeft = 22;
Skin(tile, 26);
tile.style.backgroundColor = new Color(0.02f, 0.07f, 0.055f, 0.74f);
tile.style.borderLeftColor = colour;
tile.style.borderLeftWidth = 5;
tile.userData = open;
Glyph(tile, 56, colour, glyph).style.marginRight = 18;
return T(name, 33, Ink, tile, true);
}
void Dock(Action glyph, Action click)
{
var b = El(_dock);
b.style.width = 104; b.style.height = 92;
b.style.alignItems = Align.Center; b.style.justifyContent = Justify.Center;
b.userData = click;
Glyph(b, 54, Ink, glyph);
}
// ───────────────────────── glyphs: our own, and drawn, so they stay sharp ─────────────────────────
static VisualElement Glyph(VisualElement parent, float size, Color c, Action draw)
{
var e = El(parent);
e.pickingMode = PickingMode.Ignore;
e.style.width = size; e.style.height = size;
e.generateVisualContent += ctx =>
{
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 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 Play(Painter2D p, float s) => Path(p, s, true, true, 26, 14, 86, 50, 26, 86);
static void Back(Painter2D p, float s) => Path(p, s, false, false, 64, 16, 30, 50, 64, 84);
static void Forward(Painter2D p, float s) => Path(p, s, false, false, 36, 16, 70, 50, 36, 84);
static void Hex(Painter2D p, float s) => Path(p, s, true, false, 50, 8, 88, 29, 88, 71, 50, 92, 12, 71, 12, 29);
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 Lens(Painter2D p, float s) { Path(p, s, true, false, 12, 12, 88, 12, 88, 88, 12, 88); Ring(p, s, 50, 50, 20); }
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 Reload(Painter2D p, float s) { Ring(p, s, 50, 52, 34, -50f, 250f); Path(p, s, true, true, 70, 6, 94, 26, 62, 34); }
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); }
// ───────────────────────── pages ─────────────────────────
public void Open(string url)
{
if (!PhoneWeb.Available) return;
EndTyping();
ShowPage(true);
_opening.text = Loc.T("Linking...");
_opening.style.display = DisplayStyle.Flex;
_address.text = Loc.T("Linking...");
_shownUrl = null;
Web.Go(url);
}
public void Home()
{
EndTyping();
if (_wide) Turn();
ShowPage(false);
_shownUrl = null;
Web.Leave();
}
void ShowPage(bool on)
{
_page = on;
_home.style.display = on ? DisplayStyle.None : DisplayStyle.Flex;
_view.style.display = _top.style.display = _dock.style.display = on ? DisplayStyle.Flex : DisplayStyle.None;
Dress();
}
/// Sideways the page takes the whole display: the view is laid along the long side and turned onto it.
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 phone onto its side and back. Only a page has anything to gain from it.
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();
Web.Show(_visible);
Dress();
if (_hover != null && !_focus) Hover(new Vector2(-1f, -1f));
}
/// The wheel, wherever the phone is held.
public void Wheel(int dir) { if (_page) Web.Wheel(dir); }
// ───────────────────────── 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 = Under(uv);
if (e == _hover) return;
if (_hover != null) _hover.style.scale = new Scale(Vector3.one);
_hover = e;
if (_hover != null) _hover.style.scale = new Scale(Vector3.one * 1.04f);
}
/// Panel pixels to the page's own: on its side the view is turned a quarter, so its x runs down the panel.
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);
}
/// The mouse on the phone: a button takes a press, and the page takes presses and drags itself.
public void Pointer(Vector2 uv, bool down, bool held, bool up)
{
if (down && Under(uv)?.userData is Action a)
{
Sfx.Play("ui_click", 0.4f, 1.2f);
a();
return;
}
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 (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();
}
}
// ───────────────────────── typing ─────────────────────────
/// A tap may have landed in a text box: the page is asked a moment later, once it has reacted.
async void CheckField()
{
await System.Threading.Tasks.Task.Delay(180);
bool field = await Web.FieldActive();
if (field == _editing) return;
if (field) { _editing = true; Listen(true); Ctl.Typing = true; }
else EndTyping();
}
void StartAddress(Label into)
{
EndTyping();
_addressing = true;
_typeInto = into;
_typed = "";
into.text = "|";
into.style.color = Ink;
Listen(true);
Ctl.Typing = true;
}
void EndTyping()
{
if (Typing) { Listen(false); Ctl.Typing = false; }
_editing = false;
if (_addressing)
{
_addressing = false;
_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;
}
/// One character from the keyboard, into the address or into the page.
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') { if (_typed.Length > 0) _typed = _typed.Substring(0, _typed.Length - 1); }
else if (c == 22) _typed += GUIUtility.systemCopyBuffer;
else if (c >= 32) _typed += c;
_typeInto.text = _typed + "|";
return;
}
if (c == '\b') Web.Key("Backspace", 8, null);
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 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.Tick(_focus);
if (Web.Fresh) _view.MarkDirtyRepaint();
var problem = Web.TakeProblem();
if (problem != null)
{
problem = Loc.T(problem);
_opening.text = problem;
_opening.style.display = DisplayStyle.Flex;
_address.text = problem;
_note.text = problem;
}
string url = Web.Url;
if (url == "about:blank")
{
// backed out past the first page
if (_page && !Web.Loading) Home();
url = null;
}
if (url != null && url != _shownUrl && !_addressing && _page)
{
_shownUrl = url;
_opening.style.display = DisplayStyle.None;
_address.text = url.StartsWith("chrome-error", StringComparison.Ordinal) ? Loc.T("Can't reach that site") : Short(url);
}
bool busy = Web.Loading && _page && !_wide;
_progress.style.display = busy ? DisplayStyle.Flex : DisplayStyle.None;
if (busy) _progress.style.left = Length.Percent(Mathf.PingPong(Time.unscaledTime * 90f, 70f));
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)
{
_mown.text = Loc.F("{0:0.0000}%", ts.Mown01 * 100.0);
_mownBar.style.width = Length.Percent(Mathf.Clamp((float)ts.Mown01 * 100f, 1f, 100f));
_money.text = UIK.Money(ts.Money.Value);
_shipped.text = Loc.F("{0} shipped", GameDefs.Kilos(ts.Shipped.Value));
}
var me = PlayerAgent.Local;
_homeDate.text = me ? Loc.F("HERBARIA-7 · SECTOR {0}", HexGrid.SectorAt(Planet.Up(me.transform.position)) + 1) : "HERBARIA-7";
_link.text = Web.Ready ? Loc.T("RELAY") : Loc.T("RELAY IDLE");
}
public void Close()
{
EndTyping();
if (_wide) { _wide = false; Web.Wide(false); Dress(); }
ShowPage(false);
_shownUrl = null;
Web.Close();
}
/// Test hook: where a tile is, in display coordinates.
public Vector2 TileAt(int i)
{
var c = _homeGrid[i].worldBound.center;
return new Vector2(c.x / W, 1f - c.y / H);
}
/// Test hook: a point of the page, in display coordinates.
public Vector2 PageAt(float x, float y) => _wide
? new Vector2(1f - y, 1f - x)
: new Vector2(x, 1f - (TopH + y * WebH) / H);
}
}