Skip to content

Commit 738124b

Browse files
committed
Spline rotations, edit-mode fixes, enum reverts
1 parent 500ab51 commit 738124b

8 files changed

Lines changed: 70 additions & 142 deletions

File tree

Assets/SlimTween/Scripts/STCurves.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ namespace Breadnone.Extension
99
public sealed class STSplines
1010
{
1111
public STFloat sfloat;
12-
public STSplines(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float time)
12+
public STSplines(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float time, bool lookAtDirection, bool is2d)
1313
{
1414
Vector3 from = transform.position;
1515
sfloat = STPool.GetInstance<STFloat>(transform.gameObject.GetInstanceID());
1616
(sfloat as ISlimRegister).GetSetDuration = time;
1717
sfloat.setEase(Ease.Linear);
18-
Three(transform, start, middle, end, time, sfloat);
18+
Three(transform, start, middle, end, time, sfloat, lookAtDirection, is2d);
1919
}
20-
void Three(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float time, STFloat sfloat)
20+
void Three(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float time, STFloat sfloat, bool lookAtDirection, bool is2d)
2121
{
2222
// Calculate control points for cubic Bezier curve
2323
Vector3 controlStart = start + 2f * (middle - start) / 3f;
@@ -39,7 +39,29 @@ void Three(Transform transform, Vector3 start, Vector3 middle, Vector3 end, floa
3939
3f * oneMinusT * t2 * controlEnd +
4040
t3 * end;
4141

42+
var dir = position;
43+
var opos = transform.position;
4244
transform.position = position;
45+
46+
// Calculate rotation to face the direction of movement
47+
if(lookAtDirection)
48+
{
49+
Vector3 direction = (dir - opos).normalized;
50+
51+
if (direction != Vector3.zero)
52+
{
53+
if(is2d)
54+
{
55+
Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, direction);
56+
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, tick);
57+
}
58+
else
59+
{
60+
Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, direction);
61+
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, tick);
62+
}
63+
}
64+
}
4365
});
4466
}
4567
}

Assets/SlimTween/Scripts/STween.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,9 +1238,9 @@ public static SlimTransform translateLocal(GameObject gameObject, Vector3 direct
12381238
/// <param name="middle">Middle point.</param>
12391239
/// <param name="end">End point.</param>
12401240
/// <param name="duration">Duration.</param>
1241-
public static STFloat spline(Transform transform, Vector3 middle, Vector3 end, float duration)
1241+
public static STFloat spline(Transform transform, Vector3 middle, Vector3 end, float duration, bool lookAtDirection, bool is2d)
12421242
{
1243-
var instance = new STSplines(transform, transform.position, middle, end, duration);
1243+
var instance = new STSplines(transform, transform.position, middle, end, duration, lookAtDirection, is2d);
12441244
return instance.sfloat;
12451245
}
12461246
/// <summary>
@@ -1252,9 +1252,9 @@ public static STFloat spline(Transform transform, Vector3 middle, Vector3 end, f
12521252
/// <param name="end">End point.</param>
12531253
/// <param name="duration"></param>
12541254
/// <returns></returns>/
1255-
public static STFloat splineFrom(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float duration)
1255+
public static STFloat splineFrom(Transform transform, Vector3 start, Vector3 middle, Vector3 end, float duration, bool lookAtDirection, bool is2d)
12561256
{
1257-
var instance = new STSplines(transform, start, middle, end, duration);
1257+
var instance = new STSplines(transform, start, middle, end, duration, lookAtDirection, is2d);
12581258
return instance.sfloat;
12591259
}
12601260
/// <summary>

Assets/SlimTween/Scripts/TweenClasses.cs

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525
using UnityEngine;
2626
using System;
2727
using System.Runtime.CompilerServices;
28-
using System.Collections.Specialized;
29-
using System.Collections;
30-
using System.Collections.Generic;
3128

3229
#if UNITY_EDITOR
3330
using UnityEditor;
@@ -43,7 +40,7 @@ public class TweenClass : ISlimRegister
4340
/// <summary>Internal use only.</summary>
4441
public TProps tprops;
4542
/// <summary>The tween state of this instance.</summary>
46-
protected int state;
43+
protected TweenState state = TweenState.None;
4744
/// <summary>The on update function.</summary>
4845
protected Action<bool> update;
4946
/// <summary>The total duration of this instance when tweening.</summary>
@@ -61,21 +58,11 @@ public class TweenClass : ISlimRegister
6158
/// <summary>Unscaled or scaled Time.delta.</summary>
6259
bool ISlimRegister.UnscaledTimeIs { get => unscaledTime; set => unscaledTime = value; }
6360
void ISlimRegister.SetEase(Ease easeType) => ease = (int)easeType;
64-
void ISlimRegister.SetState(TweenState stateType) => state = (int)stateType;
61+
void ISlimRegister.SetState(TweenState stateType) => state = stateType;
6562
void ISlimRegister.SetEstimationTime()=> EstimateDuration(0, 1f, tprops.speed);
6663
/// <summary>Flips the delta ticks</summary>
6764
protected void FlipTick()
6865
{
69-
if (flipTick)
70-
{
71-
tprops.runningFloat = 1f;
72-
}
73-
else
74-
{
75-
tprops.runningFloat = 0f;
76-
}
77-
78-
7966
flipTick = !flipTick;
8067

8168
//2 = speed based here
@@ -140,6 +127,7 @@ protected virtual void InternalOnUpdate()
140127
return;
141128
}
142129
#endif
130+
143131
if (!unscaledTime)
144132
{
145133
if (!flipTick)
@@ -170,7 +158,7 @@ protected virtual void ResetLoop() { }
170158
public void RunUpdate()
171159
{
172160
//if Paused or or None. Stop!
173-
if (state < 2)
161+
if ((int)state < 2)
174162
return;
175163

176164
if (tprops.updatecondition == 3)
@@ -182,7 +170,7 @@ public void RunUpdate()
182170
}
183171
else if (tprops.updatecondition == 4)
184172
{
185-
if (runningTime < 0.0001f && CheckIfFinished())
173+
if (runningTime < 0.001f && CheckIfFinished())
186174
{
187175
return;
188176
}
@@ -292,7 +280,6 @@ protected bool InvokeRepeat()
292280

293281
ResetLoop();
294282
runningTime = 0.00013f;
295-
tprops.runningFloat = 0.00013f;
296283

297284
if (tprops.oncompleteRepeat)
298285
{
@@ -320,7 +307,6 @@ bool checkInfiniteClamp()
320307
{
321308
ResetLoop();
322309
runningTime = 0.00013f;
323-
tprops.runningFloat = 0.00013f;
324310
return true;
325311
}
326312

@@ -337,30 +323,35 @@ protected void Clear()
337323
ease = 0;
338324
TweenManager.RemoveFromActiveTween(this);
339325
}
340-
public float EstimateDuration(float current, float target, float speed)
326+
/// <summary>Estimation based interpolation</summary>
327+
/// <param name="current"></param>
328+
/// <param name="target"></param>
329+
/// <param name="speed"></param>
330+
float EstimateDuration(float current, float target, float speed)
341331
{
342332
float deltaTime = !unscaledTime ? Time.deltaTime : Time.unscaledDeltaTime;
343333

344334
// Calculate the distance between current and target value
345335
float distance = Mathf.Abs(target - current);
346336

347337
// Calculate the time duration to reach the target value
348-
float duration = distance / (speed / 3f * deltaTime);
338+
float duration = distance / (speed / 3f);
349339
return duration;
350340
}
351341
///<summary>Checks if tweening. Paused tween will also mean tweening.</summary>
352-
public bool IsTweening => state != 0;
342+
public bool IsTweening => state != TweenState.None;
353343
///<summary>Checks if paused.</summary>
354-
public bool IsPaused => state == 1;
344+
public bool IsPaused => state == TweenState.Paused;
355345
/// <summary>This can mean it's completed or not doing anything. Use IsTweening and IsPaused to check the states instead.</summary>
356-
public bool IsNone => state == 0;
346+
public bool IsNone => state == TweenState.None;
357347
///<summary>Pauses the tweening.</summary>
358348
public void Pause()
359349
{
360350
if (!IsTweening || IsPaused)
361351
return;
362352

363-
state = 1;
353+
//1 = paused
354+
state = TweenState.Paused;
364355
}
365356
///<summary>Resumes paused tween instances, if any.</summary>
366357
//The parameter updaterTransform this should be useful for re-scheduling purposes. e.g : Tween chaining.
@@ -379,7 +370,8 @@ public void Resume(bool updateTransform = false)
379370
UpdateFrame();
380371
}
381372

382-
state = 2;
373+
//2 = resume
374+
state = TweenState.Tweening;
383375
}
384376
/// <summary>
385377
/// Registers onComplete that will be executed at the very last of the tween (if successfully tweened).
@@ -465,8 +457,6 @@ public void Dispose()
465457
public sealed class TProps
466458
{
467459
public float runningSpeed;
468-
/// <summary>The running underlying tick value;</summary>
469-
public float runningFloat = 0.00012f;
470460
/// <summary>Instance id</summary>
471461
public int id = -1;
472462
/// <summary>Instance unique id based on the hashcode.</summary>
@@ -499,7 +489,8 @@ public void SetDefault()
499489
animationCurve = null;
500490
delayedTime = -1;
501491
lerptype = 0;
502-
runningFloat = 0.00012f;
492+
runningSpeed = 0;
493+
updatecondition = 0;
503494
}
504495
public void ResetLoopProperties()
505496
{
@@ -655,7 +646,7 @@ void ISlimTween.UpdateTransform()
655646
interp.SetFrom(transform.localScale);
656647
}
657648
}
658-
(Vector3 from, Vector3 to) ISlimTween.FromTo { get { return (interp.from(), interp.to()); } set { interp.SetFrom(value.from); interp.SetTo(value.to); } }
649+
(Vector3 from, Vector3 to) ISlimTween.FromTo { get { return (interp.from, interp.to); } set { interp.SetFrom(value.from); interp.SetTo(value.to); } }
659650
/// <summary>Initialize transform base value.</summary>
660651
/// <param name="objectTransform">The transform.</param>
661652
/// <param name="from">Starting value</param>
@@ -737,22 +728,22 @@ void LerpEuler(float value)
737728
{
738729
if (!isLocal)
739730
{
740-
transform.rotation = Quaternion.Euler(interp.to() * value);
731+
transform.rotation = Quaternion.Euler(interp.to * value);
741732
}
742733
else
743734
{
744-
transform.localRotation = Quaternion.Euler(interp.to() * value);
735+
transform.localRotation = Quaternion.Euler(interp.to * value);
745736
}
746737
}
747738
/// <summary>Rotates based on target point.</summary>
748739
void LerpRotateAround(float value)
749740
{
750-
transform.RotateAround(interp.from(), interp.to(), angle * value);
741+
transform.RotateAround(interp.from, interp.to, angle * value);
751742
}
752743
/// <summary>Rotates based on target point.</summary>
753744
void LerpTranslate(float value)
754745
{
755-
transform.Translate(interp.to() * value, !isLocal ? Space.World : Space.Self);
746+
transform.Translate(interp.to * value, !isLocal ? Space.World : Space.Self);
756747
}
757748
}
758749
/// <summary>The sub base class.</summary>
@@ -775,7 +766,7 @@ public SlimRect()
775766
bool disableLerps;
776767
float angle;
777768
void ISlimTween.DisableLerps(bool state) { disableLerps = state; }
778-
(Vector3 from, Vector3 to) ISlimTween.FromTo { get { return (interp.from(), interp.to()); } set { interp.SetFrom(value.from); interp.SetTo(value.to); } }
769+
(Vector3 from, Vector3 to) ISlimTween.FromTo { get { return (interp.from, interp.to); } set { interp.SetFrom(value.from); interp.SetTo(value.to); } }
779770
bool ISlimTween.Locality { get => isLocal; set => isLocal = value; }
780771
/// <summary>Previous assigned type.</summary>
781772
TransformType ISlimTween.GetTransformType { get => (TransformType)type; set => type = value; }
@@ -924,17 +915,17 @@ void LerpEuler(float value)
924915
{
925916
if (!isLocal)
926917
{
927-
transform.rotation = Quaternion.Euler(interp.to() * value);
918+
transform.rotation = Quaternion.Euler(interp.to * value);
928919
}
929920
else
930921
{
931-
transform.localRotation = Quaternion.Euler(interp.to() * value);
922+
transform.localRotation = Quaternion.Euler(interp.to * value);
932923
}
933924
}
934925
/// <summary>Rotates based on target point.</summary>
935926
void LerpRotateAround(float value)
936927
{
937-
transform.RotateAround(interp.from(), interp.to(), angle * value);
928+
transform.RotateAround(interp.from, interp.to, angle * value);
938929
}
939930
/// <summary>Interpolate delta value.</summary>
940931
void LerpSizeDelta(float value)
@@ -944,7 +935,7 @@ void LerpSizeDelta(float value)
944935
void LerpSizeAnchored(float value)
945936
{
946937
Vector2 myPrevPivot = transform.pivot;
947-
var to = interp.to();
938+
var to = interp.to;
948939
transform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, Mathf.Lerp(transform.rect.width, to.x, value));
949940
transform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Mathf.Lerp(transform.rect.height, to.y, value));
950941
transform.pivot = myPrevPivot;
@@ -989,27 +980,10 @@ public struct InterpolatorStruct
989980
float b;
990981
float z;
991982
float c;
992-
static Vector3 vec;
993-
public ref Vector3 from()
994-
{
995-
vec.Set(a, b, c);
996-
return ref vec;
997-
}
998-
public ref Vector3 to()
999-
{
1000-
vec.Set(x, y, z);
1001-
return ref vec;
1002-
}
1003-
public ref Vector3 UpdateRef(float tick)
1004-
{
1005-
vec.Set(
1006-
a + (x - a) * tick,
1007-
b + (y - b) * tick,
1008-
c + (z - c) * tick);
1009-
return ref vec;
1010-
}
1011-
public ref Vector3 GetVector() => ref vec;
1012-
983+
/// <summary>Starting value</summary>
984+
public Vector3 from => new Vector3(a, b, c);
985+
/// <summary>Target value</summary>
986+
public Vector3 to => new Vector3(x, y, z);
1013987
public void Set(Vector3 from, Vector3 to)
1014988
{
1015989
SetFrom(from);
@@ -1030,13 +1004,12 @@ public void SetTo(Vector3 to)
10301004
z = to.z;
10311005
}
10321006
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1033-
public ref Vector3 Interpolate(float tick)
1007+
public Vector3 Interpolate(float tick)
10341008
{
1035-
vec.Set(
1009+
return new Vector3(
10361010
a + (x - a) * tick,
10371011
b + (y - b) * tick,
10381012
c + (z - c) * tick);
1039-
return ref vec;
10401013
}
10411014
}
10421015
public struct TFloat6
@@ -1091,7 +1064,6 @@ public void Init(Transform transform, Transform[] followers, float closeDistance
10911064
protected override void InternalOnUpdate()
10921065
{
10931066
base.InternalOnUpdate();
1094-
10951067
isMoving = Vector3.Distance(lastpos, transform.position) > 0.001f;
10961068

10971069
for (int i = 0; i < followers.Length; i++)

Assets/SlimTween/Scripts/TweenManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static void ClearRemoveList()
166166
{
167167
var tmp = removeList[i];
168168

169-
activeTweens.Remove(tmp);
169+
activeTweens?.Remove(tmp);
170170
removeList[i] = null;
171171
ISlimRegister ievent = tmp;
172172

0 commit comments

Comments
 (0)