LUADOC - Farming Simulator 19

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
151function ButtonOverlay:getButtonWidth(buttonText, height)
152 setTextBold(true)
153 setTextAlignment(RenderText.ALIGN_LEFT)
154 buttonText = utf8ToUpper(tostring(buttonText))
155 local textSize = height * self.textSizeFactor
156 local textWidth = getTextWidth(textSize, buttonText)
157 local leftButtonWidth = self.buttonLeftRatio * height
158 local rightButtonWidth = self.buttonRightRatio * height
159
160 local minWidth = ((height * g_screenHeight) / g_screenWidth) - leftButtonWidth - rightButtonWidth
161 textWidth = math.max(textWidth, minWidth)
162
163 setTextBold(false)
164
165 local totalWidth = textWidth + leftButtonWidth + rightButtonWidth
166 return totalWidth, textWidth, leftButtonWidth, rightButtonWidth, textSize
167end

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.5 -- 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(getNormalizedUVs({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(getNormalizedUVs({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(getNormalizedUVs({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)
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()
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 setTextBold(false)
120 setTextAlignment(RenderText.ALIGN_CENTER)
121
122 local yCenter = posY + height * 0.5 - textSize * self.textSizeFactor
123 renderText(pos + textWidth * 0.5, yCenter + textSize * self.textYOffsetFactor, textSize, utf8ToUpper(buttonText))
124 setTextBold(false)
125 setTextColor(1, 1, 1, 1)
126
127 self.buttonScaleOverlay:setPosition(pos, posY)
128 self.buttonScaleOverlay:render()
129 pos = pos + textWidth
130 self.buttonRightOverlay:setPosition(pos, posY)
131 self.buttonRightOverlay:render()
132 pos = pos + rightButtonWidth
133
134 if self.debugEnabled or g_uiDebugEnabled then
135 local xPixel = 1 / g_screenWidth
136 local yPixel = 1 / g_screenHeight
137
138 setOverlayColor(GuiElement.debugOverlay, 1, 0, 1, 0.5)
139
140 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY - yPixel, totalWidth + 2 * xPixel, yPixel)
141 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY + height, totalWidth + 2 * xPixel, yPixel)
142 renderOverlay(GuiElement.debugOverlay, posX - xPixel, posY, xPixel, height)
143 renderOverlay(GuiElement.debugOverlay, posX + totalWidth, posY, xPixel, height)
144 end
145
146 return totalWidth
147end

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