Coverage Summary for Class: Text (com.mygdx.game.Components)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Text | 0% (0/1) | 0% (0/9) | 0% (0/27) |
1 package com.mygdx.game.Components; 2 3 import com.badlogic.gdx.graphics.Color; 4 import com.badlogic.gdx.graphics.g2d.BitmapFont; 5 import com.badlogic.gdx.math.Vector2; 6 import com.badlogic.gdx.math.Vector3; 7 import com.mygdx.game.Managers.RenderLayer; 8 import com.mygdx.game.Managers.RenderingManager; 9 10 /** 11 * Renders text with give font and colour. 12 * Uses transform component for position but if not present then internal position is the bottom left 13 * mainly used for debug 14 * doen't fully work but is usable 15 */ 16 public class Text extends Component { 17 BitmapFont font; 18 Vector3 fontColour; 19 Vector2 position; 20 Vector2 offset; 21 String text; 22 23 public Text() { 24 super(); 25 position = new Vector2(); 26 offset = new Vector2(); 27 type = ComponentType.Text; 28 } 29 30 public Text(Vector3 fontColour) { 31 this(); 32 // font = ResourceManager.getFont(font_id); 33 font = new BitmapFont(); 34 this.fontColour = fontColour; 35 RenderingManager.addItem(this, RenderLayer.Transparent); 36 } 37 38 /** 39 * isn't used if parent has a transform component 40 * 41 * @param pos pos to render the text 42 */ 43 public void setPosition(Vector2 pos) { 44 position.set(pos); 45 } 46 47 /** 48 * isn't used if parent has a transform component 49 */ 50 public void setPosition(float x, float y) { 51 position.set(x, y); 52 } 53 54 public void setFontColour(Vector3 col) { 55 fontColour.set(col); 56 } 57 58 public void setText(String text) { 59 this.text = text; 60 } 61 62 @Override 63 public void update() { 64 super.update(); 65 Transform t = parent.getComponent(Transform.class); 66 if (t == null) { 67 return; 68 } 69 position.set(t.getPosition()); 70 } 71 72 @Override 73 public void render() { 74 super.render(); 75 Color c = new Color(); 76 c.r = fontColour.x; 77 c.g = fontColour.y; 78 c.b = fontColour.z; 79 c.a = 1; 80 font.setColor(c); 81 float x = position.x + offset.x; 82 float y = position.y + font.getXHeight() * 2.f + offset.x; 83 font.draw(RenderingManager.getBatch(), text, x, y); 84 } 85 86 @Override 87 public void cleanUp() { 88 super.cleanUp(); 89 } 90 }