|
| 1 | +package com.github.simulatan.meteornotificationsaddon.utils; |
| 2 | + |
| 3 | +import meteordevelopment.meteorclient.renderer.Renderer2D; |
| 4 | +import meteordevelopment.meteorclient.utils.render.color.Color; |
| 5 | + |
| 6 | +public class RoundedRenderer2D { |
| 7 | + |
| 8 | + private static final double circleNone = 0; |
| 9 | + private static final double circleQuarter = Math.PI / 2; |
| 10 | + private static final double circleHalf = circleQuarter * 2; |
| 11 | + private static final double circleThreeQuarter = circleQuarter * 3; |
| 12 | + |
| 13 | + public static void quadRoundedOutline(Renderer2D mb, double x, double y, double width, double height, Color color, double r, double s) { |
| 14 | + r = getR(r, width, height); |
| 15 | + if (r <= 0) { |
| 16 | + mb.quad(x, y, width, s, color); |
| 17 | + mb.quad(x, y + height - s, width, s, color); |
| 18 | + mb.quad(x, y + s, s, height - s * 2, color); |
| 19 | + mb.quad(x + width - s, y + s, s, height - s * 2, color); |
| 20 | + } |
| 21 | + else { |
| 22 | + //top |
| 23 | + circlePartOutline(mb, x + r, y + r, r, circleThreeQuarter, circleQuarter, color, s); |
| 24 | + mb.quad(x + r, y, width - r * 2, s, color); |
| 25 | + circlePartOutline(mb, x + width - r, y + r, r, circleNone, circleQuarter, color, s); |
| 26 | + //middle |
| 27 | + mb.quad(x, y + r, s, height - r * 2, color); |
| 28 | + mb.quad(x + width - s, y + r, s, height - r * 2, color); |
| 29 | + //bottom |
| 30 | + circlePartOutline(mb, x + width - r, y + height - r, r, circleQuarter, circleQuarter, color, s); |
| 31 | + mb.quad(x + r, y + height - s, width - r * 2, s, color); |
| 32 | + circlePartOutline(mb, x + r, y + height - r, r, circleHalf, circleQuarter, color, s); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void quadRounded(Renderer2D mb, double x, double y, double width, double height, Color color, double r, boolean roundTop) { |
| 37 | + r = getR(r, width, height); |
| 38 | + if (r <= 0) |
| 39 | + mb.quad(x, y, width, height, color); |
| 40 | + else { |
| 41 | + if (roundTop) { |
| 42 | + //top |
| 43 | + circlePart(mb, x + r, y + r, r, circleThreeQuarter, circleQuarter, color); |
| 44 | + mb.quad(x + r, y, width - 2 * r, r, color); |
| 45 | + circlePart(mb, x + width - r, y + r, r, circleNone, circleQuarter, color); |
| 46 | + //middle |
| 47 | + mb.quad(x, y + r, width, height - 2 * r, color); |
| 48 | + } |
| 49 | + else { |
| 50 | + //middle |
| 51 | + mb.quad(x, y, width, height - r, color); |
| 52 | + } |
| 53 | + //bottom |
| 54 | + circlePart(mb, x + width - r, y + height - r, r, circleQuarter, circleQuarter, color); |
| 55 | + mb.quad(x + r, y + height - r, width - 2 * r, r, color); |
| 56 | + circlePart(mb, x + r, y + height - r, r, circleHalf, circleQuarter, color); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static void quadRoundedSide(Renderer2D mb, double x, double y, double width, double height, Color color, double r, boolean right) { |
| 61 | + r = getR(r, width, height); |
| 62 | + if (r <= 0) |
| 63 | + mb.quad(x, y, width, height, color); |
| 64 | + else { |
| 65 | + if (right) { |
| 66 | + circlePart(mb, x + width - r, y + r, r, circleNone, circleQuarter, color); |
| 67 | + circlePart(mb, x + width - r, y + height - r, r, circleQuarter, circleQuarter, color); |
| 68 | + mb.quad(x, y, width - r, height, color); |
| 69 | + mb.quad(x + width - r, y + r, r, height - r * 2, color); |
| 70 | + } |
| 71 | + else { |
| 72 | + circlePart(mb, x + r, y + r, r, circleThreeQuarter, circleQuarter, color); |
| 73 | + circlePart(mb, x + r, y + height - r, r, circleHalf, circleQuarter, color); |
| 74 | + mb.quad(x + r, y, width - r, height, color); |
| 75 | + mb.quad(x, y + r, r, height - r * 2, color); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static double getR(double r, double w, double h) { |
| 81 | + if (r * 2 > h) { |
| 82 | + r = h / 2; |
| 83 | + } |
| 84 | + if (r * 2 > w) { |
| 85 | + r = w / 2; |
| 86 | + } |
| 87 | + return r; |
| 88 | + } |
| 89 | + |
| 90 | + private static int getCirDepth(double r, double angle) { |
| 91 | + return Math.max(1, (int)(angle * r / circleQuarter)); |
| 92 | + } |
| 93 | + |
| 94 | + public static void circlePart(Renderer2D mb, double x, double y, double r, double startAngle, double angle, Color color) { |
| 95 | + int cirDepth = getCirDepth(r, angle); |
| 96 | + double cirPart = angle / cirDepth; |
| 97 | + mb.triangles.ensureTriCapacity(); |
| 98 | + int center = mb.triangles.vec2(x, y).color(color).next(); |
| 99 | + int prev = vecOnCircle(mb, x, y, r, startAngle, color); |
| 100 | + for (int i = 1; i < cirDepth + 1; i++) { |
| 101 | + int next = vecOnCircle(mb, x, y, r, startAngle + cirPart * i, color); |
| 102 | + mb.triangles.quad(prev, center, next, next); |
| 103 | + prev = next; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static void circlePartOutline(Renderer2D mb, double x, double y, double r, double startAngle, double angle, Color color, double outlineWidth) { |
| 108 | + if (outlineWidth >= r) { |
| 109 | + circlePart(mb, x, y, r, startAngle, angle, color); |
| 110 | + return; |
| 111 | + } |
| 112 | + int cirDepth = getCirDepth(r, angle); |
| 113 | + double cirPart = angle / cirDepth; |
| 114 | + int innerPrev = vecOnCircle(mb, x, y, r - outlineWidth, startAngle, color); |
| 115 | + int outerPrev = vecOnCircle(mb, x, y, r, startAngle, color); |
| 116 | + for (int i = 1; i < cirDepth + 1; i++) { |
| 117 | + int inner = vecOnCircle(mb, x, y, r - outlineWidth, startAngle + cirPart * i, color); |
| 118 | + int outer = vecOnCircle(mb, x, y, r, startAngle + cirPart * i, color); |
| 119 | + mb.triangles.quad(inner, innerPrev, outerPrev, outer); |
| 120 | + innerPrev = inner; |
| 121 | + outerPrev = outer; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static int vecOnCircle(Renderer2D mb, double x, double y, double r, double angle, Color color) { |
| 126 | + mb.triangles.ensureTriCapacity(); |
| 127 | + return mb.triangles.vec2(x + Math.sin(angle) * r, y - Math.cos(angle) * r).color(color).next(); |
| 128 | + } |
| 129 | +} |
0 commit comments