mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-08 22:53:33 +08:00
Add side slider for future use
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -34,7 +34,7 @@ public class MyComboBox : MonoBehaviour
|
||||
}
|
||||
var localizer = go.GetComponent<Localizer>();
|
||||
if (localizer) DestroyImmediate(localizer);
|
||||
|
||||
|
||||
var rect = (RectTransform)go.transform;
|
||||
var cbctrl = rect.transform.Find("ComboBox").GetComponent<UIComboBox>();
|
||||
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);
|
||||
|
||||
@@ -14,7 +14,7 @@ public class MyConfigWindow : MyWindowWithTabs
|
||||
{
|
||||
return MyWindowManager.CreateWindow<MyConfigWindow>("UXAConfigWindow", "UXAssist Config");
|
||||
}
|
||||
|
||||
|
||||
public static void DestroyInstance(MyConfigWindow win)
|
||||
{
|
||||
MyWindowManager.DestroyWindow(win);
|
||||
|
||||
@@ -43,7 +43,7 @@ public class MyKeyBinder : MonoBehaviour
|
||||
public UIButton setNoneKeyUIButton;
|
||||
|
||||
private bool _nextNotOn;
|
||||
|
||||
|
||||
protected void OnDestroy()
|
||||
{
|
||||
OnFree?.Invoke();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public class MySlider : MonoBehaviour
|
||||
}
|
||||
}
|
||||
sl.labelFormat = "G";
|
||||
|
||||
|
||||
sl.handleSlideArea = sl.transform.Find("Handle Slide Area")?.GetComponent<RectTransform>();
|
||||
|
||||
var bg = sl.slider.transform.Find("Background")?.GetComponent<Image>();
|
||||
@@ -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;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class MyWindow : ManualBehaviour
|
||||
go.SetActive(false);
|
||||
go.name = "my-window";
|
||||
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;
|
||||
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<T>
|
||||
{
|
||||
public virtual int Min => 1;
|
||||
@@ -277,45 +265,6 @@ public class MyWindow : ManualBehaviour
|
||||
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 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)
|
||||
{
|
||||
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,
|
||||
UnityAction<string> onEditEnd = null)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user