CustomAssets
Askowl Custom Assets
Quotes.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using System.Text.RegularExpressions;
5 using Askowl;
6 using UnityEditor;
7 using UnityEngine;
8 
9 namespace CustomAsset.Constant {
10  /// <a href="http://bit.ly/2QR9rF8">Class for picking one or a list of quotes - either set in the inspector or in a separate text file. Each quote is on a separate line in the form:</a> <inheritdoc />
11  [Serializable] public sealed class QuoteSet : Set<string> {
12  [SerializeField, Tooltip("Asset with one quote per line (with attribution in brackets at end)")]
13  private TextAsset[] quoteFiles = default;
14 
15  /// <a href="http://bit.ly/2QR9rF8"></a> <inheritdoc />
16  protected override void BuildSelector() {
17  Fifo<string> choices = new Fifo<string>();
18  base.BuildSelector(); // renews Choices
19  Rtf(choices, Selector.Choices);
20 
21  for (var i = 0; i < quoteFiles.Length; i++) {
22  if (quoteFiles[0] != null) Rtf(choices, quoteFiles[i].text.Split('\n'));
23  }
24 
25  if (choices.Count > 0) Selector.Choices = choices.ToArray();
26  }
27 
28  /// <a href="http://bit.ly/2QR9rF8"></a>
29  public int Count => Selector.Choices.Length;
30 
31  /// <a href="http://bit.ly/2RjdIRe">Turn a list of strings into a list of formatted quotes</a>
32  public static void Rtf(Fifo<string> to, string[] from) {
33  for (int i = 0; i < from.Length; i++) {
34  if (!string.IsNullOrEmpty(from[i])) to.Push(Rtf(from[i]));
35  }
36  }
37 
38  /// <a href="http://bit.ly/2RjdIRe">Turn a string into a quote. Any text at the end of the string that is in brackets becomes an attribution in grey</a>
39  public static string Rtf(string quote) =>
40  Regex.Replace(
41  input: quote, pattern: @"^(.*?)\s*\((.*)\)$", evaluator: m =>
42  $"<b>\"</b><i>{m.Groups[1].Value}</i><b>\"</b> <color=grey>{(m.Groups.Count > 1 ? m.Groups[2].Value : "")}</color>");
43  }
44 
45  /// <a href="http://bit.ly/2QR9rF8">Custom Asset for picking one or a list of quotes - either kept in the asset or as a separate text file. Each quote is on a separate line in the form: <code>Quote body (attribution)</code></a> <inheritdoc cref="QuoteSet" />
46  [CreateAssetMenu(menuName = "Custom Assets/Constant/Quotes")]
47  public class Quotes : OfType<QuoteSet>, Pick<string> {
48  /// <a href="http://bit.ly/2QR9rF8"></a> <inheritdoc cref="Quotes()" />
49  protected override void OnEnable() {
50  base.OnEnable();
51  #if UNITY_EDITOR
52  if (!EditorApplication.isPlayingOrWillChangePlaymode) return;
53  #endif
54  Value.Reset();
55  }
56 
57  /// <a href="http://bit.ly/2QMafv0"></a> <inheritdoc />
58  public string Pick() => Value.Pick();
59 
60  /// <a href="http://bit.ly/2RjdIRe"><see cref="QuoteSet.Rtf(string)"/></a>
61  public static string Rtf(string quote) => QuoteSet.Rtf(quote);
62 
63  /// <a href="http://bit.ly/2QR9rF8"></a>
64  public int Count => Value.Count;
65  }
66 }
Custom Asset for picking one or a list of quotes - either kept in the asset or as a separate text fil...
Definition: Quotes.cs:47
static void Rtf(Fifo< string > to, string[] from)
Turn a list of strings into a list of formatted quotes
Definition: Quotes.cs:32
Base class for a custom asset. Provides getters and setters for the contained value and templates for...
override void BuildSelector()
Definition: Quotes.cs:16
override void OnEnable()
Definition: Quotes.cs:49
Class for picking one or a list of quotes - either set in the inspector or in a separate text file...
Definition: Quotes.cs:11