LUADOC - Farming Simulator 22

Overlay

Description
Image display overlay. This class is used to display textures or usually parts thereof as rectangular panels in the UI or on the HUD. Example usages include button icons, showing images in the main menu or drawing the in-game map.
Functions

delete

Description
Delete this overlay. Releases the texture file handle.
Definition
delete()
Code
76function Overlay:delete()
77 if self.overlayId ~= 0 then
78 delete(self.overlayId)
79 end
80end

getPosition

Description
Get this overlay's position.
Definition
getPosition()
Return Values
floatXposition in screen space
floatYposition in screen space
Code
124function Overlay:getPosition()
125 return self.x, self.y
126end

getScale

Description
Get this overlay's scale values.
Definition
getScale()
Return Values
floatWidthscale factor
floatHeightscale factor
Code
195function Overlay:getScale()
196 return self.scaleWidth, self.scaleHeight
197end

new

Description
Create a new Overlay.
Definition
new(overlayFilename File, x Screen, y Screen, width Display, height Display)
Arguments
overlayFilenameFilepath of the source texture
xScreenposition x
yScreenposition y
widthDisplaywidth
heightDisplayheight
Code
28function Overlay.new(overlayFilename, x, y, width, height, customMt)
29 local overlayId = 0
30 if overlayFilename ~= nil then
31 overlayId = createImageOverlay(overlayFilename)
32 end
33
34 local self = setmetatable({}, customMt or Overlay_mt)
35
36 self.overlayId = overlayId
37 self.filename = overlayFilename
38 self.uvs = {1,0, 1,1, 0,0, 0,1}
39
40 self.x = x
41 self.y = y
42 self.offsetX = 0
43 self.offsetY = 0
44
45 self.defaultWidth = width
46 self.width = width
47 self.defaultHeight = height
48 self.height = height
49
50 self.scaleWidth = 1.0
51 self.scaleHeight = 1.0
52
53 self.visible = true
54
55 self.alignmentVertical = Overlay.ALIGN_VERTICAL_BOTTOM
56 self.alignmentHorizontal = Overlay.ALIGN_HORIZONTAL_LEFT
57
58 self.invertX = false
59 self.rotation = 0
60 self.rotationCenterX = 0
61 self.rotationCenterY = 0
62
63 self.r = 1.0
64 self.g = 1.0
65 self.b = 1.0
66 self.a = 1.0
67
68 self.debugEnabled = false
69
70 return self
71end

render

Description
Render this overlay.
Definition
render()
Code
201function Overlay:render(clipX1, clipY1, clipX2, clipY2)
202 if self.visible then
203 if self.overlayId ~= 0 and self.a > 0 then
204 local posX, posY = self.x + self.offsetX, self.y + self.offsetY
205 local sizeX, sizeY = self.width, self.height
206
207 -- Apply clipping
208 if clipX1 ~= nil then
209 local u1, v1, u2, v2, u3, v3, u4, v4 = unpack(self.uvs)
210
211 local oldX1, oldY1, oldX2, oldY2 = posX, posY, sizeX + posX, sizeY + posY
212
213 local posX2 = posX + sizeX
214 local posY2 = posY + sizeY
215
216 posX = math.max(posX, clipX1)
217 posY = math.max(posY, clipY1)
218
219 sizeX = math.max(math.min(posX2, clipX2) - posX, 0)
220 sizeY = math.max(math.min(posY2, clipY2) - posY, 0)
221
222 if sizeX == 0 or sizeY == 0 then
223 -- Cancel, no visible pixels
224 return
225 end
226
227 local ou1, ov1, ou2, ov2, ou3, ov3, ou4, ov4 = u1, v1, u2, v2, u3, v3, u4, v4
228
229 local p1 = (posX - oldX1) / (oldX2 - oldX1) -- start x
230 local p2 = (posY - oldY1) / (oldY2 - oldY1) -- start y
231 local p3 = ((posX + sizeX) - oldX1) / (oldX2 - oldX1) -- end x
232 local p4 = ((posY + sizeY) - oldY1) / (oldY2 - oldY1) -- end y
233
234 -- start x, start y
235 u1 = (ou3-ou1)*p1 + ou1
236 v1 = (ov2-ov1)*p2 + ov1
237
238 -- start x, end y
239 u2 = (ou3-ou1)*p1 + ou1
240 v2 = (ov4-ov3)*p4 + ov3
241
242 -- end x, start y
243 u3 = (ou3-ou1)*p3 + ou1
244 v3 = (ov2-ov1)*p2 + ov1
245
246 -- end x, end y
247 u4 = (ou4-ou2)*p3 + ou2
248 v4 = (ov4-ov3)*p4 + ov3
249
250 setOverlayUVs(self.overlayId, u1, v1, u2, v2, u3, v3, u4, v4)
251 end
252
253 renderOverlay(self.overlayId, posX, posY, sizeX, sizeY)
254
255 if clipX1 ~= nil then
256 -- Reset to original to not affect other draw calls
257 setOverlayUVs(self.overlayId, unpack(self.uvs))
258 end
259 end
260
261 --#debug if self.debugEnabled or g_uiDebugEnabled then
262 --#debug local xPixel = 1 / g_screenWidth
263 --#debug local yPixel = 1 / g_screenHeight
264
265 --#debug setOverlayColor(GuiElement.debugOverlay, 0, 0, 1, 1)
266
267 --#debug renderOverlay(GuiElement.debugOverlay, self.x + self.offsetX - xPixel, self.y + self.offsetY - yPixel, self.width + 2 * xPixel, yPixel)
268 --#debug renderOverlay(GuiElement.debugOverlay, self.x + self.offsetX - xPixel, self.y + self.offsetY + self.height, self.width + 2 * xPixel, yPixel)
269 --#debug renderOverlay(GuiElement.debugOverlay, self.x + self.offsetX - xPixel, self.y + self.offsetY, xPixel, self.height)
270 --#debug renderOverlay(GuiElement.debugOverlay, self.x + self.offsetX + self.width, self.y + self.offsetY, xPixel, self.height)
271 --#debug end
272 end
273end

resetDimensions

Description
Reset width, height and scale to initial values set in the constructor.
Definition
resetDimensions()
Code
139function Overlay:resetDimensions()
140 self.scaleWidth = 1.0
141 self.scaleHeight = 1.0
142 self:setDimension(self.defaultWidth, self.defaultHeight)
143end

setAlignment

Description
Set this overlay's alignment.
Definition
setAlignment(vertical Vertical, horizontal Horizontal)
Arguments
verticalVerticalalignment value, one of Overlay.ALIGN_VERTICAL_[...]
horizontalHorizontalalignment value, one of Overlay.ALIGN_HORIZONTAL_[...]
Code
279function Overlay:setAlignment(vertical, horizontal)
280 if vertical == Overlay.ALIGN_VERTICAL_TOP then
281 self.offsetY = -self.height
282 elseif vertical == Overlay.ALIGN_VERTICAL_MIDDLE then
283 self.offsetY = -self.height * 0.5
284 else
285 self.offsetY = 0
286 end
287 self.alignmentVertical = vertical or Overlay.ALIGN_VERTICAL_BOTTOM
288
289 if horizontal == Overlay.ALIGN_HORIZONTAL_RIGHT then
290 self.offsetX = -self.width
291 elseif horizontal == Overlay.ALIGN_HORIZONTAL_CENTER then
292 self.offsetX = -self.width * 0.5
293 else
294 self.offsetX = 0
295 end
296 self.alignmentHorizontal = horizontal or Overlay.ALIGN_HORIZONTAL_LEFT
297end

setColor

Description
Set this overlay's color. The color is multiplied with the texture color. For no modification of the texture color, use full opaque white, i.e. {1, 1, 1, 1}.
Definition
setColor(r Red, g Green, b Blue, a Alpha)
Arguments
rRedchannel
gGreenchannel
bBluechannel
aAlphachannel
Code
90function Overlay:setColor(r, g, b, a)
91 r = r or self.r
92 g = g or self.g
93 b = b or self.b
94 a = a or self.a
95 if r ~= self.r or g ~= self.g or b ~= self.b or a ~= self.a then
96 self.r, self.g, self.b, self.a = r, g, b, a
97 if self.overlayId ~= 0 then
98 setOverlayColor(self.overlayId, self.r, self.g, self.b, self.a)
99 end
100 end
101end

setDimension

Description
Set this overlay's width and height. Either value can be omitted (== nil) for no change.
Definition
setDimension()
Code
131function Overlay:setDimension(width, height)
132 self.width = width or self.width
133 self.height = height or self.height
134 self:setAlignment(self.alignmentVertical, self.alignmentHorizontal)
135end

setImage

Description
Set a different image for this overlay. The previously targeted image's handle will be released.
Definition
setImage(overlayFilename File)
Arguments
overlayFilenameFilepath to new target image
Code
309function Overlay:setImage(overlayFilename)
310 if self.filename ~= overlayFilename then
311 if self.overlayId ~= 0 then
312 delete(self.overlayId)
313 end
314 self.filename = overlayFilename
315 self.overlayId = createImageOverlay(overlayFilename)
316 end
317end

setInvertX

Description
Set horizontal flipping state.
Definition
setInvertX(invertX If)
Arguments
invertXIftrue, will set the overlay to display its image flipped horizontally
Code
148function Overlay:setInvertX(invertX)
149 if self.invertX ~= invertX then
150 self.invertX = invertX
151 if self.overlayId ~= 0 then
152 if invertX then
153 setOverlayUVs(self.overlayId, self.uvs[5],self.uvs[6], self.uvs[7],self.uvs[8], self.uvs[1],self.uvs[2], self.uvs[3], self.uvs[4])
154 else
155 setOverlayUVs(self.overlayId, unpack(self.uvs))
156 end
157 end
158 end
159end

setIsVisible

Description
Set this overlay's visibility.
Definition
setIsVisible()
Code
301function Overlay:setIsVisible(visible)
302 self.visible = visible
303end

setPosition

Description
Set this overlay's position.
Definition
setPosition()
Code
115function Overlay:setPosition(x, y)
116 self.x = x or self.x
117 self.y = y or self.y
118end

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
166function Overlay:setRotation(rotation, centerX, centerY)
167 if self.rotation ~= rotation or self.rotationCenterX ~= centerX or self.rotationCenterY ~= centerY then
168 self.rotation = rotation
169 self.rotationCenterX = centerX
170 self.rotationCenterY = centerY
171 if self.overlayId ~= 0 then
172 setOverlayRotation(self.overlayId, rotation, centerX, centerY)
173 end
174 end
175end

setScale

Description
Set this overlay's scale. Multiplies the scale values with the initial dimensions and sets those as the current dimensions.
Definition
setScale(float scaleWidth, float scaleHeight)
Arguments
floatscaleWidthWidth scale factor
floatscaleHeightHeight scale factor
Code
182function Overlay:setScale(scaleWidth, scaleHeight)
183 self.width = self.defaultWidth * scaleWidth
184 self.height = self.defaultHeight * scaleHeight
185 self.scaleWidth = scaleWidth
186 self.scaleHeight = scaleHeight
187 -- update alignment offsets
188 self:setAlignment(self.alignmentVertical, self.alignmentHorizontal)
189end

setUVs

Description
Set this overlay's UVs which define the area to be displayed within the target texture.
Definition
setUVs(uvs UV)
Arguments
uvsUVcoordinates in the form of {u1, v1, u2, v2, u3, v3, u4, v4}
Code
106function Overlay:setUVs(uvs)
107 if self.overlayId ~= 0 then
108 self.uvs = uvs
109 setOverlayUVs(self.overlayId, unpack(uvs))
110 end
111end