Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 4b9de24

Browse files
authored
Merge pull request #142 from UnityTech/yczhang
Yczhang
2 parents 5ecbc0c + 65a0e61 commit 4b9de24

File tree

7 files changed

+157
-22
lines changed

7 files changed

+157
-22
lines changed

Runtime/animation/animation_controller.cs

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ public TickerFuture forward(float? from = null) {
155155

156156
return true;
157157
});
158+
D.assert(
159+
this._ticker != null,
160+
"AnimationController.forward() called after AnimationController.dispose()\n" +
161+
"AnimationController methods should not be used after calling dispose."
162+
);
158163
this._direction = _AnimationDirection.forward;
159164
if (from != null) {
160165
this.setValue(from.Value);
@@ -175,6 +180,11 @@ public TickerFuture reverse(float? from = null) {
175180

176181
return true;
177182
});
183+
D.assert(
184+
this._ticker != null,
185+
"AnimationController.reverse() called after AnimationController.dispose()\n" +
186+
"AnimationController methods should not be used after calling dispose."
187+
);
178188
this._direction = _AnimationDirection.reverse;
179189
if (from != null) {
180190
this.setValue(from.Value);
@@ -184,12 +194,28 @@ public TickerFuture reverse(float? from = null) {
184194
}
185195

186196
public TickerFuture animateTo(float target, TimeSpan? duration = null, Curve curve = null) {
197+
D.assert(
198+
this._ticker != null,
199+
"AnimationController.animateTo() called after AnimationController.dispose()\n" +
200+
"AnimationController methods should not be used after calling dispose."
201+
);
187202
curve = curve ?? Curves.linear;
188203

189204
this._direction = _AnimationDirection.forward;
190205
return this._animateToInternal(target, duration: duration, curve: curve);
191206
}
192207

208+
public TickerFuture animateBack(float target, TimeSpan? duration, Curve curve = null) {
209+
D.assert(
210+
this._ticker != null,
211+
"AnimationController.animateBack() called after AnimationController.dispose()\n" +
212+
"AnimationController methods should not be used after calling dispose."
213+
);
214+
curve = curve ?? Curves.linear;
215+
this._direction = _AnimationDirection.reverse;
216+
return this._animateToInternal(target, duration, curve);
217+
}
218+
193219
TickerFuture _animateToInternal(float target, TimeSpan? duration = null, Curve curve = null) {
194220
curve = curve ?? Curves.linear;
195221

@@ -236,7 +262,8 @@ TickerFuture _animateToInternal(float target, TimeSpan? duration = null, Curve c
236262
new _InterpolationSimulation(this._value, target, simulationDuration.Value, curve));
237263
}
238264

239-
public TickerFuture repeat(float? min = null, float? max = null, TimeSpan? period = null) {
265+
public TickerFuture repeat(float? min = null, float? max = null, bool reverse = false,
266+
TimeSpan? period = null) {
240267
min = min ?? this.lowerBound;
241268
max = max ?? this.upperBound;
242269
period = period ?? this.duration;
@@ -252,7 +279,10 @@ public TickerFuture repeat(float? min = null, float? max = null, TimeSpan? perio
252279

253280
return true;
254281
});
255-
return this.animateWith(new _RepeatingSimulation(min.Value, max.Value, period.Value));
282+
283+
D.assert(max >= min);
284+
D.assert(max <= this.upperBound && min >= this.lowerBound);
285+
return this.animateWith(new _RepeatingSimulation(this._value, min.Value, max.Value, reverse, period.Value));
256286
}
257287

258288
public TickerFuture fling(float velocity = 1.0f) {
@@ -268,6 +298,11 @@ public TickerFuture fling(float velocity = 1.0f) {
268298

269299

270300
public TickerFuture animateWith(Simulation simulation) {
301+
D.assert(
302+
this._ticker != null,
303+
"AnimationController.animateWith() called after AnimationController.dispose()\n" +
304+
"AnimationController methods should not be used after calling dispose."
305+
);
271306
this.stop();
272307
return this._startSimulation(simulation);
273308
}
@@ -287,6 +322,11 @@ TickerFuture _startSimulation(Simulation simulation) {
287322
}
288323

289324
public void stop(bool canceled = true) {
325+
D.assert(
326+
this._ticker != null,
327+
"AnimationController.stop() called after AnimationController.dispose()\n" +
328+
"AnimationController methods should not be used after calling dispose."
329+
);
290330
this._simulation = null;
291331
this._lastElapsedDuration = null;
292332
this._ticker.stop(canceled: canceled);
@@ -395,21 +435,35 @@ public override bool isDone(float timeInSeconds) {
395435
}
396436

397437
class _RepeatingSimulation : Simulation {
398-
internal _RepeatingSimulation(float min, float max, TimeSpan period) {
438+
internal _RepeatingSimulation(float initialValue, float min, float max, bool reverse, TimeSpan period) {
399439
this._min = min;
400440
this._max = max;
401441
this._periodInSeconds = (float) period.Ticks / TimeSpan.TicksPerSecond;
442+
this._initialT =
443+
(max == min) ? 0.0f : (initialValue / (max - min)) * (period.Ticks / TimeSpan.TicksPerSecond);
444+
this._reverse = reverse;
402445
D.assert(this._periodInSeconds > 0.0f);
446+
D.assert(this._initialT >= 0.0f);
403447
}
404448

405449
readonly float _min;
406450
readonly float _max;
407451
readonly float _periodInSeconds;
452+
readonly bool _reverse;
453+
readonly float _initialT;
408454

409455
public override float x(float timeInSeconds) {
410456
D.assert(timeInSeconds >= 0.0f);
411-
float t = (timeInSeconds / this._periodInSeconds) % 1.0f;
412-
return MathUtils.lerpFloat(this._min, this._max, t);
457+
float totalTimeInSeconds = timeInSeconds + this._initialT;
458+
float t = (totalTimeInSeconds / this._periodInSeconds) % 1.0f;
459+
bool _isPlayingReverse = ((int) (totalTimeInSeconds / this._periodInSeconds)) % 2 == 1;
460+
461+
if (this._reverse && _isPlayingReverse) {
462+
return MathUtils.lerpFloat(this._max, this._min, t);
463+
}
464+
else {
465+
return MathUtils.lerpFloat(this._min, this._max, t);
466+
}
413467
}
414468

415469
public override float dx(float timeInSeconds) {

Runtime/animation/curves.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,68 @@ public static class Curves {
283283

284284
public static readonly Curve decelerate = new _DecelerateCurve();
285285

286+
public static readonly Cubic fastLinearToSlowEaseIn = new Cubic(0.18f, 1.0f, 0.04f, 1.0f);
287+
286288
public static readonly Curve ease = new Cubic(0.25f, 0.1f, 0.25f, 1.0f);
287289

288290
public static readonly Curve easeIn = new Cubic(0.42f, 0.0f, 1.0f, 1.0f);
289291

292+
public static readonly Cubic easeInToLinear = new Cubic(0.67f, 0.03f, 0.65f, 0.09f);
293+
294+
public static readonly Cubic easeInSine = new Cubic(0.47f, 0, 0.745f, 0.715f);
295+
296+
public static readonly Cubic easeInQuad = new Cubic(0.55f, 0.085f, 0.68f, 0.53f);
297+
298+
public static readonly Cubic easeInCubic = new Cubic(0.55f, 0.055f, 0.675f, 0.19f);
299+
300+
public static readonly Cubic easeInQuart = new Cubic(0.895f, 0.03f, 0.685f, 0.22f);
301+
302+
public static readonly Cubic easeInQuint = new Cubic(0.755f, 0.05f, 0.855f, 0.06f);
303+
304+
public static readonly Cubic easeInExpo = new Cubic(0.95f, 0.05f, 0.795f, 0.035f);
305+
306+
public static readonly Cubic easeInCirc = new Cubic(0.6f, 0.04f, 0.98f, 0.335f);
307+
308+
public static readonly Cubic easeInBack = new Cubic(0.6f, -0.28f, 0.735f, 0.045f);
309+
290310
public static readonly Curve easeOut = new Cubic(0.0f, 0.0f, 0.58f, 1.0f);
291311

312+
public static readonly Cubic linearToEaseOut = new Cubic(0.35f, 0.91f, 0.33f, 0.97f);
313+
314+
public static readonly Cubic easeOutSine = new Cubic(0.39f, 0.575f, 0.565f, 1.0f);
315+
316+
public static readonly Cubic easeOutQuad = new Cubic(0.25f, 0.46f, 0.45f, 0.94f);
317+
318+
public static readonly Cubic easeOutCubic = new Cubic(0.215f, 0.61f, 0.355f, 1.0f);
319+
320+
public static readonly Cubic easeOutQuart = new Cubic(0.165f, 0.84f, 0.44f, 1.0f);
321+
322+
public static readonly Cubic easeOutQuint = new Cubic(0.23f, 1.0f, 0.32f, 1.0f);
323+
324+
public static readonly Cubic easeOutExpo = new Cubic(0.19f, 1.0f, 0.22f, 1.0f);
325+
326+
public static readonly Cubic easeOutCirc = new Cubic(0.075f, 0.82f, 0.165f, 1.0f);
327+
328+
public static readonly Cubic easeOutBack = new Cubic(0.175f, 0.885f, 0.32f, 1.275f);
329+
292330
public static readonly Curve easeInOut = new Cubic(0.42f, 0.0f, 0.58f, 1.0f);
293331

332+
public static readonly Cubic easeInOutSine = new Cubic(0.445f, 0.05f, 0.55f, 0.95f);
333+
334+
public static readonly Cubic easeInOutQuad = new Cubic(0.455f, 0.03f, 0.515f, 0.955f);
335+
336+
public static readonly Cubic easeInOutCubic = new Cubic(0.645f, 0.045f, 0.355f, 1.0f);
337+
338+
public static readonly Cubic easeInOutQuart = new Cubic(0.77f, 0, 0.175f, 1.0f);
339+
340+
public static readonly Cubic easeInOutQuint = new Cubic(0.86f, 0, 0.07f, 1.0f);
341+
342+
public static readonly Cubic easeInOutExpo = new Cubic(1.0f, 0, 0, 1.0f);
343+
344+
public static readonly Cubic easeInOutCirc = new Cubic(0.785f, 0.135f, 0.15f, 0.86f);
345+
346+
public static readonly Cubic easeInOutBack = new Cubic(0.68f, -0.55f, 0.265f, 1.55f);
347+
294348
public static readonly Curve fastOutSlowIn = new Cubic(0.4f, 0.0f, 0.2f, 1.0f);
295349

296350
public static readonly Curve bounceIn = new _BounceInCurve();

Runtime/animation/listener_helpers.mixin.gen.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public override void addListener(VoidCallback listener) {
6161
}
6262

6363
public override void removeListener(VoidCallback listener) {
64-
this._listeners.Remove(listener);
65-
this.didUnregisterListener();
64+
bool removed = this._listeners.Remove(listener);
65+
if (removed) {
66+
this.didUnregisterListener();
67+
}
6668
}
6769

6870
public void notifyListeners() {
@@ -98,8 +100,10 @@ public override void addListener(VoidCallback listener) {
98100
}
99101

100102
public override void removeListener(VoidCallback listener) {
101-
this._listeners.Remove(listener);
102-
this.didUnregisterListener();
103+
bool removed = this._listeners.Remove(listener);
104+
if (removed) {
105+
this.didUnregisterListener();
106+
}
103107
}
104108

105109
public void notifyListeners() {
@@ -136,8 +140,10 @@ public override void addStatusListener(AnimationStatusListener listener) {
136140
}
137141

138142
public override void removeStatusListener(AnimationStatusListener listener) {
139-
this._statusListeners.Remove(listener);
140-
this.didUnregisterListener();
143+
bool removed = this._statusListeners.Remove(listener);
144+
if (removed) {
145+
this.didUnregisterListener();
146+
}
141147
}
142148

143149
public void notifyStatusListeners(AnimationStatus status) {
@@ -173,8 +179,10 @@ public override void addStatusListener(AnimationStatusListener listener) {
173179
}
174180

175181
public override void removeStatusListener(AnimationStatusListener listener) {
176-
this._statusListeners.Remove(listener);
177-
this.didUnregisterListener();
182+
bool removed = this._statusListeners.Remove(listener);
183+
if (removed) {
184+
this.didUnregisterListener();
185+
}
178186
}
179187

180188
public void notifyStatusListeners(AnimationStatus status) {
@@ -210,8 +218,10 @@ public override void addStatusListener(AnimationStatusListener listener) {
210218
}
211219

212220
public override void removeStatusListener(AnimationStatusListener listener) {
213-
this._statusListeners.Remove(listener);
214-
this.didUnregisterListener();
221+
bool removed = this._statusListeners.Remove(listener);
222+
if (removed) {
223+
this.didUnregisterListener();
224+
}
215225
}
216226

217227
public void notifyStatusListeners(AnimationStatus status) {

Runtime/animation/listener_helpers.mixin.njk

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ namespace Unity.UIWidgets.animation {
6262
}
6363

6464
public override void removeListener(VoidCallback listener) {
65-
this._listeners.Remove(listener);
66-
this.didUnregisterListener();
65+
bool removed = this._listeners.Remove(listener);
66+
if (removed) {
67+
this.didUnregisterListener();
68+
}
6769
}
6870

6971
public void notifyListeners() {
@@ -103,8 +105,10 @@ namespace Unity.UIWidgets.animation {
103105
}
104106

105107
public override void removeStatusListener(AnimationStatusListener listener) {
106-
this._statusListeners.Remove(listener);
107-
this.didUnregisterListener();
108+
bool removed = this._statusListeners.Remove(listener);
109+
if (removed) {
110+
this.didUnregisterListener();
111+
}
108112
}
109113

110114
public void notifyStatusListeners(AnimationStatus status) {

Runtime/animation/tween.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ public override Offset lerp(float t) {
213213
}
214214
}
215215

216+
class ConstantTween<T> : Tween<T> {
217+
public ConstantTween(T value) : base(begin: value, end: value) {
218+
}
219+
220+
public override T lerp(float t) {
221+
return this.begin;
222+
}
223+
224+
public override string ToString() {
225+
return $"{this.GetType()}(value: {this.begin})";
226+
}
227+
}
228+
216229
public class CurveTween : Animatable<float> {
217230
public CurveTween(Curve curve = null) {
218231
D.assert(curve != null);

Runtime/foundation/node.mixin.gen.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ CanonicalMixinDiagnosticableTree _getCanonical() {
204204

205205
static readonly Dictionary<_DependencyList, WeakReference> _canonicalObjects =
206206
new Dictionary<_DependencyList, WeakReference>();
207-
208-
public bool pureWidget { get; set; } // pure = false, if canonicalEquals should not be used.
207+
208+
public bool pureWidget { get; set; } // if canonicalEquals should not be used.
209209

210210
public override bool Equals(object obj) {
211211
if (ReferenceEquals(null, obj)) {

Runtime/rendering/object.mixin.gen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ public int childCount {
994994
get { return this._childCount; }
995995
}
996996

997-
public new bool debugValidateChild(RenderObject child) {
997+
public bool debugValidateChild(RenderObject child) {
998998
D.assert(() => {
999999
if (!(child is ChildType)) {
10001000
throw new UIWidgetsError(

0 commit comments

Comments
 (0)