From 2fe9317b2733582e0b8ab687f786b07f9e603762 Mon Sep 17 00:00:00 2001 From: RobinFiredrake Date: Tue, 10 Nov 2026 18:58:33 -0600 Subject: [PATCH] Phone layout and touch gestures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MobileLayout (Android, or --mobile to try it on a desktop): the List fills the screen with a Spread/List swap; the bar drops the title, hint, zoom, View, Help and Play/Build (the last three move into ☰); the tray hides its Earlier column and shortens Roll again; DPI scaling. TouchInput (every platform): a long press is a right-click, two fingers pan and pinch-zoom. Co-Authored-By: Claude Opus 5.5 --- tests/test_mobile.gd | 79 ++++++++++++++++++++++++++++++++++++ tests/test_mobile.gd.uid | 1 + ui/actions.gd | 5 +++ ui/board_view.gd | 8 ++++ ui/list_view.gd | 4 +- ui/main.gd | 6 +++ ui/main_top_bar.gd | 3 ++ ui/mobile_layout.gd | 51 +++++++++++++++++++++++ ui/mobile_layout.gd.uid | 1 + ui/result_tray.gd | 7 +++- ui/touch_input.gd | 87 ++++++++++++++++++++++++++++++++++++++++ ui/touch_input.gd.uid | 1 + 12 files changed, 251 insertions(+), 2 deletions(-) create mode 100644 tests/test_mobile.gd create mode 100644 tests/test_mobile.gd.uid create mode 100644 ui/mobile_layout.gd create mode 100644 ui/mobile_layout.gd.uid create mode 100644 ui/touch_input.gd create mode 100644 ui/touch_input.gd.uid diff --git a/tests/test_mobile.gd b/tests/test_mobile.gd new file mode 100644 index 0000000..b209099 --- /dev/null +++ b/tests/test_mobile.gd @@ -0,0 +1,79 @@ +extends SceneTree +## The phone layout (List fills the screen, Spread/List swap, slim bar and +## tray, View/Help/Play-Build in ☰) and touch gestures (pinch zoom, a long +## press timing out into a right-click). +## Run: godot --headless --path . -s res://tests/test_mobile.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_layout() + await _test_touch() + print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures]) + quit(1 if _failures else 0) + + +func _test_layout() -> void: + main.mobile = MobileLayout.new(main) + main.mobile.apply() + await _frames() + _check(main.list_view.visible and not main.board.visible, "the list fills the screen") + _check(not main.top_bar.view_menu.visible and not main.top_bar.help_menu.visible and not main.tray.side_panel.visible, "the bar and tray slim down") + main.mobile.swap.pressed.emit() + _check(main.board.visible and not main.list_view.visible and main.mobile.swap.text == "List", "Spread/List swaps to the spread") + main.mobile.swap.pressed.emit() + var texts: Array = Actions.background(main, Vector2.ZERO).map(func(a: Dictionary) -> String: return str(a.get("text", ""))) + _check(texts.has("Switch to Build") and texts.has("Help…"), "☰ carries Play/Build and Help: %s" % str(texts)) + main.mobile.show_list(false) + + +func _test_touch() -> void: + var touch: TouchInput = null + for c in main.get_children(): + if c is TouchInput: + touch = c + _check(touch != null, "touch gestures are on") + var z := main.board.zoom + touch._input(_tap(0, Vector2(100, 300), true)) + touch._input(_tap(1, Vector2(200, 300), true)) + var d := InputEventScreenDrag.new() + d.index = 1 + d.position = Vector2(300, 300) + touch._input(d) + _check(main.board.zoom > z * 1.5, "two fingers spreading apart zoom in (%.2f → %.2f)" % [z, main.board.zoom]) + touch._input(_tap(1, Vector2(300, 300), false)) + touch._input(_tap(0, Vector2(100, 300), false)) + touch._input(_tap(0, Vector2(120, 320), true)) + touch._process(0.3) + _check(touch._hold_t > 0.0, "a held finger counts toward a long press") + touch._process(0.3) + _check(touch._hold_t < 0.0, "after half a second it fires (as a right-click)") + touch._input(_tap(0, Vector2(120, 320), false)) + + +func _tap(i: int, at: Vector2, down: bool) -> InputEventScreenTouch: + var t := InputEventScreenTouch.new() + t.index = i + t.position = at + t.pressed = down + return t + + +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_mobile.gd.uid b/tests/test_mobile.gd.uid new file mode 100644 index 0000000..f9bd803 --- /dev/null +++ b/tests/test_mobile.gd.uid @@ -0,0 +1 @@ +uid://dgu87qrdhygdr diff --git a/ui/actions.gd b/ui/actions.gd index c9ccd9e..a639309 100644 --- a/ui/actions.gd +++ b/ui/actions.gd @@ -168,6 +168,11 @@ static func background(m: Main, at: Vector2) -> Array: "tip": "The cards as a list with folders"}) list.append({"sep": ""}) list.append({"text": "Theme…", "do": func() -> void: m.add_child(ThemeDialog.new(m))}) + if m.mobile: + # The phone layout has no room for these on the bar. + var build := m.mode == Main.Mode.BUILD + list.append({"text": "Switch to Play" if build else "Switch to Build", "do": m.set_mode.bind(Main.Mode.PLAY if build else Main.Mode.BUILD)}) + list.append({"text": "Help…", "do": func() -> void: m.top_bar.help_menu.open("")}) return list diff --git a/ui/board_view.gd b/ui/board_view.gd index 7461b0b..b03ce73 100644 --- a/ui/board_view.gd +++ b/ui/board_view.gd @@ -208,6 +208,14 @@ static func pattern_texture(kind: String) -> ImageTexture: return null +## Drops a half-made selection box or pan (a second finger landed: TouchInput). +func cancel_drag() -> void: + _banding = false + _panning = false + _press_at = Vector2(-1, -1) + overlay.queue_redraw() + + ## Shows or hides every card's and collapsed group's CardShield. func set_compact(on: bool) -> void: compact = on diff --git a/ui/list_view.gd b/ui/list_view.gd index b18303b..88035f9 100644 --- a/ui/list_view.gd +++ b/ui/list_view.gd @@ -13,6 +13,7 @@ var m: Main var tree: ListTree var splitter: PanelSplitter var _search: LineEdit +var close_button: Button var _rev := -1 @@ -32,7 +33,8 @@ func setup(p_main: Main) -> void: folder.tooltip_text = "A new folder (a group on the spread). Drag cards onto it." folder.pressed.connect(new_folder) head.add_child(folder) - var close := Style.bar_button("×") + close_button = Style.bar_button("×") + var close := close_button close.tooltip_text = "Close the list" close.pressed.connect(func() -> void: set_shown(false)) head.add_child(close) diff --git a/ui/main.gd b/ui/main.gd index 20a4cf2..870762a 100644 --- a/ui/main.gd +++ b/ui/main.gd @@ -45,6 +45,8 @@ var selection: Dictionary = {} var selecting: MainSelection ## The List view panel (left dock). var list_view: ListView +## The phone layout, when it is on (MobileLayout.wanted). +var mobile: MobileLayout var _save_timer: Timer var _saving_enabled := true @@ -116,6 +118,10 @@ func _ready() -> void: data.name = "Fantasy Loot" _build_ui() + add_child(TouchInput.new(self)) + if MobileLayout.wanted(): + mobile = MobileLayout.new(self) + mobile.apply() _rebuild_cards() rolling._seed_session() # Let the cards lay out before fitting them to the window. diff --git a/ui/main_top_bar.gd b/ui/main_top_bar.gd index 685042f..4509ec9 100644 --- a/ui/main_top_bar.gd +++ b/ui/main_top_bar.gd @@ -14,6 +14,8 @@ var _switcher: CollectionSwitcher var _board_menu: IoMenu ## The bar's actions menu (Actions.background), like right-clicking the spread. var actions_menu: MenuButton +## The name beside the logo (hidden on phones). +var brand_title: Label var view_menu: ViewMenu var help_menu: HelpMenu var zoom: ZoomControl @@ -67,6 +69,7 @@ func _build_top_bar() -> Control: if mark: brand.add_child(mark) var title := Style.make_label("Table Tarot", Style.display(), 23, Style.ON_BG) + brand_title = title title.size_flags_vertical = Control.SIZE_SHRINK_CENTER brand.add_child(title) h.add_child(brand) diff --git a/ui/mobile_layout.gd b/ui/mobile_layout.gd new file mode 100644 index 0000000..f488723 --- /dev/null +++ b/ui/mobile_layout.gd @@ -0,0 +1,51 @@ +class_name MobileLayout +extends RefCounted +## The phone layout (Android, or --mobile on a desktop to try it): the List +## fills the screen and a Spread/List button swaps it with the spread; the +## top bar drops its hint and zoom control; everything scales to the screen's +## density. Touch gestures come from TouchInput (added on every platform, so +## touch screens on laptops work too). + +var m: Main +var showing_list := true +var swap: Button + + +func _init(p_main: Main) -> void: + m = p_main + + +static func wanted() -> bool: + return OS.has_feature("mobile") or OS.get_cmdline_user_args().has("--mobile") + + +func apply() -> void: + if OS.has_feature("mobile"): + m.get_window().content_scale_factor = clampf(DisplayServer.screen_get_dpi() / 160.0, 1.0, 3.0) + # Room is short: View, Help and Play/Build move into the ☰ menu + # (Actions.background adds them when the phone layout is on). + var bar := m.top_bar + for c: Control in [bar._hint, bar.zoom, bar.view_menu, bar.help_menu, bar.brand_title, bar._play_button.get_parent().get_parent()]: + if c: + c.visible = false + m.tray.side_panel.visible = false + m.tray.compact = true + m.list_view.close_button.visible = false + m.list_view.splitter.visible = false + m.list_view.size_flags_horizontal = Control.SIZE_EXPAND_FILL + m.board.minimap.visible = false + swap = Style.bar_button("Spread") + swap.tooltip_text = "Swap between the list and the spread" + swap.pressed.connect(func() -> void: show_list(not showing_list)) + bar.actions_menu.get_parent().add_child(swap) + bar.actions_menu.get_parent().move_child(swap, bar.actions_menu.get_index()) + show_list(true) + + +## The list or the spread fills the screen. +func show_list(on: bool) -> void: + showing_list = on + m.list_view.visible = on + m.list_view._rev = -1 + m.board.visible = not on + swap.text = "Spread" if on else "List" diff --git a/ui/mobile_layout.gd.uid b/ui/mobile_layout.gd.uid new file mode 100644 index 0000000..f830705 --- /dev/null +++ b/ui/mobile_layout.gd.uid @@ -0,0 +1 @@ +uid://50k70t2tcw6l diff --git a/ui/result_tray.gd b/ui/result_tray.gd index 256bada..e9bfab4 100644 --- a/ui/result_tray.gd +++ b/ui/result_tray.gd @@ -33,6 +33,10 @@ var _copy: MenuButton var _flow: HFlowContainer var _empty: Label var _earlier: EarlierRolls +## Earlier / Variables on the right (hidden in the phone layout). +var side_panel: Control +## Phone layout: shorter labels. +var compact := false var _vars: VariablesPanel var _tab_earlier: Button var _tab_vars: Button @@ -147,6 +151,7 @@ func setup(p_main: Main) -> void: now.add_child(_empty) var right := VBoxContainer.new() + side_panel = right right.add_theme_constant_override("separation", 8) right.custom_minimum_size = Vector2(340, 0) right.size_flags_vertical = SIZE_EXPAND_FILL @@ -253,7 +258,7 @@ func render() -> void: else: var root_name := Formatter.strip_tags(str(current["root_name"])) _title.text = ("Result · %s" % root_name).to_upper() - _again.text = "Roll %s again" % root_name + _again.text = "Roll again" if compact else "Roll %s again" % root_name _again.visible = main.data.get_table(current["root"]) != null var done: bool = current.get("done", false) _copy.visible = done diff --git a/ui/touch_input.gd b/ui/touch_input.gd new file mode 100644 index 0000000..3268692 --- /dev/null +++ b/ui/touch_input.gd @@ -0,0 +1,87 @@ +class_name TouchInput +extends Node +## Touch gestures for phones and tablets: a long press acts as a right-click +## (the same menus as on a desktop), and two fingers pan and pinch-zoom the +## spread. One finger still taps and drags as the mouse does (Godot turns +## touches into mouse events). + +const HOLD := 0.5 +const SLOP := 12.0 + +var m: Main +var _touches: Dictionary = {} +var _hold_at := Vector2.ZERO +var _hold_t := -1.0 +var _pinch_dist := 0.0 +var _pinch_mid := Vector2.ZERO + + +func _init(p_main: Main) -> void: + m = p_main + + +func _input(ev: InputEvent) -> void: + var t := ev as InputEventScreenTouch + if t: + if t.pressed: + _touches[t.index] = t.position + else: + _touches.erase(t.index) + _hold_t = 0.0 if t.pressed and _touches.size() == 1 else -1.0 + _hold_at = t.position + if _touches.size() == 2: + _start_pinch() + return + var d := ev as InputEventScreenDrag + if d: + _touches[d.index] = d.position + if _hold_t >= 0.0 and d.position.distance_to(_hold_at) > SLOP: + _hold_t = -1.0 + if _touches.size() == 2: + _pinch() + m.get_viewport().set_input_as_handled() + + +func _process(delta: float) -> void: + if _hold_t < 0.0: + return + _hold_t += delta + if _hold_t >= HOLD: + _hold_t = -1.0 + right_click(_hold_at) + + +## Sends a right-click at a screen position, as a long press does. +static func right_click(at: Vector2) -> void: + var e := InputEventMouseButton.new() + e.button_index = MOUSE_BUTTON_RIGHT + e.pressed = true + e.position = at + e.global_position = at + Input.parse_input_event(e) + var up := e.duplicate() as InputEventMouseButton + up.pressed = false + Input.parse_input_event(up) + + +func _two() -> Array: + return _touches.values().slice(0, 2) + + +func _start_pinch() -> void: + var p := _two() + _pinch_dist = maxf(1.0, (p[0] as Vector2).distance_to(p[1])) + _pinch_mid = ((p[0] as Vector2) + (p[1] as Vector2)) / 2.0 + _hold_t = -1.0 + m.board.cancel_drag() + + +func _pinch() -> void: + var p := _two() + var dist := maxf(1.0, (p[0] as Vector2).distance_to(p[1])) + var mid := ((p[0] as Vector2) + (p[1] as Vector2)) / 2.0 + var local := m.board.get_global_transform_with_canvas().affine_inverse() * mid + m.board.world.position += mid - _pinch_mid + m.board.set_zoom(m.board.zoom * dist / _pinch_dist, local) + _pinch_dist = dist + _pinch_mid = mid diff --git a/ui/touch_input.gd.uid b/ui/touch_input.gd.uid new file mode 100644 index 0000000..e69b75e --- /dev/null +++ b/ui/touch_input.gd.uid @@ -0,0 +1 @@ +uid://c0oql7pq3vhfj