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 b12d727..b3fc354 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 @@ -4,6 +4,8 @@ import javafx.geometry.Pos; import javafx.scene.image.ImageView; import javafx.scene.text.Text; 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; @@ -11,50 +13,50 @@ import org.toop.local.AppContext; import java.io.File; -public class BaseTutorialWidget extends ViewWidget { +public class BaseTutorialWidget extends PopupWidget { - private TState state; private Text tutorialText; private Button previousButton; private Button nextButton; - private Button noButton; - private Button yesButton; - private Button neverButton; private ImageView imagery; + private int currentTextIndex = 0; - public BaseTutorialWidget(String key, Runnable onNo, Runnable onYes, Runnable onNever) { + public BaseTutorialWidget(String key) { System.out.println("Trying to initialize..."); this.tutorialText = Primitive.text(key); - this.yesButton = Primitive.button("ok", () -> onYes.run()); - this.noButton = Primitive.button("no", () -> onNo.run()); - this.neverButton = Primitive.button("never", () -> onNever.run()); - var a = Primitive.hbox(yesButton, noButton, neverButton); - add(Pos.CENTER, Primitive.vbox(tutorialText, a)); + WidgetContainer.add(Pos.CENTER, this); } - public BaseTutorialWidget(TState state, String key, Runnable onPrevious, Runnable onNext) { - this.state = state; - this.tutorialText = Primitive.text(key); - this.previousButton = Primitive.button("<", () -> onPrevious.run()); - this.nextButton = Primitive.button(">", () -> onNext.run()); - var w = Primitive.hbox(previousButton, nextButton); - add(Pos.CENTER, Primitive.vbox(tutorialText, w)); + private void setOnPrevious(Runnable onPrevious) { + this.previousButton = Primitive.button("<", onPrevious); } - public BaseTutorialWidget(TState state, String key, File image, Runnable onPrevious, Runnable onNext) { - this.state = state; + private void setOnNext(Runnable onNext) { + this.nextButton = Primitive.button(">", onNext); + } + + public void setTutorial(File image, Runnable onPrevious, Runnable onNext) { + setOnPrevious(onPrevious); + setOnNext(onNext); this.imagery = Primitive.image(image); - this.tutorialText = Primitive.text(key); - this.previousButton = Primitive.button("<", () -> onPrevious.run()); - this.nextButton = Primitive.button(">", () -> onNext.run()); var w = Primitive.hbox(previousButton, nextButton); var x = Primitive.vbox(imagery, tutorialText); add(Pos.CENTER, Primitive.vbox(x, w)); } - public void update(String key, File image) { + public void update(boolean next, String[] locKeys, File[] imgs) { + currentTextIndex = next ? currentTextIndex + 1 : currentTextIndex - 1; + + if (currentTextIndex >= locKeys.length) { + currentTextIndex--; + return; + } else if (currentTextIndex < 0) { + currentTextIndex++; + return; + } + tutorialText.textProperty().unbind(); - tutorialText.setText(AppContext.getString(key)); - imagery.setImage(Primitive.image(image).getImage()); + tutorialText.setText(AppContext.getString(locKeys[currentTextIndex])); + imagery.setImage(Primitive.image(imgs[currentTextIndex]).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 1fdd4e3..5f13ed8 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 @@ -5,35 +5,35 @@ import org.toop.app.widget.complex.ViewWidget; import java.io.File; -public class Connect4TutorialWidget extends ViewWidget { - private TState state; - private String[] keys = {"connect4.1", "connect4.2"}; - private File[] images = {new File("app/src/main/resources/assets/images/connect41.png"), new File("app/src/main/resources/assets/images/connect42.png")}; - private BaseTutorialWidget tutorialWidget; +public class Connect4TutorialWidget extends BaseTutorialWidget { + private final String[] keys; + + private final File[] images = { + new File("app/src/main/resources/assets/images/connect41.png"), + new File("app/src/main/resources/assets/images/connect42.png") + }; public Connect4TutorialWidget() { - this.state = new TState(keys.length); - tutorialWidget = new BaseTutorialWidget( - state, - keys[state.getCurrent()], - images[state.getCurrent()], - () -> { - if (state.hasPrevious()) { - state.previous(); - update(); - } - }, - () -> { - if (state.hasNext()) { - state.next(); - update(); - } - } + String[] newKeys = { + "connect4.1", "connect4.2" + }; + + super(newKeys[0]); + + keys = newKeys; + + setTutorial( + images[0], + () -> update(false, keys, images), + () -> update(true, keys, images) ); - add(Pos.CENTER, tutorialWidget); } - private void update() { - tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]); + public String[] getKeys() { + return keys; + } + + public File[] getImages() { + return images; } } 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 1ec990a..ae632ff 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 @@ -5,35 +5,29 @@ import org.toop.app.widget.complex.ViewWidget; import java.io.File; -public class ReversiTutorialWidget extends ViewWidget { - private TState state; - private String[] keys = {"reversi1", "reversi2", "reversi3", "reversi4"}; - private File[] images = {new File("app/src/main/resources/assets/images/reversi1.png"), new File("app/src/main/resources/assets/images/reversi2.png"), new File("app/src/main/resources/assets/images/cat.jpg"), new File("app/src/main/resources/assets/images/cat.jpg")}; - private BaseTutorialWidget tutorialWidget; +public class ReversiTutorialWidget extends BaseTutorialWidget { + private final String[] keys; + private final File[] images = {new File("app/src/main/resources/assets/images/reversi1.png"), new File("app/src/main/resources/assets/images/reversi2.png"), new File("app/src/main/resources/assets/images/cat.jpg"), new File("app/src/main/resources/assets/images/cat.jpg")}; public ReversiTutorialWidget() { - this.state = new TState(keys.length); - tutorialWidget = new BaseTutorialWidget( - state, - keys[state.getCurrent()], - images[state.getCurrent()], - () -> { - if (state.hasPrevious()) { - state.previous(); - update(); - } - }, - () -> { - if (state.hasNext()) { - state.next(); - update(); - } - } + String[] newKeys = {"reversi1", "reversi2", "reversi3", "reversi4"}; + + super(newKeys[0]); + + keys = newKeys; + + setTutorial( + images[0], + () -> update(false, keys, images), + () -> update(true, keys, images) ); - add(Pos.CENTER, tutorialWidget); } - private void update() { - tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]); + public String[] getKeys() { + return keys; + } + + public File[] getImages() { + return images; } } 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 new file mode 100644 index 0000000..28b3714 --- /dev/null +++ b/app/src/main/java/org/toop/app/widget/tutorial/ShowEnableTutorialWidget.java @@ -0,0 +1,28 @@ +package org.toop.app.widget.tutorial; + +import javafx.geometry.Pos; +import org.toop.app.widget.Primitive; +import org.toop.app.widget.WidgetContainer; +import org.toop.app.widget.complex.PopupWidget; + +public class ShowEnableTutorialWidget extends PopupWidget { +// private final Button yesButton; +// private final Button noButton; +// private final Button neverButton; + + public ShowEnableTutorialWidget(String words, Runnable onYes, Runnable onNo, Runnable onNever) { +// this.yesButton = Primitive.button("ok", onYes); +// this.noButton = Primitive.button("no", onNo); +// this.neverButton = Primitive.button("never", onNever); + + var a = Primitive.hbox( + Primitive.button("ok", onYes), + Primitive.button("no", onNo), + Primitive.button("never", onNever) + ); + + var txt = Primitive.text(words); + add(Pos.CENTER, Primitive.vbox(txt, a)); + WidgetContainer.add(Pos.CENTER, this); + } +} \ No newline at end of file diff --git a/app/src/main/java/org/toop/app/widget/tutorial/TState.java b/app/src/main/java/org/toop/app/widget/tutorial/TState.java deleted file mode 100644 index 4efc2d2..0000000 --- a/app/src/main/java/org/toop/app/widget/tutorial/TState.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.toop.app.widget.tutorial; - -public class TState { - - private int current; - private int total; - - public TState(int total) { - this.total = total; - this.current = 0; - } - - public int getCurrent() { - return current; - } - - public void setCurrent(int current) { - this.current = current; - } - - public int getTotal() { - return total; - } - - public void setTotal(int total) { - this.total = total; - } - - public void next() { - current = current + 1; - } - - public void previous() { - current = current - 1; - } - - public boolean hasNext() { - return current < total - 1; - } - - public boolean hasPrevious() { - return current > 0; - } -} 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 85355a4..f1b3c6c 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 @@ -4,39 +4,33 @@ import javafx.geometry.Pos; import org.toop.app.widget.complex.ViewWidget; import java.io.File; -public class TicTacToeTutorialWidget extends ViewWidget { +public class TicTacToeTutorialWidget extends BaseTutorialWidget { - private TState state; - private String[] keys = {"tictactoe1", "tictactoe2"}; - private File[] images = { + private final String[] keys; + private final File[] images = { new File("app/src/main/resources/assets/images/tictactoe1.png"), new File("app/src/main/resources/assets/images/tictactoe2.png") }; - private BaseTutorialWidget tutorialWidget; public TicTacToeTutorialWidget() { - this.state = new TState(keys.length); - tutorialWidget = new BaseTutorialWidget( - state, - keys[state.getCurrent()], - images[state.getCurrent()], - () -> { - if (state.hasPrevious()) { - state.previous(); - update(); - } - }, - () -> { - if (state.hasNext()) { - state.next(); - update(); - } - } + String[] newKeys = {"tictactoe1", "tictactoe2"}; + + super(newKeys[0]); + + keys = newKeys; + + setTutorial( + images[0], + () -> update(false, keys, images), + () -> update(true, keys, images) ); - add(Pos.CENTER, tutorialWidget); } - private void update() { - tutorialWidget.update(keys[state.getCurrent()], images[state.getCurrent()]); + public String[] getKeys() { + return keys; + } + + public File[] getImages() { + return images; } } diff --git a/app/src/main/java/org/toop/app/widget/view/GameView.java b/app/src/main/java/org/toop/app/widget/view/GameView.java index 334aa51..7977176 100644 --- a/app/src/main/java/org/toop/app/widget/view/GameView.java +++ b/app/src/main/java/org/toop/app/widget/view/GameView.java @@ -53,23 +53,13 @@ public final class GameView extends ViewWidget { switch(gameType) { case "TicTacToe": - this.tutorialButton = Primitive.button("tutorialstring", () -> { - transitionNext(new TicTacToeTutorialWidget()); - }); - break; + this.tutorialButton = Primitive.button("tutorialstring", TicTacToeTutorialWidget::new); break; case "Reversi": - this.tutorialButton = Primitive.button("tutorialstring", () -> { - transitionNext(new ReversiTutorialWidget()); - }); - break; + this.tutorialButton = Primitive.button("tutorialstring", ReversiTutorialWidget::new); break; case "Connect4": - this.tutorialButton = Primitive.button("tutorialstring", () -> { - transitionNext(new Connect4TutorialWidget()); - }); - break; + this.tutorialButton = Primitive.button("tutorialstring", Connect4TutorialWidget::new); break; default: - this.tutorialButton = null; - break; + this.tutorialButton = null; break; } setupLayout(); 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 5cab8b7..a5e3931 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 @@ -10,10 +10,7 @@ import org.toop.app.widget.WidgetContainer; import org.toop.app.widget.complex.PlayerInfoWidget; import org.toop.app.widget.complex.ViewWidget; import org.toop.app.widget.popup.ErrorPopup; -import org.toop.app.widget.tutorial.BaseTutorialWidget; -import org.toop.app.widget.tutorial.Connect4TutorialWidget; -import org.toop.app.widget.tutorial.ReversiTutorialWidget; -import org.toop.app.widget.tutorial.TicTacToeTutorialWidget; +import org.toop.app.widget.tutorial.*; import org.toop.local.AppContext; import javafx.geometry.Pos; @@ -40,84 +37,78 @@ public class LocalMultiplayerView extends ViewWidget { switch (information.type) { case TICTACTOE: if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) { - BaseTutorialWidget a = new BaseTutorialWidget( + new ShowEnableTutorialWidget( "tutorial", - () -> { - AppSettings.getSettings().setFirstTTT(false); - Platform.runLater(() -> { - new TicTacToeGameThread(information); - }); - }, - () -> { - ViewWidget c = new TicTacToeTutorialWidget(); - transitionNext(c); - WidgetContainer.setCurrentView(c); - AppSettings.getSettings().setFirstTTT(false); - }, - () -> { - AppSettings.getSettings().setTutorialFlag(false); - Platform.runLater(() -> { - new TicTacToeGameThread(information); - }); - } - ); - transitionNext(a); + () -> { + AppSettings.getSettings().setFirstTTT(false); + new TicTacToeTutorialWidget(); + }, + () -> { + AppSettings.getSettings().setFirstTTT(false); + Platform.runLater(() -> { + new TicTacToeGameThread(information); + }); + }, + () -> { + AppSettings.getSettings().setTutorialFlag(false); + Platform.runLater(() -> { + new TicTacToeGameThread(information); + }); + } + + ); break; } new TicTacToeGameThread(information); break; case REVERSI: if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) { - BaseTutorialWidget a = new BaseTutorialWidget( + new ShowEnableTutorialWidget( "tutorial", - () -> { Platform.runLater(() -> { - AppSettings.getSettings().setFirstReversi(false); - new ReversiGame(information); - }); + () -> { + AppSettings.getSettings().setFirstTTT(false); + new ReversiTutorialWidget(); }, () -> { + AppSettings.getSettings().setFirstTTT(false); Platform.runLater(() -> { - ViewWidget c = new ReversiTutorialWidget(); - transitionNext(c); - WidgetContainer.setCurrentView(c); - AppSettings.getSettings().setFirstReversi(false); - }); - }, - () -> { - Platform.runLater(() -> { - AppSettings.getSettings().setTutorialFlag(false); new ReversiGame(information); }); - }); - transitionNext(a); + }, + () -> { + AppSettings.getSettings().setTutorialFlag(false); + Platform.runLater(() -> { + new ReversiGame(information); + }); + } + + ); break; } new ReversiGame(information); break; case CONNECT4: if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstConnect4()) { - BaseTutorialWidget a = new BaseTutorialWidget( + new ShowEnableTutorialWidget( "tutorial", - () -> { Platform.runLater(() -> { - AppSettings.getSettings().setFirstConnect4(false); - new Connect4Game(information); - }); + () -> { + AppSettings.getSettings().setFirstTTT(false); + new Connect4TutorialWidget(); }, () -> { + AppSettings.getSettings().setFirstTTT(false); Platform.runLater(() -> { - ViewWidget c = new Connect4TutorialWidget(); - transitionNext(c); - WidgetContainer.setCurrentView(c); - AppSettings.getSettings().setFirstConnect4(false); - }); - }, - () -> { - Platform.runLater(() -> { - AppSettings.getSettings().setTutorialFlag(false); new Connect4Game(information); }); + }, + () -> { + AppSettings.getSettings().setTutorialFlag(false); + Platform.runLater(() -> { + new Connect4Game(information); }); - transitionNext(a); + } + + ); break; } new Connect4Game(information);