diff --git a/1.6/Assemblies/WulaFallenEmpire.dll b/1.6/Assemblies/WulaFallenEmpire.dll index d62c5b8b..74c54aec 100644 Binary files a/1.6/Assemblies/WulaFallenEmpire.dll and b/1.6/Assemblies/WulaFallenEmpire.dll differ diff --git a/1.6/Defs/CustomUIDefs/CustomUI_Example.xml b/1.6/Defs/CustomUIDefs/CustomUI_Example.xml index cb2b8e41..26cde88f 100644 --- a/1.6/Defs/CustomUIDefs/CustomUI_Example.xml +++ b/1.6/Defs/CustomUIDefs/CustomUI_Example.xml @@ -12,8 +12,12 @@
  • +
  • + wula_event_progress + 1 +
  • - 你选择继续... + 你选择继续... (事件进度变量 'wula_event_progress' 已设为 1) PositiveEvent
  • @@ -40,17 +44,37 @@ 这是事件链的第二部分。你已经从第一个窗口来到了这里。
  • - +
  • - 事件链已完成!一位流浪者加入了你的殖民地。 + 事件链已完成!一位流浪者加入了你的殖民地,帝国对你的行为表示赞赏。
  • WandererJoin
  • +
  • + Empire + 15 +
  • +
  • + + 需要事件进度=1 + +
  • + wula_event_progress + 1 +
  • + + +
  • + 你触发了特殊选项! +
  • +
  • + +
  • diff --git a/Documentation/EventSystem_Documentation.md b/Documentation/EventSystem_Documentation.md new file mode 100644 index 00000000..cbf1a381 --- /dev/null +++ b/Documentation/EventSystem_Documentation.md @@ -0,0 +1,255 @@ +# 自定义UI事件系统文档 + +## 1. 简介 + +本事件系统旨在为RimWorld提供一个强大的、数据驱动的、类似视觉小说的事件和事件链创建框架。它的设计灵感来源于Stellaris等策略游戏,允许开发者在XML中定义复杂的UI窗口、交互选项、事件效果和触发条件。 + +系统的核心由四个部分组成: +- **`CustomUIDef`**: 定义一个独立事件(UI窗口)的所有内容。 +- **`Effect`**: 定义一个选项被点击后执行的具体动作(例如,给予物品、改变关系、打开新窗口等)。 +- **`Condition`**: 定义一个选项是否可选的前提条件(例如,需要某个变量达到特定值)。 +- **`EventContext`**: 一个全局的静态变量存储系统,允许在不同事件和效果之间传递数据。 + +--- + +## 2. 如何创建事件 (`CustomUIDef`) + +每个事件都是一个 `CustomUIDef`。你需要在一个 `Defs` XML文件中定义它。 + +**基本结构:** +```xml + + + MyEvent_UniqueName + + Textures/UI/MyCharacter + 角色名称 + 这里是事件的描述文本。 + + + + + +``` + +**字段说明:** +- `defName`: 事件的唯一ID,用于在代码或其他事件中引用它。 +- `label`: 显示在窗口顶部的标题(当前版本未在UI中显示,但建议填写)。 +- `portraitPath`: 立绘的纹理路径(相对于`Resources`或`Textures`目录)。 +- `characterName`: 显示在名称框中的文本。 +- `description`: 显示在描述框中的主要文本。 +- `options`: 一个 `
  • ` 列表,定义了所有的交互选项。 + +--- + +## 3. 核心概念:选项 (`CustomUIOption`) + +每个选项都在 `` 列表中的一个 `
  • ` 标签内定义。 + +**字段说明:** +- `label`: (必须) 按钮上显示的文本。 +- `effects`: (可选) 一个 `
  • ` 列表,定义了点击此按钮后按顺序执行的所有 `Effect`。 +- `conditions`: (可选) 一个 `
  • ` 列表,定义了此按钮可选所必须满足的所有 `Condition`。只有所有条件都满足,按钮才能被点击。 +- `disabledReason`: (可选) 一个字符串。当按钮因不满足`conditions`而禁用时,鼠标悬停在按钮上会显示此文本。如果未提供,则会自动显示第一个未满足的条件的原因。 + +--- + +## 4. 核心概念:效果 (`Effect`) + +效果定义了“做什么”。每个效果都在 `effects` 列表中的一个 `
  • ` 标签内定义,并且必须有一个 `Class` 属性。 + +### 已实现的 `Effect` 列表 + +#### 4.1 `Effect_OpenCustomUI` +- **功能**: 打开另一个自定义UI事件窗口。 +- **Class**: `WulaFallenEmpire.Effect_OpenCustomUI` +- **字段**: + - `defName`: (必须) 要打开的 `CustomUIDef` 的 `defName`。 +- **示例**: + ```xml +
  • + MyEvent_Step2 +
  • + ``` + +#### 4.2 `Effect_CloseDialog` +- **功能**: 关闭当前的事件窗口。 +- **Class**: `WulaFallenEmpire.Effect_CloseDialog` +- **字段**: 无 +- **示例**: + ```xml +
  • + ``` + +#### 4.3 `Effect_ShowMessage` +- **功能**: 在屏幕左上角显示一条游戏消息。 +- **Class**: `WulaFallenEmpire.Effect_ShowMessage` +- **字段**: + - `message`: (必须) 要显示的文本。 + - `messageTypeDef`: (可选) 消息类型 (例如 `PositiveEvent`, `NegativeEvent`, `NeutralEvent`)。默认为 `PositiveEvent`。 +- **示例**: + ```xml +
  • + 你获得了一个物品! + PositiveEvent +
  • + ``` + +#### 4.4 `Effect_FireIncident` +- **功能**: 触发一个原版或Mod添加的游戏内事件。 +- **Class**: `WulaFallenEmpire.Effect_FireIncident` +- **字段**: + - `incident`: (必须) 要触发的 `IncidentDef` 的 `defName`。 +- **示例**: + ```xml +
  • + RaidEnemy +
  • + ``` + +#### 4.5 `Effect_ChangeFactionRelation` +- **功能**: 改变与指定派系的好感度。 +- **Class**: `WulaFallenEmpire.Effect_ChangeFactionRelation` +- **字段**: + - `faction`: (必须) 目标 `FactionDef` 的 `defName`。 + - `goodwillChange`: (必须) 好感度的改变量,可以是正数或负数。 +- **示例**: + ```xml +
  • + Empire + 15 +
  • + ``` + +#### 4.6 `Effect_SetVariable` +- **功能**: 在 `EventContext` 中设置或修改一个变量的值。 +- **Class**: `WulaFallenEmpire.Effect_SetVariable` +- **字段**: + - `name`: (必须) 变量的名称。 + - `value`: (必须) 变量的值。系统会尝试将其解析为整数或浮点数,如果失败则存为字符串。 +- **示例**: + ```xml +
  • + my_quest_progress + 1 +
  • + ``` + +--- + +## 5. 核心概念:条件 (`Condition`) + +条件定义了选项是否可选的“前提”。每个条件都在 `conditions` 列表中的一个 `
  • ` 标签内定义,并且必须有一个 `Class` 属性。 + +### 已实现的 `Condition` 列表 + +#### 5.1 `Condition_VariableEquals` +- **功能**: 检查一个变量是否等于指定的值。 +- **Class**: `WulaFallenEmpire.Condition_VariableEquals` +- **字段**: + - `name`: (必须) 要检查的变量的名称。 + - `value`: (必须) 要比较的值(作为字符串)。 +- **示例**: + ```xml +
  • + my_quest_progress + 1 +
  • + ``` + +#### 5.2 `Condition_VariableGreaterThan` +- **功能**: 检查一个变量是否大于指定的值。 +- **Class**: `WulaFallenEmpire.Condition_VariableGreaterThan` +- **字段**: + - `name`: (必须) 要检查的变量的名称。 + - `value`: (必须) 要比较的数值。 +- **示例**: + ```xml +
  • + player_reputation + 50 +
  • + ``` + +--- + +## 6. 核心概念:变量系统 (`EventContext`) + +`EventContext` 是一个全局的静态字典,用于在事件链的不同部分之间传递信息。 + +- **设置变量**: 使用 `Effect_SetVariable` 在XML中设置变量。 +- **检查变量**: 使用 `Condition_VariableEquals` 或其他条件类来检查变量的值,从而控制事件流程。 +- **使用变量**: 一些特殊的 `Effect` (例如 `Effect_ChangeFactionRelation_FromVariable`) 可以被设计为从 `EventContext` 中读取值来执行操作。 + +**注意**: 当前 `EventContext` 是全局共享的。在一个事件链结束后,最好能有一个 `Effect` 来清理掉设置的变量,以避免对其他不相关的事件产生影响(此功能待实现)。 + +--- + +## 7. 完整示例 + +以下是一个演示了事件链、变量和条件的完整示例。 + +```xml + + + + + Wula_ExampleUI + + 这是一个事件链的开端。 + +
  • + + + +
  • + wula_event_progress + 1 +
  • + +
  • + Wula_ExampleUI_Next +
  • + +
  • + +
  • +
    +
    + + + + Wula_ExampleUI_Next + + 这是事件链的第二部分。 + +
  • + + +
  • + 事件链已完成! +
  • +
  • + +
  • +
  • + + 需要事件进度=1 + + +
  • + wula_event_progress + 1 +
  • + + +
  • + 你触发了特殊选项! +
  • +
  • + +
  • +
    +
    + +
    diff --git a/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/.suo b/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/.suo index a8a344fa..653b39b7 100644 Binary files a/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/.suo and b/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/.suo differ diff --git a/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/DocumentLayout.json b/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/DocumentLayout.json index 9877f5de..01d50c37 100644 --- a/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/DocumentLayout.json +++ b/Source/WulaFallenEmpire/.vs/WulaFallenEmpire/v17/DocumentLayout.json @@ -7,7 +7,7 @@ "RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:debugactions.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" }, { - "AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|c:\\steam\\steamapps\\common\\rimworld\\mods\\3516260226\\source\\wulafallenempire\\mentalstate_brokenpersonality.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", + "AbsoluteMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|C:\\Steam\\steamapps\\common\\RimWorld\\Mods\\3516260226\\Source\\WulaFallenEmpire\\mentalstate_brokenpersonality.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "RelativeMoniker": "D:0:0:{F5AE8C3B-0221-4C16-A128-9A62D521A8FF}|WulaFallenEmpire.csproj|solutionrelative:mentalstate_brokenpersonality.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" }, { @@ -91,8 +91,7 @@ "RelativeToolTip": "MentalState_BrokenPersonality.cs", "ViewState": "AQIAABMAAAAAAAAAAAAAwEsAAAAjAAAA", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", - "WhenOpened": "2025-07-25T13:51:03.13Z", - "EditorCaption": "" + "WhenOpened": "2025-07-25T13:51:03.13Z" }, { "$type": "Document", diff --git a/Source/WulaFallenEmpire/Condition.cs b/Source/WulaFallenEmpire/Condition.cs new file mode 100644 index 00000000..d898b4cc --- /dev/null +++ b/Source/WulaFallenEmpire/Condition.cs @@ -0,0 +1,64 @@ +using Verse; + +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(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(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; + } + } +} diff --git a/Source/WulaFallenEmpire/CustomUIDef.cs b/Source/WulaFallenEmpire/CustomUIDef.cs index cfe71551..438c69d4 100644 --- a/Source/WulaFallenEmpire/CustomUIDef.cs +++ b/Source/WulaFallenEmpire/CustomUIDef.cs @@ -15,5 +15,7 @@ namespace WulaFallenEmpire { public string label; public List effects; + public List conditions; + public string disabledReason; // Custom text to show if conditions aren't met } } diff --git a/Source/WulaFallenEmpire/Dialog_CustomDisplay.cs b/Source/WulaFallenEmpire/Dialog_CustomDisplay.cs index d1487557..21207fdc 100644 --- a/Source/WulaFallenEmpire/Dialog_CustomDisplay.cs +++ b/Source/WulaFallenEmpire/Dialog_CustomDisplay.cs @@ -31,27 +31,42 @@ namespace WulaFallenEmpire public override void DoWindowContents(Rect inRect) { - // Top-left defName + // Top-left defName and Label Text.Font = GameFont.Tiny; GUI.color = Color.gray; - Widgets.Label(new Rect(0, 0, inRect.width, 30f), def.defName); + Widgets.Label(new Rect(5, 5, inRect.width - 10, 20f), def.defName); GUI.color = Color.white; + Text.Font = GameFont.Small; + Widgets.Label(new Rect(5, 20f, inRect.width - 10, 30f), def.label); - // Scaling factor to fit the new window size while maintaining layout proportions. - float scale = 0.65f; - // The original CSS was based on a large canvas. We create a virtual canvas inside our window. - // Center the main content block. - float contentWidth = 1200f * scale; - float contentHeight = 1100f * scale; - Rect contentRect = new Rect((inRect.width - contentWidth) / 2, (inRect.height - contentHeight) / 2, contentWidth, contentHeight); + // Define virtual total size from the CSS layout + float virtualWidth = 500f + 650f; // lihui + text + float virtualHeight = 800f; // lihui height - // All original positions are now relative to this contentRect and scaled. - Rect mainBodySRect = new Rect(contentRect.x + 200f * scale, contentRect.y + 400f * scale, 1050f * scale, 1000f * scale); + // 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 + + // 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; + + // Center the whole content block + float totalContentWidth = scaledLihuiWidth + scaledTextWidth; + float totalContentHeight = scaledLihuiHeight; + float startX = (inRect.width - totalContentWidth) / 2; + float startY = (inRect.height - totalContentHeight) / 2; // lihui (Portrait) - Rect lihuiRect = new Rect(mainBodySRect.x - 150f * scale, mainBodySRect.y - 200f * scale, 500f * scale, 800f * scale); + Rect lihuiRect = new Rect(startX, startY, scaledLihuiWidth, scaledLihuiHeight); if (portrait != null) { GUI.DrawTexture(lihuiRect, portrait, ScaleMode.ScaleToFit); @@ -62,7 +77,7 @@ namespace WulaFallenEmpire // name - Rect nameRect = new Rect(lihuiRect.xMax, mainBodySRect.y - 30f * scale, 260f * scale, 130f * scale); + Rect nameRect = new Rect(lihuiRect.xMax, lihuiRect.y, scaledNameWidth, scaledNameHeight); GUI.color = Color.white; Widgets.DrawBox(nameRect); GUI.color = Color.white; // Reset color @@ -73,7 +88,7 @@ namespace WulaFallenEmpire Text.Anchor = TextAnchor.UpperLeft; // text (Description) - Rect textRect = new Rect(nameRect.x, nameRect.yMax + 50f * scale, 650f * scale, 250f * scale); + 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 @@ -81,7 +96,7 @@ namespace WulaFallenEmpire Widgets.Label(textInnerRect, def.description); // option (Buttons) - Rect optionRect = new Rect(nameRect.x, textRect.yMax, 610f * scale, 300f * scale); + Rect optionRect = new Rect(nameRect.x, textRect.yMax + 20f * scale, scaledOptionsWidth, lihuiRect.height - nameRect.height - textRect.height - 40f * scale); // No need to draw a box for the options area, the buttons will be listed inside. Listing_Standard listing = new Listing_Standard(); @@ -90,9 +105,22 @@ namespace WulaFallenEmpire { foreach (var option in def.options) { - if (listing.ButtonText(option.label)) + string reason; + bool conditionsMet = AreConditionsMet(option.conditions, out reason); + + if (conditionsMet) { - HandleAction(option.effects); + 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)); } } } @@ -111,5 +139,33 @@ namespace WulaFallenEmpire effect.Execute(this); } } + + private bool AreConditionsMet(List 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; + } } } diff --git a/Source/WulaFallenEmpire/Effect.cs b/Source/WulaFallenEmpire/Effect.cs index a1fe47e3..a078daee 100644 --- a/Source/WulaFallenEmpire/Effect.cs +++ b/Source/WulaFallenEmpire/Effect.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using Verse; using RimWorld; @@ -87,4 +89,101 @@ namespace WulaFallenEmpire return; } - Faction.OfPlayer.TryAffectGoodwillWith(faction, goodwillChange, canSendMessage: true, canSendHostilityLetter: true, reason: HistoryEventDefOf.QuestGoodwill, lookTarget: null); + 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(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 spawnedPawns = new List(); + 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); + } + } + } +} diff --git a/Source/WulaFallenEmpire/EventContext.cs b/Source/WulaFallenEmpire/EventContext.cs new file mode 100644 index 00000000..ec1c72bf --- /dev/null +++ b/Source/WulaFallenEmpire/EventContext.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using Verse; + +namespace WulaFallenEmpire +{ + public static class EventContext + { + private static Dictionary variables = new Dictionary(); + + 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(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."); + } + } +} diff --git a/Source/WulaFallenEmpire/WulaFallenEmpire.csproj b/Source/WulaFallenEmpire/WulaFallenEmpire.csproj index b0cbd491..9bb016c8 100644 --- a/Source/WulaFallenEmpire/WulaFallenEmpire.csproj +++ b/Source/WulaFallenEmpire/WulaFallenEmpire.csproj @@ -106,6 +106,8 @@ + +