TableTower/ui/image_field.gd
RobinFiredrake 57041d41b3 Pictures on results and Book pages; export/import keeps everything new
A result or page can have a picture (TableEntry.image, BookPage.image),
chosen from a file, shrunk to 512 px and kept in the spread file as WebP
under "images" (BoardImages; only used pictures are saved). It shows as
a thumbnail on the card row and in the details pop-up, and comes with a
page. Imported BBCode still can't point at files.

Fix: the Table Tarot JSON export and import (JsonIO.canonical) dropped
every key it didn't know, losing the Book, pictures, card colours and
sizes, limits, nesting, costs and thread options. They now pass through;
a round-trip test covers them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:21:05 -06:00

50 lines
1.5 KiB
GDScript

class_name ImageField
extends HBoxContainer
## Picks a picture for a result or a Book page: a thumbnail, "Choose…" (a
## file, shrunk and kept in the spread: BoardImages) and "Remove". Calls
## on_change with the picture's id ("" for none).
var m: Main
var _on_change: Callable
var _thumb: TextureRect
var _remove: Button
func _init(p_main: Main, id: String, on_change: Callable) -> void:
m = p_main
_on_change = on_change
add_theme_constant_override("separation", 8)
_thumb = TextureRect.new()
_thumb.custom_minimum_size = Vector2(48, 48)
_thumb.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
_thumb.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
add_child(_thumb)
var choose := ModalCard.make_button("Choose…")
choose.tooltip_text = "A picture file; it's shrunk and kept inside the spread, so exports carry it"
choose.pressed.connect(_choose)
add_child(choose)
_remove = ModalCard.make_button("Remove")
_remove.pressed.connect(func() -> void: _pick(""))
add_child(_remove)
_show(id)
func _show(id: String) -> void:
_thumb.texture = m.data.picture(id)
_thumb.visible = _thumb.texture != null
_remove.visible = id != ""
func _choose() -> void:
m.file_dialog("Choose a picture", ["*.png, *.jpg, *.jpeg, *.webp, *.svg ; Pictures"], FileDialog.FILE_MODE_OPEN_FILE, func(path: String) -> void:
var r := BoardImages.from_file(path)
if r.has("error"):
m.toast(str(r["error"]))
return
m.data.images[r["id"]] = r["data"]
_pick(str(r["id"])))
func _pick(id: String) -> void:
_show(id)
_on_change.call(id)