UITool = UITool or {} if UE_SERVER then return UITool; end --- 设置 ---@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 --- 遍历所有子项 ---@param InBox UPanelWidget ---@param InFunc fun(index: int32, Widget: UUserWidget) function UITool.ForeachAllChildren(InBox, InFunc) if type(InFunc) ~= 'function' then return ; end for i = 1, InBox:GetChildrenCount() do local Item = InBox:GetChildAt(i - 1); InFunc(i, Item); end end --- 隐藏所有子项 ---@param InBox UPanelWidget function UITool.HideAllChildren(InBox) UITool.ForeachAllChildren(InBox, function(index, Widget) Widget:SetVisibility(ESlateVisibility.Collapsed); end) end --- 自动适配数量 ---@param InBox UPanelWidget ---@param InCount int32 ---@param InClass UClass ---@param Func fun(Widget:UUserWidget, Index:int32) function UITool.AdaptChildren(InBox, InCount, InClass, Func, ...) local Count = UITool.GetChildrenCount(InBox) local TargetCount = InCount - Count; if TargetCount == 0 then elseif TargetCount < 0 then -- 说明当前多了 for i = Count, InCount + 1, -1 do local Item = InBox:GetChildAt(i - 1); Item:SetVisibility(ESlateVisibility.Collapsed); end else for i = Count + 1, InCount, 1 do UITool.AddWidgetItem(InBox, InClass, InBox); end end local Params = { ... }; UITool.ForeachAllChildren(InBox, function(index, Widget) if Func and type(Func) == 'function' then Func(Widget, index, table.unpackTable(Params)); end end) end ---@param Fmt string function UITool.ShowTips(Fmt, ...) UGCWidgetManagerSystem.ShowTipsUI(string.format(Fmt, ...)); 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) ---@field FailedFun fun() function UITool.DownloadImage(ImageURL, IsSucceedFun, FailedFun) local ResHandle = AsyncTaskDownloadImage.DownloadImage(ImageURL) if type(IsSucceedFun) == "function" then ResHandle.OnSuccess:Add(IsSucceedFun) if FailedFun ~= nil and type(FailedFun) == 'function' then ResHandle.OnFail:Add(FailedFun) end end end function UITool.ButtonOnClickShowPanel(TargetButton, UIType, IsShow) TargetButton.OnClicked:Add(function() if IsShow then WidgetManager:ShowPanel(UIType, false) else WidgetManager:ClosePanel(UIType) end end) end --- 自动适配数量 ---@param InBox UPanelWidget ---@param InCount int32 ---@param InClass UClass ---@param Func fun(Widget:UUserWidget, Index:int32) function UITool.AdaptChildren(InBox, InCount, InClass, Func, ...) local Count = UITool.GetChildrenCount(InBox) local TargetCount = InCount - Count; if TargetCount == 0 then elseif TargetCount < 0 then -- 说明当前多了 for i = Count, InCount + 1, -1 do local Item = InBox:GetChildAt(i - 1); Item:SetVisibility(ESlateVisibility.Collapsed); end else for i = Count + 1, InCount, 1 do UITool.AddWidgetItem(InBox, InClass, InBox); end end local Params = { ... }; UITool.ForeachAllChildren(InBox, function(index, Widget) if Func and type(Func) == 'function' then Func(Widget, index, table.unpackTable(Params)); 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 function UITool.SliderOnValueChanged(Slider, Func, Obj) Slider.OnValueChanged:Add(Func, Obj) 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(InPC, function() if UE.IsValid(self.ClearSpectateButtonTimer) then UGCEventSystem.StopTimer(self.ClearSpectateButtonTimer); end end, 10) 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