From 164708dcf5782043995c1d4c611a4e8af825f609 Mon Sep 17 00:00:00 2001 From: lieght <49651652+BAFGdeJong@users.noreply.github.com> Date: Fri, 28 Nov 2025 19:26:15 +0100 Subject: [PATCH] Added button to continue and start game. Refactors --- .../widget/tutorial/BaseTutorialWidget.java | 104 ++++++++++++------ .../tutorial/Connect4TutorialWidget.java | 48 +++----- .../tutorial/ReversiTutorialWidget.java | 51 ++++----- .../tutorial/ShowEnableTutorialWidget.java | 6 +- .../tutorial/TicTacToeTutorialWidget.java | 41 +++---- .../app/widget/view/LocalMultiplayerView.java | 6 +- .../localization/localization_en.properties | 2 + 7 files changed, 126 insertions(+), 132 deletions(-) diff --git a/app/src/main/java/org/toop/app/widget/tutorial/BaseTutorialWidget.java b/app/src/main/java/org/toop/app/widget/tutorial/BaseTutorialWidget.java index 01354ac..aed9f28 100644 --- a/app/src/main/java/org/toop/app/widget/tutorial/BaseTutorialWidget.java +++ b/app/src/main/java/org/toop/app/widget/tutorial/BaseTutorialWidget.java @@ -1,63 +1,95 @@ package org.toop.app.widget.tutorial; import javafx.geometry.Pos; +import javafx.scene.control.Button; import javafx.scene.image.ImageView; import javafx.scene.text.Text; +import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair; +import org.toop.app.GameInformation; +import org.toop.app.game.BaseGameThread; import org.toop.app.widget.Primitive; import org.toop.app.widget.WidgetContainer; import org.toop.app.widget.complex.PopupWidget; -import org.toop.app.widget.complex.ViewWidget; -import javafx.scene.control.Button; import org.toop.framework.resource.resources.ImageAsset; import org.toop.local.AppContext; -import java.io.File; +import java.util.List; public class BaseTutorialWidget extends PopupWidget { - private Text tutorialText; - private Button previousButton; - private Button nextButton; - private ImageView imagery; - private int currentTextIndex = 0; + private final Text tutorialText; + private final ImageView imagery; + private final Button previousButton; + private final Button nextButton; + private int pageIndex = 0; + private final List> pages; + private final Runnable nextScreen; + + public BaseTutorialWidget(List> pages, Runnable nextScreen) { + this.tutorialText = Primitive.text(pages.getFirst().getKey()); + this.imagery = Primitive.image(pages.getFirst().getValue()); + + this.pages = pages; + this.nextScreen = nextScreen; + + previousButton = Primitive.button("goback", () -> { update(false); this.hide(); }); + nextButton = Primitive.button(">", () -> update(true)); + + var w = Primitive.hbox( + previousButton, + nextButton + ); + + var x = Primitive.vbox(imagery, tutorialText); + + add(Pos.CENTER, Primitive.vbox(x, w)); - public BaseTutorialWidget(String key) { - System.out.println("Trying to initialize..."); - this.tutorialText = Primitive.text(key); WidgetContainer.add(Pos.CENTER, this); } - private void setOnPrevious(Runnable onPrevious) { - this.previousButton = Primitive.button("<", onPrevious); - } + public void update(boolean next) { + pageIndex = next ? pageIndex + 1 : pageIndex - 1; - private void setOnNext(Runnable onNext) { - this.nextButton = Primitive.button(">", onNext); - } - - public void setTutorial(ImageAsset image, Runnable onPrevious, Runnable onNext) { - setOnPrevious(onPrevious); - setOnNext(onNext); - this.imagery = Primitive.image(image); - var w = Primitive.hbox(previousButton, nextButton); - var x = Primitive.vbox(imagery, tutorialText); - add(Pos.CENTER, Primitive.vbox(x, w)); - } - - public void update(boolean next, String[] locKeys, ImageAsset[] imgs) { - currentTextIndex = next ? currentTextIndex + 1 : currentTextIndex - 1; - - if (currentTextIndex >= locKeys.length) { - currentTextIndex--; + if (pageIndex >= pages.size()) { + pageIndex--; return; - } else if (currentTextIndex < 0) { - currentTextIndex++; + } else if (pageIndex < 0) { + pageIndex++; return; } + if (pageIndex == pages.size()-1) { + nextButton.textProperty().unbind(); + nextButton.setText(AppContext.getString("startgame")); + nextButton.setOnAction((_) -> { + this.hide(); + nextScreen.run(); + }); + + } else { + nextButton.textProperty().unbind(); + nextButton.setText(AppContext.getString(">")); + nextButton.setOnAction((_) -> this.update(true)); + } + + if (pageIndex == 0) { + previousButton.textProperty().unbind(); + previousButton.setText(AppContext.getString("goback")); + previousButton.setOnAction((_) -> this.hide()); + } else { + previousButton.textProperty().unbind(); + previousButton.setText(AppContext.getString("<")); + previousButton.setOnAction((_) -> this.update(false)); + } + + var currentPage = pages.get(pageIndex); + + var text = currentPage.getKey(); + var image = currentPage.getValue(); + tutorialText.textProperty().unbind(); - tutorialText.setText(AppContext.getString(locKeys[currentTextIndex])); - imagery.setImage(Primitive.image(imgs[currentTextIndex]).getImage()); + tutorialText.setText(AppContext.getString(text)); + imagery.setImage(Primitive.image(image).getImage()); } } diff --git a/app/src/main/java/org/toop/app/widget/tutorial/Connect4TutorialWidget.java b/app/src/main/java/org/toop/app/widget/tutorial/Connect4TutorialWidget.java index a413af0..6c0bc10 100644 --- a/app/src/main/java/org/toop/app/widget/tutorial/Connect4TutorialWidget.java +++ b/app/src/main/java/org/toop/app/widget/tutorial/Connect4TutorialWidget.java @@ -1,43 +1,25 @@ package org.toop.app.widget.tutorial; -import javafx.geometry.Pos; -import org.toop.app.widget.complex.ViewWidget; +import javafx.application.Platform; +import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair; +import org.toop.app.GameInformation; +import org.toop.app.game.Connect4Game; import org.toop.framework.resource.ResourceManager; -import org.toop.framework.resource.resources.ImageAsset; -import java.io.File; +import java.util.List; public class Connect4TutorialWidget extends BaseTutorialWidget { - private final String[] keys; - private final ImageAsset[] images; + public Connect4TutorialWidget(GameInformation information) { + super(List.of( + new ImmutablePair<>("connect4.1", ResourceManager.get("connect41.png")), + new ImmutablePair<>("connect4.2", ResourceManager.get("connect42.png")) + ), () -> Platform.runLater(() -> new Connect4Game(information))); + } public Connect4TutorialWidget() { - String[] newKeys = { - "connect4.1", "connect4.2" - }; - - ImageAsset[] newImages = { - ResourceManager.get("connect41.png"), - ResourceManager.get("connect42.png") - }; - - super(newKeys[0]); - - keys = newKeys; - images = newImages; - - setTutorial( - images[0], - () -> update(false, keys, images), - () -> update(true, keys, images) - ); - } - - public String[] getKeys() { - return keys; - } - - public ImageAsset[] getImages() { - return images; + super(List.of( + new ImmutablePair<>("connect4.1", ResourceManager.get("connect41.png")), + new ImmutablePair<>("connect4.2", ResourceManager.get("connect42.png")) + ), () -> {}); } } diff --git a/app/src/main/java/org/toop/app/widget/tutorial/ReversiTutorialWidget.java b/app/src/main/java/org/toop/app/widget/tutorial/ReversiTutorialWidget.java index 0577f16..fe01311 100644 --- a/app/src/main/java/org/toop/app/widget/tutorial/ReversiTutorialWidget.java +++ b/app/src/main/java/org/toop/app/widget/tutorial/ReversiTutorialWidget.java @@ -1,42 +1,29 @@ package org.toop.app.widget.tutorial; -import javafx.geometry.Pos; -import org.toop.app.widget.complex.ViewWidget; +import javafx.application.Platform; +import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair; +import org.toop.app.GameInformation; +import org.toop.app.game.ReversiGame; import org.toop.framework.resource.ResourceManager; -import org.toop.framework.resource.resources.ImageAsset; -import java.io.File; +import java.util.List; public class ReversiTutorialWidget extends BaseTutorialWidget { - private final String[] keys; - private final ImageAsset[] images; + public ReversiTutorialWidget(GameInformation gameInformation) { + super(List.of( + new ImmutablePair<>("reversi1", ResourceManager.get("reversi1.png")), + new ImmutablePair<>("reversi2", ResourceManager.get("reversi2.png")), + new ImmutablePair<>("reversi3", ResourceManager.get("cat.jpg")), + new ImmutablePair<>("reversi4", ResourceManager.get("cat.jpg")) + ), () -> Platform.runLater(() -> new ReversiGame(gameInformation))); + } public ReversiTutorialWidget() { - String[] newKeys = {"reversi1", "reversi2", "reversi3", "reversi4"}; - ImageAsset[] newImages = { - ResourceManager.get("reversi1.png"), - ResourceManager.get("reversi2.png"), - ResourceManager.get("cat.jpg"), - ResourceManager.get("cat.jpg") - }; - - super(newKeys[0]); - - keys = newKeys; - images = newImages; - - setTutorial( - images[0], - () -> update(false, keys, images), - () -> update(true, keys, images) - ); - } - - public String[] getKeys() { - return keys; - } - - public ImageAsset[] getImages() { - return images; + super(List.of( + new ImmutablePair<>("reversi1", ResourceManager.get("reversi1.png")), + new ImmutablePair<>("reversi2", ResourceManager.get("reversi2.png")), + new ImmutablePair<>("reversi3", ResourceManager.get("cat.jpg")), + new ImmutablePair<>("reversi4", ResourceManager.get("cat.jpg")) + ), () -> {}); } } diff --git a/app/src/main/java/org/toop/app/widget/tutorial/ShowEnableTutorialWidget.java b/app/src/main/java/org/toop/app/widget/tutorial/ShowEnableTutorialWidget.java index 03c75ec..8096444 100644 --- a/app/src/main/java/org/toop/app/widget/tutorial/ShowEnableTutorialWidget.java +++ b/app/src/main/java/org/toop/app/widget/tutorial/ShowEnableTutorialWidget.java @@ -9,9 +9,9 @@ public class ShowEnableTutorialWidget extends PopupWidget { public ShowEnableTutorialWidget(String text, Runnable onYes, Runnable onNo, Runnable onNever) { var a = Primitive.hbox( - Primitive.button("ok", onYes), - Primitive.button("no", onNo), - Primitive.button("never", onNever) + Primitive.button("ok", () -> { onYes.run(); this.hide(); }), + Primitive.button("no", () -> { onNo.run(); this.hide(); }), + Primitive.button("never", () -> { onNever.run(); this.hide(); }) ); var txt = Primitive.text(text); diff --git a/app/src/main/java/org/toop/app/widget/tutorial/TicTacToeTutorialWidget.java b/app/src/main/java/org/toop/app/widget/tutorial/TicTacToeTutorialWidget.java index f76bd74..37e356f 100644 --- a/app/src/main/java/org/toop/app/widget/tutorial/TicTacToeTutorialWidget.java +++ b/app/src/main/java/org/toop/app/widget/tutorial/TicTacToeTutorialWidget.java @@ -1,41 +1,32 @@ package org.toop.app.widget.tutorial; +import javafx.application.Platform; import javafx.geometry.Pos; +import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair; +import org.toop.app.GameInformation; +import org.toop.app.game.ReversiGame; +import org.toop.app.game.TicTacToeGameThread; import org.toop.app.widget.complex.ViewWidget; import org.toop.framework.resource.ResourceManager; import org.toop.framework.resource.resources.ImageAsset; import java.io.File; +import java.util.List; public class TicTacToeTutorialWidget extends BaseTutorialWidget { - private final String[] keys; - private final ImageAsset[] images; + public TicTacToeTutorialWidget(GameInformation gameInformation) { + super(List.of( + new ImmutablePair<>("tictactoe1", ResourceManager.get("tictactoe1.png")), + new ImmutablePair<>("tictactoe2", ResourceManager.get("tictactoe2.png")) + ), () -> Platform.runLater(() -> new TicTacToeGameThread(gameInformation))); + } public TicTacToeTutorialWidget() { - String[] newKeys = {"tictactoe1", "tictactoe2"}; - ImageAsset[] newImages = { - ResourceManager.get("tictactoe1.png"), - ResourceManager.get("tictactoe2.png") - }; - - super(newKeys[0]); - - keys = newKeys; - images = newImages; - - setTutorial( - images[0], - () -> update(false, keys, images), - () -> update(true, keys, images) - ); + super(List.of( + new ImmutablePair<>("tictactoe1", ResourceManager.get("tictactoe1.png")), + new ImmutablePair<>("tictactoe2", ResourceManager.get("tictactoe2.png")) + ), () -> {}); } - public String[] getKeys() { - return keys; - } - - public ImageAsset[] getImages() { - return images; - } } diff --git a/app/src/main/java/org/toop/app/widget/view/LocalMultiplayerView.java b/app/src/main/java/org/toop/app/widget/view/LocalMultiplayerView.java index a5e3931..a7c1b27 100644 --- a/app/src/main/java/org/toop/app/widget/view/LocalMultiplayerView.java +++ b/app/src/main/java/org/toop/app/widget/view/LocalMultiplayerView.java @@ -41,7 +41,7 @@ public class LocalMultiplayerView extends ViewWidget { "tutorial", () -> { AppSettings.getSettings().setFirstTTT(false); - new TicTacToeTutorialWidget(); + new TicTacToeTutorialWidget(information); }, () -> { AppSettings.getSettings().setFirstTTT(false); @@ -67,7 +67,7 @@ public class LocalMultiplayerView extends ViewWidget { "tutorial", () -> { AppSettings.getSettings().setFirstTTT(false); - new ReversiTutorialWidget(); + new ReversiTutorialWidget(information); }, () -> { AppSettings.getSettings().setFirstTTT(false); @@ -93,7 +93,7 @@ public class LocalMultiplayerView extends ViewWidget { "tutorial", () -> { AppSettings.getSettings().setFirstTTT(false); - new Connect4TutorialWidget(); + new Connect4TutorialWidget(information); }, () -> { AppSettings.getSettings().setFirstTTT(false); diff --git a/app/src/main/resources/assets/localization/localization_en.properties b/app/src/main/resources/assets/localization/localization_en.properties index d0e20b0..44043fc 100644 --- a/app/src/main/resources/assets/localization/localization_en.properties +++ b/app/src/main/resources/assets/localization/localization_en.properties @@ -84,6 +84,8 @@ reversi2=Clicking on a dot will flip all the moves between where you place the d reversi3=Your turn may be skipped if there is no legal move. This will let your opponent play again until you get an opportunity at a legal move. reversi4=The player who wins at the end of the game is the one who has the most pieces on the board. tutorialstring=Tutorial +startgame=Start game! +goback=Go back arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabic)