getting rid of code repetition

This commit is contained in:
yugecin 2016-12-26 23:36:25 +01:00
parent af50fc01ad
commit b562a50254
5 changed files with 135 additions and 91 deletions

View File

@ -0,0 +1,95 @@
/*
* 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.render;
import itdelatrisu.opsu.objects.curves.Vec2f;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import java.util.ArrayList;
public class MovablePointCollectionRenderer extends ArrayList<Vec2f> {
private static final int POINTSIZE = 6;
private final Color pointColor;
private Vec2f currentPoint;
private int currentPointIndex;
public MovablePointCollectionRenderer(Color pointColor) {
this.pointColor = pointColor;
}
public int getCurrentPointIndex() {
return currentPointIndex;
}
public boolean update(int x, int y) {
if (currentPoint != null) {
currentPoint.x = x;
currentPoint.y = y;
return true;
}
return false;
}
public boolean mousePressed(int x, int y) {
currentPointIndex = 0;
for (Vec2f point : this) {
if (point.x - POINTSIZE <= x && x <= point.x + POINTSIZE && point.y - POINTSIZE <= y && y <= point.y + POINTSIZE) {
currentPoint = point;
return true;
}
currentPointIndex++;
}
return false;
}
public boolean mouseReleased() {
if (currentPoint != null) {
currentPoint = null;
return true;
}
return false;
}
public void render(Graphics g) {
g.setColor(pointColor);
for (Vec2f point : this) {
RenderUtils.fillCenteredRect(g, point.x, point.y, POINTSIZE);
}
}
public void renderWithDottedLines(Graphics g, Color lineColor, Vec2f start, Vec2f end) {
g.setColor(lineColor);
Vec2f lastPoint = start;
for (Vec2f point : this) {
if (lastPoint == null) {
continue;
}
RenderUtils.drawDottedLine(g, lastPoint.x, lastPoint.y, point.x, point.y, 20, 0);
lastPoint = point;
}
if (lastPoint != null && end != null) {
RenderUtils.drawDottedLine(g, lastPoint.x, lastPoint.y, end.x, end.y, 20, 0);
}
render(g);
}
}

View File

@ -22,6 +22,7 @@ import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.animations.AnimationEquation; 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.render.MovablePointCollectionRenderer;
import yugecin.opsudance.render.RenderUtils; import yugecin.opsudance.render.RenderUtils;
import yugecin.opsudance.sbv2.movers.StoryboardMover; import yugecin.opsudance.sbv2.movers.StoryboardMover;
@ -30,17 +31,14 @@ import java.util.List;
public class StoryboardMoveImpl implements StoryboardMove { public class StoryboardMoveImpl implements StoryboardMove {
private static final int POINTSIZE = 6; private final int screenWidth;
private int screenWidth; private final Vec2f start;
private final Vec2f end;
private Vec2f start; private final List<StoryboardMover> movers;
private Vec2f end; private final MovablePointCollectionRenderer movablePointCollectionRenderer;
private List<StoryboardMover> movers;
private List<Vec2f> midPoints;
private StoryboardMover nextMover; private StoryboardMover nextMover;
private Vec2f currentPoint;
private StoryboardMover prevMover; private StoryboardMover prevMover;
private float totalLength; private float totalLength;
@ -55,7 +53,7 @@ public class StoryboardMoveImpl implements StoryboardMove {
this.end = end; this.end = end;
this.screenWidth = screenWidth; this.screenWidth = screenWidth;
movers = new ArrayList<>(); movers = new ArrayList<>();
midPoints = new ArrayList<>(); movablePointCollectionRenderer = new MovablePointCollectionRenderer(Color.cyan);
recalculateDelay = 700; recalculateDelay = 700;
} }
@ -80,7 +78,7 @@ public class StoryboardMoveImpl implements StoryboardMove {
(lastMover.start.x + lastMover.end.x) / 2f, (lastMover.start.x + lastMover.end.x) / 2f,
(lastMover.start.y + lastMover.end.y) / 2f (lastMover.start.y + lastMover.end.y) / 2f
); );
midPoints.add(mid); movablePointCollectionRenderer.add(mid);
lastMover.end = mid; lastMover.end = mid;
totalLength -= lastMover.getLength(); totalLength -= lastMover.getLength();
lastMover.recalculateLength(); lastMover.recalculateLength();
@ -114,7 +112,7 @@ public class StoryboardMoveImpl implements StoryboardMove {
for (StoryboardMover mover : movers) { for (StoryboardMover mover : movers) {
mover.update(delta, x, y); mover.update(delta, x, y);
} }
if (currentPoint != null) { if (movablePointCollectionRenderer.update(x, y)) {
moveCurrentPoint(x, y); moveCurrentPoint(x, y);
recalculateDelay -= delta; recalculateDelay -= delta;
if (recalculateDelay < 0) { if (recalculateDelay < 0) {
@ -127,27 +125,25 @@ public class StoryboardMoveImpl implements StoryboardMove {
@Override @Override
public void mousePressed(int x, int y) { public void mousePressed(int x, int y) {
int i = 0;
for(StoryboardMover mover : movers) { for(StoryboardMover mover : movers) {
if (mover.mousePressed(x, y)) { if (mover.mousePressed(x, y)) {
return; return;
} }
} }
for (Vec2f point : midPoints) { if (movablePointCollectionRenderer.mousePressed(x, y)) {
if (point.x - POINTSIZE <= x && x <= point.x + POINTSIZE && point.y - POINTSIZE <= y && y <= point.y + POINTSIZE) { prevMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex());
currentPoint = point; nextMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex() + 1);
prevMover = movers.get(i);
nextMover = movers.get(i + 1);
return;
}
i++;
} }
} }
@Override @Override
public void mouseReleased(int x, int y) { public void mouseReleased(int x, int y) {
int posY = 200; int posY = 200;
if (currentPoint == null) { if (movablePointCollectionRenderer.mouseReleased()) {
moveCurrentPoint(x, y);
recalculateLengths();
recalculateTimes();
} else {
for (int i = 0; i < movers.size(); i++) { for (int i = 0; i < movers.size(); i++) {
int dif = posY; int dif = posY;
posY += Fonts.SMALL.getLineHeight() * 1.1f; posY += Fonts.SMALL.getLineHeight() * 1.1f;
@ -159,10 +155,10 @@ public class StoryboardMoveImpl implements StoryboardMove {
} }
StoryboardMover mover = movers.get(i); StoryboardMover mover = movers.get(i);
if (i == movers.size() - 1) { if (i == movers.size() - 1) {
midPoints.remove(i - 1); movablePointCollectionRenderer.remove(i - 1);
movers.get(i - 1).end = mover.end; movers.get(i - 1).end = mover.end;
} else { } else {
midPoints.remove(i); movablePointCollectionRenderer.remove(i);
movers.get(i + 1).start = mover.start; movers.get(i + 1).start = mover.start;
} }
movers.remove(i); movers.remove(i);
@ -179,11 +175,6 @@ public class StoryboardMoveImpl implements StoryboardMove {
recalculateTimes(); recalculateTimes();
} }
} }
} else {
moveCurrentPoint(x, y);
recalculateLengths();
recalculateTimes();
currentPoint = null;
} }
recalculateDelay = 700; recalculateDelay = 700;
} }
@ -204,10 +195,8 @@ public class StoryboardMoveImpl implements StoryboardMove {
} }
private void moveCurrentPoint(int x, int y) { private void moveCurrentPoint(int x, int y) {
currentPoint.x = x; prevMover.end.set(x, y);
currentPoint.y = y; nextMover.start.set(x, y);
prevMover.end = currentPoint;
nextMover.start = currentPoint;
} }
@Override @Override
@ -223,10 +212,7 @@ public class StoryboardMoveImpl implements StoryboardMove {
dif = y - dif; dif = y - dif;
RenderUtils.fillCenteredRect(g, screenWidth - 20, y - dif / 2, 5); RenderUtils.fillCenteredRect(g, screenWidth - 20, y - dif / 2, 5);
} }
g.setColor(Color.cyan); movablePointCollectionRenderer.render(g);
for (Vec2f point : midPoints) {
RenderUtils.fillCenteredRect(g, point.x, point.y, POINTSIZE);
}
} }
} }

View File

@ -29,16 +29,18 @@ public class CubicStoryboardMover extends StoryboardMultipointMover {
@Override @Override
public void setInitialStart(Vec2f start) { public void setInitialStart(Vec2f start) {
super.setInitialStart(start); super.setInitialStart(start);
super.addPoint(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2)); super.movablePointCollectionRenderer.add(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2));
super.addPoint(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2)); super.movablePointCollectionRenderer.add(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2));
} }
@Override @Override
public float[] getPointAt(float t) { public float[] getPointAt(float t) {
float ct = 1f - t; float ct = 1f - t;
Vec2f p1 = super.movablePointCollectionRenderer.get(0);
Vec2f p2 = super.movablePointCollectionRenderer.get(1);
return new float[] { return new float[] {
ct * ct * ct * start.x + 3 * ct * ct * t * getPoint(0).x + 3 * ct * t * t * getPoint(1).x + t * t * t * end.x, ct * ct * ct * start.x + 3 * ct * ct * t * p1.x + 3 * ct * t * t * p2.x + t * t * t * end.x,
ct * ct * ct * start.y + 3 * ct * ct * t * getPoint(0).y + 3 * ct * t * t * getPoint(1).y + t * t * t * end.y, ct * ct * ct * start.y + 3 * ct * ct * t * p1.y + 3 * ct * t * t * p2.y + t * t * t * end.y,
}; };
} }

View File

@ -29,15 +29,16 @@ public class QuadraticStoryboardMover extends StoryboardMultipointMover {
@Override @Override
public void setInitialStart(Vec2f start) { public void setInitialStart(Vec2f start) {
super.setInitialStart(start); super.setInitialStart(start);
super.addPoint(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2)); super.movablePointCollectionRenderer.add(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2));
} }
@Override @Override
public float[] getPointAt(float t) { public float[] getPointAt(float t) {
float ct = 1f - t; float ct = 1f - t;
Vec2f p1 = super.movablePointCollectionRenderer.get(0);
return new float[] { return new float[] {
ct * ct * start.x + ct * 2 * t * getPoint(0).x + t * t * end.x, ct * ct * start.x + ct * 2 * t * p1.x + t * t * end.x,
ct * ct * start.y + ct * 2 * t * getPoint(0).y + t * t * end.y, ct * ct * start.y + ct * 2 * t * p1.y + t * t * end.y,
}; };
} }

View File

@ -17,78 +17,38 @@
*/ */
package yugecin.opsudance.sbv2.movers; package yugecin.opsudance.sbv2.movers;
import itdelatrisu.opsu.objects.curves.Vec2f;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics; import org.newdawn.slick.Graphics;
import yugecin.opsudance.render.RenderUtils; import yugecin.opsudance.render.MovablePointCollectionRenderer;
import java.util.ArrayList;
import java.util.List;
public abstract class StoryboardMultipointMover extends StoryboardMover { public abstract class StoryboardMultipointMover extends StoryboardMover {
private static final int POINTSIZE = 6; protected final MovablePointCollectionRenderer movablePointCollectionRenderer;
private List<Vec2f> points;
private final Color pointColor;
private Vec2f currentPoint;
public StoryboardMultipointMover(Color renderColor, Color pointColor) { public StoryboardMultipointMover(Color renderColor, Color pointColor) {
super(renderColor); super(renderColor);
this.pointColor = pointColor; movablePointCollectionRenderer = new MovablePointCollectionRenderer(pointColor);
this.points = new ArrayList<>();
}
public void addPoint(Vec2f point) {
points.add(point);
} }
@Override @Override
public void update(int delta, int x, int y) { public void update(int delta, int x, int y) {
if (currentPoint != null) { movablePointCollectionRenderer.update(x, y);
currentPoint.x = x;
currentPoint.y = y;
}
} }
@Override @Override
public boolean mousePressed(int x, int y) { public boolean mousePressed(int x, int y) {
for (Vec2f point : points) { return movablePointCollectionRenderer.mousePressed(x, y);
if (point.x - POINTSIZE <= x && x <= point.x + POINTSIZE && point.y - POINTSIZE <= y && y <= point.y + POINTSIZE) {
currentPoint = point;
return true;
}
}
return false;
} }
@Override @Override
public boolean mouseReleased(int x, int y) { public boolean mouseReleased(int x, int y) {
if (currentPoint != null) { return movablePointCollectionRenderer.mouseReleased();
currentPoint = null;
return true;
}
return false;
}
protected Vec2f getPoint(int index) {
return points.get(index);
} }
@Override @Override
public void render(Graphics g) { public void render(Graphics g) {
g.setColor(Color.gray); movablePointCollectionRenderer.renderWithDottedLines(g, Color.gray, start, end);
Vec2f lastPoint = start;
for (Vec2f point : points) {
RenderUtils.drawDottedLine(g, lastPoint.x, lastPoint.y, point.x, point.y, 20, 0);
lastPoint = point;
}
RenderUtils.drawDottedLine(g, lastPoint.x, lastPoint.y, end.x, end.y, 20, 0);
g.setColor(pointColor);
for (Vec2f point : points) {
RenderUtils.fillCenteredRect(g, point.x, point.y, POINTSIZE);
}
super.render(g); super.render(g);
} }
} }