class_name SheetStackView extends ScrollContainer ## The phone's Play view of a sheet tab (the "stacked view"): every box of ## the open tab in one full-width column, in phone order (order_of). Each box ## is the same SheetBoxView as on the canvas, built with touch-size buttons ## and ticks (SheetFieldViews.touch). SheetPhoneLayout swaps it with the canvas. const GAP := 10 const PAD := 12 var ed: SheetEditor var column: VBoxContainer var views: Dictionary = {} # box id -> SheetBoxView var _backdrop: BoardBackdrop func setup(p_editor: SheetEditor) -> void: ed = p_editor name = "StackView" horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED add_theme_stylebox_override("panel", StyleBoxEmpty.new()) _backdrop = BoardBackdrop.new(ThemeStore.current) draw.connect(func() -> void: _backdrop.draw(self, Rect2(Vector2.ZERO, size))) var pad := MarginContainer.new() pad.size_flags_horizontal = SIZE_EXPAND_FILL for side in ["left", "top", "right", "bottom"]: pad.add_theme_constant_override("margin_" + side, PAD) add_child(pad) column = VBoxContainer.new() column.add_theme_constant_override("separation", GAP) pad.add_child(column) ## Phone order: boxes with a Phone order number come first, smallest first; ## then the automatic ones (0) in reading order: top-down, then left-right. ## Equal numbers also fall back to reading order. static func order_of(boxes: Array) -> Array: var sorted := boxes.duplicate() sorted.sort_custom(func(first: SheetBox, second: SheetBox) -> bool: var first_rank := first.phone_order if first.phone_order > 0 else 1 << 30 var second_rank := second.phone_order if second.phone_order > 0 else 1 << 30 if first_rank != second_rank: return first_rank < second_rank if first.y != second.y: return first.y < second.y return first.x < second.x) return sorted func rebuild() -> void: for child in column.get_children(): column.remove_child(child) child.queue_free() views.clear() for box: SheetBox in order_of(ed.current_tab()["boxes"]): var view := _make_view(box) column.add_child(view) _fit_height(view) views[box.id] = view queue_redraw() ## Redraws every box but `keep_id` (the one being typed in), as the canvas does. func rebuild_except(keep_id: String) -> void: for id: String in views.keys(): if id == keep_id: continue var box := ed.doc.find_box(id) var old: SheetBoxView = views[id] if box == null: continue var view := _make_view(box) column.add_child(view) column.move_child(view, old.get_index()) column.remove_child(old) old.queue_free() _fit_height(view) views[id] = view ## A box view at full width, its buttons and ticks at touch size. func _make_view(box: SheetBox) -> SheetBoxView: var view := SheetBoxView.new() SheetFieldViews.touch = true view.setup(ed, box) SheetFieldViews.touch = false view.custom_minimum_size = Vector2(0, box.h) view.size_flags_horizontal = SIZE_EXPAND_FILL return view ## The box's own height, or taller when its bigger buttons need it. func _fit_height(view: SheetBoxView) -> void: var content := view.get_child(0) as Control view.custom_minimum_size.y = maxf(view.box.h, content.get_combined_minimum_size().y)