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

325 lines
13 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.BindButtonHovered(self, button)
return self.subsystem:BindButtonHovered(button)
end
function UEButtonDelegateBackend.prototype.BindButtonUnhovered(self, button)
return self.subsystem:BindButtonUnhovered(button)
end
function UEButtonDelegateBackend.prototype.BindCheckBoxChanged(self, checkBox)
return self.subsystem:BindCheckBoxChanged(checkBox)
end
function UEButtonDelegateBackend.prototype.BindSliderChanged(self, slider)
return self.subsystem:BindSliderChanged(slider)
end
function UEButtonDelegateBackend.prototype.BindSpinBoxChanged(self, spinBox)
return self.subsystem:BindSpinBoxChanged(spinBox)
end
function UEButtonDelegateBackend.prototype.BindEditableTextChanged(self, textBox)
return self.subsystem:BindEditableTextChanged(textBox)
end
function UEButtonDelegateBackend.prototype.BindEditableTextCommitted(self, textBox)
return self.subsystem:BindEditableTextCommitted(textBox)
end
function UEButtonDelegateBackend.prototype.BindComboBoxSelectionChanged(self, comboBox)
return self.subsystem:BindComboBoxSelectionChanged(comboBox)
end
function UEButtonDelegateBackend.prototype.UnbindButtonClicked(self, bindingId)
return self.subsystem:UnbindWidgetEvent(bindingId)
end
function UEButtonDelegateBackend.prototype.UnbindWidgetEvent(self, bindingId)
return self.subsystem:UnbindWidgetEvent(bindingId)
end
function UEButtonDelegateBackend.prototype.GetActiveBindingCount(self)
return self.subsystem:GetActiveBindingCount()
end
local WidgetBindingHandle = __TS__DefineClass("WidgetBindingHandle", __module)
WidgetBindingHandle.name = "WidgetBindingHandle"
function WidgetBindingHandle.prototype.____constructor(self, bridge, bindingId)
self.bridge = bridge
self.bindingId = bindingId
end
function WidgetBindingHandle.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.RegisterBinding(self, bindingId, callback, owner)
assert(bindingId > 0, "UIDelegateBridge: failed to bind UMG widget event")
self.bindings[bindingId] = {bindingId = bindingId, callback = callback, owner = owner}
local handle = __TS__New(WidgetBindingHandle, self, bindingId)
if owner ~= nil and __TS__InstanceOf(owner, ScopedObject) then
owner:AddScopedObject(handle)
end
return handle
end
function UIDelegateBridge.prototype.BindButtonClick(self, button, callback, owner)
local bindingId = self.backend:BindButtonClicked(button)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindButtonHovered(self, button, callback, owner)
assert(self.backend.BindButtonHovered ~= nil, "UIDelegateBridge backend does not support BindButtonHovered")
local bindingId = self.backend:BindButtonHovered(button)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindButtonUnhovered(self, button, callback, owner)
assert(self.backend.BindButtonUnhovered ~= nil, "UIDelegateBridge backend does not support BindButtonUnhovered")
local bindingId = self.backend:BindButtonUnhovered(button)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindCheckBoxChanged(self, checkBox, callback, owner)
assert(self.backend.BindCheckBoxChanged ~= nil, "UIDelegateBridge backend does not support BindCheckBoxChanged")
local bindingId = self.backend:BindCheckBoxChanged(checkBox)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindSliderChanged(self, slider, callback, owner)
assert(self.backend.BindSliderChanged ~= nil, "UIDelegateBridge backend does not support BindSliderChanged")
local bindingId = self.backend:BindSliderChanged(slider)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindSpinBoxChanged(self, spinBox, callback, owner)
assert(self.backend.BindSpinBoxChanged ~= nil, "UIDelegateBridge backend does not support BindSpinBoxChanged")
local bindingId = self.backend:BindSpinBoxChanged(spinBox)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindEditableTextChanged(self, textBox, callback, owner)
assert(self.backend.BindEditableTextChanged ~= nil, "UIDelegateBridge backend does not support BindEditableTextChanged")
local bindingId = self.backend:BindEditableTextChanged(textBox)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindEditableTextCommitted(self, textBox, callback, owner)
assert(self.backend.BindEditableTextCommitted ~= nil, "UIDelegateBridge backend does not support BindEditableTextCommitted")
local bindingId = self.backend:BindEditableTextCommitted(textBox)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.BindComboBoxSelectionChanged(self, comboBox, callback, owner)
assert(self.backend.BindComboBoxSelectionChanged ~= nil, "UIDelegateBridge backend does not support BindComboBoxSelectionChanged")
local bindingId = self.backend:BindComboBoxSelectionChanged(comboBox)
return self:RegisterBinding(bindingId, callback, owner)
end
function UIDelegateBridge.prototype.UnbindById(self, bindingId)
if self.bindings[bindingId] == nil then
return
end
self.bindings[bindingId] = nil
if self.backend.UnbindWidgetEvent ~= nil then
self.backend:UnbindWidgetEvent(bindingId)
else
self.backend:UnbindButtonClicked(bindingId)
end
end
function UIDelegateBridge.prototype.ResolveLiveRecord(self, bindingId)
if self:IsDisposed() then
return nil
end
local rec = self.bindings[bindingId]
if rec == nil then
return nil
end
if rec.owner ~= nil and rec.owner:IsDisposed() then
self:UnbindById(bindingId)
return nil
end
return rec
end
function UIDelegateBridge.prototype.DispatchButtonClicked(self, bindingId)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback)
end
end
function UIDelegateBridge.prototype.DispatchActionEvent(self, bindingId)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback)
end
end
function UIDelegateBridge.prototype.DispatchBoolChanged(self, bindingId, value)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback, value)
end
end
function UIDelegateBridge.prototype.DispatchFloatChanged(self, bindingId, value)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback, value)
end
end
function UIDelegateBridge.prototype.DispatchTextChanged(self, bindingId, text)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback, text)
end
end
function UIDelegateBridge.prototype.DispatchTextCommitted(self, bindingId, text, commitMethod)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback, text, commitMethod)
end
end
function UIDelegateBridge.prototype.DispatchSelectionChanged(self, bindingId, selectedItem, selectType)
local rec = self:ResolveLiveRecord(bindingId)
if rec ~= nil then
tbpcall(rec.callback, selectedItem, selectType)
end
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
---
-- @noSelf
function ____exports.OnNativeButtonClicked(gameInstance, bindingId)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchButtonClicked(bindingId)
end
end
---
-- @noSelf
function ____exports.OnNativeActionEvent(gameInstance, bindingId)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchActionEvent(bindingId)
end
end
---
-- @noSelf
function ____exports.OnNativeBoolChanged(gameInstance, bindingId, value)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchBoolChanged(bindingId, value)
end
end
---
-- @noSelf
function ____exports.OnNativeFloatChanged(gameInstance, bindingId, value)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchFloatChanged(bindingId, value)
end
end
---
-- @noSelf
function ____exports.OnNativeTextChanged(gameInstance, bindingId, text)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchTextChanged(bindingId, text)
end
end
---
-- @noSelf
function ____exports.OnNativeTextCommitted(gameInstance, bindingId, text, commitMethod)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchTextCommitted(bindingId, text, commitMethod)
end
end
---
-- @noSelf
function ____exports.OnNativeSelectionChanged(gameInstance, bindingId, selectedItem, selectType)
local bridge = bridgeByGameInstanceKey[GetGameInstanceKey(gameInstance)]
if bridge ~= nil then
bridge:DispatchSelectionChanged(bindingId, selectedItem, selectType)
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