Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
neogeek committed Jul 10, 2020
0 parents commit 5b21abc
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 0 deletions.
15 changes: 15 additions & 0 deletions CandyCoded.Forms.asmdef
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
}
7 changes: 7 additions & 0 deletions CandyCoded.Forms.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE.md
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.
7 changes: 7 additions & 0 deletions LICENSE.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions Scripts/Form.cs
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);

}

}

}

}

}

}
11 changes: 11 additions & 0 deletions Scripts/Form.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

139 changes: 139 additions & 0 deletions Scripts/FormField.cs
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;

}

}

}
11 changes: 11 additions & 0 deletions Scripts/FormField.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b21abc

Please sign in to comment.