123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- class ZTween {
- /**
- * That works:
- *
- * .use(gameObject)
- * .scaleFrom(Vector3)
- * .scaleTo(Vector3, time = 0, transition = Equations.none)
- * .moveFrom(Vector3)
- * .moveTo(Vector3, time = 0, transition = Equations.none)
- * .rotateFrom(Quaternion)
- * .rotateTo(Quaternion, time = 0, transition = Equations.none)
- * .call(Action)
- * .wait(time)
- *
- * use(getter, setter)
- * .
- * //.play()
- * //.pause()
- *
- * Does not work:
- *
- * use(ref float)
- *
- * TODO: decide:
- * . parallellize changes? how to rotate AND move something at the same time?
- *
- */
- /**
- * Examples:
- * .call(() => logDone("over"))
- * .call(someFunction)
- * // Getter/setter, for function pairs
- * ZTween.use(getValue, setValue).valueTo(1.0f, 1, Easing.quadOut).call(() => logDone("value ok"));
- * // Lambdas, for getter-setter or member pairs
- * ZTween.use(() => testNum, x => testNum = x).valueTo(1.0f, 1, Easing.quadOut).call(() => logDone("value ok"));
- */
- /*
- //transform.localScale = new Vector3(2, 2, 2);
- // http://www.createjs.com/Docs/TweenJS/modules/TweenJS.html
- ZTween.use(gameObject).scaleTo(new Vector3(2, 2, 2), 1.0f, Easing.quadOut).wait(1).call(Func).set("visible", false).play();
- ZTween.use(gameObject).pause();
- ZTween.use(gameObject).resume();
- ZTween.use(gameObject).stop();
- ZTween.use(gameObject, loop, useTicks);
- */
- #region ZTween static interface
- // Static properties
- private static ZTweenSurrogate tweenSurrogate;
- // ================================================================================================================
- // PUBLIC STATIC INTERFACE ----------------------------------------------------------------------------------------
- public static ZTweenGameObjectSequence use(GameObject gameObject) {
- return new ZTweenGameObjectSequence(gameObject);
- }
- public static ZTweenGetterSetterSequence use(Func<float> getValueFunction, Action<float> setValueFunction) {
- return new ZTweenGetterSetterSequence(getValueFunction, setValueFunction);
- }
- // ================================================================================================================
- // PRIVATE STATIC INTERFACE ---------------------------------------------------------------------------------------
- static ZTween() {
- GameObject surrogateObject = new GameObject("ZTween-controller");
- tweenSurrogate = surrogateObject.AddComponent<ZTweenSurrogate>();
- }
- #endregion
- // ================================================================================================================
- // INTERNAL CLASSES -----------------------------------------------------------------------------------------------
- // Aux classes
- class ZTweenStepMetadata {
- public bool hasStarted;
- public bool hasCompleted;
- public float timeStart;
- public float timeEnd;
- public float timeDuration {
- get {
- return timeEnd - timeStart;
- }
- }
- }
- internal interface IZTweenStep {
- void start();
- void update(float t);
- void end();
- float getDuration();
- }
- // Sequences
- internal class ZTweenSequence {
- // Instances
- private List<IZTweenStep> sequenceSteps;
- private List<ZTweenStepMetadata> sequenceStepsMetadatas;
- // Properties
- private bool isPlaying;
- private int currentStep;
- private float startTime;
- private float pauseTime;
- private float executedTime;
- private float duration;
- // ================================================================================================================
- // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
- public ZTweenSequence() {
- // Create a new ZTween
- // Add to surrogate
- ZTween.tweenSurrogate.add(this);
- // Create lists
- sequenceSteps = new List<IZTweenStep>();
- sequenceStepsMetadatas = new List<ZTweenStepMetadata>();
- // Set properties
- isPlaying = true;
- currentStep = 0;
- startTime = Time.time;
- executedTime = 0;
- duration = 0;
- }
- // ================================================================================================================
- // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
- // Play control methods
- public ZTweenSequence play() {
- if (!isPlaying) {
- isPlaying = true;
- float timePaused = Time.time - pauseTime;
- startTime += timePaused;
- }
- return this;
- }
- public ZTweenSequence pause() {
- if (isPlaying) {
- isPlaying = false;
- pauseTime = Time.time;
- }
- return this;
- }
- // Utility methods
- public ZTweenSequence call(Action action) {
- addStep(new ZTweenStepCall(action));
- return this;
- }
- public ZTweenSequence wait(float duration) {
- duration += duration;
- return this;
- }
- // ================================================================================================================
- // PRIVATE INTERFACE ----------------------------------------------------------------------------------------------
- // Core tween step control methods
- internal void addStep(IZTweenStep step) {
- sequenceSteps.Add(step);
- ZTweenStepMetadata tweenMetadata = new ZTweenStepMetadata();
- tweenMetadata.timeStart = startTime + duration;
- duration += step.getDuration();
- tweenMetadata.timeEnd = startTime + duration;
- sequenceStepsMetadatas.Add(tweenMetadata);
- }
- internal void update() {
- // Update current step(s) based on the time
- // Check if finished
- if (currentStep >= sequenceSteps.Count) {
- // Finished all, remove itself
- destroy();
- } else {
- bool shouldUpdateOnce = isPlaying;
- while (shouldUpdateOnce && currentStep < sequenceSteps.Count) {
- shouldUpdateOnce = false;
- if (Time.time >= sequenceStepsMetadatas[currentStep].timeStart) {
- // Start the current tween step if necessary
- if (!sequenceStepsMetadatas[currentStep].hasStarted) {
- sequenceSteps[currentStep].start();
- sequenceStepsMetadatas[currentStep].hasStarted = true;
- }
- // Update the current tween step
- sequenceSteps[currentStep].update(Mathf.Clamp01(Mathf.InverseLerp(sequenceStepsMetadatas[currentStep].timeStart, sequenceStepsMetadatas[currentStep].timeEnd, Time.time)));
- // Check if it's finished
- if (Time.time >= sequenceStepsMetadatas[currentStep].timeEnd) {
- if (!sequenceStepsMetadatas[currentStep].hasCompleted) {
- sequenceSteps[currentStep].end();
- sequenceStepsMetadatas[currentStep].hasCompleted = true;
- executedTime += sequenceStepsMetadatas[currentStep].timeDuration;
- shouldUpdateOnce = true;
- currentStep++;
- }
- }
- }
- }
- }
- }
- internal void destroy() {
- tweenSurrogate.remove(this);
- }
- internal Func<float, float> getTransition(Func<float, float> transition) {
- return transition == null ? Easing.none : transition;
- }
- }
- internal class ZTweenGetterSetterSequence:ZTweenSequence {
- // Instances
- private Func<float> targetGet;
- private Action<float> targetSet;
- // ================================================================================================================
- // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
- public ZTweenGetterSetterSequence(Func<float> targetGet, Action<float> targetSet):base() {
- this.targetGet = targetGet;
- this.targetSet = targetSet;
- }
- // ================================================================================================================
- // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
- public ZTweenGetterSetterSequence valueFrom(float value) {
- addStep(new ZTweenStepValueFrom(targetSet, value));
- return this;
- }
- public ZTweenGetterSetterSequence valueTo(float value, float duration = 0, Func<float, float> transition = null) {
- addStep(new ZTweenStepValueTo(targetGet, targetSet, value, duration, getTransition(transition)));
- return this;
- }
- }
- internal class ZTweenGameObjectSequence:ZTweenSequence {
- // Instances
- private GameObject targetGameObject;
- // ================================================================================================================
- // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
- public ZTweenGameObjectSequence(GameObject gameObject):base() {
- this.targetGameObject = gameObject;
- }
- // ================================================================================================================
- // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
- public ZTweenGameObjectSequence scaleFrom(Vector3 scale) {
- addStep(new ZTweenStepScaleFrom(targetGameObject, scale));
- return this;
- }
- public ZTweenGameObjectSequence scaleTo(Vector3 scale, float duration = 0, Func<float, float> transition = null) {
- addStep(new ZTweenStepScaleTo(targetGameObject, scale, duration, getTransition(transition)));
- return this;
- }
- public ZTweenGameObjectSequence moveFrom(Vector3 position) {
- addStep(new ZTweenStepPositionFrom(targetGameObject, position));
- return this;
- }
- public ZTweenGameObjectSequence moveTo(Vector3 position, float duration = 0, Func<float, float> transition = null) {
- addStep(new ZTweenStepPositionTo(targetGameObject, position, duration, getTransition(transition)));
- return this;
- }
- public ZTweenGameObjectSequence rotateFrom(Quaternion rotation) {
- addStep(new ZTweenStepRotationFrom(targetGameObject, rotation));
- return this;
- }
- public ZTweenGameObjectSequence rotateTo(Quaternion rotation, float duration = 0, Func<float, float> transition = null) {
- addStep(new ZTweenStepRotationTo(targetGameObject, rotation, duration, getTransition(transition)));
- return this;
- }
- }
- // Steps for generic sequences
- class ZTweenStepCall:IZTweenStep {
- // Properties
- private Action action;
- // Extension functions
- public ZTweenStepCall(Action action) {
- this.action = action;
- }
- public void start() { }
- public void update(float t) { }
- public void end() {
- action();
- }
- public float getDuration() {
- return 0;
- }
- }
- // Steps for GetterSetter sequences
- class ZTweenStepValueFrom:IZTweenStep {
- // Properties
- private Action<float> targetSet;
- private float targetValue;
- // Extension functions
- public ZTweenStepValueFrom(Action<float> targetSet, float targetValue) {
- this.targetSet = targetSet;
- this.targetValue = targetValue;
- }
- public void start() { }
- public void update(float t) { }
- public void end() {
- targetSet(targetValue);
- }
- public float getDuration() {
- return 0;
- }
- }
- class ZTweenStepValueTo:IZTweenStep {
- // Properties
- private Func<float> targetGet;
- private Action<float> targetSet;
- private float duration;
- private float startValue;
- private float targetValue;
- private Func<float, float> transition;
- // Extension functions
- public ZTweenStepValueTo(Func<float> targetGet, Action<float> targetSet, float targetValue, float duration, Func<float, float> transition) {
- this.targetGet = targetGet;
- this.targetSet = targetSet;
- this.duration = duration;
- this.targetValue = targetValue;
- this.transition = transition;
- }
- public void start() {
- this.startValue = targetGet();
- }
- public void update(float t) {
- targetSet(Mathf.LerpUnclamped(startValue, targetValue, transition(t)));
- }
- public void end() {
- targetSet(targetValue);
- }
- public float getDuration() {
- return duration;
- }
- }
- // Steps for GameObject sequences
- class ZTweenStepScaleFrom:IZTweenStep {
- // Properties
- private GameObject target;
- private Vector3 targetValue;
- // Extension functions
- public ZTweenStepScaleFrom(GameObject target, Vector3 targetValue) {
- this.target = target;
- this.targetValue = targetValue;
- }
- public void start() { }
- public void update(float t) { }
- public void end() {
- target.transform.localScale = targetValue;
- }
- public float getDuration() {
- return 0;
- }
- }
- class ZTweenStepScaleTo:IZTweenStep {
- // Properties
- private GameObject target;
- private float duration;
- private Vector3 startValue;
- private Vector3 targetValue;
- private Func<float, float> transition;
- // Extension functions
- public ZTweenStepScaleTo(GameObject target, Vector3 targetValue, float duration, Func<float, float> transition) {
- this.target = target;
- this.duration = duration;
- this.targetValue = targetValue;
- this.transition = transition;
- }
- public void start() {
- this.startValue = target.transform.localScale;
- }
- public void update(float t) {
- target.transform.localScale = Vector3.LerpUnclamped(startValue, targetValue, transition(t));
- }
- public void end() {
- target.transform.localScale = targetValue;
- }
- public float getDuration() {
- return duration;
- }
- }
- class ZTweenStepPositionFrom:IZTweenStep {
- // Properties
- private GameObject target;
- private Vector3 targetValue;
- // Extension functions
- public ZTweenStepPositionFrom(GameObject target, Vector3 targetValue) {
- this.target = target;
- this.targetValue = targetValue;
- }
- public void start() { }
- public void update(float t) { }
- public void end() {
- target.transform.localPosition = targetValue;
- }
- public float getDuration() {
- return 0;
- }
- }
- class ZTweenStepPositionTo:IZTweenStep {
- // Properties
- private GameObject target;
- private float duration;
- private Vector3 startValue;
- private Vector3 targetValue;
- private Func<float, float> transition;
- // Extension functions
- public ZTweenStepPositionTo(GameObject target, Vector3 targetValue, float duration, Func<float, float> transition) {
- this.target = target;
- this.duration = duration;
- this.targetValue = targetValue;
- this.transition = transition;
- }
- public void start() {
- this.startValue = target.transform.localPosition;
- }
- public void update(float t) {
- target.transform.localPosition = Vector3.LerpUnclamped(startValue, targetValue, transition(t));
- }
- public void end() {
- target.transform.localPosition = targetValue;
- }
- public float getDuration() {
- return duration;
- }
- }
- class ZTweenStepRotationFrom:IZTweenStep {
- // Properties
- private GameObject target;
- private Quaternion targetValue;
- // Extension functions
- public ZTweenStepRotationFrom(GameObject target, Quaternion targetValue) {
- this.target = target;
- this.targetValue = targetValue;
- }
- public void start() { }
- public void update(float t) { }
- public void end() {
- target.transform.localRotation = targetValue;
- }
- public float getDuration() {
- return 0;
- }
- }
- class ZTweenStepRotationTo:IZTweenStep {
- // Properties
- private GameObject target;
- private float duration;
- private Quaternion startValue;
- private Quaternion targetValue;
- private Func<float, float> transition;
- // Extension functions
- public ZTweenStepRotationTo(GameObject target, Quaternion targetValue, float duration, Func<float, float> transition) {
- this.target = target;
- this.duration = duration;
- this.targetValue = targetValue;
- this.transition = transition;
- }
- public void start() {
- this.startValue = target.transform.localRotation;
- }
- public void update(float t) {
- target.transform.localRotation = Quaternion.SlerpUnclamped(startValue, targetValue, transition(t));
- }
- public void end() {
- target.transform.localRotation = targetValue;
- }
- public float getDuration() {
- return duration;
- }
- }
- }
|