Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_progress.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * PROGRESS
7 *
8 * ===============================================================*/
9NK_LIB nk_size
10nk_progress_behavior(nk_flags *state, struct nk_input *in,
11 struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, nk_bool modifiable)
12{
13 int left_mouse_down = 0;
14 int left_mouse_click_in_cursor = 0;
15
16 nk_widget_state_reset(state);
17 if (!in || !modifiable) return value;
18 left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
19 left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
20 NK_BUTTON_LEFT, cursor, nk_true);
21 if (nk_input_is_mouse_hovering_rect(in, r))
23
24 if (in && left_mouse_down && left_mouse_click_in_cursor) {
25 if (left_mouse_down && left_mouse_click_in_cursor) {
26 float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
27 value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
28 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
29 *state |= NK_WIDGET_STATE_ACTIVE;
30 }
31 }
32 /* set progressbar widget state */
33 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
35 else if (nk_input_is_mouse_prev_hovering_rect(in, r))
36 *state |= NK_WIDGET_STATE_LEFT;
37 return value;
38}
39NK_LIB void
40nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
41 const struct nk_style_progress *style, const struct nk_rect *bounds,
42 const struct nk_rect *scursor, nk_size value, nk_size max)
43{
44 const struct nk_style_item *background;
45 const struct nk_style_item *cursor;
46
47 NK_UNUSED(max);
48 NK_UNUSED(value);
49
50 /* select correct colors/images to draw */
51 if (state & NK_WIDGET_STATE_ACTIVED) {
52 background = &style->active;
53 cursor = &style->cursor_active;
54 } else if (state & NK_WIDGET_STATE_HOVER){
55 background = &style->hover;
56 cursor = &style->cursor_hover;
57 } else {
58 background = &style->normal;
59 cursor = &style->cursor_normal;
60 }
61
62 /* draw background */
63 switch(background->type) {
64 case NK_STYLE_ITEM_IMAGE:
65 nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
66 break;
67 case NK_STYLE_ITEM_NINE_SLICE:
68 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
69 break;
70 case NK_STYLE_ITEM_COLOR:
71 nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
72 nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
73 break;
74 }
75
76 /* draw cursor */
77 switch(cursor->type) {
78 case NK_STYLE_ITEM_IMAGE:
79 nk_draw_image(out, *scursor, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
80 break;
81 case NK_STYLE_ITEM_NINE_SLICE:
82 nk_draw_nine_slice(out, *scursor, &cursor->data.slice, nk_rgb_factor(nk_white, style->color_factor));
83 break;
84 case NK_STYLE_ITEM_COLOR:
85 nk_fill_rect(out, *scursor, style->rounding, nk_rgb_factor(cursor->data.color, style->color_factor));
86 nk_stroke_rect(out, *scursor, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
87 break;
88 }
89}
90NK_LIB nk_size
91nk_do_progress(nk_flags *state,
92 struct nk_command_buffer *out, struct nk_rect bounds,
93 nk_size value, nk_size max, nk_bool modifiable,
94 const struct nk_style_progress *style, struct nk_input *in)
95{
96 float prog_scale;
97 nk_size prog_value;
98 struct nk_rect cursor;
99
100 NK_ASSERT(style);
101 NK_ASSERT(out);
102 if (!out || !style) return 0;
103
104 /* calculate progressbar cursor */
105 cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
106 cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
107 cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
108 prog_scale = (float)value / (float)max;
109
110 /* update progressbar */
111 prog_value = NK_MIN(value, max);
112 prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
113 cursor.w = cursor.w * prog_scale;
114
115 /* draw progressbar */
116 if (style->draw_begin) style->draw_begin(out, style->userdata);
117 nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
118 if (style->draw_end) style->draw_end(out, style->userdata);
119 return prog_value;
120}
121NK_API nk_bool
122nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, nk_bool is_modifyable)
123{
124 struct nk_window *win;
125 struct nk_panel *layout;
126 const struct nk_style *style;
127 struct nk_input *in;
128
129 struct nk_rect bounds;
130 enum nk_widget_layout_states state;
131 nk_size old_value;
132
133 NK_ASSERT(ctx);
134 NK_ASSERT(cur);
135 NK_ASSERT(ctx->current);
136 NK_ASSERT(ctx->current->layout);
137 if (!ctx || !ctx->current || !ctx->current->layout || !cur)
138 return 0;
139
140 win = ctx->current;
141 style = &ctx->style;
142 layout = win->layout;
143 state = nk_widget(&bounds, ctx);
144 if (!state) return 0;
145
146 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
147 old_value = *cur;
148 *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
149 *cur, max, is_modifyable, &style->progress, in);
150 return (*cur != old_value);
151}
152NK_API nk_size
153nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, nk_bool modifyable)
154{
155 nk_progress(ctx, &cur, max, modifyable);
156 return cur;
157}
158
main API and documentation file
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
Definition nuklear.h:5492
NK_API void nk_draw_image(struct nk_command_buffer *, struct nk_rect, const struct nk_image *, struct nk_color)
misc
NK_API void nk_fill_rect(struct nk_command_buffer *, struct nk_rect, float rounding, struct nk_color)
filled shades
@ NK_WIDGET_STATE_LEFT
!< widget is currently activated
Definition nuklear.h:3093
@ NK_WIDGET_STATE_ACTIVED
!< widget is being hovered
Definition nuklear.h:3092
@ NK_WIDGET_STATE_ENTERED
!< widget is neither active nor hovered
Definition nuklear.h:3090
@ NK_WIDGET_STATE_HOVER
!< widget has been hovered on the current frame
Definition nuklear.h:3091
@ NK_WIDGET_STATE_ACTIVE
!< widget is being hovered
Definition nuklear.h:3095
@ NK_WIDGET_STATE_HOVERED
!< widget is from this frame on not hovered anymore
Definition nuklear.h:3094
nk_widget_layout_states
Definition nuklear.h:3081
@ NK_WIDGET_DISABLED
The widget is manually disabled and acts like NK_WIDGET_ROM.
Definition nuklear.h:3085
@ NK_WIDGET_ROM
The widget is partially visible and cannot be updated.
Definition nuklear.h:3084