summaryrefslogtreecommitdiffhomepage
path: root/not/Settings.lua
blob: 7d5f4896199b004db2897c427a05dd3f2386d902 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--- `Settings`
-- Stores, loads, saves and changes game settings including Controller sets.
Settings = {
	current = {}
}

-- Converts from old settings format to the one after `02aba07e03465205b45c41df7aec6894d4e89909`.
local function convertToNew (old)
	return {sets = old, display = "fullscreen"}
end

local function filePrepare ()
	if not love.filesystem.exists("settings") then
		local def = love.filesystem.newFile("settings.default")
		local new = love.filesystem.newFile("settings")
		new:open("w") def:open("r")
		new:write(def:read())
		new:close() def:close()
	end
end

local function fileLoad ()
	local getSettings = love.filesystem.load("settings")
	local settings = getSettings()
	if not settings.sets then
		settings = convertToNew(settings)
	end
	Settings.current = settings
end

local function controllerLoad ()
	if Controller then
		Controller.reset()
		local joysticksList = love.joystick.getJoysticks()
		for _,set in pairs(Settings.current.sets) do
			local isJoystick = set[7]
			local joystick
			if isJoystick then
				joystick = table.remove(joysticksList, 1)
			end
			if not isJoystick or joystick then
				Controller.registerSet(set[1], set[2], set[3], set[4], set[5], set[6], joystick)
			end
		end
	end
end

local function displayLoad ()
	local width, height, flags = love.window.getMode()
	if Settings.current.display == "fullscreen" then
		if not flags.fullscreen then
			love.window.setFullscreen(true, "desktop")
		end
	else
		local scale = tonumber(Settings.current.display) or 1
		local expectedWidth, expectedHeight = 320 * scale, 180 * scale
		if flags.fullscreen then
			love.window.setFullscreen(false)
		end
		if width ~= expectedWidth or height ~= expectedHeight then
			love.window.setMode(expectedWidth, expectedHeight)
		end
	end
end

function Settings.load ()
	filePrepare()
	fileLoad()
	controllerLoad()
	displayLoad()
end

function Settings.save () 
	local new = love.filesystem.newFile("settings")
	local sets = Settings.current.sets
	local string = "return {\n\tsets = {\n"
	for i,set in pairs(sets) do
		string = string .. "\t\t{"
		for j,word in pairs(set) do
			if j ~= 7 then
				string = string .. "\"" .. word .. "\", "
			else
				if word then
					string = string .. "true"
				else
					string = string .. "false"
				end
			end
		end
		string = string .. "},\n"
	end
	string = string .. "\t},\n"
	string = string .. "\tdisplay = \"fullscreen\",\n"
	string = string .. "}\n"
	new:open("w")
	new:write(string)
	new:close()
end

function Settings.change (n, left, right, up, down, attack, jump, joystick)
	local bool
	if joystick then
		bool = true
	else
		bool = false
	end
	-- Save current settings
	Settings.current.sets[n] = {left, right, up, down, attack, jump, bool}
	Settings.save()
	-- Load settings
	Settings.load()
end