- was displaying 1cm as 10mm because of double precission problem.

- typing in numbers without any units will use the units displayed before editing the value.
- fixed some errors
This commit is contained in:
2009-08-12 07:23:10 +00:00
parent d1085715a3
commit 63b276efbf
3 changed files with 65 additions and 14 deletions

View File

@@ -34,7 +34,7 @@ extern "C" {
void bUnit_AsString(char *str, double value, int prec, int system, int type, int split, int pad);
/* replace units with values, used before python button evaluation */
int bUnit_ReplaceString(char *str, char *str_orig, double scale_pref, int system, int type);
int bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale_pref, int system, int type);
#ifdef __cplusplus
}

View File

@@ -90,6 +90,7 @@ static struct bUnitDef buNaturalTimeDef[] = {
};
static struct bUnitCollection buNaturalTimeCollecton = {buNaturalTimeDef, 3, 0};
#define UNIT_SYSTEM_MAX 3
static struct bUnitCollection *bUnitSystems[][8] = {
{0,0,0,0,0,0,0,0},
{0,&buMetricLenCollecton, 0,0,0,0, &buNaturalTimeCollecton,0}, /* metric */
@@ -103,18 +104,25 @@ static bUnitCollection *unit_get_system(int system, int type)
return bUnitSystems[system][type]; /* select system to use, metric/imperial/other? */
}
static bUnitDef *unit_default(bUnitCollection *usys)
{
return &usys->units[usys->base_unit];
}
static bUnitDef *unit_best_fit(double value, bUnitCollection *usys, bUnitDef *unit_start)
{
bUnitDef *unit;
double value_abs= value>0.0?value:-value;
for(unit= unit_start ? unit_start:usys->units; unit->name; unit++)
if (value_abs >= unit->mul)
if (value_abs >= unit->mul*0.9999) /* scale down mul so 1cm doesnt convert to 10mm because of float error */
return unit;
return &usys->units[usys->base_unit];
return unit_default(usys);
}
/* convert into 2 units and 2 values for "2ft, 3inch" syntax */
static void unit_dual_convert(double value, bUnitCollection *usys,
bUnitDef **unit_a, bUnitDef **unit_b, double *value_a, double *value_b)
@@ -140,7 +148,7 @@ static int unit_as_string(char *str, double value, int prec, bUnitCollection *us
}
else if(value == 0.0) {
/* use the default units since there is no way to convert */
unit= &usys->units[usys->base_unit];
unit= unit_default(usys);
}
else {
unit= unit_best_fit(value, usys, NULL);
@@ -244,7 +252,7 @@ static int unit_scale_str(char *str, char *str_tmp, double scale_pref, bUnitDef
/* next char cannot be alphanum */
if (!isalpha(*(str_found+len_name))) {
int len= strlen(str);
int len_num= sprintf(str_tmp, "*%g", scale_pref*unit->mul);
int len_num= sprintf(str_tmp, "*%g", unit->mul/scale_pref);
memmove(str_found+len_num, str_found+len_name, (len+1)-(int)((str_found+len_name)-str)); /* may grow or shrink the string, 1+ to copy the string terminator */
memcpy(str_found, str_tmp, len_num); /* without the string terminator */
change= 1;
@@ -256,7 +264,6 @@ static int unit_scale_str(char *str, char *str_tmp, double scale_pref, bUnitDef
static int unit_replace(char *str, char *str_tmp, double scale_pref, bUnitDef *unit)
{
//unit_replace_delimit(str, str_tmp);
int change= 0;
change |= unit_scale_str(str, str_tmp, scale_pref, unit, unit->name_short);
change |= unit_scale_str(str, str_tmp, scale_pref, unit, unit->name_plural);
@@ -273,11 +280,14 @@ static int unit_replace(char *str, char *str_tmp, double scale_pref, bUnitDef *u
* 10.1km -> 10.1*1000.0
* ...will be resolved by python.
*
* str_prev is optional, when valid it is used to get a base unit when none is set.
*
* return true of a change was made.
*/
int bUnit_ReplaceString(char *str, char *str_orig, double scale_pref, int system, int type)
int bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale_pref, int system, int type)
{
bUnitCollection *usys = unit_get_system(system, type);
bUnitDef *unit;
char str_tmp[256];
int change= 0;
@@ -287,14 +297,53 @@ int bUnit_ReplaceString(char *str, char *str_orig, double scale_pref, int system
if(usys==NULL || usys->units[0].name==NULL) {
return 0;
}
scale_pref= 1.0/scale_pref;
for(unit= usys->units; unit->name; unit++) {
/* incase there are multiple instances */
while(unit_replace(str, str_tmp, scale_pref, unit))
change= 1;
}
unit= NULL;
{
/* try other unit systems now, so we can evaluate imperial when metric is set for eg. */
bUnitCollection *usys_iter;
int system_iter;
for(system_iter= 1; system_iter<UNIT_SYSTEM_MAX; system_iter++) {
if (system_iter != system) {
usys_iter= unit_get_system(system_iter, type);
for(unit= usys_iter->units; unit->name; unit++) {
/* incase there are multiple instances */
while(unit_replace(str, str_tmp, scale_pref, unit))
change= 1;
}
}
}
}
unit= NULL;
if(change==0) {
/* no units given so infer a unit from the previous string or default */
if(str_prev) {
/* see which units the original value had */
strcpy(str, str_prev); /* temp overwrite */
for(unit= usys->units; unit->name; unit++) {
if (unit_replace(str, str_tmp, scale_pref, unit))
break;
}
strcpy(str, str_orig); /* temp overwrite */
}
if(unit==NULL)
unit= unit_default(usys);
/* add the unit prefic and re-run */
sprintf(str_tmp, "%s %s", str, unit->name);
return bUnit_ReplaceString(str, str_tmp, NULL, scale_pref, system, type);
}
// printf("replace %s\n", str);
return change;
}

View File

@@ -1327,14 +1327,14 @@ static double ui_get_but_scale_unit(uiBut *but, double value)
static void ui_get_but_string_unit(uiBut *but, char *str, double value, int pad)
{
int do_split= U.unit_flag & USER_UNIT_OPT_SPLIT ? 1:0;
int unit_system= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
int precission= but->a2;
/* Sanity checks */
if(precission>4) precission= 4;
else if(precission==0) precission= 2;
bUnit_AsString(str, ui_get_but_scale_unit(but, value), precission, U.unit_system, unit_system, do_split, pad);
bUnit_AsString(str, ui_get_but_scale_unit(but, value), precission, U.unit_system, unit_type, do_split, pad);
}
@@ -1473,9 +1473,11 @@ int ui_set_but_string(bContext *C, uiBut *but, const char *str)
#ifndef DISABLE_PYTHON
{
char str_unit_convert[256];
int change= -1;
if(U.unit_system != USER_UNIT_NONE) {
bUnit_ReplaceString(str_unit_convert, str, ui_get_but_scale_unit(but, 1.0), U.unit_system, RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop)));
int unit_type= RNA_SUBTYPE_UNIT_VALUE(RNA_property_subtype(but->rnaprop));
if(U.unit_system != USER_UNIT_NONE && unit_type) {
/* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
bUnit_ReplaceString(str_unit_convert, str, but->drawstr, ui_get_but_scale_unit(but, 1.0), U.unit_system, unit_type);
}
else {
strcpy(str_unit_convert, str);