mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-09 08:53:34 +08:00
Add side slider for future use
This commit is contained in:
156
UXAssist/UI/MySideSlider.cs
Normal file
156
UXAssist/UI/MySideSlider.cs
Normal file
@@ -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<UIButton>());
|
||||||
|
go.SetActive(true);
|
||||||
|
var sl = go.AddComponent<MySideSlider>();
|
||||||
|
var rect = Util.NormalizeRectWithTopLeft(sl, x, y, parent);
|
||||||
|
sl.rectTrans = rect;
|
||||||
|
|
||||||
|
sl.slider = go.transform.Find("slider").GetComponent<Slider>();
|
||||||
|
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<Text>();
|
||||||
|
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<Image>();
|
||||||
|
// if (bg != null)
|
||||||
|
// {
|
||||||
|
// bg.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
|
||||||
|
// }
|
||||||
|
// var fill = sl.slider.fillRect.GetComponent<Image>();
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ public class MyWindow : ManualBehaviour
|
|||||||
go.SetActive(false);
|
go.SetActive(false);
|
||||||
go.name = "my-window";
|
go.name = "my-window";
|
||||||
Destroy(go.GetComponent<UITankWindow>());
|
Destroy(go.GetComponent<UITankWindow>());
|
||||||
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;
|
var child = go.transform.GetChild(i).gameObject;
|
||||||
if (child.name != "panel-bg" && child.name != "shadow")
|
if (child.name != "panel-bg" && child.name != "shadow")
|
||||||
@@ -245,19 +245,7 @@ public class MyWindow : ManualBehaviour
|
|||||||
return comboBox;
|
return comboBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MySlider AddSlider(float x, float y, RectTransform parent, float value, float minValue, float maxValue, string format = "G", float width = 0f)
|
#region Slider
|
||||||
{
|
|
||||||
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 class ValueMapper<T>
|
public class ValueMapper<T>
|
||||||
{
|
{
|
||||||
public virtual int Min => 1;
|
public virtual int Min => 1;
|
||||||
@@ -277,45 +265,6 @@ public class MyWindow : ManualBehaviour
|
|||||||
public override int Max => max;
|
public override int Max => max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MySlider AddSlider<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, ValueMapper<T> 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<T> 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<T> : ValueMapper<T>
|
private class ArrayMapper<T> : ValueMapper<T>
|
||||||
{
|
{
|
||||||
private readonly T[] _values;
|
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<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, ValueMapper<T> 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<T> 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<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, ValueMapper<T> 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<T> 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<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, T[] valueList, string format = "G", float width = 0f)
|
public MySlider AddSlider<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, T[] valueList, string format = "G", float width = 0f)
|
||||||
{
|
{
|
||||||
return AddSlider(x, y, parent, config, new ArrayMapper<T>(valueList), format, width);
|
return AddSlider(x, y, parent, config, new ArrayMapper<T>(valueList), format, width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MySideSlider AddSideSlider<T>(float x, float y, RectTransform parent, ConfigEntry<T> config, T[] valueList, string format = "G", float width = 0f)
|
||||||
|
{
|
||||||
|
return AddSideSlider(x, y, parent, config, new ArrayMapper<T>(valueList), format, width);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
public InputField AddInputField(float x, float y, RectTransform parent, string text = "", int fontSize = 16, string objName = "input", UnityAction<string> onChanged = null,
|
public InputField AddInputField(float x, float y, RectTransform parent, string text = "", int fontSize = 16, string objName = "input", UnityAction<string> onChanged = null,
|
||||||
UnityAction<string> onEditEnd = null)
|
UnityAction<string> onEditEnd = null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user