namespace UIWidgets
{
using System;
using System.IO;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
///
/// FileDialog.
///
public class FileDialog : Picker, IUpgradeable
{
///
/// ListView.
///
[SerializeField]
public FileListView FileListView;
///
/// Confirm Dialog.
///
[SerializeField]
public PickerBool ConfirmDialog;
///
/// Filename Input.
///
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with FilenameInputAdapter.")]
public InputField FilenameInput;
///
/// Proxy for InputField.
/// Required to improve compatibility between different InputFields (like Unity.UI and TextMeshPro versions).
///
[Obsolete("Replaced with FilenameInputAdapter.")]
protected IInputFieldProxy FilenameInputProxy
{
get
{
return FilenameInputAdapter;
}
}
///
/// Filename Input.
///
[SerializeField]
public InputFieldAdapter FilenameInputAdapter;
///
/// OK button.
///
[SerializeField]
public Button OkButton;
///
/// Is specified file should exists?
///
[SerializeField]
public bool FileShouldExists = false;
///
/// Request confirmation if file exists?
///
[SerializeField]
[FormerlySerializedAs("RequestConfirmationIsFileExists")]
public bool RequestConfirmationIfFileExists = true;
///
/// Init InputProxy.
///
protected virtual void InitFilenameInputProxy()
{
}
///
/// Prepare picker to open.
///
/// Default value.
public override void BeforeOpen(string defaultValue)
{
InitFilenameInputProxy();
FileListView.OnSelectObject.AddListener(FileSelected);
FilenameInputAdapter.onValueChanged.AddListener(FilenameChanged);
OkButton.onClick.AddListener(OkClick);
if (!string.IsNullOrEmpty(defaultValue))
{
FileListView.CurrentDirectory = Path.GetDirectoryName(defaultValue);
FileListView.Select(FileListView.FullName2Index(defaultValue));
}
FilenameChanged(FilenameInputAdapter.text);
}
///
/// Callback when file selected.
///
/// Index.
protected virtual void FileSelected(int index)
{
FilenameInputAdapter.text = FileListView.SelectedItem.IsFile
? Path.GetFileName(FileListView.SelectedItem.FullName)
: string.Empty;
}
///
/// Callback when filename changed.
///
/// Filename.
protected virtual void FilenameChanged(string filename)
{
OkButton.interactable = IsValidFile(filename);
}
///
/// Is file valid?
///
/// Filename.
/// true if file valid; otherwise, false.
protected virtual bool IsValidFile(string filename)
{
if (string.IsNullOrEmpty(filename))
{
return false;
}
var fullname = Path.Combine(FileListView.CurrentDirectory, filename);
if (FileShouldExists && !File.Exists(fullname))
{
return false;
}
return true;
}
///
/// Handle OK click event.
///
public void OkClick()
{
var fullname = Path.Combine(FileListView.CurrentDirectory, FilenameInputAdapter.text);
if (RequestConfirmationIfFileExists && File.Exists(fullname))
{
var confirm = ConfirmDialog.Clone();
confirm.Show(false, ConfirmAction, DoNothing);
}
else
{
Selected(fullname);
}
}
///
/// Process confirmation.
///
/// If action confirmed.
protected void ConfirmAction(bool isConfirmed)
{
if (isConfirmed)
{
var fullname = Path.Combine(FileListView.CurrentDirectory, FilenameInputAdapter.text);
Selected(fullname);
}
}
///
/// Do nothing.
///
protected virtual void DoNothing()
{
}
///
/// Prepare picker to close.
///
public override void BeforeClose()
{
FileListView.OnSelectObject.RemoveListener(FileSelected);
FilenameInputAdapter.onValueChanged.RemoveListener(FilenameChanged);
OkButton.onClick.RemoveListener(OkClick);
}
///
/// Upgrade this instance.
///
public virtual void Upgrade()
{
#pragma warning disable 0612, 0618
Utilities.GetOrAddComponent(FilenameInput, ref FilenameInputAdapter);
#pragma warning restore 0612, 0618
}
#if UNITY_EDITOR
///
/// Validate this instance.
///
protected virtual void OnValidate()
{
Compatibility.Upgrade(this);
}
#endif
#region IStylable implementation
///
public override bool SetStyle(Style style)
{
base.SetStyle(style);
FileListView.SetStyle(style);
style.InputField.ApplyTo(FilenameInputAdapter);
style.Dialog.Button.ApplyTo(OkButton.gameObject);
style.Dialog.Button.ApplyTo(transform.Find("Buttons/Cancel"));
ConfirmDialog.SetStyle(style);
return true;
}
///
public override bool GetStyle(Style style)
{
base.GetStyle(style);
FileListView.GetStyle(style);
style.InputField.GetFrom(FilenameInputAdapter);
style.Dialog.Button.GetFrom(OkButton.gameObject);
style.Dialog.Button.GetFrom(transform.Find("Buttons/Cancel"));
ConfirmDialog.GetStyle(style);
return true;
}
#endregion
}
}