|
| 1 | +/* |
| 2 | + * This file is part of Cubic World Generation, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) 2015-2020 contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui; |
| 25 | + |
| 26 | +import com.google.common.base.Converter; |
| 27 | +import io.github.opencubicchunks.cubicchunks.cubicgen.CustomCubicMod; |
| 28 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiButton; |
| 29 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiCheckBox; |
| 30 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.CwgGuiSlider; |
| 31 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component.WrappedVanillaButton; |
| 32 | +import io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.converter.Converters; |
| 33 | +import net.malisis.core.client.gui.MalisisGui; |
| 34 | +import net.minecraft.client.gui.GuiButton; |
| 35 | + |
| 36 | +import java.util.function.BiPredicate; |
| 37 | +import java.util.function.Consumer; |
| 38 | +import java.util.function.DoubleSupplier; |
| 39 | +import java.util.function.Function; |
| 40 | + |
| 41 | +import javax.annotation.Nonnull; |
| 42 | + |
| 43 | +public class CwgGuiFactory { |
| 44 | + |
| 45 | + private CwgGuiFactory() { |
| 46 | + throw new Error(); |
| 47 | + } |
| 48 | + |
| 49 | + public static <T extends GuiButton> WrappedVanillaButton<T> wrap(MalisisGui gui, T vanillaComponent) { |
| 50 | + return new WrappedVanillaButton<>(gui, vanillaComponent); |
| 51 | + } |
| 52 | + |
| 53 | + public static CwgGuiButton makeButton(String formatString) { |
| 54 | + return new CwgGuiButton(str(formatString), null); |
| 55 | + } |
| 56 | + |
| 57 | + public static CwgGuiButton makeButton(String formatString, Consumer<CwgGuiButton> onClick) { |
| 58 | + return new CwgGuiButton(str(formatString), onClick); |
| 59 | + } |
| 60 | + |
| 61 | + public static CwgGuiCheckBox makeCheckBox(String formatString, boolean defaultValue) { |
| 62 | + return new CwgGuiCheckBox(str(formatString), defaultValue); |
| 63 | + } |
| 64 | + |
| 65 | + public static CwgGuiCheckBox makeCheckBoxUnlocalized(String text, boolean defaultValue) { |
| 66 | + return new CwgGuiCheckBox(text, defaultValue); |
| 67 | + } |
| 68 | + |
| 69 | + public static CwgGuiSlider makeSlider(double min, double max, double defVal, String formatString, Function<Double, String> toString) { |
| 70 | + |
| 71 | + CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1]; |
| 72 | + BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider); |
| 73 | + |
| 74 | + double defMult = defVal == 0 ? 1 : defVal; |
| 75 | + |
| 76 | + Converter<Double, Double> conv = Converters.builder() |
| 77 | + .linearScale(min, max).rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128) |
| 78 | + .withRoundingRadiusPredicate(isInRoundRadius) |
| 79 | + .build(); |
| 80 | + |
| 81 | + CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defVal); |
| 82 | + wrappedSlider[0] = slider; |
| 83 | + return slider; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public static CwgGuiSlider makeSlider(double min, double max, double defVal, String formatString) { |
| 88 | + return makeSlider(min, max, defVal, formatString, x -> String.format("%.3f", x)); |
| 89 | + } |
| 90 | + |
| 91 | + public static CwgGuiSlider makeIntSlider(int min, int max, int defVal, String formatString) { |
| 92 | + return makeIntSlider(min, max, defVal, formatString, String::valueOf); |
| 93 | + } |
| 94 | + |
| 95 | + public static CwgGuiSlider makeIntSlider(int min, int max, int defVal, String formatString, Function<Integer, String> toString) { |
| 96 | + |
| 97 | + CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1]; |
| 98 | + BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider); |
| 99 | + |
| 100 | + double defMult = defVal == 0 ? 1 : defVal; |
| 101 | + |
| 102 | + Converter<Double, Double> conv = Converters.builder() |
| 103 | + .linearScale(min, max).rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128) |
| 104 | + .withRoundingRadiusPredicate(isInRoundRadius) |
| 105 | + .build(); |
| 106 | + |
| 107 | + CwgGuiSlider slider = new CwgGuiSlider(str(formatString), x -> toString.apply((int) Math.round(x)), conv, defVal); |
| 108 | + wrappedSlider[0] = slider; |
| 109 | + return slider; |
| 110 | + } |
| 111 | + |
| 112 | + public static CwgGuiSlider makePositiveExponentialSlider(double minPos, double maxPos, double defaultVal, |
| 113 | + String formatString, Function<Double, String> toString) { |
| 114 | + |
| 115 | + CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1]; |
| 116 | + BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider); |
| 117 | + |
| 118 | + double defMult = defaultVal == 0 ? 1 : defaultVal; |
| 119 | + |
| 120 | + Converter<Double, Double> conv = Converters.builder() |
| 121 | + .exponential().withBaseValue(2).withPositiveExponentRange(minPos, maxPos) |
| 122 | + .rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128) |
| 123 | + .withRoundingRadiusPredicate(isInRoundRadius) |
| 124 | + .withInfinity().positiveAt(Math.pow(2, maxPos)).negativeAt(Double.NaN) |
| 125 | + .build(); |
| 126 | + |
| 127 | + CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal); |
| 128 | + wrappedSlider[0] = slider; |
| 129 | + return slider; |
| 130 | + } |
| 131 | + |
| 132 | + public static CwgGuiSlider makePositiveExponentialSlider(double minPos, double maxPos, double defaultVal, String formatString) { |
| 133 | + return makePositiveExponentialSlider(minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x)); |
| 134 | + } |
| 135 | + |
| 136 | + public static CwgGuiSlider makeExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, |
| 137 | + String formatString, Function<Double, String> toString) { |
| 138 | + |
| 139 | + CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1]; |
| 140 | + BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider); |
| 141 | + |
| 142 | + double defMult = defaultVal == 0 ? 1 : defaultVal; |
| 143 | + |
| 144 | + Converter<Double, Double> conv = Converters.builder() |
| 145 | + .exponential().withZero().withBaseValue(2).withNegativeExponentRange(minNeg, maxNeg).withPositiveExponentRange(minPos, maxPos) |
| 146 | + .rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128) |
| 147 | + .withRoundingRadiusPredicate(isInRoundRadius) |
| 148 | + .build(); |
| 149 | + |
| 150 | + CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal); |
| 151 | + wrappedSlider[0] = slider; |
| 152 | + return slider; |
| 153 | + } |
| 154 | + |
| 155 | + public static CwgGuiSlider makeExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, String formatString) { |
| 156 | + return makeExponentialSlider(minNeg, maxNeg, minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x)); |
| 157 | + } |
| 158 | + |
| 159 | + public static CwgGuiSlider makeSymmetricExponentialSlider(double min, double max, double defaultVal, String formatString, Function<Double, String> toString) { |
| 160 | + return makeExponentialSlider(min, max, min, max, defaultVal, formatString, toString); |
| 161 | + } |
| 162 | + |
| 163 | + public static CwgGuiSlider makeSymmetricExponentialSlider(double min, double max, double defaultVal, String formatString) { |
| 164 | + return makeExponentialSlider(min, max, min, max, defaultVal, formatString); |
| 165 | + } |
| 166 | + |
| 167 | + public static CwgGuiSlider makeInvertedExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, |
| 168 | + String formatString, Function<Double, String> toString) { |
| 169 | + |
| 170 | + CwgGuiSlider[] wrappedSlider = new CwgGuiSlider[1]; |
| 171 | + BiPredicate<Double, Double> isInRoundRadius = getIsInRoundRadiusPredicate(wrappedSlider); |
| 172 | + |
| 173 | + double defMult = defaultVal == 0 ? 1 : defaultVal; |
| 174 | + |
| 175 | + Converter<Double, Double> conv = Converters.builder() |
| 176 | + .reverse() |
| 177 | + .pow(2) |
| 178 | + .exponential().withZero().withBaseValue(2).withNegativeExponentRange(minNeg, maxNeg).withPositiveExponentRange(minPos, maxPos) |
| 179 | + .inverse() |
| 180 | + .rounding().withBase(2, 1).withBase(10, 1).withBase(2, defMult).withBase(10, defMult).withMaxExp(128) |
| 181 | + .withRoundingRadiusPredicate(isInRoundRadius) |
| 182 | + .build(); |
| 183 | + |
| 184 | + CwgGuiSlider slider = new CwgGuiSlider(str(formatString), toString, conv, defaultVal); |
| 185 | + wrappedSlider[0] = slider; |
| 186 | + return slider; |
| 187 | + } |
| 188 | + |
| 189 | + public static CwgGuiSlider makeInvertedExponentialSlider(double minNeg, double maxNeg, double minPos, double maxPos, double defaultVal, String formatString) { |
| 190 | + return makeInvertedExponentialSlider(minNeg, maxNeg, minPos, maxPos, defaultVal, formatString, x -> String.format("%.3f", x)); |
| 191 | + } |
| 192 | + |
| 193 | + public static CwgGuiSlider makeInvertedPositiveExponentialSlider(double min, double max, double defaultVal, String formatString) { |
| 194 | + return makeInvertedExponentialSlider(Double.NaN, Double.NaN, min, max, defaultVal, formatString); |
| 195 | + } |
| 196 | + |
| 197 | + public static CwgGuiSlider makeInvertedPositiveExponentialSlider(double min, double max, double defaultVal, |
| 198 | + String formatString, Function<Double, String> toString) { |
| 199 | + return makeInvertedExponentialSlider(Double.NaN, Double.NaN, min, max, defaultVal, formatString, toString); |
| 200 | + } |
| 201 | + |
| 202 | + // internal utils |
| 203 | + |
| 204 | + @Nonnull private static BiPredicate<Double, Double> getIsInRoundRadiusPredicate(CwgGuiSlider[] slider) { |
| 205 | + return getIsInRoundRadiusPredicate(() -> slider[0] == null ? 1000 : slider[0].width); |
| 206 | + } |
| 207 | + |
| 208 | + @Nonnull private static BiPredicate<Double, Double> getIsInRoundRadiusPredicate(DoubleSupplier width) { |
| 209 | + return (previousSlide, foundSlide) -> { |
| 210 | + double w = width.getAsDouble(); |
| 211 | + double rangeCenter = Math.round(previousSlide * w) / w; |
| 212 | + double minRange = rangeCenter - 0.5 / w; |
| 213 | + double maxRange = rangeCenter + 0.5 / w; |
| 214 | + |
| 215 | + return foundSlide >= minRange && foundSlide <= maxRange; |
| 216 | + }; |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + private static String str(String name) { |
| 221 | + String unloc = CustomCubicMod.MODID + ".gui.cubicgen." + name; |
| 222 | + return unloc; |
| 223 | + } |
| 224 | +} |
0 commit comments