UI: refactor color picker flags out of buttons

These are specialized color picker options which don't need to be
stored in the button (frees of flags for buttons too).
This commit is contained in:
Campbell Barton 2019-03-22 14:01:11 +11:00
parent 00f7dcd5be
commit 27da43ba86
5 changed files with 42 additions and 33 deletions

View File

@ -181,7 +181,7 @@ enum {
UI_BUT_DRAG_LOCK = 1 << 10,
/** grayed out and uneditable */
UI_BUT_DISABLED = 1 << 11,
UI_BUT_COLOR_LOCK = 1 << 12,
UI_BUT_ANIMATED = 1 << 13,
UI_BUT_ANIMATED_KEY = 1 << 14,
UI_BUT_DRIVEN = 1 << 15,
@ -193,10 +193,6 @@ enum {
UI_BUT_IMMEDIATE = 1 << 20,
UI_BUT_NO_UTF8 = 1 << 21,
/** used to flag if color hsv-circle should keep luminance */
UI_BUT_VEC_SIZE_LOCK = 1 << 22,
/** cubic saturation for the color wheel */
UI_BUT_COLOR_CUBIC = 1 << 23,
/** This but is "inside" a list item (currently used to change theme colors). */
UI_BUT_LIST_ITEM = 1 << 24,
/** edit this button as well as the active button (not just dragging) */

View File

@ -6130,7 +6130,7 @@ static bool ui_numedit_but_HSVCIRCLE(
/* exception, when using color wheel in 'locked' value state:
* allow choosing a hue for black values, by giving a tiny increment */
if (but->flag & UI_BUT_COLOR_LOCK) {
if (cpicker->use_color_lock) {
if (U.color_picker_type == USER_CP_CIRCLE_HSV) { // lock
if (hsv[2] == 0.f) hsv[2] = 0.0001f;
}
@ -6151,7 +6151,7 @@ static bool ui_numedit_but_HSVCIRCLE(
ui_rgb_to_color_picker_compat_v(rgbo, hsvo);
/* and original position */
ui_hsvcircle_pos_from_vals(but, &rect, hsvo, &xpos, &ypos);
ui_hsvcircle_pos_from_vals(cpicker, &rect, hsvo, &xpos, &ypos);
mx_fl = xpos - (data->dragstartx - mx_fl);
my_fl = ypos - (data->dragstarty - my_fl);
@ -6160,8 +6160,9 @@ static bool ui_numedit_but_HSVCIRCLE(
ui_hsvcircle_vals_from_pos(hsv, hsv + 1, &rect, mx_fl, my_fl);
if ((but->flag & UI_BUT_COLOR_CUBIC) && (U.color_picker_type == USER_CP_CIRCLE_HSV))
if ((cpicker->use_color_cubic) && (U.color_picker_type == USER_CP_CIRCLE_HSV)) {
hsv[1] = 1.0f - sqrt3f(1.0f - hsv[1]);
}
if (snap != SNAP_OFF) {
ui_color_snap_hue(snap, &hsv[0]);
@ -6169,8 +6170,10 @@ static bool ui_numedit_but_HSVCIRCLE(
ui_color_picker_to_rgb_v(hsv, rgb);
if ((but->flag & UI_BUT_VEC_SIZE_LOCK) && (rgb[0] || rgb[1] || rgb[2])) {
normalize_v3_length(rgb, but->a2);
if ((cpicker->use_luminosity_lock)) {
if (!is_zero_v3(rgb)) {
normalize_v3_length(rgb, cpicker->luminosity_lock_value);
}
}
ui_color_picker_to_scene_linear_space(but, rgb);
@ -6224,7 +6227,7 @@ static void ui_ndofedit_but_HSVCIRCLE(
/* exception, when using color wheel in 'locked' value state:
* allow choosing a hue for black values, by giving a tiny increment */
if (but->flag & UI_BUT_COLOR_LOCK) {
if (cpicker->use_color_lock) {
if (U.color_picker_type == USER_CP_CIRCLE_HSV) { // lock
if (hsv[2] == 0.f) hsv[2] = 0.0001f;
}
@ -6242,8 +6245,10 @@ static void ui_ndofedit_but_HSVCIRCLE(
ui_color_picker_to_rgb_v(hsv, data->vec);
if ((but->flag & UI_BUT_VEC_SIZE_LOCK) && (data->vec[0] || data->vec[1] || data->vec[2])) {
normalize_v3_length(data->vec, but->a2);
if (cpicker->use_luminosity_lock) {
if (!is_zero_v3(data->vec)) {
normalize_v3_length(data->vec, cpicker->luminosity_lock_value);
}
}
ui_color_picker_to_scene_linear_space(but, data->vec);

View File

@ -341,6 +341,11 @@ typedef struct ColorPicker {
/** Initial color data (detect changes). */
float color_data_init[3];
bool is_init;
/** Cubic saturation for the color wheel. */
bool use_color_cubic;
bool use_color_lock;
bool use_luminosity_lock;
float luminosity_lock_value;
} ColorPicker;
typedef struct ColorPickerData {
@ -506,8 +511,8 @@ extern void ui_but_v3_set(uiBut *but, const float vec[3]);
extern void ui_hsvcircle_vals_from_pos(
float *val_rad, float *val_dist, const rcti *rect,
const float mx, const float my);
extern void ui_hsvcircle_pos_from_vals(struct uiBut *but, const rcti *rect, float *hsv, float *xpos, float *ypos);
extern void ui_hsvcube_pos_from_vals(struct uiBut *but, const rcti *rect, float *hsv, float *xp, float *yp);
extern void ui_hsvcircle_pos_from_vals(const ColorPicker *cpicker, const rcti *rect, const float *hsv, float *xpos, float *ypos);
extern void ui_hsvcube_pos_from_vals(const struct uiBut *but, const rcti *rect, const float *hsv, float *xp, float *yp);
extern void ui_but_string_get_ex(
uiBut *but, char *str, const size_t maxlen,

View File

@ -3347,20 +3347,17 @@ void uiTemplateColorPicker(
but->custom_data = cpicker;
if (lock) {
but->flag |= UI_BUT_COLOR_LOCK;
}
cpicker->use_color_lock = lock;
cpicker->use_color_cubic = cubic;
cpicker->use_luminosity_lock = lock_luminosity;
if (lock_luminosity) {
float color[4]; /* in case of alpha */
but->flag |= UI_BUT_VEC_SIZE_LOCK;
RNA_property_float_get_array(ptr, prop, color);
but->a2 = len_v3(color);
cpicker->luminosity_lock_value = len_v3(color);
}
if (cubic)
but->flag |= UI_BUT_COLOR_CUBIC;
if (value_slider) {
switch (U.color_picker_type) {

View File

@ -2575,7 +2575,9 @@ void ui_hsvcircle_vals_from_pos(
}
/* cursor in hsv circle, in float units -1 to 1, to map on radius */
void ui_hsvcircle_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float *xpos, float *ypos)
void ui_hsvcircle_pos_from_vals(
const ColorPicker *cpicker, const rcti *rect, const float *hsv,
float *r_xpos, float *r_ypos)
{
/* duplication of code... well, simple is better now */
const float centx = BLI_rcti_cent_x_fl(rect);
@ -2585,14 +2587,14 @@ void ui_hsvcircle_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float
ang = 2.0f * (float)M_PI * hsv[0] + (float)M_PI_2;
if ((but->flag & UI_BUT_COLOR_CUBIC) && (U.color_picker_type == USER_CP_CIRCLE_HSV))
if (cpicker->use_color_cubic && (U.color_picker_type == USER_CP_CIRCLE_HSV))
radius_t = (1.0f - pow3f(1.0f - hsv[1]));
else
radius_t = hsv[1];
radius = clamp_f(radius_t, 0.0f, 1.0f) * radius;
*xpos = centx + cosf(-ang) * radius;
*ypos = centy + sinf(-ang) * radius;
*r_xpos = centx + cosf(-ang) * radius;
*r_ypos = centy + sinf(-ang) * radius;
}
static void ui_draw_but_HSVCIRCLE(uiBut *but, const uiWidgetColors *wcol, const rcti *rect)
@ -2622,11 +2624,13 @@ static void ui_draw_but_HSVCIRCLE(uiBut *but, const uiWidgetColors *wcol, const
/* exception: if 'lock' is set
* lock the value of the color wheel to 1.
* Useful for color correction tools where you're only interested in hue. */
if (but->flag & UI_BUT_COLOR_LOCK) {
if (U.color_picker_type == USER_CP_CIRCLE_HSV)
if (cpicker->use_color_lock) {
if (U.color_picker_type == USER_CP_CIRCLE_HSV) {
hsv[2] = 1.0f;
else
}
else {
hsv[2] = 0.5f;
}
}
const float hsv_center[3] = {0.0f, 0.0f, hsv[2]};
@ -2694,7 +2698,7 @@ static void ui_draw_but_HSVCIRCLE(uiBut *but, const uiWidgetColors *wcol, const
ui_rgb_to_color_picker_compat_v(rgb, hsv);
float xpos, ypos;
ui_hsvcircle_pos_from_vals(but, rect, hsv, &xpos, &ypos);
ui_hsvcircle_pos_from_vals(cpicker, rect, hsv, &xpos, &ypos);
ui_hsv_cursor(xpos, ypos);
}
@ -2851,7 +2855,9 @@ void ui_draw_gradient(const rcti *rect, const float hsv[3], const int type, cons
immUnbindProgram();
}
void ui_hsvcube_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float *xp, float *yp)
void ui_hsvcube_pos_from_vals(
const uiBut *but, const rcti *rect, const float *hsv,
float *r_xp, float *r_yp)
{
float x = 0.0f, y = 0.0f;
@ -2881,8 +2887,8 @@ void ui_hsvcube_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float *x
}
/* cursor */
*xp = rect->xmin + x * BLI_rcti_size_x(rect);
*yp = rect->ymin + y * BLI_rcti_size_y(rect);
*r_xp = rect->xmin + x * BLI_rcti_size_x(rect);
*r_yp = rect->ymin + y * BLI_rcti_size_y(rect);
}
static void ui_draw_but_HSVCUBE(uiBut *but, const rcti *rect)