UGCProjects/ProjectTemp_T/Script/UI/Tool/WB_SelectParts.lua
2025-01-04 23:00:19 +08:00

221 lines
6.4 KiB
Lua

---@class WB_SelectParts_C:UUserWidget
---@field HorizontalBox_Buttons UHorizontalBox
---@field WrapBox_Items UWrapBox
--Edit Below--
---@type WB_SelectParts_C
local WB_SelectParts = { bInitDoOnce = false; };
WB_SelectParts.WeaponPartTypes = {};
function WB_SelectParts:Construct()
self:LuaInit();
end
function WB_SelectParts:LuaInit()
if self.bInitDoOnce then return end
self.WrapBox_Items:SetVisibility(ESlateVisibility.Collapsed);
-- 设置第一个是无
UGCEventSystem.AddListener(EventTypes.PlayerChangeWeapon, self.OnPlayerChangeWeapon, self)
UITool.ForeachAllChildren(self.WrapBox_Items, function(index, Widget)
Widget:LuaInit();
Widget:Init(self, self.OnClickItem);
Widget:SetAsNone(index == 1);
Widget:SetSelect(false);
end);
UITool.ForeachAllChildren(self.HorizontalBox_Buttons, function(index, Widget)
Widget:SetSelect(false);
Widget:Init(self, self.OnClickPartItem);
end)
self.bInitDoOnce = true;
end
WB_SelectParts.AllPartItems = {};
function WB_SelectParts:SetType(PartType)
self.WeaponPartTypes = PartType;
UITool.AdaptChildren(self.HorizontalBox_Buttons, #PartType, ObjectPath.WB_SelectPartButton, function(Widget, index)
Widget:Init(self, self.OnClickPartItem);
end);
-- 看看有几个
for i = 1, #PartType do
local Item = self.HorizontalBox_Buttons:GetChildAt(i - 1);
Item:SetPartType(PartType[i]);
-- 进行加载
self.AllPartItems[PartType[i]] = Item;
end
end
function WB_SelectParts:OnClickOpen()
-- 检查当前武器
if self.WrapBox_Items:IsVisible() then
self.WrapBox_Items:SetVisibility(ESlateVisibility.Collapsed);
else
self.WrapBox_Items:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
end
end
function WB_SelectParts:GetCurrPartType()
if self.PartTypeItem then
return self.PartTypeItem:GetPartType();
end
return nil;
end
WB_SelectParts.CurrWeapon = nil;
-- 当玩家换武器的时候调用
function WB_SelectParts:OnPlayerChangeWeapon(InPawn, slot, InWeapon)
-- 检查当前用的是什么东西
local Pawn = UGCGameSystem.GetPlayerPawnByPlayerKey(LocalPlayerKey);
if InPawn ~= Pawn then return; end
self:HideAllChildren();
self:HideAllPartButton(true);
if InWeapon == nil or not UE.IsValid(InWeapon) then
InWeapon = UGCWeaponManagerSystem.GetCurrentWeapon(Pawn);
if InWeapon == nil then return; end
end
-- 检查当前有哪些槽位
local HasSame, SameTypes = self:CheckHasSomeType(InWeapon)
if not HasSame then
self:SetVisibility(ESlateVisibility.Collapsed);
return;
end
UGCLogSystem.LogTree(string.format("[WB_SelectParts:OnPlayerChangeWeapon] SameTypes ="), SameTypes)
self:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
for i, v in pairs(self.WeaponPartTypes) do
if SameTypes[v] == nil and self.AllPartItems[v] then
self.AllPartItems[v]:SetVisibility(ESlateVisibility.Collapsed);
else
self.AllPartItems[v]:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
end
end
self.CurrWeapon = InWeapon;
if self.WrapBox_Items:IsVisible() then
if self.PartTypeItem ~= nil then
self:OnUpdatePartList(self.CurrWeapon, self.PartTypeItem:GetPartType());
end
else
if self.PartTypeItem then
self.PartTypeItem:SetSelect(false);
self.PartTypeItem = nil;
end
end
end
---@param InWeapon ASTExtraWeapon
function WB_SelectParts:CheckHasSomeType(InWeapon)
if InWeapon == nil or not UE.IsValid(InWeapon) then return false; end
local ItemId = InWeapon:GetWeaponItemID()
local HaveSameTypes = false;
local SameTypes = {};
if table.isEmpty(WeaponSuits[ItemId]) then return false; end
for i, v in pairs(self.WeaponPartTypes) do
if not table.isEmpty(WeaponSuits[ItemId][v]) then SameTypes[v] = 1; end
end
return not table.isEmpty(SameTypes), SameTypes;
end
--- 更新列表
function WB_SelectParts:OnUpdatePartList(InWeapon, InPartType)
self:HideAllChildren();
if InWeapon == nil or not UE.IsValid(InWeapon) then return; end
local ItemId = InWeapon:GetWeaponItemID();
if ItemId == nil then return; end
UGCLogSystem.Log("[WB_SelectParts:OnPlayerChangeWeapon] ItemId = %d", ItemId);
-- 关闭所有
if table.isEmpty(WeaponSuits[ItemId]) then
return;
end
local List = WeaponSuits[ItemId][InPartType];
if table.isEmpty(List) then return; end
UITool.AdaptChildren(self.WrapBox_Items, table.getCount(List) + 1, ObjectPath.WB_SelectPartItem, function(Widget)
Widget:Init(self, self.OnClickItem);
end);
local PartId = self:CheckWeaponIdUsedPart(InWeapon, InPartType);
for i = 1, #List do
local Item = self.WrapBox_Items:GetChildAt(i);
Item:SetPartId(List[i]);
Item:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
if PartId == List[i] then
Item:SetSelect(true);
self.CurrItem = Item;
else
Item:SetSelect(false);
end
end
if PartId == nil then
self.WrapBox_Items:GetChildAt(0):SetSelect(true);
end
self.WrapBox_Items:GetChildAt(0):SetVisibility(ESlateVisibility.SelfHitTestInvisible);
end
-- 检查武器使用的是什么东西
function WB_SelectParts:CheckWeaponIdUsedPart(InWeapon, InPartType)
local Item = ItemTool.GetDefineIDBySocketType(InWeapon, ItemTool.GetWeaponAttachmentSocketType(InPartType));
if Item then return Item.TypeSpecificID; end
return nil;
end
WB_SelectParts.CurrItem = nil;
function WB_SelectParts:HideAllChildren()
UITool.HideAllChildren(self.WrapBox_Items);
if self.CurrItem ~= nil then
self.CurrItem:SetSelect(false);
self.CurrItem = nil;
end
end
function WB_SelectParts:HideAllPartButton(IsHide)
UITool.ForeachAllChildren(self.HorizontalBox_Buttons, function(index, Widget)
if IsHide then
Widget:SetVisibility(ESlateVisibility.Collapsed);
else
Widget:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
end
end)
end
function WB_SelectParts:OnClickItem(InItem)
if self.CurrItem ~= InItem then
if self.CurrItem then self.CurrItem:SetSelect(false); end
self.CurrItem = InItem;
self.CurrItem:SetSelect(true);
end
self:OnClickOpen();
end
WB_SelectParts.PartTypeItem = nil;
function WB_SelectParts:OnClickPartItem(InItem)
if self.PartTypeItem ~= InItem then
if self.PartTypeItem then self.PartTypeItem:SetSelect(false); end
UITool.ForeachAllChildren(self.WrapBox_Items, function(index, Widget)
Widget:ClearPartId();
end);
self.PartTypeItem = InItem;
self.PartTypeItem:SetSelect(true);
self:OnUpdatePartList(self.CurrWeapon, self.PartTypeItem:GetPartType());
self.WrapBox_Items:SetVisibility(ESlateVisibility.SelfHitTestInvisible);
else
self:OnClickOpen();
end
end
-- function WB_SelectParts:Tick(MyGeometry, InDeltaTime)
-- end
-- function WB_SelectParts:Destruct()
-- end
return WB_SelectParts;