|
| 1 | +package appeng.core.localization; |
| 2 | + |
| 3 | +import appeng.api.config.PowerUnits; |
| 4 | +import com.github.bsideup.jabel.Desugar; |
| 5 | +import net.minecraft.util.text.*; |
| 6 | + |
| 7 | +import java.text.DecimalFormat; |
| 8 | +import java.text.MessageFormat; |
| 9 | +import java.util.Arrays; |
| 10 | + |
| 11 | +/** |
| 12 | + * Static utilities for constructing tooltips in various places. |
| 13 | + */ |
| 14 | +public final class Tooltips { |
| 15 | + |
| 16 | + private static final char SEP; |
| 17 | + static { |
| 18 | + var format = (DecimalFormat) DecimalFormat.getInstance(); |
| 19 | + var symbols = format.getDecimalFormatSymbols(); |
| 20 | + SEP = symbols.getDecimalSeparator(); |
| 21 | + } |
| 22 | + |
| 23 | + public static final Style UNIT_TEXT = new Style().setColor(TextFormatting.YELLOW).setItalic(false); |
| 24 | + public static final Style NORMAL_TOOLTIP_TEXT = new Style().setColor(TextFormatting.GRAY).setItalic(false); |
| 25 | + public static final Style NUMBER_TEXT = new Style().setColor(TextFormatting.LIGHT_PURPLE).setItalic(false); |
| 26 | + |
| 27 | + |
| 28 | + public static final String[] units = new String[] { "k", "M", "G", "T", "P", "E" }; |
| 29 | + public static final long[] DECIMAL_NUMS = new long[] { 1000L, 1000_000L, 1000_000_000L, 1000_000_000_000L, |
| 30 | + 1000_000_000_000_000L, |
| 31 | + 1000_000_000_000_000_000L }; |
| 32 | + |
| 33 | + public static ITextComponent of(ITextComponent... components) { |
| 34 | + ITextComponent s = new TextComponentString(""); |
| 35 | + for (var c : components) { |
| 36 | + s = s.appendSibling(c); |
| 37 | + } |
| 38 | + return s; |
| 39 | + } |
| 40 | + |
| 41 | + public static ITextComponent of(String s) { |
| 42 | + return new TextComponentString(s); |
| 43 | + } |
| 44 | + |
| 45 | + public static ITextComponent ofPercent(double percent) { |
| 46 | + return ofPercent(percent,true); |
| 47 | + } |
| 48 | + |
| 49 | + public static ITextComponent ofPercent(double percent, boolean oneIsGreen) { |
| 50 | + return new TextComponentString(MessageFormat.format("{0,number,#.##%}", percent)) |
| 51 | + .setStyle(colorFromRatio(percent, oneIsGreen)); |
| 52 | + } |
| 53 | + |
| 54 | + public static ITextComponent of(PowerUnits pU) { |
| 55 | + return new TextComponentTranslation(pU.unlocalizedName).setStyle(UNIT_TEXT); |
| 56 | + } |
| 57 | + |
| 58 | + public static ITextComponent ofNumber(double number, double max) { |
| 59 | + MaxedAmount amount = getMaxedAmount(number, max); |
| 60 | + return ofNumber(amount); |
| 61 | + } |
| 62 | + |
| 63 | + public static ITextComponent of(GuiText guiText, Object... args) { |
| 64 | + return of(guiText, NORMAL_TOOLTIP_TEXT, args); |
| 65 | + } |
| 66 | + |
| 67 | + public static ITextComponent of(GuiText guiText, Style style, Object... args) { |
| 68 | + |
| 69 | + if (args.length > 0 && args[0] instanceof Integer) { |
| 70 | + return guiText.getLocalizedWithArgs(Arrays.stream(args).map((o) -> ofUnformattedNumber((Integer) o)).toArray()).createCopy() |
| 71 | + .setStyle(style); |
| 72 | + } else if (args.length > 0 && args[0] instanceof Long) { |
| 73 | + return guiText.getLocalizedWithArgs(Arrays.stream(args).map((o) -> ofUnformattedNumber((Long) o)).toArray()).createCopy() |
| 74 | + .setStyle(style); |
| 75 | + } |
| 76 | + return guiText.getLocalizedWithArgs(args).createCopy().setStyle(style); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + public static ITextComponent ofUnformattedNumber(long number) { |
| 81 | + return new TextComponentString(String.valueOf(number)).setStyle(NUMBER_TEXT); |
| 82 | + } |
| 83 | + |
| 84 | + public static ITextComponent ofUnformattedNumberWithRatioColor(long number, double ratio, boolean oneIsGreen) { |
| 85 | + return new TextComponentString(String.valueOf(number)).setStyle(colorFromRatio(ratio, oneIsGreen)); |
| 86 | + } |
| 87 | + |
| 88 | + private static ITextComponent ofNumber(MaxedAmount number) { |
| 89 | + boolean numberUnit = !number.digit().equals("0"); |
| 90 | + return new TextComponentString(number.digit() + (numberUnit ? number.unit() : "")).setStyle(NUMBER_TEXT) |
| 91 | + .appendSibling(new TextComponentString("/") |
| 92 | + .setStyle(NORMAL_TOOLTIP_TEXT)) |
| 93 | + .appendText(number.maxDigit() + number.unit()).setStyle(NUMBER_TEXT); |
| 94 | + } |
| 95 | + @Desugar |
| 96 | + public record MaxedAmount(String digit, String maxDigit, String unit) { |
| 97 | + } |
| 98 | + |
| 99 | + @Desugar |
| 100 | + public record Amount(String digit, String unit) { |
| 101 | + } |
| 102 | + |
| 103 | + public static Style colorFromRatio(double ratio, boolean oneIsGreen) { |
| 104 | + double p = ratio; |
| 105 | + |
| 106 | + if (!oneIsGreen) { |
| 107 | + p = 1 - p; |
| 108 | + } |
| 109 | + |
| 110 | + TextFormatting colorCode = getColorCode(p); |
| 111 | + |
| 112 | + return new Style().setItalic(false).setColor(colorCode); |
| 113 | + } |
| 114 | + |
| 115 | + public static String getAmount(double amount, long num) { |
| 116 | + double fract = amount / num; |
| 117 | + String returned; |
| 118 | + if (fract < 10) { |
| 119 | + returned = String.format("%.3f", fract); |
| 120 | + } else if (fract < 100) { |
| 121 | + returned = String.format("%.2f", fract); |
| 122 | + } else { |
| 123 | + returned = String.format("%.1f", fract); |
| 124 | + } |
| 125 | + while (returned.endsWith("0")) { |
| 126 | + returned = returned.substring(0, returned.length() - 1); |
| 127 | + } |
| 128 | + if (returned.endsWith(String.valueOf(SEP))) { |
| 129 | + returned = returned.substring(0, returned.length() - 1); |
| 130 | + } |
| 131 | + return returned; |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + public static Amount getAmount(double amount) { |
| 136 | + if (amount < 10000) { |
| 137 | + return new Amount(getAmount(amount, 1), ""); |
| 138 | + } else { |
| 139 | + int i = 0; |
| 140 | + while (amount / DECIMAL_NUMS[i] >= 1000) { |
| 141 | + i++; |
| 142 | + } |
| 143 | + return new Amount(getAmount(amount, DECIMAL_NUMS[i]), units[i]); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public static MaxedAmount getMaxedAmount(double amount, double max) { |
| 148 | + if (max < 10000) { |
| 149 | + return new MaxedAmount(getAmount(amount, 1), getAmount(max, 1), ""); |
| 150 | + } else { |
| 151 | + int i = 0; |
| 152 | + while (max / DECIMAL_NUMS[i] >= 1000) { |
| 153 | + i++; |
| 154 | + } |
| 155 | + return new MaxedAmount(getAmount(amount, DECIMAL_NUMS[i]), getAmount(max, DECIMAL_NUMS[i]), units[i]); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static TextFormatting getColorCode(double p) { |
| 160 | + if (p < 0.33) { |
| 161 | + return TextFormatting.RED; |
| 162 | + } else if (p < 0.66) { |
| 163 | + return TextFormatting.YELLOW; |
| 164 | + } else { |
| 165 | + return TextFormatting.GREEN; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public static ITextComponent energyStorageComponent(double energy, double max) { |
| 170 | + return Tooltips.of( |
| 171 | + Tooltips.of(GuiText.StoredEnergy.getLocal()), |
| 172 | + Tooltips.of(": "), |
| 173 | + Tooltips.ofNumber(energy, max), |
| 174 | + Tooltips.of(" "), |
| 175 | + Tooltips.of(PowerUnits.AE), |
| 176 | + Tooltips.of(" ("), |
| 177 | + Tooltips.ofPercent(energy / max), |
| 178 | + Tooltips.of(")")); |
| 179 | + } |
| 180 | + |
| 181 | + public static ITextComponent bytesUsed(long bytes, long max) { |
| 182 | + return of( |
| 183 | + Tooltips.of( |
| 184 | + ofUnformattedNumberWithRatioColor(bytes, (double) bytes / max, false), |
| 185 | + of(" "), |
| 186 | + of(GuiText.Of), |
| 187 | + of(" "), |
| 188 | + ofUnformattedNumber(max), |
| 189 | + of(" "), |
| 190 | + of(GuiText.BytesUsed))); |
| 191 | + } |
| 192 | + |
| 193 | + public static ITextComponent typesUsed(long types, long max) { |
| 194 | + return Tooltips.of( |
| 195 | + ofUnformattedNumberWithRatioColor(types, (double) types / max, false), |
| 196 | + of(" "), |
| 197 | + of(GuiText.Of), |
| 198 | + of(" "), |
| 199 | + ofUnformattedNumber(max), |
| 200 | + of(" "), |
| 201 | + of(GuiText.Types)); |
| 202 | + } |
| 203 | +} |
0 commit comments