diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:38:23 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 20:38:23 +0000 |
commit | ff6e3c025658a5fa1affd094f220b623e7e1b24b (patch) | |
tree | 9faab72d69c92d24e349d184f5869b9796f17e0c /demos/ui.h | |
parent | Initial commit. (diff) | |
download | libplacebo-upstream.tar.xz libplacebo-upstream.zip |
Adding upstream version 6.338.2.upstream/6.338.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | demos/ui.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/demos/ui.h b/demos/ui.h new file mode 100644 index 0000000..9344e68 --- /dev/null +++ b/demos/ui.h @@ -0,0 +1,59 @@ +// License: CC0 / Public Domain +#pragma once + +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_STANDARD_IO +#define NK_INCLUDE_STANDARD_BOOL +#define NK_INCLUDE_STANDARD_VARARGS +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#define NK_INCLUDE_FONT_BAKING +#define NK_INCLUDE_DEFAULT_FONT +#define NK_BUTTON_TRIGGER_ON_RELEASE +#define NK_UINT_DRAW_INDEX +#include <nuklear.h> + +#include "common.h" +#include "window.h" + +struct ui; + +struct ui *ui_create(pl_gpu gpu); +void ui_destroy(struct ui **ui); + +// Update/Logic/Draw cycle +void ui_update_input(struct ui *ui, const struct window *window); +struct nk_context *ui_get_context(struct ui *ui); +bool ui_draw(struct ui *ui, const struct pl_swapchain_frame *frame); + +// Helper function to draw a custom widget for drag&drop operations, returns +// true if the widget is hovered +static inline bool ui_widget_hover(struct nk_context *nk, const char *label) +{ + struct nk_rect bounds; + if (!nk_widget(&bounds, nk)) + return false; + + struct nk_command_buffer *canvas = nk_window_get_canvas(nk); + bool hover = nk_input_is_mouse_hovering_rect(&nk->input, bounds); + + float h, s, v; + nk_color_hsv_f(&h, &s, &v, nk->style.window.background); + struct nk_color background = nk_hsv_f(h, s, v + (hover ? 0.1f : -0.02f)); + struct nk_color border = nk_hsv_f(h, s, v + 0.20f); + nk_fill_rect(canvas, bounds, 0.0f, background); + nk_stroke_rect(canvas, bounds, 0.0f, 2.0f, border); + + const float pad = 10.0f; + struct nk_rect text = { + .x = bounds.x + pad, + .y = bounds.y + pad, + .w = bounds.w - 2 * pad, + .h = bounds.h - 2 * pad, + }; + + nk_draw_text(canvas, text, label, nk_strlen(label), nk->style.font, + background, nk->style.text.color); + + return hover; +} |