opsu-dance/src/yugecin/opsudance/sbv2/StoryboardMoveImpl.java

219 lines
6.1 KiB
Java
Raw Normal View History

2016-12-25 12:16:32 +01:00
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2016 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.sbv2;
import itdelatrisu.opsu.objects.curves.Vec2f;
import itdelatrisu.opsu.ui.Fonts;
2016-12-25 15:46:11 +01:00
import itdelatrisu.opsu.ui.animations.AnimationEquation;
2016-12-25 12:16:32 +01:00
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
2016-12-26 23:36:25 +01:00
import yugecin.opsudance.render.MovablePointCollectionRenderer;
2016-12-25 14:19:08 +01:00
import yugecin.opsudance.render.RenderUtils;
2016-12-25 12:16:32 +01:00
import yugecin.opsudance.sbv2.movers.StoryboardMover;
import java.util.ArrayList;
import java.util.List;
public class StoryboardMoveImpl implements StoryboardMove {
2016-12-26 23:36:25 +01:00
private final int screenWidth;
2016-12-25 13:19:54 +01:00
2016-12-26 23:36:25 +01:00
private final Vec2f start;
private final Vec2f end;
private final List<StoryboardMover> movers;
private final MovablePointCollectionRenderer movablePointCollectionRenderer;
2016-12-25 12:16:32 +01:00
private StoryboardMover nextMover;
private StoryboardMover prevMover;
private float totalLength;
private int recalculateDelay;
2016-12-25 15:46:11 +01:00
private AnimationEquation animationEquation;
2016-12-25 13:19:54 +01:00
public StoryboardMoveImpl(Vec2f start, Vec2f end, int screenWidth) {
2016-12-25 15:46:11 +01:00
this.animationEquation = AnimationEquation.LINEAR;
2016-12-25 12:16:32 +01:00
this.start = start;
this.end = end;
2016-12-25 13:19:54 +01:00
this.screenWidth = screenWidth;
2016-12-25 12:16:32 +01:00
movers = new ArrayList<>();
2016-12-26 23:36:25 +01:00
movablePointCollectionRenderer = new MovablePointCollectionRenderer(Color.cyan);
recalculateDelay = 700;
2016-12-25 12:16:32 +01:00
}
2016-12-25 15:46:11 +01:00
@Override
public void setAnimationEquation(AnimationEquation animationEquation) {
this.animationEquation = animationEquation;
}
2016-12-25 13:19:54 +01:00
@Override
public int getAmountOfMovers() {
return movers.size();
}
2016-12-25 12:16:32 +01:00
@Override
public void add(StoryboardMover mover) {
mover.end = end;
if (movers.size() == 0) {
2016-12-25 13:58:31 +01:00
mover.setInitialStart(start);
2016-12-25 12:16:32 +01:00
} else {
StoryboardMover lastMover = movers.get(movers.size() - 1);
Vec2f mid = new Vec2f(
(lastMover.start.x + lastMover.end.x) / 2f,
(lastMover.start.y + lastMover.end.y) / 2f
);
2016-12-26 23:36:25 +01:00
movablePointCollectionRenderer.add(mid);
2016-12-25 12:16:32 +01:00
lastMover.end = mid;
totalLength -= lastMover.getLength();
lastMover.recalculateLength();
totalLength += lastMover.getLength();
2016-12-25 13:58:31 +01:00
mover.setInitialStart(mid);
2016-12-25 12:16:32 +01:00
}
2016-12-25 13:58:31 +01:00
mover.recalculateLength();
totalLength += mover.getLength();
2016-12-25 12:16:32 +01:00
movers.add(mover);
recalculateTimes();
}
@Override
public float[] getPointAt(float t) {
if (movers.size() == 0) {
return new float[] { end.x, end.y };
}
2016-12-25 15:46:11 +01:00
t = animationEquation.calc(t);
2016-12-25 12:16:32 +01:00
float cumulativeTime = 0f;
for (StoryboardMover mover : movers) {
cumulativeTime += mover.timeLengthPercentOfTotalTime;
if (cumulativeTime > t) {
return mover.getPointAt((t - (cumulativeTime - mover.timeLengthPercentOfTotalTime)) / mover.timeLengthPercentOfTotalTime);
}
}
return new float[] { end.x, end.y };
}
@Override
public void update(int delta, int x, int y) {
2016-12-25 13:58:31 +01:00
for (StoryboardMover mover : movers) {
mover.update(delta, x, y);
}
2016-12-26 23:36:25 +01:00
if (movablePointCollectionRenderer.update(x, y)) {
2016-12-25 12:16:32 +01:00
moveCurrentPoint(x, y);
recalculateDelay -= delta;
if (recalculateDelay < 0) {
recalculateDelay = 700;
recalculateLengths();
recalculateTimes();
}
2016-12-25 12:16:32 +01:00
}
}
@Override
public void mousePressed(int x, int y) {
2016-12-25 13:58:31 +01:00
for(StoryboardMover mover : movers) {
if (mover.mousePressed(x, y)) {
return;
}
}
2016-12-26 23:36:25 +01:00
if (movablePointCollectionRenderer.mousePressed(x, y)) {
prevMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex());
nextMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex() + 1);
2016-12-25 12:16:32 +01:00
}
}
@Override
public void mouseReleased(int x, int y) {
2016-12-25 13:19:54 +01:00
int posY = 200;
2016-12-26 23:36:25 +01:00
if (movablePointCollectionRenderer.mouseReleased()) {
moveCurrentPoint(x, y);
recalculateLengths();
recalculateTimes();
} else {
2016-12-25 13:19:54 +01:00
for (int i = 0; i < movers.size(); i++) {
int dif = posY;
posY += Fonts.SMALL.getLineHeight() * 1.1f;
dif = posY - dif;
if (screenWidth - 20 <= x && x <= screenWidth - 10 && posY - dif / 2 - 5 <= y && y <= posY - dif / 2 + 5) {
if (movers.size() == 1) {
movers.clear();
return;
}
StoryboardMover mover = movers.get(i);
if (i == movers.size() - 1) {
2016-12-26 23:36:25 +01:00
movablePointCollectionRenderer.remove(i - 1);
2016-12-25 13:19:54 +01:00
movers.get(i - 1).end = mover.end;
} else {
2016-12-26 23:36:25 +01:00
movablePointCollectionRenderer.remove(i);
2016-12-25 13:19:54 +01:00
movers.get(i + 1).start = mover.start;
}
movers.remove(i);
totalLength -= mover.getLength();
recalculateTimes();
2016-12-25 13:58:31 +01:00
return;
}
}
for(StoryboardMover mover : movers) {
float prevLen = mover.getLength();
if (mover.mouseReleased(x, y)) {
mover.recalculateLength();
totalLength += mover.getLength() - prevLen;
recalculateTimes();
2016-12-25 13:19:54 +01:00
}
}
2016-12-25 12:16:32 +01:00
}
recalculateDelay = 700;
2016-12-25 12:16:32 +01:00
}
@Override
public void recalculateTimes() {
for (StoryboardMover mover : movers) {
mover.timeLengthPercentOfTotalTime = mover.getLength() / totalLength;
}
}
private void recalculateLengths() {
totalLength -= prevMover.getLength() + nextMover.getLength();
prevMover.recalculateLength();
nextMover.recalculateLength();
totalLength += prevMover.getLength() + nextMover.getLength();
recalculateTimes();
}
2016-12-25 12:16:32 +01:00
private void moveCurrentPoint(int x, int y) {
2016-12-26 23:36:25 +01:00
prevMover.end.set(x, y);
nextMover.start.set(x, y);
2016-12-25 12:16:32 +01:00
}
@Override
public void render(Graphics g) {
2016-12-25 13:19:54 +01:00
g.setColor(Color.red);
int y = 200;
2016-12-25 12:16:32 +01:00
for (StoryboardMover mover : movers) {
mover.render(g);
2016-12-25 13:19:54 +01:00
String text = mover.toString();
Fonts.SMALL.drawString(screenWidth - Fonts.SMALL.getWidth(text) - 30, y, text);
int dif = y;
y += Fonts.SMALL.getLineHeight() * 1.1f;
dif = y - dif;
2016-12-25 14:19:08 +01:00
RenderUtils.fillCenteredRect(g, screenWidth - 20, y - dif / 2, 5);
2016-12-25 12:16:32 +01:00
}
2016-12-26 23:36:25 +01:00
movablePointCollectionRenderer.render(g);
2016-12-25 12:16:32 +01:00
}
}