Skip to content

Commit

Permalink
Upgrade to Godot 4.3 and small refactor
Browse files Browse the repository at this point in the history
Godot 4.3 change: No longer using _get_property_list(). Using the new @export_storage annotation.
  • Loading branch information
LuisEscorza committed Sep 26, 2024
1 parent befb6d5 commit 76b75bd
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 164 deletions.
78 changes: 17 additions & 61 deletions addons/label_font_auto_sizer/label_auto_sizer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class_name LabelAutoSizer
## The minimum size value in pixels that the font will shrink to.
@export_range(1, 192, 1, "or_greater", "suffix:px") var _min_size: int = 1:
set(value):
@export_group("Debug settings")
## Set this to true if you want to debug the steps happening in the class. The calls are commented so you need to decomment them.
@export var _print_debug_enabled: bool = false
@export_group("")
if value < _max_size:
_min_size = value
else:
Expand All @@ -33,27 +29,23 @@ class_name LabelAutoSizer
call_deferred(_check_line_count.get_method())
#endregion

#region --Internal variables--
var _base_font_size: int
var _current_font_size: int
var _last_size_state: LABEL_SIZE_STATE = LABEL_SIZE_STATE.IDLE
var _size_just_modified_by_autosizer: bool = false
#region Internal variables
@export_storage var _current_font_size: int
@export_storage var _last_size_state: LABEL_SIZE_STATE = LABEL_SIZE_STATE.IDLE
@export_storage var _size_just_modified_by_autosizer: bool = false
@export_storage var _editor_defaults_set: bool = false
var _label_settings_just_duplicated: bool = false
var _set_defaults: bool = false

enum LABEL_SIZE_STATE {JUST_SHRUNK, IDLE, JUST_ENLARGED}
#endregion


#region --Signal funcs--
#region Virtual/Signal functions
## Gets called in-editor and in-game. Sets some default values if necessary.
func _ready() -> void:
if !_set_defaults:
set_editor_defaults()
#else:
#_print_debug_message(str(name) + " Base font size: " + str(_base_font_size) + "px.")
if !_editor_defaults_set:
call_deferred(_set_editor_defaults.get_method())
if Engine.is_editor_hint():
call_deferred("_connect_signals")
call_deferred(_connect_signals.get_method())
else:
call_deferred(_check_line_count.get_method())
LabelFontAutoSizeManager.register_label(self)
Expand All @@ -62,7 +54,6 @@ func _ready() -> void:
## Gets called when there are changes in either the Theme or Label Settings resources.
## Checks if the change was made by the script of by the user and if not, sets the base font size value.
func _on_font_resource_changed() -> void:
#_print_debug_message(str(name) + "' Font resource changed.")
if _size_just_modified_by_autosizer:
_size_just_modified_by_autosizer = false ## Early return because the change wasn't made by the user.
else:
Expand All @@ -72,7 +63,7 @@ func _on_font_resource_changed() -> void:

## Gets called whenever the size of the control rect is modified (in editor). Calls the line count check.
func _on_label_rect_resized() -> void:
if !_set_defaults:
if !_editor_defaults_set:
return
call_deferred(_check_line_count.get_method())

Expand All @@ -88,8 +79,9 @@ func _exit_tree() -> void:
LabelFontAutoSizeManager.erase_label(self)
#endregion

#region --Private funcs--
##Only in-editor, keeps stuff in check while manually changing font resources and resizing the label.

#region Private functions
##Only in-editor, keeps stuff in check while manually changing font resources and resizing the label (if you are going to change the label settings or the theme via code runtime, connect these signals at runtime tooby deleting "if Engine.is_editor_hint():" at line 44)
func _connect_signals() -> void:
if label_settings != null:
if !label_settings.changed.is_connected(_on_font_resource_changed):
Expand Down Expand Up @@ -138,34 +130,12 @@ func _check_font_size() -> void:
elif get("theme_override_font_sizes/font_size") != null:
_current_font_size = get("theme_override_font_sizes/font_size")
elif get_theme_font_size("font_size") != null:
#_print_debug_message(str(name) + " Base font size: " + str(_base_font_size) + "px.")


## Makes variables persistent without exposing them in the editor.
## Will get removed in Godot 4.3 with the upcoming @export_storage annotation.
func _get_property_list():
var properties: Array = []
var bool_properties: Array[String] = ["_size_just_modified_by_autosizer","_set_defaults"]
for name in bool_properties:
properties.append({
"name": name,
"type": TYPE_BOOL,
"usage": PROPERTY_USAGE_STORAGE,
})
var int_properties: Array[String] = ["_base_font_size", "_current_font_size", "_last_size_state"]
for name in int_properties:
properties.append({
"name": name,
"type": TYPE_INT,
"usage": PROPERTY_USAGE_STORAGE,
})
return properties
_current_font_size = get_theme_font_size("font_size")


## Checks the current font size and amount of lines in the text against the visible lines inside the rect.
## Calls for the shrink or enlarge methods accordingly.
func _check_line_count() -> void:
#_print_debug_message("Checking lines of " + str(name))
if Engine.is_editor_hint() and _lock_size_in_editor:
return

Expand All @@ -187,27 +157,22 @@ func _check_line_count() -> void:

## Makes the font size smaller. Rechecks or stops the cycle depending on the conditions.
func _shrink_font():
#_print_debug_message(str(name) + "' shrink method called")
#_print_debug_message(str(name) + " shrunk " + str(_size_per_step) + "px.")
_apply_font_size(_current_font_size - 1)
if _last_size_state == LABEL_SIZE_STATE.JUST_ENLARGED: ## To stop infinite cycles.
_last_size_state = LABEL_SIZE_STATE.IDLE
#_print_debug_message(str(name) + " finished shrinking. Was just enlarged.")
else:
_last_size_state = LABEL_SIZE_STATE.JUST_SHRUNK
_check_line_count()


## Makes the font size larger. Rechecks/Shrinks/stops the cycle depending on the conditions.
func _enlarge_font():
#_print_debug_message(str(name) + "' enlarge method called")
_apply_font_size(_current_font_size + 1)
if _last_size_state == LABEL_SIZE_STATE.JUST_SHRUNK:
if get_line_count() > get_visible_line_count():
_last_size_state = LABEL_SIZE_STATE.JUST_ENLARGED
_shrink_font()
else: ## To stop infinite cycles.
#_print_debug_message(str(name) + " finished enlarging. Was just shrunk.")
_last_size_state = LABEL_SIZE_STATE.IDLE
else:
_last_size_state = LABEL_SIZE_STATE.JUST_ENLARGED
Expand All @@ -222,27 +187,18 @@ func _apply_font_size(new_size: int) -> void:
else:
set("theme_override_font_sizes/font_size", new_size)
_current_font_size = new_size


## Prints message on console, for debugging was used while developing. You can decomment all the calls to debug.
func _print_debug_message(message: String) -> void:
if _print_debug_enabled:
print(message)
#endregion


#region --Public funcs--
#region Public functions
## Gets called in-editor and sets the default values.
func set_editor_defaults() -> void:
_set_defaults = true
func _set_editor_defaults() -> void:
_editor_defaults_set = true
clip_text = true
autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
if label_settings != null:
label_settings = label_settings.duplicate() ## These are not unique by default, so we it gets duplicated to not override every instance.
label_settings.changed.connect(_on_font_resource_changed)
call_deferred("_set_base_font_size")
set_deferred("_current_font_size", _base_font_size)
call_deferred("_connect_signals")
_check_font_size()
_connect_signals()
set_deferred("_max_size", _current_font_size)
Expand Down
5 changes: 5 additions & 0 deletions addons/label_font_auto_sizer/label_font_auto_size_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
extends Node
class_name LabelFontAutoSizeManager

#region Variables
static var _active_labels : Array[Control]
#endregion


#region Public functions
static func register_label(label: Control) -> void:
_active_labels.append(label)

Expand All @@ -19,3 +22,5 @@ static func erase_label(label: Control) -> void:
static func locale_chaged() -> void:
for label: Control in _active_labels:
label._on_locale_changed()
#endregion

4 changes: 2 additions & 2 deletions addons/label_font_auto_sizer/plugin.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[plugin]

name="LabelFontAutoSizer - Godot4"
name="LabelFontAutoSizer - Godot 4.3"
description="Tool that makes labels text fit into the rect."
author="Lescandez"
version="1.0.2"
version="1.1.0"
script="plugin.gd"
73 changes: 15 additions & 58 deletions addons/label_font_auto_sizer/rich_label_auto_sizer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class_name RichLabelAutoSizer
## The minimum size value in pixels that the font will shrink to.
@export_range(1, 192, 1, "or_greater", "suffix:px") var _min_size: int = 1:
set(value):
@export_group("Debug settings")
## Set this to true if you want to debug the steps happening in the class. The calls are commented so you need to decomment them.
@export var _print_debug_enabled: bool = false
@export_group("")
if value < _max_size:
_min_size = value
else:
Expand All @@ -33,28 +29,23 @@ class_name RichLabelAutoSizer
call_deferred(_check_line_count.get_method())
#endregion

#region --Internal variables--
var _base_font_size: int
var _current_font_size: int
var _last_size_state: LABEL_SIZE_STATE = LABEL_SIZE_STATE.IDLE
var _size_just_modified_by_autosizer: bool = false
#region Internal variables
@export_storage var _current_font_size: int
@export_storage var _last_size_state: LABEL_SIZE_STATE = LABEL_SIZE_STATE.IDLE
@export_storage var _size_just_modified_by_autosizer: bool = false
@export_storage var _editor_defaults_set: bool = false
var _label_settings_just_duplicated: bool = false
var _set_defaults: bool = false

enum LABEL_SIZE_STATE {JUST_SHRUNK, IDLE, JUST_ENLARGED}
#endregion



#region --Signal funcs--
#region Virtual/Signal functions
## Gets called in-editor and in-game. Sets some default values if necessary.
func _ready() -> void:
if !_set_defaults:
set_editor_defaults()
#else:
#_print_debug_message(str(name) + " Base font size: " + str(_base_font_size) + "px.")
if !_editor_defaults_set:
call_deferred(_set_editor_defaults.get_method())
if Engine.is_editor_hint():
call_deferred("_connect_signals")
call_deferred(_connect_signals.get_method())
else:
call_deferred(_check_line_count.get_method())
LabelFontAutoSizeManager.register_label(self)
Expand All @@ -63,7 +54,6 @@ func _ready() -> void:
## Gets called when there are changes in either the Theme or Label Settings resources.
## Checks if the change was made by the script of by the user and if not, sets the base font size value.
func _on_font_resource_changed() -> void:
#_print_debug_message(str(name) + "' Font resource changed.")
if _size_just_modified_by_autosizer:
_size_just_modified_by_autosizer = false ## Early return because the change wasn't made by the user.
else:
Expand All @@ -72,7 +62,7 @@ func _on_font_resource_changed() -> void:

## Gets called whenever the size of the control rect is modified (in editor). Calls the line count check.
func _on_label_rect_resized() -> void:
if !_set_defaults:
if !_editor_defaults_set:
return
call_deferred(_check_line_count.get_method())

Expand All @@ -88,7 +78,8 @@ func _exit_tree() -> void:
LabelFontAutoSizeManager.erase_label(self)
#endregion

#region --Private funcs--

#region Private functions
##Only in-editor, keeps stuff in check while manually changing font resources and resizing the label.
func _connect_signals() -> void:
if !theme_changed.is_connected(_on_font_resource_changed):
Expand Down Expand Up @@ -116,35 +107,12 @@ func _check_font_size() -> void:
if get("theme_override_font_sizes/normal_font_size") != null:
_current_font_size = get("theme_override_font_sizes/normal_font_size")
elif get_theme_font_size("normal_font_size") != null:
#_print_debug_message(str(name) + " Base font size: " + str(_base_font_size) + "px.")


## Makes variables persistent without exposing them in the editor.
## Will get removed in Godot 4.3 with the upcoming @export_storage annotation.
func _get_property_list():
var properties: Array = []
var bool_properties: Array[String] = ["_size_just_modified_by_autosizer","_set_defaults"]
for name in bool_properties:
properties.append({
"name": name,
"type": TYPE_BOOL,
"usage": PROPERTY_USAGE_STORAGE,
})
var int_properties: Array[String] = ["_base_font_size", "_current_font_size", "_last_size_state"]
for name in int_properties:
properties.append({
"name": name,
"type": TYPE_INT,
"usage": PROPERTY_USAGE_STORAGE,
})
return properties
_current_font_size = get_theme_font_size("normal_font_size")


## Checks the current font size and amount of lines in the text against the visible lines inside the rect.
## Calls for the shrink or enlarge methods accordingly.
func _check_line_count() -> void:
#_print_debug_message("Checking lines of " + str(name))
if Engine.is_editor_hint() and _lock_size_in_editor:
return

Expand All @@ -168,27 +136,22 @@ func _check_line_count() -> void:

## Makes the font size smaller. Rechecks or stops the cycle depending on the conditions.
func _shrink_font():
#_print_debug_message(str(name) + "' shrink method called")
#_print_debug_message(str(name) + " shrunk " + str(_size_per_step) + "px.")
_apply_font_size(_current_font_size - 1)
if _last_size_state == LABEL_SIZE_STATE.JUST_ENLARGED: ## To stop infinite cycles.
_last_size_state = LABEL_SIZE_STATE.IDLE
#_print_debug_message(str(name) + " finished shrinking. Was just enlarged.")
else:
_last_size_state = LABEL_SIZE_STATE.JUST_SHRUNK
call_deferred(_check_line_count.get_method())


## Makes the font size larger. Rechecks/Shrinks/stops the cycle depending on the conditions.
func _enlarge_font():
#_print_debug_message(str(name) + "' enlarge method called")
_apply_font_size(_current_font_size + 1)
if _last_size_state == LABEL_SIZE_STATE.JUST_SHRUNK:
if get_content_height() > size.y:
_last_size_state = LABEL_SIZE_STATE.JUST_ENLARGED
_shrink_font()
else: ## To stop infinite cycles.
#_print_debug_message(str(name) + " finished enlarging. Was just shrunk.")
_last_size_state = LABEL_SIZE_STATE.IDLE
else:
_last_size_state = LABEL_SIZE_STATE.JUST_ENLARGED
Expand All @@ -200,19 +163,13 @@ func _apply_font_size(new_size: int) -> void:
_size_just_modified_by_autosizer = true
set("theme_override_font_sizes/normal_font_size", new_size)
_current_font_size = new_size


## Prints message on console, for debugging was used while developing. You can decomment all the calls to debug.
func _print_debug_message(message: String) -> void:
if _print_debug_enabled:
print(message)
#endregion


#region --Public funcs--
#region Public functions
## Gets called in-editor and sets the default values.
func set_editor_defaults() -> void:
_set_defaults = true
func _set_editor_defaults() -> void:
_editor_defaults_set = true
clip_contents = true
fit_content = false
scroll_active = false
Expand Down
1 change: 1 addition & 0 deletions demo/Anta-Regular.ttf.import
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dest_files=["res://.godot/imported/Anta-Regular.ttf-33a1d4b91c58e15249e5e53adb13
Rendering=null
antialiasing=1
generate_mipmaps=false
disable_embedded_bitmaps=true
multichannel_signed_distance_field=false
msdf_pixel_range=8
msdf_size=48
Expand Down
Loading

0 comments on commit 76b75bd

Please sign in to comment.