Skip to content

Commit c752f8a

Browse files
committed
credits keyboard nav
1 parent 08c76ca commit c752f8a

File tree

2 files changed

+119
-33
lines changed

2 files changed

+119
-33
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies {
3232
modImplementation "org.quiltmc:quilt-loader:${project.loader_version}"
3333
modImplementation "org.quiltmc.quilted-fabric-api:quilted-fabric-api:${quilted_fabric_api_version}"
3434

35-
include(modImplementation "com.github.AxolotlClient:AxolotlClient-config:087a1c3")
35+
include(modImplementation "com.github.AxolotlClient:AxolotlClient-config:3e59430")
3636

3737
modImplementation "com.terraformersmc:modmenu:4.0.5"
3838

src/main/java/io/github/axolotlclient/config/screen/CreditsScreen.java

Lines changed: 118 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package io.github.axolotlclient.config.screen;
22

33
import com.mojang.blaze3d.glfw.Window;
4+
import com.mojang.blaze3d.platform.InputUtil;
45
import io.github.axolotlclient.AxolotlClient;
6+
import io.github.axolotlclient.AxolotlclientConfig.Color;
57
import io.github.axolotlclient.modules.hud.util.DrawUtil;
68
import io.github.axolotlclient.modules.hud.util.Rectangle;
79
import io.github.axolotlclient.util.Util;
810
import net.minecraft.client.MinecraftClient;
11+
import net.minecraft.client.gui.AbstractParentElement;
12+
import net.minecraft.client.gui.Element;
13+
import net.minecraft.client.gui.Selectable;
914
import net.minecraft.client.gui.screen.Screen;
1015
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
1116
import net.minecraft.client.gui.screen.narration.NarrationPart;
1217
import net.minecraft.client.gui.widget.ButtonWidget;
18+
import net.minecraft.client.gui.widget.ElementListWidget;
1319
import net.minecraft.client.gui.widget.EntryListWidget;
1420
import net.minecraft.client.resource.language.I18n;
1521
import net.minecraft.client.sound.PositionedSoundInstance;
@@ -20,11 +26,13 @@
2026
import net.minecraft.text.Style;
2127
import net.minecraft.text.Text;
2228
import net.minecraft.util.Formatting;
29+
import org.jetbrains.annotations.Nullable;
2330
import org.lwjgl.opengl.GL11;
2431

2532
import java.util.ArrayList;
2633
import java.util.HashMap;
2734
import java.util.List;
35+
import java.util.stream.Collectors;
2836

2937
public class CreditsScreen extends Screen {
3038

@@ -41,7 +49,7 @@ public class CreditsScreen extends Screen {
4149
private final SoundInstance bgm = PositionedSoundInstance.master(SoundEvents.MUSIC_DISC_CHIRP, 1, 1);
4250

4351
public CreditsScreen(Screen parent){
44-
super(Text.empty());
52+
super(Text.translatable("credits"));
4553
this.parent=parent;
4654
}
4755

@@ -93,6 +101,19 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
93101
return false;
94102
}
95103

104+
@Override
105+
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
106+
if(keyCode == InputUtil.KEY_ESCAPE_CODE){
107+
if(creditOverlay == null) {
108+
MinecraftClient.getInstance().setScreen(parent);
109+
} else {
110+
creditOverlay = null;
111+
}
112+
return true;
113+
}
114+
return super.keyPressed(keyCode, scanCode, modifiers);
115+
}
116+
96117
@Override
97118
public boolean mouseReleased(double mouseX, double mouseY, int button) {
98119
return creditsList.mouseReleased(mouseX, mouseY, button) || super.mouseReleased(mouseX, mouseY, button);
@@ -106,6 +127,13 @@ public void resize(MinecraftClient client, int width, int height) {
106127

107128
@Override
108129
public void init() {
130+
credits.clear();
131+
initCredits();
132+
133+
creditsList = new CreditsList(client, width, height, 50, height - 50, 25);
134+
addSelectableChild(creditsList);
135+
136+
109137
this.addDrawableChild(new ButtonWidget(
110138
width/2 -75, height - 50 + 22, 150, 20,
111139
Text.translatable("back"), buttonWidget -> {
@@ -127,11 +155,6 @@ public void init() {
127155
.append(Text.translatable(AxolotlClient.CONFIG.creditsBGM.get()?"options.on":"options.off")));
128156
}
129157
));
130-
131-
credits.clear();
132-
initCredits();
133-
134-
creditsList = new CreditsList(client, width, height, 50, height - 50, 25);
135158
}
136159

137160
private void initCredits(){
@@ -168,7 +191,27 @@ public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
168191
return super.mouseScrolled(mouseX, mouseY, amount) || creditsList.mouseScrolled(mouseX, mouseY, amount);
169192
}
170193

171-
private class CreditsList extends EntryListWidget<Credit> {
194+
@Override
195+
public boolean changeFocus(boolean lookForwards) {
196+
/*if(creditOverlay != null){
197+
setFocused(creditOverlay);
198+
}*/
199+
return super.changeFocus(lookForwards);
200+
}
201+
202+
@Override
203+
public List<? extends Element> children() {
204+
if(CreditsScreen.this.creditOverlay != null){
205+
List<? extends Element> l = new ArrayList<>(super.children());
206+
l.remove(creditsList);
207+
return l;
208+
}
209+
return super.children();
210+
}
211+
212+
213+
214+
private class CreditsList extends ElementListWidget<Credit> {
172215

173216
public CreditsList(MinecraftClient minecraftClient, int width, int height, int top, int bottom, int entryHeight) {
174217
super(minecraftClient, width, height, top, bottom, entryHeight);
@@ -198,30 +241,45 @@ protected void renderList(MatrixStack matrices, int mouseX, int mouseY, float de
198241
public int getRowLeft() {
199242
return width/2;
200243
}
244+
245+
@Override
246+
public boolean changeFocus(boolean lookForwards) {
247+
if(creditOverlay != null){
248+
return false;
249+
}
250+
return super.changeFocus(lookForwards);
251+
}
201252
}
202253

203-
private class Credit extends EntryListWidget.Entry<Credit> {
254+
private class Credit extends ElementListWidget.Entry<Credit> {
204255

205256
private final String name;
206257
private final String[] things;
207258

208259
private boolean hovered;
209260

261+
private final ButtonWidget c = new ButtonWidget(-2, -2, 1, 1, Text.empty(), buttonWidget -> creditOverlay = new Overlay(this));
262+
210263
public Credit(String name, String... things){
211264
this.name=name;
212265
this.things=things;
213266
}
214267

215268
@Override
216269
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
217-
if(hovered) {
270+
if(hovered || c.isFocused()) {
218271
drawVerticalLine(matrices, x - 100, y, y + 20, io.github.axolotlclient.AxolotlclientConfig.Color.ERROR.getAsInt());
219272
drawVerticalLine(matrices, x + 100, y, y + 20, io.github.axolotlclient.AxolotlclientConfig.Color.ERROR.getAsInt());
220273
drawHorizontalLine(matrices, x - 100, x + 100, y + 20, io.github.axolotlclient.AxolotlclientConfig.Color.ERROR.getAsInt());
221274
drawHorizontalLine(matrices, x - 100, x + 100, y, io.github.axolotlclient.AxolotlclientConfig.Color.ERROR.getAsInt());
222275
}
223276
this.hovered=hovered;
224-
DrawUtil.drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer, name, x, y + 5, hovered ? io.github.axolotlclient.AxolotlclientConfig.Color.SELECTOR_RED.getAsInt() : -1, true);
277+
DrawUtil.drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer, name, x, y + 5, hovered || c.isFocused() ? io.github.axolotlclient.AxolotlclientConfig.Color.SELECTOR_RED.getAsInt() : -1, true);
278+
}
279+
280+
@Override
281+
public List<? extends Element> children() {
282+
return List.of(c);
225283
}
226284

227285
@Override
@@ -232,9 +290,23 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
232290
}
233291
return false;
234292
}
293+
294+
@Override
295+
public List<? extends Selectable> selectableChildren() {
296+
return List.of(c);
297+
}
298+
299+
@Nullable
300+
@Override
301+
public Element getFocused() {
302+
if(super.getFocused() == null){
303+
setFocused(c);
304+
}
305+
return super.getFocused();
306+
}
235307
}
236308

237-
private class Overlay extends DrawUtil {
309+
private class Overlay {
238310

239311
private Window window;
240312
Credit credit;
@@ -247,54 +319,53 @@ private class Overlay extends DrawUtil {
247319
protected HashMap<Integer, String> lines = new HashMap<>();
248320

249321
public Overlay(Credit credit) {
250-
x=100;
251-
y=50;
252-
this.credit=credit;
322+
x = 100;
323+
y = 50;
324+
this.credit = credit;
253325

254326
init();
255327
}
256328

257-
public void init(){
329+
public void init() {
258330
window = MinecraftClient.getInstance().getWindow();
259-
this.width=window.getScaledWidth()-200;
260-
this.height=window.getScaledHeight()-100;
331+
this.width = window.getScaledWidth() - 200;
332+
this.height = window.getScaledHeight() - 100;
261333

262-
int startY=y+50;
263-
for(String t:credit.things){
334+
int startY = y + 50;
335+
for (String t : credit.things) {
264336

265-
if(t.startsWith("http")){
337+
if (t.startsWith("http")) {
266338
effects.put(t, new ClickEvent(ClickEvent.Action.OPEN_URL, t));
267339
lines.put(startY, Formatting.UNDERLINE + t);
268340
} else {
269341
lines.put(startY, t);
270342
}
271-
startY+=12;
343+
startY += 12;
272344
}
273345
}
274346

275347
public void render(MatrixStack matrices) {
276348
DrawUtil.fillRect(matrices, new io.github.axolotlclient.modules.hud.util.Rectangle(x, y, width, height), io.github.axolotlclient.AxolotlclientConfig.Color.DARK_GRAY.withAlpha(127));
277349
DrawUtil.outlineRect(matrices, new Rectangle(x, y, width, height), io.github.axolotlclient.AxolotlclientConfig.Color.BLACK);
278350

279-
drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer, credit.name, window.getScaledWidth()/2, y+7, -16784327, true);
280-
351+
DrawUtil.drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer, credit.name, window.getScaledWidth() / 2, y + 7, -16784327, true);
281352

282353
lines.forEach((integer, s) ->
283-
drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer,
284-
s, x+width/2, integer,
285-
io.github.axolotlclient.AxolotlclientConfig.Color.SELECTOR_GREEN.getAsInt(), true)
354+
DrawUtil.drawCenteredString(matrices, MinecraftClient.getInstance().textRenderer,
355+
s, x + width / 2, integer,
356+
Color.SELECTOR_GREEN.getAsInt(), true)
286357
);
287358
}
288359

289-
public boolean isMouseOver(double mouseX, double mouseY){
290-
return mouseX>=x && mouseX<=x+width && mouseY >=y && mouseY <= y+height;
360+
public boolean isMouseOver(double mouseX, double mouseY) {
361+
return mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
291362
}
292363

293-
public void mouseClicked(double mouseX, double mouseY){
364+
public void mouseClicked(double mouseX, double mouseY) {
294365
lines.forEach((integer, s) -> {
295-
if((mouseY>=integer && mouseY<integer+11) &&
296-
mouseX >= x+width/2F-MinecraftClient.getInstance().textRenderer.getWidth(s)/2F &&
297-
mouseX<= x+width/2F+ MinecraftClient.getInstance().textRenderer.getWidth(s)/2F){
366+
if ((mouseY >= integer && mouseY < integer + 11) &&
367+
mouseX >= x + width / 2F - MinecraftClient.getInstance().textRenderer.getWidth(s) / 2F &&
368+
mouseX <= x + width / 2F + MinecraftClient.getInstance().textRenderer.getWidth(s) / 2F) {
298369
handleTextClick(Style.EMPTY.withClickEvent(effects.get(Formatting.strip(s))));
299370
}
300371
});
@@ -316,5 +387,20 @@ public void render(MatrixStack matrices, int index, int y, int x, int entryWidth
316387
public boolean mouseClicked(double mouseX, double mouseY, int button) {
317388
return false;
318389
}
390+
391+
@Override
392+
public boolean changeFocus(boolean lookForwards) {
393+
return false;
394+
}
395+
396+
@Override
397+
public List<? extends Element> children() {
398+
return List.of();
399+
}
400+
401+
@Override
402+
public List<? extends Selectable> selectableChildren() {
403+
return List.of();
404+
}
319405
}
320406
}

0 commit comments

Comments
 (0)