Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.dermetfan.someLibgdxTests.screens;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.files.FileHandle;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class RPGFontTest implements Screen {
- private SpriteBatch batch;
- private RPGFont font;
- @Override
- public void show() {
- batch = new SpriteBatch();
- font = new RPGFont(Gdx.files.internal("font/white32.fnt"), false);
- }
- @Override
- public void render(float delta) {
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- font.drawWrapped(batch, "This is a long multiline string. Some text that you can read through in a RPG, that scrolls like in the pokemon games or something.", 0, Gdx.graphics.getHeight(), Gdx.graphics.getWidth());
- batch.end();
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void dispose() {
- batch.dispose();
- font.dispose();
- }
- @Override
- public void hide() {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- public class RPGFont extends BitmapFont {
- float numLetters;
- float speed = 20, cursorTime;
- boolean cursorVisible;
- public RPGFont(FileHandle fontFile, boolean flip) {
- super(fontFile, flip);
- }
- @Override
- public TextBounds drawWrapped(SpriteBatch spriteBatch, CharSequence str, float x, float y, float width) {
- str = update(str);
- return super.drawWrapped(spriteBatch, str, x, y, width);
- }
- private CharSequence update(CharSequence str) {
- float delta = Gdx.graphics.getDeltaTime();
- numLetters += speed * delta;
- if(numLetters > str.length()) {
- numLetters = str.length();
- cursorTime += delta;
- if(cursorTime >= speed / 25)
- cursorVisible = true;
- if(cursorTime >= speed / 15) {
- cursorTime = 0;
- cursorVisible = false;
- }
- }
- return (cursorVisible ? str.toString() + "_" : str).subSequence(0, (int) numLetters + (cursorVisible ? 1 : 0));
- }
- public void setSpeed(int speed) {
- this.speed = speed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement