/*
 * Copyright (C) 2018 Florian Dreier
 *
 * This file is part of MyTargets.
 *
 * MyTargets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * MyTargets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package de.dreier.mytargets.shared.utils

import android.animation.TypeEvaluator
import android.graphics.Matrix

/**
 * This class is passed to ValueAnimator in order to animate matrix changes
 */
class MatrixEvaluator : TypeEvaluator<Matrix> {

    private val tempStartValues = FloatArray(9)
    private val tempEndValues = FloatArray(9)
    private val tempMatrix = Matrix()

    override fun evaluate(fraction: Float, startValue: Matrix, endValue: Matrix): Matrix {
        startValue.getValues(tempStartValues)
        endValue.getValues(tempEndValues)
        for (i in 0..8) {
            val diff = tempEndValues[i] - tempStartValues[i]
            tempEndValues[i] = tempStartValues[i] + fraction * diff
        }
        tempMatrix.setValues(tempEndValues)

        return tempMatrix
    }
}
