Normalize logbook creature portraits

This commit is contained in:
Alexander Sellite 2026-08-24 12:01:33 -04:00
parent 7f5a522ae5
commit 8b13ba1d7d
3 changed files with 90 additions and 4 deletions

View file

@ -3,6 +3,7 @@ extends TextureRect
const ENTRY_FRAME_SIZE := Vector2(86.0, 40.0)
const DETAIL_FRAME_SIZE := Vector2(240.0, 132.0)
const UNIFORM_FOOTPRINT_AREA_RATIO: float = 0.6
static var _normalized_textures: Dictionary[String, Texture2D] = {}
@ -52,6 +53,53 @@ func configure_fitted(
custom_minimum_size = texture_size * fit_scale
func configure_uniform_footprint(
portrait_texture: Texture2D,
maximum_size: Vector2,
portrait_material: Material = null,
) -> void:
source_texture = portrait_texture
material = portrait_material
texture = _normalize_visible_bounds(portrait_texture)
if texture == null:
custom_minimum_size = Vector2.ZERO
return
custom_minimum_size = uniform_footprint_size(
texture.get_size(),
maximum_size,
)
static func uniform_footprint_size(
visible_size: Vector2,
maximum_size: Vector2,
) -> Vector2:
if (
visible_size.x <= 0.0
or visible_size.y <= 0.0
or maximum_size.x <= 0.0
or maximum_size.y <= 0.0
):
return Vector2.ZERO
var target_area: float = (
maximum_size.x
* maximum_size.y
* UNIFORM_FOOTPRINT_AREA_RATIO
)
var area_scale: float = sqrt(
target_area / (visible_size.x * visible_size.y)
)
var fit_scale: float = minf(
maximum_size.x / visible_size.x,
maximum_size.y / visible_size.y,
)
var scale: float = minf(area_scale, fit_scale)
return Vector2(
minf(maximum_size.x, maxf(1.0, roundf(visible_size.x * scale))),
minf(maximum_size.y, maxf(1.0, roundf(visible_size.y * scale))),
)
static func _normalize_visible_bounds(
portrait_texture: Texture2D,
) -> Texture2D:
@ -67,6 +115,9 @@ static func _normalize_visible_bounds(
if image == null or image.is_empty():
_normalized_textures[cache_key] = portrait_texture
return portrait_texture
if image.is_compressed() and image.decompress() != OK:
_normalized_textures[cache_key] = portrait_texture
return portrait_texture
var full_rect := Rect2i(Vector2i.ZERO, image.get_size())
var visible_rect: Rect2i = image.get_used_rect()
if visible_rect.size == Vector2i.ZERO or visible_rect == full_rect:

View file

@ -666,13 +666,18 @@ func _add_entry_content(
content.add_theme_constant_override("separation", 4)
content_margin.add_child(content)
var portrait_frame := CenterContainer.new()
portrait_frame.custom_minimum_size = CATALOG_PORTRAIT_SIZE
portrait_frame.mouse_filter = Control.MOUSE_FILTER_IGNORE
content.add_child(portrait_frame)
var portrait_view := LogbookPortraitType.new()
portrait_view.configure(
portrait_view.configure_uniform_footprint(
portrait,
CATALOG_PORTRAIT_SIZE,
_silhouette_material if unknown else null,
)
content.add_child(portrait_view)
portrait_frame.add_child(portrait_view)
var name_label := _label(_entry_label_text(entry_name), 16)
name_label.custom_minimum_size.y = 38.0