diff options
author | Alexander Gavrilov <angavrilov@gmail.com> | 2021-12-26 18:09:19 +0300 |
---|---|---|
committer | Alexander Gavrilov <angavrilov@gmail.com> | 2022-01-18 16:24:54 +0300 |
commit | 0d7b3ed39c77e1cdd43815a65993afe179691672 (patch) | |
tree | 6440d7ec2e07800185898e0c9703e8f0901d3ce9 /source/blender/editors | |
parent | 695222838699921865a1f5685c420c9155f27ca7 (diff) |
RNA: display exact integer values without fraction if step is integer.
Display exact integer values of a floating point fields without
a fraction if the step is also an exact integer. This is intended
for cases when the value can technically be fractional, but most
commonly is supposed to be integer.
This handling is not applied if the field has any unit except frames,
because integer values aren't special for quantities like length.
The fraction is discarded in the normal display mode and when copying
the value to clipboard, but not when editing to remind the user that
the field allows fractions.
Differential Revision: https://developer.blender.org/D13753
Diffstat (limited to 'source/blender/editors')
-rw-r--r-- | source/blender/editors/interface/interface.c | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index a275a59a4e7..3c701cc403b 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -661,8 +661,43 @@ static float ui_but_get_float_precision(uiBut *but) return but->a2; } +static float ui_but_get_float_step_size(uiBut *but) +{ + if (but->type == UI_BTYPE_NUM) { + return ((uiButNumber *)but)->step_size; + } + + return but->a1; +} + +static bool ui_but_hide_fraction(uiBut *but, double value) +{ + /* Hide the fraction if both the value and the step are exact integers. */ + if (floor(value) == value) { + const float step = ui_but_get_float_step_size(but) * UI_PRECISION_FLOAT_SCALE; + + if (floorf(step) == step) { + /* Don't hide if it has any unit except frame count. */ + switch (UI_but_unit_type_get(but)) { + case PROP_UNIT_NONE: + case PROP_UNIT_TIME: + return true; + + default: + return false; + } + } + } + + return false; +} + static int ui_but_calc_float_precision(uiBut *but, double value) { + if (ui_but_hide_fraction(but, value)) { + return 0; + } + int prec = (int)ui_but_get_float_precision(but); /* first check for various special cases: @@ -2813,8 +2848,14 @@ void ui_but_string_get_ex(uiBut *but, } if (ui_but_is_float(but)) { - int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : - float_precision; + int prec = float_precision; + + if (float_precision == -1) { + prec = ui_but_calc_float_precision(but, value); + } + else if (!use_exp_float && ui_but_hide_fraction(but, value)) { + prec = 0; + } if (ui_but_is_unit(but)) { ui_get_but_string_unit(but, str, maxlen, value, false, prec); |