diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-11-11 01:01:30 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-11-11 01:09:42 +0800 |
| commit | 57f59e7da8ae83f1ad952e410601409eecf2e1c4 (patch) | |
| tree | 13770a0c6b3a24b71ab8a575259a3f19a4ef7977 | |
| parent | aba4c079befd4bb32f732b65c746a22559644d35 (diff) | |
| download | RoughlyEnoughItems-57f59e7da8ae83f1ad952e410601409eecf2e1c4.tar.gz RoughlyEnoughItems-57f59e7da8ae83f1ad952e410601409eecf2e1c4.tar.bz2 RoughlyEnoughItems-57f59e7da8ae83f1ad952e410601409eecf2e1c4.zip | |
Add Dispose Here region, introduce ValueAnimator
33 files changed, 2755 insertions, 106 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java index e9ce917dd..2ccba2166 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java @@ -29,7 +29,8 @@ import me.shedaniel.clothconfig2.api.ScissorsHandler; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.gui.AbstractRenderer; import me.shedaniel.rei.api.client.gui.Renderer; -import me.shedaniel.rei.api.common.util.Animator; +import me.shedaniel.rei.api.client.gui.animator.NumberAnimator; +import me.shedaniel.rei.api.client.gui.animator.ValueAnimator; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.Util; @@ -40,7 +41,7 @@ import java.util.function.IntSupplier; @Environment(EnvType.CLIENT) public class CompoundFavoriteRenderer extends AbstractRenderer { - protected Animator offset = new Animator(0); + protected NumberAnimator<Double> offset = ValueAnimator.ofDouble(); protected Rectangle scissorArea = new Rectangle(); protected long nextSwitch = -1; protected IntFunction<Renderer> renderers; @@ -114,7 +115,7 @@ public class CompoundFavoriteRenderer extends AbstractRenderer { } if (Util.getMillis() - nextSwitch > 1000) { nextSwitch = Util.getMillis(); - offset.setTo(((int) offset.target() + 1) % count, 500); + offset.setTo((offset.target().intValue() + 1) % count, 500); } } else { offset.setTo(supplier.getAsInt() % count, 500); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java new file mode 100644 index 000000000..f213702b4 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java @@ -0,0 +1,53 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +final class ConstantValueProvider<T> implements ValueProvider<T> { + private final T value; + + public ConstantValueProvider(T value) { + this.value = value; + } + + @Override + public T value() { + return value; + } + + @Override + public T target() { + return value; + } + + @Override + public void completeImmediately() { + } + + @Override + public void update(double delta) { + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java new file mode 100644 index 000000000..41f4e8263 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java @@ -0,0 +1,67 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.Objects; +import java.util.function.Supplier; + +@ApiStatus.Internal +final class ConventionValueAnimator<T> implements ValueAnimator<T> { + private final ValueAnimator<T> parent; + private final Supplier<T> convention; + private final long duration; + + ConventionValueAnimator(ValueAnimator<T> parent, Supplier<T> convention, long duration) { + this.parent = parent; + this.convention = convention; + this.duration = duration; + setAs(convention.get()); + } + + @Override + public ValueAnimator<T> setTo(T value, long duration) { + return parent.setTo(value, duration); + } + + @Override + public T target() { + return convention.get(); + } + + @Override + public T value() { + return parent.value(); + } + + @Override + public void update(double delta) { + parent.update(delta); + T target = target(); + if (!Objects.equals(parent.target(), target)) { + setTo(target, duration); + } + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java new file mode 100644 index 000000000..d3db45a40 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java @@ -0,0 +1,109 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import me.shedaniel.clothconfig2.impl.EasingMethod; +import net.minecraft.Util; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +final class DoubleValueAnimatorImpl extends NumberAnimator<Double> { + private double amount; + private double target; + private long start; + private long duration; + + DoubleValueAnimatorImpl() { + } + + DoubleValueAnimatorImpl(double amount) { + setAs(amount); + } + + @Override + public NumberAnimator<Double> setToNumber(Number value, long duration) { + double doubleValue = value.doubleValue(); + if (target != doubleValue) { + this.set(doubleValue, duration); + } + + return this; + } + + private void set(double value, long duration) { + this.target = value; + this.start = Util.getMillis(); + + if (duration > 0) { + this.duration = duration; + } else { + this.duration = 0; + this.amount = this.target; + } + } + + @Override + public void update(double delta) { + if (duration != 0) { + if (amount < target) { + this.amount = Math.min(ease(amount, target + (target - amount), Math.min(((double) Util.getMillis() - start) / duration * delta * 3.0D, 1.0D), EasingMethod.EasingMethodImpl.LINEAR), target); + } else if (amount > target) { + this.amount = Math.max(ease(amount, target - (amount - target), Math.min(((double) Util.getMillis() - start) / duration * delta * 3.0D, 1.0D), EasingMethod.EasingMethodImpl.LINEAR), target); + } + } + } + + private static double ease(double start, double end, double amount, EasingMethod easingMethod) { + return start + (end - start) * easingMethod.apply(amount); + } + + @Override + public int intValue() { + return (int) amount; + } + + @Override + public long longValue() { + return (long) amount; + } + + @Override + public float floatValue() { + return (float) amount; + } + + @Override + public double doubleValue() { + return amount; + } + + public Double target() { + return target; + } + + @Override + public Double value() { + return amount; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java new file mode 100644 index 000000000..6de268b2a --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java @@ -0,0 +1,68 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class MappingProgressValueAnimator<R> implements ProgressValueAnimator<R> { + private final ValueAnimator<Double> parent; + private final Function<Double, R> converter; + private final Function<R, Double> backwardsConverter; + + MappingProgressValueAnimator(ValueAnimator<Double> parent, Function<Double, R> converter, Function<R, Double> backwardsConverter) { + this.parent = parent; + this.converter = converter; + this.backwardsConverter = backwardsConverter; + } + + @Override + public ProgressValueAnimator<R> setTo(R value, long duration) { + parent.setTo(backwardsConverter.apply(value), duration); + return this; + } + + @Override + public R target() { + return converter.apply(parent.target()); + } + + @Override + public R value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.update(delta); + } + + + @Override + public double progress() { + return parent.value() / 100; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java new file mode 100644 index 000000000..947807972 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java @@ -0,0 +1,62 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class MappingValueAnimator<T, R> implements ValueAnimator<R> { + private final ValueAnimator<T> parent; + private final Function<T, R> converter; + private final Function<R, T> backwardsConverter; + + MappingValueAnimator(ValueAnimator<T> parent, Function<T, R> converter, Function<R, T> backwardsConverter) { + this.parent = parent; + this.converter = converter; + this.backwardsConverter = backwardsConverter; + } + + @Override + public ValueAnimator<R> setTo(R value, long duration) { + parent.setTo(backwardsConverter.apply(value), duration); + return this; + } + + @Override + public R target() { + return converter.apply(parent.target()); + } + + @Override + public R value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.update(delta); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java new file mode 100644 index 000000000..bf12081ed --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java @@ -0,0 +1,116 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Supplier; + +@ApiStatus.Experimental +public abstract class NumberAnimator<T extends Number> extends Number implements ValueAnimator<T> { + public NumberAnimator<Double> asDouble() { + return new NumberAnimatorWrapped<>(this, Number::doubleValue); + } + + public NumberAnimator<Float> asFloat() { + return new NumberAnimatorWrapped<>(this, Number::floatValue); + } + + public NumberAnimator<Integer> asInt() { + return new NumberAnimatorWrapped<>(this, d -> (int) Math.round(d.doubleValue())); + } + + public NumberAnimator<Long> asLong() { + return new NumberAnimatorWrapped<>(this, d -> Math.round(d.doubleValue())); + } + + @Override + public NumberAnimator<T> setAs(T value) { + ValueAnimator.super.setAs(value); + return this; + } + + public NumberAnimator<T> setAs(int value) { + setAsNumber(value); + return this; + } + + public NumberAnimator<T> setAs(long value) { + setAsNumber(value); + return this; + } + + public NumberAnimator<T> setAs(float value) { + setAsNumber(value); + return this; + } + + public NumberAnimator<T> setAs(double value) { + setAsNumber(value); + return this; + } + + @Override + public NumberAnimator<T> setTo(T value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator<T> setTo(int value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator<T> setTo(long value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator<T> setTo(float value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator<T> setTo(double value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator<T> setAsNumber(Number value) { + return setToNumber(value, -1); + } + + public abstract NumberAnimator<T> setToNumber(Number value, long duration); + + @Override + public NumberAnimator<T> withConvention(Supplier<T> convention, long duration) { + ValueAnimator<T> parentConvention = ValueAnimator.super.withConvention(convention, duration); + return new ValueAnimatorAsNumberAnimator<T>(parentConvention) { + @Override + public NumberAnimator<T> setToNumber(Number value, long duration) { + return NumberAnimator.this.setToNumber(value, duration); + } + }; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java new file mode 100644 index 000000000..0214fb637 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java @@ -0,0 +1,80 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class NumberAnimatorWrapped<T extends Number, R extends Number> extends NumberAnimator<T> { + private final NumberAnimator<R> parent; + private final Function<R, T> converter; + + NumberAnimatorWrapped(NumberAnimator<R> parent, Function<R, T> converter) { + this.parent = parent; + this.converter = converter; + } + + @Override + public NumberAnimator<T> setToNumber(Number value, long duration) { + this.parent.setToNumber(value, duration); + return this; + } + + @Override + public T target() { + return converter.apply(parent.target()); + } + + @Override + public T value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.update(delta); + } + + @Override + public int intValue() { + return parent.intValue(); + } + + @Override + public long longValue() { + return parent.longValue(); + } + + @Override + public float floatValue() { + return parent.floatValue(); + } + + @Override + public double doubleValue() { + return parent.doubleValue(); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ProgressValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ProgressValueAnimator.java new file mode 100644 index 000000000..e7f596092 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ProgressValueAnimator.java @@ -0,0 +1,46 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. |
