sonic-radiance/sonic-radiance.love/game/modules/confirmdialog/init.lua
2021-04-18 16:36:40 +02:00

111 lines
No EOL
2.9 KiB
Lua

local ConfirmDialog = Object:extend()
local gui = require "game.modules.gui"
local WIDTH = 256
local PADWIDTH = 16
local PADHEIGHT = 16
function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, choice2)
self.scene = scene
self.lines = 2
self.message = message
self.choiceLabel = {}
self.choiceFunc = {}
self.choiceSound = {}
self.choiceLabel[1] = choice1 or "Yes"
self.choiceLabel[2] = choice2 or "No"
self.choiceFunc[1] = choice1func
self.choiceFunc[2] = choice2func or function() self:dismiss() end
self.choiceSound[1] = "mSelect"
self.choiceSound[2] = "mBack"
self.darken = true
self.currentChoice = 0
self.cancelChoice = -1
self.isActive = false
self.scene.dialog = self
self.texture = self:createTexture()
end
function ConfirmDialog:setLines(lines)
self.lines = lines
self.texture = self:createTexture()
end
function ConfirmDialog:createTexture()
self.height = 32 + (self.lines * 16) + PADHEIGHT
return gui.newTextBox("assets/gui/dialogbox.png", WIDTH + PADWIDTH, self.height)
end
function ConfirmDialog:setCancelChoice(choice)
self.cancelChoice = choice - 1
end
function ConfirmDialog:update(dt)
if (self.isActive) then
self:keycheck()
else
self.isActive = true
end
end
function ConfirmDialog:keycheck()
local keys = self.scene.sources[1].keys
if (keys["up"].isPressed or
self.scene.sources[1].keys["down"].isPressed) then
self.currentChoice = (self.currentChoice + 1) % 2
self.scene.assets.sfx["mBeep"]:play()
end
if (keys["A"].isPressed) then
self:doAction(self.currentChoice)
end
if (keys["B"].isPressed) then
self:doAction(self.cancelChoice)
end
end
function ConfirmDialog:doAction(choice)
if (self.choiceFunc[choice + 1] ~= nil) then
self.scene.assets.sfx[self.choiceSound[choice + 1]]:play()
self.choiceFunc[choice + 1]()
end
end
function ConfirmDialog:dismiss()
self:destroy()
end
function ConfirmDialog:destroy()
self.scene.dialog = nil
end
function ConfirmDialog:draw()
local x, y = self:getCoord()
local padx, pady = 8, 6
if (self.darken) then
love.graphics.setColor(0,0,0,0.5)
love.graphics.rectangle("fill", 0, 0, 424, 240)
utils.graphics.resetColor()
end
love.graphics.draw(self.texture, x, y)
self.scene.assets.fonts["small"]:draw(self.message ,x + padx, y + pady,WIDTH,"left")
for i = 1, 2, 1 do
self.scene.assets.fonts["small"]:draw(self.choiceLabel[i], x + padx + 8, y + pady + (self.lines + i - 1)*16)
if ((self.currentChoice + 1) == i) then
self.scene.assets.fonts["small"]:draw(">", x + padx, y + pady + (self.lines + i - 1)*16)
end
end
end
function ConfirmDialog:getCoord()
return (424-(WIDTH + PADWIDTH))/2, (240 - self.height)/2
end
return ConfirmDialog