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

@@ -1,4 +1,5 @@
using Verse;
using RimWorld;
namespace WulaFallenEmpire
{
@@ -61,4 +62,5 @@ namespace WulaFallenEmpire
return met;
}
}
}

View File

@@ -9,6 +9,7 @@ namespace WulaFallenEmpire
public string characterName;
public new string description;
public List<CustomUIOption> options;
public string backgroundImagePath; // Override default background
}
public class CustomUIOption

View File

@@ -9,6 +9,20 @@ namespace WulaFallenEmpire
{
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);
@@ -25,62 +39,77 @@ namespace WulaFallenEmpire
base.PreOpen();
if (!def.portraitPath.NullOrEmpty())
{
this.portrait = ContentFinder<Texture2D>.Get(def.portraitPath);
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)
{
// Top-left defName and Label
// 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 = GameFont.Small;
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;
// Define virtual total size from the CSS layout
float virtualWidth = 500f + 650f; // lihui + text
float virtualHeight = 800f; // lihui height
// Calculate scale to fit the window, maintaining aspect ratio
float scaleX = inRect.width / virtualWidth;
float scaleY = inRect.height / virtualHeight;
float scale = Mathf.Min(scaleX, scaleY) * 0.95f; // Use 95% of space to leave some margin
float scale = Mathf.Min(scaleX, scaleY) * 0.95f;
// Calculate scaled dimensions
float scaledLihuiWidth = 500f * scale;
float scaledLihuiHeight = 800f * scale;
float scaledNameWidth = 260f * scale;
float scaledNameHeight = 130f * scale;
float scaledTextWidth = 650f * scale;
float scaledTextHeight = 250f * scale;
float scaledOptionsWidth = 610f * scale;
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;
// Center the whole content block
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);
}
GUI.color = Color.white;
Widgets.DrawBox(lihuiRect);
GUI.color = Color.white; // Reset color
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);
GUI.color = Color.white;
Widgets.DrawBox(nameRect);
GUI.color = Color.white; // Reset color
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);
@@ -88,15 +117,18 @@ namespace WulaFallenEmpire
Text.Anchor = TextAnchor.UpperLeft;
// text (Description)
Rect textRect = new Rect(nameRect.x, nameRect.yMax + 20f * scale, scaledTextWidth, scaledTextHeight);
GUI.color = Color.white;
Widgets.DrawBox(textRect);
GUI.color = Color.white; // Reset color
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 + 20f * scale, scaledOptionsWidth, lihuiRect.height - nameRect.height - textRect.height - 40f * scale);
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();

View File

@@ -186,4 +186,86 @@ namespace WulaFallenEmpire
}
}
}
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,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;
}
}

View File

@@ -102,12 +102,13 @@
<Compile Include="MentalState_BrokenPersonality.cs" />
<Compile Include="MentalStateDefExtension_BrokenPersonality.cs" />
<Compile Include="MentalBreakWorker_BrokenPersonality.cs" />
<Compile Include="Dialog_CustomDisplay.cs" />
<Compile Include="CustomUIDef.cs" />
<Compile Include="Effect.cs" />
<Compile Include="DebugActions.cs" />
<Compile Include="EventContext.cs" />
<Compile Include="Condition.cs" />
<Compile Include="EventSystem\Condition.cs" />
<Compile Include="EventSystem\CustomUIDef.cs" />
<Compile Include="EventSystem\Dialog_CustomDisplay.cs" />
<Compile Include="EventSystem\Effect.cs" />
<Compile Include="EventSystem\EventContext.cs" />
<Compile Include="EventSystem\EventUIConfigDef.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />