feat: add confirmation dialogs

Fixes #86
This commit is contained in:
Kazhnuz 2021-04-18 16:36:40 +02:00
parent dbf14c59a3
commit 9523edb555
7 changed files with 158 additions and 4 deletions

View file

@ -92,7 +92,7 @@ end
function Scene:updateMenus(dt) function Scene:updateMenus(dt)
if (self.menusystem ~= nil) then if (self.menusystem ~= nil) then
self.menusystem:update(dt) self.menusystem:update(dt)
if (core.screen:isActive()) then if (core.screen:isActive() and (self.dialog == nil)) then
self.menusystem:keycheck() self.menusystem:keycheck()
end end
end end

View file

@ -82,6 +82,7 @@ function SceneManager:update(dt)
self.currentScene:setKeys() self.currentScene:setKeys()
self.currentScene.assets:update(dt) self.currentScene.assets:update(dt)
self.currentScene:updateMenus(dt) self.currentScene:updateMenus(dt)
self.currentScene:updateDialog(dt)
self.currentScene:updateWorld(dt) self.currentScene:updateWorld(dt)
self.currentScene:update(dt) self.currentScene:update(dt)
self.currentScene:updateEnd(dt) self.currentScene:updateEnd(dt)
@ -132,6 +133,7 @@ function SceneManager:draw()
self.currentScene:drawWorld() self.currentScene:drawWorld()
self.currentScene:draw() self.currentScene:draw()
self.currentScene:drawMenus() self.currentScene:drawMenus()
self.currentScene:drawDialog()
self.currentScene:drawEnd() self.currentScene:drawEnd()
self.controller.screen:drawTransition() self.controller.screen:drawTransition()
self.currentScene:drawOverTransition() self.currentScene:drawOverTransition()

View file

@ -160,6 +160,10 @@ function Game:removeFromMetadata()
self:writeMetadata(metadata) self:writeMetadata(metadata)
end end
function Game:reload()
self:read(self.slot)
end
function Game:read(save_id) function Game:read(save_id)
self.slot = save_id self.slot = save_id
if (self.slot > 0) then if (self.slot > 0) then

View file

@ -0,0 +1,111 @@
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

View file

@ -29,6 +29,7 @@ function OptionsMenu:new()
Widgets.Audio(self, "audio", "sfx", "SFX") Widgets.Audio(self, "audio", "sfx", "SFX")
Widgets.Audio(self, "audio", "music", "Music") Widgets.Audio(self, "audio", "music", "Music")
Widgets.Delete(self, "main")
Widgets.Exit(self, "main") Widgets.Exit(self, "main")
self.menusystem.menus["main"]:finalize() self.menusystem.menus["main"]:finalize()

View file

@ -14,8 +14,10 @@ widgets.Lang = RadianceListMenu.DualTextWidget:extend()
widgets.PlayerSubMenu = RadianceListMenu.DualTextWidget:extend() widgets.PlayerSubMenu = RadianceListMenu.DualTextWidget:extend()
widgets.Key = RadianceListMenu.DualTextWidget:extend() widgets.Key = RadianceListMenu.DualTextWidget:extend()
widgets.Audio = RadianceListMenu.DualTextWidget:extend() widgets.Audio = RadianceListMenu.DualTextWidget:extend()
widgets.Delete = RadianceListMenu.DualTextWidget:extend()
local defTransitions = require "core.modules.transitions" local defTransitions = require "core.modules.transitions"
local ConfirmDialog = require "game.modules.confirmdialog"
-- BASIC WIDGETS -- BASIC WIDGETS
-- Simple and reusables widgets -- Simple and reusables widgets
@ -63,6 +65,29 @@ function widgets.Dummy:action()
-- shoosh -- shoosh
end end
-- Delete Save : delete the current Save
function widgets.Delete:new(scene, menu)
widgets.Exit.super.new(self, scene, menu, "Delete save", "")
self.color = {1, 0.3, 0.3}
end
function widgets.Delete:action()
self.scene.assets:playSFX("mSelect")
local confirm = ConfirmDialog(self.scene, "Do you want to delete your save ? \nYou won't be able to recover your data.",
function() self:deleteSave() end)
confirm:setCancelChoice(2)
end
function widgets.Delete:deleteSave()
core.scenemanager:setStoredScene("mainmenu")
game:deleteCurrentSave()
core.screen:startTransition(defTransitions.default, defTransitions.circle,
function() scenes.title(true) end,
424/2, 240/2)
end
-- Exit Widget : exit the examples -- Exit Widget : exit the examples
function widgets.Exit:new(scene, menu) function widgets.Exit:new(scene, menu)

View file

@ -14,6 +14,7 @@ local SaveExitWidget = menu.BaseWidget:extend()
local defTransitions = require "core.modules.transitions" local defTransitions = require "core.modules.transitions"
local radTransitions = require "game.modules.transitions" local radTransitions = require "game.modules.transitions"
local ConfirmDialog = require "game.modules.confirmdialog"
local const = require "scenes.overworld.screens.mainmenu.const" local const = require "scenes.overworld.screens.mainmenu.const"
@ -115,12 +116,22 @@ function SaveExitWidget:action()
game:write() game:write()
end end
if (self.exit) then if (self.exit) then
core.screen:startTransition(defTransitions.default, defTransitions.circle, function() scenes.debug.menu() end, 424/2, 240/2) if (self.save) then
self.scene.tweens:newTween(0, 0.3, {ringBorder=-16}, "inOutQuad") self:exitToMenu()
self.scene.tweens:newTween(0, 0.3, {timeBorder=-20}, "inOutQuad") else
local confirm = ConfirmDialog(self.scene, "Do you to exit the game ? \nAll unsaved data will be lost.",
function() self:exitToMenu() end)
confirm:setCancelChoice(2)
end
else else
self.scene:unpause() self.scene:unpause()
end end
end end
function SaveExitWidget:exitToMenu()
core.screen:startTransition(defTransitions.default, defTransitions.circle, function() game:reload() scenes.debug.menu() end, 424/2, 240/2)
self.scene.tweens:newTween(0, 0.3, {ringBorder=-16}, "inOutQuad")
self.scene.tweens:newTween(0, 0.3, {timeBorder=-20}, "inOutQuad")
end
return PauseScreen return PauseScreen