From c3b641613cce942e5d2b6db39b1f70917b65a546 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Tue, 22 Jun 2021 16:51:34 -0300 Subject: Cleanup: deduplicate code and rename variables This makes it clearer that variables are strings and how they are used in the Move header code. --- .../editors/transform/transform_mode_translate.c | 98 ++++++++++------------ 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/source/blender/editors/transform/transform_mode_translate.c b/source/blender/editors/transform/transform_mode_translate.c index 3088f6a7776..c7c5f3f8dd8 100644 --- a/source/blender/editors/transform/transform_mode_translate.c +++ b/source/blender/editors/transform/transform_mode_translate.c @@ -52,12 +52,27 @@ /** \name Transform (Translation) * \{ */ +static void translate_dist_to_str(char *r_str, + const int len_max, + const float val, + const UnitSettings *unit) +{ + if (unit) { + BKE_unit_value_as_string( + r_str, len_max, val * unit->scale_length, 4, B_UNIT_LENGTH, unit, false); + } + else { + /* Check range to prevent string buffer overflow. */ + BLI_snprintf(r_str, len_max, IN_RANGE_INCL(val, -1e10f, 1e10f) ? "%.4f" : "%.4e", val); + } +} + static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_DRAW_STR]) { size_t ofs = 0; - char tvec[NUM_STR_REP_LEN * 3]; - char distvec[NUM_STR_REP_LEN]; - char autoik[NUM_STR_REP_LEN]; + char dvec_str[3][NUM_STR_REP_LEN]; + char dist_str[NUM_STR_REP_LEN]; + char autoik_str[NUM_STR_REP_LEN]; float dist; UnitSettings *unit = NULL; @@ -66,7 +81,7 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ } if (hasNumInput(&t->num)) { - outputNumInput(&(t->num), tvec, &t->scene->unit); + outputNumInput(&(t->num), dvec_str, &t->scene->unit); dist = len_v3(t->num.val); } else { @@ -94,48 +109,25 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ dist = len_v3(dvec); - if (unit) { - for (int i = 0; i < 3; i++) { - BKE_unit_value_as_string(&tvec[NUM_STR_REP_LEN * i], - NUM_STR_REP_LEN, - dvec[i] * unit->scale_length, - 4, - B_UNIT_LENGTH, - unit, - true); - } - } - else { - BLI_snprintf(&tvec[0], NUM_STR_REP_LEN, "%.4f", dvec[0]); - BLI_snprintf(&tvec[NUM_STR_REP_LEN], NUM_STR_REP_LEN, "%.4f", dvec[1]); - BLI_snprintf(&tvec[NUM_STR_REP_LEN * 2], NUM_STR_REP_LEN, "%.4f", dvec[2]); + for (int i = 0; i < 3; i++) { + translate_dist_to_str(dvec_str[i], sizeof(dvec_str[i]), dvec[i], unit); } } - if (unit) { - BKE_unit_value_as_string( - distvec, sizeof(distvec), dist * unit->scale_length, 4, B_UNIT_LENGTH, unit, false); - } - else if (dist > 1e10f || dist < -1e10f) { - /* prevent string buffer overflow */ - BLI_snprintf(distvec, NUM_STR_REP_LEN, "%.4e", dist); - } - else { - BLI_snprintf(distvec, NUM_STR_REP_LEN, "%.4f", dist); - } + translate_dist_to_str(dist_str, sizeof(dist_str), dist, unit); if (t->flag & T_AUTOIK) { short chainlen = t->settings->autoik_chainlen; if (chainlen) { - BLI_snprintf(autoik, NUM_STR_REP_LEN, TIP_("AutoIK-Len: %d"), chainlen); + BLI_snprintf(autoik_str, sizeof(autoik_str), TIP_("AutoIK-Len: %d"), chainlen); } else { - autoik[0] = '\0'; + autoik_str[0] = '\0'; } } else { - autoik[0] = '\0'; + autoik_str[0] = '\0'; } if (t->con.mode & CON_APPLY) { @@ -144,34 +136,34 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ ofs += BLI_snprintf_rlen(str + ofs, UI_MAX_DRAW_STR - ofs, "D: %s (%s)%s %s %s", - &tvec[0], - distvec, + dvec_str[0], + dist_str, t->con.text, t->proptext, - autoik); + autoik_str); break; case 1: ofs += BLI_snprintf_rlen(str + ofs, UI_MAX_DRAW_STR - ofs, "D: %s D: %s (%s)%s %s %s", - &tvec[0], - &tvec[NUM_STR_REP_LEN], - distvec, + dvec_str[0], + dvec_str[1], + dist_str, t->con.text, t->proptext, - autoik); + autoik_str); break; case 2: ofs += BLI_snprintf_rlen(str + ofs, UI_MAX_DRAW_STR - ofs, "D: %s D: %s D: %s (%s)%s %s %s", - &tvec[0], - &tvec[NUM_STR_REP_LEN], - &tvec[NUM_STR_REP_LEN * 2], - distvec, + dvec_str[0], + dvec_str[1], + dvec_str[2], + dist_str, t->con.text, t->proptext, - autoik); + autoik_str); break; } } @@ -180,9 +172,9 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ ofs += BLI_snprintf_rlen(str + ofs, UI_MAX_DRAW_STR - ofs, "Dx: %s Dy: %s (%s)%s %s", - &tvec[0], - &tvec[NUM_STR_REP_LEN], - distvec, + dvec_str[0], + dvec_str[1], + dist_str, t->con.text, t->proptext); } @@ -190,13 +182,13 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ ofs += BLI_snprintf_rlen(str + ofs, UI_MAX_DRAW_STR - ofs, "Dx: %s Dy: %s Dz: %s (%s)%s %s %s", - &tvec[0], - &tvec[NUM_STR_REP_LEN], - &tvec[NUM_STR_REP_LEN * 2], - distvec, + dvec_str[0], + dvec_str[1], + dvec_str[2], + dist_str, t->con.text, t->proptext, - autoik); + autoik_str); } } -- cgit v1.2.3