rimworld-blax-zones/Source/BlaxZones/ITab_Delivery.cs

187 lines
8.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxZones
{
/// <summary>
/// Package Tab. The delivery zone tab: priority dropdown (Low to Critical), source checkboxes
/// (orbital in 1.0), the
/// "Caravans camp here" checkbox and ThingFilterUI.DoThingFilterConfigWindow under
/// DeliveryFilters.Parent.
///
/// Layout follows ITab_Storage (ITab_Storage.cs:104-151): the same 300x480 window contracted by 10,
/// the priority button top left, the filter below. Two checkbox rows sit between them.
/// </summary>
public class ITab_Delivery : ITab
{
private static readonly Vector2 WinSize = new Vector2(300f, 480f);
private const float PriorityButtonWidth = 160f;
private const float PriorityButtonHeight = 29f;
private const float RowHeight = 24f;
private const float Gap = 6f;
/// <summary>The filter tree's scroll position and search box. UI state of this shared tab, not game state.</summary>
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. 1.0 has orbital only; 1.1 adds rewards and caravan rows here.
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 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();
}
}
/// <summary>"Priority: Normal", the stockpile tab's button text (ITab_Storage.cs:114).</summary>
public static string PriorityButtonLabel(StoragePriority p) =>
"Priority".Translate() + ": " + p.Label().CapitalizeFirst();
/// <summary>The dropdown's entries: every StoragePriority from Low to Critical, never Unstored, each setting the zone's priority.</summary>
public static List<FloatMenuOption> PriorityMenuOptions(Zone_Delivery zone)
{
var list = new List<FloatMenuOption>();
foreach (StoragePriority p in Priorities())
{
StoragePriority local = p;
list.Add(new FloatMenuOption(local.Label().CapitalizeFirst(), () =>
{
if (zone != null)
zone.priority = local;
}));
}
return list;
}
/// <summary>Low, Normal, Preferred, Important, Critical: the enum without Unstored, in enum order.</summary>
public static IEnumerable<StoragePriority> Priorities()
{
foreach (StoragePriority p in Enum.GetValues(typeof(StoragePriority)))
{
if (p != StoragePriority.Unstored)
yield return p;
}
}
/// <summary>
/// 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.
/// </summary>
public static string OrbitalTip()
{
string tip = "BlaxZones_AcceptOrbitalTip".Translate();
if (!BlaxZonesMod.S.routeOrbital)
tip += "\n\n" + "BlaxZones_AcceptOrbitalOffTip".Translate();
return tip;
}
/// <summary>The camp checkbox's tooltip. Adds a note while camping is off globally or stands down for a trading-spot mod.</summary>
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<ThingDef> hiddenDefs;
/// <summary>
/// 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.
/// </summary>
public static List<ThingDef> HiddenDefs()
{
if (hiddenDefs == null)
{
var list = new List<ThingDef>();
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;
}
/// <summary>The meat-source filters the stockpile tab hides with Ideology (ITab_Storage.cs:178-187), null-checked.</summary>
private static IEnumerable<SpecialThingFilterDef> 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;
}
}
}