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 gui = require "game.modules.gui"
|
||||||
|
|
||||||
local WIDTH = 256
|
local WIDTH = 256
|
||||||
local PADWIDTH = 16
|
local PADWIDTH = 16
|
||||||
local PADHEIGHT = 16
|
local PADHEIGHT = 16
|
||||||
|
local DEFAULT_LINES = 2
|
||||||
|
|
||||||
function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, choice2)
|
function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, choice2)
|
||||||
self.scene = scene
|
self.scene = scene
|
||||||
self.lines = 2
|
self.lines = DEFAULT_LINES
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
self.choiceLabel = {}
|
self.choiceLabel = {}
|
||||||
|
@ -26,50 +28,43 @@ function ConfirmDialog:new(scene, message, choice1func, choice1, choice2func, ch
|
||||||
|
|
||||||
self.currentChoice = 0
|
self.currentChoice = 0
|
||||||
self.cancelChoice = -1
|
self.cancelChoice = -1
|
||||||
self.isActive = false
|
|
||||||
|
|
||||||
self.scene.dialog = self
|
ConfirmDialog.super.new(self, "dialog", 424/2, 240/2, WIDTH + PADWIDTH, self:computeHeight())
|
||||||
self.texture = self:createTexture()
|
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
|
end
|
||||||
|
|
||||||
function ConfirmDialog:setLines(lines)
|
function ConfirmDialog:setLines(lines)
|
||||||
self.lines = 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
|
end
|
||||||
|
|
||||||
function ConfirmDialog:createTexture()
|
function ConfirmDialog:computeHeight()
|
||||||
self.height = 32 + (self.lines * 16) + PADHEIGHT
|
return 32 + (self.lines * 16) + PADHEIGHT
|
||||||
return gui.newTextBox("assets/gui/dialogbox.png", WIDTH + PADWIDTH, self.height)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:setCancelChoice(choice)
|
function ConfirmDialog:setCancelChoice(choice)
|
||||||
self.cancelChoice = choice - 1
|
self.cancelChoice = choice - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:update(dt)
|
function ConfirmDialog:keypressed(key)
|
||||||
if (self.isActive) then
|
if (key == "up" or key == "down") 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.currentChoice = (self.currentChoice + 1) % 2
|
||||||
self.scene.assets.sfx["mBeep"]:play()
|
self.scene.assets.sfx["mBeep"]:play()
|
||||||
end
|
self.canvas.needRedraw = true
|
||||||
|
elseif (key == "A") then
|
||||||
if (keys["A"].isPressed) then
|
|
||||||
self:doAction(self.currentChoice)
|
self:doAction(self.currentChoice)
|
||||||
end
|
elseif (key == "B") then
|
||||||
|
|
||||||
if (keys["B"].isPressed) then
|
|
||||||
self:doAction(self.cancelChoice)
|
self:doAction(self.cancelChoice)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:doAction(choice)
|
function ConfirmDialog:doAction(choice)
|
||||||
|
@ -83,33 +78,21 @@ function ConfirmDialog:doAction(choice)
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:dismiss()
|
function ConfirmDialog:dismiss()
|
||||||
|
local guiSystem = self:getGui()
|
||||||
|
guiSystem:setLastFocus()
|
||||||
self:destroy()
|
self:destroy()
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:destroy()
|
function ConfirmDialog:drawTexture()
|
||||||
self.scene.dialog = nil
|
love.graphics.draw(self.back, 0)
|
||||||
end
|
|
||||||
|
|
||||||
function ConfirmDialog:draw()
|
|
||||||
local x, y = self:getCoord()
|
|
||||||
local padx, pady = 8, 6
|
local padx, pady = 8, 6
|
||||||
if (self.darken) then
|
self.scene.assets.fonts["small"]:draw(self.message ,padx, pady, WIDTH,"left")
|
||||||
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
|
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
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function ConfirmDialog:getCoord()
|
|
||||||
return (424-(WIDTH + PADWIDTH))/2, (240 - self.height)/2
|
|
||||||
end
|
|
||||||
|
|
||||||
return ConfirmDialog
|
return ConfirmDialog
|
Loading…
Reference in a new issue