Choose interface font

iOtero

Active Member
Aug 4, 2020
925
1,460
Hello, I would like to know if there is any way to change the font defined in gui.rpy "gui.interface_text_font" by means of a checkbox inside secreens.rpy (Preferences or Options) with something like this:

Code:
                    vbox:
                        style_prefix "radio"
                        label _("Fonts")
                        textbutton _("arial") action xxxx arial.ttf
                        textbutton _("times") action xxxx times.ttf
                        textbutton _("ubuntu") action xxxx ubuntu.ttf
(This code, of course, doesn't work, I just put it to clarify my intention of how to do it)

To choose between three font types in the game.
 

rayminator

Engaged Member
Respected User
Sep 26, 2018
3,130
3,194
Indexed Styles

Indexed styles are lightweight styles that can be used to customize the look of a displayable based on the data supplied to that displayable. An index style is created by indexing a style object with a string or integer. If an indexed style does not exist, indexing creates it.


init python:
style.button['Foo'].background = "#f00"
style.button['Bar'].background = "#00f"


An index style is used by supplying the indexed style to a displayable.:


screen indexed_style_test:
vbox:
textbutton "Foo" style style.button["Foo"]
textbutton "Bar" style style.button["Bar"]



more info
 
  • Like
Reactions: iOtero

Erogrey

New Member
May 20, 2019
8
6
Hello!

If you just want to change fonts "live" (without restarting the game or using "full"/"utter" restart), then several ways come to mind ("Indexed Styles" too).

Another way is to override the font (size, etc.) all the existing styles you need with "rebuild". Like:
Python:
define gui.new_text_font= "gui/fonts/NewFont.ttf"

init -1 python:
    ## If we want store the player choice
    if persistent.current_font is None:
        persistent.current_font = gui.interface_text_font ## At first start of the game - default font

    def ChangeThisFont(): ## Override only basic/main styles (or specified)
        style.input.font = persistent.c_font ## style.input_prompt.font/style.say_dialogue.font/etc.
        style.gui_text.font = persistent.c_font
        style.button_text.font = persistent.c_font
        [...]
        style.rebuild()
Then use it like you want in Preferences:
Python:
                vbox:
                    style_prefix "radio"
                    label _("Fonts")
                    textbutton _("New font") action [SetVariable("persistent.current_font", gui.new_text_font), ChangeThisFont]
                    textbutton _("Default font") action [SetVariable("persistent.current_font", gui.interface_text_font), ChangeThisFont]
And just load the last font value somewhere before main menu:
Python:
label splashscreen:
    $ ChangeThisFont()
    [...]

Oh, and there is a way to override all fonts using Preference("font transform", "Your_TTF_Font") (like "Font Override" function from "A" key menu). But that's ugly.
 
  • Like
Reactions: iOtero

iOtero

Active Member
Aug 4, 2020
925
1,460
Hello!

If you just want to change fonts "live" (without restarting the game or using "full"/"utter" restart), then several ways come to mind ("Indexed Styles" too).

Another way is to override the font (size, etc.) all the existing styles you need with "rebuild". Like:
Python:
define gui.new_text_font= "gui/fonts/NewFont.ttf"

init -1 python:
    ## If we want store the player choice
    if persistent.current_font is None:
        persistent.current_font = gui.interface_text_font ## At first start of the game - default font

    def ChangeThisFont(): ## Override only basic/main styles (or specified)
        style.input.font = persistent.c_font ## style.input_prompt.font/style.say_dialogue.font/etc.
        style.gui_text.font = persistent.c_font
        style.button_text.font = persistent.c_font
        [...]
        style.rebuild()
Then use it like you want in Preferences:
Python:
                vbox:
                    style_prefix "radio"
                    label _("Fonts")
                    textbutton _("New font") action [SetVariable("persistent.current_font", gui.new_text_font), ChangeThisFont]
                    textbutton _("Default font") action [SetVariable("persistent.current_font", gui.interface_text_font), ChangeThisFont]
And just load the last font value somewhere before main menu:
Python:
label splashscreen:
    $ ChangeThisFont()
    [...]

Oh, and there is a way to override all fonts using Preference("font transform", "Your_TTF_Font") (like "Font Override" function from "A" key menu). But that's ugly.
Thanks for your help, I have followed all the steps you indicate and the font change works perfectly, but I have a small problem:

Code:
File "game/screens.rpy", line 419: expected a keyword argument or child statement.
    $ ChangeThisFont()
    ^
I've put it here: (which is probably wrong...)

Code:
## Main Menu screen ############################################################
##
## Used to display the main menu when Ren'Py starts.
##
## http://www.renpy.org/doc/html/screen_special.html#main-menu

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"

    add gui.main_menu_background

    ## This empty frame darkens the main menu.
    frame:
        pass

    label splashscreen:
        $ ChangeThisFont()


    ## The use statement includes another screen inside this one. The actual
    ## contents of the main menu are in the navigation screen.

    imagebutton:
        xpos 1725
        ypos 0
        focus_mask True
        idle Transform ("images/gui/buttons/patreon.png")
        hover Transform ("images/gui/buttons/patreon_hover.png")
        action OpenURL("https://www.patreon.com/bePatron?u=7842637")

    imagebutton:
        xpos 25
        ypos 450
        focus_mask True
        idle Transform ("images/gui/buttons/new_game.png")
        hover Transform ("images/gui/buttons/new_game_hover.png")
        action Start()
I'm taking my first steps with python and renpy, so excuse my ignorance. :oops:

All is inside screens.rpy:

Code:
################################################################################
## Initialization
################################################################################

init offset = -1

init -1 python:
    ## If we want store the player choice
    if persistent.current_font is None:
        persistent.current_font = gui.interface_text_font ## At first start of the game - default font

    def ChangeThisFont(): ## Override only basic/main styles (or specified)
        style.input.font = persistent.current_font ## style.input_prompt.font/style.say_dialogue.font/etc.
        style.gui_text.font = persistent.current_font
        style.button_text.font = persistent.current_font
        style.rebuild()
And the vbox radio is in Preferences and works perfectly.
 
Last edited:

Erogrey

New Member
May 20, 2019
8
6
About .

Just put this out of screen main_menu(), and add "return":

Python:
label splashscreen:
        $ ChangeThisFont()
        return

screen main_menu():
        [...]
 
Last edited:
  • Like
Reactions: iOtero

iOtero

Active Member
Aug 4, 2020
925
1,460
About .

Just put this out of screen main_menu(), and add "return":

Python:
label splashscreen:
        $ ChangeThisFont()
        return

screen main_menu():
        [...]
Ok, it works. Thanks verry much.
 
  • Like
Reactions: Erogrey