Skip to content

Instantly share code, notes, and snippets.

@digiwombat
Created December 5, 2020 18:35
Show Gist options
  • Save digiwombat/21ec82fa92c258d2bc572ecb22263e86 to your computer and use it in GitHub Desktop.
Save digiwombat/21ec82fa92c258d2bc572ecb22263e86 to your computer and use it in GitHub Desktop.
An anchor tweener for Juce Feedbacks.
using Juce.Tween;
using System.Collections.Generic;
using UnityEngine;
namespace Juce.Feedbacks
{
[FeedbackIdentifier("Anchors", "RectTransform/")]
public class RectTransformAnchorFeedback : Feedback
{
[Header(FeedbackSectionsUtils.TargetSection)]
[SerializeField] private RectTransform target = default;
[Header(FeedbackSectionsUtils.ValuesSection)]
[SerializeField] private StartEndVector2Property value = default;
[Header(FeedbackSectionsUtils.TimingSection)]
[SerializeField] [Min(0)] private float delay = default;
[SerializeField] [Min(0)] private float duration = 1.0f;
[Header(FeedbackSectionsUtils.EasingSection)]
[SerializeField] private EasingProperty easing = default;
[Header(FeedbackSectionsUtils.LoopSection)]
[SerializeField] private LoopProperty looping = default;
public RectTransform Target { get => target; set => target = value; }
public StartEndVector2Property Value => value;
public float Delay { get => delay; set => delay = Mathf.Max(0, value); }
public float Duration { get => duration; set => duration = Mathf.Max(0, value); }
public EasingProperty Easing => easing;
public LoopProperty Looping => looping;
public override bool GetFeedbackErrors(out string errors)
{
if (target == null)
{
errors = ErrorUtils.TargetNullErrorMessage;
return true;
}
errors = string.Empty;
return false;
}
public override string GetFeedbackTargetInfo()
{
return target != null ? target.gameObject.name : string.Empty;
}
public override void GetFeedbackInfo(ref List<string> infoList)
{
InfoUtils.GetTimingInfo(ref infoList, delay, duration);
InfoUtils.GetStartEndVector2PropertyInfo(ref infoList, value);
}
public override ExecuteResult OnExecute(FlowContext context, SequenceTween sequenceTween)
{
if (target == null)
{
return null;
}
Tween.Tween delayTween = null;
if (delay > 0)
{
delayTween = new WaitTimeTween(delay);
sequenceTween.Append(delayTween);
}
Vector2 newValue = Vector2.zero;
var maxOffset = target.anchorMax - target.anchorMin;
if (value.UseStartValue)
{
SequenceTween startSequence = new SequenceTween();
if (value.UseStartX)
{
newValue.x = value.StartValueX;
}
if (value.UseStartY)
{
newValue.y = value.StartValueY;
}
startSequence.Join(target.TweenAnchorMin(newValue, 0.0f));
startSequence.Join(target.TweenAnchorMax(newValue + maxOffset, 0.0f));
sequenceTween.Append(startSequence);
}
SequenceTween endSequence = new SequenceTween();
newValue = Vector2.zero;
if (value.UseEndX)
{
newValue.x = value.EndValueX;
}
if (value.UseEndY)
{
newValue.y = value.EndValueY;
}
endSequence.Join(target.TweenAnchorMin(newValue, duration));
endSequence.Join(target.TweenAnchorMax(newValue + maxOffset, duration));
Tween.Tween progressTween = endSequence;
sequenceTween.Append(endSequence);
EasingUtils.SetEasing(sequenceTween, easing);
LoopUtils.SetLoop(sequenceTween, looping);
ExecuteResult result = new ExecuteResult();
result.DelayTween = delayTween;
result.ProgresTween = progressTween;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment