Added confirmation for amputations

This commit is contained in:
ZioPao
2024-10-17 23:50:51 +02:00
parent 4af127196c
commit 132ce24328
3 changed files with 164 additions and 15 deletions

View File

@@ -0,0 +1,116 @@
---@class ConfirmationPanel : ISPanel
local ConfirmationPanel = ISPanel:derive("ConfirmationPanel")
---Starts a new confirmation panel
---@param x number
---@param y number
---@param width number
---@param height number
---@param alertText string
---@param onConfirmFunc function
---@return ConfirmationPanel
function ConfirmationPanel:new(x, y, width, height, alertText, parentPanel, onConfirmFunc)
local o = ISPanel:new(x, y, width, height)
setmetatable(o, self)
self.__index = self
o:initialise()
o.alertText = alertText
o.onConfirmFunc = onConfirmFunc
o.parentPanel = parentPanel
ConfirmationPanel.instance = o
---@cast o ConfirmationPanel
return o
end
function ConfirmationPanel:createChildren()
ISPanel.createChildren(self)
self.borderColor = { r = 1, g = 0, b = 0, a = 1 }
self.textPanel = ISRichTextPanel:new(0, 0, self.width, self.height)
self.textPanel:initialise()
self:addChild(self.textPanel)
self.textPanel.defaultFont = UIFont.Medium
self.textPanel.anchorTop = true
self.textPanel.anchorLeft = false
self.textPanel.anchorBottom = true
self.textPanel.anchorRight = false
self.textPanel.marginLeft = 0
self.textPanel.marginTop = 10
self.textPanel.marginRight = 0
self.textPanel.marginBottom = 0
self.textPanel.autosetheight = false
self.textPanel.background = false
self.textPanel:setText(self.alertText)
self.textPanel:paginate()
local yPadding = 10
local xPadding = self:getWidth() / 4
local btnWidth = 100
local btnHeight = 25
local yButton = self:getHeight() - yPadding - btnHeight
self.btnYes = ISButton:new(xPadding, yButton, btnWidth, btnHeight, "Yes", self, self.onClick)
self.btnYes.internal = "YES"
self.btnYes:initialise()
self.btnYes.borderColor = { r = 1, g = 0, b = 0, a = 1 }
self.btnYes:setEnable(true)
self:addChild(self.btnYes)
self.btnNo = ISButton:new(self:getWidth() - xPadding - btnWidth, yButton, btnWidth, btnHeight, "No", self,
self.onClick)
self.btnNo.internal = "NO"
self.btnNo:initialise()
self.btnNo:setEnable(true)
self:addChild(self.btnNo)
end
function ConfirmationPanel:onClick(btn)
if btn.internal == 'YES' then
self.onConfirmFunc(self.parentPanel)
self:close()
elseif btn.internal == 'NO' then
self:close()
end
end
-------------------------
---@param alertText string
---@param x any
---@param y any
---@param parentPanel any
---@param onConfirmFunc any
---@return ConfirmationPanel
function ConfirmationPanel.Open(alertText, x, y, parentPanel, onConfirmFunc)
local width = 500
local height = 120
local screenWidth = getCore():getScreenWidth()
local screenHeight = getCore():getScreenHeight()
-- Check for oversize
if x+width > screenWidth then
x = screenWidth - width
end
if y+height > screenHeight then
y = screenHeight - height
end
local panel = ConfirmationPanel:new(x, y, width, height, alertText, parentPanel, onConfirmFunc)
panel:initialise()
panel:addToUIManager()
panel:bringToTop()
return panel
end
function ConfirmationPanel.Close()
ConfirmationPanel.instance:close()
end
return ConfirmationPanel

View File

@@ -1,11 +1,12 @@
local BaseHandler = require("TOC/UI/Interactions/HealthPanelBaseHandler")
local StaticData = require("TOC/StaticData")
local DataController = require("TOC/Controllers/DataController")
local ConfirmationPanel = require("TOC/UI/ConfirmationPanel")
local CutLimbAction = require("TOC/TimedActions/CutLimbAction")
---------------------
-- TODO Add interaction to cut and bandage!
--* Various functions to help during these pesky checks
@@ -61,6 +62,12 @@ local function GetStitchesConsumableItem(player)
end
local textConfirmAmp = getText("IGUI_Confirmation_Amputate")
local textAmp = getText("ContextMenu_Amputate")
local textAmpBandage = getText("ContextMenu_Amputate_Bandage")
local textAmpStitch = getText("ContextMenu_Amputate_Stitch")
local textAmpStitchBandage = getText("ContextMenu_Amputate_Stitch_Bandage")
---Add the action to the queue
---@param limbName string
---@param surgeon IsoPlayer
@@ -69,24 +76,32 @@ end
---@param stitchesItem InventoryItem?
---@param bandageItem InventoryItem?
local function PerformAction(surgeon, patient, limbName, sawItem, stitchesItem, bandageItem)
-- get saw in hand
-- todo primary or secondary depending on amputation status of surgeon
ISTimedActionQueue.add(ISEquipWeaponAction:new(surgeon, sawItem, 50, true, false))
local lHandItem = surgeon:getSecondaryHandItem()
if lHandItem then
ISTimedActionQueue.add(ISUnequipAction:new(surgeon, lHandItem, 50))
end
ISTimedActionQueue.add(CutLimbAction:new(surgeon, patient, limbName, sawItem, stitchesItem, bandageItem))
local x = (getCore():getScreenWidth() - 500) / 2
local y = getCore():getScreenHeight() / 2
ConfirmationPanel.Open(textConfirmAmp, x, y, nil, function()
-- get saw in hand
-- todo primary or secondary depending on amputation status of surgeon
ISTimedActionQueue.add(ISEquipWeaponAction:new(surgeon, sawItem, 50, true, false))
local lHandItem = surgeon:getSecondaryHandItem()
if lHandItem then
ISTimedActionQueue.add(ISUnequipAction:new(surgeon, lHandItem, 50))
end
ISTimedActionQueue.add(CutLimbAction:new(surgeon, patient, limbName, sawItem, stitchesItem, bandageItem))
end)
end
local textAmp = getText("ContextMenu_Amputate")
local textAmpBandage = getText("ContextMenu_Amputate_Bandage")
local textAmpStitch = getText("ContextMenu_Amputate_Stitch")
local textAmpStitchBandage = getText("ContextMenu_Amputate_Stitch_Bandage")
---Adds the actions to the inventory context menu
---@param player IsoPlayer
@@ -214,6 +229,18 @@ function CutLimbInteractionHandler:checkItem(item)
end
end
---@param x number
---@param y number
---@param type any
function CutLimbInteractionHandler:openConfirmation(x, y, type)
ConfirmationPanel.Open(textConfirmAmp, x, y, nil, function()
self.onMenuOptionSelected(self, type)
end)
end
---@param context ISContextMenu
function CutLimbInteractionHandler:addToMenu(context)
--TOC_DEBUG.print("CutLimbInteractionHandler addToMenu")
@@ -221,8 +248,12 @@ function CutLimbInteractionHandler:addToMenu(context)
local patientUsername = self:getPatient():getUsername()
if #types > 0 and StaticData.LIMBS_TO_BODYLOCS_IND_BPT[self.limbName] and not DataController.GetInstance(patientUsername):getIsCut(self.limbName) then
TOC_DEBUG.print("addToMenu, types > 0")
local x = (getCore():getScreenWidth() - 500) / 2
local y = getCore():getScreenHeight() / 2
for i=1, #types do
context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected, types[i])
context:addOption(getText("ContextMenu_Amputate"), self, self.openConfirmation, x, y, types[i])
end
end
end