improvement: port confirmdialog to Gui
This commit is contained in:
parent
839813768d
commit
759daf5ec0
1 changed files with 30 additions and 47 deletions
|
@ -1,14 +1,16 @@
|
|||
local ConfirmDialog = Object:extend()
|
||||
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
|
||||
local DEFAULT_LINES = 2
|
||||
|
||||
function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, choice2)
|
||||
self.scene = scene
|
||||
self.lines = 2
|
||||
self.lines = DEFAULT_LINES
|
||||
self.message = message
|
||||
|
||||
self.choiceLabel = {}
|
||||
|
@ -26,50 +28,43 @@ function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, ch
|
|||
|
||||
self.currentChoice = 0
|
||||
self.cancelChoice = -1
|
||||
self.isActive = false
|
||||
|
||||
self.scene.dialog = self
|
||||
self.texture = self:createTexture()
|
||||
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
|
||||
self.texture = self:createTexture()
|
||||
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
|
||||
|
||||
function ConfirmDialog:createTexture()
|
||||
self.height = 32 + (self.lines * 16) + PADHEIGHT
|
||||
return gui.newTextBox("assets/gui/dialogbox.png", WIDTH + PADWIDTH, self.height)
|
||||
function ConfirmDialog:computeHeight()
|
||||
return 32 + (self.lines * 16) + PADHEIGHT
|
||||
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
|
||||
function ConfirmDialog:keypressed(key)
|
||||
if (key == "up" or key == "down") then
|
||||
self.currentChoice = (self.currentChoice + 1) % 2
|
||||
self.scene.assets.sfx["mBeep"]:play()
|
||||
end
|
||||
|
||||
if (keys["A"].isPressed) then
|
||||
self.canvas.needRedraw = true
|
||||
elseif (key == "A") then
|
||||
self:doAction(self.currentChoice)
|
||||
end
|
||||
|
||||
if (keys["B"].isPressed) then
|
||||
elseif (key == "B") then
|
||||
self:doAction(self.cancelChoice)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function ConfirmDialog:doAction(choice)
|
||||
|
@ -83,33 +78,21 @@ function ConfirmDialog:doAction(choice)
|
|||
end
|
||||
|
||||
function ConfirmDialog:dismiss()
|
||||
local guiSystem = self:getGui()
|
||||
guiSystem:setLastFocus()
|
||||
self:destroy()
|
||||
end
|
||||
|
||||
function ConfirmDialog:destroy()
|
||||
self.scene.dialog = nil
|
||||
end
|
||||
|
||||
function ConfirmDialog:draw()
|
||||
local x, y = self:getCoord()
|
||||
function ConfirmDialog:drawTexture()
|
||||
love.graphics.draw(self.back, 0)
|
||||
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")
|
||||
self.scene.assets.fonts["small"]:draw(self.message ,padx, 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)
|
||||
self.scene.assets.fonts["small"]:draw(self.choiceLabel[i], padx + 8, 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)
|
||||
self.scene.assets.fonts["small"]:draw(">", padx, pady + (self.lines + i - 1)*16)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ConfirmDialog:getCoord()
|
||||
return (424-(WIDTH + PADWIDTH))/2, (240 - self.height)/2
|
||||
end
|
||||
|
||||
return ConfirmDialog
|
Loading…
Reference in a new issue