TableTower/ui/image_field.gd

51 lines
1.5 KiB
GDScript3
Raw Permalink Normal View History

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)