TableTower/ui/main_rolling_copy.gd

47 lines
1.8 KiB
GDScript3
Raw Normal View History

class_name MainRollingCopy
extends RefCounted
## Turns a roll's result into text for the clipboard: plain text, BBCode, or
## text with each result's details. MainRolling uses it.
## Copy-as-text indents by depth up to this many levels.
const MAX_INDENT := 8
var m: Main
func _init(p_main: Main) -> void:
m = p_main
## Copies the current result: ResultTray.COPY_TEXT, COPY_BBCODE or COPY_DETAILS.
func copy_current(kind: int) -> void:
if m.current.is_empty():
return
var text := result_text(m.current.get("all", []), kind)
DisplayServer.clipboard_set(text)
m.toast(["Copied as text", "Copied as BBCode", "Copied with details"][clampi(kind, 0, 2)])
## One line per table rolled, indented by depth. Inline rolls are already part
## of their parent's text, so they don't get lines of their own.
func result_text(steps: Array, kind: int) -> String:
var lines := PackedStringArray()
for step: Dictionary in steps:
if step.get("inline", false):
continue
# Long pipe chains nest thousands deep; cap the indent so the text stays usable.
var indent := " ".repeat(mini(int(step.get("depth", 0)), MAX_INDENT))
var table_name := str(step.get("table", ""))
var text := str(step.get("text", ""))
if kind == ResultTray.COPY_BBCODE:
lines.append(indent + "[b]%s:[/b] %s" % [Formatter.to_export_bbcode(table_name), Formatter.to_export_bbcode(text)])
else:
lines.append(indent + "%s: %s" % [Formatter.strip_tags(table_name), Formatter.strip_tags(text)])
if kind == ResultTray.COPY_DETAILS:
var table := m.data.get_table(str(step.get("table_id", "")))
var entry := table.find_entry(str(step.get("entry_id", ""))) if table else null
if entry and entry.details.strip_edges() != "":
var pad := indent + " "
lines.append(pad + Formatter.strip_tags(entry.details).strip_edges().replace("\n", "\n" + pad))
return "\n".join(lines)