LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

SideNotification

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

addNotification

Description
Add a notification message to display.
Definition
addNotification(string text, table color, int displayDuration)
Arguments
stringtextDisplay message text
tablecolorColor array as {r, g, b, a}
intdisplayDurationDisplay duration of message in milliseconds
Code
43function SideNotification:addNotification(text, color, displayDuration)
44 local notification = {text=text, color=color, duration=displayDuration, startDuration=displayDuration}
45 table.insert(self.notificationQueue, notification)
46
47 self:updateSizeAndPositions()
48end

createBackground

Description
Create the background overlay.
Definition
createBackground()
Code
167function SideNotification.createBackground(hudAtlasPath)
168 local posX, posY = SideNotification.getBackgroundPosition(1)
169 local width, height = getNormalizedScreenValues(unpack(SideNotification.SIZE.SELF))
170
171 local overlay = Overlay:new(hudAtlasPath, posX - width, posY - height, width, height)
172 overlay:setUVs(getNormalizedUVs(SideNotification.UV.DEFAULT_BACKGROUND))
173 overlay:setColor(unpack(SideNotification.COLOR.DEFAULT_BACKGROUND))
174 return overlay
175end

createComponents

Description
Create required display components.
Definition
createComponents()
Code
179function SideNotification:createComponents(hudAtlasPath)
180end

draw

Description
Draw the notifications.
Definition
draw()
Code
79function SideNotification:draw()
80 if self:getVisible() and #self.notificationQueue > 0 then
81 SideNotification:superClass().draw(self)
82
83 local baseX, baseY = self:getPosition()
84 local width, height = self:getWidth(), self:getHeight()
85
86 local offsetX = 1 / g_screenWidth
87 local offsetY = 1 / g_screenHeight
88 local notificationX = baseX + width - self.notificationMarginX
89 local notificationY = baseY + height - self.textSize - self.notificationMarginY
90
91 local _, _, _, alpha = self:getColor()
92 for i = 1, math.min(#self.notificationQueue, SideNotification.MAX_NOTIFICATIONS) do
93 local notification = self.notificationQueue[i]
94
95 local fadeAlpha = 1
96 if notification.startDuration - notification.duration < SideNotification.FADE_DURATION then
97 fadeAlpha = (notification.startDuration - notification.duration) / SideNotification.FADE_DURATION
98 elseif notification.duration < SideNotification.FADE_DURATION then
99 fadeAlpha = notification.duration / SideNotification.FADE_DURATION
100 end
101
102 setTextBold(false)
103 setTextAlignment(RenderText.ALIGN_RIGHT)
104 -- render shadow
105 setTextColor(0, 0, 0, alpha * fadeAlpha)
106 renderText(notificationX + offsetX, notificationY - offsetY + self.textOffsetY, self.textSize, notification.text)
107 -- render text
108 setTextColor(notification.color[1], notification.color[2], notification.color[3], notification.color[4] * alpha * fadeAlpha)
109 renderText(notificationX, notificationY + self.textOffsetY, self.textSize, notification.text)
110
111 notificationY = notificationY - self.textSize - self.lineOffset
112 end
113 end
114end

getBackgroundPosition

Description
Get this element's base background position.
Definition
getBackgroundPosition(float uiScale)
Arguments
floatuiScaleCurrent UI scale factor
Code
124function SideNotification.getBackgroundPosition(uiScale)
125 local offX, offY = getNormalizedScreenValues(unpack(SideNotification.POSITION.SELF))
126 return 1 - g_safeFrameOffsetX + offX * uiScale, 1 - g_safeFrameOffsetY + offY * uiScale -- top right corner plus offset
127end

new

Description
Create a new SideNotification.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD atlas texture
Return Values
tableSideNotificationinstance
Code
24function SideNotification.new(hudAtlasPath)
25 local backgroundOverlay = SideNotification.createBackground(hudAtlasPath)
26 local self = SideNotification:superClass().new(SideNotification_mt, backgroundOverlay, nil)
27
28 self.notificationQueue = {} -- i={text=<text>, color={r,g,b,a}, duration=<time in ms>}
29
30 self.textSize = 0
31 self.textOffsetY = 0 -- vertical alignment compensation offset
32 self.lineOffset = 0
33 self.notificationMarginX, self.notificationMarginY = 0, 0
34
35 return self
36end

setScale

Description
Set uniform UI scale.
Definition
setScale()
Code
131function SideNotification:setScale(uiScale)
132 SideNotification:superClass().setScale(self, uiScale)
133 self:updateSizeAndPositions()
134end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
154function SideNotification:storeScaledValues()
155 self.textSize = self:scalePixelToScreenHeight(SideNotification.TEXT_SIZE.DEFAULT_NOTIFICATION)
156 self.textOffsetY = self.textSize * 0.15
157 self.lineOffset = self.textSize * 0.3
158 self.notificationMarginX, self.notificationMarginY = self:scalePixelToScreenVector(SideNotification.SIZE.NOTIFICATION_MARGIN)
159end

update

Description
Update notifications state.
Definition
update()
Code
56function SideNotification:update(dt)
57 local hasRemoval = false
58 for i = math.min(#self.notificationQueue, SideNotification.MAX_NOTIFICATIONS), 1, -1 do
59 local notification = self.notificationQueue[i]
60 if notification.duration <= 0 then
61 table.remove(self.notificationQueue, i)
62 hasRemoval = true
63 else
64 notification.duration = math.max(0, notification.duration - dt) -- limit to zero for alpha calculations
65 end
66 end
67
68 if hasRemoval then
69 self:updateSizeAndPositions()
70 end
71end

updateSizeAndPositions

Description
Update sizes and positions of this elements and its children.
Definition
updateSizeAndPositions()
Code
138function SideNotification:updateSizeAndPositions()
139 local numLines = math.min(#self.notificationQueue, SideNotification.MAX_NOTIFICATIONS)
140
141 local height = numLines * self.textSize + (numLines - 1) * self.lineOffset + self.notificationMarginY * 2
142 local width = self:getWidth()
143 self:setDimension(width, height)
144
145 local topRightX, topRightY = SideNotification.getBackgroundPosition(self:getScale())
146 local bottomY = topRightY - self:getHeight()
147 self:setPosition(topRightX - width, bottomY)
148
149 self:storeScaledValues()
150end