Skip to content

Commit

Permalink
Add runtime font weight adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
drojf committed Feb 11, 2024
1 parent e7a0c4b commit 0659f3c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
33 changes: 33 additions & 0 deletions Assets.Scripts.UI/MainUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class MainUIController : MonoBehaviour
public MODMenu modMenu;
private MODToaster toaster;

private float? FontWeight;

public void UpdateGuiPosition(int x, int y)
{
unscaledPosition = new Vector3((float)x, (float)y, 0f);
Expand Down Expand Up @@ -825,12 +827,43 @@ private static void GUIUnclickableTextArea(Rect rect, string text)
/// <summary>
/// Sets the font outline width, and updates the current textwindow outline width
/// Hopefully if you toggle language (which toggles the font on chapters 1-9), the outline width is retained.
/// You will likely need to increase the font weight if you increase the outline width, as the outline 'eats'
/// into the characters, rather than expanding outwards.
/// </summary>
/// <param name="outlineWidth">The outline width as a float typically between (0, 1)</param>
public void SetFontOutlineWidth(float outlineWidth)
{
gameSystem.OutlineWidth = outlineWidth;
TextWindow.outlineWidth = GameSystem.Instance.OutlineWidth;
}

public float GetNormalFontWeight()
{
if (!FontWeight.HasValue)
{
FontWeight = TextWindow.fontSharedMaterial.GetFloat(TMPro.ShaderUtilities.ID_WeightNormal);
}

return FontWeight.Value;
}

/// <summary>
/// Some chapters TextWindow don't let you set the font weight. For those chapters, the "_WeightNormal"
/// parameter on the material is set directly.
/// </summary>
/// <param name="weight">The font weight, between -1 and 1 (more or less).
/// Negative numbers reduce the weight, positive numbers increase it. </param>
public void SetNormalFontWeight(float weight)
{
// Modifying fontSharedMaterial works, I guess doing so modifies all instances,
// while fontMaterial only modifies a single instance?
// See the docs:
// - http://digitalnativestudios.com/textmeshpro/docs/ScriptReference/TextMeshPro.html
// - http://digitalnativestudios.com/textmeshpro/docs/ScriptReference/TextMeshPro-fontSharedMaterial.html
TextWindow.fontSharedMaterial.SetFloat(TMPro.ShaderUtilities.ID_WeightNormal, weight);

FontWeight = weight;
TextWindow.ForceMeshUpdate();
}
}
}
4 changes: 2 additions & 2 deletions MOD.Scripts.UI/MODMenuCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static string TextField(string text, int maxLength, params GUILayoutOptio
return GUILayout.TextField(text, maxLength, MODStyleManager.OnGUIInstance.Group.textField, options);
}

public static string TextField(string text, int maxLength, out bool hasChanged, params GUILayoutOption[] options)
public static string TextField(string text, out bool hasChanged, params GUILayoutOption[] options)
{
string newValue = GUILayout.TextField(text, maxLength, MODStyleManager.OnGUIInstance.Group.textField, options);
string newValue = GUILayout.TextField(text, MODStyleManager.OnGUIInstance.Group.textField, options);

hasChanged = text != newValue;
return newValue;
Expand Down
12 changes: 9 additions & 3 deletions MOD.Scripts.UI/MODMenuNormal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class MODMenuNormal : MODMenuModuleInterface
private static int gameClearClickCount = 3;

string TextField_FontOutlineWidth;
string TextField_NormalFontWeight;

public MODMenuNormal(MODMenu modMenu, MODMenuAudioOptions audioOptionsMenu)
{
Expand Down Expand Up @@ -168,6 +169,7 @@ public void OnBeforeMenuVisible() {
TextField_ComputedLipSyncThreshold1 = threshold1.ToString();
TextField_ComputedLipSyncThreshold2 = threshold2.ToString();
TextField_FontOutlineWidth = GameSystem.Instance.OutlineWidth.ToString();
TextField_NormalFontWeight = GameSystem.Instance.MainUIController.GetNormalFontWeight().ToString();

gameClearClickCount = 3;
}
Expand Down Expand Up @@ -475,18 +477,22 @@ private void OnGUIFontDebug()
GUILayout.BeginHorizontal();
{
Label(new GUIContent("Outline", "Font Outline Width"));
TextField_FontOutlineWidth = TextField(TextField_FontOutlineWidth, 5, out bool hasChanged);
if (hasChanged)
TextField_FontOutlineWidth = TextField(TextField_FontOutlineWidth, out bool outlineHasChanged);

Label(new GUIContent("Normal Weight", "Normal Font Weight"));
TextField_NormalFontWeight = TextField(TextField_NormalFontWeight, out bool fontWeightHasChanged);

if (outlineHasChanged || fontWeightHasChanged)
{
try
{
GameSystem.Instance.MainUIController.SetFontOutlineWidth(float.Parse(TextField_FontOutlineWidth));
GameSystem.Instance.MainUIController.SetNormalFontWeight(float.Parse(TextField_NormalFontWeight));
}
catch (Exception e)
{
MODToaster.Show("Failed to set font settings:" + e.ToString());
}
MODToaster.Show("Font Updated");
}
}
GUILayout.EndHorizontal();
Expand Down

0 comments on commit 0659f3c

Please sign in to comment.