rimworld-blax-laws/Source/BlaxLaws/UI/ActPickerTree.cs

155 lines
5.6 KiB
C#

using System.Collections.Generic;
using RimWorld;
using Verse;
namespace BlaxLaws
{
/// <summary>One visible line of the act picker: a category header or an entry.</summary>
public sealed class ActPickerRow
{
public ActCategoryDef category;
/// <summary>Null for a category header.</summary>
public ActEntry entry;
/// <summary>Header: the category is open.</summary>
public bool open;
/// <summary>Header: entries that pass the search (all of them without a search).</summary>
public int count;
/// <summary>Entry: the lawbook has a row for this act key (the tick).</summary>
public bool inBook;
public bool IsHeader => entry == null;
public override string ToString() => IsHeader ? "[" + category?.defName + " " + count + (open ? " open" : "") + "]" : entry.key + (inBook ? " (in book)" : "");
}
/// <summary>
/// The act picker's tree (design: "Act picker"): the catalogue's categories as collapsible headers, each
/// with its groups first and then every entry A to Z (ActCatalogue's order). The search matches each
/// entry's label and defName; typing filters every category at once, opens those with hits and hides
/// those without. The visible lines are rebuilt only when the search, an open state, the catalogue or the
/// lawbook changes, so the window draws only the lines in view, even with hundreds of bills.
/// </summary>
public sealed class ActPickerTree
{
public readonly QuickSearchFilter filter = new QuickSearchFilter();
private readonly HashSet<string> open = new HashSet<string>();
private readonly List<ActPickerRow> rows = new List<ActPickerRow>();
private readonly HashSet<string> inBook = new HashSet<string>();
private string builtText;
private int builtCatalogue = -1;
private int builtWatch = -1;
private int builtLaws = -1;
private bool dirty = true;
/// <summary>The lines to draw, top to bottom.</summary>
public IReadOnlyList<ActPickerRow> Rows
{
get
{
Refresh();
return rows;
}
}
/// <summary>Entries the search leaves, over every category.</summary>
public int Hits { get; private set; }
public string SearchText
{
get => filter.Text;
set
{
if (filter.Text == value)
return;
filter.Text = value ?? "";
dirty = true;
}
}
/// <summary>Whether the category's entries show. While searching, a category with hits is always open.</summary>
public bool IsOpen(string categoryId) => filter.Active || open.Contains(categoryId);
/// <summary>Opens or closes a category (no effect while searching, when hits decide).</summary>
public void Toggle(string categoryId)
{
if (filter.Active || categoryId == null)
return;
if (!open.Remove(categoryId))
open.Add(categoryId);
dirty = true;
}
public void SetOpen(string categoryId, bool isOpen)
{
if (categoryId == null)
return;
if (isOpen ? open.Add(categoryId) : open.Remove(categoryId))
dirty = true;
}
/// <summary>Whether the entry passes the search: its label or its defName (a group's id) contains the text.</summary>
public bool Matches(ActEntry e)
{
if (e == null)
return false;
if (!filter.Active)
return true;
return filter.Matches(e.label) || filter.Matches(e.name) || (e.def != null && filter.Matches(e.def.defName));
}
/// <summary>The entry rows of a category that pass the search, in picker order.</summary>
public List<ActEntry> EntriesShown(string categoryId)
{
var list = new List<ActEntry>();
foreach (ActEntry e in ActCatalogue.EntriesOf(categoryId))
{
if (Matches(e))
list.Add(e);
}
return list;
}
public void MarkDirty() => dirty = true;
private void Refresh()
{
int laws = Lawbook.Laws.Count;
if (!dirty && builtText == filter.Text && builtCatalogue == ActCatalogue.Version && builtWatch == Watch.Version && builtLaws == laws)
return;
dirty = false;
builtText = filter.Text;
builtCatalogue = ActCatalogue.Version;
builtWatch = Watch.Version;
builtLaws = laws;
inBook.Clear();
foreach (Law l in Lawbook.Laws)
{
if (!l.actKey.NullOrEmpty())
inBook.Add(l.actKey);
}
rows.Clear();
Hits = 0;
foreach (ActCategoryDef cat in ActCatalogue.Categories)
{
List<ActEntry> shown = EntriesShown(cat.defName);
Hits += shown.Count;
if (filter.Active && shown.Count == 0)
continue;
bool isOpen = IsOpen(cat.defName);
rows.Add(new ActPickerRow { category = cat, open = isOpen, count = shown.Count });
if (!isOpen)
continue;
foreach (ActEntry e in shown)
rows.Add(new ActPickerRow { category = cat, entry = e, inBook = inBook.Contains(e.key) });
}
}
}
}