11using System ;
22using System . Collections . Generic ;
3+ using MelonLoader ;
34using ReMod . Core . Unity ;
45using ReMod . Core . VRChat ;
56using TMPro ;
67using UnityEngine ;
78using UnityEngine . UI ;
89using VRC . UI . Elements ;
910using VRC . UI . Elements . Controls ;
10- using Object = Il2CppSystem . Object ;
11+ using Object = System . Object ;
1112
1213namespace ReMod . Core . UI . QuickMenu
1314{
@@ -39,10 +40,9 @@ public string TitleText
3940 }
4041
4142 private TextMeshProUGUI _titleText ;
42- private ListBinding ListBinding ;
43- private GameObject RadioButtonPrefab ;
44- private RadioButtonSelectorGroup RadioButtonSelectorGroup ;
45- private Dictionary < string , Tuple < string , Object , Action > > _radioElementSource = new ( ) ;
43+ private GameObject _toggleGroupRoot ;
44+ private List < Tuple < String , Object > > _radioElementSource = new ( ) ;
45+ private List < ReRadioToggle > _radioElements = new ( ) ;
4646 private bool _isUpdated ;
4747
4848 private readonly bool _isRoot ;
@@ -62,11 +62,11 @@ public ReRadioTogglePage(string name) : base(MenuPrefab, QuickMenuEx.MenuParent,
6262 _container = RectTransform . GetComponentInChildren < ScrollRect > ( ) . content ;
6363
6464 var inputMenu = RectTransform . GetComponent < AudioInputDeviceMenu > ( ) ;
65- RadioButtonPrefab = inputMenu . field_Public_GameObject_0 ;
66- ListBinding = inputMenu . field_Public_ListBinding_0 ;
67- RadioButtonSelectorGroup = ListBinding . gameObject . GetComponent < RadioButtonSelectorGroup > ( ) ;
6865
69- ListBinding . field_Protected_Dictionary_2_Object_GameObject_0 = new Il2CppSystem . Collections . Generic . Dictionary < Object , GameObject > ( ) ;
66+ _toggleGroupRoot = inputMenu . field_Public_ListBinding_0 . gameObject ;
67+
68+ UnityEngine . Object . DestroyImmediate ( _toggleGroupRoot . gameObject . GetComponent < ListBinding > ( ) ) ;
69+ UnityEngine . Object . DestroyImmediate ( _toggleGroupRoot . gameObject . GetComponent < RadioButtonSelectorGroup > ( ) ) ;
7070
7171 //Get rid of the AudioInputDeviceMenu component
7272 UnityEngine . Object . DestroyImmediate ( inputMenu ) ;
@@ -87,46 +87,62 @@ public ReRadioTogglePage(string name) : base(MenuPrefab, QuickMenuEx.MenuParent,
8787 listener . OnDisableEvent += ( ) => OnClose ? . Invoke ( ) ;
8888 }
8989
90- public void Open ( )
90+ /// <summary>
91+ /// Opens and configures the ReRadioTogglePage, optionally with default object to set toggles active
92+ /// </summary>
93+ /// <param name="selected">Default object matching ones on toggle elements</param>
94+ public void Open ( Object selected = null )
9195 {
9296 QuickMenuEx . MenuStateCtrl . PushPage ( UiPage . field_Public_String_0 ) ;
9397
94- if ( ! _isUpdated )
95- return ;
98+ if ( _isUpdated )
99+ {
100+ _isUpdated = false ;
101+
102+ foreach ( var element in _radioElements )
103+ UnityEngine . Object . DestroyImmediate ( element . GameObject ) ;
104+ _radioElements . Clear ( ) ;
96105
97- _isUpdated = false ;
106+ foreach ( var newElement in _radioElementSource )
107+ {
108+ var toggle = new ReRadioToggle ( _toggleGroupRoot . transform , newElement . Item1 , newElement . Item1 , newElement . Item2 ) ;
109+ toggle . ToggleStateUpdated += OnToggleSelect ;
110+ _radioElements . Add ( toggle ) ;
111+ }
112+ }
98113
99- foreach ( var element in ListBinding . field_Protected_Dictionary_2_Object_GameObject_0 )
100- UnityEngine . Object . DestroyImmediate ( element . value ) ;
101- ListBinding . field_Protected_Dictionary_2_Object_GameObject_0 . Clear ( ) ;
114+ if ( selected == null )
115+ return ;
116+
117+ //Update the toggles to display the current active state
118+ foreach ( var element in _radioElements )
119+ {
120+ if ( element . ToggleData == selected )
121+ element . SetToggle ( true ) ;
122+ }
123+ }
102124
103- foreach ( var newElement in _radioElementSource )
125+ private void OnToggleSelect ( ReRadioToggle toggle , bool state )
126+ {
127+ foreach ( var element in _radioElements )
104128 {
105- var radioButton = UnityEngine . Object . Instantiate ( RadioButtonPrefab , ListBinding . gameObject . transform ) ;
106- radioButton . active = true ;
107- var radioButtonSelector = radioButton . GetComponent < RadioButtonSelector > ( ) ;
108- radioButtonSelector . field_Public_TextMeshProUGUI_0 = radioButtonSelector . GetComponentInChildren < TextMeshProUGUI > ( ) ;
109- radioButtonSelector . field_Private_Button_0 = radioButtonSelector . GetComponent < Button > ( ) ;
110- UnityEngine . Object . DestroyImmediate ( radioButtonSelector . GetComponent < AudioDeviceButton > ( ) ) ;
111- radioButtonSelector . field_Private_Button_0 . onClick . AddListener ( new Action ( ( ) => OnSelect ? . Invoke ( newElement . Value . Item2 ) ) ) ;
112- if ( newElement . Value . Item3 != null )
113- radioButtonSelector . field_Private_Button_0 . onClick . AddListener ( newElement . Value . Item3 ) ;
114- radioButtonSelector . field_Public_String_0 = newElement . Value . Item1 ;
115- radioButtonSelector . SetTitle ( newElement . Key , newElement . Value . Item1 ) ;
116- radioButtonSelector . prop_RadioButtonSelectorGroup_0 = RadioButtonSelectorGroup ;
117- ListBinding . field_Protected_Dictionary_2_Object_GameObject_0 . Add ( newElement . Key , radioButton ) ;
129+ if ( element == toggle )
130+ continue ;
131+
132+ element . SetToggle ( false ) ;
118133 }
134+
135+ OnSelect ? . Invoke ( toggle . ToggleData ) ;
119136 }
120137
121138 /// <summary>
122139 /// Adds a item to the radio element source
123140 /// </summary>
124141 /// <param name="name">Name that will appear on radio toggle</param>
125142 /// <param name="obj">Object to be send in OnSelect event</param>
126- /// <param name="onClick">OnClick when the toggle is selected in menu</param>
127- public void AddItem ( string name , Object obj , Action onClick = null )
143+ public void AddItem ( string name , Object obj )
128144 {
129- _radioElementSource . Add ( $ " { _radioElementSource . Count } _ { name } " , new Tuple < string , Object , Action > ( name , obj , onClick ) ) ;
145+ _radioElementSource . Add ( new Tuple < string , Object > ( name , obj ) ) ;
130146 _isUpdated = true ;
131147 }
132148
0 commit comments