class_name SheetFieldViews extends RefCounted ## The inside of a sheet box, by kind (SheetKinds): what the player sees and ## changes in Play. This file has the basic kinds and shared helpers; the ## others live in SheetFieldsTrackers, SheetFieldsSmart, SheetFieldsList and ## SheetFieldsNiche. Changes go through SheetEditor.edit_value (typing) or ## set_value (clicks), so they can be undone. ## Touch targets at least this big (px) while `touch` is on. const TOUCH := 44.0 ## True while the stacked phone view (SheetStackView) builds a box: buttons ## and ticks get touch size (tap_size). Set and cleared around one build. static var touch := false ## `normal` grown to touch size while `touch` is on. static func tap_size(normal: Vector2) -> Vector2: return normal.max(Vector2(TOUCH, TOUCH)) if touch else normal static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control: var ink: Color = look["ink"] match box.kind: SheetKinds.HEADING: return _heading(box, ink) SheetKinds.NUMBER: return _number(ed, box, ink) SheetKinds.SHORT_TEXT: return _short_text(ed, box, ink) SheetKinds.CHECKBOX: return _checkbox(ed, box, ink) SheetKinds.DROPDOWN: return _dropdown(ed, box, ink) SheetKinds.DIVIDER: return _divider(ink) SheetKinds.PICTURE: return _picture(ed, box, look) SheetKinds.RESOURCE, SheetKinds.TICKS, SheetKinds.CLOCK, SheetKinds.DOTS, SheetKinds.STEP_DIE, SheetKinds.USAGE_DIE, SheetKinds.LADDER: return SheetFieldsTrackers.make(ed, box, look) SheetKinds.FORMULA, SheetKinds.DICE: return SheetFieldsSmart.make(ed, box, look) SheetKinds.LIST: return SheetFieldsList.make(ed, box, look) SheetKinds.SLOTS, SheetKinds.CONDITIONS, SheetKinds.SPELL_SLOTS, SheetKinds.XP, SheetKinds.CURRENCY: return SheetFieldsNiche.make(ed, box, look) return _text(ed, box, ink) static func _heading(box: SheetBox, ink: Color) -> Control: var l := Style.make_label(box.title, Style.display(), 30, ink) l.vertical_alignment = VERTICAL_ALIGNMENT_CENTER l.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS return l static func _divider(ink: Color) -> Control: var c := Control.new() c.draw.connect(func() -> void: c.draw_line(Vector2(0, c.size.y / 2.0), Vector2(c.size.x, c.size.y / 2.0), Color(ink, 0.5), 2.0)) return c ## A number with − and + beside it; type to set it. static func _number(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var row := HBoxContainer.new() row.alignment = BoxContainer.ALIGNMENT_CENTER row.add_theme_constant_override("separation", 4) var value := LineEdit.new() value.name = "Value" value.text = SheetValues.show(SheetValues.number(box.value)) value.alignment = HORIZONTAL_ALIGNMENT_CENTER value.size_flags_horizontal = Control.SIZE_EXPAND_FILL value.custom_minimum_size.x = 24 value.add_theme_constant_override("minimum_character_width", 1) value.flat = true value.add_theme_font_override("font", Style.display()) value.add_theme_font_size_override("font_size", 26) value.add_theme_color_override("font_color", ink) var commit := func(text: String) -> void: var n: Variant = clamp_number(box, SheetValues.number(parse_number(text, box.value))) value.text = SheetValues.show(n) if SheetValues.number(n) != SheetValues.number(box.value): ed.edit_value(box, n) value.text_submitted.connect(commit) value.focus_exited.connect(func() -> void: commit.call(value.text)) for step: int in [-1, 1]: var b := step_button("−" if step < 0 else "+", ink) b.name = "Minus" if step < 0 else "Plus" b.pressed.connect(func() -> void: var n: Variant = clamp_number(box, SheetValues.number(box.value) + step) value.text = SheetValues.show(n) ed.edit_value(box, n)) row.add_child(b) if step < 0: row.add_child(value) return row ## Keeps a number inside the box's min and max settings (when set). static func clamp_number(box: SheetBox, n: float) -> Variant: if str(box.config.get("min", "")) != "": n = maxf(n, SheetValues.number(box.config["min"])) if str(box.config.get("max", "")) != "": n = minf(n, SheetValues.number(box.config["max"])) return int(n) if n == floorf(n) else n static func _short_text(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var e := LineEdit.new() e.name = "Value" e.text = str(box.value if box.value != null else "") e.placeholder_text = "Type here" e.flat = true e.size_flags_vertical = Control.SIZE_SHRINK_CENTER e.add_theme_font_size_override("font_size", 17) e.add_theme_color_override("font_color", ink) e.add_theme_color_override("font_placeholder_color", Color(ink, 0.4)) e.text_changed.connect(func(text: String) -> void: ed.edit_value(box, text)) return e static func _checkbox(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var c := CheckBox.new() c.name = "Value" c.text = "Yes" if bool(box.value) else "No" c.button_pressed = bool(box.value) for key in ["font_color", "font_hover_color", "font_pressed_color", "font_hover_pressed_color"]: c.add_theme_color_override(key, ink) c.toggled.connect(func(on: bool) -> void: c.text = "Yes" if on else "No" ed.edit_value(box, on)) return c static func _dropdown(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: var o := OptionButton.new() o.name = "Value" var options: Array = box.config.get("options", []) for i in options.size(): o.add_item(str(options[i])) if str(options[i]) == str(box.value): o.select(i) o.size_flags_vertical = Control.SIZE_SHRINK_CENTER o.add_theme_color_override("font_color", ink) o.item_selected.connect(func(i: int) -> void: ed.edit_value(box, str(options[i]))) return o ## The picture from its file, or a button to pick one (Play). static func _picture(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control: var path := str(box.value if box.value != null else "") var tex: Texture2D = null if path != "" and FileAccess.file_exists(path): var img := Image.load_from_file(path) if img: tex = ImageTexture.create_from_image(img) if tex: var tr := TextureRect.new() tr.texture = tex tr.expand_mode = TextureRect.EXPAND_IGNORE_SIZE tr.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED tr.tooltip_text = "Double-click to change the picture" tr.gui_input.connect(func(ev: InputEvent) -> void: var mb := ev as InputEventMouseButton if mb and mb.double_click and not ed.building(): pick_picture(ed, box)) return tr var b := step_button("Choose a picture…", look["ink"]) b.name = "Choose" b.pressed.connect(pick_picture.bind(ed, box)) return b static func pick_picture(ed: SheetEditor, box: SheetBox) -> void: ed.m.layout.file_dialog("Choose a picture", ["*.png, *.jpg, *.jpeg, *.webp ; Pictures"], FileDialog.FILE_MODE_OPEN_FILE, func(path: String) -> void: var kept := ed.screen.store.keep_picture(path) if kept == "": ed.m.toast("Couldn't read that picture") else: ed.set_value(box, kept)) ## Long notes: formatted text, edited with a toolbar (SheetTextBox). static func _text(ed: SheetEditor, box: SheetBox, ink: Color) -> Control: return SheetTextBox.make(ed, box, ink) ## Live values: {@name} shows the value of the box used in tables as @name. static func expand(ed: SheetEditor, text: String) -> String: return ed.m.sheet_links.expand(ed.doc, text) static func parse_number(text: String, fallback: Variant) -> Variant: var t := text.strip_edges() if t.is_valid_int(): return t.to_int() if t.is_valid_float(): return t.to_float() return SheetValues.number(fallback) ## A small button drawn in the box's own ink colour. Pass `button` to skin another ## kind of button (a MenuButton). static func step_button(text: String, ink: Color, button: Button = null) -> Button: if button == null: button = Button.new() button.text = text button.custom_minimum_size = tap_size(Vector2(26, 26)) button.size_flags_vertical = Control.SIZE_SHRINK_CENTER button.focus_mode = Control.FOCUS_NONE button.add_theme_font_override("font", Style.body_bold()) button.add_theme_font_size_override("font_size", 15) var normal := Style.bordered(Style.box(Color(ink, 0.04), 6, [6, 0, 6, 0]), 1, Color(ink, 0.35)) var hover := Style.bordered(Style.box(Color(ink, 0.12), 6, [6, 0, 6, 0]), 1, Color(ink, 0.6)) Style.skin_button(button, normal, hover, hover, ink, ink, ink) return button ## A muted label in the box's ink. static func note(text: String, ink: Color, size: int = 13) -> Label: var l := Style.make_label(text, Style.body(), size, Color(ink, 0.7)) l.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART return l