lnvis

nanovg lightning network visualizer
git clone git://jb55.com/lnvis
Log | Files | Refs | README | LICENSE

commit 0572d3de479222937a981f0a3275a3fa3cd58bea
parent 75b582227aae98f9fc3d4af5f8fb7f6031351781
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 12 Aug 2018 23:15:30 -0700

click filtering

Diffstat:
Mdefs.h | 1+
Mln.c | 7++-----
Mln.h | 5++++-
Mmain.c | 2+-
Mrender.c | 50++++++++++++++++++++++++++++++++++++++++----------
Mupdate.c | 4++++
6 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/defs.h b/defs.h @@ -35,6 +35,7 @@ static inline int streq(const char *a, const char *b) _a > _b ? _a : _b; }) +#define nodeid_eq streq #endif /* LNVIS_DEFS_H */ diff --git a/ln.c b/ln.c @@ -113,7 +113,7 @@ static void print_node(struct node *node) -void filter_network(const char *nodeid, struct ln *ln) +void filter_network(const char *nodeid, struct node *filter_node, struct ln *ln) { u32 i; struct node *node = NULL; @@ -123,11 +123,8 @@ void filter_network(const char *nodeid, struct ln *ln) for (i = 0; i < ln->node_count; i++) { node = &ln->nodes[i]; - if (streq(nodeid, node->id) || rand_0to1() < 0.002) { + if (filter_node == node || (nodeid && streq(nodeid, node->id))) node->filtered = 1; - printf("filtering "); - print_node(node); - } else node->filtered = 0; } diff --git a/ln.h b/ln.h @@ -96,6 +96,9 @@ struct channel { u32 fee_per_millionth; u64 satoshis; + + // app specific stuff + int draw_last; }; enum display_flags { @@ -135,6 +138,6 @@ void init_ln(struct ln *ln, int grid_div); void free_ln(struct ln *ln); void random_network(int ww, int wh, int max_per_node, int num_nodes, struct ln *ln); void init_network(int ww, int wh, struct ln *ln); -void filter_network(const char *nodeid, struct ln *ln); +void filter_network(const char *nodeid, struct node *filter_node, struct ln *ln); #endif /* LNVIS_LN_H */ diff --git a/main.c b/main.c @@ -302,7 +302,7 @@ int main() if (first) { /* random_network(winWidth, winHeight, 3, 500, &ln); */ init_network(winWidth, winHeight, &ln); - filter_network(filter, &ln); + filter_network(filter, NULL, &ln); first = 0; } diff --git a/render.c b/render.c @@ -28,19 +28,28 @@ void draw_channel(NVGcontext *vg, struct ln *ln, struct channel *channel) n1t.nvg_color = n1->color.nvg_color; n2t.nvg_color = n2->color.nvg_color; - if (n1 != ln->last_drag_target) - n1t.a = 0.4; + NVGcolor c; - if (n2 != ln->last_drag_target) - n2t.a = 0.4; + if (nodeid_eq(channel->source, n1->id)) + c = n1t.nvg_color; + else + c = n2t.nvg_color; + + if (channel->nodes[0] == ln->last_drag_target || + channel->nodes[1] == ln->last_drag_target) + c.a = 1.0; + else + c.a = 0.4; - NVGpaint linear_grad = - nvgLinearGradient(vg, sx, sy, ex, ey, n1t.nvg_color, n2t.nvg_color); + + /* NVGpaint linear_grad = */ + /* nvgLinearGradient(vg, sx, sy, ex, ey, n1t.nvg_color, n2t.nvg_color); */ nvgSave(vg); nvgStrokeWidth(vg, stroke); - nvgStrokePaint(vg, linear_grad); + /* nvgStrokePaint(vg, linear_grad); */ + nvgStrokeColor(vg, c); nvgBeginPath(vg); nvgMoveTo(vg, n1->x, n1->y); nvgLineTo(vg, n2->x, n2->y); @@ -155,7 +164,7 @@ void draw_node(NVGcontext *vg, struct ln *ln, struct node *node) if (ln->display_flags & DISP_ALIASES) { nvgBeginPath(vg); - nvgRoundedRect(vg, -bounds[2] / 2.0, bounds[3], bounds[2], bounds[3], 5.0); + nvgRoundedRect(vg, -bounds[2] / 2.0 - pad, bounds[3] - pad, bounds[2] + pad * 2.0, bounds[3] + pad * 2.0, 5.0); nvgFillColor(vg, nvgRGBAf(node_color.r, node_color.g, node_color.b, 0.9)); nvgFill(vg); @@ -172,11 +181,32 @@ void render_ln(struct ln *ln) u32 i; NVGcontext *vg = ln->vg; + struct channel *draw_last = NULL, *c = NULL; + draw_grid(vg, ln); // render channels first - for (i = 0; i < ln->channel_count; i++) - draw_channel(vg, ln, &ln->channels[i]); + for (i = 0; i < ln->channel_count; i++) { + c = &ln->channels[i]; + + if (c->nodes[0] == ln->last_drag_target || + c->nodes[1] == ln->last_drag_target) + c->draw_last = 1; + else + draw_channel(vg, ln, c); + } + + // draw important channels last + for (i = 0; i < ln->channel_count; i++) { + c = &ln->channels[i]; + + if (!c->draw_last) + continue; + + c->draw_last = 0; + draw_channel(vg, ln, c); + } + for (i = 0; i < ln->node_count; i++) draw_node(vg, ln, &ln->nodes[i]); diff --git a/update.c b/update.c @@ -9,6 +9,8 @@ struct node *hit_node(struct ln *ln) { for (u32 i = 0; i < ln->node_count; ++i) { struct node *n = &ln->nodes[i]; + if (!n->filtered) + continue; const double dx = fabs(n->x - ln->mx); const double dy = fabs(n->y - ln->my); @@ -157,6 +159,8 @@ void update(struct ln *ln, double dt) struct node *hit = hit_node(ln); ln->drag_target = hit; ln->last_drag_target = hit; + if (hit != NULL) + filter_network(NULL, hit, ln); } // stop dragging