LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

TopNotification

Description
HUD top notification element. Displays notifications issued by other game components at the top of the screen.
Parent
HUDDisplayElement
Functions

createBackground

Description
Create the background overlay.
Definition
createBackground()
Code
244function TopNotification.createBackground(hudAtlasPath)
245 local width, height = getNormalizedScreenValues(unpack(TopNotification.SIZE.SELF))
246 local posX, posY = TopNotification.getBackgroundPosition(1, width, height)
247
248 local overlay = Overlay.new(g_baseUIFilename, posX, posY, width, height)
249 overlay:setUVs(g_colorBgUVs)
250 overlay:setColor(unpack(TopNotification.COLOR.BACKGROUND))
251
252 return overlay
253end

createComponents

Description
Create required display components.
Definition
createComponents()
Code
257function TopNotification:createComponents(hudAtlasPath)
258end

createIconOverlays

Description
Definition
createIconOverlays()
Code
262function TopNotification:createIconOverlays()
263 local width, height = getNormalizedScreenValues(unpack(TopNotification.SIZE.ICON))
264
265 local iconOverlay = Overlay.new(g_iconsUIFilename, 0, 0, width, height)
266 iconOverlay:setUVs(GuiUtils.getUVs(TopNotification.UV.ICON_RADIO_STREAM))
267 iconOverlay:setColor(unpack(TopNotification.COLOR.ICON))
268 self.icons[TopNotification.ICON.RADIO] = iconOverlay
269end

delete

Description
Definition
delete()
Code
59function TopNotification:delete()
60 for k, overlay in pairs(self.icons) do
61 overlay:delete()
62 self.icons[k] = nil
63 end
64
65 if self.customIcon ~= nil then
66 self.customIcon:delete()
67 self.customIcon = nil
68 end
69
70 TopNotification:superClass().delete(self)
71end

draw

Description
Draw notification.
Definition
draw()
Code
140function TopNotification:draw()
141 if self:getVisible() then
142 TopNotification:superClass().draw(self) -- background and frame
143
144 local notification = self.currentNotification
145 local title = Utils.limitTextToWidth(notification.title, self.titleTextSize, self.maxTextWidth, false, "...")
146 local text = Utils.limitTextToWidth(notification.text, self.descTextSize, self.maxTextWidth, false, "...")
147 local info = Utils.limitTextToWidth(notification.info, self.infoTextSize, self.maxTextWidth, false, "...")
148
149 local fadeAlpha = 1
150 if self.notificationStartDuration - self.currentNotification.duration < TopNotification.FADE_DURATION then
151 fadeAlpha = (self.notificationStartDuration - self.currentNotification.duration) / TopNotification.FADE_DURATION
152 elseif self.currentNotification.duration < TopNotification.FADE_DURATION then
153 fadeAlpha = self.currentNotification.duration / TopNotification.FADE_DURATION
154 end
155
156 local _, _, _, baseAlpha = self:getColor()
157 local baseX, baseY = self:getPosition()
158 local width, height = self:getWidth(), self:getHeight()
159
160 local alpha = baseAlpha * fadeAlpha
161
162 if notification.iconFilename ~= nil then
163 local icon = self.customIcon
164 icon:setColor(nil, nil, nil, alpha)
165 icon:setPosition(baseX + self.iconOffsetX, -- left
166 baseY + (height - notification.icon.height) * 0.5) -- middle
167 icon:render()
168 elseif notification.icon ~= nil then
169 local icon = notification.icon
170 icon:setColor(nil, nil, nil, alpha)
171 icon:setPosition(baseX + self.iconOffsetX, -- left
172 baseY + (height - notification.icon.height) * 0.5) -- middle
173 icon:render()
174 end
175
176 local r, g, b, a = unpack(TopNotification.COLOR.TEXT_TITLE)
177 setTextColor(r, g, b, a * alpha)
178 setTextBold(false)
179 setTextAlignment(RenderText.ALIGN_CENTER)
180
181 local centerX = baseX + width * 0.5
182 renderText(centerX + self.titleOffsetX, baseY + self.titleOffsetY, self.titleTextSize, title)
183
184 r, g, b, a = unpack(TopNotification.COLOR.TEXT_DESC)
185 setTextColor(r, g, b, a * alpha)
186 renderText(centerX + self.descOffsetX, baseY + self.descOffsetY, self.descTextSize, text)
187
188 r, g, b, a = unpack(TopNotification.COLOR.TEXT_INFO)
189 setTextColor(r, g, b, a * alpha)
190 renderText(centerX + self.infoOffsetX, baseY + self.infoOffsetY, self.infoTextSize, info)
191 end
192end

getBackgroundPosition

Description
Get this element's base background position.
Definition
getBackgroundPosition(float uiScale)
Arguments
floatuiScaleCurrent UI scale factor
Code
201function TopNotification.getBackgroundPosition(uiScale, width, height)
202 local offX, offY = getNormalizedScreenValues(unpack(TopNotification.POSITION.SELF))
203 return 0.5 - width * 0.5 + offX * uiScale, 1 - g_safeFrameOffsetY - height + offY * uiScale -- top center plus offset
204end

getHidingTranslation

Description
Get the screen space translation for hiding. Override in sub-classes if a different translation is required.
Definition
getHidingTranslation()
Return Values
floatScreenspace X translation
floatScreenspace Y translation
Code
108function TopNotification:getHidingTranslation()
109 return 0, 0.5 -- hide half a screen height above the upper screen border
110end

new

Description
Create a new TopNotification.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD atlas texture.
Return Values
tableTopNotificationinstance
Code
31function TopNotification.new(hudAtlasPath)
32 local backgroundOverlay = TopNotification.createBackground(hudAtlasPath)
33 local self = TopNotification:superClass().new(backgroundOverlay, nil, TopNotification_mt)
34
35 self.currentNotification = TopNotification.NO_NOTIFICATION
36 self.icons = {} -- icon key -> Overlay
37
38 self.titleTextSize = 0
39 self.descTextSize = 0
40 self.infoTextSize = 0
41 self.maxTextWidth = 0
42
43 self.titleOffsetX, self.titleOffsetY = 0, 0
44 self.descOffsetX, self.descOffsetY = 0, 0
45 self.infoOffsetX, self.infoOffsetY = 0, 0
46 self.iconOffsetX, self.iconOffsetY = 0, 0
47
48 self.notificationStartDuration = 0
49
50 self:storeScaledValues()
51 self:createComponents(hudAtlasPath)
52 self:createIconOverlays()
53
54 return self
55end

setNotification

Description
Set a notification to be displayed in a frame at the top of the screen. If another notification is being displayed, it is immediately replaced by this new one.
Definition
setNotification(string title, string text, string info, table iconKey, int duration)
Arguments
stringtitleNotification title
stringtextNotification message text
stringinfoAdditional info text
tableiconKey[optional] Icon key for a display icon, use a value from TopNotification.ICON
intduration[optional] Display duration in milliseconds. Negative values or nil default to a long-ish standard duration.
Code
82function TopNotification:setNotification(title, text, info, iconKey, duration, iconFilename)
83 local icon = nil
84 if iconKey ~= nil and self.icons[iconKey] ~= nil then
85 icon = self.icons[iconKey]
86 end
87
88 if duration == nil or duration < 0 then
89 duration = TopNotification.DEFAULT_DURATION
90 end
91
92 local notification = {title=title, text=text, info=info, icon=icon, duration=duration, iconFilename=iconFilename}
93 self.notificationStartDuration = duration
94 self.currentNotification = notification
95
96 if iconFilename ~= nil then
97 self.customIcon = self:createCustomIcon(iconFilename)
98 end
99
100 self:setVisible(true, true) -- animate in
101end

setScale

Description
Set uniform UI scale.
Definition
setScale()
Code
208function TopNotification:setScale(uiScale)
209 TopNotification:superClass().setScale(self, uiScale)
210 self:storeScaledValues()
211
212 -- set position again because we anchor from the top (scaling protrudes from bottom left)
213 local width, height = self:scalePixelToScreenVector(TopNotification.SIZE.SELF)
214 local posX, posY = TopNotification.getBackgroundPosition(uiScale, width, height)
215 self:setPosition(posX, posY)
216end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
220function TopNotification:storeScaledValues()
221 self.titleTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.TITLE)
222 self.descTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.TEXT)
223 self.infoTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.INFO)
224
225 self.maxTextWidth = self:scalePixelToScreenWidth(TopNotification.TEXT_SIZE.MAX_TEXT_WIDTH)
226
227 self.titleOffsetX, self.titleOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.TITLE_OFFSET)
228 self.descOffsetX, self.descOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.TEXT_OFFSET)
229 self.infoOffsetX, self.infoOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.INFO_OFFSET)
230
231 self.iconOffsetX, self.iconOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.ICON)
232 local iconWidth, iconHeight = self:scalePixelToScreenVector(TopNotification.SIZE.ICON)
233 for _, overlay in pairs(self.icons) do
234 overlay:setDimension(iconWidth, iconHeight)
235 end
236end

update

Description
Update notification state.
Definition
update()
Code
118function TopNotification:update(dt)
119 TopNotification:superClass().update(self, dt)
120
121 if self:getVisible() and self.currentNotification ~= TopNotification.NO_NOTIFICATION then
122 if self.currentNotification.duration < TopNotification.FADE_DURATION and self.animation:getFinished() then
123 self:setVisible(false, true) -- animate out
124 end
125
126 if self.currentNotification.duration <= 0 then
127 self.currentNotification = TopNotification.NO_NOTIFICATION
128 else
129 self.currentNotification.duration = self.currentNotification.duration - dt
130 end
131 end
132end