[Bf-blender-cvs] [5ccb47a97e7] ui-asset-view-template: Add own button/widget type for the asset previews

Julian Eisel noreply at git.blender.org
Thu Mar 4 15:58:15 CET 2021


Commit: 5ccb47a97e70fcba9bc6291e30e2b7201fd6781c
Author: Julian Eisel
Date:   Thu Mar 4 15:54:25 2021 +0100
Branches: ui-asset-view-template
https://developer.blender.org/rB5ccb47a97e70fcba9bc6291e30e2b7201fd6781c

Add own button/widget type for the asset previews

The new button type (UI_BTYPE_PREVIEW_TILE) shows the items similar to
the Asset Browser, or the File Browser in thumbnail display mode. That
is, previews bigger than normal buttons, with the name drawn centered
below it.

Would be nice if the File/Asset Browser and preview templates could
adopt that new button type, so that there's no ad-hoc drawing for these
anymore. That's for a future cleanup though.

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

M	release/scripts/addons
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_handlers.c
M	source/blender/editors/interface/interface_intern.h
M	source/blender/editors/interface/interface_region_search.c
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/interface/interface_widgets.c
M	source/tools

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

diff --git a/release/scripts/addons b/release/scripts/addons
index d142dc2d11d..c8752443311 160000
--- a/release/scripts/addons
+++ b/release/scripts/addons
@@ -1 +1 @@
-Subproject commit d142dc2d11daf8100085fb8b345088e8de8154ec
+Subproject commit c8752443311b133f8783a6b9b2152eb7c92d06a7
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 6de6ea4b7e1..eb924bd5094 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -375,6 +375,9 @@ typedef enum {
   /** Buttons with value >= #UI_BTYPE_SEARCH_MENU don't get undo pushes. */
   UI_BTYPE_SEARCH_MENU = 41 << 9,
   UI_BTYPE_EXTRA = 42 << 9,
+  /** A preview image (#PreviewImage), with text under it. Typically bigger than normal buttons and
+   * laid out in a grid, e.g. like the File Browser in thumbnail display mode. */
+  UI_BTYPE_PREVIEW_TILE = 43 << 9,
   UI_BTYPE_HOTKEY_EVENT = 46 << 9,
   /** Non-interactive image, used for splash screen */
   UI_BTYPE_IMAGE = 47 << 9,
diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c
index bd4ae7db457..ee2639154ff 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -7681,6 +7681,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent *
     case UI_BTYPE_IMAGE:
     case UI_BTYPE_PROGRESS_BAR:
     case UI_BTYPE_NODE_SOCKET:
+    case UI_BTYPE_PREVIEW_TILE:
       retval = ui_do_but_EXIT(C, but, data, event);
       break;
     case UI_BTYPE_HISTOGRAM:
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index 086df7f7785..21da9eedbc4 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -1022,8 +1022,18 @@ void ui_draw_menu_item(const struct uiFontStyle *fstyle,
                        int state,
                        uiMenuItemSeparatorType separator_type,
                        int *r_xmax);
-void ui_draw_preview_item(
-    const struct uiFontStyle *fstyle, rcti *rect, const char *name, int iconid, int state);
+void ui_draw_preview_item(const struct uiFontStyle *fstyle,
+                          rcti *rect,
+                          const char *name,
+                          int iconid,
+                          int state,
+                          eFontStyle_Align text_align);
+void ui_draw_preview_item_stateless(const struct uiFontStyle *fstyle,
+                                    rcti *rect,
+                                    const char *name,
+                                    int iconid,
+                                    const uchar text_col[4],
+                                    eFontStyle_Align text_align);
 
 #define UI_TEXT_MARGIN_X 0.4f
 #define UI_POPUP_MARGIN (UI_DPI_FAC * 12)
diff --git a/source/blender/editors/interface/interface_region_search.c b/source/blender/editors/interface/interface_region_search.c
index 12044863b8c..14239e586e5 100644
--- a/source/blender/editors/interface/interface_region_search.c
+++ b/source/blender/editors/interface/interface_region_search.c
@@ -599,8 +599,12 @@ static void ui_searchbox_region_draw_cb(const bContext *C, ARegion *region)
         ui_searchbox_butrect(&rect, data, a);
 
         /* widget itself */
-        ui_draw_preview_item(
-            &data->fstyle, &rect, data->items.names[a], data->items.icons[a], state);
+        ui_draw_preview_item(&data->fstyle,
+                             &rect,
+                             data->items.names[a],
+                             data->items.icons[a],
+                             state,
+                             UI_STYLE_TEXT_LEFT);
       }
 
       /* indicate more */
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 23058bdab44..81db25b55bc 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -98,20 +98,21 @@ static void asset_view_draw_item(uiList *ui_list,
     /* TODO ED_fileselect_init_layout(). Share somehow? */
     float size_x = (96.0f / 20.0f) * UI_UNIT_X;
     float size_y = (96.0f / 20.0f) * UI_UNIT_Y;
-    uiBut *but = uiDefIconBut(block,
-                              UI_BTYPE_LABEL,
-                              0,
-                              file->preview_icon_id,
-                              0,
-                              0,
-                              size_x,
-                              size_y,
-                              nullptr,
-                              0,
-                              0,
-                              0,
-                              0,
-                              "");
+    uiBut *but = uiDefIconTextBut(block,
+                                  UI_BTYPE_PREVIEW_TILE,
+                                  0,
+                                  file->preview_icon_id,
+                                  file->name,
+                                  0,
+                                  0,
+                                  size_x,
+                                  size_y,
+                                  nullptr,
+                                  0,
+                                  0,
+                                  0,
+                                  0,
+                                  "");
     ui_def_but_icon(but, file->preview_icon_id, UI_HAS_ICON | UI_BUT_ICON_PREVIEW);
     asset_view_item_but_drag_set(but, list_data, file);
   }
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c
index 0fa5999976b..78484327933 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -1,4 +1,4 @@
-/*
+/*
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
@@ -105,6 +105,7 @@ typedef enum {
   /* specials */
   UI_WTYPE_ICON,
   UI_WTYPE_ICON_LABEL,
+  UI_WTYPE_PREVIEW_TILE,
   UI_WTYPE_SWATCH,
   UI_WTYPE_RGB_PICKER,
   UI_WTYPE_UNITVEC,
@@ -3942,6 +3943,14 @@ static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roun
   widgetbase_draw(&wtb, wcol);
 }
 
+static void widget_preview_tile(
+    uiBut *but, uiWidgetColors *wcol, rcti *rect, int UNUSED(state), int UNUSED(roundboxalign))
+{
+  const uiStyle *style = UI_style_get();
+  ui_draw_preview_item_stateless(
+      &style->widget, rect, but->drawstr, but->icon, wcol->text, UI_STYLE_TEXT_CENTER);
+}
+
 static void widget_menuiconbut(uiWidgetColors *wcol,
                                rcti *rect,
                                int UNUSED(state),
@@ -4407,6 +4416,13 @@ static uiWidgetType *widget_type(uiWidgetTypeEnum type)
       wt.custom = widget_icon_has_anim;
       break;
 
+    case UI_WTYPE_PREVIEW_TILE:
+      wt.draw = NULL;
+      /* Drawn via the `custom` callback. */
+      wt.text = NULL;
+      wt.custom = widget_preview_tile;
+      break;
+
     case UI_WTYPE_SWATCH:
       wt.custom = widget_swatch;
       break;
@@ -4698,6 +4714,10 @@ void ui_draw_but(const bContext *C, struct ARegion *region, uiStyle *style, uiBu
         wt = widget_type(UI_WTYPE_BOX);
         break;
 
+      case UI_BTYPE_PREVIEW_TILE:
+        wt = widget_type(UI_WTYPE_PREVIEW_TILE);
+        break;
+
       case UI_BTYPE_EXTRA:
         widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
         break;
@@ -4846,13 +4866,15 @@ void ui_draw_but(const bContext *C, struct ARegion *region, uiStyle *style, uiBu
     wt->draw(&wt->wcol, rect, state, roundboxalign);
   }
 
-  if (use_alpha_blend) {
-    GPU_blend(GPU_BLEND_ALPHA);
-  }
+  if (wt->text) {
+    if (use_alpha_blend) {
+      GPU_blend(GPU_BLEND_ALPHA);
+    }
 
-  wt->text(fstyle, &wt->wcol, but, rect);
-  if (use_alpha_blend) {
-    GPU_blend(GPU_BLEND_NONE);
+    wt->text(fstyle, &wt->wcol, but, rect);
+    if (use_alpha_blend) {
+      GPU_blend(GPU_BLEND_NONE);
+    }
   }
 }
 
@@ -5374,17 +5396,20 @@ void ui_draw_menu_item(const uiFontStyle *fstyle,
   }
 }
 
-void ui_draw_preview_item(
-    const uiFontStyle *fstyle, rcti *rect, const char *name, int iconid, int state)
+/**
+ * Version of #ui_draw_preview_item() that does not draw the menu background and item text based on
+ * state. It just draws the preview and text directly.
+ */
+void ui_draw_preview_item_stateless(const uiFontStyle *fstyle,
+                                    rcti *rect,
+                                    const char *name,
+                                    int iconid,
+                                    const uchar text_col[4],
+                                    eFontStyle_Align text_align)
 {
   rcti trect = *rect;
   const float text_size = UI_UNIT_Y;
   float font_dims[2] = {0.0f, 0.0f};
-  uiWidgetType *wt = widget_type(UI_WTYPE_MENU_ITEM);
-
-  /* drawing button background */
-  wt->state(wt, state, 0, UI_EMBOSS_UNDEFINED);
-  wt->draw(&wt->wcol, rect, 0, 0);
 
   /* draw icon in rect above the space reserved for the label */
   rect->ymin += text_size;
@@ -5396,8 +5421,6 @@ void ui_draw_preview_item(
       fstyle->uifont_id, name, BLF_DRAW_STR_DUMMY_MAX, &font_dims[0], &font_dims[1]);
 
   /* text rect */
-  trect.xmin += 0;
-  trect.xmax = trect.xmin + font_dims[0] + U.widget_unit / 2;
   trect.ymin += U.widget_unit / 2;
   trect.ymax = trect.ymin + font_dims[1];
   if (trect.xmax > rect->xmax - PREVIEW_PAD) {
@@ -5416,11 +5439,27 @@ void ui_draw_preview_item(
     UI_fontstyle_draw(fstyle,
                       &trect,
                       drawstr,
-                      wt->wcol.text,
+                      text_col,
                       &(struct uiFontStyleDraw_Params){
-                          .align = UI_STYLE_TEXT_CENTER,
+                          .align = text_align,
                       });
   }
 }
 
+void ui

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list