[Bf-blender-cvs] [c3b641613cc] master: Cleanup: deduplicate code and rename variables

Germano Cavalcante noreply at git.blender.org
Tue Jun 22 21:51:45 CEST 2021


Commit: c3b641613cce942e5d2b6db39b1f70917b65a546
Author: Germano Cavalcante
Date:   Tue Jun 22 16:51:34 2021 -0300
Branches: master
https://developer.blender.org/rBc3b641613cce942e5d2b6db39b1f70917b65a546

Cleanup: deduplicate code and rename variables

This makes it clearer that variables are strings and how they are used
in the Move header code.

===================================================================

M	source/blender/editors/transform/transform_mode_translate.c

===================================================================

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);
     }
   }



More information about the Bf-blender-cvs mailing list