LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

MultiTextOptionElement

Description
Multiple choice text element. This element requires a specific configuration setup to be properly used: In the configuration, it must contain the following child elements in this order: 1. ButtonElement, 2. ButtonElement, 3. TextElement, 4. TextElement. The first three elements are mandatory, as they are the buttons to change this element's value and the label which displays the value. The fourth (text) element is optional and used as a header label for this element if defined.
Parent
GuiElement
XML Configuration Parameters
GuiElement#wrapbool [optional] If false, values will not cycle when the end or start of the value list is reached.
GuiElement#buttonRLChangebool [optional] If true, requires controller shoulder buttons as input to change the value.
GuiElement#textsstring [optional] Selectable options, defaults to empty list. Format: "[text1]|[text2]|[text3]". If the prefix "$l10n_" is used in an option text, it is resolved as a variable by the localization system to the corresponding text in the current language.
GuiElement#onClickcallback [optional] onClick(index, element) Called when one of the change buttons is clicked / activated. Receives the current option index and this element.
GuiElement#onFocuscallback [optional] onFocus(element) Called when this element receives focus. Receives this element as an argument.
GuiElement#onLeavecallback [optional] onLeave(element) Called when this element loses focus. Receives this element as an argument.

Functions

addElement

Description
Definition
addElement()
Code
148function MultiTextOptionElement:addElement(element)
149 MultiTextOptionElement:superClass().addElement(self, element)
150 -- TODO: this is super easy to get wrong when writing configurations from scratch. Make it smarter, e.g. using names to resolve collaborators.
151 if table.getn(self.elements) == 1 then
152 -- left
153 self.leftButtonElement = element
154 self.leftButtonElement.forceHighlight = true
155 element:setHandleFocus(false)
156 element.target = self
157 element.onClickCallback = self.onLeftButtonClicked
158 self:setDisabled(self.disabled)
159 elseif table.getn(self.elements) == 2 then
160 -- right button
161 self.rightButtonElement = element
162 self.rightButtonElement.forceHighlight = true
163 element.target = self
164 element:setHandleFocus(false)
165 element.onClickCallback = self.onRightButtonClicked
166 self:setDisabled(self.disabled)
167 elseif table.getn(self.elements) == 3 then
168 self.textElement = element
169 self:updateTextElement()
170 elseif table.getn(self.elements) == 4 then
171 self.labelElement = element
172 end
173end

addText

Description
Definition
addText()
Code
189function MultiTextOptionElement:addText(text, i)
190 if i == nil then
191 table.insert(self.texts, text)
192 else
193 table.insert(self.texts, i, text)
194 end
195 self:updateTextElement()
196
197 self:notifyIndexChange(self.state, #self.texts)
198end

canReceiveFocus

Description
Definition
canReceiveFocus()
Code
392function MultiTextOptionElement:canReceiveFocus(element, direction)
393 return not self.disabled and self:getIsVisible()
394end

copyAttributes

Description
Definition
copyAttributes()
Code
103function MultiTextOptionElement:copyAttributes(src)
104 MultiTextOptionElement:superClass().copyAttributes(self, src)
105
106 self.isChecked = src.isChecked
107 self.buttonLRChange = src.buttonLRChange
108 self.state = src.state
109 self.wrap = src.wrap
110 self.scrollDelayDuration = src.scrollDelayDuration
111 self.canChangeState = src.canChangeState
112
113 self.onClickCallback = src.onClickCallback
114 self.onLeaveCallback = src.onLeaveCallback
115 self.onFocusCallback = src.onFocusCallback
116 for _, text in pairs(src.texts) do
117 self:addText(text)
118 end
119
120 GuiMixin.cloneMixin(IndexChangeSubjectMixin, src, self)
121 GuiMixin.cloneMixin(PlaySampleMixin, src, self)
122end

disableButtonSounds

Description
Disable automatic playing of sound samples in child buttons.
Definition
disableButtonSounds()
Code
177function MultiTextOptionElement:disableButtonSounds()
178 if self.leftButtonElement ~= nil then
179 self.leftButtonElement:disablePlaySample()
180 end
181
182 if self.rightButtonElement ~= nil then
183 self.rightButtonElement:disablePlaySample()
184 end
185end

getCanChangeState

Description
Definition
getCanChangeState()
Code
380function MultiTextOptionElement:getCanChangeState()
381 return self.canChangeState
382end

getState

Description
Definition
getState()
Code
467function MultiTextOptionElement:getState()
468 return self.state
469end

inputEvent

Description
Definition
inputEvent()
Code
241function MultiTextOptionElement:inputEvent(action, value, eventUsed)
242 eventUsed = MultiTextOptionElement:superClass().inputEvent(self, action, value, eventUsed)
243
244 if not eventUsed then
245 if action == InputAction.MENU_AXIS_LEFT_RIGHT then
246 if value < -g_analogStickHTolerance then
247 eventUsed = true
248 self:inputLeft(false)
249 elseif value > g_analogStickHTolerance then
250 eventUsed = true
251 self:inputRight(false)
252 end
253 elseif action == InputAction.MENU_PAGE_PREV then
254 eventUsed = true
255 self:inputLeft(true)
256 elseif action == InputAction.MENU_PAGE_NEXT then
257 eventUsed = true
258 self:inputRight(true)
259 end
260 end
261 return eventUsed
262end

loadFromXML

Description
Definition
loadFromXML()
Code
59function MultiTextOptionElement:loadFromXML(xmlFile, key)
60 MultiTextOptionElement:superClass().loadFromXML(self, xmlFile, key)
61
62 self:addCallback(xmlFile, key.."#onClick", "onClickCallback")
63 self:addCallback(xmlFile, key.."#onFocus", "onFocusCallback")
64 self:addCallback(xmlFile, key.."#onLeave", "onLeaveCallback")
65
66 self.wrap = Utils.getNoNil(getXMLBool(xmlFile, key.."#wrap"), self.wrap)
67 self.buttonLRChange = Utils.getNoNil(getXMLBool(xmlFile, key.."#buttonLRChange"), self.buttonLRChange)
68
69 local text = getXMLString(xmlFile, key.."#texts")
70 if text ~= nil then
71 local texts = StringUtil.splitString("|", text)
72 for _, text in pairs(texts) do
73 if text:sub(1,6) == "$l10n_" then
74 text = g_i18n:getText(text:sub(7))
75 end
76 table.insert(self.texts, text)
77 end
78 end
79end

loadProfile

Description
Definition
loadProfile()
Code
83function MultiTextOptionElement:loadProfile(profile, applyProfile)
84 MultiTextOptionElement:superClass().loadProfile(self, profile, applyProfile)
85
86 self.wrap = profile:getBool("wrap", self.wrap)
87 self.buttonLRChange = profile:getBool("buttonLRChange", self.buttonLRChange)
88
89 local text = profile:getValue("texts")
90 if text ~= nil then
91 local texts = StringUtil.splitString("|", text)
92 for _, text in pairs(texts) do
93 if text:sub(1,6) == "$l10n_" then
94 text = g_i18n:getText(text:sub(7))
95 end
96 table.insert(self.texts, text)
97 end
98 end
99end

mouseEvent

Description
Definition
mouseEvent()
Code
218function MultiTextOptionElement:mouseEvent(posX, posY, isDown, isUp, button, eventUsed)
219 if self:getIsActive() then
220 if MultiTextOptionElement:superClass().mouseEvent(self, posX, posY, isDown, isUp, button, eventUsed) then
221 eventUsed = true
222 end
223
224 if not eventUsed and not self.forceHighlight and GuiUtils.checkOverlayOverlap(posX, posY, self.absPosition[1], self.absPosition[2], self.size[1], self.size[2], nil) then
225 if not self.mouseEntered and not self.focusActive then
226 FocusManager:setHighlight(self)
227 self.mouseEntered = true
228 end
229 else
230 if self.mouseEntered and not self.focusActive then
231 FocusManager:unsetHighlight(self)
232 self.mouseEntered = false
233 end
234 end
235 end
236 return eventUsed
237end

new

Description
Definition
new()
Code
29function MultiTextOptionElement:new(target, custom_mt)
30 local self = GuiElement:new(target, custom_mt or MultiTextOptionElement_mt)
31 self:include(IndexChangeSubjectMixin) -- add index change subject mixin for observers
32 self:include(PlaySampleMixin) -- add sound playing
33
34 self.isChecked = false
35 self.mouseEntered = false
36 self.buttonLRChange = false
37 self.canChangeState = true
38
39 self.state = 1
40 self.wrap = true
41 self.texts = {}
42
43 self.scrollDelayDuration = 300
44 self.leftDelayTime = 0
45 self.rightDelayTime = 0
46
47 self.forceHighlight = false
48
49 self.leftButtonElement = nil
50 self.rightButtonElement = nil
51 self.textElement = nil
52 self.labelElement = nil
53
54 return self
55end

onFocusEnter

Description
Definition
onFocusEnter()
Code
413function MultiTextOptionElement:onFocusEnter()
414 MultiTextOptionElement:superClass().onFocusEnter(self)
415
416 if self.rightButtonElement ~= nil and self.rightButtonElement.state ~= GuiOverlay.STATE_FOCUSED then
417 self.rightButtonElement:onFocusEnter()
418 end
419 if self.leftButtonElement ~= nil and self.leftButtonElement.state ~= GuiOverlay.STATE_FOCUSED then
420 self.leftButtonElement:onFocusEnter()
421 end
422 self:raiseCallback("onFocusCallback", self)
423end

onFocusLeave

Description
Definition
onFocusLeave()
Code
398function MultiTextOptionElement:onFocusLeave()
399 MultiTextOptionElement:superClass().onFocusLeave(self)
400
401 self:raiseCallback("onLeaveCallback", self)
402
403 if self.rightButtonElement ~= nil and self.rightButtonElement.state ~= GuiOverlay.STATE_NORMAL then
404 self.rightButtonElement:onFocusLeave()
405 end
406 if self.leftButtonElement ~= nil and self.leftButtonElement.state ~= GuiOverlay.STATE_NORMAL then
407 self.leftButtonElement:onFocusLeave()
408 end
409end

onHighlight

Description
Definition
onHighlight()
Code
427function MultiTextOptionElement:onHighlight()
428 MultiTextOptionElement:superClass().onHighlight(self)
429
430 if self.rightButtonElement ~= nil and self.rightButtonElement:getOverlayState() == GuiOverlay.STATE_NORMAL then
431 self.rightButtonElement:setOverlayState(GuiOverlay.STATE_HIGHLIGHTED)
432 end
433
434 if self.leftButtonElement ~= nil and self.leftButtonElement:getOverlayState() == GuiOverlay.STATE_NORMAL then
435 self.leftButtonElement:setOverlayState(GuiOverlay.STATE_HIGHLIGHTED)
436 end
437end

onHighlightRemove

Description
Definition
onHighlightRemove()
Code
441function MultiTextOptionElement:onHighlightRemove()
442 MultiTextOptionElement:superClass().onHighlightRemove(self)
443
444 if self.rightButtonElement ~= nil and self.rightButtonElement:getOverlayState() == GuiOverlay.STATE_HIGHLIGHTED then
445 self.rightButtonElement:setOverlayState(GuiOverlay.STATE_NORMAL)
446 end
447
448 if self.leftButtonElement ~= nil and self.leftButtonElement:getOverlayState() == GuiOverlay.STATE_HIGHLIGHTED then
449 self.leftButtonElement:setOverlayState(GuiOverlay.STATE_NORMAL)
450 end
451end

onLeftButtonClicked

Description
Definition
onLeftButtonClicked()
Code
342function MultiTextOptionElement:onLeftButtonClicked(steps, noFocus)
343 if self:getCanChangeState() then
344 if steps == nil then steps = 1 end
345 if steps ~= nil and type(steps) ~= "number" then steps = 1 end
346 for i = 1, steps do
347 if self.wrap then
348 self.state = self.state - 1
349 if self.state < 1 then
350 self.state = table.getn(self.texts)
351 end
352 else
353 self.state = math.max(self.state-1, 1)
354 end
355 end
356
357 self:playSample(GuiSoundPlayer.SOUND_SAMPLES.SLIDER)
358
359 self:setSoundSuppressed(true)
360 FocusManager:setFocus(self)
361 self:setSoundSuppressed(false)
362
363 self:updateTextElement()
364 self:raiseCallback("onClickCallback", self.state, self)
365 self:notifyIndexChange(self.state, #self.texts)
366
367 if (noFocus == nil or not noFocus) then
368 if self.leftButtonElement ~= nil then
369 self.leftButtonElement:onFocusEnter()
370 end
371 if self.rightButtonElement ~= nil then
372 self.rightButtonElement:onFocusEnter()
373 end
374 end
375 end
376end

onRightButtonClicked

Description
Definition
onRightButtonClicked()
Code
304function MultiTextOptionElement:onRightButtonClicked(steps, noFocus)
305 if self:getCanChangeState() then
306 if steps == nil then steps = 1 end
307 if steps ~= nil and type(steps) ~= "number" then steps = 1 end
308 for i = 1, steps do
309 if self.wrap then
310 self.state = self.state + 1
311 if self.state > table.getn(self.texts) then
312 self.state = 1
313 end
314 else
315 self.state = math.min(self.state+1, table.getn(self.texts))
316 end
317 end
318
319 self:playSample(GuiSoundPlayer.SOUND_SAMPLES.SLIDER)
320
321 self:setSoundSuppressed(true)
322 FocusManager:setFocus(self)
323 self:setSoundSuppressed(false)
324
325 self:updateTextElement()
326 self:raiseCallback("onClickCallback", self.state, self)
327 self:notifyIndexChange(self.state, #self.texts)
328
329 if (noFocus == nil or not noFocus) then
330 if self.leftButtonElement ~= nil then
331 self.leftButtonElement:onFocusEnter()
332 end
333 if self.rightButtonElement ~= nil then
334 self.rightButtonElement:onFocusEnter()
335 end
336 end
337 end
338end

setCanChangeState

Description
Definition
setCanChangeState()
Code
386function MultiTextOptionElement:setCanChangeState(canChangeState)
387 self.canChangeState = canChangeState
388end

setForceHighlight

Description
Set the force highlight state of this element. If force highlight is enabled, this element will not be highlighted automatically but must receive the highlight state manually by calls to FocusManager:setHighlight().
Definition
setForceHighlight()
Code
142function MultiTextOptionElement:setForceHighlight(needForceHighlight)
143 self.forceHighlight = needForceHighlight
144end

setLabel

Description
Definition
setLabel()
Code
212function MultiTextOptionElement:setLabel(labelString)
213 self.labelElement:setText(labelString)
214end

setState

Description
Definition
setState()
Code
126function MultiTextOptionElement:setState(state, forceEvent)
127 local numTexts = #self.texts
128 self.state = math.max(math.min(state, numTexts), 1)
129 self:updateTextElement()
130
131 if forceEvent then
132 self:raiseCallback("onClickCallback", self.state, self)
133 end
134
135 self:notifyIndexChange(self.state, numTexts)
136end

setTexts

Description
Definition
setTexts()
Code
202function MultiTextOptionElement:setTexts(texts)
203 self.texts = texts or {}
204 self.state = math.min(self.state, #self.texts)
205 self:updateTextElement()
206
207 self:notifyIndexChange(self.state, #self.texts)
208end

updateTextElement

Description
Definition
updateTextElement()
Code
455function MultiTextOptionElement:updateTextElement()
456 if self.textElement ~= nil then
457 if self.texts[self.state] ~= nil then
458 self.textElement:setText(self.texts[self.state])
459 else
460 self.textElement:setText("")
461 end
462 end
463end