This commit is contained in:
2025-07-29 18:08:39 +08:00
parent 6c3ab94d6c
commit 6b5d9166e7
10 changed files with 238 additions and 34 deletions

View File

@@ -0,0 +1,60 @@
using RimWorld;
using Verse;
using System.Collections.Generic;
using Verse.AI;
namespace WulaFallenEmpire
{
public class CompProperties_OpenCustomUI : CompProperties
{
public string uiDefName;
public string label; // The text to display in the float menu
public string failReason; // Optional: Custom text to show if the pawn can't reach the building
public CompProperties_OpenCustomUI()
{
this.compClass = typeof(CompOpenCustomUI);
}
}
public class CompOpenCustomUI : ThingComp
{
public CompProperties_OpenCustomUI Props => (CompProperties_OpenCustomUI)this.props;
public override IEnumerable<FloatMenuOption> CompFloatMenuOptions(Pawn selPawn)
{
// Check if the pawn can interact with the building
if (!selPawn.CanReserveAndReach(this.parent, PathEndMode.InteractionCell, Danger.Deadly))
{
string reason = Props.failReason ?? "CannotUseNoPath".Translate();
yield return new FloatMenuOption(reason, null);
yield break;
}
// Check for power if the building has a power component
CompPowerTrader powerComp = this.parent.GetComp<CompPowerTrader>();
if (powerComp != null && !powerComp.PowerOn)
{
yield return new FloatMenuOption("CannotUseNoPower".Translate(), null);
yield break;
}
string label = Props.label ?? "Open Custom UI"; // Use default label if not provided
FloatMenuOption option = new FloatMenuOption(label, delegate()
{
CustomUIDef uiDef = DefDatabase<CustomUIDef>.GetNamed(Props.uiDefName, false);
if (uiDef != null)
{
Find.WindowStack.Add(new Dialog_CustomDisplay(uiDef));
}
else
{
Log.Error($"[CompOpenCustomUI] Could not find CustomUIDef named '{Props.uiDefName}'.");
}
});
yield return option;
}
}
}

View File

@@ -1,17 +1,51 @@
using System.Collections.Generic;
using UnityEngine;
using Verse;
namespace WulaFallenEmpire
{
public enum DescriptionSelectionMode
{
Random,
Sequential
}
public class CustomUIDef : Def
{
public string portraitPath;
public string characterName;
public new string description;
// New system: list of descriptions
public List<string> descriptions;
public DescriptionSelectionMode descriptionMode = DescriptionSelectionMode.Random;
// Backwards compatibility: old single description field
[System.Obsolete("Use 'descriptions' list instead. This field is for backwards compatibility only.")]
public new string description = null;
public Vector2 windowSize = Vector2.zero;
public List<CustomUIOption> options;
public string backgroundImagePath; // Override default background
public string backgroundImagePath;
public List<Effect> onOpenEffects;
public List<Effect> dismissEffects;
public override void PostLoad()
{
base.PostLoad();
#pragma warning disable 0618
// If the old description field is used, move its value to the new list for processing.
if (!description.NullOrEmpty())
{
if (descriptions.NullOrEmpty())
{
descriptions = new List<string>();
}
descriptions.Insert(0, description);
description = null; // Clear the old field to prevent confusion
}
#pragma warning restore 0618
}
}
public class CustomUIOption
@@ -19,6 +53,6 @@ namespace WulaFallenEmpire
public string label;
public List<Effect> effects;
public List<Condition> conditions;
public string disabledReason; // Custom text to show if conditions aren't met
public string disabledReason;
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Verse;
@@ -10,6 +11,7 @@ namespace WulaFallenEmpire
private CustomUIDef def;
private Texture2D portrait;
private Texture2D background;
private string selectedDescription; // Store the chosen description for this window instance
private static EventUIConfigDef config;
public static EventUIConfigDef Config
@@ -24,14 +26,47 @@ namespace WulaFallenEmpire
}
}
public override Vector2 InitialSize => new Vector2(750f, 500f);
public override Vector2 InitialSize
{
get
{
if (def.windowSize != Vector2.zero)
{
return def.windowSize;
}
return Config.defaultWindowSize; // Fallback to size from config
}
}
public Dialog_CustomDisplay(CustomUIDef def)
{
this.def = def;
this.forcePause = true;
this.absorbInputAroundWindow = true;
this.doCloseX = true; // Add a close button to the window
this.doCloseX = true;
// Select the description text
if (!def.descriptions.NullOrEmpty())
{
if (def.descriptionMode == DescriptionSelectionMode.Random)
{
selectedDescription = def.descriptions.RandomElement();
}
else // Sequential
{
string indexVarName = $"_seq_desc_index_{def.defName}";
int currentIndex = EventContext.GetVariable<int>(indexVarName, 0);
selectedDescription = def.descriptions[currentIndex];
int nextIndex = (currentIndex + 1) % def.descriptions.Count;
EventContext.SetVariable(indexVarName, nextIndex);
}
}
else
{
selectedDescription = "Error: No descriptions found in def.";
}
}
public override void PreOpen()
@@ -127,12 +162,11 @@ namespace WulaFallenEmpire
GUI.color = Color.white;
}
Rect textInnerRect = textRect.ContractedBy(10f * scale);
Widgets.Label(textInnerRect, def.description);
Widgets.Label(textInnerRect, selectedDescription); // Use the selected description
// option (Buttons)
Rect optionRect = new Rect(nameRect.x, textRect.yMax + Config.optionsTextOffset * scale, scaledOptionsWidth, lihuiRect.height - nameRect.height - textRect.height - (Config.textNameOffset + Config.optionsTextOffset) * scale);
// No need to draw a box for the options area, the buttons will be listed inside.
Listing_Standard listing = new Listing_Standard();
listing.Begin(optionRect.ContractedBy(10f * scale));
if (def.options != null)
@@ -151,7 +185,6 @@ namespace WulaFallenEmpire
}
else
{
// Draw a disabled button and add a tooltip
Rect rect = listing.GetRect(30f);
Widgets.ButtonText(rect, option.label, false, true, false);
TooltipHandler.TipRegion(rect, GetDisabledReason(option, reason));

View File

@@ -9,6 +9,7 @@ namespace WulaFallenEmpire
public GameFont labelFont = GameFont.Small;
public bool drawBorders = true;
public string defaultBackgroundImagePath;
public Vector2 defaultWindowSize = new Vector2(750f, 500f);
// Virtual Layout Dimensions
public Vector2 lihuiSize = new Vector2(500f, 800f);