diff options
-rw-r--r-- | source/blender/blenlib/BLI_string.h | 5 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string.c | 28 | ||||
-rw-r--r-- | source/blender/editors/interface/interface.c | 36 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_handlers.c | 10 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_intern.h | 3 |
5 files changed, 70 insertions, 12 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index 70c89773f02..603831246ac 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -226,6 +226,11 @@ void BLI_ascii_strtoupper(char *str, const size_t len) __attribute__((nonnull)) #endif ; +int BLI_str_rstrip_float_zero(char *str, const char pad) +#ifdef __GNUC__ +__attribute__((nonnull)) +#endif +; #ifdef __cplusplus } diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 3500f3f1805..08d4f8e1198 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -454,3 +454,31 @@ void BLI_ascii_strtoupper(char *str, const size_t len) if (str[i] >= 'a' && str[i] <= 'z') str[i] -= 'a' - 'A'; } + +/** + * Strip trailing zeros from a float, eg: + * 0.0000 -> 0.0 + * 2.0010 -> 2.001 + * + * \param str + * \param len + * \return The number of zeto's stripped. + */ +int BLI_str_rstrip_float_zero(char *str, const char pad) +{ + char *p = strchr(str, '.'); + int totstrip = 0; + if (p) { + char *end_p; + p++; /* position at first decimal place */ + end_p = p + (strlen(p) - 1); /* position at last character */ + if (end_p > p) { + while (end_p != p && *end_p == '0') { + *end_p = pad; + end_p--; + } + } + } + + return totstrip; +} diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 2c17a629ed4..42d704f318c 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -1674,18 +1674,28 @@ void ui_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen) } } -static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad) +/** + * \param float_precision Override the button precision. + */ +static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, int pad, int float_precision) { UnitSettings *unit = but->block->unit; int do_split = unit->flag & USER_UNIT_OPT_SPLIT; int unit_type = uiButGetUnitType(but); - int precision = but->a2; + int precision; if (unit->scale_length < 0.0001f) unit->scale_length = 1.0f; // XXX do_versions - /* Sanity checks */ - if (precision > PRECISION_FLOAT_MAX) precision = PRECISION_FLOAT_MAX; - else if (precision == 0) precision = 2; + /* Use precision override? */ + if (float_precision == -1) { + /* Sanity checks */ + precision = (int)but->a2; + if (precision > PRECISION_FLOAT_MAX) precision = PRECISION_FLOAT_MAX; + else if (precision == 0) precision = 2; + } + else { + precision = float_precision; + } bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad); @@ -1706,8 +1716,10 @@ static float ui_get_but_step_unit(uiBut *but, float step_default) } } - -void ui_get_but_string(uiBut *but, char *str, size_t maxlen) +/** + * \param float_precision For number buttons the precission to use or -1 to fallback to the button default. + */ +void ui_get_but_string_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision) { if (but->rnaprop && ELEM4(but->type, TEX, IDPOIN, SEARCH_MENU, SEARCH_MENU_UNLINK)) { PropertyType type; @@ -1779,10 +1791,10 @@ void ui_get_but_string(uiBut *but, char *str, size_t maxlen) if (ui_is_but_float(but)) { if (ui_is_but_unit(but)) { - ui_get_but_string_unit(but, str, maxlen, value, 0); + ui_get_but_string_unit(but, str, maxlen, value, 0, float_precision); } else { - const int prec = ui_but_float_precision(but, value); + const int prec = (float_precision == -1) ? ui_but_float_precision(but, value) : float_precision; BLI_snprintf(str, maxlen, "%.*f", prec, value); } } @@ -1790,6 +1802,10 @@ void ui_get_but_string(uiBut *but, char *str, size_t maxlen) BLI_snprintf(str, maxlen, "%d", (int)value); } } +void ui_get_but_string(uiBut *but, char *str, const size_t maxlen) +{ + ui_get_but_string_ex(but, str, maxlen, -1); +} #ifdef WITH_PYTHON @@ -2345,7 +2361,7 @@ void ui_check_but(uiBut *but) /* support length type buttons */ else if (ui_is_but_unit(but)) { char new_str[sizeof(but->drawstr)]; - ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE); + ui_get_but_string_unit(but, new_str, sizeof(new_str), value, TRUE, -1); BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, new_str); } else { diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index e0d6c293be5..fe0ba210a2f 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -1402,7 +1402,11 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, /* pass */ } else if (mode == 'c') { - ui_get_but_string(but, buf, sizeof(buf)); + /* Get many decimal places, then strip trailing zeros. + * note: too high values start to give strange results (6 or so is ok) */ + ui_get_but_string_ex(but, buf, sizeof(buf), 6); + BLI_str_rstrip_float_zero(buf, '\0'); + WM_clipboard_text_set(buf, 0); } else { @@ -1984,6 +1988,10 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data) data->str = MEM_callocN(sizeof(char) * data->maxlen + 1, "textedit str"); ui_get_but_string(but, data->str, data->maxlen); + if (ui_is_but_float(but) && !ui_is_but_unit(but)) { + BLI_str_rstrip_float_zero(data->str, '\0'); + } + if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { ui_convert_to_unit_alt_name(but, data->str, data->maxlen); } diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h index 9093e8f8d30..63a93315a97 100644 --- a/source/blender/editors/interface/interface_intern.h +++ b/source/blender/editors/interface/interface_intern.h @@ -394,7 +394,8 @@ extern void ui_set_but_vectorf(uiBut *but, const float vec[3]); extern void ui_hsvcircle_vals_from_pos(float *val_rad, float *val_dist, const rcti *rect, const float mx, const float my); -extern void ui_get_but_string(uiBut *but, char *str, size_t maxlen); +extern void ui_get_but_string_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision); +extern void ui_get_but_string(uiBut *but, char *str, const size_t maxlen); extern void ui_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen); extern int ui_set_but_string(struct bContext *C, uiBut *but, const char *str); extern int ui_get_but_string_max_length(uiBut *but); |