added text shortening label

This commit is contained in:
Harrison Deng 2017-10-29 18:13:21 -05:00
parent 67684e8634
commit f506e934ab
3 changed files with 59 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 119 KiB

View File

@ -19,9 +19,9 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
private Table table;
private Image imageIcon;
private Label displayName;
private ShortenedTextLabel displayName;
private Label durationLabel;
private Label authorLabel;
private ShortenedTextLabel authorLabel;
private FileHandle musicFile;
@ -47,16 +47,16 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
imageIcon = new Image(albumCover);
table.add(imageIcon).size(180f).left().expandX();
displayName = new Label(musicFile.nameWithoutExtension().replace('_', ' '), skin, "sub-font", skin.getColor("default"));
displayName = new ShortenedTextLabel(musicFile.nameWithoutExtension().replace('_', ' '), skin, "sub-font", skin.getColor("default"));
vGroup.addActor(displayName);
durationLabel = new Label("Loading...", skin, "sub-font", skin.getColor("default"));
vGroup.addActor(durationLabel);
authorLabel = new Label("Loading...", skin, "sub-font", skin.getColor("default"));
authorLabel = new ShortenedTextLabel("Loading...", skin, "sub-font", skin.getColor("default"));
vGroup.addActor(authorLabel);
table.add(vGroup).center();
table.add(vGroup).expandX().center().fillX();
addActor(table);
}
@ -66,18 +66,28 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
* needs to be called in thread with gl context.
*/
public void updateInfo() {
displayName.setOriginalText(songInfo.getSongName());
displayName.setToOriginalText();
durationLabel.setText("Runtime: "
+ ((songInfo.getDurationInSeconds() / 60 < 1) ? "00" : songInfo.getDurationInSeconds() / 60) + ":"
+ ((songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60) < 10
? "0" + (songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60)
: (songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60)));
authorLabel.setText("Author: " + songInfo.getAuthor());
authorLabel.setOriginalText("Author: " + songInfo.getAuthor());
authorLabel.setToOriginalText();
songInfo.setupTexture(albumCover);
albumCover = songInfo.getAlbumCover();
imageIcon.setDrawable((new TextureRegionDrawable(new TextureRegion(albumCover))));
}
@Override
public void layout() {
displayName.setTargetWidth((int) (getWidth() - 300));
authorLabel.setTargetWidth((int) (getWidth() - 300));
super.layout();
}
@Override
public void act(float delta) {
table.setSize(getWidth(), getHeight());

View File

@ -0,0 +1,42 @@
package zero1hd.rhythmbullet.graphics.ui.components;
import com.badlogic.gdx.graphics.Color;
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;
public ShortenedTextLabel(CharSequence text, Skin skin, String fontName, Color color) {
super(text, skin, fontName, color);
originalText = text.toString();
}
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 setToOriginalText() {
setText(originalText);
}
@Override
public void layout() {
super.layout();
}
public void setOriginalText(String originalText) {
this.originalText = originalText;
}
}