text scrolling now works

This commit is contained in:
Harrison Deng 2017-05-03 12:46:20 -05:00
parent df5eea0a1b
commit b4c92a4477
4 changed files with 81 additions and 15 deletions

View File

@ -134,16 +134,17 @@ public class Polyjet extends Game {
defaultSkin.add("sub-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() { defaultSkin.add("sub-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
{ {
size = fontScale(0.04f); size = fontScale(0.05f);
} }
})); }));
defaultSkin.add("default-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() { defaultSkin.add("default-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
{ {
size = fontScale(0.06f); size = fontScale(0.07f);
} }
})); }));
defaultSkin.add("large-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() { defaultSkin.add("large-font", default_fontGenerator.generateFont(new FreeTypeFontParameter() {
{ {
size = fontScale(0.08f); size = fontScale(0.08f);

View File

@ -73,11 +73,9 @@ public class MusicSelectable extends Button implements Disposable {
public void addInfoToPanel() { public void addInfoToPanel() {
debug(); displayName = new ScrollText(songName, skin, true);
displayName = new ScrollText(songName, skin);
add(displayName).fillX().spaceBottom(20f); add(displayName).fillX().spaceBottom(20f);
row(); row();
String formattedTime = "Run time: "+ String.valueOf(durationInSeconds/60) + ":"; String formattedTime = "Run time: "+ String.valueOf(durationInSeconds/60) + ":";

View File

@ -2,41 +2,108 @@ package zero1hd.polyjet.ui.builders;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Widget; import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack; import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
public class ScrollText extends Widget { public class ScrollText extends Widget {
Rectangle scissors = new Rectangle(); Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(); Rectangle clipBounds = new Rectangle();
GlyphLayout gLayout;
String text; String text;
BitmapFont font; BitmapFont font;
private float fontHeight;
private float fontWidth;
private boolean scrollOnHover;
private boolean currentlyHovering;
private float textOffset;
public ScrollText(String text, Skin skin, boolean scrollOnHover) {
super();
setName(text);
this.scrollOnHover = scrollOnHover;
public ScrollText(String text, Skin skin) {
this.text = text; this.text = text;
font = skin.getFont("default-font"); font = skin.getFont("default-font");
setWidth(20f); font.setColor(skin.getColor("default"));
setHeight(40f); gLayout = new GlyphLayout(font, text);
fontHeight = gLayout.height;
fontWidth = gLayout.width;
addListener(new ClickListener() {
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
currentlyHovering = true;
super.enter(event, x, y, pointer, fromActor);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
currentlyHovering = false;
super.exit(event, x, y, pointer, toActor);
}
});
}
public float getFontHeight() {
return fontHeight;
}
public float getFontWidth() {
return fontWidth;
} }
@Override @Override
public void layout() { public void layout() {
clipBounds.setPosition(getX(), getY()); clipBounds.set(getParent().getX()+getX(), getParent().getY()+getY(), getWidth(), getHeight());
clipBounds.setSize(getWidth(), getHeight());
super.layout(); super.layout();
} }
@Override
public void act(float delta) {
if (scrollOnHover) {
if (currentlyHovering) {
if (textOffset < -fontWidth) {
textOffset = clipBounds.getWidth();
}
textOffset -= 60*delta;
}
} else {
if (textOffset < -fontWidth) {
textOffset = clipBounds.getWidth();
}
textOffset -= 60*delta;
}
super.act(delta);
}
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
ScissorStack.calculateScissors(getStage().getCamera(), batch.getTransformMatrix(), clipBounds, scissors); validate();
getStage().calculateScissors(clipBounds, scissors);
batch.flush();
if (ScissorStack.pushScissors(scissors)) { if (ScissorStack.pushScissors(scissors)) {
font.draw(batch, text, getX(), getY()); font.draw(batch, text, clipBounds.getX() + textOffset, clipBounds.getY() + (fontHeight));
batch.flush(); batch.flush();
ScissorStack.popScissors(); ScissorStack.popScissors();
}; };
}
super.draw(batch, parentAlpha); @Override
public float getMinHeight() {
return fontHeight;
} }
} }

View File

@ -83,7 +83,7 @@ public class MusicSelectionPage extends Page {
Gdx.app.postRunnable(new Runnable() { Gdx.app.postRunnable(new Runnable() {
@Override @Override
public void run() { public void run() {
musicChoices.add(selectable).size(0.2f*getWidth(), 0.8f*getHeight()); musicChoices.add(selectable).size(256f, 0.8f*getHeight());
selectable.addInfoToPanel(); selectable.addInfoToPanel();
} }
}); });