using System; using System.Collections.Generic; using Steamworks; using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// Owns the UI Toolkit document: main menu, modals (browser, settings, pause, messages), HUD and shop. [RequireComponent(typeof(UIDocument))] public partial class UIRoot : MonoBehaviour { public static UIRoot I { get; private set; } public StyleSheet style; VisualElement _root, _world, _hud, _screens, _modals, _cinema, _fade; /// Over the HUD and the screens, under the fade: the opening film's black and the call's captions. public VisualElement Cinema => _cinema; bool _hudHidden; /// The opening film hides the HUD while it runs. public void HideHud(bool hide) { _hudHidden = hide; if (_inGame) _hud.style.display = hide ? DisplayStyle.None : DisplayStyle.Flex; } VisualElement _menu, _connecting; Label _connectingText; readonly List _modalStack = new List(); public Hud Hud { get; private set; } ShopScreen _shop; public ShopScreen Shop => _shop; bool _inGame; public bool InGame => _inGame; public bool AnyModal => _modalStack.Count > 0; VisualElement _consoleLayer; public CheatConsole Console { get; private set; } /// ~ / F1: drop the cheat console down (or put it away). public void ToggleConsole() { if (_consoleLayer != null) { CloseModal(_consoleLayer); return; } if (!_inGame || !Cheats.Allowed) return; _consoleLayer = OpenModal(layer => { Console = new CheatConsole(layer); return null; }, dim: false); UIK.SlideIn(Console.Panel, new Vector2(0f, -400f), 0f, 0.25f); } void Awake() { I = this; var bank = AssetBank.I; Loc.Init(); Loc.Fonts(bank, out UIK.Display, out UIK.Body); } void Start() { var doc = GetComponent(); _root = doc.rootVisualElement; if (style) _root.styleSheets.Add(style); _root.AddToClassList("root"); UIK.SetFont(_root, false); _world = UIK.El("layer", _root); _hud = UIK.El("layer", _root); _screens = UIK.El("layer", _root); _modals = UIK.El("layer", _root); _cinema = UIK.El("layer", _root); _cinema.pickingMode = PickingMode.Ignore; _fade = UIK.El("layer fade", _root); _fade.style.opacity = 1f; Hud = new Hud(_hud, _world); _hud.style.display = DisplayStyle.None; gameObject.AddComponent(); gameObject.AddComponent(); gameObject.AddComponent(); gameObject.AddComponent(); BuildMenu(); BuildConnecting(); var ns = NetSession.Instance; ns.Started += OnGameStarted; ns.Ended += OnGameEnded; ns.Failed += msg => Message(Loc.T("Whoops!"), Loc.Unpack(msg)); ns.JoinAsked += lobby => Confirm(Loc.T("Join your friend?"), Loc.T("You'll leave this game. It's saved first."), () => ns.JoinLobby(lobby)); ns.StatusChanged += s => { bool show = ns.Current == NetSession.State.Connecting; _connecting.style.display = show ? DisplayStyle.Flex : DisplayStyle.None; if (show) { _connectingText.text = Loc.Unpack(s); UIK.PopIn(_connecting.Q(className: "card")); } }; GameSettings.Apply(); GameSettings.ApplyDisplay(); Sfx.Music("music_menu_loop", 2f); UIK.Fade(_fade, 0f, 0.8f); _fade.pickingMode = PickingMode.Ignore; Loc.Changed += Relocalize; SteamManager.OverlayChanged += active => { if (active && _inGame && !AnyModal && !CommandLine.Has("-autodemo")) OpenPause(); }; } void Update() { if (_holdSince >= 0f && Time.unscaledTime - _holdSince > 15f) ReleaseFade(0.6f); // a test bot runs on the same desktop: Esc pressed elsewhere is not for it if (_capture != null) PollCapture(); else if (Ctl.PausePressed && !CommandLine.Has("-autodemo")) { if (_modalStack.Count > 0) CloseTopModal(); else if (Phone.Back()) { } // the ending takes Esc itself: it jumps to the last card else if (_inGame && !EndingCinematic.Running) OpenPause(); } if (Ctl.ConsolePressed) ToggleConsole(); if (Ctl.MapPressed && _inGame && _consoleLayer == null) { if (_mapLayer != null && _modalStack.Count > 0 && _modalStack[_modalStack.Count - 1] == _mapLayer) CloseModal(_mapLayer); else if (!AnyModal) OpenMap(); } if (Ctl.NotebookPressed && _inGame && _consoleLayer == null) { if (_notebookLayer != null && _modalStack.Count > 0 && _modalStack[_modalStack.Count - 1] == _notebookLayer) CloseModal(_notebookLayer); else if (!AnyModal) OpenNotebook(); } Ctl.UIOpen = _modalStack.Count > 0 || !_inGame; if (_inGame) Ctl.SetCursor(!AnyModal && !Phone.Focused && !SteamManager.OverlayActive); Hud?.Tick(_inGame && !AnyModal); TickDemo(); Hud?.SetTabList(_inGame && !AnyModal && Ctl.TabHeld); _spinAngle += Time.unscaledDeltaTime * 360f; var sp = _connecting?.Q(className: "spinner"); if (sp != null) sp.style.rotate = new Rotate(_spinAngle); } float _spinAngle; // ───────────────────────── menu ───────────────────────── void BuildMenu() { var bank = AssetBank.I; _menu = UIK.El("layer", _screens); // soft vignette panel behind the buttons var shade = UIK.El("layer", _menu); shade.style.backgroundImage = new StyleBackground(GradientTex()); shade.style.unityBackgroundScaleMode = ScaleMode.StretchToFill; var logo = UIK.Img(bank.Tex("logo_grasscutter"), "menu-logo", _menu); var tag = UIK.Text(Loc.T("Earth has run out of grass. Herbaria-7 has too much."), "menu-tagline t-outline", _menu); var side = UIK.El("menu-side", _menu); side.pickingMode = PickingMode.Ignore; var buttons = new List { UIK.Btn(Loc.T("PLAY"), OpenSlots, "btn btn-wide", side), UIK.Btn(Loc.T("FIND GAMES"), OpenBrowser, "btn btn-blue btn-wide", side), UIK.Btn(Loc.T("CHARACTER"), OpenCharacter, "btn btn-white btn-wide", side), UIK.Btn(Loc.T("SETTINGS"), OpenSettings, "btn btn-white btn-wide", side), UIK.Btn(Loc.T("QUIT"), Quit, "btn btn-red btn-wide", side), }; if (Demo.On) { // the demo says so under the logo, and the wishlist sits with the big buttons var badge = UIK.Text(Loc.T("DEMO"), "menu-tagline t-outline", _menu); badge.style.top = Length.Percent(63); badge.style.fontSize = 44; badge.style.color = new Color(1f, 0.83f, 0.25f); var wish = UIK.Btn(Loc.T("WISHLIST THE FULL GAME"), () => SteamManager.OpenStore(), "btn btn-green btn-wide", side); wish.PlaceBehind(buttons[buttons.Count - 1]); buttons.Insert(buttons.Count - 1, wish); } var hint = UIK.Text(Loc.T(SteamManager.Initialized ? Loc.N("PLAY alone, on your LAN, or in a Steam lobby: invite friends with Shift+Tab.") : Loc.N("Steam not running: play alone or on your LAN.")), "t-small t-outline", side, false); hint.style.marginTop = 14; // the language in a click (Chinese above all), the store page and where to tell us what's wrong var links = UIK.El("menu-links", side); links.style.flexDirection = FlexDirection.Row; links.style.flexWrap = Wrap.Wrap; links.style.marginTop = 18; var langBtn = UIK.Btn(Loc.T("Language") + ": " + Loc.Current.native, OpenLanguages, "btn btn-white btn-small", links); SetLangFont(langBtn, Loc.Current); if (!Demo.On) UIK.Btn(Loc.T("STEAM PAGE"), () => SteamManager.OpenStore(), "btn btn-blue btn-small", links).style.marginLeft = 10; UIK.Btn(Loc.T("FEEDBACK"), () => SteamManager.OpenWeb(SteamManager.FeedbackUrl), "btn btn-green btn-small", links).style.marginLeft = 10; var footer = UIK.El("menu-footer", _menu); var pill = UIK.El("pill", footer); var av = UIK.El("avatar", pill); var avatarTex = SteamManager.Initialized ? SteamManager.GetAvatar(SteamManager.MySteamId) : null; if (avatarTex) av.style.backgroundImage = new StyleBackground(avatarTex); UIK.Text(SteamManager.MyName, "t-body", pill); var dot = UIK.El("status-dot", pill); dot.style.backgroundColor = SteamManager.Initialized ? new Color(0.22f, 0.85f, 0.54f) : new Color(1f, 0.55f, 0.24f); UIK.Text(SteamManager.Initialized ? "Steam" : Loc.T("Offline"), "t-small t-muted", pill, false); UIK.Text("v" + NetSession.Version, "version", _menu, false); // entrance animation + idle logo bob UIK.PopIn(logo, 0.25f, 0.7f); UIK.SlideIn(tag, new Vector2(0, 30), 0.6f); for (int i = 0; i < buttons.Count; i++) UIK.SlideIn(buttons[i], new Vector2(-420, 0), 0.15f + i * 0.07f); logo.schedule.Execute(() => { float t = Time.unscaledTime; logo.style.rotate = new Rotate(Mathf.Sin(t * 1.3f) * 2f); logo.style.translate = new Translate(0, Mathf.Sin(t * 1.9f) * 10f); }).Every(16).StartingIn(1000); } static Texture2D _grad; static Texture2D GradientTex() { if (_grad) return _grad; _grad = new Texture2D(256, 1, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp }; for (int x = 0; x < 256; x++) { float k = x / 255f; float a = Mathf.Clamp01(1f - k * 1.6f) * 0.72f; _grad.SetPixel(x, 0, new Color(0.08f, 0.12f, 0.05f, a)); } _grad.Apply(); return _grad; } void BuildConnecting() { _connecting = UIK.El("layer dim", _screens); _connecting.pickingMode = PickingMode.Position; _connecting.style.alignItems = Align.Center; _connecting.style.justifyContent = Justify.Center; var card = UIK.El("card", _connecting); card.style.alignItems = Align.Center; card.style.minWidth = 520; UIK.El("spinner", card); _connectingText = UIK.Text(Loc.T("Connecting…"), "t-h2 t-dark t-center", card); UIK.Btn(Loc.T("CANCEL"), () => NetSession.Instance.Leave(), "btn btn-red btn-small", card).style.marginTop = 16; _connecting.style.display = DisplayStyle.None; } float _holdSince = -1f; /// The opening film decides when the black lifts: at once under its own black, or a normal fade. public void ReleaseFade(float seconds) { if (_holdSince < 0f) return; _holdSince = -1f; Tween.Kill(_fade); if (seconds <= 0f) _fade.style.opacity = 0f; else UIK.Fade(_fade, 0f, seconds); } void OnGameStarted() { _inGame = true; _fade.style.opacity = 1f; // a new game may open on the film: stay black until it says, so the world never flashes up first if (IntroCinematic.MightRun) { Tween.Kill(_fade); _holdSince = Time.unscaledTime; } else UIK.Fade(_fade, 0f, 0.6f); _menu.style.display = DisplayStyle.None; _connecting.style.display = DisplayStyle.None; _hud.style.display = _hudHidden ? DisplayStyle.None : DisplayStyle.Flex; Hud.OnGameStarted(); CloseAllModals(); Sfx.Music("music_game_loop", 1.5f); Ctl.SetCursor(true); } void OnGameEnded() { _inGame = false; CloseAllModals(); _hud.style.display = DisplayStyle.None; Hud.OnGameEnded(); _menu.style.display = DisplayStyle.Flex; _fade.style.opacity = 1f; UIK.Fade(_fade, 0f, 0.6f); foreach (var b in _menu.Query