ArnoldSmith86 / virtualtabletop

a virtual surface in the browser on which you can play board, dice and card games
https://virtualtabletop.io
GNU General Public License v3.0
164 stars 29 forks source link

Trailing Zeros Eliminated in Labels #2319

Open bjalder26 opened 12 hours ago

bjalder26 commented 12 hours ago

I notice that issue https://github.com/ArnoldSmith86/virtualtabletop/issues/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.

(https://github.com/ArnoldSmith86/virtualtabletop/blob/045d0d166c0eb207a0097bd8a3abba0287a8255e/client/js/widgets/widget.js#L2502-L2522)

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 commented 9 hours ago

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..