diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-03-04 08:21:51 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-03-04 08:21:51 +0400 |
commit | 24da4a6032543e3bdae5960e8b956caa25b38aae (patch) | |
tree | 413fa05306ca964d15c54e39b55f619a858e7696 /source/blender/editors | |
parent | c2157518de1ff81e5f8f2842bb13ef49de8f635c (diff) |
fix [#34490] Copy and paste floating point number fields losses precision
- copy now gets up to 6 decimal places
- copy and UI float button editing now strip zeros: 1.000 -> 1.0
Diffstat (limited to 'source/blender/editors')
-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 |
3 files changed, 37 insertions, 12 deletions
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); |