spine-runtimes/spine-haxe/spine-haxe/spine/animation/PathConstraintMixTimeline.hx
2025-07-16 01:35:13 +02:00

134 lines
5.2 KiB
Haxe

/******************************************************************************
* Spine Runtimes License Agreement
* Last updated April 5, 2025. Replaces all prior versions.
*
* Copyright (c) 2013-2025, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
package spine.animation;
import spine.Event;
import spine.PathConstraint;
import spine.Skeleton;
/** Changes a path constraint's PathConstraint.mixRotate, PathConstraint.mixX, and
* PathConstraint.mixY. */
class PathConstraintMixTimeline extends CurveTimeline implements ConstraintTimeline {
private static inline var ENTRIES:Int = 4;
private static inline var ROTATE:Int = 1;
private static inline var X:Int = 2;
private static inline var Y:Int = 3;
/** The index of the path constraint in spine.Skeleton.pathConstraints that will be changed when this timeline is
* applied. */
public var constraintIndex:Int = 0;
public function new(frameCount:Int, bezierCount:Int, constraintIndex:Int) {
super(frameCount, bezierCount, Property.pathConstraintMix + "|" + constraintIndex);
this.constraintIndex = constraintIndex;
}
public override function getFrameEntries():Int {
return ENTRIES;
}
public function getConstraintIndex() {
return constraintIndex;
}
/** Sets the time and color for the specified frame.
* @param frame Between 0 and frameCount, inclusive.
* @param time The frame time in seconds. */
public function setFrame(frame:Int, time:Float, mixRotate:Float, mixX:Float, mixY:Float):Void {
frame <<= 2;
frames[frame] = time;
frames[frame + ROTATE] = mixRotate;
frames[frame + X] = mixX;
frames[frame + Y] = mixY;
}
public function apply(skeleton:Skeleton, lastTime:Float, time:Float, events:Array<Event>, alpha:Float, blend:MixBlend, direction:MixDirection,
appliedPose:Bool) {
var constraint = cast(skeleton.constraints[constraintIndex], PathConstraint);
if (!constraint.active)
return;
var pose = appliedPose ? constraint.applied : constraint.pose;
if (time < frames[0]) {
var setup = constraint.data.setup;
switch (blend) {
case MixBlend.setup:
pose.mixRotate = setup.mixRotate;
pose.mixX = setup.mixX;
pose.mixY = setup.mixY;
case MixBlend.first:
pose.mixRotate += (setup.mixRotate - pose.mixRotate) * alpha;
pose.mixX += (setup.mixX - pose.mixX) * alpha;
pose.mixY += (setup.mixY - pose.mixY) * alpha;
}
return;
}
var rotate:Float, x:Float, y:Float;
var i:Int = Timeline.search(frames, time, ENTRIES);
var curveType:Int = Std.int(curves[i >> 2]);
switch (curveType) {
case CurveTimeline.LINEAR:
var before:Float = frames[i];
rotate = frames[i + ROTATE];
x = frames[i + X];
y = frames[i + Y];
var t:Float = (time - before) / (frames[i + ENTRIES] - before);
rotate += (frames[i + ENTRIES + ROTATE] - rotate) * t;
x += (frames[i + ENTRIES + X] - x) * t;
y += (frames[i + ENTRIES + Y] - y) * t;
case CurveTimeline.STEPPED:
rotate = frames[i + ROTATE];
x = frames[i + X];
y = frames[i + Y];
default:
rotate = getBezierValue(time, i, ROTATE, curveType - CurveTimeline.BEZIER);
x = getBezierValue(time, i, X, curveType + CurveTimeline.BEZIER_SIZE - CurveTimeline.BEZIER);
y = getBezierValue(time, i, Y, curveType + CurveTimeline.BEZIER_SIZE * 2 - CurveTimeline.BEZIER);
}
switch (blend) {
case MixBlend.setup:
var setup = constraint.data.setup;
pose.mixRotate = setup.mixRotate + (rotate - setup.mixRotate) * alpha;
pose.mixX = setup.mixX + (x - setup.mixX) * alpha;
pose.mixY = setup.mixY + (y - setup.mixY) * alpha;
case MixBlend.first, MixBlend.replace:
pose.mixRotate += (rotate - pose.mixRotate) * alpha;
pose.mixX += (x - pose.mixX) * alpha;
pose.mixY += (y - pose.mixY) * alpha;
case MixBlend.add:
pose.mixRotate += rotate * alpha;
pose.mixX += x * alpha;
pose.mixY += y * alpha;
}
}
}