text shortening now works better
This commit is contained in:
		@@ -72,6 +72,7 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
 | 
				
			|||||||
				super.clicked(event, x, y);
 | 
									super.clicked(event, x, y);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
							table.pack();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -80,7 +81,6 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void updateInfo() {
 | 
						public void updateInfo() {
 | 
				
			||||||
		displayName.setOriginalText(songInfo.getMusicName());
 | 
							displayName.setOriginalText(songInfo.getMusicName());
 | 
				
			||||||
		displayName.setToOriginalText();
 | 
					 | 
				
			||||||
		durationLabel.setText("Runtime: "
 | 
							durationLabel.setText("Runtime: "
 | 
				
			||||||
				+ ((songInfo.getDurationInSeconds() / 60 < 1) ? "00" : songInfo.getDurationInSeconds() / 60) + ":"
 | 
									+ ((songInfo.getDurationInSeconds() / 60 < 1) ? "00" : songInfo.getDurationInSeconds() / 60) + ":"
 | 
				
			||||||
				+ ((songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60) < 10
 | 
									+ ((songInfo.getDurationInSeconds() - (songInfo.getDurationInSeconds() / 60) * 60) < 10
 | 
				
			||||||
@@ -96,9 +96,9 @@ public class MusicSelectable extends WidgetGroup implements Disposable {
 | 
				
			|||||||
	
 | 
						
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public void layout() {
 | 
						public void layout() {
 | 
				
			||||||
		table.pack();
 | 
					 | 
				
			||||||
		displayName.setTargetWidth((int) (getWidth() - 300));
 | 
							displayName.setTargetWidth((int) (getWidth() - 300));
 | 
				
			||||||
		authorLabel.setTargetWidth((int) (getWidth() - 300));
 | 
							authorLabel.setTargetWidth((int) (getWidth() - 300));
 | 
				
			||||||
 | 
							displayName.resize();
 | 
				
			||||||
		super.layout();
 | 
							super.layout();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,34 +1,46 @@
 | 
				
			|||||||
package zero1hd.rhythmbullet.graphics.ui.components;
 | 
					package zero1hd.rhythmbullet.graphics.ui.components;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.badlogic.gdx.graphics.Color;
 | 
					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.Label;
 | 
				
			||||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 | 
					import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ShortenedTextLabel extends Label {
 | 
					public class ShortenedTextLabel extends Label {
 | 
				
			||||||
	private String originalText;
 | 
						private String originalText;
 | 
				
			||||||
	private int targetWidth;
 | 
						private int targetWidth;
 | 
				
			||||||
 | 
						private GlyphLayout gl;
 | 
				
			||||||
 | 
						private BitmapFont font;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	public ShortenedTextLabel(CharSequence text, Skin skin, String fontName, Color color) {
 | 
						public ShortenedTextLabel(CharSequence text, Skin skin, String fontName, Color color) {
 | 
				
			||||||
		super(text, skin, fontName, color);
 | 
							super(text, skin, fontName, color);
 | 
				
			||||||
		originalText = text.toString();
 | 
							originalText = text.toString();
 | 
				
			||||||
 | 
							font = skin.getFont(fontName);
 | 
				
			||||||
 | 
							if (text != null) {
 | 
				
			||||||
 | 
								gl = new GlyphLayout(skin.getFont(fontName), text);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	public void setTargetWidth(int targetWidth) {
 | 
						public void setTargetWidth(int targetWidth) {
 | 
				
			||||||
		this.targetWidth = targetWidth;
 | 
							this.targetWidth = targetWidth;
 | 
				
			||||||
		validate();
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public void act(float delta) {
 | 
						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);
 | 
							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() {
 | 
						public void setToOriginalText() {
 | 
				
			||||||
		setText(originalText);
 | 
							setText(originalText);
 | 
				
			||||||
 | 
							gl.setText(font, originalText);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
@@ -38,5 +50,10 @@ public class ShortenedTextLabel extends Label {
 | 
				
			|||||||
	
 | 
						
 | 
				
			||||||
	public void setOriginalText(String originalText) {
 | 
						public void setOriginalText(String originalText) {
 | 
				
			||||||
		this.originalText = originalText;
 | 
							this.originalText = originalText;
 | 
				
			||||||
 | 
							gl.setText(font, originalText);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						public int getTargetWidth() {
 | 
				
			||||||
 | 
							return targetWidth;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user