using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BepInEx;
using Illusion.Extensions;
using KKAPI.Utilities;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;
namespace KKAPI.Maker.UI
{
///
/// Custom control that displays multiple radio buttons
///
public class MakerRadioButtons : BaseEditableGuiEntry
{
private readonly string _settingName;
private readonly string[] _buttons;
///
/// Objects of all of the radio buttons
///
public ReadOnlyCollection Buttons { get; private set; }
///
/// Create a new custom control. Create and register it in .
///
/// Text displayed next to the buttons
/// Category the control will be created under
/// Plugin that owns the control
/// Names of the radio buttons. Need at least 2 buttons.
public MakerRadioButtons(MakerCategory category, BaseUnityPlugin owner, string settingName, params string[] buttons) : this(category, owner, settingName, 0, buttons) { }
///
/// Create a new custom control. Create and register it in .
///
/// Text displayed next to the buttons
/// Category the control will be created under
/// Plugin that owns the control
/// Initial value of the control
/// Names of the radio buttons. Need at least 2 buttons.
public MakerRadioButtons(MakerCategory category, BaseUnityPlugin owner, string settingName, int initialValue, params string[] buttons) : base(category, initialValue, owner)
{
if (buttons.Length < 2) throw new ArgumentException("Need at least two buttons.", nameof(buttons));
_settingName = settingName;
_buttons = buttons;
}
///
protected internal override void Initialize()
{
}
///
protected override GameObject OnCreateControl(Transform subCategoryList)
{
var selGo = Object.Instantiate(GameObject.Find("SettingWindow/WinBody/B_Nip/SelectMenu"), subCategoryList);
selGo.name = "rb";
var toggleGroup = selGo.GetComponent();
Transform singleButton = null;
foreach (var c in selGo.transform.Children())
{
var singleToggle = c.GetComponent();
toggleGroup.UnregisterToggle(singleToggle);
if (c.name == "tgl01")
{
singleToggle.onValueChanged.ActuallyRemoveAllListeners();
singleButton = c;
}
else
{
Object.DestroyImmediate(c.gameObject);
}
}
var txtGo = Object.Instantiate(GameObject.Find("SettingWindow/WinBody/B_Nip/Setting/Setting02/Scroll View/Viewport/Content/ColorSet/Text"), selGo.transform);
txtGo.transform.SetAsFirstSibling();
const int textWidth = 110;
txtGo.AddComponent().minWidth = textWidth;
var txtCmp = txtGo.GetComponent();
txtCmp.text = _settingName;
txtCmp.color = TextColor;
SetTextAutosize(txtCmp);
RemoveLocalisation(selGo);
var newButtons = new List();
for (int i = 0; i < _buttons.Length; i++)
{
var toggleId = i;
Transform newBtn;
if (toggleId <= 0)
{
newBtn = singleButton ?? throw new ArgumentNullException(nameof(singleButton));
}
else
{
newBtn = Object.Instantiate(singleButton, selGo.transform);
newBtn.name = "tgl0" + toggleId;
}
var newTglText = newBtn.GetComponentInChildren();
newTglText.text = _buttons[i];
SetTextAutosize(newTglText);
var newTgl = newBtn.GetComponent();
newTgl.group = toggleGroup;
newTgl.onValueChanged.AddListener(
val =>
{
if (val)
SetValue(toggleId);
});
newButtons.Add(newTgl);
}
Buttons = newButtons.AsReadOnly();
var singleToggleWidth = (selGo.GetComponent().sizeDelta.x - textWidth - 10) / Buttons.Count;
foreach (var button in Buttons)
button.GetComponent().minWidth = singleToggleWidth;
BufferedValueChanged.Subscribe(
i =>
{
for (var index = 0; index < Buttons.Count; index++)
{
var tgl = Buttons[index];
tgl.isOn = index == i;
}
});
return selGo.gameObject;
}
}
}