LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

ChatWindow

Description
HUD chat window. Displays chat messages.
Parent
HUDDisplayElement
Functions

createBackground

Description
Create the background overlay.
Definition
createBackground()
Code
191function ChatWindow.createBackground(hudAtlasPath)
192 local posX, posY = ChatWindow.getBackgroundPosition(1)
193 local width, height = getNormalizedScreenValues(unpack(ChatWindow.SIZE.SELF))
194
195 local overlay = Overlay:new(hudAtlasPath, posX, posY, width, height)
196 overlay:setUVs(getNormalizedUVs(ChatWindow.UV.BACKGROUND))
197 overlay:setColor(unpack(ChatWindow.COLOR.BACKGROUND))
198 overlay.visible = false -- initialize as invisible, important for first hide call and hide timer
199 return overlay
200end

draw

Description
Draw the chat window.
Definition
draw()
Code
111function ChatWindow:draw()
112 -- TODO: implement a non-hacky way of knowing when the chat dialog is open:
113
114 if self:getVisible() and (not self.isMenuVisible or g_gui.currentGuiName == "ChatDialog") and #self.messages > 0 then
115 ChatWindow:superClass().draw(self)
116
117 setTextWrapWidth(self:getWidth())
118 setTextBold(false)
119 setTextAlignment(RenderText.ALIGN_LEFT)
120 local currentLine = 0
121
122 local baseX, baseY = self:getPosition()
123 local posX, posY = baseX + self.messageOffsetX, baseY + self.messageOffsetY
124 local lineHeight = self.textSize + self.lineOffset
125
126 local maxLineShown = self.maxLines + self.chatMessagesShowOffset
127 for i = #self.messages, 1, -1 do
128 local text = self.messages[i].sender .. ": " .. self.messages[i].msg
129 local _, numLines = getTextHeight(self.textSize, text)
130 currentLine = currentLine + numLines
131 if currentLine > self.chatMessagesShowOffset then
132 local lineShowOffset = math.max(currentLine - maxLineShown, 0)
133 local numLinesShow = math.min(numLines, currentLine - self.chatMessagesShowOffset)
134 numLinesShow = numLinesShow - lineShowOffset
135
136 setTextLineBounds(lineShowOffset, numLinesShow)
137
138 -- render shadow
139 setTextColor(unpack(ChatWindow.COLOR.MESSAGE_SHADOW))
140 renderText(posX + self.shadowOffset, posY + (currentLine - lineShowOffset - self.chatMessagesShowOffset - 1) * lineHeight - self.shadowOffset, self.textSize, text)
141 -- render text
142 setTextColor(unpack(ChatWindow.COLOR.MESSAGE))
143 renderText(posX, posY + (currentLine - lineShowOffset - self.chatMessagesShowOffset - 1) * lineHeight, self.textSize, text)
144 setTextLineBounds(0, 0)
145 if lineShowOffset > 0 then
146 break
147 end
148 end
149 end
150
151 setTextWrapWidth(0)
152 end
153end

getBackgroundPosition

Description
Get this element's base background position.
Definition
getBackgroundPosition(float uiScale)
Arguments
floatuiScaleCurrent UI scale factor
Code
169function ChatWindow.getBackgroundPosition(uiScale)
170 local offX, offY = getNormalizedScreenValues(unpack(ChatWindow.POSITION.SELF))
171 return g_safeFrameOffsetX + offX, g_safeFrameOffsetY + offY
172end

new

Description
Create a new ChatWindow.
Definition
new(string hudAtlasPath, table speakerDisplay)
Arguments
stringhudAtlasPathPath to the HUD atlas texture.
tablespeakerDisplaySpeakerDisplay reference which is notified when this window is visible
Return Values
tableChatWindowinstance
Code
25function ChatWindow.new(hudAtlasPath, speakerDisplay)
26 local backgroundOverlay = ChatWindow.createBackground(hudAtlasPath)
27 local self = ChatWindow:superClass().new(ChatWindow_mt, backgroundOverlay, nil)
28
29 self.speakerDisplay = speakerDisplay
30
31 self.maxLines = ChatWindow.MAX_NUM_MESSAGES -- overrides parent class value
32 self.messages = {} -- reference to chat message history owned by mission object
33
34 self.chatMessagesShowOffset = 0
35 self.hideTime = 0
36
37 self.messageOffsetX, self.messageOffsetY = 0, 0
38 self.textSize = 0
39 self.textOffsetY = 0
40 self.lineOffset = 0
41 self.shadowOffset = 0
42
43 self:storeScaledValues()
44
45 return self
46end

onMenuVisibilityChange

Description
Handle menu visibility state change.
Definition
onMenuVisibilityChange()
Code
83function ChatWindow:onMenuVisibilityChange(isMenuVisible)
84 self.isMenuVisible = isMenuVisible
85end

scrollChatMessages

Description
Scroll chat messages by a given amount.
Definition
scrollChatMessages(int delta, int numMessages)
Arguments
intdeltaNumber of lines (positive or negative) to scroll
intnumMessagesNumber of currently stored chat messages
Code
77function ChatWindow:scrollChatMessages(delta, numMessages)
78 self.chatMessagesShowOffset = MathUtil.clamp(self.chatMessagesShowOffset + delta, 0, numMessages - self.maxLines)
79end

setChatMessages

Description
Set the chat message history reference for displaying. The array is owned by the caller and must not be modified.
Definition
setChatMessages(table messages)
Arguments
tablemessagesMessages array as {i={msg=, sender=}}
Code
52function ChatWindow:setChatMessages(messages)
53 self.messages = messages
54end

setScale

Description
Set this element's UI scale.
Definition
setScale()
Code
161function ChatWindow:setScale(uiScale)
162 ChatWindow:superClass().setScale(self, uiScale)
163 self:storeScaledValues()
164end

setVisible

Description
Definition
setVisible()
Code
58function ChatWindow:setVisible(isVisible, animate)
59 if isVisible then
60 self.speakerDisplay:onChatVisibilityChange(true)
61 ChatWindow:superClass().setVisible(self, true, false)
62
63 if animate then
64 self.hideTime = ChatWindow.DISPLAY_DURATION
65 else
66 self.hideTime = -1
67 end
68 else
69 self.hideTime = self:getVisible() and ChatWindow.DISPLAY_DURATION or 0
70 end
71end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
176function ChatWindow:storeScaledValues()
177 self.messageOffsetX, self.messageOffsetY = self:scalePixelToScreenVector(ChatWindow.POSITION.MESSAGE)
178
179 self.textSize = self:scalePixelToScreenHeight(ChatWindow.TEXT_SIZE.MESSAGE)
180 self.textOffsetY = self.textSize * 0.15
181 self.lineOffset = self.textSize * 0.3
182 self.shadowOffset = ChatWindow.SHADOW_OFFSET_FACTOR * self.textSize
183end

update

Description
Update element state.
Definition
update()
Code
93function ChatWindow:update(dt)
94 ChatWindow:superClass().update(self, dt)
95
96 if self.hideTime >= 0 then -- also update and hide if time has been set to 0, see test below
97 self.hideTime = self.hideTime - dt
98 if self.hideTime <= 0 then
99 self.speakerDisplay:onChatVisibilityChange(false)
100 ChatWindow:superClass().setVisible(self, false, false)
101 end
102 end
103end