namespace UIWidgets { using System; using UnityEngine; /// /// Base class for the dialog to add/edit track. /// /// Type of the data. /// Type of the points. /// TrackForm type. public class TrackDialogBase : MonoBehaviour where TTrackForm : TrackFormBase where TData : class, ITrackData where TPoint : IComparable { /// /// Dialog template. /// [SerializeField] public Dialog DialogTemplate; /// /// Create header text. /// [SerializeField] public string HeaderCreate = "Create Track"; /// /// Create button text. /// [SerializeField] public string ButtonCreate = "Create"; /// /// Cancel button text. /// [SerializeField] public string ButtonCancel = "Cancel"; /// /// Edit header text. /// [SerializeField] public string HeaderEdit = "Edit Track"; /// /// Save button text. /// [SerializeField] public string ButtonEdit = "Save"; /// /// Create track. /// /// Function to call after track created. public void Create(Action> onSuccess) { var dialog = DialogTemplate.Clone(); var form = dialog.GetComponent(); form.Create(); OpenDialog(dialog, form, HeaderCreate, ButtonCreate, onSuccess); } /// /// Edit track. /// /// Track. /// Function to call after track edited. public void Edit(Track track, Action> onSuccess) { var dialog = DialogTemplate.Clone(); var form = dialog.GetComponent(); form.Edit(track); OpenDialog(dialog, form, HeaderEdit, ButtonEdit, onSuccess); } /// /// Open dialog. /// /// Dialog instance. /// Form. /// Title. /// OK button text. /// Function to call on OK button click. protected void OpenDialog(Dialog dialog, TTrackForm form, string title, string okButton, Action> onSuccess) { #if CSHARP_7_3_OR_NEWER bool OkAction(DialogBase dialogInstance, int index) #else Func OkAction = (dialogInstance, index) => #endif { onSuccess(form.Data); return true; } #if !CSHARP_7_3_OR_NEWER ; #endif var actions = new DialogButton[] { new DialogButton(okButton, OkAction), new DialogButton(ButtonCancel, DialogBase.DefaultClose), }; dialog.Show( title: title, buttons: actions, focusButton: okButton, modal: true, modalColor: new Color(0, 0, 0, 0.8f)); } } }