Exemple #1
0
static void hat_widget(struct nk_context* nk, unsigned char state)
{
    float radius;
    struct nk_rect area;
    struct nk_vec2 center;

    if (nk_widget(&area, nk) != NK_WIDGET_VALID)
        return;

    center = nk_vec2(area.x + area.w / 2.f, area.y + area.h / 2.f);
    radius = NK_MIN(area.w, area.h) / 2.f;

    nk_stroke_circle(nk_window_get_canvas(nk),
                     nk_rect(center.x - radius,
                             center.y - radius,
                             radius * 2.f,
                             radius * 2.f),
                     1.f,
                     nk_rgb(175, 175, 175));

    if (state)
    {
        const float angles[] =
        {
            0.f,           0.f,
            NK_PI * 1.5f,  NK_PI * 1.75f,
            NK_PI,         0.f,
            NK_PI * 1.25f, 0.f,
            NK_PI * 0.5f,  NK_PI * 0.25f,
            0.f,           0.f,
            NK_PI * 0.75f, 0.f,
        };
        const float cosa = nk_cos(angles[state]);
        const float sina = nk_sin(angles[state]);
        const struct nk_vec2 p0 = nk_vec2(0.f, -radius);
        const struct nk_vec2 p1 = nk_vec2( radius / 2.f, -radius / 3.f);
        const struct nk_vec2 p2 = nk_vec2(-radius / 2.f, -radius / 3.f);

        nk_fill_triangle(nk_window_get_canvas(nk),
                            center.x + cosa * p0.x + sina * p0.y,
                            center.y + cosa * p0.y - sina * p0.x,
                            center.x + cosa * p1.x + sina * p1.y,
                            center.y + cosa * p1.y - sina * p1.x,
                            center.x + cosa * p2.x + sina * p2.y,
                            center.y + cosa * p2.y - sina * p2.x,
                            nk_rgb(175, 175, 175));
    }
}
int main(int argc, char *argv[]) {

	/* register callbacks */
		mpx_register_vc(&this_vc);
  
    /* GUI */
    static struct nk_context ctx;
    static struct nk_canvas canvas;
    

    uint32_t width = 0, 
        height = 0;

    static struct nk_user_font font;
    font.userdata.ptr = &width;
    font.height = font_vga_8x16.height;
    font.width = your_text_width_calculation;
    nk_init_default(&ctx, &font);
    
    width = this_vc.fb->var.xres;
    height = this_vc.fb->var.yres;
    
    /* Draw */
    while (1) 
    {
        /* what to draw */
        canvas_begin(&ctx, &canvas, 0, 0, 0, width, height, nk_rgb(100,100,100));
        {         
            canvas.painter->use_clipping = NK_CLIPPING_OFF;  

            nk_fill_rect(canvas.painter, nk_rect(15,15,140,140), 5, nk_rgb(247, 230, 154));
            nk_fill_rect(canvas.painter, nk_rect(20,20,135,135), 5, nk_rgb(188, 174, 118));
            nk_draw_text(canvas.painter, nk_rect(30, 30, 100, 20), "Text to draw", 12, &font, nk_rgb(188,174,118), nk_rgb(0,0,0));
            nk_fill_rect(canvas.painter, nk_rect(160,20,70,70), 0, nk_rgb(0,0,255));
            nk_fill_circle(canvas.painter, nk_rect(20,160,60,60), nk_rgb(255,0,0));
            nk_fill_triangle(canvas.painter, 160, 160, 230, 160, 195, 220, nk_rgb(0,255,0));
            nk_fill_arc(canvas.painter, 195, 120, 30, 0, 3.141592654f * 3.0f / 4.0f, nk_rgb(255,255,0));
            nk_stroke_line(canvas.painter, 15, 10, 100, 10, 2.0f, nk_rgb(189,45,75));
            nk_stroke_rect(canvas.painter, nk_rect(235, 20, 70, 70), 10, 3, nk_rgb(0,0,255));
            nk_stroke_curve(canvas.painter, 235, 130, 252, 170, 288, 80, 305, 130, 1, nk_rgb(0,150,220));
            nk_stroke_triangle(canvas.painter, 235, 160, 305, 160, 270, 220, 10, nk_rgb(255,0,143));
            nk_stroke_circle(canvas.painter, nk_rect(90, 160, 60, 60), 2, nk_rgb(0,255,120));
            

            /* load some image */
            // uint32_t im_w, im_h, im_format;
            // images[0] = stbi_load("SPBGU_logo.png", &im_w, &im_h, &im_format, 0);
            // if (images[0] == NULL)
            //     printf("\nstbi_load doesn't work. :(\n");
            // else {
            //     printf("\nLoaded image: id = %i   width = %i\theight = %i\tformat = %i", (int)*images[0], im_w, im_h, im_format);

                struct nk_image im;
                im.handle.ptr = "SPBGU_logo.png";
                im.handle.id = 0;
                im.w = 0;
                im.h = 0;
                im.region[0] = 0;
                im.region[1] = 0;
                im.region[2] = 267;
                im.region[3] = 333;
                
                nk_draw_image(canvas.painter, nk_rect(320, 10, 130, 150), &im, nk_rgb(100, 0, 0));
           // }

           // stbi_image_free(images[0]);            
        }
        canvas_end(&ctx, &canvas);

         /* Draw each element */
         draw(&this_vc, &ctx, width, height);
    }
    nk_free(&ctx);

    printf("\nEnd of program.\nIf you see it then something goes wrong.\n");
    return 0;
}