Refactored switch statement

This commit is contained in:
lieght
2025-11-28 20:05:08 +01:00
parent fd85a73608
commit c21f254234
4 changed files with 37 additions and 69 deletions

View File

@@ -0,0 +1,5 @@
package org.toop.app.widget;
public interface Updatable {
void update();
}

View File

@@ -6,6 +6,7 @@ import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import org.apache.maven.surefire.shared.lang3.tuple.ImmutablePair;
import org.toop.app.widget.Primitive;
import org.toop.app.widget.Updatable;
import org.toop.app.widget.WidgetContainer;
import org.toop.app.widget.complex.PopupWidget;
@@ -14,16 +15,17 @@ import org.toop.local.AppContext;
import java.util.List;
public class BaseTutorialWidget extends PopupWidget {
public class BaseTutorialWidget extends PopupWidget implements Updatable {
private final Text tutorialText;
private final ImageView imagery;
private final Button previousButton;
private final Button nextButton;
private int pageIndex = 0;
private final List<ImmutablePair<String, ImageAsset>> pages;
private final Runnable nextScreen;
private int pageIndex = 0;
public BaseTutorialWidget(List<ImmutablePair<String, ImageAsset>> pages, Runnable nextScreen) {
this.tutorialText = Primitive.text(pages.getFirst().getKey());
this.imagery = Primitive.image(pages.getFirst().getValue());
@@ -46,6 +48,11 @@ public class BaseTutorialWidget extends PopupWidget {
WidgetContainer.add(Pos.CENTER, this);
}
@Override
public void update() {
update(true);
}
public void update(boolean next) {
pageIndex = next ? pageIndex + 1 : pageIndex - 1;

View File

@@ -4,17 +4,18 @@ import javafx.geometry.Pos;
import org.toop.app.widget.Primitive;
import org.toop.app.widget.WidgetContainer;
import org.toop.app.widget.complex.PopupWidget;
import org.toop.local.AppSettings;
public class ShowEnableTutorialWidget extends PopupWidget {
public ShowEnableTutorialWidget(String text, Runnable onYes, Runnable onNo, Runnable onNever) {
public ShowEnableTutorialWidget(Runnable tutorial, Runnable nextScreen, Runnable appSettingsSetter) {
var a = Primitive.hbox(
Primitive.button("ok", () -> { onYes.run(); this.hide(); }),
Primitive.button("no", () -> { onNo.run(); this.hide(); }),
Primitive.button("never", () -> { onNever.run(); this.hide(); })
Primitive.button("ok", () -> { appSettingsSetter.run(); tutorial.run(); this.hide(); }),
Primitive.button("no", () -> { appSettingsSetter.run(); nextScreen.run(); this.hide(); }),
Primitive.button("never", () -> { AppSettings.getSettings().setTutorialFlag(false); nextScreen.run(); this.hide(); })
);
var txt = Primitive.text(text);
var txt = Primitive.text("tutorial");
add(Pos.CENTER, Primitive.vbox(txt, a));
WidgetContainer.add(Pos.CENTER, this);
}

View File

@@ -38,84 +38,39 @@ public class LocalMultiplayerView extends ViewWidget {
case TICTACTOE:
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
new ShowEnableTutorialWidget(
"tutorial",
() -> {
AppSettings.getSettings().setFirstTTT(false);
new TicTacToeTutorialWidget(() -> new TicTacToeGameThread(information));
},
() -> {
AppSettings.getSettings().setFirstTTT(false);
Platform.runLater(() -> {
new TicTacToeGameThread(information);
});
},
() -> {
AppSettings.getSettings().setTutorialFlag(false);
Platform.runLater(() -> {
new TicTacToeGameThread(information);
});
}
() -> new TicTacToeTutorialWidget(() -> new TicTacToeGameThread(information)),
() -> Platform.runLater(() -> new TicTacToeGameThread(information)),
() -> AppSettings.getSettings().setFirstTTT(false)
);
break;
} else {
new TicTacToeGameThread(information);
}
new TicTacToeGameThread(information);
break;
case REVERSI:
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
new ShowEnableTutorialWidget(
"tutorial",
() -> {
AppSettings.getSettings().setFirstTTT(false);
new ReversiTutorialWidget(() -> new ReversiGame(information));
},
() -> {
AppSettings.getSettings().setFirstTTT(false);
Platform.runLater(() -> {
new ReversiGame(information);
});
},
() -> {
AppSettings.getSettings().setTutorialFlag(false);
Platform.runLater(() -> {
new ReversiGame(information);
});
}
() -> new ReversiTutorialWidget(() -> new ReversiGame(information)),
() -> Platform.runLater(() -> new ReversiGame(information)),
() -> AppSettings.getSettings().setFirstReversi(false)
);
break;
} else {
new ReversiGame(information);
}
new ReversiGame(information);
break;
case CONNECT4:
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstConnect4()) {
new ShowEnableTutorialWidget(
"tutorial",
() -> {
AppSettings.getSettings().setFirstTTT(false);
new Connect4TutorialWidget(() -> new Connect4Game(information));
},
() -> {
AppSettings.getSettings().setFirstTTT(false);
Platform.runLater(() -> {
new Connect4Game(information);
});
},
() -> {
AppSettings.getSettings().setTutorialFlag(false);
Platform.runLater(() -> {
new Connect4Game(information);
});
}
() -> new Connect4TutorialWidget(() -> new Connect4Game(information)),
() -> Platform.runLater(() -> new Connect4Game(information)),
() -> AppSettings.getSettings().setFirstConnect4(false)
);
break;
} else {
new Connect4Game(information);
}
new Connect4Game(information);
break;
}
}
// case BATTLESHIP -> new BattleshipGame(information);
});
});
var playerSection = setupPlayerSections();