LUADOC - Farming Simulator 19

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
229function TopNotification.createBackground(hudAtlasPath)
230 local width, height = getNormalizedScreenValues(unpack(TopNotification.SIZE.SELF))
231 local posX, posY = TopNotification.getBackgroundPosition(1, width, height)
232
233 local overlay = Overlay:new(nil, posX, posY, width, height)
234 overlay:setUVs(getNormalizedUVs(TopNotification.UV.BACKGROUND))
235 return overlay
236end

createComponents

Description
Create required display components.
Definition
createComponents()
Code
240function TopNotification:createComponents(hudAtlasPath)
241 local posX, posY = self:getPosition()
242 local width, height = self:getWidth(), self:getHeight()
243
244 local frame = HUDFrameElement:new(hudAtlasPath, posX, posY, width, height)
245 frame:setColor(unpack(HUD.COLOR.FRAME_BACKGROUND))
246 self:addChild(frame)
247end

createIconOverlays

Description
Definition
createIconOverlays()
Code
251function TopNotification:createIconOverlays()
252 local width, height = getNormalizedScreenValues(unpack(TopNotification.SIZE.ICON))
253
254 local iconOverlay = Overlay:new(g_baseUIFilename, 0, 0, width, height)
255 iconOverlay:setUVs(getNormalizedUVs(TopNotification.UV.ICON_RADIO_STREAM))
256 iconOverlay:setColor(unpack(TopNotification.COLOR.ICON))
257 self.icons[TopNotification.ICON.RADIO] = iconOverlay
258end

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 TopNotification:superClass().delete(self)
66end

draw

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

getBackgroundPosition

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

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
99function TopNotification:getHidingTranslation()
100 return 0, 0.5 -- hide half a screen height above the upper screen border
101end

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(TopNotification_mt, backgroundOverlay, nil)
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
77function TopNotification:setNotification(title, text, info, iconKey, duration)
78 local icon = nil
79 if iconKey ~= nil and self.icons[iconKey] ~= nil then
80 icon = self.icons[iconKey]
81 end
82
83 if duration == nil or duration < 0 then
84 duration = TopNotification.DEFAULT_DURATION
85 end
86
87 local notification = {title=title, text=text, info=info, icon=icon, duration=duration}
88 self.notificationStartDuration = duration
89 self.currentNotification = notification
90
91 self:setVisible(true, true) -- animate in
92end

setScale

Description
Set uniform UI scale.
Definition
setScale()
Code
193function TopNotification:setScale(uiScale)
194 TopNotification:superClass().setScale(self, uiScale)
195 self:storeScaledValues()
196
197 -- set position again because we anchor from the top (scaling protrudes from bottom left)
198 local width, height = self:scalePixelToScreenVector(TopNotification.SIZE.SELF)
199 local posX, posY = TopNotification.getBackgroundPosition(uiScale, width, height)
200 self:setPosition(posX, posY)
201end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
205function TopNotification:storeScaledValues()
206 self.titleTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.TITLE)
207 self.descTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.TEXT)
208 self.infoTextSize = self:scalePixelToScreenHeight(TopNotification.TEXT_SIZE.INFO)
209
210 self.maxTextWidth = self:scalePixelToScreenWidth(TopNotification.TEXT_SIZE.MAX_TEXT_WIDTH)
211
212 self.titleOffsetX, self.titleOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.TITLE_OFFSET)
213 self.descOffsetX, self.descOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.TEXT_OFFSET)
214 self.infoOffsetX, self.infoOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.INFO_OFFSET)
215
216 self.iconOffsetX, self.iconOffsetY = self:scalePixelToScreenVector(TopNotification.POSITION.ICON)
217 local iconWidth, iconHeight = self:scalePixelToScreenVector(TopNotification.SIZE.ICON)
218 for _, overlay in pairs(self.icons) do
219 overlay:setDimension(iconWidth, iconHeight)
220 end
221end

update

Description
Update notification state.
Definition
update()
Code
109function TopNotification:update(dt)
110 TopNotification:superClass().update(self, dt)
111
112 if self:getVisible() and self.currentNotification ~= TopNotification.NO_NOTIFICATION then
113 if self.currentNotification.duration < TopNotification.FADE_DURATION and self.animation:getFinished() then
114 self:setVisible(false, true) -- animate out
115 end
116
117 if self.currentNotification.duration <= 0 then
118 self.currentNotification = TopNotification.NO_NOTIFICATION
119 else
120 self.currentNotification.duration = self.currentNotification.duration - dt
121 end
122 end
123end