diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-03-13 18:58:00 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-03-13 18:58:00 +0300 |
commit | 107c94c338a7b58acd9627fe85ec5d5bbdf36803 (patch) | |
tree | 4c5e81768478e11ecbb925390ae1e596ab03fd2f /source/blender/editors/interface/interface.c | |
parent | 17e0565cc5748f959ec318c472b28ff9e41ed507 (diff) |
UI: New option to display factors as percentages
The option can be changed in the Preferences
in `Interface -> Editors -> Factor Display Type`.
Reviewers: campbellbarton, brecht, billreynish
Differential Revision: https://developer.blender.org/D4462
Diffstat (limited to 'source/blender/editors/interface/interface.c')
-rw-r--r-- | source/blender/editors/interface/interface.c | 121 |
1 files changed, 91 insertions, 30 deletions
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 840c68ec747..f783c4a6906 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -2425,12 +2425,27 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int value = ui_but_value_get(but); + PropertySubType subtype = PROP_NONE; + if (but->rnaprop) { + subtype = RNA_property_subtype(but->rnaprop); + } + if (ui_but_is_float(but)) { + int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision; + if (ui_but_is_unit(but)) { - ui_get_but_string_unit(but, str, maxlen, value, false, float_precision); + ui_get_but_string_unit(but, str, maxlen, value, false, prec); + } + else if (subtype == PROP_FACTOR) { + if (U.factor_display_type == USER_FACTOR_AS_FACTOR) { + BLI_snprintf(str, maxlen, "%.*f", prec, value); + } + else { + BLI_snprintf(str, maxlen, "%.*f", MAX2(0, prec - 2), value * 100); + } + } else { - int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision; if (use_exp_float) { const int int_digits_num = integer_digits_f(value); if (int_digits_num < -6 || int_digits_num > 12) { @@ -2528,44 +2543,80 @@ static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char return user_string_to_number(C, str, unit, type, r_value); } -bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *r_value) +static bool ui_number_from_string(bContext *C, const char *str, double *r_value) { - bool ok = false; - #ifdef WITH_PYTHON + return BPY_execute_string_as_number(C, NULL, str, true, r_value); +#else + *r_value = atof(str); + return true; +#endif +} - if (str[0] != '\0') { - bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but)); - /* only enable verbose if we won't run again with units */ - if (BPY_execute_string_as_number(C, NULL, str, is_unit_but == false, r_value)) { - /* if the value parsed ok without unit conversion - * this button may still need a unit multiplier */ - if (is_unit_but) { - char str_new[128]; - - BLI_snprintf(str_new, sizeof(str_new), "%f", *r_value); - ok = ui_set_but_string_eval_num_unit(C, but, str_new, r_value); - } - else { - ok = true; /* parse normal string via py (no unit conversion needed) */ - } +static bool ui_number_from_string_factor(bContext *C, const char *str, double *r_value) +{ + int len = strlen(str); + if (BLI_strn_endswith(str, "%", len)) { + char *str_new = BLI_strdupn(str, len - 1); + bool success = ui_number_from_string(C, str_new, r_value); + MEM_freeN(str_new); + *r_value /= 100.0; + return success; + } + else { + if (!ui_number_from_string(C, str, r_value)) { + return false; } - else if (is_unit_but) { - /* parse failed, this is a unit but so run replacements and parse again */ - ok = ui_set_but_string_eval_num_unit(C, but, str, r_value); + if (U.factor_display_type == USER_FACTOR_AS_PERCENTAGE) { + *r_value /= 100.0; } + return true; } +} -#else /* WITH_PYTHON */ - - *r_value = atof(str); - ok = true; +static bool ui_number_from_string_percentage(bContext *C, const char *str, double *r_value) +{ + int len = strlen(str); + if (BLI_strn_endswith(str, "%", len)) { + char *str_new = BLI_strdupn(str, len - 1); + bool success = ui_number_from_string(C, str_new, r_value); + MEM_freeN(str_new); + return success; + } + else { + return ui_number_from_string(C, str, r_value); + } +} - UNUSED_VARS(C, but); +bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *r_value) +{ + if (str[0] == '\0') { + *r_value = 0.0; + return true; + } -#endif /* WITH_PYTHON */ + PropertySubType subtype = PROP_NONE; + if (but->rnaprop) { + subtype = RNA_property_subtype(but->rnaprop); + } - return ok; + if (ui_but_is_float(but)) { + if (ui_but_is_unit(but)) { + return ui_set_but_string_eval_num_unit(C, but, str, r_value); + } + else if (subtype == PROP_FACTOR) { + return ui_number_from_string_factor(C, str, r_value); + } + else if (subtype == PROP_PERCENTAGE) { + return ui_number_from_string_percentage(C, str, r_value); + } + else { + return ui_number_from_string(C, str, r_value); + } + } + else { + return ui_number_from_string(C, str, r_value); + } } /* just the assignment/free part */ @@ -3094,6 +3145,16 @@ static void ui_but_build_drawstr_float(uiBut *but, double value) int prec = ui_but_calc_float_precision(but, value); STR_CONCATF(but->drawstr, slen, "%.*f px", prec, value); } + else if (subtype == PROP_FACTOR) { + int precision = ui_but_calc_float_precision(but, value); + + if (U.factor_display_type == USER_FACTOR_AS_FACTOR) { + STR_CONCATF(but->drawstr, slen, "%.*f", precision, value); + } + else { + STR_CONCATF(but->drawstr, slen, "%.*f %%", MAX2(0, precision - 2), value * 100); + } + } 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); |