TableTower/ui/modal_card.gd

278 lines
9.9 KiB
GDScript3
Raw Normal View History

class_name ModalCard
extends Control
## A card-styled modal dialog: dimmed backdrop, a paper-coloured card with a
## Rokkitt title, a scrolling body and a right-aligned footer of buttons.
## Add it to the app with `host.add_child(dialog)`; it frees itself on close().
signal closed
var card: PanelContainer
var body: VBoxContainer
var footer: HBoxContainer
var title_label: Label
var close_on_backdrop := true
## Fade and rise in when shown (roll pop-ups, unless animations are skipped).
var animate_in := false
var _scroll: ScrollContainer
var _width := 520.0
func _init(title: String = "", width: float = 520.0) -> void:
_width = width
set_anchors_and_offsets_preset(PRESET_FULL_RECT)
mouse_filter = Control.MOUSE_FILTER_STOP
theme = make_theme()
var dim := ColorRect.new()
dim.color = Color(0, 0, 0, 0.55)
dim.set_anchors_and_offsets_preset(PRESET_FULL_RECT)
dim.gui_input.connect(func(ev: InputEvent) -> void:
if close_on_backdrop and ev is InputEventMouseButton and ev.pressed:
close()
)
add_child(dim)
var center := CenterContainer.new()
center.set_anchors_and_offsets_preset(PRESET_FULL_RECT)
center.mouse_filter = Control.MOUSE_FILTER_IGNORE
add_child(center)
card = PanelContainer.new()
var sb := Style.box(Style.CARD, 14, [26, 22, 26, 20])
sb.shadow_color = Color(0, 0, 0, 0.5)
sb.shadow_size = 28
sb.shadow_offset = Vector2(0, 12)
card.add_theme_stylebox_override("panel", sb)
card.custom_minimum_size.x = width
center.add_child(card)
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 14)
card.add_child(col)
title_label = Style.make_label(title, Style.display(), 28, Style.INK)
title_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
col.add_child(title_label)
_scroll = ScrollContainer.new()
_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_scroll.size_flags_vertical = SIZE_EXPAND_FILL
col.add_child(_scroll)
body = VBoxContainer.new()
body.add_theme_constant_override("separation", 12)
body.size_flags_horizontal = SIZE_EXPAND_FILL
_scroll.add_child(body)
footer = HBoxContainer.new()
footer.add_theme_constant_override("separation", 10)
footer.alignment = BoxContainer.ALIGNMENT_END
col.add_child(footer)
func _ready() -> void:
resized.connect(_fit)
_fit()
if animate_in:
modulate.a = 0.0
await get_tree().process_frame
_fit()
if animate_in and is_inside_tree():
_play_entrance()
## The backdrop fades in while the card grows into place with a small overshoot.
## (Scale only: the card sits in a CenterContainer, which owns its position.)
func _play_entrance() -> void:
card.pivot_offset = card.size / 2.0
card.scale = Vector2(0.94, 0.94)
var tween := create_tween().set_parallel(true)
tween.tween_property(self, "modulate:a", 1.0, 0.2).set_ease(Tween.EASE_OUT)
tween.tween_property(card, "scale", Vector2.ONE, 0.3).set_trans(Tween.TRANS_BACK).set_ease(Tween.EASE_OUT)
func _fit() -> void:
card.custom_minimum_size.x = minf(_width, maxf(240.0, size.x - 32.0))
var want := body.get_combined_minimum_size().y
_scroll.custom_minimum_size.y = clampf(minf(want, size.y - 190.0), 40.0, 4000.0)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_cancel") and is_inside_tree():
get_viewport().set_input_as_handled()
close()
func close() -> void:
closed.emit()
queue_free()
# ---------- Building blocks ----------
## kind: "primary" (felt), "plain" (outlined) or "danger".
func add_button(text: String, kind: String, on_press: Callable) -> Button:
var b := make_button(text, kind)
b.pressed.connect(on_press)
footer.add_child(b)
return b
static func make_button(text: String, kind: String = "plain") -> Button:
var b := Button.new()
b.text = text
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 16)
var m := [16, 7, 16, 7]
match kind:
"primary":
Style.skin_button(b, Style.box(Style.BG, 8, m), Style.box(Style.BG_HOVER, 8, m),
Style.box(Style.BG_HOVER, 8, m), Style.ACCENT)
"danger":
Style.skin_button(b, Style.box(Style.DANGER_INK, 8, m), Style.box(Style.DANGER_INK.lightened(0.12), 8, m),
Style.box(Style.DANGER_INK.lightened(0.12), 8, m), Color.WHITE)
_:
var n := Style.bordered(Style.box(Color(0, 0, 0, 0), 8, m), 1, Style.DASH)
var h := Style.bordered(Style.box(Style.HOVER_ROW, 8, m), 1, Style.DASH)
Style.skin_button(b, n, h, h, Style.INK)
b.add_theme_color_override("font_disabled_color", Style.MUTED)
return b
## Small caps heading above a group of fields.
static func section(text: String) -> Label:
var l := Style.make_label(text.to_upper(), Style.caps(), 12, Style.MUTED)
return l
static func note(text: String, color: Color = Style.MUTED) -> Label:
var l := Style.make_label(text, Style.body(), 14, color)
l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
l.size_flags_horizontal = SIZE_EXPAND_FILL
return l
## A label above a control, as one block.
static func field(heading: String, control: Control) -> VBoxContainer:
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 4)
v.add_child(section(heading))
v.add_child(control)
return v
## A segmented choice: options are strings; returns the row. `on_change` gets the index.
static func segmented(options: Array, selected: int, on_change: Callable) -> PanelContainer:
var seg := PanelContainer.new()
seg.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.07), 9, [3, 3, 3, 3]))
seg.size_flags_horizontal = SIZE_SHRINK_BEGIN
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 0)
seg.add_child(row)
var group := ButtonGroup.new()
for i in options.size():
var b := Button.new()
b.text = str(options[i])
b.toggle_mode = true
b.button_group = group
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 15)
var normal := Style.box(Color(0, 0, 0, 0), 7, [14, 5, 14, 5])
var hover := Style.box(Color(0, 0, 0, 0.06), 7, [14, 5, 14, 5])
var pressed := Style.box(Style.BG, 7, [14, 5, 14, 5])
Style.skin_button(b, normal, hover, pressed, Style.MUTED, Style.INK, Style.ACCENT)
b.set_pressed_no_signal(i == selected)
b.pressed.connect(on_change.bind(i))
row.add_child(b)
return seg
static func style_line_edit(le: LineEdit) -> void:
le.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 7, [10, 6, 10, 6]), 1, Style.DASH))
le.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 7, [10, 6, 10, 6]), 1, Style.TRIM))
Style.field_colors(le)
le.add_theme_constant_override("minimum_character_width", 4)
le.size_flags_horizontal = SIZE_EXPAND_FILL
static func style_text_edit(te: TextEdit) -> void:
te.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 7, [10, 7, 10, 7]), 1, Style.DASH))
te.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 7, [10, 7, 10, 7]), 1, Style.TRIM))
Style.field_colors(te)
te.add_theme_color_override("background_color", Color(0, 0, 0, 0))
## Styles a popup menu like a small card (used by the top-bar menus too).
static func style_popup(pm: PopupMenu) -> void:
pm.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.CARD, 10, [6, 6, 6, 6]), 1, Style.DASH))
pm.add_theme_stylebox_override("hover", Style.box(Style.HOVER_ROW, 6))
pm.add_theme_stylebox_override("separator", Style.box(Style.RULE, 0, [0, 0, 0, 0]))
pm.add_theme_color_override("font_color", Style.INK)
pm.add_theme_color_override("font_hover_color", Style.INK)
pm.add_theme_color_override("font_disabled_color", Style.MUTED)
pm.add_theme_color_override("font_separator_color", Style.MUTED)
pm.add_theme_font_override("font", Style.body())
pm.add_theme_font_size_override("font_size", 16)
pm.add_theme_constant_override("v_separation", 8)
pm.add_theme_constant_override("item_start_padding", 8)
pm.add_theme_constant_override("item_end_padding", 8)
## A top-bar styled MenuButton (same look as Style.bar_button).
static func bar_menu_button(text: String) -> MenuButton:
var mb := Style.menu_button(text)
mb.add_theme_color_override("font_disabled_color", Style.ON_BG_MUTED)
style_popup(mb.get_popup())
return mb
static func make_theme() -> Theme:
var th := Style.card_theme()
th.set_color("default_color", "RichTextLabel", Style.INK)
th.set_font("normal_font", "RichTextLabel", Style.body())
th.set_font("bold_font", "RichTextLabel", Style.body_bold())
th.set_font("italics_font", "RichTextLabel", Style.body_italic())
th.set_font_size("normal_font_size", "RichTextLabel", 16)
th.set_font_size("bold_font_size", "RichTextLabel", 16)
th.set_font_size("italics_font_size", "RichTextLabel", 16)
th.set_color("font_color", "TooltipLabel", Style.INK)
th.set_stylebox("panel", "TooltipPanel", Style.box(Style.CARD, 6, [8, 5, 8, 5]))
return th
# ---------- Ready-made dialogs ----------
## Asks for one line of text; calls on_ok(text) with non-blank text.
## Asks for one line of text. `allow_empty` accepts an empty answer.
static func ask_text(title: String, prompt: String, initial: String, ok_text: String, on_ok: Callable, allow_empty: bool = false) -> ModalCard:
var d := ModalCard.new(title, 420.0)
var le := LineEdit.new()
le.text = initial
style_line_edit(le)
d.body.add_child(field(prompt, le))
var accept := func() -> void:
var t := le.text.strip_edges()
if t == "" and not allow_empty:
return
d.close()
on_ok.call(t)
le.text_submitted.connect(func(_t: String) -> void: accept.call())
d.add_button("Cancel", "plain", d.close)
d.add_button(ok_text, "primary", accept)
d.ready.connect(func() -> void:
le.grab_focus()
le.select_all()
)
return d
## A yes/no question; calls on_ok when confirmed. `danger` makes the button red.
static func confirm(title: String, message: String, ok_text: String, on_ok: Callable, danger: bool = false) -> ModalCard:
var d := ModalCard.new(title, 420.0)
d.body.add_child(note(message, Style.INK))
d.add_button("Cancel", "plain", d.close)
d.add_button(ok_text, "danger" if danger else "primary", func() -> void:
d.close()
on_ok.call()
)
return d