-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.lua
148 lines (128 loc) · 3.69 KB
/
settings.lua
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require "stringhelper"
Settings = Object:extend()
function Settings:new()
self.settings = self:loadSettings()
end
function Settings:currentSettings()
return self.settings
end
function Settings:loadSettings()
local f = io.open("settings.conf", "r")
if f == nil then
return self:loadDefaultSettings()
end
local storedSettings = dofile("settings.conf")
for i, storedSetting in ipairs(storedSettings) do
for j, setting in ipairs(assets.config.defaultsettings) do
if storedSetting[1] == setting[1] then
setting[3] = storedSetting[2]
self:applySetting(setting)
end
end
end
return assets.config.defaultsettings
end
function Settings:loadDefaultSettings()
local defaultSettings = assets.config.defaultsettings
local settings = {}
for i, setting in ipairs(defaultSettings) do
local setting = {setting[1], setting[2], setting[3], setting[4]}
table.insert(settings, setting)
self:applySetting(setting)
end
return settings
end
function Settings:toString(setting)
local str = setting[1]
if setting[2] == "boolean" then
if setting[3] then
str = str .. " [X]"
else
str = str .. " [ ]"
end
elseif setting[2] == "string" then
str = str .. " < " .. setting[3] .. " >"
end
return str
end
function Settings:nextSetting(index)
local setting = self.settings[index]
if setting[2] == "boolean" then
setting[3] = not setting[3]
elseif setting[2] == "string" then
self:nextString(setting)
end
self:applySetting(setting)
end
function Settings:previousSetting(index)
local setting = self.settings[index]
if setting[2] == "boolean" then
setting[3] = not setting[3]
elseif setting[2] == "string" then
self:previousString(setting)
end
self:applySetting(setting)
end
function Settings:applySetting(setting)
if setting[1] == "FullScreen" then
local w, h, flags = love.window.getMode()
flags.fullscreen = setting[3]
if self.settings ~= nil then
for i, setting in ipairs(self.settings) do
if setting[1] == "Resolução" then
local parts = setting[3]:split("x")
w = parts[1]
h = parts[2]
break
end
end
end
love.window.setMode(w, h, flags)
-- XXX screen doesn't resize automatically when disabling fullscreen mode
elseif setting[1] == "Resolução" then
local parts = setting[3]:split("x")
local w, h, flags = love.window.getMode()
w, h = parts[1], parts[2]
love.window.setMode(w, h, flags)
end
love.event.clear() -- XXX when we change modes, looks like lua ignores previous key presses
-- and the "ispressed" parameter in love.keypressed is always false.
-- here we clear the event queue to fix this.
end
function Settings:screenScaleFactor()
return love.graphics.getHeight() / 1080
end
function Settings:menuScaleFactor()
return math.max(1, 1 / self:screenScaleFactor())
end
function Settings:nextString(setting)
local current = setting[3]
local index = 0
for i, v in ipairs(setting[4]) do
if v == current then
index = i
break
end
end
if i == 0 then
return
end
index = index % #setting[4] + 1
setting[3] = setting[4][index]
end
function Settings:previousString(setting)
local current = setting[3]
local index = 0
for i, v in ipairs(setting[4]) do
if v == current then
index = i
break
end
end
if i == 0 then
return
end
index = (index - 2) % #setting[4] + 1
setting[3] = setting[4][index]
end
settings = Settings()