From bcbd0ea7daae3a18fc6d443d044df9c3f9d03db4 Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 14 Jul 2024 16:08:18 +0300 Subject: [PATCH] Fix crash when the theme has no default font, by using Godot's fallback font instead --- src/Autoload/Themes.gd | 6 ++++++ src/Classes/Cel3DObject.gd | 2 +- src/UI/Canvas/Gizmos3D.gd | 2 +- src/UI/Canvas/Measurements.gd | 2 +- src/UI/Canvas/Rulers/Guide.gd | 2 +- src/UI/Canvas/Rulers/HorizontalRuler.gd | 2 +- src/UI/Canvas/Rulers/VerticalRuler.gd | 2 +- src/UI/Dialogs/HelperScripts/SmartSlicePreview.gd | 2 +- 8 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Autoload/Themes.gd b/src/Autoload/Themes.gd index 59ef67af9..0fa2bd6cf 100644 --- a/src/Autoload/Themes.gd +++ b/src/Autoload/Themes.gd @@ -88,3 +88,9 @@ func change_icon_colors() -> void: texture.modulate = Global.modulate_icon_color if node.disabled: texture.modulate.a = 0.5 + + +func get_font() -> Font: + if Global.control.theme.has_default_font(): + return Global.control.theme.default_font + return ThemeDB.fallback_font diff --git a/src/Classes/Cel3DObject.gd b/src/Classes/Cel3DObject.gd index b17a32606..b03f07ee0 100644 --- a/src/Classes/Cel3DObject.gd +++ b/src/Classes/Cel3DObject.gd @@ -210,7 +210,7 @@ func _set_type(value: Type) -> void: Type.TEXT: node3d_type = MeshInstance3D.new() var mesh := TextMesh.new() - mesh.font = Global.control.theme.default_font + mesh.font = Themes.get_font() mesh.text = "Sample" node3d_type.mesh = mesh Type.DIR_LIGHT: diff --git a/src/UI/Canvas/Gizmos3D.gd b/src/UI/Canvas/Gizmos3D.gd index 6dbf164f4..e6e2801c1 100644 --- a/src/UI/Canvas/Gizmos3D.gd +++ b/src/UI/Canvas/Gizmos3D.gd @@ -219,7 +219,7 @@ func _draw() -> void: draw_circle(proj_back_local_scale, SCALE_CIRCLE_RADIUS, Color.BLUE) # Draw X, Y, Z characters on top of the scale circles - var font: Font = Global.control.theme.default_font + var font := Themes.get_font() var font_height := font.get_height() var char_position := Vector2(-font_height, font_height) * CHAR_SCALE / 4 * draw_scale draw_set_transform(gizmos_origin + char_position, 0, draw_scale * CHAR_SCALE) diff --git a/src/UI/Canvas/Measurements.gd b/src/UI/Canvas/Measurements.gd index 08d1831c2..ca8318859 100644 --- a/src/UI/Canvas/Measurements.gd +++ b/src/UI/Canvas/Measurements.gd @@ -10,7 +10,7 @@ var rect_bounds: Rect2i func _ready() -> void: - font = Global.control.theme.default_font + font = Themes.get_font() func update_measurement(mode_idx := Global.MeasurementMode.NONE) -> void: diff --git a/src/UI/Canvas/Rulers/Guide.gd b/src/UI/Canvas/Rulers/Guide.gd index e862512e7..b97c50323 100644 --- a/src/UI/Canvas/Rulers/Guide.gd +++ b/src/UI/Canvas/Rulers/Guide.gd @@ -5,7 +5,7 @@ enum Types { HORIZONTAL, VERTICAL } const INPUT_WIDTH := 4 -var font: Font = Global.control.theme.default_font +var font := Themes.get_font() var has_focus := true var mouse_pos := Vector2.ZERO var type := Types.HORIZONTAL diff --git a/src/UI/Canvas/Rulers/HorizontalRuler.gd b/src/UI/Canvas/Rulers/HorizontalRuler.gd index e2651c27b..988f30267 100644 --- a/src/UI/Canvas/Rulers/HorizontalRuler.gd +++ b/src/UI/Canvas/Rulers/HorizontalRuler.gd @@ -26,7 +26,7 @@ func _gui_input(event: InputEvent) -> void: # Code taken and modified from Godot's source code func _draw() -> void: - var font: Font = Global.control.theme.default_font + var font := Themes.get_font() var transform := Transform2D() var ruler_transform := Transform2D() var major_subdivide := Transform2D() diff --git a/src/UI/Canvas/Rulers/VerticalRuler.gd b/src/UI/Canvas/Rulers/VerticalRuler.gd index efc74db4a..6911cfff2 100644 --- a/src/UI/Canvas/Rulers/VerticalRuler.gd +++ b/src/UI/Canvas/Rulers/VerticalRuler.gd @@ -26,7 +26,7 @@ func _gui_input(event: InputEvent) -> void: # Code taken and modified from Godot's source code func _draw() -> void: - var font: Font = Global.control.theme.default_font + var font := Themes.get_font() var transform := Transform2D() var ruler_transform := Transform2D() var major_subdivide := Transform2D() diff --git a/src/UI/Dialogs/HelperScripts/SmartSlicePreview.gd b/src/UI/Dialogs/HelperScripts/SmartSlicePreview.gd index a53ee617a..c0208bff8 100644 --- a/src/UI/Dialogs/HelperScripts/SmartSlicePreview.gd +++ b/src/UI/Dialogs/HelperScripts/SmartSlicePreview.gd @@ -32,7 +32,7 @@ func _draw() -> void: draw_set_transform(_offset + scaled_rect.position, 0, Vector2.ONE) # var font: Font = Control.new().get_font("font") # replace with font used by pixelorama - var font: Font = Global.control.theme.default_font + var font := Themes.get_font() var font_height := font.get_height() draw_string(font, Vector2(1, font_height), str(i)) draw_set_transform(_offset, 0, Vector2.ONE)