Skip to content

Commit

Permalink
WIP settings translation support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Epicguru committed Feb 5, 2024
1 parent ce08e20 commit 7fa38e3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
25 changes: 25 additions & 0 deletions Languages/English/Keyed/Keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,29 @@

<AM.Patch.OptDisabled>The invisibilty optimization has been disabled by the Melee Animation mod due to visual bugs.</AM.Patch.OptDisabled>

<!-- Translate settings menu: -->
<SimpleSettings.Preset>Preset:</SimpleSettings.Preset>
<SimpleSettings.Preset.Custom>Custom</SimpleSettings.Preset.Custom>
<SimpleSettings.ClickToReset>Right-click to set an option back to its default value.</SimpleSettings.ClickToReset>

<AM.AMSettings.Presets.Default>Default</AM.AMSettings.Presets.Default>
<AM.AMSettings.Presets.Default.Desc>Enables all features the mod has to offer</AM.AMSettings.Presets.Default.Desc>

<AM.AMSettings.Presets.NoLassos>No Lassos</AM.AMSettings.Presets.NoLassos>
<AM.AMSettings.Presets.NoLassos.Desc>Everything is enabled except lassos. You can still craft and use lassos manually if you want but enemies will not spawn with or use lassos.
* Adds melee attack animations.
* Adds melee weapon idle animations
* Adds duels
* Adds execution animations
* Adds Unique Skills</AM.AMSettings.Presets.NoLassos.Desc>

<AM.AMSettings.Presets.VanillaPlus>Vanilla+</AM.AMSettings.Presets.VanillaPlus>
<AM.AMSettings.Presets.VanillaPlus.Desc>Keeps things very close to vanilla combat. Your pawns will not do executions or use lassos unless you manually instruct them to. Enemies will never do executions or use lassos.
* Adds melee attack animations.
* Adds melee weapon idle animations</AM.AMSettings.Presets.VanillaPlus.Desc>


<AM.AMSettings.Settings.Header0>LKAJSDLKJAKLSD</AM.AMSettings.Settings.Header0>


</LanguageData>
8 changes: 4 additions & 4 deletions Source/ThingGenerator/AMSettings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class Settings : SimpleSettingsBase
#endregion

#region Executions & Duels
[Header("Executions & Duels")]
[Header("Executions & Duels", order = 0)]
[Description("If true, your pawns will automatically execute enemy pawns in combat, without your input.\n" +
"This may include opportunistically using their grappling hooks if the Auto Grapple setting is enabled.\n\n" +
"This only changes the <b>default</b> setting. It can also be configured on a per-pawn basis.")]
Expand Down Expand Up @@ -205,7 +205,7 @@ public class Settings : SimpleSettingsBase
#endregion

#region Visuals
[Header("Visuals")]
[Header("Visuals", order = 1)]
[Description("Should pawn hands be displayed holding melee weapons?")]
[WebContent("HandsEnabled", false)]
public bool ShowHands = true;
Expand Down Expand Up @@ -277,7 +277,7 @@ public class Settings : SimpleSettingsBase
#endregion

#region Performance
[Header("Performance")]
[Header("Performance", order = 2)]
[Description("The maximum number of CPU threads to use when processing pawns for automatic executions & lasso usage.\n" +
"If set to 0, the thread count is automatically determined based on your CPU, and if set to 1 then multi-threaded processing is disabled.\n" +
"Set to 1 if you experience error spam caused by a mod conflict, although it will decrease performance considerably.")]
Expand All @@ -300,7 +300,7 @@ public class Settings : SimpleSettingsBase

#region Other

[Header("Other")]
[Header("Other", order = 3)]
[Label("Friendly Pawn Lethality Bonus")]
[Description("Positive values act as a lethality bonus for friendly pawns (including slaves) in execution & duel outcomes, meaning that they will be lethal more often.")]
[Percentage]
Expand Down
62 changes: 53 additions & 9 deletions Source/ThingGenerator/AMSettings/SimpleSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ namespace AM.AMSettings;

public abstract class SimpleSettingsBase : ModSettings
{
public string GetName()
{
string translationKey = GetType().FullName;
if (translationKey.TryTranslate(out var result))
return result;
return PresetName;
}

public string GetDescription()
{
string translationKey = $"{GetType().FullName}.Desc";
if (translationKey.TryTranslate(out var result))
return result;
return PresetDescription;
}

public virtual string PresetName => null;
public virtual string PresetDescription => null;
}
Expand All @@ -41,7 +57,7 @@ public static class SimpleSettings
{ typeof(bool), DrawToggle },
{ typeof(Color), DrawColor }
};
public static Func<MemberWrapper, DrawHandler> SelectDrawHandler = DefaultDrawHandlerSelector;
public static Func<MemberWrapper, DrawHandler> SelectDrawHandler { get; set; } = DefaultDrawHandlerSelector;

private static readonly Dictionary<Type, FieldHolder> settingsFields = new Dictionary<Type, FieldHolder>();
private static readonly Stack<ScribeSaver> saverStack = new Stack<ScribeSaver>();
Expand All @@ -53,6 +69,31 @@ public static class SimpleSettings

internal static string TranslateOrSelf(this string str) => str.TryTranslate(out var found) ? found : str;

private static string GenerateTranslationKeys(SimpleSettingsBase settings)
{
FieldHolder holder = GetHolder(settings);

var str = new StringBuilder(1024);
string settingsType = settings.GetType().FullName;

void Emit(string key, string contents)
{
str.Append('<').Append(key).Append("><").Append(contents).Append("></").Append(key).AppendLine(">");
}

foreach (var member in holder.Members.Values)
{
var header = member.TryGetCustomAttribute<HeaderAttribute>();
if (header != null)
{
Emit($"{settingsType}.Header{header.order}", header.header);
}

var label = member.TryGetCustomAttribute<LabelAttribute>();
Emit()

Check failure on line 93 in Source/ThingGenerator/AMSettings/SimpleSettings.cs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest

; expected

Check failure on line 93 in Source/ThingGenerator/AMSettings/SimpleSettings.cs

View workflow job for this annotation

GitHub Actions / Build on ubuntu-latest

; expected
}
}

public static void Init(SimpleSettingsBase settings)
{
if (settings == null)
Expand Down Expand Up @@ -243,13 +284,13 @@ public static void DrawWindow(SimpleSettingsBase settings, Rect inRect)

var holder = GetHolder(settings);

if (Widgets.ButtonText(inRect with {height = 24, width = inRect.width * 0.2f}, $"<b>Preset:</b> {currentPreset?.PresetName ?? "Custom"}"))
if (Widgets.ButtonText(inRect with {height = 24, width = inRect.width * 0.2f}, $"<b>{"SimpleSettings.Preset".Translate()}</b> {currentPreset?.GetName() ?? "SimpleSettings.Preset.Custom".Translate()}"))
ShowPresetsDropdown(settings, holder);
if (currentPreset != null)
TooltipHandler.TipRegion(inRect with {height = 24, width = inRect.width * 0.2f}, currentPreset.PresetDescription);
TooltipHandler.TipRegion(inRect with {height = 24, width = inRect.width * 0.2f}, currentPreset.GetDescription());

var tips = inRect with {height = 24, x = inRect.width * 0.2f + 20};
Widgets.LabelFit(tips, "Right-click to set an option back to its default value.");
Widgets.LabelFit(tips, "SimpleSettings.ClickToReset".Translate());
Widgets.DrawLineHorizontal(inRect.x, inRect.y + 30, inRect.width);
inRect.yMin += 38;

Expand Down Expand Up @@ -288,7 +329,7 @@ public static void DrawWindow(SimpleSettingsBase settings, Rect inRect)
if(isCurrentTab)
{
var headerRect = new Rect(new Vector2(pos.x, pos.y + 12), new Vector2(inRect.width - 20, headerHeight));
string headerText = $"{settings.GetType().FullName}.{header.header}".TryTranslate(out var found) ? found : header.header;
string headerText = $"{settings.GetType().FullName}.Header{header.order}".TryTranslate(out var found) ? found : header.header;
Widgets.Label(headerRect, $"<color=cyan><b><size=22>{headerText}</size></b></color>");

pos.y += headerHeight + 12;
Expand Down Expand Up @@ -1007,15 +1048,15 @@ public bool IsDefault(SimpleSettingsBase settings)

protected virtual string MakeDisplayName()
{
if (TranslationName.TryTranslate(out var found))
return found;

var label = TryGetCustomAttribute<LabelAttribute>();
if (label != null)
{
return label.Label.TranslateOrSelf();
}

if (TranslationName.TryTranslate(out var found))
return found;

var str = new StringBuilder();
bool lastWasLower = true;
foreach (var c in Name)
Expand Down Expand Up @@ -1047,9 +1088,12 @@ public virtual string ValueToString(object value)

public virtual string GetDescription()
{
if ($"{TranslationName}.Desc".TryTranslate(out var found))
return found;

var attr = TryGetCustomAttribute<DescriptionAttribute>();
if (attr == null)
return $"{TranslationName}.Desc".TryTranslate(out var found) ? found : null;
return string.Empty;

return attr.Description.TranslateOrSelf();
}
Expand Down

0 comments on commit 7fa38e3

Please sign in to comment.