namespace UIWidgets
{
using System;
using System.Collections.Generic;
using UnityEngine;
///
/// AutocompleteDataSource.
/// Set Autocomplete.DataSource with strings from file.
///
[AddComponentMenu("UI/New UI Widgets/Collections/Autocomplete DataSource")]
public class AutocompleteDataSource : MonoBehaviour
{
[SerializeField]
TextAsset file;
///
/// Gets or sets the file.
///
/// The file.
public TextAsset File
{
get
{
return file;
}
set
{
file = value;
if (file != null)
{
SetDataSource(file);
}
}
}
///
/// The comments in file start with specified strings.
///
[SerializeField]
public List CommentsStartWith = new List() { "#", "//" };
bool isInited;
///
/// Start this instance.
///
public virtual void Start()
{
Init();
}
///
/// Init this instance.
///
public virtual void Init()
{
if (isInited)
{
return;
}
isInited = true;
File = file;
}
///
/// Gets the items from file.
///
/// Source file.
public virtual void SetDataSource(TextAsset sourceFile)
{
if (file == null)
{
return;
}
var data = new List();
foreach (var item in sourceFile.text.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
var trimmed = item.TrimEnd();
if (!string.IsNullOrEmpty(trimmed))
{
if (!IsComment(trimmed))
{
data.Add(trimmed);
}
}
}
var autocomplete = GetComponent();
if (autocomplete != null)
{
autocomplete.DataSource = data;
}
else
{
#pragma warning disable 0618
var autocomplete_obsolete = GetComponent();
#pragma warning restore 0618
if (autocomplete_obsolete != null)
{
autocomplete_obsolete.DataSource = data;
}
}
}
///
/// Is comment?
///
/// String to check.
/// true if input is comment; otherwise false.
protected virtual bool IsComment(string str)
{
for (int i = 0; i < CommentsStartWith.Count; i++)
{
if (str.StartsWith(CommentsStartWith[i], StringComparison.InvariantCulture))
{
return true;
}
}
return false;
}
}
}