canvas.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* nuklear - v1.05 - public domain */
  2. struct nk_canvas {
  3. struct nk_command_buffer *painter;
  4. struct nk_vec2 item_spacing;
  5. struct nk_vec2 panel_padding;
  6. struct nk_style_item window_background;
  7. };
  8. static nk_bool
  9. canvas_begin(struct nk_context *ctx, struct nk_canvas *canvas, nk_flags flags,
  10. int x, int y, int width, int height, struct nk_color background_color)
  11. {
  12. /* save style properties which will be overwritten */
  13. canvas->panel_padding = ctx->style.window.padding;
  14. canvas->item_spacing = ctx->style.window.spacing;
  15. canvas->window_background = ctx->style.window.fixed_background;
  16. /* use the complete window space and set background */
  17. ctx->style.window.spacing = nk_vec2(0,0);
  18. ctx->style.window.padding = nk_vec2(0,0);
  19. ctx->style.window.fixed_background = nk_style_item_color(background_color);
  20. /* create/update window and set position + size */
  21. if (!nk_begin(ctx, "Canvas", nk_rect(x, y, width, height), NK_WINDOW_NO_SCROLLBAR|flags))
  22. return nk_false;
  23. /* allocate the complete window space for drawing */
  24. {
  25. struct nk_rect total_space;
  26. total_space = nk_window_get_content_region(ctx);
  27. nk_layout_row_dynamic(ctx, total_space.h, 1);
  28. nk_widget(&total_space, ctx);
  29. canvas->painter = nk_window_get_canvas(ctx);
  30. }
  31. return nk_true;
  32. }
  33. static void
  34. canvas_end(struct nk_context *ctx, struct nk_canvas *canvas)
  35. {
  36. nk_end(ctx);
  37. ctx->style.window.spacing = canvas->panel_padding;
  38. ctx->style.window.padding = canvas->item_spacing;
  39. ctx->style.window.fixed_background = canvas->window_background;
  40. }
  41. static void
  42. canvas(struct nk_context *ctx)
  43. {
  44. struct nk_canvas canvas;
  45. if (canvas_begin(ctx, &canvas, NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  46. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE, 10, 10, 500, 550, nk_rgb(250,250,250)))
  47. {
  48. float x = canvas.painter->clip.x, y = canvas.painter->clip.y;
  49. nk_fill_rect(canvas.painter, nk_rect(x + 15, y + 15, 210, 210), 5, nk_rgb(247, 230, 154));
  50. nk_fill_rect(canvas.painter, nk_rect(x + 20, y + 20, 200, 200), 5, nk_rgb(188, 174, 118));
  51. /* nk_draw_text(canvas.painter, nk_rect(x + 30, y + 30, 150, 20), "Text to draw", 12, &font->handle, nk_rgb(188,174,118), nk_rgb(0,0,0)); */
  52. nk_fill_rect(canvas.painter, nk_rect(x + 250, y + 20, 100, 100), 0, nk_rgb(0,0,255));
  53. nk_fill_circle(canvas.painter, nk_rect(x + 20, y + 250, 100, 100), nk_rgb(255,0,0));
  54. nk_fill_triangle(canvas.painter, x + 250, y + 250, x + 350, y + 250, x + 300, y + 350, nk_rgb(0,255,0));
  55. nk_fill_arc(canvas.painter, x + 300, y + 420, 50, 0, 3.141592654f * 3.0f / 4.0f, nk_rgb(255,255,0));
  56. {
  57. float points[12];
  58. points[0] = x + 200; points[1] = y + 250;
  59. points[2] = x + 250; points[3] = y + 350;
  60. points[4] = x + 225; points[5] = y + 350;
  61. points[6] = x + 200; points[7] = y + 300;
  62. points[8] = x + 175; points[9] = y + 350;
  63. points[10] = x + 150; points[11] = y + 350;
  64. nk_fill_polygon(canvas.painter, points, 6, nk_rgb(0,0,0));
  65. }
  66. {
  67. float points[12];
  68. points[0] = x + 200; points[1] = y + 370;
  69. points[2] = x + 250; points[3] = y + 470;
  70. points[4] = x + 225; points[5] = y + 470;
  71. points[6] = x + 200; points[7] = y + 420;
  72. points[8] = x + 175; points[9] = y + 470;
  73. points[10] = x + 150; points[11] = y + 470;
  74. nk_stroke_polygon(canvas.painter, points, 6, 4, nk_rgb(0,0,0));
  75. }
  76. {
  77. float points[8];
  78. points[0] = x + 250; points[1] = y + 200;
  79. points[2] = x + 275; points[3] = y + 220;
  80. points[4] = x + 325; points[5] = y + 170;
  81. points[6] = x + 350; points[7] = y + 200;
  82. nk_stroke_polyline(canvas.painter, points, 4, 2, nk_rgb(255,128,0));
  83. }
  84. nk_stroke_line(canvas.painter, x + 15, y + 10, x + 200, y + 10, 2.0f, nk_rgb(189,45,75));
  85. nk_stroke_rect(canvas.painter, nk_rect(x + 370, y + 20, 100, 100), 10, 3, nk_rgb(0,0,255));
  86. nk_stroke_curve(canvas.painter, x + 380, y + 200, x + 405, y + 270, x + 455, y + 120, x + 480, y + 200, 2, nk_rgb(0,150,220));
  87. nk_stroke_circle(canvas.painter, nk_rect(x + 20, y + 370, 100, 100), 5, nk_rgb(0,255,120));
  88. nk_stroke_triangle(canvas.painter, x + 370, y + 250, x + 470, y + 250, x + 420, y + 350, 6, nk_rgb(255,0,143));
  89. nk_stroke_arc(canvas.painter, x + 420, y + 420, 50, 0, 3.141592654f * 3.0f / 4.0f, 5, nk_rgb(0,255,255));
  90. }
  91. canvas_end(ctx, &canvas);
  92. }