[Bf-blender-cvs] [b8dce8f3ca1] asset-browser: Draw hint instead of file list if repository path is invalid

Julian Eisel noreply at git.blender.org
Sun Nov 22 17:35:34 CET 2020


Commit: b8dce8f3ca1adcd1fde102b4de86c299fb5b987a
Author: Julian Eisel
Date:   Sun Nov 22 17:31:36 2020 +0100
Branches: asset-browser
https://developer.blender.org/rBb8dce8f3ca1adcd1fde102b4de86c299fb5b987a

Draw hint instead of file list if repository path is invalid

If the repository is not found, draw a nice hint why nothing is shown and how
you can address that.
This should greatly help understand users how to set up repositories and where
the Asset Browser expects to find the assets.

{F9355048}

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

M	source/blender/editors/interface/interface_style.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/file_intern.h
M	source/blender/editors/space_file/file_utils.c
M	source/blender/editors/space_file/space_file.c

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

diff --git a/source/blender/editors/interface/interface_style.c b/source/blender/editors/interface/interface_style.c
index c3d528ad5c5..a37fb0dfde1 100644
--- a/source/blender/editors/interface/interface_style.c
+++ b/source/blender/editors/interface/interface_style.c
@@ -206,8 +206,12 @@ void UI_fontstyle_draw_ex(const uiFontStyle *fs,
 
   BLF_disable(fs->uifont_id, font_flag);
 
-  *r_xofs = xofs;
-  *r_yofs = yofs;
+  if (r_xofs) {
+    *r_xofs = xofs;
+  }
+  if (r_yofs) {
+    *r_yofs = yofs;
+  }
 }
 
 void UI_fontstyle_draw(const uiFontStyle *fs,
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index 0238f6f9b4d..b9b6b9bb5e4 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -21,6 +21,7 @@
  * \ingroup spfile
  */
 
+#include <alloca.h>
 #include <errno.h>
 #include <math.h>
 #include <string.h>
@@ -218,6 +219,65 @@ static void file_draw_string(int sx,
                     });
 }
 
+/**
+ * \param r_sx, r_sy: The lower right corner of the last line drawn. AKA the cursor position on
+ *                    completion.
+ */
+static void file_draw_string_multiline(int sx,
+                                       int sy,
+                                       const char *string,
+                                       int wrap_width,
+                                       int line_height,
+                                       const uchar text_col[4],
+                                       int *r_sx,
+                                       int *r_sy)
+{
+  rcti rect;
+
+  if (string[0] == '\0' || wrap_width < 1) {
+    return;
+  }
+
+  const uiStyle *style = UI_style_get();
+  int font_id = style->widgetlabel.uifont_id;
+  int len = strlen(string);
+
+  rctf textbox;
+  BLF_wordwrap(font_id, wrap_width);
+  BLF_enable(font_id, BLF_WORD_WRAP);
+  BLF_boundbox(font_id, string, len, &textbox);
+  BLF_disable(font_id, BLF_WORD_WRAP);
+
+  /* no text clipping needed, UI_fontstyle_draw does it but is a bit too strict
+   * (for buttons it works) */
+  rect.xmin = sx;
+  rect.xmax = sx + wrap_width;
+  /* Need to increase the clipping rect by one more line, since the #UI_fontstyle_draw_ex() will
+   * actually start drawing at (ymax - line-height). */
+  rect.ymin = sy - round_fl_to_int(BLI_rctf_size_y(&textbox)) - line_height;
+  rect.ymax = sy;
+
+  struct ResultBLF result;
+  UI_fontstyle_draw_ex(&style->widget,
+                       &rect,
+                       string,
+                       text_col,
+                       &(struct uiFontStyleDraw_Params){
+                           .align = UI_STYLE_TEXT_LEFT,
+                           .word_wrap = true,
+                       },
+                       len,
+                       NULL,
+                       NULL,
+                       &result);
+  if (r_sx) {
+    *r_sx = result.width;
+  }
+  if (r_sy) {
+    *r_sy = rect.ymin + line_height;
+  }
+}
+
 void file_calc_previews(const bContext *C, ARegion *region)
 {
   SpaceFile *sfile = CTX_wm_space_file(C);
@@ -944,3 +1004,67 @@ void file_draw_list(const bContext *C, ARegion *region)
 
   layout->curr_size = params->thumbnail_size;
 }
+
+static void file_draw_invalid_repository_hint(const SpaceFile *sfile, const ARegion *region)
+{
+  const FileAssetSelectParams *asset_params = ED_fileselect_get_asset_params(sfile);
+
+  char repository_ui_path[PATH_MAX];
+  file_path_to_ui_path(
+      asset_params->base_params.dir, repository_ui_path, sizeof(repository_ui_path));
+
+  uchar text_col[4];
+  uchar text_alert_col[4];
+  UI_GetThemeColor4ubv(TH_TEXT, text_col);
+  UI_GetThemeColor4ubv(TH_REDALERT, text_alert_col);
+
+  const View2D *v2d = &region->v2d;
+  const int pad = sfile->layout->tile_border_x;
+  const int width = BLI_rctf_size_x(&v2d->tot) - (2 * pad);
+  const int line_height = sfile->layout->textheight;
+  int sx = v2d->tot.xmin + pad;
+  /* For some reason no padding needed. */
+  int sy = v2d->tot.ymax;
+
+  {
+    const char *message = TIP_("Repository not found");
+    const int draw_string_str_len = strlen(message) + 2 + sizeof(repository_ui_path);
+    char *draw_string = alloca(draw_string_str_len);
+    BLI_snprintf(draw_string, draw_string_str_len, "%s: %s", message, repository_ui_path);
+    file_draw_string_multiline(sx, sy, draw_string, width, line_height, text_alert_col, NULL, &sy);
+  }
+
+  /* Next line, but separate it a bit further. */
+  sy -= line_height;
+
+  {
+    UI_icon_draw(sx, sy - UI_UNIT_Y, ICON_INFO);
+
+    const char *suggestion = TIP_(
+        "Set up the repository or edit repositories in the Preferences, File Paths section.");
+    file_draw_string_multiline(
+        sx + UI_UNIT_X, sy, suggestion, width - UI_UNIT_X, line_height, text_col, NULL, NULL);
+  }
+}
+
+/**
+ * Draw a string hint if the file list is invalid.
+ * \return true if the list is invalid and a hint was drawn.
+ */
+bool file_draw_hint_if_invalid(const SpaceFile *sfile, const ARegion *region)
+{
+  FileAssetSelectParams *asset_params = ED_fileselect_get_asset_params(sfile);
+  /* Only for asset browser. */
+  if (!ED_fileselect_is_asset_browser(sfile)) {
+    return false;
+  }
+  /* Check if the repository exists. */
+  if ((asset_params->asset_repository.type == FILE_ASSET_REPO_LOCAL) ||
+      filelist_is_dir(sfile->files, asset_params->base_params.dir)) {
+    return false;
+  }
+
+  file_draw_invalid_repository_hint(sfile, region);
+
+  return true;
+}
diff --git a/source/blender/editors/space_file/file_intern.h b/source/blender/editors/space_file/file_intern.h
index a0e02681e0e..56fb588776e 100644
--- a/source/blender/editors/space_file/file_intern.h
+++ b/source/blender/editors/space_file/file_intern.h
@@ -38,6 +38,7 @@ struct View2D;
 
 void file_calc_previews(const bContext *C, ARegion *region);
 void file_draw_list(const bContext *C, ARegion *region);
+bool file_draw_hint_if_invalid(const SpaceFile *sfile, const ARegion *region);
 
 void file_draw_check_ex(bContext *C, struct ScrArea *area);
 void file_draw_check(bContext *C);
@@ -117,3 +118,5 @@ void file_execute_region_panels_register(struct ARegionType *art);
 
 /* file_utils.c */
 void file_tile_boundbox(const ARegion *region, FileLayout *layout, const int file, rcti *r_bounds);
+
+void file_path_to_ui_path(const char *path, char *r_pathi, int max_size);
diff --git a/source/blender/editors/space_file/file_utils.c b/source/blender/editors/space_file/file_utils.c
index 452f2f704cf..9d85996c559 100644
--- a/source/blender/editors/space_file/file_utils.c
+++ b/source/blender/editors/space_file/file_utils.c
@@ -18,12 +18,14 @@
  * \ingroup spfile
  */
 
+#include "BLI_fileops.h"
 #include "BLI_listbase.h"
+#include "BLI_path_util.h"
 #include "BLI_rect.h"
-
-#include "BLO_readfile.h"
+#include "BLI_string.h"
 
 #include "BKE_context.h"
+#include "BLO_readfile.h"
 
 #include "ED_fileselect.h"
 #include "ED_screen.h"
@@ -44,3 +46,14 @@ void file_tile_boundbox(const ARegion *region, FileLayout *layout, const int fil
                 ymax - layout->tile_h - layout->tile_border_y,
                 ymax);
 }
+
+/**
+ * If \a path leads to a .blend, remove the trailing slash (if needed).
+ */
+void file_path_to_ui_path(const char *path, char *r_path, int max_size)
+{
+  char tmp_path[PATH_MAX];
+  BLI_strncpy(tmp_path, path, sizeof(tmp_path));
+  BLI_path_slash_rstrip(tmp_path);
+  BLI_strncpy(r_path, BLO_has_bfile_extension(tmp_path) ? tmp_path : path, max_size);
+}
diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c
index 460adb2b501..f3c15bc2efa 100644
--- a/source/blender/editors/space_file/space_file.c
+++ b/source/blender/editors/space_file/space_file.c
@@ -551,7 +551,9 @@ static void file_main_region_draw(const bContext *C, ARegion *region)
     file_highlight_set(sfile, region, event->x, event->y);
   }
 
-  file_draw_list(C, region);
+  if (!file_draw_hint_if_invalid(sfile, region)) {
+    file_draw_list(C, region);
+  }
 
   /* reset view matrix */
   UI_view2d_view_restore(C);



More information about the Bf-blender-cvs mailing list