Files
DSP_Mods/UniverseGenTweaks/Patches/GalaxySelectUIPatch.cs
T
soarqin ba2648a3e3 refactor: remove single-owner helpers and redundant plumbing
Return feature-only helpers to their owners, remove inactive scaffolding and duplicate state, and reuse existing calculation and UI work without changing public contracts or gameplay.

Record repository-wide coverage, justified retentions, independent reviews and validation, including baseline dashboard fixture failures.

Assisted-by: Cindy (https://cindy.app)
2026-11-09 02:36:52 +08:00

238 lines
11 KiB
C#

using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using UXAssist.Common;
using UXAssist.Common.GameConstants;
using UXAssist.Common.ModFeatures;
using GameLogicProc = UXAssist.Common.GameLogic;
using Object = UnityEngine.Object;
namespace UniverseGenTweaks;
[ModFeature("UniverseGenGalaxySelectUI", Order = 10)]
public static class GalaxySelectUIPatch
{
private static Slider _minDistSlider;
private static Slider _minStepSlider;
private static Slider _maxStepSlider;
private static Slider _flattenSlider;
private static Text _minDistText;
private static Text _minStepText;
private static Text _maxStepText;
private static Text _flattenText;
private static Harmony _patch;
public static void Init()
{
MoreSettings.Enabled.SettingChanged += OnEnabledChanged;
Enable(MoreSettings.Enabled.Value);
}
public static void Uninit()
{
MoreSettings.Enabled.SettingChanged -= OnEnabledChanged;
Enable(false);
}
private static void OnEnabledChanged(object sender, System.EventArgs e)
{
Enable(MoreSettings.Enabled.Value);
}
internal static void Enable(bool on)
{
if (on)
{
_patch ??= Harmony.CreateAndPatchAll(typeof(Patch));
return;
}
_patch?.UnpatchSelf();
_patch = null;
}
private static class Patch
{
[HarmonyPostfix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnCreate))]
private static void UIGalaxySelect__OnCreate_Postfix(UIGalaxySelect __instance)
{
__instance.starCountSlider.maxValue = MoreSettings.MaxStarCount.Value;
CreateSliderWithText(__instance.starCountSlider, out var minDistTitle, out _minDistSlider, out _minDistText, out var minDistLocalizer);
CreateSliderWithText(__instance.starCountSlider, out var minStepTitle, out _minStepSlider, out _minStepText, out var minStepLocalizer);
CreateSliderWithText(__instance.starCountSlider, out var maxStepTitle, out _maxStepSlider, out _maxStepText, out var maxStepLocalizer);
CreateSliderWithText(__instance.starCountSlider, out var flattenTitle, out _flattenSlider, out _flattenText, out var flattenLocalizer);
minDistLocalizer.stringKey = Localization.StarDistanceMin;
minDistTitle.text = Localization.StarDistanceMin.Translate();
minStepLocalizer.stringKey = Localization.StepDistanceMin;
minStepTitle.text = Localization.StepDistanceMin.Translate();
maxStepLocalizer.stringKey = Localization.StepDistanceMax;
maxStepTitle.text = Localization.StepDistanceMax.Translate();
flattenLocalizer.stringKey = Localization.Flatness;
flattenTitle.text = Localization.Flatness.Translate();
minDistTitle.name = "min-dist";
minStepTitle.name = "min-step";
maxStepTitle.name = "max-step";
flattenTitle.name = "flatten";
TransformDeltaY(minDistTitle.transform, UniverseGenConstants.SliderRowSpacing);
TransformDeltaY(minStepTitle.transform, UniverseGenConstants.SliderRowSpacing * 2);
TransformDeltaY(maxStepTitle.transform, UniverseGenConstants.SliderRowSpacing * 3);
TransformDeltaY(flattenTitle.transform, UniverseGenConstants.SliderRowSpacing * 4);
TransformDeltaY(__instance.darkFogToggle.transform.parent, UniverseGenConstants.SliderRowSpacing * 4);
TransformDeltaY(__instance.resourceMultiplierSlider.transform.parent, UniverseGenConstants.SliderRowSpacing * 4);
TransformDeltaY(__instance.sandboxToggle.transform.parent, UniverseGenConstants.SliderRowSpacing * 4);
TransformDeltaY(__instance.propertyMultiplierText.transform, UniverseGenConstants.SliderRowSpacing * 4);
TransformDeltaY(__instance.addrText.transform.parent, UniverseGenConstants.SliderRowSpacing * 4);
RemoveAllListeners();
UpdateSliderControls();
AddListeners(__instance);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnOpen))]
private static void UIGalaxySelect__OnOpen_Prefix(UIGalaxySelect __instance)
{
GalaxyGenSettingsPatch.MinDist = UniverseGenConstants.DefaultMinDist;
GalaxyGenSettingsPatch.MinStep = UniverseGenConstants.DefaultMinStep;
GalaxyGenSettingsPatch.MaxStep = UniverseGenConstants.DefaultMaxStep;
GalaxyGenSettingsPatch.Flatten = UniverseGenConstants.DefaultFlatten;
RemoveAllListeners();
UpdateSliderControls();
AddListeners(__instance);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIGalaxySelect), nameof(UIGalaxySelect._OnUnregEvent))]
private static void UIGalaxySelect__OnUnregEvent_Postfix()
{
RemoveAllListeners();
}
}
private static void CreateSliderWithText(Slider orig, out Text title, out Slider slider, out Text text, out Localizer loc)
{
var origText = orig.transform.parent.GetComponent<Text>();
title = Object.Instantiate(origText, origText.transform.parent);
title.text = "";
title.fontStyle = FontStyle.Normal;
title.horizontalOverflow = HorizontalWrapMode.Overflow;
title.verticalOverflow = VerticalWrapMode.Overflow;
title.enabled = true;
foreach (var button in title.GetComponentsInChildren<UIButton>(true))
{
UXAssist.UI.Util.ResetButton(button);
}
foreach (var image in title.GetComponentsInChildren<Image>(true))
{
image.enabled = true;
}
slider = title.transform.FindChildRecur("Slider").GetComponent<Slider>();
slider.enabled = true;
slider.interactable = true;
slider.onValueChanged.RemoveAllListeners();
text = slider.transform.FindChildRecur("Text").GetComponent<Text>();
text.text = "";
text.fontStyle = FontStyle.Normal;
text.horizontalOverflow = HorizontalWrapMode.Overflow;
text.verticalOverflow = VerticalWrapMode.Overflow;
text.enabled = true;
loc = title.GetComponent<Localizer>();
}
private static void TransformDeltaY(Transform trans, float delta)
{
var pos = ((RectTransform)trans).anchoredPosition3D;
pos.y += delta;
((RectTransform)trans).anchoredPosition3D = pos;
}
private static void UpdateSliderControls()
{
_minDistSlider.minValue = UniverseGenConstants.MinDistSliderMin;
_minDistSlider.maxValue = UniverseGenConstants.MinDistSliderMax;
_minDistSlider.value = (float)(GalaxyGenSettingsPatch.MinDist * UniverseGenConstants.StepSliderScale);
_minStepSlider.minValue = (float)(GalaxyGenSettingsPatch.MinDist * UniverseGenConstants.StepSliderScale);
_minStepSlider.maxValue = (float)(GalaxyGenSettingsPatch.MaxStep * UniverseGenConstants.StepSliderScale);
_minStepSlider.value = (float)(GalaxyGenSettingsPatch.MinStep * UniverseGenConstants.StepSliderScale);
_maxStepSlider.minValue = (float)(GalaxyGenSettingsPatch.MinStep * UniverseGenConstants.StepSliderScale);
_maxStepSlider.maxValue = UniverseGenConstants.MaxStepSliderMax;
_maxStepSlider.value = (float)(GalaxyGenSettingsPatch.MaxStep * UniverseGenConstants.StepSliderScale);
_flattenSlider.minValue = UniverseGenConstants.FlattenSliderMin;
_flattenSlider.maxValue = UniverseGenConstants.FlattenSliderMax;
_flattenSlider.value = (float)(GalaxyGenSettingsPatch.Flatten * UniverseGenConstants.FlattenSliderScale);
_minDistText.text = GalaxyGenSettingsPatch.MinDist.ToString();
_minStepText.text = GalaxyGenSettingsPatch.MinStep.ToString();
_maxStepText.text = GalaxyGenSettingsPatch.MaxStep.ToString();
_flattenText.text = GalaxyGenSettingsPatch.Flatten.ToString();
UniverseGenTweaks.Logger.LogDebug($"Updated slider controls: {_minStepSlider.minValue}, {_minStepSlider.maxValue}, {_maxStepSlider.minValue}, {_maxStepSlider.maxValue}");
}
private static void RemoveAllListeners()
{
_minDistSlider.onValueChanged.RemoveAllListeners();
_minStepSlider.onValueChanged.RemoveAllListeners();
_maxStepSlider.onValueChanged.RemoveAllListeners();
_flattenSlider.onValueChanged.RemoveAllListeners();
}
private static void AddListeners(UIGalaxySelect uiGalaxySelect)
{
_minDistSlider.onValueChanged.AddListener(val =>
{
var newVal = Mathf.Round(val) / UniverseGenConstants.StepSliderScale;
if (newVal.Equals(GalaxyGenSettingsPatch.MinDist)) return;
GalaxyGenSettingsPatch.MinDist = newVal;
_minDistText.text = GalaxyGenSettingsPatch.MinDist.ToString();
if (GalaxyGenSettingsPatch.MinStep < GalaxyGenSettingsPatch.MinDist)
{
GalaxyGenSettingsPatch.MinStep = GalaxyGenSettingsPatch.MinDist;
_minStepSlider.value = (float)(GalaxyGenSettingsPatch.MinStep * UniverseGenConstants.StepSliderScale);
_minStepText.text = GalaxyGenSettingsPatch.MinStep.ToString();
if (GalaxyGenSettingsPatch.MaxStep < GalaxyGenSettingsPatch.MinStep)
{
GalaxyGenSettingsPatch.MaxStep = GalaxyGenSettingsPatch.MinStep;
_maxStepSlider.value = (float)(GalaxyGenSettingsPatch.MaxStep * UniverseGenConstants.StepSliderScale);
_maxStepText.text = GalaxyGenSettingsPatch.MaxStep.ToString();
}
}
_minStepSlider.minValue = (float)(GalaxyGenSettingsPatch.MinDist * UniverseGenConstants.StepSliderScale);
uiGalaxySelect.SetStarmapGalaxy();
});
_minStepSlider.onValueChanged.AddListener(val =>
{
var newVal = Mathf.Round(val) / UniverseGenConstants.StepSliderScale;
if (newVal.Equals(GalaxyGenSettingsPatch.MinStep)) return;
GalaxyGenSettingsPatch.MinStep = newVal;
_maxStepSlider.minValue = (float)(newVal * UniverseGenConstants.StepSliderScale);
_minStepText.text = GalaxyGenSettingsPatch.MinStep.ToString();
uiGalaxySelect.SetStarmapGalaxy();
});
_maxStepSlider.onValueChanged.AddListener(val =>
{
var newVal = Mathf.Round(val) / UniverseGenConstants.StepSliderScale;
if (newVal.Equals(GalaxyGenSettingsPatch.MaxStep)) return;
GalaxyGenSettingsPatch.MaxStep = newVal;
_minStepSlider.maxValue = (float)(newVal * UniverseGenConstants.StepSliderScale);
_maxStepText.text = GalaxyGenSettingsPatch.MaxStep.ToString();
uiGalaxySelect.SetStarmapGalaxy();
});
_flattenSlider.onValueChanged.AddListener(val =>
{
var newVal = Mathf.Round(val) / UniverseGenConstants.FlattenSliderScale;
if (newVal.Equals(GalaxyGenSettingsPatch.Flatten)) return;
GalaxyGenSettingsPatch.Flatten = newVal;
_flattenText.text = GalaxyGenSettingsPatch.Flatten.ToString();
uiGalaxySelect.SetStarmapGalaxy();
});
}
}