| 1 | package com.jellypudding.offlineclient.util;
|
| 2 |
|
| 3 | import net.minecraft.client.gui.Font;
|
| 4 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 5 | import org.joml.Matrix3x2fStack;
|
| 6 |
|
| 7 | import java.util.List;
|
| 8 |
|
| 9 | // 2D drawing helpers on top of GuiGraphicsExtractor.
|
| 10 | public final class RenderUtil {
|
| 11 |
|
| 12 | // Grey for text that should sit back from the main line.
|
| 13 | public static final int MUTED_TEXT = 0xFFB0B0C0;
|
| 14 |
|
| 15 | private RenderUtil() {
|
| 16 | }
|
| 17 |
|
| 18 | public static void gradientText(GuiGraphicsExtractor context, Font font, String text,
|
| 19 | int x, int y, int from, int to) {
|
| 20 | gradientText(context, font, text, x, y, from, to, 1f);
|
| 21 | }
|
| 22 |
|
| 23 | // Alpha runs from 0 to 1.
|
| 24 | public static void gradientText(GuiGraphicsExtractor context, Font font, String text,
|
| 25 | int x, int y, int from, int to, float alpha) {
|
| 26 | gradientText(context, font, text, x, y, from, to, alpha, true);
|
| 27 | }
|
| 28 |
|
| 29 | // The drop shadow doubles up at a large scale. Big titles turn it off.
|
| 30 | public static void gradientText(GuiGraphicsExtractor context, Font font, String text,
|
| 31 | int x, int y, int from, int to, float alpha, boolean shadow) {
|
| 32 | if (alpha < 0.05f) {
|
| 33 | return;
|
| 34 | }
|
| 35 | context.guiRenderState.up();
|
| 36 | int length = text.length();
|
| 37 | for (int i = 0; i < length; i++) {
|
| 38 | String ch = String.valueOf(text.charAt(i));
|
| 39 | int color = ColorUtil.lerp(from, to, length <= 1 ? 0 : (float) i / (length - 1));
|
| 40 | context.text(font, ch, x, y, ColorUtil.fade(color, alpha), shadow);
|
| 41 | x += font.width(ch);
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public static void gradientTextScaled(GuiGraphicsExtractor context, Font font, String text,
|
| 46 | float x, float y, int from, int to, float scale) {
|
| 47 | gradientTextScaled(context, font, text, x, y, from, to, scale, 1f);
|
| 48 | }
|
| 49 |
|
| 50 | public static void gradientTextScaled(GuiGraphicsExtractor context, Font font, String text,
|
| 51 | float x, float y, int from, int to, float scale, float alpha) {
|
| 52 | if (alpha < 0.05f) {
|
| 53 | return;
|
| 54 | }
|
| 55 | Matrix3x2fStack pose = context.pose();
|
| 56 | pose.pushMatrix();
|
| 57 | pose.translate(x, y);
|
| 58 | pose.scale(scale, scale);
|
| 59 | gradientText(context, font, text, 0, 0, from, to, alpha, scale < 1.5f);
|
| 60 | pose.popMatrix();
|
| 61 | }
|
| 62 |
|
| 63 | // An animated rainbow wave with one colour per character.
|
| 64 | public static void rainbowText(GuiGraphicsExtractor context, Font font, String text, int x, int y) {
|
| 65 | rainbowText(context, font, text, x, y, 1f, 1f);
|
| 66 | }
|
| 67 |
|
| 68 | public static void rainbowText(GuiGraphicsExtractor context, Font font, String text,
|
| 69 | float x, float y, float scale) {
|
| 70 | rainbowText(context, font, text, x, y, scale, 1f);
|
| 71 | }
|
| 72 |
|
| 73 | public static void rainbowText(GuiGraphicsExtractor context, Font font, String text,
|
| 74 | float x, float y, float scale, float alpha) {
|
| 75 | if (alpha < 0.05f) {
|
| 76 | return;
|
| 77 | }
|
| 78 | Matrix3x2fStack pose = context.pose();
|
| 79 | pose.pushMatrix();
|
| 80 | pose.translate(x, y);
|
| 81 | pose.scale(scale, scale);
|
| 82 | context.guiRenderState.up();
|
| 83 | int cx = 0;
|
| 84 | for (int i = 0; i < text.length(); i++) {
|
| 85 | String ch = String.valueOf(text.charAt(i));
|
| 86 | context.text(font, ch, cx, 0, ColorUtil.fade(ColorUtil.rainbow(i), alpha), true);
|
| 87 | cx += font.width(ch);
|
| 88 | }
|
| 89 | pose.popMatrix();
|
| 90 | }
|
| 91 |
|
| 92 | public static void borderedRect(GuiGraphicsExtractor context, int x, int y, int x2, int y2,
|
| 93 | int fillColor, int borderColor) {
|
| 94 | context.fill(x, y, x2, y2, borderColor);
|
| 95 | context.fill(x + 1, y + 1, x2 - 1, y2 - 1, fillColor);
|
| 96 | }
|
| 97 |
|
| 98 | public static void roundedRect(GuiGraphicsExtractor context, int x, int y, int x2, int y2,
|
| 99 | int radius, int color) {
|
| 100 | roundedRect(context, x, y, x2, y2, radius, color, true, true);
|
| 101 | }
|
| 102 |
|
| 103 | // Each end can keep square corners.
|
| 104 | public static void roundedRect(GuiGraphicsExtractor context, int x, int y, int x2, int y2,
|
| 105 | int radius, int color, boolean roundTop, boolean roundBottom) {
|
| 106 | int r = Math.min(radius, Math.min((x2 - x) / 2, (y2 - y) / 2));
|
| 107 | if (r <= 0) {
|
| 108 | context.fill(x, y, x2, y2, color);
|
| 109 | return;
|
| 110 | }
|
| 111 | context.fill(x, y + (roundTop ? r : 0), x2, y2 - (roundBottom ? r : 0), color);
|
| 112 | for (int i = 0; i < r; i++) {
|
| 113 | // Corner inset taken from a circle of the same radius.
|
| 114 | double dy = r - i - 0.5;
|
| 115 | int inset = (int) Math.ceil(r - Math.sqrt(r * r - dy * dy));
|
| 116 | if (roundTop) {
|
| 117 | context.fill(x + inset, y + i, x2 - inset, y + i + 1, color);
|
| 118 | }
|
| 119 | if (roundBottom) {
|
| 120 | context.fill(x + inset, y2 - i - 1, x2 - inset, y2 - i, color);
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public static void roundedBorderedRect(GuiGraphicsExtractor context, int x, int y, int x2, int y2,
|
| 126 | int radius, int fillColor, int borderColor) {
|
| 127 | roundedRect(context, x, y, x2, y2, radius, borderColor);
|
| 128 | roundedRect(context, x + 1, y + 1, x2 - 1, y2 - 1, radius - 1, fillColor);
|
| 129 | }
|
| 130 |
|
| 131 | public static void shadow(GuiGraphicsExtractor context, int x, int y, int x2, int y2, int spread) {
|
| 132 | for (int i = spread; i >= 1; i--) {
|
| 133 | context.fill(x - i, y - i, x2 + i, y2 + i, 0x30000000);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public static void toggle(GuiGraphicsExtractor context, int x, int y, int w, int h,
|
| 138 | boolean on, int onColor, int offColor, int knobColor) {
|
| 139 | roundedRect(context, x, y, x + w, y + h, h / 2, on ? onColor : offColor);
|
| 140 | context.guiRenderState.up();
|
| 141 | int knob = h - 4;
|
| 142 | int knobX = on ? x + w - 2 - knob : x + 2;
|
| 143 | roundedRect(context, knobX, y + 2, knobX + knob, y + 2 + knob, knob / 2, knobColor);
|
| 144 | }
|
| 145 |
|
| 146 | public static void chevron(GuiGraphicsExtractor context, int x, int y, boolean down, int color) {
|
| 147 | for (int i = 0; i < 3; i++) {
|
| 148 | int row = down ? y + i : y + 2 - i;
|
| 149 | context.fill(x + i, row, x + 6 - i, row + 1, color);
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | public static final int STAR_SIZE = 11;
|
| 154 |
|
| 155 | // Sample points per pixel side.
|
| 156 | private static final int STAR_SAMPLES = 4;
|
| 157 | // Inner radius of a regular five point star as a share of the outer.
|
| 158 | private static final double STAR_INNER = 0.382;
|
| 159 | // How much smaller the cut out of the hollow star is.
|
| 160 | private static final double STAR_HOLLOW = 0.6;
|
| 161 |
|
| 162 | private static final float[] STAR_FILL = rasteriseStar(1);
|
| 163 | private static final float[] STAR_RING = ring(STAR_FILL, rasteriseStar(STAR_HOLLOW));
|
| 164 |
|
| 165 | // A five point star with soft edges. Chosen is solid and not chosen is a hollow ring.
|
| 166 | // Each pixel carries the share of its samples inside the shape. Edges stay crisp.
|
| 167 | public static void star(GuiGraphicsExtractor context, int x, int y, int color, boolean filled) {
|
| 168 | float[] cover = filled ? STAR_FILL : STAR_RING;
|
| 169 | for (int row = 0; row < STAR_SIZE; row++) {
|
| 170 | for (int col = 0; col < STAR_SIZE; col++) {
|
| 171 | float share = cover[row * STAR_SIZE + col];
|
| 172 | if (share > 0.02f) {
|
| 173 | context.fill(x + col, y + row, x + col + 1, y + row + 1,
|
| 174 | ColorUtil.fade(color, share));
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | // The outer shape with the inner one cut away.
|
| 181 | private static float[] ring(float[] outer, float[] inner) {
|
| 182 | float[] result = new float[outer.length];
|
| 183 | for (int i = 0; i < outer.length; i++) {
|
| 184 | result[i] = Math.max(0, outer[i] - inner[i]);
|
| 185 | }
|
| 186 | return result;
|
| 187 | }
|
| 188 |
|
| 189 | // Coverage of every pixel by a star scaled about the middle of the cell.
|
| 190 | private static float[] rasteriseStar(double scale) {
|
| 191 | double centre = STAR_SIZE / 2.0;
|
| 192 | double outer = centre * scale;
|
| 193 | double[] xs = new double[10];
|
| 194 | double[] ys = new double[10];
|
| 195 | for (int i = 0; i < xs.length; i++) {
|
| 196 | double angle = Math.toRadians(-90 + i * 36);
|
| 197 | double radius = i % 2 == 0 ? outer : outer * STAR_INNER;
|
| 198 | xs[i] = centre + Math.cos(angle) * radius;
|
| 199 | ys[i] = centre + Math.sin(angle) * radius;
|
| 200 | }
|
| 201 | float[] cover = new float[STAR_SIZE * STAR_SIZE];
|
| 202 | int perPixel = STAR_SAMPLES * STAR_SAMPLES;
|
| 203 | for (int row = 0; row < STAR_SIZE; row++) {
|
| 204 | for (int col = 0; col < STAR_SIZE; col++) {
|
| 205 | int hits = 0;
|
| 206 | for (int sy = 0; sy < STAR_SAMPLES; sy++) {
|
| 207 | for (int sx = 0; sx < STAR_SAMPLES; sx++) {
|
| 208 | double px = col + (sx + 0.5) / STAR_SAMPLES;
|
| 209 | double py = row + (sy + 0.5) / STAR_SAMPLES;
|
| 210 | if (insidePolygon(xs, ys, px, py)) {
|
| 211 | hits++;
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 | cover[row * STAR_SIZE + col] = (float) hits / perPixel;
|
| 216 | }
|
| 217 | }
|
| 218 | return cover;
|
| 219 | }
|
| 220 |
|
| 221 | // Crossing test. Works for the concave outline of a star.
|
| 222 | private static boolean insidePolygon(double[] xs, double[] ys, double px, double py) {
|
| 223 | boolean inside = false;
|
| 224 | for (int i = 0, j = xs.length - 1; i < xs.length; j = i++) {
|
| 225 | boolean crosses = (ys[i] > py) != (ys[j] > py);
|
| 226 | if (crosses && px < (xs[j] - xs[i]) * (py - ys[i]) / (ys[j] - ys[i]) + xs[i]) {
|
| 227 | inside = !inside;
|
| 228 | }
|
| 229 | }
|
| 230 | return inside;
|
| 231 | }
|
| 232 |
|
| 233 | // A filled triangle between three screen points drawn as one pixel rows.
|
| 234 | public static void triangle(GuiGraphicsExtractor context, double ax, double ay, double bx, double by,
|
| 235 | double cx, double cy, int color) {
|
| 236 | int top = (int) Math.floor(Math.min(ay, Math.min(by, cy)));
|
| 237 | int bottom = (int) Math.ceil(Math.max(ay, Math.max(by, cy)));
|
| 238 | for (int row = top; row < bottom; row++) {
|
| 239 | double y = row + 0.5;
|
| 240 | double left = Double.MAX_VALUE;
|
| 241 | double right = -Double.MAX_VALUE;
|
| 242 | double[] xs = {ax, bx, cx, ax};
|
| 243 | double[] ys = {ay, by, cy, ay};
|
| 244 | for (int i = 0; i < 3; i++) {
|
| 245 | if ((ys[i] <= y) != (ys[i + 1] <= y)) {
|
| 246 | double x = xs[i] + (y - ys[i]) * (xs[i + 1] - xs[i]) / (ys[i + 1] - ys[i]);
|
| 247 | left = Math.min(left, x);
|
| 248 | right = Math.max(right, x);
|
| 249 | }
|
| 250 | }
|
| 251 | if (left <= right) {
|
| 252 | context.fill((int) Math.round(left), row, (int) Math.round(right), row + 1, color);
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | // Five pixels square.
|
| 258 | public static void tick(GuiGraphicsExtractor context, int x, int y, int color) {
|
| 259 | context.fill(x, y + 2, x + 1, y + 4, color);
|
| 260 | context.fill(x + 1, y + 3, x + 2, y + 5, color);
|
| 261 | context.fill(x + 2, y + 2, x + 3, y + 4, color);
|
| 262 | context.fill(x + 3, y + 1, x + 4, y + 3, color);
|
| 263 | context.fill(x + 4, y, x + 5, y + 2, color);
|
| 264 | }
|
| 265 |
|
| 266 | // Behind a world label. Text over bright terrain is unreadable without it.
|
| 267 | public static final int LABEL_BACKGROUND = 0x90000000;
|
| 268 |
|
| 269 | private static final int TOOLTIP_FILL = 0xF00E0E14;
|
| 270 | private static final int TOOLTIP_BORDER = 0x50FFFFFF;
|
| 271 | private static final int TOOLTIP_TEXT = 0xFFD8D8E4;
|
| 272 |
|
| 273 | // A scaled label centred on a screen point. Each part carries its own
|
| 274 | // colour and they run left to right on one line.
|
| 275 | public static void label(GuiGraphicsExtractor context, Font font, double screenX, double screenY,
|
| 276 | float scale, List<String> parts, List<Integer> colors) {
|
| 277 | label(context, font, screenX, screenY, scale, parts, colors, LABEL_BACKGROUND);
|
| 278 | }
|
| 279 |
|
| 280 | // The same label over a background of the module's own colour.
|
| 281 | public static void label(GuiGraphicsExtractor context, Font font, double screenX, double screenY,
|
| 282 | float scale, List<String> parts, List<Integer> colors, int background) {
|
| 283 | int width = 0;
|
| 284 | for (String part : parts) {
|
| 285 | width += font.width(part);
|
| 286 | }
|
| 287 | Matrix3x2fStack pose = context.pose();
|
| 288 | pose.pushMatrix();
|
| 289 | pose.translate((float) screenX, (float) screenY);
|
| 290 | pose.scale(scale, scale);
|
| 291 | int half = width / 2;
|
| 292 | context.fill(-half - 2, -2, half + 2, font.lineHeight, background);
|
| 293 | context.guiRenderState.up();
|
| 294 | int x = -half;
|
| 295 | for (int i = 0; i < parts.size(); i++) {
|
| 296 | String part = parts.get(i);
|
| 297 | context.text(font, part, x, -1, colors.get(i), false);
|
| 298 | x += font.width(part);
|
| 299 | }
|
| 300 | pose.popMatrix();
|
| 301 | }
|
| 302 |
|
| 303 | // Sits beside the cursor and stays inside the screen.
|
| 304 | public static void tooltip(GuiGraphicsExtractor context, Font font, List<String> lines,
|
| 305 | int mouseX, int mouseY, int screenWidth, int screenHeight) {
|
| 306 | if (lines.isEmpty()) {
|
| 307 | return;
|
| 308 | }
|
| 309 | int lineHeight = font.lineHeight + 1;
|
| 310 | int width = 0;
|
| 311 | for (String line : lines) {
|
| 312 | width = Math.max(width, font.width(line));
|
| 313 | }
|
| 314 | int height = lines.size() * lineHeight;
|
| 315 | int x = mouseX + 10;
|
| 316 | int y = mouseY + 10;
|
| 317 | if (x + width + 4 > screenWidth) {
|
| 318 | x = Math.max(4, mouseX - width - 12);
|
| 319 | }
|
| 320 | if (y + height + 4 > screenHeight) {
|
| 321 | y = Math.max(4, screenHeight - height - 4);
|
| 322 | }
|
| 323 | context.guiRenderState.up();
|
| 324 | roundedBorderedRect(context, x - 4, y - 3, x + width + 4, y + height + 2,
|
| 325 | 3, TOOLTIP_FILL, TOOLTIP_BORDER);
|
| 326 | context.guiRenderState.up();
|
| 327 | for (int i = 0; i < lines.size(); i++) {
|
| 328 | context.text(font, lines.get(i), x, y + i * lineHeight, TOOLTIP_TEXT, false);
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 | // Horizontal hue ramp. The vanilla gradient fill only runs top to bottom.
|
| 333 | public static void hueBar(GuiGraphicsExtractor context, int x, int y, int width, int height) {
|
| 334 | int band = 3;
|
| 335 | for (int i = 0; i < width; i += band) {
|
| 336 | int end = Math.min(width, i + band);
|
| 337 | context.fill(x + i, y, x + end, y + height, ColorUtil.hsv(360f * i / width, 0.8f, 1f));
|
| 338 | }
|
| 339 | }
|
| 340 | }
|
| 341 |
|