167 lines
4.8 KiB
Lua
167 lines
4.8 KiB
Lua
UITool = UITool or {
|
||
---@type table<PlayerKey, UTexture2D> 头像
|
||
PlayerHeadPicture = {};
|
||
}
|
||
|
||
--- 设置
|
||
---@param widget UUserWidget
|
||
---@param offset float
|
||
function UITool.SetWidgetToRightBorder(widget, offset)
|
||
if not widget then
|
||
return
|
||
end
|
||
local slot = WidgetLayoutLibrary.SlotAsCanvasSlot(widget)
|
||
slot:SetAnchors({ Minimum = { X = 1, Y = 0 }, Maximum = { X = 1, Y = 0 } })
|
||
slot:SetAlignment({ X = 1, Y = 0 })
|
||
local offsets = slot:GetOffsets()
|
||
slot:SetOffsets({ Left = offset, Right = offsets.Right, Bottom = offsets.Bottom, Top = offsets.Top })
|
||
end
|
||
|
||
--- 为防止被清除做出的全局处理
|
||
function UITool.BindButtonClicked(TargetButton, Func, Obj)
|
||
TargetButton.OnClicked:Add(Func, Obj)
|
||
end
|
||
|
||
--- 为防止被清除做出的全局处理
|
||
function UITool.BindButtonPressed(TargetButton, Func, Obj)
|
||
TargetButton.OnPressed:Add(Func, Obj)
|
||
end
|
||
|
||
--- 直接绑定按键点击打开WidgetManager的页面
|
||
function UITool.ButtonOnClickShowPanel(TargetButton, UIType)
|
||
TargetButton.OnClicked:Add(
|
||
function()
|
||
WidgetManager:ShowPanel(UIType, false)
|
||
end
|
||
)
|
||
end
|
||
|
||
--- 绑定UI文本
|
||
function UITool.TextBlockBindingPropertyText(TargetTextBlock, Func, Obj)
|
||
TargetTextBlock:BindingProperty("Text", Func, Obj)
|
||
end
|
||
|
||
function UITool.TextBlockBindingProperty(TargetWidget, PropertyName, Func, Obj)
|
||
TargetWidget:BindingProperty(PropertyName, Func, Obj)
|
||
end
|
||
|
||
function UITool.SetBrushFromTexture(InImage, InIcon)
|
||
InImage:SetBrushFromTexture(InIcon);
|
||
end
|
||
|
||
---@field IsSucceedFun:fun(Callback:fun(Texture:UTexture2DDynamic),DelegateWrapper:UObject)
|
||
function UITool.DownloadImage(ImageURL, IsSucceedFun)
|
||
local ResHandle = AsyncTaskDownloadImage.DownloadImage(ImageURL)
|
||
if type(IsSucceedFun) == "function" then
|
||
ResHandle.OnSuccess:Add(IsSucceedFun)
|
||
ResHandle.OnFail:Add(IsSucceedFun)
|
||
end
|
||
end
|
||
|
||
--- 异步加载图片,成功后设置到UIImageObj,若该UIImageObj重复加载,则会判断设置时间决定是否设置
|
||
---@param UIImageObj UImage
|
||
---@param ResPath string
|
||
function UITool.AsyncLoadTextureToUIImage(UIImageObj, ResPath)
|
||
if UITool.ImageObjUpdateTime == nil then
|
||
-- {Obj = float, ...}
|
||
UITool.ImageObjUpdateTime = {}
|
||
end
|
||
|
||
local BeginLoadTime = KismetSystemLibrary.GetGameTimeInSeconds(UGCGameSystem.GameState)
|
||
local CallBackFunc = function(TextureObj)
|
||
if UE.IsValid(TextureObj) then
|
||
if UITool.ImageObjUpdateTime[UIImageObj] == nil
|
||
or UITool.ImageObjUpdateTime[UIImageObj] < BeginLoadTime
|
||
then
|
||
UITool.ImageObjUpdateTime[UIImageObj] = BeginLoadTime
|
||
UIImageObj:SetBrushFromTexture(TextureObj);
|
||
end
|
||
end
|
||
end
|
||
|
||
FileTool.AsyncLoadAsset(ResPath, CallBackFunc, nil, true)
|
||
end
|
||
|
||
function UITool.ButtonOnClickShowPanel(TargetButton, UIType)
|
||
TargetButton.OnClicked:Add(function()
|
||
WidgetManager:ShowPanel(UIType, false)
|
||
end)
|
||
end
|
||
|
||
-- 加载一个头像
|
||
function UITool.LoadPlayerHeadPicture(InPlayerKey, InUrl)
|
||
if InUrl ~= nil and UITool.PlayerHeadPicture[InPlayerKey] == nil then
|
||
UITool.DownloadImage(InUrl, function(Texture)
|
||
if UE.IsValid(Texture) then
|
||
UITool.PlayerHeadPicture[InPlayerKey] = Texture
|
||
else
|
||
print(string.format("[UGCPlayerState:OnRep_AccountInfo] Download Texture Failure. URL: %s", Url))
|
||
end
|
||
end)
|
||
end
|
||
end
|
||
|
||
--- 添加一个 继承自 PanelWidget 的控件的子类
|
||
---@param InBox UPanelWidget
|
||
---@param InClas UClass*
|
||
---@param InOwner UObject*
|
||
---@return UUserWidget*
|
||
function UITool.AddWidgetItem(InBox, InClas, InOwner)
|
||
if InOwner == nil then
|
||
InOwner = UGCGameSystem.GameState
|
||
end
|
||
local Item = UserWidget.NewWidgetObjectBP(InOwner, InClas);
|
||
InBox:AddChild(Item);
|
||
return Item;
|
||
end
|
||
|
||
-- PanelWidget 中从后面逐个隐藏 Item 的操作
|
||
function UITool.HideWidgetItem(InBox, InCount)
|
||
if InBox == nil then
|
||
return;
|
||
end
|
||
local Count = InBox:GetChildrenCount();
|
||
if InCount == nil or InCount > Count then
|
||
InCount = Count;
|
||
end
|
||
-- 从后往前逐个隐藏
|
||
for i = 1, InCount do
|
||
local Item = InBox:GetChildAt(Count - i);
|
||
Item:SetVisibility(ESlateVisibility.Collapsed);
|
||
end
|
||
end
|
||
|
||
UITool.ClearSpectateButtonTimer = nil;
|
||
|
||
-- 将观战页面退出按钮进行隐藏(否则点击到了直接退出就麻烦了)
|
||
function UITool.HideSpectate(InPC)
|
||
if not UE.IsValid(InPC) then
|
||
return;
|
||
end
|
||
|
||
self.ClearSpectateButtonTimer = UGCEventSystem.SetTimerLoop(InPC, function()
|
||
InPC:CastUIMsg("UIMsg_HideQuitWatch", "");
|
||
UGCEventSystem.SetTimer(self, function()
|
||
if UE.IsValid(self.ClearSpectateButtonTimer) then
|
||
UGCEventSystem.StopTimer(self.ClearSpectateButtonTimer);
|
||
end
|
||
end, 15)
|
||
end, 0.3)
|
||
end
|
||
|
||
--- 在滚动视图中的按钮可以被 handle 的方法
|
||
function UITool.EnableButtonScroll(InButton)
|
||
InButton:SetTouchMethod(EButtonTouchMethod.PreciseTap)
|
||
end
|
||
|
||
function UITool.ButtonSetEnable(InButton, IsEnable)
|
||
InButton:SetIsEnabled(IsEnable);
|
||
end
|
||
|
||
function UITool.GetChildrenCount(InBox)
|
||
return InBox:GetChildrenCount();
|
||
end
|
||
|
||
function UITool.GetChildAt(InBox, InIndex)
|
||
return InBox:GetChildAt(InIndex);
|
||
end |