|
| 1 | +/* |
| 2 | + * This file is licensed under the MIT License, part of Roughly Enough Items. |
| 3 | + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package me.shedaniel.rei.impl.client.gui.widget.search; |
| 25 | + |
| 26 | +import me.shedaniel.clothconfig2.api.animator.NumberAnimator; |
| 27 | +import me.shedaniel.clothconfig2.api.animator.ValueAnimator; |
| 28 | +import me.shedaniel.math.Rectangle; |
| 29 | +import me.shedaniel.rei.api.client.config.ConfigObject; |
| 30 | +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; |
| 31 | +import net.minecraft.client.Minecraft; |
| 32 | +import net.minecraft.client.gui.GuiGraphics; |
| 33 | +import net.minecraft.network.chat.Component; |
| 34 | +import net.minecraft.util.FormattedCharSequence; |
| 35 | + |
| 36 | +import java.math.RoundingMode; |
| 37 | +import java.text.DecimalFormat; |
| 38 | +import java.text.DecimalFormatSymbols; |
| 39 | +import java.util.Locale; |
| 40 | +import java.util.function.Consumer; |
| 41 | + |
| 42 | +public class CalculatorDisplay implements Consumer<String> { |
| 43 | + private static final int MAX_LEN = 7; |
| 44 | + private static final DecimalFormat DEC; |
| 45 | + private static final DecimalFormat SCI; |
| 46 | + static { |
| 47 | + DecimalFormatSymbols sym = DecimalFormatSymbols.getInstance(Locale.ROOT); |
| 48 | + DEC = new DecimalFormat("0.##########", sym); |
| 49 | + DEC.setRoundingMode(RoundingMode.HALF_UP); |
| 50 | + DEC.setGroupingUsed(false); |
| 51 | + SCI = new DecimalFormat("0.##########E0", sym); |
| 52 | + SCI.setRoundingMode(RoundingMode.HALF_UP); |
| 53 | + SCI.setGroupingUsed(false); |
| 54 | + } |
| 55 | + |
| 56 | + private final OverlaySearchField searchField; |
| 57 | + private final NumberAnimator<Integer> width = ValueAnimator.ofDouble().asInt(); |
| 58 | + private Component fullText = Component.empty(); |
| 59 | + private FormattedCharSequence text = FormattedCharSequence.EMPTY; |
| 60 | + |
| 61 | + public CalculatorDisplay(OverlaySearchField searchField) { |
| 62 | + this.searchField = searchField; |
| 63 | + } |
| 64 | + |
| 65 | + public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { |
| 66 | + int prevWidth = width.value(); |
| 67 | + this.width.update(delta); |
| 68 | + if (prevWidth != width.value()) { |
| 69 | + // Keep the search field bounded |
| 70 | + this.searchField.addText(""); |
| 71 | + } |
| 72 | + |
| 73 | + if (width.value() <= 0) return; |
| 74 | + Rectangle searchFieldBounds = searchField.getBounds(); |
| 75 | + boolean contains = searchFieldBounds.contains(mouseX, mouseY) || searchField.isFocused(); |
| 76 | + graphics.fill(searchFieldBounds.getMaxX() - width.value(), searchFieldBounds.y + 1, searchFieldBounds.getMaxX() - 1, searchFieldBounds.getMaxY() - 1, contains ? 0xBBFFFFFF : 0x80FFFFFF); |
| 77 | + graphics.enableScissor(searchFieldBounds.getMaxX() - width.value() + 3, searchFieldBounds.y, searchFieldBounds.getMaxX() - 3, searchFieldBounds.getMaxY()); |
| 78 | + graphics.drawString(Minecraft.getInstance().font, text, searchFieldBounds.getMaxX() - width.value() + 3, searchFieldBounds.getCenterY() - 4, 0xFF000000, false); |
| 79 | + graphics.disableScissor(); |
| 80 | + |
| 81 | + if (!this.fullText.getString().isEmpty() && contains && new Rectangle(searchFieldBounds.getMaxX() - width.value() + 3, searchFieldBounds.y, width.value() - 6, searchFieldBounds.getHeight()).contains(mouseX, mouseY)) { |
| 82 | + Tooltip.create(this.fullText).queue(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void accept(String text) { |
| 88 | + if (!text.startsWith("=")) { |
| 89 | + this.width.setTo(0, ConfigObject.getInstance().isReducedMotion() ? 0 : 400); |
| 90 | + this.text = Component.literal("NaN").getVisualOrderText(); |
| 91 | + this.fullText = Component.empty(); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (TextCalculator.isValid(text)) { |
| 96 | + double eval = new TextCalculator(text).eval(); |
| 97 | + this.text = Component.literal(fmt(eval)).getVisualOrderText(); |
| 98 | + this.fullText = Component.literal(fmtAccurate(eval)); |
| 99 | + } |
| 100 | + |
| 101 | + this.width.setTo(Minecraft.getInstance().font.width(this.text) + 6, ConfigObject.getInstance().isReducedMotion() ? 0 : 400); |
| 102 | + } |
| 103 | + |
| 104 | + public int width() { |
| 105 | + return width.value(); |
| 106 | + } |
| 107 | + |
| 108 | + public static String fmt(double x) { |
| 109 | + if (Double.isNaN(x) || Double.isInfinite(x)) |
| 110 | + return String.valueOf(x); |
| 111 | + |
| 112 | + boolean neg = x < 0; |
| 113 | + double a = Math.abs(x); |
| 114 | + |
| 115 | + // 1) SMALL <1: decimals to fill WIDTH, else sci |
| 116 | + if (a > 0 && a < 1) { |
| 117 | + int used = neg ? 1 : 0; |
| 118 | + int avail = MAX_LEN - used; // total chars left |
| 119 | + // "0" + "." → 2 chars, rest decimals |
| 120 | + int dec = avail - 2; |
| 121 | + if (dec > 0) { |
| 122 | + double minShow = Math.pow(10, -dec); |
| 123 | + if (a >= minShow) { |
| 124 | + String fmt = "%." + dec + "f"; |
| 125 | + String s = String.format(Locale.ROOT, fmt, a) |
| 126 | + .replaceFirst("0+$", "") // drop trailing zeros |
| 127 | + .replaceFirst("\\.$", ""); // drop trailing dot |
| 128 | + // if we got something like ".123", prepend "0" |
| 129 | + if (s.startsWith(".")) s = "0" + s; |
| 130 | + return neg ? "-" + s : s; |
| 131 | + } |
| 132 | + } |
| 133 | + // too small → scientific |
| 134 | + return sciFmt(a, neg); |
| 135 | + } |
| 136 | + |
| 137 | + // 2) exact under threshold |
| 138 | + double thresh = neg ? 1_000_000 : 10_000_000; |
| 139 | + if (a < thresh) { |
| 140 | + String small = (a == Math.rint(a)) |
| 141 | + ? String.valueOf((long) a) |
| 142 | + : String.format(Locale.ROOT, "%.2f", a) |
| 143 | + .replaceFirst("\\.?0+$", ""); |
| 144 | + if (small.length() + (neg ? 1 : 0) <= MAX_LEN) |
| 145 | + return neg ? "-" + small : small; |
| 146 | + } |
| 147 | + |
| 148 | + // 3) suffix m/b/t |
| 149 | + char suf; |
| 150 | + double v; |
| 151 | + if (a >= 1e12 && a < 1e15) { |
| 152 | + suf = 't'; |
| 153 | + v = a / 1e12; |
| 154 | + } else if (a >= 1e9) { |
| 155 | + suf = 'b'; |
| 156 | + v = a / 1e9; |
| 157 | + } else if (a >= 1e6) { |
| 158 | + suf = 'm'; |
| 159 | + v = a / 1e6; |
| 160 | + } else { |
| 161 | + // small ≥1 but <1e6 (or neg ≥1e6) |
| 162 | + return sciFmt(a, neg); |
| 163 | + } |
| 164 | + { |
| 165 | + int used = (neg ? 1 : 0) + 1; // sign + suffix |
| 166 | + int avail = MAX_LEN - used; |
| 167 | + String intP = String.valueOf((long) v); |
| 168 | + int ip = intP.length(); |
| 169 | + int dec = Math.max(0, avail - ip - 1); // -1 for dot |
| 170 | + for (; dec >= 0; dec--) { |
| 171 | + String fmt = dec > 0 ? "%." + dec + "f" : "%.0f"; |
| 172 | + String man = String.format(Locale.ROOT, fmt, v); |
| 173 | + if (man.length() <= avail) |
| 174 | + return (neg ? "-" : "") + man + suf; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // 4) fallback sci |
| 179 | + return sciFmt(a, neg); |
| 180 | + } |
| 181 | + |
| 182 | + private static String sciFmt(double a, boolean neg) { |
| 183 | + int exp = (int) Math.floor(Math.log10(a)); |
| 184 | + double man = a / Math.pow(10, exp); |
| 185 | + String expS = String.valueOf(exp); |
| 186 | + int used = (neg ? 1 : 0) + 1 + expS.length(); // sign + 'e'+exp |
| 187 | + int avail = MAX_LEN - used; |
| 188 | + String intP = String.valueOf((long) man); |
| 189 | + int ip = intP.length(); |
| 190 | + int dec = Math.max(0, avail - ip - 1); |
| 191 | + for (; dec >= 0; dec--) { |
| 192 | + String fmt = dec > 0 ? "%." + dec + "f" : "%.0f"; |
| 193 | + String mS = String.format(Locale.ROOT, fmt, man); |
| 194 | + if (mS.length() <= avail) |
| 195 | + return (neg ? "-" : "") + mS + "e" + expS; |
| 196 | + } |
| 197 | + // worst‐case: truncate integer mantissa |
| 198 | + String mS = intP; |
| 199 | + if (mS.length() > avail) mS = mS.substring(0, avail); |
| 200 | + return (neg ? "-" : "") + mS + "e" + expS; |
| 201 | + } |
| 202 | + |
| 203 | + public static String fmtAccurate(double x) { |
| 204 | + if (Double.isNaN(x) || Double.isInfinite(x)) |
| 205 | + return String.valueOf(x); |
| 206 | + |
| 207 | + double a = Math.abs(x); |
| 208 | + if (a != 0 && (a < 1e-30 || a >= 1e30)) { |
| 209 | + // scientific |
| 210 | + return SCI.format(x).replace("E", "e"); |
| 211 | + } else { |
| 212 | + return DEC.format(x); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments