Skip to content
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

Trailing Zeros Eliminated in Labels #2319

Open
bjalder26 opened this issue Sep 22, 2024 · 1 comment
Open

Trailing Zeros Eliminated in Labels #2319

bjalder26 opened this issue Sep 22, 2024 · 1 comment

Comments

@bjalder26
Copy link
Collaborator

I notice that issue #399 is either back, or was never really resolved.

The issue is that when entering a number in a label trailing zeros won't be kept. So, if you type "12.0" in a label, then it will almost immediately revert to "12". "12.140" would revert to "12.14". The biggest time this would be a problem is when someone was trying to enter a number like "12.05", because they would have a hard time getting past "12", and would either need to type quickly, or enter "12.5", then change it to "12.05".

This bolded line appears to be the line that converts the entered string to a number, which will then be kept with the fewest needed digits:
else if(typeof text == 'string' && text.match(/^[-+]?[0-9]+(.[0-9]+)?$/))
await this.set('text', +text);

This is part of the "setText()" function in widget.js shown below.

We are working under the assumption that if someone enters or sets a number to a label, then they want it stored as a number. I think that is probably the right assumption to make, and I would guess that changing that would be game-breaking. I believe the only way to maintain the number of desired decimal places would be to store the "number" as a string.

One option would be to give widgits a property (e.g. setAsString or something better), which defaults to false, and when set to true, the entered string is never converted to a number. In games they can always be converted back to a number for calculations.

I suspect it would just involve updating the preceding line...
else if(typeof text == 'string' && text.match(/^[-+]?[0-9]+(.[0-9]+)?$/) && this.get(setAsString) == false)

... and adding a default property to widgets, and maybe updating the editor.

I think the relevant questions are if there is a better way to deal with this, and if this is worth adding another property to address.

(

async setText(text, mode, problems) {
if (this.get('text') !== undefined) {
if(mode == 'inc' || mode == 'dec') {
let newText = (parseFloat(this.get('text')) || 0) + (mode == 'dec' ? -1 : 1) * text;
const decimalPlacesOld = String(this.get('text')).match(/\..*$/);
const decimalPlacesChange = (+text).toString().match(/\..*$/);
const decimalPlaces = Math.max(decimalPlacesOld ? decimalPlacesOld[0].length-1 : 0, decimalPlacesChange ? decimalPlacesChange[0].length-1 : 0);
const factor = 10**decimalPlaces;
newText = Math.round(newText*factor)/factor;
await this.set('text', newText);
} else if(mode == 'append')
await this.set('text', this.get('text') + text);
else if(Array.isArray(text))
await this.set('text', text.join(', '));
else if(typeof text == 'string' && text.match(/^[-+]?[0-9]+(\.[0-9]+)?$/))
await this.set('text', +text);
else
await this.set('text', text);
} else
problems.push(`Tried setting text property which doesn't exist for ${this.id}.`);
}
)

I came up with a workaround, but it is very convoluted. It requires a lot of steps, arrays, adding and removing spaces, and it limits how fast you can type in a label.

@ArnoldSmith86
Copy link
Owner

If the problem is that entering 12.05 doesn't work, the solution should probably be that the widget shouldn't update the value in the HTML if it is currently focused and if +receivedValue == +enteredValue. Then in a blur event handler (when losing focus), it could set it to the current value like it currently does.

That would still mean that trailing zeroes would disappear when done, though..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants