2#include "nuklear_internal.h"
11 struct nk_buffer *b,
enum nk_command_clipping clip)
15 if (!cb || !b)
return;
17 cb->clip = nk_null_rect;
18 cb->use_clipping = (int)clip;
31 b->clip = nk_null_rect;
32#ifdef NK_INCLUDE_COMMAND_USERDATA
38 enum nk_command_type t, nk_size size)
40 NK_STORAGE
const nk_size align = NK_ALIGNOF(
struct nk_command);
49 cmd = (
struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
53 b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->
memory.ptr);
54 unaligned = (nk_byte*)cmd + size;
55 memory = NK_ALIGN_PTR(unaligned, align);
56 alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
57#ifdef NK_ZERO_COMMAND_MEMORY
58 NK_MEMSET(cmd, 0, size + alignment);
62 cmd->next = b->base->
allocated + alignment;
63#ifdef NK_INCLUDE_COMMAND_USERDATA
64 cmd->userdata = b->userdata;
81 nk_command_buffer_push(b, NK_COMMAND_SCISSOR,
sizeof(*cmd));
86 cmd->w = (
unsigned short)NK_MAX(0, r.w);
87 cmd->h = (
unsigned short)NK_MAX(0, r.h);
91 float x1,
float y1,
float line_thickness,
struct nk_color c)
95 if (!b || line_thickness <= 0)
return;
97 nk_command_buffer_push(b, NK_COMMAND_LINE,
sizeof(*cmd));
99 cmd->line_thickness = (
unsigned short)line_thickness;
100 cmd->begin.x = (short)x0;
101 cmd->begin.y = (short)y0;
102 cmd->end.x = (short)x1;
103 cmd->end.y = (short)y1;
108 float ctrl0x,
float ctrl0y,
float ctrl1x,
float ctrl1y,
109 float bx,
float by,
float line_thickness,
struct nk_color col)
113 if (!b || col.a == 0 || line_thickness <= 0)
return;
116 nk_command_buffer_push(b, NK_COMMAND_CURVE,
sizeof(*cmd));
118 cmd->line_thickness = (
unsigned short)line_thickness;
119 cmd->begin.x = (short)ax;
120 cmd->begin.y = (short)ay;
121 cmd->ctrl[0].x = (short)ctrl0x;
122 cmd->ctrl[0].y = (short)ctrl0y;
123 cmd->ctrl[1].x = (short)ctrl1x;
124 cmd->ctrl[1].y = (short)ctrl1y;
125 cmd->end.x = (short)bx;
126 cmd->end.y = (short)by;
131 float rounding,
float line_thickness,
struct nk_color c)
135 if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0)
return;
136 if (b->use_clipping) {
137 const struct nk_rect *clip = &b->clip;
138 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
139 clip->x, clip->y, clip->w, clip->h))
return;
142 nk_command_buffer_push(b, NK_COMMAND_RECT,
sizeof(*cmd));
144 cmd->rounding = (
unsigned short)rounding;
145 cmd->line_thickness = (
unsigned short)line_thickness;
146 cmd->x = (short)rect.x;
147 cmd->y = (short)rect.y;
148 cmd->w = (
unsigned short)NK_MAX(0, rect.w);
149 cmd->h = (
unsigned short)NK_MAX(0, rect.h);
158 if (!b || c.a == 0 || rect.w == 0 || rect.h == 0)
return;
159 if (b->use_clipping) {
160 const struct nk_rect *clip = &b->clip;
161 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
162 clip->x, clip->y, clip->w, clip->h))
return;
166 nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED,
sizeof(*cmd));
168 cmd->rounding = (
unsigned short)rounding;
169 cmd->x = (short)rect.x;
170 cmd->y = (short)rect.y;
171 cmd->w = (
unsigned short)NK_MAX(0, rect.w);
172 cmd->h = (
unsigned short)NK_MAX(0, rect.h);
182 if (!b || rect.w == 0 || rect.h == 0)
return;
183 if (b->use_clipping) {
184 const struct nk_rect *clip = &b->clip;
185 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
186 clip->x, clip->y, clip->w, clip->h))
return;
190 nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR,
sizeof(*cmd));
192 cmd->x = (short)rect.x;
193 cmd->y = (short)rect.y;
194 cmd->w = (
unsigned short)NK_MAX(0, rect.w);
195 cmd->h = (
unsigned short)NK_MAX(0, rect.h);
199 cmd->bottom = bottom;
203 float line_thickness,
struct nk_color c)
206 if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0)
return;
207 if (b->use_clipping) {
208 const struct nk_rect *clip = &b->clip;
209 if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
214 nk_command_buffer_push(b, NK_COMMAND_CIRCLE,
sizeof(*cmd));
216 cmd->line_thickness = (
unsigned short)line_thickness;
219 cmd->w = (
unsigned short)NK_MAX(r.w, 0);
220 cmd->h = (
unsigned short)NK_MAX(r.h, 0);
228 if (!b || c.a == 0 || r.w == 0 || r.h == 0)
return;
229 if (b->use_clipping) {
230 const struct nk_rect *clip = &b->clip;
231 if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
236 nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED,
sizeof(*cmd));
240 cmd->w = (
unsigned short)NK_MAX(r.w, 0);
241 cmd->h = (
unsigned short)NK_MAX(r.h, 0);
246 float a_min,
float a_max,
float line_thickness,
struct nk_color c)
249 if (!b || c.a == 0 || line_thickness <= 0)
return;
251 nk_command_buffer_push(b, NK_COMMAND_ARC,
sizeof(*cmd));
253 cmd->line_thickness = (
unsigned short)line_thickness;
256 cmd->r = (
unsigned short)radius;
263 float a_min,
float a_max,
struct nk_color c)
267 if (!b || c.a == 0)
return;
269 nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED,
sizeof(*cmd));
273 cmd->r = (
unsigned short)radius;
280 float y1,
float x2,
float y2,
float line_thickness,
struct nk_color c)
284 if (!b || c.a == 0 || line_thickness <= 0)
return;
285 if (b->use_clipping) {
286 const struct nk_rect *clip = &b->clip;
287 if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
288 !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
289 !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
294 nk_command_buffer_push(b, NK_COMMAND_TRIANGLE,
sizeof(*cmd));
296 cmd->line_thickness = (
unsigned short)line_thickness;
297 cmd->a.x = (short)x0;
298 cmd->a.y = (short)y0;
299 cmd->b.x = (short)x1;
300 cmd->b.y = (short)y1;
301 cmd->c.x = (short)x2;
302 cmd->c.y = (short)y2;
307 float y1,
float x2,
float y2,
struct nk_color c)
311 if (!b || c.a == 0)
return;
313 if (b->use_clipping) {
314 const struct nk_rect *clip = &b->clip;
315 if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
316 !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
317 !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
322 nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED,
sizeof(*cmd));
324 cmd->a.x = (short)x0;
325 cmd->a.y = (short)y0;
326 cmd->b.x = (short)x1;
327 cmd->b.y = (short)y1;
328 cmd->c.x = (short)x2;
329 cmd->c.y = (short)y2;
333nk_stroke_polygon(
struct nk_command_buffer *b,
const float *points,
int point_count,
334 float line_thickness,
struct nk_color col)
341 if (!b || col.a == 0 || line_thickness <= 0)
return;
342 size =
sizeof(*cmd) +
sizeof(short) * 2 * (nk_size)point_count;
343 cmd = (
struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
346 cmd->line_thickness = (
unsigned short)line_thickness;
347 cmd->point_count = (
unsigned short)point_count;
348 for (i = 0; i < point_count; ++i) {
349 cmd->points[i].x = (short)points[i*2];
350 cmd->points[i].y = (short)points[i*2+1];
354nk_fill_polygon(
struct nk_command_buffer *b,
const float *points,
int point_count,
362 if (!b || col.a == 0)
return;
363 size =
sizeof(*cmd) +
sizeof(short) * 2 * (nk_size)point_count;
365 nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
368 cmd->point_count = (
unsigned short)point_count;
369 for (i = 0; i < point_count; ++i) {
370 cmd->points[i].x = (short)points[i*2+0];
371 cmd->points[i].y = (short)points[i*2+1];
375nk_stroke_polyline(
struct nk_command_buffer *b,
const float *points,
int point_count,
376 float line_thickness,
struct nk_color col)
383 if (!b || col.a == 0 || line_thickness <= 0)
return;
384 size =
sizeof(*cmd) +
sizeof(short) * 2 * (nk_size)point_count;
388 cmd->point_count = (
unsigned short)point_count;
389 cmd->line_thickness = (
unsigned short)line_thickness;
390 for (i = 0; i < point_count; ++i) {
391 cmd->points[i].x = (short)points[i*2];
392 cmd->points[i].y = (short)points[i*2+1];
402 if (b->use_clipping) {
403 const struct nk_rect *c = &b->clip;
404 if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
409 nk_command_buffer_push(b, NK_COMMAND_IMAGE,
sizeof(*cmd));
413 cmd->w = (
unsigned short)NK_MAX(0, r.w);
414 cmd->h = (
unsigned short)NK_MAX(0, r.h);
424 nk_ushort rgnX, rgnY, rgnW, rgnH;
425 rgnX = slcimg->region[0];
426 rgnY = slcimg->region[1];
427 rgnW = slcimg->region[2];
428 rgnH = slcimg->region[3];
431 img.handle = slcimg->handle;
434 img.region[0] = rgnX;
435 img.region[1] = rgnY;
436 img.region[2] = slc->l;
437 img.region[3] = slc->t;
440 nk_rect(r.x, r.y, (
float)slc->l, (
float)slc->t),
443#define IMG_RGN(x, y, w, h) img.region[0] = (nk_ushort)(x); img.region[1] = (nk_ushort)(y); img.region[2] = (nk_ushort)(w); img.region[3] = (nk_ushort)(h);
446 IMG_RGN(rgnX + slc->l, rgnY, rgnW - slc->l - slc->r, slc->t);
448 nk_rect(r.x + (
float)slc->l, r.y, (
float)(r.w - slc->l - slc->r), (
float)slc->t),
452 IMG_RGN(rgnX + rgnW - slc->r, rgnY, slc->r, slc->t);
454 nk_rect(r.x + r.w - (
float)slc->r, r.y, (
float)slc->r, (
float)slc->t),
458 IMG_RGN(rgnX, rgnY + slc->t, slc->l, rgnH - slc->t - slc->b);
460 nk_rect(r.x, r.y + (
float)slc->t, (
float)slc->l, (
float)(r.h - slc->t - slc->b)),
464 IMG_RGN(rgnX + slc->l, rgnY + slc->t, rgnW - slc->l - slc->r, rgnH - slc->t - slc->b);
466 nk_rect(r.x + (
float)slc->l, r.y + (
float)slc->t, (
float)(r.w - slc->l - slc->r), (
float)(r.h - slc->t - slc->b)),
470 IMG_RGN(rgnX + rgnW - slc->r, rgnY + slc->t, slc->r, rgnH - slc->t - slc->b);
472 nk_rect(r.x + r.w - (
float)slc->r, r.y + (
float)slc->t, (
float)slc->r, (
float)(r.h - slc->t - slc->b)),
476 IMG_RGN(rgnX, rgnY + rgnH - slc->b, slc->l, slc->b);
478 nk_rect(r.x, r.y + r.h - (
float)slc->b, (
float)slc->l, (
float)slc->b),
482 IMG_RGN(rgnX + slc->l, rgnY + rgnH - slc->b, rgnW - slc->l - slc->r, slc->b);
484 nk_rect(r.x + (
float)slc->l, r.y + r.h - (
float)slc->b, (
float)(r.w - slc->l - slc->r), (
float)slc->b),
488 IMG_RGN(rgnX + rgnW - slc->r, rgnY + rgnH - slc->b, slc->r, slc->b);
490 nk_rect(r.x + r.w - (
float)slc->r, r.y + r.h - (
float)slc->b, (
float)slc->r, (
float)slc->b),
497 nk_command_custom_callback cb,
nk_handle usr)
502 if (b->use_clipping) {
503 const struct nk_rect *c = &b->clip;
504 if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
509 nk_command_buffer_push(b, NK_COMMAND_CUSTOM,
sizeof(*cmd));
513 cmd->w = (
unsigned short)NK_MAX(0, r.w);
514 cmd->h = (
unsigned short)NK_MAX(0, r.h);
515 cmd->callback_data = usr;
520 const char *
string,
int length,
const struct nk_user_font *font,
523 float text_width = 0;
528 if (!b || !
string || !length || (bg.a == 0 && fg.a == 0))
return;
529 if (b->use_clipping) {
530 const struct nk_rect *c = &b->clip;
531 if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
536 text_width = font->
width(font->userdata, font->
height,
string, length);
537 if (text_width > r.w){
539 float txt_width = (float)text_width;
540 length = nk_text_clamp(font,
string, length, r.w, &glyphs, &txt_width, 0,0);
545 nk_command_buffer_push(b, NK_COMMAND_TEXT,
sizeof(*cmd) + (nk_size)(length + 1));
549 cmd->w = (
unsigned short)r.w;
550 cmd->h = (
unsigned short)r.h;
551 cmd->background = bg;
552 cmd->foreground = fg;
554 cmd->length = length;
555 cmd->height = font->
height;
556 NK_MEMCPY(cmd->string,
string, (nk_size)length);
557 cmd->string[length] =
'\0';
struct nk_memory memory
!< memory management type
nk_size allocated
!< growing factor for dynamic memory management
command base and header of every command inside the buffer
nk_text_width_f width
!< max height of the font
float height
!< user provided font handle