[Bf-blender-cvs] [53d9ddf772b] blender2.8: UI: Color Ramp: Add menu and functions to distribute color stops

Charlie Jolly noreply at git.blender.org
Tue Nov 20 18:02:55 CET 2018


Commit: 53d9ddf772be064bcde132b1f79ed2239f74b741
Author: Charlie Jolly
Date:   Mon Nov 19 16:15:05 2018 +0000
Branches: blender2.8
https://developer.blender.org/rB53d9ddf772be064bcde132b1f79ed2239f74b741

UI: Color Ramp: Add menu and functions to distribute color stops

Suggestion from Right-Click Select: https://blender.community/c/rightclickselect/Npcbbc/color-ramp-flags-auto-distribution-function

Differential Revision: https://developer.blender.org/D3965

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

M	source/blender/blenkernel/intern/colorband.c
M	source/blender/editors/interface/interface_templates.c

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

diff --git a/source/blender/blenkernel/intern/colorband.c b/source/blender/blenkernel/intern/colorband.c
index 38631c76009..7fc1124a422 100644
--- a/source/blender/blenkernel/intern/colorband.c
+++ b/source/blender/blenkernel/intern/colorband.c
@@ -78,7 +78,9 @@ void BKE_colorband_init(ColorBand *coba, bool rangetype)
 	}
 
 	coba->tot = 2;
+	coba->cur = 0;
 	coba->color_mode = COLBAND_BLEND_RGB;
+	coba->ipotype = COLBAND_INTERP_LINEAR;
 }
 
 static void colorband_init_from_table_rgba_simple(
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 5bdf8f45832..1d9a2433217 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -2295,6 +2295,105 @@ static void rna_update_cb(bContext *C, void *arg_cb, void *UNUSED(arg))
 	RNA_property_update(C, &cb->ptr, cb->prop);
 }
 
+enum {
+	CB_FUNC_FLIP,
+	CB_FUNC_DISTRIBUTE_LR,
+	CB_FUNC_DISTRIBUTE_EVENLY,
+	CB_FUNC_RESET,
+};
+
+static void colorband_flip_cb(bContext *C, ColorBand *coba)
+{
+	CBData data_tmp[MAXCOLORBAND];
+
+	int a;
+
+	for (a = 0; a < coba->tot; a++) {
+		data_tmp[a] = coba->data[coba->tot - (a + 1)];
+	}
+	for (a = 0; a < coba->tot; a++) {
+		data_tmp[a].pos = 1.0f - data_tmp[a].pos;
+		coba->data[a] = data_tmp[a];
+	}
+
+	/* may as well flip the cur*/
+	coba->cur = coba->tot - (coba->cur + 1);
+
+	ED_undo_push(C, "Flip Color Ramp");
+}
+
+static void colorband_distribute_cb(bContext *C, ColorBand *coba, bool evenly)
+{
+	if (coba->tot > 1) {
+		int a;
+		int tot = evenly ? coba->tot - 1 : coba->tot;
+		float gap = 1.0f / tot;
+		float pos = 0.0f;
+		for (a = 0; a < coba->tot; a++) {
+			coba->data[a].pos = pos;
+			pos += gap;
+		}
+		ED_undo_push(C, evenly ? "Distribute Stops Evenly" : "Distribute Stops from Left");
+	}
+}
+
+static void colorband_tools_dofunc(bContext *C, void *coba_v, int event)
+{
+	ColorBand *coba = coba_v;
+
+	switch (event) {
+		case CB_FUNC_FLIP:
+			colorband_flip_cb(C, coba);
+			break;
+		case CB_FUNC_DISTRIBUTE_LR:
+			colorband_distribute_cb(C, coba, false);
+			break;
+		case CB_FUNC_DISTRIBUTE_EVENLY:
+			colorband_distribute_cb(C, coba, true);
+			break;
+		case CB_FUNC_RESET:
+			BKE_colorband_init(coba, true);
+			ED_undo_push(C, "Reset Color Ramp");
+			break;
+	}
+	ED_region_tag_redraw(CTX_wm_region(C));
+}
+
+static uiBlock *colorband_tools_func(
+	bContext *C, ARegion *ar, ColorBand *coba)
+{
+	uiBut *bt;
+	uiBlock *block;
+	short yco = 0, menuwidth = 10 * UI_UNIT_X;
+
+	block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
+	UI_block_func_butmenu_set(block, colorband_tools_dofunc, coba);
+	
+	{
+		uiDefIconTextBut(
+			block, UI_BTYPE_BUT_MENU, 1, ICON_BLANK1, IFACE_("Flip Color Ramp"),
+			0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, CB_FUNC_FLIP, "");
+		uiDefIconTextBut(
+			block, UI_BTYPE_BUT_MENU, 1, ICON_BLANK1, IFACE_("Distribute Stops from Left"),
+			0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, CB_FUNC_DISTRIBUTE_LR, "");
+		uiDefIconTextBut(
+			block, UI_BTYPE_BUT_MENU, 1, ICON_BLANK1, IFACE_("Distribute Stops Evenly"),
+			0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, CB_FUNC_DISTRIBUTE_EVENLY, "");
+		bt = uiDefIconTextButO(
+			block, UI_BTYPE_BUT_MENU, "UI_OT_eyedropper_colorband", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, IFACE_("Eyedropper"),
+			0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, "");
+			bt->custom_data = coba;
+		uiDefIconTextBut(
+			block, UI_BTYPE_BUT_MENU, 1, ICON_BLANK1, IFACE_("Reset Color Ramp"),
+			0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, CB_FUNC_RESET, "");
+	}
+	
+	UI_block_direction_set(block, UI_DIR_DOWN);
+	UI_block_bounds_set_text(block, 3.0f * UI_UNIT_X);
+
+	return block;
+}
+
 static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
 {
 	ColorBand *coba = coba_v;
@@ -2307,7 +2406,7 @@ static void colorband_add_cb(bContext *C, void *cb_v, void *coba_v)
 
 	if (BKE_colorband_element_add(coba, pos)) {
 		rna_update_cb(C, cb_v, NULL);
-		ED_undo_push(C, "Add colorband");
+		ED_undo_push(C, "Add Color Ramp Stop");
 	}
 }
 
@@ -2316,34 +2415,11 @@ static void colorband_del_cb(bContext *C, void *cb_v, void *coba_v)
 	ColorBand *coba = coba_v;
 
 	if (BKE_colorband_element_remove(coba, coba->cur)) {
-		ED_undo_push(C, "Delete colorband");
+		ED_undo_push(C, "Delete Color Ramp Stop");
 		rna_update_cb(C, cb_v, NULL);
 	}
 }
 
-static void colorband_flip_cb(bContext *C, void *cb_v, void *coba_v)
-{
-	CBData data_tmp[MAXCOLORBAND];
-
-	ColorBand *coba = coba_v;
-	int a;
-
-	for (a = 0; a < coba->tot; a++) {
-		data_tmp[a] = coba->data[coba->tot - (a + 1)];
-	}
-	for (a = 0; a < coba->tot; a++) {
-		data_tmp[a].pos = 1.0f - data_tmp[a].pos;
-		coba->data[a] = data_tmp[a];
-	}
-
-	/* may as well flip the cur*/
-	coba->cur = coba->tot - (coba->cur + 1);
-
-	ED_undo_push(C, "Flip colorband");
-
-	rna_update_cb(C, cb_v, NULL);
-}
-
 static void colorband_update_cb(bContext *UNUSED(C), void *bt_v, void *coba_v)
 {
 	uiBut *bt = bt_v;
@@ -2376,8 +2452,7 @@ static void colorband_buttons_layout(
 
 	bt = uiDefIconTextBut(
 	        block, UI_BTYPE_BUT, 0, ICON_ADD, "", 0, 0, 2.0f * unit, UI_UNIT_Y, NULL,
-	        0, 0, 0, 0, TIP_("Add a new color stop to the colorband"));
-
+	        0, 0, 0, 0, TIP_("Add a new color stop to the color ramp"));
 	UI_but_funcN_set(bt, colorband_add_cb, MEM_dupallocN(cb), coba);
 
 	bt = uiDefIconTextBut(
@@ -2385,14 +2460,10 @@ static void colorband_buttons_layout(
 	        NULL, 0, 0, 0, 0, TIP_("Delete the active position"));
 	UI_but_funcN_set(bt, colorband_del_cb, MEM_dupallocN(cb), coba);
 
-	bt = uiDefIconTextBut(
-	        block, UI_BTYPE_BUT, 0, ICON_ARROW_LEFTRIGHT, "", xs + 4.0f * unit, ys + UI_UNIT_Y, 2.0f * unit, UI_UNIT_Y,
-	        NULL, 0, 0, 0, 0, TIP_("Flip the color ramp"));
-	UI_but_funcN_set(bt, colorband_flip_cb, MEM_dupallocN(cb), coba);
-
-	bt = uiDefIconButO(block, UI_BTYPE_BUT, "UI_OT_eyedropper_colorband", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, xs + 6.0f * unit, ys + UI_UNIT_Y, UI_UNIT_X, UI_UNIT_Y, NULL);
-	bt->custom_data = coba;
-	bt->func_argN = MEM_dupallocN(cb);
+	bt = uiDefIconBlockBut(
+	        block, colorband_tools_func, coba, 0, ICON_DOWNARROW_HLT,
+	        xs + 4.0f * unit, ys + UI_UNIT_Y, 2.0f * unit, UI_UNIT_Y, TIP_("Tools"));
+	UI_but_funcN_set(bt, rna_update_cb, MEM_dupallocN(cb), coba);
 
 	UI_block_align_end(block);
 	UI_block_emboss_set(block, UI_EMBOSS);
@@ -2921,8 +2992,8 @@ static uiBlock *curvemap_tools_func(
 		        0, yco -= UI_UNIT_Y, menuwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, reset_mode, "");
 	}
 
-	UI_block_direction_set(block, UI_DIR_RIGHT);
-	UI_block_bounds_set_text(block, 50);
+	UI_block_direction_set(block, UI_DIR_DOWN);
+	UI_block_bounds_set_text(block, 3.0f * UI_UNIT_X);
 
 	return block;
 }



More information about the Bf-blender-cvs mailing list