Skip to content

Commit

Permalink
refactor: MilHandleFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
buger404 committed Jul 22, 2024
1 parent 26c863d commit d5d3ce9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 52 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ var animation =
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
)
.Then(
Text.Milease((o, p) =>
Text.Milease((e) =>
{
var text = o as TMP_Text;
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
}, null,2f, 0f, EaseFunction.Linear)
var text = e.GetTarget<TMP_Text>();
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
}, null, 2f, 0f, EaseFunction.Linear)
)
.Then(
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)
Expand Down
3 changes: 1 addition & 2 deletions Scripts/Milease/Core/Animation/MilAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace Milease.Core.Animation
{
public delegate void MileaseHandleFunction(object target, float progress);
public class MilAnimation : ScriptableObject
{
public enum BlendingMode
Expand Down Expand Up @@ -107,7 +106,7 @@ public static AnimationPart SimplePart(object startValue, object toValue, float
};
}

internal static AnimationPart SimplePart(MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction, float duration, float delay = 0f,
internal static AnimationPart SimplePart(float duration, float delay = 0f,
EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In, BlendingMode blendingMode = BlendingMode.Default)
{
return new AnimationPart()
Expand Down
18 changes: 18 additions & 0 deletions Scripts/Milease/Core/Animation/MilHandleFunctionArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Milease.Core.Animator;

namespace Milease.Core.Animation
{
public struct MilHandleFunctionArgs
{
internal object target;
public float Progress { get; internal set; }
public RuntimeAnimationPart Animation { get; internal set; }
public MilInstantAnimator Animator { get; internal set; }

public T GetTarget<T>()
{
return (T)target;
}
}
public delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
}
3 changes: 3 additions & 0 deletions Scripts/Milease/Core/Animation/MilHandleFunctionArgs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions Scripts/Milease/Core/RuntimeAnimationPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Milease.Core.Animation;
using Milease.Core.Animator;
using Milease.Enums;
using Milease.Milease.Exception;
using UnityEngine;
Expand Down Expand Up @@ -34,19 +35,21 @@ public enum AnimationResetMode
public object StartValue, ToValue, OriginalValue;
public readonly object Target;
public readonly MilAnimation.AnimationPart Source;
public readonly MilInstantAnimator ParentAnimator;
public bool IsPrepared { get; private set; }
public string MemberPath { get; private set; }

private float lastProgress = -1f;

public RuntimeAnimationPart(object target, MilAnimation.AnimationPart animation, MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction = null)
public RuntimeAnimationPart(object target, MilInstantAnimator animator, MilAnimation.AnimationPart animation, MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction = null)
{
HandleFunction = handleFunction;
Source = animation;
Target = target;
Valid = true;
ResetFunction = resetFunction;
ValueType = ValueTypeEnum.SelfHandle;
ParentAnimator = animator;
MemberPath = handleFunction.GetHashCode().ToString();
}

Expand All @@ -71,7 +74,13 @@ public bool Reset(AnimationResetMode resetMode)

if (ResetFunction != null)
{
ResetFunction(Target, 0f);
ResetFunction(new MilHandleFunctionArgs()
{
Animation = this,
target = Target,
Progress = 0f,
Animator = ParentAnimator
});
return true;
}

Expand Down Expand Up @@ -118,14 +127,15 @@ public bool Reset(AnimationResetMode resetMode)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public RuntimeAnimationPart(object target, MilAnimation.AnimationPart animation, Type baseType, MemberInfo memberInfo = null)
public RuntimeAnimationPart(object target, MilInstantAnimator animator, MilAnimation.AnimationPart animation, Type baseType, MemberInfo memberInfo = null)
{
if (target == null)
{
throw new MilTargetNotFoundException();
}

Source = animation;
ParentAnimator = animator;

var curType = baseType;
if (memberInfo != null)
Expand Down Expand Up @@ -259,7 +269,13 @@ public static void SetValue(RuntimeAnimationPart ani, float pro)

if (ani.ValueType == ValueTypeEnum.SelfHandle)
{
ani.HandleFunction(ani.Target, pro);
ani.HandleFunction(new MilHandleFunctionArgs()
{
Animation = ani,
target = ani.Target,
Progress = pro,
Animator = ani.ParentAnimator
});
return;
}

Expand Down
44 changes: 22 additions & 22 deletions Scripts/Milease/Extension/MilInstantAnimatorExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Milease.Utils
public static class MilInstantAnimatorExtension
{
public static MilInstantAnimator AsMileaseKeyEvent(this Action action, float delay = 0f)
=> Milease(action, (_, _) => action.Invoke(), null, 0f, delay);
=> Milease(action, (_) => action.Invoke(), null, 0f, delay);

public static MilInstantAnimator AsMileaseHandleFunction(this MileaseHandleFunction func, float duration, float delay = 0f)
=> Milease(func, func, null, duration, delay);
Expand All @@ -32,37 +32,37 @@ public static MilInstantAnimator Milease(this object target, MileaseHandleFuncti
EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In,
MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
{
var animation = new MilInstantAnimator();
var ani = MilAnimation.SimplePart(handleFunction, resetFunction, duration, delay, easeFunction, easeType, blendingMode);
animation.Collection.Add(new List<RuntimeAnimationPart>()
var animator = new MilInstantAnimator();
var ani = MilAnimation.SimplePart(duration, delay, easeFunction, easeType, blendingMode);
animator.Collection.Add(new List<RuntimeAnimationPart>()
{
new (target, ani, handleFunction, resetFunction)
new (target, animator, ani, handleFunction, resetFunction)
});
return animation;
return animator;
}

public static MilInstantAnimator MileaseTo(this object target, string memberName, object toValue,
float duration, float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad,
EaseType easeType = EaseType.In)
{
var animation = new MilInstantAnimator();
var animator = new MilInstantAnimator();
var type = target.GetType();
var members = type.GetMember(memberName);
if (members.Length == 0)
{
throw new MilMemberNotFoundException(memberName);
}
var info = members[0];
animation.Collection.Add(new List<RuntimeAnimationPart>()
animator.Collection.Add(new List<RuntimeAnimationPart>()
{
new (target, MilAnimation.SimplePartTo(toValue, duration, delay, easeFunction, easeType), info.MemberType switch
new (target, animator, MilAnimation.SimplePartTo(toValue, duration, delay, easeFunction, easeType), info.MemberType switch
{
MemberTypes.Field => ((FieldInfo)info).FieldType,
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
_ => null
}, info)
});
return animation;
return animator;
}

public static MilInstantAnimator MileaseAdditive(this object target, string memberName, object startValue,
Expand All @@ -74,24 +74,24 @@ public static MilInstantAnimator Milease(this object target, string memberName,
float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In,
MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
{
var animation = new MilInstantAnimator();
var animator = new MilInstantAnimator();
var type = target.GetType();
var members = type.GetMember(memberName);
if (members.Length == 0)
{
throw new MilMemberNotFoundException(memberName);
}
var info = members[0];
animation.Collection.Add(new List<RuntimeAnimationPart>()
animator.Collection.Add(new List<RuntimeAnimationPart>()
{
new (target, MilAnimation.SimplePart(startValue, delay, easeFunction, easeType, blendingMode), info.MemberType switch
new (target, animator, MilAnimation.SimplePart(startValue, delay, easeFunction, easeType, blendingMode), info.MemberType switch
{
MemberTypes.Field => ((FieldInfo)info).FieldType,
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
_ => null
}, info)
});
return animation;
return animator;
}

public static MilInstantAnimator MileaseAdditive(this object target, string memberName, object startValue,
Expand All @@ -104,47 +104,47 @@ public static MilInstantAnimator Milease(this object target, string memberName,
object toValue, float duration, float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad,
EaseType easeType = EaseType.In, MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
{
var animation = new MilInstantAnimator();
var animator = new MilInstantAnimator();
var type = target.GetType();
var members = type.GetMember(memberName);
if (members.Length == 0)
{
throw new MilMemberNotFoundException(memberName);
}
var info = members[0];
animation.Collection.Add(new List<RuntimeAnimationPart>()
animator.Collection.Add(new List<RuntimeAnimationPart>()
{
new (target, MilAnimation.SimplePart(startValue, toValue, duration, delay, easeFunction, easeType, blendingMode), info.MemberType switch
new (target, animator, MilAnimation.SimplePart(startValue, toValue, duration, delay, easeFunction, easeType, blendingMode), info.MemberType switch
{
MemberTypes.Field => ((FieldInfo)info).FieldType,
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
_ => null
}, info)
});
return animation;
return animator;
}

public static MilInstantAnimator Milease(this object target, string memberName, params MilAnimation.AnimationPart[] animations)
{
var animation = new MilInstantAnimator();
var animator = new MilInstantAnimator();
var type = target.GetType();
var members = type.GetMember(memberName);
if (members.Length == 0)
{
throw new MilMemberNotFoundException(memberName);
}
var info = members[0];
animation.Collection.Add(
animator.Collection.Add(
animations.Select(x =>
new RuntimeAnimationPart(target, x, info.MemberType switch
new RuntimeAnimationPart(target, animator, x, info.MemberType switch
{
MemberTypes.Field => ((FieldInfo)info).FieldType,
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
_ => null
}, info)
).ToList()
);
return animation;
return animator;
}
}
}
16 changes: 8 additions & 8 deletions Scripts/Milease/Utils/HandleFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ namespace Milease.Utils
{
public static class HandleFunction
{
public static void Hide(object o, float t)
public static void Hide(MilHandleFunctionArgs e)
{
(o as GameObject)!.SetActive(t < 1f);
e.GetTarget<GameObject>().SetActive(e.Progress < 1f);
}
public static void Show(object o, float t)
public static void Show(MilHandleFunctionArgs e)
{
(o as GameObject)!.SetActive(t >= 1f);
e.GetTarget<GameObject>().SetActive(e.Progress >= 1f);
}
public static void DeativeWhenReset(object o, float t)
public static void DeativeWhenReset(MilHandleFunctionArgs e)
{
(o as GameObject)!.SetActive(false);
e.GetTarget<GameObject>().SetActive(false);
}
public static void ActiveWhenReset(object o, float t)
public static void ActiveWhenReset(MilHandleFunctionArgs e)
{
(o as GameObject)!.SetActive(true);
e.GetTarget<GameObject>().SetActive(true);
}

public static MileaseHandleFunction AutoActiveReset(GameObject go)
Expand Down
12 changes: 6 additions & 6 deletions ~Document/1. Instant Animator.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ animator.Reset(mode);
Implement the following delegate to create custom animations:

```c#
delegate void MileaseHandleFunction(object target, float progress);
delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
```

`target` is the object being animated, and `progress` is the eased progress of the animation, ranging approximately from [0.0, 1.0].
Call `e.GetTarget<T>()` to get the object being animated, and `e.Progress` is the eased progress of the animation, ranging approximately from [0.0, 1.0].

To create a custom animation with `Milease`:

Expand Down Expand Up @@ -152,11 +152,11 @@ var animation =
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
)
.Then(
Text.Milease((o, p) =>
Text.Milease((e) =>
{
var text = o as TMP_Text;
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
}, null,2f, 0f, EaseFunction.Linear)
var text = e.GetTarget<TMP_Text>();
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
}, null, 2f, 0f, EaseFunction.Linear)
)
.Then(
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)
Expand Down
12 changes: 6 additions & 6 deletions ~Document/1. 即时动画机.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ animator.Reset(mode);
通过实现以下委托来实现:

```c#
delegate void MileaseHandleFunction(object target, float progress);
delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
```

`target` 为目标物体,`progress` 为动画进度(经过缓动处理后的进行),范围大约是[0.0, 1.0]
调用`e.GetTarget<T>()` 获取目标物体,`e.Progress` 为动画进度(经过缓动处理后的进行),范围大约是[0.0, 1.0]

使用`Milease`创建自定义动画:

Expand Down Expand Up @@ -152,11 +152,11 @@ var animation =
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
)
.Then(
Text.Milease((o, p) =>
Text.Milease((e) =>
{
var text = o as TMP_Text;
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
}, null,2f, 0f, EaseFunction.Linear)
var text = e.GetTarget<TMP_Text>();
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
}, null, 2f, 0f, EaseFunction.Linear)
)
.Then(
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)
Expand Down

0 comments on commit d5d3ce9

Please sign in to comment.