aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Config.lua
blob: 394b115b12b305b55f98a9952f5d3fde5175132a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
--[[
	Gamestates/Config.lua
	User defined configuration
	by piernov

	Comment: currently only used for player_name, but maybe allowing other parameters like sound or display settings will be implemented.
		A similar file could be used to implement an "High score" feature.
]]--

Config = {}
Config.player_name = {}

local Gui = require "Quickie"
local Utils = require "Utils"

function Config:loadUserConfig() -- Load user config file
	if not love.filesystem.exists("userConfig") then
		love.filesystem.write( "userConfig", "") -- Create the save file if it doesn't exists
	else
		for line in love.filesystem.lines("userConfig") do -- Read line by line
			var, arg = line:match("(.*): (.*)") -- Split following the "<KEY>: <VALUE>" schema
			if var == "PLAYERNAME" then
				Config.player_name.text = arg -- Handle player_name
			end
		end
	end
	if not Config.player_name.text then
		Config.player_name.text = "BlueMind" -- Set default player_name if nothing was found in the user config file
	end
end

function Config:enter()
	Previous = "Gamestates/Menu"
end

function Config:update(dt)
	Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(20, 20)}}
		Gui.Label{text = "Player name", size = {Utils.percentCoordinates(10, 10)}}
		Gui.Input{info = Config.player_name, size = {Utils.percentCoordinates(50, 10)}}
		if Gui.Button{text = "Save", size = {Utils.percentCoordinates(60, 10)}} then
			love.filesystem.write( "userConfig", "PLAYERNAME: " .. Config.player_name.text) -- Write the new name to the config file
		end
	Gui.group.pop{}
end

-- Quickie's functions
function Config:draw()
	Gui.core.draw()
end

function Config:keypressed(key, code)
    Gui.keyboard.pressed(key)
end

function love.textinput(str)
    Gui.keyboard.textinput(str)
end
-- End

function Config:leave()
	love.keyboard.setTextInput(false)
end

return Config