Skip to content

Commit c47a406

Browse files
committed
Added support for OnValueChanged events.
1 parent 08b303e commit c47a406

2 files changed

Lines changed: 204 additions & 41 deletions

File tree

Scripts/Form.cs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ namespace CandyCoded.Forms
1717
public class Form : MonoBehaviour
1818
{
1919

20-
public SubmitEventObject FormSubmittedObject;
20+
public FormEventObjectEvent FormChangedObject;
2121

22-
public SubmitEventJSON FormSubmittedJSON;
22+
public FormEventJSONEvent FormChangedJSON;
23+
24+
public FormEventObjectEvent FormSubmittedObject;
25+
26+
public FormEventJSONEvent FormSubmittedJSON;
2327

2428
public Button submitButton;
2529

@@ -66,6 +70,44 @@ private void Update()
6670

6771
}
6872

73+
private void OnEnable()
74+
{
75+
76+
foreach (var formField in GetChildFormFields())
77+
{
78+
79+
formField.OnValueChanged.AddListener(HandleValueChanged);
80+
81+
}
82+
83+
if (submitButton)
84+
{
85+
86+
submitButton.onClick.AddListener(HandleReturnPress);
87+
88+
}
89+
90+
}
91+
92+
private void OnDisable()
93+
{
94+
95+
foreach (var formField in GetChildFormFields())
96+
{
97+
98+
formField.OnValueChanged.RemoveListener(HandleValueChanged);
99+
100+
}
101+
102+
if (submitButton)
103+
{
104+
105+
submitButton.onClick.RemoveListener(HandleReturnPress);
106+
107+
}
108+
109+
}
110+
69111
private void HandleTabPress(Selectable selectable, Selectable[] allSelectable)
70112
{
71113

@@ -193,7 +235,9 @@ public void LoadFormValues<T>(T values)
193235
if (fieldInfo.Name.Equals(formField.name))
194236
{
195237

238+
formField.RemoveOnValueChangedEvent();
196239
formField.value = fieldInfo.GetValue(values);
240+
formField.AddOnValueChangedEvent();
197241

198242
}
199243

@@ -220,38 +264,23 @@ public void LoadFormValues<T>(T values)
220264

221265
}
222266

223-
private void OnEnable()
267+
private void HandleValueChanged()
224268
{
225269

226-
if (submitButton)
227-
{
228-
229-
submitButton.onClick.AddListener(HandleReturnPress);
270+
FormChangedObject?.Invoke(GetFormRawValues());
230271

231-
}
232-
233-
}
234-
235-
private void OnDisable()
236-
{
237-
238-
if (submitButton)
239-
{
240-
241-
submitButton.onClick.RemoveListener(HandleReturnPress);
242-
243-
}
272+
FormChangedJSON?.Invoke(ToJSON());
244273

245274
}
246275

247276
[Serializable]
248-
public class SubmitEventObject : UnityEvent<Dictionary<string, object>>
277+
public class FormEventObjectEvent : UnityEvent<Dictionary<string, object>>
249278
{
250279

251280
}
252281

253282
[Serializable]
254-
public class SubmitEventJSON : UnityEvent<string>
283+
public class FormEventJSONEvent : UnityEvent<string>
255284
{
256285

257286
}

Scripts/FormField.cs

Lines changed: 153 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
22

33
using UnityEngine;
4+
using UnityEngine.Events;
45
using UnityEngine.UI;
56

67
namespace CandyCoded.Forms
@@ -19,9 +20,32 @@ public class FormField : MonoBehaviour
1920
private string _name;
2021
#pragma warning restore CS0649
2122

23+
public UnityEvent OnValueChanged;
24+
25+
private InputField _inputField;
26+
27+
private Toggle _toggleField;
28+
29+
private Dropdown _dropdownField;
30+
31+
private Slider _sliderField;
32+
33+
private Form _parentForm;
34+
2235
private object _value;
2336

24-
public Form parentForm { get; private set; }
37+
public Form parentForm
38+
{
39+
get
40+
{
41+
if (_parentForm == null)
42+
{
43+
_parentForm = gameObject.GetComponentInParent<Form>();
44+
}
45+
46+
return _parentForm;
47+
}
48+
}
2549

2650
public new string name
2751
{
@@ -43,31 +67,31 @@ public object value
4367
get
4468
{
4569

46-
if (gameObject.TryGetComponent<InputField>(out var inputField))
70+
if (_inputField)
4771
{
4872

49-
return inputField.text;
73+
return _inputField.text;
5074

5175
}
5276

53-
if (gameObject.TryGetComponent<Toggle>(out var toggle))
77+
if (_toggleField)
5478
{
5579

56-
return toggle.isOn;
80+
return _toggleField.isOn;
5781

5882
}
5983

60-
if (gameObject.TryGetComponent<Dropdown>(out var dropdown))
84+
if (_dropdownField)
6185
{
6286

63-
return dropdown.value;
87+
return _dropdownField.value;
6488

6589
}
6690

67-
if (gameObject.TryGetComponent<Slider>(out var slider))
91+
if (_sliderField)
6892
{
6993

70-
return slider.value;
94+
return _sliderField.value;
7195

7296
}
7397

@@ -76,57 +100,73 @@ public object value
76100
}
77101
set
78102
{
79-
if (gameObject.TryGetComponent<InputField>(out var inputField))
103+
if (_inputField)
80104
{
81105

82-
inputField.text = value.ToString();
106+
_inputField.text = value.ToString();
83107

84108
}
85109

86-
if (gameObject.TryGetComponent<Toggle>(out var toggle))
110+
if (_toggleField)
87111
{
88112

89113
if (bool.TryParse(value.ToString(), out var valueBool))
90114
{
91115

92-
toggle.isOn = valueBool;
116+
_toggleField.isOn = valueBool;
93117

94118
}
95119

96120
}
97121

98-
if (gameObject.TryGetComponent<Dropdown>(out var dropdown))
122+
if (_dropdownField)
99123
{
100124

101125
if (int.TryParse(value.ToString(), out var valueInt))
102126
{
103127

104-
dropdown.value = valueInt;
128+
_dropdownField.value = valueInt;
105129

106130
}
107131

108132
}
109133

110-
if (gameObject.TryGetComponent<Slider>(out var slider))
134+
if (_sliderField)
111135
{
112136

113137
if (float.TryParse(value.ToString(), out var valueFloat))
114138
{
115139

116-
slider.value = valueFloat;
140+
_sliderField.value = valueFloat;
117141

118142
}
119143

120144
}
121145

122146
_value = value;
147+
123148
}
124149
}
125150

126-
private void Awake()
151+
public void OnEnable()
152+
{
153+
154+
gameObject.TryGetComponent(out _inputField);
155+
156+
gameObject.TryGetComponent(out _toggleField);
157+
158+
gameObject.TryGetComponent(out _dropdownField);
159+
160+
gameObject.TryGetComponent(out _sliderField);
161+
162+
AddOnValueChangedEvent();
163+
164+
}
165+
166+
public void OnDisable()
127167
{
128168

129-
parentForm = gameObject.GetComponentInParent<Form>();
169+
RemoveOnValueChangedEvent();
130170

131171
}
132172

@@ -158,6 +198,100 @@ public void SetBoolValue(bool value)
158198

159199
}
160200

201+
public void AddOnValueChangedEvent()
202+
{
203+
204+
if (_inputField)
205+
{
206+
207+
_inputField.onValueChanged.AddListener(OnValueChangedEvent);
208+
209+
}
210+
211+
if (_toggleField)
212+
{
213+
214+
_toggleField.onValueChanged.AddListener(OnValueChangedEvent);
215+
216+
}
217+
218+
if (_dropdownField)
219+
{
220+
221+
_dropdownField.onValueChanged.AddListener(OnValueChangedEvent);
222+
223+
}
224+
225+
if (_sliderField)
226+
{
227+
228+
_sliderField.onValueChanged.AddListener(OnValueChangedEvent);
229+
230+
}
231+
232+
}
233+
234+
public void RemoveOnValueChangedEvent()
235+
{
236+
237+
if (_inputField)
238+
{
239+
240+
_inputField.onValueChanged.RemoveListener(OnValueChangedEvent);
241+
242+
}
243+
244+
if (_toggleField)
245+
{
246+
247+
_toggleField.onValueChanged.RemoveListener(OnValueChangedEvent);
248+
249+
}
250+
251+
if (_dropdownField)
252+
{
253+
254+
_dropdownField.onValueChanged.RemoveListener(OnValueChangedEvent);
255+
256+
}
257+
258+
if (_sliderField)
259+
{
260+
261+
_sliderField.onValueChanged.RemoveListener(OnValueChangedEvent);
262+
263+
}
264+
265+
}
266+
267+
private void OnValueChangedEvent(string _)
268+
{
269+
270+
OnValueChanged?.Invoke();
271+
272+
}
273+
274+
private void OnValueChangedEvent(bool _)
275+
{
276+
277+
OnValueChanged?.Invoke();
278+
279+
}
280+
281+
private void OnValueChangedEvent(float _)
282+
{
283+
284+
OnValueChanged?.Invoke();
285+
286+
}
287+
288+
private void OnValueChangedEvent(int _)
289+
{
290+
291+
OnValueChanged?.Invoke();
292+
293+
}
294+
161295
}
162296

163297
}

0 commit comments

Comments
 (0)