animation for the bar notifications

This commit is contained in:
yugecin 2017-01-17 17:08:05 +01:00
parent 3d5dacf70f
commit 97e71e158d

View File

@ -18,6 +18,7 @@
package yugecin.opsudance.core.state.specialstates; package yugecin.opsudance.core.state.specialstates;
import itdelatrisu.opsu.ui.Fonts; import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.animations.AnimationEquation;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.DisplayContainer;
@ -26,30 +27,38 @@ import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.BarNotificationEvent; import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.ResolutionChangedEvent; import yugecin.opsudance.events.ResolutionChangedEvent;
import java.util.List;
public class BarNotificationState implements EventListener<BarNotificationEvent> { public class BarNotificationState implements EventListener<BarNotificationEvent> {
private final int NOTIFICATION_TIME = 5000; private final int IN_TIME = 200;
private final int DISPLAY_TIME = 5700 + IN_TIME;
private final int OUT_TIME = 200;
private final int TOTAL_TIME = DISPLAY_TIME + OUT_TIME;
private final DisplayContainer displayContainer; private final DisplayContainer displayContainer;
private final Color bgcol; private final Color bgcol;
private final Color textCol;
private int timeShown; private int timeShown;
private String message; private String message;
private int textX; private List<String> lines;
private int textY; private int textY;
private int barY;
private int barHeight; private int barHalfTargetHeight;
private int barHalfHeight;
public BarNotificationState(DisplayContainer displayContainer, EventBus eventBus) { public BarNotificationState(DisplayContainer displayContainer, EventBus eventBus) {
this.displayContainer = displayContainer; this.displayContainer = displayContainer;
this.bgcol = new Color(0f, 0f, 0f, 0f); this.bgcol = new Color(Color.black);
this.timeShown = NOTIFICATION_TIME; this.textCol = new Color(Color.white);
this.timeShown = TOTAL_TIME;
eventBus.subscribe(BarNotificationEvent.class, this); eventBus.subscribe(BarNotificationEvent.class, this);
eventBus.subscribe(ResolutionChangedEvent.class, new EventListener<ResolutionChangedEvent>() { eventBus.subscribe(ResolutionChangedEvent.class, new EventListener<ResolutionChangedEvent>() {
@Override @Override
public void onEvent(ResolutionChangedEvent event) { public void onEvent(ResolutionChangedEvent event) {
if (timeShown >= NOTIFICATION_TIME) { if (timeShown >= TOTAL_TIME) {
return; return;
} }
calculatePosition(); calculatePosition();
@ -58,22 +67,44 @@ public class BarNotificationState implements EventListener<BarNotificationEvent>
} }
public void render(Graphics g, int delta) { public void render(Graphics g, int delta) {
if (timeShown >= NOTIFICATION_TIME) { if (timeShown >= TOTAL_TIME) {
return; return;
} }
timeShown += delta; timeShown += delta;
processAnimations();
g.setColor(bgcol); g.setColor(bgcol);
g.fillRect(0, barY, displayContainer.width, barHeight); g.fillRect(0, displayContainer.height / 2 - barHalfHeight, displayContainer.width, barHalfHeight * 2);
Fonts.LARGE.drawString(textX, textY, message); int y = textY;
for (String line : lines) {
Fonts.LARGE.drawString((displayContainer.width - Fonts.LARGE.getWidth(line)) / 2, y, line, textCol);
y += Fonts.LARGE.getLineHeight();
}
}
private void processAnimations() {
if (timeShown < IN_TIME) {
float progress = (float) timeShown / IN_TIME;
barHalfHeight = (int) (barHalfTargetHeight * AnimationEquation.OUT_BACK.calc(progress));
textCol.a = progress;
bgcol.a = 0.4f * progress;
return;
}
if (timeShown > DISPLAY_TIME) {
float progress = 1f - (float) (timeShown - DISPLAY_TIME) / OUT_TIME;
textCol.a = progress;
bgcol.a = 0.4f * progress;
return;
}
barHalfHeight = barHalfTargetHeight;
textCol.a = 1f;
bgcol.a = 0.4f;
} }
private void calculatePosition() { private void calculatePosition() {
int textHeight = Fonts.LARGE.getHeight(message); this.lines = Fonts.wrap(Fonts.LARGE, message, (int) (displayContainer.width * 0.96f), true);
int textWidth = Fonts.LARGE.getWidth(message); int textHeight = (int) (Fonts.LARGE.getLineHeight() * (lines.size() + 0.5f));
textX = (displayContainer.width - textWidth) / 2; textY = (displayContainer.height - textHeight) / 2 + Fonts.LARGE.getLineHeight() / 8;
textY = (displayContainer.height - textHeight) / 2; barHalfTargetHeight = textHeight / 2 + Fonts.LARGE.getLineHeight() / 8;
barY = textY - 5; // TODO uiscale stuff?
barHeight = textHeight + 10;
} }
@Override @Override