Created
April 21, 2018 17:55
-
-
Save HyCraftHD/bd3e96a0a6d17a24876aa5b82b372e54 to your computer and use it in GitHub Desktop.
Basic scrolling text render with some settings and correct scaling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.u_team.music_player.impl.event; | |
import java.math.BigDecimal; | |
import org.lwjgl.opengl.GL11; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.gui.*; | |
public class ScrollingTextRender { | |
private FontRenderer fontrender; | |
private int width; | |
private String text; | |
private float textwidth; | |
private int color; | |
private boolean shadow; | |
private float scale; | |
private float speed; | |
private int waittime; | |
private float movedifference = 0F; | |
private long lasttime = 0; | |
private State state = State.WAITING; | |
public ScrollingTextRender(FontRenderer fontrender) { | |
this.fontrender = fontrender; | |
this.width = 100; | |
this.setText("default"); | |
this.color = 0x000; | |
this.shadow = false; | |
this.scale = 1F; | |
this.speed = 0.5F; | |
this.waittime = 4000; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public void setText(String text) { | |
this.text = text; | |
this.textwidth = fontrender.getStringWidth(text); | |
} | |
public void setColor(int color) { | |
this.color = color; | |
} | |
public void setShadow(boolean shadow) { | |
this.shadow = shadow; | |
} | |
public void setScale(float scale) { | |
this.scale = scale; | |
} | |
public void setSpeed(float speed) { | |
this.speed = speed; | |
} | |
public void setWaitTime(int waittime) { | |
this.waittime = waittime; | |
} | |
public void draw(float x, float y) { | |
Minecraft minecraft = Minecraft.getMinecraft(); | |
ScaledResolution scaledresolution = new ScaledResolution(minecraft, minecraft.displayWidth, minecraft.displayHeight); | |
int scaledwidth = (int) (width * scaledresolution.getScaleFactor() * scale); | |
int scaledheight = (int) (fontrender.FONT_HEIGHT * scaledresolution.getScaleFactor() * scale); | |
int scaledtextwidth = (int) (textwidth * scaledresolution.getScaleFactor() * scale); | |
GL11.glPushMatrix(); | |
GL11.glEnable(GL11.GL_SCISSOR_TEST); | |
GL11.glScissor((int) (x * scaledresolution.getScaleFactor()), (int) (minecraft.displayHeight - (y * scaledresolution.getScaleFactor() + scaledheight)), scaledwidth, scaledheight); | |
// Gui.drawRect(0, 0, scaledresolution.getScaledWidth(), scaledresolution.getScaledHeight(), 0xFF00FF00); // test scissor | |
x = move(x * scaledresolution.getScaleFactor(), scaledwidth, scaledtextwidth) / scaledresolution.getScaleFactor(); | |
BigDecimal bigdecimal = new BigDecimal(x).setScale(1, BigDecimal.ROUND_DOWN); // Why the fuck is lwjgl so dump (or fontrender) to have problems with to long decimals .... | |
x = bigdecimal.floatValue(); | |
GL11.glTranslatef(x, y, 0); | |
GL11.glScalef(scale, scale, 0); | |
fontrender.drawString(text, 0, 0, color, shadow); | |
GL11.glDisable(GL11.GL_SCISSOR_TEST); | |
GL11.glPopMatrix(); | |
} | |
private float move(float x, int scaledwidth, int scaledtextwidth) { | |
if (scaledwidth < scaledtextwidth) { | |
int maxmove = scaledwidth - scaledtextwidth; | |
if (state == State.WAITING) { | |
if (lasttime == 0) { | |
lasttime = System.currentTimeMillis(); | |
} | |
if (System.currentTimeMillis() - waittime >= lasttime) { | |
if (movedifference >= 0) { | |
state = State.LEFT; | |
} else { | |
state = State.RIGHT; | |
} | |
lasttime = 0; | |
} | |
} else if (state == State.LEFT) { | |
if (movedifference >= maxmove) { | |
movedifference -= speed; | |
} else { | |
state = State.WAITING; | |
} | |
} else if (state == State.RIGHT) { | |
if (movedifference <= 0) { | |
movedifference += speed; | |
} else { | |
state = State.WAITING; | |
} | |
} | |
return x + movedifference; | |
} | |
return x; | |
} | |
private enum State { | |
WAITING, | |
LEFT, | |
RIGHT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment