mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-09 15:33:31 +08:00
work in progress
This commit is contained in:
143
UXAssist/UI/MyCornerComboBox.cs
Normal file
143
UXAssist/UI/MyCornerComboBox.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using BepInEx.Configuration;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UXAssist.UI;
|
||||
|
||||
public class MyCornerComboBox : MonoBehaviour
|
||||
{
|
||||
private RectTransform _rectTrans;
|
||||
private UIComboBox _comboBox;
|
||||
public Action<int> OnSelChanged;
|
||||
|
||||
private static GameObject _baseObject;
|
||||
|
||||
public static void InitBaseObject()
|
||||
{
|
||||
if (_baseObject) return;
|
||||
var go = Instantiate(UIRoot.instance.uiGame.starDetail.displayCombo.gameObject);
|
||||
go.name = "my-small-combobox";
|
||||
go.SetActive(false);
|
||||
|
||||
var cbctrl = go.transform.GetComponent<UIComboBox>();
|
||||
foreach (var button in cbctrl.ItemButtons)
|
||||
{
|
||||
Destroy(button.gameObject);
|
||||
}
|
||||
cbctrl.Items.Clear();
|
||||
cbctrl.ItemButtons.Clear();
|
||||
cbctrl.onSubmit.RemoveAllListeners();
|
||||
cbctrl.onItemIndexChange.RemoveAllListeners();
|
||||
_baseObject = go;
|
||||
}
|
||||
|
||||
public static MyCornerComboBox CreateComboBox(float x, float y, RectTransform parent, bool topRight = false)
|
||||
{
|
||||
var gameObject = Instantiate(_baseObject);
|
||||
gameObject.name = "my-combobox";
|
||||
gameObject.SetActive(true);
|
||||
var cb = gameObject.AddComponent<MyCornerComboBox>();
|
||||
RectTransform rtrans;
|
||||
if (topRight)
|
||||
{
|
||||
rtrans = Util.NormalizeRectWithTopRight(cb, x, y, parent);
|
||||
}
|
||||
else
|
||||
{
|
||||
rtrans = Util.NormalizeRectWithTopLeft(cb, x, y, parent);
|
||||
}
|
||||
cb._rectTrans = rtrans;
|
||||
var box = rtrans.GetComponent<UIComboBox>();
|
||||
cb._comboBox = box;
|
||||
box.onItemIndexChange.AddListener(() => { cb.OnSelChanged?.Invoke(box.itemIndex); });
|
||||
|
||||
return cb;
|
||||
}
|
||||
|
||||
protected void OnDestroy()
|
||||
{
|
||||
_config.SettingChanged -= _configChanged;
|
||||
}
|
||||
|
||||
public void SetFontSize(int size)
|
||||
{
|
||||
var textComp = _comboBox.transform.Find("Main Button")?.GetComponentInChildren<Text>();
|
||||
if (textComp) textComp.fontSize = size;
|
||||
_comboBox.ItemButtons.ForEach(b => b.GetComponentInChildren<Text>().fontSize = size);
|
||||
_comboBox.m_ListItemRes.GetComponentInChildren<Text>().fontSize = size;
|
||||
}
|
||||
|
||||
public void SetItems(params string[] items)
|
||||
{
|
||||
_comboBox.Items = [.. items.Select(s => s.Translate())];
|
||||
_comboBox.StartItemIndex = 0;
|
||||
_comboBox.DropDownCount = items.Length;
|
||||
}
|
||||
|
||||
public void SetIndex(int index) => _comboBox.itemIndex = index;
|
||||
|
||||
public void SetSize(float width, float height)
|
||||
{
|
||||
_rectTrans.sizeDelta = new Vector2(width > 0f ? width : _rectTrans.sizeDelta.x, height > 0f ? height : _rectTrans.sizeDelta.y);
|
||||
}
|
||||
|
||||
public void AddOnSelChanged(Action<int> action) => OnSelChanged += action;
|
||||
|
||||
private EventHandler _configChanged;
|
||||
private Action<int> _selChanged;
|
||||
private ConfigEntry<int> _config;
|
||||
public void SetConfigEntry(ConfigEntry<int> config)
|
||||
{
|
||||
if (_selChanged != null) OnSelChanged -= _selChanged;
|
||||
if (_configChanged != null) config.SettingChanged -= _configChanged;
|
||||
|
||||
_comboBox.itemIndex = config.Value;
|
||||
_config = config;
|
||||
_selChanged = value => config.Value = value;
|
||||
OnSelChanged += _selChanged;
|
||||
_configChanged = (_, _) => SetIndex(config.Value);
|
||||
config.SettingChanged += _configChanged;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithFontSize(int size)
|
||||
{
|
||||
SetFontSize(size);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithItems(params string[] items)
|
||||
{
|
||||
SetItems(items);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithIndex(int index)
|
||||
{
|
||||
SetIndex(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithSize(float width, float height)
|
||||
{
|
||||
SetSize(width, height);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithOnSelChanged(params Action<int>[] action)
|
||||
{
|
||||
foreach (var act in action)
|
||||
AddOnSelChanged(act);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyCornerComboBox WithConfigEntry(ConfigEntry<int> config)
|
||||
{
|
||||
SetConfigEntry(config);
|
||||
return this;
|
||||
}
|
||||
|
||||
public float Width => _rectTrans.sizeDelta.x;
|
||||
public float Height => _rectTrans.sizeDelta.y;
|
||||
}
|
||||
@@ -245,13 +245,13 @@ public class MyWindow : ManualBehaviour
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
// public MySmallComboBox AddSmallComboBox(float x, float y, RectTransform parent, int fontSize = 15)
|
||||
// {
|
||||
// var comboBox = MySmallComboBox.CreateComboBox(x, y, parent).WithFontSize(fontSize);
|
||||
// _maxX = Math.Max(_maxX, x + comboBox.Width);
|
||||
// MaxY = Math.Max(MaxY, y + comboBox.Height);
|
||||
// return comboBox;
|
||||
// }
|
||||
public MyCornerComboBox AddCornerComboBox(float x, float y, RectTransform parent, int fontSize = 15)
|
||||
{
|
||||
var comboBox = MyCornerComboBox.CreateComboBox(x, y, parent).WithFontSize(fontSize);
|
||||
_maxX = Math.Max(_maxX, x + comboBox.Width);
|
||||
MaxY = Math.Max(MaxY, y + comboBox.Height);
|
||||
return comboBox;
|
||||
}
|
||||
|
||||
#region Slider
|
||||
public class ValueMapper<T>
|
||||
@@ -566,7 +566,7 @@ public abstract class MyWindowManager
|
||||
MyWindow.InitBaseObject();
|
||||
MyCheckBox.InitBaseObject();
|
||||
MyComboBox.InitBaseObject();
|
||||
// MySmallComboBox.InitBaseObject();
|
||||
MyCornerComboBox.InitBaseObject();
|
||||
}
|
||||
|
||||
public static T CreateWindow<T>(string name, string title = "") where T : MyWindow
|
||||
|
||||
Reference in New Issue
Block a user