using System;
using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxZones
{
///
/// Package Tab. The delivery zone tab: priority dropdown (Low to Critical), source checkboxes
/// (orbital trade, quest rewards and caravan purchases), the "Caravans camp here" checkbox and
/// ThingFilterUI.DoThingFilterConfigWindow under DeliveryFilters.Parent.
///
/// Layout follows ITab_Storage (ITab_Storage.cs:104-151): the same window width contracted by 10, the
/// priority button top left, the filter below. Four checkbox rows sit between them, so the window is
/// two rows taller than the stockpile tab's 480.
///
public class ITab_Delivery : ITab
{
private static readonly Vector2 WinSize = new Vector2(300f, 528f);
private const float PriorityButtonWidth = 160f;
private const float PriorityButtonHeight = 29f;
private const float RowHeight = 24f;
private const float Gap = 6f;
/// The filter tree's scroll position and search box. UI state of this shared tab, not game state.
private readonly ThingFilterUI.UIState thingFilterState = new ThingFilterUI.UIState();
public Zone_Delivery SelZone => SelObject as Zone_Delivery;
public ITab_Delivery()
{
size = WinSize;
labelKey = "BlaxZones_TabDelivery";
}
public override bool IsVisible => SelZone != null;
public override void OnOpen()
{
base.OnOpen();
thingFilterState.quickSearch.Reset();
}
public override void Notify_ClickOutsideWindow()
{
base.Notify_ClickOutsideWindow();
thingFilterState.quickSearch.Unfocus();
}
protected override void FillTab()
{
Zone_Delivery zone = SelZone;
if (zone == null)
return;
zone.filter ??= DeliveryFilters.NewDefaultFilter();
Rect rect = new Rect(0f, 0f, WinSize.x, WinSize.y).ContractedBy(10f);
Widgets.BeginGroup(rect);
try
{
Text.Font = GameFont.Small;
float y = 0f;
// Priority: a button that opens a float menu, as the stockpile tab does.
Rect priorityRect = new Rect(0f, y, PriorityButtonWidth, PriorityButtonHeight);
if (Widgets.ButtonText(priorityRect, PriorityButtonLabel(zone.priority)))
Find.WindowStack.Add(new FloatMenu(PriorityMenuOptions(zone)));
TooltipHandler.TipRegion(priorityRect, "BlaxZones_PriorityTip".Translate());
y += PriorityButtonHeight + Gap;
// Sources: orbital trade, quest rewards, caravan purchases.
Rect orbitalRect = new Rect(0f, y, rect.width, RowHeight);
Widgets.CheckboxLabeled(orbitalRect, "BlaxZones_AcceptOrbital".Translate(), ref zone.acceptOrbital);
TooltipHandler.TipRegion(orbitalRect, OrbitalTip());
y += RowHeight;
Rect rewardsRect = new Rect(0f, y, rect.width, RowHeight);
Widgets.CheckboxLabeled(rewardsRect, "BlaxZones_AcceptRewards".Translate(), ref zone.acceptRewards);
TooltipHandler.TipRegion(rewardsRect, RewardsTip());
y += RowHeight;
Rect caravanRect = new Rect(0f, y, rect.width, RowHeight);
Widgets.CheckboxLabeled(caravanRect, "BlaxZones_AcceptCaravan".Translate(), ref zone.acceptCaravan);
TooltipHandler.TipRegion(caravanRect, CaravanTip());
y += RowHeight;
Rect campRect = new Rect(0f, y, rect.width, RowHeight);
Widgets.CheckboxLabeled(campRect, "BlaxZones_CampHere".Translate(), ref zone.campHere);
TooltipHandler.TipRegion(campRect, CampTip());
y += RowHeight + Gap;
Rect filterRect = new Rect(0f, y, rect.width, rect.height - y);
ThingFilterUI.DoThingFilterConfigWindow(filterRect, thingFilterState, zone.filter, DeliveryFilters.Parent, 8,
HiddenDefs(), HiddenSpecialFilters());
}
finally
{
Widgets.EndGroup();
}
}
/// "Priority: Normal", the stockpile tab's button text (ITab_Storage.cs:114).
public static string PriorityButtonLabel(StoragePriority p) =>
"Priority".Translate() + ": " + p.Label().CapitalizeFirst();
/// The dropdown's entries: every StoragePriority from Low to Critical, never Unstored, each setting the zone's priority.
public static List PriorityMenuOptions(Zone_Delivery zone)
{
var list = new List();
foreach (StoragePriority p in Priorities())
{
StoragePriority local = p;
list.Add(new FloatMenuOption(local.Label().CapitalizeFirst(), () =>
{
if (zone != null)
zone.priority = local;
}));
}
return list;
}
/// Low, Normal, Preferred, Important, Critical: the enum without Unstored, in enum order.
public static IEnumerable Priorities()
{
foreach (StoragePriority p in Enum.GetValues(typeof(StoragePriority)))
{
if (p != StoragePriority.Unstored)
yield return p;
}
}
///
/// The orbital checkbox's tooltip. Adds the Blax Slaves line while Slaves is active, and a note while
/// routing is off globally. "Route orbital trade" gates only DeliverFromOrbit; ZonesApi ignores it,
/// so with Slaves active the note says its payouts still land here.
///
public static string OrbitalTip()
{
string tip = "BlaxZones_AcceptOrbitalTip".Translate();
if (!BlaxZonesMod.S.routeOrbital)
tip += "\n\n" + "BlaxZones_AcceptOrbitalOffTip".Translate();
return tip;
}
/// The quest rewards checkbox's tooltip. Adds a note while "Route quest rewards" is off.
public static string RewardsTip()
{
string tip = "BlaxZones_AcceptRewardsTip".Translate();
if (!BlaxZonesMod.S.routeRewards)
tip += "\n\n" + "BlaxZones_AcceptRewardsOffTip".Translate();
return tip;
}
/// The caravan checkbox's tooltip ("Trader caravans and guests"). Adds a note while "Route caravan purchases" is off.
public static string CaravanTip()
{
string tip = "BlaxZones_AcceptCaravanTip".Translate();
if (!BlaxZonesMod.S.routeCaravan)
tip += "\n\n" + "BlaxZones_AcceptCaravanOffTip".Translate();
return tip;
}
/// The camp checkbox's tooltip. Adds a note while camping is off globally or stands down for a trading-spot mod.
public static string CampTip()
{
string tip = "BlaxZones_CampHereTip".Translate();
if (!BlaxZonesMod.S.campAtZones)
tip += "\n\n" + "BlaxZones_CampHereOffTip".Translate();
string spotMod = Compat.ActiveTradingSpotMod();
if (spotMod != null)
{
string name = ModLister.GetActiveModWithIdentifier(spotMod, ignorePostfix: true)?.Name ?? spotMod;
tip += "\n\n" + "BlaxZones_CampHereStandDownTip".Translate(name);
}
return tip;
}
private static List hiddenDefs;
///
/// Wastepack (Biotech, null-checked) and every corpse def. The parent filter already keeps them
/// out of the tree; passing them here also keeps "Clear all" and category checkboxes from
/// touching them (ThingFilter.SetAllow(ThingCategoryDef, ...) excepts these, ThingFilter.cs:718-730).
/// Built from defs once; no game state.
///
public static List HiddenDefs()
{
if (hiddenDefs == null)
{
var list = new List();
ThingDef wastepack = ThingDefOf.Wastepack; // Biotech only; null without it
if (wastepack != null)
list.Add(wastepack);
if (ThingCategoryDefOf.Corpses != null)
list.AddRange(ThingCategoryDefOf.Corpses.DescendantThingDefs);
hiddenDefs = list;
}
return hiddenDefs;
}
/// The meat-source filters the stockpile tab hides with Ideology (ITab_Storage.cs:178-187), null-checked.
private static IEnumerable HiddenSpecialFilters()
{
if (!ModsConfig.IdeologyActive)
yield break;
if (SpecialThingFilterDefOf.AllowVegetarian != null) yield return SpecialThingFilterDefOf.AllowVegetarian;
if (SpecialThingFilterDefOf.AllowCarnivore != null) yield return SpecialThingFilterDefOf.AllowCarnivore;
if (SpecialThingFilterDefOf.AllowCannibal != null) yield return SpecialThingFilterDefOf.AllowCannibal;
if (SpecialThingFilterDefOf.AllowInsectMeat != null) yield return SpecialThingFilterDefOf.AllowInsectMeat;
}
}
}