SHOW:
|
|
- or go back to the newest paste.
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 draw(SpriteBatch spriteBatch, CharSequence str, float x, float y) { | |
64 | str = update(str); | |
65 | return super.draw(spriteBatch, str, x, y); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public TextBounds draw(SpriteBatch spriteBatch, CharSequence str, float x, float y, int start, int end) { | |
70 | str = update(str); | |
71 | return super.draw(spriteBatch, str, x, y, start, end); | |
72 | } | |
73 | ||
74 | @Override | |
75 | public TextBounds drawWrapped(SpriteBatch spriteBatch, CharSequence str, float x, float y, float width) { | |
76 | str = update(str); | |
77 | return super.drawWrapped(spriteBatch, str, x, y, width); | |
78 | } | |
79 | ||
80 | @Override | |
81 | public TextBounds drawMultiLine(SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) { | |
82 | str = update(str); | |
83 | return super.drawMultiLine(spriteBatch, str, x, y, alignmentWidth, alignment); | |
84 | } | |
85 | ||
86 | @Override | |
87 | - | public void setSpeed(int speed) { |
87 | + | public TextBounds drawMultiLine(SpriteBatch spriteBatch, CharSequence str, float x, float y) { |
88 | str = update(str); | |
89 | return super.drawMultiLine(spriteBatch, str, x, y); | |
90 | } | |
91 | ||
92 | @Override | |
93 | public TextBounds drawWrapped(SpriteBatch spriteBatch, CharSequence str, float x, float y, float wrapWidth, HAlignment alignment) { | |
94 | str = update(str); | |
95 | return super.drawWrapped(spriteBatch, str, x, y, wrapWidth, alignment); | |
96 | } | |
97 | ||
98 | private CharSequence update(CharSequence str) { | |
99 | ||
100 | float delta = Gdx.graphics.getDeltaTime(); | |
101 | ||
102 | numLetters += speed * delta; | |
103 | if(numLetters > str.length()) { | |
104 | numLetters = str.length(); | |
105 | ||
106 | cursorTime += delta; | |
107 | ||
108 | if(cursorTime >= speed / 25) | |
109 | cursorVisible = true; | |
110 | ||
111 | if(cursorTime >= speed / 15) { | |
112 | cursorTime = 0; | |
113 | cursorVisible = false; | |
114 | } | |
115 | } | |
116 | ||
117 | return (cursorVisible ? str.toString() + "_" : str).subSequence(0, (int) numLetters + (cursorVisible ? 1 : 0)); | |
118 | } | |
119 | ||
120 | /** @return the speed */ | |
121 | public float getSpeed() { | |
122 | return speed; | |
123 | } | |
124 | ||
125 | /** @param speed the speed to set */ | |
126 | public void setSpeed(float speed) { | |
127 | this.speed = speed; | |
128 | } | |
129 | ||
130 | /** @return the cursorTime */ | |
131 | public float getCursorTime() { | |
132 | return cursorTime; | |
133 | } | |
134 | ||
135 | /** @param cursorTime the cursorTime to set */ | |
136 | public void setCursorTime(float cursorTime) { | |
137 | this.cursorTime = cursorTime; | |
138 | } | |
139 | ||
140 | /** @return the cursorVisible */ | |
141 | public boolean isCursorVisible() { | |
142 | return cursorVisible; | |
143 | } | |
144 | ||
145 | /** @param cursorVisible the cursorVisible to set */ | |
146 | public void setCursorVisible(boolean cursorVisible) { | |
147 | this.cursorVisible = cursorVisible; | |
148 | } | |
149 | ||
150 | /** @return the numLetters */ | |
151 | public float getNumLetters() { | |
152 | return numLetters; | |
153 | } | |
154 | ||
155 | } | |
156 | ||
157 | } |