Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ModularPanel buildUI(GuiData data, PanelSyncManager syncManager, UISettin
if (!syncManager.isClient())
getStack().setItemDamage(val);
}))
.setNumbers(0, Short.MAX_VALUE - 1))
.numbersInt(0, Short.MAX_VALUE - 1))
.child(IKey.str(" Amount: ").asWidget())
.child(new TextFieldWidget()
.size(30, 16)
Expand All @@ -95,7 +95,7 @@ public ModularPanel buildUI(GuiData data, PanelSyncManager syncManager, UISettin
if (!syncManager.isClient())
getStack().stackSize = value;
}))
.setNumbers(1, 127)))
.numbersInt(1, 127)))
.child(new TextFieldWidget()
.height(20)
.widthRel(1f)
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/cleanroommc/modularui/test/TestGuis.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,30 @@ public static ModularPanel buildContextMenu() {
.name("side_options"));
}

public static @NotNull ModularPanel buildTextFieldUI() {
return new ModularPanel("text_fields")
.coverChildrenHeight()
.width(120)
.padding(7)
.child(Flow.col()
.coverChildrenHeight()
.childPadding(2)
.crossAxisAlignment(Alignment.CrossAxis.START)
.fullWidth()
.child(IKey.str("Any").asWidget())
.child(new TextFieldWidget().fullWidth())
.child(IKey.str("Decimal numbers").asWidget())
.child(new TextFieldWidget()
.fullWidth()
.numbersDouble(-1000, 1000)
.usingScrollStep())
.child(IKey.str("Whole numbers").asWidget())
.child(new TextFieldWidget()
.fullWidth()
.numbersLong(-100_000_000_000_000L, 100_000_000_000_000L)
.usingScrollStep()));
}

private static Rectangle rndRect(IntList colors, Random random) {
int i = random.nextInt(colors.size());
int c = colors.removeInt(i);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/cleanroommc/modularui/utils/DAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,29 @@ public static double[] flatten(double[]... src) {

public interface UnaryDoubleOperator {

UnaryDoubleOperator IDENTITY = v -> v;

double apply(double v);
}

public interface BinaryDoubleOperator {

BinaryDoubleOperator IDENTITY = (v, op) -> v;

double apply(double v, double op);
}

public interface TernaryDoubleOperator {

TernaryDoubleOperator IDENTITY = (v, op1, op2) -> v;

double apply(double v, double op1, double op2);
}

public interface NDoubleOperator {

NDoubleOperator IDENTITY = (v, op) -> v;

double apply(double v, double[] op);
}
}
88 changes: 78 additions & 10 deletions src/main/java/com/cleanroommc/modularui/utils/MathUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.cleanroommc.modularui.utils;

import com.cleanroommc.modularui.utils.math.CustomDataAccessor;
import com.cleanroommc.modularui.utils.math.PostfixPercentOperator;

import com.cleanroommc.modularui.widgets.textfield.INumberParser;

import com.google.common.math.LongMath;

import net.minecraft.util.MathHelper;

import com.ezylang.evalex.BaseException;
Expand All @@ -11,6 +16,7 @@
import org.apache.commons.lang3.tuple.Pair;

import java.math.BigDecimal;
import java.util.function.LongSupplier;

public class MathUtils {

Expand All @@ -23,30 +29,50 @@ public class MathUtils {
.arraysAllowed(false)
.structuresAllowed(false)
.stripTrailingZeros(true)
.allowOverwriteConstants(true)
.dataAccessorSupplier(() -> new CustomDataAccessor(false))
.build()
.withAdditionalOperators(Pair.of("%", new PostfixPercentOperator()));

public static ParseResult parseExpression(String expression) {
return parseExpression(expression, Double.NaN, false);
}
public static final ExpressionConfiguration MATH_CFG_CASE_SENSITIVE = MATH_CFG.toBuilder()
.dataAccessorSupplier(() -> new CustomDataAccessor(true))
.build();

public static ParseResult parseExpression(String expression, boolean useSiPrefixes) {
return parseExpression(expression, Double.NaN, useSiPrefixes);
}
public static final INumberParser PARSER_WITH_SI = MathUtils::parseExpression;
public static final INumberParser PARSER_WHOLE_NUMBER = MathUtils::parseExpressionWholeNumber;

public static ParseResult parseExpression(String expression, double defaultValue) {
return parseExpression(expression, defaultValue, true);
return parseExpression(expression, defaultValue, true, false);
}

public static ParseResult parseExpression(String expression, double defaultValue, boolean useSiPrefixes) {
public static ParseResult parseExpression(String expression, double defaultValue, boolean useSiPrefixes, boolean biggerThanOne) {
if (expression == null || expression.isEmpty()) {
return ParseResult.success(EvaluationValue.numberValue(new BigDecimal(defaultValue)));
}

Expression e = new Expression(expression, MATH_CFG);
Expression e = new Expression(expression, MATH_CFG_CASE_SENSITIVE);
if (useSiPrefixes) {
SIPrefix.addAllToExpression(e);
SIPrefix.addAllToExpression(e, biggerThanOne);
}
try {
return ParseResult.success(e.evaluate());
} catch (BaseException exception) {
return ParseResult.failure(exception);
}
}

public static ParseResult parseExpressionWholeNumber(String expression, double defaultValue) {
if (expression == null || expression.isEmpty()) {
return ParseResult.success(EvaluationValue.numberValue(new BigDecimal(defaultValue)));
}

Expression e = new Expression(expression, MATH_CFG);
SIPrefix.Kilo.addToExpression(e);
SIPrefix.Mega.addToExpression(e);
SIPrefix.Giga.addToExpression(e, "b");
SIPrefix.Tera.addToExpression(e);
e.with("i", 144); // ingot
e.with("s", 64); // stack
try {
return ParseResult.success(e.evaluate());
} catch (BaseException exception) {
Expand Down Expand Up @@ -220,4 +246,46 @@ public static int intPlaces(double x) {
if (Math.pow(10, d - 1) > x) d--;
return Math.max(d, 1);
}

public static boolean areBothSmallerOrBiggerThanOne(double a, double b) {
return a > 1 ? b > 1 : b <= 1;
}

public static long percentOrSelf(double value, long maxValue) {
long rounded = Math.round(value);
if (value > 1 || Math.abs(value - rounded) < 0.0000001) return rounded;
return Math.round(value * maxValue);
}

public static int castToIntSaturated(long l) {
if (l >= Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (l <= Integer.MIN_VALUE) return Integer.MIN_VALUE;
return (int) l;
}

public static short castToShortSaturated(long l) {
if (l >= Short.MAX_VALUE) return Short.MAX_VALUE;
if (l <= Short.MIN_VALUE) return Short.MIN_VALUE;
return (short) l;
}

public static byte castToByteSaturated(long l) {
if (l >= Byte.MAX_VALUE) return Byte.MAX_VALUE;
if (l <= Byte.MIN_VALUE) return Byte.MIN_VALUE;
return (byte) l;
}

public interface UnaryLongOperator {

UnaryLongOperator IDENTITY = v -> v;

long apply(long l);
}

public interface UnaryIntOperator {

UnaryIntOperator IDENTITY = v -> v;

int apply(int l);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/cleanroommc/modularui/utils/SIPrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void addToExpression(Expression e) {
e.with(String.valueOf(this.symbol), this.factor);
}

public void addToExpression(Expression e, String alternativeSymbol) {
addToExpression(e);
e.with(alternativeSymbol, this.factor);
}

public static final SIPrefix[] VALUES = values();
public static final SIPrefix[] HIGH = new SIPrefix[values().length / 2];
public static final SIPrefix[] LOW = new SIPrefix[values().length / 2];
Expand All @@ -82,8 +87,9 @@ public void addToExpression(Expression e) {
}
}

public static void addAllToExpression(Expression e) {
public static void addAllToExpression(Expression e, boolean biggerThanOne) {
for (SIPrefix siPrefix : VALUES) {
if (siPrefix == One || siPrefix.infiniteLike || (biggerThanOne && siPrefix.factor < 1)) continue;
siPrefix.addToExpression(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.cleanroommc.modularui.utils.math;

import com.ezylang.evalex.data.DataAccessorIfc;
import com.ezylang.evalex.data.EvaluationValue;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

import java.util.Locale;
import java.util.Map;

public class CustomDataAccessor implements DataAccessorIfc {

private final Map<String, EvaluationValue> map = new Object2ObjectOpenHashMap<>();
private final boolean caseSensitive;

public CustomDataAccessor(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}

public boolean isCaseSensitive() {
return caseSensitive;
}

@Override
public EvaluationValue getData(String variable) {
if (this.caseSensitive) {
return this.map.get(variable);
}
return this.map.get(variable.toLowerCase(Locale.ROOT));
}

@Override
public void setData(String variable, EvaluationValue value) {
if (this.caseSensitive) {
this.map.put(variable, value);
} else {
this.map.put(variable, value);
String lower = variable.toLowerCase(Locale.ROOT);
if (!lower.equals(variable)) {
this.map.put(lower, value);
}
String upper = variable.toUpperCase(Locale.ROOT);
if (!upper.equals(variable)) {
this.map.put(upper, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,19 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
this.handler.moveCursorDown(Interactable.hasControlDown(), Interactable.hasShiftDown());
return Result.SUCCESS;
}
case Keyboard.KEY_HOME: {
this.handler.moveCursorStart(Interactable.hasControlDown(), Interactable.hasShiftDown());
return Result.SUCCESS;
}
case Keyboard.KEY_END: {
this.handler.moveCursorEnd(Interactable.hasControlDown(), Interactable.hasShiftDown());
return Result.SUCCESS;
}
case Keyboard.KEY_DELETE:
this.handler.delete(true);
this.handler.delete(true, Interactable.hasControlDown(), Interactable.hasShiftDown());
return Result.SUCCESS;
case Keyboard.KEY_BACK:
this.handler.delete();
this.handler.delete(Interactable.hasControlDown(), Interactable.hasShiftDown());
return Result.SUCCESS;
}

Expand All @@ -275,16 +283,14 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
GuiScreen.setClipboardString(this.handler.getSelectedText());
return Result.SUCCESS;
} else if (Interactable.isKeyComboCtrlV(keyCode)) {
if (this.handler.hasTextMarked()) {
this.handler.delete();
}
this.handler.deleteMarked();
// paste copied text in marked text
this.handler.insert(GuiScreen.getClipboardString().replace("§", ""), canScrollHorizontally());
return Result.SUCCESS;
} else if (Interactable.isKeyComboCtrlX(keyCode) && this.handler.hasTextMarked()) {
// copy and delete copied text
GuiScreen.setClipboardString(this.handler.getSelectedText());
this.handler.delete();
this.handler.deleteMarked();
return Result.SUCCESS;
} else if (Interactable.isKeyComboCtrlA(keyCode)) {
// mark whole text
Expand All @@ -295,9 +301,7 @@ public void onMouseDrag(int mouseButton, long timeSinceClick) {
if (ModularUI.Mods.LWJGL3IFY.isLoaded()) {
return Result.SUCCESS;
}
if (this.handler.hasTextMarked()) {
this.handler.delete();
}
this.handler.deleteMarked();
// insert typed char
this.handler.insert(String.valueOf(character), canScrollHorizontally());
return Result.SUCCESS;
Expand All @@ -320,9 +324,7 @@ public boolean onTextInput(InputEvents.TextEvent event) {
}
String t = event.text.replaceAll("§", "");
if (!t.isEmpty() && handler.test(t)) {
if (this.handler.hasTextMarked()) {
this.handler.delete();
}
this.handler.deleteMarked();
// insert typed char
this.handler.insert(t, canScrollHorizontally());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cleanroommc.modularui.widgets.textfield;

import com.cleanroommc.modularui.utils.ParseResult;

public interface INumberParser {

ParseResult parse(String expr, double defaultValue);
}
Loading