From 2dab85fde1013dc378dfa7a8cb76c6e4216c83d1 Mon Sep 17 00:00:00 2001 From: vurtun Date: Mon, 15 Aug 2016 10:35:42 +0200 Subject: [PATCH] Enhanced the canvas example a little bit Removed some global varibles and made the canvas a little bit easier to use and embed into code. --- example/canvas.c | 60 ++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/example/canvas.c b/example/canvas.c index 7b07e31..66774f4 100644 --- a/example/canvas.c +++ b/example/canvas.c @@ -332,27 +332,37 @@ pump_input(struct nk_context *ctx, GLFWwindow *win) nk_input_end(ctx); } +struct nk_canvas { + struct nk_panel layout; + struct nk_command_buffer *painter; + struct nk_vec2 item_spacing; + struct nk_vec2 panel_padding; +}; + static void -canvas_begin(struct nk_context *ctx, int x, int y, int width, int height, struct nk_command_buffer **out) +canvas_begin(struct nk_context *ctx, struct nk_canvas *canvas, int x, int y, int width, int height) { - static struct nk_panel layout; - struct nk_rect total_space; + canvas->panel_padding = ctx->style.window.padding; + canvas->item_spacing = ctx->style.window.spacing; ctx->style.window.spacing = nk_vec2(0,0); ctx->style.window.padding = nk_vec2(0,0); - nk_begin(ctx, &layout, "Window", nk_rect(x, y, width, height), NK_WINDOW_NO_SCROLLBAR); + + nk_begin(ctx, &canvas->layout, "Window", nk_rect(x, y, width, height), NK_WINDOW_NO_SCROLLBAR); nk_window_set_bounds(ctx, nk_rect(x, y, width, height)); + + {struct nk_rect total_space; total_space = nk_window_get_content_region(ctx); nk_layout_row_dynamic(ctx, total_space.h, 1); nk_widget(&total_space, ctx); - *out = nk_window_get_canvas(ctx); + canvas->painter = nk_window_get_canvas(ctx);} } static void -canvas_end(struct nk_context *ctx) +canvas_end(struct nk_context *ctx, struct nk_canvas *canvas) { nk_end(ctx); - ctx->style.window.spacing = nk_vec2(4,4); - ctx->style.window.padding = nk_vec2(8,8); + ctx->style.window.spacing = canvas->panel_padding; + ctx->style.window.padding = canvas->item_spacing; } int main(int argc, char *argv[]) @@ -409,17 +419,17 @@ int main(int argc, char *argv[]) pump_input(&ctx, win); /* draw */ - {struct nk_command_buffer *canvas; - canvas_begin(&ctx, 0, 0, width, height, &canvas); + {struct nk_canvas canvas; + canvas_begin(&ctx, &canvas, 0, 0, width, height); { - nk_fill_rect(canvas, nk_rect(0,0,width,height), 0, nk_rgb(150,150,150)); - nk_fill_rect(canvas, nk_rect(15,15,210,210), 5, nk_rgb(247, 230, 154)); - nk_fill_rect(canvas, nk_rect(20,20,200,200), 5, nk_rgb(188, 174, 118)); - nk_draw_text(canvas, nk_rect(30, 30, 150, 20), "Text to draw", 12, &font->handle, nk_rgb(188,174,118), nk_rgb(0,0,0)); - nk_fill_rect(canvas, nk_rect(250,20,100,100), 0, nk_rgb(0,0,255)); - nk_fill_circle(canvas, nk_rect(20,250,100,100), nk_rgb(255,0,0)); - nk_fill_triangle(canvas, 250, 250, 350, 250, 300, 350, nk_rgb(0,255,0)); - nk_fill_arc(canvas, 300, 180, 50, 0, 3.141592654f * 3.0f / 4.0f, nk_rgb(255,255,0)); + nk_fill_rect(canvas.painter, nk_rect(0,0,width,height), 0, nk_rgb(150,150,150)); + nk_fill_rect(canvas.painter, nk_rect(15,15,210,210), 5, nk_rgb(247, 230, 154)); + nk_fill_rect(canvas.painter, nk_rect(20,20,200,200), 5, nk_rgb(188, 174, 118)); + nk_draw_text(canvas.painter, nk_rect(30, 30, 150, 20), "Text to draw", 12, &font->handle, nk_rgb(188,174,118), nk_rgb(0,0,0)); + nk_fill_rect(canvas.painter, nk_rect(250,20,100,100), 0, nk_rgb(0,0,255)); + nk_fill_circle(canvas.painter, nk_rect(20,250,100,100), nk_rgb(255,0,0)); + nk_fill_triangle(canvas.painter, 250, 250, 350, 250, 300, 350, nk_rgb(0,255,0)); + nk_fill_arc(canvas.painter, 300, 180, 50, 0, 3.141592654f * 3.0f / 4.0f, nk_rgb(255,255,0)); {float points[12]; points[0] = 200; points[1] = 250; @@ -428,15 +438,15 @@ int main(int argc, char *argv[]) points[6] = 200; points[7] = 300; points[8] = 175; points[9] = 350; points[10] = 150; points[11] = 350; - nk_fill_polygon(canvas, points, 6, nk_rgb(255,255,255));} + nk_fill_polygon(canvas.painter, points, 6, nk_rgb(255,255,255));} - nk_stroke_line(canvas, 15, 10, 200, 10, 2.0f, nk_rgb(189,45,75)); - nk_stroke_rect(canvas, nk_rect(370, 20, 100, 100), 10, 3, nk_rgb(0,0,255)); - nk_stroke_curve(canvas, 380, 200, 405, 270, 455, 120, 480, 200, 2, nk_rgb(0,150,220)); - nk_stroke_circle(canvas, nk_rect(20, 370, 100, 100), 5, nk_rgb(0,255,120)); - nk_stroke_triangle(canvas, 370, 250, 470, 250, 420, 350, 6, nk_rgb(255,0,143)); + nk_stroke_line(canvas.painter, 15, 10, 200, 10, 2.0f, nk_rgb(189,45,75)); + nk_stroke_rect(canvas.painter, nk_rect(370, 20, 100, 100), 10, 3, nk_rgb(0,0,255)); + nk_stroke_curve(canvas.painter, 380, 200, 405, 270, 455, 120, 480, 200, 2, nk_rgb(0,150,220)); + nk_stroke_circle(canvas.painter, nk_rect(20, 370, 100, 100), 5, nk_rgb(0,255,120)); + nk_stroke_triangle(canvas.painter, 370, 250, 470, 250, 420, 350, 6, nk_rgb(255,0,143)); } - canvas_end(&ctx);} + canvas_end(&ctx, &canvas);} /* Draw */ glfwGetWindowSize(win, &width, &height);