LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

ButtonOverlay

Description
Keyboard button display overlay. Overlay type which displays a keyboard key button symbol.
Functions

delete

Description
Delete this button overlay.
Definition
delete()
Code
53function ButtonOverlay:delete()
54 if self.buttonScaleOverlay ~= 0 then
55 self.buttonScaleOverlay:delete()
56 end
57 if self.buttonLeftOverlay ~= 0 then
58 self.buttonLeftOverlay:delete()
59 end
60 if self.buttonRightOverlay ~= 0 then
61 self.buttonRightOverlay:delete()
62 end
63end

getButtonWidth

Description
Get the total display width of this button overlay for a given button text and height
Definition
getButtonWidth()
Code
156function ButtonOverlay:getButtonWidth(buttonText, height)
157 setTextBold(true)
158 setTextAlignment(RenderText.ALIGN_LEFT)
159 buttonText = utf8ToUpper(tostring(buttonText))
160 local textSize = height * self.textSizeFactor
161 local leftButtonWidth = self.buttonLeftRatio * height * g_aspectScaleX
162 local rightButtonWidth = self.buttonRightRatio * height * g_aspectScaleX
163
164 -- add small padding so text is not clamped to border
165 local textWidth = getTextWidth(textSize, buttonText) + 2 * leftButtonWidth
166
167 local minWidth = ((height * g_screenHeight) / g_screenWidth) - leftButtonWidth - rightButtonWidth
168 textWidth = math.max(textWidth, minWidth)
169
170 setTextBold(false)
171
172 local totalWidth = textWidth + leftButtonWidth + rightButtonWidth
173 return totalWidth, textWidth, leftButtonWidth, rightButtonWidth, textSize
174end

new

Description
Definition
new()
Code
17function ButtonOverlay.new()
18 local self = {}
19 setmetatable(self, ButtonOverlay_mt)
20
21 -- TODO: get atlas UVs as parameters
22 local atlasRefSize = {1024, 1024}
23 local uBase = 101
24 local vBase = 100
25 local height = 40
26 local width = 37
27 local sideWidth = 5
28
29 self.textSizeFactor = 0.47 -- ratio of text size to button height
30 self.textYOffsetFactor = 0.125 -- compensates base line offset for size factor 0.5 TODO: get proper vertical alignment from engine in renderText()
31
32 -- scaling middle part overlay
33 self.buttonScaleOverlay = Overlay.new(g_baseUIFilename, 0, 0, 0, 0)
34 self.buttonScaleOverlay:setUVs(GuiUtils.getUVs({uBase + sideWidth, vBase, 1, height}, atlasRefSize))
35 -- left end part overlay
36 self.buttonLeftOverlay = Overlay.new(g_baseUIFilename, 0, 0, 0, 0)
37 self.buttonLeftOverlay:setUVs(GuiUtils.getUVs({uBase, vBase, sideWidth, height}, atlasRefSize))
38 self.buttonLeftRatio = sideWidth * 0.5 / width
39 -- right end part overlay
40 self.buttonRightOverlay = Overlay.new(g_baseUIFilename, 0, 0, 0, 0)
41 self.buttonRightOverlay:setUVs(GuiUtils.getUVs({uBase + width - sideWidth, vBase, sideWidth, height}, atlasRefSize))
42 self.buttonRightRatio = sideWidth * 0.5 / width
43
44 self:setColor(0.0723, 0.0723, 0.0723, 1)
45
46 self.debugEnabled = false
47
48 return self
49end

renderButton

Description
Render this overlay with the given parameters.
Definition
renderButton(buttonText Text, posX Screen, posY Screen, height Button, alignment Text)
Arguments
buttonTextTextto display as the key value, e.g. "A", "Space", "Ctrl", etc.
posXScreenx position
posYScreeny position
heightButtondisplay height
alignmentTextalignment, one of the constants of RenderText.ALIGN_[...]
Code
94function ButtonOverlay:renderButton(buttonText, posX, posY, height, alignment, colorText, clipX1, clipY1, clipX2, clipY2)
95 alignment = Utils.getNoNil(alignment, RenderText.ALIGN_LEFT)
96
97 local totalWidth, textWidth, leftButtonWidth, rightButtonWidth, textSize = self:getButtonWidth(buttonText, height)
98 local pos = posX
99
100 self.buttonLeftOverlay:setDimension(leftButtonWidth, height)
101 self.buttonScaleOverlay:setDimension(textWidth, height)
102 self.buttonRightOverlay:setDimension(rightButtonWidth, height)
103
104 if alignment == RenderText.ALIGN_RIGHT then
105 pos = pos - (textWidth+leftButtonWidth + rightButtonWidth)
106 elseif alignment == RenderText.ALIGN_CENTER then
107 pos = pos - (textWidth+leftButtonWidth + rightButtonWidth) / 2
108 end
109
110 self.buttonLeftOverlay:setPosition(pos, posY)
111 self.buttonLeftOverlay:render(clipX1, clipY1, clipX2, clipY2)
112 pos = pos + leftButtonWidth
113
114 if colorText then
115 setTextColor(self.r, self.g, self.b, self.a)
116 else
117 setTextColor(1, 1, 1, self.a)
118 end
119
120 setTextBold(true)
121 setTextAlignment(RenderText.ALIGN_CENTER)
122 local yCenter = posY + height * 0.5 - textSize * self.textSizeFactor
123 if clipX1 ~= nil then
124 setTextClipArea(clipX1, clipY1, clipX2, clipY2)
125 end
126 renderText(pos + textWidth * 0.5, yCenter + textSize * self.textYOffsetFactor, textSize, utf8ToUpper(buttonText))
127 if clipX1 ~= nil then
128 setTextClipArea(0, 0, 1, 1)
129 end
130 setTextBold(false)
131 setTextColor(1, 1, 1, 1)
132
133 self.buttonScaleOverlay:setPosition(pos, posY)
134 self.buttonScaleOverlay:render(clipX1, clipY1, clipX2, clipY2)
135 pos = pos + textWidth
136 self.buttonRightOverlay:setPosition(pos, posY)
137 self.buttonRightOverlay:render(clipX1, clipY1, clipX2, clipY2)
138
139 if self.debugEnabled or g_uiDebugEnabled then
140 local xPixel = 1 / g_screenWidth
141 local yPixel = 1 / g_screenHeight
142
143 setOverlayColor(GuiElement.debugOverlay, 1, 0, 1, 0.5)
144
145 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY - yPixel, totalWidth + 2 * xPixel, yPixel)
146 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY + height, totalWidth + 2 * xPixel, yPixel)
147 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY, xPixel, height)
148 renderOverlay(GuiElement.debugOverlay, posX + totalWidth, posY, xPixel, height)
149 end
150
151 return totalWidth
152end

setColor

Description
Set this overlay's background color.
Definition
setColor(r Red, g Green, b Blue, a Alpha)
Arguments
rRedchannel [0, 1]
gGreenchannel [0, 1]
bBluechannel [0, 1]
aAlpha(transparency) channel [0, 1], 0 is fully transparent, 1 is opaque
Code
71function ButtonOverlay:setColor(r, g, b, a)
72 self.r = Utils.getNoNil(r, self.r)
73 self.g = Utils.getNoNil(g, self.g)
74 self.b = Utils.getNoNil(b, self.b)
75 self.a = Utils.getNoNil(a, self.a)
76 if self.buttonScaleOverlay ~= 0 then
77 setOverlayColor(self.buttonScaleOverlay.overlayId, self.r, self.g, self.b, self.a)
78 end
79 if self.buttonLeftOverlay ~= 0 then
80 setOverlayColor(self.buttonLeftOverlay.overlayId, self.r, self.g, self.b, self.a)
81 end
82 if self.buttonRightOverlay ~= 0 then
83 setOverlayColor(self.buttonRightOverlay.overlayId, self.r, self.g, self.b, self.a)
84 end
85end