[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [40068] trunk/blender/source/blender/ editors/interface/interface_widgets.c: replace magic numbers with defines to make the interface source more readable .

Campbell Barton ideasman42 at gmail.com
Fri Sep 9 15:10:18 CEST 2011


Revision: 40068
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=40068
Author:   campbellbarton
Date:     2011-09-09 13:10:18 +0000 (Fri, 09 Sep 2011)
Log Message:
-----------
replace magic numbers with defines to make the interface source more readable.
only functional change is the uiWidgetBase struct stored up to 64 verts, changed this to 36 since thats the most that is used atm, added assert if this ever changes.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/interface/interface_widgets.c

Modified: trunk/blender/source/blender/editors/interface/interface_widgets.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_widgets.c	2011-09-09 13:02:15 UTC (rev 40067)
+++ trunk/blender/source/blender/editors/interface/interface_widgets.c	2011-09-09 13:10:18 UTC (rev 40068)
@@ -86,12 +86,25 @@
 	
 } uiWidgetTrias;
 
+/* max as used by round_box__edges */
+#define WIDGET_CURVE_RESOLU 9
+#define WIDGET_SIZE_MAX (WIDGET_CURVE_RESOLU*4)
+
+enum {
+	WIDGET_TOP_LEFT= 1,
+	WIDGET_TOP_RIGHT= 2,
+	WIDGET_BOTTOM_RIGHT= 4,
+	WIDGET_BOTTOM_LEFT= 8,
+	/* just for convenience */
+	WIDGET_ALL_CORNERS= (WIDGET_TOP_LEFT | WIDGET_TOP_RIGHT | WIDGET_BOTTOM_RIGHT | WIDGET_BOTTOM_LEFT)
+};
+
 typedef struct uiWidgetBase {
 	
 	int totvert, halfwayvert;
-	float outer_v[64][2];
-	float inner_v[64][2];
-	float inner_uv[64][2];
+	float outer_v[WIDGET_SIZE_MAX][2];
+	float inner_v[WIDGET_SIZE_MAX][2];
+	float inner_uv[WIDGET_SIZE_MAX][2];
 	
 	short inner, outline, emboss; /* set on/off */
 	short shadedir;
@@ -123,7 +136,7 @@
 
 /* *********************** draw data ************************** */
 
-static float cornervec[9][2]= {{0.0, 0.0}, {0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, 
+static float cornervec[WIDGET_CURVE_RESOLU][2]= {{0.0, 0.0}, {0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169},
 {0.707, 0.293}, {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}, {1.0, 1.0}};
 
 static float jit[8][2]= {{0.468813 , -0.481430}, {-0.155755 , -0.352820}, 
@@ -216,7 +229,7 @@
 /* return tot */
 static int round_box_shadow_edges(float (*vert)[2], rcti *rect, float rad, int roundboxalign, float step)
 {
-	float vec[9][2];
+	float vec[WIDGET_CURVE_RESOLU][2];
 	float minx, miny, maxx, maxy;
 	int a, tot= 0;
 	
@@ -231,59 +244,59 @@
 	maxy= rect->ymax+step;
 	
 	/* mult */
-	for(a=0; a<9; a++) {
+	for(a=0; a < WIDGET_CURVE_RESOLU; a++) {
 		vec[a][0]= rad*cornervec[a][0]; 
 		vec[a][1]= rad*cornervec[a][1]; 
 	}
 	
 	/* start with left-top, anti clockwise */
-	if(roundboxalign & 1) {
-		for(a=0; a<9; a++, tot++) {
+	if(roundboxalign & WIDGET_TOP_LEFT) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= minx+rad-vec[a][0];
 			vert[tot][1]= maxy-vec[a][1];
 		}
 	}
 	else {
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= minx;
 			vert[tot][1]= maxy;
 		}
 	}
 	
-	if(roundboxalign & 8) {
-		for(a=0; a<9; a++, tot++) {
+	if(roundboxalign & WIDGET_BOTTOM_LEFT) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= minx+vec[a][1];
 			vert[tot][1]= miny+rad-vec[a][0];
 		}
 	}
 	else {
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= minx;
 			vert[tot][1]= miny;
 		}
 	}
 	
-	if(roundboxalign & 4) {
-		for(a=0; a<9; a++, tot++) {
+	if(roundboxalign & WIDGET_BOTTOM_RIGHT) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= maxx-rad+vec[a][0];
 			vert[tot][1]= miny+vec[a][1];
 		}
 	}
 	else {
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= maxx;
 			vert[tot][1]= miny;
 		}
 	}
 	
-	if(roundboxalign & 2) {
-		for(a=0; a<9; a++, tot++) {
+	if(roundboxalign & WIDGET_TOP_RIGHT) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= maxx-vec[a][1];
 			vert[tot][1]= maxy-rad+vec[a][0];
 		}
 	}
 	else {
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			vert[tot][0]= maxx;
 			vert[tot][1]= maxy;
 		}
@@ -294,7 +307,7 @@
 /* this call has 1 extra arg to allow mask outline */
 static void round_box__edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad, float radi)
 {
-	float vec[9][2], veci[9][2];
+	float vec[WIDGET_CURVE_RESOLU][2], veci[WIDGET_CURVE_RESOLU][2];
 	float minx= rect->xmin, miny= rect->ymin, maxx= rect->xmax, maxy= rect->ymax;
 	float minxi= minx + 1.0f; /* boundbox inner */
 	float maxxi= maxx - 1.0f;
@@ -303,8 +316,10 @@
 	float facxi= (maxxi!=minxi) ? 1.0f/(maxxi-minxi) : 0.0f; /* for uv, can divide by zero */
 	float facyi= (maxyi!=minyi) ? 1.0f/(maxyi-minyi) : 0.0f;
 	int a, tot= 0, minsize;
-	const int hnum= ((roundboxalign & (1|2))==(1|2) || (roundboxalign & (4|8))==(4|8)) ? 1 : 2;
-	const int vnum= ((roundboxalign & (1|8))==(1|8) || (roundboxalign & (2|4))==(2|4)) ? 1 : 2;
+	const int hnum= ((roundboxalign & (WIDGET_TOP_LEFT|WIDGET_TOP_RIGHT))==(WIDGET_TOP_LEFT|WIDGET_TOP_RIGHT) ||
+	                 (roundboxalign & (WIDGET_BOTTOM_RIGHT|WIDGET_BOTTOM_LEFT))==(WIDGET_BOTTOM_RIGHT|WIDGET_BOTTOM_LEFT)) ? 1 : 2;
+	const int vnum= ((roundboxalign & (WIDGET_TOP_LEFT|WIDGET_BOTTOM_LEFT))==(WIDGET_TOP_LEFT|WIDGET_BOTTOM_LEFT) ||
+	                 (roundboxalign & (WIDGET_TOP_RIGHT|WIDGET_BOTTOM_RIGHT))==(WIDGET_TOP_RIGHT|WIDGET_BOTTOM_RIGHT)) ? 1 : 2;
 
 	minsize= MIN2((rect->xmax-rect->xmin)*hnum, (rect->ymax-rect->ymin)*vnum);
 	
@@ -315,7 +330,7 @@
 		radi= 0.5f*minsize - 1.0f;
 	
 	/* mult */
-	for(a=0; a<9; a++) {
+	for(a=0; a < WIDGET_CURVE_RESOLU; a++) {
 		veci[a][0]= radi*cornervec[a][0]; 
 		veci[a][1]= radi*cornervec[a][1]; 
 		vec[a][0]= rad*cornervec[a][0]; 
@@ -323,9 +338,9 @@
 	}
 	
 	/* corner left-bottom */
-	if(roundboxalign & 8) {
+	if(roundboxalign & WIDGET_BOTTOM_LEFT) {
 		
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			wt->inner_v[tot][0]= minxi+veci[a][1];
 			wt->inner_v[tot][1]= minyi+radi-veci[a][0];
 			
@@ -350,9 +365,9 @@
 	}
 	
 	/* corner right-bottom */
-	if(roundboxalign & 4) {
+	if(roundboxalign & WIDGET_BOTTOM_RIGHT) {
 		
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			wt->inner_v[tot][0]= maxxi-radi+veci[a][0];
 			wt->inner_v[tot][1]= minyi+veci[a][1];
 			
@@ -379,9 +394,9 @@
 	wt->halfwayvert= tot;
 	
 	/* corner right-top */
-	if(roundboxalign & 2) {
+	if(roundboxalign & WIDGET_TOP_RIGHT) {
 		
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			wt->inner_v[tot][0]= maxxi-veci[a][1];
 			wt->inner_v[tot][1]= maxyi-radi+veci[a][0];
 			
@@ -406,9 +421,9 @@
 	}
 	
 	/* corner left-top */
-	if(roundboxalign & 1) {
+	if(roundboxalign & WIDGET_TOP_LEFT) {
 		
-		for(a=0; a<9; a++, tot++) {
+		for(a=0; a < WIDGET_CURVE_RESOLU; a++, tot++) {
 			wt->inner_v[tot][0]= minxi+radi-veci[a][0];
 			wt->inner_v[tot][1]= maxyi-veci[a][1];
 			
@@ -433,7 +448,9 @@
 		
 		tot++;
 	}
-		
+
+	BLI_assert(tot <= WIDGET_SIZE_MAX);
+
 	wt->totvert= tot;
 }
 
@@ -1620,14 +1637,14 @@
 		rect1.ymax -= 2.0f*radout;
 	
 	/* inner part */
-	tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & 12, 0.0f);
+	tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & (WIDGET_BOTTOM_RIGHT | WIDGET_BOTTOM_LEFT), 0.0f);
 	
 	/* inverse linear shadow alpha */
 	alpha= 0.15;
 	alphastep= 0.67;
 	
 	for(step= 1; step<=radout; step++, alpha*=alphastep) {
-		round_box_shadow_edges(wtb.outer_v, &rect1, radin, 15, (float)step);
+		round_box_shadow_edges(wtb.outer_v, &rect1, radin, WIDGET_ALL_CORNERS, (float)step);
 		
 		glColor4f(0.0f, 0.0f, 0.0f, alpha);
 		
@@ -1644,7 +1661,7 @@
 static void widget_menu_back(uiWidgetColors *wcol, rcti *rect, int flag, int direction)
 {
 	uiWidgetBase wtb;
-	int roundboxalign= 15;
+	int roundboxalign= WIDGET_ALL_CORNERS;
 	
 	widget_init(&wtb);
 	
@@ -1654,11 +1671,11 @@
 		//rect->ymax += 4.0;
 	}
 	else if (direction == UI_DOWN) {
-		roundboxalign= 12;
+		roundboxalign= (WIDGET_BOTTOM_RIGHT | WIDGET_BOTTOM_LEFT);
 		rect->ymin -= 4.0;
 	} 
 	else if (direction == UI_TOP) {
-		roundboxalign= 3;
+		roundboxalign= WIDGET_TOP_LEFT | WIDGET_TOP_RIGHT;
 		rect->ymax += 4.0;
 	}
 	
@@ -2008,7 +2025,7 @@
 	widget_init(&wtb);
 	
 	/* fully rounded */
-	round_box_edges(&wtb, 15, rect, rad);
+	round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, rad);
 	
 	/* setup temp colors */
 	wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
@@ -2148,7 +2165,7 @@
 	if(horizontal)
 		SWAP(short, wcol->shadetop, wcol->shadedown);
 	
-	round_box_edges(&wtb, 15, rect, rad); 
+	round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, rad);
 	widgetbase_draw(&wtb, wcol);
 	
 	/* slider */
@@ -2176,7 +2193,7 @@
 		if (state & UI_SCROLL_NO_OUTLINE)	
 			SWAP(short, outline, wtb.outline);
 		
-		round_box_edges(&wtb, 15, slider, rad); 
+		round_box_edges(&wtb, WIDGET_ALL_CORNERS, slider, rad);
 		
 		if(state & UI_SCROLL_ARROWS) {
 			if(wcol->item[0] > 48) wcol->item[0]-= 48;
@@ -2343,7 +2360,7 @@
 		
 		/* left part of slider, always rounded */
 		rect1.xmax= rect1.xmin + ceil(offs+1.0f);
-		round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
+		round_box_edges(&wtb1, roundboxalign & ~(WIDGET_TOP_RIGHT | WIDGET_BOTTOM_RIGHT), &rect1, offs);
 		wtb1.outline= 0;
 		widgetbase_draw(&wtb1, wcol);
 		
@@ -2354,7 +2371,7 @@
 			offs*= (rect1.xmax + offs - rect->xmax)/offs;
 		else 
 			offs= 0.0f;
-		round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
+		round_box_edges(&wtb1, roundboxalign & ~(WIDGET_TOP_LEFT | WIDGET_BOTTOM_LEFT), &rect1, offs);
 		
 		widgetbase_draw(&wtb1, wcol);
 		VECCOPY(wcol->outline, outline);
@@ -2436,7 +2453,7 @@
 		wtb.outline= 0;
 		
 		/* rounded */
-		round_box_edges(&wtb, 15, rect, 10.0f);
+		round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, 10.0f);
 		widgetbase_draw(&wtb, wcol);
 	}	
 }
@@ -2499,7 +2516,7 @@
 		widget_init(&wtb);
 		
 		/* half rounded */
-		round_box_edges(&wtb, 15, rect, rad);
+		round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, rad);
 		
 		widgetbase_draw(&wtb, wcol);
 	}
@@ -2526,7 +2543,7 @@
 	
 	/* rounded, but no outline */
 	wtb.outline= 0;
-	round_box_edges(&wtb, 15, rect, 4.0f);
+	round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, 4.0f);
 	
 	widgetbase_draw(&wtb, wcol);
 }
@@ -2550,7 +2567,7 @@
 	recttemp.ymax-= delta;
 	
 	/* half rounded */
-	round_box_edges(&wtb, 15, &recttemp, 4.0f);
+	round_box_edges(&wtb, WIDGET_ALL_CORNERS, &recttemp, 4.0f);
 	
 	/* decoration */
 	if(state & UI_SELECT) {
@@ -2650,12 +2667,12 @@
 		UI_GetThemeColor3ubv(TH_BACK, col);
 		glColor3ubv(col);
 		
-		round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
+		round_box__edges(&wtb, WIDGET_ALL_CORNERS, rect, 0.0f, 4.0);
 		widgetbase_outline(&wtb);
 	}
 	
 	/* outline */
-	round_box_edges(&wtb, 15, rect, 5.0f);
+	round_box_edges(&wtb, WIDGET_ALL_CORNERS, rect, 5.0f);
 	wtb.outline= 1;
 	wtb.inner= 0;
 	widgetbase_draw(&wtb, &wt->wcol);
@@ -2824,7 +2841,7 @@
 }
 
 
-static int widget_roundbox_set(uiBut *but, rcti *rect)
+static int WIDGET_set(uiBut *but, rcti *rect)
 {
 	/* alignment */

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list