First options menu and a lot of fixes

This commit is contained in:
2020-07-19 20:02:54 +02:00
parent 6521ceeec7
commit 201fac09cc
16 changed files with 289 additions and 78 deletions

View File

@@ -11,6 +11,7 @@ import de.siphalor.was.content.quest.QuestGenerator;
import de.siphalor.was.content.quest.QuestManager;
import de.siphalor.was.content.quest.RandomQuestGenerator;
import de.siphalor.was.game.Balance;
import de.siphalor.was.game.Options;
import de.siphalor.was.game.Storage;
import de.siphalor.was.util.Util;
import de.siphalor.was.visual.JFXVisual;
@@ -39,6 +40,8 @@ public class WhatAStorage {
private final ProductManager productManager;
private final QuestManager questManager;
private final Options options;
private Visual visual;
private boolean stopScheduled;
@@ -54,6 +57,7 @@ public class WhatAStorage {
contentManager.addPack(mainPack);
productManager = new ProductManager();
questManager = new QuestManager();
options = new Options();
visual = new JFXVisual();
}
@@ -70,6 +74,10 @@ public class WhatAStorage {
return questManager;
}
public Options getOptions() {
return options;
}
public void reload() {
contentManager.clear();
@@ -102,8 +110,8 @@ public class WhatAStorage {
}
public void loadGame() {
//questGenerator = questManager.get("test");
questGenerator = new RandomQuestGenerator();
questGenerator = questManager.get("example.test");
//questGenerator = new RandomQuestGenerator();
quests.clear();
storage = new Storage(GRID_SIZE, GRID_SIZE, GRID_SIZE);
balance = new Balance();
@@ -122,6 +130,13 @@ public class WhatAStorage {
return balance;
}
public String[] getLangs() {
return contentManager.getResources("lang", "properties").map(resource -> {
int index = resource.getId().lastIndexOf('.');
return resource.getId().substring(Math.max(index, 0));
}).toArray(String[]::new);
}
private void addTransaction(Balance.Transaction transaction, int change) {
balance.add(transaction, change);
visual.onBalanceChanged(balance.getBudget(), transaction, change, balance.getTotalIncome(), balance.getTotalLoss());
@@ -213,6 +228,7 @@ public class WhatAStorage {
}
public boolean resolveQuests(int in, int out) {
if (options.getAllowQuestResolving()) {
int s = quests.size();
if (in < s && in >= 0 && out < s && out >= 0) {
if (quests.get(in).getType() == Quest.Type.IN && quests.get(out).getType() == Quest.Type.OUT) {
@@ -231,6 +247,7 @@ public class WhatAStorage {
}
}
}
}
return false;
}

View File

@@ -20,6 +20,12 @@ public class I18n extends ResourceBundle {
@Nullable
private Lang lang;
public Lang getLang() {
if (lang != null)
return lang;
return DEFAULT_LANG;
}
public void setLang(@NotNull String code, @NotNull ContentManager contentManager) {
if (lang == null || !lang.getCode().equals(code)) {
lang = new Lang(code);

View File

@@ -19,13 +19,14 @@ public class Lang {
this.code = code;
}
@NotNull
public String getCode() {
return code;
}
public void load(@NotNull ContentManager contentManager) {
properties.clear();
contentManager.getAllOfResource("lang/" + code + ".lang").forEachOrdered(resource -> {
contentManager.getAllOfResource("lang/" + code + ".properties").forEachOrdered(resource -> {
InputStream inputStream = resource.getInputStream();
if (inputStream != null) {
try {

View File

@@ -26,8 +26,8 @@ public class FileContentPack implements ContentPack {
Path dir = base.resolve(Path.of(location));
if (dir.toFile().isDirectory()) {
try {
return Files.find(dir, Integer.MAX_VALUE, (fPath, fileAttributes) -> fileAttributes.isRegularFile() && fPath.endsWith(extension)).map(path ->
new FileResource(Util.pathToId(id, path.relativize(base).toString()), path.toFile())
return Files.find(dir, Integer.MAX_VALUE, (path, attributes) -> attributes.isRegularFile() && path.getFileName().getName(0).toString().endsWith(extension)).map(path ->
new FileResource(Util.pathToId(id, dir.relativize(path).toString()), path.toFile())
);
} catch (IOException e) {
e.printStackTrace();

View File

@@ -0,0 +1,22 @@
package de.siphalor.was.game;
public class Options {
private boolean autoRefillQuests;
private boolean allowQuestResolving;
public boolean getAllowQuestResolving() {
return allowQuestResolving;
}
public void setAllowQuestResolving(boolean allowQuestResolving) {
this.allowQuestResolving = allowQuestResolving;
}
public boolean getAutoRefillQuests() {
return autoRefillQuests;
}
public void setAutoRefillQuests(boolean autoRefillQuests) {
this.autoRefillQuests = autoRefillQuests;
}
}

View File

@@ -11,6 +11,7 @@ import de.siphalor.was.game.Balance;
import de.siphalor.was.util.Util;
import de.siphalor.was.visual.jfx.controller.BalanceController;
import de.siphalor.was.visual.jfx.controller.MainController;
import de.siphalor.was.visual.jfx.controller.OptionsController;
import de.siphalor.was.visual.jfx.controller.QuestController;
import de.siphalor.was.visual.jfx.util.JFXUtil;
import de.siphalor.was.visual.jfx.util.StorageSlot;
@@ -37,14 +38,15 @@ import java.util.logging.Level;
public class JFXVisual extends Application implements Visual {
private static Stage primaryStage;
private static final FXMLLoader loader = new FXMLLoader();
private static Scene mainScene;
private static MainController mainController;
private static GridPane storageGrid;
private static StorageSlot[][] storageSlots;
private static Scene optionsScene;
private static OptionsController optionsController;
private static Stage balanceStage;
private static BalanceController balanceController;
@@ -115,60 +117,23 @@ public class JFXVisual extends Application implements Visual {
primaryStage.setMinHeight(500);
primaryStage.setTitle(WhatAStorage.TITLE);
loader.setResources(I18n.getInstance());
mainController = new MainController(WhatAStorage.getInstance());
loader.setController(mainController);
Pane pane = loader.load(AssetsManager.getStream("jfx/game.fxml").get());
mainScene = new Scene(pane);
mainScene.getStylesheets().add("assets/jfx/main.css");
loadMainScene();
mainController.balanceButton.setOnAction(event -> {
if (balanceStage != null && balanceStage.isShowing()) {
return;
}
balanceStage = new Stage();
FXMLLoader balanceLoader = new FXMLLoader();
balanceLoader.setResources(I18n.getInstance());
try {
balanceController = new BalanceController();
balanceLoader.setController(balanceController);
Parent parent = balanceLoader.load(AssetsManager.getStream("jfx/balance.fxml").orElseThrow());
balanceController.init();
Scene balanceScene = new Scene(parent);
balanceScene.getStylesheets().add("assets/jfx/main.css");
balanceStage.setOnCloseRequest(e -> {
balanceStage = null;
balanceController = null;
});
balanceStage.setAlwaysOnTop(true);
balanceStage.setScene(balanceScene);
balanceStage.setTitle(I18n.getInstance().getString("game.balance"));
balanceStage.show();
} catch (IOException e) {
e.printStackTrace();
}
});
loadOptionsScene();
primaryStage.setOnCloseRequest(event -> {
if (balanceStage != null)
balanceStage.hide();
});
primaryStage.setScene(mainScene);
primaryStage.show();
}
public void loadMainScene() {
primaryStage.setScene(mainScene);
mainController = new MainController(WhatAStorage.getInstance());
mainScene = JFXUtil.loadScene("game", mainController, I18n.getInstance());
prepareStorageGrid(3, 3, 3);
@@ -177,6 +142,41 @@ public class JFXVisual extends Application implements Visual {
});
onBalanceChanged(0, Balance.Transaction.NOOP, 0, 0, 0);
mainController.balanceButton.setOnAction(event -> {
if (balanceStage != null && balanceStage.isShowing()) {
return;
}
balanceStage = new Stage();
balanceController = new BalanceController();
Scene scene = JFXUtil.loadScene("balance", balanceController, I18n.getInstance());
balanceController.init();
balanceStage.setOnCloseRequest(e -> {
balanceStage = null;
balanceController = null;
});
balanceStage.setAlwaysOnTop(true);
balanceStage.setScene(scene);
balanceStage.setTitle(I18n.getInstance().getString("game.balance"));
balanceStage.show();
});
mainController.optionsButton.setOnAction(event -> openOptionsScene());
}
private void loadOptionsScene() {
optionsController = new OptionsController();
optionsScene = JFXUtil.loadScene("options", optionsController, I18n.getInstance());
}
private void openOptionsScene() {
optionsController.beforeOpen(() -> primaryStage.setScene(mainScene));
primaryStage.setScene(optionsScene);
}
private void addQuest(Quest quest) {

View File

@@ -19,6 +19,7 @@ public class MainController {
public Label budgetLabel;
public Button balanceButton;
public Button optionsButton;
public Pane storagePane;

View File

@@ -0,0 +1,71 @@
package de.siphalor.was.visual.jfx.controller;
import de.siphalor.was.WhatAStorage;
import de.siphalor.was.content.lang.I18n;
import de.siphalor.was.game.Options;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ToggleButton;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class OptionsController {
private Runnable exitRunnable;
public ChoiceBox<LangEntry> languageChoice;
public ToggleButton allowQuestResolvingToggle;
public ToggleButton autoQuestRefillToggle;
public void beforeOpen(@NotNull Runnable exitRunnable) {
Options options = WhatAStorage.getInstance().getOptions();
this.exitRunnable = exitRunnable;
I18n i18n = I18n.getInstance();
String curLang = i18n.getLang().getCode();
languageChoice.getItems().clear();
String[] langs = WhatAStorage.getInstance().getLangs();
Arrays.sort(langs);
for (String lang : langs) {
LangEntry langEntry = new LangEntry(lang, i18n.getString("langs." + lang));
languageChoice.getItems().add(langEntry);
if (curLang.equals(lang)) {
languageChoice.setValue(langEntry);
}
}
allowQuestResolvingToggle.setSelected(options.getAllowQuestResolving());
autoQuestRefillToggle.setSelected(options.getAutoRefillQuests());
}
@FXML
private void onLeaveClicked() {
Options options = WhatAStorage.getInstance().getOptions();
I18n.getInstance().setLang(languageChoice.getValue().getCode(), WhatAStorage.getInstance().getContentManager());
options.setAllowQuestResolving(allowQuestResolvingToggle.isSelected());
options.setAutoRefillQuests(autoQuestRefillToggle.isSelected());
exitRunnable.run();
}
private static class LangEntry {
private final String code;
private final String name;
private LangEntry(String code, String name) {
this.code = code;
this.name = name;
}
@Override
public String toString() {
return name;
}
public String getCode() {
return code;
}
}
}

View File

@@ -39,8 +39,10 @@ public class QuestController {
if (dragEvent.getDragboard().hasContent(DraggedProduct.FORMAT)) {
DraggedProduct product = (DraggedProduct) dragEvent.getDragboard().getContent(DraggedProduct.FORMAT);
if (product instanceof DraggedProduct.Quest) {
if (WhatAStorage.getInstance().getOptions().getAllowQuestResolving()) {
DraggedProduct.Quest quest = (DraggedProduct.Quest) product;
return WhatAStorage.getInstance().canQuestsResolve(quest.index, getIndex());
}
} else if (product instanceof DraggedProduct.Slot) {
DraggedProduct.Slot slot = (DraggedProduct.Slot) product;
return WhatAStorage.getInstance().canDeliverProduct(getIndex(), slot.x, slot.y);

View File

@@ -1,11 +1,18 @@
package de.siphalor.was.visual.jfx.util;
import de.siphalor.was.assets.AssetsManager;
import de.siphalor.was.content.lang.I18n;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import java.io.IOException;
public class JFXUtil {
public static Image scaleTo(Image image, int width) {
double scale = image.getWidth() / (double) width;
@@ -33,4 +40,19 @@ public class JFXUtil {
node.getStyleClass().remove(clazz);
}
}
public static Scene loadScene(String fxml, Object controller, I18n i18n) {
FXMLLoader loader = new FXMLLoader();
loader.setResources(i18n);
loader.setController(controller);
Parent parent = null;
try {
parent = loader.load(AssetsManager.getStream("jfx/" + fxml + ".fxml").orElseThrow());
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(parent);
scene.getStylesheets().add("assets/jfx/main.css");
return scene;
}
}

View File

@@ -3,11 +3,16 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/9.0.1">
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<tabs>
<Tab text="%game.balance.history">
<content>
@@ -15,7 +20,8 @@
<columns>
<TableColumn prefWidth="50.666626400416135" styleClass="align-right" text="%game.balance.history.index" />
<TableColumn editable="false" maxWidth="500.0" prefWidth="100.0" styleClass="align-right" text="%game.balance.history.change" />
<TableColumn maxWidth="1.7976931348623157E308" prefWidth="300.0" text="%game.balance.history.type" />
<TableColumn maxWidth="1.7976931348623157E308" prefWidth="207.0" text="%game.balance.history.type" />
<TableColumn prefWidth="161.0" text="%game.balance.history.product" />
</columns>
<placeholder>
<Label text="%game.balance.history.no-data" />

View File

@@ -1,10 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox maxHeight="1.7976931348623157E308" minHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane>
@@ -22,7 +32,16 @@
</Label>
</items>
</ToolBar>
<Button alignment="CENTER" mnemonicParsing="false" onMouseClicked="#scheduleStop" styleClass="red" text="%game.quit" textFill="WHITE" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="7.0" />
<HBox layoutX="710.5" layoutY="7.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="7.0">
<children>
<Button fx:id="optionsButton" mnemonicParsing="false" text="%menu.options">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Button>
<Button alignment="CENTER" mnemonicParsing="false" onMouseClicked="#scheduleStop" styleClass="red" text="%game.quit" textFill="WHITE" />
</children>
</HBox>
</children>
</AnchorPane>
<SplitPane dividerPositions="0.3140277777777778" maxHeight="1.7976931348623157E308" VBox.vgrow="ALWAYS">

View File

@@ -11,6 +11,20 @@ Button.red:pressed {
-fx-background-color: #9c2121;
}
.toggle-button {
-fx-min-width: 50;
-fx-max-width: 50;
-fx-min-height: 20;
-fx-border-color: #aaaaaa;
-fx-border-radius: 5;
-fx-background-radius: 5;
-fx-background-color: linear-gradient(to right, white 40%, #999999 40%, #dddddd);
-fx-background-insets: 0;
}
.toggle-button:selected {
-fx-background-color: linear-gradient(to right, #1695e8, #1279bb 60%, white 60%);
}
ScrollPane {
-fx-background-insets: 0;
-fx-border-insets: 0;
@@ -34,6 +48,14 @@ Label.red {
Label.green {
-fx-text-fill: #457229;
}
.options-menu > Label {
-fx-font-size: 16;
}
.options-menu .heading {
-fx-padding: 10 0 0 0;
-fx-font-size: 20;
-fx-font-weight: bold;
}
.align-right {
-fx-alignment: center-right;

View File

@@ -1,5 +1,12 @@
test.hello-world = Hallo Welt!
menu.back = Zur\u00fcck
menu.options = Optionen
menu.options.general = Allgemein
menu.options.gameplay = Spielmechanik
menu.options.gameplay.quest-resolving = Erlauben Auftr\u00e4ge gegeneinander aufzul\u00f6sen
menu.options.gameplay.auto-quest-refill = Automatisch neue Auftr\u00e4ge akzeptieren
game.budget = Budget: %d\u20ac
game.quit = Spiel beenden
game.quests = Auftr\u00e4ge
@@ -21,6 +28,7 @@ game.balance.history.type.move = Produkt bewegt
game.balance.history.type.noop = NOOP
game.balance.history.type.deliver = Produkt ausgelagert
game.balance.history.type.store = Produkt eingelagert
game.balance.history.product = Produkt
game.balance.chart = Bilanz
game.balance.chart.line = Budget
game.balance.total-income = Gesamteinahmen: %d\u20ac

View File

@@ -1,5 +1,15 @@
test.hello-world = Hello World!
langs.en_us = English (America)
langs.de_de = Deutsch
menu.options = Options
menu.options.back = Apply & Leave
menu.options.general = General
menu.options.gameplay = Gameplay
menu.options.gameplay.quest-resolving = Allow quest resolving
menu.options.gameplay.auto-quest-refill = Automatically refill quests
game.budget = Budget: %d$
game.quit = Quit Game
game.quests = Quests
@@ -21,6 +31,7 @@ game.balance.history.type.move = Product moved
game.balance.history.type.noop = NOOP
game.balance.history.type.deliver = Product delivered
game.balance.history.type.store = Product stored
game.balance.history.product = Product
game.balance.chart = Balance
game.balance.chart.line = Budget
game.balance.total-income = Total Income: %d$

View File

@@ -35,18 +35,21 @@ public class ContentTests {
@Test
public void areLangsUpToDate() throws IOException {
Properties properties = new Properties();
InputStream inputStream = contentManager.getResource("lang/en_us.lang").orElseThrow().getInputStream();
InputStream inputStream = contentManager.getResource("lang/en_us.properties").orElseThrow().getInputStream();
properties.load(inputStream);
Assertions.assertAll(contentManager.getResources("lang", "lang").flatMap(resource -> {
if (resource.getId().equals("lang/en_us.lang")) {
if (resource.getId().equals("lang/en_us.properties")) {
return Stream.empty();
}
Properties props = new Properties();
return Stream.of(() -> {
Assertions.assertDoesNotThrow(() -> props.load(resource.getInputStream()));
Assertions.assertAll(properties.keySet().stream().map(key -> () ->
Assertions.assertTrue(props.containsKey(key), "Missing lang key " + key + " for file " + resource.getId())
Assertions.assertAll(properties.keySet().stream().map(key -> () -> {
if (!key.toString().startsWith("langs.")) {
Assertions.assertTrue(props.containsKey(key), "Missing lang key " + key + " for file " + resource.getId());
}
}
));
});
}));