[Bf-blender-cvs] [079f415debd] master: Cleanup: use enum types for screen direction variables

Campbell Barton noreply at git.blender.org
Fri May 14 16:51:04 CEST 2021


Commit: 079f415debd85b44f9ceaca17edfb82517240448
Author: Campbell Barton
Date:   Thu May 13 15:43:53 2021 +1000
Branches: master
https://developer.blender.org/rB079f415debd85b44f9ceaca17edfb82517240448

Cleanup: use enum types for screen direction variables

The term direction was used in 3 different ways in screen editing code,
making it hard to follow:

- 0-3 for as magic numbers mapped to [west,north,east,south].
- `h`, `v` characters for [horizontal,vertical] axes.
- Cycle direction SPACE_CONTEXT_CYCLE_PREV, SPACE_CONTEXT_CYCLE_NEXT

The following changes have been made:

- Add `eScreenDir` for [west,north,east,south], use variable name `dir`.
- Add `eScreenAxis` for [horizontal,vertical] values, use variable name
  `dir_axis`.
- Add `eScreenCycle` for existing enum `SPACE_CONTEXT_CYCLE_{PREV/NEXT}`.
- Add macros `SCREEN_DIR_IS_VERTICAL(dir)`,
  `SCREEN_DIR_IS_HORIZONTAL(dir)`.
  Replacing `ELEM(dir, 1, 3)`, `ELEM(dir, 0, 2)`.
- Move `ED_screen_draw_join_highlight`, `ED_screen_draw_split_preview`
  to `screen_intern.h`.

Reviewed By: Severin

Ref D11245

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

M	source/blender/editors/include/ED_screen.h
M	source/blender/editors/screen/area.c
M	source/blender/editors/screen/screen_draw.c
M	source/blender/editors/screen/screen_edit.c
M	source/blender/editors/screen/screen_geometry.c
M	source/blender/editors/screen/screen_intern.h
M	source/blender/editors/screen/screen_ops.c

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

diff --git a/source/blender/editors/include/ED_screen.h b/source/blender/editors/include/ED_screen.h
index c46f0f78eb5..bdd7ec571dc 100644
--- a/source/blender/editors/include/ED_screen.h
+++ b/source/blender/editors/include/ED_screen.h
@@ -200,8 +200,6 @@ ScrArea *ED_screen_areas_iter_next(const bScreen *screen, const ScrArea *area);
 /* screens */
 void ED_screens_init(struct Main *bmain, struct wmWindowManager *wm);
 void ED_screen_draw_edges(struct wmWindow *win);
-void ED_screen_draw_join_highlight(struct ScrArea *sa1, struct ScrArea *sa2);
-void ED_screen_draw_split_preview(struct ScrArea *area, const int dir, const float fac);
 void ED_screen_refresh(struct wmWindowManager *wm, struct wmWindow *win);
 void ED_screen_ensure_updated(struct wmWindowManager *wm,
                               struct wmWindow *win,
@@ -450,10 +448,10 @@ enum {
 };
 
 /* SCREEN_OT_space_context_cycle direction */
-enum {
+typedef enum eScreenCycle {
   SPACE_CONTEXT_CYCLE_PREV,
   SPACE_CONTEXT_CYCLE_NEXT,
-};
+} eScreenCycle;
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/editors/screen/area.c b/source/blender/editors/screen/area.c
index bd2b1c4c553..b83eccdcfdd 100644
--- a/source/blender/editors/screen/area.c
+++ b/source/blender/editors/screen/area.c
@@ -1172,12 +1172,12 @@ static void region_azones_add(const bScreen *screen, ScrArea *area, ARegion *reg
 }
 
 /* dir is direction to check, not the splitting edge direction! */
-static int rct_fits(const rcti *rect, char dir, int size)
+static int rct_fits(const rcti *rect, const eScreenAxis dir_axis, int size)
 {
-  if (dir == 'h') {
+  if (dir_axis == SCREEN_AXIS_H) {
     return BLI_rcti_size_x(rect) + 1 - size;
   }
-  /* 'v' */
+  /* Vertical. */
   return BLI_rcti_size_y(rect) + 1 - size;
 }
 
@@ -1398,7 +1398,8 @@ static void region_rect_recursive(
       region->flag |= RGN_FLAG_TOO_SMALL;
     }
   }
-  else if (rct_fits(remainder, 'v', 1) < 0 || rct_fits(remainder, 'h', 1) < 0) {
+  else if (rct_fits(remainder, SCREEN_AXIS_V, 1) < 0 ||
+           rct_fits(remainder, SCREEN_AXIS_H, 1) < 0) {
     /* remainder is too small for any usage */
     region->flag |= RGN_FLAG_TOO_SMALL;
   }
@@ -1410,11 +1411,11 @@ static void region_rect_recursive(
   else if (ELEM(alignment, RGN_ALIGN_TOP, RGN_ALIGN_BOTTOM)) {
     rcti *winrct = (region->overlap) ? overlap_remainder : remainder;
 
-    if ((prefsizey == 0) || (rct_fits(winrct, 'v', prefsizey) < 0)) {
+    if ((prefsizey == 0) || (rct_fits(winrct, SCREEN_AXIS_V, prefsizey) < 0)) {
       region->flag |= RGN_FLAG_TOO_SMALL;
     }
     else {
-      int fac = rct_fits(winrct, 'v', prefsizey);
+      int fac = rct_fits(winrct, SCREEN_AXIS_V, prefsizey);
 
       if (fac < 0) {
         prefsizey += fac;
@@ -1436,11 +1437,11 @@ static void region_rect_recursive(
   else if (ELEM(alignment, RGN_ALIGN_LEFT, RGN_ALIGN_RIGHT)) {
     rcti *winrct = (region->overlap) ? overlap_remainder : remainder;
 
-    if ((prefsizex == 0) || (rct_fits(winrct, 'h', prefsizex) < 0)) {
+    if ((prefsizex == 0) || (rct_fits(winrct, SCREEN_AXIS_H, prefsizex) < 0)) {
       region->flag |= RGN_FLAG_TOO_SMALL;
     }
     else {
-      int fac = rct_fits(winrct, 'h', prefsizex);
+      int fac = rct_fits(winrct, SCREEN_AXIS_H, prefsizex);
 
       if (fac < 0) {
         prefsizex += fac;
@@ -1464,7 +1465,7 @@ static void region_rect_recursive(
     region->winrct = *remainder;
 
     if (alignment == RGN_ALIGN_HSPLIT) {
-      if (rct_fits(remainder, 'h', prefsizex) > 4) {
+      if (rct_fits(remainder, SCREEN_AXIS_H, prefsizex) > 4) {
         region->winrct.xmax = BLI_rcti_cent_x(remainder);
         remainder->xmin = region->winrct.xmax + 1;
       }
@@ -1473,7 +1474,7 @@ static void region_rect_recursive(
       }
     }
     else {
-      if (rct_fits(remainder, 'v', prefsizey) > 4) {
+      if (rct_fits(remainder, SCREEN_AXIS_V, prefsizey) > 4) {
         region->winrct.ymax = BLI_rcti_cent_y(remainder);
         remainder->ymin = region->winrct.ymax + 1;
       }
diff --git a/source/blender/editors/screen/screen_draw.c b/source/blender/editors/screen/screen_draw.c
index 6d1409a9044..2c45524ef94 100644
--- a/source/blender/editors/screen/screen_draw.c
+++ b/source/blender/editors/screen/screen_draw.c
@@ -237,23 +237,25 @@ void ED_screen_draw_edges(wmWindow *win)
  * \param sa1: Area from which the resultant originates.
  * \param sa2: Target area that will be replaced.
  */
-void ED_screen_draw_join_highlight(ScrArea *sa1, ScrArea *sa2)
+void screen_draw_join_highlight(ScrArea *sa1, ScrArea *sa2)
 {
-  int dir = area_getorientation(sa1, sa2);
-  if (dir == -1) {
+  const eScreenDir dir = area_getorientation(sa1, sa2);
+  if (dir == SCREEN_DIR_NONE) {
     return;
   }
 
   /* Rect of the combined areas.*/
-  bool vertical = ELEM(dir, 1, 3);
-  rctf combined = {.xmin = vertical ? MAX2(sa1->totrct.xmin, sa2->totrct.xmin) :
-                                      MIN2(sa1->totrct.xmin, sa2->totrct.xmin),
-                   .xmax = vertical ? MIN2(sa1->totrct.xmax, sa2->totrct.xmax) :
-                                      MAX2(sa1->totrct.xmax, sa2->totrct.xmax),
-                   .ymin = vertical ? MIN2(sa1->totrct.ymin, sa2->totrct.ymin) :
-                                      MAX2(sa1->totrct.ymin, sa2->totrct.ymin),
-                   .ymax = vertical ? MAX2(sa1->totrct.ymax, sa2->totrct.ymax) :
-                                      MIN2(sa1->totrct.ymax, sa2->totrct.ymax)};
+  const bool vertical = SCREEN_DIR_IS_VERTICAL(dir);
+  const rctf combined = {
+      .xmin = vertical ? MAX2(sa1->totrct.xmin, sa2->totrct.xmin) :
+                         MIN2(sa1->totrct.xmin, sa2->totrct.xmin),
+      .xmax = vertical ? MIN2(sa1->totrct.xmax, sa2->totrct.xmax) :
+                         MAX2(sa1->totrct.xmax, sa2->totrct.xmax),
+      .ymin = vertical ? MIN2(sa1->totrct.ymin, sa2->totrct.ymin) :
+                         MAX2(sa1->totrct.ymin, sa2->totrct.ymin),
+      .ymax = vertical ? MAX2(sa1->totrct.ymax, sa2->totrct.ymax) :
+                         MIN2(sa1->totrct.ymax, sa2->totrct.ymax),
+  };
 
   uint pos_id = GPU_vertformat_attr_add(
       immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
@@ -320,7 +322,7 @@ void ED_screen_draw_join_highlight(ScrArea *sa1, ScrArea *sa2)
   UI_draw_roundbox_4fv(&combined, false, 7 * U.pixelsize, (float[4]){1.0f, 1.0f, 1.0f, 0.8f});
 }
 
-void ED_screen_draw_split_preview(ScrArea *area, const int dir, const float fac)
+void screen_draw_split_preview(ScrArea *area, const eScreenAxis dir_axis, const float fac)
 {
   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
   immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
@@ -332,7 +334,7 @@ void ED_screen_draw_split_preview(ScrArea *area, const int dir, const float fac)
 
   immBegin(GPU_PRIM_LINES, 2);
 
-  if (dir == 'h') {
+  if (dir_axis == SCREEN_AXIS_H) {
     const float y = (1 - fac) * area->totrct.ymin + fac * area->totrct.ymax;
 
     immVertex2f(pos, area->totrct.xmin, y);
@@ -350,7 +352,7 @@ void ED_screen_draw_split_preview(ScrArea *area, const int dir, const float fac)
     immEnd();
   }
   else {
-    BLI_assert(dir == 'v');
+    BLI_assert(dir_axis == SCREEN_AXIS_V);
     const float x = (1 - fac) * area->totrct.xmin + fac * area->totrct.xmax;
 
     immVertex2f(pos, x, area->totrct.ymin);
diff --git a/source/blender/editors/screen/screen_edit.c b/source/blender/editors/screen/screen_edit.c
index 03a5b67149a..6fb5f33d836 100644
--- a/source/blender/editors/screen/screen_edit.c
+++ b/source/blender/editors/screen/screen_edit.c
@@ -107,7 +107,7 @@ static void screen_delarea(bContext *C, bScreen *screen, ScrArea *area)
 ScrArea *area_split(const wmWindow *win,
                     bScreen *screen,
                     ScrArea *area,
-                    char dir,
+                    const eScreenAxis dir_axis,
                     const float fac,
                     const bool merge)
 {
@@ -120,7 +120,7 @@ ScrArea *area_split(const wmWindow *win,
   rcti window_rect;
   WM_window_rect_calc(win, &window_rect);
 
-  short split = screen_geom_find_area_split_point(area, &window_rect, dir, fac);
+  short split = screen_geom_find_area_split_point(area, &window_rect, dir_axis, fac);
   if (split == 0) {
     return NULL;
   }
@@ -129,7 +129,7 @@ ScrArea *area_split(const wmWindow *win,
    * normally it shouldn't matter which is used since the copy should match the original
    * however with viewport rendering and python console this isn't the case. - campbell */
 
-  if (dir == 'h') {
+  if (dir_axis == SCREEN_AXIS_H) {
     /* new vertices */
     ScrVert *sv1 = screen_geom_vertex_add(screen, area->v1->vec.x, split);
     ScrVert *sv2 = screen_geom_vertex_add(screen, area->v4->vec.x, split);
@@ -288,10 +288,10 @@ void screen_new_activate_prepare(const wmWindow *win, bScreen *screen_new)
  * -1 = not valid check.
  * used with join operator.
  */
-int area_getorientation(ScrArea *sa_a, ScrArea *sa_b)
+eScreenDir area_getorientation(ScrArea *sa_a, ScrArea *sa_b)
 {
   if (sa_a == NULL || sa_b == NULL || sa_a == sa_b) {
-    return -1;
+    return SCREEN_DIR_NONE;
   }
 
   const vec2s *sa_bl = &sa_a->v1->vec;
@@ -331,29 +331,31 @@ int area_getorientation(ScrArea *sa_a, ScrArea *sa_b)
 /**
  * Get alignment offset of adjacent areas. 'dir' value is like #area_getorientation().
  */
-void area_getoffsets(ScrArea *sa_a, ScrArea *sa_b, const int dir, int *r_offset1, int *r_offset2)
+void area_getoffsets(
+    ScrArea *sa_a, ScrArea *sa_b, const eScreenDir dir, int *r_offset1, int *r_offset2)
 {
   if (sa_a == NULL || sa_b == NULL) {
     *r_offset1 = INT_MAX;
     *r_offset2 = INT_MAX;
   }
-  else if (dir == 0) { /* West: sa on right and sa_b to the left. */
+  else if (dir == SCREEN_DIR_W) { /* West: sa on right and sa_b to the left. */
     *r_offset1 = sa_b->v3->vec.y - sa_a->v2->vec.y;
     *r_offset2 = sa_b->v4->vec.y - sa_a->v1->vec.y;
   }
-  else if (dir == 1) { /* North: sa below and sa_b above. */
+  else if (dir == SCREEN_DIR_N) { /* North: sa below and sa_b above. */
     *r_offset1 = sa_a->v2->vec.x - sa_b->v1->vec.x;
     *r_offset2 = sa_a->v3->vec.x - sa_b->v4->vec.x;
   }
-  else if (dir == 2) { /

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list