using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxLaws { /// One visible line of the act picker: a category header or an entry. public sealed class ActPickerRow { public ActCategoryDef category; /// Null for a category header. public ActEntry entry; /// Header: the category is open. public bool open; /// Header: entries that pass the search (all of them without a search). public int count; /// Entry: the lawbook has a row for this act key (the tick). public bool inBook; public bool IsHeader => entry == null; public override string ToString() => IsHeader ? "[" + category?.defName + " " + count + (open ? " open" : "") + "]" : entry.key + (inBook ? " (in book)" : ""); } /// /// 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. /// public sealed class ActPickerTree { public readonly QuickSearchFilter filter = new QuickSearchFilter(); private readonly HashSet open = new HashSet(); private readonly List rows = new List(); private readonly HashSet inBook = new HashSet(); private string builtText; private int builtCatalogue = -1; private int builtWatch = -1; private int builtLaws = -1; private bool dirty = true; /// The lines to draw, top to bottom. public IReadOnlyList Rows { get { Refresh(); return rows; } } /// Entries the search leaves, over every category. public int Hits { get; private set; } public string SearchText { get => filter.Text; set { if (filter.Text == value) return; filter.Text = value ?? ""; dirty = true; } } /// Whether the category's entries show. While searching, a category with hits is always open. public bool IsOpen(string categoryId) => filter.Active || open.Contains(categoryId); /// Opens or closes a category (no effect while searching, when hits decide). 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; } /// Whether the entry passes the search: its label or its defName (a group's id) contains the text. 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)); } /// The entry rows of a category that pass the search, in picker order. public List EntriesShown(string categoryId) { var list = new List(); 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 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) }); } } } }