diff --git a/UXAssist/Functions/UIFunctions.cs b/UXAssist/Functions/UIFunctions.cs new file mode 100644 index 0000000..d293afb --- /dev/null +++ b/UXAssist/Functions/UIFunctions.cs @@ -0,0 +1,278 @@ +namespace UXAssist.Functions; + +using UI; +using Common; +using CommonAPI.Systems; +using UnityEngine; +using UnityEngine.UI; +using System; + +public static class UIFunctions +{ + private static bool _initialized; + private static PressKeyBind _toggleKey; + private static bool _configWinInitialized; + private static MyConfigWindow _configWin; + private static GameObject _buttonOnPlanetGlobe; + private static int _cornerComboBoxIndex; + + public static void Init() + { + _toggleKey = KeyBindings.RegisterKeyBinding(new BuiltinKey + { + key = new CombineKey((int)KeyCode.BackQuote, CombineKey.ALT_COMB, ECombineKeyAction.OnceClick, false), + conflictGroup = KeyBindConflict.MOVEMENT | KeyBindConflict.FLYING | KeyBindConflict.SAILING | KeyBindConflict.BUILD_MODE_1 | KeyBindConflict.KEYBOARD_KEYBIND, + name = "OpenUXAssistConfigWindow", + canOverride = true + }); + I18N.Add("KEYOpenUXAssistConfigWindow", "[UXA] Open UXAssist Config Window", "[UXA] 打开UX助手设置面板"); + I18N.OnInitialized += RecreateConfigWindow; + } + + public static void OnUpdate() + { + if (_toggleKey.keyValue) + { + ToggleConfigWindow(); + } + } + + public static void ToggleConfigWindow() + { + if (!_configWinInitialized) + { + if (!I18N.Initialized()) return; + _configWinInitialized = true; + _configWin = MyConfigWindow.CreateInstance(); + } + + if (_configWin.active) + { + _configWin._Close(); + } + else + { + _configWin.Open(); + } + } + + public static void InitMenuButtons() + { + if (_initialized) return; + var uiRoot = UIRoot.instance; + if (!uiRoot) return; + { + var mainMenu = uiRoot.uiMainMenu; + var src = mainMenu.newGameButton; + var parent = src.transform.parent; + var btn = GameObject.Instantiate(src, parent); + btn.name = "button-uxassist-config"; + var l = btn.text.GetComponent(); + if (l != null) + { + l.stringKey = "UXAssist Config"; + l.translation = "UXAssist Config".Translate(); + } + + btn.text.text = "UXAssist Config".Translate(); + btn.text.fontSize = btn.text.fontSize * 7 / 8; + I18N.OnInitialized += () => { btn.text.text = "UXAssist Config".Translate(); }; + var vec = ((RectTransform)mainMenu.exitButton.transform).anchoredPosition3D; + var vec2 = ((RectTransform)mainMenu.creditsButton.transform).anchoredPosition3D; + var transform1 = (RectTransform)btn.transform; + transform1.anchoredPosition3D = new Vector3(vec.x, vec.y + (vec.y - vec2.y) * 2, vec.z); + btn.button.onClick.RemoveAllListeners(); + btn.button.onClick.AddListener(Functions.UIFunctions.ToggleConfigWindow); + } + { + var panel = uiRoot.uiGame.planetGlobe; + var src = panel.button2; + var sandboxMenu = uiRoot.uiGame.sandboxMenu; + var icon = sandboxMenu.categoryButtons[6].transform.Find("icon")?.GetComponent()?.sprite; + var b = GameObject.Instantiate(src, src.transform.parent); + _buttonOnPlanetGlobe = b.gameObject; + var rect = (RectTransform)_buttonOnPlanetGlobe.transform; + var btn = _buttonOnPlanetGlobe.GetComponent(); + var img = _buttonOnPlanetGlobe.transform.Find("button-2/icon")?.GetComponent(); + if (img != null) + { + img.sprite = icon; + } + + if (_buttonOnPlanetGlobe != null && btn != null) + { + _buttonOnPlanetGlobe.name = "open-uxassist-config"; + rect.localScale = new Vector3(0.6f, 0.6f, 0.6f); + rect.anchoredPosition3D = new Vector3(64f, -5f, 0f); + b.onClick.RemoveAllListeners(); + btn.onClick += _ => { Functions.UIFunctions.ToggleConfigWindow(); }; + btn.tips.tipTitle = "UXAssist Config"; + I18N.OnInitialized += () => { btn.tips.tipTitle = "UXAssist Config".Translate(); }; + btn.tips.tipText = null; + btn.tips.corner = 9; + btn.tips.offset = new Vector2(-20f, -20f); + _buttonOnPlanetGlobe.SetActive(true); + } + } + { + var cb = UI.MyCornerComboBox.CreateComboBox(125, 0, uiRoot.uiGame.starmap.transform as RectTransform, true).WithItems("显示原始名称", "显示距离", "显示行星数", "显示主要矿物", "显示全部信息"); + cb.SetIndex(Functions.UIFunctions.CornerComboBoxIndex); + cb.OnSelChanged += (index) => + { + Functions.UIFunctions.CornerComboBoxIndex = index; + }; + } + _initialized = true; + } + + public static void RecreateConfigWindow() + { + if (!_configWinInitialized) return; + var wasActive = _configWin.active; + if (wasActive) _configWin._Close(); + MyConfigWindow.DestroyInstance(_configWin); + _configWinInitialized = false; + if (wasActive) ToggleConfigWindow(); + } + + public static void UpdateGlobeButtonPosition(UIPlanetGlobe planetGlobe) + { + if (_buttonOnPlanetGlobe == null) return; + var rect = (RectTransform)_buttonOnPlanetGlobe.transform; + if (planetGlobe.dysonSphereSystemUnlocked || planetGlobe.logisticsSystemUnlocked) + { + rect.anchoredPosition3D = new Vector3(64f, -5f, 0f); + } + else + { + rect.anchoredPosition3D = new Vector3(128f, -100f, 0f); + } + } + + public static int CornerComboBoxIndex + { + get => _cornerComboBoxIndex; + set + { + _cornerComboBoxIndex = value; + Patches.PlayerPatch.ShortcutKeysForStarsName.SetForceShowAllStarsNameExternal(_cornerComboBoxIndex != 0); + UpdateStarmapStarNames(); + } + } + + public static void UpdateStarmapStarNames() + { + var galaxy = GameMain.data.galaxy; + string[] starOrderNames = null; + if (_cornerComboBoxIndex > 0) + { + starOrderNames = new string[galaxy.starCount]; + StarData[] stars = new StarData[galaxy.starCount]; + for (int i = 0; i < galaxy.starCount; i++) + { + stars[i] = galaxy.stars[i]; + } + Array.Sort(stars, (a, b) => a.position.sqrMagnitude.CompareTo(b.position.sqrMagnitude)); + int[] spectrCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + for (int i = 0; i < galaxy.starCount; i++) + { + var star = stars[i]; + var index = star.index; + switch (star.type) + { + case EStarType.MainSeqStar: + switch (star.spectr) + { + case ESpectrType.M: + starOrderNames[index] = String.Format("M{0}", ++spectrCount[0]); + break; + case ESpectrType.K: + starOrderNames[index] = String.Format("K{0}", ++spectrCount[1]); + break; + case ESpectrType.G: + starOrderNames[index] = String.Format("G{0}", ++spectrCount[2]); + break; + case ESpectrType.F: + starOrderNames[index] = String.Format("F{0}", ++spectrCount[3]); + break; + case ESpectrType.A: + starOrderNames[index] = String.Format("A{0}", ++spectrCount[4]); + break; + case ESpectrType.B: + starOrderNames[index] = String.Format("B{0}", ++spectrCount[5]); + break; + case ESpectrType.O: + starOrderNames[index] = String.Format("O{0}", ++spectrCount[6]); + break; + } + break; + case EStarType.GiantStar: + starOrderNames[index] = String.Format("GS{0}", ++spectrCount[7]); + break; + case EStarType.WhiteDwarf: + starOrderNames[index] = String.Format("WD{0}", ++spectrCount[8]); + break; + case EStarType.NeutronStar: + starOrderNames[index] = String.Format("NS{0}", ++spectrCount[9]); + break; + case EStarType.BlackHole: + starOrderNames[index] = String.Format("BH{0}", ++spectrCount[10]); + break; + } + } + } + foreach (var starUI in UIRoot.instance.uiGame.starmap.starUIs) + { + var star = starUI?.star; + if (star == null) continue; + switch (_cornerComboBoxIndex) + { + case 1: + starUI.nameText.text = String.Format("{0}-{1:0.00LY}", starOrderNames[star.index], GetStarDist(star)); + break; + case 2: + { + var (nongas, total) = GetStarPlanetCount(star); + starUI.nameText.text = String.Format("{0}-{1}-{2}", starOrderNames[star.index], nongas, total); + break; + } + case 3: + starUI.nameText.text = String.Format("{0}-{1}", starOrderNames[star.index], GetStarSpecialOres(star)); + break; + case 4: + { + var (nongas, total) = GetStarPlanetCount(star); + starUI.nameText.text = String.Format("{0}-{1:0.00LY}-{2}-{3}-{4}", starOrderNames[star.index], GetStarDist(star), GetStarSpecialOres(star), nongas, total); + break; + } + default: + starUI.nameText.text = star.displayName; + break; + }; + } + return; + + double GetStarDist(StarData star) + { + return star.position.magnitude; + } + + (int, int) GetStarPlanetCount(StarData star) + { + int total = 0; + int nongas = 0; + foreach (var planet in star.planets) + { + if (planet == null) continue; + if (planet.type != EPlanetType.Gas) nongas++; + total++; + } + return (nongas, total); + } + + string GetStarSpecialOres(StarData star) + { + return ""; + } + } +} diff --git a/UXAssist/Patches/UIPatch.cs b/UXAssist/Patches/UIPatch.cs new file mode 100644 index 0000000..2e72d37 --- /dev/null +++ b/UXAssist/Patches/UIPatch.cs @@ -0,0 +1,36 @@ +namespace UXAssist.Patches; + +using Common; +using HarmonyLib; +using UnityEngine; + +[PatchGuid(PluginInfo.PLUGIN_GUID)] +public class UIPatch: PatchImpl +{ + public static void Start() + { + Enable(true); + Functions.UIFunctions.InitMenuButtons(); + } + + // Add config button to main menu + [HarmonyPostfix, HarmonyPatch(typeof(UIRoot), nameof(UIRoot._OnOpen))] + public static void UIRoot__OnOpen_Postfix() + { + Functions.UIFunctions.InitMenuButtons(); + } + + [HarmonyPostfix] + [HarmonyPatch(typeof(UIPlanetGlobe), nameof(UIPlanetGlobe.DistributeButtons))] + private static void UIPlanetGlobe_DistributeButtons_Postfix(UIPlanetGlobe __instance) + { + Functions.UIFunctions.UpdateGlobeButtonPosition(__instance); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(UIStarmapStar), nameof(UIStarmapStar.OnStarDisplayNameChange))] + private static bool UIStarmapStar_OnStarDisplayNameChange_Prefix() + { + return Functions.UIFunctions.CornerComboBoxIndex == 0; + } +}