[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22565] branches/blender2.5/blender/source /blender: units with no separator can now be typed into number buttons - eg , 5'7" 2km4m

Campbell Barton ideasman42 at gmail.com
Mon Aug 17 14:48:56 CEST 2009


Revision: 22565
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22565
Author:   campbellbarton
Date:     2009-08-17 14:48:56 +0200 (Mon, 17 Aug 2009)

Log Message:
-----------
units with no separator can now be typed into number buttons - eg, 5'7"  2km4m

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h
    branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c
    branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c
    branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c

Modified: branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h	2009-08-17 11:45:24 UTC (rev 22564)
+++ branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h	2009-08-17 12:48:56 UTC (rev 22565)
@@ -47,7 +47,7 @@
 
 void	bUnit_GetSystem(void **usys_pt, int *len, int system, int type);
 char*	bUnit_GetName(void *usys_pt, int index);
-char*	bUnit_GetNamePlural(void *usys_pt, int index);
+char*	bUnit_GetNameDisplay(void *usys_pt, int index);
 double	bUnit_GetScaler(void *usys_pt, int index);
 
 /* aligned with PropertyUnit */

Modified: branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c	2009-08-17 11:45:24 UTC (rev 22564)
+++ branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c	2009-08-17 12:48:56 UTC (rev 22565)
@@ -33,6 +33,12 @@
 
 #define TEMP_STR_SIZE 256
 
+#define SEP_CHR		'#'
+#define SEP_STR		"#"
+
+#define EUL 0.000001
+
+
 /* define a single unit */
 typedef struct bUnitDef {
 	char *name;
@@ -40,6 +46,8 @@
 	char *name_short;	/* this is used for display*/
 	char *name_alt;		/* can be NULL */
 	
+	char *name_display;		/* can be NULL */
+
 	double scalar;
 	double bias;		/* not used yet, needed for converting temperature */
 	int flag;
@@ -58,22 +66,22 @@
 
 /* Dummy */
 static struct bUnitDef buDummyDef[] = {
-	{"", NULL, "", NULL,	1.0, 0.0},
-	{NULL, NULL, NULL,	NULL, 0.0, 0.0}
+	{"",	NULL, "",	NULL, NULL, 1.0, 0.0},
+	{NULL,	NULL, NULL,	NULL, NULL, 0.0, 0.0}
 };
 static struct bUnitCollection buDummyCollecton = {buDummyDef, 0, 0, sizeof(buDummyDef)};
 
 
 /* Lengths */
 static struct bUnitDef buMetricLenDef[] = {
-	{"kilometer", "Kilometers",		"km", NULL,	1000.0, 0.0,		B_UNIT_DEF_NONE},
-	{"hectometer", "100 Meters",	"hm", NULL,	100.0, 0.0,			B_UNIT_DEF_SUPPRESS},
-	{"dekameter", "10 Meters",		"dkm",NULL,	10.0, 0.0,			B_UNIT_DEF_SUPPRESS},
-	{"meter", "Meters",				"m",  NULL,	1.0, 0.0, 			B_UNIT_DEF_NONE}, /* base unit */
-	{"decimetre", "10 Centimeters",	"dm", NULL,	0.1, 0.0,			B_UNIT_DEF_SUPPRESS},
-	{"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?
+	{"kilometer", "kilometers",		"km", NULL,	"Kilometers", 1000.0, 0.0,		B_UNIT_DEF_NONE},
+	{"hectometer", "hectometers",	"hm", NULL,	"10 Meters", 100.0, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"dekameter", "dekameters",		"dkm",NULL,	"10 Meters", 10.0, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"meter", "meters",				"m",  NULL,	"Meters", 1.0, 0.0, 			B_UNIT_DEF_NONE}, /* base unit */
+	{"decimetre", "decimetres",		"dm", NULL,	"10 Centimeters", 0.1, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"centimeter", "centimeters",	"cm", NULL,	"Centimeters", 0.01, 0.0,			B_UNIT_DEF_NONE},
+	{"millimeter", "millimeters",	"mm", NULL,	"Millimeters", 0.001, 0.0,			B_UNIT_DEF_NONE},
+	{"micrometer", "micrometers",	"um", "µm",	"Micrometers", 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 */
@@ -81,19 +89,19 @@
 	{"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}
+	{NULL, NULL, NULL,	NULL, NULL, 0.0, 0.0}
 };
 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},
-	{"furlong", "Furlongs",			"fur", NULL,201.168, 0.0,	B_UNIT_DEF_SUPPRESS},
-	{"chain", "Chains",				"ch", NULL,	0.9144*22.0, 0.0,	B_UNIT_DEF_SUPPRESS},
-	{"yard", "Yards",				"yd", NULL,	0.9144, 0.0,	B_UNIT_DEF_NONE},
-	{"foot", "Feet",				"'", "ft",	0.3048, 0.0,	B_UNIT_DEF_NONE},
-	{"inch", "Inches",				"\"", "in",	0.0254, 0.0,	B_UNIT_DEF_NONE}, /* base unit */
-	{"thou", "Thous",				"mil", NULL,0.0000254, 0.0,	B_UNIT_DEF_NONE},
-	{NULL, NULL, NULL, NULL, 0.0, 0.0}
+	{"mile", "miles",		"mi", "m", "Miles",		1609.344, 0.0,	B_UNIT_DEF_NONE},
+	{"furlong", "furlongs",	"fur", NULL, "Furlongs",201.168, 0.0,	B_UNIT_DEF_SUPPRESS},
+	{"chain", "chains",		"ch", NULL, "Chains",	0.9144*22.0, 0.0,	B_UNIT_DEF_SUPPRESS},
+	{"yard", "yards",		"yd", NULL, "Yards",	0.9144, 0.0,	B_UNIT_DEF_NONE},
+	{"foot", "feet",		"'", "ft", "Feet",		0.3048, 0.0,	B_UNIT_DEF_NONE},
+	{"inch", "inches",		"\"", "in", "Inches",	0.0254, 0.0,	B_UNIT_DEF_NONE}, /* base unit */
+	{"thou", "thous",		"mil", NULL, "Thous",	0.0000254, 0.0,	B_UNIT_DEF_NONE},
+	{NULL, NULL, NULL, NULL, NULL, 0.0, 0.0}
 };
 static struct bUnitCollection buImperialLenCollecton = {buImperialLenDef, 4, 0, sizeof(buImperialLenDef)/sizeof(bUnitDef)};
 
@@ -101,13 +109,13 @@
 /* Time */
 static struct bUnitDef buNaturalTimeDef[] = {
 	/* weeks? - probably not needed for blender */
-	{"day", "Days",					"d", NULL,	90000.0, 0.0,	B_UNIT_DEF_NONE},
-	{"hour", "Hours",				"hr", "h",	3600.0, 0.0,	B_UNIT_DEF_NONE},
-	{"minute", "Minutes",			"min", "m",	60.0, 0.0,		B_UNIT_DEF_NONE},
-	{"second", "Seconds",			"sec", "s",	1.0, 0.0,		B_UNIT_DEF_NONE}, /* base unit */
-	{"millisecond", "Milliseconds",	"ms", NULL,	0.001, 0.0	,	B_UNIT_DEF_NONE},
-	{"microsecond", "Microseconds",	"us", NULL,	0.000001, 0.0,	B_UNIT_DEF_NONE},
-	{NULL, NULL, NULL, NULL, 0.0, 0.0}
+	{"day", "days",					"d", NULL,	"Days",			90000.0, 0.0,	B_UNIT_DEF_NONE},
+	{"hour", "hours",				"hr", "h",	"Hours",		3600.0, 0.0,	B_UNIT_DEF_NONE},
+	{"minute", "minutes",			"min", "m",	"Minutes",		60.0, 0.0,		B_UNIT_DEF_NONE},
+	{"second", "seconds",			"sec", "s",	"Seconds",		1.0, 0.0,		B_UNIT_DEF_NONE}, /* base unit */
+	{"millisecond", "milliseconds",	"ms", NULL,	"Milliseconds",	0.001, 0.0	,	B_UNIT_DEF_NONE},
+	{"microsecond", "microseconds",	"us", NULL,	"Microseconds",	0.000001, 0.0,	B_UNIT_DEF_NONE},
+	{NULL, NULL, NULL, NULL, NULL, 0.0, 0.0}
 };
 static struct bUnitCollection buNaturalTimeCollecton = {buNaturalTimeDef, 3, 0, sizeof(buNaturalTimeDef)/sizeof(bUnitDef)};
 
@@ -140,7 +148,7 @@
 		if(suppress && (unit->flag & B_UNIT_DEF_SUPPRESS))
 			continue;
 
-		if (value_abs >= unit->scalar*0.9999) /* scale down scalar so 1cm doesnt convert to 10mm because of float error */
+		if (value_abs >= unit->scalar*(1.0-EUL)) /* scale down scalar so 1cm doesnt convert to 10mm because of float error */
 			return unit;
 	}
 
@@ -254,8 +262,7 @@
 			i= unit_as_string(str, len_max, value_a, prec, usys,  unit_a, '\0');
 
 			/* is there enough space for at least 1 char of the next unit? */
-			if(i+3 < len_max) {
-				str[i++]= ',';
+			if(i+2 < len_max) {
 				str[i++]= ' ';
 
 				/* use low precision since this is a smaller unit */
@@ -292,6 +299,35 @@
 
 }
 
+/* Note that numbers are added within brackets
+ * ") " - is used to detect numbers we added so we can detect if commas need to be added
+ *
+ * "1m1cm+2mm"				- Original value
+ * "1*1#1*0.01#+2*0.001#"	- Replace numbers
+ * "1*1,1*0.01 +2*0.001 "	- Add comma's if ( - + * / % ^ < > ) not found in between
+ *
+ */
+
+/* not too strict, (- = * /) are most common  */
+static int ch_is_op(char op)
+{
+	switch(op) {
+	case '+':
+	case '-':
+	case '*':
+	case '/':
+	case '|':
+	case '<':
+	case '>':
+	case '^':
+	case '!':
+	case '=':
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static int unit_scale_str(char *str, int len_max, char *str_tmp, double scale_pref, bUnitDef *unit, char *replace_str)
 {
 	char *str_found;
@@ -305,7 +341,7 @@
 
 		len_name = strlen(replace_str);
 		len_move= (len - (found_ofs+len_name)) + 1; /* 1+ to copy the string terminator */
-		len_num= snprintf(str_tmp, TEMP_STR_SIZE, "*%lg", unit->scalar/scale_pref);
+		len_num= snprintf(str_tmp, TEMP_STR_SIZE, "*%lg"SEP_STR, unit->scalar/scale_pref); /* # removed later */
 
 		if(len_num > len_max)
 			len_num= len_max;
@@ -366,6 +402,9 @@
  * 10.1km -> 10.1*1000.0
  * ...will be resolved by python.
  * 
+ * values will be split by a comma's
+ * 5'2" -> 5'0.0254, 2*0.3048
+ *
  * 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.
@@ -382,6 +421,17 @@
 		return 0;
 	}
 	
+
+	{	/* make lowercase */
+		int i;
+		char *ch= str;
+
+		for(i=0; (i>=len_max || *ch=='\0'); i++, ch++)
+			if((*ch>='A') && (*ch<='Z'))
+				*ch += ('a'-'A');
+	}
+
+
 	for(unit= usys->units; unit->name; unit++) {
 
 		if(unit->flag & B_UNIT_DEF_SUPPRESS)
@@ -432,13 +482,51 @@
 		if(unit==NULL)
 			unit= unit_default(usys);
 
-		/* add the unit prefic and re-run */
-		snprintf(str_tmp, sizeof(str_tmp), "%s %s", str, unit->name);
-		strncpy(str, str_tmp, len_max);
+		/* add the unit prefic and re-run, use brackets incase there was an expression given */
+		if(snprintf(str_tmp, sizeof(str_tmp), "(%s)%s", str, unit->name) < sizeof(str_tmp)) {
+			strncpy(str, str_tmp, len_max);
+			return bUnit_ReplaceString(str, len_max, NULL, scale_pref, system, type);
+		}
+		else {
+			/* snprintf would not fit into str_tmp, cant do much in this case
+			 * check for this because otherwise bUnit_ReplaceString could call its self forever */
+			return 0;
+		}
 
-		return bUnit_ReplaceString(str, len_max, NULL, scale_pref, system, type);
 	}
 
+	/* replace # with commas when there is no operator between it and the next number
+	 *
+	 * "1*1# 3*100# * 3"  ->  "1 *1, 3 *100  * 3"
+	 *
+	 * */
+	{
+		char *str_found= str;
+		char *ch= str;
+
+		while((str_found= strchr(str_found, SEP_CHR))) {
+
+			int op_found= 0;
+			/* any operators after this?*/
+			for(ch= str_found+1; *ch!='\0'; ch++) {
+
+				if(*ch==' ' || *ch=='\t') {
+					/* do nothing */
+				}
+				else if (ch_is_op(*ch) || *ch==',') { /* found an op, no need to insert a ,*/
+					op_found= 1;
+					break;
+				}
+				else { /* found a non-op character */
+					op_found= 0;
+					break;
+				}
+			}
+
+			*str_found++ = op_found ? ' ':',';
+		}
+	}
+
 	// printf("replace %s\n", str);
 	return change;
 }
@@ -483,9 +571,9 @@
 {
 	return ((bUnitCollection *)usys_pt)->units[index].name;
 }
-char *bUnit_GetNamePlural(void *usys_pt, int index)

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list