From eedfae4270ed16d5e3dc407beed3032cf24b88e3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?H=C3=A5vard=20Daleng?=
 <142524365+MrMarHVD@users.noreply.github.com>
Date: Fri, 17 May 2024 11:17:06 +0200
Subject: [PATCH] Created basic combobox solution. Not yet fully functional.

---
 .../controller/game/ChaosGameDescription.java |  9 +++
 .../controller/game/ChaosGameFileHandler.java |  1 -
 .../controller/game/GuiButtonController.java  |  1 +
 .../ChaosGameDescriptionFactory.java          |  2 +-
 .../stud/chaosgame/view/ChaosGameGui.java     | 56 ++++++++++++-------
 .../desc_1.txt                                |  2 +-
 .../desc_2.txt                                |  2 +-
 .../desc_3.txt                                |  2 +-
 .../desc_4.txt                                |  0
 .../desc_5.txt                                |  0
 10 files changed, 51 insertions(+), 24 deletions(-)
 rename src/main/resources/descriptions/{factory => saved_descriptions}/desc_1.txt (96%)
 rename src/main/resources/descriptions/{factory => saved_descriptions}/desc_2.txt (96%)
 rename src/main/resources/descriptions/{factory => saved_descriptions}/desc_3.txt (96%)
 rename src/main/resources/descriptions/{factory => saved_descriptions}/desc_4.txt (100%)
 rename src/main/resources/descriptions/{factory => saved_descriptions}/desc_5.txt (100%)

diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java
index fc06e50..3c84ec8 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java
@@ -94,5 +94,14 @@ public class ChaosGameDescription {
         return maxCoords;
     }
 
+  /**
+   * Getter method for the name of the chaos game.
+   *
+   * @return the name of the chaos game description.
+   */
+  public String getName() {
+        return name;
+    }
+
 
 }
diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java
index 671d08d..f4d4a10 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameFileHandler.java
@@ -64,7 +64,6 @@ public class ChaosGameFileHandler {
         }
 
 
-
         // Read the transforms
         while (scanner.hasNextDouble()) {
           double a00 = scanner.nextDouble();
diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java
index 5c06b2b..ca8b5e0 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java
@@ -176,6 +176,7 @@ public class GuiButtonController {
    * Clear the image view.
    */
   public void clearImageView() {
+    chaosCanvas.clearCanvas();
     GraphicsContext gc = gui.getCanvas().getGraphicsContext2D();
     gc.clearRect(0, 0, gui.getCanvas().getWidth(), gui.getCanvas().getHeight());
     gui.getImageView().setImage(null);
diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java
index 11cd36f..73555b7 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java
@@ -30,7 +30,7 @@ public class ChaosGameDescriptionFactory {
     /**
      * The directory where the descriptions are located.
      */
-    String directory1 = "src/main/resources/descriptions/factory/";
+    String directory1 = "src/main/resources/descriptions/saved_descriptions/";
     this.descriptions = new ArrayList<>();
     ChaosGameFileHandler handler = new ChaosGameFileHandler();
     File directory = new File(directory1);
diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java
index 5b1db2d..c6f3aba 100644
--- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java
+++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java
@@ -19,7 +19,6 @@ import javafx.scene.control.*;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 
-import javafx.scene.image.WritableImage;
 import javafx.scene.layout.*;
 import javafx.scene.paint.Color;
 import javafx.scene.text.Font;
@@ -32,9 +31,7 @@ import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import javafx.stage.Window;
 import javafx.util.Duration;
-import javafx.scene.control.ToolBar;
 
-import javax.tools.Tool;
 import java.io.IOException;
 // todo: look through GUI and get rid of redundancies, clean up code
 public class ChaosGameGui implements ChaosGameObserver {
@@ -166,6 +163,9 @@ public class ChaosGameGui implements ChaosGameObserver {
    */
   private RadioButton improvedBarnsleyButton;
 
+  /**
+   * A description ComboBox for choosing different fractal descriptions.
+   */
   private ComboBox<String> descriptionComboBox;
 
   /**
@@ -212,12 +212,12 @@ public class ChaosGameGui implements ChaosGameObserver {
     resizeCanvas();
   });
 
-// Bind the width of the sideMenu to the width of the scene
-sideMenu.prefWidthProperty().bind(scene.widthProperty().multiply(0.2)); // 20% of the scene width
+  // Bind the width of the sideMenu to the width of the scene
+  sideMenu.prefWidthProperty().bind(scene.widthProperty().multiply(0.2)); // 20% of the scene width
 
-// Bind the height of the sideMenu to the height of the scene
-sideMenu.prefHeightProperty().bind(scene.heightProperty());
-}
+  // Bind the height of the sideMenu to the height of the scene
+  sideMenu.prefHeightProperty().bind(scene.heightProperty());
+  }
 
 
   /**
@@ -230,7 +230,7 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
     this.initializeImageView();
 
     //this.initializeMainButtons();
-    this.initializeFractalButtons();
+    this.initializeFractalComponents();
     this.initializeSideMenu();
 
     this.scene = new Scene(this.borderPane,1700,1000);
@@ -290,7 +290,7 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
   /**
    * Initialize the buttons related to managing the fractals.
    */
-  private void initializeFractalButtons() {
+  private void initializeFractalComponents() {
 
     // Radio buttons for choosing fractal type
     ToggleGroup group = new ToggleGroup();
@@ -304,7 +304,6 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
     this.improvedBarnsleyButton = new RadioButton("Improved Barnsley");
     improvedBarnsleyButton.setToggleGroup(group);
 
-
     this.descriptionComboBox = new ComboBox<>();
 
 
@@ -327,6 +326,26 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
       controller.updateDescription(3);
     });
 
+    // Recreate the ChaosGameDescriptionFactory and update the ComboBox items each time the ComboBox is shown
+    descriptionComboBox.setOnShowing(event -> {
+      factory = new ChaosGameDescriptionFactory();
+      descriptionComboBox.getItems().clear();
+      for (ChaosGameDescription description : factory.getDescriptions()) {
+        descriptionComboBox.getItems().add(description.getName());
+      }
+    });
+
+    // Update the controller's description when a new item is selected in the ComboBox
+    descriptionComboBox.getSelectionModel().selectedItemProperty()
+        .addListener((observable, oldValue, newValue) -> {
+      for (int i = 0; i < factory.getDescriptions().size(); i++) {
+        if (description.getName().equals(newValue)) {
+          controller.updateDescription(i);
+          break;
+        }
+      }
+    });
+
     // Load fractal file button and tooltip
     this.loadFractalFromFileButton = new Button("Load Fractal");
     Tooltip loadFractalFromFileButtonTooltip = new Tooltip("Load a text file describing a new fractal chaos game");
@@ -379,9 +398,9 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
     VBox controlButtonBox = new VBox();
     controlButtonBox.setBorder(blackBorder);
     controlButtonBox.setPadding(new Insets(5,5,5,5));
-    VBox radioButtonBox = new VBox();
-    radioButtonBox.setBorder(blackBorder);
-    radioButtonBox.setPadding(new Insets(5,5,5,5));
+    VBox descriptionBox = new VBox();
+    descriptionBox.setBorder(blackBorder);
+    descriptionBox.setPadding(new Insets(5,5,5,5));
     VBox bottomButtonBox = new VBox();
 
     // Step Count GUI
@@ -491,16 +510,15 @@ sideMenu.prefHeightProperty().bind(scene.heightProperty());
     sideMenu.getChildren().addAll(startButton,stopButton, clearButton);
 
 
-
     //Radio Button header label
     Label chaosGameTypeLabel = new Label("Chaos Game Selection");
     chaosGameTypeLabel.setFont(new Font("Arial",20));
     chaosGameTypeLabel.setAlignment(Pos.CENTER);
     // Add fractal radio buttons
-    radioButtonBox.getChildren().addAll(chaosGameTypeLabel,sierpinskiRadioButton, barnsleyRadioButton, juliaRadioButton,
-            improvedBarnsleyButton);
-    radioButtonBox.setSpacing(5);
-    sideMenu.getChildren().add(radioButtonBox);
+    descriptionBox.getChildren().addAll(chaosGameTypeLabel,sierpinskiRadioButton, barnsleyRadioButton, juliaRadioButton,
+            improvedBarnsleyButton, descriptionComboBox);
+    descriptionBox.setSpacing(5);
+    sideMenu.getChildren().add(descriptionBox);
 
     sideMenu.getChildren().addAll(separator1, colorVBox, separator2);
     //this.initializeColorButtonHandler();
diff --git a/src/main/resources/descriptions/factory/desc_1.txt b/src/main/resources/descriptions/saved_descriptions/desc_1.txt
similarity index 96%
rename from src/main/resources/descriptions/factory/desc_1.txt
rename to src/main/resources/descriptions/saved_descriptions/desc_1.txt
index 1981cb8..9962b6f 100644
--- a/src/main/resources/descriptions/factory/desc_1.txt
+++ b/src/main/resources/descriptions/saved_descriptions/desc_1.txt
@@ -1,5 +1,5 @@
 Affine2D                # Type of transform
-Test1
+Sierpinski
 0, 0                    # Lower left
 1, 1                    # Upper right
 .5, 0, 0, .5, 0, 0      # 1st transform (a00, a01, a10, a11, b0, b1)
diff --git a/src/main/resources/descriptions/factory/desc_2.txt b/src/main/resources/descriptions/saved_descriptions/desc_2.txt
similarity index 96%
rename from src/main/resources/descriptions/factory/desc_2.txt
rename to src/main/resources/descriptions/saved_descriptions/desc_2.txt
index 43a4dd6..e0f91d7 100644
--- a/src/main/resources/descriptions/factory/desc_2.txt
+++ b/src/main/resources/descriptions/saved_descriptions/desc_2.txt
@@ -1,5 +1,5 @@
 Affine2D                # Type of transform
-Test2
+Barnsley
 -2.65, 0                # lower left
 2.65, 10                # upper right
 0, 0, 0, .16, 0, 0      # transforms
diff --git a/src/main/resources/descriptions/factory/desc_3.txt b/src/main/resources/descriptions/saved_descriptions/desc_3.txt
similarity index 96%
rename from src/main/resources/descriptions/factory/desc_3.txt
rename to src/main/resources/descriptions/saved_descriptions/desc_3.txt
index 8fdd084..9201507 100644
--- a/src/main/resources/descriptions/factory/desc_3.txt
+++ b/src/main/resources/descriptions/saved_descriptions/desc_3.txt
@@ -1,5 +1,5 @@
 Julia               # Type of transform
-Test3
+Julia
 -1.6, -1            # Lower left
 1.6, 1              # Upper right
 -.74543, .11301     # Real and imaginary parts of the constant c
\ No newline at end of file
diff --git a/src/main/resources/descriptions/factory/desc_4.txt b/src/main/resources/descriptions/saved_descriptions/desc_4.txt
similarity index 100%
rename from src/main/resources/descriptions/factory/desc_4.txt
rename to src/main/resources/descriptions/saved_descriptions/desc_4.txt
diff --git a/src/main/resources/descriptions/factory/desc_5.txt b/src/main/resources/descriptions/saved_descriptions/desc_5.txt
similarity index 100%
rename from src/main/resources/descriptions/factory/desc_5.txt
rename to src/main/resources/descriptions/saved_descriptions/desc_5.txt
-- 
GitLab