diff options
author | Julian Eisel <julian@blender.org> | 2020-09-04 22:18:45 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2020-09-04 22:26:31 +0300 |
commit | e6f0b60c2e9111af1878d31dcc295c59ed1bea77 (patch) | |
tree | 4df10fc869f5f512c233a2755246c1749bafc1c7 /source/blender/editors/interface/interface.c | |
parent | 47881791be0538f9558b2ef091e1761187df250d (diff) |
UI Code Quality: Use derived struct for number buttons
For the man rationale behind this design, see 49f088e2d093. Further,
this removes users of uiBut.a1/uiBut.a2, which is a very ugly design
choice (hard to reason about).
Note that I had to do rather ugly, specific exceptions for the number
buttons in `ui_def_but_rna()`. But once all users of a1/a2 are removed,
this special handling shouldn't be needed anymore.
I also had to move a sanity check out of the button definition. It's now
moved into a new debug only sanity checking function executed when
finishing the layout definition (block end).
Diffstat (limited to 'source/blender/editors/interface/interface.c')
-rw-r--r-- | source/blender/editors/interface/interface.c | 90 |
1 files changed, 74 insertions, 16 deletions
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index d8330d62907..bbe097b5c79 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -622,9 +622,18 @@ void UI_block_bounds_set_explicit(uiBlock *block, int minx, int miny, int maxx, block->bounds_type = UI_BLOCK_BOUNDS_NONE; } +static float ui_but_get_float_precision(uiBut *but) +{ + if (but->type == UI_BTYPE_NUM) { + return ((uiButNumber *)but)->precision; + } + + return but->a2; +} + static int ui_but_calc_float_precision(uiBut *but, double value) { - int prec = (int)but->a2; + int prec = (int)ui_but_get_float_precision(but); /* first check for various special cases: * * If button is radians, we want additional precision (see T39861). @@ -1737,6 +1746,24 @@ void UI_block_update_from_old(const bContext *C, uiBlock *block) block->oldblock = NULL; } +#ifndef NDEBUG +/** + * Extra sanity checks for invariants (debug builds only). + */ +static void ui_but_validate(const uiBut *but) +{ + /* Number buttons must have a click-step, + * assert instead of correcting the value to ensure the caller knows what they're doing. */ + if (but->type == UI_BTYPE_NUM) { + uiButNumber *number_but = (uiButNumber *)but; + + if (ELEM(but->pointype, UI_BUT_POIN_CHAR, UI_BUT_POIN_SHORT, UI_BUT_POIN_INT)) { + BLI_assert((int)number_but->step_size > 0); + } + } +} +#endif + void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_xy[2]) { wmWindow *window = CTX_wm_window(C); @@ -1778,6 +1805,10 @@ void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_x ui_but_anim_decorate_update_from_flag((uiButDecorator *)but); } ui_but_predefined_extra_operator_icons_add(but); + +#ifndef NDEBUG + ui_but_validate(but); +#endif } /* handle pending stuff */ @@ -2562,7 +2593,7 @@ static void ui_get_but_string_unit( /* Use precision override? */ if (float_precision == -1) { /* Sanity checks */ - precision = (int)but->a2; + precision = (int)ui_but_get_float_precision(but); if (precision > UI_PRECISION_FLOAT_MAX) { precision = UI_PRECISION_FLOAT_MAX; } @@ -3768,6 +3799,10 @@ static void ui_but_alloc_info(const eButType type, bool has_custom_type = true; switch (type) { + case UI_BTYPE_NUM: + alloc_size = sizeof(uiButNumber); + alloc_str = "uiButNumber"; + break; case UI_BTYPE_COLOR: alloc_size = sizeof(uiButColor); alloc_str = "uiButColor"; @@ -3914,14 +3949,6 @@ static uiBut *ui_def_but(uiBlock *block, (a1 != 0.0f && a1 != 1.0f)) == false); } - /* Number buttons must have a click-step, - * assert instead of correcting the value to ensure the caller knows what they're doing. */ - if ((type & BUTTYPE) == UI_BTYPE_NUM) { - if (ELEM((type & UI_BUT_POIN_TYPES), UI_BUT_POIN_CHAR, UI_BUT_POIN_SHORT, UI_BUT_POIN_INT)) { - BLI_assert((int)a1 > 0); - } - } - if (type & UI_BUT_POIN_TYPES) { /* a pointer is required */ if (poin == NULL) { BLI_assert(0); @@ -4360,6 +4387,7 @@ static uiBut *ui_def_but_rna(uiBlock *block, uiBut *but; int icon = 0; uiMenuCreateFunc func = NULL; + const bool always_set_a1_a2 = ELEM(type, UI_BTYPE_NUM); if (ELEM(type, UI_BTYPE_COLOR, UI_BTYPE_HSVCIRCLE, UI_BTYPE_HSVCUBE)) { BLI_assert(index == -1); @@ -4424,7 +4452,7 @@ static uiBut *ui_def_but_rna(uiBlock *block, tip = RNA_property_ui_description(prop); } - if (min == max || a1 == -1 || a2 == -1) { + if (min == max || a1 == -1 || a2 == -1 || always_set_a1_a2) { if (proptype == PROP_INT) { int hardmin, hardmax, softmin, softmax, step; @@ -4435,10 +4463,10 @@ static uiBut *ui_def_but_rna(uiBlock *block, min = hardmin; max = hardmax; } - if (a1 == -1) { + if (a1 == -1 || always_set_a1_a2) { a1 = step; } - if (a2 == -1) { + if (a2 == -1 || always_set_a1_a2) { a2 = 0; } } @@ -4452,10 +4480,10 @@ static uiBut *ui_def_but_rna(uiBlock *block, min = hardmin; max = hardmax; } - if (a1 == -1) { + if (a1 == -1 || always_set_a1_a2) { a1 = step; } - if (a2 == -1) { + if (a2 == -1 || always_set_a1_a2) { a2 = precision; } } @@ -4469,6 +4497,12 @@ static uiBut *ui_def_but_rna(uiBlock *block, /* now create button */ but = ui_def_but(block, type, retval, str, x, y, width, height, NULL, min, max, a1, a2, tip); + if (but->type == UI_BTYPE_NUM) { + /* Set default values, can be overriden later. */ + UI_but_number_step_size_set(but, a1); + UI_but_number_precision_set(but, a2); + } + but->rnapoin = *ptr; but->rnaprop = prop; @@ -4506,7 +4540,13 @@ static uiBut *ui_def_but_rna(uiBlock *block, /* If this button uses units, calculate the step from this */ if ((proptype == PROP_FLOAT) && ui_but_is_unit(but)) { - but->a1 = ui_get_but_step_unit(but, but->a1); + if (type == UI_BTYPE_NUM) { + uiButNumber *number_but = (uiButNumber *)but; + number_but->step_size = ui_get_but_step_unit(but, number_but->step_size); + } + else { + but->a1 = ui_get_but_step_unit(but, but->a1); + } } if (func) { @@ -6707,6 +6747,24 @@ void UI_but_node_link_set(uiBut *but, bNodeSocket *socket, const float draw_colo rgba_float_to_uchar(but->col, draw_color); } +void UI_but_number_step_size_set(uiBut *but, float step_size) +{ + uiButNumber *but_number = (uiButNumber *)but; + BLI_assert(but->type == UI_BTYPE_NUM); + + but_number->step_size = step_size; + BLI_assert(step_size > 0); +} + +void UI_but_number_precision_set(uiBut *but, float precision) +{ + uiButNumber *but_number = (uiButNumber *)but; + BLI_assert(but->type == UI_BTYPE_NUM); + + but_number->precision = precision; + BLI_assert(precision > -1); +} + /** * push a new event onto event queue to activate the given button * (usually a text-field) upon entering a popup |