Skip to content

Commit

Permalink
Make add friends screen scrollable on 1.21.4+
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Dec 3, 2024
1 parent 531060e commit 14ae1f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ protected void init() {
.build()
);

maxFriends = (cancelY - 90) / 24;
//#if MC >= 1.21.4
maxFriends = 25;
//#else
//$$ maxFriends = (cancelY - 94) / 24;
//#endif

userList = addRenderableWidget(new UserListWidget(
font,
widgetsX, 90, widgetsWidth, cancelY - 90,
widgetsX, 90, widgetsWidth, cancelY - 94,
this::getActions,
userList
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.gaming32.worldhost.gui.widget;

import com.google.common.collect.Lists;
import com.mojang.blaze3d.systems.RenderSystem;
import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.gui.screen.WorldHostScreen;
Expand Down Expand Up @@ -46,7 +47,8 @@ public final class UserListWidget
//#endif
{
private final List<UserInfo> users = new ArrayList<>();
private final List<Button> actionButtons = new ArrayList<>();
private final List<ActionButtonWrapper> actionButtons = new ArrayList<>();
private final List<? extends GuiEventListener> children = Lists.transform(actionButtons, ActionButtonWrapper::button);
private final Font font;
private final Function<FriendListFriend, List<Action>> getApplicableActions;

Expand Down Expand Up @@ -91,6 +93,13 @@ public void renderWidget(
//#endif
int mouseX, int mouseY, float partialTick
) {
context.pose().pushPose();

//#if MC >= 1.21.4
context.enableScissor(getX(), getY(), getX() + width, getY() + height);
context.pose().translate(0, -scrollAmount(), 0);
//#endif

final int textYOffset = 10 - font.lineHeight / 2;
final int x = getX();
int y = getY();
Expand Down Expand Up @@ -120,9 +129,25 @@ public void renderWidget(
}
y += 24;
}
for (final Button button : actionButtons) {
button.render(context, mouseX, mouseY, partialTick);
context.pose().popPose();

for (final var button : actionButtons) {
var buttonX = button.baseX;
var buttonY = button.baseY;
//#if MC >= 1.21.4
if (scrollbarVisible()) {
buttonX -= 10;
}
buttonY -= (int)scrollAmount();
//#endif
button.button.setPosition(buttonX, buttonY);
button.button.render(context, mouseX, mouseY, partialTick);
}

//#if MC >= 1.21.4
context.disableScissor();
renderScrollbar(context);
//#endif
}

//#if MC >= 1.19.4
Expand Down Expand Up @@ -169,13 +194,13 @@ private void addButtons(int fromI) {
final UserInfo user = users.get(i);
int x = getRight() - 24 * user.actions.size() + 4;
for (final Action action : user.actions) {
actionButtons.add(
actionButtons.add(new ActionButtonWrapper(
WorldHostScreen.button(action.text, b -> action.apply.run())
.tooltip(action.tooltip)
.pos(x, y)
.size(20, 20)
.build()
);
.build(),
x, y
));
x += 24;
}
y += 24;
Expand All @@ -192,7 +217,7 @@ public int getVisibleCount() {

@Override
public @NotNull List<? extends GuiEventListener> children() {
return actionButtons;
return children;
}

//#if MC >= 1.21.4
Expand Down Expand Up @@ -312,4 +337,7 @@ public Action(Component text, Runnable apply) {
this(text, null, apply);
}
}

public record ActionButtonWrapper(Button button, int baseX, int baseY) {
}
}
2 changes: 1 addition & 1 deletion version.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ dependencies {
})

when {
isFabric -> modImplementation("net.fabricmc:fabric-loader:0.16.7")
isFabric -> modImplementation("net.fabricmc:fabric-loader:0.16.9")
isForge ->
when (mcVersion) {
1_20_01 -> "47.1.3"
Expand Down

0 comments on commit 14ae1f7

Please sign in to comment.