[Bf-blender-cvs] [4e52fac] pie-menus: replace switches with lookups

Campbell Barton noreply at git.blender.org
Sat Aug 2 18:15:39 CEST 2014


Commit: 4e52fac1479db0eecf2dbcdc0764753a1b678dad
Author: Campbell Barton
Date:   Sun Aug 3 02:03:48 2014 +1000
Branches: pie-menus
https://developer.blender.org/rB4e52fac1479db0eecf2dbcdc0764753a1b678dad

replace switches with lookups

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

M	source/blender/editors/interface/interface.c
M	source/blender/editors/interface/interface_handlers.c
M	source/blender/editors/interface/interface_intern.h
M	source/blender/editors/interface/interface_layout.c

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

diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 9abe890..524cafb 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -1076,12 +1076,40 @@ static bool ui_but_event_property_operator_string(const bContext *C, uiBut *but,
 	return found;
 }
 
-const char ui_radial_dir_to_num[8] = {8, 9, 6, 3, 2, 1, 4, 7};
+/* this goes in a seemingly weird pattern:
+ *
+ *     4
+ *  5     6
+ * 1       2
+ *  7     8
+ *     3
+ *
+ * but it's actually quite logical. It's designed to be 'upwards compatible'
+ * for muscle memory so that the menu item locations are fixed and don't move
+ * as new items are added to the menu later on. It also optimises efficiency -
+ * a radial menu is best kept symmetrical, with as large an angle between
+ * items as possible, so that the gestural mouse movements can be fast and inexact.
+
+ * It starts off with two opposite sides for the first two items
+ * then joined by the one below for the third (this way, even with three items,
+ * the menu seems to still be 'in order' reading left to right). Then the fourth is
+ * added to complete the compass directions. From here, it's just a matter of
+ * subdividing the rest of the angles for the last 4 items.
+ *
+ * --Matt 07/2006
+ */
+const char ui_radial_dir_order[8] = {
+    UI_RADIAL_W,  UI_RADIAL_E,  UI_RADIAL_S,  UI_RADIAL_N,
+    UI_RADIAL_NW, UI_RADIAL_NE, UI_RADIAL_SW, UI_RADIAL_SE};
+
+const char  ui_radial_dir_to_numpad[8] = {8, 9, 6, 3, 2, 1, 4, 7};
+const short ui_radial_dir_to_angle_visual[8] = {90, 40, 0, 320, 270, 220, 180, 140};
+const short ui_radial_dir_to_angle[8] =        {90, 45, 0, 315, 270, 225, 180, 135};
 
 static void ui_pie_direction_string(uiBut *but, char *buf, int size)
 {
-	BLI_assert((unsigned int)(but->pie_dir - 1) < ARRAY_SIZE(ui_radial_dir_to_num));
-	BLI_snprintf(buf, size, "%d", ui_radial_dir_to_num[but->pie_dir - 1]);
+	BLI_assert((unsigned int)(but->pie_dir - 1) < ARRAY_SIZE(ui_radial_dir_to_numpad));
+	BLI_snprintf(buf, size, "%d", ui_radial_dir_to_numpad[but->pie_dir - 1]);
 }
 
 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c
index f129a2c..1b004ea 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -6379,89 +6379,34 @@ static bool ui_but_contains_pt(uiBut *but, float mx, float my)
 	return BLI_rctf_isect_pt(&but->rect, mx, my);
 }
 
-static void ui_but_pie_visual_dir(RadialDirection dir, float vec[2]) {
+static void ui_but_pie_dir__internal(RadialDirection dir, float vec[2], const short angles[8])
+{
 	float angle;
 
-	switch (dir) {
-		case UI_RADIAL_W:
-			angle = 180.0f;
-			break;
-		case UI_RADIAL_E:
-			angle = 0.0f;
-			break;
-		case UI_RADIAL_S:
-			angle = 270.0f;
-			break;
-		case UI_RADIAL_N:
-			angle = 90.0f;
-			break;
-		case UI_RADIAL_NW:
-			angle = 140.0f;
-			break;
-		case UI_RADIAL_NE:
-			angle = 40.0f;
-			break;
-		case UI_RADIAL_SW:
-			angle = 220.0f;
-			break;
-		case UI_RADIAL_SE:
-			angle = 320.0f;
-			break;
-		default:
-			angle = 0.0f;
-			break;
-	}
+	BLI_assert(dir != UI_RADIAL_NONE);
 
-	angle = DEG2RADF(angle);
+	angle = DEG2RADF((float)angles[dir]);
 	vec[0] = cosf(angle);
 	vec[1] = sinf(angle);
 }
+void ui_but_pie_dir_visual(RadialDirection dir, float vec[2])
+{
+	ui_but_pie_dir__internal(dir, vec, ui_radial_dir_to_angle_visual);
+}
+void ui_but_pie_dir(RadialDirection dir, float vec[2])
+{
+	ui_but_pie_dir__internal(dir, vec, ui_radial_dir_to_angle);
+}
 
 static bool ui_but_isect_pie_seg(uiBlock *block, uiBut *but)
 {
 	const float angle_range = (block->pie_data.flags & UI_PIE_DEGREES_RANGE_LARGE) ? M_PI_4 : M_PI_4 / 2.0;
-	float angle_pie;
 	float vec[2];
 
 	if (block->pie_data.flags & UI_PIE_INVALID_DIR)
 		return false;
 
-	switch (but->pie_dir) {
-		case UI_RADIAL_E:
-			angle_pie = 0.0;
-			break;
-
-		case UI_RADIAL_NE:
-			angle_pie = M_PI_4;
-			break;
-
-		case UI_RADIAL_N:
-			angle_pie = M_PI_2;
-			break;
-
-		case UI_RADIAL_NW:
-			angle_pie = M_PI_2 + M_PI_4;
-			break;
-
-		case UI_RADIAL_W:
-			angle_pie = M_PI;
-			break;
-
-		case UI_RADIAL_SW:
-			angle_pie = M_PI + M_PI_4;
-			break;
-
-		case UI_RADIAL_S:
-			angle_pie = 3 * M_PI_2;
-			break;
-
-		case UI_RADIAL_SE:
-			angle_pie = 3 * M_PI_2 + M_PI_4;
-			break;
-	}
-
-	vec[0] = cosf(angle_pie);
-	vec[1] = sinf(angle_pie);
+	ui_but_pie_dir(but->pie_dir, vec);
 
 	if (saacos(dot_v2v2(vec, block->pie_data.pie_dir)) < angle_range)
 		return true;
@@ -8661,7 +8606,7 @@ static int ui_handler_pie(bContext *C, const wmEvent *event, uiPopupBlockHandle
 					if (but->pie_dir) {
 						float dir[2];
 
-						ui_but_pie_visual_dir(but->pie_dir, dir);
+						ui_but_pie_dir_visual(but->pie_dir, dir);
 
 						mul_v2_fl(dir, pie_radius );
 						add_v2_v2(dir, block->pie_data.pie_center_spawned);
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index 73b48e3..e81d427 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -135,7 +135,10 @@ typedef enum RadialDirection {
 	UI_RADIAL_NW = 8,
 } RadialDirection;
 
-extern const char ui_radial_dir_to_num[8];
+extern const char  ui_radial_dir_order[8];
+extern const char  ui_radial_dir_to_numpad[8];
+extern const short ui_radial_dir_to_angle_visual[8];
+extern const short ui_radial_dir_to_angle[8];
 
 /* internal panel drawing defines */
 #define PNL_GRID    (UI_UNIT_Y / 5) /* 4 default */
@@ -599,6 +602,8 @@ extern bool ui_button_is_active(struct ARegion *ar) ATTR_WARN_UNUSED_RESULT;
 extern int ui_button_open_menu_direction(uiBut *but);
 extern void ui_button_text_password_hide(char password_str[UI_MAX_DRAW_STR], uiBut *but, const bool restore);
 extern uiBut *ui_but_find_activated(struct ARegion *ar);
+void ui_but_pie_dir_visual(RadialDirection dir, float vec[2]);
+void ui_but_pie_dir(RadialDirection dir, float vec[2]);
 void ui_block_calculate_pie_segment(struct uiBlock *block, const float event_xy[2]);
 
 
diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c
index fbe5191..604942b 100644
--- a/source/blender/editors/interface/interface_layout.c
+++ b/source/blender/editors/interface/interface_layout.c
@@ -2114,75 +2114,11 @@ static void ui_litem_layout_column(uiLayout *litem)
  * stores a float vector in unit circle */
 static RadialDirection ui_get_radialbut_vec(float vec[2], short itemnum)
 {
-	float angle = 0.0f;
-	RadialDirection dir = UI_RADIAL_NONE;
-
-	/* this goes in a seemingly weird pattern:
-	 *
-	 *     4
-	 *  5     6
-	 * 1       2
-	 *  7     8
-	 *     3
-	 *
-	 * but it's actually quite logical. It's designed to be 'upwards compatible'
-	 * for muscle memory so that the menu item locations are fixed and don't move
-	 * as new items are added to the menu later on. It also optimises efficiency -
-	 * a radial menu is best kept symmetrical, with as large an angle between
-	 * items as possible, so that the gestural mouse movements can be fast and inexact.
-
-	 * It starts off with two opposite sides for the first two items
-	 * then joined by the one below for the third (this way, even with three items,
-	 * the menu seems to still be 'in order' reading left to right). Then the fourth is
-	 * added to complete the compass directions. From here, it's just a matter of
-	 * subdividing the rest of the angles for the last 4 items.
-	 *
-	 * --Matt 07/2006
-	 */
+	RadialDirection dir;
+	BLI_assert(itemnum < 8);
 
-	/* if (itemnum < 5) { */
-	switch (itemnum) {
-		case 1:
-			dir = UI_RADIAL_W;
-			angle = 180.0f;
-			break;
-		case 2:
-			dir = UI_RADIAL_E;
-			angle = 0.0f;
-			break;
-		case 3:
-			dir = UI_RADIAL_S;
-			angle = 270.0f;
-			break;
-		case 4:
-			dir = UI_RADIAL_N;
-			angle = 90.0f;
-			break;
-		case 5:
-			dir = UI_RADIAL_NW;
-			angle = 140;
-			break;
-		case 6:
-			dir = UI_RADIAL_NE;
-			angle = 40;
-			break;
-		case 7:
-			dir = UI_RADIAL_SW;
-			angle = 220;
-			break;
-		case 8:
-			dir = UI_RADIAL_SE;
-			angle = 320;
-			break;
-
-		default:
-			break;
-	}
-
-	angle = DEG2RADF(angle);
-
-	vec[0] = cosf(angle);
-	vec[1] = sinf(angle);
+	dir = ui_radial_dir_order[itemnum];
+	ui_but_pie_dir_visual(dir, vec);
 
 	return dir;
 }
@@ -2241,10 +2177,10 @@ static void ui_litem_layout_radial(uiLayout *litem)
 			RadialDirection dir;
 			float vec[2];
 
-			itemnum++;
-
 			dir = ui_get_radialbut_vec(vec, itemnum);
 
+			itemnum++;
+
 			if (item->type == ITEM_BUTTON) {
 				uiButtonItem *bitem = (uiButtonItem *) item;




More information about the Bf-blender-cvs mailing list