Fix bad loss of precision when manually editing values in numbuttons.
While drawing nice 'rounded' values is OK also for 'low precision' editing like dragging and such, it's quite an issue when you type in a precise value, validate, edit again the value, and find a rounded version of it instead of what you typed in! So now, *only when entering textedit of num buttons*, we always get the highest reasonable precision for floats (and use exponential notation when values are too low or too high, to avoid tremendous amounts of zero's).
This commit is contained in:
parent
2174a2118b
commit
ecb5b55d7f
@ -2146,9 +2146,14 @@ static float ui_get_but_step_unit(uiBut *but, float step_default)
|
||||
|
||||
/**
|
||||
* \param float_precision For number buttons the precision to use or -1 to fallback to the button default.
|
||||
* \param use_exp_float Use exponent representation of floats when out of reasonable range (outside of 1e3/1e-3).
|
||||
*/
|
||||
void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision)
|
||||
void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision, const bool use_exp_float, bool *r_use_exp_float)
|
||||
{
|
||||
if (r_use_exp_float) {
|
||||
*r_use_exp_float = false;
|
||||
}
|
||||
|
||||
if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
|
||||
PropertyType type;
|
||||
const char *buf = NULL;
|
||||
@ -2217,16 +2222,31 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int
|
||||
}
|
||||
else {
|
||||
const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
|
||||
BLI_snprintf(str, maxlen, "%.*f", prec, value);
|
||||
if (use_exp_float) {
|
||||
const int l10 = (int)log10(fabs(value));
|
||||
if (l10 < -6 || l10 > 12) {
|
||||
BLI_snprintf(str, maxlen, "%.*g", prec, value);
|
||||
if (r_use_exp_float) {
|
||||
*r_use_exp_float = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
BLI_snprintf(str, maxlen, "%.*f", prec - l10 + (int)(l10 < 0), value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
BLI_snprintf(str, maxlen, "%.*f", prec, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
else {
|
||||
BLI_snprintf(str, maxlen, "%d", (int)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
void ui_but_string_get(uiBut *but, char *str, const size_t maxlen)
|
||||
{
|
||||
ui_but_string_get_ex(but, str, maxlen, -1);
|
||||
ui_but_string_get_ex(but, str, maxlen, -1, false, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2319,7 +2319,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
|
||||
/* Get many decimal places, then strip trailing zeros.
|
||||
* note: too high values start to give strange results */
|
||||
char buf_copy[UI_MAX_DRAW_STR];
|
||||
ui_but_string_get_ex(but, buf_copy, sizeof(buf_copy), UI_PRECISION_FLOAT_MAX);
|
||||
ui_but_string_get_ex(but, buf_copy, sizeof(buf_copy), UI_PRECISION_FLOAT_MAX, false, NULL);
|
||||
BLI_str_rstrip_float_zero(buf_copy, '\0');
|
||||
|
||||
WM_clipboard_text_set(buf_copy, 0);
|
||||
@ -3060,6 +3060,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
|
||||
wmWindow *win = CTX_wm_window(C);
|
||||
int len;
|
||||
const bool is_num_but = ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER);
|
||||
bool no_zero_strip = false;
|
||||
|
||||
if (data->str) {
|
||||
MEM_freeN(data->str);
|
||||
@ -3092,14 +3093,16 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
|
||||
data->maxlen = ui_but_string_get_max_length(but);
|
||||
if (data->maxlen != 0) {
|
||||
data->str = MEM_callocN(sizeof(char) * data->maxlen, "textedit str");
|
||||
ui_but_string_get(but, data->str, data->maxlen);
|
||||
/* We do not want to truncate precision to default here, it's nice to show value,
|
||||
* not to edit it - way too much precision is lost then. */
|
||||
ui_but_string_get_ex(but, data->str, data->maxlen, UI_PRECISION_FLOAT_MAX, true, &no_zero_strip);
|
||||
}
|
||||
else {
|
||||
data->is_str_dynamic = true;
|
||||
data->str = ui_but_string_get_dynamic(but, &data->maxlen);
|
||||
}
|
||||
|
||||
if (ui_but_is_float(but) && !ui_but_is_unit(but) && !ui_but_anim_expression_get(but, NULL, 0)) {
|
||||
if (ui_but_is_float(but) && !ui_but_is_unit(but) && !ui_but_anim_expression_get(but, NULL, 0) && !no_zero_strip) {
|
||||
BLI_str_rstrip_float_zero(data->str, '\0');
|
||||
}
|
||||
|
||||
|
@ -473,7 +473,9 @@ extern void ui_hsvcircle_pos_from_vals(struct uiBut *but, const rcti *rect, floa
|
||||
extern void ui_hsvcube_pos_from_vals(struct uiBut *but, const rcti *rect, float *hsv, float *xp, float *yp);
|
||||
bool ui_but_is_colorpicker_display_space(struct uiBut *but);
|
||||
|
||||
extern void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision) ATTR_NONNULL();
|
||||
extern void ui_but_string_get_ex(
|
||||
uiBut *but, char *str, const size_t maxlen,
|
||||
const int float_precision, const bool use_exp_float, bool *r_use_exp_float) ATTR_NONNULL(1, 2);
|
||||
extern void ui_but_string_get(uiBut *but, char *str, const size_t maxlen) ATTR_NONNULL();
|
||||
extern char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size);
|
||||
extern void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen) ATTR_NONNULL();
|
||||
|
Loading…
Reference in New Issue
Block a user