-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
377 lines (326 loc) · 10.4 KB
/
main.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
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
374
375
376
377
local toggleGuiKey = "F4"
--### Development Mode ###
function enableDevMode(resource)
setDevelopmentMode(true)
outputChatBox("Enabled Development Mode (use 'showcol' command to make colshapes visible)")
bindKey(toggleGuiKey,"down",toggleGui)
outputChatBox("Toggle SCM Helper GUI with "..toggleGuiKey)
end
addEventHandler("onClientResourceStart", getResourceRootElement(), enableDevMode)
function disableDevMode(resource)
setDevelopmentMode(false)
outputChatBox("Disabled Development Mode")
closeGui()
end
addEventHandler("onClientResourceStop", getResourceRootElement(), disableDevMode)
local originalWeather
function setClearWeather()
originalWeather = getWeather()
setWeather(0)
setFogDistance(300)
end
function resetClearWeather()
resetFogDistance()
setWeather(originalWeather)
end
local gui = {}
local markers = {}
local blips = {}
local linePosition = {}
local previewMarker = nil
local previewRow = -1
local currentFocus
local lastTeleportTo
local wasFreecamEnabled = false
function openGui()
createGui()
guiSetVisible(gui.window, true)
guiSetInputEnabled(true)
wasFreecamEnabled = exports.freecam:isFreecamEnabled()
exports.freecam:setFreecamDisabled()
showCursor(true)
end
function closeGui()
guiSetVisible(gui.window, false)
guiSetInputEnabled(false)
if wasFreecamEnabled then
-- Unfortunately this doesn't really work, since setFreecamEnabled doesn't take lookAt coordinates
-- (and it doesn't even take the angle from getCameraMatrix() but rather holds it in it's own variable)
exports.freecam:setFreecamEnabled()
end
showCursor(false)
end
function isGuiOpen()
return gui.window ~= nil and guiGetVisible(gui.window)
end
function toggleGui()
if isGuiOpen() then
closeGui()
else
openGui()
end
end
function table.contains(table, element)
for _,value in pairs(table) do
if value == element then
return true
end
end
return false
end
local function setRowState(row, state)
guiGridListSetItemText(gui.list, row, gui.stateColumn, state, false, false)
end
local function clearElements(elements)
for _,element in pairs(elements) do
destroyElement(element)
end
end
local function clearMarkers()
clearElements(markers)
markers = {}
if previewMarker ~= nil then
destroyElement(previewMarker)
previewRow = -1
previewMarker = nil
end
end
local function clearBlips()
clearElements(blips)
blips = {}
end
local function getSelectedMarker()
local row,_ = guiGridListGetSelectedItem(gui.list)
local marker = markers[row]
if marker == nil then
if previewMarker ~= nil then
return previewMarker
else
return false
end
end
return marker
end
local function cameraToSelected()
local marker = getSelectedMarker()
if not marker then
return
end
local x,y,z = getElementPosition(marker)
local radius = 100
if exports.freecam:isFreecamEnabled() then
exports.freecam:setFreecamDisabled()
end
setCameraMatrix(x+radius,y+radius,z+radius,x,y,z)
end
local function clearPreview()
local row,_ = guiGridListGetSelectedItem(gui.list)
if previewMarker ~= nil then
destroyElement(previewMarker)
previewMarker = nil
if previewRow ~= -1 then
setRowState(previewRow, "")
previewRow = -1
end
end
end
local function toggleMarkerForSelected()
clearPreview()
local row,_ = guiGridListGetSelectedItem(gui.list)
local newState
if markers[row] ~= nil then
destroyElement(markers[row])
destroyElement(blips[row])
newState = ""
markers[row] = nil
blips[row] = nil
else
local code = guiGridListGetItemText(gui.list, row, gui.codeColumn)
local result = createMarkerForCode(code)
if result == false then
newState = "Error"
else
markers[row] = result
newState = "Showing"
blips[row] = createBlipAttachedTo(result)
end
end
guiGridListSetItemText(gui.list, row, gui.stateColumn, newState, false, false)
end
local function updateList()
clearMarkers()
clearBlips()
guiGridListClear(gui.list)
local text = guiGetText(gui.code)
local lines = text:split("\n")
local section
local coordinates = {}
local textPos = 0
for i,line in ipairs(lines) do
local split = line:split(" ")
local checkForCoordinates = string.match(line, "^%d+@%s+=%s+(-?%d+%.%d+)%s*$")
if split[1] ~= nil and split[1]:starts(":") then
section = line
elseif split[1] ~= nil and table.contains(opcodesDef, split[1]) then
local row = guiGridListAddRow(gui.list)
guiGridListSetItemText(gui.list, row, gui.lineColumn, tostring(i), false, true)
guiGridListSetItemText(gui.list, row, gui.codeColumn, line, false, false)
end
if checkForCoordinates ~= nil then
coordinates[#coordinates+1] = tonumber(checkForCoordinates)
if #coordinates == 3 then
local row = guiGridListAddRow(gui.list)
line = section.." COORDINATES:"..coordinates[1].." "..coordinates[2].." "..coordinates[3]
guiGridListSetItemText(gui.list, row, gui.lineColumn, tostring(i), false, true)
guiGridListSetItemText(gui.list, row, gui.codeColumn, line, false, false)
end
else
coordinates = {}
end
linePosition[i] = textPos
textPos = textPos + line:len() + 1 -- Add 1 to adjust for linebreaks
end
guiSetSelectedTab(gui.tabs, gui.tabList)
end
--### GUI Events ###
local function listItemChanged()
local row,_ = guiGridListGetSelectedItem(gui.list)
clearPreview()
if row == -1 then
return
end
-- Jump to selected line
local codeLine = tonumber(guiGridListGetItemText(gui.list, row, gui.lineColumn))
guiMemoSetCaretIndex(gui.code, guiGetText(gui.code):len()) -- Jump to end first, so selected line is always at the top
guiMemoSetCaretIndex(gui.code, linePosition[codeLine])
if guiCheckBoxGetSelected(gui.previewEnabled) and markers[row] == nil then
local code = guiGridListGetItemText(gui.list, row, gui.codeColumn)
local result = createMarkerForCode(code)
if result then
previewMarker = result
previewRow = row
setRowState(row, "Preview")
if guiCheckBoxGetSelected(gui.previewCameraEnabled) then
cameraToSelected()
end
else
setRowState(row, "Error")
end
end
end
local function handleClick()
if source == gui.closeButton then
closeGui()
elseif source == gui.parseButton then
updateList()
elseif source == gui.list then
listItemChanged()
elseif source == gui.clearWeatherButton then
if guiCheckBoxGetSelected(gui.clearWeatherButton) then
setClearWeather()
else
resetClearWeather()
end
elseif source == gui.morningTime then
setTime(6,20)
elseif source == gui.dayTime then
setTime(12,00)
elseif source == gui.fixedTime then
if guiCheckBoxGetSelected(gui.fixedTime) then
setMinuteDuration(2147483647)
else
setMinuteDuration(1000)
end
end
end
local function handleDoubleClick()
if source == gui.list then
toggleMarkerForSelected()
end
end
local function isFocusOnEdit()
if currentFocus == nil then
return false
end
local elementType = getElementType(currentFocus)
if elementType == "gui-memo" or elementType == "gui-edit" then
return true
end
return false
end
local function handleKeyEvent(button, pressed)
if not pressed then
return
end
if isFocusOnEdit() then
return
end
if button == "q" then
toggleGui()
end
if not isGuiOpen() then
return
end
if currentFocus == gui.list then
local row,_ = guiGridListGetSelectedItem(gui.list)
if button == "w" then
if row == -1 then
-- Start from the bottom when nothing is selected
row = guiGridListGetRowCount(gui.list)
end
guiGridListSetSelectedItem(gui.list, row-1, 1)
listItemChanged()
elseif button == "s" then
-- If nothing is selected, the row is -1, so it starts at the top
guiGridListSetSelectedItem(gui.list, row+1, 1)
listItemChanged()
elseif button == "e" then
toggleMarkerForSelected()
elseif button == "t" then
local marker = getSelectedMarker()
local x,y,z = getElementPosition(marker)
if lastTeleportTo == marker then
z = z+20
end
lastTeleportTo = marker
triggerServerEvent("teleportToMarker", getRootElement(), x, y, z)
elseif button == "g" then
cameraToSelected()
end
end
end
local function handleFocusEvent()
currentFocus = source
end
--### Create GUI ###
function createGui()
if gui.window ~= nil then
return
end
gui.window = guiCreateWindow(320, 240, 1000, 560, "Stuff", false)
gui.tabs = guiCreateTabPanel(0,20,1000,500,false,gui.window)
gui.tabCode = guiCreateTab("Code",gui.tabs)
gui.tabList = guiCreateTab("List",gui.tabs)
gui.tabSettings = guiCreateTab("Settings/Help",gui.tabs)
-- Code
gui.code = guiCreateMemo(1, 1, 980, 440, "00FE: actor $PLAYER_ACTOR sphere 0 in_sphere 812.4495 -1343.488 12.532 radius 80.0 80.0 20.0 // Must be in this area to spawn taxi\n00A4: actor $PLAYER_ACTOR sphere 0 in_cube_cornerA 685.9716 -1433.523 11.0857 cornerB 965.9512 -1379.386 14.9731 // If in this area, then the taxi spawns in the north", false, gui.tabCode)
gui.parseButton = guiCreateButton(1, 442, 980, 34, "Parse", false, gui.tabCode)
-- List
gui.list = guiCreateGridList(1,1,980,440,false,gui.tabList)
gui.lineColumn = guiGridListAddColumn(gui.list, "Line", 0.05)
gui.codeColumn = guiGridListAddColumn(gui.list, "Code", 0.85)
gui.stateColumn = guiGridListAddColumn(gui.list, "State", 0.1)
gui.previewEnabled = guiCreateCheckBox(10,443,100,30,"Preview",false,false,gui.tabList)
gui.previewCameraEnabled = guiCreateCheckBox(100,443,100,30,"Preview Camera",false,false,gui.tabList)
-- Settings
gui.clearWeatherButton = guiCreateCheckBox(12, 14, 200, 25, "Clear weather", false, false, gui.tabSettings)
gui.morningTime = guiCreateButton(300, 14, 100, 25, "Set to morning", false, gui.tabSettings)
gui.dayTime = guiCreateButton(420, 14, 100, 25, "Set to day", false, gui.tabSettings)
gui.fixedTime = guiCreateCheckBox(540, 14, 200, 30, "Fixed Time", false, false, gui.tabSettings)
gui.help = guiCreateMemo(10, 50, 800, 300, "You must enter 'showcol' into the console to show the wireframes.\n\nF4 or Q to toggle this GUI (if in map editor, F4 toggles HUD as well).\n\nShorcuts in List:\nW/S to go up/down the list\nG to move the camera to the selected entry (only if wireframe is showing)\nE to show wireframe for selected entry\nT to teleport to selected entry and give jetpack (only if wireframe is showing)\n\nYou can use the settings above to create better screenshot conditions.", false, gui.tabSettings)
gui.closeButton = guiCreateButton(0, 528, 1000, 30, "Close", false, gui.window)
addEventHandler("onClientGUIClick", gui.window, handleClick)
addEventHandler("onClientGUIDoubleClick", gui.window, handleDoubleClick)
addEventHandler("onClientGUIFocus", gui.window, handleFocusEvent)
end
addEventHandler("onClientKey", getRootElement(), handleKeyEvent)