From 809499a3d0a2ef17c612f5e970cbed9ad4fb750d Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 1 Feb 2024 12:42:25 -0500 Subject: [PATCH] Refactor: UI: Use derived struct for number slider buttons Similar to d204830107ef7f6f5802. This is the last real use of the a1 and a2 arguments for many button definition functions. --- .../editors/asset/intern/asset_shelf.cc | 25 +++++----- .../blender/editors/include/UI_interface_c.hh | 3 ++ source/blender/editors/interface/interface.cc | 39 ++++++++++++++- .../editors/interface/interface_handlers.cc | 3 +- .../editors/interface/interface_intern.hh | 10 +++- .../editors/interface/interface_layout.cc | 17 ++++--- .../interface_region_color_picker.cc | 48 ++++++++++++------- .../editors/interface/interface_utils.cc | 4 +- 8 files changed, 108 insertions(+), 41 deletions(-) diff --git a/source/blender/editors/asset/intern/asset_shelf.cc b/source/blender/editors/asset/intern/asset_shelf.cc index 661a3c5279d..1e060aed143 100644 --- a/source/blender/editors/asset/intern/asset_shelf.cc +++ b/source/blender/editors/asset/intern/asset_shelf.cc @@ -644,19 +644,20 @@ static uiBut *add_tab_button(uiBlock &block, StringRefNull name) const int pad_x = UI_UNIT_X * 0.3f; const int but_width = std::min(string_width + 2 * pad_x, UI_UNIT_X * 8); - uiBut *but = uiDefBut(&block, - UI_BTYPE_TAB, - 0, + uiBut *but = uiDefBut( + &block, + UI_BTYPE_TAB, + 0, name.c_str(), - 0, - 0, - but_width, - UI_UNIT_Y, - nullptr, - 0, - 0, - 0, - 0, + 0, + 0, + but_width, + UI_UNIT_Y, + nullptr, + 0, + 0, + 0, + 0, "Enable catalog, making contained assets visible in the asset shelf"); UI_but_drawflag_enable(but, UI_BUT_ALIGN_DOWN); diff --git a/source/blender/editors/include/UI_interface_c.hh b/source/blender/editors/include/UI_interface_c.hh index feb73d810a1..760afb2abcf 100644 --- a/source/blender/editors/include/UI_interface_c.hh +++ b/source/blender/editors/include/UI_interface_c.hh @@ -1734,6 +1734,9 @@ void UI_but_node_link_set(uiBut *but, bNodeSocket *socket, const float draw_colo void UI_but_number_step_size_set(uiBut *but, float step_size); void UI_but_number_precision_set(uiBut *but, float precision); +void UI_but_number_slider_step_size_set(uiBut *but, float step_size); +void UI_but_number_slider_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.cc b/source/blender/editors/interface/interface.cc index 8b96529f9f4..1b92b0e3268 100644 --- a/source/blender/editors/interface/interface.cc +++ b/source/blender/editors/interface/interface.cc @@ -641,6 +641,9 @@ static float ui_but_get_float_precision(uiBut *but) if (but->type == UI_BTYPE_NUM) { return ((uiButNumber *)but)->precision; } + if (but->type == UI_BTYPE_NUM_SLIDER) { + return ((uiButNumberSlider *)but)->precision; + } return but->a2; } @@ -650,6 +653,9 @@ static float ui_but_get_float_step_size(uiBut *but) if (but->type == UI_BTYPE_NUM) { return ((uiButNumber *)but)->step_size; } + if (but->type == UI_BTYPE_NUM_SLIDER) { + return ((uiButNumberSlider *)but)->step_size; + } return but->a1; } @@ -3964,6 +3970,9 @@ static uiBut *ui_but_new(const eButType type) case UI_BTYPE_NUM: but = MEM_new("uiButNumber"); break; + case UI_BTYPE_NUM_SLIDER: + but = MEM_new("uiButNumber"); + break; case UI_BTYPE_COLOR: but = MEM_new("uiButColor"); break; @@ -4544,7 +4553,7 @@ static uiBut *ui_def_but_rna(uiBlock *block, const PropertyType proptype = RNA_property_type(prop); int icon = 0; uiMenuCreateFunc func = nullptr; - const bool always_set_a1_a2 = ELEM(type, UI_BTYPE_NUM); + const bool always_set_a1_a2 = ELEM(type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER); if (ELEM(type, UI_BTYPE_COLOR, UI_BTYPE_HSVCIRCLE, UI_BTYPE_HSVCUBE)) { BLI_assert(index == -1); @@ -4659,6 +4668,11 @@ static uiBut *ui_def_but_rna(uiBlock *block, UI_but_number_step_size_set(but, a1); UI_but_number_precision_set(but, a2); } + else if (but->type == UI_BTYPE_NUM_SLIDER) { + /* Set default values, can be overridden later. */ + UI_but_number_slider_step_size_set(but, a1); + UI_but_number_slider_precision_set(but, a2); + } but->rnapoin = *ptr; but->rnaprop = prop; @@ -4710,6 +4724,10 @@ static uiBut *ui_def_but_rna(uiBlock *block, uiButNumber *number_but = (uiButNumber *)but; number_but->step_size = ui_get_but_step_unit(but, number_but->step_size); } + if (type == UI_BTYPE_NUM_SLIDER) { + uiButNumberSlider *number_but = (uiButNumberSlider *)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); } @@ -6480,6 +6498,25 @@ void UI_but_number_precision_set(uiBut *but, float precision) BLI_assert(precision > -2); } +void UI_but_number_slider_step_size_set(uiBut *but, float step_size) +{ + uiButNumberSlider *but_number = (uiButNumberSlider *)but; + BLI_assert(but->type == UI_BTYPE_NUM_SLIDER); + + but_number->step_size = step_size; + BLI_assert(step_size > 0); +} + +void UI_but_number_slider_precision_set(uiBut *but, float precision) +{ + uiButNumberSlider *but_number = (uiButNumberSlider *)but; + BLI_assert(but->type == UI_BTYPE_NUM_SLIDER); + + but_number->precision = precision; + /* -1 is a valid value, UI code figures out an appropriate precision then. */ + BLI_assert(precision > -2); +} + void UI_but_focus_on_enter_event(wmWindow *win, uiBut *but) { wmEvent event; diff --git a/source/blender/editors/interface/interface_handlers.cc b/source/blender/editors/interface/interface_handlers.cc index 5d58c76e916..493c73479c1 100644 --- a/source/blender/editors/interface/interface_handlers.cc +++ b/source/blender/editors/interface/interface_handlers.cc @@ -5624,6 +5624,7 @@ static bool ui_numedit_but_SLI(uiBut *but, const bool snap, const bool shift) { + uiButNumberSlider *slider_but = reinterpret_cast(but); float cursor_x_range, f, tempf, softmin, softmax, softrange; int temp, lvalue; bool changed = false; @@ -5655,7 +5656,7 @@ static bool ui_numedit_but_SLI(uiBut *but, const float size = (is_horizontal) ? BLI_rctf_size_x(&but->rect) : -BLI_rctf_size_y(&but->rect); cursor_x_range = size * (but->softmax - but->softmin) / - (but->softmax - but->softmin + but->a1); + (but->softmax - but->softmin + slider_but->step_size); } else { const float ofs = (BLI_rctf_size_y(&but->rect) / 2.0f); diff --git a/source/blender/editors/interface/interface_intern.hh b/source/blender/editors/interface/interface_intern.hh index 9ca678c6cfb..914b6a51a86 100644 --- a/source/blender/editors/interface/interface_intern.hh +++ b/source/blender/editors/interface/interface_intern.hh @@ -321,8 +321,14 @@ struct uiBut { /** Derived struct for #UI_BTYPE_NUM */ struct uiButNumber : public uiBut { - float step_size = 0; - float precision = 0; + float step_size = 0.0f; + float precision = 0.0f; +}; + +/** Derived struct for #UI_BTYPE_NUM_SLIDER */ +struct uiButNumberSlider : public uiBut { + float step_size = 0.0f; + float precision = 0.0f; }; /** Derived struct for #UI_BTYPE_COLOR */ diff --git a/source/blender/editors/interface/interface_layout.cc b/source/blender/editors/interface/interface_layout.cc index 0cf35db848c..33d6e2f00f2 100644 --- a/source/blender/editors/interface/interface_layout.cc +++ b/source/blender/editors/interface/interface_layout.cc @@ -649,9 +649,10 @@ static void ui_item_array(uiLayout *layout, UI_UNIT_Y); if (slider && but->type == UI_BTYPE_NUM) { uiButNumber *number_but = (uiButNumber *)but; - - but->a1 = number_but->step_size; + const float step_size = number_but->step_size; but = ui_but_change_type(but, UI_BTYPE_NUM_SLIDER); + uiButNumberSlider *slider_but = reinterpret_cast(but); + slider_but->step_size = step_size; } } } @@ -719,9 +720,10 @@ static void ui_item_array(uiLayout *layout, block, ptr, prop, a, str_buf, icon, 0, 0, width_item, UI_UNIT_Y); if (slider && but->type == UI_BTYPE_NUM) { uiButNumber *number_but = (uiButNumber *)but; - - but->a1 = number_but->step_size; + const float step_size = number_but->step_size; but = ui_but_change_type(but, UI_BTYPE_NUM_SLIDER); + uiButNumberSlider *slider_but = reinterpret_cast(but); + slider_but->step_size = step_size; } if ((toggle == 1) && but->type == UI_BTYPE_CHECKBOX) { but->type = UI_BTYPE_TOGGLE; @@ -2448,10 +2450,11 @@ void uiItemFullR(uiLayout *layout, but = uiDefAutoButR(block, ptr, prop, index, name, icon, 0, 0, w, h); if (slider && but->type == UI_BTYPE_NUM) { - uiButNumber *num_but = (uiButNumber *)but; - - but->a1 = num_but->step_size; + uiButNumber *number_but = (uiButNumber *)but; + const float step_size = number_but->step_size; but = ui_but_change_type(but, UI_BTYPE_NUM_SLIDER); + uiButNumberSlider *slider_but = reinterpret_cast(but); + slider_but->step_size = step_size; } if (flag & UI_ITEM_R_CHECKBOX_INVERT) { diff --git a/source/blender/editors/interface/interface_region_color_picker.cc b/source/blender/editors/interface/interface_region_color_picker.cc index 732f86c2f2f..6940058830b 100644 --- a/source/blender/editors/interface/interface_region_color_picker.cc +++ b/source/blender/editors/interface/interface_region_color_picker.cc @@ -623,9 +623,11 @@ static void ui_block_colorpicker(uiBlock *block, 0, 0.0, 0.0, - 10, - 3, + 0, + 0, TIP_("Red")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_func_set(bt, ui_colorpicker_rgba_update_cb, bt, nullptr); bt->custom_data = cpicker; bt = uiDefButR_prop(block, @@ -641,9 +643,11 @@ static void ui_block_colorpicker(uiBlock *block, 1, 0.0, 0.0, - 10, - 3, + 0, + 0, TIP_("Green")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_func_set(bt, ui_colorpicker_rgba_update_cb, bt, nullptr); bt->custom_data = cpicker; bt = uiDefButR_prop(block, @@ -659,9 +663,11 @@ static void ui_block_colorpicker(uiBlock *block, 2, 0.0, 0.0, - 10, - 3, + 0, + 0, TIP_("Blue")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_func_set(bt, ui_colorpicker_rgba_update_cb, bt, nullptr); bt->custom_data = cpicker; @@ -683,9 +689,11 @@ static void ui_block_colorpicker(uiBlock *block, cpicker->hsv_scene_linear, 0.0, 1.0, - 10, - 3, + 0, + 0, TIP_("Hue")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_flag_disable(bt, UI_BUT_UNDO); UI_but_func_set(bt, ui_colorpicker_hsv_update_cb, bt, nullptr); bt->custom_data = cpicker; @@ -700,9 +708,11 @@ static void ui_block_colorpicker(uiBlock *block, cpicker->hsv_scene_linear + 1, 0.0, 1.0, - 10, - 3, + 0, + 0, TIP_("Saturation")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_flag_disable(bt, UI_BUT_UNDO); UI_but_func_set(bt, ui_colorpicker_hsv_update_cb, bt, nullptr); bt->custom_data = cpicker; @@ -718,9 +728,11 @@ static void ui_block_colorpicker(uiBlock *block, cpicker->hsv_scene_linear + 2, 0.0, 1.0, - 10, - 3, + 0, + 0, TIP_("Lightness")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); } else { bt = uiDefButF(block, @@ -734,10 +746,12 @@ static void ui_block_colorpicker(uiBlock *block, cpicker->hsv_scene_linear + 2, 0.0, softmax, - 10, - 3, + 0, + 0, CTX_TIP_(BLT_I18NCONTEXT_COLOR, "Value")); } + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_flag_disable(bt, UI_BUT_UNDO); bt->hardmax = hardmax; /* Not common but RGB may be over 1.0. */ @@ -760,9 +774,11 @@ static void ui_block_colorpicker(uiBlock *block, 3, 0.0, 0.0, - 10, - 3, + 0, + 0, TIP_("Alpha")); + UI_but_number_slider_step_size_set(bt, 10); + UI_but_number_slider_precision_set(bt, 3); UI_but_func_set(bt, ui_colorpicker_rgba_update_cb, bt, nullptr); bt->custom_data = cpicker; } diff --git a/source/blender/editors/interface/interface_utils.cc b/source/blender/editors/interface/interface_utils.cc index f70d324369d..d8576177ebb 100644 --- a/source/blender/editors/interface/interface_utils.cc +++ b/source/blender/editors/interface/interface_utils.cc @@ -163,8 +163,8 @@ uiBut *uiDefAutoButR(uiBlock *block, index, 0, 0, - -1, - -1, + 0, + 0, nullptr); } else {