From c529a375c2357838690dc3dafcd25eba2ce1d1b5 Mon Sep 17 00:00:00 2001 From: Marcin Chwedczuk <0xmarcin+gh@gmail.com> Date: Wed, 20 Nov 2024 12:09:52 +0100 Subject: [PATCH] make digit butons work --- .../ScientificCalculatorViewModel.java | 7 +++ .../gui/views/scientific/ScientificView.fxml | 16 +++--- .../gui/views/scientific/ScientificView.java | 49 ++++++++++++++++++- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/gui/src/main/java/mscalc/gui/viewmodel/ScientificCalculatorViewModel.java b/gui/src/main/java/mscalc/gui/viewmodel/ScientificCalculatorViewModel.java index 6ccbe6a..b200c19 100644 --- a/gui/src/main/java/mscalc/gui/viewmodel/ScientificCalculatorViewModel.java +++ b/gui/src/main/java/mscalc/gui/viewmodel/ScientificCalculatorViewModel.java @@ -8,12 +8,19 @@ import mscalc.engine.commands.Command; import mscalc.engine.commands.IExpressionCommand; import mscalc.engine.resource.JavaBundleResourceProvider; +import mscalc.gui.views.scientific.ScientificView; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.ArrayList; import java.util.List; import java.util.Objects; public class ScientificCalculatorViewModel { + private static final Logger logger = LogManager.getLogger(ScientificCalculatorViewModel.class); + + + private final List allInputs = new ArrayList<>(); private final CalculatorManager calculatorManager = new CalculatorManager( diff --git a/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.fxml b/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.fxml index 27a60bc..9adc8a3 100644 --- a/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.fxml +++ b/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.fxml @@ -2,11 +2,9 @@ - - + type="javafx.scene.layout.VBox" + stylesheets="@ScientificView.css" styleClass="scientificView"> @@ -14,12 +12,12 @@ - + - - - - + + + + diff --git a/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java b/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java index a3cde5c..17a4be6 100644 --- a/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java +++ b/gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java @@ -1,19 +1,27 @@ package mscalc.gui.views.scientific; +import javafx.beans.binding.Bindings; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.TextField; +import javafx.scene.control.*; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.VBox; +import mscalc.engine.RadixType; +import mscalc.gui.App; import mscalc.gui.viewmodel.ScientificCalculatorViewModel; import mscalc.gui.views.CalculatorView; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.IOException; public class ScientificView extends VBox implements CalculatorView { + private static final Logger logger = LogManager.getLogger(ScientificView.class); + private final ScientificCalculatorViewModel viewModel = new ScientificCalculatorViewModel(); public ScientificView() { @@ -35,6 +43,20 @@ public ScientificView() { @FXML private TextField display; + @FXML + private ToggleGroup radixToggleGroup; + @FXML + private RadioButton radioRadixHex; + @FXML + private RadioButton radioRadixDec; + @FXML + private RadioButton radioRadixOct; + @FXML + private RadioButton radioRadixBin; + + @FXML + private Button bDigit0; + @FXML private Button bDigit1; @@ -89,6 +111,29 @@ public ScientificView() { public void install(Scene scene) { display.textProperty().bind(viewModel.displayProperty); + radixToggleGroup.selectToggle(radioRadixDec); + viewModel.radixProperty.addListener((observable, oldValue, newValue) -> { + logger.info("Selected radix from ViewModel: {}", newValue); + radixToggleGroup.selectToggle(switch (newValue) { + case Hex -> radioRadixHex; + case Decimal -> radioRadixDec; + case Octal -> radioRadixOct; + case Binary -> radioRadixBin; + default -> null; + }); + }); + radixToggleGroup.selectedToggleProperty().addListener((observableValue, oldValue, newValue) -> { + RadixType radix = + (newValue == radioRadixHex) ? RadixType.Hex : + (newValue == radioRadixDec) ? RadixType.Decimal : + (newValue == radioRadixOct) ? RadixType.Octal : + (newValue == radioRadixBin) ? RadixType.Binary : + null; + logger.info("Selected radix from UI: {}", radix); + viewModel.radixProperty.set(radix); + }); + + bindButton(bDigit0, viewModel.digit0Button); bindButton(bDigit1, viewModel.digit1Button); bindButton(bDigit2, viewModel.digit2Button); bindButton(bDigit3, viewModel.digit3Button);