ncplane_mouseevent_p()->ncplane_translate_abs()

ncplane_mouseevent_p() is retired--it was poorly named, and
ncplane_translate_abs() does what it does, plus more, plus
more generally (it works on any y, x, not necessarily an
ncinput). update c++ wrappers #394.
This commit is contained in:
nick black 2020-03-03 00:59:35 -05:00
parent 6966632821
commit 3498956370
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
10 changed files with 49 additions and 33 deletions

View File

@ -51,7 +51,6 @@ libnotcurses.so.1 libnotcurses1 #MINVER#
ncplane_gradient@Base 1.2.1
ncplane_greyscale@Base 1.2.1
ncplane_hline_interp@Base 1.2.1
ncplane_mouseevent_p@Base 1.2.2
ncplane_move_above_unsafe@Base 1.2.1
ncplane_move_below_unsafe@Base 1.2.1
ncplane_move_bottom@Base 1.2.1
@ -90,6 +89,7 @@ libnotcurses.so.1 libnotcurses1 #MINVER#
ncplane_styles_on@Base 1.2.1
ncplane_styles_set@Base 1.2.1
ncplane_translate@Base 1.2.2
ncplane_translate_abs@Base 1.2.3
ncplane_userptr@Base 1.2.1
ncplane_visual_open@Base 1.2.1
ncplane_vline_interp@Base 1.2.1

View File

@ -876,17 +876,17 @@ namespace ncpp
ncplane_translate (src.plane, dst.plane, y, x);
}
bool translate_abs (int *y = nullptr, int *x = nullptr)
{
return ncplane_translate_abs (*this, y, x);
}
// Upstream call doesn't take ncplane* but we put it here for parity with has_no_background below
bool has_no_foreground (Cell &cell) const noexcept
{
return cell.has_no_foreground ();
}
bool is_mouse_event (const ncinput *ni) const noexcept
{
return ncplane_mouseevent_p (plane, ni);
}
const char* get_extended_gcluster (Cell &cell) const noexcept
{
return cell_extended_gcluster (plane, cell);

View File

@ -328,11 +328,6 @@ API int notcurses_mouse_enable(struct notcurses* n);
// Disable mouse events. Any events in the input queue can still be delivered.
API int notcurses_mouse_disable(struct notcurses* n);
// Was the provided mouse event 'ni' within the bounds of the ncplane 'n'? Note
// that this doesn't necessarily mean the event affected 'n'; there could be a
// plane above it, this plane could be transparent, etc.
API bool ncplane_mouseevent_p(const struct ncplane* n, const struct ncinput *ni);
// Refresh our idea of the terminal's dimensions, reshaping the standard plane
// if necessary, without a fresh render. References to ncplanes (and the
// egcpools underlying cells) remain valid following a resize operation.
@ -414,6 +409,12 @@ API struct ncplane* ncplane_dup(struct ncplane* n, void* opaque);
API void ncplane_translate(const struct ncplane* src, const struct ncplane* dst,
int* RESTRICT y, int* RESTRICT x);
// Fed absolute 'y'/'x' coordinates, determine whether that coordinate is
// within the ncplane 'n'. If not, return false. If so, return true. Either
// way, translate the absolute coordinates relative to 'n'. If the point is not
// within 'n', these coordinates will not be within the dimensions of the plane.
API bool ncplane_translate_abs(const struct ncplane* n, int* RESTRICT y, int* RESTRICT x);
// Returns a 16-bit bitmask of supported curses-style attributes
// (NCSTYLE_UNDERLINE, NCSTYLE_BOLD, etc.) The attribute is only
// indicated as supported if the terminal can support it together with color.

View File

@ -315,6 +315,11 @@ int hud_grab(int y, int x){
// new grab. stash point of original grab, and location of HUD at original
// grab. any delta while grabbed (relative to the original grab point)
// will see the HUD moved by delta (relative to the original HUD location).
int ty = y, tx = x;
// first, though, verify that we're clicking within the hud
if(!ncplane_translate_abs(hud, &ty, &tx)){
return 0;
}
hud_grab_x = x;
hud_grab_y = y;
ncplane_yx(hud, &hud_pos_y, &hud_pos_x);

View File

@ -76,6 +76,7 @@ handle_mouse(const ncinput* ni){
if(ni->id != NCKEY_BUTTON1 && ni->id != NCKEY_RELEASE){
return 0;
}
int y, x;
int ret;
if(ni->id == NCKEY_RELEASE){
ret = hud_release();

View File

@ -491,20 +491,3 @@ int prep_special_keys(notcurses* nc){
}
return 0;
}
bool ncplane_mouseevent_p(const ncplane* n, const ncinput *ni){
// incoming coordinates are absolute and 0-indexed
int y = ni->y;
int x = ni->x;
ncplane_translate(ncplane_stdplane_const(n), n, &y, &x);
if(y < 0 || x < 0){
return false;
}
if(y >= n->leny){
return false;
}
if(x >= n->lenx){
return false;
}
return true;
}

View File

@ -508,12 +508,14 @@ const char* ncmenu_selected(const ncmenu* n, ncinput* ni){
}
bool ncmenu_offer_input(ncmenu* n, const ncinput* nc){
if(nc->id == NCKEY_RELEASE && ncplane_mouseevent_p(n->ncp, nc)){
if(nc->id == NCKEY_RELEASE){
int y, x, dimy, dimx;
y = nc->y;
x = nc->x;
ncplane_dim_yx(n->ncp, &dimy, &dimx);
ncplane_translate(ncplane_stdplane(n->ncp), n->ncp, &y, &x);
if(!ncplane_translate_abs(n->ncp, &y, &x)){
return false;
}
if(y != (n->bottom ? dimy - 1 : 0)){
return false;
}

View File

@ -1895,6 +1895,27 @@ void palette256_free(palette256* p){
free(p);
}
bool ncplane_translate_abs(const ncplane* n, int* restrict y, int* restrict x){
ncplane_translate(ncplane_stdplane_const(n), n, y, x);
if(y){
if(*y < 0){
return false;
}
if(*y >= n->leny){
return false;
}
}
if(x){
if(*x < 0){
return false;
}
if(*x >= n->lenx){
return false;
}
}
return true;
}
void ncplane_translate(const ncplane* src, const ncplane* dst,
int* restrict y, int* restrict x){
if(y){

View File

@ -337,9 +337,11 @@ bool ncselector_offer_input(ncselector* n, const ncinput* nc){
}else if(nc->id == NCKEY_SCROLL_DOWN){
ncselector_nextitem(n);
return true;
}else if(nc->id == NCKEY_RELEASE && ncplane_mouseevent_p(n->ncp, nc)){
}else if(nc->id == NCKEY_RELEASE){
int y = nc->y, x = nc->x;
ncplane_translate(ncplane_stdplane(n->ncp), n->ncp, &y, &x);
if(!ncplane_translate_abs(n->ncp, &y, &x)){
return false;
}
if(y == n->uarrowy && x == n->arrowx){
ncselector_previtem(n);
return true;

View File

@ -1005,7 +1005,8 @@ TEST_CASE("NCPlane") {
int total = 0;
for(ni.y = 0 ; ni.y < 5 ; ++ni.y){
for(ni.x = 0 ; ni.x < 5 ; ++ni.x){
bool p = ncplane_mouseevent_p(n, &ni);
int y = ni.y, x = ni.x;
bool p = ncplane_translate_abs(n, &y, &x);
if(ni.y >= 1 && ni.y <= 2 && ni.x >= 1 && ni.x <= 2){
CHECK(p);
}else{