72 lines
1.4 KiB
GDScript
72 lines
1.4 KiB
GDScript
class_name ABGScreenStack
|
|
extends RefCounted
|
|
|
|
const ScreenContextScript = preload("res://foundation/screen_context.gd")
|
|
|
|
signal changed
|
|
|
|
var _entries: Array = []
|
|
|
|
|
|
func push(context) -> Variant:
|
|
if context == null or String(context.screen_id).is_empty():
|
|
return null
|
|
_entries.append(context.duplicate_context())
|
|
changed.emit()
|
|
return current()
|
|
|
|
|
|
func replace(context) -> Variant:
|
|
if context == null or String(context.screen_id).is_empty():
|
|
return null
|
|
if _entries.is_empty():
|
|
return push(context)
|
|
_entries[_entries.size() - 1] = context.duplicate_context()
|
|
changed.emit()
|
|
return current()
|
|
|
|
|
|
func pop() -> Variant:
|
|
if _entries.is_empty():
|
|
return null
|
|
var removed = _entries.pop_back()
|
|
changed.emit()
|
|
return removed
|
|
|
|
|
|
func clear() -> void:
|
|
if _entries.is_empty():
|
|
return
|
|
_entries.clear()
|
|
changed.emit()
|
|
|
|
|
|
func current() -> Variant:
|
|
if _entries.is_empty():
|
|
return null
|
|
return _entries[_entries.size() - 1].duplicate_context()
|
|
|
|
|
|
func at(index: int) -> Variant:
|
|
if index < 0 or index >= _entries.size():
|
|
return null
|
|
return _entries[index].duplicate_context()
|
|
|
|
|
|
func contains(screen_id: String) -> bool:
|
|
for entry in _entries:
|
|
if String(entry.screen_id) == screen_id:
|
|
return true
|
|
return false
|
|
|
|
|
|
func depth() -> int:
|
|
return _entries.size()
|
|
|
|
|
|
func snapshot() -> Array[Dictionary]:
|
|
var result: Array[Dictionary] = []
|
|
for entry in _entries:
|
|
result.append(entry.to_dictionary())
|
|
return result
|