using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace BlaxLaws
{
///
/// The act picker (design: "Lawbook tab", Act picker), a window like the ingredient filter of a tailoring
/// bill: the vanilla QuickSearchWidget on top, below it the catalogue's categories as a collapsible tree
/// (ActPickerTree), groups first, then every entry A to Z with its icon and its source mod in grey. An
/// entry already in the book shows a tick. One click picks an entry and closes the window. Only the lines
/// in view are drawn.
///
public class Dialog_PickAct : Window
{
private const float RowHeight = 26f;
private const float IconSize = 24f;
private const float Indent = 22f;
private const float TitleHeight = 36f;
private const float Gap = 6f;
private readonly Action onPick;
private readonly QuickSearchWidget search = new QuickSearchWidget();
private readonly ActPickerTree tree = new ActPickerTree();
private Vector2 scroll;
private bool focusSearch = true;
/// The tree the window draws, for tests and callers that preset the search.
public ActPickerTree Tree => tree;
/// Called with the picked act key.
public Dialog_PickAct(Action onPick)
{
this.onPick = onPick;
doCloseX = true;
closeOnClickedOutside = true;
absorbInputAroundWindow = true;
forcePause = false;
onlyOneOfTypeAllowed = true;
}
/// "Add law": a pick adds a row (LawbookUi.AddFromPicker), or shows the existing one.
public static Dialog_PickAct ForNewRow() => new Dialog_PickAct(key => LawbookUi.AddFromPicker(key, out _));
/// A click on a row's Action: a pick changes the row's action (LawbookUi.ChangeAction).
public static Dialog_PickAct ForRow(Law law)
{
var d = new Dialog_PickAct(key => LawbookUi.ChangeAction(law, key));
string cat = law?.CategoryId;
if (cat != null)
d.tree.SetOpen(cat, true);
return d;
}
public override Vector2 InitialSize => new Vector2(560f, 680f);
public override void DoWindowContents(Rect inRect)
{
Rect title = new Rect(inRect.x, inRect.y, inRect.width, TitleHeight);
using (new TextBlock(GameFont.Medium))
Widgets.Label(title, "BlaxLaws_PickerTitle".Translate());
Rect searchRect = new Rect(inRect.x, title.yMax, inRect.width, QuickSearchWidget.WidgetHeight);
search.noResultsMatched = tree.filter.Active && tree.Hits == 0;
search.OnGUI(searchRect, () => tree.SearchText = search.filter.Text);
if (focusSearch)
{
search.Focus();
focusSearch = false;
}
if (tree.SearchText != search.filter.Text)
tree.SearchText = search.filter.Text;
Rect outRect = new Rect(inRect.x, searchRect.yMax + Gap, inRect.width, inRect.yMax - searchRect.yMax - Gap);
IReadOnlyList rows = tree.Rows;
if (rows.Count == 0)
{
using (new TextBlock(GameFont.Small, TextAnchor.UpperLeft, ColoredText.SubtleGrayColor))
Widgets.Label(outRect, tree.filter.Active ? "BlaxLaws_PickerNoMatch".Translate() : "BlaxLaws_PickerEmpty".Translate());
return;
}
Rect viewRect = new Rect(0f, 0f, outRect.width - 16f, rows.Count * RowHeight);
Widgets.BeginScrollView(outRect, ref scroll, viewRect);
try
{
int first = Mathf.Max(0, Mathf.FloorToInt(scroll.y / RowHeight));
int last = Mathf.Min(rows.Count - 1, Mathf.CeilToInt((scroll.y + outRect.height) / RowHeight));
for (int i = first; i <= last; i++)
{
Rect r = new Rect(0f, i * RowHeight, viewRect.width, RowHeight);
try
{
if (rows[i].IsHeader)
DrawHeader(r, rows[i]);
else if (DrawEntry(r, rows[i], i))
break;
}
catch (Exception e)
{
LawsLog.Once("Act picker row", e);
}
}
}
finally
{
Widgets.EndScrollView();
}
}
private void DrawHeader(Rect r, ActPickerRow row)
{
Widgets.DrawHighlightIfMouseover(r);
Rect icon = new Rect(r.x, r.y + (r.height - 18f) / 2f, 18f, 18f);
GUI.DrawTexture(icon, row.open ? TexButton.Collapse : TexButton.Reveal);
Rect label = new Rect(icon.xMax + 6f, r.y, r.width - icon.width - 6f, r.height);
using (new TextBlock(GameFont.Small, TextAnchor.MiddleLeft))
Widgets.Label(label, "BlaxLaws_PickerCategory".Translate(row.category.LabelCap, row.count));
string tip = HookGuard.Run("Category tooltip", () => row.category.Worker.Tooltip, null);
if (!tip.NullOrEmpty())
TooltipHandler.TipRegion(r, tip);
if (Widgets.ButtonInvisible(r))
{
tree.Toggle(row.category.defName);
(row.open ? SoundDefOf.TabClose : SoundDefOf.TabOpen).PlayOneShotOnCamera();
}
}
/// True when the click picked the entry and the window closed.
private bool DrawEntry(Rect r, ActPickerRow row, int index)
{
ActEntry e = row.entry;
if (index % 2 == 1)
Widgets.DrawLightHighlight(r);
Widgets.DrawHighlightIfMouseover(r);
float x = r.x + Indent;
if (ActIcons.HasIcon(e))
{
ActIcons.Draw(new Rect(x, r.y + (r.height - IconSize) / 2f, IconSize, IconSize), e);
x += IconSize + 4f;
}
Rect tick = new Rect(r.xMax - 22f, r.y + (r.height - 18f) / 2f, 18f, 18f);
float sourceWidth = Mathf.Min(150f, r.width * 0.3f);
Rect source = new Rect(tick.x - sourceWidth - 4f, r.y, sourceWidth, r.height);
Rect label = new Rect(x, r.y, source.x - x - 4f, r.height);
using (new TextBlock(GameFont.Small, TextAnchor.MiddleLeft))
{
Widgets.Label(label, e.label.Truncate(label.width));
GUI.color = ColoredText.SubtleGrayColor;
Text.Anchor = TextAnchor.MiddleRight;
Widgets.Label(source, (e.sourceMod ?? "").Truncate(source.width));
GUI.color = Color.white;
}
if (row.inBook)
GUI.DrawTexture(tick, LawsTex.InBook);
TooltipHandler.TipRegion(r, () => EntryTooltip(e, row.inBook), e.key.GetHashCode());
if (!Widgets.ButtonInvisible(r))
return false;
SoundDefOf.Click.PlayOneShotOnCamera();
Pick(e.key);
return true;
}
/// The entry's tooltip: label, source, stages, what the act is and whether the book has it.
public static string EntryTooltip(ActEntry e, bool inBook)
{
if (e == null)
return null;
var parts = new List { e.label, "BlaxLaws_SourceMod".Translate(e.sourceMod ?? DlcSupport.CoreName) };
string stages = string.Join(", ", Lawbook.StagesOf(e).Select(LawLabels.Stage));
if (!stages.NullOrEmpty())
parts.Add("BlaxLaws_PickerStages".Translate(stages));
if (e.IsBreak && e.hostileBreak)
parts.Add("BlaxLaws_PickerHostile".Translate());
string what = LawbookUi.EntryDescription(e);
if (!what.NullOrEmpty())
parts.Add(what);
if (inBook)
parts.Add("BlaxLaws_PickerInBook".Translate());
return string.Join("\n\n", parts);
}
/// Returns the key to the caller and closes.
protected void Pick(string actKey)
{
Close();
HookGuard.Run("Picking an act", () => onPick?.Invoke(actKey));
}
}
}