1 #pragma once
2
3 #define NK_INCLUDE_FIXED_TYPES
4 #define NK_INCLUDE_DEFAULT_ALLOCATOR
5 #define NK_INCLUDE_STANDARD_BOOL
6 #define NK_INCLUDE_STANDARD_VARARGS
7 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
8 #define NK_INCLUDE_FONT_BAKING
9 #define NK_INCLUDE_DEFAULT_FONT
10 #define NK_BUTTON_TRIGGER_ON_RELEASE
11 #include <nuklear.h>
12
13 #include "common.h"
14 #include "window.h"
15
16 struct ui;
17
18 struct ui *ui_create(pl_gpu gpu);
19 void ui_destroy(struct ui **ui);
20
21 // Update/Logic/Draw cycle
22 void ui_update_input(struct ui *ui, const struct window *window);
23 struct nk_context *ui_get_context(struct ui *ui);
24 bool ui_draw(struct ui *ui, const struct pl_swapchain_frame *frame);
25
26 // Helper function to draw a custom widget for drag&drop operations, returns
27 // true if the widget is hovered
ui_widget_hover(struct nk_context * nk,const char * label)28 static inline bool ui_widget_hover(struct nk_context *nk, const char *label)
29 {
30 struct nk_rect bounds;
31 if (!nk_widget(&bounds, nk))
32 return false;
33
34 struct nk_command_buffer *canvas = nk_window_get_canvas(nk);
35 bool hover = nk_input_is_mouse_hovering_rect(&nk->input, bounds);
36
37 float h, s, v;
38 nk_color_hsv_f(&h, &s, &v, nk->style.window.background);
39 struct nk_color background = nk_hsv_f(h, s, v + (hover ? 0.1f : -0.02f));
40 struct nk_color border = nk_hsv_f(h, s, v + 0.20f);
41 nk_fill_rect(canvas, bounds, 0.0f, background);
42 nk_stroke_rect(canvas, bounds, 0.0f, 2.0f, border);
43
44 const float pad = 10.0f;
45 struct nk_rect text = {
46 .x = bounds.x + pad,
47 .y = bounds.y + pad,
48 .w = bounds.w - 2 * pad,
49 .h = bounds.h - 2 * pad,
50 };
51
52 nk_draw_text(canvas, text, label, nk_strlen(label), nk->style.font,
53 background, nk->style.text.color);
54
55 return hover;
56 }
57