-
Notifications
You must be signed in to change notification settings - Fork 21
Improve number field #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Improve number field #101
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ public class TextFieldWidget extends BaseTextFieldWidget<TextFieldWidget> { | |
|
|
||
| private IStringValue<?> stringValue; | ||
| private Function<String, String> validator = val -> val; | ||
| private boolean numbers = false; | ||
| private boolean isNumber = false; | ||
| private String mathFailMessage = null; | ||
| private double defaultNumber = 0; | ||
| private boolean tooltipOverride = false; | ||
|
|
@@ -52,6 +52,7 @@ public double parse(String num) { | |
| return 0.0; | ||
| } | ||
| } | ||
| num = num.replaceAll("\\D+", ""); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whats this for?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for this : Improve QOL when a field is numric, remove all non numeric chars onRemoveFocus instead set 0.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not desired behaviour. It will break expressions for example. |
||
| ParseResult result = MathUtils.parseExpression(num, this.defaultNumber, true); | ||
| if (result.isFailure()) { | ||
| this.mathFailMessage = result.getErrorMessage(); | ||
|
|
@@ -144,15 +145,15 @@ public void onRemoveFocus(ModularGuiContext context) { | |
| } else { | ||
| throw new IllegalStateException("TextFieldWidget can only have one line!"); | ||
| } | ||
| this.stringValue.setStringValue(this.numbers ? format.parse(getText(), new ParsePosition(0)).toString() : getText()); | ||
| this.stringValue.setStringValue(this.isNumber ? format.parse(getText(), new ParsePosition(0)).toString() : getText()); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onTextChanged() { | ||
| super.onTextChanged(); | ||
| if (this.autoUpdateOnChange) { | ||
| String text = this.validator.apply(getText()); | ||
| this.stringValue.setStringValue(this.numbers ? format.parse(text, new ParsePosition(0)).toString() : getText()); | ||
| this.stringValue.setStringValue(this.isNumber ? format.parse(text, new ParsePosition(0)).toString() : getText()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -210,7 +211,7 @@ public TextFieldWidget setValidator(Function<String, String> validator) { | |
| } | ||
|
|
||
| public TextFieldWidget setNumbersLong(Function<Long, Long> validator) { | ||
| this.numbers = true; | ||
| this.isNumber = true; | ||
| setValidator(val -> { | ||
| long num; | ||
| if (val.isEmpty()) { | ||
|
|
@@ -224,7 +225,7 @@ public TextFieldWidget setNumbersLong(Function<Long, Long> validator) { | |
| } | ||
|
|
||
| public TextFieldWidget setNumbers(Function<Integer, Integer> validator) { | ||
| this.numbers = true; | ||
| this.isNumber = true; | ||
| return setValidator(val -> { | ||
| int num; | ||
| if (val.isEmpty()) { | ||
|
|
@@ -237,7 +238,7 @@ public TextFieldWidget setNumbers(Function<Integer, Integer> validator) { | |
| } | ||
|
|
||
| public TextFieldWidget setNumbersDouble(Function<Double, Double> validator) { | ||
| this.numbers = true; | ||
| this.isNumber = true; | ||
| return setValidator(val -> { | ||
| double num; | ||
| if (val.isEmpty()) { | ||
|
|
@@ -271,6 +272,9 @@ public TextFieldWidget setDefaultNumber(double defaultNumber) { | |
| } | ||
|
|
||
| public TextFieldWidget setFormatAsInteger(boolean formatAsInteger) { | ||
| if (formatAsInteger && !this.isNumber) { | ||
| setNumbers(Integer.MIN_VALUE, Integer.MAX_VALUE); | ||
| } | ||
| this.renderer.setFormatAsInteger(formatAsInteger); | ||
| return getThis(); | ||
| } | ||
|
|
@@ -287,7 +291,7 @@ public TextFieldWidget value(IStringValue<?> stringValue) { | |
| @Override | ||
| public boolean onMouseScroll(UpOrDown scrollDirection, int amount) { | ||
| // default to basic behavior if scroll step isn't on, if the widget is not using numbers, and if it is focused | ||
| if (!this.usingScrollStep || !this.numbers || !isFocused()) return super.onMouseScroll(scrollDirection, amount); | ||
| if (!this.usingScrollStep || !this.isNumber || !isFocused()) return super.onMouseScroll(scrollDirection, amount); | ||
|
|
||
| double value; | ||
| if (Interactable.hasControlDown()) value = scrollDirection.modifier * scrollStepCtrl; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why rename?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the name for a boolean is not explicit in my opinion. But i rollback if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, roll this back. It just makes backporting commits harder.