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 17package com.badlogic.gdx.tests; 18 19import com.badlogic.gdx.Gdx; 20import com.badlogic.gdx.graphics.Color; 21import com.badlogic.gdx.graphics.GL20; 22import com.badlogic.gdx.graphics.Texture.TextureFilter; 23import com.badlogic.gdx.graphics.g2d.BitmapFont; 24import com.badlogic.gdx.graphics.g2d.SpriteBatch; 25import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 26import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 27import com.badlogic.gdx.scenes.scene2d.Actor; 28import com.badlogic.gdx.scenes.scene2d.Stage; 29import com.badlogic.gdx.scenes.scene2d.ui.Label; 30import com.badlogic.gdx.scenes.scene2d.ui.Skin; 31import com.badlogic.gdx.scenes.scene2d.ui.Table; 32import com.badlogic.gdx.tests.utils.GdxTest; 33import com.badlogic.gdx.utils.Align; 34import com.badlogic.gdx.utils.viewport.ScreenViewport; 35 36public class LabelTest extends GdxTest { 37 Skin skin; 38 Stage stage; 39 SpriteBatch batch; 40 Actor root; 41 ShapeRenderer renderer; 42 43 @Override 44 public void create () { 45 batch = new SpriteBatch(); 46 renderer = new ShapeRenderer(); 47 skin = new Skin(Gdx.files.internal("data/uiskin.json")); 48 skin.getAtlas().getTextures().iterator().next().setFilter(TextureFilter.Nearest, TextureFilter.Nearest); 49 skin.getFont("default-font").getData().markupEnabled = true; 50 float scale = 1; 51 skin.getFont("default-font").getData().setScale(scale); 52 stage = new Stage(new ScreenViewport()); 53 Gdx.input.setInputProcessor(stage); 54 55 Table table = new Table(); 56 stage.addActor(table); 57 table.setPosition(200, 65); 58 59 table.debug(); 60 table.add(new Label("This is regular text.", skin)); 61 table.row(); 62 table.add(new Label("This is regular text\nwith a newline.", skin)); 63 table.row(); 64 Label label3 = new Label("This is [RED]regular text\n\nwith newlines,\naligned bottom, right.", skin); 65 label3.setColor(Color.GREEN); 66 label3.setAlignment(Align.bottom | Align.right); 67 table.add(label3).minWidth(200 * scale).minHeight(110 * scale).fill(); 68 table.row(); 69 Label label4 = new Label("This is regular text with NO newlines, wrap enabled and aligned bottom, right.", skin); 70 label4.setWrap(true); 71 label4.setAlignment(Align.bottom | Align.right); 72 table.add(label4).minWidth(200 * scale).minHeight(110 * scale).fill(); 73 table.row(); 74 Label label5 = new Label("This is regular text with\n\nnewlines, wrap\nenabled and aligned bottom, right.", skin); 75 label5.setWrap(true); 76 label5.setAlignment(Align.bottom | Align.right); 77 table.add(label5).minWidth(200 * scale).minHeight(110 * scale).fill(); 78 79 table.pack(); 80 } 81 82 @Override 83 public void dispose () { 84 stage.dispose(); 85 skin.dispose(); 86 } 87 88 @Override 89 public void render () { 90 Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1); 91 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 92 93 stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f)); 94 stage.draw(); 95 96 float x = 40, y = 40; 97 98 BitmapFont font = skin.getFont("default-font"); 99 batch.begin(); 100 font.draw(batch, "The quick brown fox jumped over the lazy cow.", x, y); 101 batch.end(); 102 103 drawLine(x, y - font.getDescent(), x + 1000, y - font.getDescent()); 104 drawLine(x, y - font.getCapHeight() + font.getDescent(), x + 1000, y - font.getCapHeight() + font.getDescent()); 105 } 106 107 public void drawLine (float x1, float y1, float x2, float y2) { 108 renderer.setProjectionMatrix(batch.getProjectionMatrix()); 109 renderer.begin(ShapeType.Line); 110 renderer.line(x1, y1, x2, y2); 111 renderer.end(); 112 } 113 114 @Override 115 public void resize (int width, int height) { 116 stage.getViewport().update(width, height, true); 117 } 118} 119