-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.lua
190 lines (170 loc) · 6.01 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
function ModSettingsGuiCount() return 1 end
dofile_once("data/scripts/lib/utilities.lua")
dofile_once("data/scripts/lib/mod_settings.lua")
dofile_once("data/scripts/perks/perk.lua")
dofile("data/scripts/perks/perk_list.lua")
local SETTINGS_VERSION = 1
local DEFAULT = { boolean = false, number = 0, string = "" }
local perk_settings = {}
local dirty = false
function ModSettingsUpdate(init_scope)
-- putting this here allows us to detect more perks
dofile("data/scripts/perks/perk_list.lua")
-- migrations can go here in the future
local version = ModSettingGet("start_perks._version")
if version ~= SETTINGS_VERSION then
ModSettingSet("start_perks._version", SETTINGS_VERSION)
end
-- refresh perk list
local hidden_states = {}
for i, setting in ipairs(perk_settings) do
hidden_states[setting.id] = setting.hidden
end
perk_settings = {}
for i, perk in ipairs(perk_list) do
local setting = {
id = perk.id,
name = perk.ui_name,
desc = perk.ui_description,
icon = perk.ui_icon,
key = "start_perks.perk_" .. perk.id,
hidden = hidden_states[perk.id] or false
}
if not perk.stackable then
setting.type = "boolean"
elseif perk.stackable_maximum then
setting.type = "number"
setting.max = perk.stackable_maximum
else
setting.type = "string"
end
table.insert(perk_settings, setting)
-- set to default if unset or incorrect type
if type(ModSettingGetNextValue(setting.key)) ~= setting.type then
ModSettingSetNextValue(setting.key, DEFAULT[setting.type], false)
end
end
table.sort(perk_settings, function(a, b)
return GameTextGetTranslatedOrNot(a.name)
< GameTextGetTranslatedOrNot(b.name)
end)
-- update everything
for _, setting in ipairs(perk_settings) do
ModSettingSet(setting.key, ModSettingGetNextValue(setting.key))
end
if init_scope <= MOD_SETTING_SCOPE_NEW_GAME then
dirty = false
end
end
---------- render ----------
local search_text = ""
function ModSettingsGui(gui, in_main_menu)
if in_main_menu then
GuiColorSetForNextWidget(gui, 1, 1, 1, 0.5)
GuiText(gui, 0, 0, "If any modded perks are missing, you'll need to be ingame to configure them.")
end
local _id = 0
local function id()
_id = _id + 1
return _id
end
-- top area
GuiOptionsAdd(gui, GUI_OPTION.DrawActiveWidgetCursorOnBothSides)
GuiLayoutBeginHorizontal(gui, 0, 0)
local clicked_clear_search = GuiButton(gui, id(), 0, 0, "Clear search")
GuiText(gui, 0, 0, " ")
local clicked_reset_all = GuiButton(gui, id(), 0, 0, "Reset all")
if not in_main_menu and dirty then
GuiColorSetForNextWidget(gui, 1, 1, 1, 0.5)
GuiText(gui, 0, 0, " (Perks will apply in a new game)")
end
GuiLayoutEnd(gui)
local input = GuiTextInput(gui, id(), 0, 0, search_text, 130, 30)
GuiOptionsRemove(gui, GUI_OPTION.DrawActiveWidgetCursorOnBothSides)
if clicked_clear_search then
input = ""
elseif clicked_reset_all then
for _, setting in ipairs(perk_settings) do
ModSettingSetNextValue(setting.key, DEFAULT[setting.type], false)
end
end
if input ~= search_text then
search_text = input
if input == "" then
for _, setting in ipairs(perk_settings) do setting.hidden = false end
else
input = input:lower()
for _, setting in ipairs(perk_settings) do
setting.hidden = not ((
GameTextGetTranslatedOrNot(setting.name):lower():find(input, 0, true)
or setting.id:lower():find(input, 0, true)
or GameTextGetTranslatedOrNot(setting.desc):lower():find(input, 0, true)
) and true or false)
end
end
end
-- begin main area
GuiLayoutBeginHorizontal(gui, 0, 0)
-- icons and labels (left)
GuiText(gui, 0, 0, " ") -- space for icons
GuiLayoutBeginVertical(gui, 0, 0)
for _, setting in ipairs(perk_settings) do
if not setting.hidden then
local value = ModSettingGetNextValue(setting.key)
local alpha = value == DEFAULT[setting.type] and 0.5 or 1
local name = GameTextGetTranslatedOrNot(setting.name)
local desc = GameTextGetTranslatedOrNot(setting.desc)
GuiLayoutAddVerticalSpacing(gui, 2)
GuiOptionsAddForNextWidget(gui, GUI_OPTION.Layout_InsertOutsideLeft)
GuiImage(gui, id(), -3, -2, setting.icon, alpha, 1, 0)
GuiColorSetForNextWidget(gui, 1, 1, 1, alpha)
GuiText(gui, 0, 0, name)
GuiTooltip(gui, name, desc)
end
end
GuiLayoutEnd(gui)
-- widgets (right)
GuiText(gui, 0, 0, " ") -- don't get too close to labels
GuiLayoutBeginVertical(gui, 0, 0)
for _, setting in ipairs(perk_settings) do
if not setting.hidden then
local value = ModSettingGetNextValue(setting.key)
if type(value) ~= setting.type then
value = DEFAULT[setting.type]
end
GuiLayoutAddVerticalSpacing(gui, 2)
if setting.type == "boolean" then
local text = value and GameTextGet("$option_on") or GameTextGet("$option_off")
if GuiButton(gui, id(), 0, 0, text) then
dirty = true
ModSettingSetNextValue(setting.key, not value, false)
end
elseif setting.type == "number" then
GuiLayoutAddVerticalSpacing(gui, 1.5)
local next_value =
GuiSlider(gui, id(), -2, 0, "", value, 0, setting.max, 0, 1, " x$0 ", 64)
GuiLayoutAddVerticalSpacing(gui, 1.5)
if next_value ~= value then
dirty = true
ModSettingSetNextValue(setting.key, next_value, false)
end
else -- setting.type == "string"
local next_value = GuiTextInput(gui, id(), 0, 0, value, 64, 10, "0123456789")
if next_value ~= value then
dirty = true
if tonumber(next_value) == 0 then next_value = "" end
ModSettingSetNextValue(setting.key, next_value, false)
end
end
end
end
GuiLayoutEnd(gui) -- end widgets
GuiLayoutEnd(gui) -- end main area
-- prevent overlap
for _, setting in ipairs(perk_settings) do
if not setting.hidden then
GuiLayoutAddVerticalSpacing(gui, 2)
GuiText(gui, 0, 0, " ")
end
end
end