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 draw(SpriteBatch spriteBatch, CharSequence str, float x, float y) {
- str = update(str);
- return super.draw(spriteBatch, str, x, y);
- }
- @Override
- public TextBounds draw(SpriteBatch spriteBatch, CharSequence str, float x, float y, int start, int end) {
- str = update(str);
- return super.draw(spriteBatch, str, x, y, start, end);
- }
- @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);
- }
- @Override
- public TextBounds drawMultiLine(SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) {
- str = update(str);
- return super.drawMultiLine(spriteBatch, str, x, y, alignmentWidth, alignment);
- }
- @Override
- public TextBounds drawMultiLine(SpriteBatch spriteBatch, CharSequence str, float x, float y) {
- str = update(str);
- return super.drawMultiLine(spriteBatch, str, x, y);
- }
- @Override
- public TextBounds drawWrapped(SpriteBatch spriteBatch, CharSequence str, float x, float y, float wrapWidth, HAlignment alignment) {
- str = update(str);
- return super.drawWrapped(spriteBatch, str, x, y, wrapWidth, alignment);
- }
- 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));
- }
- /** @return the speed */
- public float getSpeed() {
- return speed;
- }
- /** @param speed the speed to set */
- public void setSpeed(float speed) {
- this.speed = speed;
- }
- /** @return the cursorTime */
- public float getCursorTime() {
- return cursorTime;
- }
- /** @param cursorTime the cursorTime to set */
- public void setCursorTime(float cursorTime) {
- this.cursorTime = cursorTime;
- }
- /** @return the cursorVisible */
- public boolean isCursorVisible() {
- return cursorVisible;
- }
- /** @param cursorVisible the cursorVisible to set */
- public void setCursorVisible(boolean cursorVisible) {
- this.cursorVisible = cursorVisible;
- }
- /** @return the numLetters */
- public float getNumLetters() {
- return numLetters;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement