整理scoure
This commit is contained in:
274
Source/WulaFallenEmpire/EventSystem/Dialog_CustomDisplay.cs
Normal file
274
Source/WulaFallenEmpire/EventSystem/Dialog_CustomDisplay.cs
Normal file
@@ -0,0 +1,274 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEngine;
|
||||
using Verse;
|
||||
|
||||
namespace WulaFallenEmpire
|
||||
{
|
||||
public class Dialog_CustomDisplay : Window
|
||||
{
|
||||
private EventDef def;
|
||||
private Texture2D portrait;
|
||||
private Texture2D background;
|
||||
private string selectedDescription;
|
||||
|
||||
private static EventUIConfigDef config;
|
||||
public static EventUIConfigDef Config
|
||||
{
|
||||
get
|
||||
{
|
||||
if (config == null)
|
||||
{
|
||||
config = DefDatabase<EventUIConfigDef>.GetNamed("Wula_EventUIConfig");
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
public override Vector2 InitialSize
|
||||
{
|
||||
get
|
||||
{
|
||||
if (def.windowSize != Vector2.zero)
|
||||
{
|
||||
return def.windowSize;
|
||||
}
|
||||
return Config.defaultWindowSize;
|
||||
}
|
||||
}
|
||||
|
||||
public Dialog_CustomDisplay(EventDef def)
|
||||
{
|
||||
this.def = def;
|
||||
this.forcePause = true;
|
||||
this.absorbInputAroundWindow = true;
|
||||
this.doCloseX = true;
|
||||
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
if (!def.descriptions.NullOrEmpty())
|
||||
{
|
||||
if (def.descriptionMode == DescriptionSelectionMode.Random)
|
||||
{
|
||||
selectedDescription = def.descriptions.RandomElement();
|
||||
}
|
||||
else
|
||||
{
|
||||
string indexVarName = $"_seq_desc_index_{def.defName}";
|
||||
int currentIndex = eventVarManager.GetVariable<int>(indexVarName, 0);
|
||||
|
||||
selectedDescription = def.descriptions[currentIndex];
|
||||
|
||||
int nextIndex = (currentIndex + 1) % def.descriptions.Count;
|
||||
eventVarManager.SetVariable(indexVarName, nextIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
selectedDescription = "Error: No descriptions found in def.";
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
HandleAction(def.immediateEffects);
|
||||
|
||||
if (!def.conditionalDescriptions.NullOrEmpty())
|
||||
{
|
||||
foreach (var condDesc in def.conditionalDescriptions)
|
||||
{
|
||||
string reason;
|
||||
if (AreConditionsMet(condDesc.conditions, out reason))
|
||||
{
|
||||
selectedDescription += "\n\n" + condDesc.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selectedDescription = FormatDescription(selectedDescription);
|
||||
}
|
||||
|
||||
public override void DoWindowContents(Rect inRect)
|
||||
{
|
||||
if (background != null)
|
||||
{
|
||||
GUI.DrawTexture(inRect, background, ScaleMode.ScaleToFit);
|
||||
}
|
||||
|
||||
if (Config.showDefName)
|
||||
{
|
||||
Text.Font = GameFont.Tiny;
|
||||
GUI.color = Color.gray;
|
||||
Widgets.Label(new Rect(5, 5, inRect.width - 10, 20f), def.defName);
|
||||
GUI.color = Color.white;
|
||||
}
|
||||
|
||||
if (Config.showLabel)
|
||||
{
|
||||
Text.Font = Config.labelFont;
|
||||
Widgets.Label(new Rect(5, 20f, inRect.width - 10, 30f), def.label);
|
||||
Text.Font = GameFont.Small;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Rect lihuiRect = new Rect(startX, startY, scaledLihuiWidth, scaledLihuiHeight);
|
||||
if (portrait != null)
|
||||
{
|
||||
GUI.DrawTexture(lihuiRect, portrait, ScaleMode.ScaleToFit);
|
||||
}
|
||||
if (Config.drawBorders)
|
||||
{
|
||||
Widgets.DrawBox(lihuiRect);
|
||||
}
|
||||
|
||||
Rect nameRect = new Rect(lihuiRect.xMax, lihuiRect.y, scaledNameWidth, scaledNameHeight);
|
||||
if (Config.drawBorders)
|
||||
{
|
||||
Widgets.DrawBox(nameRect);
|
||||
}
|
||||
Text.Anchor = TextAnchor.MiddleCenter;
|
||||
Text.Font = GameFont.Medium;
|
||||
Widgets.Label(nameRect, def.characterName);
|
||||
Text.Font = GameFont.Small;
|
||||
Text.Anchor = TextAnchor.UpperLeft;
|
||||
|
||||
Rect textRect = new Rect(nameRect.x, nameRect.yMax + Config.textNameOffset * scale, scaledTextWidth, scaledTextHeight);
|
||||
if (Config.drawBorders)
|
||||
{
|
||||
Widgets.DrawBox(textRect);
|
||||
}
|
||||
Rect textInnerRect = textRect.ContractedBy(10f * scale);
|
||||
Widgets.Label(textInnerRect, selectedDescription);
|
||||
|
||||
Rect optionRect = new Rect(nameRect.x, textRect.yMax + Config.optionsTextOffset * scale, scaledOptionsWidth, lihuiRect.height - nameRect.height - textRect.height - (Config.textNameOffset + Config.optionsTextOffset) * scale);
|
||||
|
||||
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.optionEffects);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (option.hideWhenDisabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
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<ConditionalEffects> conditionalEffects)
|
||||
{
|
||||
if (conditionalEffects.NullOrEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var ce in conditionalEffects)
|
||||
{
|
||||
if (AreConditionsMet(ce.conditions, out _))
|
||||
{
|
||||
ce.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(EventOption option, string reason)
|
||||
{
|
||||
if (!option.disabledReason.NullOrEmpty())
|
||||
{
|
||||
return option.disabledReason;
|
||||
}
|
||||
return reason;
|
||||
}
|
||||
|
||||
public override void PostClose()
|
||||
{
|
||||
base.PostClose();
|
||||
HandleAction(def.dismissEffects);
|
||||
}
|
||||
|
||||
private string FormatDescription(string description)
|
||||
{
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
// Use regex to find all placeholders like {variableName}
|
||||
return Regex.Replace(description, @"\{(.+?)\}", match =>
|
||||
{
|
||||
string varName = match.Groups[1].Value;
|
||||
if (eventVarManager.HasVariable(varName))
|
||||
{
|
||||
// Important: GetVariable<object> to get any type
|
||||
return eventVarManager.GetVariable<object>(varName)?.ToString() ?? "";
|
||||
}
|
||||
return match.Value; // Keep placeholder if variable not found
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user