cramos
Topic Author
Posts: 4
Joined: 30 Jan 2024, 16:40

How to change the FontFamily ingame?

04 Jun 2024, 16:56

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.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: How to change the FontFamily ingame?

05 Jun 2024, 13:15

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?
 
cramos
Topic Author
Posts: 4
Joined: 30 Jan 2024, 16:40

Re: How to change the FontFamily ingame?

05 Jun 2024, 15:41

This is exactly what I needed, thanks!
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: How to change the FontFamily ingame?

05 Jun 2024, 17:42

Great, marking this as solved.

Who is online

Users browsing this forum: Bing [Bot] and 5 guests