Added class to help with simple GridLayouts
This commit is contained in:
parent
c621b0525e
commit
114574bff3
2 changed files with 140 additions and 56 deletions
122
src/main/java/ru/bclib/gui/GridLayout.java
Normal file
122
src/main/java/ru/bclib/gui/GridLayout.java
Normal file
|
@ -0,0 +1,122 @@
|
|||
package ru.bclib.gui;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.Button.OnPress;
|
||||
import net.minecraft.client.gui.components.MultiLineLabel;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class GridLayout {
|
||||
class LablePos {
|
||||
final MultiLineLabel label;
|
||||
final int top;
|
||||
|
||||
LablePos(MultiLineLabel label, int top){
|
||||
this.label = label;
|
||||
this.top = top;
|
||||
}
|
||||
}
|
||||
|
||||
class ButtonPos {
|
||||
final int height;
|
||||
final int width;
|
||||
final float alpha;
|
||||
final Component component;
|
||||
final Button.OnPress onPress;
|
||||
|
||||
ButtonPos(float alpha, int width, int height, Component component, OnPress onPress) {
|
||||
this.height = height;
|
||||
this.width = width;
|
||||
this.alpha = alpha;
|
||||
this.component = component;
|
||||
this.onPress = onPress;
|
||||
}
|
||||
}
|
||||
public final int width;
|
||||
@NotNull
|
||||
private final Font font;
|
||||
private final Consumer<Button> addButtonFunction;
|
||||
public final int topStart;
|
||||
private int top;
|
||||
private int currentRowHeight = 0;
|
||||
|
||||
final private List<LablePos> labels;
|
||||
final private List<ButtonPos> buttons;
|
||||
|
||||
public GridLayout(int topStart, int width, Font font, Consumer<Button> addButtonFunction){
|
||||
Objects.requireNonNull(font);
|
||||
this.topStart = topStart;
|
||||
top = topStart + 20;
|
||||
this.width = width;
|
||||
this.font = font;
|
||||
this.addButtonFunction = addButtonFunction;
|
||||
labels = new ArrayList<>(4);
|
||||
buttons = new ArrayList<>(8);
|
||||
}
|
||||
|
||||
public void addMessageRow(MultiLineLabel lb){
|
||||
final int LABEL_MARGIN_BOTTOM = 12;
|
||||
labels.add(new LablePos(lb, top));
|
||||
int promptLines = lb.getLineCount() + 1;
|
||||
int height = promptLines * 9;
|
||||
|
||||
currentRowHeight = height + LABEL_MARGIN_BOTTOM;;
|
||||
}
|
||||
|
||||
public void startRow(){
|
||||
this.endRow();
|
||||
top += currentRowHeight;
|
||||
currentRowHeight = 0;
|
||||
}
|
||||
|
||||
public void endRow(){
|
||||
final int BUTTON_SPACING = 10;
|
||||
int count = buttons.size();
|
||||
int rowWidth = buttons.stream().map(b -> b.width).reduce(0, (p, c) -> p+c) + (count-1) * BUTTON_SPACING;
|
||||
int left = (width-rowWidth)/2;
|
||||
|
||||
for (ButtonPos bp:buttons){
|
||||
Button customButton = new Button(left, top, bp.width, bp.height, bp.component, bp.onPress);
|
||||
customButton.setAlpha(bp.alpha);
|
||||
addButtonFunction.accept(customButton);
|
||||
|
||||
left += BUTTON_SPACING + bp.width;
|
||||
};
|
||||
buttons.clear();
|
||||
}
|
||||
public void addButton(int width, int height, Component component, Button.OnPress onPress){
|
||||
addButton(1.0f, width, height, component, onPress);
|
||||
}
|
||||
|
||||
public void addButton(int height, Component component, Button.OnPress onPress){
|
||||
addButton(1.0f, height, component, onPress);
|
||||
}
|
||||
|
||||
public void addButton(float alpha, int height, Component component, Button.OnPress onPress){
|
||||
final int BUTTON_PADDING = 12;
|
||||
int width = font.width(component.getVisualOrderText()) + 2*BUTTON_PADDING;
|
||||
addButton(alpha, width, height, component, onPress);
|
||||
}
|
||||
|
||||
public void addButton(float alpha, int width, int height, Component component, Button.OnPress onPress){
|
||||
final int BUTTON_MARGIN_BOTTOM = 6;
|
||||
|
||||
currentRowHeight = Math.max(currentRowHeight, height + BUTTON_MARGIN_BOTTOM);
|
||||
buttons.add(new ButtonPos(alpha, width, height, component, onPress));
|
||||
}
|
||||
|
||||
public void render(PoseStack poseStack){
|
||||
labels.forEach(lp -> {
|
||||
lp.label.renderCentered(poseStack, this.width / 2, lp.top);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,55 +1,19 @@
|
|||
package ru.bclib.gui.screens;
|
||||
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.Font;
|
||||
import net.minecraft.client.gui.components.Button;
|
||||
import net.minecraft.client.gui.components.MultiLineLabel;
|
||||
import net.minecraft.client.gui.screens.BackupConfirmScreen;
|
||||
import net.minecraft.client.gui.screens.Screen;
|
||||
import net.minecraft.client.renderer.GameRenderer;
|
||||
import net.minecraft.network.chat.CommonComponents;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.util.Mth;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.bclib.BCLib;
|
||||
|
||||
import java.util.Objects;
|
||||
class ColoredButton extends Button {
|
||||
private final float r;
|
||||
private final float g;
|
||||
private final float b;
|
||||
public ColoredButton(int x, int y, int width, int height, Component component, OnPress onPress, float r, float g, float b) {
|
||||
super(x, y, width, height, component, onPress);
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderButton(PoseStack poseStack, int i, int j, float f) {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
Font font = minecraft.font;
|
||||
RenderSystem.setShader(GameRenderer::getPositionTexShader);
|
||||
RenderSystem.setShaderTexture(0, WIDGETS_LOCATION);
|
||||
RenderSystem.setShaderColor(r, g, b, this.alpha);
|
||||
int k = this.getYImage(this.isHovered());
|
||||
RenderSystem.enableBlend();
|
||||
RenderSystem.defaultBlendFunc();
|
||||
RenderSystem.enableDepthTest();
|
||||
this.blit(poseStack, this.x, this.y, 0, 46 + k * 20, this.width / 2, this.height);
|
||||
this.blit(poseStack, this.x + this.width / 2, this.y, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);
|
||||
this.renderBg(poseStack, minecraft, i, j);
|
||||
int l = this.active ? 16777215 : 10526880;
|
||||
drawCenteredString(poseStack, font, this.getMessage(), this.x + this.width / 2, this.y + (this.height - 8) / 2, l | Mth.ceil(this.alpha * 255.0F) << 24);
|
||||
}
|
||||
}
|
||||
import ru.bclib.gui.GridLayout;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class ConfirmFixScreen extends Screen {
|
||||
|
@ -61,54 +25,52 @@ public class ConfirmFixScreen extends Screen {
|
|||
private final Component description;
|
||||
private MultiLineLabel message;
|
||||
protected int id;
|
||||
private GridLayout grid = null;
|
||||
|
||||
public ConfirmFixScreen(@Nullable Screen screen, BackupConfirmScreen.Listener listener) {
|
||||
super(new TranslatableComponent("bclib.datafixer.backupWarning.title"));
|
||||
this.message = MultiLineLabel.EMPTY;
|
||||
this.lastScreen = screen;
|
||||
this.listener = listener;
|
||||
|
||||
this.description = new TranslatableComponent("bclib.datafixer.backupWarning.message");
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
super.init();
|
||||
this.message = MultiLineLabel.create(this.font, this.description, this.width - 50);
|
||||
int promptLines = this.message.getLineCount() + 1;
|
||||
Objects.requireNonNull(this.font);
|
||||
int height = promptLines * 9;
|
||||
this.grid = new GridLayout(30, this.width, this.font, this::addRenderableWidget);
|
||||
|
||||
final int BUTTON_WIDTH = 150;
|
||||
final int BUTTON_SPACE = 10;
|
||||
final int BUTTON_HEIGHT = 20;
|
||||
|
||||
Button customButton = new Button((this.width -BUTTON_WIDTH)/2, 100 + height, BUTTON_WIDTH, BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.backup"), (button) -> {
|
||||
grid.addMessageRow(MultiLineLabel.create(this.font, this.description, this.width - 50));
|
||||
|
||||
grid.startRow();
|
||||
grid.addButton( BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.backup"), (button) -> {
|
||||
this.listener.proceed(true, true);
|
||||
});
|
||||
this.addRenderableWidget(customButton);
|
||||
|
||||
|
||||
customButton = new Button((this.width - 2*BUTTON_WIDTH - BUTTON_SPACE)/ 2 , 124 + height, BUTTON_WIDTH, BUTTON_HEIGHT, CommonComponents.GUI_CANCEL, (button) -> {
|
||||
grid.startRow();
|
||||
grid.addButton( BUTTON_HEIGHT, CommonComponents.GUI_CANCEL, (button) -> {
|
||||
this.minecraft.setScreen(this.lastScreen);
|
||||
});
|
||||
this.addRenderableWidget(customButton);
|
||||
|
||||
customButton = new Button((this.width - 2*BUTTON_WIDTH - BUTTON_SPACE)/ 2 + BUTTON_WIDTH+BUTTON_SPACE, 124 + height, BUTTON_WIDTH, BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.continue"), (button) -> {
|
||||
grid.addButton(0.5f, BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.continue"), (button) -> {
|
||||
this.listener.proceed(false, true);
|
||||
});
|
||||
customButton.setAlpha(0.5f);
|
||||
this.addRenderableWidget(customButton);
|
||||
|
||||
|
||||
customButton = new Button((this.width - BUTTON_WIDTH)/ 2, 148 + height, BUTTON_WIDTH, BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.nofixes"), (button) -> {
|
||||
grid.startRow();
|
||||
grid.addButton(0.5f, BUTTON_HEIGHT, new TranslatableComponent("bclib.datafixer.backupWarning.nofixes"), (button) -> {
|
||||
this.listener.proceed(false, false);
|
||||
});
|
||||
customButton.setAlpha(0.5f);
|
||||
this.addRenderableWidget(customButton);
|
||||
|
||||
grid.endRow();
|
||||
}
|
||||
|
||||
public void render(PoseStack poseStack, int i, int j, float f) {
|
||||
this.renderBackground(poseStack);
|
||||
drawCenteredString(poseStack, this.font, this.title, this.width / 2, 50, 16777215);
|
||||
this.message.renderCentered(poseStack, this.width / 2, 70);
|
||||
drawCenteredString(poseStack, this.font, this.title, grid.width / 2, grid.topStart, 16777215);
|
||||
if (grid!=null) grid.render(poseStack);
|
||||
super.render(poseStack, i, j, f);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue