From 5b21abc4a580e24b2217accc174cf8101526eb96 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Fri, 10 Jul 2020 03:56:02 -0400 Subject: [PATCH] Initial commit. --- CandyCoded.Forms.asmdef | 15 ++++ CandyCoded.Forms.asmdef.meta | 7 ++ LICENSE.md | 21 ++++++ LICENSE.md.meta | 7 ++ Scripts.meta | 8 ++ Scripts/Form.cs | 141 +++++++++++++++++++++++++++++++++++ Scripts/Form.cs.meta | 11 +++ Scripts/FormField.cs | 139 ++++++++++++++++++++++++++++++++++ Scripts/FormField.cs.meta | 11 +++ Scripts/FormLabel.cs | 49 ++++++++++++ Scripts/FormLabel.cs.meta | 11 +++ Version.txt | 1 + Version.txt.meta | 7 ++ package.json | 27 +++++++ package.json.meta | 7 ++ 15 files changed, 462 insertions(+) create mode 100644 CandyCoded.Forms.asmdef create mode 100644 CandyCoded.Forms.asmdef.meta create mode 100644 LICENSE.md create mode 100644 LICENSE.md.meta create mode 100644 Scripts.meta create mode 100644 Scripts/Form.cs create mode 100644 Scripts/Form.cs.meta create mode 100644 Scripts/FormField.cs create mode 100644 Scripts/FormField.cs.meta create mode 100644 Scripts/FormLabel.cs create mode 100644 Scripts/FormLabel.cs.meta create mode 100644 Version.txt create mode 100644 Version.txt.meta create mode 100644 package.json create mode 100644 package.json.meta diff --git a/CandyCoded.Forms.asmdef b/CandyCoded.Forms.asmdef new file mode 100644 index 0000000..5e9886e --- /dev/null +++ b/CandyCoded.Forms.asmdef @@ -0,0 +1,15 @@ +{ + "name": "CandyCoded.Forms", + "references": [ + "UnityEngine.UI" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/CandyCoded.Forms.asmdef.meta b/CandyCoded.Forms.asmdef.meta new file mode 100644 index 0000000..a0dcb6c --- /dev/null +++ b/CandyCoded.Forms.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c8933e07309364c7cbdaeda7b8d55a6a +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..97752be --- /dev/null +++ b/LICENSE.md @@ -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. diff --git a/LICENSE.md.meta b/LICENSE.md.meta new file mode 100644 index 0000000..a5a133d --- /dev/null +++ b/LICENSE.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 523268dbcf9b541939b117fbc5b68b76 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts.meta b/Scripts.meta new file mode 100644 index 0000000..601c516 --- /dev/null +++ b/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 68f337c9c2c544832a7b610f74383a60 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Form.cs b/Scripts/Form.cs new file mode 100644 index 0000000..395c7ab --- /dev/null +++ b/Scripts/Form.cs @@ -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(); + + var allSelectable = _eventSystem.currentSelectedGameObject.GetComponentInParent
() + .GetComponentsInChildren(); + + 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 GetFormRawValues() + { + + return gameObject.GetComponentsInChildren().Where(field => field.name != "") + .ToDictionary(field => field.name, field => field.value); + + } + + public T GetFormValues() 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 values) + { + + var formFields = gameObject.transform.GetComponentsInChildren(); + + foreach (var value in values) + { + + foreach (var formField in formFields) + { + + if (value.Key.Equals(formField.name)) + { + + formField.value = value.Value; + + } + + } + + } + + } + + public void LoadFormValues(T values) + { + + var formFields = gameObject.transform.GetComponentsInChildren(); + + 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); + + } + + } + + } + + } + + } + +} diff --git a/Scripts/Form.cs.meta b/Scripts/Form.cs.meta new file mode 100644 index 0000000..a41896d --- /dev/null +++ b/Scripts/Form.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8bc435d2d546f42d28c4da79d99e0051 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/FormField.cs b/Scripts/FormField.cs new file mode 100644 index 0000000..a87c8c4 --- /dev/null +++ b/Scripts/FormField.cs @@ -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(out var inputField)) + { + + return inputField.text; + + } + + if (gameObject.TryGetComponent(out var toggle)) + { + + return toggle.isOn; + + } + + if (gameObject.TryGetComponent(out var dropdown)) + { + + return dropdown.value; + + } + + if (gameObject.TryGetComponent(out var slider)) + { + + return slider.value; + + } + + return _value; + + } + set + { + if (gameObject.TryGetComponent(out var inputField)) + { + + inputField.text = (string)value; + + } + + if (gameObject.TryGetComponent(out var toggle)) + { + + toggle.isOn = (bool)value; + + } + + if (gameObject.TryGetComponent(out var dropdown)) + { + + dropdown.value = (int)value; + + } + + if (gameObject.TryGetComponent(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; + + } + + } + +} diff --git a/Scripts/FormField.cs.meta b/Scripts/FormField.cs.meta new file mode 100644 index 0000000..2c29022 --- /dev/null +++ b/Scripts/FormField.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: da9bfe098f16a4c6b939f9dc8d4a7f1b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/FormLabel.cs b/Scripts/FormLabel.cs new file mode 100644 index 0000000..42fa143 --- /dev/null +++ b/Scripts/FormLabel.cs @@ -0,0 +1,49 @@ +// 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.EventSystems; +using UnityEngine.UI; + +namespace CandyCoded.Forms +{ + + [AddComponentMenu("CandyCoded / Forms / Form Label")] + public class FormLabel : MonoBehaviour, IPointerClickHandler + { + +#pragma warning disable CS0649 + [SerializeField] + private Selectable _selectable; +#pragma warning restore CS0649 + + private EventSystem _eventSystem; + + private void Awake() + { + + _eventSystem = EventSystem.current; + + } + + private void OnEnable() + { + + if (!_selectable) + { + + _selectable = gameObject.transform.parent.GetComponentInChildren(); + + } + + } + + public void OnPointerClick(PointerEventData eventData) + { + + _eventSystem.SetSelectedGameObject(_selectable.gameObject, null); + + } + + } + +} diff --git a/Scripts/FormLabel.cs.meta b/Scripts/FormLabel.cs.meta new file mode 100644 index 0000000..9b54367 --- /dev/null +++ b/Scripts/FormLabel.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9f400cd0438fc4d73857d85ba555ba1e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Version.txt b/Version.txt new file mode 100644 index 0000000..0ec25f7 --- /dev/null +++ b/Version.txt @@ -0,0 +1 @@ +v1.0.0 diff --git a/Version.txt.meta b/Version.txt.meta new file mode 100644 index 0000000..6c989b0 --- /dev/null +++ b/Version.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2e3e3fec1016d44be8fb1fc5be8194d3 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json new file mode 100644 index 0000000..e5f82a6 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "xyz.candycoded.forms", + "displayName": "CandyCoded.Forms", + "version": "1.0.0", + "unity": "2019.4", + "unityRelease": "0f1", + "description": "Components used to simplify the handling of form inputs in Unity.", + "license": "MIT", + "dependencies": { + "com.unity.ugui": "1.0.0", + "com.unity.modules.uielements": "1.0.0" + }, + "keywords": [ + "unity", + "form" + ], + "author": { + "name": "Scott Doxey", + "email": "hello@scottdoxey.com", + "homepage": "http://scottdoxey.com/" + }, + "homepage": "https://github.com/CandyCoded/Forms", + "repository": { + "type": "git", + "url": "git@github.com:CandyCoded/Forms.git" + } +} diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..adbdea5 --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 40a27118fbee544ad9b63f7c21ed3120 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: