[Bf-blender-cvs] [076a93b855e] master: UI Code Quality: Use derived struct for progessbar buttons

Julian Eisel noreply at git.blender.org
Fri Aug 7 15:18:49 CEST 2020


Commit: 076a93b855e89fd915779da5991132bc956aa0f4
Author: Julian Eisel
Date:   Fri Aug 7 15:16:26 2020 +0200
Branches: master
https://developer.blender.org/rB076a93b855e89fd915779da5991132bc956aa0f4

UI Code Quality: Use derived struct for progessbar buttons

For the main rationale behind this design, see 03b122e2a18df. Further,
this removes users of `uiBut.a1`, which is a very ugly design
choice (hard to reason about).

Part of T74432.

===================================================================

M	source/blender/editors/interface/interface.c
M	source/blender/editors/interface/interface_intern.h
M	source/blender/editors/interface/interface_templates.c
M	source/blender/editors/interface/interface_widgets.c

===================================================================

diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 0d8f682ea51..a84ca33a7d7 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -820,10 +820,10 @@ static bool ui_but_update_from_old_block(const bContext *C,
       oldbut->hardmax = but->hardmax;
     }
 
-    /* Selectively copy a1, a2 since their use differs across all button types
-     * (and we'll probably split these out later) */
-    if (ELEM(oldbut->type, UI_BTYPE_PROGRESS_BAR)) {
-      oldbut->a1 = but->a1;
+    if (oldbut->type == UI_BTYPE_PROGRESS_BAR) {
+      uiButProgressbar *progress_oldbut = (uiButProgressbar *)oldbut;
+      uiButProgressbar *progress_but = (uiButProgressbar *)but;
+      progress_oldbut->progress = progress_but->progress;
     }
 
     if (!BLI_listbase_is_empty(&block->butstore)) {
@@ -3789,6 +3789,10 @@ static void ui_but_alloc_info(const eButType type,
       alloc_size = sizeof(uiButSearch);
       alloc_str = "uiButSearch";
       break;
+    case UI_BTYPE_PROGRESS_BAR:
+      alloc_size = sizeof(uiButProgressbar);
+      alloc_str = "uiButProgressbar";
+      break;
     default:
       alloc_size = sizeof(uiBut);
       alloc_str = "uiBut";
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index 9840444c0e0..41110883729 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_PROGRESS_BAR: Use to store progress (0..1).
    */
   float a1;
 
@@ -322,6 +321,13 @@ typedef struct uiButDecorator {
   int rnaindex;
 } uiButDecorator;
 
+typedef struct uiButProgressbar {
+  uiBut but;
+
+  /* 0..1 range */
+  float progress;
+} uiButProgressbar;
+
 /**
  * Additional, superimposed icon for a button, invoking an operator.
  */
diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 8593e7e3118..c7d3d7bf501 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -6791,22 +6791,24 @@ void uiTemplateRunningJobs(uiLayout *layout, bContext *C)
       struct ProgressTooltip_Store *tip_arg = MEM_mallocN(sizeof(*tip_arg), __func__);
       tip_arg->wm = wm;
       tip_arg->owner = owner;
-      uiBut *but_progress = uiDefIconTextBut(block,
-                                             UI_BTYPE_PROGRESS_BAR,
-                                             0,
-                                             0,
-                                             text,
-                                             UI_UNIT_X,
-                                             0,
-                                             UI_UNIT_X * 6.0f,
-                                             UI_UNIT_Y,
-                                             NULL,
-                                             0.0f,
-                                             0.0f,
-                                             progress,
-                                             0,
-                                             NULL);
-      UI_but_func_tooltip_set(but_progress, progress_tooltip_func, tip_arg);
+      uiButProgressbar *but_progress = (uiButProgressbar *)uiDefIconTextBut(block,
+                                                                            UI_BTYPE_PROGRESS_BAR,
+                                                                            0,
+                                                                            0,
+                                                                            text,
+                                                                            UI_UNIT_X,
+                                                                            0,
+                                                                            UI_UNIT_X * 6.0f,
+                                                                            UI_UNIT_Y,
+                                                                            NULL,
+                                                                            0.0f,
+                                                                            0.0f,
+                                                                            0.0f,
+                                                                            0,
+                                                                            NULL);
+
+      but_progress->progress = progress;
+      UI_but_func_tooltip_set(&but_progress->but, progress_tooltip_func, tip_arg);
     }
 
     if (!wm->is_interface_locked) {
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c
index 5d342e928f9..7c33c5e7048 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -3609,6 +3609,7 @@ static void widget_scroll(
 static void widget_progressbar(
     uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int roundboxalign)
 {
+  uiButProgressbar *but_progressbar = (uiButProgressbar *)but;
   uiWidgetBase wtb, wtb_bar;
   rcti rect_prog = *rect, rect_bar = *rect;
 
@@ -3616,7 +3617,7 @@ static void widget_progressbar(
   widget_init(&wtb_bar);
 
   /* round corners */
-  float value = but->a1;
+  float value = but_progressbar->progress;
   float offs = wcol->roundness * BLI_rcti_size_y(&rect_prog);
   float w = value * BLI_rcti_size_x(&rect_prog);



More information about the Bf-blender-cvs mailing list