Skip to content

Commit

Permalink
improvements around invert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 21, 2024
1 parent 29addf9 commit ac3d77a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public class ScientificCalculatorViewModel {

public final StringProperty displayProperty = new SimpleStringProperty("");

// -- INVERT & HYP BUTTONS ---
public final InputViewModel invertButton = newInputViewModel()
.withText("Inverse")
.withCommand(Command.CommandINV)
.withKeyboardShortcut(KeyCode.I)
.build();

public final InputViewModel hyperbolicButton = newInputViewModel()
.withText("Hyper")
.withCommand(Command.CommandHYP)
.withKeyboardShortcut(KeyCode.H)
.build();

// --- CONTROL BUTTONS ---
public final InputViewModel clearButton = newInputViewModel()
.withText("C")
Expand Down Expand Up @@ -168,8 +181,9 @@ public class ScientificCalculatorViewModel {
.build();

public final InputViewModel piButton = newInputViewModel()
.withText("π")
.withCommand(Command.CommandPI)
.withModeText("π", "2π")
// TODO: Implement 2xPI
.withModeCommand(Command.CommandPI, Command.CommandPI)
.withKeyboardShortcut(KeyCode.P)
.withEnabled(radixProperty.isEqualTo(RadixType.Decimal))
.build();
Expand Down Expand Up @@ -395,6 +409,8 @@ public class ScientificCalculatorViewModel {
public ScientificCalculatorViewModel() {
// Initialize Scientific mode
calculatorManager.sendCommand(Command.ModeScientific);
// Select radians
calculatorManager.sendCommand(Command.CommandRAD);
}

public InputViewModelBuilder newInputViewModel() {
Expand Down Expand Up @@ -534,7 +550,19 @@ public InputViewModel(StringExpression textProperty,
}

public void execute() {
calculatorManager.sendCommand(this.commandProperty.get());
switch (this.commandProperty.get()) {
case CommandINV -> {
invertedModeProperty.set(!invertedModeProperty.get());
}

case CommandHYP -> {
hyperbolicModeProperty.set(!hyperbolicModeProperty.get());
}

default -> {
calculatorManager.sendCommand(this.commandProperty.get());
}
}
}

public StringExpression textProperty() {
Expand Down
27 changes: 4 additions & 23 deletions gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import mscalc.engine.RadixType;
import mscalc.gui.App;
import mscalc.gui.viewmodel.ScientificCalculatorViewModel;
import mscalc.gui.viewmodel.ScientificCalculatorViewModel.KeyboardCode;
import mscalc.gui.views.CalculatorView;
Expand All @@ -26,7 +21,7 @@ public class ScientificView extends VBox implements CalculatorView {
private static final Logger logger = LogManager.getLogger(ScientificView.class);

private final ScientificCalculatorViewModel viewModel = new ScientificCalculatorViewModel();
private final HashMap<KeyboardCode, Button> keyboardMapping = new HashMap<>();
private final HashMap<KeyboardCode, ButtonBase> keyboardMapping = new HashMap<>();

public ScientificView() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ScientificView.fxml"));
Expand Down Expand Up @@ -234,8 +229,8 @@ public ScientificView() {
public void install(Scene scene) {
display.textProperty().bind(viewModel.displayProperty);

cbInvert.selectedProperty().bindBidirectional(viewModel.invertedModeProperty);
cbHyperbolic.selectedProperty().bindBidirectional(viewModel.hyperbolicModeProperty);
bindButton(cbInvert, viewModel.invertButton);
bindButton(cbHyperbolic, viewModel.hyperbolicButton);

radixToggleGroup.selectToggle(radioRadixDec);
viewModel.radixProperty.addListener((observable, oldValue, newValue) -> {
Expand Down Expand Up @@ -335,23 +330,9 @@ public void uninstall() {
throw new RuntimeException("TODO");
}

private void bindButton(Button button, ScientificCalculatorViewModel.InputViewModel operation) {
private void bindButton(ButtonBase button, ScientificCalculatorViewModel.InputViewModel operation) {
button.textProperty().bind(operation.textProperty());

var defaultFont = button.getFont();
button.fontProperty().bind(button.textProperty().map(t -> {
if (t == null) return defaultFont;
if (t.length() < 2) {
// Increase font size
return new Font(defaultFont.getName(), defaultFont.getSize() + 1);
} else if (t.length() > 3) {
// Decrease font size
return new Font(defaultFont.getName(), defaultFont.getSize() - 1);
} else {
return defaultFont;
}
}));

// Pre-create the tooltip object
Tooltip t = new Tooltip();
t.textProperty().bind(operation.tooltipProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

.button {
-fx-pref-width: 3.3em;
-fx-padding: 0.333333em 0.2em;

// disable focus on buttons
-fx-focus-traversable: false;

Expand Down

0 comments on commit ac3d77a

Please sign in to comment.