-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b21abc
Showing
15 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "CandyCoded.Forms", | ||
"references": [ | ||
"UnityEngine.UI" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Scott Doxey | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using UnityEngine; | ||
using UnityEngine.EventSystems; | ||
using UnityEngine.UI; | ||
|
||
namespace CandyCoded.Forms | ||
{ | ||
|
||
[AddComponentMenu("CandyCoded / Forms / Form")] | ||
public class Form : MonoBehaviour | ||
{ | ||
|
||
private EventSystem _eventSystem; | ||
|
||
private void Awake() | ||
{ | ||
|
||
_eventSystem = EventSystem.current; | ||
|
||
} | ||
|
||
private void Update() | ||
{ | ||
|
||
if (!Input.GetKeyDown(KeyCode.Tab)) | ||
{ | ||
return; | ||
} | ||
|
||
var selectable = _eventSystem.currentSelectedGameObject.GetComponent<Selectable>(); | ||
|
||
var allSelectable = _eventSystem.currentSelectedGameObject.GetComponentInParent<Form>() | ||
.GetComponentsInChildren<Selectable>(); | ||
|
||
var prevSelectable = selectable.FindSelectableOnUp() ?? allSelectable.Last(); | ||
var nextSelectable = selectable.FindSelectableOnDown() ?? allSelectable.First(); | ||
|
||
var next = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) | ||
? prevSelectable | ||
: nextSelectable; | ||
|
||
_eventSystem.SetSelectedGameObject(next.gameObject, null); | ||
|
||
} | ||
|
||
public Dictionary<string, object> GetFormRawValues() | ||
{ | ||
|
||
return gameObject.GetComponentsInChildren<FormField>().Where(field => field.name != "") | ||
.ToDictionary(field => field.name, field => field.value); | ||
|
||
} | ||
|
||
public T GetFormValues<T>() where T : class, new() | ||
{ | ||
|
||
var newObject = new T(); | ||
var newObjectType = newObject.GetType(); | ||
|
||
foreach (var item in GetFormRawValues()) | ||
{ | ||
newObjectType.GetField(item.Key)?.SetValue(newObject, item.Value); | ||
newObjectType.GetProperty(item.Key)?.SetValue(newObject, item.Value); | ||
} | ||
|
||
return newObject; | ||
|
||
} | ||
|
||
public void LoadFormRawValues(Dictionary<string, object> values) | ||
{ | ||
|
||
var formFields = gameObject.transform.GetComponentsInChildren<FormField>(); | ||
|
||
foreach (var value in values) | ||
{ | ||
|
||
foreach (var formField in formFields) | ||
{ | ||
|
||
if (value.Key.Equals(formField.name)) | ||
{ | ||
|
||
formField.value = value.Value; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public void LoadFormValues<T>(T values) | ||
{ | ||
|
||
var formFields = gameObject.transform.GetComponentsInChildren<FormField>(); | ||
|
||
foreach (var fieldInfo in values.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) | ||
{ | ||
|
||
foreach (var formField in formFields) | ||
{ | ||
|
||
if (fieldInfo.Name.Equals(formField.name)) | ||
{ | ||
|
||
formField.value = fieldInfo.GetValue(values); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
foreach (var propertyInfo in values.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) | ||
{ | ||
|
||
foreach (var formField in formFields) | ||
{ | ||
|
||
if (propertyInfo.Name.Equals(formField.name)) | ||
{ | ||
|
||
formField.value = propertyInfo.GetValue(values); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
namespace CandyCoded.Forms | ||
{ | ||
|
||
[AddComponentMenu("CandyCoded / Forms / Form Field")] | ||
public class FormField : MonoBehaviour | ||
{ | ||
|
||
private const string FIELD_NAME_PREFIX = "Field_"; | ||
|
||
private static int _fieldCount; | ||
|
||
#pragma warning disable CS0649 | ||
[SerializeField] | ||
private string _name; | ||
#pragma warning restore CS0649 | ||
|
||
private object _value; | ||
|
||
public new string name | ||
{ | ||
get | ||
{ | ||
if (_name == "") | ||
{ | ||
|
||
_name = $"{FIELD_NAME_PREFIX}{++_fieldCount}"; | ||
|
||
} | ||
|
||
return _name; | ||
} | ||
} | ||
|
||
public object value | ||
{ | ||
get | ||
{ | ||
|
||
if (gameObject.TryGetComponent<InputField>(out var inputField)) | ||
{ | ||
|
||
return inputField.text; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Toggle>(out var toggle)) | ||
{ | ||
|
||
return toggle.isOn; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Dropdown>(out var dropdown)) | ||
{ | ||
|
||
return dropdown.value; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Slider>(out var slider)) | ||
{ | ||
|
||
return slider.value; | ||
|
||
} | ||
|
||
return _value; | ||
|
||
} | ||
set | ||
{ | ||
if (gameObject.TryGetComponent<InputField>(out var inputField)) | ||
{ | ||
|
||
inputField.text = (string)value; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Toggle>(out var toggle)) | ||
{ | ||
|
||
toggle.isOn = (bool)value; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Dropdown>(out var dropdown)) | ||
{ | ||
|
||
dropdown.value = (int)value; | ||
|
||
} | ||
|
||
if (gameObject.TryGetComponent<Slider>(out var slider)) | ||
{ | ||
|
||
slider.value = (float)value; | ||
|
||
} | ||
|
||
_value = value; | ||
} | ||
} | ||
|
||
public void SetStringValue(string value) | ||
{ | ||
|
||
_value = value; | ||
|
||
} | ||
|
||
public void SetIntValue(int value) | ||
{ | ||
|
||
_value = value; | ||
|
||
} | ||
|
||
public void SetFloatValue(float value) | ||
{ | ||
|
||
_value = value; | ||
|
||
} | ||
|
||
public void SetBoolValue(bool value) | ||
{ | ||
|
||
_value = value; | ||
|
||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.