This repository has been archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_editor_addon.lua
244 lines (207 loc) · 11.5 KB
/
c_editor_addon.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
--[[
This resource is officially available on GitHub at
https://github.com/patrikjuvonen/editor_addon_lights
MIT License
Copyright (c) 2021 patrikjuvonen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local lightMatrix = {}
local function getRepresentation(element, type)
local elemTable = {}
for _, elem in pairs(getElementsByType(type, element)) do
if (elem ~= exports.edf:edfGetHandle(elem)) then
table.insert(elemTable, elem)
end
end
if (#elemTable == 0) then
return false
elseif (#elemTable == 1) then
return elemTable[1]
end
return elemTable
end
local function hex2rgba(hex)
local hex = hex:gsub("#", "")
return tonumber("0x" .. hex:sub(1, 2)), tonumber("0x" .. hex:sub(3, 4)), tonumber("0x" .. hex:sub(5, 6)), tonumber("0x" .. hex:sub(7, 8))
end
local function createLight(element)
element = element or source
if (not isElement(element)) or (getElementType(element) ~= "addon_light") then return end
if (isElement(lightMatrix[element])) then
if (getElementType(lightMatrix[source]) == "searchlight") then
destroyElement(lightMatrix[source])
elseif (not getElementData(lightMatrix[source], "deleting")) then
setElementData(lightMatrix[source], "deleting", true, false)
call(getResourceFromName("dl_lightmanager"), "destroyLight", lightMatrix[source])
end
end
local lightType = exports.edf:edfGetElementProperty(element, "type")
local x, y, z = exports.edf:edfGetElementPosition(element)
local _, _, rz = exports.edf:edfGetElementRotation(element)
local _tx, _ty, tz = unpack(exports.edf:edfGetElementProperty(element, "target"))
local tx, ty = x + (_tx - x) * math.cos(math.rad(rz)) - (_ty - y) * math.sin(math.rad(rz)), y + (_tx - x) * math.sin(math.rad(rz)) + (_ty - y) * math.cos(math.rad(rz))
local r, g, b, a = hex2rgba(exports.edf:edfGetElementProperty(element, "color"))
local attenuation = exports.edf:edfGetElementProperty(element, "attenuation") or 5
local dimension = exports.edf:edfGetElementDimension(element) or 0
local interior = exports.edf:edfGetElementInterior(element) or 0
if (lightType == "GTASearchLight") then
lightMatrix[element] = createSearchLight(
x, y, z,
tx, ty, tz,
exports.edf:edfGetElementProperty(element, "startRadius"),
exports.edf:edfGetElementProperty(element, "endRadius"),
exports.edf:edfGetElementProperty(element, "renderSpot") == "true"
)
setElementDimension(lightMatrix[element], dimension)
setElementInterior(lightMatrix[element], interior)
else
if (lightType == "DLPointLight") then
lightMatrix[element] = call(
getResourceFromName("dl_lightmanager"), "createPointLight",
x, y, z,
r, g, b, a,
attenuation,
exports.edf:edfGetElementProperty(element, "generateNormals") == "true", exports.edf:edfGetElementProperty(element, "skipNormals") == "true", dimension, interior
)
elseif (lightType == "DLSpotLight") then
lightMatrix[element] = call(
getResourceFromName("dl_lightmanager"), "createSpotLight",
x, y, z,
r, g, b, a,
tx, ty, tz,
exports.edf:edfGetElementProperty(element, "falloff") or 5,
exports.edf:edfGetElementProperty(element, "theta") or 5,
exports.edf:edfGetElementProperty(element, "phi") or 10,
attenuation,
exports.edf:edfGetElementProperty(element, "generateNormals") == "true", exports.edf:edfGetElementProperty(element, "skipNormals") == "true", dimension, interior
)
end
call(getResourceFromName("dl_lightmanager"), "setLightDimension", lightMatrix[element], dimension)
call(getResourceFromName("dl_lightmanager"), "setLightInterior", lightMatrix[element], interior)
addEventHandler("onClientElementDestroy", lightMatrix[element], function ()
if (getElementData(source, "deleting")) then return end
setElementData(source, "deleting", true, false)
call(getResourceFromName("dl_lightmanager"), "destroyLight", source)
end)
end
setElementParent(lightMatrix[element], element)
setElementAlpha(getRepresentation(element, "marker"), 0)
end
addEventHandler("onClientElementCreate", root, function ()
setElementData(source, "unplaced", true, false)
createLight(source)
end)
local function localCleanUp()
if (not isElement(source)) or (getElementType(source) ~= "addon_light") or (not lightMatrix[source]) then return end
if (isElement(lightMatrix[source])) then
if (getElementType(lightMatrix[source]) == "searchlight") then
destroyElement(lightMatrix[source])
elseif (not getElementData(lightMatrix[source], "deleting")) then
setElementData(lightMatrix[source], "deleting", true, false)
call(getResourceFromName("dl_lightmanager"), "destroyLight", lightMatrix[source])
end
end
lightMatrix[source] = nil
end
addEventHandler("onClientElementDestroyed", root, localCleanUp)
addEventHandler("onClientElementDestroy", root, localCleanUp)
addEventHandler("onClientElementPropertyChanged", root, function (propertyName, v)
if (not isElement(source)) or (getElementType(source) ~= "addon_light") or (not lightMatrix[source]) then return end
if (not isElement(lightMatrix[source])) then
createLight(source)
return
end
if (getElementType(lightMatrix[source]) ~= "searchlight") then
if (propertyName == "position") then
call(getResourceFromName("dl_lightmanager"), "setLightPosition", lightMatrix[source], exports.edf:edfGetElementPosition(source))
elseif (propertyName == "rotation") then
call(getResourceFromName("dl_lightmanager"), "setLightRotation", lightMatrix[source], exports.edf:edfGetElementRotation(source))
elseif (propertyName == "target") then
call(getResourceFromName("dl_lightmanager"), "setLightDirection", lightMatrix[source], unpack(exports.edf:edfGetElementProperty(source, "target") or {0, 0, 0}))
elseif (propertyName == "color") then
call(getResourceFromName("dl_lightmanager"), "setLightColor", lightMatrix[source], hex2rgba(exports.edf:edfGetElementProperty(source, "color") or "#ffffffff"))
elseif (propertyName == "attenuation") then
call(getResourceFromName("dl_lightmanager"), "setLightAttenuation", lightMatrix[source], exports.edf:edfGetElementProperty(source, "attenuation") or 5)
elseif (propertyName == "falloff") then
call(getResourceFromName("dl_lightmanager"), "setLightFalloff", lightMatrix[source], exports.edf:edfGetElementProperty(source, "falloff") or 5)
elseif (propertyName == "theta") then
call(getResourceFromName("dl_lightmanager"), "setLightTheta", lightMatrix[source], exports.edf:edfGetElementProperty(source, "theta") or 5)
elseif (propertyName == "phi") then
call(getResourceFromName("dl_lightmanager"), "setLightPhi", lightMatrix[source], exports.edf:edfGetElementProperty(source, "phi") or 5)
else
createLight(source)
end
elseif (propertyName == "position") then
setSearchLightStartPosition(lightMatrix[source], getElementPosition(source))
elseif (propertyName == "target") then
setSearchLightEndPosition(lightMatrix[source], Vector3(exports.edf:edfGetElementProperty(source, "target") or {0, 0, 0}))
elseif (propertyName == "startRadius") then
setSearchLightStartRadius(lightMatrix[source], exports.edf:edfGetElementProperty(source, "startRadius") or 0.1)
elseif (propertyName == "endRadius") then
setSearchLightEndRadius(lightMatrix[source], exports.edf:edfGetElementProperty(source, "endRadius") or 20)
else
createLight(source)
end
end)
addEvent("onClientElementDrop", true)
addEventHandler("onClientElementDrop", root, function ()
if (not isElement(source)) or (getElementType(source) ~= "addon_light") or (not lightMatrix[source]) then return end
setElementData(source, "unplaced", nil)
end)
addEventHandler("onClientPreRender", root, function ()
for element, light in pairs(lightMatrix) do
if (isElement(element)) then
if (exports.edf:edfGetElementProperty(element, "type") == "GTASearchLight") then
setSearchLightStartPosition(light, exports.edf:edfGetElementPosition(element))
local _, _, rz = exports.edf:edfGetElementRotation(element)
local x, y, z = exports.edf:edfGetElementPosition(element)
if (getElementData(element, "unplaced")) then
exports.edf:edfSetElementProperty(element, "target", { x, y, getGroundPosition(x, y, z) })
end
local tx, ty, tz = unpack(exports.edf:edfGetElementProperty(element, "target"))
setSearchLightEndPosition(
light,
x + (tx - x) * math.cos(math.rad(rz)) - (ty - y) * math.sin(math.rad(rz)),
y + (tx - x) * math.sin(math.rad(rz)) + (ty - y) * math.cos(math.rad(rz)),
tz
)
else
call(getResourceFromName("dl_lightmanager"), "setLightPosition", light, exports.edf:edfGetElementPosition(element))
call(getResourceFromName("dl_lightmanager"), "setLightRotation", light, exports.edf:edfGetElementRotation(element))
end
end
end
end)
function onStart()
for _, element in pairs(getElementsByType("addon_light")) do
createLight(element)
end
end
function onStop()
for element, light in pairs(lightMatrix) do
if (isElement(light)) then
if (getElementType(light) == "searchlight") then
destroyElement(light)
elseif (not getElementData(light, "deleting")) then
setElementData(light, "deleting", true, false)
call(getResourceFromName("dl_lightmanager"), "destroyLight", light)
end
end
lightMatrix[element] = nil
end
end
addEventHandler("onClientResourceStop", resourceRoot, onStop)