| 1 | package net.minecraft.client.gui.screens.packs;
|
| 2 |
|
| 3 | import java.util.stream.Stream;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.ChatFormatting;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 11 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 12 | import net.minecraft.client.gui.components.SelectableEntry;
|
| 13 | import net.minecraft.client.gui.components.StringWidget;
|
| 14 | import net.minecraft.client.gui.screens.ConfirmScreen;
|
| 15 | import net.minecraft.client.input.KeyEvent;
|
| 16 | import net.minecraft.client.input.MouseButtonEvent;
|
| 17 | import net.minecraft.client.renderer.RenderPipelines;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.chat.ComponentUtils;
|
| 20 | import net.minecraft.network.chat.Style;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.server.packs.repository.PackCompatibility;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class TransferableSelectionList extends ObjectSelectionList<TransferableSelectionList.Entry> {
|
| 27 | private static final Identifier SELECT_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("transferable_list/select_highlighted");
|
| 28 | private static final Identifier SELECT_SPRITE = Identifier.withDefaultNamespace("transferable_list/select");
|
| 29 | private static final Identifier UNSELECT_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("transferable_list/unselect_highlighted");
|
| 30 | private static final Identifier UNSELECT_SPRITE = Identifier.withDefaultNamespace("transferable_list/unselect");
|
| 31 | private static final Identifier MOVE_UP_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("transferable_list/move_up_highlighted");
|
| 32 | private static final Identifier MOVE_UP_SPRITE = Identifier.withDefaultNamespace("transferable_list/move_up");
|
| 33 | private static final Identifier MOVE_DOWN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("transferable_list/move_down_highlighted");
|
| 34 | private static final Identifier MOVE_DOWN_SPRITE = Identifier.withDefaultNamespace("transferable_list/move_down");
|
| 35 | private static final Component INCOMPATIBLE_TITLE = Component.translatable("pack.incompatible");
|
| 36 | private static final Component INCOMPATIBLE_CONFIRM_TITLE = Component.translatable("pack.incompatible.confirm.title");
|
| 37 | private static final int ENTRY_PADDING = 2;
|
| 38 | private final Component title;
|
| 39 | private final PackSelectionScreen screen;
|
| 40 |
|
| 41 | public TransferableSelectionList(final Minecraft minecraft, final PackSelectionScreen screen, final int width, final int height, final Component title) {
|
| 42 | super(minecraft, width, height, 33, 36);
|
| 43 | this.screen = screen;
|
| 44 | this.title = title;
|
| 45 | this.centerListVertically = false;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public int getRowWidth() {
|
| 50 | return this.width - 4;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected int scrollBarX() {
|
| 55 | return this.getRight() - this.scrollbarWidth();
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public boolean keyPressed(final KeyEvent event) {
|
| 60 | return this.getSelected() != null ? this.getSelected().keyPressed(event) : super.keyPressed(event);
|
| 61 | }
|
| 62 |
|
| 63 | public void updateList(final Stream<PackSelectionModel.Entry> entries, final PackSelectionModel.@Nullable EntryBase transferredEntry) {
|
| 64 | this.clearEntries();
|
| 65 | Component header = Component.empty().append(this.title).withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 66 | this.addEntry(new TransferableSelectionList.HeaderEntry(this.minecraft.font, header), (int)(9.0F * 1.5F));
|
| 67 | this.setSelected(null);
|
| 68 | entries.forEach(e -> {
|
| 69 | TransferableSelectionList.PackEntry entry = new TransferableSelectionList.PackEntry(this.minecraft, this, e);
|
| 70 | this.addEntry(entry);
|
| 71 | if (transferredEntry != null && transferredEntry.getId().equals(e.getId())) {
|
| 72 | this.screen.setFocused(this);
|
| 73 | this.setFocused(entry);
|
| 74 | }
|
| 75 | });
|
| 76 | this.refreshScrollAmount();
|
| 77 | }
|
| 78 |
|
| 79 | @Environment(EnvType.CLIENT)
|
| 80 | public abstract class Entry extends ObjectSelectionList.Entry<TransferableSelectionList.Entry> {
|
| 81 | @Override
|
| 82 | public int getWidth() {
|
| 83 | return super.getWidth() - (TransferableSelectionList.this.scrollable() ? TransferableSelectionList.this.scrollbarWidth() : 0);
|
| 84 | }
|
| 85 |
|
| 86 | public abstract String getPackId();
|
| 87 | }
|
| 88 |
|
| 89 | @Environment(EnvType.CLIENT)
|
| 90 | public class HeaderEntry extends TransferableSelectionList.Entry {
|
| 91 | private final Font font;
|
| 92 | private final Component text;
|
| 93 |
|
| 94 | public HeaderEntry(final Font font, final Component text) {
|
| 95 | this.font = font;
|
| 96 | this.text = text;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 101 | graphics.centeredText(this.font, this.text, this.getX() + this.getWidth() / 2, this.getContentYMiddle() - 9 / 2, -1);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public Component getNarration() {
|
| 106 | return this.text;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public String getPackId() {
|
| 111 | return "";
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | @Environment(EnvType.CLIENT)
|
| 116 | public class PackEntry extends TransferableSelectionList.Entry implements SelectableEntry {
|
| 117 | private static final int MAX_DESCRIPTION_WIDTH_PIXELS = 157;
|
| 118 | public static final int ICON_SIZE = 32;
|
| 119 | private final TransferableSelectionList parent;
|
| 120 | protected final Minecraft minecraft;
|
| 121 | private final PackSelectionModel.Entry pack;
|
| 122 | private final StringWidget nameWidget;
|
| 123 | private final MultiLineTextWidget descriptionWidget;
|
| 124 |
|
| 125 | public PackEntry(final Minecraft minecraft, final TransferableSelectionList parent, final PackSelectionModel.Entry pack) {
|
| 126 | this.minecraft = minecraft;
|
| 127 | this.pack = pack;
|
| 128 | this.parent = parent;
|
| 129 | this.nameWidget = new StringWidget(pack.getTitle(), minecraft.font);
|
| 130 | this.descriptionWidget = new MultiLineTextWidget(ComponentUtils.mergeStyles(pack.getExtendedDescription(), Style.EMPTY.withColor(-8355712)), minecraft.font);
|
| 131 | this.descriptionWidget.setMaxRows(2);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public Component getNarration() {
|
| 136 | return Component.translatable("narrator.select", this.pack.getTitle());
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 141 | PackCompatibility compatibility = this.pack.getCompatibility();
|
| 142 | if (!compatibility.isCompatible()) {
|
| 143 | int x0 = this.getContentX() - 1;
|
| 144 | int y0 = this.getContentY() - 1;
|
| 145 | int x1 = this.getContentRight() + 1;
|
| 146 | int y1 = this.getContentBottom() + 1;
|
| 147 | graphics.fill(x0, y0, x1, y1, -8978432);
|
| 148 | }
|
| 149 |
|
| 150 | graphics.blit(RenderPipelines.GUI_TEXTURED, this.pack.getIconTexture(), this.getContentX(), this.getContentY(), 0.0F, 0.0F, 32, 32, 32, 32);
|
| 151 | if (!this.nameWidget.getMessage().equals(this.pack.getTitle())) {
|
| 152 | this.nameWidget.setMessage(this.pack.getTitle());
|
| 153 | }
|
| 154 |
|
| 155 | if (!this.descriptionWidget.getMessage().getContents().equals(this.pack.getExtendedDescription().getContents())) {
|
| 156 | this.descriptionWidget.setMessage(ComponentUtils.mergeStyles(this.pack.getExtendedDescription(), Style.EMPTY.withColor(-8355712)));
|
| 157 | }
|
| 158 |
|
| 159 | if (this.showHoverOverlay() && (hovered || this.parent.getSelected() == this && this.parent.isFocused())) {
|
| 160 | graphics.fill(this.getContentX(), this.getContentY(), this.getContentX() + 32, this.getContentY() + 32, -1601138544);
|
| 161 | int relX = mouseX - this.getContentX();
|
| 162 | int relY = mouseY - this.getContentY();
|
| 163 | if (!this.pack.getCompatibility().isCompatible()) {
|
| 164 | this.nameWidget.setMessage(TransferableSelectionList.INCOMPATIBLE_TITLE);
|
| 165 | this.descriptionWidget.setMessage(this.pack.getCompatibility().getDescription());
|
| 166 | }
|
| 167 |
|
| 168 | if (this.pack.canSelect()) {
|
| 169 | if (this.mouseOverIcon(relX, relY, 32)) {
|
| 170 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.SELECT_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 171 | TransferableSelectionList.this.handleCursor(graphics);
|
| 172 | } else {
|
| 173 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.SELECT_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 174 | }
|
| 175 | } else {
|
| 176 | if (this.pack.canUnselect()) {
|
| 177 | if (this.mouseOverLeftHalf(relX, relY, 32)) {
|
| 178 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.UNSELECT_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 179 | TransferableSelectionList.this.handleCursor(graphics);
|
| 180 | } else {
|
| 181 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.UNSELECT_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | if (this.pack.canMoveUp()) {
|
| 186 | if (this.mouseOverTopRightQuarter(relX, relY, 32)) {
|
| 187 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.MOVE_UP_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 188 | TransferableSelectionList.this.handleCursor(graphics);
|
| 189 | } else {
|
| 190 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.MOVE_UP_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | if (this.pack.canMoveDown()) {
|
| 195 | if (this.mouseOverBottomRightQuarter(relX, relY, 32)) {
|
| 196 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.MOVE_DOWN_HIGHLIGHTED_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 197 | TransferableSelectionList.this.handleCursor(graphics);
|
| 198 | } else {
|
| 199 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TransferableSelectionList.MOVE_DOWN_SPRITE, this.getContentX(), this.getContentY(), 32, 32);
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | this.nameWidget.setMaxWidth(157 - (TransferableSelectionList.this.scrollable() ? 6 : 0));
|
| 206 | this.nameWidget.setPosition(this.getContentX() + 32 + 2, this.getContentY() + 1);
|
| 207 | this.nameWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 208 | this.descriptionWidget.setMaxWidth(157 - (TransferableSelectionList.this.scrollable() ? 6 : 0));
|
| 209 | this.descriptionWidget.setPosition(this.getContentX() + 32 + 2, this.getContentY() + 12);
|
| 210 | this.descriptionWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 215 | if (this.showHoverOverlay()) {
|
| 216 | int relX = (int)event.x() - this.getContentX();
|
| 217 | int relY = (int)event.y() - this.getContentY();
|
| 218 | if (this.pack.canSelect() && this.mouseOverIcon(relX, relY, 32)) {
|
| 219 | this.handlePackSelection();
|
| 220 | return true;
|
| 221 | }
|
| 222 |
|
| 223 | if (this.pack.canUnselect() && this.mouseOverLeftHalf(relX, relY, 32)) {
|
| 224 | this.pack.unselect();
|
| 225 | return true;
|
| 226 | }
|
| 227 |
|
| 228 | if (this.pack.canMoveUp() && this.mouseOverTopRightQuarter(relX, relY, 32)) {
|
| 229 | this.pack.moveUp();
|
| 230 | return true;
|
| 231 | }
|
| 232 |
|
| 233 | if (this.pack.canMoveDown() && this.mouseOverBottomRightQuarter(relX, relY, 32)) {
|
| 234 | this.pack.moveDown();
|
| 235 | return true;
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | return super.mouseClicked(event, doubleClick);
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | public boolean keyPressed(final KeyEvent event) {
|
| 244 | if (event.isConfirmation()) {
|
| 245 | this.keyboardSelection();
|
| 246 | return true;
|
| 247 | }
|
| 248 |
|
| 249 | if (event.hasShiftDown()) {
|
| 250 | if (event.isUp()) {
|
| 251 | this.keyboardMoveUp();
|
| 252 | return true;
|
| 253 | }
|
| 254 |
|
| 255 | if (event.isDown()) {
|
| 256 | this.keyboardMoveDown();
|
| 257 | return true;
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | return super.keyPressed(event);
|
| 262 | }
|
| 263 |
|
| 264 | private boolean showHoverOverlay() {
|
| 265 | return !this.pack.isFixedPosition() || !this.pack.isRequired();
|
| 266 | }
|
| 267 |
|
| 268 | public void keyboardSelection() {
|
| 269 | if (this.pack.canSelect()) {
|
| 270 | this.handlePackSelection();
|
| 271 | } else if (this.pack.canUnselect()) {
|
| 272 | this.pack.unselect();
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | private void keyboardMoveUp() {
|
| 277 | if (this.pack.canMoveUp()) {
|
| 278 | this.pack.moveUp();
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | private void keyboardMoveDown() {
|
| 283 | if (this.pack.canMoveDown()) {
|
| 284 | this.pack.moveDown();
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | private void handlePackSelection() {
|
| 289 | if (this.pack.getCompatibility().isCompatible()) {
|
| 290 | this.pack.select();
|
| 291 | } else {
|
| 292 | Component reason = this.pack.getCompatibility().getConfirmation();
|
| 293 | this.minecraft.gui.setScreen(new ConfirmScreen(result -> {
|
| 294 | this.minecraft.gui.setScreen(this.parent.screen);
|
| 295 | if (result) {
|
| 296 | this.pack.select();
|
| 297 | }
|
| 298 | }, TransferableSelectionList.INCOMPATIBLE_CONFIRM_TITLE, reason));
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public String getPackId() {
|
| 304 | return this.pack.getId();
|
| 305 | }
|
| 306 |
|
| 307 | @Override
|
| 308 | public boolean shouldTakeFocusAfterInteraction() {
|
| 309 | return TransferableSelectionList.this.children().stream().anyMatch(entry -> entry.getPackId().equals(this.getPackId()));
|
| 310 | }
|
| 311 | }
|
| 312 | }
|
| 313 |
|