This commit is contained in:
2025-07-27 16:45:42 +08:00
parent 5d1181ef97
commit 502edf03a0
12 changed files with 291 additions and 51 deletions

View File

@@ -0,0 +1,66 @@
using Verse;
using RimWorld;
namespace WulaFallenEmpire
{
public abstract class Condition
{
public abstract bool IsMet(out string reason);
}
public class Condition_VariableEquals : Condition
{
public string name;
public string value;
public override bool IsMet(out string reason)
{
object variable = EventContext.GetVariable<object>(name);
if (variable == null)
{
reason = $"Variable '{name}' not set.";
return false;
}
// Simple string comparison for now. Can be expanded.
bool met = variable.ToString() == value;
if (!met)
{
reason = $"Requires {name} = {value} (Current: {variable})";
}
else
{
reason = "";
}
return met;
}
}
public class Condition_VariableGreaterThan : Condition
{
public string name;
public float value;
public override bool IsMet(out string reason)
{
float variable = EventContext.GetVariable<float>(name, float.MinValue);
if (variable == float.MinValue)
{
reason = $"Variable '{name}' not set.";
return false;
}
bool met = variable > value;
if (!met)
{
reason = $"Requires {name} > {value} (Current: {variable})";
}
else
{
reason = "";
}
return met;
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public class CustomUIDef : Def
{
public string portraitPath;
public string characterName;
public new string description;
public List<CustomUIOption> options;
public string backgroundImagePath; // Override default background
}
public class CustomUIOption
{
public string label;
public List<Effect> effects;
public List<Condition> conditions;
public string disabledReason; // Custom text to show if conditions aren't met
}
}

View File

@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Verse;
namespace WulaFallenEmpire
{
public class Dialog_CustomDisplay : Window
{
private CustomUIDef def;
private Texture2D portrait;
private Texture2D background;
private static EventUIConfigDef config;
public static EventUIConfigDef Config
{
get
{
if (config == null)
{
config = DefDatabase<EventUIConfigDef>.GetNamed("Wula_EventUIConfig");
}
return config;
}
}
public override Vector2 InitialSize => new Vector2(1000f, 750f);
public Dialog_CustomDisplay(CustomUIDef def)
{
this.def = def;
this.forcePause = true;
this.absorbInputAroundWindow = true;
this.doCloseX = true; // Add a close button to the window
}
public override void PreOpen()
{
base.PreOpen();
if (!def.portraitPath.NullOrEmpty())
{
portrait = ContentFinder<Texture2D>.Get(def.portraitPath);
}
string bgPath = !def.backgroundImagePath.NullOrEmpty() ? def.backgroundImagePath : Config.defaultBackgroundImagePath;
if (!bgPath.NullOrEmpty())
{
background = ContentFinder<Texture2D>.Get(bgPath);
}
}
public override void DoWindowContents(Rect inRect)
{
// 1. Draw Background
if (background != null)
{
GUI.DrawTexture(inRect, background, ScaleMode.ScaleToFit);
}
// 2. Draw Top-left defName and Label
Text.Font = GameFont.Tiny;
GUI.color = Color.gray;
Widgets.Label(new Rect(5, 5, inRect.width - 10, 20f), def.defName);
GUI.color = Color.white;
Text.Font = Config.labelFont;
Widgets.Label(new Rect(5, 20f, inRect.width - 10, 30f), def.label);
Text.Font = GameFont.Small; // Reset to default
// 3. Calculate Layout based on ConfigDef
float virtualWidth = Config.lihuiSize.x + Config.textSize.x;
float virtualHeight = Config.lihuiSize.y;
float scaleX = inRect.width / virtualWidth;
float scaleY = inRect.height / virtualHeight;
float scale = Mathf.Min(scaleX, scaleY) * 0.95f;
float scaledLihuiWidth = Config.lihuiSize.x * scale;
float scaledLihuiHeight = Config.lihuiSize.y * scale;
float scaledNameWidth = Config.nameSize.x * scale;
float scaledNameHeight = Config.nameSize.y * scale;
float scaledTextWidth = Config.textSize.x * scale;
float scaledTextHeight = Config.textSize.y * scale;
float scaledOptionsWidth = Config.optionsWidth * scale;
float totalContentWidth = scaledLihuiWidth + scaledTextWidth;
float totalContentHeight = scaledLihuiHeight;
float startX = (inRect.width - totalContentWidth) / 2;
float startY = (inRect.height - totalContentHeight) / 2;
// 4. Draw UI Elements
// lihui (Portrait)
Rect lihuiRect = new Rect(startX, startY, scaledLihuiWidth, scaledLihuiHeight);
if (portrait != null)
{
GUI.DrawTexture(lihuiRect, portrait, ScaleMode.ScaleToFit);
}
if (Config.drawBorders)
{
GUI.color = Color.white;
Widgets.DrawBox(lihuiRect);
GUI.color = Color.white;
}
// name
Rect nameRect = new Rect(lihuiRect.xMax, lihuiRect.y, scaledNameWidth, scaledNameHeight);
if (Config.drawBorders)
{
GUI.color = Color.white;
Widgets.DrawBox(nameRect);
GUI.color = Color.white;
}
Text.Anchor = TextAnchor.MiddleCenter;
Text.Font = GameFont.Medium;
Widgets.Label(nameRect, def.characterName);
Text.Font = GameFont.Small;
Text.Anchor = TextAnchor.UpperLeft;
// text (Description)
Rect textRect = new Rect(nameRect.x, nameRect.yMax + Config.textNameOffset * scale, scaledTextWidth, scaledTextHeight);
if (Config.drawBorders)
{
GUI.color = Color.white;
Widgets.DrawBox(textRect);
GUI.color = Color.white;
}
Rect textInnerRect = textRect.ContractedBy(10f * scale);
Widgets.Label(textInnerRect, def.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)
{
foreach (var option in def.options)
{
string reason;
bool conditionsMet = AreConditionsMet(option.conditions, out reason);
if (conditionsMet)
{
if (listing.ButtonText(option.label))
{
HandleAction(option.effects);
}
}
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));
}
}
}
listing.End();
}
private void HandleAction(List<Effect> effects)
{
if (effects.NullOrEmpty())
{
return;
}
foreach (var effect in effects)
{
effect.Execute(this);
}
}
private bool AreConditionsMet(List<Condition> conditions, out string reason)
{
reason = "";
if (conditions.NullOrEmpty())
{
return true;
}
foreach (var condition in conditions)
{
if (!condition.IsMet(out string singleReason))
{
reason = singleReason;
return false;
}
}
return true;
}
private string GetDisabledReason(CustomUIOption option, string reason)
{
if (!option.disabledReason.NullOrEmpty())
{
return option.disabledReason;
}
return reason;
}
}
}

View File

@@ -0,0 +1,271 @@
using System.Collections.Generic;
using System.Linq;
using Verse;
using RimWorld;
namespace WulaFallenEmpire
{
public abstract class Effect
{
public abstract void Execute(Dialog_CustomDisplay dialog);
}
public class Effect_OpenCustomUI : Effect
{
public string defName;
public override void Execute(Dialog_CustomDisplay dialog)
{
CustomUIDef nextDef = DefDatabase<CustomUIDef>.GetNamed(defName);
if (nextDef != null)
{
Find.WindowStack.Add(new Dialog_CustomDisplay(nextDef));
}
else
{
Log.Error($"[WulaFallenEmpire] Effect_OpenCustomUI could not find CustomUIDef named '{defName}'");
}
}
}
public class Effect_CloseDialog : Effect
{
public override void Execute(Dialog_CustomDisplay dialog)
{
dialog.Close();
}
}
public class Effect_ShowMessage : Effect
{
public string message;
public MessageTypeDef messageTypeDef;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (messageTypeDef == null)
{
messageTypeDef = MessageTypeDefOf.PositiveEvent;
}
Messages.Message(message, messageTypeDef);
}
}
public class Effect_FireIncident : Effect
{
public IncidentDef incident;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (incident == null)
{
Log.Error("[WulaFallenEmpire] Effect_FireIncident has a null incident Def.");
return;
}
IncidentParms parms = new IncidentParms
{
target = Find.CurrentMap,
forced = true
};
if (!incident.Worker.TryExecute(parms))
{
Log.Error($"[WulaFallenEmpire] Could not fire incident {incident.defName}");
}
}
}
public class Effect_ChangeFactionRelation : Effect
{
public FactionDef faction;
public int goodwillChange;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (faction == null)
{
Log.Error("[WulaFallenEmpire] Effect_ChangeFactionRelation has a null faction Def.");
return;
}
Faction targetFaction = Find.FactionManager.FirstFactionOfDef(faction);
if (targetFaction == null)
{
Log.Warning($"[WulaFallenEmpire] Could not find an active faction for FactionDef '{faction.defName}'.");
return;
}
Faction.OfPlayer.TryAffectGoodwillWith(targetFaction, goodwillChange, canSendMessage: true, canSendHostilityLetter: true, reason: null, lookTarget: null);
}
}
public class Effect_SetVariable : Effect
{
public string name;
public string value;
public override void Execute(Dialog_CustomDisplay dialog)
{
// Try to parse as int, then float, otherwise keep as string
if (int.TryParse(value, out int intValue))
{
EventContext.SetVariable(name, intValue);
}
else if (float.TryParse(value, out float floatValue))
{
EventContext.SetVariable(name, floatValue);
}
else
{
EventContext.SetVariable(name, value);
}
}
}
public class Effect_ChangeFactionRelation_FromVariable : Effect
{
public FactionDef faction;
public string goodwillVariableName;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (faction == null)
{
Log.Error("[WulaFallenEmpire] Effect_ChangeFactionRelation_FromVariable has a null faction Def.");
return;
}
Faction targetFaction = Find.FactionManager.FirstFactionOfDef(faction);
if (targetFaction == null)
{
Log.Warning($"[WulaFallenEmpire] Could not find an active faction for FactionDef '{faction.defName}'.");
return;
}
int goodwillChange = EventContext.GetVariable<int>(goodwillVariableName);
Faction.OfPlayer.TryAffectGoodwillWith(targetFaction, goodwillChange, canSendMessage: true, canSendHostilityLetter: true, reason: null, lookTarget: null);
}
}
public class Effect_SpawnPawnAndStore : Effect
{
public PawnKindDef kindDef;
public int count = 1;
public string storeAs;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (kindDef == null)
{
Log.Error("[WulaFallenEmpire] Effect_SpawnPawnAndStore has a null kindDef.");
return;
}
if (storeAs.NullOrEmpty())
{
Log.Error("[WulaFallenEmpire] Effect_SpawnPawnAndStore needs a 'storeAs' variable name.");
return;
}
List<Pawn> spawnedPawns = new List<Pawn>();
for (int i = 0; i < count; i++)
{
Pawn newPawn = PawnGenerator.GeneratePawn(kindDef, Faction.OfPlayer);
IntVec3 loc = CellFinder.RandomSpawnCellForPawnNear(Find.CurrentMap.mapPawns.FreeColonists.First().Position, Find.CurrentMap, 10);
GenSpawn.Spawn(newPawn, loc, Find.CurrentMap);
spawnedPawns.Add(newPawn);
}
if (count == 1)
{
EventContext.SetVariable(storeAs, spawnedPawns.First());
}
else
{
EventContext.SetVariable(storeAs, spawnedPawns);
}
}
}
public class Effect_GiveThing : Effect
{
public ThingDef thingDef;
public int count = 1;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (thingDef == null)
{
Log.Error("[WulaFallenEmpire] Effect_GiveThing has a null thingDef.");
return;
}
Map currentMap = Find.CurrentMap;
if (currentMap == null)
{
Log.Error("[WulaFallenEmpire] Effect_GiveThing cannot execute without a current map.");
return;
}
Thing thing = ThingMaker.MakeThing(thingDef);
thing.stackCount = count;
IntVec3 dropCenter = DropCellFinder.TradeDropSpot(currentMap);
DropPodUtility.DropThingsNear(dropCenter, currentMap, new List<Thing> { thing }, 110, false, false, false, false);
Messages.Message("LetterLabelCargoPodCrash".Translate(), new TargetInfo(dropCenter, currentMap), MessageTypeDefOf.PositiveEvent);
}
}
public class Effect_SpawnPawn : Effect
{
public PawnKindDef kindDef;
public int count = 1;
public bool joinPlayerFaction = true;
public string letterLabel;
public string letterText;
public LetterDef letterDef;
public override void Execute(Dialog_CustomDisplay dialog)
{
if (kindDef == null)
{
Log.Error("[WulaFallenEmpire] Effect_SpawnPawn has a null kindDef.");
return;
}
Map map = Find.CurrentMap;
if (map == null)
{
Log.Error("[WulaFallenEmpire] Effect_SpawnPawn cannot execute without a current map.");
return;
}
for (int i = 0; i < count; i++)
{
Faction faction = joinPlayerFaction ? Faction.OfPlayer : null;
PawnGenerationRequest request = new PawnGenerationRequest(
kindDef, faction, PawnGenerationContext.NonPlayer, -1, true, false, false, false,
true, 20f, false, true, false, true, true, false, false, false, false, 0f, 0f, null, 1f,
null, null, null, null, null, null, null, null, null, null, null, null, false
);
Pawn pawn = PawnGenerator.GeneratePawn(request);
if (!CellFinder.TryFindRandomEdgeCellWith((IntVec3 c) => map.reachability.CanReachColony(c) && !c.Fogged(map), map, CellFinder.EdgeRoadChance_Neutral, out IntVec3 cell))
{
cell = DropCellFinder.RandomDropSpot(map);
}
GenSpawn.Spawn(pawn, cell, map, WipeMode.Vanish);
if (!string.IsNullOrEmpty(letterLabel) && !string.IsNullOrEmpty(letterText))
{
TaggedString finalLabel = letterLabel.Formatted(pawn.Named("PAWN")).AdjustedFor(pawn);
TaggedString finalText = letterText.Formatted(pawn.Named("PAWN")).AdjustedFor(pawn);
PawnRelationUtility.TryAppendRelationsWithColonistsInfo(ref finalText, ref finalLabel, pawn);
Find.LetterStack.ReceiveLetter(finalLabel, finalText, letterDef ?? LetterDefOf.PositiveEvent, pawn);
}
}
}
}
}

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public static class EventContext
{
private static Dictionary<string, object> variables = new Dictionary<string, object>();
public static void SetVariable(string name, object value)
{
if (variables.ContainsKey(name))
{
variables[name] = value;
}
else
{
variables.Add(name, value);
}
Log.Message($"[EventContext] Set variable '{name}' to '{value}'.");
}
public static T GetVariable<T>(string name, T defaultValue = default)
{
if (variables.TryGetValue(name, out object value))
{
if (value is T typedValue)
{
return typedValue;
}
// Try to convert, e.g., from int to float
try
{
return (T)System.Convert.ChangeType(value, typeof(T));
}
catch (System.Exception)
{
Log.Warning($"[EventContext] Variable '{name}' is of type {value.GetType()} but could not be converted to {typeof(T)}.");
return defaultValue;
}
}
Log.Warning($"[EventContext] Variable '{name}' not found. Returning default value.");
return defaultValue;
}
public static void Clear()
{
variables.Clear();
Log.Message("[EventContext] All variables cleared.");
}
}
}

View File

@@ -0,0 +1,23 @@
using UnityEngine;
using Verse;
namespace WulaFallenEmpire
{
public class EventUIConfigDef : Def
{
// General Style
public GameFont labelFont = GameFont.Small;
public bool drawBorders = true;
public string defaultBackgroundImagePath;
// Virtual Layout Dimensions
public Vector2 lihuiSize = new Vector2(500f, 800f);
public Vector2 nameSize = new Vector2(260f, 130f);
public Vector2 textSize = new Vector2(650f, 500f);
public float optionsWidth = 610f;
// Virtual Layout Offsets
public float textNameOffset = 20f;
public float optionsTextOffset = 20f;
}
}