Skip to content

Commit

Permalink
small modifications to the SVGPaths creation
Browse files Browse the repository at this point in the history
Adding a setting pane to be able to change language in game
  • Loading branch information
hellminister committed Feb 3, 2021
1 parent 8f1dba3 commit bb7bfd1
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/pc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/puzzlegame/language/Localize.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public static void load(String language){
localize.loadLanguage(language);
}

public static String getCurrentLanguage(){
return localize.getLanguage();
}

private final Map<Target, StringProperty> localizedStrings;
private String language;

private Localize() {
EnumMap<Target, StringProperty> temp = new EnumMap<>(Target.class);
Expand All @@ -44,9 +49,8 @@ private ReadOnlyStringProperty getString(Target target){
return localizedStrings.get(target);
}



private void loadLanguage(String language){
this.language = language;
EnumSet<Target> notLoaded = EnumSet.allOf(Target.class);
try (BufferedReader br = Files.newBufferedReader(Paths.get("src/resources/language/" + language + ".txt"))) {
String line = br.readLine();
Expand All @@ -72,10 +76,15 @@ private void loadLanguage(String language){
}
}

private String getLanguage(){
return language;
}

public enum Target{
GAME_TITLE,
START_SCREEN_CONTINUE,
START_SCREEN_START_NEW,
START_SCREEN_SETTINGS,
START_SCREEN_QUIT,
PUZZLE_SCENE_ZOOM,
PUZZLE_SCENE_SHOW_IMAGE,
Expand All @@ -85,7 +94,11 @@ public enum Target{
PUZZLE_CHOOSER_SCENE_NB_PIECE_TEXT,
PUZZLE_CHOOSER_SCENE_START,
PUZZLE_CHOOSER_SCENE_CANCEL,
SETTINGS_SCENE_LANGUAGE,
SETTINGS_SCENE_ACCEPT_BUTTON,
SETTINGS_SCENE_CANCEL_BUTTON,
SETTINGS_SCENE_APPLY_BUTTON,
PUZZLE_SCENE_SHOW_MINIMAP

}
}
}
8 changes: 1 addition & 7 deletions src/puzzlegame/puzzlescreen/puzzletable/Puzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import puzzlegame.puzzlescreen.factors.Factor;
import puzzlegame.puzzlescreen.factors.Factors;
import puzzlegame.puzzlescreen.puzzletable.puzzlepiece.svgpath.PuzzleMaker;

import java.util.ArrayList;
Expand All @@ -26,10 +24,6 @@ public Puzzle(Image image, int numberOfPieces, PuzzleTable puzzleScene){

finished = new FinishedPuzzle(fragments);

Factor fact = (new Factors(numberOfPieces)).nearestRatioTo(image.getWidth()/image.getHeight());

Size size = new Size(image.getWidth()/fact.getX(), image.getHeight()/fact.getY());

var piecesTemp = PuzzleMaker.makePuzzle(image, numberOfPieces);

for (PuzzlePiece piece : piecesTemp){
Expand Down Expand Up @@ -86,4 +80,4 @@ protected boolean computeValue() {
return puzzle.size() <= 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public final String getLineText(Size size, Side side){
return stripM(result);
}

protected final SVGPoint markPoint(double xy, double yx, Direction dir){
return switch (dir){
case VERTICAL -> new SVGPoint(xy, yx);
case HORIZONTAL -> new SVGPoint(yx, xy);
};
}

protected final double parallelSize(Size size, Direction dir){
return (switch (dir){
case VERTICAL -> size.y();
case HORIZONTAL -> size.x();
});
}

protected final double perpendicularSize(Size size, Direction dir){
return (switch (dir){
case VERTICAL -> size.x();
case HORIZONTAL -> size.y();
});
}

private String stripM(String path){
return path.replaceAll("^(M|m)( |[0-9]|\\.)*", "");
}
Expand Down Expand Up @@ -87,4 +108,4 @@ public String adjustedCommand(Size size, Side side){

}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package puzzlegame.puzzlescreen.puzzletable.puzzlepiece.svgpath;

import java.util.Arrays;
import java.util.Set;

@SuppressWarnings("unused")
enum Operators {
M(1, points -> "M " + points[0].x() + " " + points[0].y()),
Expand All @@ -16,8 +19,8 @@ enum Operators {
s(2, points -> "s " + points[0].x() + " " + points[0].y() + " " + points[1].x() + " " + points[1].y()),
Q(2, points -> "Q " + points[0].x() + " " + points[0].y() + " " + points[1].x() + " " + points[1].y()),
q(2, points -> "q " + points[0].x() + " " + points[0].y() + " " + points[1].x() + " " + points[1].y()),
T(2, points -> "T " + points[0].x() + " " + points[0].y() + " " + points[1].x() + " " + points[1].y()),
t(2, points -> "t " + points[0].x() + " " + points[0].y() + " " + points[1].x() + " " + points[1].y()),
T(1, points -> "T " + points[0].x() + " " + points[0].y()),
t(1, points -> "t " + points[0].x() + " " + points[0].y()),
Z(0, points -> "Z"),
z(0, points -> "z"),
;
Expand All @@ -38,7 +41,13 @@ public int getNbPoints(){
return nbPoints;
}

private static final Set<String> availableOperators = Set.of(Arrays.stream(Operators.values()).map(Enum::name).toArray(String[]::new));

public static boolean isOperator(String operator){
return availableOperators.contains(operator);
}

private interface Transform {
String toString(SVGPoint... points);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package puzzlegame.puzzlescreen.puzzletable.puzzlepiece.svgpath;

import puzzlegame.puzzlescreen.puzzletable.Size;

import java.util.Random;

public class PuzzleLineDrawer extends LineDrawer{

private static final Random rand = new Random();

public PuzzleLineDrawer(Size size, Direction direction) {
super(size, direction);

commands.add(new Command(Operators.M, new SVGPoint(0,0)));

double range = perpendicularSize(size, direction) * 0.2;
double startPin = 0.2 + rand.nextDouble() * 0.6;

// this is horizontal
// M 0,0 C 10,0 10,10 100,-20 Q 150,-60 100,-100 T150,-150 T200,-100 Q150,-60 250,-20 C300,10 350,0 400,0
// M 0,0 L100,-20 Q 150,-60 100,-100 T150,-150 T200,-100 Q150,-60 250,-20 L400,0


// the line from the start of the piece to the start of the dent
commands.add(new Command(Operators.L, markPoint(0, parallelSize(size, direction) * 0.3, direction)));

// lower half of the left* half
commands.add(new Command(Operators.Q, markPoint(perpendicularSize(size, direction) * 0.025, parallelSize(size, direction)* 0.4, direction),
markPoint(perpendicularSize(size, direction) * 0.05, parallelSize(size, direction)* 0.25, direction)));

//upper half of the left* half
commands.add(new Command(Operators.T, markPoint(perpendicularSize(size, direction) * 0.15, parallelSize(size, direction)* 0.35, direction)));

//upper half of the right* half
commands.add(new Command(Operators.T, markPoint(perpendicularSize(size, direction) * 0.06, parallelSize(size, direction)* 0.30, direction)));

// lower half of the right* half
commands.add(new Command(Operators.Q, markPoint(perpendicularSize(size, direction) * 0.025, parallelSize(size, direction)* 0.3, direction),
markPoint(0, parallelSize(size, direction)* 0.40, direction)));

// the line from end of dent to the end of the piece
commands.add((new Command(Operators.L, markPoint(0, parallelSize(size, direction),direction))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ public SVGPath generatePath() {
path.setContent(svgPath);
return path;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package puzzlegame.puzzlescreen.puzzletable.puzzlepiece.svgpath;

import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import puzzlegame.puzzlescreen.minimap.RatioBinding;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Set;


//Does not work with at least T and maybe S
//The algorithm is flawed, seems to work with M L C Q at least
public class SVGPathUtilities {

public static void main(String[] args) {
Expand Down Expand Up @@ -56,7 +57,6 @@ public static SVGPathBinding adjustableSize(String path, RatioBinding ratio){
return new SVGPathBinding(parsed, ratio);
}

private static final Set<String> acceptedOperators = Set.of("M", "m", "L", "l", "H", "h", "V", "v", "C", "c", "S", "s", "Q", "q", "T", "t", "Z", "z");
private static SVGPathDescription parse(String toParse){
ObservableList<Operators> operators = FXCollections.observableArrayList();
ObservableList<SVGPoint> points = FXCollections.observableArrayList();
Expand All @@ -65,7 +65,7 @@ private static SVGPathDescription parse(String toParse){

Double x = null;
for (String s : splitted){
if (acceptedOperators.contains(s)){
if (Operators.isOperator(s)){
operators.add(Operators.valueOf(s));
if (x != null){
points.add(new SVGPoint(x,0));
Expand Down Expand Up @@ -106,4 +106,4 @@ private static SVGPoint[] pop(LinkedList<SVGPoint> points, int nbPoints){
return ps;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package puzzlegame.puzzlescreen.puzzletable.puzzlepiece.svgpath;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.SimpleDoubleProperty;

public class SVGPointProperty extends ObjectPropertyBase<SVGPointProperty> {

private final DoubleProperty x;
private final DoubleProperty y;

public SVGPointProperty(double x, double y) {
this.x = new SimpleDoubleProperty(x);
this.y = new SimpleDoubleProperty(y);

this.x.addListener((observable, oldValue, newValue) -> this.fireValueChangedEvent());
this.y.addListener((observable, oldValue, newValue) -> this.fireValueChangedEvent());
}


/**
* Returns the {@code Object} that contains this property. If this property
* is not contained in an {@code Object}, {@code null} is returned.
*
* @return the containing {@code Object} or {@code null}
*/
@Override
public Object getBean() {
return null;
}

/**
* Returns the name of this property. If the property does not have a name,
* this method returns an empty {@code String}.
*
* @return the name or an empty {@code String}
*/
@Override
public String getName() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ public class StraightLineDrawer extends LineDrawer{
public StraightLineDrawer(Size size, Direction direction) {
super(size, direction);
commands.add(new Command(Operators.M, new SVGPoint(0,0)));
switch (direction){
case VERTICAL -> commands.add(new Command(Operators.L, new SVGPoint(0, size.y())));
case HORIZONTAL -> commands.add(new Command(Operators.L, new SVGPoint(size.x(), 0)));
}

commands.add(new Command(Operators.L, markPoint(0, parallelSize(size, direction), direction)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ public VLineDrawer(Size size, Direction direction) {
super(size, direction);
commands.add(new Command(Operators.M, new SVGPoint(0,0)));

double range = perpendicularSize(size, direction) * 0.2;
double vPoint = rand.nextDouble() * (range * 2) - range;
double position = 0.1 + rand.nextDouble() * 0.8;

switch (direction){
case VERTICAL -> {
double range = size.x() / 5;
double x = rand.nextDouble() * (range * 2) - range;
double position = 0.1 + rand.nextDouble() * 0.8;

commands.add(new Command(Operators.L, new SVGPoint(x, size.y() * position)));
commands.add(new Command(Operators.L, new SVGPoint(0, size.y())));
}
case HORIZONTAL -> {
double range = size.y() / 5;
double y = rand.nextDouble() * (range * 2) - range;
double position = 0.1 + rand.nextDouble() * 0.8;

commands.add(new Command(Operators.L, new SVGPoint(size.x() * position, y)));
commands.add(new Command(Operators.L, new SVGPoint(size.x(), 0)));
}
}
commands.add(new Command(Operators.L, markPoint(vPoint, parallelSize(size, direction) * position, direction)));
commands.add(new Command(Operators.L, markPoint(0, parallelSize(size, direction), direction)));
}
}


}
25 changes: 25 additions & 0 deletions src/puzzlegame/settingsdialog/SettingsDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package puzzlegame.settingsdialog;

import javafx.stage.Modality;
import javafx.stage.Stage;
import puzzlegame.PuzzleMain;

public class SettingsDialog extends Stage {
private final PuzzleMain puzzleGame;
private final SettingsScene scene;


public SettingsDialog(PuzzleMain mainWindow){
super();
puzzleGame = mainWindow;
initOwner(mainWindow.getPrimaryStage());
initModality(Modality.WINDOW_MODAL);
scene = new SettingsScene(this);
setScene(scene);
}

public void showMe(){
scene.update();
show();
}
}
Loading

0 comments on commit bb7bfd1

Please sign in to comment.