130 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local BagGridWidget = {}
 | |
| local UIUtils = require("UI.Utils")
 | |
| local GamePlayUtils = require("GamePlay.Utils")
 | |
| local RoleUtils = require("GamePlay.Utils.RoleUtils")
 | |
| local BuildUtils = require("GamePlay.Utils.BuildUtils")
 | |
| local ESlateVisibility = import("ESlateVisibility")
 | |
| local BlueprintGameplayTagLibrary = import("BlueprintGameplayTagLibrary")
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| local function SetIsSelected(check_box, is_selected)
 | |
|     if is_selected == nil then
 | |
|         check_box:SetVisibility(ESlateVisibility.Collapsed)
 | |
|     else
 | |
|         check_box:SetVisibility(ESlateVisibility.Visible)
 | |
|         check_box:SetIsChecked(is_selected)
 | |
|     end
 | |
| end
 | |
| 
 | |
| local function SetItemAndCnt(entry, item_id, item_cnt)
 | |
|     if item_id ~= nil then
 | |
|         entry.Icon:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
 | |
|         local config = GamePlayUtils.GetItemDescConfig(item_id)
 | |
|         local x = slua.loadObject(config.IconResource:ToString())
 | |
|         entry.Icon:SetBrushResourceObject(x)
 | |
|     else
 | |
|         entry.Icon:SetVisibility(ESlateVisibility.Collapsed)
 | |
|     end
 | |
|     item_cnt = item_cnt or 0
 | |
|     if item_cnt > 0 then
 | |
|         entry.Icon:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
 | |
|         if item_cnt > 1 then
 | |
|             entry.TxtCnt:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
 | |
|         else
 | |
|             entry.TxtCnt:SetVisibility(ESlateVisibility.Collapsed)
 | |
|         end
 | |
|     else
 | |
|         entry.TxtCnt:SetVisibility(ESlateVisibility.Collapsed)
 | |
|         entry.Icon:SetVisibility(ESlateVisibility.Collapsed)
 | |
|     end
 | |
|     entry.item_id = item_id
 | |
| end
 | |
| 
 | |
| local function GetItemMenuContent(item, item_id)
 | |
|     local config = GamePlayUtils.GetItemConfigByID(item_id)
 | |
|     if config == nil then return {} end
 | |
| 
 | |
|     local contents = {}
 | |
| 
 | |
|     local consume_function = function()
 | |
|         local inventory = item.inventory
 | |
|         inventory:ConsumeItems(item.index, 1)
 | |
|         local grid = inventory:GetGridWithIndex(item.index)
 | |
|         item:SetData(grid)
 | |
|     end
 | |
|     local is_food = BlueprintGameplayTagLibrary.HasTag(
 | |
|         config.TypeTagContainer,
 | |
|         GamePlayUtils.GetGameplayTag("GameItem.Food"),
 | |
|         false
 | |
|     )
 | |
|     local is_building = BlueprintGameplayTagLibrary.HasTag(
 | |
|         config.TypeTagContainer,
 | |
|         GamePlayUtils.GetGameplayTag("GameItem.Building"),
 | |
|         false
 | |
|     )
 | |
|     if is_food then
 | |
|         table.insert(contents, {"使用", function()
 | |
|             consume_function()
 | |
|             RoleUtils.EatFood(item, item_id)
 | |
|         end})
 | |
|     end
 | |
|     if is_building then
 | |
|         table.insert(contents, {"建造", function()
 | |
|             if BuildUtils.Build(item, item_id) == true then
 | |
|                 consume_function()
 | |
|             end
 | |
|             -- RoleUtils.EatFood(item, item_id)
 | |
|         end})
 | |
|     end
 | |
|     table.insert(contents, {"丢弃", function()
 | |
|          consume_function()
 | |
|     end})
 | |
|     return contents
 | |
| end
 | |
| 
 | |
| 
 | |
| function BagGridWidget:OnInitialized()
 | |
|     self.BtnMain.OnClicked:Add(function() self:OnGridClicked() end)
 | |
| 
 | |
|     self.CheckBox.OnCheckStateChanged:Add(function(is_selected)
 | |
|         if self.item == nil then return end
 | |
|         self.item.selected = is_selected
 | |
|     end)
 | |
| end
 | |
| 
 | |
| function BagGridWidget:SetData(grid)
 | |
|     if grid == nil then grid = {} end
 | |
|     SetIsSelected(self.CheckBox, grid.selected)
 | |
|     SetItemAndCnt(self, grid.ItemID, grid.CurrentCount)
 | |
| end
 | |
| 
 | |
| function BagGridWidget:SetInventoryInfo(inventory, index)
 | |
|     self.index = index
 | |
|     self.inventory = inventory
 | |
| end
 | |
| 
 | |
| function BagGridWidget:OnDestroy()
 | |
| 
 | |
| end
 | |
| 
 | |
| function BagGridWidget:OnGridClicked()
 | |
|     if not self.item_id then return end
 | |
|     local FVector2D = import("Vector2D")
 | |
|     local SlateBlueprintLibrary = import("SlateBlueprintLibrary")
 | |
| 
 | |
|     local geometry = self:GetCachedGeometry()
 | |
|     local size = SlateBlueprintLibrary.GetLocalSize(geometry)
 | |
|     local center = FVector2D()
 | |
|     center.X, center.Y = size.X * 0.5, size.Y * 0.5
 | |
|     local _, viewport_pos = SlateBlueprintLibrary.LocalToViewport(self, geometry, center, nil, nil)
 | |
| 
 | |
|     UIUtils.ShowWidget(self, "MenuPanel", {
 | |
|         viewport_pos,
 | |
|         GetItemMenuContent(self, self.item_id)
 | |
|     })
 | |
| 
 | |
| end
 | |
| 
 | |
| return Class(nil, nil, BagGridWidget) |