Skip to content

Commit

Permalink
More work on issue #13 , more improvement.
Browse files Browse the repository at this point in the history
Now a TextraLabel will show the correct debug bounds even without calling pack(), which is the same behavior as TypingLabel. It shows the correct actual height in the println() call once pack() has been called.
  • Loading branch information
tommyettinger committed Sep 6, 2023
1 parent 8502d64 commit d01f08e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/github/tommyettinger/textra/TextraLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,20 @@ public TextraLabel(String text, Font font, Color color) {
public void draw(Batch batch, float parentAlpha) {
super.validate();

//TODO: Figure out how expensive this is to call frequently; see if we can limit how many calls we make.
// float oldWidth = layout.getWidth();
// float oldHeight = layout.getHeight();
// font.calculateSize(layout);
float height = layout.getHeight();
float width;// = layout.getWidth();
// if(oldWidth != width || oldHeight != height)
// invalidateHierarchy();
float width = layout.getWidth();
final float rot = getRotation();
final float originX = getOriginX();
final float originY = getOriginY();
final float sn = MathUtils.sinDeg(rot);
final float cs = MathUtils.cosDeg(rot);

int bgc;

if(width != (width = getWidth()) || height != (height = getHeight()))
invalidateHierarchy();


final int lines = layout.lines();
float baseX = getX(), baseY = getY();

Expand All @@ -273,8 +272,7 @@ public void draw(Batch batch, float parentAlpha) {
baseX -= sn * height * 0.5f;
baseY += cs * height * 0.5f;
}
width = getWidth();
height = getHeight();

if (Align.isRight(align)) {
baseX += cs * width;
baseY += sn * width;
Expand Down
38 changes: 16 additions & 22 deletions src/test/java/com/github/tommyettinger/textra/Issue13Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,26 @@ public void create () {
// https://i.imgur.com/LFYLAPc.png
text.append("this is a normal text test test test!");
// It works for TextraLabel and TypingLabel, in the same way.
TextraLabel label = new TextraLabel(text.toString(), skin);
label.setWrap(true);

// Runs in the next render thread so the layout is ready.
Gdx.app.postRunnable(() -> System.out.println("Height: " + label.getHeight()));

// TypingLabel label = new TypingLabel(text.toString(), skin);
// label.setWrap(true);
// label.setSelectable(true);
// label.setTypingListener(new TypingAdapter(){
// @Override
// public void event(String event) {
// if("*SELECTED".equals(event)){
// System.out.println(label.getSelectedText());
// }
// }
// });
// // Runs in the next render thread so the layout is ready.
// Gdx.app.postRunnable(() -> System.out.println("Lines: " + label.workingLayout.lines()));
// Gdx.app.postRunnable(() -> System.out.println("Height: " + label.getHeight()));

TextraLabel label;
// if("TEXTRA".equals("TYPING")) {
if("TEXTRA".equals("TEXTRA")) {
label = new TextraLabel(text.toString(), skin);
label.setWrap(true);
// Runs in the next render thread so the layout is ready.
Gdx.app.postRunnable(() -> System.out.println("Height: " + label.getHeight()));
} else {
label = new TypingLabel(text.toString(), skin);
label.setWrap(true);
// Runs in the next render thread so the layout is ready.
Gdx.app.postRunnable(() -> System.out.println("Height: " + label.getHeight()));
}
Table table = new Table();
table.debug();
table.add(label).prefWidth(100).row();
// pack() sets the actual size to the preferred size and then validates the sizing.
table.pack();
// without calling pack(), the actual height will be reported as one line's worth,
// but after calling pack(), it should match the actual size.
// table.pack();
Stack stack = new Stack(table);
stack.setFillParent(true);
stage.addActor(stack);
Expand Down

0 comments on commit d01f08e

Please sign in to comment.