diff --git a/src/main/java/com/github/tommyettinger/textra/TextraLabel.java b/src/main/java/com/github/tommyettinger/textra/TextraLabel.java index 5b067c3c..632bf91d 100644 --- a/src/main/java/com/github/tommyettinger/textra/TextraLabel.java +++ b/src/main/java/com/github/tommyettinger/textra/TextraLabel.java @@ -248,14 +248,8 @@ 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(); @@ -263,6 +257,11 @@ public void draw(Batch batch, float parentAlpha) { 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(); @@ -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; diff --git a/src/test/java/com/github/tommyettinger/textra/Issue13Test.java b/src/test/java/com/github/tommyettinger/textra/Issue13Test.java index d076696f..6c5ea6ef 100644 --- a/src/test/java/com/github/tommyettinger/textra/Issue13Test.java +++ b/src/test/java/com/github/tommyettinger/textra/Issue13Test.java @@ -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);