unit grid snap while transforming, display units in the header.
authorCampbell Barton <ideasman42@gmail.com>
Wed, 12 Aug 2009 17:02:03 +0000 (17:02 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 12 Aug 2009 17:02:03 +0000 (17:02 +0000)
source/blender/blenkernel/BKE_unit.h
source/blender/blenkernel/intern/unit.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/editors/transform/transform.c

index c64c5aa50a1cab61d4eb116c32c6e624111de4cb..df29971867387e4d1f7afc4d6a1c44c90aa9d754 100644 (file)
@@ -39,6 +39,9 @@ int           bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale
 /* the size of the unit used for this value (used for calculating the ckickstep) */
 double bUnit_ClosestScalar(double value, int system, int type);
 
+/* base scale for these units */
+double bUnit_BaseScalar(int system, int type);
+
 /* loop over scales, coudl add names later */
 //double bUnit_Iter(void **unit, char **name, int system, int type);
 
index fef15f83ccdbc7e1aed0bd8efeb6462c587b6749..8c63a8756a4d082d4e61e36b9c1159ef684641b2 100644 (file)
@@ -68,11 +68,16 @@ static struct bUnitDef buMetricLenDef[] = {
        {"centimeter", "Centimeters",   "cm", NULL,     0.01, 0.0,                      B_UNIT_DEF_NONE},
        {"millimeter", "Millimeters",   "mm", NULL,     0.001, 0.0,                     B_UNIT_DEF_NONE},
        {"micrometer", "Micrometers",   "um", "┬Ám",    0.000001, 0.0,          B_UNIT_DEF_NONE}, // micron too?
+
+       /* These get displayed because of float precision problems in the transform header,
+        * could work around, but for now probably people wont use these */
+       /*
        {"nanometer", "Nanometers",             "nm", NULL,     0.000000001, 0.0,       B_UNIT_DEF_NONE},
        {"picometer", "Picometers",             "pm", NULL,     0.000000000001, 0.0,B_UNIT_DEF_NONE},
+       */
        {NULL, NULL, NULL,      NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buMetricLenCollecton = {buMetricLenDef, 1, 0, sizeof(buMetricLenDef)/sizeof(bUnitDef)};
+static struct bUnitCollection buMetricLenCollecton = {buMetricLenDef, 3, 0, sizeof(buMetricLenDef)/sizeof(bUnitDef)};
 
 static struct bUnitDef buImperialLenDef[] = {
        {"mile", "Miles",                               "mi", "m",      1609.344, 0.0,  B_UNIT_DEF_NONE},
@@ -83,7 +88,7 @@ static struct bUnitDef buImperialLenDef[] = {
        {"thou", "Thous",                               "mil", NULL,0.0000254, 0.0,     B_UNIT_DEF_NONE},
        {NULL, NULL, NULL, NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buImperialLenCollecton = {buImperialLenDef, 2, 0, sizeof(buImperialLenDef)/sizeof(bUnitDef)};
+static struct bUnitCollection buImperialLenCollecton = {buImperialLenDef, 3, 0, sizeof(buImperialLenDef)/sizeof(bUnitDef)};
 
 
 /* Time */
@@ -390,6 +395,12 @@ double bUnit_ClosestScalar(double value, int system, int type)
        return unit->scalar;
 }
 
+double bUnit_BaseScalar(int system, int type)
+{
+       bUnitCollection *usys = unit_get_system(system, type);
+       return unit_default(usys)->scalar;
+}
+
 /* external access */
 void bUnit_GetSystem(void **usys_pt, int *len, int system, int type)
 {
index fb4ef3349e602807454c3d7c8bbf0b56ebead004..fcc6692f621cce172cb415eeae411d6b6ecf7255 100644 (file)
@@ -244,7 +244,6 @@ static void drawgrid(ARegion *ar, View3D *v3d, char **grid_unit)
        float wx, wy, x, y, fw, fx, fy, dx;
        float vec4[4];
        char col[3], col2[3];
-       short sublines = v3d->gridsubdiv;
        
        *grid_unit= NULL;
 
@@ -261,7 +260,8 @@ static void drawgrid(ARegion *ar, View3D *v3d, char **grid_unit)
        x= (wx)*fx/fw;
        y= (wy)*fy/fw;
 
-       vec4[0]=vec4[1]=v3d->grid; 
+       vec4[0]=vec4[1]= (U.unit_system) ? 1.0 : v3d->grid;
+
        vec4[2]= 0.0;
        vec4[3]= 1.0;
        Mat4MulVec4fl(rv3d->persmat, vec4);
@@ -278,6 +278,8 @@ static void drawgrid(ARegion *ar, View3D *v3d, char **grid_unit)
        UI_ThemeColor(TH_GRID);
        
        if(U.unit_system) {
+               /* Use GRID_MIN_PX*2 for units because very very small grid
+                * items are less useful when dealing with units */
                void *usys;
                int len, i;
                double scalar;
@@ -292,14 +294,15 @@ static void drawgrid(ARegion *ar, View3D *v3d, char **grid_unit)
                                scalar= bUnit_GetScaler(usys, i);
 
                                dx_scalar = dx * scalar * U.unit_scale_length;
-                               if (dx_scalar < GRID_MIN_PX)
+                               if (dx_scalar < (GRID_MIN_PX*2))
                                        continue;
 
-                               /* Store the smallest drawn grid size units name so users know how bit each grid cell is */
-                               if(*grid_unit==NULL)
+                               /* Store the smallest drawn grid size units name so users know how big each grid cell is */
+                               if(*grid_unit==NULL) {
                                        *grid_unit= bUnit_GetNamePlural(usys, i);
-
-                               blend_fac= 1-(GRID_MIN_PX/dx_scalar);
+                                       v3d->gridview= (scalar * U.unit_scale_length);
+                               }
+                               blend_fac= 1-((GRID_MIN_PX*2)/dx_scalar);
 
                                /* tweak to have the fade a bit nicer */
                                blend_fac= (blend_fac * blend_fac) * 2.0f;
@@ -313,6 +316,8 @@ static void drawgrid(ARegion *ar, View3D *v3d, char **grid_unit)
                }
        }
        else {
+               short sublines = v3d->gridsubdiv;
+
                if(dx<GRID_MIN_PX) {
                        v3d->gridview*= sublines;
                        dx*= sublines;
index 510193fdade96ff1c3b3daed1f4c0597af5ab9ab..1d4882c2da251ce94f0366190629dd86c07f7dc5 100644 (file)
@@ -89,6 +89,7 @@
 #include "BKE_pointcache.h"
 #include "BKE_utildefines.h"
 #include "BKE_context.h"
+#include "BKE_unit.h"
 
 //#include "BSE_view.h"
 
@@ -3039,12 +3040,22 @@ static void headerTranslation(TransInfo *t, float vec[3], char *str) {
                applyAspectRatio(t, dvec);
 
                dist = VecLength(vec);
-               sprintf(&tvec[0], "%.4f", dvec[0]);
-               sprintf(&tvec[20], "%.4f", dvec[1]);
-               sprintf(&tvec[40], "%.4f", dvec[2]);
+               if(U.unit_system) {
+                       int i, do_split= U.unit_flag & USER_UNIT_OPT_SPLIT ? 1:0;
+
+                       for(i=0; i<3; i++)
+                               bUnit_AsString(&tvec[i*20], dvec[i]*U.unit_scale_length, 4, U.unit_system, B_UNIT_LENGTH, do_split, 1);
+               }
+               else {
+                       sprintf(&tvec[0], "%.4f", dvec[0]);
+                       sprintf(&tvec[20], "%.4f", dvec[1]);
+                       sprintf(&tvec[40], "%.4f", dvec[2]);
+               }
        }
 
-       if( dist > 1e10 || dist < -1e10 )       /* prevent string buffer overflow */
+       if(U.unit_system)
+               bUnit_AsString(distvec, dist*U.unit_scale_length, 4, U.unit_system, B_UNIT_LENGTH, U.unit_flag & USER_UNIT_OPT_SPLIT, 0);
+       else if( dist > 1e10 || dist < -1e10 )  /* prevent string buffer overflow */
                sprintf(distvec, "%.4e", dist);
        else
                sprintf(distvec, "%.4f", dist);