using UnityEngine;
using System.Collections.Generic;
using System;
using System.Linq;
namespace UIWidgets {
///
/// AutocompleteDataSource.
/// Set Autocomplete.DataSource with strings from file.
///
[AddComponentMenu("UI/UIWidgets/AutocompleteDataSource")]
[RequireComponent(typeof(Autocomplete))]
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 isStarted;
///
/// Start this instance.
///
public virtual void Start()
{
if (isStarted)
{
return ;
}
isStarted = true;
File = file;
}
///
/// Gets the items from file.
///
/// Source file.
public virtual void SetDataSource(TextAsset sourceFile)
{
if (file==null)
{
return ;
}
var new_items = sourceFile.text.Split(new string[] {"\r\n", "\r", "\n"}, StringSplitOptions.None).Select(StringTrimEnd).Where(x => x!="");
if (CommentsStartWith.Count > 0)
{
new_items = new_items.Where(NotComment);
}
GetComponent().DataSource = new_items.ToList();
}
///
/// Removes ending characters from specified string.
///
/// String.
/// String.
protected string StringTrimEnd(string str)
{
return str.TrimEnd();
}
///
/// Check if string is the comment.
///
/// true, if string not comment, false otherwise.
/// String.
protected virtual bool NotComment(string str)
{
return !CommentsStartWith.Any(str.StartsWith);
}
}
}