Skip to content

Commit

Permalink
add connection for digit buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 20, 2024
1 parent c4e50bc commit 65b8da1
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 69 deletions.
32 changes: 15 additions & 17 deletions engine/src/main/java/mscalc/engine/CCalcEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;

import static java.util.Map.entry;
Expand Down Expand Up @@ -250,7 +248,7 @@ public static String OpCodeToUnaryString(int nOpCode, boolean fInv, AngleType an
int m_precedenceOpCount; /* Current number of precedence ops in holding. */
int m_nLastCom; // Last command entered.
AngleType m_angletype; // Current Angle type when in dec mode. one of deg, rad or grad
NUM_WIDTH m_numwidth; // one of qword, dword, word or byte mode.
NumberWidth m_numwidth; // one of qword, dword, word or byte mode.
int m_dwWordBitWidth; // # of bits in currently selected word size

Random m_randomGeneratorEngine = new Random();
Expand Down Expand Up @@ -298,7 +296,7 @@ public CCalcEngine(boolean fPrecedence,
m_precedenceOpCount = (0);
m_nLastCom = (0);
m_angletype = (AngleType.Degrees);
m_numwidth = (NUM_WIDTH.QWORD_WIDTH);
m_numwidth = (NumberWidth.QWORD_WIDTH);
m_HistoryCollector = new History(pCalcDisplay, pHistoryDisplay, DEFAULT_DEC_SEPARATOR);
m_groupSeparator = (DEFAULT_GRP_SEPARATOR);

Expand Down Expand Up @@ -954,7 +952,7 @@ void ProcessCommandWorker(int wParam) {
case IDM_DEC:
case IDM_OCT:
case IDM_BIN: {
SetRadixTypeAndNumWidth(RadixType.fromCppValue(wParam - IDM_HEX), NUM_WIDTH.UNDEFINED); // TODO: cast -1 to enum; Add undef value to enum
SetRadixTypeAndNumWidth(RadixType.fromInt(wParam - IDM_HEX), NumberWidth.UNDEFINED); // TODO: cast -1 to enum; Add undef value to enum
m_HistoryCollector.updateHistoryExpression(m_radix, m_precision);
break;
}
Expand All @@ -969,7 +967,7 @@ void ProcessCommandWorker(int wParam) {
}

// Compat. mode BaseX: Qword, Dword, Word, Byte
SetRadixTypeAndNumWidth(RadixType.Unknown, NUM_WIDTH.fromInt(wParam - IDM_QWORD));
SetRadixTypeAndNumWidth(RadixType.Unknown, NumberWidth.fromInt(wParam - IDM_QWORD));
break;

case IDM_DEG:
Expand Down Expand Up @@ -1277,7 +1275,7 @@ static class LASTDISP {
int precision;
uint radix;
int nFE;
NUM_WIDTH numwidth;
NumberWidth numwidth;
boolean fIntMath;
boolean bRecord;
boolean bUseSep;
Expand All @@ -1287,7 +1285,7 @@ public LASTDISP(
int precision,
uint radix,
int nFE,
NUM_WIDTH numwidth,
NumberWidth numwidth,
boolean fIntMath,
boolean bRecord,
boolean bUseSep
Expand All @@ -1304,7 +1302,7 @@ public LASTDISP(
}
}

static LASTDISP gldPrevious = new LASTDISP(Rational.of(0), -1, uint.ZERO, -1, NUM_WIDTH.UNDEFINED, false, false, false);
static LASTDISP gldPrevious = new LASTDISP(Rational.of(0), -1, uint.ZERO, -1, NumberWidth.UNDEFINED, false, false, false);

// Truncates if too big, makes it a non negative - the number in rat. Doesn't do anything if not in INT mode
Rational TruncateNumForIntMath(Rational rat) {
Expand Down Expand Up @@ -2005,7 +2003,7 @@ Rational DoOperation(int operation, Rational lhs, Rational rhs) {

// To be called when either the radix or num width changes. You can use -1 in either of these values to mean
// dont change that.
void SetRadixTypeAndNumWidth(RadixType radixtype, NUM_WIDTH numwidth) {
void SetRadixTypeAndNumWidth(RadixType radixtype, NumberWidth numwidth) {
// When in integer mode, the number is represented in 2's complement form. When a bit width is changing, we can
// change the number representation back to sign, abs num form in ratpak. Soon when display sees this, it will
// convert to 2's complement form, but this time all high bits will be propagated. Eg. -127, in byte mode is
Expand All @@ -2023,13 +2021,13 @@ void SetRadixTypeAndNumWidth(RadixType radixtype, NUM_WIDTH numwidth) {
}
}

if (radixtype.cppValue() >= RadixType.Hex.cppValue() && radixtype.cppValue() <= RadixType.Binary.cppValue()) {
if (radixtype.toInt() >= RadixType.Hex.toInt() && radixtype.toInt() <= RadixType.Binary.toInt()) {
m_radix = uint.of(NRadixFromRadixType(radixtype));
// radixtype is not even saved
}

// TODO: Better validation
if (numwidth.toInt() >= NUM_WIDTH.QWORD_WIDTH.toInt() && numwidth.toInt() <= NUM_WIDTH.BYTE_WIDTH.toInt()) {
if (numwidth.toInt() >= NumberWidth.QWORD_WIDTH.toInt() && numwidth.toInt() <= NumberWidth.BYTE_WIDTH.toInt()) {
m_numwidth = numwidth;
m_dwWordBitWidth = DwWordBitWidthFromNumWidth(numwidth);
}
Expand All @@ -2042,15 +2040,15 @@ void SetRadixTypeAndNumWidth(RadixType radixtype, NUM_WIDTH numwidth) {
DisplayNum();
}

int DwWordBitWidthFromNumWidth(NUM_WIDTH numwidth) {
int DwWordBitWidthFromNumWidth(NumberWidth numwidth) {
switch (numwidth) {
case NUM_WIDTH.DWORD_WIDTH:
case NumberWidth.DWORD_WIDTH:
return 32;
case NUM_WIDTH.WORD_WIDTH:
case NumberWidth.WORD_WIDTH:
return 16;
case NUM_WIDTH.BYTE_WIDTH:
case NumberWidth.BYTE_WIDTH:
return 8;
case NUM_WIDTH.QWORD_WIDTH:
case NumberWidth.QWORD_WIDTH:
default:
return 64;
}
Expand Down
5 changes: 5 additions & 0 deletions engine/src/main/java/mscalc/engine/DegreeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mscalc.engine;

public enum DegreeType {
Radians, Gradians, Degrees
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mscalc.engine;

// This is expected to be in same order as IDM_QWORD, IDM_DWORD etc.
enum NUM_WIDTH
public enum NumberWidth
{
UNDEFINED(-1),
QWORD_WIDTH(0), // Number width of 64 bits mode (default)
Expand All @@ -11,15 +11,15 @@ enum NUM_WIDTH

private final int value;

private NUM_WIDTH(int v) {
private NumberWidth(int v) {
this.value = v;
}

public int toInt() {
return value;
}

public static NUM_WIDTH fromInt(int n) {
public static NumberWidth fromInt(int n) {
return switch (n) {
case 0 -> QWORD_WIDTH;
case 1 -> DWORD_WIDTH;
Expand Down
23 changes: 18 additions & 5 deletions engine/src/main/java/mscalc/engine/RadixType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

// This is expected to be in same order as IDM_HEX, IDM_DEC, IDM_OCT, IDM_BIN
public enum RadixType {
Unknown(-1), Hex(0), Decimal(1), Octal(2), Binary(3);
Unknown(-1),
Hex(0),
Decimal(1),
Octal(2),
Binary(3);

private final int value;

private RadixType(int value) {
RadixType(int value) {
this.value = value;
}

// TODO: Fix it later
public int cppValue() {
public boolean hasDigit(int digit) {
return switch (this) {
case Unknown -> false;
case Hex -> (0 <= digit && digit < 16);
case Decimal -> (0 <= digit && digit < 10);
case Octal -> (0 <= digit && digit < 8);
case Binary -> (0 <= digit && digit < 2);
};
}

public int toInt() {
return this.value;
}

public static RadixType fromCppValue(int value) {
public static RadixType fromInt(int value) {
return switch (value) {
case 0 -> Hex;
case 1 -> Decimal;
Expand Down
18 changes: 18 additions & 0 deletions gui/src/main/java/mscalc/gui/viewmodel/MoreBindings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mscalc.gui.viewmodel;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;

import java.util.function.Function;
import java.util.function.Predicate;

public class MoreBindings {
public static <T> BooleanBinding map(ObjectProperty<T> objProperty, Predicate<T> pred) {
return Bindings.createBooleanBinding(() -> {
T value = objProperty.get();
return (value != null) && pred.test(value);
}, objProperty);
}
}
Loading

0 comments on commit 65b8da1

Please sign in to comment.