[Bf-blender-cvs] [e6f0b60c2e9] master: UI Code Quality: Use derived struct for number buttons
Julian Eisel
noreply at git.blender.org
Fri Sep 4 21:29:43 CEST 2020
Commit: e6f0b60c2e9111af1878d31dcc295c59ed1bea77
Author: Julian Eisel
Date: Fri Sep 4 21:18:45 2020 +0200
Branches: master
https://developer.blender.org/rBe6f0b60c2e9111af1878d31dcc295c59ed1bea77
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).
===================================================================
M source/blender/editors/animation/fmodifier_ui.c
M source/blender/editors/include/UI_interface.h
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
M source/blender/editors/interface/interface_templates.c
M source/blender/editors/interface/interface_utils.c
M source/blender/editors/space_clip/clip_buttons.c
M source/blender/editors/space_graph/graph_buttons.c
M source/blender/editors/space_view3d/view3d_buttons.c
M source/blender/editors/uvedit/uvedit_buttons.c
===================================================================
diff --git a/source/blender/editors/animation/fmodifier_ui.c b/source/blender/editors/animation/fmodifier_ui.c
index 1ecdf5accb8..9d54be61171 100644
--- a/source/blender/editors/animation/fmodifier_ui.c
+++ b/source/blender/editors/animation/fmodifier_ui.c
@@ -196,9 +196,10 @@ static void draw_modifier__generator(uiLayout *layout,
&data->poly_order,
1,
100,
- 1,
+ 0,
0,
TIP_("'Order' of the Polynomial (for a polynomial with n terms, 'order' is n-1)"));
+ UI_but_number_step_size_set(but, 1);
UI_but_func_set(but, validate_fmodifier_cb, fcm, fcurve_owner_id);
/* calculate maximum width of label for "x^n" labels */
@@ -256,20 +257,22 @@ static void draw_modifier__generator(uiLayout *layout,
}
/* coefficient */
- uiDefButF(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- "",
- 0,
- 0,
- bwidth / 2,
- UI_UNIT_Y,
- cp,
- -UI_FLT_MAX,
- UI_FLT_MAX,
- 10,
- 3,
- TIP_("Coefficient for polynomial"));
+ but = uiDefButF(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ "",
+ 0,
+ 0,
+ bwidth / 2,
+ UI_UNIT_Y,
+ cp,
+ -UI_FLT_MAX,
+ UI_FLT_MAX,
+ 0,
+ 0,
+ TIP_("Coefficient for polynomial"));
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 3);
/* 'x' param (and '+' if necessary) */
if (i == 0) {
@@ -334,10 +337,11 @@ static void draw_modifier__generator(uiLayout *layout,
&data->poly_order,
1,
100,
- 1,
+ 0,
0,
TIP_("'Order' of the Polynomial (for a polynomial with n terms, 'order' is n-1)"));
UI_but_func_set(but, validate_fmodifier_cb, fcm, fcurve_owner_id);
+ UI_but_number_step_size_set(but, 1);
/* draw controls for each pair of coefficients */
row = uiLayoutRow(layout, true);
@@ -386,20 +390,22 @@ static void draw_modifier__generator(uiLayout *layout,
block, UI_BTYPE_LABEL, 1, "(", 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
/* coefficients */
- uiDefButF(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- "",
- 0,
- 0,
- 5 * UI_UNIT_X,
- UI_UNIT_Y,
- cp,
- -UI_FLT_MAX,
- UI_FLT_MAX,
- 10,
- 3,
- TIP_("Coefficient of x"));
+ but = uiDefButF(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ "",
+ 0,
+ 0,
+ 5 * UI_UNIT_X,
+ UI_UNIT_Y,
+ cp,
+ -UI_FLT_MAX,
+ UI_FLT_MAX,
+ 0,
+ 0,
+ TIP_("Coefficient of x"));
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 3);
uiDefBut(block,
UI_BTYPE_LABEL,
@@ -416,20 +422,22 @@ static void draw_modifier__generator(uiLayout *layout,
0,
"");
- uiDefButF(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- "",
- 0,
- 0,
- 5 * UI_UNIT_X,
- UI_UNIT_Y,
- cp + 1,
- -UI_FLT_MAX,
- UI_FLT_MAX,
- 10,
- 3,
- TIP_("Second coefficient"));
+ but = uiDefButF(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ "",
+ 0,
+ 0,
+ 5 * UI_UNIT_X,
+ UI_UNIT_Y,
+ cp + 1,
+ -UI_FLT_MAX,
+ UI_FLT_MAX,
+ 0,
+ 0,
+ TIP_("Second coefficient"));
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 3);
/* closing bracket and multiplication sign */
if ((i != (data->poly_order - 1)) || ((i == 0) && data->poly_order == 2)) {
@@ -724,54 +732,60 @@ static void draw_modifier__envelope(uiLayout *layout,
block = uiLayoutGetBlock(row);
UI_block_align_begin(block);
- uiDefButR(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- IFACE_("Fra:"),
- 0,
- 0,
- 4.5 * UI_UNIT_X,
- UI_UNIT_Y,
- &ctrl_ptr,
- "frame",
- -1,
- -MAXFRAMEF,
- MAXFRAMEF,
- 10,
- 1,
- NULL);
- uiDefButR(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- IFACE_("Min:"),
- 0,
- 0,
- 5 * UI_UNIT_X,
- UI_UNIT_Y,
- &ctrl_ptr,
- "min",
- -1,
- -UI_FLT_MAX,
- UI_FLT_MAX,
- 10,
- 2,
- NULL);
- uiDefButR(block,
- UI_BTYPE_NUM,
- B_FMODIFIER_REDRAW,
- IFACE_("Max:"),
- 0,
- 0,
- 5 * UI_UNIT_X,
- UI_UNIT_Y,
- &ctrl_ptr,
- "max",
- -1,
- -UI_FLT_MAX,
- UI_FLT_MAX,
- 10,
- 2,
- NULL);
+ but = uiDefButR(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ IFACE_("Fra:"),
+ 0,
+ 0,
+ 4.5 * UI_UNIT_X,
+ UI_UNIT_Y,
+ &ctrl_ptr,
+ "frame",
+ -1,
+ -MAXFRAMEF,
+ MAXFRAMEF,
+ 0,
+ 0,
+ NULL);
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 1);
+ but = uiDefButR(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ IFACE_("Min:"),
+ 0,
+ 0,
+ 5 * UI_UNIT_X,
+ UI_UNIT_Y,
+ &ctrl_ptr,
+ "min",
+ -1,
+ -UI_FLT_MAX,
+ UI_FLT_MAX,
+ 0,
+ 0,
+ NULL);
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 2);
+ but = uiDefButR(block,
+ UI_BTYPE_NUM,
+ B_FMODIFIER_REDRAW,
+ IFACE_("Max:"),
+ 0,
+ 0,
+ 5 * UI_UNIT_X,
+ UI_UNIT_Y,
+ &ctrl_ptr,
+ "max",
+ -1,
+ -UI_FLT_MAX,
+ UI_FLT_MAX,
+ 0,
+ 0,
+ NULL);
+ UI_but_number_step_size_set(but, 10);
+ UI_but_number_precision_set(but, 2);
but = uiDefIconBut(block,
UI_BTYPE_BUT,
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index b23f8ae11c9..d5d489b1742 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -1602,6 +1602,9 @@ int UI_search_items_find_index(uiSearchItems *items, const char *name);
void UI_but_node_link_set(uiBut *but, struct bNodeSocket *socket, const float draw_color[4]);
+void UI_but_number_step_size_set(uiBut *but, float step_size);
+void UI_but_number_precision_set(uiBut *but, float precision);
+
void UI_block_func_handle_set(uiBlock *block, uiBlockHandleFunc func, void *arg);
void UI_block_func_butmenu_set(uiBlock *block, uiMenuHandleFunc func, void *arg);
void UI_block_func_set(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2);
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_POI
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list