Skip to content

Commit

Permalink
Merge pull request #80699 from aXu-AP/spin-box-comma-decimals
Browse files Browse the repository at this point in the history
Allow comma as a decimal separator for SpinBox
  • Loading branch information
YuriSizov committed Sep 28, 2023
2 parents 813cd1d + 4d3dc0e commit 4f0e2ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
21 changes: 14 additions & 7 deletions editor/gui/editor_spin_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,24 @@ String EditorSpinSlider::get_suffix() const {
}

void EditorSpinSlider::_evaluate_input_text() {
// Replace comma with dot to support it as decimal separator (GH-6028).
// This prevents using functions like `pow()`, but using functions
// in EditorSpinSlider is a barely known (and barely used) feature.
// Instead, we'd rather support German/French keyboard layouts out of the box.
const String text = TS->parse_number(value_input->get_text().replace(",", "."));

Ref<Expression> expr;
expr.instantiate();

// Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
String text = value_input->get_text().replace(",", ".");
text = text.replace(";", ",");
text = TS->parse_number(text);

Error err = expr->parse(text);
if (err != OK) {
return;
// If the expression failed try without converting commas to dots - they might have been for parameter separation.
text = value_input->get_text();
text = TS->parse_number(text);

err = expr->parse(text);
if (err != OK) {
return;
}
}

Variant v = expr->execute(Array(), nullptr, false, true);
Expand Down
20 changes: 16 additions & 4 deletions scene/gui/spin_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,24 @@ void SpinBox::_text_submitted(const String &p_string) {
Ref<Expression> expr;
expr.instantiate();

String num = TS->parse_number(p_string);
// Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
String text = p_string.replace(",", ".");
text = text.replace(";", ",");
text = TS->parse_number(text);
// Ignore the prefix and suffix in the expression.
Error err = expr->parse(num.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);

Error err = expr->parse(text);
if (err != OK) {
_update_text();
return;
// If the expression failed try without converting commas to dots - they might have been for parameter separation.
text = p_string;
text = TS->parse_number(text);
text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);

err = expr->parse(text);
if (err != OK) {
return;
}
}

Variant value = expr->execute(Array(), nullptr, false, true);
Expand Down

0 comments on commit 4f0e2ea

Please sign in to comment.