@@ 24,7 24,6 @@ import com.badlogic.gdx.scenes.scene2d.u
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Payload;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Source;
-import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop.Target;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pools;
@@ 123,7 122,7 @@ public class JigsawPuzzle {
}
@Override
- public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, Target target) {
+ public void dragStop(InputEvent event, float x, float y, int pointer, Payload payload, DragAndDrop.Target target) {
final Actor actor = payload.getDragActor();
if(actor.getParent() == null) { // move back to where the piece was dragged from
// put it on the stage in the correct position
@@ 144,27 143,69 @@ public class JigsawPuzzle {
};
}
- public Target createTarget(final Group board, final Runnable solvedCallback) {
- return new Target(board) {
- @Override
- public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
- return true;
- }
+ /** @author dermetfan */
+ public static class Target extends DragAndDrop.Target {
+
+ /** the puzzle */
+ private JigsawPuzzle puzzle;
+
+ /** the distance by which each piece is allowed to be off */
+ private float tolerance;
- @Override
- public void drop(Source source, Payload payload, float x, float y, int pointer) {
- Actor dragged = payload.getDragActor();
- Scene2DUtils.addAtStageCoordinates(dragged, board);
- if(dragged instanceof Piece) {
- Piece piece = (Piece) dragged, ref = findClosest(piece);
- if(piece.isPlacedCorrectly(ref, Constants.tolerance)) {
- piece.place(ref); // snap it into its perfect position in relation to some already placed piece
- if(isSolved(Constants.tolerance) && solvedCallback != null)
- solvedCallback.run();
- }
+ /** @param tolerance the {@link #tolerance} */
+ public Target(Group group, JigsawPuzzle puzzle, float tolerance) {
+ super(group);
+ this.puzzle = puzzle;
+ this.tolerance = tolerance;
+ }
+
+ @Override
+ public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
+ return true;
+ }
+
+ @Override
+ public void drop(Source source, Payload payload, float x, float y, int pointer) {
+ Actor dragged = payload.getDragActor();
+ Scene2DUtils.addAtStageCoordinates(dragged, (Group) getActor());
+ if(dragged instanceof Piece) {
+ Piece piece = (Piece) dragged, ref = puzzle.findClosest(piece);
+ if(piece.isPlacedCorrectly(ref, tolerance)) {
+ piece.place(ref); // snap it into its perfect position in relation to some already placed piece
+ placed(piece);
}
}
- };
+ }
+
+ protected void placed(Piece piece) {
+ if(puzzle.isSolved(tolerance))
+ solved();
+ }
+
+ protected void solved() {}
+
+ // getters and setters
+
+ /** @return the {@link #puzzle} */
+ public JigsawPuzzle getPuzzle() {
+ return puzzle;
+ }
+
+ /** @param puzzle the {@link #puzzle} to set */
+ public void setPuzzle(JigsawPuzzle puzzle) {
+ this.puzzle = puzzle;
+ }
+
+ /** @return the {@link #tolerance} */
+ public float getTolerance() {
+ return tolerance;
+ }
+
+ /** @param tolerance the {@link #tolerance} to set */
+ public void setTolerance(float tolerance) {
+ this.tolerance = tolerance;
+ }
+
}
/** a piece on a {@link JigsawPuzzle}
@@ 229,6 270,8 @@ public class JigsawPuzzle {
return rel;
}
+ // getters and setters
+
/** @return the {@link #slotX} */
public float getSlotX() {
return slotX;
@@ 32,6 32,7 @@ import net.dermetfan.jigsawPuzzle.Assets
import net.dermetfan.jigsawPuzzle.Constants;
import net.dermetfan.jigsawPuzzle.puzzle.JigsawPuzzle;
import net.dermetfan.jigsawPuzzle.puzzle.JigsawPuzzle.Piece;
+import net.dermetfan.jigsawPuzzle.puzzle.JigsawPuzzle.Target;
import net.dermetfan.jigsawPuzzle.utils.PolygonRegionDrawable;
/** a UI for a {@link JigsawPuzzle}
@@ 47,7 48,7 @@ public class PlayScreen extends ScreenAd
Gdx.input.getTextInput(new TextInputListener() { // temporary solution - ask user for name of the level directory to load psh files (pieces) from
@Override
public void input(String level) {
- initUI(level);
+ start(level);
}
@Override
@@ 63,9 64,9 @@ public class PlayScreen extends ScreenAd
Gdx.input.setInputProcessor(stage); // let the stage receive input events
}
- /** creates the UI and puzzle
+ /** creates the puzzle
* @param level the name of the level directory in {@link Assets#puzzlesDir} to load the puzzle from */
- private void initUI(String level) {
+ private void start(String level) {
stage.clear(); // clear everything for subsequent calls
Table table = new Table();
if(Constants.debug)
@@ 93,15 94,14 @@ public class PlayScreen extends ScreenAd
DragAndDrop dnd = new DragAndDrop();
dnd.addSource(puzzle.createSource(board, dnd));
- dnd.addTarget(puzzle.createTarget(board, new Runnable() {
+ dnd.addTarget(new Target(board, puzzle, Constants.tolerance) {
@Override
- public void run() {
+ protected void solved() {
dialog.hide();
- Gdx.input.getTextInput(new TextInputListener() { // temporary solution
-
+ Gdx.input.getTextInput(new TextInputListener() {
@Override
public void input(String level) {
- initUI(level);
+ start(level);
}
@Override
@@ 111,7 111,7 @@ public class PlayScreen extends ScreenAd
}, "Puzzle solved!", "", "Play again? Enter level name");
}
- }));
+ });
dialog.getContentTable().add(board).expand().fill().minSize(nextX, tallest);