|
1 | 1 | package dsns.betterhud; |
2 | 2 |
|
| 3 | +import dsns.betterhud.util.BaseMod; |
| 4 | +import dsns.betterhud.util.CustomText; |
| 5 | +import dsns.betterhud.util.ModSettings; |
| 6 | +import it.unimi.dsi.fastutil.objects.ObjectArrayList; |
3 | 7 | import java.util.ArrayList; |
4 | 8 | import java.util.List; |
5 | | - |
6 | | -import it.unimi.dsi.fastutil.objects.ObjectArrayList; |
7 | 9 | import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; |
8 | 10 | import net.minecraft.client.MinecraftClient; |
9 | 11 | import net.minecraft.client.gui.DrawContext; |
10 | 12 | import net.minecraft.client.render.RenderTickCounter; |
11 | | -import dsns.betterhud.mods.*; |
12 | | -import dsns.betterhud.util.BaseMod; |
13 | | -import dsns.betterhud.util.CustomText; |
14 | 13 |
|
15 | 14 | public class BetterHUDGUI implements ClientTickEvents.StartTick { |
16 | | - private final MinecraftClient client = MinecraftClient.getInstance(); |
17 | | - private final List<CustomText> topLeftText = new ObjectArrayList<>(); |
18 | | - private final List<CustomText> topRightText = new ObjectArrayList<>(); |
19 | | - private final List<CustomText> bottomLeftList = new ObjectArrayList<>(); |
20 | | - private final List<CustomText> bottomRightText = new ObjectArrayList<>(); |
21 | | - private final List<CustomText> customPositionText = new ObjectArrayList<>(); |
22 | | - |
23 | | - @Override |
24 | | - public void onStartTick(MinecraftClient client) { |
25 | | - this.topLeftText.clear(); |
26 | | - this.topRightText.clear(); |
27 | | - this.bottomLeftList.clear(); |
28 | | - this.bottomRightText.clear(); |
29 | | - this.customPositionText.clear(); |
30 | | - |
31 | | - ArrayList<BaseMod> mods = new ArrayList<>(); |
32 | | - mods.add(new FPS()); |
33 | | - mods.add(new Ping()); |
34 | | - mods.add(new Momentum()); |
35 | | - mods.add(new Coordinates()); |
36 | | - mods.add(new Biome()); |
37 | | - mods.add(new Facing()); |
38 | | - mods.add(new Time()); |
39 | | - |
40 | | - for (BaseMod mod : mods) { |
41 | | - ModSettings modSettings = Config.settings.get(mod.getModID()); |
42 | | - if (!modSettings.enabled) |
43 | | - continue; |
44 | | - |
45 | | - CustomText modText = mod.onStartTick(client); |
46 | | - if (modText == null) |
47 | | - continue; |
48 | | - |
49 | | - if (modSettings.customPosition) { |
50 | | - modText.customPosition = true; |
51 | | - modText.customX = modSettings.customX; |
52 | | - modText.customY = modSettings.customY; |
53 | | - this.customPositionText.add(modText); |
54 | | - } else if (modSettings.orientation.equals("top-left")) { |
55 | | - this.topLeftText.add(modText); |
56 | | - } else if (modSettings.orientation.equals("top-right")) { |
57 | | - this.topRightText.add(modText); |
58 | | - } else if (modSettings.orientation.equals("bottom-left")) { |
59 | | - this.bottomLeftList.add(modText); |
60 | | - } else if (modSettings.orientation.equals("bottom-right")) { |
61 | | - this.bottomRightText.add(modText); |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - public void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter) { |
67 | | - if (client.getDebugHud().shouldShowDebugHud()) |
68 | | - return; |
69 | | - if (client.options.hudHidden) |
70 | | - return; |
71 | | - |
72 | | - int x = Config.horizontalMargin; |
73 | | - int y = Config.verticalMargin; |
74 | | - |
75 | | - for (CustomText text : topLeftText) { |
76 | | - drawString(drawContext, text, x, y); |
77 | | - |
78 | | - y += (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2) + Config.lineHeight; |
79 | | - } |
80 | | - |
81 | | - y = client.getWindow().getScaledHeight() - Config.verticalMargin; |
82 | | - |
83 | | - for (CustomText text : bottomLeftList) { |
84 | | - y -= (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2); |
85 | | - drawString(drawContext, text, x, y); |
86 | | - y -= Config.lineHeight; |
87 | | - } |
88 | | - |
89 | | - y = Config.verticalMargin; |
90 | | - for (CustomText text : topRightText) { |
91 | | - int offset = (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2) |
92 | | - + Config.horizontalMargin; |
93 | | - x = client.getWindow().getScaledWidth() - offset; |
94 | | - drawString(drawContext, text, x, y); |
95 | | - |
96 | | - y += (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2) + Config.lineHeight; |
97 | | - } |
98 | | - |
99 | | - y = client.getWindow().getScaledHeight() - Config.verticalMargin; |
100 | | - for (CustomText text : bottomRightText) { |
101 | | - int offset = (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2) |
102 | | - + Config.horizontalMargin; |
103 | | - x = client.getWindow().getScaledWidth() - offset; |
104 | | - |
105 | | - y -= (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2); |
106 | | - |
107 | | - drawString(drawContext, text, x, y); |
108 | | - |
109 | | - y -= Config.lineHeight; |
110 | | - } |
111 | | - |
112 | | - for (CustomText text : customPositionText) { |
113 | | - float xPercent = text.customX / 100.0f; |
114 | | - float yPercent = text.customY / 100.0f; |
115 | | - |
116 | | - int maxX = client.getWindow().getScaledWidth() - (Config.horizontalPadding * 2) |
117 | | - - (client.textRenderer.getWidth(text.text) - 1); |
118 | | - int maxY = client.getWindow().getScaledHeight() - (Config.verticalPadding * 2) |
119 | | - - (client.textRenderer.fontHeight - 1); |
120 | | - |
121 | | - int scaledX = (int) (xPercent * maxX); |
122 | | - int scaledY = (int) (yPercent * maxY); |
123 | | - |
124 | | - drawString(drawContext, text, scaledX, scaledY); |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - private void drawString(DrawContext drawContext, CustomText text, int x, int y) { |
129 | | - drawContext.fill(x, y, |
130 | | - x + (client.textRenderer.getWidth(text.text) - 1) + (Config.horizontalPadding * 2), |
131 | | - y + (client.textRenderer.fontHeight - 1) + (Config.verticalPadding * 2), text.backgroundColor); |
132 | | - |
133 | | - drawContext.drawText(client.textRenderer, text.text, x + Config.horizontalPadding, y + Config.verticalPadding, |
134 | | - text.color, true); |
135 | | - } |
| 15 | + |
| 16 | + public static int verticalPadding = 4; |
| 17 | + public static int horizontalPadding = 4; |
| 18 | + |
| 19 | + public static int verticalMargin = 1; |
| 20 | + public static int horizontalMargin = 1; |
| 21 | + |
| 22 | + public static int lineHeight = 1; |
| 23 | + |
| 24 | + private final MinecraftClient client = MinecraftClient.getInstance(); |
| 25 | + private final List<CustomText> topLeftText = new ObjectArrayList<>(); |
| 26 | + private final List<CustomText> topRightText = new ObjectArrayList<>(); |
| 27 | + private final List<CustomText> bottomLeftList = new ObjectArrayList<>(); |
| 28 | + private final List<CustomText> bottomRightText = new ObjectArrayList<>(); |
| 29 | + private final List<CustomText> customPositionText = new ObjectArrayList<>(); |
| 30 | + |
| 31 | + private ArrayList<BaseMod> mods = new ArrayList<>(); |
| 32 | + |
| 33 | + public void instantiateMods(ArrayList<BaseMod> mods) { |
| 34 | + this.mods = mods; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onStartTick(MinecraftClient client) { |
| 39 | + this.topLeftText.clear(); |
| 40 | + this.topRightText.clear(); |
| 41 | + this.bottomLeftList.clear(); |
| 42 | + this.bottomRightText.clear(); |
| 43 | + this.customPositionText.clear(); |
| 44 | + |
| 45 | + for (BaseMod mod : mods) { |
| 46 | + ModSettings modSettings = mod.getModSettings(); |
| 47 | + if (!modSettings.getSetting("enabled").getBooleanValue()) continue; |
| 48 | + |
| 49 | + CustomText modText = mod.onStartTick(client); |
| 50 | + if (modText == null) continue; |
| 51 | + |
| 52 | + String orientation = modSettings |
| 53 | + .getSetting("orientation") |
| 54 | + .getStringValue(); |
| 55 | + |
| 56 | + if (modSettings.getSetting("customPosition").getBooleanValue()) { |
| 57 | + modText.customPosition = true; |
| 58 | + modText.customX = modSettings |
| 59 | + .getSetting("customX") |
| 60 | + .getIntValue(); |
| 61 | + modText.customY = modSettings |
| 62 | + .getSetting("customY") |
| 63 | + .getIntValue(); |
| 64 | + this.customPositionText.add(modText); |
| 65 | + } else if (orientation.equals("top-left")) { |
| 66 | + this.topLeftText.add(modText); |
| 67 | + } else if (orientation.equals("top-right")) { |
| 68 | + this.topRightText.add(modText); |
| 69 | + } else if (orientation.equals("bottom-left")) { |
| 70 | + this.bottomLeftList.add(modText); |
| 71 | + } else if (orientation.equals("bottom-right")) { |
| 72 | + this.bottomRightText.add(modText); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void onHudRender( |
| 78 | + DrawContext drawContext, |
| 79 | + RenderTickCounter tickCounter |
| 80 | + ) { |
| 81 | + if (client.getDebugHud().shouldShowDebugHud()) return; |
| 82 | + if (client.options.hudHidden) return; |
| 83 | + |
| 84 | + int x = horizontalMargin; |
| 85 | + int y = verticalMargin; |
| 86 | + |
| 87 | + for (CustomText text : topLeftText) { |
| 88 | + drawString(drawContext, text, x, y); |
| 89 | + |
| 90 | + y += |
| 91 | + (client.textRenderer.fontHeight - 1) + |
| 92 | + (verticalPadding * 2) + |
| 93 | + lineHeight; |
| 94 | + } |
| 95 | + |
| 96 | + y = client.getWindow().getScaledHeight() - verticalMargin; |
| 97 | + |
| 98 | + for (CustomText text : bottomLeftList) { |
| 99 | + y -= (client.textRenderer.fontHeight - 1) + (verticalPadding * 2); |
| 100 | + drawString(drawContext, text, x, y); |
| 101 | + y -= lineHeight; |
| 102 | + } |
| 103 | + |
| 104 | + y = verticalMargin; |
| 105 | + for (CustomText text : topRightText) { |
| 106 | + int offset = |
| 107 | + (client.textRenderer.getWidth(text.text) - 1) + |
| 108 | + (horizontalPadding * 2) + |
| 109 | + horizontalMargin; |
| 110 | + x = client.getWindow().getScaledWidth() - offset; |
| 111 | + drawString(drawContext, text, x, y); |
| 112 | + |
| 113 | + y += |
| 114 | + (client.textRenderer.fontHeight - 1) + |
| 115 | + (verticalPadding * 2) + |
| 116 | + lineHeight; |
| 117 | + } |
| 118 | + |
| 119 | + y = client.getWindow().getScaledHeight() - verticalMargin; |
| 120 | + for (CustomText text : bottomRightText) { |
| 121 | + int offset = |
| 122 | + (client.textRenderer.getWidth(text.text) - 1) + |
| 123 | + (horizontalPadding * 2) + |
| 124 | + horizontalMargin; |
| 125 | + x = client.getWindow().getScaledWidth() - offset; |
| 126 | + |
| 127 | + y -= (client.textRenderer.fontHeight - 1) + (verticalPadding * 2); |
| 128 | + |
| 129 | + drawString(drawContext, text, x, y); |
| 130 | + |
| 131 | + y -= lineHeight; |
| 132 | + } |
| 133 | + |
| 134 | + for (CustomText text : customPositionText) { |
| 135 | + float xPercent = text.customX / 100.0f; |
| 136 | + float yPercent = text.customY / 100.0f; |
| 137 | + |
| 138 | + int maxX = |
| 139 | + client.getWindow().getScaledWidth() - |
| 140 | + (horizontalPadding * 2) - |
| 141 | + (client.textRenderer.getWidth(text.text) - 1); |
| 142 | + int maxY = |
| 143 | + client.getWindow().getScaledHeight() - |
| 144 | + (verticalPadding * 2) - |
| 145 | + (client.textRenderer.fontHeight - 1); |
| 146 | + |
| 147 | + int scaledX = (int) (xPercent * maxX); |
| 148 | + int scaledY = (int) (yPercent * maxY); |
| 149 | + |
| 150 | + drawString(drawContext, text, scaledX, scaledY); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void drawString( |
| 155 | + DrawContext drawContext, |
| 156 | + CustomText text, |
| 157 | + int x, |
| 158 | + int y |
| 159 | + ) { |
| 160 | + drawContext.fill( |
| 161 | + x, |
| 162 | + y, |
| 163 | + x + |
| 164 | + (client.textRenderer.getWidth(text.text) - 1) + |
| 165 | + (horizontalPadding * 2), |
| 166 | + y + (client.textRenderer.fontHeight - 1) + (verticalPadding * 2), |
| 167 | + text.backgroundColor |
| 168 | + ); |
| 169 | + |
| 170 | + drawContext.drawText( |
| 171 | + client.textRenderer, |
| 172 | + text.text, |
| 173 | + x + horizontalPadding, |
| 174 | + y + verticalPadding, |
| 175 | + text.color, |
| 176 | + true |
| 177 | + ); |
| 178 | + } |
136 | 179 | } |
0 commit comments