[Bf-blender-cvs] [f531dff9b35] master: Cleanup: Use rectangle for file browser tile drawing

Julian Eisel noreply at git.blender.org
Wed Mar 2 19:25:48 CET 2022


Commit: f531dff9b35120ef0ab0e6476f8a88f18b85609e
Author: Julian Eisel
Date:   Wed Mar 2 19:02:20 2022 +0100
Branches: master
https://developer.blender.org/rBf531dff9b35120ef0ab0e6476f8a88f18b85609e

Cleanup: Use rectangle for file browser tile drawing

Passing around coordinates for drawing can be quite confusing, it's
often not clear what they represent and where they are currently.
Instead pass around the tile rectangle for drawing and let all code draw
based on that, it's way more clear that way.

Changes shouldn't be user visible.

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

M	source/blender/editors/include/ED_fileselect.h
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/filesel.c

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

diff --git a/source/blender/editors/include/ED_fileselect.h b/source/blender/editors/include/ED_fileselect.h
index e45779b8a26..6a207a6e453 100644
--- a/source/blender/editors/include/ED_fileselect.h
+++ b/source/blender/editors/include/ED_fileselect.h
@@ -136,7 +136,7 @@ bool ED_fileselect_layout_isect_rect(const FileLayout *layout,
                                      const struct View2D *v2d,
                                      const struct rcti *rect,
                                      struct rcti *r_dst);
-void ED_fileselect_layout_tilepos(FileLayout *layout, int tile, int *x, int *y);
+void ED_fileselect_layout_tilepos(const FileLayout *layout, int tile, int *x, int *y);
 
 void ED_operatormacros_file(void);
 
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index b7286c7794c..92469773ed4 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -114,29 +114,22 @@ static char *file_draw_tooltip_func(bContext *UNUSED(C), void *argN, const char
   return BLI_strdup(dyn_tooltip);
 }
 
-static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
+static void draw_tile_background(const rcti *draw_rect, int colorid, int shade)
 {
   float color[4];
+  rctf draw_rect_fl;
+  BLI_rctf_rcti_copy(&draw_rect_fl, draw_rect);
+
   UI_GetThemeColorShade4fv(colorid, shade, color);
   UI_draw_roundbox_corner_set(UI_CNR_ALL);
-  UI_draw_roundbox_aa(
-      &(const rctf){
-          .xmin = (float)sx,
-          .xmax = (float)(sx + width),
-          .ymin = (float)(sy - height),
-          .ymax = (float)sy,
-      },
-      true,
-      5.0f,
-      color);
+  UI_draw_roundbox_aa(&draw_rect_fl, true, 5.0f, color);
 }
 
 static void file_draw_icon(const SpaceFile *sfile,
                            uiBlock *block,
                            const FileDirEntry *file,
                            const char *path,
-                           int sx,
-                           int sy,
+                           const rcti *tile_draw_rect,
                            int icon,
                            int width,
                            int height,
@@ -144,10 +137,9 @@ static void file_draw_icon(const SpaceFile *sfile,
                            bool dimmed)
 {
   uiBut *but;
-  int x, y;
 
-  x = sx;
-  y = sy - height;
+  const int x = tile_draw_rect->xmin;
+  const int y = tile_draw_rect->ymax - sfile->layout->tile_border_y - height;
 
   /* For uiDefIconBut(), if a1==1.0 then a2 is alpha 0.0 - 1.0 */
   const float a1 = dimmed ? 1.0f : 0.0f;
@@ -304,8 +296,7 @@ static void file_draw_preview(const SpaceFile *sfile,
                               uiBlock *block,
                               const FileDirEntry *file,
                               const char *path,
-                              int sx,
-                              int sy,
+                              const rcti *tile_draw_rect,
                               const float icon_aspect,
                               ImBuf *imb,
                               const int icon,
@@ -357,8 +348,8 @@ static void file_draw_preview(const SpaceFile *sfile,
   fy = ((float)layout->prv_h - (float)ey) / 2.0f;
   dx = (fx + 0.5f + layout->prv_border_x);
   dy = (fy + 0.5f - layout->prv_border_y);
-  xco = sx + (int)dx;
-  yco = sy - layout->prv_h + (int)dy;
+  xco = tile_draw_rect->xmin + (int)dx;
+  yco = tile_draw_rect->ymax - layout->prv_h + (int)dy;
 
   GPU_blend(GPU_BLEND_ALPHA);
 
@@ -782,13 +773,12 @@ static const char *filelist_get_details_column_string(
 static void draw_details_columns(const FileSelectParams *params,
                                  const FileLayout *layout,
                                  FileDirEntry *file,
-                                 const int pos_x,
-                                 const int pos_y,
+                                 const rcti *tile_draw_rect,
                                  const uchar text_col[4])
 {
   const bool small_size = SMALL_SIZE_CHECK(params->thumbnail_size);
   const bool update_stat_strings = small_size != SMALL_SIZE_CHECK(layout->curr_size);
-  int sx = pos_x - layout->tile_border_x - (UI_UNIT_X * 0.1f), sy = pos_y;
+  int sx = tile_draw_rect->xmin - layout->tile_border_x - (UI_UNIT_X * 0.1f);
 
   for (FileAttributeColumnType column_type = 0; column_type < ATTRIBUTE_COLUMN_MAX;
        column_type++) {
@@ -808,7 +798,7 @@ static void draw_details_columns(const FileSelectParams *params,
 
     if (str) {
       file_draw_string(sx + ATTRIBUTE_COLUMN_PADDING,
-                       sy - layout->tile_border_y,
+                       tile_draw_rect->ymax - layout->tile_border_y,
                        IFACE_(str),
                        column->width - 2 * ATTRIBUTE_COLUMN_PADDING,
                        layout->tile_h,
@@ -820,6 +810,28 @@ static void draw_details_columns(const FileSelectParams *params,
   }
 }
 
+static rcti tile_draw_rect_get(const View2D *v2d,
+                               const FileLayout *layout,
+                               const enum eFileDisplayType display,
+                               const int file_idx,
+                               const int padx)
+{
+  int tile_pos_x, tile_pos_y;
+  ED_fileselect_layout_tilepos(layout, file_idx, &tile_pos_x, &tile_pos_y);
+  tile_pos_x += (int)(v2d->tot.xmin);
+  tile_pos_y = (int)(v2d->tot.ymax - tile_pos_y);
+
+  rcti rect;
+  rect.xmin = tile_pos_x + padx;
+  rect.xmax = rect.xmin + (ELEM(display, FILE_VERTICALDISPLAY, FILE_HORIZONTALDISPLAY) ?
+                               layout->tile_w - (2 * padx) :
+                               layout->tile_w);
+  rect.ymax = tile_pos_y;
+  rect.ymin = rect.ymax - layout->tile_h - layout->tile_border_y;
+
+  return rect;
+}
+
 void file_draw_list(const bContext *C, ARegion *region)
 {
   wmWindowManager *wm = CTX_wm_manager(C);
@@ -835,7 +847,6 @@ void file_draw_list(const bContext *C, ARegion *region)
   uiBlock *block = UI_block_begin(C, region, __func__, UI_EMBOSS);
   int numfiles;
   int numfiles_layout;
-  int sx, sy;
   int offset;
   int textwidth, textheight;
   int i;
@@ -913,12 +924,10 @@ void file_draw_list(const bContext *C, ARegion *region)
   for (i = offset; (i < numfiles) && (i < offset + numfiles_layout); i++) {
     uint file_selflag;
     char path[FILE_MAX_LIBEXTRA];
-    int padx = 0.1f * UI_UNIT_X;
+    const int padx = 0.1f * UI_UNIT_X;
     int icon_ofs = 0;
 
-    ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
-    sx += (int)(v2d->tot.xmin + padx);
-    sy = (int)(v2d->tot.ymax - sy);
+    const rcti tile_draw_rect = tile_draw_rect_get(v2d, layout, params->display, i, padx);
 
     file = filelist_file(files, i);
     file_selflag = filelist_entry_select_get(sfile->files, file, CHECK_ALL);
@@ -931,14 +940,12 @@ void file_draw_list(const bContext *C, ARegion *region)
         int colorid = (file_selflag & FILE_SEL_SELECTED) ? TH_HILITE : TH_BACK;
         int shade = (params->highlight_file == i) || (file_selflag & FILE_SEL_HIGHLIGHTED) ? 35 :
                                                                                              0;
-        const short width = ELEM(params->display, FILE_VERTICALDISPLAY, FILE_HORIZONTALDISPLAY) ?
-                                layout->tile_w - (2 * padx) :
-                                layout->tile_w;
-
         BLI_assert(i == 0 || !FILENAME_IS_CURRPAR(file->relpath));
 
-        draw_tile(
-            sx, sy - 1, width, sfile->layout->tile_h + layout->tile_border_y, colorid, shade);
+        rcti tile_bg_rect = tile_draw_rect;
+        /* One pixel downwards, places it more in the center. */
+        BLI_rcti_translate(&tile_bg_rect, 0, -1);
+        draw_tile_background(&tile_bg_rect, colorid, shade);
       }
     }
     UI_draw_roundbox_corner_set(UI_CNR_NONE);
@@ -961,8 +968,7 @@ void file_draw_list(const bContext *C, ARegion *region)
                         block,
                         file,
                         path,
-                        sx,
-                        sy,
+                        &tile_draw_rect,
                         thumb_icon_aspect,
                         imb,
                         icon,
@@ -977,8 +983,7 @@ void file_draw_list(const bContext *C, ARegion *region)
                      block,
                      file,
                      path,
-                     sx,
-                     sy - layout->tile_border_y,
+                     &tile_draw_rect,
                      filelist_geticon(files, i, true),
                      ICON_DEFAULT_WIDTH_SCALE,
                      ICON_DEFAULT_HEIGHT_SCALE,
@@ -997,8 +1002,8 @@ void file_draw_list(const bContext *C, ARegion *region)
                             UI_BTYPE_TEXT,
                             1,
                             "",
-                            sx + icon_ofs,
-                            sy - layout->tile_h - 0.15f * UI_UNIT_X,
+                            tile_draw_rect.xmin + icon_ofs,
+                            tile_draw_rect.ymin + layout->tile_border_y - 0.15f * UI_UNIT_X,
                             width - icon_ofs,
                             textheight,
                             params->renamefile,
@@ -1029,10 +1034,11 @@ void file_draw_list(const bContext *C, ARegion *region)
 
     /* file_selflag might have been modified by branch above. */
     if ((file_selflag & FILE_SEL_EDITING) == 0) {
-      const int txpos = (params->display == FILE_IMGDISPLAY) ? sx : sx + 1 + icon_ofs;
+      const int txpos = (params->display == FILE_IMGDISPLAY) ? tile_draw_rect.xmin :
+                                                               tile_draw_rect.xmin + 1 + icon_ofs;
       const int typos = (params->display == FILE_IMGDISPLAY) ?
-                            sy - layout->tile_h + layout->textheight :
-                            sy - layout->tile_border_y;
+                            tile_draw_rect.ymin + layout->tile_border_y + layout->textheight :
+                            tile_draw_rect.ymax - layout->tile_border_y;
       const int twidth = (params->display == FILE_IMGDISPLAY) ?
                              textwidth :
                       

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list