-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathnuklear_context.c
344 lines (323 loc) · 9.98 KB
/
nuklear_context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "nuklear.h"
#include "nuklear_internal.h"
/* ==============================================================
*
* CONTEXT
*
* ===============================================================*/
NK_INTERN void
nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
{
NK_ASSERT(ctx);
if (!ctx) return;
nk_zero_struct(*ctx);
nk_style_default(ctx);
ctx->seq = 1;
if (font) ctx->style.font = font;
#ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
nk_draw_list_init(&ctx->draw_list);
#endif
}
#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
NK_API nk_bool
nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
{
struct nk_allocator alloc;
alloc.userdata.ptr = 0;
alloc.alloc = nk_malloc;
alloc.free = nk_mfree;
return nk_init(ctx, &alloc, font);
}
#endif
NK_API nk_bool
nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
const struct nk_user_font *font)
{
NK_ASSERT(memory);
if (!memory) return 0;
nk_setup(ctx, font);
nk_buffer_init_fixed(&ctx->memory, memory, size);
ctx->use_pool = nk_false;
return 1;
}
NK_API nk_bool
nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
struct nk_buffer *pool, const struct nk_user_font *font)
{
NK_ASSERT(cmds);
NK_ASSERT(pool);
if (!cmds || !pool) return 0;
nk_setup(ctx, font);
ctx->memory = *cmds;
if (pool->type == NK_BUFFER_FIXED) {
/* take memory from buffer and alloc fixed pool */
nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
} else {
/* create dynamic pool from buffer allocator */
struct nk_allocator *alloc = &pool->pool;
nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
}
ctx->use_pool = nk_true;
return 1;
}
NK_API nk_bool
nk_init(struct nk_context *ctx, const struct nk_allocator *alloc,
const struct nk_user_font *font)
{
NK_ASSERT(alloc);
if (!alloc) return 0;
nk_setup(ctx, font);
nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
ctx->use_pool = nk_true;
return 1;
}
#ifdef NK_INCLUDE_COMMAND_USERDATA
NK_API void
nk_set_user_data(struct nk_context *ctx, nk_handle handle)
{
if (!ctx) return;
ctx->userdata = handle;
if (ctx->current)
ctx->current->buffer.userdata = handle;
}
#endif
NK_API void
nk_free(struct nk_context *ctx)
{
NK_ASSERT(ctx);
if (!ctx) return;
nk_buffer_free(&ctx->memory);
if (ctx->use_pool)
nk_pool_free(&ctx->pool);
nk_zero(&ctx->input, sizeof(ctx->input));
nk_zero(&ctx->style, sizeof(ctx->style));
nk_zero(&ctx->memory, sizeof(ctx->memory));
ctx->seq = 0;
ctx->build = 0;
ctx->begin = 0;
ctx->end = 0;
ctx->active = 0;
ctx->current = 0;
ctx->freelist = 0;
ctx->count = 0;
}
NK_API void
nk_clear(struct nk_context *ctx)
{
struct nk_window *iter;
struct nk_window *next;
NK_ASSERT(ctx);
if (!ctx) return;
if (ctx->use_pool)
nk_buffer_clear(&ctx->memory);
else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
ctx->build = 0;
ctx->memory.calls = 0;
ctx->last_widget_state = 0;
ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
/* garbage collector */
iter = ctx->begin;
while (iter) {
/* make sure valid minimized windows do not get removed */
if ((iter->flags & NK_WINDOW_MINIMIZED) &&
!(iter->flags & NK_WINDOW_CLOSED) &&
iter->seq == ctx->seq) {
iter = iter->next;
continue;
}
/* remove hotness from hidden or closed windows*/
if (((iter->flags & NK_WINDOW_HIDDEN) ||
(iter->flags & NK_WINDOW_CLOSED)) &&
iter == ctx->active) {
ctx->active = iter->prev;
ctx->end = iter->prev;
if (!ctx->end)
ctx->begin = 0;
if (ctx->active)
ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
}
/* free unused popup windows */
if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
nk_free_window(ctx, iter->popup.win);
iter->popup.win = 0;
}
/* remove unused window state tables */
{struct nk_table *n, *it = iter->tables;
while (it) {
n = it->next;
if (it->seq != ctx->seq) {
nk_remove_table(iter, it);
nk_zero(it, sizeof(union nk_page_data));
nk_free_table(ctx, it);
if (it == iter->tables)
iter->tables = n;
} it = n;
}}
/* window itself is not used anymore so free */
if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
next = iter->next;
nk_remove_window(ctx, iter);
nk_free_window(ctx, iter);
iter = next;
} else iter = iter->next;
}
ctx->seq++;
}
NK_LIB void
nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
{
NK_ASSERT(ctx);
NK_ASSERT(buffer);
if (!ctx || !buffer) return;
buffer->begin = ctx->memory.allocated;
buffer->end = buffer->begin;
buffer->last = buffer->begin;
buffer->clip = nk_null_rect;
}
NK_LIB void
nk_start(struct nk_context *ctx, struct nk_window *win)
{
NK_ASSERT(ctx);
NK_ASSERT(win);
nk_start_buffer(ctx, &win->buffer);
}
NK_LIB void
nk_start_popup(struct nk_context *ctx, struct nk_window *win)
{
struct nk_popup_buffer *buf;
NK_ASSERT(ctx);
NK_ASSERT(win);
if (!ctx || !win) return;
/* save buffer fill state for popup */
buf = &win->popup.buf;
buf->begin = win->buffer.end;
buf->end = win->buffer.end;
buf->parent = win->buffer.last;
buf->last = buf->begin;
buf->active = nk_true;
}
NK_LIB void
nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
{
struct nk_popup_buffer *buf;
NK_ASSERT(ctx);
NK_ASSERT(win);
if (!ctx || !win) return;
buf = &win->popup.buf;
buf->last = win->buffer.last;
buf->end = win->buffer.end;
}
NK_LIB void
nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
{
NK_ASSERT(ctx);
NK_ASSERT(buffer);
if (!ctx || !buffer) return;
buffer->end = ctx->memory.allocated;
}
NK_LIB void
nk_finish(struct nk_context *ctx, struct nk_window *win)
{
struct nk_popup_buffer *buf;
struct nk_command *parent_last;
void *memory;
NK_ASSERT(ctx);
NK_ASSERT(win);
if (!ctx || !win) return;
nk_finish_buffer(ctx, &win->buffer);
if (!win->popup.buf.active) return;
buf = &win->popup.buf;
memory = ctx->memory.memory.ptr;
parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
parent_last->next = buf->end;
}
NK_LIB void
nk_build(struct nk_context *ctx)
{
struct nk_window *it = 0;
struct nk_command *cmd = 0;
nk_byte *buffer = 0;
/* draw cursor overlay */
if (!ctx->style.cursor_active)
ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
struct nk_rect mouse_bounds;
const struct nk_cursor *cursor = ctx->style.cursor_active;
nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
nk_start_buffer(ctx, &ctx->overlay);
mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
mouse_bounds.w = cursor->size.x;
mouse_bounds.h = cursor->size.y;
nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
nk_finish_buffer(ctx, &ctx->overlay);
}
/* build one big draw command list out of all window buffers */
it = ctx->begin;
buffer = (nk_byte*)ctx->memory.memory.ptr;
while (it != 0) {
struct nk_window *next = it->next;
if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
it->seq != ctx->seq)
goto cont;
cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
while (next && ((next->buffer.last == next->buffer.begin) ||
(next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
next = next->next; /* skip empty command buffers */
if (next) cmd->next = next->buffer.begin;
cont: it = next;
}
/* append all popup draw commands into lists */
it = ctx->begin;
while (it != 0) {
struct nk_window *next = it->next;
struct nk_popup_buffer *buf;
if (!it->popup.buf.active)
goto skip;
buf = &it->popup.buf;
cmd->next = buf->begin;
cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
buf->active = nk_false;
skip: it = next;
}
if (cmd) {
/* append overlay commands */
if (ctx->overlay.end != ctx->overlay.begin)
cmd->next = ctx->overlay.begin;
else cmd->next = ctx->memory.allocated;
}
}
NK_API const struct nk_command*
nk__begin(struct nk_context *ctx)
{
struct nk_window *iter;
nk_byte *buffer;
NK_ASSERT(ctx);
if (!ctx) return 0;
if (!ctx->count) return 0;
buffer = (nk_byte*)ctx->memory.memory.ptr;
if (!ctx->build) {
nk_build(ctx);
ctx->build = nk_true;
}
iter = ctx->begin;
while (iter && ((iter->buffer.begin == iter->buffer.end) ||
(iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
iter = iter->next;
if (!iter) return 0;
return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
}
NK_API const struct nk_command*
nk__next(struct nk_context *ctx, const struct nk_command *cmd)
{
nk_byte *buffer;
const struct nk_command *next;
NK_ASSERT(ctx);
if (!ctx || !cmd || !ctx->count) return 0;
if (cmd->next >= ctx->memory.allocated) return 0;
buffer = (nk_byte*)ctx->memory.memory.ptr;
next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
return next;
}