-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.lua
More file actions
303 lines (300 loc) · 8.97 KB
/
client.lua
File metadata and controls
303 lines (300 loc) · 8.97 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
local web = CreateWebUI(0, 0, 0, 0, 1, 16)
SetWebAlignment(web, 0, 0)
SetWebAnchors(web, 0, 0, 1, 1)
SetWebURL(web, "http://asset/"..GetPackageName().."/dialog.html")
SetWebVisibility(web, WEB_HITINVISIBLE)
local nextId = 1
local dialogs = {}
local lastOpened = -1
local globalTheme = "themes/default-dark.css"
function createDialog(title, text, ...)
local id = nextId
nextId = nextId + 1
dialogs[id] = {
title = title,
text = text,
columns = { { inputs = {}, buttons = {} } },
buttons = {...},
variables = {},
autoclose = "true"
}
return id
end
function setDialogButtons(dialog, column, ...)
if dialogs[dialog] == nil then
return
end
if column == 0 then
dialogs[dialog].buttons = {...}
return
end
if dialogs[dialog].columns[column] == nil then
dialogs[dialog].columns[column] = {
inputs = {},
buttons = {}
}
end
dialogs[dialog].columns[column].buttons = {...}
end
function addDialogSelect(dialog, column, label, size, ...)
if dialogs[dialog] == nil then
return
end
if dialogs[dialog].columns[column] == nil then
dialogs[dialog].columns[column] = {
inputs = {},
buttons = {}
}
end
table.insert(dialogs[dialog].columns[column].inputs, {
type = "select",
name = label,
size = size,
labelMode = false,
options = {...}
})
return #dialogs[dialog].columns[column].inputs
end
function addDialogCheckbox(dialog, column, label)
if dialogs[dialog] == nil then
return
end
if dialogs[dialog].columns[column] == nil then
dialogs[dialog].columns[column] = {
inputs = {},
buttons = {}
}
end
table.insert(dialogs[dialog].columns[column].inputs, {
type = "checkbox",
name = label
})
return #dialogs[dialog].columns[column].inputs
end
function setDialogSelectOptions(dialog, column, input, ...)
if dialogs[dialog] == nil then
return
end
if dialogs[dialog].columns[column] == nil then
return
end
if dialogs[dialog].columns[column].inputs[input] == nil then
return
end
dialogs[dialog].columns[column].inputs[input].labelMode = false
if dialogs[dialog].columns[column].inputs[input].options == nil then
return
end
dialogs[dialog].columns[column].inputs[input].options = {...}
end
function setDialogSelectOptionsWithLabels(dialog, column, input, options)
if dialogs[dialog] == nil then
return
end
if dialogs[dialog].columns[column] == nil then
return
end
if dialogs[dialog].columns[column].inputs[input] == nil then
return
end
dialogs[dialog].columns[column].inputs[input].labelMode = true
if dialogs[dialog].columns[column].inputs[input].options == nil then
return
end
for k,v in pairs(options) do
if type(k) == "number" then
options[tostring(k)] = options[k]
options[k] = nil
end
end
dialogs[dialog].columns[column].inputs[input].options = options
end
function addDialogTextInput(dialog, column, label)
if dialogs[dialog] == nil then
return
end
if dialogs[dialog].columns[column] == nil then
dialogs[dialog].columns[column] = {
inputs = {},
buttons = {}
}
end
table.insert(dialogs[dialog].columns[column].inputs, {
type = "text",
name = label
})
return #dialogs[dialog].columns[column].inputs
end
function setVariable(dialog, name, value)
if dialogs[dialog] == nil then
return
end
dialogs[dialog].variables[name] = value
end
function setDialogAutoclose(dialog, autoclose)
if dialogs[dialog] == nil then
return
end
if autoclose then
dialogs[dialog].autoclose = "true"
else
dialogs[dialog].autoclose = "false"
end
end
function replaceVariables(text, variables)
for k,v in pairs(variables) do
text = text:gsub("{"..k.."}", v)
end
return text
end
function closeDialog()
if dialogs[lastOpened].theme ~= nil then
applyTheme(globalTheme)
end
lastOpened = -1
ExecuteWebJS(web, "CloseDialog();");
SetIgnoreLookInput(false)
SetIgnoreMoveInput(false)
ShowMouseCursor(false)
SetInputMode(INPUT_GAME)
SetWebVisibility(web, WEB_HITINVISIBLE)
end
function destroyDialog(dialog)
if lastOpened == dialog then
closeDialog()
end
dialogs[dialog] = nil
end
function showDialog(dialog)
if dialogs[dialog] == nil then
return
end
lastOpened = dialog
if dialogs[dialog].theme ~= nil then
applyTheme(dialogs[dialog].theme)
else
applyTheme(globalTheme)
end
local d = dialogs[dialog]
local json = {
autoclose = d.autoclose == "true",
columns = {},
buttons = {}
}
if d.title ~= nil then
json["title"] = replaceVariables(d.title, d.variables)
end
if d.text ~= nil then
json["text"] = replaceVariables(d.text, d.variables)
end
for j=1,#d.columns do
json.columns[j] = {}
if d.columns[j].inputs ~= nil then
json.columns[j].inputs = {}
for i=1,#d.columns[j].inputs do
json.columns[j].inputs[i] = {
type = d.columns[j].inputs[i].type
}
if d.columns[j].inputs[i].name ~= nil then
json.columns[j].inputs[i].name = replaceVariables(d.columns[j].inputs[i].name, d.variables)
end
if d.columns[j].inputs[i].options ~= nil then
json.columns[j].inputs[i].options = {}
if d.columns[j].inputs[i].labelMode then
for k,v in pairs(d.columns[j].inputs[i].options) do
json.columns[j].inputs[i].options[k] = v
end
else
for k=1,#d.columns[j].inputs[i].options do
table.insert(json.columns[j].inputs[i].options, d.columns[j].inputs[i].options[k])
end
end
end
if d.columns[j].inputs[i].size ~= nil then
json.columns[j].inputs[i].size = d.columns[j].inputs[i].size
end
end
end
json.columns[j].buttons = {}
for i=1,#d.columns[j].buttons do
table.insert(json.columns[j].buttons, replaceVariables(d.columns[j].buttons[i], d.variables))
end
end
for i=1,#d.buttons do
table.insert(json.buttons, replaceVariables(d.buttons[i], d.variables))
end
SetWebVisibility(web, WEB_VISIBLE)
ExecuteWebJS(web, "SetDialog("..dialog..","..json_encode(json)..");")
SetIgnoreLookInput(true)
SetIgnoreMoveInput(true)
ShowMouseCursor(true)
SetInputMode(INPUT_GAMEANDUI)
end
function getCurrent()
if lastOpened ~= -1 then
return lastOpened
end
end
function isVisible()
return getCurrent() ~= nil
end
function applyTheme(theme)
ExecuteWebJS(web, "SetTheme(\""..theme.."\");")
end
function setDialogTheme(dialog, theme)
if dialogs[dialog] == nil then
return
end
if (theme:len() > 5) and (theme:sub(1,5) == "http:") then
dialogs[dialog].theme = theme
else
dialogs[dialog].theme = "themes/"..theme..".css"
end
end
function setGlobalTheme(theme)
globalTheme = theme
if (theme:len() > 5) and (theme:sub(1,5) == "http:") then
globalTheme = theme
else
globalTheme = "themes/"..theme..".css"
end
end
AddEvent("__dialog_system_closed", function()
lastOpened = -1
SetIgnoreLookInput(false)
SetIgnoreMoveInput(false)
ShowMouseCursor(false)
SetInputMode(INPUT_GAME)
SetWebVisibility(web, WEB_HITINVISIBLE)
end)
AddEvent("OnHideMainMenu", function()
if lastOpened ~= -1 then
Delay(1, function()
SetIgnoreLookInput(true)
SetIgnoreMoveInput(true)
ShowMouseCursor(true)
SetInputMode(INPUT_GAMEANDUI)
end)
end
end)
AddEvent("OnDialogUIReady", function()
if lastOpened ~= -1 then
showDialog(lastOpened)
end
end)
AddFunctionExport("create", createDialog)
AddFunctionExport("setButtons", setDialogButtons)
AddFunctionExport("addSelect", addDialogSelect)
AddFunctionExport("addTextInput", addDialogTextInput)
AddFunctionExport("addCheckbox", addDialogCheckbox)
AddFunctionExport("setVariable", setVariable)
AddFunctionExport("show", showDialog)
AddFunctionExport("close", closeDialog)
AddFunctionExport("destroy", destroyDialog)
AddFunctionExport("setSelectOptions", setDialogSelectOptions)
AddFunctionExport("setSelectLabeledOptions", setDialogSelectOptionsWithLabels)
AddFunctionExport("setAutoClose", setDialogAutoclose)
AddFunctionExport("setGlobalTheme", setGlobalTheme)
AddFunctionExport("setDialogTheme", setDialogTheme)
AddFunctionExport("getCurrent", getCurrent)
AddFunctionExport("isVisible", isVisible)