From d85a6018cec88a8748930e48ecbfb962e579f604 Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Tue, 10 Nov 2026 18:32:09 -0600 Subject: [PATCH] Resizable cards: drag the corner grip for width and list height The grip in each card's bottom-right corner sets RollTable.width and list_height (saved as "w" and "list_h"); with a height the results list scrolls. Double-click the grip for the automatic size again. Co-Authored-By: Claude Opus 5.5 --- core/roll_table.gd | 10 ++++++ tests/test_card_size.gd | 52 +++++++++++++++++++++++++++++++ tests/test_card_size.gd.uid | 1 + ui/card_grip.gd | 61 +++++++++++++++++++++++++++++++++++++ ui/card_grip.gd.uid | 1 + ui/table_card.gd | 46 +++++++++++++++++++++++++--- 6 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 tests/test_card_size.gd create mode 100644 tests/test_card_size.gd.uid create mode 100644 ui/card_grip.gd create mode 100644 ui/card_grip.gd.uid diff --git a/core/roll_table.gd b/core/roll_table.gd index 4924720..a159f8c 100644 --- a/core/roll_table.gd +++ b/core/roll_table.gd @@ -51,6 +51,10 @@ var details: String = "" var tags: Array[String] = [] ## The card's own colour ("#hex") for its frame and header; "" = the theme's. var color: String = "" +## The size the user dragged the card to: its width in Play (Build adds a +## little), and the height of its results list, which then scrolls. 0 = automatic. +var width: float = 0.0 +var list_height: float = 0.0 ## Dice expression for ranged tables, e.g. "1d100", "2d6", "1d8+2", or any ## expression such as "2d6 + dm(@end)". var dice: String = DEFAULT_DICE @@ -425,6 +429,10 @@ func to_dict() -> Dictionary: d["tags"] = tags.duplicate() if color != "": d["color"] = color + if width > 0.0: + d["w"] = roundi(width) + if list_height > 0.0: + d["list_h"] = roundi(list_height) if is_ranged(): d["dice"] = dice if has_mod(): @@ -490,6 +498,8 @@ static func from_dict(d: Dictionary) -> RollTable: for tag: Variant in d.get("tags", []): t.tags.append(str(tag)) t.color = str(d.get("color", "")) + t.width = maxf(0.0, float(d.get("w", 0))) + t.list_height = maxf(0.0, float(d.get("list_h", 0))) if d.has("dice"): t.dice = str(d["dice"]) elif int(d.get("die", 0)) > 1: diff --git a/tests/test_card_size.gd b/tests/test_card_size.gd new file mode 100644 index 0000000..25a7a24 --- /dev/null +++ b/tests/test_card_size.gd @@ -0,0 +1,52 @@ +extends SceneTree +## Resizable cards: the grip's corner hit area, a dragged width and list +## height (the list scrolls), saving them, and double-click back to automatic. +## Run: godot --headless --path . -s res://tests/test_card_size.gd -- --sample + +var _failures := 0 +var main: Main + + +func _init() -> void: + if not OS.get_cmdline_user_args().has("--sample"): + print("Run with -- --sample so the test can't touch saved data.") + quit(2) + return + main = load("res://ui/main.tscn").instantiate() + root.add_child(main) + await _frames(5) + await _test_resize() + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _test_resize() -> void: + var t := main.data.get_table("road") + var card := main.card_for("road") + var grip := card.grip + _check(grip != null and grip.get_index() == card.get_child_count() - 1, "the grip is the card's last child") + _check(grip._has_point(grip.size - Vector2(4, 4)) and not grip._has_point(Vector2(4, 4)), "only the corner takes the mouse") + var auto_w := card.size.x + card.set_user_size(380.0, 120.0) + await _frames() + _check(is_equal_approx(card.size.x, 380.0 + card.extra_width()), "the card takes the dragged width") + _check(card.list_scroll != null and is_equal_approx(card.list_scroll.size.y, 120.0), "a set height makes the results scroll") + _check(card.grip.get_index() == card.get_child_count() - 1, "the grip stays on top after a rebuild") + var d := t.to_dict() + _check(d.get("w") == 380 and d.get("list_h") == 120, "the size is saved") + var back := RollTable.from_dict(d) + _check(back.width == 380.0 and back.list_height == 120.0, "and loaded") + card.set_user_size(0.0, 0.0) + await _frames() + _check(is_equal_approx(card.size.x, auto_w) and card.list_scroll == null, "back to automatic") + + +func _frames(n: int = 2) -> void: + for i in n: + await process_frame + + +func _check(ok: bool, what: String) -> void: + if not ok: + _failures += 1 + print("FAIL ", what) diff --git a/tests/test_card_size.gd.uid b/tests/test_card_size.gd.uid new file mode 100644 index 0000000..2feccd2 --- /dev/null +++ b/tests/test_card_size.gd.uid @@ -0,0 +1 @@ +uid://bny2cvqtj0pgr diff --git a/ui/card_grip.gd b/ui/card_grip.gd new file mode 100644 index 0000000..e59e077 --- /dev/null +++ b/ui/card_grip.gd @@ -0,0 +1,61 @@ +class_name CardGrip +extends Control +## The resize handle in a card's bottom-right corner. Dragging it sets the +## card's width and the height of its results list (RollTable.width, +## list_height; the list then scrolls). Double-click returns to automatic. +## +## The card's container lays it over the whole card, so _has_point limits the +## part that takes the mouse to the corner. It stays the card's last child. + +const HIT := 18.0 +const MIN_WIDTH := 200.0 +const MAX_WIDTH := 1200.0 +const MIN_LIST := 72.0 + +var card: TableCard +var _from := Vector2.ZERO +var _start_w := 0.0 +var _start_h := 0.0 +var _dragging := false + + +func _init(p_card: TableCard) -> void: + card = p_card + mouse_default_cursor_shape = CURSOR_FDIAGSIZE + tooltip_text = "Drag to resize the card. Double-click for its automatic size." + + +func _has_point(p: Vector2) -> bool: + return p.x >= size.x - HIT and p.y >= size.y - HIT + + +func _draw() -> void: + var c := Color(Style.MUTED, 0.7) + for i in 3: + var o := 4.0 + i * 4.0 + draw_line(Vector2(size.x - o, size.y - 2), Vector2(size.x - 2, size.y - o), c, 1.5, true) + + +func _gui_input(ev: InputEvent) -> void: + var mb := ev as InputEventMouseButton + if mb and mb.button_index == MOUSE_BUTTON_LEFT: + if mb.pressed and mb.double_click: + _dragging = false + card.set_user_size(0.0, 0.0) + card.main.mark_dirty() + elif mb.pressed: + card.main.undo_redo._begin_edit() + _dragging = true + _from = card.main.board.world_mouse() + _start_w = card.size.x - card.extra_width() + _start_h = card.list_area_height() + elif _dragging: + _dragging = false + card.main.mark_dirty() + accept_event() + return + var mm := ev as InputEventMouseMotion + if mm and _dragging: + var d := card.main.board.world_mouse() - _from + card.set_user_size(clampf(_start_w + d.x, MIN_WIDTH, MAX_WIDTH), maxf(MIN_LIST, _start_h + d.y)) + accept_event() diff --git a/ui/card_grip.gd.uid b/ui/card_grip.gd.uid new file mode 100644 index 0000000..fe60e3a --- /dev/null +++ b/ui/card_grip.gd.uid @@ -0,0 +1 @@ +uid://b1lodexd8ob16 diff --git a/ui/table_card.gd b/ui/table_card.gd index 263021e..ee68e05 100644 --- a/ui/table_card.gd +++ b/ui/table_card.gd @@ -51,6 +51,8 @@ var edit_ui: TableCardEditing var value_ui: TableCardValue ## The ornamental border (null when the theme has none). var frame: CardFrame +## The resize handle (CardGrip), kept as the last child. +var grip: CardGrip ## Rules in plain words (read from RulesView at each rebuild). var plain := true @@ -109,6 +111,8 @@ func setup(p_main: Main, p_table: RollTable) -> void: if Style.CARD_FRAME == "ornate": frame = CardFrame.new() add_child(frame) + grip = CardGrip.new(self) + add_child(grip) header_ui = TableCardHeader.new(self) rows_ui = TableCardRows.new(self) edit_ui = TableCardEditing.new(self) @@ -135,7 +139,7 @@ func _on_rules_view_changed(_v: Variant = null) -> void: func rebuild() -> void: for c in get_children(): - if c == frame: + if c == frame or c == grip: continue remove_child(c) c.queue_free() @@ -177,6 +181,7 @@ func rebuild() -> void: col.add_child(edit_ui.make_footer(ranged)) _apply_style() refresh_live() + move_child(grip, -1) return var pad := MarginContainer.new() @@ -185,10 +190,10 @@ func rebuild() -> void: pad.add_theme_constant_override("margin_bottom", 8) _list_box = vbox(0) pad.add_child(_list_box) - if not collapsed and table.entries.size() > LONG_LIST: + if not collapsed and (table.entries.size() > LONG_LIST or table.list_height > 0.0): list_scroll = ScrollContainer.new() list_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED - list_scroll.custom_minimum_size = Vector2(0, LIST_HEIGHT) + list_scroll.custom_minimum_size = Vector2(0, table.list_height if table.list_height > 0.0 else LIST_HEIGHT) list_scroll.mouse_filter = MOUSE_FILTER_PASS _skin_scrollbar(list_scroll.get_v_scroll_bar()) pad.size_flags_horizontal = SIZE_EXPAND_FILL @@ -223,6 +228,7 @@ func rebuild() -> void: col.add_child(edit_ui.make_footer(ranged)) _apply_style() refresh_live() + move_child(grip, -1) if list_scroll != null: for eid in rows: if main.is_entry_lit(table.id, eid): @@ -402,7 +408,39 @@ func _shrink() -> void: func card_width() -> float: - return BUILD_WIDTH if main.mode == Main.Mode.BUILD else WIDTH + return (table.width if table.width > 0.0 else WIDTH) + extra_width() + + +## Build mode has more controls per row, so cards are a little wider there. +func extra_width() -> float: + return BUILD_WIDTH - WIDTH if main.mode == Main.Mode.BUILD else 0.0 + + +## The height the results take now (the scrolling list's, if there is one). +func list_area_height() -> float: + if is_instance_valid(list_scroll): + return list_scroll.size.y + if is_instance_valid(_list_box) and _list_box.get_parent() is Control: + return (_list_box.get_parent() as Control).size.y + return 0.0 + + +## Sets the size the user dragged to (0 = automatic) and lays the card out +## again: cheaply while a scrolling list just changes height, else rebuilt. +func set_user_size(w: float, list_h: float) -> void: + if table.kind == RollTable.KIND_VARIABLE or is_collapsed(): + list_h = 0.0 + var rebuild_needed := (list_h > 0.0) != is_instance_valid(list_scroll) or (list_h == 0.0 and table.list_height > 0.0) + table.width = w + table.list_height = list_h + if rebuild_needed: + rebuild() + else: + custom_minimum_size.x = card_width() + if is_instance_valid(list_scroll) and list_h > 0.0: + list_scroll.custom_minimum_size.y = list_h + _shrink() + main.board.geometry_rev += 1 func set_rolling(on: bool) -> void: