From 759daf5ec01035bfa930d002462e547570fd1d4e Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 31 Aug 2021 19:02:43 +0200 Subject: [PATCH] improvement: port confirmdialog to Gui --- .../game/modules/confirmdialog/init.lua | 77 ++++++++----------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/sonic-radiance.love/game/modules/confirmdialog/init.lua b/sonic-radiance.love/game/modules/confirmdialog/init.lua index 4a1272c..5ace915 100644 --- a/sonic-radiance.love/game/modules/confirmdialog/init.lua +++ b/sonic-radiance.love/game/modules/confirmdialog/init.lua @@ -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 \ No newline at end of file