Page 1 of 1

How to change the FontFamily ingame?

Posted: 04 Jun 2024, 16:56
by cramos
Hi,

We're currently using a ResourceDictionary that includes the default font used by our game, something like this:
<FontFamily x:Key="MainFont">/Assets/UI/Fonts/#MainFont</FontFamily>
We want to change this MainFont depending on TWO things:
·The language selected, for example if the user selects Japanese we want to use another font, if they select Korean another, etc...
·The user can also select between two different fonts for the english language, for example they can play with MainFontA or MainFontB.

I'm not sure if Noesis supports this and what would be the best way to handle this. Any pointers would be greatly appreciated.

Re: How to change the FontFamily ingame?

Posted: 05 Jun 2024, 13:15
by sfernandez
Hello,

One way to achieve what you want is to merge a different dictionary depending on the selected language/font. For example, you can have a master dictionary for the fonts in the Application Resources, call it Fonts.xaml, and then have specific dictionaries for each language/font, like Fonts.English.xaml:
Fonts.xaml
----------
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Fonts.English.xaml"/>
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Fonts.English.xaml
------------------
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <FontFamily x:Key="MainFont">Fonts/#Font 1</FontFamily>
</ResourceDictionary>

Fonts.French.xaml
-----------------
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <FontFamily x:Key="MainFont">Fonts/#Font 2</FontFamily>
</ResourceDictionary>
Then you can reference that font in your root page (or anywhere you need) with a DynamicResource:
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  FontFamily="{DynamicResource MainFont}">
  ...
</Page>
And when the selected language changes you just update the merged dictionary:
void ChangeLanguageFont(ResourceDictionary fontsDictionary, string languagePath)
{
  fontsDictionary.MergedDictionaries.Clear();
  ResourceDictionary lang = (ResourceDictionary)Noesis.GUI.LoadXaml(languagePath);
  fontsDictionary.MergedDictionaries.Add(lang);
}
Could something like this work for you?

Re: How to change the FontFamily ingame?

Posted: 05 Jun 2024, 15:41
by cramos
This is exactly what I needed, thanks!

Re: How to change the FontFamily ingame?

Posted: 05 Jun 2024, 17:42
by sfernandez
Great, marking this as solved.