Add functionality to clear all generated portrait images in AIImages mod. Implement UI button for clearing images and add confirmation dialog. Update localized strings in English and Russian for new feature. Update AIImages.dll to reflect these changes.

This commit is contained in:
Leonid Pershin
2025-10-26 23:16:08 +03:00
parent ff5f679c4a
commit 9fb05e4e7e
5 changed files with 86 additions and 0 deletions

View File

@@ -86,5 +86,59 @@ namespace AIImages.Helpers
comp.PortraitPath = null;
}
}
/// <summary>
/// Очистить все сгенерированные портреты
/// </summary>
public static int ClearAllPortraits(string savePath)
{
int deletedCount = 0;
// Очищаем компоненты всех пешек
if (Current.Game != null && Current.Game.Maps != null)
{
foreach (var map in Current.Game.Maps)
{
foreach (var pawn in map.mapPawns.AllPawns)
{
var comp = GetPortraitComp(pawn);
if (comp != null && comp.HasPortrait)
{
comp.PortraitPath = null;
}
}
}
}
// Удаляем файлы из директории
string fullPath = Path.Combine(GenFilePaths.SaveDataFolderPath, savePath);
if (Directory.Exists(fullPath))
{
try
{
var files = Directory.GetFiles(fullPath, "*.png");
foreach (var file in files)
{
try
{
File.Delete(file);
deletedCount++;
}
catch (System.Exception ex)
{
Log.Warning($"[AI Images] Failed to delete file {file}: {ex.Message}");
}
}
Log.Message($"[AI Images] Deleted {deletedCount} portrait files");
}
catch (System.Exception ex)
{
Log.Error($"[AI Images] Error cleaning portraits directory: {ex.Message}");
}
}
return deletedCount;
}
}
}

View File

@@ -366,6 +366,14 @@ namespace AIImages
listingStandard.Label("AIImages.Settings.SavePath".Translate() + ":");
settings.savePath = listingStandard.TextEntry(settings.savePath);
listingStandard.Gap(12f);
// Кнопка очистки всех изображений
if (listingStandard.ButtonText("AIImages.Settings.ClearAllImages".Translate()))
{
ClearAllGeneratedImages(settings);
}
}
private static void TestApiConnection(
@@ -476,5 +484,23 @@ namespace AIImages
selectedValue = availableValues[0];
}
}
private static void ClearAllGeneratedImages(AIImagesModSettings settings)
{
Find.WindowStack.Add(
Dialog_MessageBox.CreateConfirmation(
"AIImages.Settings.ClearAllImagesConfirm".Translate(),
delegate
{
int deletedCount = PawnPortraitHelper.ClearAllPortraits(settings.savePath);
Messages.Message(
"AIImages.Settings.ClearAllImagesSuccess".Translate(deletedCount),
MessageTypeDefOf.PositiveEvent
);
},
destructive: true
)
);
}
}
}