Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Submit event #5

Merged
merged 3 commits into from
Jul 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

Expand All @@ -14,6 +16,11 @@ namespace CandyCoded.Forms
public class Form : MonoBehaviour
{

public SubmitEvent FormSubmitted;

[SerializeField]
public Button _submitButton;

private EventSystem _eventSystem;

private Form _parentForm;
Expand All @@ -25,21 +32,38 @@ private void Awake()

_parentForm = gameObject.GetComponentsInParent<Form>().FirstOrDefault(form => !form.Equals(this));

if (_submitButton)
{

_submitButton.onClick.AddListener(HandleReturnPress);

}

}

private void Update()
{

if (_eventSystem.currentSelectedGameObject == null)
if (_eventSystem.currentSelectedGameObject == null || _parentForm != null)
{
return;
}

if (!Input.GetKeyDown(KeyCode.Tab) || _parentForm != null)
if (Input.GetKeyDown(KeyCode.Tab))
{
return;
HandleTabPress();
}

if (Input.GetKeyDown(KeyCode.Return))
{
HandleReturnPress();
}

}

private void HandleTabPress()
{

var selectable = _eventSystem.currentSelectedGameObject.GetComponent<Selectable>();

var allSelectable = _eventSystem.currentSelectedGameObject.GetComponentInParent<Form>()
Expand All @@ -56,6 +80,13 @@ private void Update()

}

private void HandleReturnPress()
{

FormSubmitted?.Invoke(GetFormRawValues());

}

public IEnumerable<FormField> GetChildFormFields()
{

Expand Down Expand Up @@ -152,6 +183,12 @@ public void LoadFormValues<T>(T values)

}

[Serializable]
public class SubmitEvent : UnityEvent<Dictionary<string, object>>
{

}

}

}