Refactoring and working drag n drop

This commit is contained in:
ZioPao
2023-11-06 14:44:35 +01:00
parent 0ef3f7284f
commit 28ba7d73ab
5 changed files with 21 additions and 5 deletions

View File

@@ -0,0 +1,63 @@
local BaseHandler = require("UI/TOC_HealthPanelBaseHandler")
local CutLimbAction = require("TimedActions/TOC_CutLimbAction")
---@class CutLimbHandler
local CutLimbHandler = BaseHandler:derive("CutLimbHandler")
local contextMenuCutLimb = "Cut"
---Creates new CutLimbHandler
---@param panel any
---@param bodyPart any
---@return CutLimbHandler
function CutLimbHandler:new(panel, bodyPart)
local o = BaseHandler.new(self, panel, bodyPart)
o.items.ITEMS = {}
return o
end
function CutLimbHandler:checkItem(item)
local itemType = item:getType()
if itemType == "Saw" or itemType == "GardenSaw" or itemType == "Chainsaw" then
self:addItem(self.items.ITEMS, item)
end
end
function CutLimbHandler:addToMenu(context)
local types = self:getAllItemTypes(self.items.ITEMS)
if #types > 0 then
local option = context:addOption(contextMenuCutLimb, nil)
local subMenu = context:getNew(context)
context:addSubMenu(option, subMenu)
for i=1,#types do
local item = self:getItemOfType(self.items.ITEMS, types[i])
subMenu:addOption(item:getName(), self, self.onMenuOptionSelected, item:getFullType())
end
end
end
function CutLimbHandler:dropItems(items)
local types = self:getAllItemTypes(items)
if #self.items.ITEMS > 0 and #types == 1 then
self:onMenuOptionSelected(types[1])
return true
end
return false
end
function CutLimbHandler:isValid(itemType)
return self:getItemOfType(self.items.ITEMS, itemType)
end
function CutLimbHandler:perform(previousAction, itemType)
print("perform cutlimbhandler")
local item = self:getItemOfType(self.items.ITEMS, itemType)
previousAction = self:toPlayerInventory(item, previousAction)
local action = CutLimbAction:new(self:getPatient(), self:getDoctor(), self.bodyPart)
ISTimedActionQueue.addAfter(previousAction, action)
end
return CutLimbHandler

View File

@@ -0,0 +1,56 @@
local CutLimbHandler = require("UI/TOC_CutLimbHandler")
-- TODO Use this to replace the sprites once a limb is cut
ISHealthBodyPartPanel = ISBodyPartPanel:derive("ISHealthBodyPartPanel")
function ISHealthBodyPartPanel:onMouseUp(x, y)
if self.selectedBp then
local dragging = ISInventoryPane.getActualItems(ISMouseDrag.dragging)
self.parent:dropItemsOnBodyPart(self.selectedBp.bodyPart, dragging)
end
end
function ISHealthBodyPartPanel:prerender()
self.nodeAlpha = 0.0
self.selectedAlpha = 0.1
if self.selectedBp then
for index,item in ipairs(self.parent.listbox.items) do
if item.item.bodyPart == self.selectedBp.bodyPart then
self.nodeAlpha = 1.0
self.selectedAlpha = 0.5
break
end
end
end
ISBodyPartPanel.prerender(self)
end
function ISHealthBodyPartPanel:cbSetSelected(bp)
if bp == nil then
self.parent.listbox.selected = 0
return
end
for index,item in ipairs(self.parent.listbox.items) do
if item.item.bodyPart == bp.bodyPart then
self.parent.listbox.selected = index
break
end
end
end
local og_ISHealthPanel_dropItemsOnBodyPart = ISHealthPanel.dropItemsOnBodyPart
function ISHealthPanel:dropItemsOnBodyPart(bodyPart, items)
og_ISHealthPanel_dropItemsOnBodyPart(self, bodyPart, items)
local cutLimbHandler = CutLimbHandler:new(self, bodyPart)
for _,item in ipairs(items) do
cutLimbHandler:checkItem(item)
end
if cutLimbHandler:dropItems(items) then
return
end
end

View File

@@ -0,0 +1,128 @@
-- Had to cop and paste this stuff from the base game since this is a local only class. Kinda shit, but eh
local BaseHandler = ISBaseObject:derive("BaseHandler")
function BaseHandler:new(panel, bodyPart)
local o = {}
setmetatable(o, self)
self.__index = self
o.panel = panel
o.bodyPart = bodyPart
o.items = {}
return o
end
function BaseHandler:isInjured()
local bodyPart = self.bodyPart
return (bodyPart:HasInjury() or bodyPart:stitched() or bodyPart:getSplintFactor() > 0) and not bodyPart:bandaged()
end
function BaseHandler:checkItems()
for k,v in pairs(self.items) do
table.wipe(v)
end
local containers = ISInventoryPaneContextMenu.getContainers(self:getDoctor())
local done = {}
local childContainers = {}
for i=1,containers:size() do
local container = containers:get(i-1)
done[container] = true
table.wipe(childContainers)
self:checkContainerItems(container, childContainers)
for _,container2 in ipairs(childContainers) do
if not done[container2] then
done[container2] = true
self:checkContainerItems(container2, nil)
end
end
end
end
function BaseHandler:checkContainerItems(container, childContainers)
local containerItems = container:getItems()
for i=1,containerItems:size() do
local item = containerItems:get(i-1)
if item:IsInventoryContainer() then
if childContainers then
table.insert(childContainers, item:getInventory())
end
else
self:checkItem(item)
end
end
end
function BaseHandler:dropItems(items)
return false
end
function BaseHandler:addItem(items, item)
table.insert(items, item)
end
function BaseHandler:getAllItemTypes(items)
local done = {}
local types = {}
for _,item in ipairs(items) do
if not done[item:getFullType()] then
table.insert(types, item:getFullType())
done[item:getFullType()] = true
end
end
return types
end
function BaseHandler:getItemOfType(items, type)
for _,item in ipairs(items) do
if item:getFullType() == type then
return item
end
end
return nil
end
function BaseHandler:getItemOfTag(items, type)
for _,item in ipairs(items) do
if item:hasTag(type) then
return item
end
end
return nil
end
function BaseHandler:getAllItemsOfType(items, type)
local items = {}
for _,item in ipairs(items) do
if item:getFullType() == type then
table.insert(items, item)
end
end
return items
end
function BaseHandler:onMenuOptionSelected(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
ISTimedActionQueue.add(HealthPanelAction:new(self:getDoctor(), self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
end
function BaseHandler:toPlayerInventory(item, previousAction)
if item:getContainer() ~= self:getDoctor():getInventory() then
local action = ISInventoryTransferAction:new(self:getDoctor(), item, item:getContainer(), self:getDoctor():getInventory())
ISTimedActionQueue.addAfter(previousAction, action)
-- FIXME: ISHealthPanel.actions never gets cleared
self.panel.actions = self.panel.actions or {}
self.panel.actions[action] = self.bodyPart
return action
end
return previousAction
end
function BaseHandler:getDoctor()
return self.panel.otherPlayer or self.panel.character
end
function BaseHandler:getPatient()
return self.panel.character
end
return BaseHandler