Skip to content

Commit

Permalink
popover save; double click in load game
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelgamix committed May 12, 2017
1 parent ef5839e commit addab76
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 60 deletions.
16 changes: 4 additions & 12 deletions src/main/java/diaballik/Diaballik.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
public class Diaballik extends Application {
public Stage stage;

public final static String DOSSIER_SAUVEGARDES = "/saves";
public final static String DOSSIER_SAUVEGARDES = "saves";
public final static String DOSSIER_TERRAINS = "/defaultTerrains";
public final static String DOSSIER_CSS = "/css";

public final static String EXTENSION_SAUVEGARDE = ".txt";

private final static String CSS_MENU = DOSSIER_CSS + "/DiaballikMenu.css";
private final static String CSS_JEU = DOSSIER_CSS + "/DiaballikJeu.css";
public final static String CSS_DIALOG = DOSSIER_CSS + "/DiaballikDialogs.css";
Expand Down Expand Up @@ -94,7 +96,7 @@ private void initSceneJeu(ConfigurationPartie cp) {
sceneJeu = new Scene(root, CaseVue.LARGEUR * Terrain.LARGEUR + 225, CaseVue.HAUTEUR * Terrain.HAUTEUR + 75);
sceneJeu.setOnKeyPressed(k -> {
if (ctrlS.match(k)) {
actionsControleur.actionSauvegarderJeu(DOSSIER_SAUVEGARDES);
actionsControleur.getActionsVue().montrerPopupSauvegarde();
} else if (ctrlZ.match(k)) {
actionsControleur.actionDefaire();
}
Expand All @@ -117,16 +119,6 @@ private void initSceneMenu() {
Optional<String> ofilename = Dialogs.montrerDialogChoisirFichier(DOSSIER_SAUVEGARDES);
if (ofilename != null)
ofilename.ifPresent(s -> nouveauJeu(DOSSIER_SAUVEGARDES + "/" + s, true));

// Méthode avec Filechooser
// Plus souple mais complexifie le choix pour l'utilisateur
/*FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File("."));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Diaballik sauvegarde", "*.txt"));
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
nouveauJeu(file.getAbsolutePath(), true);
}*/
});

Button regles = new Button("Règles");
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/diaballik/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static BufferedReader readerConditionnel(String fichier, boolean estSauve

if (estSauvegarde) {
try {
br = new BufferedReader(new FileReader("." + fichier));
br = new BufferedReader(new FileReader(fichier));
} catch (IOException e) {}
} else {
final File jarFile = new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().getPath());
Expand All @@ -65,7 +65,7 @@ public static BufferedReader readerConditionnel(String fichier, boolean estSauve
public static Metadonnees getMetadonneesSauvegarde(String fichier) {
Metadonnees md = new Metadonnees();

try (BufferedReader i = new BufferedReader(new FileReader("." + Diaballik.DOSSIER_SAUVEGARDES + "/" + fichier + ".txt"))) {
try (BufferedReader i = new BufferedReader(new FileReader(Diaballik.DOSSIER_SAUVEGARDES + "/" + fichier + Diaballik.EXTENSION_SAUVEGARDE))) {
String sCurrentLine;
if ((sCurrentLine = i.readLine()) != null) { // tour
md.tour = Integer.parseInt(sCurrentLine.split(":")[0]);
Expand All @@ -84,4 +84,9 @@ public static Metadonnees getMetadonneesSauvegarde(String fichier) {

return null;
}

public static boolean saveExists(String filename) {
File f = new File(Diaballik.DOSSIER_SAUVEGARDES + "/" + filename + Diaballik.EXTENSION_SAUVEGARDE);
return f.exists();
}
}
23 changes: 6 additions & 17 deletions src/main/java/diaballik/controleur/ActionsControleur.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import diaballik.model.Jeu;
import diaballik.vue.ActionsVue;
import diaballik.vue.Dialogs;
import javafx.stage.FileChooser;

import java.io.File;

Expand Down Expand Up @@ -33,26 +32,16 @@ public void actionMenu() {
}
}

public void actionSauvegarderJeu(String directory) {
String filename;
final FileChooser fileChooser = new FileChooser();

final File saveDir = new File("." + directory);
public void actionSauvegarderJeu(String filename) {
final File saveDir = new File(Diaballik.DOSSIER_SAUVEGARDES);
if (!saveDir.exists())
saveDir.mkdir();

fileChooser.setInitialDirectory(saveDir);
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Diaballik Sauvegarde", "*.txt"));
File file = fileChooser.showSaveDialog(this.diaballik.stage);
if (file != null) {
filename = file.getAbsolutePath();

if (!filename.endsWith(".txt"))
filename += ".txt";
if (!filename.endsWith(Diaballik.EXTENSION_SAUVEGARDE))
filename += Diaballik.EXTENSION_SAUVEGARDE;

System.out.println("Sauvegarde vers " + filename);
this.jeu.sauvegarde(filename);
}
System.out.println("Sauvegarde vers " + filename);
this.jeu.sauvegarde(Diaballik.DOSSIER_SAUVEGARDES + "/" + filename);
}

public void actionAntijeu() {
Expand Down
141 changes: 115 additions & 26 deletions src/main/java/diaballik/vue/ActionsVue.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package diaballik.vue;

import diaballik.Diaballik;
import diaballik.Utils;
import diaballik.controleur.ActionsControleur;
import diaballik.model.Jeu;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import org.controlsfx.control.PopOver;
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.Glyph;
import org.controlsfx.validation.Severity;
import org.controlsfx.validation.ValidationResult;
import org.controlsfx.validation.ValidationSupport;

import java.util.Observable;
import java.util.Observer;
import java.util.function.Supplier;

public class ActionsVue extends BorderPane implements Observer {
private final ActionsControleur actionsControleur;
Expand All @@ -31,34 +36,114 @@ public class ActionsVue extends BorderPane implements Observer {
private final Label deplInd;
private final Label passInd;

public final Button annuler;
public final Button refaire;
private final Button annuler;
private final Button refaire;

private final Button sauvegarde;

private PopOver p;
private boolean saveExists = false;

private final ValidationSupport validationSupport = new ValidationSupport();

private ValidationResult checkFileExists(Control control, String filename) {
saveExists = Utils.saveExists(filename);
return ValidationResult.fromMessageIf(control, "file exists", Severity.ERROR, saveExists);
}

private PopOver getSauvegarderPopover() {
PopOver p = new PopOver();
if (p != null) return p;

BorderPane b = new BorderPane();
p = new PopOver();
p.setDetachable(false);
p.setArrowLocation(PopOver.ArrowLocation.TOP_CENTER);

b.setTop(new Label("Nom de la sauvegarde :"));
TextField chemin = new TextField("Partie 1");
BorderPane.setMargin(chemin, new Insets(10, 0, 10, 0));
b.setCenter(chemin);
BorderPane contentSave = new BorderPane();
BorderPane contentValider = new BorderPane();
BorderPane contentFin = new BorderPane();

// Déclarations
// contentSave
TextField chemin = new TextField();
Button valider = new Button("Valider");
valider.setAlignment(Pos.CENTER);

b.setBottom(valider);
b.setPadding(new Insets(5));

p.setContentNode(b);
// contentValider
Label contentValiderLabel = new Label();
HBox boutonsChoix = new HBox(5);
Button choixValider = new Button("Ecraser");
Button choixAnnuler = new Button("Annuler");

// contentFin
Label contentFinLabel = new Label("Sauvegarde effectuée.");

// Utils
Timeline t = new Timeline(new KeyFrame(
Duration.seconds(2),
e -> p.setContentNode(contentSave)
));

Supplier<Void> sp = () -> {
if (saveExists) {
contentValiderLabel.setText("Le fichier de sauvegarde " + chemin.getText() + ".txt existe déjà.\n" +
"Voulez-vous le remplacer?");
p.setContentNode(contentValider);
} else {
actionsControleur.actionSauvegarderJeu(chemin.getText());
p.setContentNode(contentFin);
t.play();
}

return null;
};

// Fonction
BorderPane.setMargin(chemin, new Insets(10, 0, 10, 0));
validationSupport.registerValidator(chemin, false, this::checkFileExists);
chemin.setText("Partie 1");
chemin.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER)
sp.get();
});

BorderPane.setAlignment(valider, Pos.CENTER);
valider.setOnAction(e -> sp.get());

contentSave.setPadding(new Insets(5));
contentSave.setTop(new Label("Nom de la sauvegarde :"));
contentSave.setCenter(chemin);
contentSave.setBottom(valider);

BorderPane.setAlignment(contentValiderLabel, Pos.CENTER);

choixValider.setOnAction(e -> {
actionsControleur.actionSauvegarderJeu(chemin.getText());
p.setContentNode(contentFin);
t.play();
});

choixAnnuler.setOnAction(e -> p.setContentNode(contentSave));

boutonsChoix.getChildren().addAll(choixAnnuler, choixValider);
boutonsChoix.setAlignment(Pos.CENTER);
boutonsChoix.setPadding(new Insets(10, 0, 5, 0));

contentValider.setPadding(new Insets(10));
contentValider.setCenter(contentValiderLabel);
contentValider.setBottom(boutonsChoix);

BorderPane.setAlignment(contentFinLabel, Pos.CENTER);

contentFin.setPadding(new Insets(10));
contentFin.setCenter(contentFinLabel);

p.setContentNode(contentSave);

return p;
}

public ActionsVue(ActionsControleur actionsControleur) {
super();

PopOver popSauvegarder = getSauvegarderPopover();

// Infos
VBox vBoxInfos = new VBox(20);
vBoxInfos.setAlignment(Pos.CENTER);
Expand Down Expand Up @@ -133,13 +218,13 @@ public ActionsVue(ActionsControleur actionsControleur) {
refaire.setMaxWidth(Double.MAX_VALUE);
gpActions.add(refaire, 1, 2);

Button sauvegarder = new Button("Sauvegarder");
sauvegarder.setOnAction(e -> {
popSauvegarder.show(sauvegarder);
sauvegarde = new Button("Sauvegarder");
sauvegarde.setOnAction(e -> {
montrerPopupSauvegarde();
//actionsControleur.actionSauvegarderJeu(Diaballik.DOSSIER_SAUVEGARDES)
});
sauvegarder.setMaxWidth(Double.MAX_VALUE);
gpActions.add(sauvegarder, 0, 3, 2, 1);
sauvegarde.setMaxWidth(Double.MAX_VALUE);
gpActions.add(sauvegarde, 0, 3, 2, 1);

Glyph roue = new Glyph("FontAwesome", FontAwesome.Glyph.COG);
roue.setFontSize(22f);
Expand All @@ -159,6 +244,10 @@ public ActionsVue(ActionsControleur actionsControleur) {
update(null, null);
}

public void montrerPopupSauvegarde() {
getSauvegarderPopover().show(sauvegarde);
}

@Override
public void update(Observable o, Object arg) {
int deplRest = jeu.getJoueurActuel().getDeplacementsRestants();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/diaballik/vue/Dialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public static Optional<String> montrerDialogChoisirFichier(String directory) {
}

private Optional<String> getDialogChoisirFichier(String directory) {
ObservableList<String> obs = FXCollections.observableArrayList(getFichiersDansDossier("." + directory, ".txt", false));
ObservableList<String> obs = FXCollections.observableArrayList(getFichiersDansDossier(directory, Diaballik.EXTENSION_SAUVEGARDE, false));

Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Charger une partie");
Expand Down Expand Up @@ -326,6 +326,10 @@ private Optional<String> getDialogChoisirFichier(String directory) {
filesView.setMaxWidth(Double.MAX_VALUE);
filesView.setMaxHeight(Double.MAX_VALUE);
filesView.setOnMouseClicked(e -> {
if (e.getClickCount() == 2) {
dialog.close();
}

if (filesView.getSelectionModel().getSelectedItem() != null)
boutonJouer.setDisable(false);

Expand All @@ -347,8 +351,8 @@ private Optional<String> getDialogChoisirFichier(String directory) {
content.setPadding(new Insets(10));

dialog.setResultConverter(b -> {
if (b == boutonOuvrirType) {
return filesView.getSelectionModel().getSelectedItem() + ".txt";
if (filesView.getSelectionModel().getSelectedItem() != null) {
return filesView.getSelectionModel().getSelectedItem() + Diaballik.EXTENSION_SAUVEGARDE;
}

return null;
Expand Down

0 comments on commit addab76

Please sign in to comment.