Files
rimworld-font-replacer/Source/Settings.cs
2026-04-02 17:53:00 -04:00

258 lines
9.7 KiB
C#

using UnityEngine;
using Verse;
using System;
using HarmonyLib;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine.UI;
using System.IO;
namespace raptureparty.RimWorldFontReplacer
{
public class FontListDialog : Window
{
protected Vector2 scrollPosition = Vector2.zero;
protected string[] _fonts = new string[0];
protected float bottomAreaHeight;
protected string _filterText = "";
private string _prevFilterText = null;
public uint FontIndex = 0;
public FontListDialog()
{
doCloseButton = true;
doCloseX = true;
forcePause = true;
absorbInputAroundWindow = true;
closeOnAccept = false;
}
public override void PreOpen()
{
base.PreOpen();
_fonts = Font.GetOSInstalledFontNames();
}
public override void PostClose()
{
base.PostClose();
}
private string CurrentFontName
{
get
{
switch (FontIndex)
{
case 0: return FontLoader.TargetTinyFontName;
case 1: return FontLoader.TargetSmallFontName;
case 2: return FontLoader.TargetMediumFontName;
case 3: return FontLoader.TargetPlanetFontName;
default: return "";
}
}
}
private string FontTypeLabel
{
get
{
switch (FontIndex)
{
case 0: return "RWFR.Tiny".Translate();
case 1: return "RWFR.Small".Translate();
case 2: return "RWFR.Medium".Translate();
case 3: return "RWFR.Planet".Translate();
default: return "";
}
}
}
public override void DoWindowContents(Rect inRect)
{
float y = inRect.y;
// Title
Verse.Text.Font = GameFont.Medium;
Widgets.Label(new Rect(inRect.x, y, inRect.width, 32f), "RWFR.ChooseFont_For".Translate(FontTypeLabel));
Verse.Text.Font = GameFont.Small;
y += 36f;
// Current selection
string current = CurrentFontName;
if (!string.IsNullOrEmpty(current))
{
GUI.color = new Color(0.7f, 0.7f, 0.7f);
Widgets.Label(new Rect(inRect.x, y, inRect.width, 24f), "RWFR.Current".Translate() + ": " + current);
GUI.color = Color.white;
y += 26f;
}
// Filter box
Widgets.Label(new Rect(inRect.x, y + 2f, 60f, 28f), "RWFR.Filter".Translate());
string newFilter = Widgets.TextField(new Rect(inRect.x + 64f, y, inRect.width - 64f, 28f), _filterText);
if (newFilter != _prevFilterText)
{
scrollPosition = Vector2.zero;
_prevFilterText = newFilter;
}
_filterText = newFilter;
y += 34f;
// Build filtered list
string[] filtered = string.IsNullOrEmpty(_filterText)
? _fonts
: System.Array.FindAll(_fonts, f => f.IndexOf(_filterText, StringComparison.OrdinalIgnoreCase) >= 0);
float listHeight = filtered.Length * 36f;
Rect outRect = new Rect(inRect.x, y, inRect.width, inRect.height - (y - inRect.y) - bottomAreaHeight - 50f);
Rect viewRect = new Rect(0f, 0f, outRect.width - 16f, listHeight);
Widgets.BeginScrollView(outRect, ref scrollPosition, viewRect, true);
for (int i = 0; i < filtered.Length; i++)
{
bool isSelected = filtered[i] == current;
if (isSelected)
GUI.color = new Color(0.8f, 1f, 0.8f);
if (Widgets.ButtonText(new Rect(0f, i * 36f, viewRect.width, 26f), filtered[i]))
{
switch (FontIndex)
{
case 0: FontLoader.TargetTinyFontName = filtered[i]; break;
case 1: FontLoader.TargetSmallFontName = filtered[i]; break;
case 2: FontLoader.TargetMediumFontName = filtered[i]; break;
case 3: FontLoader.TargetPlanetFontName = filtered[i]; break;
}
Close();
}
if (isSelected)
GUI.color = Color.white;
}
Widgets.EndScrollView();
}
}
class Settings : ModSettings
{
public static Vector2 scrollPosition = Vector2.zero;
private static string s_fontTinySizeBuffer = FontLoader.FontTinySize.ToString();
private static string s_fontSmallSizeBuffer = FontLoader.FontSmallSize.ToString();
private static string s_fontMediumSizeBuffer = FontLoader.FontMediumSize.ToString();
public static void Build(Rect inRect)
{
inRect.yMin += 15f;
inRect.yMax -= 15f;
var defaultColumnWidth = inRect.width - 50;
Listing_Standard list = new Listing_Standard() { ColumnWidth = defaultColumnWidth };
var outRect = new Rect(inRect.x, inRect.y, inRect.width, inRect.height);
var scrollRect = new Rect(0f, 0f, inRect.width - 16f, inRect.height + 200);
Widgets.BeginScrollView(outRect, ref scrollPosition, scrollRect, true);
list.Begin(scrollRect);
list.Label("RWFR.Fonts".Translate());
list.GapLine();
list.Label("RWFR.Tiny".Translate());
list.Indent();
FontLoader.TargetTinyFontName = list.TextEntryLabeled("RWFR.Font_family".Translate(), FontLoader.TargetTinyFontName, 1);
list.TextFieldNumericLabeled<int>("RWFR.Font_size".Translate(), ref FontLoader.FontTinySize, ref s_fontTinySizeBuffer, 1);
if (list.ButtonTextLabeled("", "RWFR.ChooseFont".Translate()))
{
var selector = new FontListDialog { FontIndex = 0 };
Find.WindowStack.Add(selector);
}
list.Label(FontLoader.FontTinyFound ? "" : (string)"RWFR.Not_found".Translate());
list.Outdent();
list.Label("RWFR.Small".Translate());
list.Indent();
FontLoader.TargetSmallFontName = list.TextEntryLabeled("RWFR.Font_family".Translate(), FontLoader.TargetSmallFontName, 1);
list.TextFieldNumericLabeled<int>("RWFR.Font_size".Translate(), ref FontLoader.FontSmallSize, ref s_fontSmallSizeBuffer, 1);
if (list.ButtonTextLabeled("", "RWFR.ChooseFont".Translate()))
{
var selector = new FontListDialog { FontIndex = 1 };
Find.WindowStack.Add(selector);
}
list.Label(FontLoader.FontSmallFound ? "" : (string)"RWFR.Not_found".Translate());
list.Outdent();
list.Label("RWFR.Medium".Translate());
list.Indent();
FontLoader.TargetMediumFontName = list.TextEntryLabeled("RWFR.Font_family".Translate(), FontLoader.TargetMediumFontName, 1);
list.TextFieldNumericLabeled<int>("RWFR.Font_size".Translate(), ref FontLoader.FontMediumSize, ref s_fontMediumSizeBuffer, 1);
if (list.ButtonTextLabeled("", "RWFR.ChooseFont".Translate()))
{
var selector = new FontListDialog { FontIndex = 2 };
Find.WindowStack.Add(selector);
}
list.Label(FontLoader.FontMediumFound ? "" : (string)"RWFR.Not_found".Translate());
list.Outdent();
list.Gap(12f);
list.Label("RWFR.Previews".Translate());
list.GapLine();
string previewText = "RWFR.Preview_text".Translate();
list.Label("RWFR.Tiny".Translate(), 28f);
list.Indent();
if (CustomGUIStyles.fontStyles != null)
{
float h = Mathf.Max(FontLoader.FontTinySize, 12) + 10f;
GUI.Label(list.GetRect(h), previewText, CustomGUIStyles.fontStyles[0]);
}
list.Outdent();
list.Label("RWFR.Small".Translate(), 28f);
list.Indent();
if (CustomGUIStyles.fontStyles != null)
{
float h = Mathf.Max(FontLoader.FontSmallSize, 12) + 10f;
GUI.Label(list.GetRect(h), previewText, CustomGUIStyles.fontStyles[1]);
}
list.Outdent();
list.Label("RWFR.Medium".Translate(), 28f);
list.Indent();
if (CustomGUIStyles.fontStyles != null)
{
float h = Mathf.Max(FontLoader.FontMediumSize, 12) + 10f;
GUI.Label(list.GetRect(h), previewText, CustomGUIStyles.fontStyles[2]);
}
list.Outdent();
list.End();
Widgets.EndScrollView();
CustomGUIStyles.UpdateFonts();
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref FontLoader.FontTinySize, "fontSizeTiny", 11);
Scribe_Values.Look(ref FontLoader.FontSmallSize, "fontSizeSmall", 14);
Scribe_Values.Look(ref FontLoader.FontMediumSize, "fontSizeMedium", 18);
Scribe_Values.Look(ref FontLoader.TargetTinyFontName, "fontNameTiny", "Helvetica");
Scribe_Values.Look(ref FontLoader.TargetSmallFontName, "fontNameSmall", "Helvetica");
Scribe_Values.Look(ref FontLoader.TargetMediumFontName, "fontNameMedium", "Helvetica");
Scribe_Values.Look(ref FontLoader.TargetPlanetFontName, "fontNamePlanet", "Helvetica");
}
}
}