TableTower/ui/demo_mode.gd

87 lines
2.5 KiB
GDScript3
Raw Normal View History

class_name DemoMode
extends RefCounted
## A demo spread from Help (DemoSpreads): shown in place of your spread,
## never saved and never listed. A bar above the spread offers Keep a copy
## (saves it as a spread of your own) and Back to my spread.
var m: Main
var _return_id := ""
var _was_saving := false
var _bar: PanelContainer
func _init(p_main: Main) -> void:
m = p_main
func active() -> bool:
return _bar != null and is_instance_valid(_bar)
func open(key: String) -> void:
if not DemoSpreads.has(key):
return
if not active():
m._save_now()
_return_id = m.data.id
_was_saving = m._saving_enabled
m._saving_enabled = false
m.collections._show_collection(DemoSpreads.build(key))
_show_bar(str(DemoSpreads.TITLES[key]))
func back() -> void:
_close_bar()
m._saving_enabled = _was_saving
var b: TowerBoard = null
if m.library and _return_id != "":
b = m.library.load_collection(_return_id)
if b == null:
b = SampleBoard.create()
b.id = "fantasy-loot"
b.name = "Fantasy Loot"
m.collections._show_collection(b)
## Saves the demo as a new spread of your own and leaves demo mode on it.
func keep_copy() -> void:
_close_bar()
m._saving_enabled = _was_saving
if m.library == null:
m.toast("Spreads can't be saved in this session")
return
var copy := TowerBoard.new()
copy.load_dict(m.data.to_dict())
copy.name = str(m.data.name).trim_prefix("Demo: ").capitalize()
copy.id = m.library.unique_id(LibraryStore.slugify(copy.name))
if m.library.save(copy):
m.collections._show_collection(copy)
m.toast("Saved “%s” as your own spread" % copy.name)
func _show_bar(title: String) -> void:
_close_bar()
_bar = PanelContainer.new()
_bar.add_theme_stylebox_override("panel", Style.bordered(Style.box(Style.BG_DEEP, 10, [14, 8, 10, 8]), 1, Style.ACCENT))
var h := HBoxContainer.new()
h.add_theme_constant_override("separation", 10)
_bar.add_child(h)
var l := Style.make_label("%s · try it out; nothing here is saved" % title, Style.body_bold(), 15, Style.ON_BG)
l.size_flags_vertical = Control.SIZE_SHRINK_CENTER
h.add_child(l)
var keep := Style.bar_button("Keep a copy")
keep.pressed.connect(keep_copy)
h.add_child(keep)
var back_b := Style.bar_button("Back to my spread", true)
back_b.pressed.connect(back)
h.add_child(back_b)
m.board.add_child(_bar)
# Along the bottom of the spread, clear of the cards at the top.
_bar.set_anchors_and_offsets_preset(Control.PRESET_BOTTOM_LEFT, Control.PRESET_MODE_MINSIZE, 14)
func _close_bar() -> void:
if active():
_bar.queue_free()
_bar = null