diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-24 00:42:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 00:42:47 +0200 |
commit | 08d8f884cd669f06d65fc7ed2c58ca56d71d6145 (patch) | |
tree | e4d6db3d7353a7e19f8d81abfe2f0c1ce9b1019f /core/string | |
parent | 34aa6b06a7002469ee54541067ce3c39bf42815a (diff) | |
parent | dbc165715bb0bdcac73e22cf81c51529e48be07f (diff) |
Merge pull request #63728 from MarcusElg/%v
Add %v for formatting vectors
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 75 |
1 files changed, 72 insertions, 3 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index c02be9e5b7..0c43ba9ccc 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4667,6 +4667,71 @@ String String::sprintf(const Array &values, bool *error) const { in_format = false; break; } + case 'v': { // Vector2/3/4/2i/3i/4i + if (value_index >= values.size()) { + return "not enough arguments for format string"; + } + + int count; + switch (values[value_index].get_type()) { + case Variant::VECTOR2: + case Variant::VECTOR2I: { + count = 2; + } break; + case Variant::VECTOR3: + case Variant::VECTOR3I: { + count = 3; + } break; + case Variant::VECTOR4: + case Variant::VECTOR4I: { + count = 4; + } break; + default: { + return "%v requires a vector type (Vector2/3/4/2i/3i/4i)"; + } + } + + Vector4 vec = values[value_index]; + String str = "("; + for (int i = 0; i < count; i++) { + double val = vec[i]; + // Pad decimals out. + String number_str = String::num(ABS(val), min_decimals).pad_decimals(min_decimals); + + int initial_len = number_str.length(); + + // Padding. Leave room for sign later if required. + int pad_chars_count = val < 0 ? min_chars - 1 : min_chars; + String pad_char = pad_with_zeros ? String("0") : String(" "); + if (left_justified) { + number_str = number_str.rpad(pad_chars_count, pad_char); + } else { + number_str = number_str.lpad(pad_chars_count, pad_char); + } + + // Add sign if needed. + if (val < 0) { + if (left_justified) { + number_str = number_str.insert(0, "-"); + } else { + number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, "-"); + } + } + + // Add number to combined string + str += number_str; + + if (i < count - 1) { + str += ", "; + } + } + str += ")"; + + formatted += str; + ++value_index; + in_format = false; + break; + } case 's': { // String if (value_index >= values.size()) { return "not enough arguments for format string"; @@ -4759,7 +4824,7 @@ String String::sprintf(const Array &values, bool *error) const { } break; } - case '.': { // Float separator. + case '.': { // Float/Vector separator. if (in_decimals) { return "too many decimal points in format"; } @@ -4773,8 +4838,12 @@ String String::sprintf(const Array &values, bool *error) const { return "not enough arguments for format string"; } - if (!values[value_index].is_num()) { - return "* wants number"; + Variant::Type value_type = values[value_index].get_type(); + if (!values[value_index].is_num() && + value_type != Variant::VECTOR2 && value_type != Variant::VECTOR2I && + value_type != Variant::VECTOR3 && value_type != Variant::VECTOR3I && + value_type != Variant::VECTOR4 && value_type != Variant::VECTOR4I) { + return "* wants number or vector"; } int size = values[value_index]; |