TaggedEngine/states/optionsstate.lua

150 lines
4.7 KiB
Lua

local logging = require("modules.logging")
local json = require("modules.json")
local files= require("modules.files")
local font = love.graphics.newFont("fonts/FridayNightFunkin-Regular.ttf", 60)
local data = love.filesystem.getSaveDirectory()
local settingList = { -- Path for the positions in JSON
Left = "Keybinds.1",
Down = "Keybinds.2",
Up = "Keybinds.3",
Right = "Keybinds.4",
Downscroll = "Downscroll",
Offset = "Offset"
}
local categoryList = { -- The way its on the screen
{
name = "Keybinds",
settings = {
"Left",
"Down",
"Up",
"Right"
},
},
{
name = "Gameplay",
settings = {
"Downscroll",
"Offset"
}
}
}
local globalList = {}
local categories = {}
for index, category in next, categoryList do
globalList[#globalList+1] = category.name
categories[category.name] = true
for index, setting in next, category.settings do
globalList[#globalList+1] = setting
end
end
local currentSetting = 1
return function()
---@type engine.state
local state = {}
local getting
local evilCurrentSetting = 1
local settings = json.parse(files.read_file(data.."/Settings.json"))
local function getSettingPath(setting)
local pathString = settingList[setting]:split(".")
print(logging.dump(pathString))
local path = settings
local last
for index, pathPiece in next, pathString do
if tonumber(pathPiece) then
pathPiece = tonumber(pathPiece)
end
if index == #pathString then
last = pathPiece
break
else
path = path[pathPiece]
end
end
return path, last
end
function state.keypressed(key)
if getting then
local path, last = getSettingPath(getting)
path[last] = key
getting = false
return
end
if key == "down" then
currentSetting = currentSetting + 1 > #globalList and 1 or currentSetting + 1
elseif key == "up" then
currentSetting = currentSetting - 1 <= 0 and #globalList or currentSetting - 1
elseif key == "return" then
local listSetting = globalList[currentSetting]
if categories[listSetting] then return end
local settingPath, settingLast = getSettingPath(listSetting)
local setting = settingPath[settingLast]
if type(setting) == "string" then
getting = listSetting
elseif type(setting) == "boolean" then
settingPath[settingLast] = not settingPath[settingLast]
end
elseif key == "escape" then
files.write_file(data.."/Settings.json", json.stringify(settings))
state.changeState("menustate")
elseif key == "right" or key == "left" then
local listSetting = globalList[currentSetting]
if categories[listSetting] then return end
local settingPath, settingLast = getSettingPath(listSetting)
local setting = settingPath[settingLast]
if type(setting) == "number" then
settingPath[settingLast] = settingPath[settingLast] + (key == "right" and 1 or -1)
end
end
end
function state.load()
love.window.setTitle("TaggedEngine: Options")
local bg = Image("images/menuBG.png")
bg.layer = -5
end
function state.update(delta)
evilCurrentSetting = Lerp(evilCurrentSetting, currentSetting, .05)
render.cameraPosition = Vector2()
render.cameraPosition = Vector2()
render.updateSprites(delta)
end
function state.draw()
render.drawSprites()
render.drawUI()
local curPos = -100 + (evilCurrentSetting - 1) * -100
print(globalList[currentSetting])
for index, category in next, categoryList do
curPos = curPos + 100
local color = globalList[currentSetting] == category.name and {.5,.5,.5} or {0,0,0}
love.graphics.print({color, category.name}, font, 0, love.graphics.getHeight() / 2 + curPos - 10, 0)
for settingIndex, setting in next, category.settings do
curPos = curPos + 100
local path, last = getSettingPath(setting)
local val = path[last]
local color = globalList[currentSetting] == setting and {.5,.5,.5} or {0,0,0}
love.graphics.print({color, string.format("%s: %s", setting, val)}, font, 0, love.graphics.getHeight() / 2 + curPos - 10, 0)
end
end
end
return state
end