From fd714d04dff2547fec6236a8b9103e31829065ba Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:52:42 +0300 Subject: [PATCH] Update the ExtensionsAPI to handle fonts --- project.godot | 2 +- src/Autoload/ExtensionsApi.gd | 23 +++++++++++++++++++++++ src/Autoload/Global.gd | 1 + src/Preferences/PreferencesDialog.gd | 13 ++++++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/project.godot b/project.godot index 78920d2f8..e5ac05993 100644 --- a/project.godot +++ b/project.godot @@ -24,7 +24,7 @@ boot_splash/use_filter=false config/icon="res://assets/graphics/icons/icon.png" config/macos_native_icon="res://assets/graphics/icons/icon.icns" config/windows_native_icon="res://assets/graphics/icons/icon.ico" -config/ExtensionsAPI_Version=4 +config/ExtensionsAPI_Version=5 config/Pxo_Version=3 [audio] diff --git a/src/Autoload/ExtensionsApi.gd b/src/Autoload/ExtensionsApi.gd index 6c4259e63..3b4faaaba 100644 --- a/src/Autoload/ExtensionsApi.gd +++ b/src/Autoload/ExtensionsApi.gd @@ -409,6 +409,29 @@ class ThemeAPI: Themes.remove_theme(theme) ExtensionsApi.remove_action("ThemeAPI", "add_theme") + ## Adds a new font. + func add_font(font: Font) -> void: + Global.loaded_fonts.append(font) + Global.font_loaded.emit() + + ## Removes a loaded font. + ## If that font is the current one of the interface, set it back to Roboto. + func remove_font(font: Font) -> void: + var font_index := Global.loaded_fonts.find(font) + if font_index == -1: + return + if Global.theme_font_index == font_index: + Global.theme_font_index = 1 + Global.loaded_fonts.remove_at(font_index) + Global.font_loaded.emit() + + ## Sets a font as the current one for the interface. The font must have been + ## added beforehand by [method add_font]. + func set_font(font: Font) -> void: + var font_index := Global.loaded_fonts.find(font) + if font_index > -1: + Global.theme_font_index = font_index + ## Gives ability to add/remove tools. class ToolAPI: diff --git a/src/Autoload/Global.gd b/src/Autoload/Global.gd index 72a8ae827..1d6e7ed22 100644 --- a/src/Autoload/Global.gd +++ b/src/Autoload/Global.gd @@ -12,6 +12,7 @@ signal project_about_to_switch ## Emitted before a project is about to be switc signal project_switched ## Emitted whenever you switch to some other project tab. signal cel_switched ## Emitted whenever you select a different cel. signal project_data_changed(project: Project) ## Emitted when project data is modified. +signal font_loaded ## Emitted when a new font has been loaded, or an old one gets unloaded. enum LayerTypes { PIXEL, GROUP, THREE_D } enum GridTypes { CARTESIAN, ISOMETRIC, ALL } diff --git a/src/Preferences/PreferencesDialog.gd b/src/Preferences/PreferencesDialog.gd index c662950d4..3d6bcf42f 100644 --- a/src/Preferences/PreferencesDialog.gd +++ b/src/Preferences/PreferencesDialog.gd @@ -241,6 +241,7 @@ class Preference: func _ready() -> void: + Global.font_loaded.connect(_add_fonts) # Replace OK since preference changes are being applied immediately, not after OK confirmation get_ok_button().text = "Close" get_ok_button().size_flags_horizontal = Control.SIZE_EXPAND_FILL @@ -293,9 +294,7 @@ func _ready() -> void: language.add_child(button) button.pressed.connect(_on_language_pressed.bind(button.get_index())) - # Add fonts to the font option button - for font_name in Global.get_available_font_names(): - %FontOptionButton.add_item(font_name) + _add_fonts() for pref in preferences: if not right_side.has_node(pref.node_path): @@ -363,6 +362,14 @@ func _on_Preference_value_changed(value, pref: Preference, button: RestoreDefaul disable_restore_default_button(button, disable) +## Add fonts to the font option button. +func _add_fonts() -> void: + %FontOptionButton.clear() + for font_name in Global.get_available_font_names(): + %FontOptionButton.add_item(font_name) + %FontOptionButton.select(Global.theme_font_index) + + func preference_update(require_restart := false) -> void: if require_restart: must_restart.visible = true