LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

GuiDataSource

Description
Data source for UI elements. Holds dynamic data and allows configuration of accessors.
Functions

addChangeListener

Description
Add a listener with a member function which is called when this data source changes.
Definition
addChangeListener()
Code
37function GuiDataSource:addChangeListener(target, callback)
38 self.changeListeners[target] = callback or NO_CALLBACK
39end

getCount

Description
Get the number of data items.
Definition
getCount()
Code
58function GuiDataSource:getCount()
59 return #self.data
60end

getItem

Description
Get a data item at a given index.
Definition
getItem()
Code
64function GuiDataSource:getItem(index)
65 return self.data[index]
66end

iterateRange

Description
Iterate a data range within the given indices. This is an iterator factory compatible with the default Lua for loop. E.g. "for _, item in source:iterateRange(1, 10) do" will loop over data items 1 to 10.
Definition
iterateRange()
Code
84function GuiDataSource:iterateRange(startIndex, endIndex)
85 local iterator = function(data, iter)
86 local item = data[iter]
87 if iter <= endIndex and item ~= nil then
88 return iter + 1, item
89 else
90 return nil, nil
91 end
92 end
93
94 return iterator, self.data, startIndex
95end

new

Description
Create a new GuiDataSource instance.
Definition
new()
Code
19function GuiDataSource.new(subclass_mt)
20 local self = setmetatable({}, subclass_mt or GuiDataSource_mt)
21
22 self.data = NO_DATA
23 self.changeListeners = {} -- {<listener table> = <listener function>}
24
25 return self
26end

notifyChange

Description
Notify this data source that its data has been changed externally. This will call the change callback if it has been set.
Definition
notifyChange()
Code
50function GuiDataSource:notifyChange()
51 for target, callback in pairs(self.changeListeners) do
52 callback(target)
53 end
54end

removeChangeListener

Description
Remove a previously added change listener from the notification table.
Definition
removeChangeListener()
Code
43function GuiDataSource:removeChangeListener(target)
44 self.changeListeners[target] = nil
45end

setData

Description
Set the data source data array.
Definition
setData()
Code
30function GuiDataSource:setData(data)
31 self.data = data or NO_DATA
32 self:notifyChange()
33end

setItem

Description
Set a data item at a given index. If the index is out bounds of the data, this will have no effect.
Definition
setItem()
Code
71function GuiDataSource:setItem(index, value, needsNotification)
72 if index > 0 and index <= #self.data then
73 self.data[index] = value
74 if needsNotification then
75 self:notifyChange()
76 end
77 end
78end