Skip to content

Commit 5b21abc

Browse files
committed
Initial commit.
0 parents  commit 5b21abc

15 files changed

+462
-0
lines changed

CandyCoded.Forms.asmdef

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "CandyCoded.Forms",
3+
"references": [
4+
"UnityEngine.UI"
5+
],
6+
"includePlatforms": [],
7+
"excludePlatforms": [],
8+
"allowUnsafeCode": false,
9+
"overrideReferences": false,
10+
"precompiledReferences": [],
11+
"autoReferenced": true,
12+
"defineConstraints": [],
13+
"versionDefines": [],
14+
"noEngineReferences": false
15+
}

CandyCoded.Forms.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Scott Doxey
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

LICENSE.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Form.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using UnityEngine;
7+
using UnityEngine.EventSystems;
8+
using UnityEngine.UI;
9+
10+
namespace CandyCoded.Forms
11+
{
12+
13+
[AddComponentMenu("CandyCoded / Forms / Form")]
14+
public class Form : MonoBehaviour
15+
{
16+
17+
private EventSystem _eventSystem;
18+
19+
private void Awake()
20+
{
21+
22+
_eventSystem = EventSystem.current;
23+
24+
}
25+
26+
private void Update()
27+
{
28+
29+
if (!Input.GetKeyDown(KeyCode.Tab))
30+
{
31+
return;
32+
}
33+
34+
var selectable = _eventSystem.currentSelectedGameObject.GetComponent<Selectable>();
35+
36+
var allSelectable = _eventSystem.currentSelectedGameObject.GetComponentInParent<Form>()
37+
.GetComponentsInChildren<Selectable>();
38+
39+
var prevSelectable = selectable.FindSelectableOnUp() ?? allSelectable.Last();
40+
var nextSelectable = selectable.FindSelectableOnDown() ?? allSelectable.First();
41+
42+
var next = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)
43+
? prevSelectable
44+
: nextSelectable;
45+
46+
_eventSystem.SetSelectedGameObject(next.gameObject, null);
47+
48+
}
49+
50+
public Dictionary<string, object> GetFormRawValues()
51+
{
52+
53+
return gameObject.GetComponentsInChildren<FormField>().Where(field => field.name != "")
54+
.ToDictionary(field => field.name, field => field.value);
55+
56+
}
57+
58+
public T GetFormValues<T>() where T : class, new()
59+
{
60+
61+
var newObject = new T();
62+
var newObjectType = newObject.GetType();
63+
64+
foreach (var item in GetFormRawValues())
65+
{
66+
newObjectType.GetField(item.Key)?.SetValue(newObject, item.Value);
67+
newObjectType.GetProperty(item.Key)?.SetValue(newObject, item.Value);
68+
}
69+
70+
return newObject;
71+
72+
}
73+
74+
public void LoadFormRawValues(Dictionary<string, object> values)
75+
{
76+
77+
var formFields = gameObject.transform.GetComponentsInChildren<FormField>();
78+
79+
foreach (var value in values)
80+
{
81+
82+
foreach (var formField in formFields)
83+
{
84+
85+
if (value.Key.Equals(formField.name))
86+
{
87+
88+
formField.value = value.Value;
89+
90+
}
91+
92+
}
93+
94+
}
95+
96+
}
97+
98+
public void LoadFormValues<T>(T values)
99+
{
100+
101+
var formFields = gameObject.transform.GetComponentsInChildren<FormField>();
102+
103+
foreach (var fieldInfo in values.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
104+
{
105+
106+
foreach (var formField in formFields)
107+
{
108+
109+
if (fieldInfo.Name.Equals(formField.name))
110+
{
111+
112+
formField.value = fieldInfo.GetValue(values);
113+
114+
}
115+
116+
}
117+
118+
}
119+
120+
foreach (var propertyInfo in values.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
121+
{
122+
123+
foreach (var formField in formFields)
124+
{
125+
126+
if (propertyInfo.Name.Equals(formField.name))
127+
{
128+
129+
formField.value = propertyInfo.GetValue(values);
130+
131+
}
132+
133+
}
134+
135+
}
136+
137+
}
138+
139+
}
140+
141+
}

Scripts/Form.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/FormField.cs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
namespace CandyCoded.Forms
7+
{
8+
9+
[AddComponentMenu("CandyCoded / Forms / Form Field")]
10+
public class FormField : MonoBehaviour
11+
{
12+
13+
private const string FIELD_NAME_PREFIX = "Field_";
14+
15+
private static int _fieldCount;
16+
17+
#pragma warning disable CS0649
18+
[SerializeField]
19+
private string _name;
20+
#pragma warning restore CS0649
21+
22+
private object _value;
23+
24+
public new string name
25+
{
26+
get
27+
{
28+
if (_name == "")
29+
{
30+
31+
_name = $"{FIELD_NAME_PREFIX}{++_fieldCount}";
32+
33+
}
34+
35+
return _name;
36+
}
37+
}
38+
39+
public object value
40+
{
41+
get
42+
{
43+
44+
if (gameObject.TryGetComponent<InputField>(out var inputField))
45+
{
46+
47+
return inputField.text;
48+
49+
}
50+
51+
if (gameObject.TryGetComponent<Toggle>(out var toggle))
52+
{
53+
54+
return toggle.isOn;
55+
56+
}
57+
58+
if (gameObject.TryGetComponent<Dropdown>(out var dropdown))
59+
{
60+
61+
return dropdown.value;
62+
63+
}
64+
65+
if (gameObject.TryGetComponent<Slider>(out var slider))
66+
{
67+
68+
return slider.value;
69+
70+
}
71+
72+
return _value;
73+
74+
}
75+
set
76+
{
77+
if (gameObject.TryGetComponent<InputField>(out var inputField))
78+
{
79+
80+
inputField.text = (string)value;
81+
82+
}
83+
84+
if (gameObject.TryGetComponent<Toggle>(out var toggle))
85+
{
86+
87+
toggle.isOn = (bool)value;
88+
89+
}
90+
91+
if (gameObject.TryGetComponent<Dropdown>(out var dropdown))
92+
{
93+
94+
dropdown.value = (int)value;
95+
96+
}
97+
98+
if (gameObject.TryGetComponent<Slider>(out var slider))
99+
{
100+
101+
slider.value = (float)value;
102+
103+
}
104+
105+
_value = value;
106+
}
107+
}
108+
109+
public void SetStringValue(string value)
110+
{
111+
112+
_value = value;
113+
114+
}
115+
116+
public void SetIntValue(int value)
117+
{
118+
119+
_value = value;
120+
121+
}
122+
123+
public void SetFloatValue(float value)
124+
{
125+
126+
_value = value;
127+
128+
}
129+
130+
public void SetBoolValue(bool value)
131+
{
132+
133+
_value = value;
134+
135+
}
136+
137+
}
138+
139+
}

Scripts/FormField.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)