local ____lualib = require("lualib_bundle") local __TS__ClassExtends = ____lualib.__TS__ClassExtends local __TS__New = ____lualib.__TS__New local ____exports = __module.env local M__share_base_logger = __MODULE__.Import("share.base.logger", __module) local Logger = M__share_base_logger.Logger local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module) local ScopedObject = M__share_base_scoped_object.ScopedObject local M__share_event_event_hub = __MODULE__.Import("share.event.event_hub", __module) local EventHub = M__share_event_event_hub.EventHub local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module) local tbpcall = M__share_base_traceback.tbpcall local M__framework_ui_ui_config = __MODULE__.Import("framework.ui.ui_config", __module) local GetUIConfig = M__framework_ui_ui_config.GetUIConfig local UI_LAYER_ZORDER = M__framework_ui_ui_config.UI_LAYER_ZORDER local logger = Logger("UIManager") ____exports.UEWidgetAdapter = __TS__DefineClass("UEWidgetAdapter", __module) local UEWidgetAdapter = ____exports.UEWidgetAdapter UEWidgetAdapter.name = "UEWidgetAdapter" function UEWidgetAdapter.prototype.____constructor(self) end function UEWidgetAdapter.prototype.FindChild(self, widget, name) return UE.ULuaUIEventSubsystem.FindChildWidgetByName(widget, name) end function UEWidgetAdapter.prototype.AddToViewport(self, widget, zOrder) if widget.IsInViewport ~= nil and widget:IsInViewport() then return end local hasPlayer = widget.GetOwningPlayer ~= nil and widget:GetOwningPlayer() ~= nil if hasPlayer and widget.AddToPlayerScreen ~= nil then widget:AddToPlayerScreen(zOrder) elseif widget.AddToViewport ~= nil then widget:AddToViewport(zOrder) end end function UEWidgetAdapter.prototype.RemoveFromParent(self, widget) if widget.RemoveFromParent ~= nil then widget:RemoveFromParent() end end function UEWidgetAdapter.prototype.SetVisible(self, widget, visible) if widget.SetVisibility ~= nil then widget:SetVisibility(visible and UE.ESlateVisibility.Visible or UE.ESlateVisibility.Collapsed) end end function UEWidgetAdapter.prototype.IsWidgetValid(self, widget) return widget ~= nil and UE.UKismetSystemLibrary.IsValid(widget) end ____exports.UEWidgetFactory = __TS__DefineClass("UEWidgetFactory", __module) local UEWidgetFactory = ____exports.UEWidgetFactory UEWidgetFactory.name = "UEWidgetFactory" function UEWidgetFactory.prototype.____constructor(self) end function UEWidgetFactory.prototype.CreateWidget(self, widgetClass, worldContext, owningPlayer) if worldContext == nil then return nil end return UE.UWidgetBlueprintLibrary.Create(worldContext, widgetClass, owningPlayer) end ____exports.UIManager = __TS__DefineClass("UIManager", __module) local UIManager = ____exports.UIManager UIManager.name = "UIManager" __TS__ClassExtends(UIManager, ScopedObject) function UIManager.prototype.____constructor(self, ctx, playerCtx, resourceService, widgetFactory, widgetAdapter) ScopedObject.prototype.____constructor(self) self.windowStack = {} self.singletonByName = {} self.cachedByName = {} self.zOrderCounter = 0 self.inputMode = "game" self.ctx = ctx self.playerCtx = playerCtx self.resourceService = resourceService self.widgetFactory = widgetFactory or __TS__New(____exports.UEWidgetFactory) self.widgetAdapter = widgetAdapter or __TS__New(____exports.UEWidgetAdapter) self.events = __TS__New( EventHub, string.format("UIManager#%d_P%d", ctx.contextId, playerCtx.playerIndex) ) ctx.appEvents.on.OnWorldCleanup( function() self:CloseAll(true) end, self ) end function UIManager.prototype.ShowUI(self, ctor, args, onOpened) if self:IsDisposed() or self.ctx:IsShuttingDown() then if onOpened ~= nil then tbpcall(onOpened, false, nil, "UIManager is shutting down") end return nil end local config = GetUIConfig(ctor) if config == nil then error("UIManager.ShowUI: window class is not registered via RegisterUIConfig") return nil end if config.singleton then local existing = self.singletonByName[config.name] if existing ~= nil and not existing:IsDisposed() and not existing:IsClosing() then if existing:IsLoaded() then local zOrder = self:AllocateZOrder(config) existing:ShowInternal(zOrder, args) self:SyncFocusAndInputMode() if onOpened ~= nil then tbpcall(onOpened, true, existing, nil) end elseif onOpened ~= nil then self.events.on.OnWindowOpened( function(openedName, openedWin) if openedName == config.name and openedWin == existing then tbpcall(onOpened, true, openedWin, nil) end end, existing ) end return existing end end local cached = self.cachedByName[config.name] if cached ~= nil and not cached:IsDisposed() then self.cachedByName[config.name] = nil if config.singleton then self.singletonByName[config.name] = cached end self.windowStack[#self.windowStack + 1] = cached local zOrder = self:AllocateZOrder(config) cached:ReuseInternal(zOrder, args) self:SyncFocusAndInputMode() self.events.emit.OnWindowOpened(config.name, cached) if onOpened ~= nil then tbpcall(onOpened, true, cached, nil) end return cached end local win = __TS__New(ctor) win:InitContextInternal(self.ctx, self, config, self.widgetAdapter) if config.singleton then self.singletonByName[config.name] = win end self.windowStack[#self.windowStack + 1] = win local loadHandle = self.resourceService:LoadClassAsync( config.assetKey, win, function(ok, widgetClass, _lease, errMsg) if win:IsDisposed() or win:IsClosing() or self:IsDisposed() then return end if not ok or widgetClass == nil then logger:Error("Failed to load widget class", {window = config.name, assetKey = config.assetKey, err = errMsg}) self:RemoveFromStackAndSingleton(win) win:SetLoadingStateInternal(false, nil) win:Dispose() if onOpened ~= nil then tbpcall(onOpened, false, nil, errMsg or "Widget class load failed") end return end local worldContext = self.ctx:GetWorldContext().world or self.playerCtx.playerController or self.ctx.gameInstance local widget = self.widgetFactory:CreateWidget(widgetClass, worldContext, self.playerCtx.playerController) if widget == nil then local createErr = string.format("[%s] Failed to instantiate UUserWidget", config.name) logger:Error(createErr) self:RemoveFromStackAndSingleton(win) win:SetLoadingStateInternal(false, nil) win:Dispose() if onOpened ~= nil then tbpcall(onOpened, false, nil, createErr) end return end win:AttachWidgetInternal(widget) local zOrder = self:AllocateZOrder(config) win:ShowInternal(zOrder, args) self:SyncFocusAndInputMode() self.events.emit.OnWindowOpened(config.name, win) if onOpened ~= nil then tbpcall(onOpened, true, win, nil) end end ) win:SetLoadingStateInternal(true, loadHandle) return win end function UIManager.prototype.GetSingletonUI(self, ctor) local config = GetUIConfig(ctor) if config == nil then return nil end return self.singletonByName[config.name] end function UIManager.prototype.CloseSingletonUI(self, ctor) local win = self:GetSingletonUI(ctor) if win ~= nil then win:Close() end end function UIManager.prototype.GetTopWindow(self) do local i = #self.windowStack - 1 while i >= 0 do local win = self.windowStack[i + 1] if not win:IsDisposed() and not win:IsClosing() then return win end i = i - 1 end end return nil end function UIManager.prototype.HandleWindowCachedInternal(self, win) self:RemoveFromStackAndSingleton(win) local oldCached = self.cachedByName[win.config.name] if oldCached ~= nil and oldCached ~= win then oldCached:ForceDisposeFromCacheInternal() end self.cachedByName[win.config.name] = win self:SyncFocusAndInputMode() self.events.emit.OnWindowClosed(win.config.name, win) end function UIManager.prototype.HandleWindowClosedInternal(self, win) self:RemoveFromStackAndSingleton(win) self:SyncFocusAndInputMode() self.events.emit.OnWindowClosed(win.config.name, win) end function UIManager.prototype.EvictCache(self) local toEvict = {} for name, win in pairs(self.cachedByName) do if win ~= nil then self.cachedByName[name] = nil toEvict[#toEvict + 1] = win end end do local i = 0 while i < #toEvict do toEvict[i + 1]:ForceDisposeFromCacheInternal() i = i + 1 end end end function UIManager.prototype.CloseAll(self, alsoEvictCache) if alsoEvictCache == nil then alsoEvictCache = true end local copy = {} do local i = #self.windowStack - 1 while i >= 0 do copy[#copy + 1] = self.windowStack[i + 1] i = i - 1 end end do local i = 0 while i < #copy do copy[i + 1]:Close() i = i + 1 end end if alsoEvictCache then self:EvictCache() end self:SyncFocusAndInputMode() end function UIManager.prototype.GetDiagnostics(self) local openWindows = 0 local loadingWindows = 0 do local i = 0 while i < #self.windowStack do local w = self.windowStack[i + 1] if not w:IsDisposed() then if w:IsLoading() then loadingWindows = loadingWindows + 1 else openWindows = openWindows + 1 end end i = i + 1 end end local cachedWindows = 0 for _, w in pairs(self.cachedByName) do if w ~= nil and not w:IsDisposed() then cachedWindows = cachedWindows + 1 end end local top = self:GetTopWindow() return { openWindows = openWindows, loadingWindows = loadingWindows, cachedWindows = cachedWindows, topWindowName = top ~= nil and top.config.name or nil, inputMode = self.inputMode } end function UIManager.prototype.AllocateZOrder(self, config) self.zOrderCounter = self.zOrderCounter + 1 return UI_LAYER_ZORDER[config.layer] + self.zOrderCounter % 90 end function UIManager.prototype.RemoveFromStackAndSingleton(self, win) if self.singletonByName[win.config.name] == win then self.singletonByName[win.config.name] = nil end do local i = #self.windowStack - 1 while i >= 0 do if self.windowStack[i + 1] == win then table.remove(self.windowStack, i + 1) break end i = i - 1 end end end function UIManager.prototype.SyncFocusAndInputMode(self) local top = self:GetTopWindow() local nextMode = top ~= nil and top.config.blockWorldClick and "ui" or "game" self.inputMode = nextMode local pc = self.playerCtx.playerController if pc ~= nil and UE ~= nil and UE.UKismetSystemLibrary.IsValid(pc) then if nextMode == "ui" then pc.bShowMouseCursor = true local ____UE_UWidgetBlueprintLibrary_SetInputMode_UIOnlyEx_1 = UE.UWidgetBlueprintLibrary.SetInputMode_UIOnlyEx local ____temp_0 if top ~= nil then ____temp_0 = top:GetWidget() else ____temp_0 = nil end ____UE_UWidgetBlueprintLibrary_SetInputMode_UIOnlyEx_1(pc, ____temp_0, UE.EMouseLockMode.DoNotLock, false) else pc.bShowMouseCursor = false UE.UWidgetBlueprintLibrary.SetInputMode_GameOnly(pc, false) end end self.events.emit.OnFocusRestored(top ~= nil and top.config.name or nil) end function UIManager.prototype.Dispose(self) if self:IsDisposed() then return end self:CloseAll(true) self.events:Dispose() ScopedObject.prototype.Dispose(self) end function __module.UpdateImports() Logger = M__share_base_logger.Logger ScopedObject = M__share_base_scoped_object.ScopedObject EventHub = M__share_event_event_hub.EventHub tbpcall = M__share_base_traceback.tbpcall GetUIConfig = M__framework_ui_ui_config.GetUIConfig UI_LAYER_ZORDER = M__framework_ui_ui_config.UI_LAYER_ZORDER end