• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import com.badlogic.gdx.Files.FileType;
20 import com.badlogic.gdx.Gdx;
21 import com.badlogic.gdx.InputAdapter;
22 import com.badlogic.gdx.graphics.Color;
23 import com.badlogic.gdx.graphics.GL20;
24 import com.badlogic.gdx.graphics.Texture;
25 import com.badlogic.gdx.graphics.g2d.BitmapFont;
26 import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
27 import com.badlogic.gdx.graphics.g2d.GlyphLayout;
28 import com.badlogic.gdx.graphics.g2d.Sprite;
29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 import com.badlogic.gdx.utils.Align;
32 
33 /** Shows how to align single line, wrapped, and multi line text within a rectangle. */
34 public class BitmapFontAlignmentTest extends GdxTest {
35 	private SpriteBatch spriteBatch;
36 	private Texture texture;
37 	private BitmapFont font;
38 	private BitmapFontCache cache;
39 	private Sprite logoSprite;
40 	int renderMode;
41 	GlyphLayout layout;
42 
43 	@Override
create()44 	public void create () {
45 		Gdx.input.setInputProcessor(new InputAdapter() {
46 			public boolean touchDown (int x, int y, int pointer, int newParam) {
47 				renderMode = (renderMode + 1) % 6;
48 				return false;
49 			}
50 		});
51 
52 		spriteBatch = new SpriteBatch();
53 		texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
54 		logoSprite = new Sprite(texture);
55 		logoSprite.setColor(1, 1, 1, 0.6f);
56 		logoSprite.setBounds(50, 100, 400, 100);
57 
58 		font = new BitmapFont(Gdx.files.getFileHandle("data/verdana39.fnt", FileType.Internal), Gdx.files.getFileHandle(
59 			"data/verdana39.png", FileType.Internal), false);
60 		cache = font.newFontCache();
61 		layout = new GlyphLayout();
62 	}
63 
64 	@Override
render()65 	public void render () {
66 		Gdx.gl.glClearColor(0.7f, 0, 0, 1);
67 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
68 		spriteBatch.begin();
69 		logoSprite.draw(spriteBatch);
70 		switch (renderMode) {
71 		case 0:
72 			renderSingleLine();
73 			break;
74 		case 1:
75 			renderSingleLineCached();
76 			break;
77 		case 2:
78 			renderWrapped();
79 			break;
80 		case 3:
81 			renderWrappedCached();
82 			break;
83 		case 4:
84 			renderMultiLine();
85 			break;
86 		case 5:
87 			renderMultiLineCached();
88 			break;
89 		}
90 		spriteBatch.end();
91 	}
92 
renderSingleLine()93 	private void renderSingleLine () {
94 		String text = "Single Line";
95 		float x = logoSprite.getX();
96 		float y = logoSprite.getY();
97 		float width = logoSprite.getWidth();
98 		float height = logoSprite.getHeight();
99 
100 		layout.setText(font, text);
101 		x += width / 2 - layout.width / 2;
102 		y += height / 2 + layout.height / 2;
103 
104 		font.draw(spriteBatch, text, x, y);
105 	}
106 
renderSingleLineCached()107 	private void renderSingleLineCached () {
108 		String text = "Single Line Cached";
109 		float x = logoSprite.getX();
110 		float y = logoSprite.getY();
111 		float width = logoSprite.getWidth();
112 		float height = logoSprite.getHeight();
113 
114 		// Obviously you wouldn't set the cache text every frame in real code.
115 		GlyphLayout layout = cache.setText(text, 0, 0);
116 		cache.setColors(Color.BLUE, 1, 4);
117 
118 		x += width / 2 - layout.width / 2;
119 		y += height / 2 + layout.height / 2;
120 		cache.setPosition(x, y);
121 
122 		cache.draw(spriteBatch);
123 	}
124 
renderWrapped()125 	private void renderWrapped () {
126 		String text = "Wrapped Wrapped Wrapped Wrapped";
127 		float x = logoSprite.getX();
128 		float y = logoSprite.getY();
129 		float width = logoSprite.getWidth();
130 		float height = logoSprite.getHeight();
131 
132 		layout.setText(font, text, Color.WHITE, width, Align.left, true);
133 		x += width / 2 - layout.width / 2;
134 		y += height / 2 + layout.height / 2;
135 
136 		font.draw(spriteBatch, text, x, y, width, Align.left, true);
137 
138 		// More efficient to draw the layout used for bounds:
139 		// font.draw(spriteBatch, layout, x, y);
140 
141 		// Note that wrapped text can be aligned:
142 		// font.draw(spriteBatch, text, x, y, width, Align.center, true);
143 	}
144 
renderWrappedCached()145 	private void renderWrappedCached () {
146 		String text = "Wrapped Cached Wrapped Cached";
147 		float x = logoSprite.getX();
148 		float y = logoSprite.getY();
149 		float width = logoSprite.getWidth();
150 		float height = logoSprite.getHeight();
151 
152 		// Obviously you wouldn't set the cache text every frame in real code.
153 		GlyphLayout layout = cache.setText(text, 0, 0, width, Align.left, true);
154 
155 		// Note that wrapped text can be aligned:
156 		// cache.setWrappedText(text, 0, 0, width, HAlignment.CENTER);
157 
158 		x += width / 2 - layout.width / 2;
159 		y += height / 2 + layout.height / 2;
160 		cache.setPosition(x, y);
161 
162 		cache.draw(spriteBatch);
163 	}
164 
renderMultiLine()165 	private void renderMultiLine () {
166 		String text = "Multi\nLine";
167 		float x = logoSprite.getX();
168 		float y = logoSprite.getY();
169 		float width = logoSprite.getWidth();
170 		float height = logoSprite.getHeight();
171 
172 		layout.setText(font, text);
173 		x += width / 2 - layout.width / 2;
174 		y += height / 2 + layout.height / 2;
175 
176 		font.draw(spriteBatch, text, x, y);
177 
178 		// Note that multi line text can be aligned:
179 		// font.draw(spriteBatch, text, x, y, width, Align.center, false);
180 	}
181 
renderMultiLineCached()182 	private void renderMultiLineCached () {
183 		String text = "Multi Line\nCached";
184 		int lines = 2;
185 		float x = logoSprite.getX();
186 		float y = logoSprite.getY();
187 		float width = logoSprite.getWidth();
188 		float height = logoSprite.getHeight();
189 
190 		// Obviously you wouldn't set the cache text every frame in real code.
191 		GlyphLayout layout = cache.setText(text, 0, 0);
192 
193 		// Note that multi line text can be aligned:
194 		// cache.setText(text, 0, 0, width, Align.center, false);
195 
196 		x += width / 2 - layout.width / 2;
197 		y += height / 2 + layout.height / 2;
198 		cache.setPosition(x, y);
199 
200 		cache.draw(spriteBatch);
201 	}
202 
203 	@Override
dispose()204 	public void dispose () {
205 		spriteBatch.dispose();
206 		font.dispose();
207 		texture.dispose();
208 	}
209 }
210