Added a new command type: NK_COMMAND_CUSTOM

Added a new command type 'NK_COMMAND_CUSTOM'.
A custom command contains a callback that will be called to fill the draw list.

Usage: 
static void custom_convert(struct nk_draw_list *list,
    short x, short y, unsigned short w, unsigned short h,
    nk_handle callback_data){/*Fill draw list.*/}

[...]nk_begin[...]
nk_custom(ctx, customConvert, nk_handle_id(0));

The handle can be used to pass data.
This commit is contained in:
ClaasJG
2017-02-25 14:13:08 +01:00
committed by GitHub
parent 064e31f3db
commit 4c25cd34f5
+58 -1
View File
@@ -1773,6 +1773,9 @@ NK_API void nk_textedit_redo(struct nk_text_edit*);
update and draw your widget. The reason for seperating is to only draw and
update what is actually neccessary which is crucial for performance.
*/
typedef void (*nk_command_custom_callback)(struct nk_draw_list *list, short x, short y, unsigned short w, unsigned short h, nk_handle callback_data);
enum nk_command_type {
NK_COMMAND_NOP,
NK_COMMAND_SCISSOR,
@@ -1791,7 +1794,8 @@ enum nk_command_type {
NK_COMMAND_POLYGON_FILLED,
NK_COMMAND_POLYLINE,
NK_COMMAND_TEXT,
NK_COMMAND_IMAGE
NK_COMMAND_IMAGE,
NK_COMMAND_CUSTOM
};
/* command base and header of every command inside the buffer */
@@ -1933,6 +1937,14 @@ struct nk_command_image {
struct nk_color col;
};
struct nk_command_custom {
struct nk_command header;
short x, y;
unsigned short w, h;
nk_handle callback_data;
nk_command_custom_callback callback;
};
struct nk_command_text {
struct nk_command header;
const struct nk_user_font *font;
@@ -1979,6 +1991,7 @@ NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count,
/* misc */
NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
NK_API void nk_draw_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback callback, nk_handle callback_data);
NK_API void nk_draw_text(struct nk_command_buffer*, struct nk_rect, const char *text, int len, const struct nk_user_font*, struct nk_color, struct nk_color);
NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
NK_API const struct nk_command* nk__begin(struct nk_context*);
@@ -6495,6 +6508,30 @@ nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
cmd->col = col;
}
NK_API void
nk_draw_custom(struct nk_command_buffer *b, struct nk_rect r,
nk_command_custom_callback callback, nk_handle callback_data)
{
struct nk_command_custom *cmd;
NK_ASSERT(b);
if (!b) return;
if (b->use_clipping) {
const struct nk_rect *c = &b->clip;
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))
return;
}
cmd = (struct nk_command_custom*)
nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
if (!cmd) return;
cmd->x = (short)r.x;
cmd->y = (short)r.y;
cmd->w = (unsigned short)NK_MAX(0, r.w);
cmd->h = (unsigned short)NK_MAX(0, r.h);
cmd->callback_data = callback_data;
cmd->callback = callback;
}
NK_API void
nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
const char *string, int length, const struct nk_user_font *font,
@@ -7821,6 +7858,10 @@ nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
case NK_COMMAND_IMAGE: {
const struct nk_command_image *i = (const struct nk_command_image*)cmd;
nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
} break;
case NK_COMMAND_CUSTOM: {
const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
} break;
default: break;
}
@@ -19162,6 +19203,22 @@ nk_image(struct nk_context *ctx, struct nk_image img)
nk_draw_image(&win->buffer, bounds, &img, nk_white);
}
NK_API void
nk_custom(struct nk_context *ctx, nk_command_custom_callback callback, nk_handle callback_data)
{
struct nk_window *win;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout) return;
win = ctx->current;
if (!nk_widget(&bounds, ctx)) return;
nk_draw_custom(&win->buffer, bounds, callback, callback_data);
}
/*----------------------------------------------------------------
*
* BUTTON