diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridCell.java b/src/main/java/ru/bclib/gui/gridlayout/GridCell.java index 6b17ef7a..f3912879 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridCell.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridCell.java @@ -12,7 +12,7 @@ import java.util.function.Function; class GridCell extends GridCellDefinition { public final float height; Function componentPlacer; - final TriConsumer customRender; + TriConsumer customRender; GridCell(double width, double height, GridLayout.GridValueType widthType, Function componentPlacer, TriConsumer customRender) { super(width, widthType); diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridCustomRenderCell.java b/src/main/java/ru/bclib/gui/gridlayout/GridCustomRenderCell.java new file mode 100644 index 00000000..d79229ea --- /dev/null +++ b/src/main/java/ru/bclib/gui/gridlayout/GridCustomRenderCell.java @@ -0,0 +1,17 @@ +package ru.bclib.gui.gridlayout; + +import com.mojang.blaze3d.vertex.PoseStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import ru.bclib.gui.gridlayout.GridLayout.GridValueType; + + +@Environment(EnvType.CLIENT) +public abstract class GridCustomRenderCell extends GridCell{ + protected GridCustomRenderCell(double width, GridValueType widthType, double height) { + super(width, height, widthType, null, null); + this.customRender = this::onRender; + } + + public abstract void onRender(PoseStack poseStack, GridTransform transform, Object context); +} diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridLayout.java b/src/main/java/ru/bclib/gui/gridlayout/GridLayout.java index ae245dfe..27687f01 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridLayout.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridLayout.java @@ -86,6 +86,7 @@ public class GridLayout extends GridColumn { public static final int COLOR_RED = 0x00FF0000; public static final int COLOR_GREEN = 0x0000FF00; public static final int COLOR_BLUE = 0x000000FF; + public static final int COLOR_GRAY = 0x007F7F7F; public final GridScreen screen; public final int screenHeight; diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridMessageCell.java b/src/main/java/ru/bclib/gui/gridlayout/GridMessageCell.java index b3bd690f..8c3c053c 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridMessageCell.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridMessageCell.java @@ -11,41 +11,51 @@ import ru.bclib.gui.gridlayout.GridLayout.GridValueType; import java.util.List; @Environment(EnvType.CLIENT) -class GridMessageCell extends GridCell { +public class GridMessageCell extends GridCell { private final Font font; - private final Component text; - private MultiLineLabel label; + private Component text; + private MultiLineLabel lastLabel; + private GridTransform lastTransform; GridMessageCell(double width, GridValueType widthType, Alignment contentAlignment, Font font, Component text) { this(width, widthType, contentAlignment, font, text, GridLayout.COLOR_WHITE); } GridMessageCell(double width, GridValueType widthType, Alignment contentAlignment, Font font, Component text, int color) { - super(width, -1, widthType, null, (poseStack, transform, context) -> { - MultiLineLabel label = (MultiLineLabel) context; - if (contentAlignment == Alignment.CENTER) { - label.renderCentered(poseStack, transform.width / 2 + transform.left, transform.top, font.lineHeight, color); - } - else if (contentAlignment == Alignment.LEFT) { - label.renderLeftAligned(poseStack, transform.left, transform.top, font.lineHeight, color); - } - }); - + super(width, -1, widthType, null, null); this.font = font; this.text = text; + + customRender = (poseStack, transform, context) -> { + //MultiLineLabel label = (MultiLineLabel) context; + if (contentAlignment == Alignment.CENTER) { + lastLabel.renderCentered(poseStack, transform.width / 2 + transform.left, transform.top, font.lineHeight, color); + } + else if (contentAlignment == Alignment.LEFT) { + lastLabel.renderLeftAligned(poseStack, transform.left, transform.top, font.lineHeight, color); + } + }; + } + + public void setText(Component text){ + this.text = text; + if (lastTransform!=null) { + create(lastTransform); + } } private MultiLineLabel getLabel(GridTransform transform) { - return label; + return lastLabel; } protected void create(GridTransform transform) { - this.label = MultiLineLabel.create(font, text, transform.width); + this.lastTransform = transform; + this.lastLabel = MultiLineLabel.create(font, text, transform.width); } @Override protected GridElement buildElementAt(int left, int top, int width, List collector) { create(new GridTransform(left, top, width, 0)); - int promptLines = this.label.getLineCount() + 1; + int promptLines = this.lastLabel.getLineCount() + 1; int height = promptLines * 9; return new GridElement(left, top, width, height, this::getLabel, customRender); diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridRow.java b/src/main/java/ru/bclib/gui/gridlayout/GridRow.java index c0609029..90ba5728 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridRow.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridRow.java @@ -122,6 +122,10 @@ public class GridRow extends GridContainer { return cell; } + public GridCustomRenderCell addCustomRender(GridCustomRenderCell cell) { + this.cells.add(cell); + return cell; + } public GridCell addImage(ResourceLocation location, int width, int height) { return addImage(location, 1.0f, width, height); @@ -170,48 +174,48 @@ public class GridRow extends GridContainer { } - public GridCell addMessage(Component text, Font font, Alignment contentAlignment) { + public GridMessageCell addMessage(Component text, Font font, Alignment contentAlignment) { return addMessage(text, font, GridLayout.COLOR_WHITE, contentAlignment); } - public GridCell addMessage(Component text, Font font, int color, Alignment contentAlignment) { + public GridMessageCell addMessage(Component text, Font font, int color, Alignment contentAlignment) { return addMessage(text, 1.0, GridLayout.GridValueType.PERCENTAGE, font, color, contentAlignment); } - public GridCell addMessage(Component text, double width, GridValueType widthType, Font font, Alignment contentAlignment) { + public GridMessageCell addMessage(Component text, double width, GridValueType widthType, Font font, Alignment contentAlignment) { return addMessage(text, width, widthType, font, GridLayout.COLOR_WHITE, contentAlignment); } - public GridCell addMessage(Component text, double width, GridValueType widthType, Font font, int color, Alignment contentAlignment) { - GridCell cell = new GridMessageCell(width, widthType, Alignment.LEFT, font, text, color); + public GridMessageCell addMessage(Component text, double width, GridValueType widthType, Font font, int color, Alignment contentAlignment) { + GridMessageCell cell = new GridMessageCell(width, widthType, contentAlignment, font, text, color); this.cells.add(cell); return cell; } - public GridCell addString(Component text, GridScreen parent) { + public GridStringCell addString(Component text, GridScreen parent) { return this.addString(text, GridLayout.COLOR_WHITE, parent); } - public GridCell addString(Component text, int color, GridScreen parent) { - final int width = parent.getFont() - .width(text.getVisualOrderText()); + + public GridStringCell addString(Component text, int color, GridScreen parent) { + final int width = parent.getWidth(text); return this.addString(text, width, GridValueType.CONSTANT, GridLayout.COLOR_WHITE, Alignment.CENTER, parent); } - public GridCell addString(Component text, Alignment contentAlignment, GridScreen parent) { + public GridStringCell addString(Component text, Alignment contentAlignment, GridScreen parent) { return this.addString(text, GridLayout.COLOR_WHITE, contentAlignment, parent); } - public GridCell addString(Component text, int color, Alignment contentAlignment, GridScreen parent) { + public GridStringCell addString(Component text, int color, Alignment contentAlignment, GridScreen parent) { return this.addString(text, 1.0, GridLayout.GridValueType.PERCENTAGE, color, contentAlignment, parent); } - public GridCell addString(Component text, double width, GridValueType widthType, Alignment contentAlignment, GridScreen parent) { + public GridStringCell addString(Component text, double width, GridValueType widthType, Alignment contentAlignment, GridScreen parent) { return addString(text, width, widthType, GridLayout.COLOR_WHITE, contentAlignment, parent); } - public GridCell addString(Component text, double width, GridValueType widthType, int color, Alignment contentAlignment, GridScreen parent) { - GridCell cell = new GridStringCell(width, widthType, parent.getFont().lineHeight, contentAlignment, parent, text, color); + public GridStringCell addString(Component text, double width, GridValueType widthType, int color, Alignment contentAlignment, GridScreen parent) { + GridStringCell cell = new GridStringCell(width, widthType, parent.getFont().lineHeight, contentAlignment, parent, text, color); this.cells.add(cell); return cell; } diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridScreen.java b/src/main/java/ru/bclib/gui/gridlayout/GridScreen.java index 7dbbffd8..4653128e 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridScreen.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridScreen.java @@ -96,4 +96,12 @@ public abstract class GridScreen extends Screen { super.render(poseStack, i, j, f); } + + public static int getWidth(Component text, Font font) { + return font.width(text.getVisualOrderText()); + } + + public int getWidth(Component text) { + return getWidth(text, getFont()); + } } diff --git a/src/main/java/ru/bclib/gui/gridlayout/GridStringCell.java b/src/main/java/ru/bclib/gui/gridlayout/GridStringCell.java index 3d984fb0..3d4a4fe6 100644 --- a/src/main/java/ru/bclib/gui/gridlayout/GridStringCell.java +++ b/src/main/java/ru/bclib/gui/gridlayout/GridStringCell.java @@ -7,19 +7,26 @@ import ru.bclib.gui.gridlayout.GridLayout.Alignment; import ru.bclib.gui.gridlayout.GridLayout.GridValueType; @Environment(EnvType.CLIENT) -class GridStringCell extends GridCell { +public class GridStringCell extends GridCell { + private Component text; GridStringCell(double width, GridValueType widthType, int height, Alignment contentAlignment, GridScreen parent, Component text) { this(width, widthType, height, contentAlignment, parent, text, GridLayout.COLOR_WHITE); } GridStringCell(double width, GridValueType widthType, int height, Alignment contentAlignment, GridScreen parent, Component text, int color) { - super(width, height, widthType, null, (poseStack, transform, context) -> { + super(width, height, widthType, null, null); + this.text = text; + this.customRender = (poseStack, transform, context) -> { if (contentAlignment == Alignment.CENTER) { - parent.drawCenteredString(poseStack, parent.getFont(), text, transform.width / 2 + transform.left, transform.top, color); + parent.drawCenteredString(poseStack, parent.getFont(), this.text, transform.width / 2 + transform.left, transform.top, color); } else if (contentAlignment == Alignment.LEFT) { - parent.drawString(poseStack, parent.getFont(), text, transform.left, transform.top, color); + parent.drawString(poseStack, parent.getFont(), this.text, transform.left, transform.top, color); } - }); + }; + } + + public void setText(Component newText){ + this.text = newText; } } diff --git a/src/main/java/ru/bclib/gui/screens/ProgressScreen.java b/src/main/java/ru/bclib/gui/screens/ProgressScreen.java new file mode 100644 index 00000000..841d1569 --- /dev/null +++ b/src/main/java/ru/bclib/gui/screens/ProgressScreen.java @@ -0,0 +1,186 @@ +package ru.bclib.gui.screens; + +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.gui.GuiComponent; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.renderer.GameRenderer; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.ProgressListener; +import org.jetbrains.annotations.Nullable; +import ru.bclib.BCLib; +import ru.bclib.gui.gridlayout.GridColumn; +import ru.bclib.gui.gridlayout.GridCustomRenderCell; +import ru.bclib.gui.gridlayout.GridLayout; +import ru.bclib.gui.gridlayout.GridLayout.Alignment; +import ru.bclib.gui.gridlayout.GridLayout.GridValueType; +import ru.bclib.gui.gridlayout.GridLayout.VerticalAlignment; +import ru.bclib.gui.gridlayout.GridMessageCell; +import ru.bclib.gui.gridlayout.GridRow; +import ru.bclib.gui.gridlayout.GridScreen; +import ru.bclib.gui.gridlayout.GridStringCell; +import ru.bclib.gui.gridlayout.GridTransform; + +class ProgressLogoRender extends GridCustomRenderCell { + public static final int SIZE = 64; + public static final int LOGO_SIZE = 512; + public static final int PIXELATED_SIZE = 512; + float percentage = 0; + double time = 0; + protected ProgressLogoRender() { + super(SIZE, GridValueType.CONSTANT, SIZE); + } + + @Override + public void onRender(PoseStack poseStack, GridTransform transform, Object context) { + time += 0.03; + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); + RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0f); + + final float fScale = (float)(0.3*((Math.sin(time)+1.0)*0.5) + 0.7); + int height = (int)(transform.height*fScale); + int width = (int)(transform.width*fScale); + width -= ((transform.width-width)%2); + height -= ((transform.height-height)%2); + final int yOffset = (transform.height-height)/2; + final int xOffset = (transform.width-width)/2; + + final int yBarLocal = (int)(transform.height*percentage); + final int yBar = transform.top + yBarLocal; + + final float relativeY = ((float)(yBarLocal - yOffset)/height); + + final int uvTopLogo = (int)(relativeY * LOGO_SIZE); + final int uvTopPixelated = (int)(relativeY * PIXELATED_SIZE); + + if (uvTopLogo>0) { + RenderSystem.setShaderTexture(0, BCLibScreen.BCLIB_LOGO_LOCATION); + GuiComponent.blit(poseStack, + xOffset + transform.left, + yOffset + transform.top, + width, + yBarLocal - yOffset, + 0, 0, LOGO_SIZE, uvTopLogo, + LOGO_SIZE, LOGO_SIZE + ); + } + + if (uvTopPixelated0 && percentage<1.0){ + GuiComponent.fill(poseStack, + transform.left, + yBar, + transform.left+transform.width, + yBar+1, + 0x3FFFFFFF + ); + } + } +} + +public class ProgressScreen extends GridScreen implements ProgressListener { + static final ResourceLocation BCLIB_LOGO_PIXELATED_LOCATION = new ResourceLocation(BCLib.MOD_ID, "iconpixelated.png"); + public ProgressScreen(@Nullable Screen parent, Component title, Component description) { + super(parent, title, 20, false); + this.description = description; + } + + Component description; + private Component stageComponent; + private GridMessageCell stage; + private GridStringCell progress; + private ProgressLogoRender progressImage; + private int currentProgress = 0; + public boolean shouldCloseOnEsc() { + return false; + } + + public Component getProgressComponent(){ + return getProgressComponent(currentProgress); + } + + private Component getProgressComponent(int pg){ + return new TranslatableComponent("title.bclib.progress").append(": " + pg + "%"); + } + @Override + protected void initLayout() { + grid.addSpacerRow(); + + GridRow row = grid.addRow(VerticalAlignment.CENTER); + row.addFiller(); + progressImage = new ProgressLogoRender(); + progressImage.percentage = currentProgress / 100.0f; + row.addCustomRender(progressImage); + row.addSpacer(); + + int textWidth = Math.max(getWidth(description), getWidth(getProgressComponent(100))); + GridColumn textCol = row.addColumn(textWidth, GridValueType.CONSTANT); + textCol.addRow().addString(description, this); + textCol.addSpacerRow(); + progress = textCol.addRow().addString(getProgressComponent(), GridLayout.COLOR_GRAY, Alignment.LEFT, this); + + row.addFiller(); + + grid.addSpacerRow(20); + row = grid.addRow(); + stage = row.addMessage(stageComponent!=null?stageComponent:new TextComponent(""), font, Alignment.CENTER); + } + + @Override + public void progressStartNoAbort(Component component) { + this.progressStage(component); + } + + @Override + public void progressStart(Component component) { + this.progressStage(component); + this.progressStagePercentage(30); + } + + @Override + public void progressStage(Component component) { + stageComponent = component; + if (stage!=null) stage.setText(component); + } + + @Override + public void progressStagePercentage(int i) { + if (i!=currentProgress) { + currentProgress = i; + if (progressImage!=null) progressImage.percentage = currentProgress / 100.0f; + BCLib.LOGGER.info(" -> progress: " + i + "%"); + if (progress!=null) progress.setText(getProgressComponent()); + } + } + + @Override + public void stop() { + + } + + double time = 0; + @Override + public void render(PoseStack poseStack, int i, int j, float f) { + //time += 0.05; + //progressStagePercentage(((int)time)%100); + //progressStagePercentage(1); + super.render(poseStack, i, j, f); + } +} diff --git a/src/main/resources/assets/bclib/iconpixelated.png b/src/main/resources/assets/bclib/iconpixelated.png new file mode 100644 index 00000000..056ed30a Binary files /dev/null and b/src/main/resources/assets/bclib/iconpixelated.png differ diff --git a/src/main/resources/assets/bclib/lang/en_us.json b/src/main/resources/assets/bclib/lang/en_us.json index 36e099a6..c63eb74b 100644 --- a/src/main/resources/assets/bclib/lang/en_us.json +++ b/src/main/resources/assets/bclib/lang/en_us.json @@ -17,6 +17,10 @@ "message.bclib.confirmrestart": "The requested content was synchronized. You need to restart Minecraft now.", "title.link.bclib.discord": "Discord", "title.bclib.modmenu.main": "BCLib Settings", + "title.bclib.progress": "Progress", + "title.bclib.filesync.progress": "File Transfer", + "message.bclib.filesync.progress": "Syncing File-Content with Server", + "message.bclib.filesync.progress.stage.empty": "", "title.config.bclib.client.auto_sync.enabled": "Enable Auto-Sync", "title.config.bclib.client.auto_sync.acceptConfigs": "Accept incoming Confog Files",