lnvis

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

commit 7370f14e96bda2dc0c8aaaf490bf9fb1429f84cb
parent dbb64e50fd2d68d22d608beed03937001053f387
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  7 Aug 2018 22:23:37 -0700

display flags

toggle between various options

Diffstat:
Mln.c | 2+-
Mln.h | 9++++++++-
Mmain.c | 58+++++++++++++++++++++++++++++++++++++++++++++-------------
Mrender.c | 42++++++++++++++++++++++++++++--------------
4 files changed, 82 insertions(+), 29 deletions(-)

diff --git a/ln.c b/ln.c @@ -48,7 +48,7 @@ void random_network(int ww, int wh, int max_per_node, int num_nodes, struct ln * n->ay = 0.0; n->vx = 0.0; n->vy = 0.0; - n->size = 10; + n->size = 8; } // connect nodes randomly diff --git a/ln.h b/ln.h @@ -77,6 +77,13 @@ struct channel { u32 last_update; }; +enum display_flags { + DISP_DARK = 1UL << 0, + DISP_GRID = 1UL << 1, + DISP_ALIASES = 1UL << 2, + DISP_STROKE_NODES = 1UL << 3, +}; + struct ln { NVGcontext *vg; @@ -87,7 +94,7 @@ struct ln { int window_height; union color clear_color; - int dark_theme; + u64 display_flags; int grid_div; struct cell *grid; diff --git a/main.c b/main.c @@ -37,10 +37,20 @@ static void key(GLFWwindow *window, int key, int scancode, int action, int mods) premult = !premult; } +static struct ln ln; static double mx, my; static int mdown = 0; static int mclicked = 0; +static const union color dark_color = { + .rgba = { 0x28 / 255.0, 0x2c / 255.0, 0x34 / 255.0, 1.0f } +}; + +static const union color light_color = { + .rgba = { 1.0, 1.0, 1.0, 1.0f } +}; + + void mouse_pos(GLFWwindow *win, double x, double y) { (void)win; @@ -62,6 +72,33 @@ void mouse_click(GLFWwindow *win, int button, int action, int mods) } +void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + (void)mods; + (void)scancode; + (void)window; + + if (action == GLFW_PRESS) { + switch (key) { + case GLFW_KEY_A: + ln.display_flags ^= DISP_ALIASES; + break; + case GLFW_KEY_S: + ln.display_flags ^= DISP_STROKE_NODES; + break; + case GLFW_KEY_G: + ln.display_flags ^= DISP_GRID; + break; + case GLFW_KEY_T: + if (ln.display_flags & DISP_DARK) + memcpy(&ln.clear_color, &light_color, sizeof(ln.clear_color)); + else + memcpy(&ln.clear_color, &dark_color, sizeof(ln.clear_color)); + ln.display_flags ^= DISP_DARK; + break; + } + } +} int main() { @@ -78,18 +115,13 @@ int main() static const int grid_div = 20; static const int dark_theme = 1; - static const union color dark_color = { - .rgba = { 0x28 / 255.0, 0x2c / 255.0, 0x34 / 255.0, 1.0f } - }; + u64 flags = DISP_DARK + /* | DISP_ALIASES */ + | DISP_GRID + /* | DISP_STROKE_NODES */ + ; - static const union color light_color = { - .rgba = { 1.0, 1.0, 1.0, 1.0f } - }; - - // clear color - struct ln ln = { - .dark_theme = dark_theme - }; + ln.display_flags = flags; if (dark_theme) memcpy(&ln.clear_color, &dark_color, sizeof(ln.clear_color)); @@ -149,7 +181,7 @@ int main() glfwSetTime(0); prevt = glfwGetTime(); - + glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_click); glfwSetCursorPosCallback(window, mouse_pos); @@ -180,7 +212,7 @@ int main() ln.window_width = winWidth; if (first) { - random_network(winWidth, winHeight, 3, 200, &ln); + random_network(winWidth, winHeight, 3, 500, &ln); printf("channels %d\n", ln.channel_count); first = 0; } diff --git a/render.c b/render.c @@ -33,7 +33,17 @@ void draw_channel(NVGcontext *vg, struct channel *channel) } -void draw_grid(NVGcontext *vg, int ww, int wh, int grid_div) { +void draw_grid(NVGcontext *vg, struct ln *ln) { + static const float adj = 0.6f; + const int grid_div = ln->grid_div; + const int ww = ln->window_width; + const int wh = ln->window_height; + + NVGcolor clear_adj = + nvgRGBAf(ln->clear_color.r * adj, + ln->clear_color.g * adj, + ln->clear_color.b * adj, + 1.0f); for (int x = 0; x < grid_div; ++x) { double px = ww / grid_div * x; @@ -42,7 +52,7 @@ void draw_grid(NVGcontext *vg, int ww, int wh, int grid_div) { nvgBeginPath(vg); nvgMoveTo(vg, px, 0); nvgLineTo(vg, px, wh); - /* nvgStrokeColor(vg, ) */ + nvgStrokeColor(vg, clear_adj); nvgStroke(vg); } @@ -93,9 +103,10 @@ void draw_node(NVGcontext *vg, struct ln *ln, struct node *node) nvgTranslate(vg, node->x, node->y); const float light = 2.0f; + const int dark_theme = ln->display_flags & DISP_DARK; // TODO: use brightness instead of clear color for white theme - if (!ln->dark_theme) + if (!dark_theme) bg = nvgRadialGradient(vg, -light, -light, 0, r+2.0, ln->clear_color.nvg_color, node_color); @@ -106,20 +117,23 @@ void draw_node(NVGcontext *vg, struct ln *ln, struct node *node) nvgCircle(vg, 0, 0, r); /* nvgPathWinding(vg, NVG_HOLE); */ - nvgStrokeWidth(vg, 3.0f); - nvgStrokeColor(vg, ln->dark_theme ? clear_adj : ln->clear_color.nvg_color); - nvgStroke(vg); + if (ln->display_flags & DISP_STROKE_NODES) { + nvgStrokeWidth(vg, 3.0f); + nvgStrokeColor(vg, dark_theme ? clear_adj : ln->clear_color.nvg_color); + nvgStroke(vg); + } nvgFillPaint(vg, bg); nvgFill(vg); - - nvgFontSize(vg, 18.0f); - nvgFontFace(vg, "sans"); - nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP); - /* nvgTextMetrics(vg, NULL, NULL, &lineh); */ - nvgFillColor(vg, node_color); - nvgText(vg, -r, r, node->alias, NULL); + if (ln->display_flags & DISP_ALIASES) { + nvgFontSize(vg, 18.0f); + nvgFontFace(vg, "sans"); + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP); + /* nvgTextMetrics(vg, NULL, NULL, &lineh); */ + nvgFillColor(vg, node_color); + nvgText(vg, -r, r, node->alias, NULL); + } nvgRestore(vg); } @@ -129,7 +143,7 @@ void render_ln(struct ln *ln) u32 i; NVGcontext *vg = ln->vg; - draw_grid(vg, ln->window_width, ln->window_height, ln->grid_div); + draw_grid(vg, ln); // render channels first for (i = 0; i < ln->channel_count; i++)