Skip to content

Commit

Permalink
support radix change
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 21, 2024
1 parent ac3d77a commit 500cf5e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import javafx.beans.binding.StringExpression;
import javafx.beans.property.*;
import javafx.scene.input.KeyCode;
import javafx.scene.text.Font;
import mscalc.engine.*;
import mscalc.engine.commands.Command;
import mscalc.engine.commands.IExpressionCommand;
Expand Down Expand Up @@ -37,6 +36,31 @@ public class ScientificCalculatorViewModel {

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

// --- RADIX SELECT ---
public final InputViewModel radixHexButton = newInputViewModel()
.withText("Hex")
.withCommand(Command.CommandHex)
.withKeyboardShortcut(KeyCode.F5)
.build();

public final InputViewModel radixDecButton = newInputViewModel()
.withText("Dec")
.withCommand(Command.CommandDec)
.withKeyboardShortcut(KeyCode.F6)
.build();

public final InputViewModel radixOctButton = newInputViewModel()
.withText("Oct")
.withCommand(Command.CommandOct)
.withKeyboardShortcut(KeyCode.F7)
.build();

public final InputViewModel radixBinButton = newInputViewModel()
.withText("Bin")
.withCommand(Command.CommandBin)
.withKeyboardShortcut(KeyCode.F8)
.build();

// -- INVERT & HYP BUTTONS ---
public final InputViewModel invertButton = newInputViewModel()
.withText("Inverse")
Expand Down Expand Up @@ -417,6 +441,26 @@ public InputViewModelBuilder newInputViewModel() {
return new InputViewModelBuilder();
}

private void processRadixChangeCommand(Command command) {
switch (command) {
case CommandHex -> {
radixProperty.set(RadixType.Hex);
}

case CommandDec -> {
radixProperty.set(RadixType.Decimal);
}

case CommandOct -> {
radixProperty.set(RadixType.Octal);
}

case CommandBin -> {
radixProperty.set(RadixType.Binary);
}
}
}

public record KeyboardCode(
KeyCode key,
boolean control,
Expand Down Expand Up @@ -560,6 +604,7 @@ public void execute() {
}

default -> {
processRadixChangeCommand(this.commandProperty.get());
calculatorManager.sendCommand(this.commandProperty.get());
}
}
Expand All @@ -586,6 +631,7 @@ public Optional<KeyboardCode> keyboardShortcut() {
}
}


public class ThisViewModelCalculatorDisplay implements CalcDisplay {
private static final Logger logger = LogManager.getLogger(ThisViewModelCalculatorDisplay.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public void install(Scene scene) {
bindButton(cbHyperbolic, viewModel.hyperbolicButton);

radixToggleGroup.selectToggle(radioRadixDec);
bindButton(radioRadixHex, viewModel.radixHexButton);
bindButton(radioRadixDec, viewModel.radixDecButton);
bindButton(radioRadixOct, viewModel.radixOctButton);
bindButton(radioRadixBin, viewModel.radixBinButton);
/*
viewModel.radixProperty.addListener((observable, oldValue, newValue) -> {
logger.info("Selected radix from ViewModel: {}", newValue);
radixToggleGroup.selectToggle(switch (newValue) {
Expand All @@ -252,7 +257,7 @@ public void install(Scene scene) {
null;
logger.info("Selected radix from UI: {}", radix);
viewModel.radixProperty.set(radix);
});
});*/

bindButton(bClear, viewModel.clearButton);
bindButton(bClearEntry, viewModel.clearEntryButton);
Expand Down

0 comments on commit 500cf5e

Please sign in to comment.