TableTower/ui/sheet_editor.gd

308 lines
8.9 KiB
GDScript3
Raw Normal View History

class_name SheetEditor
extends VBoxContainer
## One open sheet: the sheet top bar (SheetTopBar), the field palette on the
## left (FieldPalette), the tab row (SheetTabsBar) over the canvas
## (SheetCanvas) and, in Build, the inspector on the right (SheetInspector).
## It owns the sheet's Play/Build mode, the open tab, the selected box, undo
## (SnapshotHistory) and saving. Every change goes through edit() or
## edit_value(), so it can be undone and is saved soon after.
const SAVE_DELAY := 0.6
## Typing in the same field this close together is one undo step.
const MERGE_SECS := 1.5
var m: Main
var screen: SheetsScreen
var doc: SheetDoc
var history := SnapshotHistory.new()
var mode: Main.Mode = Main.Mode.PLAY
var tab_id := ""
var selected := ""
var top_bar: SheetTopBar
var palette_dock: PanelContainer
var palette: FieldPalette
var tabs_bar: SheetTabsBar
var canvas: SheetCanvas
var inspector: SheetInspector
## The side panel for a list entry's full details.
var details: SheetDetailPanel
var status_label: Label
var undo_label: Label
var _save_timer: Timer
var _dirty := false
var _merge_key := ""
var _merge_at := 0.0
func setup(p_main: Main, p_screen: SheetsScreen) -> void:
m = p_main
screen = p_screen
add_theme_constant_override("separation", 0)
top_bar = SheetTopBar.new()
top_bar.setup(self)
add_child(top_bar)
tabs_bar = SheetTabsBar.new()
tabs_bar.setup(self)
add_child(tabs_bar)
var middle := HBoxContainer.new()
middle.add_theme_constant_override("separation", 0)
middle.size_flags_vertical = SIZE_EXPAND_FILL
add_child(middle)
palette_dock = _make_palette_dock()
middle.add_child(palette_dock)
canvas = SheetCanvas.new()
canvas.setup(self)
canvas.size_flags_horizontal = SIZE_EXPAND_FILL
middle.add_child(canvas)
inspector = SheetInspector.new()
inspector.setup(self)
middle.add_child(inspector)
details = SheetDetailPanel.new()
details.setup(self)
middle.add_child(details)
add_child(_make_status_strip())
_save_timer = Timer.new()
_save_timer.one_shot = true
_save_timer.wait_time = SAVE_DELAY
_save_timer.timeout.connect(save_now)
add_child(_save_timer)
func _make_palette_dock() -> PanelContainer:
var dock := PanelContainer.new()
dock.custom_minimum_size.x = 232
var sb := Style.box(Style.BG_DEEP, 0, [12, 14, 12, 14])
sb.border_color = Color(1, 1, 1, 0.08)
sb.border_width_right = 1
dock.add_theme_stylebox_override("panel", sb)
palette = FieldPalette.new()
palette.setup(self)
dock.add_child(palette)
return dock
## The strip along the bottom: what Build is doing (grid, the selected box's
## place and size) or a Play hint on the left; the next undo step on the right.
func _make_status_strip() -> PanelContainer:
var strip := PanelContainer.new()
strip.add_theme_stylebox_override("panel", Style.box(Style.BG_DEEP.darkened(0.25), 0, [16, 6, 16, 6]))
var row := HBoxContainer.new()
strip.add_child(row)
status_label = Style.make_label("", Style.body(), 13, Style.ON_BG_MUTED)
status_label.size_flags_horizontal = SIZE_EXPAND_FILL
status_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
row.add_child(status_label)
undo_label = Style.make_label("", Style.body(), 13, Style.ON_BG_MUTED)
row.add_child(undo_label)
return strip
func update_status() -> void:
if doc == null:
return
if building():
var text := "Snap to %d px grid · hold Alt to place freely" % int(SheetKinds.GRID)
var box := doc.find_box(selected) if selected != "" else null
if box:
text += " · %s: X %d Y %d · W %d H %d" % [box.title if box.title != "" else "Box", box.x, box.y, box.w, box.h]
status_label.text = text
else:
status_label.text = "Play mode · type in the boxes to change values"
undo_label.text = "Undo: %s" % history.undo_label() if history.can_undo() else ""
func open(p_doc: SheetDoc) -> void:
doc = p_doc
history.clear()
_merge_key = ""
tab_id = str(doc.tabs[0]["id"])
selected = ""
canvas.recentre()
var empty := doc.tabs.all(func(t: Dictionary) -> bool: return (t["boxes"] as Array).is_empty())
set_mode(Main.Mode.BUILD if empty else Main.Mode.PLAY)
func close() -> void:
save_now()
doc = null
func save_now() -> void:
if doc and _dirty:
screen.store.save(doc)
_dirty = false
func mark_dirty() -> void:
_dirty = true
_save_timer.start()
func current_tab() -> Dictionary:
var tab := doc.get_tab(tab_id)
return tab if not tab.is_empty() else doc.tabs[0]
func building() -> bool:
return mode == Main.Mode.BUILD
func set_mode(new_mode: Main.Mode) -> void:
mode = new_mode
if not building():
selected = ""
refresh()
func set_palette_shown(on: bool) -> void:
palette_dock.visible = on
top_bar.sync()
func show_tab(id: String) -> void:
if id != tab_id:
canvas.recentre()
tab_id = id
selected = ""
refresh()
func select(box_id: String) -> void:
selected = box_id
canvas.paint_selection()
inspector.rebuild()
update_status()
## Rebuilds everything from the sheet (after undo, a mode switch, a new tab…).
func refresh() -> void:
if doc == null:
return
if doc.get_tab(tab_id).is_empty():
tab_id = str(doc.tabs[0]["id"])
if selected != "" and doc.find_box(selected) == null:
selected = ""
top_bar.sync()
tabs_bar.rebuild()
canvas.rebuild()
inspector.visible = building()
inspector.rebuild()
if details.box_id != "":
details.rebuild()
update_status()
## A change that can be undone: records the sheet, runs `change`, saves soon
## and redraws. A `merge_key` joins quick repeats (typing) into one step.
func edit(label: String, change: Callable, merge_key: String = "", redraw: bool = true) -> void:
var now := Time.get_ticks_msec() / 1000.0
var merge := merge_key != "" and merge_key == _merge_key and now - _merge_at < MERGE_SECS
if not merge:
history.record(doc.to_dict(), label)
_merge_key = merge_key
_merge_at = now
change.call()
mark_dirty()
if redraw:
refresh()
else:
top_bar.sync()
update_status()
## A player's value change from inside a box while typing: the box itself
## isn't redrawn (it keeps focus), the boxes that may show it are. Typing
## elsewhere (the details panel) passes keep_id "" so the box redraws too.
func edit_value(box: SheetBox, value: Variant, keep_id: String = "-") -> void:
edit("Change %s" % (box.title if box.title != "" else "a value"), func() -> void: box.value = value, "value:" + box.id, false)
canvas.rebuild_except(box.id if keep_id == "-" else keep_id)
## A value change from a click (a tick, a chip, + Add): one undo step, all redrawn.
func set_value(box: SheetBox, value: Variant) -> void:
edit("Change %s" % (box.title if box.title != "" else "a value"), func() -> void: box.value = value)
## A settings change from the inspector: one undo step per setting while
## typing; the boxes are redrawn but the inspector keeps focus.
func edit_config(box: SheetBox, label: String, change: Callable) -> void:
edit(label, change, "config:%s:%s" % [box.id, label], false)
canvas.rebuild_except("")
## Opens a list entry's details in the side panel.
func show_details(box_id: String, index: int) -> void:
details.open(box_id, index)
## Records `before` (taken at the start of a drag) when the drag changed something.
func record_from(before: Dictionary, label: String) -> void:
if before != doc.to_dict():
history.record(before, label)
_merge_key = ""
mark_dirty()
top_bar.sync()
update_status()
func undo() -> void:
_step(history.undo(doc.to_dict()), "Undid")
func redo() -> void:
_step(history.redo(doc.to_dict()), "Redid")
func _step(state: Dictionary, verb: String) -> void:
if state.is_empty():
return
var label := history.redo_label() if verb == "Undid" else history.undo_label()
var id := doc.id
doc = SheetDoc.from_dict(state)
doc.id = id
_merge_key = ""
mark_dirty()
refresh()
m.toast("%s: %s" % [verb, label])
## Adds a box from palette item (or kind) `kind` with its top-left at `at` on the open tab (INF = the
## middle of the visible canvas) and selects it.
func add_box(kind: String, at: Vector2 = Vector2.INF) -> SheetBox:
var spot := canvas.visible_center() - Vector2(96, 48) if at == Vector2.INF else at
var box := SheetKinds.make_box(kind, doc.new_box_id(), spot.max(Vector2.ZERO))
edit("Add %s" % SheetKinds.label_of(box), func() -> void: current_tab()["boxes"].append(box))
select(box.id)
return box
func delete_selected() -> void:
if selected == "":
return
var id := selected
selected = ""
edit("Delete box", func() -> void: doc.remove_box(id))
## Keys while the Sheets screen shows. True when the key was used.
func shortcut(k: InputEventKey) -> bool:
if doc == null or not visible:
return false
var ctrl := k.ctrl_pressed or k.meta_pressed
if ctrl and k.keycode == KEY_Z:
redo() if k.shift_pressed else undo()
return true
if ctrl and k.keycode == KEY_Y:
redo()
return true
var f := m.get_viewport().gui_get_focus_owner()
if f is LineEdit or f is TextEdit:
return false
if building() and k.keycode == KEY_DELETE and not k.echo:
delete_selected()
return true
if k.keycode == KEY_ESCAPE and selected != "":
select("")
return true
return false