LUADOC - Farming Simulator 22

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
169function SideNotification:createBackground(hudAtlasPath)
170 local posX, posY = SideNotification.getBackgroundPosition(1)
171 local width, height = getNormalizedScreenValues(unpack(SideNotification.SIZE.SELF))
172
173 local overlay = Overlay.new(hudAtlasPath, posX - width, posY - height, width, height)
174 overlay:setUVs(GuiUtils.getUVs(SideNotification.UV.DEFAULT_BACKGROUND))
175 overlay:setColor(unpack(SideNotification.COLOR.DEFAULT_BACKGROUND))
176 return overlay
177end

createComponents

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

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 -- reset color to prevent following texts from being colored
114 setTextColor(1, 1, 1, 1)
115 end
116end

getBackgroundPosition

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

new

Description
Create a new SideNotification.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD atlas texture
Return Values
tableSideNotificationinstance
Code
24function SideNotification.new(customMt, hudAtlasPath)
25 local self = SideNotification:superClass().new(nil, nil, customMt or SideNotification_mt)
26 self.overlay = self:createBackground(hudAtlasPath)
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
133function SideNotification:setScale(uiScale)
134 SideNotification:superClass().setScale(self, uiScale)
135 self:updateSizeAndPositions()
136end

storeScaledValues

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

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
140function SideNotification:updateSizeAndPositions()
141 local numLines = math.min(#self.notificationQueue, SideNotification.MAX_NOTIFICATIONS)
142
143 local height = numLines * self.textSize + (numLines - 1) * self.lineOffset + self.notificationMarginY * 2
144 local width = self:getWidth()
145 self:setDimension(width, height)
146
147 local topRightX, topRightY = SideNotification.getBackgroundPosition(self:getScale())
148 local bottomY = topRightY - self:getHeight()
149 self:setPosition(topRightX - width, bottomY)
150
151 self:storeScaledValues()
152end