diff options
author | Julian Eisel <julian@blender.org> | 2020-08-07 16:02:07 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2020-08-07 16:17:52 +0300 |
commit | 68c00e6f8f2612d76f95fb7744cb9cfbfcfacdf8 (patch) | |
tree | 8785ad94ffea2c266ce662b1fcf3f14a9f9fdc3f /source/blender | |
parent | 49f088e2d0936ed3b5f08881a14ad83c19951791 (diff) |
UI Code Quality: Use derived struct for color buttons
For the main rationale behind this design, see 03b122e2a18df. Further,
this removes users of `uiBut.a1`/`uiBut.a2`, which is a very ugly design
choice (hard to reason about).
Part of Part of T74432.
Diffstat (limited to 'source/blender')
7 files changed, 50 insertions, 41 deletions
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h index 4a98d72a463..e8352bc6b21 100644 --- a/source/blender/editors/include/UI_interface.h +++ b/source/blender/editors/include/UI_interface.h @@ -390,8 +390,6 @@ enum { UI_GRAD_L_ALT = 10, }; -#define UI_PALETTE_COLOR 20 - /* Drawing * * Functions to draw various shapes, taking theme settings into account. diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 9a4eb73c20e..0d8f682ea51 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -2329,7 +2329,8 @@ bool ui_but_supports_cycling(const uiBut *but) { return ((ELEM(but->type, UI_BTYPE_ROW, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER, UI_BTYPE_LISTBOX)) || (but->type == UI_BTYPE_MENU && ui_but_menu_step_poll(but)) || - (but->type == UI_BTYPE_COLOR && but->a1 != -1) || (but->menu_step_func != NULL)); + (but->type == UI_BTYPE_COLOR && ((uiButColor *)but)->is_pallete_color) || + (but->menu_step_func != NULL)); } double ui_but_value_get(uiBut *but) @@ -3772,6 +3773,10 @@ static void ui_but_alloc_info(const eButType type, bool has_custom_type = true; switch (type) { + case UI_BTYPE_COLOR: + alloc_size = sizeof(uiButColor); + alloc_str = "uiButColor"; + break; case UI_BTYPE_DECORATOR: alloc_size = sizeof(uiButDecorator); alloc_str = "uiButDecorator"; diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index f55d8d5ccc6..999ddca65b9 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -5713,21 +5713,24 @@ static bool ui_numedit_but_UNITVEC( return changed; } -static void ui_palette_set_active(uiBut *but) +static void ui_palette_set_active(uiButColor *color_but) { - if ((int)(but->a1) == UI_PALETTE_COLOR) { - Palette *palette = (Palette *)but->rnapoin.owner_id; - PaletteColor *color = but->rnapoin.data; + if (color_but->is_pallete_color) { + Palette *palette = (Palette *)color_but->but.rnapoin.owner_id; + PaletteColor *color = color_but->but.rnapoin.data; palette->active_color = BLI_findindex(&palette->colors, color); } } static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, const wmEvent *event) { + BLI_assert(but->type == UI_BTYPE_COLOR); + uiButColor *color_but = (uiButColor *)but; + if (data->state == BUTTON_STATE_HIGHLIGHT) { /* first handle click on icondrag type button */ if (event->type == LEFTMOUSE && but->dragpoin && event->val == KM_PRESS) { - ui_palette_set_active(but); + ui_palette_set_active(color_but); if (ui_but_contains_point_px_icon(but, data->region, event)) { button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG); data->dragstartx = event->x; @@ -5737,7 +5740,7 @@ static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, co } #ifdef USE_DRAG_TOGGLE if (event->type == LEFTMOUSE && event->val == KM_PRESS) { - ui_palette_set_active(but); + ui_palette_set_active(color_but); button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG); data->dragstartx = event->x; data->dragstarty = event->y; @@ -5746,7 +5749,7 @@ static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, co #endif /* regular open menu */ if (ELEM(event->type, LEFTMOUSE, EVT_PADENTER, EVT_RETKEY) && event->val == KM_PRESS) { - ui_palette_set_active(but); + ui_palette_set_active(color_but); button_activate_state(C, but, BUTTON_STATE_MENU_OPEN); return WM_UI_HANDLER_BREAK; } @@ -5777,8 +5780,7 @@ static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, co ui_apply_but(C, but->block, but, data, true); return WM_UI_HANDLER_BREAK; } - if ((int)(but->a1) == UI_PALETTE_COLOR && event->type == EVT_DELKEY && - event->val == KM_PRESS) { + if (color_but->is_pallete_color && (event->type == EVT_DELKEY) && (event->val == KM_PRESS)) { Palette *palette = (Palette *)but->rnapoin.owner_id; PaletteColor *color = but->rnapoin.data; @@ -5809,7 +5811,7 @@ static int ui_do_but_COLOR(bContext *C, uiBut *but, uiHandleButtonData *data, co } if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { - if ((int)(but->a1) == UI_PALETTE_COLOR) { + if (color_but->is_pallete_color) { if (!event->ctrl) { float color[3]; Paint *paint = BKE_paint_get_active_from_context(C); @@ -7613,13 +7615,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent * retval = ui_do_but_BUT(C, but, data, event); break; case UI_BTYPE_COLOR: - if (but->a1 == -1) { - /* signal to prevent calling up color picker */ - retval = ui_do_but_EXIT(C, but, data, event); - } - else { - retval = ui_do_but_COLOR(C, but, data, event); - } + retval = ui_do_but_COLOR(C, but, data, event); break; case UI_BTYPE_UNITVEC: retval = ui_do_but_UNITVEC(C, block, but, data, event); diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h index 0dfa23a4332..9840444c0e0 100644 --- a/source/blender/editors/interface/interface_intern.h +++ b/source/blender/editors/interface/interface_intern.h @@ -177,7 +177,6 @@ struct uiBut { * - UI_BTYPE_LABEL: Use `(a1 == 1.0f)` to use a2 as a blending factor (imaginative!). * - UI_BTYPE_SCROLL: Use as scroll size. * - UI_BTYPE_SEARCH_MENU: Use as number or rows. - * - UI_BTYPE_COLOR: Use as indication of color palette. * - UI_BTYPE_PROGRESS_BAR: Use to store progress (0..1). */ float a1; @@ -188,7 +187,6 @@ struct uiBut { * - UI_BTYPE_NUM: Use to store RNA 'precision' value, for dragging and click-step. * - UI_BTYPE_LABEL: If `(a1 == 1.0f)` use a2 as a blending factor. * - UI_BTYPE_SEARCH_MENU: Use as number or columns. - * - UI_BTYPE_COLOR: Use as index in palette (not so good, needs refactor). */ float a2; @@ -281,6 +279,14 @@ struct uiBut { uiBlock *block; }; +/** Derived struct for #UI_BTYPE_COLOR */ +typedef struct uiButColor { + uiBut but; + + bool is_pallete_color; + int palette_color_index; +} uiButColor; + /** Derived struct for #UI_BTYPE_TAB */ typedef struct uiButTab { uiBut but; diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c index 8c1d070b5e4..8593e7e3118 100644 --- a/source/blender/editors/interface/interface_templates.c +++ b/source/blender/editors/interface/interface_templates.c @@ -5512,22 +5512,24 @@ void uiTemplatePalette(uiLayout *layout, } RNA_pointer_create(&palette->id, &RNA_PaletteColor, color, &color_ptr); - uiDefButR(block, - UI_BTYPE_COLOR, - 0, - "", - 0, - 0, - UI_UNIT_X, - UI_UNIT_Y, - &color_ptr, - "color", - -1, - 0.0, - 1.0, - UI_PALETTE_COLOR, - col_id, - ""); + uiButColor *color_but = (uiButColor *)uiDefButR(block, + UI_BTYPE_COLOR, + 0, + "", + 0, + 0, + UI_UNIT_X, + UI_UNIT_Y, + &color_ptr, + "color", + -1, + 0.0, + 1.0, + 0.0, + 0.0, + ""); + color_but->is_pallete_color = true; + color_but->palette_color_index = col_id; row_cols++; col_id++; } diff --git a/source/blender/editors/interface/interface_utils.c b/source/blender/editors/interface/interface_utils.c index 208fd7136da..4a1c7be918e 100644 --- a/source/blender/editors/interface/interface_utils.c +++ b/source/blender/editors/interface/interface_utils.c @@ -148,7 +148,7 @@ uiBut *uiDefAutoButR(uiBlock *block, if (RNA_property_array_check(prop) && index == -1) { if (ELEM(RNA_property_subtype(prop), PROP_COLOR, PROP_COLOR_GAMMA)) { but = uiDefButR_prop( - block, UI_BTYPE_COLOR, 0, name, x1, y1, x2, y2, ptr, prop, -1, 0, 0, -1, -1, NULL); + block, UI_BTYPE_COLOR, 0, name, x1, y1, x2, y2, ptr, prop, -1, 0, 0, 0, 0, NULL); } else { return NULL; diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index 01f89a8bdb1..5d342e928f9 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -3768,6 +3768,8 @@ static void widget_numslider( static void widget_swatch( uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign) { + BLI_assert(but->type == UI_BTYPE_COLOR); + uiButColor *color_but = (uiButColor *)but; uiWidgetBase wtb; float rad, col[4]; @@ -3822,8 +3824,8 @@ static void widget_swatch( } widgetbase_draw_ex(&wtb, wcol, show_alpha_checkers); - if (but->a1 == UI_PALETTE_COLOR && - ((Palette *)but->rnapoin.owner_id)->active_color == (int)but->a2) { + if (color_but->is_pallete_color && + ((Palette *)but->rnapoin.owner_id)->active_color == color_but->palette_color_index) { float width = rect->xmax - rect->xmin; float height = rect->ymax - rect->ymin; /* find color luminance and change it slightly */ |