sonic-radiance/sonic-radiance.love/game/modules/confirmdialog/init.lua

97 lines
2.8 KiB
Lua
Raw Normal View History

2021-08-31 19:02:43 +02:00
local GuiElement = require "birb.modules.gui.elements.canvas"
local ConfirmDialog = GuiElement:extend()
local gui = require "game.modules.gui"
local WIDTH = 256
local PADWIDTH = 16
local PADHEIGHT = 16
2021-08-31 19:02:43 +02:00
local DEFAULT_LINES = 2
function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, choice2)
self.scene = scene
2021-08-31 19:02:43 +02:00
self.lines = DEFAULT_LINES
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.autoDismiss = false
self.darken = true
self.currentChoice = 0
self.cancelChoice = -1
2021-08-31 19:02:43 +02:00
ConfirmDialog.super.new(self, "dialog", 424/2, 240/2, WIDTH + PADWIDTH, self:computeHeight())
self.back = gui.newTextBox("assets/gui/dialogbox.png", self.w, self.h)
self.ox = self.w/2
self.oy = self.h/2
self.canvas.padding = 0
self.depth = 0
self.isVisible = 1
self:getFocus()
end
function ConfirmDialog:setLines(lines)
self.lines = lines
2021-08-31 19:02:43 +02:00
self.h = self:computeHeight()
self.oy = self.h/2
self.canvas.needRedraw = true
self.back = gui.newTextBox("assets/gui/dialogbox.png", self.w, self.h)
end
2021-08-31 19:02:43 +02:00
function ConfirmDialog:computeHeight()
return 32 + (self.lines * 16) + PADHEIGHT
end
function ConfirmDialog:setCancelChoice(choice)
self.cancelChoice = choice - 1
end
2021-08-31 19:02:43 +02:00
function ConfirmDialog:keypressed(key)
if (key == "up" or key == "down") then
self.currentChoice = (self.currentChoice + 1) % 2
self.scene.assets.sfx["mBeep"]:play()
2021-08-31 19:02:43 +02:00
self.canvas.needRedraw = true
elseif (key == "A") then
self:doAction(self.currentChoice)
2021-08-31 19:02:43 +02:00
elseif (key == "B") 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]()
if (self.autoDismiss) then
self:dismiss()
end
end
end
function ConfirmDialog:dismiss()
self.gui:setLastFocus()
self:destroy()
end
2021-08-31 19:02:43 +02:00
function ConfirmDialog:drawTexture()
love.graphics.draw(self.back, 0)
local padx, pady = 8, 6
2021-08-31 19:02:43 +02:00
self.scene.assets.fonts["small"]:draw(self.message ,padx, pady, WIDTH,"left")
for i = 1, 2, 1 do
2021-08-31 19:02:43 +02:00
self.scene.assets.fonts["small"]:draw(self.choiceLabel[i], padx + 8, pady + (self.lines + i - 1)*16)
if ((self.currentChoice + 1) == i) then
2021-08-31 19:02:43 +02:00
self.scene.assets.fonts["small"]:draw(">", padx, pady + (self.lines + i - 1)*16)
end
end
end
return ConfirmDialog