Files
UnrealLuaDemo/Script/lua/framework/ui/ui_delegate_bridge.lua
T

162 lines
5.8 KiB
Lua
Raw Normal View History

local ____lualib = require("lualib_bundle")
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__New = ____lualib.__TS__New
local __TS__InstanceOf = ____lualib.__TS__InstanceOf
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_base_traceback = __MODULE__.Import("share.base.traceback", __module)
local tbpcall = M__share_base_traceback.tbpcall
local M__framework_context_game_context = __MODULE__.Import("framework.context.game_context", __module)
local GetGameInstanceKey = M__framework_context_game_context.GetGameInstanceKey
local logger = Logger("UIDelegateBridge")
____exports.UEButtonDelegateBackend = __TS__DefineClass("UEButtonDelegateBackend", __module)
local UEButtonDelegateBackend = ____exports.UEButtonDelegateBackend
UEButtonDelegateBackend.name = "UEButtonDelegateBackend"
function UEButtonDelegateBackend.prototype.____constructor(self, subsystem)
self.subsystem = subsystem
end
function UEButtonDelegateBackend.prototype.BindButtonClicked(self, button)
return self.subsystem:BindButtonClicked(button)
end
function UEButtonDelegateBackend.prototype.UnbindButtonClicked(self, bindingId)
return self.subsystem:UnbindButtonClicked(bindingId)
end
function UEButtonDelegateBackend.prototype.GetActiveBindingCount(self)
return self.subsystem:GetActiveBindingCount()
end
local ButtonBindingHandle = __TS__DefineClass("ButtonBindingHandle", __module)
ButtonBindingHandle.name = "ButtonBindingHandle"
function ButtonBindingHandle.prototype.____constructor(self, bridge, bindingId)
self.bridge = bridge
self.bindingId = bindingId
end
function ButtonBindingHandle.prototype.Dispose(self)
if self.bridge ~= nil then
local b = self.bridge
self.bridge = nil
b:UnbindById(self.bindingId)
end
end
---
-- @module_data
local bridgeByGameInstanceKey = {}
____exports.UIDelegateBridge = __TS__DefineClass("UIDelegateBridge", __module)
local UIDelegateBridge = ____exports.UIDelegateBridge
UIDelegateBridge.name = "UIDelegateBridge"
__TS__ClassExtends(UIDelegateBridge, ScopedObject)
function UIDelegateBridge.prototype.____constructor(self, ctx, backend)
ScopedObject.prototype.____constructor(self)
self.bindings = {}
self.backend = backend
if ctx.gameInstance ~= nil then
self.giKey = GetGameInstanceKey(ctx.gameInstance)
bridgeByGameInstanceKey[self.giKey] = self
end
end
function UIDelegateBridge.prototype.BindButtonClick(self, button, callback, owner)
local bindingId = self.backend:BindButtonClicked(button)
assert(bindingId > 0, "UIDelegateBridge.BindButtonClick: failed to bind button")
self.bindings[bindingId] = {bindingId = bindingId, callback = callback, owner = owner}
local handle = __TS__New(ButtonBindingHandle, self, bindingId)
if owner ~= nil and __TS__InstanceOf(owner, ScopedObject) then
owner:AddScopedObject(handle)
end
return handle
end
function UIDelegateBridge.prototype.UnbindById(self, bindingId)
if self.bindings[bindingId] == nil then
return
end
self.bindings[bindingId] = nil
self.backend:UnbindButtonClicked(bindingId)
end
function UIDelegateBridge.prototype.DispatchButtonClicked(self, bindingId)
if self:IsDisposed() then
return
end
local rec = self.bindings[bindingId]
if rec == nil then
return
end
if rec.owner ~= nil and rec.owner:IsDisposed() then
self:UnbindById(bindingId)
return
end
tbpcall(rec.callback)
end
function UIDelegateBridge.prototype.GetActiveBindingCount(self)
return self.backend:GetActiveBindingCount()
end
function UIDelegateBridge.prototype.Dispose(self)
if self:IsDisposed() then
return
end
local ids = {}
for id in pairs(self.bindings) do
ids[#ids + 1] = id
end
do
local i = 0
while i < #ids do
self:UnbindById(ids[i + 1])
i = i + 1
end
end
if self.giKey ~= nil and bridgeByGameInstanceKey[self.giKey] == self then
bridgeByGameInstanceKey[self.giKey] = nil
end
ScopedObject.prototype.Dispose(self)
end
--- 供 C++ FLuaCallbackBridge::CallInt 回调的纯函数入口。
--
-- @noSelf
function ____exports.OnNativeButtonClicked(gameInstance, bindingId)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchButtonClicked(bindingId)
end
end
---
-- @noSelf
function ____exports.CreateUEUIDelegateBridge(ctx)
if ctx.gameInstance == nil then
return nil
end
local subsystem = UE.ULuaUIEventSubsystem.GetFromGameInstance(ctx.gameInstance)
if subsystem == nil then
logger:Error("ULuaUIEventSubsystem unavailable on GameInstance")
return nil
end
return __TS__New(
____exports.UIDelegateBridge,
ctx,
__TS__New(____exports.UEButtonDelegateBackend, subsystem)
)
end
function __module.StashData(env, to)
local ____has = to.__has__
if not ____has then
____has = {}
to.__has__ = ____has
end
to.bridgeByGameInstanceKey = bridgeByGameInstanceKey
____has.bridgeByGameInstanceKey = true
end
function __module.RestoreData(env, from)
local ____has = from.__has__
if not ____has then
return
end
if ____has.bridgeByGameInstanceKey then
bridgeByGameInstanceKey = from.bridgeByGameInstanceKey
end
end
function __module.UpdateImports()
Logger = M__share_base_logger.Logger
ScopedObject = M__share_base_scoped_object.ScopedObject
tbpcall = M__share_base_traceback.tbpcall
GetGameInstanceKey = M__framework_context_game_context.GetGameInstanceKey
end