From b562a50254ca1fd70192511a1138f84596ee0507 Mon Sep 17 00:00:00 2001 From: yugecin Date: Mon, 26 Dec 2016 23:36:25 +0100 Subject: [PATCH] getting rid of code repetition --- .../MovablePointCollectionRenderer.java | 95 +++++++++++++++++++ .../opsudance/sbv2/StoryboardMoveImpl.java | 58 +++++------ .../sbv2/movers/CubicStoryboardMover.java | 10 +- .../sbv2/movers/QuadraticStoryboardMover.java | 7 +- .../movers/StoryboardMultipointMover.java | 56 ++--------- 5 files changed, 135 insertions(+), 91 deletions(-) create mode 100644 src/yugecin/opsudance/render/MovablePointCollectionRenderer.java diff --git a/src/yugecin/opsudance/render/MovablePointCollectionRenderer.java b/src/yugecin/opsudance/render/MovablePointCollectionRenderer.java new file mode 100644 index 00000000..135a2832 --- /dev/null +++ b/src/yugecin/opsudance/render/MovablePointCollectionRenderer.java @@ -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 . + */ +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 { + + 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); + } + +} diff --git a/src/yugecin/opsudance/sbv2/StoryboardMoveImpl.java b/src/yugecin/opsudance/sbv2/StoryboardMoveImpl.java index e55e2058..06850cd6 100644 --- a/src/yugecin/opsudance/sbv2/StoryboardMoveImpl.java +++ b/src/yugecin/opsudance/sbv2/StoryboardMoveImpl.java @@ -22,6 +22,7 @@ import itdelatrisu.opsu.ui.Fonts; import itdelatrisu.opsu.ui.animations.AnimationEquation; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; +import yugecin.opsudance.render.MovablePointCollectionRenderer; import yugecin.opsudance.render.RenderUtils; import yugecin.opsudance.sbv2.movers.StoryboardMover; @@ -30,17 +31,14 @@ import java.util.List; public class StoryboardMoveImpl implements StoryboardMove { - private static final int POINTSIZE = 6; + private final int screenWidth; - private int screenWidth; - - private Vec2f start; - private Vec2f end; - private List movers; - private List midPoints; + private final Vec2f start; + private final Vec2f end; + private final List movers; + private final MovablePointCollectionRenderer movablePointCollectionRenderer; private StoryboardMover nextMover; - private Vec2f currentPoint; private StoryboardMover prevMover; private float totalLength; @@ -55,7 +53,7 @@ public class StoryboardMoveImpl implements StoryboardMove { this.end = end; this.screenWidth = screenWidth; movers = new ArrayList<>(); - midPoints = new ArrayList<>(); + movablePointCollectionRenderer = new MovablePointCollectionRenderer(Color.cyan); recalculateDelay = 700; } @@ -80,7 +78,7 @@ public class StoryboardMoveImpl implements StoryboardMove { (lastMover.start.x + lastMover.end.x) / 2f, (lastMover.start.y + lastMover.end.y) / 2f ); - midPoints.add(mid); + movablePointCollectionRenderer.add(mid); lastMover.end = mid; totalLength -= lastMover.getLength(); lastMover.recalculateLength(); @@ -114,7 +112,7 @@ public class StoryboardMoveImpl implements StoryboardMove { for (StoryboardMover mover : movers) { mover.update(delta, x, y); } - if (currentPoint != null) { + if (movablePointCollectionRenderer.update(x, y)) { moveCurrentPoint(x, y); recalculateDelay -= delta; if (recalculateDelay < 0) { @@ -127,27 +125,25 @@ public class StoryboardMoveImpl implements StoryboardMove { @Override public void mousePressed(int x, int y) { - int i = 0; for(StoryboardMover mover : movers) { if (mover.mousePressed(x, y)) { return; } } - for (Vec2f point : midPoints) { - if (point.x - POINTSIZE <= x && x <= point.x + POINTSIZE && point.y - POINTSIZE <= y && y <= point.y + POINTSIZE) { - currentPoint = point; - prevMover = movers.get(i); - nextMover = movers.get(i + 1); - return; - } - i++; + if (movablePointCollectionRenderer.mousePressed(x, y)) { + prevMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex()); + nextMover = movers.get(movablePointCollectionRenderer.getCurrentPointIndex() + 1); } } @Override public void mouseReleased(int x, int y) { int posY = 200; - if (currentPoint == null) { + if (movablePointCollectionRenderer.mouseReleased()) { + moveCurrentPoint(x, y); + recalculateLengths(); + recalculateTimes(); + } else { for (int i = 0; i < movers.size(); i++) { int dif = posY; posY += Fonts.SMALL.getLineHeight() * 1.1f; @@ -159,10 +155,10 @@ public class StoryboardMoveImpl implements StoryboardMove { } StoryboardMover mover = movers.get(i); if (i == movers.size() - 1) { - midPoints.remove(i - 1); + movablePointCollectionRenderer.remove(i - 1); movers.get(i - 1).end = mover.end; } else { - midPoints.remove(i); + movablePointCollectionRenderer.remove(i); movers.get(i + 1).start = mover.start; } movers.remove(i); @@ -179,11 +175,6 @@ public class StoryboardMoveImpl implements StoryboardMove { recalculateTimes(); } } - } else { - moveCurrentPoint(x, y); - recalculateLengths(); - recalculateTimes(); - currentPoint = null; } recalculateDelay = 700; } @@ -204,10 +195,8 @@ public class StoryboardMoveImpl implements StoryboardMove { } private void moveCurrentPoint(int x, int y) { - currentPoint.x = x; - currentPoint.y = y; - prevMover.end = currentPoint; - nextMover.start = currentPoint; + prevMover.end.set(x, y); + nextMover.start.set(x, y); } @Override @@ -223,10 +212,7 @@ public class StoryboardMoveImpl implements StoryboardMove { dif = y - dif; RenderUtils.fillCenteredRect(g, screenWidth - 20, y - dif / 2, 5); } - g.setColor(Color.cyan); - for (Vec2f point : midPoints) { - RenderUtils.fillCenteredRect(g, point.x, point.y, POINTSIZE); - } + movablePointCollectionRenderer.render(g); } } diff --git a/src/yugecin/opsudance/sbv2/movers/CubicStoryboardMover.java b/src/yugecin/opsudance/sbv2/movers/CubicStoryboardMover.java index 5c10eb42..7515ae50 100644 --- a/src/yugecin/opsudance/sbv2/movers/CubicStoryboardMover.java +++ b/src/yugecin/opsudance/sbv2/movers/CubicStoryboardMover.java @@ -29,16 +29,18 @@ public class CubicStoryboardMover extends StoryboardMultipointMover { @Override public void setInitialStart(Vec2f start) { super.setInitialStart(start); - super.addPoint(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)); + super.movablePointCollectionRenderer.add(new Vec2f((start.x + end.x) / 2, (start.y + end.y) / 2)); } @Override public float[] getPointAt(float t) { float ct = 1f - t; + Vec2f p1 = super.movablePointCollectionRenderer.get(0); + Vec2f p2 = super.movablePointCollectionRenderer.get(1); 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.y + 3 * ct * ct * t * getPoint(0).y + 3 * ct * t * t * getPoint(1).y + t * t * t * end.y, + 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 * p1.y + 3 * ct * t * t * p2.y + t * t * t * end.y, }; } diff --git a/src/yugecin/opsudance/sbv2/movers/QuadraticStoryboardMover.java b/src/yugecin/opsudance/sbv2/movers/QuadraticStoryboardMover.java index 769f209b..6acc8eb6 100644 --- a/src/yugecin/opsudance/sbv2/movers/QuadraticStoryboardMover.java +++ b/src/yugecin/opsudance/sbv2/movers/QuadraticStoryboardMover.java @@ -29,15 +29,16 @@ public class QuadraticStoryboardMover extends StoryboardMultipointMover { @Override public void setInitialStart(Vec2f 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 public float[] getPointAt(float t) { float ct = 1f - t; + Vec2f p1 = super.movablePointCollectionRenderer.get(0); return new float[] { - ct * ct * start.x + ct * 2 * t * getPoint(0).x + t * t * end.x, - ct * ct * start.y + ct * 2 * t * getPoint(0).y + t * t * end.y, + ct * ct * start.x + ct * 2 * t * p1.x + t * t * end.x, + ct * ct * start.y + ct * 2 * t * p1.y + t * t * end.y, }; } diff --git a/src/yugecin/opsudance/sbv2/movers/StoryboardMultipointMover.java b/src/yugecin/opsudance/sbv2/movers/StoryboardMultipointMover.java index 687c60bb..07a504cd 100644 --- a/src/yugecin/opsudance/sbv2/movers/StoryboardMultipointMover.java +++ b/src/yugecin/opsudance/sbv2/movers/StoryboardMultipointMover.java @@ -17,78 +17,38 @@ */ package yugecin.opsudance.sbv2.movers; -import itdelatrisu.opsu.objects.curves.Vec2f; import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; -import yugecin.opsudance.render.RenderUtils; - -import java.util.ArrayList; -import java.util.List; +import yugecin.opsudance.render.MovablePointCollectionRenderer; public abstract class StoryboardMultipointMover extends StoryboardMover { - private static final int POINTSIZE = 6; - - private List points; - private final Color pointColor; - - private Vec2f currentPoint; + protected final MovablePointCollectionRenderer movablePointCollectionRenderer; public StoryboardMultipointMover(Color renderColor, Color pointColor) { super(renderColor); - this.pointColor = pointColor; - this.points = new ArrayList<>(); - } - - public void addPoint(Vec2f point) { - points.add(point); + movablePointCollectionRenderer = new MovablePointCollectionRenderer(pointColor); } @Override public void update(int delta, int x, int y) { - if (currentPoint != null) { - currentPoint.x = x; - currentPoint.y = y; - } + movablePointCollectionRenderer.update(x, y); } @Override public boolean mousePressed(int x, int y) { - for (Vec2f point : points) { - if (point.x - POINTSIZE <= x && x <= point.x + POINTSIZE && point.y - POINTSIZE <= y && y <= point.y + POINTSIZE) { - currentPoint = point; - return true; - } - } - return false; + return movablePointCollectionRenderer.mousePressed(x, y); } @Override public boolean mouseReleased(int x, int y) { - if (currentPoint != null) { - currentPoint = null; - return true; - } - return false; - } - - protected Vec2f getPoint(int index) { - return points.get(index); + return movablePointCollectionRenderer.mouseReleased(); } @Override public void render(Graphics g) { - g.setColor(Color.gray); - 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); - } + movablePointCollectionRenderer.renderWithDottedLines(g, Color.gray, start, end); super.render(g); } + }