LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

TweenSequence

Description
Allows setting up more complex tweening by defining sequences of tweens, intervals and callbacks. A sequence is itself a Tween, so you may even define and add sub-sequences. Before a sequence reacts to update() calls, it must be started with start(). This also applies after resetting. Adding tweens, callbacks and intervals will append them to the current sequence. Insertion of tweens and callbacks will insert them at the given relative instants, allowing for overlapping tweens and arbitrary callback times. Inserting an interval will push pack all later instants by the given time.
Parent
Tween
Functions

addCallback

Description
Add a callback at the end of the sequence.
Definition
addCallback(function callback, table callbackState)
Arguments
functioncallbackCallback function with signature of either callback(target, value) or callback(value)
tablecallbackStateAny value which is passed to the callback as its first (no target) or second (with target) argument
Code
107function TweenSequence:addCallback(callback, callbackState)
108 self:insertCallback(callback, callbackState, self.totalDuration)
109end

addInterval

Description
Add an interval at the end of the sequence. Use this to add a pause to the sequence.
Definition
addInterval()
Code
88function TweenSequence:addInterval(interval)
89 self:insertInterval(interval, self.totalDuration)
90end

addTween

Description
Add a tween to the end of the sequence.
Definition
addTween(table tween)
Arguments
tabletweenTween instance
Code
58function TweenSequence:addTween(tween)
59 self:insertTween(tween, self.totalDuration)
60end

getDuration

Description
Get this tween's duration in milliseconds.
Definition
getDuration()
Code
113function TweenSequence:getDuration()
114 return self.totalDuration
115end

insertCallback

Description
Insert a callback at the given instant.
Definition
insertCallback(function callback, table callbackState, float instant)
Arguments
functioncallbackCallback function with signature of either callback(target, value) or callback(value)
tablecallbackStateAny value which is passed to the callback as its first (no target) or second (with target) argument
floatinstantTime in milliseconds after sequence start
Code
97function TweenSequence:insertCallback(callback, callbackState, instant)
98 self.callbackInstants[callback] = instant
99 self.callbackStates[callback] = callbackState
100 self.callbacksCalled[callback] = false
101end

insertInterval

Description
Insert an interval at the given instant. This will push back all later instants by the interval. Use this to insert pauses into the sequence.
Definition
insertInterval(float interval, float instant)
Arguments
floatintervalInterval time in milliseconds
floatinstantTime in milliseconds after sequence start
Code
67function TweenSequence:insertInterval(interval, instant)
68 for tween, range in pairs(self.tweenUpdateRanges) do
69 local tweenStartInstant, tweenEndInstant = range[1], range[2]
70 if tweenStartInstant >= instant then
71 self.tweenUpdateRanges[tween][1] = tweenStartInstant + interval
72 self.tweenUpdateRanges[tween][2] = tweenEndInstant + interval
73 end
74 end
75
76 for callback, callbackInstant in pairs(self.callbackInstants) do
77 if callbackInstant >= instant then
78 self.callbackInstants[callback] = callbackInstant + interval
79 end
80 end
81
82 self.totalDuration = self.totalDuration + interval
83end

insertTween

Description
Insert a tween at a given instant.
Definition
insertTween(table tween, float instant)
Arguments
tabletweenTween instance
floatinstantTime in milliseconds after sequence start
Code
45function TweenSequence:insertTween(tween, instant)
46 self.tweenUpdateRanges[tween] = {instant, instant + tween:getDuration()}
47
48 self.totalDuration = math.max(instant + tween:getDuration(), self.totalDuration)
49
50 if self.functionTarget ~= nil then
51 tween:setTarget(self.functionTarget)
52 end
53end

new

Description
Create a new TweenSequence.
Definition
new(table functionTarget)
Arguments
tablefunctionTarget[optional] Target table which is supplied by default to all tween setter functions and callbacks as the first argument. If not specified, the setters and callbacks will be called with one value only.
Code
24function TweenSequence.new(functionTarget)
25 local self = Tween.new(nil, nil, nil, nil, TweenSequence_mt)
26
27 self.functionTarget = functionTarget
28 self.callbackStates = {} -- callback -> callback state
29 self.callbacksCalled = {} -- callback -> bool
30
31 self.tweenUpdateRanges = {} -- tween -> {startInstant, endInstant}
32 self.callbackInstants = {} -- callback -> instant
33
34 self.isLooping = false
35 self.totalDuration = 0
36 self.isFinished = true
37
38 return self
39end

reset

Description
Reset the sequence to its initial state.
Definition
reset()
Code
146function TweenSequence:reset()
147 self.elapsedTime = 0
148 self.isFinished = true
149
150 for tween in pairs(self.tweenUpdateRanges) do
151 tween:reset()
152 end
153
154 for callback in pairs(self.callbacksCalled) do
155 self.callbacksCalled[callback] = false
156 end
157end

setLooping

Description
Set the looping state for this sequence.
Definition
setLooping(bool isLooping)
Arguments
boolisLoopingIf true, will restart the sequence when finished, including callbacks!
Code
127function TweenSequence:setLooping(isLooping)
128 self.isLooping = isLooping
129end

setTarget

Description
Set a callback target for this tween. If a target has been set, the setter function must support receiving the target as its first argument.
Definition
setTarget()
Code
120function TweenSequence:setTarget(target)
121 self.functionTarget = target
122end

start

Description
Start the sequence. A sequence will only update its state when it has been started.
Definition
start()
Code
134function TweenSequence:start()
135 self.isFinished = false
136end

stop

Description
Stop the sequence.
Definition
stop()
Code
140function TweenSequence:stop()
141 self.isFinished = true
142end

update

Description
Update the sequence state over time.
Definition
update()
Code
161function TweenSequence:update(dt)
162 if not self.isFinished then
163 local lastUpdateInstant = self.elapsedTime
164 self.elapsedTime = self.elapsedTime + dt
165
166 local allFinished = self:updateTweens(lastUpdateInstant, dt)
167 self:updateCallbacks()
168
169 if self.elapsedTime >= self.totalDuration and allFinished then
170 if self.isLooping then
171 self:reset()
172 self:start()
173 else
174 self.isFinished = true
175 end
176 end
177 end
178end

updateCallbacks

Description
Update callback states.
Definition
updateCallbacks()
Code
201function TweenSequence:updateCallbacks()
202 for callback, instant in pairs(self.callbackInstants) do
203 if not self.callbacksCalled[callback] and instant <= self.elapsedTime then
204 if self.functionTarget ~= nil then
205 callback(self.functionTarget, self.callbackStates[callback])
206 else
207 callback(self.callbackStates[callback])
208 end
209
210 self.callbacksCalled[callback] = true
211 end
212 end
213end

updateTweens

Description
Update active sequence tweens.
Definition
updateTweens(float lastInstant, float dt)
Arguments
floatlastInstantLast instant which received an update
floatdtDelta time
Code
184function TweenSequence:updateTweens(lastInstant, dt)
185 local allFinished = true
186
187 for tween, range in pairs(self.tweenUpdateRanges) do
188 local tweenStart = range[1]
189 if not tween:getFinished() and self.elapsedTime >= tweenStart then
190 local maxDt = math.min(self.elapsedTime - tweenStart, dt)
191 tween:update(maxDt)
192 allFinished = allFinished and tween:getFinished()
193 end
194 end
195
196 return allFinished
197end