Spaces:
Build error
Build error
File size: 6,793 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
package net.minecraft.client.gui.components;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.layouts.FrameLayout;
import net.minecraft.client.gui.layouts.LinearLayout;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class PopupScreen extends Screen {
private static final ResourceLocation BACKGROUND_SPRITE = ResourceLocation.withDefaultNamespace("popup/background");
private static final int SPACING = 12;
private static final int BG_BORDER_WITH_SPACING = 18;
private static final int BUTTON_SPACING = 6;
private static final int IMAGE_SIZE_X = 130;
private static final int IMAGE_SIZE_Y = 64;
private static final int POPUP_DEFAULT_WIDTH = 250;
private final Screen backgroundScreen;
@Nullable
private final ResourceLocation image;
private final Component message;
private final List<PopupScreen.ButtonOption> buttons;
@Nullable
private final Runnable onClose;
private final int contentWidth;
private final LinearLayout layout = LinearLayout.vertical();
PopupScreen(
Screen p_311716_,
int p_312972_,
@Nullable ResourceLocation p_312263_,
Component p_311243_,
Component p_313078_,
List<PopupScreen.ButtonOption> p_312924_,
@Nullable Runnable p_309530_
) {
super(p_311243_);
this.backgroundScreen = p_311716_;
this.image = p_312263_;
this.message = p_313078_;
this.buttons = p_312924_;
this.onClose = p_309530_;
this.contentWidth = p_312972_ - 36;
}
@Override
public void added() {
super.added();
this.backgroundScreen.clearFocus();
}
@Override
protected void init() {
this.backgroundScreen.init(this.minecraft, this.width, this.height);
this.layout.spacing(12).defaultCellSetting().alignHorizontallyCenter();
this.layout
.addChild(new MultiLineTextWidget(this.title.copy().withStyle(ChatFormatting.BOLD), this.font).setMaxWidth(this.contentWidth).setCentered(true));
if (this.image != null) {
this.layout.addChild(ImageWidget.texture(130, 64, this.image, 130, 64));
}
this.layout.addChild(new MultiLineTextWidget(this.message, this.font).setMaxWidth(this.contentWidth).setCentered(true));
this.layout.addChild(this.buildButtonRow());
this.layout.visitWidgets(p_325330_ -> {
AbstractWidget abstractwidget = this.addRenderableWidget(p_325330_);
});
this.repositionElements();
}
private LinearLayout buildButtonRow() {
int i = 6 * (this.buttons.size() - 1);
int j = Math.min((this.contentWidth - i) / this.buttons.size(), 150);
LinearLayout linearlayout = LinearLayout.horizontal();
linearlayout.spacing(6);
for (PopupScreen.ButtonOption popupscreen$buttonoption : this.buttons) {
linearlayout.addChild(
Button.builder(popupscreen$buttonoption.message(), p_310515_ -> popupscreen$buttonoption.action().accept(this)).width(j).build()
);
}
return linearlayout;
}
@Override
protected void repositionElements() {
this.backgroundScreen.resize(this.minecraft, this.width, this.height);
this.layout.arrangeElements();
FrameLayout.centerInRectangle(this.layout, this.getRectangle());
}
@Override
public void renderBackground(GuiGraphics p_312654_, int p_312824_, int p_310533_, float p_313128_) {
this.backgroundScreen.render(p_312654_, -1, -1, p_313128_);
p_312654_.flush();
RenderSystem.clear(256);
this.renderTransparentBackground(p_312654_);
p_312654_.blitSprite(
RenderType::guiTextured,
BACKGROUND_SPRITE,
this.layout.getX() - 18,
this.layout.getY() - 18,
this.layout.getWidth() + 36,
this.layout.getHeight() + 36
);
}
@Override
public Component getNarrationMessage() {
return CommonComponents.joinForNarration(this.title, this.message);
}
@Override
public void onClose() {
if (this.onClose != null) {
this.onClose.run();
}
this.minecraft.setScreen(this.backgroundScreen);
}
@OnlyIn(Dist.CLIENT)
public static class Builder {
private final Screen backgroundScreen;
private final Component title;
private Component message = CommonComponents.EMPTY;
private int width = 250;
@Nullable
private ResourceLocation image;
private final List<PopupScreen.ButtonOption> buttons = new ArrayList<>();
@Nullable
private Runnable onClose = null;
public Builder(Screen p_311941_, Component p_309447_) {
this.backgroundScreen = p_311941_;
this.title = p_309447_;
}
public PopupScreen.Builder setWidth(int p_311856_) {
this.width = p_311856_;
return this;
}
public PopupScreen.Builder setImage(ResourceLocation p_309878_) {
this.image = p_309878_;
return this;
}
public PopupScreen.Builder setMessage(Component p_309841_) {
this.message = p_309841_;
return this;
}
public PopupScreen.Builder addButton(Component p_309455_, Consumer<PopupScreen> p_311142_) {
this.buttons.add(new PopupScreen.ButtonOption(p_309455_, p_311142_));
return this;
}
public PopupScreen.Builder onClose(Runnable p_311998_) {
this.onClose = p_311998_;
return this;
}
public PopupScreen build() {
if (this.buttons.isEmpty()) {
throw new IllegalStateException("Popup must have at least one button");
} else {
return new PopupScreen(
this.backgroundScreen, this.width, this.image, this.title, this.message, List.copyOf(this.buttons), this.onClose
);
}
}
}
@OnlyIn(Dist.CLIENT)
static record ButtonOption(Component message, Consumer<PopupScreen> action) {
}
} |