diff --git a/UXAssist/UI/MyCheckbox.cs b/UXAssist/UI/MyCheckbox.cs index 124fdc4..a9ecd77 100644 --- a/UXAssist/UI/MyCheckbox.cs +++ b/UXAssist/UI/MyCheckbox.cs @@ -21,7 +21,7 @@ public class MyCheckBox : MonoBehaviour private static readonly Color BoxColor = new Color(1f, 1f, 1f, 100f / 255f); private static readonly Color CheckColor = new Color(1f, 1f, 1f, 1f); private static readonly Color TextColor = new Color(178f / 255f, 178f / 255f, 178f / 255f, 168f / 255f); - + public static void InitBaseObject() { if (_baseObject) return; @@ -82,7 +82,7 @@ public class MyCheckBox : MonoBehaviour cb.uiButton.onClick += cb.OnClick; return cb; } - + private void UpdateLabelTextWidth() { if (labelText) labelText.rectTransform.sizeDelta = new Vector2(labelText.preferredWidth, labelText.rectTransform.sizeDelta.y); @@ -150,7 +150,7 @@ public class MyCheckBox : MonoBehaviour Checked = check; return this; } - + public MyCheckBox WithEnable(bool on) { SetEnable(on); diff --git a/UXAssist/UI/MyComboBox.cs b/UXAssist/UI/MyComboBox.cs index 98b8437..4b3a636 100644 --- a/UXAssist/UI/MyComboBox.cs +++ b/UXAssist/UI/MyComboBox.cs @@ -34,7 +34,7 @@ public class MyComboBox : MonoBehaviour } var localizer = go.GetComponent(); if (localizer) DestroyImmediate(localizer); - + var rect = (RectTransform)go.transform; var cbctrl = rect.transform.Find("ComboBox").GetComponent(); foreach (var button in cbctrl.ItemButtons) @@ -146,7 +146,7 @@ public class MyComboBox : MonoBehaviour SetPrompt(prompt); return this; } - + public MyComboBox WithFontSize(int size) { SetFontSize(size); @@ -158,7 +158,7 @@ public class MyComboBox : MonoBehaviour SetItems(items); return this; } - + public MyComboBox WithIndex(int index) { SetIndex(index); diff --git a/UXAssist/UI/MyConfigWindow.cs b/UXAssist/UI/MyConfigWindow.cs index 08925c4..ad09b05 100644 --- a/UXAssist/UI/MyConfigWindow.cs +++ b/UXAssist/UI/MyConfigWindow.cs @@ -14,7 +14,7 @@ public class MyConfigWindow : MyWindowWithTabs { return MyWindowManager.CreateWindow("UXAConfigWindow", "UXAssist Config"); } - + public static void DestroyInstance(MyConfigWindow win) { MyWindowManager.DestroyWindow(win); diff --git a/UXAssist/UI/MyKeyBinder.cs b/UXAssist/UI/MyKeyBinder.cs index 3fd2082..9ae9440 100644 --- a/UXAssist/UI/MyKeyBinder.cs +++ b/UXAssist/UI/MyKeyBinder.cs @@ -43,7 +43,7 @@ public class MyKeyBinder : MonoBehaviour public UIButton setNoneKeyUIButton; private bool _nextNotOn; - + protected void OnDestroy() { OnFree?.Invoke(); diff --git a/UXAssist/UI/MySideSlider.cs b/UXAssist/UI/MySideSlider.cs new file mode 100644 index 0000000..23e8ce9 --- /dev/null +++ b/UXAssist/UI/MySideSlider.cs @@ -0,0 +1,156 @@ +using System; +using UnityEngine; +using UnityEngine.UI; + +namespace UXAssist.UI; + +// MySlider modified from LSTM: https://github.com/hetima/DSP_LSTM/blob/main/LSTM/MySlider.cs + +public class MySideSlider : MonoBehaviour +{ + public RectTransform rectTrans; + public Slider slider; + public Text labelText; + public string labelFormat; + public event Action OnValueChanged; + + public static MySideSlider CreateSlider(float x, float y, RectTransform parent, float value, float minValue, float maxValue, string format = "G", float width = 0f, float textWidth = 0f) + { + return CreateSlider(x, y, parent, width, textWidth).WithLabelFormat(format).WithMinMaxValue(minValue, maxValue).WithValue(value); + } + + public static MySideSlider CreateSlider(float x, float y, RectTransform parent, float width = 0f, float textWidth = 0f) + { + var go = Instantiate(UIRoot.instance.uiGame.stationWindow.maxMiningSpeedGroup.gameObject); + //sizeDelta = 240, 20 + go.name = "my-side-slider"; + Destroy(go.transform.Find("label").gameObject); + Destroy(go.GetComponent()); + go.SetActive(true); + var sl = go.AddComponent(); + var rect = Util.NormalizeRectWithTopLeft(sl, x, y, parent); + sl.rectTrans = rect; + + sl.slider = go.transform.Find("slider").GetComponent(); + sl.slider.minValue = 0f; + sl.slider.maxValue = 100f; + sl.slider.onValueChanged.RemoveAllListeners(); + sl.slider.onValueChanged.AddListener(sl.SliderChanged); + if (width == 0) width = 160f; + if (sl.slider.transform is RectTransform rectTrans) + { + rectTrans.localPosition = new Vector3(width, rectTrans.localPosition.y, rectTrans.localPosition.z); + rectTrans.sizeDelta = new Vector2(textWidth <= 0f ? width + 5f : width, rectTrans.sizeDelta.y); + } + sl.Value = 0f; + + sl.labelText = go.transform.Find("value").GetComponent(); + sl.labelText.alignment = textWidth <= 0f ? TextAnchor.MiddleLeft : TextAnchor.MiddleRight; + if (sl.labelText.transform is RectTransform rectTrans2) + { + if (textWidth > 0f) + { + rectTrans2.sizeDelta = new Vector2(textWidth, rectTrans2.sizeDelta.y); + } + rectTrans2.pivot = new Vector2(0f, 1f); + rectTrans2.localPosition = new Vector3(width, rectTrans2.localPosition.y, rectTrans2.localPosition.z); + } + sl.labelFormat = "G"; + + // var bg = sl.slider.transform.Find("Background")?.GetComponent(); + // if (bg != null) + // { + // bg.color = new Color(0.5f, 0.5f, 0.5f, 0.5f); + // } + // var fill = sl.slider.fillRect.GetComponent(); + // if (fill != null) + // { + // fill.color = new Color(1f, 1f, 1f, 0.28f); + // } + + sl.UpdateLabel(); + + return sl; + } + + public void SetEnable(bool on) + { + lock (this) + { + if (slider) slider.interactable = on; + } + } + + public float Value + { + get => slider.value; + set + { + var sliderVal = value; + if (sliderVal.Equals(slider.value)) return; + if (sliderVal > slider.maxValue) + { + sliderVal = slider.maxValue; + } + else if (sliderVal < slider.minValue) + { + sliderVal = slider.minValue; + } + + slider.value = sliderVal; + UpdateLabel(); + } + } + + public MySideSlider WithValue(float value) + { + Value = value; + return this; + } + + public MySideSlider WithMinMaxValue(float min, float max) + { + slider.minValue = min; + slider.maxValue = max; + return this; + } + + public MySideSlider WithLabelFormat(string format) + { + if (format == labelFormat) return this; + labelFormat = format; + UpdateLabel(); + return this; + } + + public MySideSlider WithEnable(bool on) + { + SetEnable(on); + return this; + } + + public void UpdateLabel() + { + if (labelText != null) + { + labelText.text = slider.value.ToString(labelFormat); + } + } + + public void SetLabelText(string text) + { + if (labelText != null) + { + labelText.text = text; + } + } + + public void SliderChanged(float val) + { + lock (this) + { + UpdateLabel(); + OnValueChanged?.Invoke(); + } + } +} diff --git a/UXAssist/UI/MySlider.cs b/UXAssist/UI/MySlider.cs index 373e871..6a47468 100644 --- a/UXAssist/UI/MySlider.cs +++ b/UXAssist/UI/MySlider.cs @@ -53,7 +53,7 @@ public class MySlider : MonoBehaviour } } sl.labelFormat = "G"; - + sl.handleSlideArea = sl.transform.Find("Handle Slide Area")?.GetComponent(); var bg = sl.slider.transform.Find("Background")?.GetComponent(); @@ -105,14 +105,14 @@ public class MySlider : MonoBehaviour Value = value; return this; } - + public MySlider WithMinMaxValue(float min, float max) { slider.minValue = min; slider.maxValue = max; return this; } - + public MySlider WithLabelFormat(string format) { if (format == labelFormat) return this; diff --git a/UXAssist/UI/MyWindow.cs b/UXAssist/UI/MyWindow.cs index 5b8059a..090d5e1 100644 --- a/UXAssist/UI/MyWindow.cs +++ b/UXAssist/UI/MyWindow.cs @@ -31,7 +31,7 @@ public class MyWindow : ManualBehaviour go.SetActive(false); go.name = "my-window"; Destroy(go.GetComponent()); - for (var i = 0; i < go.transform.childCount; i++) + for (var i = go.transform.childCount - 1; i >= 0; i--) { var child = go.transform.GetChild(i).gameObject; if (child.name != "panel-bg" && child.name != "shadow") @@ -245,19 +245,7 @@ public class MyWindow : ManualBehaviour return comboBox; } - public MySlider AddSlider(float x, float y, RectTransform parent, float value, float minValue, float maxValue, string format = "G", float width = 0f) - { - var slider = MySlider.CreateSlider(x, y, parent, value, minValue, maxValue, format, width); - var rect = slider.rectTrans; - if (rect != null) - { - _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); - MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); - } - - return slider; - } - +#region Slider public class ValueMapper { public virtual int Min => 1; @@ -277,45 +265,6 @@ public class MyWindow : ManualBehaviour public override int Max => max; } - public MySlider AddSlider(float x, float y, RectTransform parent, ConfigEntry config, ValueMapper valueMapper, string format = "G", float width = 0f) - { - var slider = MySlider.CreateSlider(x, y, parent, OnConfigValueChanged(config), valueMapper.Min, valueMapper.Max, format, width); - slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); - EventHandler func = (_, _) => - { - var index = OnConfigValueChanged(config); - slider.Value = index; - slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); - }; - config.SettingChanged += func; - OnFree += () => config.SettingChanged -= func; - slider.OnValueChanged += () => - { - var index = Mathf.RoundToInt(slider.Value); - config.Value = valueMapper.IndexToValue(index); - slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); - }; - - var rect = slider.rectTrans; - if (rect != null) - { - _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); - MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); - } - - return slider; - - int OnConfigValueChanged(ConfigEntry conf) - { - var index = valueMapper.ValueToIndex(conf.Value); - if (index >= 0) return index; - index = ~index; - index = Math.Max(0, Math.Min(valueMapper.Max, index)); - conf.Value = valueMapper.IndexToValue(index); - return index; - } - } - private class ArrayMapper : ValueMapper { private readonly T[] _values; @@ -340,11 +289,122 @@ public class MyWindow : ManualBehaviour } } + public MySlider AddSlider(float x, float y, RectTransform parent, float value, float minValue, float maxValue, string format = "G", float width = 0f) + { + var slider = MySlider.CreateSlider(x, y, parent, value, minValue, maxValue, format, width); + var rect = slider.rectTrans; + if (rect != null) + { + _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); + MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); + } + + return slider; + } + + public MySideSlider AddSideSlider(float x, float y, RectTransform parent, float value, float minValue, float maxValue, string format = "G", float width = 0f, float textWidth = 0f) + { + var slider = MySideSlider.CreateSlider(x, y, parent, value, minValue, maxValue, format, width, textWidth); + var rect = slider.rectTrans; + if (rect != null) + { + _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); + MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); + } + + return slider; + } + + public MySlider AddSlider(float x, float y, RectTransform parent, ConfigEntry config, ValueMapper valueMapper, string format = "G", float width = 0f, float textWidth = 0f) + { + var slider = MySlider.CreateSlider(x, y, parent, OnConfigValueChanged(config), valueMapper.Min, valueMapper.Max, format, width, textWidth); + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + config.SettingChanged += SettingsChanged; + OnFree += () => config.SettingChanged -= SettingsChanged; + slider.OnValueChanged += () => + { + var index = Mathf.RoundToInt(slider.Value); + config.Value = valueMapper.IndexToValue(index); + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + }; + + var rect = slider.rectTrans; + if (rect != null) + { + _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); + MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); + } + + return slider; + + void SettingsChanged(object o, EventArgs a) + { + var index = OnConfigValueChanged(config); + slider.Value = index; + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + } + + int OnConfigValueChanged(ConfigEntry conf) + { + var index = valueMapper.ValueToIndex(conf.Value); + if (index >= 0) return index; + index = ~index; + index = Math.Max(0, Math.Min(valueMapper.Max, index)); + conf.Value = valueMapper.IndexToValue(index); + return index; + } + } + + public MySideSlider AddSideSlider(float x, float y, RectTransform parent, ConfigEntry config, ValueMapper valueMapper, string format = "G", float width = 0f, float textWidth = 0f) + { + var slider = MySideSlider.CreateSlider(x, y, parent, OnConfigValueChanged(config), valueMapper.Min, valueMapper.Max, format, width, textWidth); + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + config.SettingChanged += SettingsChanged; + OnFree += () => config.SettingChanged -= SettingsChanged; + slider.OnValueChanged += () => + { + var index = Mathf.RoundToInt(slider.Value); + config.Value = valueMapper.IndexToValue(index); + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + }; + + var rect = slider.rectTrans; + if (rect != null) + { + _maxX = Math.Max(_maxX, x + rect.sizeDelta.x); + MaxY = Math.Max(MaxY, y + rect.sizeDelta.y); + } + + return slider; + + void SettingsChanged(object o, EventArgs a) + { + var index = OnConfigValueChanged(config); + slider.Value = index; + slider.SetLabelText(valueMapper.FormatValue(format, config.Value)); + } + int OnConfigValueChanged(ConfigEntry conf) + { + var index = valueMapper.ValueToIndex(conf.Value); + if (index >= 0) return index; + index = ~index; + index = Math.Max(0, Math.Min(valueMapper.Max, index)); + conf.Value = valueMapper.IndexToValue(index); + return index; + } + } + public MySlider AddSlider(float x, float y, RectTransform parent, ConfigEntry config, T[] valueList, string format = "G", float width = 0f) { return AddSlider(x, y, parent, config, new ArrayMapper(valueList), format, width); } + public MySideSlider AddSideSlider(float x, float y, RectTransform parent, ConfigEntry config, T[] valueList, string format = "G", float width = 0f) + { + return AddSideSlider(x, y, parent, config, new ArrayMapper(valueList), format, width); + } +#endregion + public InputField AddInputField(float x, float y, RectTransform parent, string text = "", int fontSize = 16, string objName = "input", UnityAction onChanged = null, UnityAction onEditEnd = null) { diff --git a/UXAssist/UI/Util.cs b/UXAssist/UI/Util.cs index f53eeed..0761e8e 100644 --- a/UXAssist/UI/Util.cs +++ b/UXAssist/UI/Util.cs @@ -18,7 +18,7 @@ public static class Util rect.anchoredPosition3D = new Vector3(left, -top, 0f); return rect; } - + public static RectTransform NormalizeRectWithBottomLeft(Component cmp, float left, float bottom, Transform parent = null) { if (cmp.transform is not RectTransform rect) return null; diff --git a/UniverseGenTweaks/UIConfigWindow.cs b/UniverseGenTweaks/UIConfigWindow.cs index e3ef76e..372b6cf 100644 --- a/UniverseGenTweaks/UIConfigWindow.cs +++ b/UniverseGenTweaks/UIConfigWindow.cs @@ -85,7 +85,7 @@ public static class UIConfigWindow sl2.OnValueChanged += () => { var val = EpicDifficulty.IndexToOilMultiplier(Mathf.RoundToInt(sl2.Value)); - EpicDifficulty.OilMultiplier.Value = val; + EpicDifficulty.OilMultiplier.Value = val; sl2.SetLabelText(val.ToString(sl2.labelFormat)); }; var tab2 = wnd.AddTab(_windowTrans, "Birth Star");