text shortening now works better

This commit is contained in:
Harrison Deng 2017-11-26 21:41:46 -06:00
parent 247885d5cf
commit b81656d187
2 changed files with 24 additions and 7 deletions

View File

@ -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();
}

View File

@ -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;
}
}