-
-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathsettings.lua
More file actions
373 lines (326 loc) · 13.1 KB
/
Copy pathsettings.lua
File metadata and controls
373 lines (326 loc) · 13.1 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
-- Some users have locale set from ox_lib v2
if GetResourceKvpInt('reset_locale') ~= 1 then
DeleteResourceKvp('locale')
SetResourceKvpInt('reset_locale', 1)
end
---@generic T
---@param fn fun(key): unknown
---@param key string
---@param default? T
---@return T
local function safeGetKvp(fn, key, default)
local ok, result = pcall(fn, key)
if not ok then
DeleteResourceKvp(key)
return default
end
return result or default
end
local settings = {
default_locale = GetConvar('ox:locale', 'en'),
notification_position = safeGetKvp(GetResourceKvpString, 'notification_position', 'top-right'),
notification_audio = safeGetKvp(GetResourceKvpInt, 'notification_audio') == 1
}
local userLocales = GetConvarInt('ox:userLocales', 1) == 1
settings.locale = userLocales and safeGetKvp(GetResourceKvpString, 'locale') or settings.default_locale
local colorblindModes = {
off = {
label = 'ui.settings.colorblind_modes.off',
primaryColor = GetConvar('ox:primaryColor', 'blue'),
primaryShade = GetConvarInt('ox:primaryShade', 8),
indicatorColor = 'red',
indicatorShade = 6,
},
protanopia = {
label = 'ui.settings.colorblind_modes.protanopia',
primaryColor = 'cyan',
primaryShade = 6,
indicatorColor = 'orange',
indicatorShade = 6,
},
deuteranopia = {
label = 'ui.settings.colorblind_modes.deuteranopia',
primaryColor = 'blue',
primaryShade = 6,
indicatorColor = 'orange',
indicatorShade = 6,
},
tritanopia = {
label = 'ui.settings.colorblind_modes.tritanopia',
primaryColor = 'pink',
primaryShade = 6,
indicatorColor = 'green',
indicatorShade = 6,
},
achromatopsia = {
label = 'ui.settings.colorblind_modes.achromatopsia',
primaryColor = 'gray',
primaryShade = 7,
indicatorColor = 'yellow',
indicatorShade = 4,
},
}
local function getColorblindData(mode)
return colorblindModes[mode] or colorblindModes.off
end
settings.colorblind_mode = safeGetKvp(GetResourceKvpString, 'colorblind_mode', 'off')
if not colorblindModes[settings.colorblind_mode] then
settings.colorblind_mode = 'off'
DeleteResourceKvp('colorblind_mode')
end
function settings.getColorblindConfig()
local colorblindData = getColorblindData(settings.colorblind_mode)
return {
colorblindMode = settings.colorblind_mode,
primaryColor = colorblindData.primaryColor,
primaryShade = colorblindData.primaryShade,
}
end
local function set(key, value)
if settings[key] == value then return false end
settings[key] = value
local valueType = type(value)
if valueType == 'nil' then
DeleteResourceKvp(key)
elseif valueType == 'string' then
SetResourceKvp(key, value)
elseif valueType == 'table' then
SetResourceKvp(key, json.encode(value))
elseif valueType == 'number' then
SetResourceKvpInt(key, value)
elseif valueType == 'boolean' then
SetResourceKvpInt(key, value and 1 or 0)
else
return false
end
return true
end
local function syncColorblindMode(mode)
if not colorblindModes[mode] then return false end
if not set('colorblind_mode', mode) then return false end
SendNUIMessage({
action = 'setColorblindMode',
data = mode,
})
return true
end
local function openColorblindMenu()
if not lib.registerContext or not lib.showContext then
local input = lib.inputDialog(locale('ui.settings.colorblind'), {
{
type = 'select',
label = locale('ui.settings.colorblind'),
default = settings.colorblind_mode,
required = true,
options = {
{ label = locale('ui.settings.colorblind_modes.off'), value = 'off' },
{ label = locale('ui.settings.colorblind_modes.protanopia'), value = 'protanopia' },
{ label = locale('ui.settings.colorblind_modes.deuteranopia'), value = 'deuteranopia' },
{ label = locale('ui.settings.colorblind_modes.tritanopia'), value = 'tritanopia' },
{ label = locale('ui.settings.colorblind_modes.achromatopsia'), value = 'achromatopsia' },
}
}
}) --[[@as table?]]
if not input then return end
local mode = input[1]
if mode and syncColorblindMode(mode) then
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale(getColorblindData(mode).label)),
type = 'success'
})
end
return
end
local options = {
{
title = locale('ui.settings.colorblind_modes.off'),
description = settings.colorblind_mode == 'off' and locale('ui.settings.colorblind_current') or nil,
icon = settings.colorblind_mode == 'off' and 'check' or 'circle',
onSelect = function()
syncColorblindMode('off')
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale('ui.settings.colorblind_modes.off')),
type = 'success'
})
end,
},
{
title = locale('ui.settings.colorblind_modes.protanopia'),
description = settings.colorblind_mode == 'protanopia' and locale('ui.settings.colorblind_current') or nil,
icon = settings.colorblind_mode == 'protanopia' and 'check' or 'circle',
onSelect = function()
syncColorblindMode('protanopia')
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale('ui.settings.colorblind_modes.protanopia')),
type = 'success'
})
end,
},
{
title = locale('ui.settings.colorblind_modes.deuteranopia'),
description = settings.colorblind_mode == 'deuteranopia' and locale('ui.settings.colorblind_current') or nil,
icon = settings.colorblind_mode == 'deuteranopia' and 'check' or 'circle',
onSelect = function()
syncColorblindMode('deuteranopia')
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale('ui.settings.colorblind_modes.deuteranopia')),
type = 'success'
})
end,
},
{
title = locale('ui.settings.colorblind_modes.tritanopia'),
description = settings.colorblind_mode == 'tritanopia' and locale('ui.settings.colorblind_current') or nil,
icon = settings.colorblind_mode == 'tritanopia' and 'check' or 'circle',
onSelect = function()
syncColorblindMode('tritanopia')
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale('ui.settings.colorblind_modes.tritanopia')),
type = 'success'
})
end,
},
{
title = locale('ui.settings.colorblind_modes.achromatopsia'),
description = settings.colorblind_mode == 'achromatopsia' and locale('ui.settings.colorblind_current') or nil,
icon = settings.colorblind_mode == 'achromatopsia' and 'check' or 'circle',
onSelect = function()
syncColorblindMode('achromatopsia')
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale('ui.settings.colorblind_modes.achromatopsia')),
type = 'success'
})
end,
},
}
lib.registerContext({
id = 'ox_lib_colorblind',
title = locale('ui.settings.colorblind'),
options = options,
})
local ok = pcall(lib.showContext, 'ox_lib_colorblind')
if ok then return end
local input = lib.inputDialog(locale('ui.settings.colorblind'), {
{
type = 'select',
label = locale('ui.settings.colorblind'),
default = settings.colorblind_mode,
required = true,
options = {
{ label = locale('ui.settings.colorblind_modes.off'), value = 'off' },
{ label = locale('ui.settings.colorblind_modes.protanopia'), value = 'protanopia' },
{ label = locale('ui.settings.colorblind_modes.deuteranopia'), value = 'deuteranopia' },
{ label = locale('ui.settings.colorblind_modes.tritanopia'), value = 'tritanopia' },
{ label = locale('ui.settings.colorblind_modes.achromatopsia'), value = 'achromatopsia' },
}
}
}) --[[@as table?]]
if not input then return end
local mode = input[1]
if mode and syncColorblindMode(mode) then
lib.notify({
title = locale('settings'),
description = locale('ui.settings.colorblind_saved', locale(getColorblindData(mode).label)),
type = 'success'
})
end
end
RegisterNUICallback('syncColorblindMode', function(data, cb)
local mode = type(data) == 'table' and data.mode or nil
if mode and colorblindModes[mode] then
syncColorblindMode(mode)
end
cb(1)
end)
RegisterCommand('ox_lib', function()
local inputSettings = {
{
type = 'checkbox',
label = locale('ui.settings.notification_audio'),
checked = settings.notification_audio,
},
{
type = 'select',
label = locale('ui.settings.notification_position'),
options = {
{ label = locale('ui.position.top-right'), value = 'top-right' },
{ label = locale('ui.position.top'), value = 'top' },
{ label = locale('ui.position.top-left'), value = 'top-left' },
{ label = locale('ui.position.center-right'), value = 'center-right' },
{ label = locale('ui.position.center-left'), value = 'center-left' },
{ label = locale('ui.position.bottom-right'), value = 'bottom-right' },
{ label = locale('ui.position.bottom'), value = 'bottom' },
{ label = locale('ui.position.bottom-left'), value = 'bottom-left' },
},
default = settings.notification_position,
required = true,
icon = 'message',
},
{
type = 'select',
label = locale('ui.settings.colorblind'),
options = {
{ label = locale('ui.settings.colorblind_modes.off'), value = 'off' },
{ label = locale('ui.settings.colorblind_modes.protanopia'), value = 'protanopia' },
{ label = locale('ui.settings.colorblind_modes.deuteranopia'), value = 'deuteranopia' },
{ label = locale('ui.settings.colorblind_modes.tritanopia'), value = 'tritanopia' },
{ label = locale('ui.settings.colorblind_modes.achromatopsia'), value = 'achromatopsia' },
},
default = settings.colorblind_mode,
required = true,
icon = 'eye',
},
{
type = 'select',
label = locale('ui.settings.ui_test_action'),
options = {
{ label = locale('ui.settings.ui_test_action_none'), value = 'none' },
{ label = locale('ui.settings.ui_test_action_circle'), value = 'circle' },
},
default = 'none',
required = true,
icon = 'flask',
},
}
if userLocales then
table.insert(inputSettings,
{
type = 'select',
label = locale('ui.settings.locale'),
searchable = true,
description = locale('ui.settings.locale_description', settings.locale),
options = GlobalState['ox_lib:locales'],
default = settings.locale,
required = true,
icon = 'book',
})
end
local input = lib.inputDialog(locale('settings'), inputSettings) --[[@as table?]]
if not input then return end
local notification_audio = input[1]
local notification_position = input[2]
local colorblind_mode = input[3]
local ui_test_action = input[4]
local localeInput = userLocales and input[5] or nil
if userLocales and set('locale', localeInput) then lib.setLocale(localeInput) end
set('notification_position', notification_position)
set('notification_audio', notification_audio)
syncColorblindMode(colorblind_mode)
if ui_test_action == 'circle' then
CreateThread(function()
lib.progressCircle({
duration = 5000,
label = locale('ui.settings.ui_test_action_circle'),
position = 'middle',
canCancel = true,
})
end)
end
end, false)
return settings