Advertisement
dermetfan

RPGFont v1

Sep 3rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Screen;
  5. import com.badlogic.gdx.files.FileHandle;
  6. import com.badlogic.gdx.graphics.GL20;
  7. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  8. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  9.  
  10. public class RPGFontTest implements Screen {
  11.  
  12.     private SpriteBatch batch;
  13.     private RPGFont font;
  14.  
  15.     @Override
  16.     public void show() {
  17.         batch = new SpriteBatch();
  18.         font = new RPGFont(Gdx.files.internal("font/white32.fnt"), false);
  19.     }
  20.  
  21.     @Override
  22.     public void render(float delta) {
  23.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  24.  
  25.         batch.begin();
  26.         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());
  27.         batch.end();
  28.     }
  29.  
  30.     @Override
  31.     public void resize(int width, int height) {
  32.     }
  33.  
  34.     @Override
  35.     public void dispose() {
  36.         batch.dispose();
  37.         font.dispose();
  38.     }
  39.  
  40.     @Override
  41.     public void hide() {
  42.     }
  43.  
  44.     @Override
  45.     public void pause() {
  46.     }
  47.  
  48.     @Override
  49.     public void resume() {
  50.     }
  51.  
  52.     public class RPGFont extends BitmapFont {
  53.  
  54.         float numLetters;
  55.         float speed = 20, cursorTime;
  56.         boolean cursorVisible;
  57.  
  58.         public RPGFont(FileHandle fontFile, boolean flip) {
  59.             super(fontFile, flip);
  60.         }
  61.  
  62.         @Override
  63.         public TextBounds drawWrapped(SpriteBatch spriteBatch, CharSequence str, float x, float y, float width) {
  64.             str = update(str);
  65.             return super.drawWrapped(spriteBatch, str, x, y, width);
  66.         }
  67.  
  68.         private CharSequence update(CharSequence str) {
  69.  
  70.             float delta = Gdx.graphics.getDeltaTime();
  71.  
  72.             numLetters += speed * delta;
  73.             if(numLetters > str.length()) {
  74.                 numLetters = str.length();
  75.                 cursorTime += delta;
  76.                 if(cursorTime >= speed / 25)
  77.                     cursorVisible = true;
  78.                 if(cursorTime >= speed / 15) {
  79.                     cursorTime = 0;
  80.                     cursorVisible = false;
  81.                 }
  82.             }
  83.  
  84.             return (cursorVisible ? str.toString() + "_" : str).subSequence(0, (int) numLetters + (cursorVisible ? 1 : 0));
  85.         }
  86.  
  87.         public void setSpeed(int speed) {
  88.             this.speed = speed;
  89.         }
  90.  
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement