recenter post-rotation in normal demo #617

This commit is contained in:
nick black 2020-05-19 19:38:38 -04:00
parent bcdf513e31
commit 99bd3fd597
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
5 changed files with 31 additions and 27 deletions

View File

@ -1232,6 +1232,9 @@ API char* ncplane_contents(const struct ncplane* nc, int begy, int begx,
API void* ncplane_set_userptr(struct ncplane* n, void* opaque);
API void* ncplane_userptr(struct ncplane* n);
API void ncplane_center_abs(const struct ncplane* n, int* RESTRICT y,
int* RESTRICT x);
// Return the column at which 'c' cols ought start in order to be aligned
// according to 'align' within ncplane 'n'. Returns INT_MAX on invalid 'align'.
// Undefined behavior on negative 'c'.

View File

@ -96,16 +96,30 @@ int normal_demo(struct notcurses* nc){
cell_release(nstd, &c);
for(int i = 0 ; i < 16 ; ++i){
demo_nanosleep(nc, &scaled);
int centy, centx;
ncplane_center_abs(n, &centy, &centx);
if(ncplane_rotate_cw(n)){
goto err;
}
int cent2y, cent2x;
int absy, absx;
ncplane_center_abs(n, &cent2y, &cent2x);
ncplane_yx(n, &absy, &absx);
ncplane_move_yx(n, absy + centy - cent2y, absx + centx - cent2x);
DEMO_RENDER(nc);
}
for(int i = 0 ; i < 16 ; ++i){
demo_nanosleep(nc, &scaled);
int centy, centx;
ncplane_center_abs(n, &centy, &centx);
if(ncplane_rotate_ccw(n)){
goto err;
}
int cent2y, cent2x;
int absy, absx;
ncplane_center_abs(n, &cent2y, &cent2x);
ncplane_yx(n, &absy, &absx);
ncplane_move_yx(n, absy + centy - cent2y, absx + centx - cent2x);
DEMO_RENDER(nc);
}
ncplane_destroy(n);

View File

@ -563,11 +563,6 @@ int ncplane_rotate_cw(ncplane* n){
}
int ret = rotate_merge(n, newp);
ret |= ncplane_destroy(newp);
int cent2y, cent2x;
int absy, absx;
ncplane_center_abs(n, &cent2y, &cent2x);
ncplane_yx(n, &absy, &absx);
ncplane_move_yx(n, absy + centy - cent2y, absx + centx - cent2x);
return ret;
}
@ -578,8 +573,6 @@ int ncplane_rotate_ccw(ncplane* n){
}
int dimy, dimx, targdimy, targdimx;
ncplane_dim_yx(n, &dimy, &dimx);
int centy, centx;
ncplane_center_abs(n, &centy, &centx);
ncplane_dim_yx(newp, &targdimy, &targdimx);
int x = dimx - 2, y;
// Each row of the target plane is taken from a column of the source plane.
@ -597,11 +590,6 @@ int ncplane_rotate_ccw(ncplane* n){
}
int ret = rotate_merge(n, newp);
ret |= ncplane_destroy(newp);
int cent2y, cent2x;
int absy, absx;
ncplane_center_abs(n, &cent2y, &cent2x);
ncplane_yx(n, &absy, &absx);
ncplane_move_yx(n, absy + centy - cent2y, absx + centx - cent2x);
return ret;
}

View File

@ -648,21 +648,6 @@ ncplane_center(const ncplane* n, int* RESTRICT y, int* RESTRICT x){
center_box(y, x);
}
// find the center coordinate of a plane, preferring the top/left in the
// case of an even number of rows/columns (in such a case, there will be one
// more cell to the bottom/right of the center than the top/left). the
// center is then modified relative to the plane's origin.
static inline void
ncplane_center_abs(const ncplane* n, int* RESTRICT y, int* RESTRICT x){
ncplane_center(n, y, x);
if(y){
*y += n->absy;
}
if(x){
*x += n->absx;
}
}
int ncvisual_bounding_box(const struct ncvisual* ncv, int* leny, int* lenx,
int* offy, int* offx);

View File

@ -2003,3 +2003,17 @@ int cells_rounded_box(struct ncplane* n, uint32_t attr, uint64_t channels,
}
return cells_ascii_box(n, attr, channels, ul, ur, ll, lr, hl, vl);
}
// find the center coordinate of a plane, preferring the top/left in the
// case of an even number of rows/columns (in such a case, there will be one
// more cell to the bottom/right of the center than the top/left). the
// center is then modified relative to the plane's origin.
void ncplane_center_abs(const ncplane* n, int* RESTRICT y, int* RESTRICT x){
ncplane_center(n, y, x);
if(y){
*y += n->absy;
}
if(x){
*x += n->absx;
}
}