TableTower/ui/sheet_preview.gd

71 lines
2.9 KiB
GDScript3
Raw Normal View History

class_name SheetPreview
extends Control
## A small drawing of a sheet's first tab, values included, inside a Sheet
## card on the spread (TableCardKindRows.make_sheet_row). Boxes the last roll
## changed are outlined in the accent colour. Double-click opens the sheet.
var links: MainSheetLinks
var card: RollTable
var doc: SheetDoc
func setup(p_links: MainSheetLinks, p_card: RollTable) -> void:
links = p_links
card = p_card
name = "SheetPreview"
custom_minimum_size = Vector2(0, 180)
size_flags_horizontal = Control.SIZE_EXPAND_FILL
mouse_filter = Control.MOUSE_FILTER_PASS
gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.double_click and mb.button_index == MOUSE_BUTTON_LEFT:
links.open(card.id)
accept_event())
reload()
## Reads the sheet again and redraws.
func reload() -> void:
doc = links.load_doc(card.sheet_id)
tooltip_text = ("%s · double-click to open%s" % [doc.name, ("\nTables read it as @%s_…" % card.sheet_prefix) if card.sheet_prefix != "" else ""]) if doc else "Sheet not found"
queue_redraw()
func _draw() -> void:
var area := Rect2(Vector2.ZERO, size)
draw_rect(area, Color(Style.BG, 0.9))
if doc == null:
draw_string(Style.body(), Vector2(8, 20), "Sheet not found", HORIZONTAL_ALIGNMENT_LEFT, size.x - 16, 13, Style.ON_BG_MUTED)
return
var boxes: Array = doc.tabs[0]["boxes"]
if boxes.is_empty():
draw_string(Style.body(), Vector2(8, 20), "An empty sheet. Double-click to add fields.", HORIZONTAL_ALIGNMENT_LEFT, size.x - 16, 13, Style.ON_BG_MUTED)
return
area = area.grow(-6)
var bounds := Rect2((boxes[0] as SheetBox).rect())
for b: SheetBox in boxes:
bounds = bounds.merge(b.rect())
var k := minf(area.size.x / bounds.size.x, area.size.y / bounds.size.y)
var marks: Dictionary = links.recent.get(doc.id, {})
for b: SheetBox in boxes:
var r := Rect2(area.position + (b.rect().position - bounds.position) * k, b.rect().size * k).grow(-1)
var look := SheetBoxView.look(b.color, b.frameless)
if not b.frameless:
var marked := marks.has(b.id)
draw_style_box(Style.bordered(Style.box(look["bg"], 3), 2 if marked else 1, Style.ACCENT if marked else look["border"]), r)
var px := clampi(int(12 * k * 1.6), 7, 14)
if r.size.y > px * 1.2 and b.title != "":
draw_string(Style.body_bold(), r.position + Vector2(3, px), b.title, HORIZONTAL_ALIGNMENT_LEFT, r.size.x - 6, px, look["ink"])
var shown := _value_text(b)
if shown != "" and r.size.y > px * 2.4:
draw_string(Style.body(), r.position + Vector2(3, px * 2.2), shown, HORIZONTAL_ALIGNMENT_LEFT, r.size.x - 6, px, look["muted"])
func _value_text(b: SheetBox) -> String:
match b.kind:
SheetKinds.HEADING, SheetKinds.DIVIDER, SheetKinds.PICTURE, SheetKinds.DICE:
return ""
SheetKinds.TEXT:
return Formatter.strip_tags(links.expand(doc, str(b.value if b.value != null else ""))).replace("\n", " ").left(80)
return SheetText._value(doc, b)