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

Binary file not shown.

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<WulaFallenEmpire.EventUIConfigDef>
<defName>Wula_EventUIConfig</defName>
<!-- General Style -->
<labelFont>Small</labelFont>
<drawBorders>true</drawBorders>
<defaultBackgroundImagePath></defaultBackgroundImagePath>
<!-- Virtual Layout Dimensions -->
<lihuiSize>(500, 800)</lihuiSize>
<nameSize>(260, 130)</nameSize>
<textSize>(650, 500)</textSize>
<optionsWidth>610</optionsWidth>
<!-- Virtual Layout Offsets -->
<textNameOffset>20</textNameOffset>
<optionsTextOffset>20</optionsTextOffset>
</WulaFallenEmpire.EventUIConfigDef>
</Defs>

View File

@@ -4,7 +4,7 @@
<!-- Event 1 -->
<WulaFallenEmpire.CustomUIDef>
<defName>Wula_ExampleUI</defName>
<label>事件链示例 - 1</label>
<label>INCOMING TRANSMISSON事件链示例 - 1</label>
<portraitPath>UI/HeroArt/Storytellers/Randy</portraitPath>
<characterName>兰迪·随机</characterName>
<description>这是一个事件链的开端。选择第一个选项将会打开第二个事件窗口。</description>

View File

@@ -9,10 +9,49 @@
- **`Effect`**: 定义一个选项被点击后执行的具体动作(例如,给予物品、改变关系、打开新窗口等)。
- **`Condition`**: 定义一个选项是否可选的前提条件(例如,需要某个变量达到特定值)。
- **`EventContext`**: 一个全局的静态变量存储系统,允许在不同事件和效果之间传递数据。
- **`EventUIConfigDef`**: 一个全局的外观和布局配置文件,用于统一管理所有事件窗口的视觉风格。
---
## 2. 如何创建事件 (`CustomUIDef`)
## 2. 全局UI配置 (`EventUIConfigDef`)
为了方便统一修改所有事件窗口的外观和布局,系统使用一个单例的 `EventUIConfigDef`。你应该在 `Defs` 文件夹下创建一个XML文件来定义它。
**文件示例 (`1.6/Defs/ConfigDefs/EventUIConfig.xml`):**
```xml
<Defs>
<WulaFallenEmpire.EventUIConfigDef>
<defName>Wula_EventUIConfig</defName>
<!-- 通用风格 -->
<labelFont>Small</labelFont>
<drawBorders>true</drawBorders>
<defaultBackgroundImagePath>UI/Backgrounds/DefaultBG</defaultBackgroundImagePath>
<!-- 虚拟布局尺寸 -->
<lihuiSize>(500, 800)</lihuiSize>
<nameSize>(260, 130)</nameSize>
<textSize>(650, 500)</textSize>
<optionsWidth>610</optionsWidth>
<!-- 虚拟布局间距 -->
<textNameOffset>20</textNameOffset>
<optionsTextOffset>20</optionsTextOffset>
</WulaFallenEmpire.EventUIConfigDef>
</Defs>
```
**字段说明:**
- `labelFont`: 事件标题 (`label`) 的字体大小。可选值: `Tiny`, `Small`, `Medium`, `Large`
- `drawBorders`: 是否为立绘、名称和描述区域绘制白色边框。
- `defaultBackgroundImagePath`: 所有事件窗口默认使用的背景图路径。
- `lihuiSize`, `nameSize`, `textSize`, `optionsWidth`: 定义了UI各部分的基础虚拟尺寸代码会根据窗口大小按比例缩放它们。
- `textNameOffset`, `optionsTextOffset`: 定义了各部分之间的垂直间距。
---
## 3. 如何创建事件 (`CustomUIDef`)
每个事件都是一个 `CustomUIDef`。你需要在一个 `Defs` XML文件中定义它。
@@ -34,15 +73,16 @@
**字段说明:**
- `defName`: 事件的唯一ID用于在代码或其他事件中引用它。
- `label`: 显示在窗口顶部的标题当前版本未在UI中显示但建议填写
- `label`: 显示在窗口左上角的标题
- `portraitPath`: 立绘的纹理路径(相对于`Resources``Textures`目录)。
- `characterName`: 显示在名称框中的文本。
- `backgroundImagePath`: (可选)为此特定事件指定的背景图路径,它会覆盖 `EventUIConfigDef` 中的默认背景。
- `description`: 显示在描述框中的主要文本。
- `options`: 一个 `<li>` 列表,定义了所有的交互选项。
---
## 3. 核心概念:选项 (`CustomUIOption`)
## 4. 核心概念:选项 (`CustomUIOption`)
每个选项都在 `<options>` 列表中的一个 `<li>` 标签内定义。
@@ -54,13 +94,13 @@
---
## 4. 核心概念:效果 (`Effect`)
## 5. 核心概念:效果 (`Effect`)
效果定义了“做什么”。每个效果都在 `effects` 列表中的一个 `<li>` 标签内定义,并且必须有一个 `Class` 属性。
### 已实现的 `Effect` 列表
#### 4.1 `Effect_OpenCustomUI`
#### 5.1 `Effect_OpenCustomUI`
- **功能**: 打开另一个自定义UI事件窗口。
- **Class**: `WulaFallenEmpire.Effect_OpenCustomUI`
- **字段**:
@@ -72,7 +112,7 @@
</li>
```
#### 4.2 `Effect_CloseDialog`
#### 5.2 `Effect_CloseDialog`
- **功能**: 关闭当前的事件窗口。
- **Class**: `WulaFallenEmpire.Effect_CloseDialog`
- **字段**: 无
@@ -81,7 +121,7 @@
<li Class="WulaFallenEmpire.Effect_CloseDialog" />
```
#### 4.3 `Effect_ShowMessage`
#### 5.3 `Effect_ShowMessage`
- **功能**: 在屏幕左上角显示一条游戏消息。
- **Class**: `WulaFallenEmpire.Effect_ShowMessage`
- **字段**:
@@ -95,7 +135,7 @@
</li>
```
#### 4.4 `Effect_FireIncident`
#### 5.4 `Effect_FireIncident`
- **功能**: 触发一个原版或Mod添加的游戏内事件。
- **Class**: `WulaFallenEmpire.Effect_FireIncident`
- **字段**:
@@ -107,7 +147,7 @@
</li>
```
#### 4.5 `Effect_ChangeFactionRelation`
#### 5.5 `Effect_ChangeFactionRelation`
- **功能**: 改变与指定派系的好感度。
- **Class**: `WulaFallenEmpire.Effect_ChangeFactionRelation`
- **字段**:
@@ -121,7 +161,7 @@
</li>
```
#### 4.6 `Effect_SetVariable`
#### 5.6 `Effect_SetVariable`
- **功能**: 在 `EventContext` 中设置或修改一个变量的值。
- **Class**: `WulaFallenEmpire.Effect_SetVariable`
- **字段**:
@@ -135,15 +175,50 @@
</li>
```
#### 5.7 `Effect_GiveThing`
- **功能**: 给予玩家一个或多个物品。
- **Class**: `WulaFallenEmpire.Effect_GiveThing`
- **字段**:
- `thingDef`: (必须) 要给予物品的 `ThingDef` 的 `defName`。
- `count`: (可选) 给予的数量,默认为 1。
- **示例**:
```xml
<li Class="WulaFallenEmpire.Effect_GiveThing">
<thingDef>Silver</thingDef>
<count>100</count>
</li>
```
#### 5.8 `Effect_SpawnPawn`
- **功能**: 在地图上生成一个或多个Pawn并可选地发送一封信件通知玩家。
- **Class**: `WulaFallenEmpire.Effect_SpawnPawn`
- **字段**:
- `kindDef`: (必须) 要生成Pawn的 `PawnKindDef` 的 `defName`。
- `count`: (可选) 生成的数量,默认为 1。
- `joinPlayerFaction`: (可选) Pawn是否加入玩家派系默认为 `true`。
- `letterLabel`: (可选) 通知信件的标题。
- `letterText`: (可选) 通知信件的内容。
- `letterDef`: (可选) 通知信件的类型 (例如 `PositiveEvent`, `NegativeEvent`)。默认为 `PositiveEvent`。
- **示例**:
```xml
<li Class="WulaFallenEmpire.Effect_SpawnPawn">
<kindDef>Colonist</kindDef>
<count>1</count>
<joinPlayerFaction>true</joinPlayerFaction>
<letterLabel>A New Colonist</letterLabel>
<letterText>{PAWN_nameDef} has decided to join your colony.</letterText>
</li>
```
---
## 5. 核心概念:条件 (`Condition`)
## 6. 核心概念:条件 (`Condition`)
条件定义了选项是否可选的“前提”。每个条件都在 `conditions` 列表中的一个 `<li>` 标签内定义,并且必须有一个 `Class` 属性。
### 已实现的 `Condition` 列表
#### 5.1 `Condition_VariableEquals`
#### 6.1 `Condition_VariableEquals`
- **功能**: 检查一个变量是否等于指定的值。
- **Class**: `WulaFallenEmpire.Condition_VariableEquals`
- **字段**:
@@ -157,7 +232,7 @@
</li>
```
#### 5.2 `Condition_VariableGreaterThan`
#### 6.2 `Condition_VariableGreaterThan`
- **功能**: 检查一个变量是否大于指定的值。
- **Class**: `WulaFallenEmpire.Condition_VariableGreaterThan`
- **字段**:
@@ -173,7 +248,7 @@
---
## 6. 核心概念:变量系统 (`EventContext`)
## 7. 核心概念:变量系统 (`EventContext`)
`EventContext` 是一个全局的静态字典,用于在事件链的不同部分之间传递信息。
@@ -185,7 +260,7 @@
---
## 7. 完整示例
## 8. 完整示例
以下是一个演示了事件链、变量和条件的完整示例。

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" />