diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-03-07 14:01:32 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-03-07 14:02:27 +0300 |
commit | d617466d87863d75a83dd01e68da7228907f4656 (patch) | |
tree | 969ce7ef4618ad0c2ba0cc109ed9c1384d51b745 /source/blender | |
parent | e25cdac24bc48cbb0f05f42b52e42adaaae63499 (diff) |
Refactor building the draw string for floats and ints
Differential Revision: https://developer.blender.org/D4466
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 4 | ||||
-rw-r--r-- | source/blender/editors/interface/interface.c | 110 |
2 files changed, 68 insertions, 46 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 91eefc0f5b1..831626b164b 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -111,6 +111,10 @@ int BLI_string_find_split_words( BLI_snprintf(dst, ARRAY_SIZE(dst), format, __VA_ARGS__) #define SNPRINTF_RLEN(dst, format, ...) \ BLI_snprintf_rlen(dst, ARRAY_SIZE(dst), format, __VA_ARGS__) +#define STR_CONCAT(dst, len, suffix) \ + len += BLI_strncpy_rlen(dst + len, suffix, ARRAY_SIZE(dst) - len) +#define STR_CONCATF(dst, len, format, ...) \ + len += BLI_snprintf_rlen(dst + len, ARRAY_SIZE(dst) - len, format, __VA_ARGS__) /** \} */ #ifdef __cplusplus diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 8051ea460fd..ffb13e9facf 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -3056,6 +3056,61 @@ void UI_block_theme_style_set(uiBlock *block, char theme_style) block->theme_style = theme_style; } +static void ui_but_build_drawstr_float(uiBut *but, double value) +{ + size_t slen = 0; + STR_CONCAT(but->drawstr, slen, but->str); + + PropertySubType subtype = PROP_NONE; + if (but->rnaprop) { + subtype = RNA_property_subtype(but->rnaprop); + } + + if (value == (double)FLT_MAX) { + STR_CONCAT(but->drawstr, slen, "inf"); + } + else if (value == (double)-FLT_MIN) { + STR_CONCAT(but->drawstr, slen, "-inf"); + } + else if (subtype == PROP_PERCENTAGE) { + int prec = ui_but_calc_float_precision(but, value); + STR_CONCATF(but->drawstr, slen, "%.*f %%", prec, value); + } + else if (subtype == PROP_PIXEL) { + int prec = ui_but_calc_float_precision(but, value); + STR_CONCATF(but->drawstr, slen, "%.*f px", prec, value); + } + else if (ui_but_is_unit(but)) { + char new_str[sizeof(but->drawstr)]; + ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1); + STR_CONCAT(but->drawstr, slen, new_str); + } + else { + int prec = ui_but_calc_float_precision(but, value); + STR_CONCATF(but->drawstr, slen, "%.*f", prec, value); + } +} + +static void ui_but_build_drawstr_int(uiBut *but, int value) +{ + size_t slen = 0; + STR_CONCAT(but->drawstr, slen, but->str); + + PropertySubType subtype = PROP_NONE; + if (but->rnaprop) { + subtype = RNA_property_subtype(but->rnaprop); + } + + STR_CONCATF(but->drawstr, slen, "%d", value); + + if (subtype == PROP_PERCENTAGE) { + STR_CONCAT(but->drawstr, slen, "%"); + } + else if (subtype == PROP_PIXEL) { + STR_CONCAT(but->drawstr, slen, " px"); + } +} + /** * \param but: Button to update. * \param validate: When set, this function may change the button value. @@ -3145,52 +3200,15 @@ void ui_but_update_ex(uiBut *but, const bool validate) case UI_BTYPE_NUM: case UI_BTYPE_NUM_SLIDER: - - if (!but->editstr) { - const char *drawstr_suffix = NULL; - size_t slen; - - UI_GET_BUT_VALUE_INIT(but, value); - - slen = BLI_strncpy_rlen(but->drawstr, but->str, sizeof(but->drawstr)); - - if (ui_but_is_float(but)) { - if (value == (double) FLT_MAX) { - slen += BLI_strncpy_rlen(but->drawstr + slen, "inf", sizeof(but->drawstr) - slen); - } - else if (value == (double) -FLT_MAX) { - slen += BLI_strncpy_rlen(but->drawstr + slen, "-inf", sizeof(but->drawstr) - slen); - } - /* support length type buttons */ - else if (ui_but_is_unit(but)) { - char new_str[sizeof(but->drawstr)]; - ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1); - slen += BLI_strncpy_rlen(but->drawstr + slen, new_str, sizeof(but->drawstr) - slen); - } - else { - const int prec = ui_but_calc_float_precision(but, value); - slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%.*f", prec, value); - } - } - else { - slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%d", (int)value); - } - - if (but->rnaprop) { - PropertySubType pstype = RNA_property_subtype(but->rnaprop); - - if (pstype == PROP_PERCENTAGE) { - drawstr_suffix = "%"; - } - else if (pstype == PROP_PIXEL) { - drawstr_suffix = " px"; - } - } - - if (drawstr_suffix) { - BLI_strncpy(but->drawstr + slen, drawstr_suffix, sizeof(but->drawstr) - slen); - } - + if (but->editstr) { + break; + } + UI_GET_BUT_VALUE_INIT(but, value); + if (ui_but_is_float(but)) { + ui_but_build_drawstr_float(but, value); + } + else { + ui_but_build_drawstr_int(but, (int)value); } break; |