Marquee
Scrolling message across the bottom of the screen
Tickertape.cs
1 // Copyright 2018,19 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using System.Collections.Generic;
5 using CustomAsset;
6 using CustomAsset.Constant;
7 using CustomAsset.Mutable;
8 using UnityEditor;
9 using UnityEngine;
10 using Object = UnityEngine.Object;
11 using Random = UnityEngine.Random;
12 using String = CustomAsset.Mutable.String;
13 
14 namespace Askowl {
15  /// <a href="http://bit.ly/2R8doWe">Manager Custom Asset to serve messages to the marquee</a>
16  [CreateAssetMenu(menuName = "Managers/Tickertape"), Serializable]
17  public sealed class Tickertape : Manager {
18  [SerializeField] private bool autoStart = true;
19  [SerializeField] private float secondsBetweenFeeds = 0.2f;
20  [SerializeField] private Quotes quotes = default;
21  [SerializeField] private String showing = default;
22  [SerializeField] private Trigger showingComplete = default;
23 
24  private readonly Fifo<string> texts = Fifo<string>.Instance;
25  private List<Quotes> allQuotes;
26  private readonly Map loadedQuotes = new Map();
27 
28  /// <a href="http://bit.ly/2R8doWe">Combined total of all messages that can be served</a>
29  public int[] Counts => allQuotes.ConvertAll(q => q.Count).ToArray();
30 
31  protected override void Initialise() {
32  base.Initialise();
33  allQuotes = new List<Quotes>();
34  Add(quotes);
35 
36  string noQuotes() {
37  #if UNITY_EDITOR
38  Selection.activeObject = (quotes as Object) ?? this;
39  #endif
40  return "Add to the Quotes custom asset in the resource often called 'Content'";
41  }
42 
43  string pick() => texts.Pop() ?? ((allQuotes.Count > 0)
44  ? allQuotes[Random.Range(0, allQuotes.Count)].Pick()
45  : noQuotes());
46 
47  showFiber = Fiber.Instance.Begin.Do(_ => showing.Value = pick())
48  .WaitFor(showingComplete.Emitter).WaitFor(secondsBetweenFeeds).Again;
49  if (autoStart) Show();
50  }
51 
52  /// <a href="http://bit.ly/2RuJR8B">Start showing messages from the currently loaded Quote custom assets</a>
53  public void Show() => showFiber.Exit().Go();
54  private Fiber showFiber;
55 
56  /// <a href="http://bit.ly/2RcaBew">Inject a message to be displayed after the current one has finished</a>
57  public void ShowNext(string text) => texts.Push(text);
58 
59  /// <a href="http://bit.ly/2R9IJb5">Inject a message to show right now - removing any half-finished message first</a>
60  public void ShowImmediate(string text) => showing.Value = text;
61 
62  /// <a href="http://bit.ly/2RvUjg9">Stop displaying messages after the current one is done</a>
63  public void Stop() => showFiber.Exit();
64 
65  /// <a href="http://bit.ly/2REqmux">Add more messages contained in a Quote custom asset</a>
66  // ReSharper disable once UnusedMethodReturnValue.Global
67  public Tickertape Add(Quotes moreQuotes) {
68  if ((moreQuotes == default) || (moreQuotes.Count == 0) || loadedQuotes[moreQuotes.name].Found) return this;
69 
70  loadedQuotes.Add(moreQuotes.name);
71  allQuotes.Add(moreQuotes);
72  return this;
73  }
74 
75  /// <a href="http://bit.ly/2GVrcP7">Remove all messages from the list to display</a>
76  // ReSharper disable once UnusedMethodReturnValue.Global
77  public Tickertape Clear() {
78  loadedQuotes.Clear();
79  allQuotes.Clear();
80  return this;
81  }
82  }
83 }
int [] Counts
Combined total of all messages that can be served
Definition: Tickertape.cs:29
Manager Custom Asset to serve messages to the marquee
Definition: Tickertape.cs:17
void Show()
Start showing messages from the currently loaded Quote custom assets
Tickertape Clear()
Remove all messages from the list to display
Definition: Tickertape.cs:77
Tickertape Add(Quotes moreQuotes)
Add more messages contained in a Quote custom asset
Definition: Tickertape.cs:67
void Stop()
Stop displaying messages after the current one is done
void ShowNext(string text)
Inject a message to be displayed after the current one has finished
void ShowImmediate(string text)
Inject a message to show right now - removing any half-finished message first