1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-11-21 15:54:47 +00:00

printout renderer, terminal, tile printer, fake player, guicomputer, fixed width font renderer

This commit is contained in:
Devan-Kerman
2020-08-29 18:47:47 -05:00
parent a4830aff86
commit 2ea816b78b
6 changed files with 421 additions and 248 deletions

View File

@@ -9,20 +9,23 @@ package dan200.computercraft.client.render;
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.GlStateManager.DestFactor;
import com.mojang.blaze3d.platform.GlStateManager.SourceFactor;
import dan200.computercraft.client.gui.FixedWidthFontRenderer;
import dan200.computercraft.core.terminal.TextBuffer;
import dan200.computercraft.shared.util.Palette;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.RenderPhase;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Matrix4f;
@SuppressWarnings ({
"LocalVariableDeclarationSideOnly",
"MethodCallSideOnly"
})
public final class PrintoutRenderer {
/**
* Width of a page
@@ -45,7 +48,7 @@ public final class PrintoutRenderer {
*/
public static final int COVER_SIZE = 12;
private static final Identifier BG = new Identifier("computercraft", "textures/gui/printout.png");
private static final double BG_SIZE = 256.0;
private static final float BG_SIZE = 256.0f;
/**
* Width of the extra page texture
*/
@@ -55,125 +58,120 @@ public final class PrintoutRenderer {
private PrintoutRenderer() {}
public static void drawText(int x, int y, int start, TextBuffer[] text, TextBuffer[] colours) {
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
for (int line = 0; line < LINES_PER_PAGE && line < text.length; line++) {
fontRenderer.drawString(text[start + line], x, y + line * FONT_HEIGHT, colours[start + line], null, 0, 0, false, Palette.DEFAULT);
public static void drawText(Matrix4f transform, VertexConsumerProvider renderer, int x, int y, int start, TextBuffer[] text, TextBuffer[] colours) {
VertexConsumer buffer = renderer.getBuffer( FixedWidthFontRenderer.TYPE );
for( int line = 0; line < LINES_PER_PAGE && line < text.length; line++ )
{
FixedWidthFontRenderer.drawString( transform, buffer,
x, y + line * FONT_HEIGHT, text[start + line], colours[start + line], null, Palette.DEFAULT,
false, 0, 0
);
}
}
public static void drawText(int x, int y, int start, String[] text, String[] colours) {
GlStateManager.color4f(1.0f, 1.0f, 1.0f, 1.0f);
GlStateManager.enableBlend();
GlStateManager.enableTexture();
GlStateManager.blendFuncSeparate(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO);
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
for (int line = 0; line < LINES_PER_PAGE && line < text.length; line++) {
fontRenderer.drawString(new TextBuffer(text[start + line]),
x,
y + line * FONT_HEIGHT,
new TextBuffer(colours[start + line]),
null,
0,
0,
false,
Palette.DEFAULT);
public static void drawText(Matrix4f transform, VertexConsumerProvider renderer, int x, int y, int start, String[] text, String[] colours) {
VertexConsumer buffer = renderer.getBuffer( FixedWidthFontRenderer.TYPE );
for( int line = 0; line < LINES_PER_PAGE && line < text.length; line++ )
{
FixedWidthFontRenderer.drawString( transform, buffer,
x, y + line * FONT_HEIGHT,
new TextBuffer( text[start + line] ), new TextBuffer( colours[start + line] ),
null, Palette.DEFAULT, false, 0, 0
);
}
}
public static void drawBorder(double x, double y, double z, int page, int pages, boolean isBook) {
GlStateManager.color4f(1.0f, 1.0f, 1.0f, 1.0f);
GlStateManager.enableBlend();
GlStateManager.enableTexture();
GlStateManager.blendFuncSeparate(SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO);
MinecraftClient.getInstance()
.getTextureManager()
.bindTextureInner(BG);
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder buffer = tessellator.getBuffer();
buffer.begin(GL11.GL_QUADS, VertexFormats.POSITION_TEXTURE);
@SuppressWarnings ("MethodCallSideOnly")
public static void drawBorder(Matrix4f transform, VertexConsumerProvider renderer, float x, float y, float z, int page, int pages, boolean isBook) {
int leftPages = page;
int rightPages = pages - page - 1;
if (isBook) {
VertexConsumer buffer = renderer.getBuffer( FixedWidthFontRenderer.TYPE );
if( isBook )
{
// Border
double offset = offsetAt(pages);
final double left = x - 4 - offset;
final double right = x + X_SIZE + offset - 4;
float offset = offsetAt( pages );
float left = x - 4 - offset;
float right = x + X_SIZE + offset - 4;
// Left and right border
drawTexture(buffer, left - 4, y - 8, z - 0.02, COVER_X, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2);
drawTexture(buffer, right, y - 8, z - 0.02, COVER_X + COVER_SIZE, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2);
drawTexture( transform, buffer, left - 4, y - 8, z - 0.02f, COVER_X, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2 );
drawTexture( transform, buffer, right, y - 8, z - 0.02f, COVER_X + COVER_SIZE, 0, COVER_SIZE, Y_SIZE + COVER_SIZE * 2 );
// Draw centre panel (just stretched texture, sorry).
drawTexture(buffer, x - offset, y, z - 0.02, X_SIZE + offset * 2, Y_SIZE, COVER_X + COVER_SIZE / 2.0f, COVER_SIZE, COVER_SIZE, Y_SIZE);
drawTexture( transform, buffer,
x - offset, y, z - 0.02f, X_SIZE + offset * 2, Y_SIZE,
COVER_X + COVER_SIZE / 2.0f, COVER_SIZE, COVER_SIZE, Y_SIZE
);
double borderX = left;
while (borderX < right) {
double thisWidth = Math.min(right - borderX, X_SIZE);
drawTexture(buffer, borderX, y - 8, z - 0.02, 0, COVER_Y, thisWidth, COVER_SIZE);
drawTexture(buffer, borderX, y + Y_SIZE - 4, z - 0.02, 0, COVER_Y + COVER_SIZE, thisWidth, COVER_SIZE);
float borderX = left;
while( borderX < right )
{
double thisWidth = Math.min( right - borderX, X_SIZE );
drawTexture( transform, buffer, borderX, y - 8, z - 0.02f, 0, COVER_Y, (float) thisWidth, COVER_SIZE );
drawTexture( transform, buffer, borderX, y + Y_SIZE - 4, z - 0.02f, 0, COVER_Y + COVER_SIZE, (float) thisWidth, COVER_SIZE );
borderX += thisWidth;
}
}
// Left half
drawTexture(buffer, x, y, z, X_FOLD_SIZE * 2, 0, X_SIZE / 2.0f, Y_SIZE);
for (int n = 0; n <= leftPages; n++) {
drawTexture(buffer, x - offsetAt(n), y, z - 1e-3 * n,
// Use the left "bold" fold for the outermost page
n == leftPages ? 0 : X_FOLD_SIZE, 0, X_FOLD_SIZE, Y_SIZE);
drawTexture( transform, buffer, x, y, z, X_FOLD_SIZE * 2, 0, X_SIZE / 2.0f, Y_SIZE );
for( int n = 0; n <= leftPages; n++ )
{
drawTexture( transform, buffer,
x - offsetAt( n ), y, z - 1e-3f * n,
// Use the left "bold" fold for the outermost page
n == leftPages ? 0 : X_FOLD_SIZE, 0,
X_FOLD_SIZE, Y_SIZE
);
}
// Right half
drawTexture(buffer, x + X_SIZE / 2.0f, y, z, X_FOLD_SIZE * 2 + X_SIZE / 2.0f, 0, X_SIZE / 2.0f, Y_SIZE);
for (int n = 0; n <= rightPages; n++) {
drawTexture(buffer, x + (X_SIZE - X_FOLD_SIZE) + offsetAt(n), y, z - 1e-3 * n,
// Two folds, then the main page. Use the right "bold" fold for the outermost page.
X_FOLD_SIZE * 2 + X_SIZE + (n == rightPages ? X_FOLD_SIZE : 0), 0, X_FOLD_SIZE, Y_SIZE);
drawTexture( transform, buffer, x + X_SIZE / 2.0f, y, z, X_FOLD_SIZE * 2 + X_SIZE / 2.0f, 0, X_SIZE / 2.0f, Y_SIZE );
for( int n = 0; n <= rightPages; n++ )
{
drawTexture( transform, buffer,
x + (X_SIZE - X_FOLD_SIZE) + offsetAt( n ), y, z - 1e-3f * n,
// Two folds, then the main page. Use the right "bold" fold for the outermost page.
X_FOLD_SIZE * 2 + X_SIZE + (n == rightPages ? X_FOLD_SIZE : 0), 0,
X_FOLD_SIZE, Y_SIZE
);
}
tessellator.draw();
}
public static double offsetAt(int page) {
return 32 * (1 - Math.pow(1.2, -page));
private static void drawTexture( Matrix4f matrix, VertexConsumer buffer, float x, float y, float z, float u, float v, float width, float height )
{
buffer.vertex( matrix, x, y + height, z ).texture( u / BG_SIZE, (v + height) / BG_SIZE ).next();
buffer.vertex( matrix, x + width, y + height, z ).texture( (u + width) / BG_SIZE, (v + height) / BG_SIZE ).next();
buffer.vertex( matrix, x + width, y, z ).texture( (u + width) / BG_SIZE, v / BG_SIZE ).next();
buffer.vertex( matrix, x, y, z ).texture( u / BG_SIZE, v / BG_SIZE ).next();
}
private static void drawTexture(BufferBuilder buffer, double x, double y, double z, double u, double v, double width, double height) {
buffer.vertex(x, y + height, z)
.texture(u / BG_SIZE, (v + height) / BG_SIZE)
.next();
buffer.vertex(x + width, y + height, z)
.texture((u + width) / BG_SIZE, (v + height) / BG_SIZE)
.next();
buffer.vertex(x + width, y, z)
.texture((u + width) / BG_SIZE, v / BG_SIZE)
.next();
buffer.vertex(x, y, z)
.texture(u / BG_SIZE, v / BG_SIZE)
.next();
private static void drawTexture( Matrix4f matrix, VertexConsumer buffer, float x, float y, float z, float width, float height, float u, float v, float tWidth, float tHeight )
{
buffer.vertex( matrix, x, y + height, z ).texture( u / BG_SIZE, (v + tHeight) / BG_SIZE ).next();
buffer.vertex( matrix, x + width, y + height, z ).texture( (u + tWidth) / BG_SIZE, (v + tHeight) / BG_SIZE ).next();
buffer.vertex( matrix, x + width, y, z ).texture( (u + tWidth) / BG_SIZE, v / BG_SIZE ).next();
buffer.vertex( matrix, x, y, z ).texture( u / BG_SIZE, v / BG_SIZE ).next();
}
private static void drawTexture(BufferBuilder buffer, double x, double y, double z, double width, double height, double u, double v, double tWidth,
double tHeight) {
buffer.vertex(x, y + height, z)
.texture(u / BG_SIZE, (v + tHeight) / BG_SIZE)
.next();
buffer.vertex(x + width, y + height, z)
.texture((u + tWidth) / BG_SIZE, (v + tHeight) / BG_SIZE)
.next();
buffer.vertex(x + width, y, z)
.texture((u + tWidth) / BG_SIZE, v / BG_SIZE)
.next();
buffer.vertex(x, y, z)
.texture(u / BG_SIZE, v / BG_SIZE)
.next();
public static float offsetAt( int page )
{
return (float) (32 * (1 - Math.pow( 1.2, -page )));
}
private static final class Type extends RenderPhase {
static final RenderLayer TYPE = RenderLayer.of("printout_background", VertexFormats.POSITION_COLOR_TEXTURE, GL11.GL_QUADS, 1024, false, false,
// useDelegate, needsSorting
RenderLayer.MultiPhaseParameters.builder()
.texture(new RenderLayer.Texture(BG, false, false)) // blur, minimap
.alpha(ONE_TENTH_ALPHA)
.lightmap(DISABLE_LIGHTMAP)
.build(false));
private Type(String name, Runnable setup, Runnable destroy) {
super(name, setup, destroy);
}
}
}