dankamongmen / notcurses

blingful character graphics/TUI library. definitely not curses.
https://nick-black.com/dankwiki/index.php/Notcurses
Other
3.4k stars 113 forks source link

Builtin way to get "topmost plane containing coordinates"? #2752

Closed zhiayang closed 5 months ago

zhiayang commented 5 months ago

Say I have some mouse position (x, y); is there a way to query notcurses to ask it for the topmost plane that contains those coordinates? Basically like a way to let me send events to the plane that the mouse is currently over.

I'm currently doing something like this, not sure if this is the most efficient way either:

for(auto plane = notcurses_top(m_notcurses); plane; plane = ncplane_below(plane))
{
    int x, y;
    uint w, h;
    ncplane_abs_yx(plane, &_y, &_x);
    ncplane_dim_yx(plane, &h, &w);
    if((x <= pos.x && pos.x < x + w) && (y <= pos.y && pos.y < y + h))
        do_stuff_with_plane(plane);
}

thanks!

dankamongmen commented 5 months ago

there is no function existing to do this, but the o(n) algorithm you've presented is exactly how it would be done.

zhiayang commented 5 months ago

okay thanks! good that i'm not doing something completely dumb LOL

dankamongmen commented 5 months ago

nope! you ask a good question, actually. for about two months in 2020 i worked on exactly this problem, for a different reason -- when we render a frame, we want to go through each viewable coordinate, starting at the top plane, and going down until we have the coordinate locked in (see "rendering" on my wiki). if we could start at the first plane that was actually on the coordinate, we could optimize that.

unfortunately, doing so required keeping a good bit of data around for the fast lookup. updating this data knocked out any benefits i could make it provide. furthermore, there just aren't many cases where you have the big-ass pile of planes necessary to get a win off this.

so just take the O(1) ncpile_top() and the O(n) ncplane_below() walk as you've done here.

btw, what are you working on with notcurses, if i might ask?

zhiayang commented 5 months ago

right, that makes sense!

i'm working on a top program (because the world needs more of those, right?), which I think you might've seen a glimpse of in one of the other issues (:

this is what it currently looks like:

image
dankamongmen commented 5 months ago

totally sweet!

one thing i'd recommend: if you're not using the graph feature of notcurses for your graphical load, you might want to. a lot of fonts don't support braille, or have really weird-looking braille. if you are, fucking fantastic!

i love it!

zhiayang commented 5 months ago

i actually implemented that before i poked around and realised you already had something for that LOL. i should probably use the library version, yea. i'm not even checking if the terminal supports braille atm xD

actually i remembered why:

image

hehe