2022-04-14 18:36:57 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
2022-04-14 18:36:57 +01:00
|
|
|
import android.util.FloatProperty;
|
|
|
|
|
import android.util.Log;
|
2022-09-13 14:40:48 -07:00
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.util.Arrays;
|
2022-04-14 18:36:57 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allows to combine multiple values set by several sources.
|
|
|
|
|
*
|
|
|
|
|
* The various sources are meant to use [set], providing different `setterIndex` params. When it is
|
|
|
|
|
* not set, 0 is used. This is meant to cover the case multiple animations are going on at the same
|
|
|
|
|
* time.
|
|
|
|
|
*
|
|
|
|
|
* This class behaves similarly to [MultiValueAlpha], but is meant to be more abstract and reusable.
|
2022-09-01 21:28:14 +01:00
|
|
|
* It aggregate all values using the provided [aggregator].
|
2022-04-14 18:36:57 +01:00
|
|
|
*
|
|
|
|
|
* @param <T> Type where to apply the property.
|
|
|
|
|
*/
|
2022-09-01 21:28:14 +01:00
|
|
|
public class MultiPropertyFactory<T> {
|
2022-04-14 18:36:57 +01:00
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
public static final FloatProperty<MultiPropertyFactory<?>.MultiProperty> MULTI_PROPERTY_VALUE =
|
|
|
|
|
new FloatProperty<MultiPropertyFactory<?>.MultiProperty>("value") {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Float get(MultiPropertyFactory<?>.MultiProperty property) {
|
|
|
|
|
return property.mValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setValue(MultiPropertyFactory<?>.MultiProperty property, float value) {
|
|
|
|
|
property.setValue(value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-14 18:36:57 +01:00
|
|
|
private static final boolean DEBUG = false;
|
2022-09-01 21:28:14 +01:00
|
|
|
private static final String TAG = "MultiPropertyFactory";
|
2022-09-13 14:40:48 -07:00
|
|
|
private final MultiPropertyFactory<?>.MultiProperty[] mProperties;
|
2022-04-14 18:36:57 +01:00
|
|
|
|
|
|
|
|
// This is an optimization for cases when set is called repeatedly with the same setterIndex.
|
|
|
|
|
private float mAggregationOfOthers = 0f;
|
2022-09-13 14:40:48 -07:00
|
|
|
private int mLastIndexSet = -1;
|
|
|
|
|
|
|
|
|
|
protected final T mTarget;
|
|
|
|
|
private final FloatProperty<T> mProperty;
|
2022-09-01 21:28:14 +01:00
|
|
|
private final FloatBiFunction mAggregator;
|
2022-04-14 18:36:57 +01:00
|
|
|
|
2022-09-01 21:28:14 +01:00
|
|
|
/**
|
|
|
|
|
* Represents a function that accepts two float and produces a float.
|
|
|
|
|
*/
|
|
|
|
|
public interface FloatBiFunction {
|
|
|
|
|
/**
|
|
|
|
|
* Applies this function to the given arguments.
|
|
|
|
|
*/
|
|
|
|
|
float apply(float a, float b);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
public MultiPropertyFactory(T target, FloatProperty<T> property, int size,
|
2022-09-01 21:28:14 +01:00
|
|
|
FloatBiFunction aggregator) {
|
2022-09-13 14:40:48 -07:00
|
|
|
this(target, property, size, aggregator, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MultiPropertyFactory(T target, FloatProperty<T> property, int size,
|
|
|
|
|
FloatBiFunction aggregator, float defaultPropertyValue) {
|
|
|
|
|
mTarget = target;
|
2022-04-14 18:36:57 +01:00
|
|
|
mProperty = property;
|
2022-09-01 21:28:14 +01:00
|
|
|
mAggregator = aggregator;
|
2022-09-13 14:40:48 -07:00
|
|
|
|
|
|
|
|
mProperties = new MultiPropertyFactory<?>.MultiProperty[size];
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
mProperties[i] = new MultiProperty(i, defaultPropertyValue);
|
|
|
|
|
}
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns the [MultiFloatProperty] associated with [inx], creating it if not present. */
|
2022-09-13 14:40:48 -07:00
|
|
|
public MultiProperty get(int index) {
|
|
|
|
|
return (MultiProperty) mProperties[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return Arrays.deepToString(mProperties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dumps the alpha channel values to the given PrintWriter
|
|
|
|
|
*
|
|
|
|
|
* @param prefix String to be used before every line
|
|
|
|
|
* @param pw PrintWriter where the logs should be dumped
|
|
|
|
|
* @param label String used to help identify this object
|
|
|
|
|
* @param alphaIndexLabels Strings that represent each alpha channel, these should be entered
|
|
|
|
|
* in the order of the indexes they represent, starting from 0.
|
|
|
|
|
*/
|
|
|
|
|
public void dump(String prefix, PrintWriter pw, String label, String... alphaIndexLabels) {
|
|
|
|
|
pw.println(prefix + label);
|
|
|
|
|
|
|
|
|
|
String innerPrefix = prefix + '\t';
|
|
|
|
|
for (int i = 0; i < alphaIndexLabels.length; i++) {
|
|
|
|
|
if (i >= mProperties.length) {
|
|
|
|
|
pw.println(innerPrefix + alphaIndexLabels[i] + " given for alpha index " + i
|
|
|
|
|
+ " however there are only " + mProperties.length + " alpha channels.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pw.println(innerPrefix + alphaIndexLabels[i] + "=" + get(i).getValue());
|
|
|
|
|
}
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Each [setValue] will be aggregated with the other properties values created by the
|
|
|
|
|
* corresponding factory.
|
|
|
|
|
*/
|
2022-09-13 14:40:48 -07:00
|
|
|
public class MultiProperty {
|
|
|
|
|
|
2022-04-14 18:36:57 +01:00
|
|
|
private final int mInx;
|
2022-09-13 14:40:48 -07:00
|
|
|
private final float mDefaultValue;
|
|
|
|
|
private float mValue;
|
2022-04-14 18:36:57 +01:00
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
MultiProperty(int inx, float defaultValue) {
|
2022-04-14 18:36:57 +01:00
|
|
|
mInx = inx;
|
2022-09-13 14:40:48 -07:00
|
|
|
mDefaultValue = defaultValue;
|
|
|
|
|
mValue = defaultValue;
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
public void setValue(float newValue) {
|
2022-04-14 18:36:57 +01:00
|
|
|
if (mLastIndexSet != mInx) {
|
2022-09-13 14:40:48 -07:00
|
|
|
mAggregationOfOthers = mDefaultValue;
|
|
|
|
|
for (MultiPropertyFactory<?>.MultiProperty other : mProperties) {
|
|
|
|
|
if (other.mInx != mInx) {
|
2022-09-01 21:28:14 +01:00
|
|
|
mAggregationOfOthers =
|
2022-09-13 14:40:48 -07:00
|
|
|
mAggregator.apply(mAggregationOfOthers, other.mValue);
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
2022-09-13 14:40:48 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 18:36:57 +01:00
|
|
|
mLastIndexSet = mInx;
|
|
|
|
|
}
|
2022-09-01 21:28:14 +01:00
|
|
|
float lastAggregatedValue = mAggregator.apply(mAggregationOfOthers, newValue);
|
2022-04-14 18:36:57 +01:00
|
|
|
mValue = newValue;
|
2022-09-13 14:40:48 -07:00
|
|
|
apply(lastAggregatedValue);
|
2022-04-14 18:36:57 +01:00
|
|
|
|
|
|
|
|
if (DEBUG) {
|
2022-09-13 14:40:48 -07:00
|
|
|
Log.d(TAG, "name=" + mProperty.getName()
|
|
|
|
|
+ " target=" + mTarget.getClass()
|
|
|
|
|
+ " newValue=" + newValue
|
|
|
|
|
+ " mInx=" + mInx
|
|
|
|
|
+ " aggregated=" + lastAggregatedValue
|
|
|
|
|
+ " others= " + Arrays.deepToString(mProperties));
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
public float getValue() {
|
2022-09-16 14:53:11 +08:00
|
|
|
return mValue;
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return String.valueOf(mValue);
|
|
|
|
|
}
|
2022-09-13 14:40:48 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates and returns an Animator from the current value to the given value. Future
|
|
|
|
|
* animator on the same target automatically cancels the previous one.
|
|
|
|
|
*/
|
|
|
|
|
public Animator animateToValue(float value) {
|
|
|
|
|
ObjectAnimator animator = ObjectAnimator.ofFloat(this, MULTI_PROPERTY_VALUE, value);
|
|
|
|
|
animator.setAutoCancel(true);
|
|
|
|
|
return animator;
|
|
|
|
|
}
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-13 14:40:48 -07:00
|
|
|
protected void apply(float value) {
|
|
|
|
|
mProperty.set(mTarget, value);
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
}
|