1
0
Fork 0

Codechange: Add GetParamMaxDigits/Value() functions.

Similar to SetDParamMaxDigits/Value(), except the value is returned instead of being set into a parameter.
pull/13636/head
Peter Nelson 2025-02-21 23:38:33 +00:00 committed by Peter Nelson
parent 4c99b5b368
commit 04a16a6b68
2 changed files with 43 additions and 16 deletions

View File

@ -176,21 +176,19 @@ uint64_t GetDParam(size_t n)
}
/**
* Set DParam n to some number that is suitable for string size computations.
* @param n Index of the string parameter.
* @param max_value The biggest value which shall be displayed.
* For the result only the number of digits of \a max_value matter.
* @param min_count Minimum number of digits independent of \a max.
* Get some number that is suitable for string size computations.
* @param count Number of digits which shall be displayable.
* @param size Font of the number
* @returns Number to use for string size computations.
*/
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize size)
uint64_t GetParamMaxDigits(uint count, FontSize size)
{
uint num_digits = 1;
while (max_value >= 10) {
num_digits++;
max_value /= 10;
auto [front, next] = GetBroadestDigit(size);
uint64_t val = count > 1 ? front : next;
for (; count > 1; count--) {
val = 10 * val + next;
}
SetDParamMaxDigits(n, std::max(min_count, num_digits), size);
return val;
}
/**
@ -201,12 +199,38 @@ void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize si
*/
void SetDParamMaxDigits(size_t n, uint count, FontSize size)
{
auto [front, next] = GetBroadestDigit(size);
uint64_t val = count > 1 ? front : next;
for (; count > 1; count--) {
val = 10 * val + next;
SetDParam(n, GetParamMaxDigits(count, size));
}
/**
* Get some number that is suitable for string size computations.
* @param max_value The biggest value which shall be displayed.
* For the result only the number of digits of \a max_value matter.
* @param min_count Minimum number of digits independent of \a max.
* @param size Font of the number
* @returns Number to use for string size computations.
*/
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
{
uint num_digits = 1;
while (max_value >= 10) {
num_digits++;
max_value /= 10;
}
SetDParam(n, val);
return GetParamMaxDigits(std::max(min_count, num_digits), size);
}
/**
* Set DParam n to some number that is suitable for string size computations.
* @param n Index of the string parameter.
* @param max_value The biggest value which shall be displayed.
* For the result only the number of digits of \a max_value matter.
* @param min_count Minimum number of digits independent of \a max.
* @param size Font of the number
*/
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize size)
{
SetDParam(n, GetParamMaxValue(max_value, min_count, size));
}
/**

View File

@ -80,6 +80,9 @@ inline int64_t PackVelocity(uint speed, VehicleType type)
return speed | (static_cast<uint64_t>(type) << 56);
}
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
uint64_t GetParamMaxDigits(uint count, FontSize size = FS_NORMAL);
void SetDParam(size_t n, uint64_t v);
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count = 0, FontSize size = FS_NORMAL);
void SetDParamMaxDigits(size_t n, uint count, FontSize size = FS_NORMAL);