2#include "nuklear_internal.h"
10nk_chart_begin_colored(
struct nk_context *ctx,
enum nk_chart_type type,
12 int count,
float min_value,
float max_value)
20 struct nk_rect bounds = {0, 0, 0, 0};
23 NK_ASSERT(ctx->current);
24 NK_ASSERT(ctx->current->layout);
26 if (!ctx || !ctx->current || !ctx->current->layout)
return 0;
27 if (!nk_widget(&bounds, ctx)) {
28 chart = &ctx->current->layout->chart;
29 nk_zero(chart,
sizeof(*chart));
35 chart = &win->layout->chart;
36 style = &config->chart;
39 nk_zero(chart,
sizeof(*chart));
40 chart->x = bounds.x + style->padding.x;
41 chart->y = bounds.y + style->padding.y;
42 chart->w = bounds.w - 2 * style->padding.x;
43 chart->h = bounds.h - 2 * style->padding.y;
44 chart->w = NK_MAX(chart->w, 2 * style->padding.x);
45 chart->h = NK_MAX(chart->h, 2 * style->padding.y);
51 slot->color = nk_rgb_factor(color, style->color_factor);
52 slot->highlight = highlight;
53 slot->min = NK_MIN(min_value, max_value);
54 slot->max = NK_MAX(min_value, max_value);
55 slot->range = slot->max - slot->min;
56 slot->show_markers = style->show_markers;}
59 background = &style->background;
61 switch(background->type) {
62 case NK_STYLE_ITEM_IMAGE:
63 nk_draw_image(&win->buffer, bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
65 case NK_STYLE_ITEM_NINE_SLICE:
66 nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
68 case NK_STYLE_ITEM_COLOR:
69 nk_fill_rect(&win->buffer, bounds, style->rounding, nk_rgb_factor(style->border_color, style->color_factor));
70 nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
71 style->rounding, nk_rgb_factor(style->background.data.color, style->color_factor));
77nk_chart_begin(
struct nk_context *ctx,
const enum nk_chart_type type,
78 int count,
float min_value,
float max_value)
80 return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
81 ctx->style.chart.selected_color, count, min_value, max_value);
84nk_chart_add_slot_colored(
struct nk_context *ctx,
const enum nk_chart_type type,
86 int count,
float min_value,
float max_value)
91 NK_ASSERT(ctx->current);
92 NK_ASSERT(ctx->current->layout);
93 NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
94 if (!ctx || !ctx->current || !ctx->current->layout)
return;
95 if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT)
return;
97 style = &ctx->style.chart;
100 {
struct nk_chart *chart = &ctx->current->layout->chart;
104 slot->color = nk_rgb_factor(color, style->color_factor);
105 slot->highlight = highlight;
106 slot->min = NK_MIN(min_value, max_value);
107 slot->max = NK_MAX(min_value, max_value);
108 slot->range = slot->max - slot->min;
109 slot->show_markers = style->show_markers;}
112nk_chart_add_slot(
struct nk_context *ctx,
const enum nk_chart_type type,
113 int count,
float min_value,
float max_value)
115 nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
116 ctx->style.chart.selected_color, count, min_value, max_value);
120 struct nk_chart *g,
float value,
int slot)
122 struct nk_panel *layout = win->layout;
123 const struct nk_input *i = ctx->current->widgets_disabled ? 0 : &ctx->input;
134 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
135 step = g->w / (float)g->slots[slot].count;
136 range = g->slots[slot].max - g->slots[slot].min;
137 ratio = (value - g->slots[slot].min) / range;
139 if (g->slots[slot].index == 0) {
141 g->slots[slot].last.x = g->x;
142 g->slots[slot].last.y = (g->y + g->h) - ratio * (
float)g->h;
144 bounds.x = g->slots[slot].last.x - 2;
145 bounds.y = g->slots[slot].last.y - 2;
146 bounds.w = bounds.h = 4;
148 color = g->slots[slot].color;
150 NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
151 ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
152 ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
153 i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
154 color = g->slots[slot].highlight;
156 if (g->slots[slot].show_markers) {
159 g->slots[slot].index += 1;
164 color = g->slots[slot].color;
165 cur.x = g->x + (float)(step * (
float)g->slots[slot].index);
166 cur.y = (g->y + g->h) - (ratio * (
float)g->h);
167 nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
169 bounds.x = cur.x - 3;
170 bounds.y = cur.y - 3;
171 bounds.w = bounds.h = 6;
175 if (nk_input_is_mouse_hovering_rect(i, bounds)) {
176 ret = NK_CHART_HOVERING;
177 ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
178 i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
179 color = g->slots[slot].highlight;
182 if (g->slots[slot].show_markers) {
187 g->slots[slot].last.x = cur.x;
188 g->slots[slot].last.y = cur.y;
189 g->slots[slot].index += 1;
194 struct nk_chart *chart,
float value,
int slot)
197 const struct nk_input *in = ctx->current->widgets_disabled ? 0 : &ctx->input;
198 struct nk_panel *layout = win->layout;
203 struct nk_rect item = {0,0,0,0};
205 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
206 if (chart->slots[slot].index >= chart->slots[slot].count)
208 if (chart->slots[slot].count) {
209 float padding = (float)(chart->slots[slot].count-1);
210 item.w = (chart->w - padding) / (
float)(chart->slots[slot].count);
214 color = chart->slots[slot].color;;
215 item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
217 ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
218 item.y = (chart->y + chart->h) - chart->h * ratio;
220 ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
221 item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
223 item.x = chart->x + ((float)chart->slots[slot].index * item.w);
224 item.x = item.x + ((float)chart->slots[slot].index);
228 NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
229 ret = NK_CHART_HOVERING;
230 ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
231 in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
232 color = chart->slots[slot].highlight;
235 chart->slots[slot].index += 1;
239nk_chart_push_slot(
struct nk_context *ctx,
float value,
int slot)
245 NK_ASSERT(ctx->current);
246 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
247 NK_ASSERT(slot < ctx->current->layout->chart.slot);
248 if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT)
return nk_false;
249 if (slot >= ctx->current->layout->chart.slot)
return nk_false;
252 if (win->layout->chart.slot < slot)
return nk_false;
253 switch (win->layout->chart.slots[slot].type) {
255 flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot);
break;
256 case NK_CHART_COLUMN:
257 flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot);
break;
265nk_chart_push(
struct nk_context *ctx,
float value)
267 return nk_chart_push_slot(ctx, value, 0);
276 NK_ASSERT(ctx->current);
277 if (!ctx || !ctx->current)
281 chart = &win->layout->chart;
282 NK_MEMSET(chart, 0,
sizeof(*chart));
286nk_plot(
struct nk_context *ctx,
enum nk_chart_type type,
const float *values,
287 int count,
int offset)
295 if (!ctx || !values || !count)
return;
297 min_value = values[offset];
298 max_value = values[offset];
299 for (i = 0; i < count; ++i) {
300 min_value = NK_MIN(values[i + offset], min_value);
301 max_value = NK_MAX(values[i + offset], max_value);
304 if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
305 for (i = 0; i < count; ++i)
306 nk_chart_push(ctx, values[i + offset]);
311nk_plot_function(
struct nk_context *ctx,
enum nk_chart_type type,
void *userdata,
312 float(*value_getter)(
void* user,
int index),
int count,
int offset)
319 NK_ASSERT(value_getter);
320 if (!ctx || !value_getter || !count)
return;
322 max_value = min_value = value_getter(userdata, offset);
323 for (i = 0; i < count; ++i) {
324 float value = value_getter(userdata, i + offset);
325 min_value = NK_MIN(value, min_value);
326 max_value = NK_MAX(value, max_value);
329 if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
330 for (i = 0; i < count; ++i)
331 nk_chart_push(ctx, value_getter(userdata, i + offset));
main API and documentation file
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
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_stroke_line(struct nk_command_buffer *b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color)
shape outlines
NK_API void nk_fill_rect(struct nk_command_buffer *, struct nk_rect, float rounding, struct nk_color)
filled shades