From b81656d18746d2a770d49270d3a2fabd25834609 Mon Sep 17 00:00:00 2001 From: Recrown Date: Sun, 26 Nov 2017 21:41:46 -0600 Subject: [PATCH] text shortening now works better --- .../ui/components/MusicSelectable.java | 4 +-- .../ui/components/ShortenedTextLabel.java | 27 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java index ef0aa22..d365642 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/MusicSelectable.java @@ -72,6 +72,7 @@ public class MusicSelectable extends WidgetGroup implements Disposable { super.clicked(event, x, y); } }); + table.pack(); } /** @@ -80,7 +81,6 @@ public class MusicSelectable extends WidgetGroup implements Disposable { */ public void updateInfo() { displayName.setOriginalText(songInfo.getMusicName()); - displayName.setToOriginalText(); durationLabel.setText("Runtime: " + ((songInfo.getDurationInSeconds() / 60 < 1) ? "00" : songInfo.getDurationInSeconds() / 60) + ":" + ((songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60) < 10 @@ -96,9 +96,9 @@ public class MusicSelectable extends WidgetGroup implements Disposable { @Override public void layout() { - table.pack(); displayName.setTargetWidth((int) (getWidth() - 300)); authorLabel.setTargetWidth((int) (getWidth() - 300)); + displayName.resize(); super.layout(); } diff --git a/core/src/zero1hd/rhythmbullet/graphics/ui/components/ShortenedTextLabel.java b/core/src/zero1hd/rhythmbullet/graphics/ui/components/ShortenedTextLabel.java index 05ece1c..fab6580 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/ui/components/ShortenedTextLabel.java +++ b/core/src/zero1hd/rhythmbullet/graphics/ui/components/ShortenedTextLabel.java @@ -1,34 +1,46 @@ package zero1hd.rhythmbullet.graphics.ui.components; import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Skin; public class ShortenedTextLabel extends Label { private String originalText; private int targetWidth; + private GlyphLayout gl; + private BitmapFont font; public ShortenedTextLabel(CharSequence text, Skin skin, String fontName, Color color) { super(text, skin, fontName, color); originalText = text.toString(); + font = skin.getFont(fontName); + if (text != null) { + gl = new GlyphLayout(skin.getFont(fontName), text); + } } public void setTargetWidth(int targetWidth) { this.targetWidth = targetWidth; - validate(); } @Override public void act(float delta) { - if (getGlyphLayout().width > targetWidth && getText().length - 4 > 0) { - setText(getText().substring(0, getText().length - 4).concat("...")); - validate(); - } super.act(delta); } + public void resize() { + setToOriginalText(); + while (gl.width > targetWidth && (getText().length - 4) > 0) { + setText(getText().substring(0, getText().length - 4).concat("...")); + gl.setText(font, getText()); + } + } + public void setToOriginalText() { setText(originalText); + gl.setText(font, originalText); } @Override @@ -38,5 +50,10 @@ public class ShortenedTextLabel extends Label { public void setOriginalText(String originalText) { this.originalText = originalText; + gl.setText(font, originalText); + } + + public int getTargetWidth() { + return targetWidth; } }