| 1 | package net.minecraft.client.gui.screens.friends;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.Collection;
|
| 5 | import java.util.List;
|
| 6 | import java.util.UUID;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 11 | import net.minecraft.client.gui.components.AbstractContainerWidget;
|
| 12 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 13 | import net.minecraft.client.gui.components.Button;
|
| 14 | import net.minecraft.client.gui.components.PlayerFaceWidget;
|
| 15 | import net.minecraft.client.gui.components.StringWidget;
|
| 16 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 17 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 18 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 19 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 20 | import net.minecraft.client.gui.screens.social.PlayerSocialManager;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | abstract class AbstractFriendsEntryContainerWidget extends AbstractContainerWidget {
|
| 26 | static final int SPRITE_TEXTURE_SIZE = 18;
|
| 27 | static final int BUTTON_SIZE = 20;
|
| 28 | static final int FACE_SIZE = 24;
|
| 29 | static final int PADDING = 4;
|
| 30 | private static final int BACKGROUND_MARGIN = 4;
|
| 31 | protected final Minecraft minecraft;
|
| 32 | protected final FriendsOverlayScreen screen;
|
| 33 | protected final PlayerFaceWidget playerFaceWidget;
|
| 34 | protected final StringWidget nameWidget;
|
| 35 | protected final String playerName;
|
| 36 | protected final UUID playerId;
|
| 37 | protected final boolean showingStatus;
|
| 38 | private final List<AbstractWidget> children = new ArrayList<>();
|
| 39 |
|
| 40 | public AbstractFriendsEntryContainerWidget(
|
| 41 | final Minecraft minecraft,
|
| 42 | final FriendsOverlayScreen screen,
|
| 43 | final int x,
|
| 44 | final int y,
|
| 45 | final int width,
|
| 46 | final int height,
|
| 47 | final PlayerSocialManager.PlayerData playerData
|
| 48 | ) {
|
| 49 | this(minecraft, screen, x, y, width, height, playerData, false);
|
| 50 | }
|
| 51 |
|
| 52 | public AbstractFriendsEntryContainerWidget(
|
| 53 | final Minecraft minecraft,
|
| 54 | final FriendsOverlayScreen screen,
|
| 55 | final int x,
|
| 56 | final int y,
|
| 57 | final int width,
|
| 58 | final int height,
|
| 59 | final PlayerSocialManager.PlayerData playerData,
|
| 60 | final boolean showingStatus
|
| 61 | ) {
|
| 62 | super(x, y, width, height, Component.empty());
|
| 63 | this.minecraft = minecraft;
|
| 64 | this.screen = screen;
|
| 65 | this.playerName = playerData.name();
|
| 66 | this.playerId = playerData.id();
|
| 67 | this.playerFaceWidget = new PlayerFaceWidget(24, ResolvableProfile.createUnresolved(this.playerId));
|
| 68 | this.nameWidget = new StringWidget(Component.literal(this.playerName), minecraft.font);
|
| 69 | this.addChild(this.playerFaceWidget);
|
| 70 | this.addChild(this.nameWidget);
|
| 71 | this.showingStatus = showingStatus;
|
| 72 | }
|
| 73 |
|
| 74 | abstract void disable();
|
| 75 |
|
| 76 | UUID playerId() {
|
| 77 | return this.playerId;
|
| 78 | }
|
| 79 |
|
| 80 | protected abstract Component getEntryNarration();
|
| 81 |
|
| 82 | static Button.CreateNarration getSpriteIconNarration(final Component actionDescription) {
|
| 83 | return var1 -> Component.translatable("narrator.select", actionDescription);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 88 | output.add(NarratedElementType.TITLE, this.nameWidget.getMessage());
|
| 89 | if (this.getFocused() instanceof AbstractWidget focusedWidget) {
|
| 90 | focusedWidget.updateNarration(output.nest());
|
| 91 | } else {
|
| 92 | output.add(NarratedElementType.USAGE, this.getEntryNarration());
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public Collection<? extends NarratableEntry> getNarratables() {
|
| 98 | List<NarratableEntry> narratables = new ArrayList<>(this.children.size() + 1);
|
| 99 | narratables.addAll(this.children);
|
| 100 | narratables.add(this);
|
| 101 | return narratables;
|
| 102 | }
|
| 103 |
|
| 104 | protected final void addChild(final AbstractWidget child) {
|
| 105 | this.children.add(child);
|
| 106 | }
|
| 107 |
|
| 108 | protected final void removeChild(final AbstractWidget child) {
|
| 109 | if (this.children.remove(child) && this.getFocused() == child) {
|
| 110 | this.setFocused(null);
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public List<? extends GuiEventListener> children() {
|
| 116 | return this.children;
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected int contentHeight() {
|
| 121 | return this.height;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 126 | if (this.minecraft.options.highContrast().get()) {
|
| 127 | graphics.fill(this.getX() - 4, this.getY(), this.getX() + this.getWidth() + 4, this.getY() + this.getHeight(), -16777216);
|
| 128 | }
|
| 129 |
|
| 130 | this.playerFaceWidget.setPosition(this.getX(), this.getY() + (this.getHeight() - this.playerFaceWidget.getHeight()) / 2);
|
| 131 | this.playerFaceWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 132 | int nameY = this.getY() + this.getHeight() / (this.showingStatus ? 3 : 2) - this.nameWidget.getHeight() / 2;
|
| 133 | this.nameWidget.setPosition(this.playerFaceWidget.getRight() + 4, nameY);
|
| 134 | this.nameWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 135 | }
|
| 136 | }
|
| 137 |
|