LUADOC - Farming Simulator 22

GuiOverlay

Description
GUI overlay manager. Handles creation, loading and basic rendering of GUI overlays. This module has no interaction with Overlay.lua.
Functions

copyOverlay

Description
Copy an overlay.
Definition
copyOverlay()
Code
277function GuiOverlay.copyOverlay(overlay, overlaySrc, overrideFilename)
278 overlay.filename = overrideFilename or overlaySrc.filename
279 overlay.uvs = {overlaySrc.uvs[1], overlaySrc.uvs[2], overlaySrc.uvs[3], overlaySrc.uvs[4], overlaySrc.uvs[5], overlaySrc.uvs[6], overlaySrc.uvs[7], overlaySrc.uvs[8]}
280 overlay.color = table.copyIndex(overlaySrc.color)
281 overlay.rotation = overlaySrc.rotation
282 overlay.alpha = overlaySrc.alpha
283 overlay.isWebOverlay = overlaySrc.isWebOverlay
284 overlay.previewFilename = overlaySrc.previewFilename
285 overlay.sdfWidth = overlaySrc.sdfWidth
286
287 if overlaySrc.uvsFocused ~= nil then
288 overlay.uvsFocused = {overlaySrc.uvsFocused[1], overlaySrc.uvsFocused[2], overlaySrc.uvsFocused[3], overlaySrc.uvsFocused[4], overlaySrc.uvsFocused[5], overlaySrc.uvsFocused[6], overlaySrc.uvsFocused[7], overlaySrc.uvsFocused[8]}
289 end
290
291 if overlaySrc.colorFocused ~= nil then
292 overlay.colorFocused = table.copyIndex(overlaySrc.colorFocused)
293 end
294
295 if overlaySrc.uvsPressed ~= nil then
296 overlay.uvsPressed = {overlaySrc.uvsPressed[1], overlaySrc.uvsPressed[2], overlaySrc.uvsPressed[3], overlaySrc.uvsPressed[4], overlaySrc.uvsPressed[5], overlaySrc.uvsPressed[6], overlaySrc.uvsPressed[7], overlaySrc.uvsPressed[8]}
297 end
298
299 if overlaySrc.colorPressed ~= nil then
300 overlay.colorPressed = table.copyIndex(overlaySrc.colorPressed)
301 end
302
303 if overlaySrc.uvsSelected ~= nil then
304 overlay.uvsSelected = {overlaySrc.uvsSelected[1], overlaySrc.uvsSelected[2], overlaySrc.uvsSelected[3], overlaySrc.uvsSelected[4], overlaySrc.uvsSelected[5], overlaySrc.uvsSelected[6], overlaySrc.uvsSelected[7], overlaySrc.uvsSelected[8]}
305 end
306
307 if overlaySrc.colorSelected ~= nil then
308 overlay.colorSelected = table.copyIndex(overlaySrc.colorSelected)
309 end
310
311 if overlaySrc.uvsDisabled ~= nil then
312 overlay.uvsDisabled = {overlaySrc.uvsDisabled[1], overlaySrc.uvsDisabled[2], overlaySrc.uvsDisabled[3], overlaySrc.uvsDisabled[4], overlaySrc.uvsDisabled[5], overlaySrc.uvsDisabled[6], overlaySrc.uvsDisabled[7], overlaySrc.uvsDisabled[8]}
313 end
314
315 if overlaySrc.colorDisabled ~= nil then
316 overlay.colorDisabled = table.copyIndex(overlaySrc.colorDisabled)
317 end
318
319 if overlaySrc.uvsHighlighted ~= nil then
320 overlay.uvsHighlighted = {overlaySrc.uvsHighlighted[1], overlaySrc.uvsHighlighted[2], overlaySrc.uvsHighlighted[3], overlaySrc.uvsHighlighted[4], overlaySrc.uvsHighlighted[5], overlaySrc.uvsHighlighted[6], overlaySrc.uvsHighlighted[7], overlaySrc.uvsHighlighted[8]}
321 end
322
323 if overlaySrc.colorHighlighted ~= nil then
324 overlay.colorHighlighted = table.copyIndex(overlaySrc.colorHighlighted)
325 end
326
327 return GuiOverlay.createOverlay(overlay)
328end

createOverlay

Description
(Re-)Create an overlay.
Definition
createOverlay(overlay Overlay, filename Path)
Arguments
overlayOverlaytable, see loadOverlay()
filenamePathto image file (can also be a URL for web images)
Return Values
Overlaytablewith added image data
Code
236function GuiOverlay.createOverlay(overlay, filename)
237 if filename ~= nil then
238 filename = string.gsub(filename, "$l10nSuffix", g_gui.languageSuffix)
239 end
240
241 if overlay.overlay ~= nil and (overlay.filename == filename or filename == nil) then
242 return overlay -- already created / loaded
243 end
244
245 -- delete previously created overlay, if necessary
246 GuiOverlay.deleteOverlay(overlay)
247
248 if filename ~= nil then
249 overlay.filename = filename
250 end
251
252 if overlay.filename ~= nil then
253 local imageOverlay
254 if overlay.isWebOverlay == nil or not overlay.isWebOverlay or (overlay.isWebOverlay and not overlay.filename:startsWith("http")) then
255 imageOverlay = createImageOverlay(overlay.filename)
256 else
257 imageOverlay = createWebImageOverlay(overlay.filename, overlay.previewFilename)
258 end
259
260 if imageOverlay ~= 0 then
261 overlay.overlay = imageOverlay
262
263 if overlay.sdfWidth ~= nil then
264 setOverlaySignedDistanceFieldWidth(imageOverlay, overlay.sdfWidth)
265 end
266 end
267 end
268
269 overlay.rotation = Utils.getNoNil(overlay.rotation, 0)
270 overlay.alpha = Utils.getNoNil(overlay.alpha, 1)
271
272 return overlay
273end

deleteOverlay

Description
Delete an overlay. Primarily releases the associated image file handle.
Definition
deleteOverlay()
Code
333function GuiOverlay.deleteOverlay(overlay)
334 if overlay ~= nil and overlay.overlay ~= nil then
335 delete(overlay.overlay)
336 overlay.overlay = nil
337 end
338end

getOverlayColor

Description
Get an overlay's color for a given overlay state.
Definition
getOverlayColor(overlay Overlay, state GuiOverlay.STATE_[...])
Arguments
overlayOverlaytable
stateGuiOverlay.STATE_[...]constant value
Return Values
Coloras{red, green, blue, alpha} with all values in the range of [0, 1]
Code
345function GuiOverlay.getOverlayColor(overlay, state)
346 local color = nil
347 if state == GuiOverlay.STATE_NORMAL then
348 color = overlay.color
349 elseif state == GuiOverlay.STATE_DISABLED then
350 color = overlay.colorDisabled
351 elseif state == GuiOverlay.STATE_FOCUSED then
352 color = overlay.colorFocused
353 elseif state == GuiOverlay.STATE_SELECTED then
354 color = overlay.colorSelected
355 elseif state == GuiOverlay.STATE_HIGHLIGHTED then
356 color = overlay.colorHighlighted
357 elseif state == GuiOverlay.STATE_PRESSED then
358 color = overlay.colorPressed
359 if color == nil then
360 color = overlay.colorFocused
361 end
362 end
363
364 if color == nil then
365 color = overlay.color
366 end
367
368 return color
369end

getOverlayUVs

Description
Get an overlay's UV coordinates for a given overlay state.
Definition
getOverlayUVs(overlay Overlay, state GuiOverlay.STATE_[...])
Arguments
overlayOverlaytable
stateGuiOverlay.STATE_[...]constant value
Return Values
UVcoordinatesas {u1, v1, u2, v2, u3, v3, u4x, v4} with all values in the range of [0, 1]
Code
376function GuiOverlay.getOverlayUVs(overlay, state)
377 local uvs = nil
378 if state == GuiOverlay.STATE_DISABLED then
379 uvs = overlay.uvsDisabled
380 elseif state == GuiOverlay.STATE_FOCUSED then
381 uvs = overlay.uvsFocused
382 elseif state == GuiOverlay.STATE_SELECTED then
383 uvs = overlay.uvsSelected
384 elseif state == GuiOverlay.STATE_HIGHLIGHTED then
385 uvs = overlay.uvsHighlighted
386 elseif state == GuiOverlay.STATE_PRESSED then
387 uvs = overlay.uvsPressed
388 if uvs == nil then
389 uvs = overlay.uvsFocused
390 end
391 end
392 if uvs == nil then
393 uvs = overlay.uvs
394 end
395
396 return uvs
397end

loadOverlay

Description
Loads overlay data from XML or a profile into a table to turn it into an overlay.
Definition
loadOverlay()
Code
19function GuiOverlay.loadOverlay(self, overlay, overlayName, imageSize, profile, xmlFile, key)
20 if overlay.uvs == nil then
21 overlay.uvs = Overlay.DEFAULT_UVS
22 end
23
24 if overlay.color == nil then
25 overlay.color = {1, 1, 1, 1}
26 end
27
28 local filename = nil
29 local previewFilename = nil
30 if xmlFile ~= nil then
31 filename = getXMLString(xmlFile, key .. "#" .. overlayName .. "Filename")
32 previewFilename = getXMLString(xmlFile, key .. "#" .. overlayName .. "PreviewFilename")
33
34 GuiOverlay.loadXMLUVs(xmlFile, key, overlay, overlayName, imageSize)
35 GuiOverlay.loadXMLColors(xmlFile, key, overlay, overlayName)
36
37 overlay.sdfWidth = Utils.getNoNil(getXMLInt(xmlFile, key .. "#" .. overlayName .. "SdfWidth"), overlay.sdfWidth)
38 elseif profile ~= nil then
39 filename = profile:getValue(overlayName .. "Filename")
40 previewFilename = profile:getValue(overlayName .. "PreviewFilename")
41
42 GuiOverlay.loadProfileUVs(profile, overlay, overlayName, imageSize)
43 GuiOverlay.loadProfileColors(profile, overlay, overlayName)
44
45 overlay.sdfWidth = profile:getNumber(overlayName .. "SdfWidth", overlay.sdfWidth)
46 end
47
48 if filename == nil then
49 return nil
50 end
51
52 if previewFilename == nil then
53 previewFilename = "dataS/menu/blank.png"
54 end
55
56 if filename == "g_baseUIFilename" then
57 filename = g_baseUIFilename
58 elseif filename == "g_iconsUIFilename" then
59 filename = g_iconsUIFilename
60 end
61
62 overlay.filename = string.gsub(filename, "$l10nSuffix", g_gui.languageSuffix)
63 overlay.previewFilename = previewFilename
64
65 return overlay
66end

loadProfileColors

Description
Load overlay color data from a profile.
Definition
loadProfileColors()
Code
187function GuiOverlay.loadProfileColors(profile, overlay, overlayName)
188 local color
189
190 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "Color"))
191 if color ~= nil then
192 overlay.color = color
193 end
194
195 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "FocusedColor"))
196 if color ~= nil then
197 overlay.colorFocused = color
198 end
199
200 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "PressedColor"))
201 if color ~= nil then
202 overlay.colorPressed = color
203 end
204
205 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "SelectedColor"))
206 if color ~= nil then
207 overlay.colorSelected = color
208 end
209
210 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "DisabledColor"))
211 if color ~= nil then
212 overlay.colorDisabled = color
213 end
214
215 color = GuiUtils.getColorGradientArray(profile:getValue(overlayName .. "HighlightedColor"))
216 if color ~= nil then
217 overlay.colorHighlighted = color
218 end
219
220 local rotation = profile:getValue(overlayName .. "Rotation")
221 if rotation ~= nil then
222 overlay.rotation = math.rad(tonumber(rotation))
223 end
224
225 local isWebOverlay = profile:getBool(overlayName .. "IsWebOverlay")
226 if isWebOverlay ~= nil then
227 overlay.isWebOverlay = isWebOverlay
228 end
229end

loadProfileUVs

Description
Load overlay UV data from a profile.
Definition
loadProfileUVs()
Code
105function GuiOverlay.loadProfileUVs(profile, overlay, overlayName, imageSize)
106 local uvs
107
108 uvs = profile:getValue(overlayName .. "UVs")
109 if uvs ~= nil then
110 overlay.uvs = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "UVRotation"))
111 end
112
113 uvs = profile:getValue(overlayName .. "FocusedUVs")
114 if uvs ~= nil then
115 overlay.uvsFocused = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "FocusedUVRotation"))
116 end
117
118 uvs = profile:getValue(overlayName .. "PressedUVs")
119 if uvs ~= nil then
120 overlay.uvsPressed = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "UVRotation"))
121 end
122
123 uvs = profile:getValue(overlayName .. "SelectedUVs")
124 if uvs ~= nil then
125 overlay.uvsSelected = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "SelectedUVRotation"))
126 end
127
128 uvs = profile:getValue(overlayName .. "DisabledUVs")
129 if uvs ~= nil then
130 overlay.uvsDisabled = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "DisabledUVRotation"))
131 end
132
133 uvs = profile:getValue(overlayName .. "HighlightedUVs")
134 if uvs ~= nil then
135 overlay.uvsHighlighted = GuiUtils.getUVs(uvs, imageSize, nil, profile:getNumber(overlayName .. "HighlightedUVRotation"))
136 end
137end

loadXMLColors

Description
Load overlay color data from XML.
Definition
loadXMLColors()
Code
141function GuiOverlay.loadXMLColors(xmlFile, key, overlay, overlayName)
142 local color
143
144 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "Color"))
145 if color ~= nil then
146 overlay.color = color
147 end
148
149 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "FocusedColor"))
150 if color ~= nil then
151 overlay.colorFocused = color
152 end
153
154 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "PressedColor"))
155 if color ~= nil then
156 overlay.colorPressed = color
157 end
158
159 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "SelectedColor"))
160 if color ~= nil then
161 overlay.colorSelected = color
162 end
163
164 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "DisabledColor"))
165 if color ~= nil then
166 overlay.colorDisabled = color
167 end
168
169 color = GuiUtils.getColorArray(getXMLString(xmlFile, key .. "#" .. overlayName .. "HighlightedColor"))
170 if color ~= nil then
171 overlay.colorHighlighted = color
172 end
173
174 local rotation = getXMLFloat(xmlFile, key .. "#" .. overlayName .. "Rotation")
175 if rotation ~= nil then
176 overlay.rotation = math.rad(rotation)
177 end
178
179 local isWebOverlay = getXMLBool(xmlFile, key .. "#" .. overlayName .. "IsWebOverlay")
180 if isWebOverlay ~= nil then
181 overlay.isWebOverlay = isWebOverlay
182 end
183end

renderOverlay

Description
Renders an overlay with the given parameters.
Definition
renderOverlay(overlay Overlay, posX Screen, posY Screen, sizeX Screen, sizeY Screen, state GuiOverlay.STATE_[...])
Arguments
overlayOverlaytable
posXScreenx position
posYScreeny position
sizeXScreenx size
sizeYScreeny size
stateGuiOverlay.STATE_[...]constant for the required display state
Code
407function GuiOverlay.renderOverlay(overlay, posX, posY, sizeX, sizeY, state, clipX1, clipY1, clipX2, clipY2)
408 if overlay.overlay ~= nil then
409 local colors = GuiOverlay.getOverlayColor(overlay, state)
410
411 -- if there is any alpha, draw.
412 if colors[4] ~= 0 or (colors[8] ~= nil and (colors[8] ~= 0 or colors[12] ~= 0 or colors[16] ~= 0)) then
413 if not overlay.hasCustomRotation then
414 local pivotX, pivotY = sizeX / 2, sizeY / 2
415 if overlay.customPivot ~= nil then
416 pivotX, pivotY = overlay.customPivot[1], overlay.customPivot[2]
417 end
418
419 setOverlayRotation(overlay.overlay, overlay.rotation, pivotX, pivotY)
420 end
421
422 local u1, v1, u2, v2, u3, v3, u4, v4 = unpack(GuiOverlay.getOverlayUVs(overlay, state))
423
424 -- Needs clipping
425 if clipX1 ~= nil then
426 local oldX1, oldY1, oldX2, oldY2 = posX, posY, sizeX + posX, sizeY + posY
427
428 local posX2 = posX + sizeX
429 local posY2 = posY + sizeY
430
431 posX = math.max(posX, clipX1)
432 posY = math.max(posY, clipY1)
433
434 sizeX = math.max(math.min(posX2, clipX2) - posX, 0)
435 sizeY = math.max(math.min(posY2, clipY2) - posY, 0)
436
437 if sizeX == 0 or sizeY == 0 then
438 return
439 end
440
441 local ou1, ov1, ou2, ov2, ou3, ov3, ou4, ov4 = u1, v1, u2, v2, u3, v3, u4, v4
442
443 local p1 = (posX - oldX1) / (oldX2 - oldX1) -- start x
444 local p2 = (posY - oldY1) / (oldY2 - oldY1) -- start y
445 local p3 = ((posX + sizeX) - oldX1) / (oldX2 - oldX1) -- end x
446 local p4 = ((posY + sizeY) - oldY1) / (oldY2 - oldY1) -- end y
447
448 -- start x, start y
449 u1 = (ou3-ou1)*p1 + ou1
450 v1 = (ov2-ov1)*p2 + ov1
451
452 -- start x, end y
453 u2 = (ou3-ou1)*p1 + ou1
454 v2 = (ov4-ov3)*p4 + ov3
455
456 -- end x, start y
457 u3 = (ou3-ou1)*p3 + ou1
458 v3 = (ov2-ov1)*p2 + ov1
459
460 -- end x, end y
461 u4 = (ou4-ou2)*p3 + ou2
462 v4 = (ov4-ov3)*p4 + ov3
463 end
464
465 setOverlayUVs(overlay.overlay, u1, v1, u2, v2, u3, v3, u4, v4)
466
467 if colors[5] ~= nil then
468 setOverlayCornerColor(overlay.overlay, 0, colors[1], colors[2], colors[3], colors[4] * overlay.alpha)
469 setOverlayCornerColor(overlay.overlay, 1, colors[5], colors[6], colors[7], colors[8] * overlay.alpha)
470 setOverlayCornerColor(overlay.overlay, 2, colors[9], colors[10], colors[11], colors[12] * overlay.alpha)
471 setOverlayCornerColor(overlay.overlay, 3, colors[13], colors[14], colors[15], colors[16] * overlay.alpha)
472 else
473 local r,g,b,a = unpack(colors)
474 setOverlayColor(overlay.overlay, r, g, b, a * overlay.alpha)
475 end
476
477 renderOverlay(overlay.overlay, posX, posY, sizeX, sizeY)
478 end
479 end
480end

setRotation

Description
Set this overlay's rotation.
Definition
setRotation(rotation Rotation, centerX Rotation, centerY Rotation)
Arguments
rotationRotationin radians
centerXRotationpivot X position offset from overlay position in screen space
centerYRotationpivot Y position offset from overlay position in screen space
Code
496function GuiOverlay.setRotation(overlay, rotation, centerX, centerY)
497 setOverlayRotation(overlay.overlay, rotation, centerX, centerY)
498 overlay.hasCustomRotation = true
499end