class_name UIFocusStack extends RefCounted signal activeLayerChanged(layer:ClosableMenu) const Z_STEP:int = 10 var _stack:Array[ClosableMenu] = [] func push(layer:ClosableMenu) -> void: if not _stack.is_empty(): _stack.back()._onFocusLost() _stack.push_back(layer) layer.z_index = _stack.size() * Z_STEP layer._onFocusGained() activeLayerChanged.emit(layer) func pop() -> void: if _stack.is_empty(): return var removed:ClosableMenu = _stack.pop_back() removed.z_index = 0 removed._onFocusLost() var next:ClosableMenu = top() if next != null: next._onFocusGained() activeLayerChanged.emit(next) func top() -> ClosableMenu: return _stack.back() if not _stack.is_empty() else null func isTop(layer:ClosableMenu) -> bool: return top() == layer