Skip to content

Commit

Permalink
Add checks for version of geometry_generator of opened file
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobTernes committed Mar 19, 2024
1 parent 9513dfb commit 0b4f165
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 47 deletions.
14 changes: 14 additions & 0 deletions src/main/java/fabulator/FABulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import lombok.Getter;

import java.util.ArrayList;
Expand All @@ -38,6 +39,7 @@ public class FABulator extends Application {
private Stage stage;
private MainView mainView;

private List<Runnable> closedRequestListeners = new ArrayList<>();
private List<Runnable> closedListeners = new ArrayList<>();

public static void main(String[] args) {
Expand All @@ -49,8 +51,10 @@ public void start(Stage stage) {
application = this;

this.stage = stage;
this.stage.setOnCloseRequest(this::closeRequested);
this.stage.setTitle(APP_NAME);
this.stage.getIcons().add(ImageIcon.FABULOUS.getImage());

this.mainView = new MainView();

Scene scene = new Scene(
Expand All @@ -75,6 +79,16 @@ public void stop() {
}
}

private void closeRequested(WindowEvent event) {
for (Runnable closedRequestListener : this.closedRequestListeners) {
closedRequestListener.run();
}
}

public void addClosedRequestListener(Runnable runnable) {
this.closedRequestListeners.add(runnable);
}

public void addClosedListener(Runnable runnable) {
this.closedListeners.add(runnable);
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/fabulator/language/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import javafx.beans.property.StringProperty;

public enum Text {
YES,
NO,
LEFT,
RIGHT,

Expand Down Expand Up @@ -96,7 +98,12 @@ public enum Text {
EXPLORER_HINT_2,

ERASE_FASM,
CLEAR_SELECTION;
CLEAR_SELECTION,

OUTDATED_VERSION,
OUTDATED_INFO_1,
OUTDATED_INFO_2,
OPEN_ANYWAYS;

private StringProperty stringProperty;

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/fabulator/object/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fabulator.object;

import lombok.Getter;

@Getter
public class Version { // TODO: tests

private int major;
private int minor;
private int patch;

public Version(String versionString) throws NumberFormatException {
this.parseString(versionString);
}

private void parseString(String versionString) throws NumberFormatException {
if (versionString != null && !versionString.isEmpty()) {
String[] parts = versionString.split("\\.");

if (parts.length == 3) {
this.major = Integer.parseInt(parts[0]);
this.minor = Integer.parseInt(parts[1]);
this.patch = Integer.parseInt(parts[2]);
} else {
throw new NumberFormatException();
}
} else {
throw new NumberFormatException();
}
}

public static boolean outdated(Version minVersion, Version version) {
boolean outdated = true;

if (version != null) {
outdated = !minVersion.lessEqual(version);
}
return outdated;
}

public boolean lessEqual(Version version) {
boolean lessEqual = false;

if (version.getMajor() > this.major) {
lessEqual = true;

} else if (version.getMajor() == this.major) {
if (version.getMinor() > this.minor) {
lessEqual = true;

} else if (version.getMinor() == this.minor) {
lessEqual = version.getPatch() >= this.patch;
}
}
return lessEqual;
}

@Override
public String toString() {
return String.format(
"%d.%d.%d",
this.major,
this.minor,
this.patch
);
}
}
6 changes: 4 additions & 2 deletions src/main/java/fabulator/parse/GeometryParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fabulator.geometry.*;
import fabulator.object.Location;
import fabulator.object.Version;
import fabulator.util.StringUtils;
import lombok.Getter;

Expand Down Expand Up @@ -37,7 +38,8 @@ enum ParsingMode {
}
private ParsingMode parsingMode;

private String generatorVersion;
private Version generatorVersion;

private String name;
private int numberOfRows;
private int numberOfColumns;
Expand Down Expand Up @@ -136,7 +138,7 @@ private void parseAsParams(String[] tokens, String attribute) {
assert tokens.length == 2;

switch (attribute) {
case "GeneratorVersion" -> this.generatorVersion = tokens[1];
case "GeneratorVersion" -> this.generatorVersion = new Version(tokens[1]);
case "Name" -> this.name = tokens[1];
case "Rows" -> this.numberOfRows = Integer.parseInt(tokens[1]);
case "Columns" -> this.numberOfColumns = Integer.parseInt(tokens[1]);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/fabulator/settings/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fabulator.language.Language;
import fabulator.logging.LogManager;
import fabulator.logging.Logger;
import fabulator.object.Version;
import fabulator.util.StringUtils;
import javafx.beans.property.*;
import javafx.scene.paint.Color;
Expand All @@ -22,6 +23,8 @@ public class Config {

private Properties properties;

private Version minGeneratorVersion;

private StringProperty openedFabricFileName;
private StringProperty openedFasmFileName;
private StringProperty openedHdlFileName;
Expand Down Expand Up @@ -63,6 +66,7 @@ private void initialize() {

Properties defaultProperties = this.loadDefaultProperties();
this.properties = this.loadProperties(defaultProperties);
this.buildNonPropertyObjs();
this.buildPropertyObjects();

} catch (IOException e) {
Expand Down Expand Up @@ -98,6 +102,12 @@ private boolean tryCreateProps() throws IOException {
return success;
}

private void buildNonPropertyObjs() {
this.minGeneratorVersion = new Version(
this.properties.getProperty("minGeneratorVersion")
);
}

private void buildPropertyObjects() {
this.openedFabricFileName = this.buildStringSetting("openedFabricFileName");
this.openedFasmFileName = this.buildStringSetting("openedFasmFileName");
Expand All @@ -115,7 +125,7 @@ private void buildPropertyObjects() {
this.belPortColor = this.buildColorSetting("belPortColor");
this.smConnInColor = this.buildColorSetting("smConnInColor");
this.smConnOutColor = this.buildColorSetting("smConnOutColor");
this.smConnJumpColor = this.buildColorSetting("smConnJumpColor");
this.smConnJumpColor = this.buildColorSetting("smConnJumpColor");
this.userDesignColor = this.buildColorSetting("userDesignColor");
this.userDesignMarkedColor = this.buildColorSetting("userDesignMarkedColor");

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fabulator/ui/builder/ButtonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public ButtonBuilder setColor(UiColor color) {
return this;
}

public ButtonBuilder setId(String id) {
this.button.setId(id);
return this;
}

public ButtonBuilder setTooltip(Text text) {
Tooltip tooltip = new Tooltip();
tooltip.textProperty().bind(text.stringProperty());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fabulator/ui/builder/LabelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public LabelBuilder setText(Text text) {
return this;
}

public LabelBuilder setWrapText(boolean wrapText) {
this.label.setWrapText(wrapText);
return this;
}

public LabelBuilder setTranslateX(double translateX) {
this.label.setTranslateX(translateX);
return this;
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/fabulator/ui/window/ChoiceDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fabulator.ui.window;

import fabulator.language.Text;
import fabulator.ui.builder.ButtonBuilder;
import fabulator.ui.builder.LabelBuilder;
import fabulator.util.LayoutUtils;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import lombok.Setter;

public class ChoiceDialog extends FABulatorWindow {

private Text[] dialogText;
private Button yesButton;
private Button noButton;

@Setter
private Runnable yesRunnable = () -> {};

@Setter
private Runnable noRunnable = () -> {};

public ChoiceDialog(int width, int height, Text title, Text... dialogText) {
super(width, height, title);

this.dialogText = dialogText;

this.initialize();
this.setup();
}

private void initialize() {
this.yesButton = new ButtonBuilder()
.setId("yesButton")
.setText(Text.YES)
.setTooltip(Text.YES)
.setOnAction(this::yesClicked)
.build();

this.noButton = new ButtonBuilder()
.setId("noButton")
.setText(Text.CANCEL)
.setTooltip(Text.CANCEL)
.setOnAction(this::noClicked)
.build();
}

private void setup() {
VBox root = new VBox();
root.getStyleClass().add("choice-dialog");

ObservableList<Node> rootChildren = root.getChildren();

for (Text text : this.dialogText) {
Label textLabel = new LabelBuilder()
.setText(text)
.setWrapText(true)
.build();
rootChildren.add(textLabel);
}

rootChildren.addAll(
LayoutUtils.vSpacer(),
new HBox(
LayoutUtils.hSpacer(),
this.yesButton,
this.noButton
)
);

this.setRoot(root);
}

private void yesClicked(ActionEvent event) {
this.close();
this.yesRunnable.run();
}

private void noClicked(ActionEvent event) {
this.close();
this.noRunnable.run();
}
}
6 changes: 6 additions & 0 deletions src/main/java/fabulator/ui/window/FABulatorWindow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fabulator.ui.window;

import fabulator.FABulator;
import fabulator.language.Text;
import fabulator.ui.icon.ImageIcon;
import fabulator.ui.style.Style;
Expand Down Expand Up @@ -27,6 +28,7 @@ public FABulatorWindow(int width, int height, Text title) {
this.setHeight(height);
this.setIcon();
this.initScene();
this.initListeners();
this.setStyle();
}

Expand Down Expand Up @@ -59,6 +61,10 @@ private void initScene() {
this.setScene(this.scene);
}

private void initListeners() {
FABulator.getApplication().addClosedRequestListener(this::close);
}

public void setRoot(Pane root) {
this.root = root;
this.scene.setRoot(root);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fabulator/ui/window/LoadingWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void buildSelf() {
);
root.setAlignment(Pos.CENTER);
root.setSpacing(20);
root.getStyleClass().add("loading-pane");
root.getStyleClass().add("loading-window"); // TODO: Add to StyleClass

this.setRoot(root);
}
Expand Down
Loading

0 comments on commit 0b4f165

Please sign in to comment.