Debian Codegraph @

src/nuklear/nuklear.h

raw symbols SCIP
1 /*
2 /// # Nuklear
3 /// ![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
4 ///
5 /// ## Contents
6 /// 1. About section
7 /// 2. Highlights section
8 /// 3. Features section
9 /// 4. Usage section
10 /// 1. Flags section
11 /// 2. Constants section
12 /// 3. Dependencies section
13 /// 5. Example section
14 /// 6. API section
15 /// 1. Context section
16 /// 2. Input section
17 /// 3. Drawing section
18 /// 4. Window section
19 /// 5. Layouting section
20 /// 6. Groups section
21 /// 7. Tree section
22 /// 8. Properties section
23 /// 7. License section
24 /// 8. Changelog section
25 /// 9. Gallery section
26 /// 10. Credits section
27 ///
28 /// ## About
29 /// This is a minimal state immediate mode graphical user interface toolkit
30 /// written in ANSI C and licensed under public domain. It was designed as a simple
31 /// embeddable user interface for application and does not have any dependencies,
32 /// a default renderbackend or OS window and input handling but instead provides a very modular
33 /// library approach by using simple input state for input and draw
34 /// commands describing primitive shapes as output. So instead of providing a
35 /// layered library that tries to abstract over a number of platform and
36 /// render backends it only focuses on the actual UI.
37 ///
38 /// ## Highlights
39 /// - Graphical user interface toolkit
40 /// - Single header library
41 /// - Written in C89 (a.k.a. ANSI C or ISO C90)
42 /// - Small codebase (~18kLOC)
43 /// - Focus on portability, efficiency and simplicity
44 /// - No dependencies (not even the standard library if not wanted)
45 /// - Fully skinnable and customizable
46 /// - Low memory footprint with total memory control if needed or wanted
47 /// - UTF-8 support
48 /// - No global or hidden state
49 /// - Customizable library modules (you can compile and use only what you need)
50 /// - Optional font baker and vertex buffer output
51 ///
52 /// ## Features
53 /// - Absolutely no platform dependent code
54 /// - Memory management control ranging from/to
55 /// - Ease of use by allocating everything from standard library
56 /// - Control every byte of memory inside the library
57 /// - Font handling control ranging from/to
58 /// - Use your own font implementation for everything
59 /// - Use this libraries internal font baking and handling API
60 /// - Drawing output control ranging from/to
61 /// - Simple shapes for more high level APIs which already have drawing capabilities
62 /// - Hardware accessible anti-aliased vertex buffer output
63 /// - Customizable colors and properties ranging from/to
64 /// - Simple changes to color by filling a simple color table
65 /// - Complete control with ability to use skinning to decorate widgets
66 /// - Bendable UI library with widget ranging from/to
67 /// - Basic widgets like buttons, checkboxes, slider, ...
68 /// - Advanced widget like abstract comboboxes, contextual menus,...
69 /// - Compile time configuration to only compile what you need
70 /// - Subset which can be used if you do not want to link or use the standard library
71 /// - Can be easily modified to only update on user input instead of frame updates
72 ///
73 /// ## Usage
74 /// This library is self contained in one single header file and can be used either
75 /// in header only mode or in implementation mode. The header only mode is used
76 /// by default when included and allows including this header in other headers
77 /// and does not contain the actual implementation. <br /><br />
78 ///
79 /// The implementation mode requires to define the preprocessor macro
80 /// NK_IMPLEMENTATION in *one* .c/.cpp file before #includeing this file, e.g.:
81 ///
82 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~C
83 /// #define NK_IMPLEMENTATION
84 /// #include "nuklear.h"
85 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86 ///
87 /// Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
88 /// below in header and implementation mode if you want to use additional functionality
89 /// or need more control over the library.
90 ///
91 /// !!! WARNING
92 /// Every time nuklear is included define the same compiler flags. This very important not doing so could lead to compiler errors or even worse stack corruptions.
93 ///
94 /// ### Flags
95 /// Flag | Description
96 /// --------------------------------|------------------------------------------
97 /// NK_PRIVATE | If defined declares all functions as static, so they can only be accessed inside the file that contains the implementation
98 /// NK_INCLUDE_FIXED_TYPES | If defined it will include header `<stdint.h>` for fixed sized types otherwise nuklear tries to select the correct type. If that fails it will throw a compiler error and you have to select the correct types yourself.
99 /// NK_INCLUDE_DEFAULT_ALLOCATOR | If defined it will include header `<stdlib.h>` and provide additional functions to use this library without caring for memory allocation control and therefore ease memory management.
100 /// NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
101 /// NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdio.h> and provide additional functions depending on file loading.
102 /// NK_INCLUDE_VERTEX_BUFFER_OUTPUT | Defining this adds a vertex draw command list backend to this library, which allows you to convert queue commands into vertex draw commands. This is mainly if you need a hardware accessible format for OpenGL, DirectX, Vulkan, Metal,...
103 /// NK_INCLUDE_FONT_BAKING | Defining this adds `stb_truetype` and `stb_rect_pack` implementation to this library and provides font baking and rendering. If you already have font handling or do not want to use this font handler you don't have to define it.
104 /// NK_INCLUDE_DEFAULT_FONT | Defining this adds the default font: ProggyClean.ttf into this library which can be loaded into a font atlas and allows using this library without having a truetype font
105 /// NK_INCLUDE_COMMAND_USERDATA | Defining this adds a userdata pointer into each command. Can be useful for example if you want to provide custom shaders depending on the used widget. Can be combined with the style structures.
106 /// NK_BUTTON_TRIGGER_ON_RELEASE | Different platforms require button clicks occurring either on buttons being pressed (up to down) or released (down to up). By default this library will react on buttons being pressed, but if you define this it will only trigger if a button is released.
107 /// NK_ZERO_COMMAND_MEMORY | Defining this will zero out memory for each drawing command added to a drawing queue (inside nk_command_buffer_push). Zeroing command memory is very useful for fast checking (using memcmp) if command buffers are equal and avoid drawing frames when nothing on screen has changed since previous frame.
108 /// NK_UINT_DRAW_INDEX | Defining this will set the size of vertex index elements when using NK_VERTEX_BUFFER_OUTPUT to 32bit instead of the default of 16bit
109 /// NK_KEYSTATE_BASED_INPUT | Define this if your backend uses key state for each frame rather than key press/release events
110 ///
111 /// !!! WARNING
112 /// The following flags will pull in the standard C library:
113 /// - NK_INCLUDE_DEFAULT_ALLOCATOR
114 /// - NK_INCLUDE_STANDARD_IO
115 /// - NK_INCLUDE_STANDARD_VARARGS
116 ///
117 /// !!! WARNING
118 /// The following flags if defined need to be defined for both header and implementation:
119 /// - NK_INCLUDE_FIXED_TYPES
120 /// - NK_INCLUDE_DEFAULT_ALLOCATOR
121 /// - NK_INCLUDE_STANDARD_VARARGS
122 /// - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
123 /// - NK_INCLUDE_FONT_BAKING
124 /// - NK_INCLUDE_DEFAULT_FONT
125 /// - NK_INCLUDE_STANDARD_VARARGS
126 /// - NK_INCLUDE_COMMAND_USERDATA
127 /// - NK_UINT_DRAW_INDEX
128 ///
129 /// ### Constants
130 /// Define | Description
131 /// --------------------------------|---------------------------------------
132 /// NK_BUFFER_DEFAULT_INITIAL_SIZE | Initial buffer size allocated by all buffers while using the default allocator functions included by defining NK_INCLUDE_DEFAULT_ALLOCATOR. If you don't want to allocate the default 4k memory then redefine it.
133 /// NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
134 /// NK_INPUT_MAX | Defines the max number of bytes which can be added as text input in one frame. Under normal circumstances this should be more than sufficient.
135 ///
136 /// !!! WARNING
137 /// The following constants if defined need to be defined for both header and implementation:
138 /// - NK_MAX_NUMBER_BUFFER
139 /// - NK_BUFFER_DEFAULT_INITIAL_SIZE
140 /// - NK_INPUT_MAX
141 ///
142 /// ### Dependencies
143 /// Function | Description
144 /// ------------|---------------------------------------------------------------
145 /// NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
146 /// NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
147 /// NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
148 /// NK_SQRT | You can define this to 'sqrt' or your own sqrt implementation replacement. If not nuklear will use its own slow and not highly accurate version.
149 /// NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
150 /// NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
151 /// NK_STRTOD | You can define this to `strtod` or your own string to double conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
152 /// NK_DTOA | You can define this to `dtoa` or your own double to string conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
153 /// NK_VSNPRINTF| If you define `NK_INCLUDE_STANDARD_VARARGS` as well as `NK_INCLUDE_STANDARD_IO` and want to be safe define this to `vsnprintf` on compilers supporting later versions of C or C++. By default nuklear will check for your stdlib version in C as well as compiler version in C++. if `vsnprintf` is available it will define it to `vsnprintf` directly. If not defined and if you have older versions of C or C++ it will be defined to `vsprintf` which is unsafe.
154 ///
155 /// !!! WARNING
156 /// The following dependencies will pull in the standard C library if not redefined:
157 /// - NK_ASSERT
158 ///
159 /// !!! WARNING
160 /// The following dependencies if defined need to be defined for both header and implementation:
161 /// - NK_ASSERT
162 ///
163 /// !!! WARNING
164 /// The following dependencies if defined need to be defined only for the implementation part:
165 /// - NK_MEMSET
166 /// - NK_MEMCPY
167 /// - NK_SQRT
168 /// - NK_SIN
169 /// - NK_COS
170 /// - NK_STRTOD
171 /// - NK_DTOA
172 /// - NK_VSNPRINTF
173 ///
174 /// ## Example
175 ///
176 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
177 /// // init gui state
178 /// enum {EASY, HARD};
179 /// static int op = EASY;
180 /// static float value = 0.6f;
181 /// static int i = 20;
182 /// struct nk_context ctx;
183 ///
184 /// nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
185 /// if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
186 /// NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
187 /// // fixed widget pixel width
188 /// nk_layout_row_static(&ctx, 30, 80, 1);
189 /// if (nk_button_label(&ctx, "button")) {
190 /// // event handling
191 /// }
192 ///
193 /// // fixed widget window ratio width
194 /// nk_layout_row_dynamic(&ctx, 30, 2);
195 /// if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
196 /// if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
197 ///
198 /// // custom widget pixel width
199 /// nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
200 /// {
201 /// nk_layout_row_push(&ctx, 50);
202 /// nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
203 /// nk_layout_row_push(&ctx, 110);
204 /// nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
205 /// }
206 /// nk_layout_row_end(&ctx);
207 /// }
208 /// nk_end(&ctx);
209 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210 ///
211 /// ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
212 ///
213 /// ## API
214 ///
215 */
216 #ifndef NK_SINGLE_FILE
217 #define NK_SINGLE_FILE
218 #endif
219
220 #ifndef NK_NUKLEAR_H_
221 #define NK_NUKLEAR_H_
222
223 #ifdef __cplusplus
224 extern "C" {
225 #endif
226 /*
227 * ==============================================================
228 *
229 * CONSTANTS
230 *
231 * ===============================================================
232 */
233 #define NK_UNDEFINED (-1.0f)
234 #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
235 #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
236 #ifndef NK_INPUT_MAX
237 #define NK_INPUT_MAX 16
238 #endif
239 #ifndef NK_MAX_NUMBER_BUFFER
240 #define NK_MAX_NUMBER_BUFFER 64
241 #endif
242 #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
243 #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
244 #endif
245 /*
246 * ==============================================================
247 *
248 * HELPER
249 *
250 * ===============================================================
251 */
252 #ifndef NK_API
253 #ifdef NK_PRIVATE
254 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
255 #define NK_API static inline
256 #elif defined(__cplusplus)
257 #define NK_API static inline
258 #else
259 #define NK_API static
260 #endif
261 #else
262 #define NK_API extern
263 #endif
264 #endif
265 #ifndef NK_LIB
266 #ifdef NK_SINGLE_FILE
267 #define NK_LIB static
268 #else
269 #define NK_LIB extern
270 #endif
271 #endif
272
273 #define NK_INTERN static
274 #define NK_STORAGE static
275 #define NK_GLOBAL static
276
277 #define NK_FLAG(x) (1 << (x))
278 #define NK_STRINGIFY(x) #x
279 #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
280 #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
281 #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
282 #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
283
284 #ifdef _MSC_VER
285 #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
286 #else
287 #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
288 #endif
289
290 #ifndef NK_STATIC_ASSERT
291 #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
292 #endif
293
294 #ifndef NK_FILE_LINE
295 #ifdef _MSC_VER
296 #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
297 #else
298 #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
299 #endif
300 #endif
301
302 #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
303 #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
304 #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
305
306 #ifdef NK_INCLUDE_STANDARD_VARARGS
307 #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
308 #include <sal.h>
309 #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
310 #else
311 #define NK_PRINTF_FORMAT_STRING
312 #endif
313 #if defined(__GNUC__)
314 #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
315 #define NK_PRINTF_VALIST_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, 0)))
316 #else
317 #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
318 #define NK_PRINTF_VALIST_FUNC(fmtargnumber)
319 #endif
320 #endif
321
322 /*
323 * ===============================================================
324 *
325 * BASIC
326 *
327 * ===============================================================
328 */
329 #ifdef NK_INCLUDE_FIXED_TYPES
330 #include <stdint.h>
331 #define NK_INT8 int8_t
332 #define NK_UINT8 uint8_t
333 #define NK_INT16 int16_t
334 #define NK_UINT16 uint16_t
335 #define NK_INT32 int32_t
336 #define NK_UINT32 uint32_t
337 #define NK_SIZE_TYPE uintptr_t
338 #define NK_POINTER_TYPE uintptr_t
339 #else
340 #ifndef NK_INT8
341 #define NK_INT8 signed char
342 #endif
343 #ifndef NK_UINT8
344 #define NK_UINT8 unsigned char
345 #endif
346 #ifndef NK_INT16
347 #define NK_INT16 signed short
348 #endif
349 #ifndef NK_UINT16
350 #define NK_UINT16 unsigned short
351 #endif
352 #ifndef NK_INT32
353 #if defined(_MSC_VER)
354 #define NK_INT32 __int32
355 #else
356 #define NK_INT32 signed int
357 #endif
358 #endif
359 #ifndef NK_UINT32
360 #if defined(_MSC_VER)
361 #define NK_UINT32 unsigned __int32
362 #else
363 #define NK_UINT32 unsigned int
364 #endif
365 #endif
366 #ifndef NK_SIZE_TYPE
367 #if defined(_WIN64) && defined(_MSC_VER)
368 #define NK_SIZE_TYPE unsigned __int64
369 #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
370 #define NK_SIZE_TYPE unsigned __int32
371 #elif defined(__GNUC__) || defined(__clang__)
372 #if defined(__x86_64__) || defined(__ppc64__)
373 #define NK_SIZE_TYPE unsigned long
374 #else
375 #define NK_SIZE_TYPE unsigned int
376 #endif
377 #else
378 #define NK_SIZE_TYPE unsigned long
379 #endif
380 #endif
381 #ifndef NK_POINTER_TYPE
382 #if defined(_WIN64) && defined(_MSC_VER)
383 #define NK_POINTER_TYPE unsigned __int64
384 #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
385 #define NK_POINTER_TYPE unsigned __int32
386 #elif defined(__GNUC__) || defined(__clang__)
387 #if defined(__x86_64__) || defined(__ppc64__)
388 #define NK_POINTER_TYPE unsigned long
389 #else
390 #define NK_POINTER_TYPE unsigned int
391 #endif
392 #else
393 #define NK_POINTER_TYPE unsigned long
394 #endif
395 #endif
396 #endif
397
398 typedef NK_INT8 nk_char;
399 typedef NK_UINT8 nk_uchar;
400 typedef NK_UINT8 nk_byte;
401 typedef NK_INT16 nk_short;
402 typedef NK_UINT16 nk_ushort;
403 typedef NK_INT32 nk_int;
404 typedef NK_UINT32 nk_uint;
405 typedef NK_SIZE_TYPE nk_size;
406 typedef NK_POINTER_TYPE nk_ptr;
407
408 typedef nk_uint nk_hash;
409 typedef nk_uint nk_flags;
410 typedef nk_uint nk_rune;
411
412 /* Make sure correct type size:
413 * This will fire with a negative subscript error if the type sizes
414 * are set incorrectly by the compiler, and compile out if not */
415 NK_STATIC_ASSERT(sizeof(nk_short) == 2);
416 NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
417 NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
418 NK_STATIC_ASSERT(sizeof(nk_int) == 4);
419 NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
420 NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
421 NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
422 NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
423 NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
424
425 /* ============================================================================
426 *
427 * API
428 *
429 * =========================================================================== */
430 struct nk_buffer;
431 struct nk_allocator;
432 struct nk_command_buffer;
433 struct nk_draw_command;
434 struct nk_convert_config;
435 struct nk_style_item;
436 struct nk_text_edit;
437 struct nk_draw_list;
438 struct nk_user_font;
439 struct nk_panel;
440 struct nk_context;
441 struct nk_draw_vertex_layout_element;
442 struct nk_style_button;
443 struct nk_style_toggle;
444 struct nk_style_selectable;
445 struct nk_style_slide;
446 struct nk_style_progress;
447 struct nk_style_scrollbar;
448 struct nk_style_edit;
449 struct nk_style_property;
450 struct nk_style_chart;
451 struct nk_style_combo;
452 struct nk_style_tab;
453 struct nk_style_window_header;
454 struct nk_style_window;
455
456 enum {nk_false, nk_true};
457 struct nk_color {nk_byte r,g,b,a;};
458 struct nk_colorf {float r,g,b,a;};
459 struct nk_vec2 {float x,y;};
460 struct nk_vec2i {short x, y;};
461 struct nk_rect {float x,y,w,h;};
462 struct nk_recti {short x,y,w,h;};
463 typedef char nk_glyph[NK_UTF_SIZE];
464 typedef union {void *ptr; int id;} nk_handle;
465 struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
466 struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
467 struct nk_scroll {nk_uint x, y;};
468
469 enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
470 enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
471 enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
472 enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
473 enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
474 enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
475 enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
476 enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
477 enum nk_color_format {NK_RGB, NK_RGBA};
478 enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
479 enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
480 enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
481
482 typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
483 typedef void (*nk_plugin_free)(nk_handle, void *old);
484 typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
485 typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
486 typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
487
488 struct nk_allocator {
489 nk_handle userdata;
490 nk_plugin_alloc alloc;
491 nk_plugin_free free;
492 };
493 enum nk_symbol_type {
494 NK_SYMBOL_NONE,
495 NK_SYMBOL_X,
496 NK_SYMBOL_UNDERSCORE,
497 NK_SYMBOL_CIRCLE_SOLID,
498 NK_SYMBOL_CIRCLE_OUTLINE,
499 NK_SYMBOL_RECT_SOLID,
500 NK_SYMBOL_RECT_OUTLINE,
501 NK_SYMBOL_TRIANGLE_UP,
502 NK_SYMBOL_TRIANGLE_DOWN,
503 NK_SYMBOL_TRIANGLE_LEFT,
504 NK_SYMBOL_TRIANGLE_RIGHT,
505 NK_SYMBOL_PLUS,
506 NK_SYMBOL_MINUS,
507 NK_SYMBOL_MAX
508 };
509 /* =============================================================================
510 *
511 * CONTEXT
512 *
513 * =============================================================================*/
514 /*/// ### Context
515 /// Contexts are the main entry point and the majestro of nuklear and contain all required state.
516 /// They are used for window, memory, input, style, stack, commands and time management and need
517 /// to be passed into all nuklear GUI specific functions.
518 ///
519 /// #### Usage
520 /// To use a context it first has to be initialized which can be achieved by calling
521 /// one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
522 /// Each takes in a font handle and a specific way of handling memory. Memory control
523 /// hereby ranges from standard library to just specifying a fixed sized block of memory
524 /// which nuklear has to manage itself from.
525 ///
526 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
527 /// struct nk_context ctx;
528 /// nk_init_xxx(&ctx, ...);
529 /// while (1) {
530 /// // [...]
531 /// nk_clear(&ctx);
532 /// }
533 /// nk_free(&ctx);
534 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
535 ///
536 /// #### Reference
537 /// Function | Description
538 /// --------------------|-------------------------------------------------------
539 /// __nk_init_default__ | Initializes context with standard library memory allocation (malloc,free)
540 /// __nk_init_fixed__ | Initializes context from single fixed size memory block
541 /// __nk_init__ | Initializes context with memory allocator callbacks for alloc and free
542 /// __nk_init_custom__ | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
543 /// __nk_clear__ | Called at the end of the frame to reset and prepare the context for the next frame
544 /// __nk_free__ | Shutdown and free all memory allocated inside the context
545 /// __nk_set_user_data__| Utility function to pass user data to draw command
546 */
547 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
548 /*/// #### nk_init_default
549 /// Initializes a `nk_context` struct with a default standard library allocator.
550 /// Should be used if you don't want to be bothered with memory management in nuklear.
551 ///
552 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
553 /// int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
554 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
555 ///
556 /// Parameter | Description
557 /// ------------|---------------------------------------------------------------
558 /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
559 /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
560 ///
561 /// Returns either `false(0)` on failure or `true(1)` on success.
562 ///
563 */
564 NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
565 #endif
566 /*/// #### nk_init_fixed
567 /// Initializes a `nk_context` struct from single fixed size memory block
568 /// Should be used if you want complete control over nuklear's memory management.
569 /// Especially recommended for system with little memory or systems with virtual memory.
570 /// For the later case you can just allocate for example 16MB of virtual memory
571 /// and only the required amount of memory will actually be committed.
572 ///
573 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
574 /// int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
575 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
576 ///
577 /// !!! Warning
578 /// make sure the passed memory block is aligned correctly for `nk_draw_commands`.
579 ///
580 /// Parameter | Description
581 /// ------------|--------------------------------------------------------------
582 /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
583 /// __memory__ | Must point to a previously allocated memory block
584 /// __size__ | Must contain the total size of __memory__
585 /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
586 ///
587 /// Returns either `false(0)` on failure or `true(1)` on success.
588 */
589 NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
590 /*/// #### nk_init
591 /// Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
592 /// memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
593 /// interface to nuklear. Can be useful for cases like monitoring memory consumption.
594 ///
595 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
596 /// int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
597 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
598 ///
599 /// Parameter | Description
600 /// ------------|---------------------------------------------------------------
601 /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
602 /// __alloc__ | Must point to a previously allocated memory allocator
603 /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
604 ///
605 /// Returns either `false(0)` on failure or `true(1)` on success.
606 */
607 NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
608 /*/// #### nk_init_custom
609 /// Initializes a `nk_context` struct from two different either fixed or growing
610 /// buffers. The first buffer is for allocating draw commands while the second buffer is
611 /// used for allocating windows, panels and state tables.
612 ///
613 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
614 /// int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
615 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616 ///
617 /// Parameter | Description
618 /// ------------|---------------------------------------------------------------
619 /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
620 /// __cmds__ | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
621 /// __pool__ | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
622 /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
623 ///
624 /// Returns either `false(0)` on failure or `true(1)` on success.
625 */
626 NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
627 /*/// #### nk_clear
628 /// Resets the context state at the end of the frame. This includes mostly
629 /// garbage collector tasks like removing windows or table not called and therefore
630 /// used anymore.
631 ///
632 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
633 /// void nk_clear(struct nk_context *ctx);
634 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
635 ///
636 /// Parameter | Description
637 /// ------------|-----------------------------------------------------------
638 /// __ctx__ | Must point to a previously initialized `nk_context` struct
639 */
640 NK_API void nk_clear(struct nk_context*);
641 /*/// #### nk_free
642 /// Frees all memory allocated by nuklear. Not needed if context was
643 /// initialized with `nk_init_fixed`.
644 ///
645 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
646 /// void nk_free(struct nk_context *ctx);
647 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
648 ///
649 /// Parameter | Description
650 /// ------------|-----------------------------------------------------------
651 /// __ctx__ | Must point to a previously initialized `nk_context` struct
652 */
653 NK_API void nk_free(struct nk_context*);
654 #ifdef NK_INCLUDE_COMMAND_USERDATA
655 /*/// #### nk_set_user_data
656 /// Sets the currently passed userdata passed down into each draw command.
657 ///
658 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
659 /// void nk_set_user_data(struct nk_context *ctx, nk_handle data);
660 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
661 ///
662 /// Parameter | Description
663 /// ------------|--------------------------------------------------------------
664 /// __ctx__ | Must point to a previously initialized `nk_context` struct
665 /// __data__ | Handle with either pointer or index to be passed into every draw commands
666 */
667 NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
668 #endif
669 /* =============================================================================
670 *
671 * INPUT
672 *
673 * =============================================================================*/
674 /*/// ### Input
675 /// The input API is responsible for holding the current input state composed of
676 /// mouse, key and text input states.
677 /// It is worth noting that no direct OS or window handling is done in nuklear.
678 /// Instead all input state has to be provided by platform specific code. This on one hand
679 /// expects more work from the user and complicates usage but on the other hand
680 /// provides simple abstraction over a big number of platforms, libraries and other
681 /// already provided functionality.
682 ///
683 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
684 /// nk_input_begin(&ctx);
685 /// while (GetEvent(&evt)) {
686 /// if (evt.type == MOUSE_MOVE)
687 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
688 /// else if (evt.type == [...]) {
689 /// // [...]
690 /// }
691 /// } nk_input_end(&ctx);
692 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
693 ///
694 /// #### Usage
695 /// Input state needs to be provided to nuklear by first calling `nk_input_begin`
696 /// which resets internal state like delta mouse position and button transistions.
697 /// After `nk_input_begin` all current input state needs to be provided. This includes
698 /// mouse motion, button and key pressed and released, text input and scrolling.
699 /// Both event- or state-based input handling are supported by this API
700 /// and should work without problems. Finally after all input state has been
701 /// mirrored `nk_input_end` needs to be called to finish input process.
702 ///
703 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
704 /// struct nk_context ctx;
705 /// nk_init_xxx(&ctx, ...);
706 /// while (1) {
707 /// Event evt;
708 /// nk_input_begin(&ctx);
709 /// while (GetEvent(&evt)) {
710 /// if (evt.type == MOUSE_MOVE)
711 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
712 /// else if (evt.type == [...]) {
713 /// // [...]
714 /// }
715 /// }
716 /// nk_input_end(&ctx);
717 /// // [...]
718 /// nk_clear(&ctx);
719 /// } nk_free(&ctx);
720 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
721 ///
722 /// #### Reference
723 /// Function | Description
724 /// --------------------|-------------------------------------------------------
725 /// __nk_input_begin__ | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
726 /// __nk_input_motion__ | Mirrors mouse cursor position
727 /// __nk_input_key__ | Mirrors key state with either pressed or released
728 /// __nk_input_button__ | Mirrors mouse button state with either pressed or released
729 /// __nk_input_scroll__ | Mirrors mouse scroll values
730 /// __nk_input_char__ | Adds a single ASCII text character into an internal text buffer
731 /// __nk_input_glyph__ | Adds a single multi-byte UTF-8 character into an internal text buffer
732 /// __nk_input_unicode__| Adds a single unicode rune into an internal text buffer
733 /// __nk_input_end__ | Ends the input mirroring process by calculating state changes. Don't call any `nk_input_xxx` function referenced above after this call
734 */
735 enum nk_keys {
736 NK_KEY_NONE,
737 NK_KEY_SHIFT,
738 NK_KEY_CTRL,
739 NK_KEY_DEL,
740 NK_KEY_ENTER,
741 NK_KEY_TAB,
742 NK_KEY_BACKSPACE,
743 NK_KEY_COPY,
744 NK_KEY_CUT,
745 NK_KEY_PASTE,
746 NK_KEY_UP,
747 NK_KEY_DOWN,
748 NK_KEY_LEFT,
749 NK_KEY_RIGHT,
750 /* Shortcuts: text field */
751 NK_KEY_TEXT_INSERT_MODE,
752 NK_KEY_TEXT_REPLACE_MODE,
753 NK_KEY_TEXT_RESET_MODE,
754 NK_KEY_TEXT_LINE_START,
755 NK_KEY_TEXT_LINE_END,
756 NK_KEY_TEXT_START,
757 NK_KEY_TEXT_END,
758 NK_KEY_TEXT_UNDO,
759 NK_KEY_TEXT_REDO,
760 NK_KEY_TEXT_SELECT_ALL,
761 NK_KEY_TEXT_WORD_LEFT,
762 NK_KEY_TEXT_WORD_RIGHT,
763 /* Shortcuts: scrollbar */
764 NK_KEY_SCROLL_START,
765 NK_KEY_SCROLL_END,
766 NK_KEY_SCROLL_DOWN,
767 NK_KEY_SCROLL_UP,
768 NK_KEY_MAX
769 };
770 enum nk_buttons {
771 NK_BUTTON_LEFT,
772 NK_BUTTON_MIDDLE,
773 NK_BUTTON_RIGHT,
774 NK_BUTTON_DOUBLE,
775 NK_BUTTON_MAX
776 };
777 /*/// #### nk_input_begin
778 /// Begins the input mirroring process by resetting text, scroll
779 /// mouse, previous mouse position and movement as well as key state transitions,
780 ///
781 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
782 /// void nk_input_begin(struct nk_context*);
783 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
784 ///
785 /// Parameter | Description
786 /// ------------|-----------------------------------------------------------
787 /// __ctx__ | Must point to a previously initialized `nk_context` struct
788 */
789 NK_API void nk_input_begin(struct nk_context*);
790 /*/// #### nk_input_motion
791 /// Mirrors current mouse position to nuklear
792 ///
793 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
794 /// void nk_input_motion(struct nk_context *ctx, int x, int y);
795 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
796 ///
797 /// Parameter | Description
798 /// ------------|-----------------------------------------------------------
799 /// __ctx__ | Must point to a previously initialized `nk_context` struct
800 /// __x__ | Must hold an integer describing the current mouse cursor x-position
801 /// __y__ | Must hold an integer describing the current mouse cursor y-position
802 */
803 NK_API void nk_input_motion(struct nk_context*, int x, int y);
804 /*/// #### nk_input_key
805 /// Mirrors the state of a specific key to nuklear
806 ///
807 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
808 /// void nk_input_key(struct nk_context*, enum nk_keys key, int down);
809 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
810 ///
811 /// Parameter | Description
812 /// ------------|-----------------------------------------------------------
813 /// __ctx__ | Must point to a previously initialized `nk_context` struct
814 /// __key__ | Must be any value specified in enum `nk_keys` that needs to be mirrored
815 /// __down__ | Must be 0 for key is up and 1 for key is down
816 */
817 NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
818 /*/// #### nk_input_button
819 /// Mirrors the state of a specific mouse button to nuklear
820 ///
821 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
822 /// void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, int down);
823 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
824 ///
825 /// Parameter | Description
826 /// ------------|-----------------------------------------------------------
827 /// __ctx__ | Must point to a previously initialized `nk_context` struct
828 /// __btn__ | Must be any value specified in enum `nk_buttons` that needs to be mirrored
829 /// __x__ | Must contain an integer describing mouse cursor x-position on click up/down
830 /// __y__ | Must contain an integer describing mouse cursor y-position on click up/down
831 /// __down__ | Must be 0 for key is up and 1 for key is down
832 */
833 NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
834 /*/// #### nk_input_scroll
835 /// Copies the last mouse scroll value to nuklear. Is generally
836 /// a scroll value. So does not have to come from mouse and could also originate
837 /// TODO finish this sentence
838 ///
839 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
840 /// void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
841 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
842 ///
843 /// Parameter | Description
844 /// ------------|-----------------------------------------------------------
845 /// __ctx__ | Must point to a previously initialized `nk_context` struct
846 /// __val__ | vector with both X- as well as Y-scroll value
847 */
848 NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
849 /*/// #### nk_input_char
850 /// Copies a single ASCII character into an internal text buffer
851 /// This is basically a helper function to quickly push ASCII characters into
852 /// nuklear.
853 ///
854 /// !!! Note
855 /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
856 ///
857 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
858 /// void nk_input_char(struct nk_context *ctx, char c);
859 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
860 ///
861 /// Parameter | Description
862 /// ------------|-----------------------------------------------------------
863 /// __ctx__ | Must point to a previously initialized `nk_context` struct
864 /// __c__ | Must be a single ASCII character preferable one that can be printed
865 */
866 NK_API void nk_input_char(struct nk_context*, char);
867 /*/// #### nk_input_glyph
868 /// Converts an encoded unicode rune into UTF-8 and copies the result into an
869 /// internal text buffer.
870 ///
871 /// !!! Note
872 /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
873 ///
874 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
875 /// void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
876 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
877 ///
878 /// Parameter | Description
879 /// ------------|-----------------------------------------------------------
880 /// __ctx__ | Must point to a previously initialized `nk_context` struct
881 /// __g__ | UTF-32 unicode codepoint
882 */
883 NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
884 /*/// #### nk_input_unicode
885 /// Converts a unicode rune into UTF-8 and copies the result
886 /// into an internal text buffer.
887 /// !!! Note
888 /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
889 ///
890 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
891 /// void nk_input_unicode(struct nk_context*, nk_rune rune);
892 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
893 ///
894 /// Parameter | Description
895 /// ------------|-----------------------------------------------------------
896 /// __ctx__ | Must point to a previously initialized `nk_context` struct
897 /// __rune__ | UTF-32 unicode codepoint
898 */
899 NK_API void nk_input_unicode(struct nk_context*, nk_rune);
900 /*/// #### nk_input_end
901 /// End the input mirroring process by resetting mouse grabbing
902 /// state to ensure the mouse cursor is not grabbed indefinitely.
903 ///
904 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
905 /// void nk_input_end(struct nk_context *ctx);
906 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
907 ///
908 /// Parameter | Description
909 /// ------------|-----------------------------------------------------------
910 /// __ctx__ | Must point to a previously initialized `nk_context` struct
911 */
912 NK_API void nk_input_end(struct nk_context*);
913 /* =============================================================================
914 *
915 * DRAWING
916 *
917 * =============================================================================*/
918 /*/// ### Drawing
919 /// This library was designed to be render backend agnostic so it does
920 /// not draw anything to screen directly. Instead all drawn shapes, widgets
921 /// are made of, are buffered into memory and make up a command queue.
922 /// Each frame therefore fills the command buffer with draw commands
923 /// that then need to be executed by the user and his own render backend.
924 /// After that the command buffer needs to be cleared and a new frame can be
925 /// started. It is probably important to note that the command buffer is the main
926 /// drawing API and the optional vertex buffer API only takes this format and
927 /// converts it into a hardware accessible format.
928 ///
929 /// #### Usage
930 /// To draw all draw commands accumulated over a frame you need your own render
931 /// backend able to draw a number of 2D primitives. This includes at least
932 /// filled and stroked rectangles, circles, text, lines, triangles and scissors.
933 /// As soon as this criterion is met you can iterate over each draw command
934 /// and execute each draw command in a interpreter like fashion:
935 ///
936 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
937 /// const struct nk_command *cmd = 0;
938 /// nk_foreach(cmd, &ctx) {
939 /// switch (cmd->type) {
940 /// case NK_COMMAND_LINE:
941 /// your_draw_line_function(...)
942 /// break;
943 /// case NK_COMMAND_RECT
944 /// your_draw_rect_function(...)
945 /// break;
946 /// case //...:
947 /// //[...]
948 /// }
949 /// }
950 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
951 ///
952 /// In program flow context draw commands need to be executed after input has been
953 /// gathered and the complete UI with windows and their contained widgets have
954 /// been executed and before calling `nk_clear` which frees all previously
955 /// allocated draw commands.
956 ///
957 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
958 /// struct nk_context ctx;
959 /// nk_init_xxx(&ctx, ...);
960 /// while (1) {
961 /// Event evt;
962 /// nk_input_begin(&ctx);
963 /// while (GetEvent(&evt)) {
964 /// if (evt.type == MOUSE_MOVE)
965 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
966 /// else if (evt.type == [...]) {
967 /// [...]
968 /// }
969 /// }
970 /// nk_input_end(&ctx);
971 /// //
972 /// // [...]
973 /// //
974 /// const struct nk_command *cmd = 0;
975 /// nk_foreach(cmd, &ctx) {
976 /// switch (cmd->type) {
977 /// case NK_COMMAND_LINE:
978 /// your_draw_line_function(...)
979 /// break;
980 /// case NK_COMMAND_RECT
981 /// your_draw_rect_function(...)
982 /// break;
983 /// case ...:
984 /// // [...]
985 /// }
986 /// nk_clear(&ctx);
987 /// }
988 /// nk_free(&ctx);
989 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
990 ///
991 /// You probably noticed that you have to draw all of the UI each frame which is
992 /// quite wasteful. While the actual UI updating loop is quite fast rendering
993 /// without actually needing it is not. So there are multiple things you could do.
994 ///
995 /// First is only update on input. This of course is only an option if your
996 /// application only depends on the UI and does not require any outside calculations.
997 /// If you actually only update on input make sure to update the UI two times each
998 /// frame and call `nk_clear` directly after the first pass and only draw in
999 /// the second pass. In addition it is recommended to also add additional timers
1000 /// to make sure the UI is not drawn more than a fixed number of frames per second.
1001 ///
1002 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1003 /// struct nk_context ctx;
1004 /// nk_init_xxx(&ctx, ...);
1005 /// while (1) {
1006 /// // [...wait for input ]
1007 /// // [...do two UI passes ...]
1008 /// do_ui(...)
1009 /// nk_clear(&ctx);
1010 /// do_ui(...)
1011 /// //
1012 /// // draw
1013 /// const struct nk_command *cmd = 0;
1014 /// nk_foreach(cmd, &ctx) {
1015 /// switch (cmd->type) {
1016 /// case NK_COMMAND_LINE:
1017 /// your_draw_line_function(...)
1018 /// break;
1019 /// case NK_COMMAND_RECT
1020 /// your_draw_rect_function(...)
1021 /// break;
1022 /// case ...:
1023 /// //[...]
1024 /// }
1025 /// nk_clear(&ctx);
1026 /// }
1027 /// nk_free(&ctx);
1028 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1029 ///
1030 /// The second probably more applicable trick is to only draw if anything changed.
1031 /// It is not really useful for applications with continuous draw loop but
1032 /// quite useful for desktop applications. To actually get nuklear to only
1033 /// draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
1034 /// allocate a memory buffer that will store each unique drawing output.
1035 /// After each frame you compare the draw command memory inside the library
1036 /// with your allocated buffer by memcmp. If memcmp detects differences
1037 /// you have to copy the command buffer into the allocated buffer
1038 /// and then draw like usual (this example uses fixed memory but you could
1039 /// use dynamically allocated memory).
1040 ///
1041 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1042 /// //[... other defines ...]
1043 /// #define NK_ZERO_COMMAND_MEMORY
1044 /// #include "nuklear.h"
1045 /// //
1046 /// // setup context
1047 /// struct nk_context ctx;
1048 /// void *last = calloc(1,64*1024);
1049 /// void *buf = calloc(1,64*1024);
1050 /// nk_init_fixed(&ctx, buf, 64*1024);
1051 /// //
1052 /// // loop
1053 /// while (1) {
1054 /// // [...input...]
1055 /// // [...ui...]
1056 /// void *cmds = nk_buffer_memory(&ctx.memory);
1057 /// if (memcmp(cmds, last, ctx.memory.allocated)) {
1058 /// memcpy(last,cmds,ctx.memory.allocated);
1059 /// const struct nk_command *cmd = 0;
1060 /// nk_foreach(cmd, &ctx) {
1061 /// switch (cmd->type) {
1062 /// case NK_COMMAND_LINE:
1063 /// your_draw_line_function(...)
1064 /// break;
1065 /// case NK_COMMAND_RECT
1066 /// your_draw_rect_function(...)
1067 /// break;
1068 /// case ...:
1069 /// // [...]
1070 /// }
1071 /// }
1072 /// }
1073 /// nk_clear(&ctx);
1074 /// }
1075 /// nk_free(&ctx);
1076 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1077 ///
1078 /// Finally while using draw commands makes sense for higher abstracted platforms like
1079 /// X11 and Win32 or drawing libraries it is often desirable to use graphics
1080 /// hardware directly. Therefore it is possible to just define
1081 /// `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
1082 /// To access the vertex output you first have to convert all draw commands into
1083 /// vertexes by calling `nk_convert` which takes in your preferred vertex format.
1084 /// After successfully converting all draw commands just iterate over and execute all
1085 /// vertex draw commands:
1086 ///
1087 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1088 /// // fill configuration
1089 /// struct your_vertex
1090 /// {
1091 /// float pos[2]; // important to keep it to 2 floats
1092 /// float uv[2];
1093 /// unsigned char col[4];
1094 /// };
1095 /// struct nk_convert_config cfg = {};
1096 /// static const struct nk_draw_vertex_layout_element vertex_layout[] = {
1097 /// {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
1098 /// {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
1099 /// {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
1100 /// {NK_VERTEX_LAYOUT_END}
1101 /// };
1102 /// cfg.shape_AA = NK_ANTI_ALIASING_ON;
1103 /// cfg.line_AA = NK_ANTI_ALIASING_ON;
1104 /// cfg.vertex_layout = vertex_layout;
1105 /// cfg.vertex_size = sizeof(struct your_vertex);
1106 /// cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
1107 /// cfg.circle_segment_count = 22;
1108 /// cfg.curve_segment_count = 22;
1109 /// cfg.arc_segment_count = 22;
1110 /// cfg.global_alpha = 1.0f;
1111 /// cfg.null = dev->null;
1112 /// //
1113 /// // setup buffers and convert
1114 /// struct nk_buffer cmds, verts, idx;
1115 /// nk_buffer_init_default(&cmds);
1116 /// nk_buffer_init_default(&verts);
1117 /// nk_buffer_init_default(&idx);
1118 /// nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
1119 /// //
1120 /// // draw
1121 /// nk_draw_foreach(cmd, &ctx, &cmds) {
1122 /// if (!cmd->elem_count) continue;
1123 /// //[...]
1124 /// }
1125 /// nk_buffer_free(&cms);
1126 /// nk_buffer_free(&verts);
1127 /// nk_buffer_free(&idx);
1128 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1129 ///
1130 /// #### Reference
1131 /// Function | Description
1132 /// --------------------|-------------------------------------------------------
1133 /// __nk__begin__ | Returns the first draw command in the context draw command list to be drawn
1134 /// __nk__next__ | Increments the draw command iterator to the next command inside the context draw command list
1135 /// __nk_foreach__ | Iterates over each draw command inside the context draw command list
1136 /// __nk_convert__ | Converts from the abstract draw commands list into a hardware accessible vertex format
1137 /// __nk_draw_begin__ | Returns the first vertex command in the context vertex draw list to be executed
1138 /// __nk__draw_next__ | Increments the vertex command iterator to the next command inside the context vertex command list
1139 /// __nk__draw_end__ | Returns the end of the vertex draw list
1140 /// __nk_draw_foreach__ | Iterates over each vertex draw command inside the vertex draw list
1141 */
1142 enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
1143 enum nk_convert_result {
1144 NK_CONVERT_SUCCESS = 0,
1145 NK_CONVERT_INVALID_PARAM = 1,
1146 NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
1147 NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
1148 NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
1149 };
1150 struct nk_draw_null_texture {
1151 nk_handle texture; /* texture handle to a texture with a white pixel */
1152 struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
1153 };
1154 struct nk_convert_config {
1155 float global_alpha; /* global alpha value */
1156 enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
1157 enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
1158 unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
1159 unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
1160 unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
1161 struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
1162 const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
1163 nk_size vertex_size; /* sizeof one vertex for vertex packing */
1164 nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
1165 };
1166 /*/// #### nk__begin
1167 /// Returns a draw command list iterator to iterate all draw
1168 /// commands accumulated over one frame.
1169 ///
1170 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1171 /// const struct nk_command* nk__begin(struct nk_context*);
1172 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1173 ///
1174 /// Parameter | Description
1175 /// ------------|-----------------------------------------------------------
1176 /// __ctx__ | must point to an previously initialized `nk_context` struct at the end of a frame
1177 ///
1178 /// Returns draw command pointer pointing to the first command inside the draw command list
1179 */
1180 NK_API const struct nk_command* nk__begin(struct nk_context*);
1181 /*/// #### nk__next
1182 /// Returns draw command pointer pointing to the next command inside the draw command list
1183 ///
1184 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1185 /// const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
1186 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1187 ///
1188 /// Parameter | Description
1189 /// ------------|-----------------------------------------------------------
1190 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1191 /// __cmd__ | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
1192 ///
1193 /// Returns draw command pointer pointing to the next command inside the draw command list
1194 */
1195 NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
1196 /*/// #### nk_foreach
1197 /// Iterates over each draw command inside the context draw command list
1198 ///
1199 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1200 /// #define nk_foreach(c, ctx)
1201 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1202 ///
1203 /// Parameter | Description
1204 /// ------------|-----------------------------------------------------------
1205 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1206 /// __cmd__ | Command pointer initialized to NULL
1207 ///
1208 /// Iterates over each draw command inside the context draw command list
1209 */
1210 #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
1211 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
1212 /*/// #### nk_convert
1213 /// Converts all internal draw commands into vertex draw commands and fills
1214 /// three buffers with vertexes, vertex draw commands and vertex indices. The vertex format
1215 /// as well as some other configuration values have to be configured by filling out a
1216 /// `nk_convert_config` struct.
1217 ///
1218 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1219 /// nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
1220 /// struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
1221 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1222 ///
1223 /// Parameter | Description
1224 /// ------------|-----------------------------------------------------------
1225 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1226 /// __cmds__ | Must point to a previously initialized buffer to hold converted vertex draw commands
1227 /// __vertices__| Must point to a previously initialized buffer to hold all produced vertices
1228 /// __elements__| Must point to a previously initialized buffer to hold all produced vertex indices
1229 /// __config__ | Must point to a filled out `nk_config` struct to configure the conversion process
1230 ///
1231 /// Returns one of enum nk_convert_result error codes
1232 ///
1233 /// Parameter | Description
1234 /// --------------------------------|-----------------------------------------------------------
1235 /// NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
1236 /// NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
1237 /// NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
1238 /// NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
1239 /// NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indicies is full or failed to allocate more memory
1240 */
1241 NK_API nk_flags nk_convert(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
1242 /*/// #### nk__draw_begin
1243 /// Returns a draw vertex command buffer iterator to iterate over the vertex draw command buffer
1244 ///
1245 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1246 /// const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
1247 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1248 ///
1249 /// Parameter | Description
1250 /// ------------|-----------------------------------------------------------
1251 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1252 /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
1253 ///
1254 /// Returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
1255 */
1256 NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
1257 /*/// #### nk__draw_end
1258 /// Returns the vertex draw command at the end of the vertex draw command buffer
1259 ///
1260 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1261 /// const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
1262 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1263 ///
1264 /// Parameter | Description
1265 /// ------------|-----------------------------------------------------------
1266 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1267 /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
1268 ///
1269 /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
1270 */
1271 NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
1272 /*/// #### nk__draw_next
1273 /// Increments the vertex draw command buffer iterator
1274 ///
1275 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1276 /// const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
1277 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1278 ///
1279 /// Parameter | Description
1280 /// ------------|-----------------------------------------------------------
1281 /// __cmd__ | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
1282 /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
1283 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1284 ///
1285 /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
1286 */
1287 NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
1288 /*/// #### nk_draw_foreach
1289 /// Iterates over each vertex draw command inside a vertex draw command buffer
1290 ///
1291 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1292 /// #define nk_draw_foreach(cmd,ctx, b)
1293 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1294 ///
1295 /// Parameter | Description
1296 /// ------------|-----------------------------------------------------------
1297 /// __cmd__ | `nk_draw_command`iterator set to NULL
1298 /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
1299 /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
1300 */
1301 #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
1302 #endif
1303 /* =============================================================================
1304 *
1305 * WINDOW
1306 *
1307 * =============================================================================
1308 /// ### Window
1309 /// Windows are the main persistent state used inside nuklear and are life time
1310 /// controlled by simply "retouching" (i.e. calling) each window each frame.
1311 /// All widgets inside nuklear can only be added inside the function pair `nk_begin_xxx`
1312 /// and `nk_end`. Calling any widgets outside these two functions will result in an
1313 /// assert in debug or no state change in release mode.<br /><br />
1314 ///
1315 /// Each window holds frame persistent state like position, size, flags, state tables,
1316 /// and some garbage collected internal persistent widget state. Each window
1317 /// is linked into a window stack list which determines the drawing and overlapping
1318 /// order. The topmost window thereby is the currently active window.<br /><br />
1319 ///
1320 /// To change window position inside the stack occurs either automatically by
1321 /// user input by being clicked on or programmatically by calling `nk_window_focus`.
1322 /// Windows by default are visible unless explicitly being defined with flag
1323 /// `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
1324 /// `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
1325 /// `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
1326 ///
1327 /// #### Usage
1328 /// To create and keep a window you have to call one of the two `nk_begin_xxx`
1329 /// functions to start window declarations and `nk_end` at the end. Furthermore it
1330 /// is recommended to check the return value of `nk_begin_xxx` and only process
1331 /// widgets inside the window if the value is not 0. Either way you have to call
1332 /// `nk_end` at the end of window declarations. Furthermore, do not attempt to
1333 /// nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
1334 /// in a segmentation fault.
1335 ///
1336 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1337 /// if (nk_begin_xxx(...) {
1338 /// // [... widgets ...]
1339 /// }
1340 /// nk_end(ctx);
1341 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1342 ///
1343 /// In the grand concept window and widget declarations need to occur after input
1344 /// handling and before drawing to screen. Not doing so can result in higher
1345 /// latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
1346 /// is called at the end of the frame. While nuklear's default platform backends
1347 /// already call `nk_clear` for you if you write your own backend not calling
1348 /// `nk_clear` can cause asserts or even worse undefined behavior.
1349 ///
1350 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1351 /// struct nk_context ctx;
1352 /// nk_init_xxx(&ctx, ...);
1353 /// while (1) {
1354 /// Event evt;
1355 /// nk_input_begin(&ctx);
1356 /// while (GetEvent(&evt)) {
1357 /// if (evt.type == MOUSE_MOVE)
1358 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
1359 /// else if (evt.type == [...]) {
1360 /// nk_input_xxx(...);
1361 /// }
1362 /// }
1363 /// nk_input_end(&ctx);
1364 ///
1365 /// if (nk_begin_xxx(...) {
1366 /// //[...]
1367 /// }
1368 /// nk_end(ctx);
1369 ///
1370 /// const struct nk_command *cmd = 0;
1371 /// nk_foreach(cmd, &ctx) {
1372 /// case NK_COMMAND_LINE:
1373 /// your_draw_line_function(...)
1374 /// break;
1375 /// case NK_COMMAND_RECT
1376 /// your_draw_rect_function(...)
1377 /// break;
1378 /// case //...:
1379 /// //[...]
1380 /// }
1381 /// nk_clear(&ctx);
1382 /// }
1383 /// nk_free(&ctx);
1384 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1385 ///
1386 /// #### Reference
1387 /// Function | Description
1388 /// ------------------------------------|----------------------------------------
1389 /// nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
1390 /// nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
1391 /// nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
1392 //
1393 /// nk_window_find | Finds and returns the window with give name
1394 /// nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
1395 /// nk_window_get_position | Returns the position of the currently processed window
1396 /// nk_window_get_size | Returns the size with width and height of the currently processed window
1397 /// nk_window_get_width | Returns the width of the currently processed window
1398 /// nk_window_get_height | Returns the height of the currently processed window
1399 /// nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
1400 /// nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
1401 /// nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
1402 /// nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
1403 /// nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
1404 /// nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
1405 /// nk_window_get_scroll | Gets the scroll offset of the current window
1406 /// nk_window_has_focus | Returns if the currently processed window is currently active
1407 /// nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
1408 /// nk_window_is_closed | Returns if the currently processed window was closed
1409 /// nk_window_is_hidden | Returns if the currently processed window was hidden
1410 /// nk_window_is_active | Same as nk_window_has_focus for some reason
1411 /// nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
1412 /// nk_window_is_any_hovered | Return if any window currently hovered
1413 /// nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
1414 //
1415 /// nk_window_set_bounds | Updates position and size of the currently processed window
1416 /// nk_window_set_position | Updates position of the currently process window
1417 /// nk_window_set_size | Updates the size of the currently processed window
1418 /// nk_window_set_focus | Set the currently processed window as active window
1419 /// nk_window_set_scroll | Sets the scroll offset of the current window
1420 //
1421 /// nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
1422 /// nk_window_collapse | Collapses the window with given window name
1423 /// nk_window_collapse_if | Collapses the window with given window name if the given condition was met
1424 /// nk_window_show | Hides a visible or reshows a hidden window
1425 /// nk_window_show_if | Hides/shows a window depending on condition
1426 */
1427 /*
1428 /// #### nk_panel_flags
1429 /// Flag | Description
1430 /// ----------------------------|----------------------------------------
1431 /// NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
1432 /// NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
1433 /// NK_WINDOW_SCALABLE | The scalable flag indicates that a window can be scaled by user input by dragging a scaler icon at the button of the window
1434 /// NK_WINDOW_CLOSABLE | Adds a closable icon into the header
1435 /// NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
1436 /// NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
1437 /// NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
1438 /// NK_WINDOW_SCROLL_AUTO_HIDE | Automatically hides the window scrollbar if no user interaction: also requires delta time in `nk_context` to be set each frame
1439 /// NK_WINDOW_BACKGROUND | Always keep window in the background
1440 /// NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-bottom corner instead right-bottom
1441 /// NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
1442 ///
1443 /// #### nk_collapse_states
1444 /// State | Description
1445 /// ----------------|-----------------------------------------------------------
1446 /// __NK_MINIMIZED__| UI section is collased and not visibile until maximized
1447 /// __NK_MAXIMIZED__| UI section is extended and visibile until minimized
1448 /// <br /><br />
1449 */
1450 enum nk_panel_flags {
1451 NK_WINDOW_BORDER = NK_FLAG(0),
1452 NK_WINDOW_MOVABLE = NK_FLAG(1),
1453 NK_WINDOW_SCALABLE = NK_FLAG(2),
1454 NK_WINDOW_CLOSABLE = NK_FLAG(3),
1455 NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
1456 NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
1457 NK_WINDOW_TITLE = NK_FLAG(6),
1458 NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
1459 NK_WINDOW_BACKGROUND = NK_FLAG(8),
1460 NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
1461 NK_WINDOW_NO_INPUT = NK_FLAG(10)
1462 };
1463 /*/// #### nk_begin
1464 /// Starts a new window; needs to be called every frame for every
1465 /// window (unless hidden) or otherwise the window gets removed
1466 ///
1467 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1468 /// int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
1469 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1470 ///
1471 /// Parameter | Description
1472 /// ------------|-----------------------------------------------------------
1473 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1474 /// __title__ | Window title and identifier. Needs to be persistent over frames to identify the window
1475 /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
1476 /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
1477 ///
1478 /// Returns `true(1)` if the window can be filled up with widgets from this point
1479 /// until `nk_end` or `false(0)` otherwise for example if minimized
1480 */
1481 NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
1482 /*/// #### nk_begin_titled
1483 /// Extended window start with separated title and identifier to allow multiple
1484 /// windows with same title but not name
1485 ///
1486 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1487 /// int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
1488 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1489 ///
1490 /// Parameter | Description
1491 /// ------------|-----------------------------------------------------------
1492 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1493 /// __name__ | Window identifier. Needs to be persistent over frames to identify the window
1494 /// __title__ | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
1495 /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
1496 /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
1497 ///
1498 /// Returns `true(1)` if the window can be filled up with widgets from this point
1499 /// until `nk_end` or `false(0)` otherwise for example if minimized
1500 */
1501 NK_API int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
1502 /*/// #### nk_end
1503 /// Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
1504 /// All widget calls after this functions will result in asserts or no state changes
1505 ///
1506 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1507 /// void nk_end(struct nk_context *ctx);
1508 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1509 ///
1510 /// Parameter | Description
1511 /// ------------|-----------------------------------------------------------
1512 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1513 */
1514 NK_API void nk_end(struct nk_context *ctx);
1515 /*/// #### nk_window_find
1516 /// Finds and returns a window from passed name
1517 ///
1518 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1519 /// struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
1520 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1521 ///
1522 /// Parameter | Description
1523 /// ------------|-----------------------------------------------------------
1524 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1525 /// __name__ | Window identifier
1526 ///
1527 /// Returns a `nk_window` struct pointing to the identified window or NULL if
1528 /// no window with the given name was found
1529 */
1530 NK_API struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
1531 /*/// #### nk_window_get_bounds
1532 /// Returns a rectangle with screen position and size of the currently processed window
1533 ///
1534 /// !!! WARNING
1535 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1536 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1537 /// struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
1538 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1539 ///
1540 /// Parameter | Description
1541 /// ------------|-----------------------------------------------------------
1542 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1543 ///
1544 /// Returns a `nk_rect` struct with window upper left window position and size
1545 */
1546 NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
1547 /*/// #### nk_window_get_position
1548 /// Returns the position of the currently processed window.
1549 ///
1550 /// !!! WARNING
1551 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1552 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1553 /// struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
1554 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1555 ///
1556 /// Parameter | Description
1557 /// ------------|-----------------------------------------------------------
1558 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1559 ///
1560 /// Returns a `nk_vec2` struct with window upper left position
1561 */
1562 NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
1563 /*/// #### nk_window_get_size
1564 /// Returns the size with width and height of the currently processed window.
1565 ///
1566 /// !!! WARNING
1567 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1568 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1569 /// struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
1570 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1571 ///
1572 /// Parameter | Description
1573 /// ------------|-----------------------------------------------------------
1574 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1575 ///
1576 /// Returns a `nk_vec2` struct with window width and height
1577 */
1578 NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
1579 /*/// #### nk_window_get_width
1580 /// Returns the width of the currently processed window.
1581 ///
1582 /// !!! WARNING
1583 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1584 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1585 /// float nk_window_get_width(const struct nk_context *ctx);
1586 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1587 ///
1588 /// Parameter | Description
1589 /// ------------|-----------------------------------------------------------
1590 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1591 ///
1592 /// Returns the current window width
1593 */
1594 NK_API float nk_window_get_width(const struct nk_context*);
1595 /*/// #### nk_window_get_height
1596 /// Returns the height of the currently processed window.
1597 ///
1598 /// !!! WARNING
1599 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1600 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1601 /// float nk_window_get_height(const struct nk_context *ctx);
1602 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1603 ///
1604 /// Parameter | Description
1605 /// ------------|-----------------------------------------------------------
1606 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1607 ///
1608 /// Returns the current window height
1609 */
1610 NK_API float nk_window_get_height(const struct nk_context*);
1611 /*/// #### nk_window_get_panel
1612 /// Returns the underlying panel which contains all processing state of the current window.
1613 ///
1614 /// !!! WARNING
1615 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1616 /// !!! WARNING
1617 /// Do not keep the returned panel pointer around, it is only valid until `nk_end`
1618 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1619 /// struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
1620 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1621 ///
1622 /// Parameter | Description
1623 /// ------------|-----------------------------------------------------------
1624 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1625 ///
1626 /// Returns a pointer to window internal `nk_panel` state.
1627 */
1628 NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
1629 /*/// #### nk_window_get_content_region
1630 /// Returns the position and size of the currently visible and non-clipped space
1631 /// inside the currently processed window.
1632 ///
1633 /// !!! WARNING
1634 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1635 ///
1636 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1637 /// struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
1638 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1639 ///
1640 /// Parameter | Description
1641 /// ------------|-----------------------------------------------------------
1642 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1643 ///
1644 /// Returns `nk_rect` struct with screen position and size (no scrollbar offset)
1645 /// of the visible space inside the current window
1646 */
1647 NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
1648 /*/// #### nk_window_get_content_region_min
1649 /// Returns the upper left position of the currently visible and non-clipped
1650 /// space inside the currently processed window.
1651 ///
1652 /// !!! WARNING
1653 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1654 ///
1655 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1656 /// struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
1657 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1658 ///
1659 /// Parameter | Description
1660 /// ------------|-----------------------------------------------------------
1661 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1662 ///
1663 /// returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
1664 /// of the visible space inside the current window
1665 */
1666 NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
1667 /*/// #### nk_window_get_content_region_max
1668 /// Returns the lower right screen position of the currently visible and
1669 /// non-clipped space inside the currently processed window.
1670 ///
1671 /// !!! WARNING
1672 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1673 ///
1674 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1675 /// struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
1676 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1677 ///
1678 /// Parameter | Description
1679 /// ------------|-----------------------------------------------------------
1680 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1681 ///
1682 /// Returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
1683 /// of the visible space inside the current window
1684 */
1685 NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
1686 /*/// #### nk_window_get_content_region_size
1687 /// Returns the size of the currently visible and non-clipped space inside the
1688 /// currently processed window
1689 ///
1690 /// !!! WARNING
1691 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1692 ///
1693 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1694 /// struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
1695 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1696 ///
1697 /// Parameter | Description
1698 /// ------------|-----------------------------------------------------------
1699 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1700 ///
1701 /// Returns `nk_vec2` struct with size the visible space inside the current window
1702 */
1703 NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
1704 /*/// #### nk_window_get_canvas
1705 /// Returns the draw command buffer. Can be used to draw custom widgets
1706 /// !!! WARNING
1707 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1708 /// !!! WARNING
1709 /// Do not keep the returned command buffer pointer around it is only valid until `nk_end`
1710 ///
1711 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1712 /// struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
1713 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1714 ///
1715 /// Parameter | Description
1716 /// ------------|-----------------------------------------------------------
1717 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1718 ///
1719 /// Returns a pointer to window internal `nk_command_buffer` struct used as
1720 /// drawing canvas. Can be used to do custom drawing.
1721 */
1722 NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
1723 /*/// #### nk_window_get_scroll
1724 /// Gets the scroll offset for the current window
1725 /// !!! WARNING
1726 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1727 ///
1728 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1729 /// void nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y);
1730 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1731 ///
1732 /// Parameter | Description
1733 /// -------------|-----------------------------------------------------------
1734 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1735 /// __offset_x__ | A pointer to the x offset output (or NULL to ignore)
1736 /// __offset_y__ | A pointer to the y offset output (or NULL to ignore)
1737 */
1738 NK_API void nk_window_get_scroll(struct nk_context*, nk_uint *offset_x, nk_uint *offset_y);
1739 /*/// #### nk_window_has_focus
1740 /// Returns if the currently processed window is currently active
1741 /// !!! WARNING
1742 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1743 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1744 /// int nk_window_has_focus(const struct nk_context *ctx);
1745 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1746 ///
1747 /// Parameter | Description
1748 /// ------------|-----------------------------------------------------------
1749 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1750 ///
1751 /// Returns `false(0)` if current window is not active or `true(1)` if it is
1752 */
1753 NK_API int nk_window_has_focus(const struct nk_context*);
1754 /*/// #### nk_window_is_hovered
1755 /// Return if the current window is being hovered
1756 /// !!! WARNING
1757 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1758 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1759 /// int nk_window_is_hovered(struct nk_context *ctx);
1760 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1761 ///
1762 /// Parameter | Description
1763 /// ------------|-----------------------------------------------------------
1764 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1765 ///
1766 /// Returns `true(1)` if current window is hovered or `false(0)` otherwise
1767 */
1768 NK_API int nk_window_is_hovered(struct nk_context*);
1769 /*/// #### nk_window_is_collapsed
1770 /// Returns if the window with given name is currently minimized/collapsed
1771 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1772 /// int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
1773 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1774 ///
1775 /// Parameter | Description
1776 /// ------------|-----------------------------------------------------------
1777 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1778 /// __name__ | Identifier of window you want to check if it is collapsed
1779 ///
1780 /// Returns `true(1)` if current window is minimized and `false(0)` if window not
1781 /// found or is not minimized
1782 */
1783 NK_API int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
1784 /*/// #### nk_window_is_closed
1785 /// Returns if the window with given name was closed by calling `nk_close`
1786 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1787 /// int nk_window_is_closed(struct nk_context *ctx, const char *name);
1788 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1789 ///
1790 /// Parameter | Description
1791 /// ------------|-----------------------------------------------------------
1792 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1793 /// __name__ | Identifier of window you want to check if it is closed
1794 ///
1795 /// Returns `true(1)` if current window was closed or `false(0)` window not found or not closed
1796 */
1797 NK_API int nk_window_is_closed(struct nk_context*, const char*);
1798 /*/// #### nk_window_is_hidden
1799 /// Returns if the window with given name is hidden
1800 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1801 /// int nk_window_is_hidden(struct nk_context *ctx, const char *name);
1802 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1803 ///
1804 /// Parameter | Description
1805 /// ------------|-----------------------------------------------------------
1806 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1807 /// __name__ | Identifier of window you want to check if it is hidden
1808 ///
1809 /// Returns `true(1)` if current window is hidden or `false(0)` window not found or visible
1810 */
1811 NK_API int nk_window_is_hidden(struct nk_context*, const char*);
1812 /*/// #### nk_window_is_active
1813 /// Same as nk_window_has_focus for some reason
1814 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1815 /// int nk_window_is_active(struct nk_context *ctx, const char *name);
1816 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1817 ///
1818 /// Parameter | Description
1819 /// ------------|-----------------------------------------------------------
1820 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1821 /// __name__ | Identifier of window you want to check if it is active
1822 ///
1823 /// Returns `true(1)` if current window is active or `false(0)` window not found or not active
1824 */
1825 NK_API int nk_window_is_active(struct nk_context*, const char*);
1826 /*/// #### nk_window_is_any_hovered
1827 /// Returns if the any window is being hovered
1828 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1829 /// int nk_window_is_any_hovered(struct nk_context*);
1830 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1831 ///
1832 /// Parameter | Description
1833 /// ------------|-----------------------------------------------------------
1834 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1835 ///
1836 /// Returns `true(1)` if any window is hovered or `false(0)` otherwise
1837 */
1838 NK_API int nk_window_is_any_hovered(struct nk_context*);
1839 /*/// #### nk_item_is_any_active
1840 /// Returns if the any window is being hovered or any widget is currently active.
1841 /// Can be used to decide if input should be processed by UI or your specific input handling.
1842 /// Example could be UI and 3D camera to move inside a 3D space.
1843 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1844 /// int nk_item_is_any_active(struct nk_context*);
1845 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1846 ///
1847 /// Parameter | Description
1848 /// ------------|-----------------------------------------------------------
1849 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1850 ///
1851 /// Returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
1852 */
1853 NK_API int nk_item_is_any_active(struct nk_context*);
1854 /*/// #### nk_window_set_bounds
1855 /// Updates position and size of window with passed in name
1856 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1857 /// void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
1858 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1859 ///
1860 /// Parameter | Description
1861 /// ------------|-----------------------------------------------------------
1862 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1863 /// __name__ | Identifier of the window to modify both position and size
1864 /// __bounds__ | Must point to a `nk_rect` struct with the new position and size
1865 */
1866 NK_API void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
1867 /*/// #### nk_window_set_position
1868 /// Updates position of window with passed name
1869 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1870 /// void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
1871 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1872 ///
1873 /// Parameter | Description
1874 /// ------------|-----------------------------------------------------------
1875 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1876 /// __name__ | Identifier of the window to modify both position
1877 /// __pos__ | Must point to a `nk_vec2` struct with the new position
1878 */
1879 NK_API void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
1880 /*/// #### nk_window_set_size
1881 /// Updates size of window with passed in name
1882 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1883 /// void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
1884 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1885 ///
1886 /// Parameter | Description
1887 /// ------------|-----------------------------------------------------------
1888 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1889 /// __name__ | Identifier of the window to modify both window size
1890 /// __size__ | Must point to a `nk_vec2` struct with new window size
1891 */
1892 NK_API void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
1893 /*/// #### nk_window_set_focus
1894 /// Sets the window with given name as active
1895 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1896 /// void nk_window_set_focus(struct nk_context*, const char *name);
1897 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1898 ///
1899 /// Parameter | Description
1900 /// ------------|-----------------------------------------------------------
1901 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1902 /// __name__ | Identifier of the window to set focus on
1903 */
1904 NK_API void nk_window_set_focus(struct nk_context*, const char *name);
1905 /*/// #### nk_window_set_scroll
1906 /// Sets the scroll offset for the current window
1907 /// !!! WARNING
1908 /// Only call this function between calls `nk_begin_xxx` and `nk_end`
1909 ///
1910 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1911 /// void nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y);
1912 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1913 ///
1914 /// Parameter | Description
1915 /// -------------|-----------------------------------------------------------
1916 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1917 /// __offset_x__ | The x offset to scroll to
1918 /// __offset_y__ | The y offset to scroll to
1919 */
1920 NK_API void nk_window_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
1921 /*/// #### nk_window_close
1922 /// Closes a window and marks it for being freed at the end of the frame
1923 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1924 /// void nk_window_close(struct nk_context *ctx, const char *name);
1925 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1926 ///
1927 /// Parameter | Description
1928 /// ------------|-----------------------------------------------------------
1929 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1930 /// __name__ | Identifier of the window to close
1931 */
1932 NK_API void nk_window_close(struct nk_context *ctx, const char *name);
1933 /*/// #### nk_window_collapse
1934 /// Updates collapse state of a window with given name
1935 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1936 /// void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
1937 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1938 ///
1939 /// Parameter | Description
1940 /// ------------|-----------------------------------------------------------
1941 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1942 /// __name__ | Identifier of the window to close
1943 /// __state__ | value out of nk_collapse_states section
1944 */
1945 NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
1946 /*/// #### nk_window_collapse_if
1947 /// Updates collapse state of a window with given name if given condition is met
1948 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1949 /// void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
1950 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1951 ///
1952 /// Parameter | Description
1953 /// ------------|-----------------------------------------------------------
1954 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1955 /// __name__ | Identifier of the window to either collapse or maximize
1956 /// __state__ | value out of nk_collapse_states section the window should be put into
1957 /// __cond__ | condition that has to be met to actually commit the collapse state change
1958 */
1959 NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
1960 /*/// #### nk_window_show
1961 /// updates visibility state of a window with given name
1962 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1963 /// void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
1964 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1965 ///
1966 /// Parameter | Description
1967 /// ------------|-----------------------------------------------------------
1968 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1969 /// __name__ | Identifier of the window to either collapse or maximize
1970 /// __state__ | state with either visible or hidden to modify the window with
1971 */
1972 NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
1973 /*/// #### nk_window_show_if
1974 /// Updates visibility state of a window with given name if a given condition is met
1975 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
1976 /// void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
1977 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1978 ///
1979 /// Parameter | Description
1980 /// ------------|-----------------------------------------------------------
1981 /// __ctx__ | Must point to an previously initialized `nk_context` struct
1982 /// __name__ | Identifier of the window to either hide or show
1983 /// __state__ | state with either visible or hidden to modify the window with
1984 /// __cond__ | condition that has to be met to actually commit the visbility state change
1985 */
1986 NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
1987 /* =============================================================================
1988 *
1989 * LAYOUT
1990 *
1991 * =============================================================================
1992 /// ### Layouting
1993 /// Layouting in general describes placing widget inside a window with position and size.
1994 /// While in this particular implementation there are five different APIs for layouting
1995 /// each with different trade offs between control and ease of use. <br /><br />
1996 ///
1997 /// All layouting methods in this library are based around the concept of a row.
1998 /// A row has a height the window content grows by and a number of columns and each
1999 /// layouting method specifies how each widget is placed inside the row.
2000 /// After a row has been allocated by calling a layouting functions and then
2001 /// filled with widgets will advance an internal pointer over the allocated row. <br /><br />
2002 ///
2003 /// To actually define a layout you just call the appropriate layouting function
2004 /// and each subsequent widget call will place the widget as specified. Important
2005 /// here is that if you define more widgets then columns defined inside the layout
2006 /// functions it will allocate the next row without you having to make another layouting <br /><br />
2007 /// call.
2008 ///
2009 /// Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
2010 /// is that you have to define the row height for each. However the row height
2011 /// often depends on the height of the font. <br /><br />
2012 ///
2013 /// To fix that internally nuklear uses a minimum row height that is set to the
2014 /// height plus padding of currently active font and overwrites the row height
2015 /// value if zero. <br /><br />
2016 ///
2017 /// If you manually want to change the minimum row height then
2018 /// use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
2019 /// reset it back to be derived from font height. <br /><br />
2020 ///
2021 /// Also if you change the font in nuklear it will automatically change the minimum
2022 /// row height for you and. This means if you change the font but still want
2023 /// a minimum row height smaller than the font you have to repush your value. <br /><br />
2024 ///
2025 /// For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
2026 /// layouting method in combination with a cassowary constraint solver (there are
2027 /// some versions on github with permissive license model) to take over all control over widget
2028 /// layouting yourself. However for quick and dirty layouting using all the other layouting
2029 /// functions should be fine.
2030 ///
2031 /// #### Usage
2032 /// 1. __nk_layout_row_dynamic__<br /><br />
2033 /// The easiest layouting function is `nk_layout_row_dynamic`. It provides each
2034 /// widgets with same horizontal space inside the row and dynamically grows
2035 /// if the owning window grows in width. So the number of columns dictates
2036 /// the size of each widget dynamically by formula:
2037 ///
2038 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2039 /// widget_width = (window_width - padding - spacing) * (1/colum_count)
2040 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2041 ///
2042 /// Just like all other layouting APIs if you define more widget than columns this
2043 /// library will allocate a new row and keep all layouting parameters previously
2044 /// defined.
2045 ///
2046 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2047 /// if (nk_begin_xxx(...) {
2048 /// // first row with height: 30 composed of two widgets
2049 /// nk_layout_row_dynamic(&ctx, 30, 2);
2050 /// nk_widget(...);
2051 /// nk_widget(...);
2052 /// //
2053 /// // second row with same parameter as defined above
2054 /// nk_widget(...);
2055 /// nk_widget(...);
2056 /// //
2057 /// // third row uses 0 for height which will use auto layouting
2058 /// nk_layout_row_dynamic(&ctx, 0, 2);
2059 /// nk_widget(...);
2060 /// nk_widget(...);
2061 /// }
2062 /// nk_end(...);
2063 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2064 ///
2065 /// 2. __nk_layout_row_static__<br /><br />
2066 /// Another easy layouting function is `nk_layout_row_static`. It provides each
2067 /// widget with same horizontal pixel width inside the row and does not grow
2068 /// if the owning window scales smaller or bigger.
2069 ///
2070 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2071 /// if (nk_begin_xxx(...) {
2072 /// // first row with height: 30 composed of two widgets with width: 80
2073 /// nk_layout_row_static(&ctx, 30, 80, 2);
2074 /// nk_widget(...);
2075 /// nk_widget(...);
2076 /// //
2077 /// // second row with same parameter as defined above
2078 /// nk_widget(...);
2079 /// nk_widget(...);
2080 /// //
2081 /// // third row uses 0 for height which will use auto layouting
2082 /// nk_layout_row_static(&ctx, 0, 80, 2);
2083 /// nk_widget(...);
2084 /// nk_widget(...);
2085 /// }
2086 /// nk_end(...);
2087 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2088 ///
2089 /// 3. __nk_layout_row_xxx__<br /><br />
2090 /// A little bit more advanced layouting API are functions `nk_layout_row_begin`,
2091 /// `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
2092 /// specify each column pixel or window ratio in a row. It supports either
2093 /// directly setting per column pixel width or widget window ratio but not
2094 /// both. Furthermore it is a immediate mode API so each value is directly
2095 /// pushed before calling a widget. Therefore the layout is not automatically
2096 /// repeating like the last two layouting functions.
2097 ///
2098 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2099 /// if (nk_begin_xxx(...) {
2100 /// // first row with height: 25 composed of two widgets with width 60 and 40
2101 /// nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
2102 /// nk_layout_row_push(ctx, 60);
2103 /// nk_widget(...);
2104 /// nk_layout_row_push(ctx, 40);
2105 /// nk_widget(...);
2106 /// nk_layout_row_end(ctx);
2107 /// //
2108 /// // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
2109 /// nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
2110 /// nk_layout_row_push(ctx, 0.25f);
2111 /// nk_widget(...);
2112 /// nk_layout_row_push(ctx, 0.75f);
2113 /// nk_widget(...);
2114 /// nk_layout_row_end(ctx);
2115 /// //
2116 /// // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
2117 /// nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
2118 /// nk_layout_row_push(ctx, 0.25f);
2119 /// nk_widget(...);
2120 /// nk_layout_row_push(ctx, 0.75f);
2121 /// nk_widget(...);
2122 /// nk_layout_row_end(ctx);
2123 /// }
2124 /// nk_end(...);
2125 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2126 ///
2127 /// 4. __nk_layout_row__<br /><br />
2128 /// The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
2129 /// functions. Instead of pushing either pixel or window ratio for every widget
2130 /// it allows to define it by array. The trade of for less control is that
2131 /// `nk_layout_row` is automatically repeating. Otherwise the behavior is the
2132 /// same.
2133 ///
2134 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2135 /// if (nk_begin_xxx(...) {
2136 /// // two rows with height: 30 composed of two widgets with width 60 and 40
2137 /// const float size[] = {60,40};
2138 /// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
2139 /// nk_widget(...);
2140 /// nk_widget(...);
2141 /// nk_widget(...);
2142 /// nk_widget(...);
2143 /// //
2144 /// // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
2145 /// const float ratio[] = {0.25, 0.75};
2146 /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
2147 /// nk_widget(...);
2148 /// nk_widget(...);
2149 /// nk_widget(...);
2150 /// nk_widget(...);
2151 /// //
2152 /// // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
2153 /// const float ratio[] = {0.25, 0.75};
2154 /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
2155 /// nk_widget(...);
2156 /// nk_widget(...);
2157 /// nk_widget(...);
2158 /// nk_widget(...);
2159 /// }
2160 /// nk_end(...);
2161 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2162 ///
2163 /// 5. __nk_layout_row_template_xxx__<br /><br />
2164 /// The most complex and second most flexible API is a simplified flexbox version without
2165 /// line wrapping and weights for dynamic widgets. It is an immediate mode API but
2166 /// unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
2167 /// before calling the templated widgets.
2168 /// The row template layout has three different per widget size specifier. The first
2169 /// one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
2170 /// They do not grow if the row grows and will always stay the same.
2171 /// The second size specifier is `nk_layout_row_template_push_variable`
2172 /// which defines a minimum widget size but it also can grow if more space is available
2173 /// not taken by other widgets.
2174 /// Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
2175 /// which are completely flexible and unlike variable widgets can even shrink
2176 /// to zero if not enough space is provided.
2177 ///
2178 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2179 /// if (nk_begin_xxx(...) {
2180 /// // two rows with height: 30 composed of three widgets
2181 /// nk_layout_row_template_begin(ctx, 30);
2182 /// nk_layout_row_template_push_dynamic(ctx);
2183 /// nk_layout_row_template_push_variable(ctx, 80);
2184 /// nk_layout_row_template_push_static(ctx, 80);
2185 /// nk_layout_row_template_end(ctx);
2186 /// //
2187 /// // first row
2188 /// nk_widget(...); // dynamic widget can go to zero if not enough space
2189 /// nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
2190 /// nk_widget(...); // static widget with fixed 80 pixel width
2191 /// //
2192 /// // second row same layout
2193 /// nk_widget(...);
2194 /// nk_widget(...);
2195 /// nk_widget(...);
2196 /// }
2197 /// nk_end(...);
2198 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2199 ///
2200 /// 6. __nk_layout_space_xxx__<br /><br />
2201 /// Finally the most flexible API directly allows you to place widgets inside the
2202 /// window. The space layout API is an immediate mode API which does not support
2203 /// row auto repeat and directly sets position and size of a widget. Position
2204 /// and size hereby can be either specified as ratio of allocated space or
2205 /// allocated space local position and pixel size. Since this API is quite
2206 /// powerful there are a number of utility functions to get the available space
2207 /// and convert between local allocated space and screen space.
2208 ///
2209 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2210 /// if (nk_begin_xxx(...) {
2211 /// // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
2212 /// nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
2213 /// nk_layout_space_push(ctx, nk_rect(0,0,150,200));
2214 /// nk_widget(...);
2215 /// nk_layout_space_push(ctx, nk_rect(200,200,100,200));
2216 /// nk_widget(...);
2217 /// nk_layout_space_end(ctx);
2218 /// //
2219 /// // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
2220 /// nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
2221 /// nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
2222 /// nk_widget(...);
2223 /// nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
2224 /// nk_widget(...);
2225 /// }
2226 /// nk_end(...);
2227 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2228 ///
2229 /// #### Reference
2230 /// Function | Description
2231 /// ----------------------------------------|------------------------------------
2232 /// nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
2233 /// nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
2234 /// nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
2235 /// nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
2236 //
2237 /// nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
2238 /// nk_layout_row_static | Current layout is divided into n same fixed sized columns
2239 /// nk_layout_row_begin | Starts a new row with given height and number of columns
2240 /// nk_layout_row_push | Pushes another column with given size or window ratio
2241 /// nk_layout_row_end | Finished previously started row
2242 /// nk_layout_row | Specifies row columns in array as either window ratio or size
2243 //
2244 /// nk_layout_row_template_begin | Begins the row template declaration
2245 /// nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
2246 /// nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
2247 /// nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
2248 /// nk_layout_row_template_end | Marks the end of the row template
2249 //
2250 /// nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
2251 /// nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
2252 /// nk_layout_space_end | Marks the end of the layouting space
2253 //
2254 /// nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
2255 /// nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
2256 /// nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
2257 /// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
2258 /// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
2259 */
2260 /*/// #### nk_layout_set_min_row_height
2261 /// Sets the currently used minimum row height.
2262 /// !!! WARNING
2263 /// The passed height needs to include both your preferred row height
2264 /// as well as padding. No internal padding is added.
2265 ///
2266 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2267 /// void nk_layout_set_min_row_height(struct nk_context*, float height);
2268 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2269 ///
2270 /// Parameter | Description
2271 /// ------------|-----------------------------------------------------------
2272 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2273 /// __height__ | New minimum row height to be used for auto generating the row height
2274 */
2275 NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
2276 /*/// #### nk_layout_reset_min_row_height
2277 /// Reset the currently used minimum row height back to `font_height + text_padding + padding`
2278 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2279 /// void nk_layout_reset_min_row_height(struct nk_context*);
2280 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2281 ///
2282 /// Parameter | Description
2283 /// ------------|-----------------------------------------------------------
2284 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2285 */
2286 NK_API void nk_layout_reset_min_row_height(struct nk_context*);
2287 /*/// #### nk_layout_widget_bounds
2288 /// Returns the width of the next row allocate by one of the layouting functions
2289 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2290 /// struct nk_rect nk_layout_widget_bounds(struct nk_context*);
2291 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2292 ///
2293 /// Parameter | Description
2294 /// ------------|-----------------------------------------------------------
2295 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2296 ///
2297 /// Return `nk_rect` with both position and size of the next row
2298 */
2299 NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
2300 /*/// #### nk_layout_ratio_from_pixel
2301 /// Utility functions to calculate window ratio from pixel size
2302 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2303 /// float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
2304 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2305 ///
2306 /// Parameter | Description
2307 /// ------------|-----------------------------------------------------------
2308 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2309 /// __pixel__ | Pixel_width to convert to window ratio
2310 ///
2311 /// Returns `nk_rect` with both position and size of the next row
2312 */
2313 NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
2314 /*/// #### nk_layout_row_dynamic
2315 /// Sets current row layout to share horizontal space
2316 /// between @cols number of widgets evenly. Once called all subsequent widget
2317 /// calls greater than @cols will allocate a new row with same layout.
2318 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2319 /// void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
2320 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2321 ///
2322 /// Parameter | Description
2323 /// ------------|-----------------------------------------------------------
2324 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2325 /// __height__ | Holds height of each widget in row or zero for auto layouting
2326 /// __columns__ | Number of widget inside row
2327 */
2328 NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
2329 /*/// #### nk_layout_row_static
2330 /// Sets current row layout to fill @cols number of widgets
2331 /// in row with same @item_width horizontal size. Once called all subsequent widget
2332 /// calls greater than @cols will allocate a new row with same layout.
2333 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2334 /// void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
2335 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2336 ///
2337 /// Parameter | Description
2338 /// ------------|-----------------------------------------------------------
2339 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2340 /// __height__ | Holds height of each widget in row or zero for auto layouting
2341 /// __width__ | Holds pixel width of each widget in the row
2342 /// __columns__ | Number of widget inside row
2343 */
2344 NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
2345 /*/// #### nk_layout_row_begin
2346 /// Starts a new dynamic or fixed row with given height and columns.
2347 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2348 /// void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
2349 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2350 ///
2351 /// Parameter | Description
2352 /// ------------|-----------------------------------------------------------
2353 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2354 /// __fmt__ | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
2355 /// __height__ | holds height of each widget in row or zero for auto layouting
2356 /// __columns__ | Number of widget inside row
2357 */
2358 NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
2359 /*/// #### nk_layout_row_push
2360 /// Specifies either window ratio or width of a single column
2361 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2362 /// void nk_layout_row_push(struct nk_context*, float value);
2363 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2364 ///
2365 /// Parameter | Description
2366 /// ------------|-----------------------------------------------------------
2367 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2368 /// __value__ | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
2369 */
2370 NK_API void nk_layout_row_push(struct nk_context*, float value);
2371 /*/// #### nk_layout_row_end
2372 /// Finished previously started row
2373 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2374 /// void nk_layout_row_end(struct nk_context*);
2375 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2376 ///
2377 /// Parameter | Description
2378 /// ------------|-----------------------------------------------------------
2379 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2380 */
2381 NK_API void nk_layout_row_end(struct nk_context*);
2382 /*/// #### nk_layout_row
2383 /// Specifies row columns in array as either window ratio or size
2384 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2385 /// void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
2386 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2387 ///
2388 /// Parameter | Description
2389 /// ------------|-----------------------------------------------------------
2390 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2391 /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
2392 /// __height__ | Holds height of each widget in row or zero for auto layouting
2393 /// __columns__ | Number of widget inside row
2394 */
2395 NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
2396 /*/// #### nk_layout_row_template_begin
2397 /// Begins the row template declaration
2398 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2399 /// void nk_layout_row_template_begin(struct nk_context*, float row_height);
2400 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2401 ///
2402 /// Parameter | Description
2403 /// ------------|-----------------------------------------------------------
2404 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2405 /// __height__ | Holds height of each widget in row or zero for auto layouting
2406 */
2407 NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
2408 /*/// #### nk_layout_row_template_push_dynamic
2409 /// Adds a dynamic column that dynamically grows and can go to zero if not enough space
2410 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2411 /// void nk_layout_row_template_push_dynamic(struct nk_context*);
2412 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2413 ///
2414 /// Parameter | Description
2415 /// ------------|-----------------------------------------------------------
2416 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2417 /// __height__ | Holds height of each widget in row or zero for auto layouting
2418 */
2419 NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
2420 /*/// #### nk_layout_row_template_push_variable
2421 /// Adds a variable column that dynamically grows but does not shrink below specified pixel width
2422 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2423 /// void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
2424 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2425 ///
2426 /// Parameter | Description
2427 /// ------------|-----------------------------------------------------------
2428 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2429 /// __width__ | Holds the minimum pixel width the next column must always be
2430 */
2431 NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
2432 /*/// #### nk_layout_row_template_push_static
2433 /// Adds a static column that does not grow and will always have the same size
2434 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2435 /// void nk_layout_row_template_push_static(struct nk_context*, float width);
2436 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2437 ///
2438 /// Parameter | Description
2439 /// ------------|-----------------------------------------------------------
2440 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2441 /// __width__ | Holds the absolute pixel width value the next column must be
2442 */
2443 NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
2444 /*/// #### nk_layout_row_template_end
2445 /// Marks the end of the row template
2446 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2447 /// void nk_layout_row_template_end(struct nk_context*);
2448 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2449 ///
2450 /// Parameter | Description
2451 /// ------------|-----------------------------------------------------------
2452 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2453 */
2454 NK_API void nk_layout_row_template_end(struct nk_context*);
2455 /*/// #### nk_layout_space_begin
2456 /// Begins a new layouting space that allows to specify each widgets position and size.
2457 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2458 /// void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
2459 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2460 ///
2461 /// Parameter | Description
2462 /// ------------|-----------------------------------------------------------
2463 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
2464 /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
2465 /// __height__ | Holds height of each widget in row or zero for auto layouting
2466 /// __columns__ | Number of widgets inside row
2467 */
2468 NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
2469 /*/// #### nk_layout_space_push
2470 /// Pushes position and size of the next widget in own coordinate space either as pixel or ratio
2471 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2472 /// void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
2473 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2474 ///
2475 /// Parameter | Description
2476 /// ------------|-----------------------------------------------------------
2477 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2478 /// __bounds__ | Position and size in laoyut space local coordinates
2479 */
2480 NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
2481 /*/// #### nk_layout_space_end
2482 /// Marks the end of the layout space
2483 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2484 /// void nk_layout_space_end(struct nk_context*);
2485 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2486 ///
2487 /// Parameter | Description
2488 /// ------------|-----------------------------------------------------------
2489 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2490 */
2491 NK_API void nk_layout_space_end(struct nk_context*);
2492 /*/// #### nk_layout_space_bounds
2493 /// Utility function to calculate total space allocated for `nk_layout_space`
2494 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2495 /// struct nk_rect nk_layout_space_bounds(struct nk_context*);
2496 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2497 ///
2498 /// Parameter | Description
2499 /// ------------|-----------------------------------------------------------
2500 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2501 ///
2502 /// Returns `nk_rect` holding the total space allocated
2503 */
2504 NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
2505 /*/// #### nk_layout_space_to_screen
2506 /// Converts vector from nk_layout_space coordinate space into screen space
2507 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2508 /// struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
2509 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2510 ///
2511 /// Parameter | Description
2512 /// ------------|-----------------------------------------------------------
2513 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2514 /// __vec__ | Position to convert from layout space into screen coordinate space
2515 ///
2516 /// Returns transformed `nk_vec2` in screen space coordinates
2517 */
2518 NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
2519 /*/// #### nk_layout_space_to_local
2520 /// Converts vector from layout space into screen space
2521 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2522 /// struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
2523 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2524 ///
2525 /// Parameter | Description
2526 /// ------------|-----------------------------------------------------------
2527 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2528 /// __vec__ | Position to convert from screen space into layout coordinate space
2529 ///
2530 /// Returns transformed `nk_vec2` in layout space coordinates
2531 */
2532 NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
2533 /*/// #### nk_layout_space_rect_to_screen
2534 /// Converts rectangle from screen space into layout space
2535 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2536 /// struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
2537 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2538 ///
2539 /// Parameter | Description
2540 /// ------------|-----------------------------------------------------------
2541 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2542 /// __bounds__ | Rectangle to convert from layout space into screen space
2543 ///
2544 /// Returns transformed `nk_rect` in screen space coordinates
2545 */
2546 NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
2547 /*/// #### nk_layout_space_rect_to_local
2548 /// Converts rectangle from layout space into screen space
2549 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2550 /// struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
2551 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2552 ///
2553 /// Parameter | Description
2554 /// ------------|-----------------------------------------------------------
2555 /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
2556 /// __bounds__ | Rectangle to convert from layout space into screen space
2557 ///
2558 /// Returns transformed `nk_rect` in layout space coordinates
2559 */
2560 NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
2561 /* =============================================================================
2562 *
2563 * GROUP
2564 *
2565 * =============================================================================
2566 /// ### Groups
2567 /// Groups are basically windows inside windows. They allow to subdivide space
2568 /// in a window to layout widgets as a group. Almost all more complex widget
2569 /// layouting requirements can be solved using groups and basic layouting
2570 /// fuctionality. Groups just like windows are identified by an unique name and
2571 /// internally keep track of scrollbar offsets by default. However additional
2572 /// versions are provided to directly manage the scrollbar.
2573 ///
2574 /// #### Usage
2575 /// To create a group you have to call one of the three `nk_group_begin_xxx`
2576 /// functions to start group declarations and `nk_group_end` at the end. Furthermore it
2577 /// is required to check the return value of `nk_group_begin_xxx` and only process
2578 /// widgets inside the window if the value is not 0.
2579 /// Nesting groups is possible and even encouraged since many layouting schemes
2580 /// can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
2581 /// to be only called if the corosponding `nk_group_begin_xxx` call does not return 0:
2582 ///
2583 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2584 /// if (nk_group_begin_xxx(ctx, ...) {
2585 /// // [... widgets ...]
2586 /// nk_group_end(ctx);
2587 /// }
2588 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2589 ///
2590 /// In the grand concept groups can be called after starting a window
2591 /// with `nk_begin_xxx` and before calling `nk_end`:
2592 ///
2593 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2594 /// struct nk_context ctx;
2595 /// nk_init_xxx(&ctx, ...);
2596 /// while (1) {
2597 /// // Input
2598 /// Event evt;
2599 /// nk_input_begin(&ctx);
2600 /// while (GetEvent(&evt)) {
2601 /// if (evt.type == MOUSE_MOVE)
2602 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
2603 /// else if (evt.type == [...]) {
2604 /// nk_input_xxx(...);
2605 /// }
2606 /// }
2607 /// nk_input_end(&ctx);
2608 /// //
2609 /// // Window
2610 /// if (nk_begin_xxx(...) {
2611 /// // [...widgets...]
2612 /// nk_layout_row_dynamic(...);
2613 /// if (nk_group_begin_xxx(ctx, ...) {
2614 /// //[... widgets ...]
2615 /// nk_group_end(ctx);
2616 /// }
2617 /// }
2618 /// nk_end(ctx);
2619 /// //
2620 /// // Draw
2621 /// const struct nk_command *cmd = 0;
2622 /// nk_foreach(cmd, &ctx) {
2623 /// switch (cmd->type) {
2624 /// case NK_COMMAND_LINE:
2625 /// your_draw_line_function(...)
2626 /// break;
2627 /// case NK_COMMAND_RECT
2628 /// your_draw_rect_function(...)
2629 /// break;
2630 /// case ...:
2631 /// // [...]
2632 /// }
2633 /// nk_clear(&ctx);
2634 /// }
2635 /// nk_free(&ctx);
2636 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2637 /// #### Reference
2638 /// Function | Description
2639 /// --------------------------------|-------------------------------------------
2640 /// nk_group_begin | Start a new group with internal scrollbar handling
2641 /// nk_group_begin_titled | Start a new group with separeted name and title and internal scrollbar handling
2642 /// nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
2643 /// nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
2644 /// nk_group_scrolled_begin | Start a new group with manual scrollbar handling
2645 /// nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
2646 /// nk_group_get_scroll | Gets the scroll offset for the given group
2647 /// nk_group_set_scroll | Sets the scroll offset for the given group
2648 */
2649 /*/// #### nk_group_begin
2650 /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
2651 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2652 /// int nk_group_begin(struct nk_context*, const char *title, nk_flags);
2653 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2654 ///
2655 /// Parameter | Description
2656 /// ------------|-----------------------------------------------------------
2657 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2658 /// __title__ | Must be an unique identifier for this group that is also used for the group header
2659 /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
2660 ///
2661 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2662 */
2663 NK_API int nk_group_begin(struct nk_context*, const char *title, nk_flags);
2664 /*/// #### nk_group_begin_titled
2665 /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
2666 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2667 /// int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
2668 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2669 ///
2670 /// Parameter | Description
2671 /// ------------|-----------------------------------------------------------
2672 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2673 /// __id__ | Must be an unique identifier for this group
2674 /// __title__ | Group header title
2675 /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
2676 ///
2677 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2678 */
2679 NK_API int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
2680 /*/// #### nk_group_end
2681 /// Ends a widget group
2682 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2683 /// void nk_group_end(struct nk_context*);
2684 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2685 ///
2686 /// Parameter | Description
2687 /// ------------|-----------------------------------------------------------
2688 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2689 */
2690 NK_API void nk_group_end(struct nk_context*);
2691 /*/// #### nk_group_scrolled_offset_begin
2692 /// starts a new widget group. requires a previous layouting function to specify
2693 /// a size. Does not keep track of scrollbar.
2694 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2695 /// int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
2696 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2697 ///
2698 /// Parameter | Description
2699 /// ------------|-----------------------------------------------------------
2700 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2701 /// __x_offset__| Scrollbar x-offset to offset all widgets inside the group horizontally.
2702 /// __y_offset__| Scrollbar y-offset to offset all widgets inside the group vertically
2703 /// __title__ | Window unique group title used to both identify and display in the group header
2704 /// __flags__ | Window flags from the nk_panel_flags section
2705 ///
2706 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2707 */
2708 NK_API int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
2709 /*/// #### nk_group_scrolled_begin
2710 /// Starts a new widget group. requires a previous
2711 /// layouting function to specify a size. Does not keep track of scrollbar.
2712 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2713 /// int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
2714 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2715 ///
2716 /// Parameter | Description
2717 /// ------------|-----------------------------------------------------------
2718 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2719 /// __off__ | Both x- and y- scroll offset. Allows for manual scrollbar control
2720 /// __title__ | Window unique group title used to both identify and display in the group header
2721 /// __flags__ | Window flags from nk_panel_flags section
2722 ///
2723 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2724 */
2725 NK_API int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
2726 /*/// #### nk_group_scrolled_end
2727 /// Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
2728 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2729 /// void nk_group_scrolled_end(struct nk_context*);
2730 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2731 ///
2732 /// Parameter | Description
2733 /// ------------|-----------------------------------------------------------
2734 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2735 */
2736 NK_API void nk_group_scrolled_end(struct nk_context*);
2737 /*/// #### nk_group_get_scroll
2738 /// Gets the scroll position of the given group.
2739 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2740 /// void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
2741 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2742 ///
2743 /// Parameter | Description
2744 /// -------------|-----------------------------------------------------------
2745 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2746 /// __id__ | The id of the group to get the scroll position of
2747 /// __x_offset__ | A pointer to the x offset output (or NULL to ignore)
2748 /// __y_offset__ | A pointer to the y offset output (or NULL to ignore)
2749 */
2750 NK_API void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
2751 /*/// #### nk_group_set_scroll
2752 /// Sets the scroll position of the given group.
2753 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2754 /// void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
2755 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2756 ///
2757 /// Parameter | Description
2758 /// -------------|-----------------------------------------------------------
2759 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2760 /// __id__ | The id of the group to scroll
2761 /// __x_offset__ | The x offset to scroll to
2762 /// __y_offset__ | The y offset to scroll to
2763 */
2764 NK_API void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
2765 /* =============================================================================
2766 *
2767 * TREE
2768 *
2769 * =============================================================================
2770 /// ### Tree
2771 /// Trees represent two different concept. First the concept of a collapsable
2772 /// UI section that can be either in a hidden or visibile state. They allow the UI
2773 /// user to selectively minimize the current set of visible UI to comprehend.
2774 /// The second concept are tree widgets for visual UI representation of trees.<br /><br />
2775 ///
2776 /// Trees thereby can be nested for tree representations and multiple nested
2777 /// collapsable UI sections. All trees are started by calling of the
2778 /// `nk_tree_xxx_push_tree` functions and ended by calling one of the
2779 /// `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
2780 /// and optionally an image to be displayed and the initial collapse state from
2781 /// the nk_collapse_states section.<br /><br />
2782 ///
2783 /// The runtime state of the tree is either stored outside the library by the caller
2784 /// or inside which requires a unique ID. The unique ID can either be generated
2785 /// automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
2786 /// by `__FILE__` and a user provided ID generated for example by loop index with
2787 /// function `nk_tree_push_id` or completely provided from outside by user with
2788 /// function `nk_tree_push_hashed`.
2789 ///
2790 /// #### Usage
2791 /// To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
2792 /// functions to start a collapsable UI section and `nk_tree_xxx_pop` to mark the
2793 /// end.
2794 /// Each starting function will either return `false(0)` if the tree is collapsed
2795 /// or hidden and therefore does not need to be filled with content or `true(1)`
2796 /// if visible and required to be filled.
2797 ///
2798 /// !!! Note
2799 /// The tree header does not require and layouting function and instead
2800 /// calculates a auto height based on the currently used font size
2801 ///
2802 /// The tree ending functions only need to be called if the tree content is
2803 /// actually visible. So make sure the tree push function is guarded by `if`
2804 /// and the pop call is only taken if the tree is visible.
2805 ///
2806 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2807 /// if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
2808 /// nk_layout_row_dynamic(...);
2809 /// nk_widget(...);
2810 /// nk_tree_pop(ctx);
2811 /// }
2812 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2813 ///
2814 /// #### Reference
2815 /// Function | Description
2816 /// ----------------------------|-------------------------------------------
2817 /// nk_tree_push | Start a collapsable UI section with internal state management
2818 /// nk_tree_push_id | Start a collapsable UI section with internal state management callable in a look
2819 /// nk_tree_push_hashed | Start a collapsable UI section with internal state management with full control over internal unique ID use to store state
2820 /// nk_tree_image_push | Start a collapsable UI section with image and label header
2821 /// nk_tree_image_push_id | Start a collapsable UI section with image and label header and internal state management callable in a look
2822 /// nk_tree_image_push_hashed | Start a collapsable UI section with image and label header and internal state management with full control over internal unique ID use to store state
2823 /// nk_tree_pop | Ends a collapsable UI section
2824 //
2825 /// nk_tree_state_push | Start a collapsable UI section with external state management
2826 /// nk_tree_state_image_push | Start a collapsable UI section with image and label header and external state management
2827 /// nk_tree_state_pop | Ends a collapsabale UI section
2828 ///
2829 /// #### nk_tree_type
2830 /// Flag | Description
2831 /// ----------------|----------------------------------------
2832 /// NK_TREE_NODE | Highlighted tree header to mark a collapsable UI section
2833 /// NK_TREE_TAB | Non-highighted tree header closer to tree representations
2834 */
2835 /*/// #### nk_tree_push
2836 /// Starts a collapsable UI section with internal state management
2837 /// !!! WARNING
2838 /// To keep track of the runtime tree collapsable state this function uses
2839 /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
2840 /// to call this function in a loop please use `nk_tree_push_id` or
2841 /// `nk_tree_push_hashed` instead.
2842 ///
2843 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2844 /// #define nk_tree_push(ctx, type, title, state)
2845 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2846 ///
2847 /// Parameter | Description
2848 /// ------------|-----------------------------------------------------------
2849 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2850 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2851 /// __title__ | Label printed in the tree header
2852 /// __state__ | Initial tree state value out of nk_collapse_states
2853 ///
2854 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2855 */
2856 #define nk_tree_push(ctx, type, title, state) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
2857 /*/// #### nk_tree_push_id
2858 /// Starts a collapsable UI section with internal state management callable in a look
2859 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2860 /// #define nk_tree_push_id(ctx, type, title, state, id)
2861 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2862 ///
2863 /// Parameter | Description
2864 /// ------------|-----------------------------------------------------------
2865 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2866 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2867 /// __title__ | Label printed in the tree header
2868 /// __state__ | Initial tree state value out of nk_collapse_states
2869 /// __id__ | Loop counter index if this function is called in a loop
2870 ///
2871 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2872 */
2873 #define nk_tree_push_id(ctx, type, title, state, id) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
2874 /*/// #### nk_tree_push_hashed
2875 /// Start a collapsable UI section with internal state management with full
2876 /// control over internal unique ID used to store state
2877 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2878 /// int nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
2879 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2880 ///
2881 /// Parameter | Description
2882 /// ------------|-----------------------------------------------------------
2883 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2884 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2885 /// __title__ | Label printed in the tree header
2886 /// __state__ | Initial tree state value out of nk_collapse_states
2887 /// __hash__ | Memory block or string to generate the ID from
2888 /// __len__ | Size of passed memory block or string in __hash__
2889 /// __seed__ | Seeding value if this function is called in a loop or default to `0`
2890 ///
2891 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2892 */
2893 NK_API int nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
2894 /*/// #### nk_tree_image_push
2895 /// Start a collapsable UI section with image and label header
2896 /// !!! WARNING
2897 /// To keep track of the runtime tree collapsable state this function uses
2898 /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
2899 /// to call this function in a loop please use `nk_tree_image_push_id` or
2900 /// `nk_tree_image_push_hashed` instead.
2901 ///
2902 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2903 /// #define nk_tree_image_push(ctx, type, img, title, state)
2904 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2905 //
2906 /// Parameter | Description
2907 /// ------------|-----------------------------------------------------------
2908 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2909 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2910 /// __img__ | Image to display inside the header on the left of the label
2911 /// __title__ | Label printed in the tree header
2912 /// __state__ | Initial tree state value out of nk_collapse_states
2913 ///
2914 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2915 */
2916 #define nk_tree_image_push(ctx, type, img, title, state) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
2917 /*/// #### nk_tree_image_push_id
2918 /// Start a collapsable UI section with image and label header and internal state
2919 /// management callable in a look
2920 ///
2921 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2922 /// #define nk_tree_image_push_id(ctx, type, img, title, state, id)
2923 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2924 ///
2925 /// Parameter | Description
2926 /// ------------|-----------------------------------------------------------
2927 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2928 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2929 /// __img__ | Image to display inside the header on the left of the label
2930 /// __title__ | Label printed in the tree header
2931 /// __state__ | Initial tree state value out of nk_collapse_states
2932 /// __id__ | Loop counter index if this function is called in a loop
2933 ///
2934 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2935 */
2936 #define nk_tree_image_push_id(ctx, type, img, title, state, id) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
2937 /*/// #### nk_tree_image_push_hashed
2938 /// Start a collapsable UI section with internal state management with full
2939 /// control over internal unique ID used to store state
2940 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2941 /// int nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
2942 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2943 ///
2944 /// Parameter | Description
2945 /// ------------|-----------------------------------------------------------
2946 /// __ctx__ | Must point to an previously initialized `nk_context` struct
2947 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2948 /// __img__ | Image to display inside the header on the left of the label
2949 /// __title__ | Label printed in the tree header
2950 /// __state__ | Initial tree state value out of nk_collapse_states
2951 /// __hash__ | Memory block or string to generate the ID from
2952 /// __len__ | Size of passed memory block or string in __hash__
2953 /// __seed__ | Seeding value if this function is called in a loop or default to `0`
2954 ///
2955 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2956 */
2957 NK_API int nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
2958 /*/// #### nk_tree_pop
2959 /// Ends a collapsabale UI section
2960 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2961 /// void nk_tree_pop(struct nk_context*);
2962 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2963 ///
2964 /// Parameter | Description
2965 /// ------------|-----------------------------------------------------------
2966 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
2967 */
2968 NK_API void nk_tree_pop(struct nk_context*);
2969 /*/// #### nk_tree_state_push
2970 /// Start a collapsable UI section with external state management
2971 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2972 /// int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
2973 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2974 ///
2975 /// Parameter | Description
2976 /// ------------|-----------------------------------------------------------
2977 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
2978 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2979 /// __title__ | Label printed in the tree header
2980 /// __state__ | Persistent state to update
2981 ///
2982 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
2983 */
2984 NK_API int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
2985 /*/// #### nk_tree_state_image_push
2986 /// Start a collapsable UI section with image and label header and external state management
2987 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
2988 /// int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
2989 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2990 ///
2991 /// Parameter | Description
2992 /// ------------|-----------------------------------------------------------
2993 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
2994 /// __img__ | Image to display inside the header on the left of the label
2995 /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
2996 /// __title__ | Label printed in the tree header
2997 /// __state__ | Persistent state to update
2998 ///
2999 /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
3000 */
3001 NK_API int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
3002 /*/// #### nk_tree_state_pop
3003 /// Ends a collapsabale UI section
3004 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3005 /// void nk_tree_state_pop(struct nk_context*);
3006 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3007 ///
3008 /// Parameter | Description
3009 /// ------------|-----------------------------------------------------------
3010 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
3011 */
3012 NK_API void nk_tree_state_pop(struct nk_context*);
3013
3014 #define nk_tree_element_push(ctx, type, title, state, sel) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
3015 #define nk_tree_element_push_id(ctx, type, title, state, sel, id) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
3016 NK_API int nk_tree_element_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, int *selected, const char *hash, int len, int seed);
3017 NK_API int nk_tree_element_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, int *selected, const char *hash, int len,int seed);
3018 NK_API void nk_tree_element_pop(struct nk_context*);
3019
3020 /* =============================================================================
3021 *
3022 * LIST VIEW
3023 *
3024 * ============================================================================= */
3025 struct nk_list_view {
3026 /* public: */
3027 int begin, end, count;
3028 /* private: */
3029 int total_height;
3030 struct nk_context *ctx;
3031 nk_uint *scroll_pointer;
3032 nk_uint scroll_value;
3033 };
3034 NK_API int nk_list_view_begin(struct nk_context*, struct nk_list_view *out, const char *id, nk_flags, int row_height, int row_count);
3035 NK_API void nk_list_view_end(struct nk_list_view*);
3036 /* =============================================================================
3037 *
3038 * WIDGET
3039 *
3040 * ============================================================================= */
3041 enum nk_widget_layout_states {
3042 NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
3043 NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
3044 NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
3045 };
3046 enum nk_widget_states {
3047 NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
3048 NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
3049 NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
3050 NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
3051 NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
3052 NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
3053 NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
3054 NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
3055 };
3056 NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
3057 NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
3058 NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
3059 NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
3060 NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
3061 NK_API float nk_widget_width(struct nk_context*);
3062 NK_API float nk_widget_height(struct nk_context*);
3063 NK_API int nk_widget_is_hovered(struct nk_context*);
3064 NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
3065 NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
3066 NK_API void nk_spacing(struct nk_context*, int cols);
3067 /* =============================================================================
3068 *
3069 * TEXT
3070 *
3071 * ============================================================================= */
3072 enum nk_text_align {
3073 NK_TEXT_ALIGN_LEFT = 0x01,
3074 NK_TEXT_ALIGN_CENTERED = 0x02,
3075 NK_TEXT_ALIGN_RIGHT = 0x04,
3076 NK_TEXT_ALIGN_TOP = 0x08,
3077 NK_TEXT_ALIGN_MIDDLE = 0x10,
3078 NK_TEXT_ALIGN_BOTTOM = 0x20
3079 };
3080 enum nk_text_alignment {
3081 NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
3082 NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
3083 NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
3084 };
3085 NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
3086 NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
3087 NK_API void nk_text_wrap(struct nk_context*, const char*, int);
3088 NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
3089 NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
3090 NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
3091 NK_API void nk_label_wrap(struct nk_context*, const char*);
3092 NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
3093 NK_API void nk_image(struct nk_context*, struct nk_image);
3094 NK_API void nk_image_color(struct nk_context*, struct nk_image, struct nk_color);
3095 #ifdef NK_INCLUDE_STANDARD_VARARGS
3096 NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
3097 NK_API void nk_labelf_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(4);
3098 NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(2);
3099 NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(3);
3100 NK_API void nk_labelfv(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
3101 NK_API void nk_labelfv_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(4);
3102 NK_API void nk_labelfv_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
3103 NK_API void nk_labelfv_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
3104 NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
3105 NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
3106 NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
3107 NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
3108 NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
3109 NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
3110 NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
3111 #endif
3112 /* =============================================================================
3113 *
3114 * BUTTON
3115 *
3116 * ============================================================================= */
3117 NK_API int nk_button_text(struct nk_context*, const char *title, int len);
3118 NK_API int nk_button_label(struct nk_context*, const char *title);
3119 NK_API int nk_button_color(struct nk_context*, struct nk_color);
3120 NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type);
3121 NK_API int nk_button_image(struct nk_context*, struct nk_image img);
3122 NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
3123 NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
3124 NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
3125 NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
3126 NK_API int nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
3127 NK_API int nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
3128 NK_API int nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
3129 NK_API int nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
3130 NK_API int nk_button_symbol_text_styled(struct nk_context*,const struct nk_style_button*, enum nk_symbol_type, const char*, int, nk_flags alignment);
3131 NK_API int nk_button_symbol_label_styled(struct nk_context *ctx, const struct nk_style_button *style, enum nk_symbol_type symbol, const char *title, nk_flags align);
3132 NK_API int nk_button_image_label_styled(struct nk_context*,const struct nk_style_button*, struct nk_image img, const char*, nk_flags text_alignment);
3133 NK_API int nk_button_image_text_styled(struct nk_context*,const struct nk_style_button*, struct nk_image img, const char*, int, nk_flags alignment);
3134 NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
3135 NK_API int nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
3136 NK_API int nk_button_pop_behavior(struct nk_context*);
3137 /* =============================================================================
3138 *
3139 * CHECKBOX
3140 *
3141 * ============================================================================= */
3142 NK_API int nk_check_label(struct nk_context*, const char*, int active);
3143 NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
3144 NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
3145 NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
3146 NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
3147 NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
3148 NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
3149 NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
3150 /* =============================================================================
3151 *
3152 * RADIO BUTTON
3153 *
3154 * ============================================================================= */
3155 NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
3156 NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
3157 NK_API int nk_option_label(struct nk_context*, const char*, int active);
3158 NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
3159 /* =============================================================================
3160 *
3161 * SELECTABLE
3162 *
3163 * ============================================================================= */
3164 NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
3165 NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
3166 NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
3167 NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
3168 NK_API int nk_selectable_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int *value);
3169 NK_API int nk_selectable_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int *value);
3170
3171 NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
3172 NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
3173 NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
3174 NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
3175 NK_API int nk_select_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, int value);
3176 NK_API int nk_select_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, int value);
3177
3178 /* =============================================================================
3179 *
3180 * SLIDER
3181 *
3182 * ============================================================================= */
3183 NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
3184 NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
3185 NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
3186 NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
3187 /* =============================================================================
3188 *
3189 * PROGRESSBAR
3190 *
3191 * ============================================================================= */
3192 NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
3193 NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
3194
3195 /* =============================================================================
3196 *
3197 * COLOR PICKER
3198 *
3199 * ============================================================================= */
3200 NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
3201 NK_API int nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
3202 /* =============================================================================
3203 *
3204 * PROPERTIES
3205 *
3206 * =============================================================================
3207 /// ### Properties
3208 /// Properties are the main value modification widgets in Nuklear. Changing a value
3209 /// can be achieved by dragging, adding/removing incremental steps on button click
3210 /// or by directly typing a number.
3211 ///
3212 /// #### Usage
3213 /// Each property requires a unique name for identifaction that is also used for
3214 /// displaying a label. If you want to use the same name multiple times make sure
3215 /// add a '#' before your name. The '#' will not be shown but will generate a
3216 /// unique ID. Each propery also takes in a minimum and maximum value. If you want
3217 /// to make use of the complete number range of a type just use the provided
3218 /// type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
3219 /// `nk_property_int` and `nk_propertyi`. In additional each property takes in
3220 /// a increment value that will be added or subtracted if either the increment
3221 /// decrement button is clicked. Finally there is a value for increment per pixel
3222 /// dragged that is added or subtracted from the value.
3223 ///
3224 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3225 /// int value = 0;
3226 /// struct nk_context ctx;
3227 /// nk_init_xxx(&ctx, ...);
3228 /// while (1) {
3229 /// // Input
3230 /// Event evt;
3231 /// nk_input_begin(&ctx);
3232 /// while (GetEvent(&evt)) {
3233 /// if (evt.type == MOUSE_MOVE)
3234 /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
3235 /// else if (evt.type == [...]) {
3236 /// nk_input_xxx(...);
3237 /// }
3238 /// }
3239 /// nk_input_end(&ctx);
3240 /// //
3241 /// // Window
3242 /// if (nk_begin_xxx(...) {
3243 /// // Property
3244 /// nk_layout_row_dynamic(...);
3245 /// nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
3246 /// }
3247 /// nk_end(ctx);
3248 /// //
3249 /// // Draw
3250 /// const struct nk_command *cmd = 0;
3251 /// nk_foreach(cmd, &ctx) {
3252 /// switch (cmd->type) {
3253 /// case NK_COMMAND_LINE:
3254 /// your_draw_line_function(...)
3255 /// break;
3256 /// case NK_COMMAND_RECT
3257 /// your_draw_rect_function(...)
3258 /// break;
3259 /// case ...:
3260 /// // [...]
3261 /// }
3262 /// nk_clear(&ctx);
3263 /// }
3264 /// nk_free(&ctx);
3265 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3266 ///
3267 /// #### Reference
3268 /// Function | Description
3269 /// --------------------|-------------------------------------------
3270 /// nk_property_int | Integer property directly modifing a passed in value
3271 /// nk_property_float | Float property directly modifing a passed in value
3272 /// nk_property_double | Double property directly modifing a passed in value
3273 /// nk_propertyi | Integer property returning the modified int value
3274 /// nk_propertyf | Float property returning the modified float value
3275 /// nk_propertyd | Double property returning the modified double value
3276 ///
3277 */
3278 /*/// #### nk_property_int
3279 /// Integer property directly modifing a passed in value
3280 /// !!! WARNING
3281 /// To generate a unique property ID using the same label make sure to insert
3282 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3283 ///
3284 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3285 /// void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
3286 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3287 ///
3288 /// Parameter | Description
3289 /// --------------------|-----------------------------------------------------------
3290 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3291 /// __name__ | String used both as a label as well as a unique identifier
3292 /// __min__ | Minimum value not allowed to be underflown
3293 /// __val__ | Integer pointer to be modified
3294 /// __max__ | Maximum value not allowed to be overflown
3295 /// __step__ | Increment added and subtracted on increment and decrement button
3296 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3297 */
3298 NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
3299 /*/// #### nk_property_float
3300 /// Float property directly modifing a passed in value
3301 /// !!! WARNING
3302 /// To generate a unique property ID using the same label make sure to insert
3303 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3304 ///
3305 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3306 /// void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
3307 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3308 ///
3309 /// Parameter | Description
3310 /// --------------------|-----------------------------------------------------------
3311 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3312 /// __name__ | String used both as a label as well as a unique identifier
3313 /// __min__ | Minimum value not allowed to be underflown
3314 /// __val__ | Float pointer to be modified
3315 /// __max__ | Maximum value not allowed to be overflown
3316 /// __step__ | Increment added and subtracted on increment and decrement button
3317 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3318 */
3319 NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
3320 /*/// #### nk_property_double
3321 /// Double property directly modifing a passed in value
3322 /// !!! WARNING
3323 /// To generate a unique property ID using the same label make sure to insert
3324 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3325 ///
3326 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3327 /// void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
3328 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3329 ///
3330 /// Parameter | Description
3331 /// --------------------|-----------------------------------------------------------
3332 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3333 /// __name__ | String used both as a label as well as a unique identifier
3334 /// __min__ | Minimum value not allowed to be underflown
3335 /// __val__ | Double pointer to be modified
3336 /// __max__ | Maximum value not allowed to be overflown
3337 /// __step__ | Increment added and subtracted on increment and decrement button
3338 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3339 */
3340 NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
3341 /*/// #### nk_propertyi
3342 /// Integer property modifing a passed in value and returning the new value
3343 /// !!! WARNING
3344 /// To generate a unique property ID using the same label make sure to insert
3345 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3346 ///
3347 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3348 /// int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
3349 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3350 ///
3351 /// Parameter | Description
3352 /// --------------------|-----------------------------------------------------------
3353 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3354 /// __name__ | String used both as a label as well as a unique identifier
3355 /// __min__ | Minimum value not allowed to be underflown
3356 /// __val__ | Current integer value to be modified and returned
3357 /// __max__ | Maximum value not allowed to be overflown
3358 /// __step__ | Increment added and subtracted on increment and decrement button
3359 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3360 ///
3361 /// Returns the new modified integer value
3362 */
3363 NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
3364 /*/// #### nk_propertyf
3365 /// Float property modifing a passed in value and returning the new value
3366 /// !!! WARNING
3367 /// To generate a unique property ID using the same label make sure to insert
3368 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3369 ///
3370 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3371 /// float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
3372 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3373 ///
3374 /// Parameter | Description
3375 /// --------------------|-----------------------------------------------------------
3376 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3377 /// __name__ | String used both as a label as well as a unique identifier
3378 /// __min__ | Minimum value not allowed to be underflown
3379 /// __val__ | Current float value to be modified and returned
3380 /// __max__ | Maximum value not allowed to be overflown
3381 /// __step__ | Increment added and subtracted on increment and decrement button
3382 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3383 ///
3384 /// Returns the new modified float value
3385 */
3386 NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
3387 /*/// #### nk_propertyd
3388 /// Float property modifing a passed in value and returning the new value
3389 /// !!! WARNING
3390 /// To generate a unique property ID using the same label make sure to insert
3391 /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
3392 ///
3393 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
3394 /// float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
3395 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3396 ///
3397 /// Parameter | Description
3398 /// --------------------|-----------------------------------------------------------
3399 /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
3400 /// __name__ | String used both as a label as well as a unique identifier
3401 /// __min__ | Minimum value not allowed to be underflown
3402 /// __val__ | Current double value to be modified and returned
3403 /// __max__ | Maximum value not allowed to be overflown
3404 /// __step__ | Increment added and subtracted on increment and decrement button
3405 /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
3406 ///
3407 /// Returns the new modified double value
3408 */
3409 NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
3410 /* =============================================================================
3411 *
3412 * TEXT EDIT
3413 *
3414 * ============================================================================= */
3415 enum nk_edit_flags {
3416 NK_EDIT_DEFAULT = 0,
3417 NK_EDIT_READ_ONLY = NK_FLAG(0),
3418 NK_EDIT_AUTO_SELECT = NK_FLAG(1),
3419 NK_EDIT_SIG_ENTER = NK_FLAG(2),
3420 NK_EDIT_ALLOW_TAB = NK_FLAG(3),
3421 NK_EDIT_NO_CURSOR = NK_FLAG(4),
3422 NK_EDIT_SELECTABLE = NK_FLAG(5),
3423 NK_EDIT_CLIPBOARD = NK_FLAG(6),
3424 NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
3425 NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
3426 NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
3427 NK_EDIT_MULTILINE = NK_FLAG(10),
3428 NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
3429 };
3430 enum nk_edit_types {
3431 NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
3432 NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
3433 NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
3434 NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
3435 };
3436 enum nk_edit_events {
3437 NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
3438 NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
3439 NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
3440 NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
3441 NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
3442 };
3443 NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
3444 NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
3445 NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
3446 NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
3447 NK_API void nk_edit_unfocus(struct nk_context*);
3448 /* =============================================================================
3449 *
3450 * CHART
3451 *
3452 * ============================================================================= */
3453 NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
3454 NK_API int nk_chart_begin_colored(struct nk_context*, enum nk_chart_type, struct nk_color, struct nk_color active, int num, float min, float max);
3455 NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
3456 NK_API void nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type, struct nk_color, struct nk_color active, int count, float min_value, float max_value);
3457 NK_API nk_flags nk_chart_push(struct nk_context*, float);
3458 NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
3459 NK_API void nk_chart_end(struct nk_context*);
3460 NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
3461 NK_API void nk_plot_function(struct nk_context*, enum nk_chart_type, void *userdata, float(*value_getter)(void* user, int index), int count, int offset);
3462 /* =============================================================================
3463 *
3464 * POPUP
3465 *
3466 * ============================================================================= */
3467 NK_API int nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
3468 NK_API void nk_popup_close(struct nk_context*);
3469 NK_API void nk_popup_end(struct nk_context*);
3470 NK_API void nk_popup_get_scroll(struct nk_context*, nk_uint *offset_x, nk_uint *offset_y);
3471 NK_API void nk_popup_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
3472 /* =============================================================================
3473 *
3474 * COMBOBOX
3475 *
3476 * ============================================================================= */
3477 NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height, struct nk_vec2 size);
3478 NK_API int nk_combo_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int selected, int count, int item_height, struct nk_vec2 size);
3479 NK_API int nk_combo_string(struct nk_context*, const char *items_separated_by_zeros, int selected, int count, int item_height, struct nk_vec2 size);
3480 NK_API int nk_combo_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void *userdata, int selected, int count, int item_height, struct nk_vec2 size);
3481 NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
3482 NK_API void nk_combobox_string(struct nk_context*, const char *items_separated_by_zeros, int *selected, int count, int item_height, struct nk_vec2 size);
3483 NK_API void nk_combobox_separator(struct nk_context*, const char *items_separated_by_separator, int separator,int *selected, int count, int item_height, struct nk_vec2 size);
3484 NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int *selected, int count, int item_height, struct nk_vec2 size);
3485 /* =============================================================================
3486 *
3487 * ABSTRACT COMBOBOX
3488 *
3489 * ============================================================================= */
3490 NK_API int nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
3491 NK_API int nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
3492 NK_API int nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
3493 NK_API int nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
3494 NK_API int nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
3495 NK_API int nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
3496 NK_API int nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
3497 NK_API int nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
3498 NK_API int nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
3499 NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
3500 NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
3501 NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
3502 NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
3503 NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
3504 NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
3505 NK_API void nk_combo_close(struct nk_context*);
3506 NK_API void nk_combo_end(struct nk_context*);
3507 /* =============================================================================
3508 *
3509 * CONTEXTUAL
3510 *
3511 * ============================================================================= */
3512 NK_API int nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
3513 NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
3514 NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
3515 NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
3516 NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
3517 NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
3518 NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
3519 NK_API void nk_contextual_close(struct nk_context*);
3520 NK_API void nk_contextual_end(struct nk_context*);
3521 /* =============================================================================
3522 *
3523 * TOOLTIP
3524 *
3525 * ============================================================================= */
3526 NK_API void nk_tooltip(struct nk_context*, const char*);
3527 #ifdef NK_INCLUDE_STANDARD_VARARGS
3528 NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
3529 NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
3530 #endif
3531 NK_API int nk_tooltip_begin(struct nk_context*, float width);
3532 NK_API void nk_tooltip_end(struct nk_context*);
3533 /* =============================================================================
3534 *
3535 * MENU
3536 *
3537 * ============================================================================= */
3538 NK_API void nk_menubar_begin(struct nk_context*);
3539 NK_API void nk_menubar_end(struct nk_context*);
3540 NK_API int nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
3541 NK_API int nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
3542 NK_API int nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
3543 NK_API int nk_menu_begin_image_text(struct nk_context*, const char*, int,nk_flags align,struct nk_image, struct nk_vec2 size);
3544 NK_API int nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
3545 NK_API int nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
3546 NK_API int nk_menu_begin_symbol_text(struct nk_context*, const char*, int,nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
3547 NK_API int nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
3548 NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
3549 NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
3550 NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
3551 NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
3552 NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
3553 NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
3554 NK_API void nk_menu_close(struct nk_context*);
3555 NK_API void nk_menu_end(struct nk_context*);
3556 /* =============================================================================
3557 *
3558 * STYLE
3559 *
3560 * ============================================================================= */
3561 enum nk_style_colors {
3562 NK_COLOR_TEXT,
3563 NK_COLOR_WINDOW,
3564 NK_COLOR_HEADER,
3565 NK_COLOR_BORDER,
3566 NK_COLOR_BUTTON,
3567 NK_COLOR_BUTTON_HOVER,
3568 NK_COLOR_BUTTON_ACTIVE,
3569 NK_COLOR_TOGGLE,
3570 NK_COLOR_TOGGLE_HOVER,
3571 NK_COLOR_TOGGLE_CURSOR,
3572 NK_COLOR_SELECT,
3573 NK_COLOR_SELECT_ACTIVE,
3574 NK_COLOR_SLIDER,
3575 NK_COLOR_SLIDER_CURSOR,
3576 NK_COLOR_SLIDER_CURSOR_HOVER,
3577 NK_COLOR_SLIDER_CURSOR_ACTIVE,
3578 NK_COLOR_PROPERTY,
3579 NK_COLOR_EDIT,
3580 NK_COLOR_EDIT_CURSOR,
3581 NK_COLOR_COMBO,
3582 NK_COLOR_CHART,
3583 NK_COLOR_CHART_COLOR,
3584 NK_COLOR_CHART_COLOR_HIGHLIGHT,
3585 NK_COLOR_SCROLLBAR,
3586 NK_COLOR_SCROLLBAR_CURSOR,
3587 NK_COLOR_SCROLLBAR_CURSOR_HOVER,
3588 NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
3589 NK_COLOR_TAB_HEADER,
3590 NK_COLOR_COUNT
3591 };
3592 enum nk_style_cursor {
3593 NK_CURSOR_ARROW,
3594 NK_CURSOR_TEXT,
3595 NK_CURSOR_MOVE,
3596 NK_CURSOR_RESIZE_VERTICAL,
3597 NK_CURSOR_RESIZE_HORIZONTAL,
3598 NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
3599 NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
3600 NK_CURSOR_COUNT
3601 };
3602 NK_API void nk_style_default(struct nk_context*);
3603 NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
3604 NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
3605 NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
3606 NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
3607 NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
3608 NK_API int nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
3609 NK_API void nk_style_show_cursor(struct nk_context*);
3610 NK_API void nk_style_hide_cursor(struct nk_context*);
3611
3612 NK_API int nk_style_push_font(struct nk_context*, const struct nk_user_font*);
3613 NK_API int nk_style_push_float(struct nk_context*, float*, float);
3614 NK_API int nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
3615 NK_API int nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
3616 NK_API int nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
3617 NK_API int nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
3618
3619 NK_API int nk_style_pop_font(struct nk_context*);
3620 NK_API int nk_style_pop_float(struct nk_context*);
3621 NK_API int nk_style_pop_vec2(struct nk_context*);
3622 NK_API int nk_style_pop_style_item(struct nk_context*);
3623 NK_API int nk_style_pop_flags(struct nk_context*);
3624 NK_API int nk_style_pop_color(struct nk_context*);
3625 /* =============================================================================
3626 *
3627 * COLOR
3628 *
3629 * ============================================================================= */
3630 NK_API struct nk_color nk_rgb(int r, int g, int b);
3631 NK_API struct nk_color nk_rgb_iv(const int *rgb);
3632 NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
3633 NK_API struct nk_color nk_rgb_f(float r, float g, float b);
3634 NK_API struct nk_color nk_rgb_fv(const float *rgb);
3635 NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
3636 NK_API struct nk_color nk_rgb_hex(const char *rgb);
3637
3638 NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
3639 NK_API struct nk_color nk_rgba_u32(nk_uint);
3640 NK_API struct nk_color nk_rgba_iv(const int *rgba);
3641 NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
3642 NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
3643 NK_API struct nk_color nk_rgba_fv(const float *rgba);
3644 NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
3645 NK_API struct nk_color nk_rgba_hex(const char *rgb);
3646
3647 NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
3648 NK_API struct nk_colorf nk_hsva_colorfv(float *c);
3649 NK_API void nk_colorf_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_colorf in);
3650 NK_API void nk_colorf_hsva_fv(float *hsva, struct nk_colorf in);
3651
3652 NK_API struct nk_color nk_hsv(int h, int s, int v);
3653 NK_API struct nk_color nk_hsv_iv(const int *hsv);
3654 NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
3655 NK_API struct nk_color nk_hsv_f(float h, float s, float v);
3656 NK_API struct nk_color nk_hsv_fv(const float *hsv);
3657
3658 NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
3659 NK_API struct nk_color nk_hsva_iv(const int *hsva);
3660 NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
3661 NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
3662 NK_API struct nk_color nk_hsva_fv(const float *hsva);
3663
3664 /* color (conversion nuklear --> user) */
3665 NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
3666 NK_API void nk_color_fv(float *rgba_out, struct nk_color);
3667 NK_API struct nk_colorf nk_color_cf(struct nk_color);
3668 NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
3669 NK_API void nk_color_dv(double *rgba_out, struct nk_color);
3670
3671 NK_API nk_uint nk_color_u32(struct nk_color);
3672 NK_API void nk_color_hex_rgba(char *output, struct nk_color);
3673 NK_API void nk_color_hex_rgb(char *output, struct nk_color);
3674
3675 NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
3676 NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
3677 NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
3678 NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
3679 NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
3680 NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
3681
3682 NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
3683 NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
3684 NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
3685 NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
3686 NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
3687 NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
3688 /* =============================================================================
3689 *
3690 * IMAGE
3691 *
3692 * ============================================================================= */
3693 NK_API nk_handle nk_handle_ptr(void*);
3694 NK_API nk_handle nk_handle_id(int);
3695 NK_API struct nk_image nk_image_handle(nk_handle);
3696 NK_API struct nk_image nk_image_ptr(void*);
3697 NK_API struct nk_image nk_image_id(int);
3698 NK_API int nk_image_is_subimage(const struct nk_image* img);
3699 NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
3700 NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
3701 NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
3702 /* =============================================================================
3703 *
3704 * MATH
3705 *
3706 * ============================================================================= */
3707 NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
3708 NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
3709
3710 NK_API struct nk_vec2 nk_vec2(float x, float y);
3711 NK_API struct nk_vec2 nk_vec2i(int x, int y);
3712 NK_API struct nk_vec2 nk_vec2v(const float *xy);
3713 NK_API struct nk_vec2 nk_vec2iv(const int *xy);
3714
3715 NK_API struct nk_rect nk_get_null_rect(void);
3716 NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
3717 NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
3718 NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
3719 NK_API struct nk_rect nk_rectv(const float *xywh);
3720 NK_API struct nk_rect nk_rectiv(const int *xywh);
3721 NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
3722 NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
3723 /* =============================================================================
3724 *
3725 * STRING
3726 *
3727 * ============================================================================= */
3728 NK_API int nk_strlen(const char *str);
3729 NK_API int nk_stricmp(const char *s1, const char *s2);
3730 NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
3731 NK_API int nk_strtoi(const char *str, const char **endptr);
3732 NK_API float nk_strtof(const char *str, const char **endptr);
3733 NK_API double nk_strtod(const char *str, const char **endptr);
3734 NK_API int nk_strfilter(const char *text, const char *regexp);
3735 NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
3736 NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
3737 /* =============================================================================
3738 *
3739 * UTF-8
3740 *
3741 * ============================================================================= */
3742 NK_API int nk_utf_decode(const char*, nk_rune*, int);
3743 NK_API int nk_utf_encode(nk_rune, char*, int);
3744 NK_API int nk_utf_len(const char*, int byte_len);
3745 NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
3746 /* ===============================================================
3747 *
3748 * FONT
3749 *
3750 * ===============================================================*/
3751 /* Font handling in this library was designed to be quite customizable and lets
3752 you decide what you want to use and what you want to provide. There are three
3753 different ways to use the font atlas. The first two will use your font
3754 handling scheme and only requires essential data to run nuklear. The next
3755 slightly more advanced features is font handling with vertex buffer output.
3756 Finally the most complex API wise is using nuklear's font baking API.
3757
3758 1.) Using your own implementation without vertex buffer output
3759 --------------------------------------------------------------
3760 So first up the easiest way to do font handling is by just providing a
3761 `nk_user_font` struct which only requires the height in pixel of the used
3762 font and a callback to calculate the width of a string. This way of handling
3763 fonts is best fitted for using the normal draw shape command API where you
3764 do all the text drawing yourself and the library does not require any kind
3765 of deeper knowledge about which font handling mechanism you use.
3766 IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
3767 over the complete life time! I know this sucks but it is currently the only
3768 way to switch between fonts.
3769
3770 float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
3771 {
3772 your_font_type *type = handle.ptr;
3773 float text_width = ...;
3774 return text_width;
3775 }
3776
3777 struct nk_user_font font;
3778 font.userdata.ptr = &your_font_class_or_struct;
3779 font.height = your_font_height;
3780 font.width = your_text_width_calculation;
3781
3782 struct nk_context ctx;
3783 nk_init_default(&ctx, &font);
3784
3785 2.) Using your own implementation with vertex buffer output
3786 --------------------------------------------------------------
3787 While the first approach works fine if you don't want to use the optional
3788 vertex buffer output it is not enough if you do. To get font handling working
3789 for these cases you have to provide two additional parameters inside the
3790 `nk_user_font`. First a texture atlas handle used to draw text as subimages
3791 of a bigger font atlas texture and a callback to query a character's glyph
3792 information (offset, size, ...). So it is still possible to provide your own
3793 font and use the vertex buffer output.
3794
3795 float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
3796 {
3797 your_font_type *type = handle.ptr;
3798 float text_width = ...;
3799 return text_width;
3800 }
3801 void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
3802 {
3803 your_font_type *type = handle.ptr;
3804 glyph.width = ...;
3805 glyph.height = ...;
3806 glyph.xadvance = ...;
3807 glyph.uv[0].x = ...;
3808 glyph.uv[0].y = ...;
3809 glyph.uv[1].x = ...;
3810 glyph.uv[1].y = ...;
3811 glyph.offset.x = ...;
3812 glyph.offset.y = ...;
3813 }
3814
3815 struct nk_user_font font;
3816 font.userdata.ptr = &your_font_class_or_struct;
3817 font.height = your_font_height;
3818 font.width = your_text_width_calculation;
3819 font.query = query_your_font_glyph;
3820 font.texture.id = your_font_texture;
3821
3822 struct nk_context ctx;
3823 nk_init_default(&ctx, &font);
3824
3825 3.) Nuklear font baker
3826 ------------------------------------
3827 The final approach if you do not have a font handling functionality or don't
3828 want to use it in this library is by using the optional font baker.
3829 The font baker APIs can be used to create a font plus font atlas texture
3830 and can be used with or without the vertex buffer output.
3831
3832 It still uses the `nk_user_font` struct and the two different approaches
3833 previously stated still work. The font baker is not located inside
3834 `nk_context` like all other systems since it can be understood as more of
3835 an extension to nuklear and does not really depend on any `nk_context` state.
3836
3837 Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
3838 functions. If you don't care about memory just call the default version
3839 `nk_font_atlas_init_default` which will allocate all memory from the standard library.
3840 If you want to control memory allocation but you don't care if the allocated
3841 memory is temporary and therefore can be freed directly after the baking process
3842 is over or permanent you can call `nk_font_atlas_init`.
3843
3844 After successfully initializing the font baker you can add Truetype(.ttf) fonts from
3845 different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
3846 functions. Adding font will permanently store each font, font config and ttf memory block(!)
3847 inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
3848 the font baker by for example adding additional fonts you can call
3849 `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
3850
3851 As soon as you added all fonts you wanted you can now start the baking process
3852 for every selected glyph to image by calling `nk_font_atlas_bake`.
3853 The baking process returns image memory, width and height which can be used to
3854 either create your own image object or upload it to any graphics library.
3855 No matter which case you finally have to call `nk_font_atlas_end` which
3856 will free all temporary memory including the font atlas image so make sure
3857 you created our texture beforehand. `nk_font_atlas_end` requires a handle
3858 to your font texture or object and optionally fills a `struct nk_draw_null_texture`
3859 which can be used for the optional vertex output. If you don't want it just
3860 set the argument to `NULL`.
3861
3862 At this point you are done and if you don't want to reuse the font atlas you
3863 can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
3864 memory. Finally if you don't use the font atlas and any of it's fonts anymore
3865 you need to call `nk_font_atlas_clear` to free all memory still being used.
3866
3867 struct nk_font_atlas atlas;
3868 nk_font_atlas_init_default(&atlas);
3869 nk_font_atlas_begin(&atlas);
3870 nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
3871 nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
3872 const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
3873 nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
3874
3875 struct nk_context ctx;
3876 nk_init_default(&ctx, &font->handle);
3877 while (1) {
3878
3879 }
3880 nk_font_atlas_clear(&atlas);
3881
3882 The font baker API is probably the most complex API inside this library and
3883 I would suggest reading some of my examples `example/` to get a grip on how
3884 to use the font atlas. There are a number of details I left out. For example
3885 how to merge fonts, configure a font with `nk_font_config` to use other languages,
3886 use another texture coordinate format and a lot more:
3887
3888 struct nk_font_config cfg = nk_font_config(font_pixel_height);
3889 cfg.merge_mode = nk_false or nk_true;
3890 cfg.range = nk_font_korean_glyph_ranges();
3891 cfg.coord_type = NK_COORD_PIXEL;
3892 nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
3893
3894 */
3895 struct nk_user_font_glyph;
3896 typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
3897 typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
3898 struct nk_user_font_glyph *glyph,
3899 nk_rune codepoint, nk_rune next_codepoint);
3900
3901 #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
3902 struct nk_user_font_glyph {
3903 struct nk_vec2 uv[2];
3904 /* texture coordinates */
3905 struct nk_vec2 offset;
3906 /* offset between top left and glyph */
3907 float width, height;
3908 /* size of the glyph */
3909 float xadvance;
3910 /* offset to the next glyph */
3911 };
3912 #endif
3913
3914 struct nk_user_font {
3915 nk_handle userdata;
3916 /* user provided font handle */
3917 float height;
3918 /* max height of the font */
3919 nk_text_width_f width;
3920 /* font string width in pixel callback */
3921 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
3922 nk_query_font_glyph_f query;
3923 /* font glyph callback to query drawing info */
3924 nk_handle texture;
3925 /* texture handle to the used font atlas or texture */
3926 #endif
3927 };
3928
3929 #ifdef NK_INCLUDE_FONT_BAKING
3930 enum nk_font_coord_type {
3931 NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
3932 NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
3933 };
3934
3935 struct nk_font;
3936 struct nk_baked_font {
3937 float height;
3938 /* height of the font */
3939 float ascent, descent;
3940 /* font glyphs ascent and descent */
3941 nk_rune glyph_offset;
3942 /* glyph array offset inside the font glyph baking output array */
3943 nk_rune glyph_count;
3944 /* number of glyphs of this font inside the glyph baking array output */
3945 const nk_rune *ranges;
3946 /* font codepoint ranges as pairs of (from/to) and 0 as last element */
3947 };
3948
3949 struct nk_font_config {
3950 struct nk_font_config *next;
3951 /* NOTE: only used internally */
3952 void *ttf_blob;
3953 /* pointer to loaded TTF file memory block.
3954 * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
3955 nk_size ttf_size;
3956 /* size of the loaded TTF file memory block
3957 * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
3958
3959 unsigned char ttf_data_owned_by_atlas;
3960 /* used inside font atlas: default to: 0*/
3961 unsigned char merge_mode;
3962 /* merges this font into the last font */
3963 unsigned char pixel_snap;
3964 /* align every character to pixel boundary (if true set oversample (1,1)) */
3965 unsigned char oversample_v, oversample_h;
3966 /* rasterize at hight quality for sub-pixel position */
3967 unsigned char padding[3];
3968
3969 float size;
3970 /* baked pixel height of the font */
3971 enum nk_font_coord_type coord_type;
3972 /* texture coordinate format with either pixel or UV coordinates */
3973 struct nk_vec2 spacing;
3974 /* extra pixel spacing between glyphs */
3975 const nk_rune *range;
3976 /* list of unicode ranges (2 values per range, zero terminated) */
3977 struct nk_baked_font *font;
3978 /* font to setup in the baking process: NOTE: not needed for font atlas */
3979 nk_rune fallback_glyph;
3980 /* fallback glyph to use if a given rune is not found */
3981 struct nk_font_config *n;
3982 struct nk_font_config *p;
3983 };
3984
3985 struct nk_font_glyph {
3986 nk_rune codepoint;
3987 float xadvance;
3988 float x0, y0, x1, y1, w, h;
3989 float u0, v0, u1, v1;
3990 };
3991
3992 struct nk_font {
3993 struct nk_font *next;
3994 struct nk_user_font handle;
3995 struct nk_baked_font info;
3996 float scale;
3997 struct nk_font_glyph *glyphs;
3998 const struct nk_font_glyph *fallback;
3999 nk_rune fallback_codepoint;
4000 nk_handle texture;
4001 struct nk_font_config *config;
4002 };
4003
4004 enum nk_font_atlas_format {
4005 NK_FONT_ATLAS_ALPHA8,
4006 NK_FONT_ATLAS_RGBA32
4007 };
4008
4009 struct nk_font_atlas {
4010 void *pixel;
4011 int tex_width;
4012 int tex_height;
4013
4014 struct nk_allocator permanent;
4015 struct nk_allocator temporary;
4016
4017 struct nk_recti custom;
4018 struct nk_cursor cursors[NK_CURSOR_COUNT];
4019
4020 int glyph_count;
4021 struct nk_font_glyph *glyphs;
4022 struct nk_font *default_font;
4023 struct nk_font *fonts;
4024 struct nk_font_config *config;
4025 int font_num;
4026 };
4027
4028 /* some language glyph codepoint ranges */
4029 NK_API const nk_rune *nk_font_default_glyph_ranges(void);
4030 NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
4031 NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
4032 NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
4033
4034 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
4035 NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
4036 #endif
4037 NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
4038 NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator *persistent, struct nk_allocator *transient);
4039 NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
4040 NK_API struct nk_font_config nk_font_config(float pixel_height);
4041 NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
4042 #ifdef NK_INCLUDE_DEFAULT_FONT
4043 NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
4044 #endif
4045 NK_API struct nk_font* nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory, nk_size size, float height, const struct nk_font_config *config);
4046 #ifdef NK_INCLUDE_STANDARD_IO
4047 NK_API struct nk_font* nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path, float height, const struct nk_font_config*);
4048 #endif
4049 NK_API struct nk_font *nk_font_atlas_add_compressed(struct nk_font_atlas*, void *memory, nk_size size, float height, const struct nk_font_config*);
4050 NK_API struct nk_font* nk_font_atlas_add_compressed_base85(struct nk_font_atlas*, const char *data, float height, const struct nk_font_config *config);
4051 NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
4052 NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
4053 NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
4054 NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
4055 NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
4056
4057 #endif
4058
4059 /* ==============================================================
4060 *
4061 * MEMORY BUFFER
4062 *
4063 * ===============================================================*/
4064 /* A basic (double)-buffer with linear allocation and resetting as only
4065 freeing policy. The buffer's main purpose is to control all memory management
4066 inside the GUI toolkit and still leave memory control as much as possible in
4067 the hand of the user while also making sure the library is easy to use if
4068 not as much control is needed.
4069 In general all memory inside this library can be provided from the user in
4070 three different ways.
4071
4072 The first way and the one providing most control is by just passing a fixed
4073 size memory block. In this case all control lies in the hand of the user
4074 since he can exactly control where the memory comes from and how much memory
4075 the library should consume. Of course using the fixed size API removes the
4076 ability to automatically resize a buffer if not enough memory is provided so
4077 you have to take over the resizing. While being a fixed sized buffer sounds
4078 quite limiting, it is very effective in this library since the actual memory
4079 consumption is quite stable and has a fixed upper bound for a lot of cases.
4080
4081 If you don't want to think about how much memory the library should allocate
4082 at all time or have a very dynamic UI with unpredictable memory consumption
4083 habits but still want control over memory allocation you can use the dynamic
4084 allocator based API. The allocator consists of two callbacks for allocating
4085 and freeing memory and optional userdata so you can plugin your own allocator.
4086
4087 The final and easiest way can be used by defining
4088 NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
4089 allocation functions malloc and free and takes over complete control over
4090 memory in this library.
4091 */
4092 struct nk_memory_status {
4093 void *memory;
4094 unsigned int type;
4095 nk_size size;
4096 nk_size allocated;
4097 nk_size needed;
4098 nk_size calls;
4099 };
4100
4101 enum nk_allocation_type {
4102 NK_BUFFER_FIXED,
4103 NK_BUFFER_DYNAMIC
4104 };
4105
4106 enum nk_buffer_allocation_type {
4107 NK_BUFFER_FRONT,
4108 NK_BUFFER_BACK,
4109 NK_BUFFER_MAX
4110 };
4111
4112 struct nk_buffer_marker {
4113 int active;
4114 nk_size offset;
4115 };
4116
4117 struct nk_memory {void *ptr;nk_size size;};
4118 struct nk_buffer {
4119 struct nk_buffer_marker marker[NK_BUFFER_MAX];
4120 /* buffer marker to free a buffer to a certain offset */
4121 struct nk_allocator pool;
4122 /* allocator callback for dynamic buffers */
4123 enum nk_allocation_type type;
4124 /* memory management type */
4125 struct nk_memory memory;
4126 /* memory and size of the current memory block */
4127 float grow_factor;
4128 /* growing factor for dynamic memory management */
4129 nk_size allocated;
4130 /* total amount of memory allocated */
4131 nk_size needed;
4132 /* totally consumed memory given that enough memory is present */
4133 nk_size calls;
4134 /* number of allocation calls */
4135 nk_size size;
4136 /* current size of the buffer */
4137 };
4138
4139 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
4140 NK_API void nk_buffer_init_default(struct nk_buffer*);
4141 #endif
4142 NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
4143 NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
4144 NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
4145 NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
4146 NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
4147 NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
4148 NK_API void nk_buffer_clear(struct nk_buffer*);
4149 NK_API void nk_buffer_free(struct nk_buffer*);
4150 NK_API void *nk_buffer_memory(struct nk_buffer*);
4151 NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
4152 NK_API nk_size nk_buffer_total(struct nk_buffer*);
4153
4154 /* ==============================================================
4155 *
4156 * STRING
4157 *
4158 * ===============================================================*/
4159 /* Basic string buffer which is only used in context with the text editor
4160 * to manage and manipulate dynamic or fixed size string content. This is _NOT_
4161 * the default string handling method. The only instance you should have any contact
4162 * with this API is if you interact with an `nk_text_edit` object inside one of the
4163 * copy and paste functions and even there only for more advanced cases. */
4164 struct nk_str {
4165 struct nk_buffer buffer;
4166 int len; /* in codepoints/runes/glyphs */
4167 };
4168
4169 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
4170 NK_API void nk_str_init_default(struct nk_str*);
4171 #endif
4172 NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
4173 NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
4174 NK_API void nk_str_clear(struct nk_str*);
4175 NK_API void nk_str_free(struct nk_str*);
4176
4177 NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
4178 NK_API int nk_str_append_str_char(struct nk_str*, const char*);
4179 NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
4180 NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
4181 NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
4182 NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
4183
4184 NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
4185 NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
4186
4187 NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
4188 NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
4189 NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
4190 NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
4191 NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
4192 NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
4193
4194 NK_API void nk_str_remove_chars(struct nk_str*, int len);
4195 NK_API void nk_str_remove_runes(struct nk_str *str, int len);
4196 NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
4197 NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
4198
4199 NK_API char *nk_str_at_char(struct nk_str*, int pos);
4200 NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
4201 NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
4202 NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
4203 NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
4204
4205 NK_API char *nk_str_get(struct nk_str*);
4206 NK_API const char *nk_str_get_const(const struct nk_str*);
4207 NK_API int nk_str_len(struct nk_str*);
4208 NK_API int nk_str_len_char(struct nk_str*);
4209
4210 /*===============================================================
4211 *
4212 * TEXT EDITOR
4213 *
4214 * ===============================================================*/
4215 /* Editing text in this library is handled by either `nk_edit_string` or
4216 * `nk_edit_buffer`. But like almost everything in this library there are multiple
4217 * ways of doing it and a balance between control and ease of use with memory
4218 * as well as functionality controlled by flags.
4219 *
4220 * This library generally allows three different levels of memory control:
4221 * First of is the most basic way of just providing a simple char array with
4222 * string length. This method is probably the easiest way of handling simple
4223 * user text input. Main upside is complete control over memory while the biggest
4224 * downside in comparison with the other two approaches is missing undo/redo.
4225 *
4226 * For UIs that require undo/redo the second way was created. It is based on
4227 * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
4228 * This is mainly useful if you want something more like a text editor but don't want
4229 * to have a dynamically growing buffer.
4230 *
4231 * The final way is using a dynamically growing nk_text_edit struct, which
4232 * has both a default version if you don't care where memory comes from and an
4233 * allocator version if you do. While the text editor is quite powerful for its
4234 * complexity I would not recommend editing gigabytes of data with it.
4235 * It is rather designed for uses cases which make sense for a GUI library not for
4236 * an full blown text editor.
4237 */
4238 #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
4239 #define NK_TEXTEDIT_UNDOSTATECOUNT 99
4240 #endif
4241
4242 #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
4243 #define NK_TEXTEDIT_UNDOCHARCOUNT 999
4244 #endif
4245
4246 struct nk_text_edit;
4247 struct nk_clipboard {
4248 nk_handle userdata;
4249 nk_plugin_paste paste;
4250 nk_plugin_copy copy;
4251 };
4252
4253 struct nk_text_undo_record {
4254 int where;
4255 short insert_length;
4256 short delete_length;
4257 short char_storage;
4258 };
4259
4260 struct nk_text_undo_state {
4261 struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
4262 nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
4263 short undo_point;
4264 short redo_point;
4265 short undo_char_point;
4266 short redo_char_point;
4267 };
4268
4269 enum nk_text_edit_type {
4270 NK_TEXT_EDIT_SINGLE_LINE,
4271 NK_TEXT_EDIT_MULTI_LINE
4272 };
4273
4274 enum nk_text_edit_mode {
4275 NK_TEXT_EDIT_MODE_VIEW,
4276 NK_TEXT_EDIT_MODE_INSERT,
4277 NK_TEXT_EDIT_MODE_REPLACE
4278 };
4279
4280 struct nk_text_edit {
4281 struct nk_clipboard clip;
4282 struct nk_str string;
4283 nk_plugin_filter filter;
4284 struct nk_vec2 scrollbar;
4285
4286 int cursor;
4287 int select_start;
4288 int select_end;
4289 unsigned char mode;
4290 unsigned char cursor_at_end_of_line;
4291 unsigned char initialized;
4292 unsigned char has_preferred_x;
4293 unsigned char single_line;
4294 unsigned char active;
4295 unsigned char padding1;
4296 float preferred_x;
4297 struct nk_text_undo_state undo;
4298 };
4299
4300 /* filter function */
4301 NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
4302 NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
4303 NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
4304 NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
4305 NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
4306 NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
4307 NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
4308
4309 /* text editor */
4310 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
4311 NK_API void nk_textedit_init_default(struct nk_text_edit*);
4312 #endif
4313 NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
4314 NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
4315 NK_API void nk_textedit_free(struct nk_text_edit*);
4316 NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
4317 NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
4318 NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
4319 NK_API void nk_textedit_select_all(struct nk_text_edit*);
4320 NK_API int nk_textedit_cut(struct nk_text_edit*);
4321 NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
4322 NK_API void nk_textedit_undo(struct nk_text_edit*);
4323 NK_API void nk_textedit_redo(struct nk_text_edit*);
4324
4325 /* ===============================================================
4326 *
4327 * DRAWING
4328 *
4329 * ===============================================================*/
4330 /* This library was designed to be render backend agnostic so it does
4331 not draw anything to screen. Instead all drawn shapes, widgets
4332 are made of, are buffered into memory and make up a command queue.
4333 Each frame therefore fills the command buffer with draw commands
4334 that then need to be executed by the user and his own render backend.
4335 After that the command buffer needs to be cleared and a new frame can be
4336 started. It is probably important to note that the command buffer is the main
4337 drawing API and the optional vertex buffer API only takes this format and
4338 converts it into a hardware accessible format.
4339
4340 To use the command queue to draw your own widgets you can access the
4341 command buffer of each window by calling `nk_window_get_canvas` after
4342 previously having called `nk_begin`:
4343
4344 void draw_red_rectangle_widget(struct nk_context *ctx)
4345 {
4346 struct nk_command_buffer *canvas;
4347 struct nk_input *input = &ctx->input;
4348 canvas = nk_window_get_canvas(ctx);
4349
4350 struct nk_rect space;
4351 enum nk_widget_layout_states state;
4352 state = nk_widget(&space, ctx);
4353 if (!state) return;
4354
4355 if (state != NK_WIDGET_ROM)
4356 update_your_widget_by_user_input(...);
4357 nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
4358 }
4359
4360 if (nk_begin(...)) {
4361 nk_layout_row_dynamic(ctx, 25, 1);
4362 draw_red_rectangle_widget(ctx);
4363 }
4364 nk_end(..)
4365
4366 Important to know if you want to create your own widgets is the `nk_widget`
4367 call. It allocates space on the panel reserved for this widget to be used,
4368 but also returns the state of the widget space. If your widget is not seen and does
4369 not have to be updated it is '0' and you can just return. If it only has
4370 to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
4371 update and draw your widget. The reason for separating is to only draw and
4372 update what is actually necessary which is crucial for performance.
4373 */
4374 enum nk_command_type {
4375 NK_COMMAND_NOP,
4376 NK_COMMAND_SCISSOR,
4377 NK_COMMAND_LINE,
4378 NK_COMMAND_CURVE,
4379 NK_COMMAND_RECT,
4380 NK_COMMAND_RECT_FILLED,
4381 NK_COMMAND_RECT_MULTI_COLOR,
4382 NK_COMMAND_CIRCLE,
4383 NK_COMMAND_CIRCLE_FILLED,
4384 NK_COMMAND_ARC,
4385 NK_COMMAND_ARC_FILLED,
4386 NK_COMMAND_TRIANGLE,
4387 NK_COMMAND_TRIANGLE_FILLED,
4388 NK_COMMAND_POLYGON,
4389 NK_COMMAND_POLYGON_FILLED,
4390 NK_COMMAND_POLYLINE,
4391 NK_COMMAND_TEXT,
4392 NK_COMMAND_IMAGE,
4393 NK_COMMAND_CUSTOM
4394 };
4395
4396 /* command base and header of every command inside the buffer */
4397 struct nk_command {
4398 enum nk_command_type type;
4399 nk_size next;
4400 #ifdef NK_INCLUDE_COMMAND_USERDATA
4401 nk_handle userdata;
4402 #endif
4403 };
4404
4405 struct nk_command_scissor {
4406 struct nk_command header;
4407 short x, y;
4408 unsigned short w, h;
4409 };
4410
4411 struct nk_command_line {
4412 struct nk_command header;
4413 unsigned short line_thickness;
4414 struct nk_vec2i begin;
4415 struct nk_vec2i end;
4416 struct nk_color color;
4417 };
4418
4419 struct nk_command_curve {
4420 struct nk_command header;
4421 unsigned short line_thickness;
4422 struct nk_vec2i begin;
4423 struct nk_vec2i end;
4424 struct nk_vec2i ctrl[2];
4425 struct nk_color color;
4426 };
4427
4428 struct nk_command_rect {
4429 struct nk_command header;
4430 unsigned short rounding;
4431 unsigned short line_thickness;
4432 short x, y;
4433 unsigned short w, h;
4434 struct nk_color color;
4435 };
4436
4437 struct nk_command_rect_filled {
4438 struct nk_command header;
4439 unsigned short rounding;
4440 short x, y;
4441 unsigned short w, h;
4442 struct nk_color color;
4443 };
4444
4445 struct nk_command_rect_multi_color {
4446 struct nk_command header;
4447 short x, y;
4448 unsigned short w, h;
4449 struct nk_color left;
4450 struct nk_color top;
4451 struct nk_color bottom;
4452 struct nk_color right;
4453 };
4454
4455 struct nk_command_triangle {
4456 struct nk_command header;
4457 unsigned short line_thickness;
4458 struct nk_vec2i a;
4459 struct nk_vec2i b;
4460 struct nk_vec2i c;
4461 struct nk_color color;
4462 };
4463
4464 struct nk_command_triangle_filled {
4465 struct nk_command header;
4466 struct nk_vec2i a;
4467 struct nk_vec2i b;
4468 struct nk_vec2i c;
4469 struct nk_color color;
4470 };
4471
4472 struct nk_command_circle {
4473 struct nk_command header;
4474 short x, y;
4475 unsigned short line_thickness;
4476 unsigned short w, h;
4477 struct nk_color color;
4478 };
4479
4480 struct nk_command_circle_filled {
4481 struct nk_command header;
4482 short x, y;
4483 unsigned short w, h;
4484 struct nk_color color;
4485 };
4486
4487 struct nk_command_arc {
4488 struct nk_command header;
4489 short cx, cy;
4490 unsigned short r;
4491 unsigned short line_thickness;
4492 float a[2];
4493 struct nk_color color;
4494 };
4495
4496 struct nk_command_arc_filled {
4497 struct nk_command header;
4498 short cx, cy;
4499 unsigned short r;
4500 float a[2];
4501 struct nk_color color;
4502 };
4503
4504 struct nk_command_polygon {
4505 struct nk_command header;
4506 struct nk_color color;
4507 unsigned short line_thickness;
4508 unsigned short point_count;
4509 struct nk_vec2i points[1];
4510 };
4511
4512 struct nk_command_polygon_filled {
4513 struct nk_command header;
4514 struct nk_color color;
4515 unsigned short point_count;
4516 struct nk_vec2i points[1];
4517 };
4518
4519 struct nk_command_polyline {
4520 struct nk_command header;
4521 struct nk_color color;
4522 unsigned short line_thickness;
4523 unsigned short point_count;
4524 struct nk_vec2i points[1];
4525 };
4526
4527 struct nk_command_image {
4528 struct nk_command header;
4529 short x, y;
4530 unsigned short w, h;
4531 struct nk_image img;
4532 struct nk_color col;
4533 };
4534
4535 typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
4536 unsigned short w, unsigned short h, nk_handle callback_data);
4537 struct nk_command_custom {
4538 struct nk_command header;
4539 short x, y;
4540 unsigned short w, h;
4541 nk_handle callback_data;
4542 nk_command_custom_callback callback;
4543 };
4544
4545 struct nk_command_text {
4546 struct nk_command header;
4547 const struct nk_user_font *font;
4548 struct nk_color background;
4549 struct nk_color foreground;
4550 short x, y;
4551 unsigned short w, h;
4552 float height;
4553 int length;
4554 char string[1];
4555 };
4556
4557 enum nk_command_clipping {
4558 NK_CLIPPING_OFF = nk_false,
4559 NK_CLIPPING_ON = nk_true
4560 };
4561
4562 struct nk_command_buffer {
4563 struct nk_buffer *base;
4564 struct nk_rect clip;
4565 int use_clipping;
4566 nk_handle userdata;
4567 nk_size begin, end, last;
4568 };
4569
4570 /* shape outlines */
4571 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);
4572 NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
4573 NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
4574 NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
4575 NK_API void nk_stroke_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, float line_thickness, struct nk_color);
4576 NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
4577 NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
4578 NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
4579
4580 /* filled shades */
4581 NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
4582 NK_API void nk_fill_rect_multi_color(struct nk_command_buffer*, struct nk_rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
4583 NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
4584 NK_API void nk_fill_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, struct nk_color);
4585 NK_API void nk_fill_triangle(struct nk_command_buffer*, float x0, float y0, float x1, float y1, float x2, float y2, struct nk_color);
4586 NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
4587
4588 /* misc */
4589 NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
4590 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);
4591 NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
4592 NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
4593
4594 /* ===============================================================
4595 *
4596 * INPUT
4597 *
4598 * ===============================================================*/
4599 struct nk_mouse_button {
4600 int down;
4601 unsigned int clicked;
4602 struct nk_vec2 clicked_pos;
4603 };
4604 struct nk_mouse {
4605 struct nk_mouse_button buttons[NK_BUTTON_MAX];
4606 struct nk_vec2 pos;
4607 struct nk_vec2 prev;
4608 struct nk_vec2 delta;
4609 struct nk_vec2 scroll_delta;
4610 unsigned char grab;
4611 unsigned char grabbed;
4612 unsigned char ungrab;
4613 };
4614
4615 struct nk_key {
4616 int down;
4617 unsigned int clicked;
4618 };
4619 struct nk_keyboard {
4620 struct nk_key keys[NK_KEY_MAX];
4621 char text[NK_INPUT_MAX];
4622 int text_len;
4623 };
4624
4625 struct nk_input {
4626 struct nk_keyboard keyboard;
4627 struct nk_mouse mouse;
4628 };
4629
4630 NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
4631 NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
4632 NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
4633 NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
4634 NK_API int nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id, struct nk_rect b, int down);
4635 NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
4636 NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
4637 NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
4638 NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
4639 NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
4640 NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
4641 NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
4642 NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
4643 NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
4644 NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
4645
4646 /* ===============================================================
4647 *
4648 * DRAW LIST
4649 *
4650 * ===============================================================*/
4651 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
4652 /* The optional vertex buffer draw list provides a 2D drawing context
4653 with antialiasing functionality which takes basic filled or outlined shapes
4654 or a path and outputs vertexes, elements and draw commands.
4655 The actual draw list API is not required to be used directly while using this
4656 library since converting the default library draw command output is done by
4657 just calling `nk_convert` but I decided to still make this library accessible
4658 since it can be useful.
4659
4660 The draw list is based on a path buffering and polygon and polyline
4661 rendering API which allows a lot of ways to draw 2D content to screen.
4662 In fact it is probably more powerful than needed but allows even more crazy
4663 things than this library provides by default.
4664 */
4665 #ifdef NK_UINT_DRAW_INDEX
4666 typedef nk_uint nk_draw_index;
4667 #else
4668 typedef nk_ushort nk_draw_index;
4669 #endif
4670 enum nk_draw_list_stroke {
4671 NK_STROKE_OPEN = nk_false,
4672 /* build up path has no connection back to the beginning */
4673 NK_STROKE_CLOSED = nk_true
4674 /* build up path has a connection back to the beginning */
4675 };
4676
4677 enum nk_draw_vertex_layout_attribute {
4678 NK_VERTEX_POSITION,
4679 NK_VERTEX_COLOR,
4680 NK_VERTEX_TEXCOORD,
4681 NK_VERTEX_ATTRIBUTE_COUNT
4682 };
4683
4684 enum nk_draw_vertex_layout_format {
4685 NK_FORMAT_SCHAR,
4686 NK_FORMAT_SSHORT,
4687 NK_FORMAT_SINT,
4688 NK_FORMAT_UCHAR,
4689 NK_FORMAT_USHORT,
4690 NK_FORMAT_UINT,
4691 NK_FORMAT_FLOAT,
4692 NK_FORMAT_DOUBLE,
4693
4694 NK_FORMAT_COLOR_BEGIN,
4695 NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
4696 NK_FORMAT_R16G15B16,
4697 NK_FORMAT_R32G32B32,
4698
4699 NK_FORMAT_R8G8B8A8,
4700 NK_FORMAT_B8G8R8A8,
4701 NK_FORMAT_R16G15B16A16,
4702 NK_FORMAT_R32G32B32A32,
4703 NK_FORMAT_R32G32B32A32_FLOAT,
4704 NK_FORMAT_R32G32B32A32_DOUBLE,
4705
4706 NK_FORMAT_RGB32,
4707 NK_FORMAT_RGBA32,
4708 NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
4709 NK_FORMAT_COUNT
4710 };
4711
4712 #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
4713 struct nk_draw_vertex_layout_element {
4714 enum nk_draw_vertex_layout_attribute attribute;
4715 enum nk_draw_vertex_layout_format format;
4716 nk_size offset;
4717 };
4718
4719 struct nk_draw_command {
4720 unsigned int elem_count;
4721 /* number of elements in the current draw batch */
4722 struct nk_rect clip_rect;
4723 /* current screen clipping rectangle */
4724 nk_handle texture;
4725 /* current texture to set */
4726 #ifdef NK_INCLUDE_COMMAND_USERDATA
4727 nk_handle userdata;
4728 #endif
4729 };
4730
4731 struct nk_draw_list {
4732 struct nk_rect clip_rect;
4733 struct nk_vec2 circle_vtx[12];
4734 struct nk_convert_config config;
4735
4736 struct nk_buffer *buffer;
4737 struct nk_buffer *vertices;
4738 struct nk_buffer *elements;
4739
4740 unsigned int element_count;
4741 unsigned int vertex_count;
4742 unsigned int cmd_count;
4743 nk_size cmd_offset;
4744
4745 unsigned int path_count;
4746 unsigned int path_offset;
4747
4748 enum nk_anti_aliasing line_AA;
4749 enum nk_anti_aliasing shape_AA;
4750
4751 #ifdef NK_INCLUDE_COMMAND_USERDATA
4752 nk_handle userdata;
4753 #endif
4754 };
4755
4756 /* draw list */
4757 NK_API void nk_draw_list_init(struct nk_draw_list*);
4758 NK_API void nk_draw_list_setup(struct nk_draw_list*, const struct nk_convert_config*, struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements, enum nk_anti_aliasing line_aa,enum nk_anti_aliasing shape_aa);
4759
4760 /* drawing */
4761 #define nk_draw_list_foreach(cmd, can, b) for((cmd)=nk__draw_list_begin(can, b); (cmd)!=0; (cmd)=nk__draw_list_next(cmd, b, can))
4762 NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
4763 NK_API const struct nk_draw_command* nk__draw_list_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_draw_list*);
4764 NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
4765
4766 /* path */
4767 NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
4768 NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
4769 NK_API void nk_draw_list_path_arc_to_fast(struct nk_draw_list*, struct nk_vec2 center, float radius, int a_min, int a_max);
4770 NK_API void nk_draw_list_path_arc_to(struct nk_draw_list*, struct nk_vec2 center, float radius, float a_min, float a_max, unsigned int segments);
4771 NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
4772 NK_API void nk_draw_list_path_curve_to(struct nk_draw_list*, struct nk_vec2 p2, struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments);
4773 NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
4774 NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
4775
4776 /* stroke */
4777 NK_API void nk_draw_list_stroke_line(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_color, float thickness);
4778 NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
4779 NK_API void nk_draw_list_stroke_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color, float thickness);
4780 NK_API void nk_draw_list_stroke_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color, unsigned int segs, float thickness);
4781 NK_API void nk_draw_list_stroke_curve(struct nk_draw_list*, struct nk_vec2 p0, struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1, struct nk_color, unsigned int segments, float thickness);
4782 NK_API void nk_draw_list_stroke_poly_line(struct nk_draw_list*, const struct nk_vec2 *pnts, const unsigned int cnt, struct nk_color, enum nk_draw_list_stroke, float thickness, enum nk_anti_aliasing);
4783
4784 /* fill */
4785 NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
4786 NK_API void nk_draw_list_fill_rect_multi_color(struct nk_draw_list*, struct nk_rect rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
4787 NK_API void nk_draw_list_fill_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color);
4788 NK_API void nk_draw_list_fill_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color col, unsigned int segs);
4789 NK_API void nk_draw_list_fill_poly_convex(struct nk_draw_list*, const struct nk_vec2 *points, const unsigned int count, struct nk_color, enum nk_anti_aliasing);
4790
4791 /* misc */
4792 NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
4793 NK_API void nk_draw_list_add_text(struct nk_draw_list*, const struct nk_user_font*, struct nk_rect, const char *text, int len, float font_height, struct nk_color);
4794 #ifdef NK_INCLUDE_COMMAND_USERDATA
4795 NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
4796 #endif
4797
4798 #endif
4799
4800 /* ===============================================================
4801 *
4802 * GUI
4803 *
4804 * ===============================================================*/
4805 enum nk_style_item_type {
4806 NK_STYLE_ITEM_COLOR,
4807 NK_STYLE_ITEM_IMAGE
4808 };
4809
4810 union nk_style_item_data {
4811 struct nk_image image;
4812 struct nk_color color;
4813 };
4814
4815 struct nk_style_item {
4816 enum nk_style_item_type type;
4817 union nk_style_item_data data;
4818 };
4819
4820 struct nk_style_text {
4821 struct nk_color color;
4822 struct nk_vec2 padding;
4823 };
4824
4825 struct nk_style_button {
4826 /* background */
4827 struct nk_style_item normal;
4828 struct nk_style_item hover;
4829 struct nk_style_item active;
4830 struct nk_color border_color;
4831
4832 /* text */
4833 struct nk_color text_background;
4834 struct nk_color text_normal;
4835 struct nk_color text_hover;
4836 struct nk_color text_active;
4837 nk_flags text_alignment;
4838
4839 /* properties */
4840 float border;
4841 float rounding;
4842 struct nk_vec2 padding;
4843 struct nk_vec2 image_padding;
4844 struct nk_vec2 touch_padding;
4845
4846 /* optional user callbacks */
4847 nk_handle userdata;
4848 void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
4849 void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
4850 };
4851
4852 struct nk_style_toggle {
4853 /* background */
4854 struct nk_style_item normal;
4855 struct nk_style_item hover;
4856 struct nk_style_item active;
4857 struct nk_color border_color;
4858
4859 /* cursor */
4860 struct nk_style_item cursor_normal;
4861 struct nk_style_item cursor_hover;
4862
4863 /* text */
4864 struct nk_color text_normal;
4865 struct nk_color text_hover;
4866 struct nk_color text_active;
4867 struct nk_color text_background;
4868 nk_flags text_alignment;
4869
4870 /* properties */
4871 struct nk_vec2 padding;
4872 struct nk_vec2 touch_padding;
4873 float spacing;
4874 float border;
4875
4876 /* optional user callbacks */
4877 nk_handle userdata;
4878 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
4879 void(*draw_end)(struct nk_command_buffer*, nk_handle);
4880 };
4881
4882 struct nk_style_selectable {
4883 /* background (inactive) */
4884 struct nk_style_item normal;
4885 struct nk_style_item hover;
4886 struct nk_style_item pressed;
4887
4888 /* background (active) */
4889 struct nk_style_item normal_active;
4890 struct nk_style_item hover_active;
4891 struct nk_style_item pressed_active;
4892
4893 /* text color (inactive) */
4894 struct nk_color text_normal;
4895 struct nk_color text_hover;
4896 struct nk_color text_pressed;
4897
4898 /* text color (active) */
4899 struct nk_color text_normal_active;
4900 struct nk_color text_hover_active;
4901 struct nk_color text_pressed_active;
4902 struct nk_color text_background;
4903 nk_flags text_alignment;
4904
4905 /* properties */
4906 float rounding;
4907 struct nk_vec2 padding;
4908 struct nk_vec2 touch_padding;
4909 struct nk_vec2 image_padding;
4910
4911 /* optional user callbacks */
4912 nk_handle userdata;
4913 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
4914 void(*draw_end)(struct nk_command_buffer*, nk_handle);
4915 };
4916
4917 struct nk_style_slider {
4918 /* background */
4919 struct nk_style_item normal;
4920 struct nk_style_item hover;
4921 struct nk_style_item active;
4922 struct nk_color border_color;
4923
4924 /* background bar */
4925 struct nk_color bar_normal;
4926 struct nk_color bar_hover;
4927 struct nk_color bar_active;
4928 struct nk_color bar_filled;
4929
4930 /* cursor */
4931 struct nk_style_item cursor_normal;
4932 struct nk_style_item cursor_hover;
4933 struct nk_style_item cursor_active;
4934
4935 /* properties */
4936 float border;
4937 float rounding;
4938 float bar_height;
4939 struct nk_vec2 padding;
4940 struct nk_vec2 spacing;
4941 struct nk_vec2 cursor_size;
4942
4943 /* optional buttons */
4944 int show_buttons;
4945 struct nk_style_button inc_button;
4946 struct nk_style_button dec_button;
4947 enum nk_symbol_type inc_symbol;
4948 enum nk_symbol_type dec_symbol;
4949
4950 /* optional user callbacks */
4951 nk_handle userdata;
4952 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
4953 void(*draw_end)(struct nk_command_buffer*, nk_handle);
4954 };
4955
4956 struct nk_style_progress {
4957 /* background */
4958 struct nk_style_item normal;
4959 struct nk_style_item hover;
4960 struct nk_style_item active;
4961 struct nk_color border_color;
4962
4963 /* cursor */
4964 struct nk_style_item cursor_normal;
4965 struct nk_style_item cursor_hover;
4966 struct nk_style_item cursor_active;
4967 struct nk_color cursor_border_color;
4968
4969 /* properties */
4970 float rounding;
4971 float border;
4972 float cursor_border;
4973 float cursor_rounding;
4974 struct nk_vec2 padding;
4975
4976 /* optional user callbacks */
4977 nk_handle userdata;
4978 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
4979 void(*draw_end)(struct nk_command_buffer*, nk_handle);
4980 };
4981
4982 struct nk_style_scrollbar {
4983 /* background */
4984 struct nk_style_item normal;
4985 struct nk_style_item hover;
4986 struct nk_style_item active;
4987 struct nk_color border_color;
4988
4989 /* cursor */
4990 struct nk_style_item cursor_normal;
4991 struct nk_style_item cursor_hover;
4992 struct nk_style_item cursor_active;
4993 struct nk_color cursor_border_color;
4994
4995 /* properties */
4996 float border;
4997 float rounding;
4998 float border_cursor;
4999 float rounding_cursor;
5000 struct nk_vec2 padding;
5001
5002 /* optional buttons */
5003 int show_buttons;
5004 struct nk_style_button inc_button;
5005 struct nk_style_button dec_button;
5006 enum nk_symbol_type inc_symbol;
5007 enum nk_symbol_type dec_symbol;
5008
5009 /* optional user callbacks */
5010 nk_handle userdata;
5011 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
5012 void(*draw_end)(struct nk_command_buffer*, nk_handle);
5013 };
5014
5015 struct nk_style_edit {
5016 /* background */
5017 struct nk_style_item normal;
5018 struct nk_style_item hover;
5019 struct nk_style_item active;
5020 struct nk_color border_color;
5021 struct nk_style_scrollbar scrollbar;
5022
5023 /* cursor */
5024 struct nk_color cursor_normal;
5025 struct nk_color cursor_hover;
5026 struct nk_color cursor_text_normal;
5027 struct nk_color cursor_text_hover;
5028
5029 /* text (unselected) */
5030 struct nk_color text_normal;
5031 struct nk_color text_hover;
5032 struct nk_color text_active;
5033
5034 /* text (selected) */
5035 struct nk_color selected_normal;
5036 struct nk_color selected_hover;
5037 struct nk_color selected_text_normal;
5038 struct nk_color selected_text_hover;
5039
5040 /* properties */
5041 float border;
5042 float rounding;
5043 float cursor_size;
5044 struct nk_vec2 scrollbar_size;
5045 struct nk_vec2 padding;
5046 float row_padding;
5047 };
5048
5049 struct nk_style_property {
5050 /* background */
5051 struct nk_style_item normal;
5052 struct nk_style_item hover;
5053 struct nk_style_item active;
5054 struct nk_color border_color;
5055
5056 /* text */
5057 struct nk_color label_normal;
5058 struct nk_color label_hover;
5059 struct nk_color label_active;
5060
5061 /* symbols */
5062 enum nk_symbol_type sym_left;
5063 enum nk_symbol_type sym_right;
5064
5065 /* properties */
5066 float border;
5067 float rounding;
5068 struct nk_vec2 padding;
5069
5070 struct nk_style_edit edit;
5071 struct nk_style_button inc_button;
5072 struct nk_style_button dec_button;
5073
5074 /* optional user callbacks */
5075 nk_handle userdata;
5076 void(*draw_begin)(struct nk_command_buffer*, nk_handle);
5077 void(*draw_end)(struct nk_command_buffer*, nk_handle);
5078 };
5079
5080 struct nk_style_chart {
5081 /* colors */
5082 struct nk_style_item background;
5083 struct nk_color border_color;
5084 struct nk_color selected_color;
5085 struct nk_color color;
5086
5087 /* properties */
5088 float border;
5089 float rounding;
5090 struct nk_vec2 padding;
5091 };
5092
5093 struct nk_style_combo {
5094 /* background */
5095 struct nk_style_item normal;
5096 struct nk_style_item hover;
5097 struct nk_style_item active;
5098 struct nk_color border_color;
5099
5100 /* label */
5101 struct nk_color label_normal;
5102 struct nk_color label_hover;
5103 struct nk_color label_active;
5104
5105 /* symbol */
5106 struct nk_color symbol_normal;
5107 struct nk_color symbol_hover;
5108 struct nk_color symbol_active;
5109
5110 /* button */
5111 struct nk_style_button button;
5112 enum nk_symbol_type sym_normal;
5113 enum nk_symbol_type sym_hover;
5114 enum nk_symbol_type sym_active;
5115
5116 /* properties */
5117 float border;
5118 float rounding;
5119 struct nk_vec2 content_padding;
5120 struct nk_vec2 button_padding;
5121 struct nk_vec2 spacing;
5122 };
5123
5124 struct nk_style_tab {
5125 /* background */
5126 struct nk_style_item background;
5127 struct nk_color border_color;
5128 struct nk_color text;
5129
5130 /* button */
5131 struct nk_style_button tab_maximize_button;
5132 struct nk_style_button tab_minimize_button;
5133 struct nk_style_button node_maximize_button;
5134 struct nk_style_button node_minimize_button;
5135 enum nk_symbol_type sym_minimize;
5136 enum nk_symbol_type sym_maximize;
5137
5138 /* properties */
5139 float border;
5140 float rounding;
5141 float indent;
5142 struct nk_vec2 padding;
5143 struct nk_vec2 spacing;
5144 };
5145
5146 enum nk_style_header_align {
5147 NK_HEADER_LEFT,
5148 NK_HEADER_RIGHT
5149 };
5150 struct nk_style_window_header {
5151 /* background */
5152 struct nk_style_item normal;
5153 struct nk_style_item hover;
5154 struct nk_style_item active;
5155
5156 /* button */
5157 struct nk_style_button close_button;
5158 struct nk_style_button minimize_button;
5159 enum nk_symbol_type close_symbol;
5160 enum nk_symbol_type minimize_symbol;
5161 enum nk_symbol_type maximize_symbol;
5162
5163 /* title */
5164 struct nk_color label_normal;
5165 struct nk_color label_hover;
5166 struct nk_color label_active;
5167
5168 /* properties */
5169 enum nk_style_header_align align;
5170 struct nk_vec2 padding;
5171 struct nk_vec2 label_padding;
5172 struct nk_vec2 spacing;
5173 };
5174
5175 struct nk_style_window {
5176 struct nk_style_window_header header;
5177 struct nk_style_item fixed_background;
5178 struct nk_color background;
5179
5180 struct nk_color border_color;
5181 struct nk_color popup_border_color;
5182 struct nk_color combo_border_color;
5183 struct nk_color contextual_border_color;
5184 struct nk_color menu_border_color;
5185 struct nk_color group_border_color;
5186 struct nk_color tooltip_border_color;
5187 struct nk_style_item scaler;
5188
5189 float border;
5190 float combo_border;
5191 float contextual_border;
5192 float menu_border;
5193 float group_border;
5194 float tooltip_border;
5195 float popup_border;
5196 float min_row_height_padding;
5197
5198 float rounding;
5199 struct nk_vec2 spacing;
5200 struct nk_vec2 scrollbar_size;
5201 struct nk_vec2 min_size;
5202
5203 struct nk_vec2 padding;
5204 struct nk_vec2 group_padding;
5205 struct nk_vec2 popup_padding;
5206 struct nk_vec2 combo_padding;
5207 struct nk_vec2 contextual_padding;
5208 struct nk_vec2 menu_padding;
5209 struct nk_vec2 tooltip_padding;
5210 };
5211
5212 struct nk_style {
5213 const struct nk_user_font *font;
5214 const struct nk_cursor *cursors[NK_CURSOR_COUNT];
5215 const struct nk_cursor *cursor_active;
5216 struct nk_cursor *cursor_last;
5217 int cursor_visible;
5218
5219 struct nk_style_text text;
5220 struct nk_style_button button;
5221 struct nk_style_button contextual_button;
5222 struct nk_style_button menu_button;
5223 struct nk_style_toggle option;
5224 struct nk_style_toggle checkbox;
5225 struct nk_style_selectable selectable;
5226 struct nk_style_slider slider;
5227 struct nk_style_progress progress;
5228 struct nk_style_property property;
5229 struct nk_style_edit edit;
5230 struct nk_style_chart chart;
5231 struct nk_style_scrollbar scrollh;
5232 struct nk_style_scrollbar scrollv;
5233 struct nk_style_tab tab;
5234 struct nk_style_combo combo;
5235 struct nk_style_window window;
5236 };
5237
5238 NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
5239 NK_API struct nk_style_item nk_style_item_color(struct nk_color);
5240 NK_API struct nk_style_item nk_style_item_hide(void);
5241
5242 /*==============================================================
5243 * PANEL
5244 * =============================================================*/
5245 #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
5246 #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
5247 #endif
5248 #ifndef NK_CHART_MAX_SLOT
5249 #define NK_CHART_MAX_SLOT 4
5250 #endif
5251
5252 enum nk_panel_type {
5253 NK_PANEL_NONE = 0,
5254 NK_PANEL_WINDOW = NK_FLAG(0),
5255 NK_PANEL_GROUP = NK_FLAG(1),
5256 NK_PANEL_POPUP = NK_FLAG(2),
5257 NK_PANEL_CONTEXTUAL = NK_FLAG(4),
5258 NK_PANEL_COMBO = NK_FLAG(5),
5259 NK_PANEL_MENU = NK_FLAG(6),
5260 NK_PANEL_TOOLTIP = NK_FLAG(7)
5261 };
5262 enum nk_panel_set {
5263 NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
5264 NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
5265 NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
5266 };
5267
5268 struct nk_chart_slot {
5269 enum nk_chart_type type;
5270 struct nk_color color;
5271 struct nk_color highlight;
5272 float min, max, range;
5273 int count;
5274 struct nk_vec2 last;
5275 int index;
5276 };
5277
5278 struct nk_chart {
5279 int slot;
5280 float x, y, w, h;
5281 struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
5282 };
5283
5284 enum nk_panel_row_layout_type {
5285 NK_LAYOUT_DYNAMIC_FIXED = 0,
5286 NK_LAYOUT_DYNAMIC_ROW,
5287 NK_LAYOUT_DYNAMIC_FREE,
5288 NK_LAYOUT_DYNAMIC,
5289 NK_LAYOUT_STATIC_FIXED,
5290 NK_LAYOUT_STATIC_ROW,
5291 NK_LAYOUT_STATIC_FREE,
5292 NK_LAYOUT_STATIC,
5293 NK_LAYOUT_TEMPLATE,
5294 NK_LAYOUT_COUNT
5295 };
5296 struct nk_row_layout {
5297 enum nk_panel_row_layout_type type;
5298 int index;
5299 float height;
5300 float min_height;
5301 int columns;
5302 const float *ratio;
5303 float item_width;
5304 float item_height;
5305 float item_offset;
5306 float filled;
5307 struct nk_rect item;
5308 int tree_depth;
5309 float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
5310 };
5311
5312 struct nk_popup_buffer {
5313 nk_size begin;
5314 nk_size parent;
5315 nk_size last;
5316 nk_size end;
5317 int active;
5318 };
5319
5320 struct nk_menu_state {
5321 float x, y, w, h;
5322 struct nk_scroll offset;
5323 };
5324
5325 struct nk_panel {
5326 enum nk_panel_type type;
5327 nk_flags flags;
5328 struct nk_rect bounds;
5329 nk_uint *offset_x;
5330 nk_uint *offset_y;
5331 float at_x, at_y, max_x;
5332 float footer_height;
5333 float header_height;
5334 float border;
5335 unsigned int has_scrolling;
5336 struct nk_rect clip;
5337 struct nk_menu_state menu;
5338 struct nk_row_layout row;
5339 struct nk_chart chart;
5340 struct nk_command_buffer *buffer;
5341 struct nk_panel *parent;
5342 };
5343
5344 /*==============================================================
5345 * WINDOW
5346 * =============================================================*/
5347 #ifndef NK_WINDOW_MAX_NAME
5348 #define NK_WINDOW_MAX_NAME 64
5349 #endif
5350
5351 struct nk_table;
5352 enum nk_window_flags {
5353 NK_WINDOW_PRIVATE = NK_FLAG(11),
5354 NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
5355 /* special window type growing up in height while being filled to a certain maximum height */
5356 NK_WINDOW_ROM = NK_FLAG(12),
5357 /* sets window widgets into a read only mode and does not allow input changes */
5358 NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT,
5359 /* prevents all interaction caused by input to either window or widgets inside */
5360 NK_WINDOW_HIDDEN = NK_FLAG(13),
5361 /* Hides window and stops any window interaction and drawing */
5362 NK_WINDOW_CLOSED = NK_FLAG(14),
5363 /* Directly closes and frees the window at the end of the frame */
5364 NK_WINDOW_MINIMIZED = NK_FLAG(15),
5365 /* marks the window as minimized */
5366 NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
5367 /* Removes read only mode at the end of the window */
5368 };
5369
5370 struct nk_popup_state {
5371 struct nk_window *win;
5372 enum nk_panel_type type;
5373 struct nk_popup_buffer buf;
5374 nk_hash name;
5375 int active;
5376 unsigned combo_count;
5377 unsigned con_count, con_old;
5378 unsigned active_con;
5379 struct nk_rect header;
5380 };
5381
5382 struct nk_edit_state {
5383 nk_hash name;
5384 unsigned int seq;
5385 unsigned int old;
5386 int active, prev;
5387 int cursor;
5388 int sel_start;
5389 int sel_end;
5390 struct nk_scroll scrollbar;
5391 unsigned char mode;
5392 unsigned char single_line;
5393 };
5394
5395 struct nk_property_state {
5396 int active, prev;
5397 char buffer[NK_MAX_NUMBER_BUFFER];
5398 int length;
5399 int cursor;
5400 int select_start;
5401 int select_end;
5402 nk_hash name;
5403 unsigned int seq;
5404 unsigned int old;
5405 int state;
5406 };
5407
5408 struct nk_window {
5409 unsigned int seq;
5410 nk_hash name;
5411 char name_string[NK_WINDOW_MAX_NAME];
5412 nk_flags flags;
5413
5414 struct nk_rect bounds;
5415 struct nk_scroll scrollbar;
5416 struct nk_command_buffer buffer;
5417 struct nk_panel *layout;
5418 float scrollbar_hiding_timer;
5419
5420 /* persistent widget state */
5421 struct nk_property_state property;
5422 struct nk_popup_state popup;
5423 struct nk_edit_state edit;
5424 unsigned int scrolled;
5425
5426 struct nk_table *tables;
5427 unsigned int table_count;
5428
5429 /* window list hooks */
5430 struct nk_window *next;
5431 struct nk_window *prev;
5432 struct nk_window *parent;
5433 };
5434
5435 /*==============================================================
5436 * STACK
5437 * =============================================================*/
5438 /* The style modifier stack can be used to temporarily change a
5439 * property inside `nk_style`. For example if you want a special
5440 * red button you can temporarily push the old button color onto a stack
5441 * draw the button with a red color and then you just pop the old color
5442 * back from the stack:
5443 *
5444 * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
5445 * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
5446 * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
5447 * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
5448 *
5449 * nk_button(...);
5450 *
5451 * nk_style_pop_style_item(ctx);
5452 * nk_style_pop_style_item(ctx);
5453 * nk_style_pop_style_item(ctx);
5454 * nk_style_pop_vec2(ctx);
5455 *
5456 * Nuklear has a stack for style_items, float properties, vector properties,
5457 * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
5458 * which can be changed at compile time.
5459 */
5460 #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
5461 #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
5462 #endif
5463
5464 #ifndef NK_FONT_STACK_SIZE
5465 #define NK_FONT_STACK_SIZE 8
5466 #endif
5467
5468 #ifndef NK_STYLE_ITEM_STACK_SIZE
5469 #define NK_STYLE_ITEM_STACK_SIZE 16
5470 #endif
5471
5472 #ifndef NK_FLOAT_STACK_SIZE
5473 #define NK_FLOAT_STACK_SIZE 32
5474 #endif
5475
5476 #ifndef NK_VECTOR_STACK_SIZE
5477 #define NK_VECTOR_STACK_SIZE 16
5478 #endif
5479
5480 #ifndef NK_FLAGS_STACK_SIZE
5481 #define NK_FLAGS_STACK_SIZE 32
5482 #endif
5483
5484 #ifndef NK_COLOR_STACK_SIZE
5485 #define NK_COLOR_STACK_SIZE 32
5486 #endif
5487
5488 #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
5489 struct nk_config_stack_##name##_element {\
5490 prefix##_##type *address;\
5491 prefix##_##type old_value;\
5492 }
5493 #define NK_CONFIG_STACK(type,size)\
5494 struct nk_config_stack_##type {\
5495 int head;\
5496 struct nk_config_stack_##type##_element elements[size];\
5497 }
5498
5499 #define nk_float float
5500 NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
5501 NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
5502 NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
5503 NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
5504 NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
5505 NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
5506 NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
5507
5508 NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
5509 NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
5510 NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
5511 NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
5512 NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
5513 NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
5514 NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
5515
5516 struct nk_configuration_stacks {
5517 struct nk_config_stack_style_item style_items;
5518 struct nk_config_stack_float floats;
5519 struct nk_config_stack_vec2 vectors;
5520 struct nk_config_stack_flags flags;
5521 struct nk_config_stack_color colors;
5522 struct nk_config_stack_user_font fonts;
5523 struct nk_config_stack_button_behavior button_behaviors;
5524 };
5525
5526 /*==============================================================
5527 * CONTEXT
5528 * =============================================================*/
5529 #define NK_VALUE_PAGE_CAPACITY \
5530 (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
5531
5532 struct nk_table {
5533 unsigned int seq;
5534 unsigned int size;
5535 nk_hash keys[NK_VALUE_PAGE_CAPACITY];
5536 nk_uint values[NK_VALUE_PAGE_CAPACITY];
5537 struct nk_table *next, *prev;
5538 };
5539
5540 union nk_page_data {
5541 struct nk_table tbl;
5542 struct nk_panel pan;
5543 struct nk_window win;
5544 };
5545
5546 struct nk_page_element {
5547 union nk_page_data data;
5548 struct nk_page_element *next;
5549 struct nk_page_element *prev;
5550 };
5551
5552 struct nk_page {
5553 unsigned int size;
5554 struct nk_page *next;
5555 struct nk_page_element win[1];
5556 };
5557
5558 struct nk_pool {
5559 struct nk_allocator alloc;
5560 enum nk_allocation_type type;
5561 unsigned int page_count;
5562 struct nk_page *pages;
5563 struct nk_page_element *freelist;
5564 unsigned capacity;
5565 nk_size size;
5566 nk_size cap;
5567 };
5568
5569 struct nk_context {
5570 /* public: can be accessed freely */
5571 struct nk_input input;
5572 struct nk_style style;
5573 struct nk_buffer memory;
5574 struct nk_clipboard clip;
5575 nk_flags last_widget_state;
5576 enum nk_button_behavior button_behavior;
5577 struct nk_configuration_stacks stacks;
5578 float delta_time_seconds;
5579
5580 /* private:
5581 should only be accessed if you
5582 know what you are doing */
5583 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
5584 struct nk_draw_list draw_list;
5585 #endif
5586 #ifdef NK_INCLUDE_COMMAND_USERDATA
5587 nk_handle userdata;
5588 #endif
5589 /* text editor objects are quite big because of an internal
5590 * undo/redo stack. Therefore it does not make sense to have one for
5591 * each window for temporary use cases, so I only provide *one* instance
5592 * for all windows. This works because the content is cleared anyway */
5593 struct nk_text_edit text_edit;
5594 /* draw buffer used for overlay drawing operation like cursor */
5595 struct nk_command_buffer overlay;
5596
5597 /* windows */
5598 int build;
5599 int use_pool;
5600 struct nk_pool pool;
5601 struct nk_window *begin;
5602 struct nk_window *end;
5603 struct nk_window *active;
5604 struct nk_window *current;
5605 struct nk_page_element *freelist;
5606 unsigned int count;
5607 unsigned int seq;
5608 };
5609
5610 /* ==============================================================
5611 * MATH
5612 * =============================================================== */
5613 #define NK_PI 3.141592654f
5614 #define NK_UTF_INVALID 0xFFFD
5615 #define NK_MAX_FLOAT_PRECISION 2
5616
5617 #define NK_UNUSED(x) ((void)(x))
5618 #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
5619 #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
5620 #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
5621 #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
5622 #define NK_INBOX(px, py, x, y, w, h)\
5623 (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
5624 #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
5625 ((x1 < (x0 + w0)) && (x0 < (x1 + w1)) && \
5626 (y1 < (y0 + h0)) && (y0 < (y1 + h1)))
5627 #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
5628 (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
5629
5630 #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
5631 #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
5632 #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
5633 #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
5634
5635 #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
5636 #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
5637 #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
5638
5639 /* ==============================================================
5640 * ALIGNMENT
5641 * =============================================================== */
5642 /* Pointer to Integer type conversion for pointer alignment */
5643 #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
5644 # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
5645 # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
5646 #elif !defined(__GNUC__) /* works for compilers other than LLVM */
5647 # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
5648 # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
5649 #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
5650 # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
5651 # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
5652 #else /* generates warning but works */
5653 # define NK_UINT_TO_PTR(x) ((void*)(x))
5654 # define NK_PTR_TO_UINT(x) ((nk_size)(x))
5655 #endif
5656
5657 #define NK_ALIGN_PTR(x, mask)\
5658 (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
5659 #define NK_ALIGN_PTR_BACK(x, mask)\
5660 (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
5661
5662 #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
5663 #define NK_CONTAINER_OF(ptr,type,member)\
5664 (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
5665
5666 #ifdef __cplusplus
5667 }
5668 #endif
5669
5670 #ifdef __cplusplus
5671 template<typename T> struct nk_alignof;
5672 template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
5673 template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
5674 template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
5675 diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
5676 #define NK_ALIGNOF(t) (nk_alignof<t>::value)
5677 #elif defined(_MSC_VER)
5678 #define NK_ALIGNOF(t) (__alignof(t))
5679 #else
5680 #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
5681 #endif
5682
5683 #endif /* NK_NUKLEAR_H_ */
5684
5685 #ifdef NK_IMPLEMENTATION
5686
5687 #ifndef NK_INTERNAL_H
5688 #define NK_INTERNAL_H
5689
5690 #ifndef NK_POOL_DEFAULT_CAPACITY
5691 #define NK_POOL_DEFAULT_CAPACITY 16
5692 #endif
5693
5694 #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
5695 #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
5696 #endif
5697
5698 #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
5699 #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
5700 #endif
5701
5702 /* standard library headers */
5703 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
5704 #include <stdlib.h> /* malloc, free */
5705 #endif
5706 #ifdef NK_INCLUDE_STANDARD_IO
5707 #include <stdio.h> /* fopen, fclose,... */
5708 #endif
5709 #ifdef NK_INCLUDE_STANDARD_VARARGS
5710 #include <stdarg.h> /* valist, va_start, va_end, ... */
5711 #endif
5712 #ifndef NK_ASSERT
5713 #include <assert.h>
5714 #define NK_ASSERT(expr) assert(expr)
5715 #endif
5716
5717 #ifndef NK_MEMSET
5718 #define NK_MEMSET nk_memset
5719 #endif
5720 #ifndef NK_MEMCPY
5721 #define NK_MEMCPY nk_memcopy
5722 #endif
5723 #ifndef NK_SQRT
5724 #define NK_SQRT nk_sqrt
5725 #endif
5726 #ifndef NK_SIN
5727 #define NK_SIN nk_sin
5728 #endif
5729 #ifndef NK_COS
5730 #define NK_COS nk_cos
5731 #endif
5732 #ifndef NK_STRTOD
5733 #define NK_STRTOD nk_strtod
5734 #endif
5735 #ifndef NK_DTOA
5736 #define NK_DTOA nk_dtoa
5737 #endif
5738
5739 #define NK_DEFAULT (-1)
5740
5741 #ifndef NK_VSNPRINTF
5742 /* If your compiler does support `vsnprintf` I would highly recommend
5743 * defining this to vsnprintf instead since `vsprintf` is basically
5744 * unbelievable unsafe and should *NEVER* be used. But I have to support
5745 * it since C89 only provides this unsafe version. */
5746 #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
5747 (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
5748 (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
5749 (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
5750 defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
5751 #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
5752 #else
5753 #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
5754 #endif
5755 #endif
5756
5757 #define NK_SCHAR_MIN (-127)
5758 #define NK_SCHAR_MAX 127
5759 #define NK_UCHAR_MIN 0
5760 #define NK_UCHAR_MAX 256
5761 #define NK_SSHORT_MIN (-32767)
5762 #define NK_SSHORT_MAX 32767
5763 #define NK_USHORT_MIN 0
5764 #define NK_USHORT_MAX 65535
5765 #define NK_SINT_MIN (-2147483647)
5766 #define NK_SINT_MAX 2147483647
5767 #define NK_UINT_MIN 0
5768 #define NK_UINT_MAX 4294967295u
5769
5770 /* Make sure correct type size:
5771 * This will fire with a negative subscript error if the type sizes
5772 * are set incorrectly by the compiler, and compile out if not */
5773 NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
5774 NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
5775 NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
5776 NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
5777 NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
5778 NK_STATIC_ASSERT(sizeof(nk_short) == 2);
5779 NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
5780 NK_STATIC_ASSERT(sizeof(nk_int) == 4);
5781 NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
5782
5783 NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
5784 #define NK_FLOAT_PRECISION 0.00000000000001
5785
5786 NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
5787 NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
5788 NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
5789 NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
5790 NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
5791 NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
5792
5793 /* widget */
5794 #define nk_widget_state_reset(s)\
5795 if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
5796 (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
5797 else (*(s)) = NK_WIDGET_STATE_INACTIVE;
5798
5799 /* math */
5800 NK_LIB float nk_inv_sqrt(float n);
5801 NK_LIB float nk_sqrt(float x);
5802 NK_LIB float nk_sin(float x);
5803 NK_LIB float nk_cos(float x);
5804 NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
5805 NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
5806 NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
5807 NK_LIB void nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0, float x1, float y1);
5808 NK_LIB double nk_pow(double x, int n);
5809 NK_LIB int nk_ifloord(double x);
5810 NK_LIB int nk_ifloorf(float x);
5811 NK_LIB int nk_iceilf(float x);
5812 NK_LIB int nk_log10(double n);
5813
5814 /* util */
5815 enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
5816 NK_LIB int nk_is_lower(int c);
5817 NK_LIB int nk_is_upper(int c);
5818 NK_LIB int nk_to_upper(int c);
5819 NK_LIB int nk_to_lower(int c);
5820 NK_LIB void* nk_memcopy(void *dst, const void *src, nk_size n);
5821 NK_LIB void nk_memset(void *ptr, int c0, nk_size size);
5822 NK_LIB void nk_zero(void *ptr, nk_size size);
5823 NK_LIB char *nk_itoa(char *s, long n);
5824 NK_LIB int nk_string_float_limit(char *string, int prec);
5825 NK_LIB char *nk_dtoa(char *s, double n);
5826 NK_LIB int nk_text_clamp(const struct nk_user_font *font, const char *text, int text_len, float space, int *glyphs, float *text_width, nk_rune *sep_list, int sep_count);
5827 NK_LIB struct nk_vec2 nk_text_calculate_text_bounds(const struct nk_user_font *font, const char *begin, int byte_len, float row_height, const char **remaining, struct nk_vec2 *out_offset, int *glyphs, int op);
5828 #ifdef NK_INCLUDE_STANDARD_VARARGS
5829 NK_LIB int nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args);
5830 #endif
5831 #ifdef NK_INCLUDE_STANDARD_IO
5832 NK_LIB char *nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc);
5833 #endif
5834
5835 /* buffer */
5836 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
5837 NK_LIB void* nk_malloc(nk_handle unused, void *old,nk_size size);
5838 NK_LIB void nk_mfree(nk_handle unused, void *ptr);
5839 #endif
5840 NK_LIB void* nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment, enum nk_buffer_allocation_type type);
5841 NK_LIB void* nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type, nk_size size, nk_size align);
5842 NK_LIB void* nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size);
5843
5844 /* draw */
5845 NK_LIB void nk_command_buffer_init(struct nk_command_buffer *cb, struct nk_buffer *b, enum nk_command_clipping clip);
5846 NK_LIB void nk_command_buffer_reset(struct nk_command_buffer *b);
5847 NK_LIB void* nk_command_buffer_push(struct nk_command_buffer* b, enum nk_command_type t, nk_size size);
5848 NK_LIB void nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type, struct nk_rect content, struct nk_color background, struct nk_color foreground, float border_width, const struct nk_user_font *font);
5849
5850 /* buffering */
5851 NK_LIB void nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
5852 NK_LIB void nk_start(struct nk_context *ctx, struct nk_window *win);
5853 NK_LIB void nk_start_popup(struct nk_context *ctx, struct nk_window *win);
5854 NK_LIB void nk_finish_popup(struct nk_context *ctx, struct nk_window*);
5855 NK_LIB void nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
5856 NK_LIB void nk_finish(struct nk_context *ctx, struct nk_window *w);
5857 NK_LIB void nk_build(struct nk_context *ctx);
5858
5859 /* text editor */
5860 NK_LIB void nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type, nk_plugin_filter filter);
5861 NK_LIB void nk_textedit_click(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
5862 NK_LIB void nk_textedit_drag(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
5863 NK_LIB void nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod, const struct nk_user_font *font, float row_height);
5864
5865 /* window */
5866 enum nk_window_insert_location {
5867 NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
5868 NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
5869 };
5870 NK_LIB void *nk_create_window(struct nk_context *ctx);
5871 NK_LIB void nk_remove_window(struct nk_context*, struct nk_window*);
5872 NK_LIB void nk_free_window(struct nk_context *ctx, struct nk_window *win);
5873 NK_LIB struct nk_window *nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name);
5874 NK_LIB void nk_insert_window(struct nk_context *ctx, struct nk_window *win, enum nk_window_insert_location loc);
5875
5876 /* pool */
5877 NK_LIB void nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc, unsigned int capacity);
5878 NK_LIB void nk_pool_free(struct nk_pool *pool);
5879 NK_LIB void nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size);
5880 NK_LIB struct nk_page_element *nk_pool_alloc(struct nk_pool *pool);
5881
5882 /* page-element */
5883 NK_LIB struct nk_page_element* nk_create_page_element(struct nk_context *ctx);
5884 NK_LIB void nk_link_page_element_into_freelist(struct nk_context *ctx, struct nk_page_element *elem);
5885 NK_LIB void nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem);
5886
5887 /* table */
5888 NK_LIB struct nk_table* nk_create_table(struct nk_context *ctx);
5889 NK_LIB void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
5890 NK_LIB void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
5891 NK_LIB void nk_push_table(struct nk_window *win, struct nk_table *tbl);
5892 NK_LIB nk_uint *nk_add_value(struct nk_context *ctx, struct nk_window *win, nk_hash name, nk_uint value);
5893 NK_LIB nk_uint *nk_find_value(struct nk_window *win, nk_hash name);
5894
5895 /* panel */
5896 NK_LIB void *nk_create_panel(struct nk_context *ctx);
5897 NK_LIB void nk_free_panel(struct nk_context*, struct nk_panel *pan);
5898 NK_LIB int nk_panel_has_header(nk_flags flags, const char *title);
5899 NK_LIB struct nk_vec2 nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type);
5900 NK_LIB float nk_panel_get_border(const struct nk_style *style, nk_flags flags, enum nk_panel_type type);
5901 NK_LIB struct nk_color nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type);
5902 NK_LIB int nk_panel_is_sub(enum nk_panel_type type);
5903 NK_LIB int nk_panel_is_nonblock(enum nk_panel_type type);
5904 NK_LIB int nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type);
5905 NK_LIB void nk_panel_end(struct nk_context *ctx);
5906
5907 /* layout */
5908 NK_LIB float nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type, float total_space, int columns);
5909 NK_LIB void nk_panel_layout(const struct nk_context *ctx, struct nk_window *win, float height, int cols);
5910 NK_LIB void nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt, float height, int cols, int width);
5911 NK_LIB void nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win);
5912 NK_LIB void nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx, struct nk_window *win, int modify);
5913 NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx);
5914 NK_LIB void nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx);
5915
5916 /* popup */
5917 NK_LIB int nk_nonblock_begin(struct nk_context *ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type);
5918
5919 /* text */
5920 struct nk_text {
5921 struct nk_vec2 padding;
5922 struct nk_color background;
5923 struct nk_color text;
5924 };
5925 NK_LIB void nk_widget_text(struct nk_command_buffer *o, struct nk_rect b, const char *string, int len, const struct nk_text *t, nk_flags a, const struct nk_user_font *f);
5926 NK_LIB void nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b, const char *string, int len, const struct nk_text *t, const struct nk_user_font *f);
5927
5928 /* button */
5929 NK_LIB int nk_button_behavior(nk_flags *state, struct nk_rect r, const struct nk_input *i, enum nk_button_behavior behavior);
5930 NK_LIB const struct nk_style_item* nk_draw_button(struct nk_command_buffer *out, const struct nk_rect *bounds, nk_flags state, const struct nk_style_button *style);
5931 NK_LIB int nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, const struct nk_style_button *style, const struct nk_input *in, enum nk_button_behavior behavior, struct nk_rect *content);
5932 NK_LIB void nk_draw_button_text(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, const char *txt, int len, nk_flags text_alignment, const struct nk_user_font *font);
5933 NK_LIB int nk_do_button_text(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *string, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_input *in, const struct nk_user_font *font);
5934 NK_LIB void nk_draw_button_symbol(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, enum nk_symbol_type type, const struct nk_user_font *font);
5935 NK_LIB int nk_do_button_symbol(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, enum nk_symbol_type symbol, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_input *in, const struct nk_user_font *font);
5936 NK_LIB void nk_draw_button_image(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state, const struct nk_style_button *style, const struct nk_image *img);
5937 NK_LIB int nk_do_button_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, struct nk_image img, enum nk_button_behavior b, const struct nk_style_button *style, const struct nk_input *in);
5938 NK_LIB void nk_draw_button_text_symbol(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *label, const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style, const char *str, int len, enum nk_symbol_type type, const struct nk_user_font *font);
5939 NK_LIB int nk_do_button_text_symbol(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, enum nk_symbol_type symbol, const char *str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_user_font *font, const struct nk_input *in);
5940 NK_LIB void nk_draw_button_text_image(struct nk_command_buffer *out, const struct nk_rect *bounds, const struct nk_rect *label, const struct nk_rect *image, nk_flags state, const struct nk_style_button *style, const char *str, int len, const struct nk_user_font *font, const struct nk_image *img);
5941 NK_LIB int nk_do_button_text_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, struct nk_image img, const char* str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button *style, const struct nk_user_font *font, const struct nk_input *in);
5942
5943 /* toggle */
5944 enum nk_toggle_type {
5945 NK_TOGGLE_CHECK,
5946 NK_TOGGLE_OPTION
5947 };
5948 NK_LIB int nk_toggle_behavior(const struct nk_input *in, struct nk_rect select, nk_flags *state, int active);
5949 NK_LIB void nk_draw_checkbox(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, int active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
5950 NK_LIB void nk_draw_option(struct nk_command_buffer *out, nk_flags state, const struct nk_style_toggle *style, int active, const struct nk_rect *label, const struct nk_rect *selector, const struct nk_rect *cursors, const char *string, int len, const struct nk_user_font *font);
5951 NK_LIB int nk_do_toggle(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r, int *active, const char *str, int len, enum nk_toggle_type type, const struct nk_style_toggle *style, const struct nk_input *in, const struct nk_user_font *font);
5952
5953 /* progress */
5954 NK_LIB nk_size nk_progress_behavior(nk_flags *state, struct nk_input *in, struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable);
5955 NK_LIB void nk_draw_progress(struct nk_command_buffer *out, nk_flags state, const struct nk_style_progress *style, const struct nk_rect *bounds, const struct nk_rect *scursor, nk_size value, nk_size max);
5956 NK_LIB nk_size nk_do_progress(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, nk_size value, nk_size max, int modifiable, const struct nk_style_progress *style, struct nk_input *in);
5957
5958 /* slider */
5959 NK_LIB float nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor, struct nk_rect *visual_cursor, struct nk_input *in, struct nk_rect bounds, float slider_min, float slider_max, float slider_value, float slider_step, float slider_steps);
5960 NK_LIB void nk_draw_slider(struct nk_command_buffer *out, nk_flags state, const struct nk_style_slider *style, const struct nk_rect *bounds, const struct nk_rect *visual_cursor, float min, float value, float max);
5961 NK_LIB float nk_do_slider(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, float min, float val, float max, float step, const struct nk_style_slider *style, struct nk_input *in, const struct nk_user_font *font);
5962
5963 /* scrollbar */
5964 NK_LIB float nk_scrollbar_behavior(nk_flags *state, struct nk_input *in, int has_scrolling, const struct nk_rect *scroll, const struct nk_rect *cursor, const struct nk_rect *empty0, const struct nk_rect *empty1, float scroll_offset, float target, float scroll_step, enum nk_orientation o);
5965 NK_LIB void nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state, const struct nk_style_scrollbar *style, const struct nk_rect *bounds, const struct nk_rect *scroll);
5966 NK_LIB float nk_do_scrollbarv(nk_flags *state, struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar *style, struct nk_input *in, const struct nk_user_font *font);
5967 NK_LIB float nk_do_scrollbarh(nk_flags *state, struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar *style, struct nk_input *in, const struct nk_user_font *font);
5968
5969 /* selectable */
5970 NK_LIB void nk_draw_selectable(struct nk_command_buffer *out, nk_flags state, const struct nk_style_selectable *style, int active, const struct nk_rect *bounds, const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym, const char *string, int len, nk_flags align, const struct nk_user_font *font);
5971 NK_LIB int nk_do_selectable(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *str, int len, nk_flags align, int *value, const struct nk_style_selectable *style, const struct nk_input *in, const struct nk_user_font *font);
5972 NK_LIB int nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, const char *str, int len, nk_flags align, int *value, const struct nk_image *img, const struct nk_style_selectable *style, const struct nk_input *in, const struct nk_user_font *font);
5973
5974 /* edit */
5975 NK_LIB void nk_edit_draw_text(struct nk_command_buffer *out, const struct nk_style_edit *style, float pos_x, float pos_y, float x_offset, const char *text, int byte_len, float row_height, const struct nk_user_font *font, struct nk_color background, struct nk_color foreground, int is_selected);
5976 NK_LIB nk_flags nk_do_edit(nk_flags *state, struct nk_command_buffer *out, struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter, struct nk_text_edit *edit, const struct nk_style_edit *style, struct nk_input *in, const struct nk_user_font *font);
5977
5978 /* color-picker */
5979 NK_LIB int nk_color_picker_behavior(nk_flags *state, const struct nk_rect *bounds, const struct nk_rect *matrix, const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar, struct nk_colorf *color, const struct nk_input *in);
5980 NK_LIB void nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix, const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar, struct nk_colorf col);
5981 NK_LIB int nk_do_color_picker(nk_flags *state, struct nk_command_buffer *out, struct nk_colorf *col, enum nk_color_format fmt, struct nk_rect bounds, struct nk_vec2 padding, const struct nk_input *in, const struct nk_user_font *font);
5982
5983 /* property */
5984 enum nk_property_status {
5985 NK_PROPERTY_DEFAULT,
5986 NK_PROPERTY_EDIT,
5987 NK_PROPERTY_DRAG
5988 };
5989 enum nk_property_filter {
5990 NK_FILTER_INT,
5991 NK_FILTER_FLOAT
5992 };
5993 enum nk_property_kind {
5994 NK_PROPERTY_INT,
5995 NK_PROPERTY_FLOAT,
5996 NK_PROPERTY_DOUBLE
5997 };
5998 union nk_property {
5999 int i;
6000 float f;
6001 double d;
6002 };
6003 struct nk_property_variant {
6004 enum nk_property_kind kind;
6005 union nk_property value;
6006 union nk_property min_value;
6007 union nk_property max_value;
6008 union nk_property step;
6009 };
6010 NK_LIB struct nk_property_variant nk_property_variant_int(int value, int min_value, int max_value, int step);
6011 NK_LIB struct nk_property_variant nk_property_variant_float(float value, float min_value, float max_value, float step);
6012 NK_LIB struct nk_property_variant nk_property_variant_double(double value, double min_value, double max_value, double step);
6013
6014 NK_LIB void nk_drag_behavior(nk_flags *state, const struct nk_input *in, struct nk_rect drag, struct nk_property_variant *variant, float inc_per_pixel);
6015 NK_LIB void nk_property_behavior(nk_flags *ws, const struct nk_input *in, struct nk_rect property, struct nk_rect label, struct nk_rect edit, struct nk_rect empty, int *state, struct nk_property_variant *variant, float inc_per_pixel);
6016 NK_LIB void nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style, const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state, const char *name, int len, const struct nk_user_font *font);
6017 NK_LIB void nk_do_property(nk_flags *ws, struct nk_command_buffer *out, struct nk_rect property, const char *name, struct nk_property_variant *variant, float inc_per_pixel, char *buffer, int *len, int *state, int *cursor, int *select_begin, int *select_end, const struct nk_style_property *style, enum nk_property_filter filter, struct nk_input *in, const struct nk_user_font *font, struct nk_text_edit *text_edit, enum nk_button_behavior behavior);
6018 NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant, float inc_per_pixel, const enum nk_property_filter filter);
6019
6020 #endif
6021
6022
6023
6024
6025
6026 /* ===============================================================
6027 *
6028 * MATH
6029 *
6030 * ===============================================================*/
6031 /* Since nuklear is supposed to work on all systems providing floating point
6032 math without any dependencies I also had to implement my own math functions
6033 for sqrt, sin and cos. Since the actual highly accurate implementations for
6034 the standard library functions are quite complex and I do not need high
6035 precision for my use cases I use approximations.
6036
6037 Sqrt
6038 ----
6039 For square root nuklear uses the famous fast inverse square root:
6040 https://en.wikipedia.org/wiki/Fast_inverse_square_root with
6041 slightly tweaked magic constant. While on today's hardware it is
6042 probably not faster it is still fast and accurate enough for
6043 nuklear's use cases. IMPORTANT: this requires float format IEEE 754
6044
6045 Sine/Cosine
6046 -----------
6047 All constants inside both function are generated Remez's minimax
6048 approximations for value range 0...2*PI. The reason why I decided to
6049 approximate exactly that range is that nuklear only needs sine and
6050 cosine to generate circles which only requires that exact range.
6051 In addition I used Remez instead of Taylor for additional precision:
6052 www.lolengine.net/blog/2011/12/21/better-function-approximations.
6053
6054 The tool I used to generate constants for both sine and cosine
6055 (it can actually approximate a lot more functions) can be
6056 found here: www.lolengine.net/wiki/oss/lolremez
6057 */
6058 NK_LIB float
6059 nk_inv_sqrt(float n)
6060 {
6061 float x2;
6062 const float threehalfs = 1.5f;
6063 union {nk_uint i; float f;} conv = {0};
6064 conv.f = n;
6065 x2 = n * 0.5f;
6066 conv.i = 0x5f375A84 - (conv.i >> 1);
6067 conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
6068 return conv.f;
6069 }
6070 NK_LIB float
6071 nk_sqrt(float x)
6072 {
6073 return x * nk_inv_sqrt(x);
6074 }
6075 NK_LIB float
6076 nk_sin(float x)
6077 {
6078 NK_STORAGE const float a0 = +1.91059300966915117e-31f;
6079 NK_STORAGE const float a1 = +1.00086760103908896f;
6080 NK_STORAGE const float a2 = -1.21276126894734565e-2f;
6081 NK_STORAGE const float a3 = -1.38078780785773762e-1f;
6082 NK_STORAGE const float a4 = -2.67353392911981221e-2f;
6083 NK_STORAGE const float a5 = +2.08026600266304389e-2f;
6084 NK_STORAGE const float a6 = -3.03996055049204407e-3f;
6085 NK_STORAGE const float a7 = +1.38235642404333740e-4f;
6086 return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
6087 }
6088 NK_LIB float
6089 nk_cos(float x)
6090 {
6091 /* New implementation. Also generated using lolremez. */
6092 /* Old version significantly deviated from expected results. */
6093 NK_STORAGE const float a0 = 9.9995999154986614e-1f;
6094 NK_STORAGE const float a1 = 1.2548995793001028e-3f;
6095 NK_STORAGE const float a2 = -5.0648546280678015e-1f;
6096 NK_STORAGE const float a3 = 1.2942246466519995e-2f;
6097 NK_STORAGE const float a4 = 2.8668384702547972e-2f;
6098 NK_STORAGE const float a5 = 7.3726485210586547e-3f;
6099 NK_STORAGE const float a6 = -3.8510875386947414e-3f;
6100 NK_STORAGE const float a7 = 4.7196604604366623e-4f;
6101 NK_STORAGE const float a8 = -1.8776444013090451e-5f;
6102 return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*(a7 + x*a8)))))));
6103 }
6104 NK_LIB nk_uint
6105 nk_round_up_pow2(nk_uint v)
6106 {
6107 v--;
6108 v |= v >> 1;
6109 v |= v >> 2;
6110 v |= v >> 4;
6111 v |= v >> 8;
6112 v |= v >> 16;
6113 v++;
6114 return v;
6115 }
6116 NK_LIB double
6117 nk_pow(double x, int n)
6118 {
6119 /* check the sign of n */
6120 double r = 1;
6121 int plus = n >= 0;
6122 n = (plus) ? n : -n;
6123 while (n > 0) {
6124 if ((n & 1) == 1)
6125 r *= x;
6126 n /= 2;
6127 x *= x;
6128 }
6129 return plus ? r : 1.0 / r;
6130 }
6131 NK_LIB int
6132 nk_ifloord(double x)
6133 {
6134 x = (double)((int)x - ((x < 0.0) ? 1 : 0));
6135 return (int)x;
6136 }
6137 NK_LIB int
6138 nk_ifloorf(float x)
6139 {
6140 x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
6141 return (int)x;
6142 }
6143 NK_LIB int
6144 nk_iceilf(float x)
6145 {
6146 if (x >= 0) {
6147 int i = (int)x;
6148 return (x > i) ? i+1: i;
6149 } else {
6150 int t = (int)x;
6151 float r = x - (float)t;
6152 return (r > 0.0f) ? t+1: t;
6153 }
6154 }
6155 NK_LIB int
6156 nk_log10(double n)
6157 {
6158 int neg;
6159 int ret;
6160 int exp = 0;
6161
6162 neg = (n < 0) ? 1 : 0;
6163 ret = (neg) ? (int)-n : (int)n;
6164 while ((ret / 10) > 0) {
6165 ret /= 10;
6166 exp++;
6167 }
6168 if (neg) exp = -exp;
6169 return exp;
6170 }
6171 NK_API struct nk_rect
6172 nk_get_null_rect(void)
6173 {
6174 return nk_null_rect;
6175 }
6176 NK_API struct nk_rect
6177 nk_rect(float x, float y, float w, float h)
6178 {
6179 struct nk_rect r;
6180 r.x = x; r.y = y;
6181 r.w = w; r.h = h;
6182 return r;
6183 }
6184 NK_API struct nk_rect
6185 nk_recti(int x, int y, int w, int h)
6186 {
6187 struct nk_rect r;
6188 r.x = (float)x;
6189 r.y = (float)y;
6190 r.w = (float)w;
6191 r.h = (float)h;
6192 return r;
6193 }
6194 NK_API struct nk_rect
6195 nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
6196 {
6197 return nk_rect(pos.x, pos.y, size.x, size.y);
6198 }
6199 NK_API struct nk_rect
6200 nk_rectv(const float *r)
6201 {
6202 return nk_rect(r[0], r[1], r[2], r[3]);
6203 }
6204 NK_API struct nk_rect
6205 nk_rectiv(const int *r)
6206 {
6207 return nk_recti(r[0], r[1], r[2], r[3]);
6208 }
6209 NK_API struct nk_vec2
6210 nk_rect_pos(struct nk_rect r)
6211 {
6212 struct nk_vec2 ret;
6213 ret.x = r.x; ret.y = r.y;
6214 return ret;
6215 }
6216 NK_API struct nk_vec2
6217 nk_rect_size(struct nk_rect r)
6218 {
6219 struct nk_vec2 ret;
6220 ret.x = r.w; ret.y = r.h;
6221 return ret;
6222 }
6223 NK_LIB struct nk_rect
6224 nk_shrink_rect(struct nk_rect r, float amount)
6225 {
6226 struct nk_rect res;
6227 r.w = NK_MAX(r.w, 2 * amount);
6228 r.h = NK_MAX(r.h, 2 * amount);
6229 res.x = r.x + amount;
6230 res.y = r.y + amount;
6231 res.w = r.w - 2 * amount;
6232 res.h = r.h - 2 * amount;
6233 return res;
6234 }
6235 NK_LIB struct nk_rect
6236 nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
6237 {
6238 r.w = NK_MAX(r.w, 2 * pad.x);
6239 r.h = NK_MAX(r.h, 2 * pad.y);
6240 r.x += pad.x; r.y += pad.y;
6241 r.w -= 2 * pad.x;
6242 r.h -= 2 * pad.y;
6243 return r;
6244 }
6245 NK_API struct nk_vec2
6246 nk_vec2(float x, float y)
6247 {
6248 struct nk_vec2 ret;
6249 ret.x = x; ret.y = y;
6250 return ret;
6251 }
6252 NK_API struct nk_vec2
6253 nk_vec2i(int x, int y)
6254 {
6255 struct nk_vec2 ret;
6256 ret.x = (float)x;
6257 ret.y = (float)y;
6258 return ret;
6259 }
6260 NK_API struct nk_vec2
6261 nk_vec2v(const float *v)
6262 {
6263 return nk_vec2(v[0], v[1]);
6264 }
6265 NK_API struct nk_vec2
6266 nk_vec2iv(const int *v)
6267 {
6268 return nk_vec2i(v[0], v[1]);
6269 }
6270 NK_LIB void
6271 nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
6272 float x1, float y1)
6273 {
6274 NK_ASSERT(a);
6275 NK_ASSERT(clip);
6276 clip->x = NK_MAX(a->x, x0);
6277 clip->y = NK_MAX(a->y, y0);
6278 clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
6279 clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
6280 clip->w = NK_MAX(0, clip->w);
6281 clip->h = NK_MAX(0, clip->h);
6282 }
6283
6284 NK_API void
6285 nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
6286 float pad_x, float pad_y, enum nk_heading direction)
6287 {
6288 float w_half, h_half;
6289 NK_ASSERT(result);
6290
6291 r.w = NK_MAX(2 * pad_x, r.w);
6292 r.h = NK_MAX(2 * pad_y, r.h);
6293 r.w = r.w - 2 * pad_x;
6294 r.h = r.h - 2 * pad_y;
6295
6296 r.x = r.x + pad_x;
6297 r.y = r.y + pad_y;
6298
6299 w_half = r.w / 2.0f;
6300 h_half = r.h / 2.0f;
6301
6302 if (direction == NK_UP) {
6303 result[0] = nk_vec2(r.x + w_half, r.y);
6304 result[1] = nk_vec2(r.x + r.w, r.y + r.h);
6305 result[2] = nk_vec2(r.x, r.y + r.h);
6306 } else if (direction == NK_RIGHT) {
6307 result[0] = nk_vec2(r.x, r.y);
6308 result[1] = nk_vec2(r.x + r.w, r.y + h_half);
6309 result[2] = nk_vec2(r.x, r.y + r.h);
6310 } else if (direction == NK_DOWN) {
6311 result[0] = nk_vec2(r.x, r.y);
6312 result[1] = nk_vec2(r.x + r.w, r.y);
6313 result[2] = nk_vec2(r.x + w_half, r.y + r.h);
6314 } else {
6315 result[0] = nk_vec2(r.x, r.y + h_half);
6316 result[1] = nk_vec2(r.x + r.w, r.y);
6317 result[2] = nk_vec2(r.x + r.w, r.y + r.h);
6318 }
6319 }
6320
6321
6322
6323
6324
6325 /* ===============================================================
6326 *
6327 * UTIL
6328 *
6329 * ===============================================================*/
6330 NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
6331 NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
6332 NK_LIB int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
6333 NK_LIB int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
6334 NK_LIB int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
6335 NK_LIB int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
6336
6337 NK_LIB void*
6338 nk_memcopy(void *dst0, const void *src0, nk_size length)
6339 {
6340 nk_ptr t;
6341 char *dst = (char*)dst0;
6342 const char *src = (const char*)src0;
6343 if (length == 0 || dst == src)
6344 goto done;
6345
6346 #define nk_word int
6347 #define nk_wsize sizeof(nk_word)
6348 #define nk_wmask (nk_wsize-1)
6349 #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
6350 #define NK_TLOOP1(s) do { s; } while (--t)
6351
6352 if (dst < src) {
6353 t = (nk_ptr)src; /* only need low bits */
6354 if ((t | (nk_ptr)dst) & nk_wmask) {
6355 if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
6356 t = length;
6357 else
6358 t = nk_wsize - (t & nk_wmask);
6359 length -= t;
6360 NK_TLOOP1(*dst++ = *src++);
6361 }
6362 t = length / nk_wsize;
6363 NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
6364 src += nk_wsize; dst += nk_wsize);
6365 t = length & nk_wmask;
6366 NK_TLOOP(*dst++ = *src++);
6367 } else {
6368 src += length;
6369 dst += length;
6370 t = (nk_ptr)src;
6371 if ((t | (nk_ptr)dst) & nk_wmask) {
6372 if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
6373 t = length;
6374 else
6375 t &= nk_wmask;
6376 length -= t;
6377 NK_TLOOP1(*--dst = *--src);
6378 }
6379 t = length / nk_wsize;
6380 NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
6381 *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
6382 t = length & nk_wmask;
6383 NK_TLOOP(*--dst = *--src);
6384 }
6385 #undef nk_word
6386 #undef nk_wsize
6387 #undef nk_wmask
6388 #undef NK_TLOOP
6389 #undef NK_TLOOP1
6390 done:
6391 return (dst0);
6392 }
6393 NK_LIB void
6394 nk_memset(void *ptr, int c0, nk_size size)
6395 {
6396 #define nk_word unsigned
6397 #define nk_wsize sizeof(nk_word)
6398 #define nk_wmask (nk_wsize - 1)
6399 nk_byte *dst = (nk_byte*)ptr;
6400 unsigned c = 0;
6401 nk_size t = 0;
6402
6403 if ((c = (nk_byte)c0) != 0) {
6404 c = (c << 8) | c; /* at least 16-bits */
6405 if (sizeof(unsigned int) > 2)
6406 c = (c << 16) | c; /* at least 32-bits*/
6407 }
6408
6409 /* too small of a word count */
6410 dst = (nk_byte*)ptr;
6411 if (size < 3 * nk_wsize) {
6412 while (size--) *dst++ = (nk_byte)c0;
6413 return;
6414 }
6415
6416 /* align destination */
6417 if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
6418 t = nk_wsize -t;
6419 size -= t;
6420 do {
6421 *dst++ = (nk_byte)c0;
6422 } while (--t != 0);
6423 }
6424
6425 /* fill word */
6426 t = size / nk_wsize;
6427 do {
6428 *(nk_word*)((void*)dst) = c;
6429 dst += nk_wsize;
6430 } while (--t != 0);
6431
6432 /* fill trailing bytes */
6433 t = (size & nk_wmask);
6434 if (t != 0) {
6435 do {
6436 *dst++ = (nk_byte)c0;
6437 } while (--t != 0);
6438 }
6439
6440 #undef nk_word
6441 #undef nk_wsize
6442 #undef nk_wmask
6443 }
6444 NK_LIB void
6445 nk_zero(void *ptr, nk_size size)
6446 {
6447 NK_ASSERT(ptr);
6448 NK_MEMSET(ptr, 0, size);
6449 }
6450 NK_API int
6451 nk_strlen(const char *str)
6452 {
6453 int siz = 0;
6454 NK_ASSERT(str);
6455 while (str && *str++ != '\0') siz++;
6456 return siz;
6457 }
6458 NK_API int
6459 nk_strtoi(const char *str, const char **endptr)
6460 {
6461 int neg = 1;
6462 const char *p = str;
6463 int value = 0;
6464
6465 NK_ASSERT(str);
6466 if (!str) return 0;
6467
6468 /* skip whitespace */
6469 while (*p == ' ') p++;
6470 if (*p == '-') {
6471 neg = -1;
6472 p++;
6473 }
6474 while (*p && *p >= '0' && *p <= '9') {
6475 value = value * 10 + (int) (*p - '0');
6476 p++;
6477 }
6478 if (endptr)
6479 *endptr = p;
6480 return neg*value;
6481 }
6482 NK_API double
6483 nk_strtod(const char *str, const char **endptr)
6484 {
6485 double m;
6486 double neg = 1.0;
6487 const char *p = str;
6488 double value = 0;
6489 double number = 0;
6490
6491 NK_ASSERT(str);
6492 if (!str) return 0;
6493
6494 /* skip whitespace */
6495 while (*p == ' ') p++;
6496 if (*p == '-') {
6497 neg = -1.0;
6498 p++;
6499 }
6500
6501 while (*p && *p != '.' && *p != 'e') {
6502 value = value * 10.0 + (double) (*p - '0');
6503 p++;
6504 }
6505
6506 if (*p == '.') {
6507 p++;
6508 for(m = 0.1; *p && *p != 'e'; p++ ) {
6509 value = value + (double) (*p - '0') * m;
6510 m *= 0.1;
6511 }
6512 }
6513 if (*p == 'e') {
6514 int i, pow, div;
6515 p++;
6516 if (*p == '-') {
6517 div = nk_true;
6518 p++;
6519 } else if (*p == '+') {
6520 div = nk_false;
6521 p++;
6522 } else div = nk_false;
6523
6524 for (pow = 0; *p; p++)
6525 pow = pow * 10 + (int) (*p - '0');
6526
6527 for (m = 1.0, i = 0; i < pow; i++)
6528 m *= 10.0;
6529
6530 if (div)
6531 value /= m;
6532 else value *= m;
6533 }
6534 number = value * neg;
6535 if (endptr)
6536 *endptr = p;
6537 return number;
6538 }
6539 NK_API float
6540 nk_strtof(const char *str, const char **endptr)
6541 {
6542 float float_value;
6543 double double_value;
6544 double_value = NK_STRTOD(str, endptr);
6545 float_value = (float)double_value;
6546 return float_value;
6547 }
6548 NK_API int
6549 nk_stricmp(const char *s1, const char *s2)
6550 {
6551 nk_int c1,c2,d;
6552 do {
6553 c1 = *s1++;
6554 c2 = *s2++;
6555 d = c1 - c2;
6556 while (d) {
6557 if (c1 <= 'Z' && c1 >= 'A') {
6558 d += ('a' - 'A');
6559 if (!d) break;
6560 }
6561 if (c2 <= 'Z' && c2 >= 'A') {
6562 d -= ('a' - 'A');
6563 if (!d) break;
6564 }
6565 return ((d >= 0) << 1) - 1;
6566 }
6567 } while (c1);
6568 return 0;
6569 }
6570 NK_API int
6571 nk_stricmpn(const char *s1, const char *s2, int n)
6572 {
6573 int c1,c2,d;
6574 NK_ASSERT(n >= 0);
6575 do {
6576 c1 = *s1++;
6577 c2 = *s2++;
6578 if (!n--) return 0;
6579
6580 d = c1 - c2;
6581 while (d) {
6582 if (c1 <= 'Z' && c1 >= 'A') {
6583 d += ('a' - 'A');
6584 if (!d) break;
6585 }
6586 if (c2 <= 'Z' && c2 >= 'A') {
6587 d -= ('a' - 'A');
6588 if (!d) break;
6589 }
6590 return ((d >= 0) << 1) - 1;
6591 }
6592 } while (c1);
6593 return 0;
6594 }
6595 NK_INTERN int
6596 nk_str_match_here(const char *regexp, const char *text)
6597 {
6598 if (regexp[0] == '\0')
6599 return 1;
6600 if (regexp[1] == '*')
6601 return nk_str_match_star(regexp[0], regexp+2, text);
6602 if (regexp[0] == '$' && regexp[1] == '\0')
6603 return *text == '\0';
6604 if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
6605 return nk_str_match_here(regexp+1, text+1);
6606 return 0;
6607 }
6608 NK_INTERN int
6609 nk_str_match_star(int c, const char *regexp, const char *text)
6610 {
6611 do {/* a '* matches zero or more instances */
6612 if (nk_str_match_here(regexp, text))
6613 return 1;
6614 } while (*text != '\0' && (*text++ == c || c == '.'));
6615 return 0;
6616 }
6617 NK_API int
6618 nk_strfilter(const char *text, const char *regexp)
6619 {
6620 /*
6621 c matches any literal character c
6622 . matches any single character
6623 ^ matches the beginning of the input string
6624 $ matches the end of the input string
6625 * matches zero or more occurrences of the previous character*/
6626 if (regexp[0] == '^')
6627 return nk_str_match_here(regexp+1, text);
6628 do { /* must look even if string is empty */
6629 if (nk_str_match_here(regexp, text))
6630 return 1;
6631 } while (*text++ != '\0');
6632 return 0;
6633 }
6634 NK_API int
6635 nk_strmatch_fuzzy_text(const char *str, int str_len,
6636 const char *pattern, int *out_score)
6637 {
6638 /* Returns true if each character in pattern is found sequentially within str
6639 * if found then out_score is also set. Score value has no intrinsic meaning.
6640 * Range varies with pattern. Can only compare scores with same search pattern. */
6641
6642 /* bonus for adjacent matches */
6643 #define NK_ADJACENCY_BONUS 5
6644 /* bonus if match occurs after a separator */
6645 #define NK_SEPARATOR_BONUS 10
6646 /* bonus if match is uppercase and prev is lower */
6647 #define NK_CAMEL_BONUS 10
6648 /* penalty applied for every letter in str before the first match */
6649 #define NK_LEADING_LETTER_PENALTY (-3)
6650 /* maximum penalty for leading letters */
6651 #define NK_MAX_LEADING_LETTER_PENALTY (-9)
6652 /* penalty for every letter that doesn't matter */
6653 #define NK_UNMATCHED_LETTER_PENALTY (-1)
6654
6655 /* loop variables */
6656 int score = 0;
6657 char const * pattern_iter = pattern;
6658 int str_iter = 0;
6659 int prev_matched = nk_false;
6660 int prev_lower = nk_false;
6661 /* true so if first letter match gets separator bonus*/
6662 int prev_separator = nk_true;
6663
6664 /* use "best" matched letter if multiple string letters match the pattern */
6665 char const * best_letter = 0;
6666 int best_letter_score = 0;
6667
6668 /* loop over strings */
6669 NK_ASSERT(str);
6670 NK_ASSERT(pattern);
6671 if (!str || !str_len || !pattern) return 0;
6672 while (str_iter < str_len)
6673 {
6674 const char pattern_letter = *pattern_iter;
6675 const char str_letter = str[str_iter];
6676
6677 int next_match = *pattern_iter != '\0' &&
6678 nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
6679 int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
6680
6681 int advanced = next_match && best_letter;
6682 int pattern_repeat = best_letter && *pattern_iter != '\0';
6683 pattern_repeat = pattern_repeat &&
6684 nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
6685
6686 if (advanced || pattern_repeat) {
6687 score += best_letter_score;
6688 best_letter = 0;
6689 best_letter_score = 0;
6690 }
6691
6692 if (next_match || rematch)
6693 {
6694 int new_score = 0;
6695 /* Apply penalty for each letter before the first pattern match */
6696 if (pattern_iter == pattern) {
6697 int count = (int)(&str[str_iter] - str);
6698 int penalty = NK_LEADING_LETTER_PENALTY * count;
6699 if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
6700 penalty = NK_MAX_LEADING_LETTER_PENALTY;
6701
6702 score += penalty;
6703 }
6704
6705 /* apply bonus for consecutive bonuses */
6706 if (prev_matched)
6707 new_score += NK_ADJACENCY_BONUS;
6708
6709 /* apply bonus for matches after a separator */
6710 if (prev_separator)
6711 new_score += NK_SEPARATOR_BONUS;
6712
6713 /* apply bonus across camel case boundaries */
6714 if (prev_lower && nk_is_upper(str_letter))
6715 new_score += NK_CAMEL_BONUS;
6716
6717 /* update pattern iter IFF the next pattern letter was matched */
6718 if (next_match)
6719 ++pattern_iter;
6720
6721 /* update best letter in str which may be for a "next" letter or a rematch */
6722 if (new_score >= best_letter_score) {
6723 /* apply penalty for now skipped letter */
6724 if (best_letter != 0)
6725 score += NK_UNMATCHED_LETTER_PENALTY;
6726
6727 best_letter = &str[str_iter];
6728 best_letter_score = new_score;
6729 }
6730 prev_matched = nk_true;
6731 } else {
6732 score += NK_UNMATCHED_LETTER_PENALTY;
6733 prev_matched = nk_false;
6734 }
6735
6736 /* separators should be more easily defined */
6737 prev_lower = nk_is_lower(str_letter) != 0;
6738 prev_separator = str_letter == '_' || str_letter == ' ';
6739
6740 ++str_iter;
6741 }
6742
6743 /* apply score for last match */
6744 if (best_letter)
6745 score += best_letter_score;
6746
6747 /* did not match full pattern */
6748 if (*pattern_iter != '\0')
6749 return nk_false;
6750
6751 if (out_score)
6752 *out_score = score;
6753 return nk_true;
6754 }
6755 NK_API int
6756 nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
6757 {
6758 return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
6759 }
6760 NK_LIB int
6761 nk_string_float_limit(char *string, int prec)
6762 {
6763 int dot = 0;
6764 char *c = string;
6765 while (*c) {
6766 if (*c == '.') {
6767 dot = 1;
6768 c++;
6769 continue;
6770 }
6771 if (dot == (prec+1)) {
6772 *c = 0;
6773 break;
6774 }
6775 if (dot > 0) dot++;
6776 c++;
6777 }
6778 return (int)(c - string);
6779 }
6780 NK_INTERN void
6781 nk_strrev_ascii(char *s)
6782 {
6783 int len = nk_strlen(s);
6784 int end = len / 2;
6785 int i = 0;
6786 char t;
6787 for (; i < end; ++i) {
6788 t = s[i];
6789 s[i] = s[len - 1 - i];
6790 s[len -1 - i] = t;
6791 }
6792 }
6793 NK_LIB char*
6794 nk_itoa(char *s, long n)
6795 {
6796 long i = 0;
6797 if (n == 0) {
6798 s[i++] = '0';
6799 s[i] = 0;
6800 return s;
6801 }
6802 if (n < 0) {
6803 s[i++] = '-';
6804 n = -n;
6805 }
6806 while (n > 0) {
6807 s[i++] = (char)('0' + (n % 10));
6808 n /= 10;
6809 }
6810 s[i] = 0;
6811 if (s[0] == '-')
6812 ++s;
6813
6814 nk_strrev_ascii(s);
6815 return s;
6816 }
6817 NK_LIB char*
6818 nk_dtoa(char *s, double n)
6819 {
6820 int useExp = 0;
6821 int digit = 0, m = 0, m1 = 0;
6822 char *c = s;
6823 int neg = 0;
6824
6825 NK_ASSERT(s);
6826 if (!s) return 0;
6827
6828 if (n == 0.0) {
6829 s[0] = '0'; s[1] = '\0';
6830 return s;
6831 }
6832
6833 neg = (n < 0);
6834 if (neg) n = -n;
6835
6836 /* calculate magnitude */
6837 m = nk_log10(n);
6838 useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
6839 if (neg) *(c++) = '-';
6840
6841 /* set up for scientific notation */
6842 if (useExp) {
6843 if (m < 0)
6844 m -= 1;
6845 n = n / (double)nk_pow(10.0, m);
6846 m1 = m;
6847 m = 0;
6848 }
6849 if (m < 1.0) {
6850 m = 0;
6851 }
6852
6853 /* convert the number */
6854 while (n > NK_FLOAT_PRECISION || m >= 0) {
6855 double weight = nk_pow(10.0, m);
6856 if (weight > 0) {
6857 double t = (double)n / weight;
6858 digit = nk_ifloord(t);
6859 n -= ((double)digit * weight);
6860 *(c++) = (char)('0' + (char)digit);
6861 }
6862 if (m == 0 && n > 0)
6863 *(c++) = '.';
6864 m--;
6865 }
6866
6867 if (useExp) {
6868 /* convert the exponent */
6869 int i, j;
6870 *(c++) = 'e';
6871 if (m1 > 0) {
6872 *(c++) = '+';
6873 } else {
6874 *(c++) = '-';
6875 m1 = -m1;
6876 }
6877 m = 0;
6878 while (m1 > 0) {
6879 *(c++) = (char)('0' + (char)(m1 % 10));
6880 m1 /= 10;
6881 m++;
6882 }
6883 c -= m;
6884 for (i = 0, j = m-1; i<j; i++, j--) {
6885 /* swap without temporary */
6886 c[i] ^= c[j];
6887 c[j] ^= c[i];
6888 c[i] ^= c[j];
6889 }
6890 c += m;
6891 }
6892 *(c) = '\0';
6893 return s;
6894 }
6895 #ifdef NK_INCLUDE_STANDARD_VARARGS
6896 #ifndef NK_INCLUDE_STANDARD_IO
6897 NK_INTERN int
6898 nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
6899 {
6900 enum nk_arg_type {
6901 NK_ARG_TYPE_CHAR,
6902 NK_ARG_TYPE_SHORT,
6903 NK_ARG_TYPE_DEFAULT,
6904 NK_ARG_TYPE_LONG
6905 };
6906 enum nk_arg_flags {
6907 NK_ARG_FLAG_LEFT = 0x01,
6908 NK_ARG_FLAG_PLUS = 0x02,
6909 NK_ARG_FLAG_SPACE = 0x04,
6910 NK_ARG_FLAG_NUM = 0x10,
6911 NK_ARG_FLAG_ZERO = 0x20
6912 };
6913
6914 char number_buffer[NK_MAX_NUMBER_BUFFER];
6915 enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
6916 int precision = NK_DEFAULT;
6917 int width = NK_DEFAULT;
6918 nk_flags flag = 0;
6919
6920 int len = 0;
6921 int result = -1;
6922 const char *iter = fmt;
6923
6924 NK_ASSERT(buf);
6925 NK_ASSERT(buf_size);
6926 if (!buf || !buf_size || !fmt) return 0;
6927 for (iter = fmt; *iter && len < buf_size; iter++) {
6928 /* copy all non-format characters */
6929 while (*iter && (*iter != '%') && (len < buf_size))
6930 buf[len++] = *iter++;
6931 if (!(*iter) || len >= buf_size) break;
6932 iter++;
6933
6934 /* flag arguments */
6935 while (*iter) {
6936 if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
6937 else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
6938 else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
6939 else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
6940 else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
6941 else break;
6942 iter++;
6943 }
6944
6945 /* width argument */
6946 width = NK_DEFAULT;
6947 if (*iter >= '1' && *iter <= '9') {
6948 const char *end;
6949 width = nk_strtoi(iter, &end);
6950 if (end == iter)
6951 width = -1;
6952 else iter = end;
6953 } else if (*iter == '*') {
6954 width = va_arg(args, int);
6955 iter++;
6956 }
6957
6958 /* precision argument */
6959 precision = NK_DEFAULT;
6960 if (*iter == '.') {
6961 iter++;
6962 if (*iter == '*') {
6963 precision = va_arg(args, int);
6964 iter++;
6965 } else {
6966 const char *end;
6967 precision = nk_strtoi(iter, &end);
6968 if (end == iter)
6969 precision = -1;
6970 else iter = end;
6971 }
6972 }
6973
6974 /* length modifier */
6975 if (*iter == 'h') {
6976 if (*(iter+1) == 'h') {
6977 arg_type = NK_ARG_TYPE_CHAR;
6978 iter++;
6979 } else arg_type = NK_ARG_TYPE_SHORT;
6980 iter++;
6981 } else if (*iter == 'l') {
6982 arg_type = NK_ARG_TYPE_LONG;
6983 iter++;
6984 } else arg_type = NK_ARG_TYPE_DEFAULT;
6985
6986 /* specifier */
6987 if (*iter == '%') {
6988 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
6989 NK_ASSERT(precision == NK_DEFAULT);
6990 NK_ASSERT(width == NK_DEFAULT);
6991 if (len < buf_size)
6992 buf[len++] = '%';
6993 } else if (*iter == 's') {
6994 /* string */
6995 const char *str = va_arg(args, const char*);
6996 NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
6997 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
6998 NK_ASSERT(precision == NK_DEFAULT);
6999 NK_ASSERT(width == NK_DEFAULT);
7000 if (str == buf) return -1;
7001 while (str && *str && len < buf_size)
7002 buf[len++] = *str++;
7003 } else if (*iter == 'n') {
7004 /* current length callback */
7005 signed int *n = va_arg(args, int*);
7006 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
7007 NK_ASSERT(precision == NK_DEFAULT);
7008 NK_ASSERT(width == NK_DEFAULT);
7009 if (n) *n = len;
7010 } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
7011 /* signed integer */
7012 long value = 0;
7013 const char *num_iter;
7014 int num_len, num_print, padding;
7015 int cur_precision = NK_MAX(precision, 1);
7016 int cur_width = NK_MAX(width, 0);
7017
7018 /* retrieve correct value type */
7019 if (arg_type == NK_ARG_TYPE_CHAR)
7020 value = (signed char)va_arg(args, int);
7021 else if (arg_type == NK_ARG_TYPE_SHORT)
7022 value = (signed short)va_arg(args, int);
7023 else if (arg_type == NK_ARG_TYPE_LONG)
7024 value = va_arg(args, signed long);
7025 else if (*iter == 'c')
7026 value = (unsigned char)va_arg(args, int);
7027 else value = va_arg(args, signed int);
7028
7029 /* convert number to string */
7030 nk_itoa(number_buffer, value);
7031 num_len = nk_strlen(number_buffer);
7032 padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
7033 if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
7034 padding = NK_MAX(padding-1, 0);
7035
7036 /* fill left padding up to a total of `width` characters */
7037 if (!(flag & NK_ARG_FLAG_LEFT)) {
7038 while (padding-- > 0 && (len < buf_size)) {
7039 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
7040 buf[len++] = '0';
7041 else buf[len++] = ' ';
7042 }
7043 }
7044
7045 /* copy string value representation into buffer */
7046 if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
7047 buf[len++] = '+';
7048 else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
7049 buf[len++] = ' ';
7050
7051 /* fill up to precision number of digits with '0' */
7052 num_print = NK_MAX(cur_precision, num_len);
7053 while (precision && (num_print > num_len) && (len < buf_size)) {
7054 buf[len++] = '0';
7055 num_print--;
7056 }
7057
7058 /* copy string value representation into buffer */
7059 num_iter = number_buffer;
7060 while (precision && *num_iter && len < buf_size)
7061 buf[len++] = *num_iter++;
7062
7063 /* fill right padding up to width characters */
7064 if (flag & NK_ARG_FLAG_LEFT) {
7065 while ((padding-- > 0) && (len < buf_size))
7066 buf[len++] = ' ';
7067 }
7068 } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
7069 /* unsigned integer */
7070 unsigned long value = 0;
7071 int num_len = 0, num_print, padding = 0;
7072 int cur_precision = NK_MAX(precision, 1);
7073 int cur_width = NK_MAX(width, 0);
7074 unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
7075
7076 /* print oct/hex/dec value */
7077 const char *upper_output_format = "0123456789ABCDEF";
7078 const char *lower_output_format = "0123456789abcdef";
7079 const char *output_format = (*iter == 'x') ?
7080 lower_output_format: upper_output_format;
7081
7082 /* retrieve correct value type */
7083 if (arg_type == NK_ARG_TYPE_CHAR)
7084 value = (unsigned char)va_arg(args, int);
7085 else if (arg_type == NK_ARG_TYPE_SHORT)
7086 value = (unsigned short)va_arg(args, int);
7087 else if (arg_type == NK_ARG_TYPE_LONG)
7088 value = va_arg(args, unsigned long);
7089 else value = va_arg(args, unsigned int);
7090
7091 do {
7092 /* convert decimal number into hex/oct number */
7093 int digit = output_format[value % base];
7094 if (num_len < NK_MAX_NUMBER_BUFFER)
7095 number_buffer[num_len++] = (char)digit;
7096 value /= base;
7097 } while (value > 0);
7098
7099 num_print = NK_MAX(cur_precision, num_len);
7100 padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
7101 if (flag & NK_ARG_FLAG_NUM)
7102 padding = NK_MAX(padding-1, 0);
7103
7104 /* fill left padding up to a total of `width` characters */
7105 if (!(flag & NK_ARG_FLAG_LEFT)) {
7106 while ((padding-- > 0) && (len < buf_size)) {
7107 if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
7108 buf[len++] = '0';
7109 else buf[len++] = ' ';
7110 }
7111 }
7112
7113 /* fill up to precision number of digits */
7114 if (num_print && (flag & NK_ARG_FLAG_NUM)) {
7115 if ((*iter == 'o') && (len < buf_size)) {
7116 buf[len++] = '0';
7117 } else if ((*iter == 'x') && ((len+1) < buf_size)) {
7118 buf[len++] = '0';
7119 buf[len++] = 'x';
7120 } else if ((*iter == 'X') && ((len+1) < buf_size)) {
7121 buf[len++] = '0';
7122 buf[len++] = 'X';
7123 }
7124 }
7125 while (precision && (num_print > num_len) && (len < buf_size)) {
7126 buf[len++] = '0';
7127 num_print--;
7128 }
7129
7130 /* reverse number direction */
7131 while (num_len > 0) {
7132 if (precision && (len < buf_size))
7133 buf[len++] = number_buffer[num_len-1];
7134 num_len--;
7135 }
7136
7137 /* fill right padding up to width characters */
7138 if (flag & NK_ARG_FLAG_LEFT) {
7139 while ((padding-- > 0) && (len < buf_size))
7140 buf[len++] = ' ';
7141 }
7142 } else if (*iter == 'f') {
7143 /* floating point */
7144 const char *num_iter;
7145 int cur_precision = (precision < 0) ? 6: precision;
7146 int prefix, cur_width = NK_MAX(width, 0);
7147 double value = va_arg(args, double);
7148 int num_len = 0, frac_len = 0, dot = 0;
7149 int padding = 0;
7150
7151 NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
7152 NK_DTOA(number_buffer, value);
7153 num_len = nk_strlen(number_buffer);
7154
7155 /* calculate padding */
7156 num_iter = number_buffer;
7157 while (*num_iter && *num_iter != '.')
7158 num_iter++;
7159
7160 prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
7161 padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
7162 if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
7163 padding = NK_MAX(padding-1, 0);
7164
7165 /* fill left padding up to a total of `width` characters */
7166 if (!(flag & NK_ARG_FLAG_LEFT)) {
7167 while (padding-- > 0 && (len < buf_size)) {
7168 if (flag & NK_ARG_FLAG_ZERO)
7169 buf[len++] = '0';
7170 else buf[len++] = ' ';
7171 }
7172 }
7173
7174 /* copy string value representation into buffer */
7175 num_iter = number_buffer;
7176 if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
7177 buf[len++] = '+';
7178 else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
7179 buf[len++] = ' ';
7180 while (*num_iter) {
7181 if (dot) frac_len++;
7182 if (len < buf_size)
7183 buf[len++] = *num_iter;
7184 if (*num_iter == '.') dot = 1;
7185 if (frac_len >= cur_precision) break;
7186 num_iter++;
7187 }
7188
7189 /* fill number up to precision */
7190 while (frac_len < cur_precision) {
7191 if (!dot && len < buf_size) {
7192 buf[len++] = '.';
7193 dot = 1;
7194 }
7195 if (len < buf_size)
7196 buf[len++] = '0';
7197 frac_len++;
7198 }
7199
7200 /* fill right padding up to width characters */
7201 if (flag & NK_ARG_FLAG_LEFT) {
7202 while ((padding-- > 0) && (len < buf_size))
7203 buf[len++] = ' ';
7204 }
7205 } else {
7206 /* Specifier not supported: g,G,e,E,p,z */
7207 NK_ASSERT(0 && "specifier is not supported!");
7208 return result;
7209 }
7210 }
7211 buf[(len >= buf_size)?(buf_size-1):len] = 0;
7212 result = (len >= buf_size)?-1:len;
7213 return result;
7214 }
7215 #endif
7216 NK_LIB int
7217 nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
7218 {
7219 int result = -1;
7220 NK_ASSERT(buf);
7221 NK_ASSERT(buf_size);
7222 if (!buf || !buf_size || !fmt) return 0;
7223 #ifdef NK_INCLUDE_STANDARD_IO
7224 result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
7225 result = (result >= buf_size) ? -1: result;
7226 buf[buf_size-1] = 0;
7227 #else
7228 result = nk_vsnprintf(buf, buf_size, fmt, args);
7229 #endif
7230 return result;
7231 }
7232 #endif
7233 NK_API nk_hash
7234 nk_murmur_hash(const void * key, int len, nk_hash seed)
7235 {
7236 /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
7237 #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
7238
7239 nk_uint h1 = seed;
7240 nk_uint k1;
7241 const nk_byte *data = (const nk_byte*)key;
7242 const nk_byte *keyptr = data;
7243 nk_byte *k1ptr;
7244 const int bsize = sizeof(k1);
7245 const int nblocks = len/4;
7246
7247 const nk_uint c1 = 0xcc9e2d51;
7248 const nk_uint c2 = 0x1b873593;
7249 const nk_byte *tail;
7250 int i;
7251
7252 /* body */
7253 if (!key) return 0;
7254 for (i = 0; i < nblocks; ++i, keyptr += bsize) {
7255 k1ptr = (nk_byte*)&k1;
7256 k1ptr[0] = keyptr[0];
7257 k1ptr[1] = keyptr[1];
7258 k1ptr[2] = keyptr[2];
7259 k1ptr[3] = keyptr[3];
7260
7261 k1 *= c1;
7262 k1 = NK_ROTL(k1,15);
7263 k1 *= c2;
7264
7265 h1 ^= k1;
7266 h1 = NK_ROTL(h1,13);
7267 h1 = h1*5+0xe6546b64;
7268 }
7269
7270 /* tail */
7271 tail = (const nk_byte*)(data + nblocks*4);
7272 k1 = 0;
7273 switch (len & 3) {
7274 case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
7275 case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
7276 case 1: k1 ^= tail[0];
7277 k1 *= c1;
7278 k1 = NK_ROTL(k1,15);
7279 k1 *= c2;
7280 h1 ^= k1;
7281 break;
7282 default: break;
7283 }
7284
7285 /* finalization */
7286 h1 ^= (nk_uint)len;
7287 /* fmix32 */
7288 h1 ^= h1 >> 16;
7289 h1 *= 0x85ebca6b;
7290 h1 ^= h1 >> 13;
7291 h1 *= 0xc2b2ae35;
7292 h1 ^= h1 >> 16;
7293
7294 #undef NK_ROTL
7295 return h1;
7296 }
7297 #ifdef NK_INCLUDE_STANDARD_IO
7298 NK_LIB char*
7299 nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
7300 {
7301 char *buf;
7302 FILE *fd;
7303 long ret;
7304
7305 NK_ASSERT(path);
7306 NK_ASSERT(siz);
7307 NK_ASSERT(alloc);
7308 if (!path || !siz || !alloc)
7309 return 0;
7310
7311 fd = fopen(path, "rb");
7312 if (!fd) return 0;
7313 fseek(fd, 0, SEEK_END);
7314 ret = ftell(fd);
7315 if (ret < 0) {
7316 fclose(fd);
7317 return 0;
7318 }
7319 *siz = (nk_size)ret;
7320 fseek(fd, 0, SEEK_SET);
7321 buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
7322 NK_ASSERT(buf);
7323 if (!buf) {
7324 fclose(fd);
7325 return 0;
7326 }
7327 *siz = (nk_size)fread(buf, 1,*siz, fd);
7328 fclose(fd);
7329 return buf;
7330 }
7331 #endif
7332 NK_LIB int
7333 nk_text_clamp(const struct nk_user_font *font, const char *text,
7334 int text_len, float space, int *glyphs, float *text_width,
7335 nk_rune *sep_list, int sep_count)
7336 {
7337 int i = 0;
7338 int glyph_len = 0;
7339 float last_width = 0;
7340 nk_rune unicode = 0;
7341 float width = 0;
7342 int len = 0;
7343 int g = 0;
7344 float s;
7345
7346 int sep_len = 0;
7347 int sep_g = 0;
7348 float sep_width = 0;
7349 sep_count = NK_MAX(sep_count,0);
7350
7351 glyph_len = nk_utf_decode(text, &unicode, text_len);
7352 while (glyph_len && (width < space) && (len < text_len)) {
7353 len += glyph_len;
7354 s = font->width(font->userdata, font->height, text, len);
7355 for (i = 0; i < sep_count; ++i) {
7356 if (unicode != sep_list[i]) continue;
7357 sep_width = last_width = width;
7358 sep_g = g+1;
7359 sep_len = len;
7360 break;
7361 }
7362 if (i == sep_count){
7363 last_width = sep_width = width;
7364 sep_g = g+1;
7365 }
7366 width = s;
7367 glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
7368 g++;
7369 }
7370 if (len >= text_len) {
7371 *glyphs = g;
7372 *text_width = last_width;
7373 return len;
7374 } else {
7375 *glyphs = sep_g;
7376 *text_width = sep_width;
7377 return (!sep_len) ? len: sep_len;
7378 }
7379 }
7380 NK_LIB struct nk_vec2
7381 nk_text_calculate_text_bounds(const struct nk_user_font *font,
7382 const char *begin, int byte_len, float row_height, const char **remaining,
7383 struct nk_vec2 *out_offset, int *glyphs, int op)
7384 {
7385 float line_height = row_height;
7386 struct nk_vec2 text_size = nk_vec2(0,0);
7387 float line_width = 0.0f;
7388
7389 float glyph_width;
7390 int glyph_len = 0;
7391 nk_rune unicode = 0;
7392 int text_len = 0;
7393 if (!begin || byte_len <= 0 || !font)
7394 return nk_vec2(0,row_height);
7395
7396 glyph_len = nk_utf_decode(begin, &unicode, byte_len);
7397 if (!glyph_len) return text_size;
7398 glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
7399
7400 *glyphs = 0;
7401 while ((text_len < byte_len) && glyph_len) {
7402 if (unicode == '\n') {
7403 text_size.x = NK_MAX(text_size.x, line_width);
7404 text_size.y += line_height;
7405 line_width = 0;
7406 *glyphs+=1;
7407 if (op == NK_STOP_ON_NEW_LINE)
7408 break;
7409
7410 text_len++;
7411 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
7412 continue;
7413 }
7414
7415 if (unicode == '\r') {
7416 text_len++;
7417 *glyphs+=1;
7418 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
7419 continue;
7420 }
7421
7422 *glyphs = *glyphs + 1;
7423 text_len += glyph_len;
7424 line_width += (float)glyph_width;
7425 glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
7426 glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
7427 continue;
7428 }
7429
7430 if (text_size.x < line_width)
7431 text_size.x = line_width;
7432 if (out_offset)
7433 *out_offset = nk_vec2(line_width, text_size.y + line_height);
7434 if (line_width > 0 || text_size.y == 0.0f)
7435 text_size.y += line_height;
7436 if (remaining)
7437 *remaining = begin+text_len;
7438 return text_size;
7439 }
7440
7441
7442
7443
7444
7445 /* ==============================================================
7446 *
7447 * COLOR
7448 *
7449 * ===============================================================*/
7450 NK_INTERN int
7451 nk_parse_hex(const char *p, int length)
7452 {
7453 int i = 0;
7454 int len = 0;
7455 while (len < length) {
7456 i <<= 4;
7457 if (p[len] >= 'a' && p[len] <= 'f')
7458 i += ((p[len] - 'a') + 10);
7459 else if (p[len] >= 'A' && p[len] <= 'F')
7460 i += ((p[len] - 'A') + 10);
7461 else i += (p[len] - '0');
7462 len++;
7463 }
7464 return i;
7465 }
7466 NK_API struct nk_color
7467 nk_rgba(int r, int g, int b, int a)
7468 {
7469 struct nk_color ret;
7470 ret.r = (nk_byte)NK_CLAMP(0, r, 255);
7471 ret.g = (nk_byte)NK_CLAMP(0, g, 255);
7472 ret.b = (nk_byte)NK_CLAMP(0, b, 255);
7473 ret.a = (nk_byte)NK_CLAMP(0, a, 255);
7474 return ret;
7475 }
7476 NK_API struct nk_color
7477 nk_rgb_hex(const char *rgb)
7478 {
7479 struct nk_color col;
7480 const char *c = rgb;
7481 if (*c == '#') c++;
7482 col.r = (nk_byte)nk_parse_hex(c, 2);
7483 col.g = (nk_byte)nk_parse_hex(c+2, 2);
7484 col.b = (nk_byte)nk_parse_hex(c+4, 2);
7485 col.a = 255;
7486 return col;
7487 }
7488 NK_API struct nk_color
7489 nk_rgba_hex(const char *rgb)
7490 {
7491 struct nk_color col;
7492 const char *c = rgb;
7493 if (*c == '#') c++;
7494 col.r = (nk_byte)nk_parse_hex(c, 2);
7495 col.g = (nk_byte)nk_parse_hex(c+2, 2);
7496 col.b = (nk_byte)nk_parse_hex(c+4, 2);
7497 col.a = (nk_byte)nk_parse_hex(c+6, 2);
7498 return col;
7499 }
7500 NK_API void
7501 nk_color_hex_rgba(char *output, struct nk_color col)
7502 {
7503 #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
7504 output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
7505 output[1] = (char)NK_TO_HEX((col.r & 0x0F));
7506 output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
7507 output[3] = (char)NK_TO_HEX((col.g & 0x0F));
7508 output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
7509 output[5] = (char)NK_TO_HEX((col.b & 0x0F));
7510 output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
7511 output[7] = (char)NK_TO_HEX((col.a & 0x0F));
7512 output[8] = '\0';
7513 #undef NK_TO_HEX
7514 }
7515 NK_API void
7516 nk_color_hex_rgb(char *output, struct nk_color col)
7517 {
7518 #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
7519 output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
7520 output[1] = (char)NK_TO_HEX((col.r & 0x0F));
7521 output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
7522 output[3] = (char)NK_TO_HEX((col.g & 0x0F));
7523 output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
7524 output[5] = (char)NK_TO_HEX((col.b & 0x0F));
7525 output[6] = '\0';
7526 #undef NK_TO_HEX
7527 }
7528 NK_API struct nk_color
7529 nk_rgba_iv(const int *c)
7530 {
7531 return nk_rgba(c[0], c[1], c[2], c[3]);
7532 }
7533 NK_API struct nk_color
7534 nk_rgba_bv(const nk_byte *c)
7535 {
7536 return nk_rgba(c[0], c[1], c[2], c[3]);
7537 }
7538 NK_API struct nk_color
7539 nk_rgb(int r, int g, int b)
7540 {
7541 struct nk_color ret;
7542 ret.r = (nk_byte)NK_CLAMP(0, r, 255);
7543 ret.g = (nk_byte)NK_CLAMP(0, g, 255);
7544 ret.b = (nk_byte)NK_CLAMP(0, b, 255);
7545 ret.a = (nk_byte)255;
7546 return ret;
7547 }
7548 NK_API struct nk_color
7549 nk_rgb_iv(const int *c)
7550 {
7551 return nk_rgb(c[0], c[1], c[2]);
7552 }
7553 NK_API struct nk_color
7554 nk_rgb_bv(const nk_byte* c)
7555 {
7556 return nk_rgb(c[0], c[1], c[2]);
7557 }
7558 NK_API struct nk_color
7559 nk_rgba_u32(nk_uint in)
7560 {
7561 struct nk_color ret;
7562 ret.r = (in & 0xFF);
7563 ret.g = ((in >> 8) & 0xFF);
7564 ret.b = ((in >> 16) & 0xFF);
7565 ret.a = (nk_byte)((in >> 24) & 0xFF);
7566 return ret;
7567 }
7568 NK_API struct nk_color
7569 nk_rgba_f(float r, float g, float b, float a)
7570 {
7571 struct nk_color ret;
7572 ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
7573 ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
7574 ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
7575 ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
7576 return ret;
7577 }
7578 NK_API struct nk_color
7579 nk_rgba_fv(const float *c)
7580 {
7581 return nk_rgba_f(c[0], c[1], c[2], c[3]);
7582 }
7583 NK_API struct nk_color
7584 nk_rgba_cf(struct nk_colorf c)
7585 {
7586 return nk_rgba_f(c.r, c.g, c.b, c.a);
7587 }
7588 NK_API struct nk_color
7589 nk_rgb_f(float r, float g, float b)
7590 {
7591 struct nk_color ret;
7592 ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
7593 ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
7594 ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
7595 ret.a = 255;
7596 return ret;
7597 }
7598 NK_API struct nk_color
7599 nk_rgb_fv(const float *c)
7600 {
7601 return nk_rgb_f(c[0], c[1], c[2]);
7602 }
7603 NK_API struct nk_color
7604 nk_rgb_cf(struct nk_colorf c)
7605 {
7606 return nk_rgb_f(c.r, c.g, c.b);
7607 }
7608 NK_API struct nk_color
7609 nk_hsv(int h, int s, int v)
7610 {
7611 return nk_hsva(h, s, v, 255);
7612 }
7613 NK_API struct nk_color
7614 nk_hsv_iv(const int *c)
7615 {
7616 return nk_hsv(c[0], c[1], c[2]);
7617 }
7618 NK_API struct nk_color
7619 nk_hsv_bv(const nk_byte *c)
7620 {
7621 return nk_hsv(c[0], c[1], c[2]);
7622 }
7623 NK_API struct nk_color
7624 nk_hsv_f(float h, float s, float v)
7625 {
7626 return nk_hsva_f(h, s, v, 1.0f);
7627 }
7628 NK_API struct nk_color
7629 nk_hsv_fv(const float *c)
7630 {
7631 return nk_hsv_f(c[0], c[1], c[2]);
7632 }
7633 NK_API struct nk_color
7634 nk_hsva(int h, int s, int v, int a)
7635 {
7636 float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
7637 float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
7638 float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
7639 float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
7640 return nk_hsva_f(hf, sf, vf, af);
7641 }
7642 NK_API struct nk_color
7643 nk_hsva_iv(const int *c)
7644 {
7645 return nk_hsva(c[0], c[1], c[2], c[3]);
7646 }
7647 NK_API struct nk_color
7648 nk_hsva_bv(const nk_byte *c)
7649 {
7650 return nk_hsva(c[0], c[1], c[2], c[3]);
7651 }
7652 NK_API struct nk_colorf
7653 nk_hsva_colorf(float h, float s, float v, float a)
7654 {
7655 int i;
7656 float p, q, t, f;
7657 struct nk_colorf out = {0,0,0,0};
7658 if (s <= 0.0f) {
7659 out.r = v; out.g = v; out.b = v; out.a = a;
7660 return out;
7661 }
7662 h = h / (60.0f/360.0f);
7663 i = (int)h;
7664 f = h - (float)i;
7665 p = v * (1.0f - s);
7666 q = v * (1.0f - (s * f));
7667 t = v * (1.0f - s * (1.0f - f));
7668
7669 switch (i) {
7670 case 0: default: out.r = v; out.g = t; out.b = p; break;
7671 case 1: out.r = q; out.g = v; out.b = p; break;
7672 case 2: out.r = p; out.g = v; out.b = t; break;
7673 case 3: out.r = p; out.g = q; out.b = v; break;
7674 case 4: out.r = t; out.g = p; out.b = v; break;
7675 case 5: out.r = v; out.g = p; out.b = q; break;}
7676 out.a = a;
7677 return out;
7678 }
7679 NK_API struct nk_colorf
7680 nk_hsva_colorfv(float *c)
7681 {
7682 return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
7683 }
7684 NK_API struct nk_color
7685 nk_hsva_f(float h, float s, float v, float a)
7686 {
7687 struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
7688 return nk_rgba_f(c.r, c.g, c.b, c.a);
7689 }
7690 NK_API struct nk_color
7691 nk_hsva_fv(const float *c)
7692 {
7693 return nk_hsva_f(c[0], c[1], c[2], c[3]);
7694 }
7695 NK_API nk_uint
7696 nk_color_u32(struct nk_color in)
7697 {
7698 nk_uint out = (nk_uint)in.r;
7699 out |= ((nk_uint)in.g << 8);
7700 out |= ((nk_uint)in.b << 16);
7701 out |= ((nk_uint)in.a << 24);
7702 return out;
7703 }
7704 NK_API void
7705 nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
7706 {
7707 NK_STORAGE const float s = 1.0f/255.0f;
7708 *r = (float)in.r * s;
7709 *g = (float)in.g * s;
7710 *b = (float)in.b * s;
7711 *a = (float)in.a * s;
7712 }
7713 NK_API void
7714 nk_color_fv(float *c, struct nk_color in)
7715 {
7716 nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
7717 }
7718 NK_API struct nk_colorf
7719 nk_color_cf(struct nk_color in)
7720 {
7721 struct nk_colorf o;
7722 nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
7723 return o;
7724 }
7725 NK_API void
7726 nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
7727 {
7728 NK_STORAGE const double s = 1.0/255.0;
7729 *r = (double)in.r * s;
7730 *g = (double)in.g * s;
7731 *b = (double)in.b * s;
7732 *a = (double)in.a * s;
7733 }
7734 NK_API void
7735 nk_color_dv(double *c, struct nk_color in)
7736 {
7737 nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
7738 }
7739 NK_API void
7740 nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
7741 {
7742 float a;
7743 nk_color_hsva_f(out_h, out_s, out_v, &a, in);
7744 }
7745 NK_API void
7746 nk_color_hsv_fv(float *out, struct nk_color in)
7747 {
7748 float a;
7749 nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
7750 }
7751 NK_API void
7752 nk_colorf_hsva_f(float *out_h, float *out_s,
7753 float *out_v, float *out_a, struct nk_colorf in)
7754 {
7755 float chroma;
7756 float K = 0.0f;
7757 if (in.g < in.b) {
7758 const float t = in.g; in.g = in.b; in.b = t;
7759 K = -1.f;
7760 }
7761 if (in.r < in.g) {
7762 const float t = in.r; in.r = in.g; in.g = t;
7763 K = -2.f/6.0f - K;
7764 }
7765 chroma = in.r - ((in.g < in.b) ? in.g: in.b);
7766 *out_h = NK_ABS(K + (in.g - in.b)/(6.0f * chroma + 1e-20f));
7767 *out_s = chroma / (in.r + 1e-20f);
7768 *out_v = in.r;
7769 *out_a = in.a;
7770
7771 }
7772 NK_API void
7773 nk_colorf_hsva_fv(float *hsva, struct nk_colorf in)
7774 {
7775 nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
7776 }
7777 NK_API void
7778 nk_color_hsva_f(float *out_h, float *out_s,
7779 float *out_v, float *out_a, struct nk_color in)
7780 {
7781 struct nk_colorf col;
7782 nk_color_f(&col.r,&col.g,&col.b,&col.a, in);
7783 nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
7784 }
7785 NK_API void
7786 nk_color_hsva_fv(float *out, struct nk_color in)
7787 {
7788 nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
7789 }
7790 NK_API void
7791 nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
7792 int *out_a, struct nk_color in)
7793 {
7794 float h,s,v,a;
7795 nk_color_hsva_f(&h, &s, &v, &a, in);
7796 *out_h = (nk_byte)(h * 255.0f);
7797 *out_s = (nk_byte)(s * 255.0f);
7798 *out_v = (nk_byte)(v * 255.0f);
7799 *out_a = (nk_byte)(a * 255.0f);
7800 }
7801 NK_API void
7802 nk_color_hsva_iv(int *out, struct nk_color in)
7803 {
7804 nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
7805 }
7806 NK_API void
7807 nk_color_hsva_bv(nk_byte *out, struct nk_color in)
7808 {
7809 int tmp[4];
7810 nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
7811 out[0] = (nk_byte)tmp[0];
7812 out[1] = (nk_byte)tmp[1];
7813 out[2] = (nk_byte)tmp[2];
7814 out[3] = (nk_byte)tmp[3];
7815 }
7816 NK_API void
7817 nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
7818 {
7819 int tmp[4];
7820 nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
7821 *h = (nk_byte)tmp[0];
7822 *s = (nk_byte)tmp[1];
7823 *v = (nk_byte)tmp[2];
7824 *a = (nk_byte)tmp[3];
7825 }
7826 NK_API void
7827 nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
7828 {
7829 int a;
7830 nk_color_hsva_i(out_h, out_s, out_v, &a, in);
7831 }
7832 NK_API void
7833 nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
7834 {
7835 int tmp[4];
7836 nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
7837 *out_h = (nk_byte)tmp[0];
7838 *out_s = (nk_byte)tmp[1];
7839 *out_v = (nk_byte)tmp[2];
7840 }
7841 NK_API void
7842 nk_color_hsv_iv(int *out, struct nk_color in)
7843 {
7844 nk_color_hsv_i(&out[0], &out[1], &out[2], in);
7845 }
7846 NK_API void
7847 nk_color_hsv_bv(nk_byte *out, struct nk_color in)
7848 {
7849 int tmp[4];
7850 nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
7851 out[0] = (nk_byte)tmp[0];
7852 out[1] = (nk_byte)tmp[1];
7853 out[2] = (nk_byte)tmp[2];
7854 }
7855
7856
7857
7858
7859
7860 /* ===============================================================
7861 *
7862 * UTF-8
7863 *
7864 * ===============================================================*/
7865 NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
7866 NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
7867 NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
7868 NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
7869
7870 NK_INTERN int
7871 nk_utf_validate(nk_rune *u, int i)
7872 {
7873 NK_ASSERT(u);
7874 if (!u) return 0;
7875 if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
7876 NK_BETWEEN(*u, 0xD800, 0xDFFF))
7877 *u = NK_UTF_INVALID;
7878 for (i = 1; *u > nk_utfmax[i]; ++i);
7879 return i;
7880 }
7881 NK_INTERN nk_rune
7882 nk_utf_decode_byte(char c, int *i)
7883 {
7884 NK_ASSERT(i);
7885 if (!i) return 0;
7886 for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
7887 if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
7888 return (nk_byte)(c & ~nk_utfmask[*i]);
7889 }
7890 return 0;
7891 }
7892 NK_API int
7893 nk_utf_decode(const char *c, nk_rune *u, int clen)
7894 {
7895 int i, j, len, type=0;
7896 nk_rune udecoded;
7897
7898 NK_ASSERT(c);
7899 NK_ASSERT(u);
7900
7901 if (!c || !u) return 0;
7902 if (!clen) return 0;
7903 *u = NK_UTF_INVALID;
7904
7905 udecoded = nk_utf_decode_byte(c[0], &len);
7906 if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
7907 return 1;
7908
7909 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
7910 udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
7911 if (type != 0)
7912 return j;
7913 }
7914 if (j < len)
7915 return 0;
7916 *u = udecoded;
7917 nk_utf_validate(u, len);
7918 return len;
7919 }
7920 NK_INTERN char
7921 nk_utf_encode_byte(nk_rune u, int i)
7922 {
7923 return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
7924 }
7925 NK_API int
7926 nk_utf_encode(nk_rune u, char *c, int clen)
7927 {
7928 int len, i;
7929 len = nk_utf_validate(&u, 0);
7930 if (clen < len || !len || len > NK_UTF_SIZE)
7931 return 0;
7932
7933 for (i = len - 1; i != 0; --i) {
7934 c[i] = nk_utf_encode_byte(u, 0);
7935 u >>= 6;
7936 }
7937 c[0] = nk_utf_encode_byte(u, len);
7938 return len;
7939 }
7940 NK_API int
7941 nk_utf_len(const char *str, int len)
7942 {
7943 const char *text;
7944 int glyphs = 0;
7945 int text_len;
7946 int glyph_len;
7947 int src_len = 0;
7948 nk_rune unicode;
7949
7950 NK_ASSERT(str);
7951 if (!str || !len) return 0;
7952
7953 text = str;
7954 text_len = len;
7955 glyph_len = nk_utf_decode(text, &unicode, text_len);
7956 while (glyph_len && src_len < len) {
7957 glyphs++;
7958 src_len = src_len + glyph_len;
7959 glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
7960 }
7961 return glyphs;
7962 }
7963 NK_API const char*
7964 nk_utf_at(const char *buffer, int length, int index,
7965 nk_rune *unicode, int *len)
7966 {
7967 int i = 0;
7968 int src_len = 0;
7969 int glyph_len = 0;
7970 const char *text;
7971 int text_len;
7972
7973 NK_ASSERT(buffer);
7974 NK_ASSERT(unicode);
7975 NK_ASSERT(len);
7976
7977 if (!buffer || !unicode || !len) return 0;
7978 if (index < 0) {
7979 *unicode = NK_UTF_INVALID;
7980 *len = 0;
7981 return 0;
7982 }
7983
7984 text = buffer;
7985 text_len = length;
7986 glyph_len = nk_utf_decode(text, unicode, text_len);
7987 while (glyph_len) {
7988 if (i == index) {
7989 *len = glyph_len;
7990 break;
7991 }
7992
7993 i++;
7994 src_len = src_len + glyph_len;
7995 glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
7996 }
7997 if (i != index) return 0;
7998 return buffer + src_len;
7999 }
8000
8001
8002
8003
8004
8005 /* ==============================================================
8006 *
8007 * BUFFER
8008 *
8009 * ===============================================================*/
8010 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
8011 NK_LIB void*
8012 nk_malloc(nk_handle unused, void *old,nk_size size)
8013 {
8014 NK_UNUSED(unused);
8015 NK_UNUSED(old);
8016 return malloc(size);
8017 }
8018 NK_LIB void
8019 nk_mfree(nk_handle unused, void *ptr)
8020 {
8021 NK_UNUSED(unused);
8022 free(ptr);
8023 }
8024 NK_API void
8025 nk_buffer_init_default(struct nk_buffer *buffer)
8026 {
8027 struct nk_allocator alloc;
8028 alloc.userdata.ptr = 0;
8029 alloc.alloc = nk_malloc;
8030 alloc.free = nk_mfree;
8031 nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
8032 }
8033 #endif
8034
8035 NK_API void
8036 nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
8037 nk_size initial_size)
8038 {
8039 NK_ASSERT(b);
8040 NK_ASSERT(a);
8041 NK_ASSERT(initial_size);
8042 if (!b || !a || !initial_size) return;
8043
8044 nk_zero(b, sizeof(*b));
8045 b->type = NK_BUFFER_DYNAMIC;
8046 b->memory.ptr = a->alloc(a->userdata,0, initial_size);
8047 b->memory.size = initial_size;
8048 b->size = initial_size;
8049 b->grow_factor = 2.0f;
8050 b->pool = *a;
8051 }
8052 NK_API void
8053 nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
8054 {
8055 NK_ASSERT(b);
8056 NK_ASSERT(m);
8057 NK_ASSERT(size);
8058 if (!b || !m || !size) return;
8059
8060 nk_zero(b, sizeof(*b));
8061 b->type = NK_BUFFER_FIXED;
8062 b->memory.ptr = m;
8063 b->memory.size = size;
8064 b->size = size;
8065 }
8066 NK_LIB void*
8067 nk_buffer_align(void *unaligned,
8068 nk_size align, nk_size *alignment,
8069 enum nk_buffer_allocation_type type)
8070 {
8071 void *memory = 0;
8072 switch (type) {
8073 default:
8074 case NK_BUFFER_MAX:
8075 case NK_BUFFER_FRONT:
8076 if (align) {
8077 memory = NK_ALIGN_PTR(unaligned, align);
8078 *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
8079 } else {
8080 memory = unaligned;
8081 *alignment = 0;
8082 }
8083 break;
8084 case NK_BUFFER_BACK:
8085 if (align) {
8086 memory = NK_ALIGN_PTR_BACK(unaligned, align);
8087 *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
8088 } else {
8089 memory = unaligned;
8090 *alignment = 0;
8091 }
8092 break;
8093 }
8094 return memory;
8095 }
8096 NK_LIB void*
8097 nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
8098 {
8099 void *temp;
8100 nk_size buffer_size;
8101
8102 NK_ASSERT(b);
8103 NK_ASSERT(size);
8104 if (!b || !size || !b->pool.alloc || !b->pool.free)
8105 return 0;
8106
8107 buffer_size = b->memory.size;
8108 temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
8109 NK_ASSERT(temp);
8110 if (!temp) return 0;
8111
8112 *size = capacity;
8113 if (temp != b->memory.ptr) {
8114 NK_MEMCPY(temp, b->memory.ptr, buffer_size);
8115 b->pool.free(b->pool.userdata, b->memory.ptr);
8116 }
8117
8118 if (b->size == buffer_size) {
8119 /* no back buffer so just set correct size */
8120 b->size = capacity;
8121 return temp;
8122 } else {
8123 /* copy back buffer to the end of the new buffer */
8124 void *dst, *src;
8125 nk_size back_size;
8126 back_size = buffer_size - b->size;
8127 dst = nk_ptr_add(void, temp, capacity - back_size);
8128 src = nk_ptr_add(void, temp, b->size);
8129 NK_MEMCPY(dst, src, back_size);
8130 b->size = capacity - back_size;
8131 }
8132 return temp;
8133 }
8134 NK_LIB void*
8135 nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
8136 nk_size size, nk_size align)
8137 {
8138 int full;
8139 nk_size alignment;
8140 void *unaligned;
8141 void *memory;
8142
8143 NK_ASSERT(b);
8144 NK_ASSERT(size);
8145 if (!b || !size) return 0;
8146 b->needed += size;
8147
8148 /* calculate total size with needed alignment + size */
8149 if (type == NK_BUFFER_FRONT)
8150 unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
8151 else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
8152 memory = nk_buffer_align(unaligned, align, &alignment, type);
8153
8154 /* check if buffer has enough memory*/
8155 if (type == NK_BUFFER_FRONT)
8156 full = ((b->allocated + size + alignment) > b->size);
8157 else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
8158
8159 if (full) {
8160 nk_size capacity;
8161 if (b->type != NK_BUFFER_DYNAMIC)
8162 return 0;
8163 NK_ASSERT(b->pool.alloc && b->pool.free);
8164 if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
8165 return 0;
8166
8167 /* buffer is full so allocate bigger buffer if dynamic */
8168 capacity = (nk_size)((float)b->memory.size * b->grow_factor);
8169 capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
8170 b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
8171 if (!b->memory.ptr) return 0;
8172
8173 /* align newly allocated pointer */
8174 if (type == NK_BUFFER_FRONT)
8175 unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
8176 else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
8177 memory = nk_buffer_align(unaligned, align, &alignment, type);
8178 }
8179 if (type == NK_BUFFER_FRONT)
8180 b->allocated += size + alignment;
8181 else b->size -= (size + alignment);
8182 b->needed += alignment;
8183 b->calls++;
8184 return memory;
8185 }
8186 NK_API void
8187 nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
8188 const void *memory, nk_size size, nk_size align)
8189 {
8190 void *mem = nk_buffer_alloc(b, type, size, align);
8191 if (!mem) return;
8192 NK_MEMCPY(mem, memory, size);
8193 }
8194 NK_API void
8195 nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
8196 {
8197 NK_ASSERT(buffer);
8198 if (!buffer) return;
8199 buffer->marker[type].active = nk_true;
8200 if (type == NK_BUFFER_BACK)
8201 buffer->marker[type].offset = buffer->size;
8202 else buffer->marker[type].offset = buffer->allocated;
8203 }
8204 NK_API void
8205 nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
8206 {
8207 NK_ASSERT(buffer);
8208 if (!buffer) return;
8209 if (type == NK_BUFFER_BACK) {
8210 /* reset back buffer either back to marker or empty */
8211 buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
8212 if (buffer->marker[type].active)
8213 buffer->size = buffer->marker[type].offset;
8214 else buffer->size = buffer->memory.size;
8215 buffer->marker[type].active = nk_false;
8216 } else {
8217 /* reset front buffer either back to back marker or empty */
8218 buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
8219 if (buffer->marker[type].active)
8220 buffer->allocated = buffer->marker[type].offset;
8221 else buffer->allocated = 0;
8222 buffer->marker[type].active = nk_false;
8223 }
8224 }
8225 NK_API void
8226 nk_buffer_clear(struct nk_buffer *b)
8227 {
8228 NK_ASSERT(b);
8229 if (!b) return;
8230 b->allocated = 0;
8231 b->size = b->memory.size;
8232 b->calls = 0;
8233 b->needed = 0;
8234 }
8235 NK_API void
8236 nk_buffer_free(struct nk_buffer *b)
8237 {
8238 NK_ASSERT(b);
8239 if (!b || !b->memory.ptr) return;
8240 if (b->type == NK_BUFFER_FIXED) return;
8241 if (!b->pool.free) return;
8242 NK_ASSERT(b->pool.free);
8243 b->pool.free(b->pool.userdata, b->memory.ptr);
8244 }
8245 NK_API void
8246 nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
8247 {
8248 NK_ASSERT(b);
8249 NK_ASSERT(s);
8250 if (!s || !b) return;
8251 s->allocated = b->allocated;
8252 s->size = b->memory.size;
8253 s->needed = b->needed;
8254 s->memory = b->memory.ptr;
8255 s->calls = b->calls;
8256 }
8257 NK_API void*
8258 nk_buffer_memory(struct nk_buffer *buffer)
8259 {
8260 NK_ASSERT(buffer);
8261 if (!buffer) return 0;
8262 return buffer->memory.ptr;
8263 }
8264 NK_API const void*
8265 nk_buffer_memory_const(const struct nk_buffer *buffer)
8266 {
8267 NK_ASSERT(buffer);
8268 if (!buffer) return 0;
8269 return buffer->memory.ptr;
8270 }
8271 NK_API nk_size
8272 nk_buffer_total(struct nk_buffer *buffer)
8273 {
8274 NK_ASSERT(buffer);
8275 if (!buffer) return 0;
8276 return buffer->memory.size;
8277 }
8278
8279
8280
8281
8282
8283 /* ===============================================================
8284 *
8285 * STRING
8286 *
8287 * ===============================================================*/
8288 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
8289 NK_API void
8290 nk_str_init_default(struct nk_str *str)
8291 {
8292 struct nk_allocator alloc;
8293 alloc.userdata.ptr = 0;
8294 alloc.alloc = nk_malloc;
8295 alloc.free = nk_mfree;
8296 nk_buffer_init(&str->buffer, &alloc, 32);
8297 str->len = 0;
8298 }
8299 #endif
8300
8301 NK_API void
8302 nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
8303 {
8304 nk_buffer_init(&str->buffer, alloc, size);
8305 str->len = 0;
8306 }
8307 NK_API void
8308 nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
8309 {
8310 nk_buffer_init_fixed(&str->buffer, memory, size);
8311 str->len = 0;
8312 }
8313 NK_API int
8314 nk_str_append_text_char(struct nk_str *s, const char *str, int len)
8315 {
8316 char *mem;
8317 NK_ASSERT(s);
8318 NK_ASSERT(str);
8319 if (!s || !str || !len) return 0;
8320 mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
8321 if (!mem) return 0;
8322 NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
8323 s->len += nk_utf_len(str, len);
8324 return len;
8325 }
8326 NK_API int
8327 nk_str_append_str_char(struct nk_str *s, const char *str)
8328 {
8329 return nk_str_append_text_char(s, str, nk_strlen(str));
8330 }
8331 NK_API int
8332 nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
8333 {
8334 int i = 0;
8335 int byte_len = 0;
8336 nk_rune unicode;
8337 if (!str || !text || !len) return 0;
8338 for (i = 0; i < len; ++i)
8339 byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
8340 nk_str_append_text_char(str, text, byte_len);
8341 return len;
8342 }
8343 NK_API int
8344 nk_str_append_str_utf8(struct nk_str *str, const char *text)
8345 {
8346 int runes = 0;
8347 int byte_len = 0;
8348 int glyph_len = 0;
8349 nk_rune unicode;
8350 if (!str || !text) return 0;
8351
8352 glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
8353 while (unicode != '\0' && glyph_len) {
8354 glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
8355 byte_len += glyph_len;
8356 }
8357 nk_str_append_text_char(str, text, byte_len);
8358 return runes;
8359 }
8360 NK_API int
8361 nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
8362 {
8363 int i = 0;
8364 int byte_len = 0;
8365 nk_glyph glyph;
8366
8367 NK_ASSERT(str);
8368 if (!str || !text || !len) return 0;
8369 for (i = 0; i < len; ++i) {
8370 byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
8371 if (!byte_len) break;
8372 nk_str_append_text_char(str, glyph, byte_len);
8373 }
8374 return len;
8375 }
8376 NK_API int
8377 nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
8378 {
8379 int i = 0;
8380 nk_glyph glyph;
8381 int byte_len;
8382 NK_ASSERT(str);
8383 if (!str || !runes) return 0;
8384 while (runes[i] != '\0') {
8385 byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
8386 nk_str_append_text_char(str, glyph, byte_len);
8387 i++;
8388 }
8389 return i;
8390 }
8391 NK_API int
8392 nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
8393 {
8394 int i;
8395 void *mem;
8396 char *src;
8397 char *dst;
8398
8399 int copylen;
8400 NK_ASSERT(s);
8401 NK_ASSERT(str);
8402 NK_ASSERT(len >= 0);
8403 if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
8404 if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
8405 (s->buffer.type == NK_BUFFER_FIXED)) return 0;
8406
8407 copylen = (int)s->buffer.allocated - pos;
8408 if (!copylen) {
8409 nk_str_append_text_char(s, str, len);
8410 return 1;
8411 }
8412 mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
8413 if (!mem) return 0;
8414
8415 /* memmove */
8416 NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
8417 NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
8418 dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
8419 src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
8420 for (i = 0; i < copylen; ++i) *dst-- = *src--;
8421 mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
8422 NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
8423 s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
8424 return 1;
8425 }
8426 NK_API int
8427 nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
8428 {
8429 int glyph_len;
8430 nk_rune unicode;
8431 const char *begin;
8432 const char *buffer;
8433
8434 NK_ASSERT(str);
8435 NK_ASSERT(cstr);
8436 NK_ASSERT(len);
8437 if (!str || !cstr || !len) return 0;
8438 begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
8439 if (!str->len)
8440 return nk_str_append_text_char(str, cstr, len);
8441 buffer = nk_str_get_const(str);
8442 if (!begin) return 0;
8443 return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
8444 }
8445 NK_API int
8446 nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
8447 {
8448 return nk_str_insert_text_utf8(str, pos, text, len);
8449 }
8450 NK_API int
8451 nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
8452 {
8453 return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
8454 }
8455 NK_API int
8456 nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
8457 {
8458 int i = 0;
8459 int byte_len = 0;
8460 nk_rune unicode;
8461
8462 NK_ASSERT(str);
8463 NK_ASSERT(text);
8464 if (!str || !text || !len) return 0;
8465 for (i = 0; i < len; ++i)
8466 byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
8467 nk_str_insert_at_rune(str, pos, text, byte_len);
8468 return len;
8469 }
8470 NK_API int
8471 nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
8472 {
8473 int runes = 0;
8474 int byte_len = 0;
8475 int glyph_len = 0;
8476 nk_rune unicode;
8477 if (!str || !text) return 0;
8478
8479 glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
8480 while (unicode != '\0' && glyph_len) {
8481 glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
8482 byte_len += glyph_len;
8483 }
8484 nk_str_insert_at_rune(str, pos, text, byte_len);
8485 return runes;
8486 }
8487 NK_API int
8488 nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
8489 {
8490 int i = 0;
8491 int byte_len = 0;
8492 nk_glyph glyph;
8493
8494 NK_ASSERT(str);
8495 if (!str || !runes || !len) return 0;
8496 for (i = 0; i < len; ++i) {
8497 byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
8498 if (!byte_len) break;
8499 nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
8500 }
8501 return len;
8502 }
8503 NK_API int
8504 nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
8505 {
8506 int i = 0;
8507 nk_glyph glyph;
8508 int byte_len;
8509 NK_ASSERT(str);
8510 if (!str || !runes) return 0;
8511 while (runes[i] != '\0') {
8512 byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
8513 nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
8514 i++;
8515 }
8516 return i;
8517 }
8518 NK_API void
8519 nk_str_remove_chars(struct nk_str *s, int len)
8520 {
8521 NK_ASSERT(s);
8522 NK_ASSERT(len >= 0);
8523 if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
8524 NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
8525 s->buffer.allocated -= (nk_size)len;
8526 s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
8527 }
8528 NK_API void
8529 nk_str_remove_runes(struct nk_str *str, int len)
8530 {
8531 int index;
8532 const char *begin;
8533 const char *end;
8534 nk_rune unicode;
8535
8536 NK_ASSERT(str);
8537 NK_ASSERT(len >= 0);
8538 if (!str || len < 0) return;
8539 if (len >= str->len) {
8540 str->len = 0;
8541 return;
8542 }
8543
8544 index = str->len - len;
8545 begin = nk_str_at_rune(str, index, &unicode, &len);
8546 end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
8547 nk_str_remove_chars(str, (int)(end-begin)+1);
8548 }
8549 NK_API void
8550 nk_str_delete_chars(struct nk_str *s, int pos, int len)
8551 {
8552 NK_ASSERT(s);
8553 if (!s || !len || (nk_size)pos > s->buffer.allocated ||
8554 (nk_size)(pos + len) > s->buffer.allocated) return;
8555
8556 if ((nk_size)(pos + len) < s->buffer.allocated) {
8557 /* memmove */
8558 char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
8559 char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
8560 NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
8561 NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
8562 s->buffer.allocated -= (nk_size)len;
8563 } else nk_str_remove_chars(s, len);
8564 s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
8565 }
8566 NK_API void
8567 nk_str_delete_runes(struct nk_str *s, int pos, int len)
8568 {
8569 char *temp;
8570 nk_rune unicode;
8571 char *begin;
8572 char *end;
8573 int unused;
8574
8575 NK_ASSERT(s);
8576 NK_ASSERT(s->len >= pos + len);
8577 if (s->len < pos + len)
8578 len = NK_CLAMP(0, (s->len - pos), s->len);
8579 if (!len) return;
8580
8581 temp = (char *)s->buffer.memory.ptr;
8582 begin = nk_str_at_rune(s, pos, &unicode, &unused);
8583 if (!begin) return;
8584 s->buffer.memory.ptr = begin;
8585 end = nk_str_at_rune(s, len, &unicode, &unused);
8586 s->buffer.memory.ptr = temp;
8587 if (!end) return;
8588 nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
8589 }
8590 NK_API char*
8591 nk_str_at_char(struct nk_str *s, int pos)
8592 {
8593 NK_ASSERT(s);
8594 if (!s || pos > (int)s->buffer.allocated) return 0;
8595 return nk_ptr_add(char, s->buffer.memory.ptr, pos);
8596 }
8597 NK_API char*
8598 nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
8599 {
8600 int i = 0;
8601 int src_len = 0;
8602 int glyph_len = 0;
8603 char *text;
8604 int text_len;
8605
8606 NK_ASSERT(str);
8607 NK_ASSERT(unicode);
8608 NK_ASSERT(len);
8609
8610 if (!str || !unicode || !len) return 0;
8611 if (pos < 0) {
8612 *unicode = 0;
8613 *len = 0;
8614 return 0;
8615 }
8616
8617 text = (char*)str->buffer.memory.ptr;
8618 text_len = (int)str->buffer.allocated;
8619 glyph_len = nk_utf_decode(text, unicode, text_len);
8620 while (glyph_len) {
8621 if (i == pos) {
8622 *len = glyph_len;
8623 break;
8624 }
8625
8626 i++;
8627 src_len = src_len + glyph_len;
8628 glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
8629 }
8630 if (i != pos) return 0;
8631 return text + src_len;
8632 }
8633 NK_API const char*
8634 nk_str_at_char_const(const struct nk_str *s, int pos)
8635 {
8636 NK_ASSERT(s);
8637 if (!s || pos > (int)s->buffer.allocated) return 0;
8638 return nk_ptr_add(char, s->buffer.memory.ptr, pos);
8639 }
8640 NK_API const char*
8641 nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
8642 {
8643 int i = 0;
8644 int src_len = 0;
8645 int glyph_len = 0;
8646 char *text;
8647 int text_len;
8648
8649 NK_ASSERT(str);
8650 NK_ASSERT(unicode);
8651 NK_ASSERT(len);
8652
8653 if (!str || !unicode || !len) return 0;
8654 if (pos < 0) {
8655 *unicode = 0;
8656 *len = 0;
8657 return 0;
8658 }
8659
8660 text = (char*)str->buffer.memory.ptr;
8661 text_len = (int)str->buffer.allocated;
8662 glyph_len = nk_utf_decode(text, unicode, text_len);
8663 while (glyph_len) {
8664 if (i == pos) {
8665 *len = glyph_len;
8666 break;
8667 }
8668
8669 i++;
8670 src_len = src_len + glyph_len;
8671 glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
8672 }
8673 if (i != pos) return 0;
8674 return text + src_len;
8675 }
8676 NK_API nk_rune
8677 nk_str_rune_at(const struct nk_str *str, int pos)
8678 {
8679 int len;
8680 nk_rune unicode = 0;
8681 nk_str_at_const(str, pos, &unicode, &len);
8682 return unicode;
8683 }
8684 NK_API char*
8685 nk_str_get(struct nk_str *s)
8686 {
8687 NK_ASSERT(s);
8688 if (!s || !s->len || !s->buffer.allocated) return 0;
8689 return (char*)s->buffer.memory.ptr;
8690 }
8691 NK_API const char*
8692 nk_str_get_const(const struct nk_str *s)
8693 {
8694 NK_ASSERT(s);
8695 if (!s || !s->len || !s->buffer.allocated) return 0;
8696 return (const char*)s->buffer.memory.ptr;
8697 }
8698 NK_API int
8699 nk_str_len(struct nk_str *s)
8700 {
8701 NK_ASSERT(s);
8702 if (!s || !s->len || !s->buffer.allocated) return 0;
8703 return s->len;
8704 }
8705 NK_API int
8706 nk_str_len_char(struct nk_str *s)
8707 {
8708 NK_ASSERT(s);
8709 if (!s || !s->len || !s->buffer.allocated) return 0;
8710 return (int)s->buffer.allocated;
8711 }
8712 NK_API void
8713 nk_str_clear(struct nk_str *str)
8714 {
8715 NK_ASSERT(str);
8716 nk_buffer_clear(&str->buffer);
8717 str->len = 0;
8718 }
8719 NK_API void
8720 nk_str_free(struct nk_str *str)
8721 {
8722 NK_ASSERT(str);
8723 nk_buffer_free(&str->buffer);
8724 str->len = 0;
8725 }
8726
8727
8728
8729
8730
8731 /* ==============================================================
8732 *
8733 * DRAW
8734 *
8735 * ===============================================================*/
8736 NK_LIB void
8737 nk_command_buffer_init(struct nk_command_buffer *cb,
8738 struct nk_buffer *b, enum nk_command_clipping clip)
8739 {
8740 NK_ASSERT(cb);
8741 NK_ASSERT(b);
8742 if (!cb || !b) return;
8743 cb->base = b;
8744 cb->use_clipping = (int)clip;
8745 cb->begin = b->allocated;
8746 cb->end = b->allocated;
8747 cb->last = b->allocated;
8748 }
8749 NK_LIB void
8750 nk_command_buffer_reset(struct nk_command_buffer *b)
8751 {
8752 NK_ASSERT(b);
8753 if (!b) return;
8754 b->begin = 0;
8755 b->end = 0;
8756 b->last = 0;
8757 b->clip = nk_null_rect;
8758 #ifdef NK_INCLUDE_COMMAND_USERDATA
8759 b->userdata.ptr = 0;
8760 #endif
8761 }
8762 NK_LIB void*
8763 nk_command_buffer_push(struct nk_command_buffer* b,
8764 enum nk_command_type t, nk_size size)
8765 {
8766 NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
8767 struct nk_command *cmd;
8768 nk_size alignment;
8769 void *unaligned;
8770 void *memory;
8771
8772 NK_ASSERT(b);
8773 NK_ASSERT(b->base);
8774 if (!b) return 0;
8775 cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
8776 if (!cmd) return 0;
8777
8778 /* make sure the offset to the next command is aligned */
8779 b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
8780 unaligned = (nk_byte*)cmd + size;
8781 memory = NK_ALIGN_PTR(unaligned, align);
8782 alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
8783 #ifdef NK_ZERO_COMMAND_MEMORY
8784 NK_MEMSET(cmd, 0, size + alignment);
8785 #endif
8786
8787 cmd->type = t;
8788 cmd->next = b->base->allocated + alignment;
8789 #ifdef NK_INCLUDE_COMMAND_USERDATA
8790 cmd->userdata = b->userdata;
8791 #endif
8792 b->end = cmd->next;
8793 return cmd;
8794 }
8795 NK_API void
8796 nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
8797 {
8798 struct nk_command_scissor *cmd;
8799 NK_ASSERT(b);
8800 if (!b) return;
8801
8802 b->clip.x = r.x;
8803 b->clip.y = r.y;
8804 b->clip.w = r.w;
8805 b->clip.h = r.h;
8806 cmd = (struct nk_command_scissor*)
8807 nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
8808
8809 if (!cmd) return;
8810 cmd->x = (short)r.x;
8811 cmd->y = (short)r.y;
8812 cmd->w = (unsigned short)NK_MAX(0, r.w);
8813 cmd->h = (unsigned short)NK_MAX(0, r.h);
8814 }
8815 NK_API void
8816 nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
8817 float x1, float y1, float line_thickness, struct nk_color c)
8818 {
8819 struct nk_command_line *cmd;
8820 NK_ASSERT(b);
8821 if (!b || line_thickness <= 0) return;
8822 cmd = (struct nk_command_line*)
8823 nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
8824 if (!cmd) return;
8825 cmd->line_thickness = (unsigned short)line_thickness;
8826 cmd->begin.x = (short)x0;
8827 cmd->begin.y = (short)y0;
8828 cmd->end.x = (short)x1;
8829 cmd->end.y = (short)y1;
8830 cmd->color = c;
8831 }
8832 NK_API void
8833 nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
8834 float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
8835 float bx, float by, float line_thickness, struct nk_color col)
8836 {
8837 struct nk_command_curve *cmd;
8838 NK_ASSERT(b);
8839 if (!b || col.a == 0 || line_thickness <= 0) return;
8840
8841 cmd = (struct nk_command_curve*)
8842 nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
8843 if (!cmd) return;
8844 cmd->line_thickness = (unsigned short)line_thickness;
8845 cmd->begin.x = (short)ax;
8846 cmd->begin.y = (short)ay;
8847 cmd->ctrl[0].x = (short)ctrl0x;
8848 cmd->ctrl[0].y = (short)ctrl0y;
8849 cmd->ctrl[1].x = (short)ctrl1x;
8850 cmd->ctrl[1].y = (short)ctrl1y;
8851 cmd->end.x = (short)bx;
8852 cmd->end.y = (short)by;
8853 cmd->color = col;
8854 }
8855 NK_API void
8856 nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
8857 float rounding, float line_thickness, struct nk_color c)
8858 {
8859 struct nk_command_rect *cmd;
8860 NK_ASSERT(b);
8861 if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
8862 if (b->use_clipping) {
8863 const struct nk_rect *clip = &b->clip;
8864 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
8865 clip->x, clip->y, clip->w, clip->h)) return;
8866 }
8867 cmd = (struct nk_command_rect*)
8868 nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
8869 if (!cmd) return;
8870 cmd->rounding = (unsigned short)rounding;
8871 cmd->line_thickness = (unsigned short)line_thickness;
8872 cmd->x = (short)rect.x;
8873 cmd->y = (short)rect.y;
8874 cmd->w = (unsigned short)NK_MAX(0, rect.w);
8875 cmd->h = (unsigned short)NK_MAX(0, rect.h);
8876 cmd->color = c;
8877 }
8878 NK_API void
8879 nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
8880 float rounding, struct nk_color c)
8881 {
8882 struct nk_command_rect_filled *cmd;
8883 NK_ASSERT(b);
8884 if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
8885 if (b->use_clipping) {
8886 const struct nk_rect *clip = &b->clip;
8887 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
8888 clip->x, clip->y, clip->w, clip->h)) return;
8889 }
8890
8891 cmd = (struct nk_command_rect_filled*)
8892 nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
8893 if (!cmd) return;
8894 cmd->rounding = (unsigned short)rounding;
8895 cmd->x = (short)rect.x;
8896 cmd->y = (short)rect.y;
8897 cmd->w = (unsigned short)NK_MAX(0, rect.w);
8898 cmd->h = (unsigned short)NK_MAX(0, rect.h);
8899 cmd->color = c;
8900 }
8901 NK_API void
8902 nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
8903 struct nk_color left, struct nk_color top, struct nk_color right,
8904 struct nk_color bottom)
8905 {
8906 struct nk_command_rect_multi_color *cmd;
8907 NK_ASSERT(b);
8908 if (!b || rect.w == 0 || rect.h == 0) return;
8909 if (b->use_clipping) {
8910 const struct nk_rect *clip = &b->clip;
8911 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
8912 clip->x, clip->y, clip->w, clip->h)) return;
8913 }
8914
8915 cmd = (struct nk_command_rect_multi_color*)
8916 nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
8917 if (!cmd) return;
8918 cmd->x = (short)rect.x;
8919 cmd->y = (short)rect.y;
8920 cmd->w = (unsigned short)NK_MAX(0, rect.w);
8921 cmd->h = (unsigned short)NK_MAX(0, rect.h);
8922 cmd->left = left;
8923 cmd->top = top;
8924 cmd->right = right;
8925 cmd->bottom = bottom;
8926 }
8927 NK_API void
8928 nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
8929 float line_thickness, struct nk_color c)
8930 {
8931 struct nk_command_circle *cmd;
8932 if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
8933 if (b->use_clipping) {
8934 const struct nk_rect *clip = &b->clip;
8935 if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
8936 return;
8937 }
8938
8939 cmd = (struct nk_command_circle*)
8940 nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
8941 if (!cmd) return;
8942 cmd->line_thickness = (unsigned short)line_thickness;
8943 cmd->x = (short)r.x;
8944 cmd->y = (short)r.y;
8945 cmd->w = (unsigned short)NK_MAX(r.w, 0);
8946 cmd->h = (unsigned short)NK_MAX(r.h, 0);
8947 cmd->color = c;
8948 }
8949 NK_API void
8950 nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
8951 {
8952 struct nk_command_circle_filled *cmd;
8953 NK_ASSERT(b);
8954 if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
8955 if (b->use_clipping) {
8956 const struct nk_rect *clip = &b->clip;
8957 if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
8958 return;
8959 }
8960
8961 cmd = (struct nk_command_circle_filled*)
8962 nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
8963 if (!cmd) return;
8964 cmd->x = (short)r.x;
8965 cmd->y = (short)r.y;
8966 cmd->w = (unsigned short)NK_MAX(r.w, 0);
8967 cmd->h = (unsigned short)NK_MAX(r.h, 0);
8968 cmd->color = c;
8969 }
8970 NK_API void
8971 nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
8972 float a_min, float a_max, float line_thickness, struct nk_color c)
8973 {
8974 struct nk_command_arc *cmd;
8975 if (!b || c.a == 0 || line_thickness <= 0) return;
8976 cmd = (struct nk_command_arc*)
8977 nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
8978 if (!cmd) return;
8979 cmd->line_thickness = (unsigned short)line_thickness;
8980 cmd->cx = (short)cx;
8981 cmd->cy = (short)cy;
8982 cmd->r = (unsigned short)radius;
8983 cmd->a[0] = a_min;
8984 cmd->a[1] = a_max;
8985 cmd->color = c;
8986 }
8987 NK_API void
8988 nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
8989 float a_min, float a_max, struct nk_color c)
8990 {
8991 struct nk_command_arc_filled *cmd;
8992 NK_ASSERT(b);
8993 if (!b || c.a == 0) return;
8994 cmd = (struct nk_command_arc_filled*)
8995 nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
8996 if (!cmd) return;
8997 cmd->cx = (short)cx;
8998 cmd->cy = (short)cy;
8999 cmd->r = (unsigned short)radius;
9000 cmd->a[0] = a_min;
9001 cmd->a[1] = a_max;
9002 cmd->color = c;
9003 }
9004 NK_API void
9005 nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
9006 float y1, float x2, float y2, float line_thickness, struct nk_color c)
9007 {
9008 struct nk_command_triangle *cmd;
9009 NK_ASSERT(b);
9010 if (!b || c.a == 0 || line_thickness <= 0) return;
9011 if (b->use_clipping) {
9012 const struct nk_rect *clip = &b->clip;
9013 if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
9014 !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
9015 !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
9016 return;
9017 }
9018
9019 cmd = (struct nk_command_triangle*)
9020 nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
9021 if (!cmd) return;
9022 cmd->line_thickness = (unsigned short)line_thickness;
9023 cmd->a.x = (short)x0;
9024 cmd->a.y = (short)y0;
9025 cmd->b.x = (short)x1;
9026 cmd->b.y = (short)y1;
9027 cmd->c.x = (short)x2;
9028 cmd->c.y = (short)y2;
9029 cmd->color = c;
9030 }
9031 NK_API void
9032 nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
9033 float y1, float x2, float y2, struct nk_color c)
9034 {
9035 struct nk_command_triangle_filled *cmd;
9036 NK_ASSERT(b);
9037 if (!b || c.a == 0) return;
9038 if (!b) return;
9039 if (b->use_clipping) {
9040 const struct nk_rect *clip = &b->clip;
9041 if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
9042 !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
9043 !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
9044 return;
9045 }
9046
9047 cmd = (struct nk_command_triangle_filled*)
9048 nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
9049 if (!cmd) return;
9050 cmd->a.x = (short)x0;
9051 cmd->a.y = (short)y0;
9052 cmd->b.x = (short)x1;
9053 cmd->b.y = (short)y1;
9054 cmd->c.x = (short)x2;
9055 cmd->c.y = (short)y2;
9056 cmd->color = c;
9057 }
9058 NK_API void
9059 nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
9060 float line_thickness, struct nk_color col)
9061 {
9062 int i;
9063 nk_size size = 0;
9064 struct nk_command_polygon *cmd;
9065
9066 NK_ASSERT(b);
9067 if (!b || col.a == 0 || line_thickness <= 0) return;
9068 size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
9069 cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
9070 if (!cmd) return;
9071 cmd->color = col;
9072 cmd->line_thickness = (unsigned short)line_thickness;
9073 cmd->point_count = (unsigned short)point_count;
9074 for (i = 0; i < point_count; ++i) {
9075 cmd->points[i].x = (short)points[i*2];
9076 cmd->points[i].y = (short)points[i*2+1];
9077 }
9078 }
9079 NK_API void
9080 nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
9081 struct nk_color col)
9082 {
9083 int i;
9084 nk_size size = 0;
9085 struct nk_command_polygon_filled *cmd;
9086
9087 NK_ASSERT(b);
9088 if (!b || col.a == 0) return;
9089 size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
9090 cmd = (struct nk_command_polygon_filled*)
9091 nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
9092 if (!cmd) return;
9093 cmd->color = col;
9094 cmd->point_count = (unsigned short)point_count;
9095 for (i = 0; i < point_count; ++i) {
9096 cmd->points[i].x = (short)points[i*2+0];
9097 cmd->points[i].y = (short)points[i*2+1];
9098 }
9099 }
9100 NK_API void
9101 nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
9102 float line_thickness, struct nk_color col)
9103 {
9104 int i;
9105 nk_size size = 0;
9106 struct nk_command_polyline *cmd;
9107
9108 NK_ASSERT(b);
9109 if (!b || col.a == 0 || line_thickness <= 0) return;
9110 size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
9111 cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
9112 if (!cmd) return;
9113 cmd->color = col;
9114 cmd->point_count = (unsigned short)point_count;
9115 cmd->line_thickness = (unsigned short)line_thickness;
9116 for (i = 0; i < point_count; ++i) {
9117 cmd->points[i].x = (short)points[i*2];
9118 cmd->points[i].y = (short)points[i*2+1];
9119 }
9120 }
9121 NK_API void
9122 nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
9123 const struct nk_image *img, struct nk_color col)
9124 {
9125 struct nk_command_image *cmd;
9126 NK_ASSERT(b);
9127 if (!b) return;
9128 if (b->use_clipping) {
9129 const struct nk_rect *c = &b->clip;
9130 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))
9131 return;
9132 }
9133
9134 cmd = (struct nk_command_image*)
9135 nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
9136 if (!cmd) return;
9137 cmd->x = (short)r.x;
9138 cmd->y = (short)r.y;
9139 cmd->w = (unsigned short)NK_MAX(0, r.w);
9140 cmd->h = (unsigned short)NK_MAX(0, r.h);
9141 cmd->img = *img;
9142 cmd->col = col;
9143 }
9144 NK_API void
9145 nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
9146 nk_command_custom_callback cb, nk_handle usr)
9147 {
9148 struct nk_command_custom *cmd;
9149 NK_ASSERT(b);
9150 if (!b) return;
9151 if (b->use_clipping) {
9152 const struct nk_rect *c = &b->clip;
9153 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))
9154 return;
9155 }
9156
9157 cmd = (struct nk_command_custom*)
9158 nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
9159 if (!cmd) return;
9160 cmd->x = (short)r.x;
9161 cmd->y = (short)r.y;
9162 cmd->w = (unsigned short)NK_MAX(0, r.w);
9163 cmd->h = (unsigned short)NK_MAX(0, r.h);
9164 cmd->callback_data = usr;
9165 cmd->callback = cb;
9166 }
9167 NK_API void
9168 nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
9169 const char *string, int length, const struct nk_user_font *font,
9170 struct nk_color bg, struct nk_color fg)
9171 {
9172 float text_width = 0;
9173 struct nk_command_text *cmd;
9174
9175 NK_ASSERT(b);
9176 NK_ASSERT(font);
9177 if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
9178 if (b->use_clipping) {
9179 const struct nk_rect *c = &b->clip;
9180 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))
9181 return;
9182 }
9183
9184 /* make sure text fits inside bounds */
9185 text_width = font->width(font->userdata, font->height, string, length);
9186 if (text_width > r.w){
9187 int glyphs = 0;
9188 float txt_width = (float)text_width;
9189 length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
9190 }
9191
9192 if (!length) return;
9193 cmd = (struct nk_command_text*)
9194 nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
9195 if (!cmd) return;
9196 cmd->x = (short)r.x;
9197 cmd->y = (short)r.y;
9198 cmd->w = (unsigned short)r.w;
9199 cmd->h = (unsigned short)r.h;
9200 cmd->background = bg;
9201 cmd->foreground = fg;
9202 cmd->font = font;
9203 cmd->length = length;
9204 cmd->height = font->height;
9205 NK_MEMCPY(cmd->string, string, (nk_size)length);
9206 cmd->string[length] = '\0';
9207 }
9208
9209
9210
9211
9212
9213 /* ===============================================================
9214 *
9215 * VERTEX
9216 *
9217 * ===============================================================*/
9218 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
9219 NK_API void
9220 nk_draw_list_init(struct nk_draw_list *list)
9221 {
9222 nk_size i = 0;
9223 NK_ASSERT(list);
9224 if (!list) return;
9225 nk_zero(list, sizeof(*list));
9226 for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
9227 const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
9228 list->circle_vtx[i].x = (float)NK_COS(a);
9229 list->circle_vtx[i].y = (float)NK_SIN(a);
9230 }
9231 }
9232 NK_API void
9233 nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
9234 struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
9235 enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
9236 {
9237 NK_ASSERT(canvas);
9238 NK_ASSERT(config);
9239 NK_ASSERT(cmds);
9240 NK_ASSERT(vertices);
9241 NK_ASSERT(elements);
9242 if (!canvas || !config || !cmds || !vertices || !elements)
9243 return;
9244
9245 canvas->buffer = cmds;
9246 canvas->config = *config;
9247 canvas->elements = elements;
9248 canvas->vertices = vertices;
9249 canvas->line_AA = line_aa;
9250 canvas->shape_AA = shape_aa;
9251 canvas->clip_rect = nk_null_rect;
9252
9253 canvas->cmd_offset = 0;
9254 canvas->element_count = 0;
9255 canvas->vertex_count = 0;
9256 canvas->cmd_offset = 0;
9257 canvas->cmd_count = 0;
9258 canvas->path_count = 0;
9259 }
9260 NK_API const struct nk_draw_command*
9261 nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
9262 {
9263 nk_byte *memory;
9264 nk_size offset;
9265 const struct nk_draw_command *cmd;
9266
9267 NK_ASSERT(buffer);
9268 if (!buffer || !buffer->size || !canvas->cmd_count)
9269 return 0;
9270
9271 memory = (nk_byte*)buffer->memory.ptr;
9272 offset = buffer->memory.size - canvas->cmd_offset;
9273 cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
9274 return cmd;
9275 }
9276 NK_API const struct nk_draw_command*
9277 nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
9278 {
9279 nk_size size;
9280 nk_size offset;
9281 nk_byte *memory;
9282 const struct nk_draw_command *end;
9283
9284 NK_ASSERT(buffer);
9285 NK_ASSERT(canvas);
9286 if (!buffer || !canvas)
9287 return 0;
9288
9289 memory = (nk_byte*)buffer->memory.ptr;
9290 size = buffer->memory.size;
9291 offset = size - canvas->cmd_offset;
9292 end = nk_ptr_add(const struct nk_draw_command, memory, offset);
9293 end -= (canvas->cmd_count-1);
9294 return end;
9295 }
9296 NK_API const struct nk_draw_command*
9297 nk__draw_list_next(const struct nk_draw_command *cmd,
9298 const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
9299 {
9300 const struct nk_draw_command *end;
9301 NK_ASSERT(buffer);
9302 NK_ASSERT(canvas);
9303 if (!cmd || !buffer || !canvas)
9304 return 0;
9305
9306 end = nk__draw_list_end(canvas, buffer);
9307 if (cmd <= end) return 0;
9308 return (cmd-1);
9309 }
9310 NK_INTERN struct nk_vec2*
9311 nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
9312 {
9313 struct nk_vec2 *points;
9314 NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
9315 NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
9316 points = (struct nk_vec2*)
9317 nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
9318 point_size * (nk_size)count, point_align);
9319
9320 if (!points) return 0;
9321 if (!list->path_offset) {
9322 void *memory = nk_buffer_memory(list->buffer);
9323 list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
9324 }
9325 list->path_count += (unsigned int)count;
9326 return points;
9327 }
9328 NK_INTERN struct nk_vec2
9329 nk_draw_list_path_last(struct nk_draw_list *list)
9330 {
9331 void *memory;
9332 struct nk_vec2 *point;
9333 NK_ASSERT(list->path_count);
9334 memory = nk_buffer_memory(list->buffer);
9335 point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
9336 point += (list->path_count-1);
9337 return *point;
9338 }
9339 NK_INTERN struct nk_draw_command*
9340 nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
9341 nk_handle texture)
9342 {
9343 NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
9344 NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
9345 struct nk_draw_command *cmd;
9346
9347 NK_ASSERT(list);
9348 cmd = (struct nk_draw_command*)
9349 nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
9350
9351 if (!cmd) return 0;
9352 if (!list->cmd_count) {
9353 nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
9354 nk_size total = nk_buffer_total(list->buffer);
9355 memory = nk_ptr_add(nk_byte, memory, total);
9356 list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
9357 }
9358
9359 cmd->elem_count = 0;
9360 cmd->clip_rect = clip;
9361 cmd->texture = texture;
9362 #ifdef NK_INCLUDE_COMMAND_USERDATA
9363 cmd->userdata = list->userdata;
9364 #endif
9365
9366 list->cmd_count++;
9367 list->clip_rect = clip;
9368 return cmd;
9369 }
9370 NK_INTERN struct nk_draw_command*
9371 nk_draw_list_command_last(struct nk_draw_list *list)
9372 {
9373 void *memory;
9374 nk_size size;
9375 struct nk_draw_command *cmd;
9376 NK_ASSERT(list->cmd_count);
9377
9378 memory = nk_buffer_memory(list->buffer);
9379 size = nk_buffer_total(list->buffer);
9380 cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
9381 return (cmd - (list->cmd_count-1));
9382 }
9383 NK_INTERN void
9384 nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
9385 {
9386 NK_ASSERT(list);
9387 if (!list) return;
9388 if (!list->cmd_count) {
9389 nk_draw_list_push_command(list, rect, list->config.null.texture);
9390 } else {
9391 struct nk_draw_command *prev = nk_draw_list_command_last(list);
9392 if (prev->elem_count == 0)
9393 prev->clip_rect = rect;
9394 nk_draw_list_push_command(list, rect, prev->texture);
9395 }
9396 }
9397 NK_INTERN void
9398 nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
9399 {
9400 NK_ASSERT(list);
9401 if (!list) return;
9402 if (!list->cmd_count) {
9403 nk_draw_list_push_command(list, nk_null_rect, texture);
9404 } else {
9405 struct nk_draw_command *prev = nk_draw_list_command_last(list);
9406 if (prev->elem_count == 0) {
9407 prev->texture = texture;
9408 #ifdef NK_INCLUDE_COMMAND_USERDATA
9409 prev->userdata = list->userdata;
9410 #endif
9411 } else if (prev->texture.id != texture.id
9412 #ifdef NK_INCLUDE_COMMAND_USERDATA
9413 || prev->userdata.id != list->userdata.id
9414 #endif
9415 ) nk_draw_list_push_command(list, prev->clip_rect, texture);
9416 }
9417 }
9418 #ifdef NK_INCLUDE_COMMAND_USERDATA
9419 NK_API void
9420 nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
9421 {
9422 list->userdata = userdata;
9423 }
9424 #endif
9425 NK_INTERN void*
9426 nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
9427 {
9428 void *vtx;
9429 NK_ASSERT(list);
9430 if (!list) return 0;
9431 vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
9432 list->config.vertex_size*count, list->config.vertex_alignment);
9433 if (!vtx) return 0;
9434 list->vertex_count += (unsigned int)count;
9435
9436 /* This assert triggers because your are drawing a lot of stuff and nuklear
9437 * defined `nk_draw_index` as `nk_ushort` to safe space be default.
9438 *
9439 * So you reached the maximum number of indicies or rather vertexes.
9440 * To solve this issue please change typdef `nk_draw_index` to `nk_uint`
9441 * and don't forget to specify the new element size in your drawing
9442 * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
9443 * instead of specifing `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
9444 * Sorry for the inconvenience. */
9445 if(sizeof(nk_draw_index)==2) NK_ASSERT((list->vertex_count < NK_USHORT_MAX &&
9446 "To many verticies for 16-bit vertex indicies. Please read comment above on how to solve this problem"));
9447 return vtx;
9448 }
9449 NK_INTERN nk_draw_index*
9450 nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
9451 {
9452 nk_draw_index *ids;
9453 struct nk_draw_command *cmd;
9454 NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
9455 NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
9456 NK_ASSERT(list);
9457 if (!list) return 0;
9458
9459 ids = (nk_draw_index*)
9460 nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
9461 if (!ids) return 0;
9462 cmd = nk_draw_list_command_last(list);
9463 list->element_count += (unsigned int)count;
9464 cmd->elem_count += (unsigned int)count;
9465 return ids;
9466 }
9467 NK_INTERN int
9468 nk_draw_vertex_layout_element_is_end_of_layout(
9469 const struct nk_draw_vertex_layout_element *element)
9470 {
9471 return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
9472 element->format == NK_FORMAT_COUNT);
9473 }
9474 NK_INTERN void
9475 nk_draw_vertex_color(void *attr, const float *vals,
9476 enum nk_draw_vertex_layout_format format)
9477 {
9478 /* if this triggers you tried to provide a value format for a color */
9479 float val[4];
9480 NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
9481 NK_ASSERT(format <= NK_FORMAT_COLOR_END);
9482 if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
9483
9484 val[0] = NK_SATURATE(vals[0]);
9485 val[1] = NK_SATURATE(vals[1]);
9486 val[2] = NK_SATURATE(vals[2]);
9487 val[3] = NK_SATURATE(vals[3]);
9488
9489 switch (format) {
9490 default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
9491 case NK_FORMAT_R8G8B8A8:
9492 case NK_FORMAT_R8G8B8: {
9493 struct nk_color col = nk_rgba_fv(val);
9494 NK_MEMCPY(attr, &col.r, sizeof(col));
9495 } break;
9496 case NK_FORMAT_B8G8R8A8: {
9497 struct nk_color col = nk_rgba_fv(val);
9498 struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
9499 NK_MEMCPY(attr, &bgra, sizeof(bgra));
9500 } break;
9501 case NK_FORMAT_R16G15B16: {
9502 nk_ushort col[3];
9503 col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
9504 col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
9505 col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
9506 NK_MEMCPY(attr, col, sizeof(col));
9507 } break;
9508 case NK_FORMAT_R16G15B16A16: {
9509 nk_ushort col[4];
9510 col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
9511 col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
9512 col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
9513 col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
9514 NK_MEMCPY(attr, col, sizeof(col));
9515 } break;
9516 case NK_FORMAT_R32G32B32: {
9517 nk_uint col[3];
9518 col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
9519 col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
9520 col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
9521 NK_MEMCPY(attr, col, sizeof(col));
9522 } break;
9523 case NK_FORMAT_R32G32B32A32: {
9524 nk_uint col[4];
9525 col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
9526 col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
9527 col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
9528 col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
9529 NK_MEMCPY(attr, col, sizeof(col));
9530 } break;
9531 case NK_FORMAT_R32G32B32A32_FLOAT:
9532 NK_MEMCPY(attr, val, sizeof(float)*4);
9533 break;
9534 case NK_FORMAT_R32G32B32A32_DOUBLE: {
9535 double col[4];
9536 col[0] = (double)val[0];
9537 col[1] = (double)val[1];
9538 col[2] = (double)val[2];
9539 col[3] = (double)val[3];
9540 NK_MEMCPY(attr, col, sizeof(col));
9541 } break;
9542 case NK_FORMAT_RGB32:
9543 case NK_FORMAT_RGBA32: {
9544 struct nk_color col = nk_rgba_fv(val);
9545 nk_uint color = nk_color_u32(col);
9546 NK_MEMCPY(attr, &color, sizeof(color));
9547 } break; }
9548 }
9549 NK_INTERN void
9550 nk_draw_vertex_element(void *dst, const float *values, int value_count,
9551 enum nk_draw_vertex_layout_format format)
9552 {
9553 int value_index;
9554 void *attribute = dst;
9555 /* if this triggers you tried to provide a color format for a value */
9556 NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
9557 if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
9558 for (value_index = 0; value_index < value_count; ++value_index) {
9559 switch (format) {
9560 default: NK_ASSERT(0 && "invalid vertex layout format"); break;
9561 case NK_FORMAT_SCHAR: {
9562 char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
9563 NK_MEMCPY(attribute, &value, sizeof(value));
9564 attribute = (void*)((char*)attribute + sizeof(char));
9565 } break;
9566 case NK_FORMAT_SSHORT: {
9567 nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
9568 NK_MEMCPY(attribute, &value, sizeof(value));
9569 attribute = (void*)((char*)attribute + sizeof(value));
9570 } break;
9571 case NK_FORMAT_SINT: {
9572 nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
9573 NK_MEMCPY(attribute, &value, sizeof(value));
9574 attribute = (void*)((char*)attribute + sizeof(nk_int));
9575 } break;
9576 case NK_FORMAT_UCHAR: {
9577 unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
9578 NK_MEMCPY(attribute, &value, sizeof(value));
9579 attribute = (void*)((char*)attribute + sizeof(unsigned char));
9580 } break;
9581 case NK_FORMAT_USHORT: {
9582 nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
9583 NK_MEMCPY(attribute, &value, sizeof(value));
9584 attribute = (void*)((char*)attribute + sizeof(value));
9585 } break;
9586 case NK_FORMAT_UINT: {
9587 nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
9588 NK_MEMCPY(attribute, &value, sizeof(value));
9589 attribute = (void*)((char*)attribute + sizeof(nk_uint));
9590 } break;
9591 case NK_FORMAT_FLOAT:
9592 NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
9593 attribute = (void*)((char*)attribute + sizeof(float));
9594 break;
9595 case NK_FORMAT_DOUBLE: {
9596 double value = (double)values[value_index];
9597 NK_MEMCPY(attribute, &value, sizeof(value));
9598 attribute = (void*)((char*)attribute + sizeof(double));
9599 } break;
9600 }
9601 }
9602 }
9603 NK_INTERN void*
9604 nk_draw_vertex(void *dst, const struct nk_convert_config *config,
9605 struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
9606 {
9607 void *result = (void*)((char*)dst + config->vertex_size);
9608 const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
9609 while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
9610 void *address = (void*)((char*)dst + elem_iter->offset);
9611 switch (elem_iter->attribute) {
9612 case NK_VERTEX_ATTRIBUTE_COUNT:
9613 default: NK_ASSERT(0 && "wrong element attribute"); break;
9614 case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
9615 case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
9616 case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
9617 }
9618 elem_iter++;
9619 }
9620 return result;
9621 }
9622 NK_API void
9623 nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
9624 const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
9625 float thickness, enum nk_anti_aliasing aliasing)
9626 {
9627 nk_size count;
9628 int thick_line;
9629 struct nk_colorf col;
9630 struct nk_colorf col_trans;
9631 NK_ASSERT(list);
9632 if (!list || points_count < 2) return;
9633
9634 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
9635 count = points_count;
9636 if (!closed) count = points_count-1;
9637 thick_line = thickness > 1.0f;
9638
9639 #ifdef NK_INCLUDE_COMMAND_USERDATA
9640 nk_draw_list_push_userdata(list, list->userdata);
9641 #endif
9642
9643 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
9644 nk_color_fv(&col.r, color);
9645 col_trans = col;
9646 col_trans.a = 0;
9647
9648 if (aliasing == NK_ANTI_ALIASING_ON) {
9649 /* ANTI-ALIASED STROKE */
9650 const float AA_SIZE = 1.0f;
9651 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
9652 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
9653
9654 /* allocate vertices and elements */
9655 nk_size i1 = 0;
9656 nk_size vertex_offset;
9657 nk_size index = list->vertex_count;
9658
9659 const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
9660 const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
9661
9662 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
9663 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
9664
9665 nk_size size;
9666 struct nk_vec2 *normals, *temp;
9667 if (!vtx || !ids) return;
9668
9669 /* temporary allocate normals + points */
9670 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
9671 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
9672 size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
9673 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
9674 if (!normals) return;
9675 temp = normals + points_count;
9676
9677 /* make sure vertex pointer is still correct */
9678 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
9679
9680 /* calculate normals */
9681 for (i1 = 0; i1 < count; ++i1) {
9682 const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
9683 struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
9684 float len;
9685
9686 /* vec2 inverted length */
9687 len = nk_vec2_len_sqr(diff);
9688 if (len != 0.0f)
9689 len = nk_inv_sqrt(len);
9690 else len = 1.0f;
9691
9692 diff = nk_vec2_muls(diff, len);
9693 normals[i1].x = diff.y;
9694 normals[i1].y = -diff.x;
9695 }
9696
9697 if (!closed)
9698 normals[points_count-1] = normals[points_count-2];
9699
9700 if (!thick_line) {
9701 nk_size idx1, i;
9702 if (!closed) {
9703 struct nk_vec2 d;
9704 temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
9705 temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
9706 d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
9707 temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
9708 temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
9709 }
9710
9711 /* fill elements */
9712 idx1 = index;
9713 for (i1 = 0; i1 < count; i1++) {
9714 struct nk_vec2 dm;
9715 float dmr2;
9716 nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
9717 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
9718
9719 /* average normals */
9720 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
9721 dmr2 = dm.x * dm.x + dm.y* dm.y;
9722 if (dmr2 > 0.000001f) {
9723 float scale = 1.0f/dmr2;
9724 scale = NK_MIN(100.0f, scale);
9725 dm = nk_vec2_muls(dm, scale);
9726 }
9727
9728 dm = nk_vec2_muls(dm, AA_SIZE);
9729 temp[i2*2+0] = nk_vec2_add(points[i2], dm);
9730 temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
9731
9732 ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
9733 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
9734 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
9735 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
9736 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
9737 ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
9738 ids += 12;
9739 idx1 = idx2;
9740 }
9741
9742 /* fill vertices */
9743 for (i = 0; i < points_count; ++i) {
9744 const struct nk_vec2 uv = list->config.null.uv;
9745 vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
9746 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
9747 vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
9748 }
9749 } else {
9750 nk_size idx1, i;
9751 const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
9752 if (!closed) {
9753 struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
9754 struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
9755
9756 temp[0] = nk_vec2_add(points[0], d1);
9757 temp[1] = nk_vec2_add(points[0], d2);
9758 temp[2] = nk_vec2_sub(points[0], d2);
9759 temp[3] = nk_vec2_sub(points[0], d1);
9760
9761 d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
9762 d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
9763
9764 temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
9765 temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
9766 temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
9767 temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
9768 }
9769
9770 /* add all elements */
9771 idx1 = index;
9772 for (i1 = 0; i1 < count; ++i1) {
9773 struct nk_vec2 dm_out, dm_in;
9774 const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
9775 nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
9776
9777 /* average normals */
9778 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
9779 float dmr2 = dm.x * dm.x + dm.y* dm.y;
9780 if (dmr2 > 0.000001f) {
9781 float scale = 1.0f/dmr2;
9782 scale = NK_MIN(100.0f, scale);
9783 dm = nk_vec2_muls(dm, scale);
9784 }
9785
9786 dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
9787 dm_in = nk_vec2_muls(dm, half_inner_thickness);
9788 temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
9789 temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
9790 temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
9791 temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
9792
9793 /* add indexes */
9794 ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
9795 ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
9796 ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
9797 ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
9798 ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
9799 ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
9800 ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
9801 ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
9802 ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
9803 ids += 18;
9804 idx1 = idx2;
9805 }
9806
9807 /* add vertices */
9808 for (i = 0; i < points_count; ++i) {
9809 const struct nk_vec2 uv = list->config.null.uv;
9810 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
9811 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
9812 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
9813 vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
9814 }
9815 }
9816 /* free temporary normals + points */
9817 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
9818 } else {
9819 /* NON ANTI-ALIASED STROKE */
9820 nk_size i1 = 0;
9821 nk_size idx = list->vertex_count;
9822 const nk_size idx_count = count * 6;
9823 const nk_size vtx_count = count * 4;
9824 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
9825 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
9826 if (!vtx || !ids) return;
9827
9828 for (i1 = 0; i1 < count; ++i1) {
9829 float dx, dy;
9830 const struct nk_vec2 uv = list->config.null.uv;
9831 const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
9832 const struct nk_vec2 p1 = points[i1];
9833 const struct nk_vec2 p2 = points[i2];
9834 struct nk_vec2 diff = nk_vec2_sub(p2, p1);
9835 float len;
9836
9837 /* vec2 inverted length */
9838 len = nk_vec2_len_sqr(diff);
9839 if (len != 0.0f)
9840 len = nk_inv_sqrt(len);
9841 else len = 1.0f;
9842 diff = nk_vec2_muls(diff, len);
9843
9844 /* add vertices */
9845 dx = diff.x * (thickness * 0.5f);
9846 dy = diff.y * (thickness * 0.5f);
9847
9848 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
9849 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
9850 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
9851 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
9852
9853 ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
9854 ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
9855 ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
9856
9857 ids += 6;
9858 idx += 4;
9859 }
9860 }
9861 }
9862 NK_API void
9863 nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
9864 const struct nk_vec2 *points, const unsigned int points_count,
9865 struct nk_color color, enum nk_anti_aliasing aliasing)
9866 {
9867 struct nk_colorf col;
9868 struct nk_colorf col_trans;
9869
9870 NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
9871 NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
9872 NK_ASSERT(list);
9873 if (!list || points_count < 3) return;
9874
9875 #ifdef NK_INCLUDE_COMMAND_USERDATA
9876 nk_draw_list_push_userdata(list, list->userdata);
9877 #endif
9878
9879 color.a = (nk_byte)((float)color.a * list->config.global_alpha);
9880 nk_color_fv(&col.r, color);
9881 col_trans = col;
9882 col_trans.a = 0;
9883
9884 if (aliasing == NK_ANTI_ALIASING_ON) {
9885 nk_size i = 0;
9886 nk_size i0 = 0;
9887 nk_size i1 = 0;
9888
9889 const float AA_SIZE = 1.0f;
9890 nk_size vertex_offset = 0;
9891 nk_size index = list->vertex_count;
9892
9893 const nk_size idx_count = (points_count-2)*3 + points_count*6;
9894 const nk_size vtx_count = (points_count*2);
9895
9896 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
9897 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
9898
9899 nk_size size = 0;
9900 struct nk_vec2 *normals = 0;
9901 unsigned int vtx_inner_idx = (unsigned int)(index + 0);
9902 unsigned int vtx_outer_idx = (unsigned int)(index + 1);
9903 if (!vtx || !ids) return;
9904
9905 /* temporary allocate normals */
9906 vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
9907 nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
9908 size = pnt_size * points_count;
9909 normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
9910 if (!normals) return;
9911 vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
9912
9913 /* add elements */
9914 for (i = 2; i < points_count; i++) {
9915 ids[0] = (nk_draw_index)(vtx_inner_idx);
9916 ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
9917 ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
9918 ids += 3;
9919 }
9920
9921 /* compute normals */
9922 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
9923 struct nk_vec2 p0 = points[i0];
9924 struct nk_vec2 p1 = points[i1];
9925 struct nk_vec2 diff = nk_vec2_sub(p1, p0);
9926
9927 /* vec2 inverted length */
9928 float len = nk_vec2_len_sqr(diff);
9929 if (len != 0.0f)
9930 len = nk_inv_sqrt(len);
9931 else len = 1.0f;
9932 diff = nk_vec2_muls(diff, len);
9933
9934 normals[i0].x = diff.y;
9935 normals[i0].y = -diff.x;
9936 }
9937
9938 /* add vertices + indexes */
9939 for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
9940 const struct nk_vec2 uv = list->config.null.uv;
9941 struct nk_vec2 n0 = normals[i0];
9942 struct nk_vec2 n1 = normals[i1];
9943 struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
9944 float dmr2 = dm.x*dm.x + dm.y*dm.y;
9945 if (dmr2 > 0.000001f) {
9946 float scale = 1.0f / dmr2;
9947 scale = NK_MIN(scale, 100.0f);
9948 dm = nk_vec2_muls(dm, scale);
9949 }
9950 dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
9951
9952 /* add vertices */
9953 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
9954 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
9955
9956 /* add indexes */
9957 ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
9958 ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
9959 ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
9960 ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
9961 ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
9962 ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
9963 ids += 6;
9964 }
9965 /* free temporary normals + points */
9966 nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
9967 } else {
9968 nk_size i = 0;
9969 nk_size index = list->vertex_count;
9970 const nk_size idx_count = (points_count-2)*3;
9971 const nk_size vtx_count = points_count;
9972 void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
9973 nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
9974
9975 if (!vtx || !ids) return;
9976 for (i = 0; i < vtx_count; ++i)
9977 vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
9978 for (i = 2; i < points_count; ++i) {
9979 ids[0] = (nk_draw_index)index;
9980 ids[1] = (nk_draw_index)(index+ i - 1);
9981 ids[2] = (nk_draw_index)(index+i);
9982 ids += 3;
9983 }
9984 }
9985 }
9986 NK_API void
9987 nk_draw_list_path_clear(struct nk_draw_list *list)
9988 {
9989 NK_ASSERT(list);
9990 if (!list) return;
9991 nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
9992 list->path_count = 0;
9993 list->path_offset = 0;
9994 }
9995 NK_API void
9996 nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
9997 {
9998 struct nk_vec2 *points = 0;
9999 struct nk_draw_command *cmd = 0;
10000 NK_ASSERT(list);
10001 if (!list) return;
10002 if (!list->cmd_count)
10003 nk_draw_list_add_clip(list, nk_null_rect);
10004
10005 cmd = nk_draw_list_command_last(list);
10006 if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
10007 nk_draw_list_push_image(list, list->config.null.texture);
10008
10009 points = nk_draw_list_alloc_path(list, 1);
10010 if (!points) return;
10011 points[0] = pos;
10012 }
10013 NK_API void
10014 nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
10015 float radius, int a_min, int a_max)
10016 {
10017 int a = 0;
10018 NK_ASSERT(list);
10019 if (!list) return;
10020 if (a_min <= a_max) {
10021 for (a = a_min; a <= a_max; a++) {
10022 const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
10023 const float x = center.x + c.x * radius;
10024 const float y = center.y + c.y * radius;
10025 nk_draw_list_path_line_to(list, nk_vec2(x, y));
10026 }
10027 }
10028 }
10029 NK_API void
10030 nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
10031 float radius, float a_min, float a_max, unsigned int segments)
10032 {
10033 unsigned int i = 0;
10034 NK_ASSERT(list);
10035 if (!list) return;
10036 if (radius == 0.0f) return;
10037
10038 /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
10039 sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
10040 cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
10041
10042 Two coordinates (x, y) of a point on a circle centered on
10043 the origin can be written in polar form as:
10044 x = r * cos(a)
10045 y = r * sin(a)
10046 where r is the radius of the circle,
10047 a is the angle between (x, y) and the origin.
10048
10049 This allows us to rotate the coordinates around the
10050 origin by an angle b using the following transformation:
10051 x' = r * cos(a + b) = x * cos(b) - y * sin(b)
10052 y' = r * sin(a + b) = y * cos(b) + x * sin(b)
10053
10054 [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
10055 */
10056 {const float d_angle = (a_max - a_min) / (float)segments;
10057 const float sin_d = (float)NK_SIN(d_angle);
10058 const float cos_d = (float)NK_COS(d_angle);
10059
10060 float cx = (float)NK_COS(a_min) * radius;
10061 float cy = (float)NK_SIN(a_min) * radius;
10062 for(i = 0; i <= segments; ++i) {
10063 float new_cx, new_cy;
10064 const float x = center.x + cx;
10065 const float y = center.y + cy;
10066 nk_draw_list_path_line_to(list, nk_vec2(x, y));
10067
10068 new_cx = cx * cos_d - cy * sin_d;
10069 new_cy = cy * cos_d + cx * sin_d;
10070 cx = new_cx;
10071 cy = new_cy;
10072 }}
10073 }
10074 NK_API void
10075 nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
10076 struct nk_vec2 b, float rounding)
10077 {
10078 float r;
10079 NK_ASSERT(list);
10080 if (!list) return;
10081 r = rounding;
10082 r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
10083 r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
10084
10085 if (r == 0.0f) {
10086 nk_draw_list_path_line_to(list, a);
10087 nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
10088 nk_draw_list_path_line_to(list, b);
10089 nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
10090 } else {
10091 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
10092 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
10093 nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
10094 nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
10095 }
10096 }
10097 NK_API void
10098 nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
10099 struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
10100 {
10101 float t_step;
10102 unsigned int i_step;
10103 struct nk_vec2 p1;
10104
10105 NK_ASSERT(list);
10106 NK_ASSERT(list->path_count);
10107 if (!list || !list->path_count) return;
10108 num_segments = NK_MAX(num_segments, 1);
10109
10110 p1 = nk_draw_list_path_last(list);
10111 t_step = 1.0f/(float)num_segments;
10112 for (i_step = 1; i_step <= num_segments; ++i_step) {
10113 float t = t_step * (float)i_step;
10114 float u = 1.0f - t;
10115 float w1 = u*u*u;
10116 float w2 = 3*u*u*t;
10117 float w3 = 3*u*t*t;
10118 float w4 = t * t *t;
10119 float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
10120 float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
10121 nk_draw_list_path_line_to(list, nk_vec2(x,y));
10122 }
10123 }
10124 NK_API void
10125 nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
10126 {
10127 struct nk_vec2 *points;
10128 NK_ASSERT(list);
10129 if (!list) return;
10130 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
10131 nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
10132 nk_draw_list_path_clear(list);
10133 }
10134 NK_API void
10135 nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
10136 enum nk_draw_list_stroke closed, float thickness)
10137 {
10138 struct nk_vec2 *points;
10139 NK_ASSERT(list);
10140 if (!list) return;
10141 points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
10142 nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
10143 closed, thickness, list->config.line_AA);
10144 nk_draw_list_path_clear(list);
10145 }
10146 NK_API void
10147 nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
10148 struct nk_vec2 b, struct nk_color col, float thickness)
10149 {
10150 NK_ASSERT(list);
10151 if (!list || !col.a) return;
10152 if (list->line_AA == NK_ANTI_ALIASING_ON) {
10153 nk_draw_list_path_line_to(list, a);
10154 nk_draw_list_path_line_to(list, b);
10155 } else {
10156 nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
10157 nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
10158 }
10159 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
10160 }
10161 NK_API void
10162 nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
10163 struct nk_color col, float rounding)
10164 {
10165 NK_ASSERT(list);
10166 if (!list || !col.a) return;
10167
10168 if (list->line_AA == NK_ANTI_ALIASING_ON) {
10169 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
10170 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
10171 } else {
10172 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
10173 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
10174 } nk_draw_list_path_fill(list, col);
10175 }
10176 NK_API void
10177 nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
10178 struct nk_color col, float rounding, float thickness)
10179 {
10180 NK_ASSERT(list);
10181 if (!list || !col.a) return;
10182 if (list->line_AA == NK_ANTI_ALIASING_ON) {
10183 nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
10184 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
10185 } else {
10186 nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
10187 nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
10188 } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
10189 }
10190 NK_API void
10191 nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
10192 struct nk_color left, struct nk_color top, struct nk_color right,
10193 struct nk_color bottom)
10194 {
10195 void *vtx;
10196 struct nk_colorf col_left, col_top;
10197 struct nk_colorf col_right, col_bottom;
10198 nk_draw_index *idx;
10199 nk_draw_index index;
10200
10201 nk_color_fv(&col_left.r, left);
10202 nk_color_fv(&col_right.r, right);
10203 nk_color_fv(&col_top.r, top);
10204 nk_color_fv(&col_bottom.r, bottom);
10205
10206 NK_ASSERT(list);
10207 if (!list) return;
10208
10209 nk_draw_list_push_image(list, list->config.null.texture);
10210 index = (nk_draw_index)list->vertex_count;
10211 vtx = nk_draw_list_alloc_vertices(list, 4);
10212 idx = nk_draw_list_alloc_elements(list, 6);
10213 if (!vtx || !idx) return;
10214
10215 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
10216 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
10217 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
10218
10219 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
10220 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
10221 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
10222 vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
10223 }
10224 NK_API void
10225 nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
10226 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
10227 {
10228 NK_ASSERT(list);
10229 if (!list || !col.a) return;
10230 nk_draw_list_path_line_to(list, a);
10231 nk_draw_list_path_line_to(list, b);
10232 nk_draw_list_path_line_to(list, c);
10233 nk_draw_list_path_fill(list, col);
10234 }
10235 NK_API void
10236 nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
10237 struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
10238 {
10239 NK_ASSERT(list);
10240 if (!list || !col.a) return;
10241 nk_draw_list_path_line_to(list, a);
10242 nk_draw_list_path_line_to(list, b);
10243 nk_draw_list_path_line_to(list, c);
10244 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
10245 }
10246 NK_API void
10247 nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
10248 float radius, struct nk_color col, unsigned int segs)
10249 {
10250 float a_max;
10251 NK_ASSERT(list);
10252 if (!list || !col.a) return;
10253 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
10254 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
10255 nk_draw_list_path_fill(list, col);
10256 }
10257 NK_API void
10258 nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
10259 float radius, struct nk_color col, unsigned int segs, float thickness)
10260 {
10261 float a_max;
10262 NK_ASSERT(list);
10263 if (!list || !col.a) return;
10264 a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
10265 nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
10266 nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
10267 }
10268 NK_API void
10269 nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
10270 struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
10271 struct nk_color col, unsigned int segments, float thickness)
10272 {
10273 NK_ASSERT(list);
10274 if (!list || !col.a) return;
10275 nk_draw_list_path_line_to(list, p0);
10276 nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
10277 nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
10278 }
10279 NK_INTERN void
10280 nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
10281 struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
10282 struct nk_color color)
10283 {
10284 void *vtx;
10285 struct nk_vec2 uvb;
10286 struct nk_vec2 uvd;
10287 struct nk_vec2 b;
10288 struct nk_vec2 d;
10289
10290 struct nk_colorf col;
10291 nk_draw_index *idx;
10292 nk_draw_index index;
10293 NK_ASSERT(list);
10294 if (!list) return;
10295
10296 nk_color_fv(&col.r, color);
10297 uvb = nk_vec2(uvc.x, uva.y);
10298 uvd = nk_vec2(uva.x, uvc.y);
10299 b = nk_vec2(c.x, a.y);
10300 d = nk_vec2(a.x, c.y);
10301
10302 index = (nk_draw_index)list->vertex_count;
10303 vtx = nk_draw_list_alloc_vertices(list, 4);
10304 idx = nk_draw_list_alloc_elements(list, 6);
10305 if (!vtx || !idx) return;
10306
10307 idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
10308 idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
10309 idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
10310
10311 vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
10312 vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
10313 vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
10314 vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
10315 }
10316 NK_API void
10317 nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
10318 struct nk_rect rect, struct nk_color color)
10319 {
10320 NK_ASSERT(list);
10321 if (!list) return;
10322 /* push new command with given texture */
10323 nk_draw_list_push_image(list, texture.handle);
10324 if (nk_image_is_subimage(&texture)) {
10325 /* add region inside of the texture */
10326 struct nk_vec2 uv[2];
10327 uv[0].x = (float)texture.region[0]/(float)texture.w;
10328 uv[0].y = (float)texture.region[1]/(float)texture.h;
10329 uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
10330 uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
10331 nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
10332 nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
10333 } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
10334 nk_vec2(rect.x + rect.w, rect.y + rect.h),
10335 nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
10336 }
10337 NK_API void
10338 nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
10339 struct nk_rect rect, const char *text, int len, float font_height,
10340 struct nk_color fg)
10341 {
10342 float x = 0;
10343 int text_len = 0;
10344 nk_rune unicode = 0;
10345 nk_rune next = 0;
10346 int glyph_len = 0;
10347 int next_glyph_len = 0;
10348 struct nk_user_font_glyph g;
10349
10350 NK_ASSERT(list);
10351 if (!list || !len || !text) return;
10352 if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
10353 list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
10354
10355 nk_draw_list_push_image(list, font->texture);
10356 x = rect.x;
10357 glyph_len = nk_utf_decode(text, &unicode, len);
10358 if (!glyph_len) return;
10359
10360 /* draw every glyph image */
10361 fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
10362 while (text_len < len && glyph_len) {
10363 float gx, gy, gh, gw;
10364 float char_width = 0;
10365 if (unicode == NK_UTF_INVALID) break;
10366
10367 /* query currently drawn glyph information */
10368 next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
10369 font->query(font->userdata, font_height, &g, unicode,
10370 (next == NK_UTF_INVALID) ? '\0' : next);
10371
10372 /* calculate and draw glyph drawing rectangle and image */
10373 gx = x + g.offset.x;
10374 gy = rect.y + g.offset.y;
10375 gw = g.width; gh = g.height;
10376 char_width = g.xadvance;
10377 nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
10378 g.uv[0], g.uv[1], fg);
10379
10380 /* offset next glyph */
10381 text_len += glyph_len;
10382 x += char_width;
10383 glyph_len = next_glyph_len;
10384 unicode = next;
10385 }
10386 }
10387 NK_API nk_flags
10388 nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
10389 struct nk_buffer *vertices, struct nk_buffer *elements,
10390 const struct nk_convert_config *config)
10391 {
10392 nk_flags res = NK_CONVERT_SUCCESS;
10393 const struct nk_command *cmd;
10394 NK_ASSERT(ctx);
10395 NK_ASSERT(cmds);
10396 NK_ASSERT(vertices);
10397 NK_ASSERT(elements);
10398 NK_ASSERT(config);
10399 NK_ASSERT(config->vertex_layout);
10400 NK_ASSERT(config->vertex_size);
10401 if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
10402 return NK_CONVERT_INVALID_PARAM;
10403
10404 nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
10405 config->line_AA, config->shape_AA);
10406 nk_foreach(cmd, ctx)
10407 {
10408 #ifdef NK_INCLUDE_COMMAND_USERDATA
10409 ctx->draw_list.userdata = cmd->userdata;
10410 #endif
10411 switch (cmd->type) {
10412 case NK_COMMAND_NOP: break;
10413 case NK_COMMAND_SCISSOR: {
10414 const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
10415 nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
10416 } break;
10417 case NK_COMMAND_LINE: {
10418 const struct nk_command_line *l = (const struct nk_command_line*)cmd;
10419 nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
10420 nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
10421 } break;
10422 case NK_COMMAND_CURVE: {
10423 const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
10424 nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
10425 nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
10426 q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
10427 config->curve_segment_count, q->line_thickness);
10428 } break;
10429 case NK_COMMAND_RECT: {
10430 const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
10431 nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
10432 r->color, (float)r->rounding, r->line_thickness);
10433 } break;
10434 case NK_COMMAND_RECT_FILLED: {
10435 const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
10436 nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
10437 r->color, (float)r->rounding);
10438 } break;
10439 case NK_COMMAND_RECT_MULTI_COLOR: {
10440 const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
10441 nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
10442 r->left, r->top, r->right, r->bottom);
10443 } break;
10444 case NK_COMMAND_CIRCLE: {
10445 const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
10446 nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
10447 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
10448 config->circle_segment_count, c->line_thickness);
10449 } break;
10450 case NK_COMMAND_CIRCLE_FILLED: {
10451 const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
10452 nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
10453 (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
10454 config->circle_segment_count);
10455 } break;
10456 case NK_COMMAND_ARC: {
10457 const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
10458 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
10459 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
10460 c->a[0], c->a[1], config->arc_segment_count);
10461 nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
10462 } break;
10463 case NK_COMMAND_ARC_FILLED: {
10464 const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
10465 nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
10466 nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
10467 c->a[0], c->a[1], config->arc_segment_count);
10468 nk_draw_list_path_fill(&ctx->draw_list, c->color);
10469 } break;
10470 case NK_COMMAND_TRIANGLE: {
10471 const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
10472 nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
10473 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
10474 t->line_thickness);
10475 } break;
10476 case NK_COMMAND_TRIANGLE_FILLED: {
10477 const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
10478 nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
10479 nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
10480 } break;
10481 case NK_COMMAND_POLYGON: {
10482 int i;
10483 const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
10484 for (i = 0; i < p->point_count; ++i) {
10485 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
10486 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
10487 }
10488 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
10489 } break;
10490 case NK_COMMAND_POLYGON_FILLED: {
10491 int i;
10492 const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
10493 for (i = 0; i < p->point_count; ++i) {
10494 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
10495 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
10496 }
10497 nk_draw_list_path_fill(&ctx->draw_list, p->color);
10498 } break;
10499 case NK_COMMAND_POLYLINE: {
10500 int i;
10501 const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
10502 for (i = 0; i < p->point_count; ++i) {
10503 struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
10504 nk_draw_list_path_line_to(&ctx->draw_list, pnt);
10505 }
10506 nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
10507 } break;
10508 case NK_COMMAND_TEXT: {
10509 const struct nk_command_text *t = (const struct nk_command_text*)cmd;
10510 nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
10511 t->string, t->length, t->height, t->foreground);
10512 } break;
10513 case NK_COMMAND_IMAGE: {
10514 const struct nk_command_image *i = (const struct nk_command_image*)cmd;
10515 nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
10516 } break;
10517 case NK_COMMAND_CUSTOM: {
10518 const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
10519 c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
10520 } break;
10521 default: break;
10522 }
10523 }
10524 res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
10525 res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
10526 res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
10527 return res;
10528 }
10529 NK_API const struct nk_draw_command*
10530 nk__draw_begin(const struct nk_context *ctx,
10531 const struct nk_buffer *buffer)
10532 {
10533 return nk__draw_list_begin(&ctx->draw_list, buffer);
10534 }
10535 NK_API const struct nk_draw_command*
10536 nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
10537 {
10538 return nk__draw_list_end(&ctx->draw_list, buffer);
10539 }
10540 NK_API const struct nk_draw_command*
10541 nk__draw_next(const struct nk_draw_command *cmd,
10542 const struct nk_buffer *buffer, const struct nk_context *ctx)
10543 {
10544 return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
10545 }
10546 #endif
10547
10548
10549
10550
10551
10552 #ifdef NK_INCLUDE_FONT_BAKING
10553 /* -------------------------------------------------------------
10554 *
10555 * RECT PACK
10556 *
10557 * --------------------------------------------------------------*/
10558 /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
10559 /* Sean Barrett 2014 */
10560 #define NK_RP__MAXVAL 0xffff
10561 typedef unsigned short nk_rp_coord;
10562
10563 struct nk_rp_rect {
10564 /* reserved for your use: */
10565 int id;
10566 /* input: */
10567 nk_rp_coord w, h;
10568 /* output: */
10569 nk_rp_coord x, y;
10570 int was_packed;
10571 /* non-zero if valid packing */
10572 }; /* 16 bytes, nominally */
10573
10574 struct nk_rp_node {
10575 nk_rp_coord x,y;
10576 struct nk_rp_node *next;
10577 };
10578
10579 struct nk_rp_context {
10580 int width;
10581 int height;
10582 int align;
10583 int init_mode;
10584 int heuristic;
10585 int num_nodes;
10586 struct nk_rp_node *active_head;
10587 struct nk_rp_node *free_head;
10588 struct nk_rp_node extra[2];
10589 /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
10590 };
10591
10592 struct nk_rp__findresult {
10593 int x,y;
10594 struct nk_rp_node **prev_link;
10595 };
10596
10597 enum NK_RP_HEURISTIC {
10598 NK_RP_HEURISTIC_Skyline_default=0,
10599 NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
10600 NK_RP_HEURISTIC_Skyline_BF_sortHeight
10601 };
10602 enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
10603
10604 NK_INTERN void
10605 nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
10606 {
10607 if (allow_out_of_mem)
10608 /* if it's ok to run out of memory, then don't bother aligning them; */
10609 /* this gives better packing, but may fail due to OOM (even though */
10610 /* the rectangles easily fit). @TODO a smarter approach would be to only */
10611 /* quantize once we've hit OOM, then we could get rid of this parameter. */
10612 context->align = 1;
10613 else {
10614 /* if it's not ok to run out of memory, then quantize the widths */
10615 /* so that num_nodes is always enough nodes. */
10616 /* */
10617 /* I.e. num_nodes * align >= width */
10618 /* align >= width / num_nodes */
10619 /* align = ceil(width/num_nodes) */
10620 context->align = (context->width + context->num_nodes-1) / context->num_nodes;
10621 }
10622 }
10623 NK_INTERN void
10624 nk_rp_init_target(struct nk_rp_context *context, int width, int height,
10625 struct nk_rp_node *nodes, int num_nodes)
10626 {
10627 int i;
10628 #ifndef STBRP_LARGE_RECTS
10629 NK_ASSERT(width <= 0xffff && height <= 0xffff);
10630 #endif
10631
10632 for (i=0; i < num_nodes-1; ++i)
10633 nodes[i].next = &nodes[i+1];
10634 nodes[i].next = 0;
10635 context->init_mode = NK_RP__INIT_skyline;
10636 context->heuristic = NK_RP_HEURISTIC_Skyline_default;
10637 context->free_head = &nodes[0];
10638 context->active_head = &context->extra[0];
10639 context->width = width;
10640 context->height = height;
10641 context->num_nodes = num_nodes;
10642 nk_rp_setup_allow_out_of_mem(context, 0);
10643
10644 /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
10645 context->extra[0].x = 0;
10646 context->extra[0].y = 0;
10647 context->extra[0].next = &context->extra[1];
10648 context->extra[1].x = (nk_rp_coord) width;
10649 context->extra[1].y = 65535;
10650 context->extra[1].next = 0;
10651 }
10652 /* find minimum y position if it starts at x1 */
10653 NK_INTERN int
10654 nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
10655 int x0, int width, int *pwaste)
10656 {
10657 struct nk_rp_node *node = first;
10658 int x1 = x0 + width;
10659 int min_y, visited_width, waste_area;
10660 NK_ASSERT(first->x <= x0);
10661 NK_UNUSED(c);
10662
10663 NK_ASSERT(node->next->x > x0);
10664 /* we ended up handling this in the caller for efficiency */
10665 NK_ASSERT(node->x <= x0);
10666
10667 min_y = 0;
10668 waste_area = 0;
10669 visited_width = 0;
10670 while (node->x < x1)
10671 {
10672 if (node->y > min_y) {
10673 /* raise min_y higher. */
10674 /* we've accounted for all waste up to min_y, */
10675 /* but we'll now add more waste for everything we've visited */
10676 waste_area += visited_width * (node->y - min_y);
10677 min_y = node->y;
10678 /* the first time through, visited_width might be reduced */
10679 if (node->x < x0)
10680 visited_width += node->next->x - x0;
10681 else
10682 visited_width += node->next->x - node->x;
10683 } else {
10684 /* add waste area */
10685 int under_width = node->next->x - node->x;
10686 if (under_width + visited_width > width)
10687 under_width = width - visited_width;
10688 waste_area += under_width * (min_y - node->y);
10689 visited_width += under_width;
10690 }
10691 node = node->next;
10692 }
10693 *pwaste = waste_area;
10694 return min_y;
10695 }
10696 NK_INTERN struct nk_rp__findresult
10697 nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
10698 {
10699 int best_waste = (1<<30), best_x, best_y = (1 << 30);
10700 struct nk_rp__findresult fr;
10701 struct nk_rp_node **prev, *node, *tail, **best = 0;
10702
10703 /* align to multiple of c->align */
10704 width = (width + c->align - 1);
10705 width -= width % c->align;
10706 NK_ASSERT(width % c->align == 0);
10707
10708 node = c->active_head;
10709 prev = &c->active_head;
10710 while (node->x + width <= c->width) {
10711 int y,waste;
10712 y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
10713 /* actually just want to test BL */
10714 if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
10715 /* bottom left */
10716 if (y < best_y) {
10717 best_y = y;
10718 best = prev;
10719 }
10720 } else {
10721 /* best-fit */
10722 if (y + height <= c->height) {
10723 /* can only use it if it first vertically */
10724 if (y < best_y || (y == best_y && waste < best_waste)) {
10725 best_y = y;
10726 best_waste = waste;
10727 best = prev;
10728 }
10729 }
10730 }
10731 prev = &node->next;
10732 node = node->next;
10733 }
10734 best_x = (best == 0) ? 0 : (*best)->x;
10735
10736 /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
10737 /* */
10738 /* e.g, if fitting */
10739 /* */
10740 /* ____________________ */
10741 /* |____________________| */
10742 /* */
10743 /* into */
10744 /* */
10745 /* | | */
10746 /* | ____________| */
10747 /* |____________| */
10748 /* */
10749 /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
10750 /* */
10751 /* This makes BF take about 2x the time */
10752 if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
10753 {
10754 tail = c->active_head;
10755 node = c->active_head;
10756 prev = &c->active_head;
10757 /* find first node that's admissible */
10758 while (tail->x < width)
10759 tail = tail->next;
10760 while (tail)
10761 {
10762 int xpos = tail->x - width;
10763 int y,waste;
10764 NK_ASSERT(xpos >= 0);
10765 /* find the left position that matches this */
10766 while (node->next->x <= xpos) {
10767 prev = &node->next;
10768 node = node->next;
10769 }
10770 NK_ASSERT(node->next->x > xpos && node->x <= xpos);
10771 y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
10772 if (y + height < c->height) {
10773 if (y <= best_y) {
10774 if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
10775 best_x = xpos;
10776 NK_ASSERT(y <= best_y);
10777 best_y = y;
10778 best_waste = waste;
10779 best = prev;
10780 }
10781 }
10782 }
10783 tail = tail->next;
10784 }
10785 }
10786 fr.prev_link = best;
10787 fr.x = best_x;
10788 fr.y = best_y;
10789 return fr;
10790 }
10791 NK_INTERN struct nk_rp__findresult
10792 nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
10793 {
10794 /* find best position according to heuristic */
10795 struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
10796 struct nk_rp_node *node, *cur;
10797
10798 /* bail if: */
10799 /* 1. it failed */
10800 /* 2. the best node doesn't fit (we don't always check this) */
10801 /* 3. we're out of memory */
10802 if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
10803 res.prev_link = 0;
10804 return res;
10805 }
10806
10807 /* on success, create new node */
10808 node = context->free_head;
10809 node->x = (nk_rp_coord) res.x;
10810 node->y = (nk_rp_coord) (res.y + height);
10811
10812 context->free_head = node->next;
10813
10814 /* insert the new node into the right starting point, and */
10815 /* let 'cur' point to the remaining nodes needing to be */
10816 /* stitched back in */
10817 cur = *res.prev_link;
10818 if (cur->x < res.x) {
10819 /* preserve the existing one, so start testing with the next one */
10820 struct nk_rp_node *next = cur->next;
10821 cur->next = node;
10822 cur = next;
10823 } else {
10824 *res.prev_link = node;
10825 }
10826
10827 /* from here, traverse cur and free the nodes, until we get to one */
10828 /* that shouldn't be freed */
10829 while (cur->next && cur->next->x <= res.x + width) {
10830 struct nk_rp_node *next = cur->next;
10831 /* move the current node to the free list */
10832 cur->next = context->free_head;
10833 context->free_head = cur;
10834 cur = next;
10835 }
10836 /* stitch the list back in */
10837 node->next = cur;
10838
10839 if (cur->x < res.x + width)
10840 cur->x = (nk_rp_coord) (res.x + width);
10841 return res;
10842 }
10843 NK_INTERN int
10844 nk_rect_height_compare(const void *a, const void *b)
10845 {
10846 const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
10847 const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
10848 if (p->h > q->h)
10849 return -1;
10850 if (p->h < q->h)
10851 return 1;
10852 return (p->w > q->w) ? -1 : (p->w < q->w);
10853 }
10854 NK_INTERN int
10855 nk_rect_original_order(const void *a, const void *b)
10856 {
10857 const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
10858 const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
10859 return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
10860 }
10861 NK_INTERN void
10862 nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
10863 {
10864 /* iterative quick sort */
10865 #define NK_MAX_SORT_STACK 64
10866 unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
10867 unsigned seed = len/2 * 69069+1;
10868 for (;;) {
10869 for (; left+1 < len; len++) {
10870 struct nk_rp_rect pivot, tmp;
10871 if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
10872 pivot = array[left+seed%(len-left)];
10873 seed = seed * 69069 + 1;
10874 stack[pos++] = len;
10875 for (right = left-1;;) {
10876 while (cmp(&array[++right], &pivot) < 0);
10877 while (cmp(&pivot, &array[--len]) < 0);
10878 if (right >= len) break;
10879 tmp = array[right];
10880 array[right] = array[len];
10881 array[len] = tmp;
10882 }
10883 }
10884 if (pos == 0) break;
10885 left = len;
10886 len = stack[--pos];
10887 }
10888 #undef NK_MAX_SORT_STACK
10889 }
10890 NK_INTERN void
10891 nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
10892 {
10893 int i;
10894 /* we use the 'was_packed' field internally to allow sorting/unsorting */
10895 for (i=0; i < num_rects; ++i) {
10896 rects[i].was_packed = i;
10897 }
10898
10899 /* sort according to heuristic */
10900 nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
10901
10902 for (i=0; i < num_rects; ++i) {
10903 struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
10904 if (fr.prev_link) {
10905 rects[i].x = (nk_rp_coord) fr.x;
10906 rects[i].y = (nk_rp_coord) fr.y;
10907 } else {
10908 rects[i].x = rects[i].y = NK_RP__MAXVAL;
10909 }
10910 }
10911
10912 /* unsort */
10913 nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
10914
10915 /* set was_packed flags */
10916 for (i=0; i < num_rects; ++i)
10917 rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
10918 }
10919
10920 /*
10921 * ==============================================================
10922 *
10923 * TRUETYPE
10924 *
10925 * ===============================================================
10926 */
10927 /* stb_truetype.h - v1.07 - public domain */
10928 #define NK_TT_MAX_OVERSAMPLE 8
10929 #define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
10930
10931 struct nk_tt_bakedchar {
10932 unsigned short x0,y0,x1,y1;
10933 /* coordinates of bbox in bitmap */
10934 float xoff,yoff,xadvance;
10935 };
10936
10937 struct nk_tt_aligned_quad{
10938 float x0,y0,s0,t0; /* top-left */
10939 float x1,y1,s1,t1; /* bottom-right */
10940 };
10941
10942 struct nk_tt_packedchar {
10943 unsigned short x0,y0,x1,y1;
10944 /* coordinates of bbox in bitmap */
10945 float xoff,yoff,xadvance;
10946 float xoff2,yoff2;
10947 };
10948
10949 struct nk_tt_pack_range {
10950 float font_size;
10951 int first_unicode_codepoint_in_range;
10952 /* if non-zero, then the chars are continuous, and this is the first codepoint */
10953 int *array_of_unicode_codepoints;
10954 /* if non-zero, then this is an array of unicode codepoints */
10955 int num_chars;
10956 struct nk_tt_packedchar *chardata_for_range; /* output */
10957 unsigned char h_oversample, v_oversample;
10958 /* don't set these, they're used internally */
10959 };
10960
10961 struct nk_tt_pack_context {
10962 void *pack_info;
10963 int width;
10964 int height;
10965 int stride_in_bytes;
10966 int padding;
10967 unsigned int h_oversample, v_oversample;
10968 unsigned char *pixels;
10969 void *nodes;
10970 };
10971
10972 struct nk_tt_fontinfo {
10973 const unsigned char* data; /* pointer to .ttf file */
10974 int fontstart;/* offset of start of font */
10975 int numGlyphs;/* number of glyphs, needed for range checking */
10976 int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
10977 int index_map; /* a cmap mapping for our chosen character encoding */
10978 int indexToLocFormat; /* format needed to map from glyph index to glyph */
10979 };
10980
10981 enum {
10982 NK_TT_vmove=1,
10983 NK_TT_vline,
10984 NK_TT_vcurve
10985 };
10986
10987 struct nk_tt_vertex {
10988 short x,y,cx,cy;
10989 unsigned char type,padding;
10990 };
10991
10992 struct nk_tt__bitmap{
10993 int w,h,stride;
10994 unsigned char *pixels;
10995 };
10996
10997 struct nk_tt__hheap_chunk {
10998 struct nk_tt__hheap_chunk *next;
10999 };
11000 struct nk_tt__hheap {
11001 struct nk_allocator alloc;
11002 struct nk_tt__hheap_chunk *head;
11003 void *first_free;
11004 int num_remaining_in_head_chunk;
11005 };
11006
11007 struct nk_tt__edge {
11008 float x0,y0, x1,y1;
11009 int invert;
11010 };
11011
11012 struct nk_tt__active_edge {
11013 struct nk_tt__active_edge *next;
11014 float fx,fdx,fdy;
11015 float direction;
11016 float sy;
11017 float ey;
11018 };
11019 struct nk_tt__point {float x,y;};
11020
11021 #define NK_TT_MACSTYLE_DONTCARE 0
11022 #define NK_TT_MACSTYLE_BOLD 1
11023 #define NK_TT_MACSTYLE_ITALIC 2
11024 #define NK_TT_MACSTYLE_UNDERSCORE 4
11025 #define NK_TT_MACSTYLE_NONE 8
11026 /* <= not same as 0, this makes us check the bitfield is 0 */
11027
11028 enum { /* platformID */
11029 NK_TT_PLATFORM_ID_UNICODE =0,
11030 NK_TT_PLATFORM_ID_MAC =1,
11031 NK_TT_PLATFORM_ID_ISO =2,
11032 NK_TT_PLATFORM_ID_MICROSOFT =3
11033 };
11034
11035 enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
11036 NK_TT_UNICODE_EID_UNICODE_1_0 =0,
11037 NK_TT_UNICODE_EID_UNICODE_1_1 =1,
11038 NK_TT_UNICODE_EID_ISO_10646 =2,
11039 NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
11040 NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
11041 };
11042
11043 enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
11044 NK_TT_MS_EID_SYMBOL =0,
11045 NK_TT_MS_EID_UNICODE_BMP =1,
11046 NK_TT_MS_EID_SHIFTJIS =2,
11047 NK_TT_MS_EID_UNICODE_FULL =10
11048 };
11049
11050 enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
11051 NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
11052 NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
11053 NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
11054 NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
11055 };
11056
11057 enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
11058 /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
11059 NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
11060 NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
11061 NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
11062 NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
11063 NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
11064 NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
11065 };
11066
11067 enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
11068 NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
11069 NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
11070 NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
11071 NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
11072 NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
11073 NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
11074 NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
11075 };
11076
11077 #define nk_ttBYTE(p) (* (const nk_byte *) (p))
11078 #define nk_ttCHAR(p) (* (const char *) (p))
11079
11080 #if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
11081 #define nk_ttUSHORT(p) (* (nk_ushort *) (p))
11082 #define nk_ttSHORT(p) (* (nk_short *) (p))
11083 #define nk_ttULONG(p) (* (nk_uint *) (p))
11084 #define nk_ttLONG(p) (* (nk_int *) (p))
11085 #else
11086 static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
11087 static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
11088 static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
11089 #endif
11090
11091 #define nk_tt_tag4(p,c0,c1,c2,c3)\
11092 ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
11093 #define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
11094
11095 NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
11096 int glyph_index, struct nk_tt_vertex **pvertices);
11097
11098 NK_INTERN nk_uint
11099 nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
11100 {
11101 /* @OPTIMIZE: binary search */
11102 nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
11103 nk_uint tabledir = fontstart + 12;
11104 nk_int i;
11105 for (i = 0; i < num_tables; ++i) {
11106 nk_uint loc = tabledir + (nk_uint)(16*i);
11107 if (nk_tt_tag(data+loc+0, tag))
11108 return nk_ttULONG(data+loc+8);
11109 }
11110 return 0;
11111 }
11112 NK_INTERN int
11113 nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
11114 {
11115 nk_uint cmap, t;
11116 nk_int i,numTables;
11117 const nk_byte *data = (const nk_byte *) data2;
11118
11119 info->data = data;
11120 info->fontstart = fontstart;
11121
11122 cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
11123 info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
11124 info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
11125 info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
11126 info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
11127 info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
11128 info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
11129 if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
11130 return 0;
11131
11132 t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
11133 if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
11134 else info->numGlyphs = 0xffff;
11135
11136 /* find a cmap encoding table we understand *now* to avoid searching */
11137 /* later. (todo: could make this installable) */
11138 /* the same regardless of glyph. */
11139 numTables = nk_ttUSHORT(data + cmap + 2);
11140 info->index_map = 0;
11141 for (i=0; i < numTables; ++i)
11142 {
11143 nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
11144 /* find an encoding we understand: */
11145 switch(nk_ttUSHORT(data+encoding_record)) {
11146 case NK_TT_PLATFORM_ID_MICROSOFT:
11147 switch (nk_ttUSHORT(data+encoding_record+2)) {
11148 case NK_TT_MS_EID_UNICODE_BMP:
11149 case NK_TT_MS_EID_UNICODE_FULL:
11150 /* MS/Unicode */
11151 info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
11152 break;
11153 default: break;
11154 } break;
11155 case NK_TT_PLATFORM_ID_UNICODE:
11156 /* Mac/iOS has these */
11157 /* all the encodingIDs are unicode, so we don't bother to check it */
11158 info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
11159 break;
11160 default: break;
11161 }
11162 }
11163 if (info->index_map == 0)
11164 return 0;
11165 info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
11166 return 1;
11167 }
11168 NK_INTERN int
11169 nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
11170 {
11171 const nk_byte *data = info->data;
11172 nk_uint index_map = (nk_uint)info->index_map;
11173
11174 nk_ushort format = nk_ttUSHORT(data + index_map + 0);
11175 if (format == 0) { /* apple byte encoding */
11176 nk_int bytes = nk_ttUSHORT(data + index_map + 2);
11177 if (unicode_codepoint < bytes-6)
11178 return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
11179 return 0;
11180 } else if (format == 6) {
11181 nk_uint first = nk_ttUSHORT(data + index_map + 6);
11182 nk_uint count = nk_ttUSHORT(data + index_map + 8);
11183 if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
11184 return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
11185 return 0;
11186 } else if (format == 2) {
11187 NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
11188 return 0;
11189 } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
11190 nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
11191 nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
11192 nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
11193 nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
11194
11195 /* do a binary search of the segments */
11196 nk_uint endCount = index_map + 14;
11197 nk_uint search = endCount;
11198
11199 if (unicode_codepoint > 0xffff)
11200 return 0;
11201
11202 /* they lie from endCount .. endCount + segCount */
11203 /* but searchRange is the nearest power of two, so... */
11204 if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
11205 search += (nk_uint)(rangeShift*2);
11206
11207 /* now decrement to bias correctly to find smallest */
11208 search -= 2;
11209 while (entrySelector) {
11210 nk_ushort end;
11211 searchRange >>= 1;
11212 end = nk_ttUSHORT(data + search + searchRange*2);
11213 if (unicode_codepoint > end)
11214 search += (nk_uint)(searchRange*2);
11215 --entrySelector;
11216 }
11217 search += 2;
11218
11219 {
11220 nk_ushort offset, start;
11221 nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
11222
11223 NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
11224 start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
11225 if (unicode_codepoint < start)
11226 return 0;
11227
11228 offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
11229 if (offset == 0)
11230 return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
11231
11232 return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
11233 }
11234 } else if (format == 12 || format == 13) {
11235 nk_uint ngroups = nk_ttULONG(data+index_map+12);
11236 nk_int low,high;
11237 low = 0; high = (nk_int)ngroups;
11238 /* Binary search the right group. */
11239 while (low < high) {
11240 nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
11241 nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
11242 nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
11243 if ((nk_uint) unicode_codepoint < start_char)
11244 high = mid;
11245 else if ((nk_uint) unicode_codepoint > end_char)
11246 low = mid+1;
11247 else {
11248 nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
11249 if (format == 12)
11250 return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
11251 else /* format == 13 */
11252 return (int)start_glyph;
11253 }
11254 }
11255 return 0; /* not found */
11256 }
11257 /* @TODO */
11258 NK_ASSERT(0);
11259 return 0;
11260 }
11261 NK_INTERN void
11262 nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
11263 {
11264 v->type = type;
11265 v->x = (nk_short) x;
11266 v->y = (nk_short) y;
11267 v->cx = (nk_short) cx;
11268 v->cy = (nk_short) cy;
11269 }
11270 NK_INTERN int
11271 nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
11272 {
11273 int g1,g2;
11274 if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
11275 if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
11276
11277 if (info->indexToLocFormat == 0) {
11278 g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
11279 g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
11280 } else {
11281 g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
11282 g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
11283 }
11284 return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
11285 }
11286 NK_INTERN int
11287 nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
11288 int *x0, int *y0, int *x1, int *y1)
11289 {
11290 int g = nk_tt__GetGlyfOffset(info, glyph_index);
11291 if (g < 0) return 0;
11292
11293 if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
11294 if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
11295 if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
11296 if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
11297 return 1;
11298 }
11299 NK_INTERN int
11300 nk_tt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
11301 int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
11302 {
11303 if (start_off) {
11304 if (was_off)
11305 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
11306 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
11307 } else {
11308 if (was_off)
11309 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
11310 else
11311 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
11312 }
11313 return num_vertices;
11314 }
11315 NK_INTERN int
11316 nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
11317 int glyph_index, struct nk_tt_vertex **pvertices)
11318 {
11319 nk_short numberOfContours;
11320 const nk_byte *endPtsOfContours;
11321 const nk_byte *data = info->data;
11322 struct nk_tt_vertex *vertices=0;
11323 int num_vertices=0;
11324 int g = nk_tt__GetGlyfOffset(info, glyph_index);
11325 *pvertices = 0;
11326
11327 if (g < 0) return 0;
11328 numberOfContours = nk_ttSHORT(data + g);
11329 if (numberOfContours > 0) {
11330 nk_byte flags=0,flagcount;
11331 nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
11332 nk_int x,y,cx,cy,sx,sy, scx,scy;
11333 const nk_byte *points;
11334 endPtsOfContours = (data + g + 10);
11335 ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
11336 points = data + g + 10 + numberOfContours * 2 + 2 + ins;
11337
11338 n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
11339 m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
11340 vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
11341 if (vertices == 0)
11342 return 0;
11343
11344 next_move = 0;
11345 flagcount=0;
11346
11347 /* in first pass, we load uninterpreted data into the allocated array */
11348 /* above, shifted to the end of the array so we won't overwrite it when */
11349 /* we create our final data starting from the front */
11350 off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
11351
11352 /* first load flags */
11353 for (i=0; i < n; ++i) {
11354 if (flagcount == 0) {
11355 flags = *points++;
11356 if (flags & 8)
11357 flagcount = *points++;
11358 } else --flagcount;
11359 vertices[off+i].type = flags;
11360 }
11361
11362 /* now load x coordinates */
11363 x=0;
11364 for (i=0; i < n; ++i) {
11365 flags = vertices[off+i].type;
11366 if (flags & 2) {
11367 nk_short dx = *points++;
11368 x += (flags & 16) ? dx : -dx; /* ??? */
11369 } else {
11370 if (!(flags & 16)) {
11371 x = x + (nk_short) (points[0]*256 + points[1]);
11372 points += 2;
11373 }
11374 }
11375 vertices[off+i].x = (nk_short) x;
11376 }
11377
11378 /* now load y coordinates */
11379 y=0;
11380 for (i=0; i < n; ++i) {
11381 flags = vertices[off+i].type;
11382 if (flags & 4) {
11383 nk_short dy = *points++;
11384 y += (flags & 32) ? dy : -dy; /* ??? */
11385 } else {
11386 if (!(flags & 32)) {
11387 y = y + (nk_short) (points[0]*256 + points[1]);
11388 points += 2;
11389 }
11390 }
11391 vertices[off+i].y = (nk_short) y;
11392 }
11393
11394 /* now convert them to our format */
11395 num_vertices=0;
11396 sx = sy = cx = cy = scx = scy = 0;
11397 for (i=0; i < n; ++i)
11398 {
11399 flags = vertices[off+i].type;
11400 x = (nk_short) vertices[off+i].x;
11401 y = (nk_short) vertices[off+i].y;
11402
11403 if (next_move == i) {
11404 if (i != 0)
11405 num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
11406
11407 /* now start the new one */
11408 start_off = !(flags & 1);
11409 if (start_off) {
11410 /* if we start off with an off-curve point, then when we need to find a point on the curve */
11411 /* where we can start, and we need to save some state for when we wraparound. */
11412 scx = x;
11413 scy = y;
11414 if (!(vertices[off+i+1].type & 1)) {
11415 /* next point is also a curve point, so interpolate an on-point curve */
11416 sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
11417 sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
11418 } else {
11419 /* otherwise just use the next point as our start point */
11420 sx = (nk_int) vertices[off+i+1].x;
11421 sy = (nk_int) vertices[off+i+1].y;
11422 ++i; /* we're using point i+1 as the starting point, so skip it */
11423 }
11424 } else {
11425 sx = x;
11426 sy = y;
11427 }
11428 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
11429 was_off = 0;
11430 next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
11431 ++j;
11432 } else {
11433 if (!(flags & 1))
11434 { /* if it's a curve */
11435 if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
11436 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
11437 cx = x;
11438 cy = y;
11439 was_off = 1;
11440 } else {
11441 if (was_off)
11442 nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
11443 else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
11444 was_off = 0;
11445 }
11446 }
11447 }
11448 num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
11449 } else if (numberOfContours == -1) {
11450 /* Compound shapes. */
11451 int more = 1;
11452 const nk_byte *comp = data + g + 10;
11453 num_vertices = 0;
11454 vertices = 0;
11455
11456 while (more)
11457 {
11458 nk_ushort flags, gidx;
11459 int comp_num_verts = 0, i;
11460 struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
11461 float mtx[6] = {1,0,0,1,0,0}, m, n;
11462
11463 flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
11464 gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
11465
11466 if (flags & 2) { /* XY values */
11467 if (flags & 1) { /* shorts */
11468 mtx[4] = nk_ttSHORT(comp); comp+=2;
11469 mtx[5] = nk_ttSHORT(comp); comp+=2;
11470 } else {
11471 mtx[4] = nk_ttCHAR(comp); comp+=1;
11472 mtx[5] = nk_ttCHAR(comp); comp+=1;
11473 }
11474 } else {
11475 /* @TODO handle matching point */
11476 NK_ASSERT(0);
11477 }
11478 if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
11479 mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11480 mtx[1] = mtx[2] = 0;
11481 } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
11482 mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11483 mtx[1] = mtx[2] = 0;
11484 mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11485 } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
11486 mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11487 mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11488 mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11489 mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
11490 }
11491
11492 /* Find transformation scales. */
11493 m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
11494 n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
11495
11496 /* Get indexed glyph. */
11497 comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
11498 if (comp_num_verts > 0)
11499 {
11500 /* Transform vertices. */
11501 for (i = 0; i < comp_num_verts; ++i) {
11502 struct nk_tt_vertex* v = &comp_verts[i];
11503 short x,y;
11504 x=v->x; y=v->y;
11505 v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
11506 v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
11507 x=v->cx; y=v->cy;
11508 v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
11509 v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
11510 }
11511 /* Append vertices. */
11512 tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
11513 (nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
11514 if (!tmp) {
11515 if (vertices) alloc->free(alloc->userdata, vertices);
11516 if (comp_verts) alloc->free(alloc->userdata, comp_verts);
11517 return 0;
11518 }
11519 if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
11520 NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
11521 if (vertices) alloc->free(alloc->userdata,vertices);
11522 vertices = tmp;
11523 alloc->free(alloc->userdata,comp_verts);
11524 num_vertices += comp_num_verts;
11525 }
11526 /* More components ? */
11527 more = flags & (1<<5);
11528 }
11529 } else if (numberOfContours < 0) {
11530 /* @TODO other compound variations? */
11531 NK_ASSERT(0);
11532 } else {
11533 /* numberOfCounters == 0, do nothing */
11534 }
11535 *pvertices = vertices;
11536 return num_vertices;
11537 }
11538 NK_INTERN void
11539 nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
11540 int *advanceWidth, int *leftSideBearing)
11541 {
11542 nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
11543 if (glyph_index < numOfLongHorMetrics) {
11544 if (advanceWidth)
11545 *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
11546 if (leftSideBearing)
11547 *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
11548 } else {
11549 if (advanceWidth)
11550 *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
11551 if (leftSideBearing)
11552 *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
11553 }
11554 }
11555 NK_INTERN void
11556 nk_tt_GetFontVMetrics(const struct nk_tt_fontinfo *info,
11557 int *ascent, int *descent, int *lineGap)
11558 {
11559 if (ascent ) *ascent = nk_ttSHORT(info->data+info->hhea + 4);
11560 if (descent) *descent = nk_ttSHORT(info->data+info->hhea + 6);
11561 if (lineGap) *lineGap = nk_ttSHORT(info->data+info->hhea + 8);
11562 }
11563 NK_INTERN float
11564 nk_tt_ScaleForPixelHeight(const struct nk_tt_fontinfo *info, float height)
11565 {
11566 int fheight = nk_ttSHORT(info->data + info->hhea + 4) - nk_ttSHORT(info->data + info->hhea + 6);
11567 return (float) height / (float)fheight;
11568 }
11569 NK_INTERN float
11570 nk_tt_ScaleForMappingEmToPixels(const struct nk_tt_fontinfo *info, float pixels)
11571 {
11572 int unitsPerEm = nk_ttUSHORT(info->data + info->head + 18);
11573 return pixels / (float)unitsPerEm;
11574 }
11575
11576 /*-------------------------------------------------------------
11577 * antialiasing software rasterizer
11578 * --------------------------------------------------------------*/
11579 NK_INTERN void
11580 nk_tt_GetGlyphBitmapBoxSubpixel(const struct nk_tt_fontinfo *font,
11581 int glyph, float scale_x, float scale_y,float shift_x, float shift_y,
11582 int *ix0, int *iy0, int *ix1, int *iy1)
11583 {
11584 int x0,y0,x1,y1;
11585 if (!nk_tt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
11586 /* e.g. space character */
11587 if (ix0) *ix0 = 0;
11588 if (iy0) *iy0 = 0;
11589 if (ix1) *ix1 = 0;
11590 if (iy1) *iy1 = 0;
11591 } else {
11592 /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
11593 if (ix0) *ix0 = nk_ifloorf((float)x0 * scale_x + shift_x);
11594 if (iy0) *iy0 = nk_ifloorf((float)-y1 * scale_y + shift_y);
11595 if (ix1) *ix1 = nk_iceilf ((float)x1 * scale_x + shift_x);
11596 if (iy1) *iy1 = nk_iceilf ((float)-y0 * scale_y + shift_y);
11597 }
11598 }
11599 NK_INTERN void
11600 nk_tt_GetGlyphBitmapBox(const struct nk_tt_fontinfo *font, int glyph,
11601 float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
11602 {
11603 nk_tt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
11604 }
11605
11606 /*-------------------------------------------------------------
11607 * Rasterizer
11608 * --------------------------------------------------------------*/
11609 NK_INTERN void*
11610 nk_tt__hheap_alloc(struct nk_tt__hheap *hh, nk_size size)
11611 {
11612 if (hh->first_free) {
11613 void *p = hh->first_free;
11614 hh->first_free = * (void **) p;
11615 return p;
11616 } else {
11617 if (hh->num_remaining_in_head_chunk == 0) {
11618 int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
11619 struct nk_tt__hheap_chunk *c = (struct nk_tt__hheap_chunk *)
11620 hh->alloc.alloc(hh->alloc.userdata, 0,
11621 sizeof(struct nk_tt__hheap_chunk) + size * (nk_size)count);
11622 if (c == 0) return 0;
11623 c->next = hh->head;
11624 hh->head = c;
11625 hh->num_remaining_in_head_chunk = count;
11626 }
11627 --hh->num_remaining_in_head_chunk;
11628 return (char *) (hh->head) + size * (nk_size)hh->num_remaining_in_head_chunk;
11629 }
11630 }
11631 NK_INTERN void
11632 nk_tt__hheap_free(struct nk_tt__hheap *hh, void *p)
11633 {
11634 *(void **) p = hh->first_free;
11635 hh->first_free = p;
11636 }
11637 NK_INTERN void
11638 nk_tt__hheap_cleanup(struct nk_tt__hheap *hh)
11639 {
11640 struct nk_tt__hheap_chunk *c = hh->head;
11641 while (c) {
11642 struct nk_tt__hheap_chunk *n = c->next;
11643 hh->alloc.free(hh->alloc.userdata, c);
11644 c = n;
11645 }
11646 }
11647 NK_INTERN struct nk_tt__active_edge*
11648 nk_tt__new_active(struct nk_tt__hheap *hh, struct nk_tt__edge *e,
11649 int off_x, float start_point)
11650 {
11651 struct nk_tt__active_edge *z = (struct nk_tt__active_edge *)
11652 nk_tt__hheap_alloc(hh, sizeof(*z));
11653 float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
11654 /*STBTT_assert(e->y0 <= start_point); */
11655 if (!z) return z;
11656 z->fdx = dxdy;
11657 z->fdy = (dxdy != 0) ? (1/dxdy): 0;
11658 z->fx = e->x0 + dxdy * (start_point - e->y0);
11659 z->fx -= (float)off_x;
11660 z->direction = e->invert ? 1.0f : -1.0f;
11661 z->sy = e->y0;
11662 z->ey = e->y1;
11663 z->next = 0;
11664 return z;
11665 }
11666 NK_INTERN void
11667 nk_tt__handle_clipped_edge(float *scanline, int x, struct nk_tt__active_edge *e,
11668 float x0, float y0, float x1, float y1)
11669 {
11670 if (y0 == y1) return;
11671 NK_ASSERT(y0 < y1);
11672 NK_ASSERT(e->sy <= e->ey);
11673 if (y0 > e->ey) return;
11674 if (y1 < e->sy) return;
11675 if (y0 < e->sy) {
11676 x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
11677 y0 = e->sy;
11678 }
11679 if (y1 > e->ey) {
11680 x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
11681 y1 = e->ey;
11682 }
11683
11684 if (x0 == x) NK_ASSERT(x1 <= x+1);
11685 else if (x0 == x+1) NK_ASSERT(x1 >= x);
11686 else if (x0 <= x) NK_ASSERT(x1 <= x);
11687 else if (x0 >= x+1) NK_ASSERT(x1 >= x+1);
11688 else NK_ASSERT(x1 >= x && x1 <= x+1);
11689
11690 if (x0 <= x && x1 <= x)
11691 scanline[x] += e->direction * (y1-y0);
11692 else if (x0 >= x+1 && x1 >= x+1);
11693 else {
11694 NK_ASSERT(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
11695 /* coverage = 1 - average x position */
11696 scanline[x] += (float)e->direction * (float)(y1-y0) * (1.0f-((x0-(float)x)+(x1-(float)x))/2.0f);
11697 }
11698 }
11699 NK_INTERN void
11700 nk_tt__fill_active_edges_new(float *scanline, float *scanline_fill, int len,
11701 struct nk_tt__active_edge *e, float y_top)
11702 {
11703 float y_bottom = y_top+1;
11704 while (e)
11705 {
11706 /* brute force every pixel */
11707 /* compute intersection points with top & bottom */
11708 NK_ASSERT(e->ey >= y_top);
11709 if (e->fdx == 0) {
11710 float x0 = e->fx;
11711 if (x0 < len) {
11712 if (x0 >= 0) {
11713 nk_tt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
11714 nk_tt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
11715 } else {
11716 nk_tt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
11717 }
11718 }
11719 } else {
11720 float x0 = e->fx;
11721 float dx = e->fdx;
11722 float xb = x0 + dx;
11723 float x_top, x_bottom;
11724 float y0,y1;
11725 float dy = e->fdy;
11726 NK_ASSERT(e->sy <= y_bottom && e->ey >= y_top);
11727
11728 /* compute endpoints of line segment clipped to this scanline (if the */
11729 /* line segment starts on this scanline. x0 is the intersection of the */
11730 /* line with y_top, but that may be off the line segment. */
11731 if (e->sy > y_top) {
11732 x_top = x0 + dx * (e->sy - y_top);
11733 y0 = e->sy;
11734 } else {
11735 x_top = x0;
11736 y0 = y_top;
11737 }
11738
11739 if (e->ey < y_bottom) {
11740 x_bottom = x0 + dx * (e->ey - y_top);
11741 y1 = e->ey;
11742 } else {
11743 x_bottom = xb;
11744 y1 = y_bottom;
11745 }
11746
11747 if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len)
11748 {
11749 /* from here on, we don't have to range check x values */
11750 if ((int) x_top == (int) x_bottom) {
11751 float height;
11752 /* simple case, only spans one pixel */
11753 int x = (int) x_top;
11754 height = y1 - y0;
11755 NK_ASSERT(x >= 0 && x < len);
11756 scanline[x] += e->direction * (1.0f-(((float)x_top - (float)x) + ((float)x_bottom-(float)x))/2.0f) * (float)height;
11757 scanline_fill[x] += e->direction * (float)height; /* everything right of this pixel is filled */
11758 } else {
11759 int x,x1,x2;
11760 float y_crossing, step, sign, area;
11761 /* covers 2+ pixels */
11762 if (x_top > x_bottom)
11763 {
11764 /* flip scanline vertically; signed area is the same */
11765 float t;
11766 y0 = y_bottom - (y0 - y_top);
11767 y1 = y_bottom - (y1 - y_top);
11768 t = y0; y0 = y1; y1 = t;
11769 t = x_bottom; x_bottom = x_top; x_top = t;
11770 dx = -dx;
11771 dy = -dy;
11772 t = x0; x0 = xb; xb = t;
11773 }
11774
11775 x1 = (int) x_top;
11776 x2 = (int) x_bottom;
11777 /* compute intersection with y axis at x1+1 */
11778 y_crossing = ((float)x1+1 - (float)x0) * (float)dy + (float)y_top;
11779
11780 sign = e->direction;
11781 /* area of the rectangle covered from y0..y_crossing */
11782 area = sign * (y_crossing-y0);
11783 /* area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) */
11784 scanline[x1] += area * (1.0f-((float)((float)x_top - (float)x1)+(float)(x1+1-x1))/2.0f);
11785
11786 step = sign * dy;
11787 for (x = x1+1; x < x2; ++x) {
11788 scanline[x] += area + step/2;
11789 area += step;
11790 }
11791 y_crossing += (float)dy * (float)(x2 - (x1+1));
11792
11793 scanline[x2] += area + sign * (1.0f-((float)(x2-x2)+((float)x_bottom-(float)x2))/2.0f) * (y1-y_crossing);
11794 scanline_fill[x2] += sign * (y1-y0);
11795 }
11796 }
11797 else
11798 {
11799 /* if edge goes outside of box we're drawing, we require */
11800 /* clipping logic. since this does not match the intended use */
11801 /* of this library, we use a different, very slow brute */
11802 /* force implementation */
11803 int x;
11804 for (x=0; x < len; ++x)
11805 {
11806 /* cases: */
11807 /* */
11808 /* there can be up to two intersections with the pixel. any intersection */
11809 /* with left or right edges can be handled by splitting into two (or three) */
11810 /* regions. intersections with top & bottom do not necessitate case-wise logic. */
11811 /* */
11812 /* the old way of doing this found the intersections with the left & right edges, */
11813 /* then used some simple logic to produce up to three segments in sorted order */
11814 /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
11815 /* across the x border, then the corresponding y position might not be distinct */
11816 /* from the other y segment, and it might ignored as an empty segment. to avoid */
11817 /* that, we need to explicitly produce segments based on x positions. */
11818
11819 /* rename variables to clear pairs */
11820 float ya = y_top;
11821 float x1 = (float) (x);
11822 float x2 = (float) (x+1);
11823 float x3 = xb;
11824 float y3 = y_bottom;
11825 float yb,y2;
11826
11827 yb = ((float)x - x0) / dx + y_top;
11828 y2 = ((float)x+1 - x0) / dx + y_top;
11829
11830 if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
11831 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
11832 nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x2,y2);
11833 nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
11834 } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
11835 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
11836 nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x1,yb);
11837 nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
11838 } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
11839 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
11840 nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
11841 } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
11842 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
11843 nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
11844 } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
11845 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
11846 nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
11847 } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
11848 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
11849 nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
11850 } else { /* one segment */
11851 nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x3,y3);
11852 }
11853 }
11854 }
11855 }
11856 e = e->next;
11857 }
11858 }
11859 NK_INTERN void
11860 nk_tt__rasterize_sorted_edges(struct nk_tt__bitmap *result, struct nk_tt__edge *e,
11861 int n, int vsubsample, int off_x, int off_y, struct nk_allocator *alloc)
11862 {
11863 /* directly AA rasterize edges w/o supersampling */
11864 struct nk_tt__hheap hh;
11865 struct nk_tt__active_edge *active = 0;
11866 int y,j=0, i;
11867 float scanline_data[129], *scanline, *scanline2;
11868
11869 NK_UNUSED(vsubsample);
11870 nk_zero_struct(hh);
11871 hh.alloc = *alloc;
11872
11873 if (result->w > 64)
11874 scanline = (float *) alloc->alloc(alloc->userdata,0, (nk_size)(result->w*2+1) * sizeof(float));
11875 else scanline = scanline_data;
11876
11877 scanline2 = scanline + result->w;
11878 y = off_y;
11879 e[n].y0 = (float) (off_y + result->h) + 1;
11880
11881 while (j < result->h)
11882 {
11883 /* find center of pixel for this scanline */
11884 float scan_y_top = (float)y + 0.0f;
11885 float scan_y_bottom = (float)y + 1.0f;
11886 struct nk_tt__active_edge **step = &active;
11887
11888 NK_MEMSET(scanline , 0, (nk_size)result->w*sizeof(scanline[0]));
11889 NK_MEMSET(scanline2, 0, (nk_size)(result->w+1)*sizeof(scanline[0]));
11890
11891 /* update all active edges; */
11892 /* remove all active edges that terminate before the top of this scanline */
11893 while (*step) {
11894 struct nk_tt__active_edge * z = *step;
11895 if (z->ey <= scan_y_top) {
11896 *step = z->next; /* delete from list */
11897 NK_ASSERT(z->direction);
11898 z->direction = 0;
11899 nk_tt__hheap_free(&hh, z);
11900 } else {
11901 step = &((*step)->next); /* advance through list */
11902 }
11903 }
11904
11905 /* insert all edges that start before the bottom of this scanline */
11906 while (e->y0 <= scan_y_bottom) {
11907 if (e->y0 != e->y1) {
11908 struct nk_tt__active_edge *z = nk_tt__new_active(&hh, e, off_x, scan_y_top);
11909 if (z != 0) {
11910 NK_ASSERT(z->ey >= scan_y_top);
11911 /* insert at front */
11912 z->next = active;
11913 active = z;
11914 }
11915 }
11916 ++e;
11917 }
11918
11919 /* now process all active edges */
11920 if (active)
11921 nk_tt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
11922
11923 {
11924 float sum = 0;
11925 for (i=0; i < result->w; ++i) {
11926 float k;
11927 int m;
11928 sum += scanline2[i];
11929 k = scanline[i] + sum;
11930 k = (float) NK_ABS(k) * 255.0f + 0.5f;
11931 m = (int) k;
11932 if (m > 255) m = 255;
11933 result->pixels[j*result->stride + i] = (unsigned char) m;
11934 }
11935 }
11936 /* advance all the edges */
11937 step = &active;
11938 while (*step) {
11939 struct nk_tt__active_edge *z = *step;
11940 z->fx += z->fdx; /* advance to position for current scanline */
11941 step = &((*step)->next); /* advance through list */
11942 }
11943 ++y;
11944 ++j;
11945 }
11946 nk_tt__hheap_cleanup(&hh);
11947 if (scanline != scanline_data)
11948 alloc->free(alloc->userdata, scanline);
11949 }
11950 NK_INTERN void
11951 nk_tt__sort_edges_ins_sort(struct nk_tt__edge *p, int n)
11952 {
11953 int i,j;
11954 #define NK_TT__COMPARE(a,b) ((a)->y0 < (b)->y0)
11955 for (i=1; i < n; ++i) {
11956 struct nk_tt__edge t = p[i], *a = &t;
11957 j = i;
11958 while (j > 0) {
11959 struct nk_tt__edge *b = &p[j-1];
11960 int c = NK_TT__COMPARE(a,b);
11961 if (!c) break;
11962 p[j] = p[j-1];
11963 --j;
11964 }
11965 if (i != j)
11966 p[j] = t;
11967 }
11968 }
11969 NK_INTERN void
11970 nk_tt__sort_edges_quicksort(struct nk_tt__edge *p, int n)
11971 {
11972 /* threshold for transitioning to insertion sort */
11973 while (n > 12) {
11974 struct nk_tt__edge t;
11975 int c01,c12,c,m,i,j;
11976
11977 /* compute median of three */
11978 m = n >> 1;
11979 c01 = NK_TT__COMPARE(&p[0],&p[m]);
11980 c12 = NK_TT__COMPARE(&p[m],&p[n-1]);
11981
11982 /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
11983 if (c01 != c12) {
11984 /* otherwise, we'll need to swap something else to middle */
11985 int z;
11986 c = NK_TT__COMPARE(&p[0],&p[n-1]);
11987 /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
11988 /* 0<mid && mid>n: 0>n => 0; 0<n => n */
11989 z = (c == c12) ? 0 : n-1;
11990 t = p[z];
11991 p[z] = p[m];
11992 p[m] = t;
11993 }
11994
11995 /* now p[m] is the median-of-three */
11996 /* swap it to the beginning so it won't move around */
11997 t = p[0];
11998 p[0] = p[m];
11999 p[m] = t;
12000
12001 /* partition loop */
12002 i=1;
12003 j=n-1;
12004 for(;;) {
12005 /* handling of equality is crucial here */
12006 /* for sentinels & efficiency with duplicates */
12007 for (;;++i) {
12008 if (!NK_TT__COMPARE(&p[i], &p[0])) break;
12009 }
12010 for (;;--j) {
12011 if (!NK_TT__COMPARE(&p[0], &p[j])) break;
12012 }
12013
12014 /* make sure we haven't crossed */
12015 if (i >= j) break;
12016 t = p[i];
12017 p[i] = p[j];
12018 p[j] = t;
12019
12020 ++i;
12021 --j;
12022
12023 }
12024
12025 /* recurse on smaller side, iterate on larger */
12026 if (j < (n-i)) {
12027 nk_tt__sort_edges_quicksort(p,j);
12028 p = p+i;
12029 n = n-i;
12030 } else {
12031 nk_tt__sort_edges_quicksort(p+i, n-i);
12032 n = j;
12033 }
12034 }
12035 }
12036 NK_INTERN void
12037 nk_tt__sort_edges(struct nk_tt__edge *p, int n)
12038 {
12039 nk_tt__sort_edges_quicksort(p, n);
12040 nk_tt__sort_edges_ins_sort(p, n);
12041 }
12042 NK_INTERN void
12043 nk_tt__rasterize(struct nk_tt__bitmap *result, struct nk_tt__point *pts,
12044 int *wcount, int windings, float scale_x, float scale_y,
12045 float shift_x, float shift_y, int off_x, int off_y, int invert,
12046 struct nk_allocator *alloc)
12047 {
12048 float y_scale_inv = invert ? -scale_y : scale_y;
12049 struct nk_tt__edge *e;
12050 int n,i,j,k,m;
12051 int vsubsample = 1;
12052 /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
12053
12054 /* now we have to blow out the windings into explicit edge lists */
12055 n = 0;
12056 for (i=0; i < windings; ++i)
12057 n += wcount[i];
12058
12059 e = (struct nk_tt__edge*)
12060 alloc->alloc(alloc->userdata, 0,(sizeof(*e) * (nk_size)(n+1)));
12061 if (e == 0) return;
12062 n = 0;
12063
12064 m=0;
12065 for (i=0; i < windings; ++i)
12066 {
12067 struct nk_tt__point *p = pts + m;
12068 m += wcount[i];
12069 j = wcount[i]-1;
12070 for (k=0; k < wcount[i]; j=k++) {
12071 int a=k,b=j;
12072 /* skip the edge if horizontal */
12073 if (p[j].y == p[k].y)
12074 continue;
12075
12076 /* add edge from j to k to the list */
12077 e[n].invert = 0;
12078 if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
12079 e[n].invert = 1;
12080 a=j,b=k;
12081 }
12082 e[n].x0 = p[a].x * scale_x + shift_x;
12083 e[n].y0 = (p[a].y * y_scale_inv + shift_y) * (float)vsubsample;
12084 e[n].x1 = p[b].x * scale_x + shift_x;
12085 e[n].y1 = (p[b].y * y_scale_inv + shift_y) * (float)vsubsample;
12086 ++n;
12087 }
12088 }
12089
12090 /* now sort the edges by their highest point (should snap to integer, and then by x) */
12091 /*STBTT_sort(e, n, sizeof(e[0]), nk_tt__edge_compare); */
12092 nk_tt__sort_edges(e, n);
12093 /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
12094 nk_tt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, alloc);
12095 alloc->free(alloc->userdata, e);
12096 }
12097 NK_INTERN void
12098 nk_tt__add_point(struct nk_tt__point *points, int n, float x, float y)
12099 {
12100 if (!points) return; /* during first pass, it's unallocated */
12101 points[n].x = x;
12102 points[n].y = y;
12103 }
12104 NK_INTERN int
12105 nk_tt__tesselate_curve(struct nk_tt__point *points, int *num_points,
12106 float x0, float y0, float x1, float y1, float x2, float y2,
12107 float objspace_flatness_squared, int n)
12108 {
12109 /* tesselate until threshold p is happy...
12110 * @TODO warped to compensate for non-linear stretching */
12111 /* midpoint */
12112 float mx = (x0 + 2*x1 + x2)/4;
12113 float my = (y0 + 2*y1 + y2)/4;
12114 /* versus directly drawn line */
12115 float dx = (x0+x2)/2 - mx;
12116 float dy = (y0+y2)/2 - my;
12117 if (n > 16) /* 65536 segments on one curve better be enough! */
12118 return 1;
12119
12120 /* half-pixel error allowed... need to be smaller if AA */
12121 if (dx*dx+dy*dy > objspace_flatness_squared) {
12122 nk_tt__tesselate_curve(points, num_points, x0,y0,
12123 (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
12124 nk_tt__tesselate_curve(points, num_points, mx,my,
12125 (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
12126 } else {
12127 nk_tt__add_point(points, *num_points,x2,y2);
12128 *num_points = *num_points+1;
12129 }
12130 return 1;
12131 }
12132 NK_INTERN struct nk_tt__point*
12133 nk_tt_FlattenCurves(struct nk_tt_vertex *vertices, int num_verts,
12134 float objspace_flatness, int **contour_lengths, int *num_contours,
12135 struct nk_allocator *alloc)
12136 {
12137 /* returns number of contours */
12138 struct nk_tt__point *points=0;
12139 int num_points=0;
12140 float objspace_flatness_squared = objspace_flatness * objspace_flatness;
12141 int i;
12142 int n=0;
12143 int start=0;
12144 int pass;
12145
12146 /* count how many "moves" there are to get the contour count */
12147 for (i=0; i < num_verts; ++i)
12148 if (vertices[i].type == NK_TT_vmove) ++n;
12149
12150 *num_contours = n;
12151 if (n == 0) return 0;
12152
12153 *contour_lengths = (int *)
12154 alloc->alloc(alloc->userdata,0, (sizeof(**contour_lengths) * (nk_size)n));
12155 if (*contour_lengths == 0) {
12156 *num_contours = 0;
12157 return 0;
12158 }
12159
12160 /* make two passes through the points so we don't need to realloc */
12161 for (pass=0; pass < 2; ++pass)
12162 {
12163 float x=0,y=0;
12164 if (pass == 1) {
12165 points = (struct nk_tt__point *)
12166 alloc->alloc(alloc->userdata,0, (nk_size)num_points * sizeof(points[0]));
12167 if (points == 0) goto error;
12168 }
12169 num_points = 0;
12170 n= -1;
12171
12172 for (i=0; i < num_verts; ++i)
12173 {
12174 switch (vertices[i].type) {
12175 case NK_TT_vmove:
12176 /* start the next contour */
12177 if (n >= 0)
12178 (*contour_lengths)[n] = num_points - start;
12179 ++n;
12180 start = num_points;
12181
12182 x = vertices[i].x, y = vertices[i].y;
12183 nk_tt__add_point(points, num_points++, x,y);
12184 break;
12185 case NK_TT_vline:
12186 x = vertices[i].x, y = vertices[i].y;
12187 nk_tt__add_point(points, num_points++, x, y);
12188 break;
12189 case NK_TT_vcurve:
12190 nk_tt__tesselate_curve(points, &num_points, x,y,
12191 vertices[i].cx, vertices[i].cy,
12192 vertices[i].x, vertices[i].y,
12193 objspace_flatness_squared, 0);
12194 x = vertices[i].x, y = vertices[i].y;
12195 break;
12196 default: break;
12197 }
12198 }
12199 (*contour_lengths)[n] = num_points - start;
12200 }
12201 return points;
12202
12203 error:
12204 alloc->free(alloc->userdata, points);
12205 alloc->free(alloc->userdata, *contour_lengths);
12206 *contour_lengths = 0;
12207 *num_contours = 0;
12208 return 0;
12209 }
12210 NK_INTERN void
12211 nk_tt_Rasterize(struct nk_tt__bitmap *result, float flatness_in_pixels,
12212 struct nk_tt_vertex *vertices, int num_verts,
12213 float scale_x, float scale_y, float shift_x, float shift_y,
12214 int x_off, int y_off, int invert, struct nk_allocator *alloc)
12215 {
12216 float scale = scale_x > scale_y ? scale_y : scale_x;
12217 int winding_count, *winding_lengths;
12218 struct nk_tt__point *windings = nk_tt_FlattenCurves(vertices, num_verts,
12219 flatness_in_pixels / scale, &winding_lengths, &winding_count, alloc);
12220
12221 NK_ASSERT(alloc);
12222 if (windings) {
12223 nk_tt__rasterize(result, windings, winding_lengths, winding_count,
12224 scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, alloc);
12225 alloc->free(alloc->userdata, winding_lengths);
12226 alloc->free(alloc->userdata, windings);
12227 }
12228 }
12229 NK_INTERN void
12230 nk_tt_MakeGlyphBitmapSubpixel(const struct nk_tt_fontinfo *info, unsigned char *output,
12231 int out_w, int out_h, int out_stride, float scale_x, float scale_y,
12232 float shift_x, float shift_y, int glyph, struct nk_allocator *alloc)
12233 {
12234 int ix0,iy0;
12235 struct nk_tt_vertex *vertices;
12236 int num_verts = nk_tt_GetGlyphShape(info, alloc, glyph, &vertices);
12237 struct nk_tt__bitmap gbm;
12238
12239 nk_tt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x,
12240 shift_y, &ix0,&iy0,0,0);
12241 gbm.pixels = output;
12242 gbm.w = out_w;
12243 gbm.h = out_h;
12244 gbm.stride = out_stride;
12245
12246 if (gbm.w && gbm.h)
12247 nk_tt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y,
12248 shift_x, shift_y, ix0,iy0, 1, alloc);
12249 alloc->free(alloc->userdata, vertices);
12250 }
12251
12252 /*-------------------------------------------------------------
12253 * Bitmap baking
12254 * --------------------------------------------------------------*/
12255 NK_INTERN int
12256 nk_tt_PackBegin(struct nk_tt_pack_context *spc, unsigned char *pixels,
12257 int pw, int ph, int stride_in_bytes, int padding, struct nk_allocator *alloc)
12258 {
12259 int num_nodes = pw - padding;
12260 struct nk_rp_context *context = (struct nk_rp_context *)
12261 alloc->alloc(alloc->userdata,0, sizeof(*context));
12262 struct nk_rp_node *nodes = (struct nk_rp_node*)
12263 alloc->alloc(alloc->userdata,0, (sizeof(*nodes ) * (nk_size)num_nodes));
12264
12265 if (context == 0 || nodes == 0) {
12266 if (context != 0) alloc->free(alloc->userdata, context);
12267 if (nodes != 0) alloc->free(alloc->userdata, nodes);
12268 return 0;
12269 }
12270
12271 spc->width = pw;
12272 spc->height = ph;
12273 spc->pixels = pixels;
12274 spc->pack_info = context;
12275 spc->nodes = nodes;
12276 spc->padding = padding;
12277 spc->stride_in_bytes = (stride_in_bytes != 0) ? stride_in_bytes : pw;
12278 spc->h_oversample = 1;
12279 spc->v_oversample = 1;
12280
12281 nk_rp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
12282 if (pixels)
12283 NK_MEMSET(pixels, 0, (nk_size)(pw*ph)); /* background of 0 around pixels */
12284 return 1;
12285 }
12286 NK_INTERN void
12287 nk_tt_PackEnd(struct nk_tt_pack_context *spc, struct nk_allocator *alloc)
12288 {
12289 alloc->free(alloc->userdata, spc->nodes);
12290 alloc->free(alloc->userdata, spc->pack_info);
12291 }
12292 NK_INTERN void
12293 nk_tt_PackSetOversampling(struct nk_tt_pack_context *spc,
12294 unsigned int h_oversample, unsigned int v_oversample)
12295 {
12296 NK_ASSERT(h_oversample <= NK_TT_MAX_OVERSAMPLE);
12297 NK_ASSERT(v_oversample <= NK_TT_MAX_OVERSAMPLE);
12298 if (h_oversample <= NK_TT_MAX_OVERSAMPLE)
12299 spc->h_oversample = h_oversample;
12300 if (v_oversample <= NK_TT_MAX_OVERSAMPLE)
12301 spc->v_oversample = v_oversample;
12302 }
12303 NK_INTERN void
12304 nk_tt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
12305 int kernel_width)
12306 {
12307 unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
12308 int safe_w = w - kernel_width;
12309 int j;
12310
12311 for (j=0; j < h; ++j)
12312 {
12313 int i;
12314 unsigned int total;
12315 NK_MEMSET(buffer, 0, (nk_size)kernel_width);
12316
12317 total = 0;
12318
12319 /* make kernel_width a constant in common cases so compiler can optimize out the divide */
12320 switch (kernel_width) {
12321 case 2:
12322 for (i=0; i <= safe_w; ++i) {
12323 total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
12324 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
12325 pixels[i] = (unsigned char) (total / 2);
12326 }
12327 break;
12328 case 3:
12329 for (i=0; i <= safe_w; ++i) {
12330 total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
12331 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
12332 pixels[i] = (unsigned char) (total / 3);
12333 }
12334 break;
12335 case 4:
12336 for (i=0; i <= safe_w; ++i) {
12337 total += (unsigned int)pixels[i] - buffer[i & NK_TT__OVER_MASK];
12338 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
12339 pixels[i] = (unsigned char) (total / 4);
12340 }
12341 break;
12342 case 5:
12343 for (i=0; i <= safe_w; ++i) {
12344 total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
12345 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
12346 pixels[i] = (unsigned char) (total / 5);
12347 }
12348 break;
12349 default:
12350 for (i=0; i <= safe_w; ++i) {
12351 total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
12352 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
12353 pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
12354 }
12355 break;
12356 }
12357
12358 for (; i < w; ++i) {
12359 NK_ASSERT(pixels[i] == 0);
12360 total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
12361 pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
12362 }
12363 pixels += stride_in_bytes;
12364 }
12365 }
12366 NK_INTERN void
12367 nk_tt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
12368 int kernel_width)
12369 {
12370 unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
12371 int safe_h = h - kernel_width;
12372 int j;
12373
12374 for (j=0; j < w; ++j)
12375 {
12376 int i;
12377 unsigned int total;
12378 NK_MEMSET(buffer, 0, (nk_size)kernel_width);
12379
12380 total = 0;
12381
12382 /* make kernel_width a constant in common cases so compiler can optimize out the divide */
12383 switch (kernel_width) {
12384 case 2:
12385 for (i=0; i <= safe_h; ++i) {
12386 total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
12387 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
12388 pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
12389 }
12390 break;
12391 case 3:
12392 for (i=0; i <= safe_h; ++i) {
12393 total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
12394 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
12395 pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
12396 }
12397 break;
12398 case 4:
12399 for (i=0; i <= safe_h; ++i) {
12400 total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
12401 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
12402 pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
12403 }
12404 break;
12405 case 5:
12406 for (i=0; i <= safe_h; ++i) {
12407 total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
12408 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
12409 pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
12410 }
12411 break;
12412 default:
12413 for (i=0; i <= safe_h; ++i) {
12414 total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
12415 buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
12416 pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
12417 }
12418 break;
12419 }
12420
12421 for (; i < h; ++i) {
12422 NK_ASSERT(pixels[i*stride_in_bytes] == 0);
12423 total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
12424 pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
12425 }
12426 pixels += 1;
12427 }
12428 }
12429 NK_INTERN float
12430 nk_tt__oversample_shift(int oversample)
12431 {
12432 if (!oversample)
12433 return 0.0f;
12434
12435 /* The prefilter is a box filter of width "oversample", */
12436 /* which shifts phase by (oversample - 1)/2 pixels in */
12437 /* oversampled space. We want to shift in the opposite */
12438 /* direction to counter this. */
12439 return (float)-(oversample - 1) / (2.0f * (float)oversample);
12440 }
12441 NK_INTERN int
12442 nk_tt_PackFontRangesGatherRects(struct nk_tt_pack_context *spc,
12443 struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
12444 int num_ranges, struct nk_rp_rect *rects)
12445 {
12446 /* rects array must be big enough to accommodate all characters in the given ranges */
12447 int i,j,k;
12448 k = 0;
12449
12450 for (i=0; i < num_ranges; ++i) {
12451 float fh = ranges[i].font_size;
12452 float scale = (fh > 0) ? nk_tt_ScaleForPixelHeight(info, fh):
12453 nk_tt_ScaleForMappingEmToPixels(info, -fh);
12454 ranges[i].h_oversample = (unsigned char) spc->h_oversample;
12455 ranges[i].v_oversample = (unsigned char) spc->v_oversample;
12456 for (j=0; j < ranges[i].num_chars; ++j) {
12457 int x0,y0,x1,y1;
12458 int codepoint = ranges[i].first_unicode_codepoint_in_range ?
12459 ranges[i].first_unicode_codepoint_in_range + j :
12460 ranges[i].array_of_unicode_codepoints[j];
12461
12462 int glyph = nk_tt_FindGlyphIndex(info, codepoint);
12463 nk_tt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * (float)spc->h_oversample,
12464 scale * (float)spc->v_oversample, 0,0, &x0,&y0,&x1,&y1);
12465 rects[k].w = (nk_rp_coord) (x1-x0 + spc->padding + (int)spc->h_oversample-1);
12466 rects[k].h = (nk_rp_coord) (y1-y0 + spc->padding + (int)spc->v_oversample-1);
12467 ++k;
12468 }
12469 }
12470 return k;
12471 }
12472 NK_INTERN int
12473 nk_tt_PackFontRangesRenderIntoRects(struct nk_tt_pack_context *spc,
12474 struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
12475 int num_ranges, struct nk_rp_rect *rects, struct nk_allocator *alloc)
12476 {
12477 int i,j,k, return_value = 1;
12478 /* save current values */
12479 int old_h_over = (int)spc->h_oversample;
12480 int old_v_over = (int)spc->v_oversample;
12481 /* rects array must be big enough to accommodate all characters in the given ranges */
12482
12483 k = 0;
12484 for (i=0; i < num_ranges; ++i)
12485 {
12486 float fh = ranges[i].font_size;
12487 float recip_h,recip_v,sub_x,sub_y;
12488 float scale = fh > 0 ? nk_tt_ScaleForPixelHeight(info, fh):
12489 nk_tt_ScaleForMappingEmToPixels(info, -fh);
12490
12491 spc->h_oversample = ranges[i].h_oversample;
12492 spc->v_oversample = ranges[i].v_oversample;
12493
12494 recip_h = 1.0f / (float)spc->h_oversample;
12495 recip_v = 1.0f / (float)spc->v_oversample;
12496
12497 sub_x = nk_tt__oversample_shift((int)spc->h_oversample);
12498 sub_y = nk_tt__oversample_shift((int)spc->v_oversample);
12499
12500 for (j=0; j < ranges[i].num_chars; ++j)
12501 {
12502 struct nk_rp_rect *r = &rects[k];
12503 if (r->was_packed)
12504 {
12505 struct nk_tt_packedchar *bc = &ranges[i].chardata_for_range[j];
12506 int advance, lsb, x0,y0,x1,y1;
12507 int codepoint = ranges[i].first_unicode_codepoint_in_range ?
12508 ranges[i].first_unicode_codepoint_in_range + j :
12509 ranges[i].array_of_unicode_codepoints[j];
12510 int glyph = nk_tt_FindGlyphIndex(info, codepoint);
12511 nk_rp_coord pad = (nk_rp_coord) spc->padding;
12512
12513 /* pad on left and top */
12514 r->x = (nk_rp_coord)((int)r->x + (int)pad);
12515 r->y = (nk_rp_coord)((int)r->y + (int)pad);
12516 r->w = (nk_rp_coord)((int)r->w - (int)pad);
12517 r->h = (nk_rp_coord)((int)r->h - (int)pad);
12518
12519 nk_tt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
12520 nk_tt_GetGlyphBitmapBox(info, glyph, scale * (float)spc->h_oversample,
12521 (scale * (float)spc->v_oversample), &x0,&y0,&x1,&y1);
12522 nk_tt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes,
12523 (int)(r->w - spc->h_oversample+1), (int)(r->h - spc->v_oversample+1),
12524 spc->stride_in_bytes, scale * (float)spc->h_oversample,
12525 scale * (float)spc->v_oversample, 0,0, glyph, alloc);
12526
12527 if (spc->h_oversample > 1)
12528 nk_tt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
12529 r->w, r->h, spc->stride_in_bytes, (int)spc->h_oversample);
12530
12531 if (spc->v_oversample > 1)
12532 nk_tt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
12533 r->w, r->h, spc->stride_in_bytes, (int)spc->v_oversample);
12534
12535 bc->x0 = (nk_ushort) r->x;
12536 bc->y0 = (nk_ushort) r->y;
12537 bc->x1 = (nk_ushort) (r->x + r->w);
12538 bc->y1 = (nk_ushort) (r->y + r->h);
12539 bc->xadvance = scale * (float)advance;
12540 bc->xoff = (float) x0 * recip_h + sub_x;
12541 bc->yoff = (float) y0 * recip_v + sub_y;
12542 bc->xoff2 = ((float)x0 + r->w) * recip_h + sub_x;
12543 bc->yoff2 = ((float)y0 + r->h) * recip_v + sub_y;
12544 } else {
12545 return_value = 0; /* if any fail, report failure */
12546 }
12547 ++k;
12548 }
12549 }
12550 /* restore original values */
12551 spc->h_oversample = (unsigned int)old_h_over;
12552 spc->v_oversample = (unsigned int)old_v_over;
12553 return return_value;
12554 }
12555 NK_INTERN void
12556 nk_tt_GetPackedQuad(struct nk_tt_packedchar *chardata, int pw, int ph,
12557 int char_index, float *xpos, float *ypos, struct nk_tt_aligned_quad *q,
12558 int align_to_integer)
12559 {
12560 float ipw = 1.0f / (float)pw, iph = 1.0f / (float)ph;
12561 struct nk_tt_packedchar *b = (struct nk_tt_packedchar*)(chardata + char_index);
12562 if (align_to_integer) {
12563 int tx = nk_ifloorf((*xpos + b->xoff) + 0.5f);
12564 int ty = nk_ifloorf((*ypos + b->yoff) + 0.5f);
12565
12566 float x = (float)tx;
12567 float y = (float)ty;
12568
12569 q->x0 = x;
12570 q->y0 = y;
12571 q->x1 = x + b->xoff2 - b->xoff;
12572 q->y1 = y + b->yoff2 - b->yoff;
12573 } else {
12574 q->x0 = *xpos + b->xoff;
12575 q->y0 = *ypos + b->yoff;
12576 q->x1 = *xpos + b->xoff2;
12577 q->y1 = *ypos + b->yoff2;
12578 }
12579 q->s0 = b->x0 * ipw;
12580 q->t0 = b->y0 * iph;
12581 q->s1 = b->x1 * ipw;
12582 q->t1 = b->y1 * iph;
12583 *xpos += b->xadvance;
12584 }
12585
12586 /* -------------------------------------------------------------
12587 *
12588 * FONT BAKING
12589 *
12590 * --------------------------------------------------------------*/
12591 struct nk_font_bake_data {
12592 struct nk_tt_fontinfo info;
12593 struct nk_rp_rect *rects;
12594 struct nk_tt_pack_range *ranges;
12595 nk_rune range_count;
12596 };
12597
12598 struct nk_font_baker {
12599 struct nk_allocator alloc;
12600 struct nk_tt_pack_context spc;
12601 struct nk_font_bake_data *build;
12602 struct nk_tt_packedchar *packed_chars;
12603 struct nk_rp_rect *rects;
12604 struct nk_tt_pack_range *ranges;
12605 };
12606
12607 NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct nk_rp_rect);
12608 NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(struct nk_tt_pack_range);
12609 NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(struct nk_tt_packedchar);
12610 NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
12611 NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
12612
12613 NK_INTERN int
12614 nk_range_count(const nk_rune *range)
12615 {
12616 const nk_rune *iter = range;
12617 NK_ASSERT(range);
12618 if (!range) return 0;
12619 while (*(iter++) != 0);
12620 return (iter == range) ? 0 : (int)((iter - range)/2);
12621 }
12622 NK_INTERN int
12623 nk_range_glyph_count(const nk_rune *range, int count)
12624 {
12625 int i = 0;
12626 int total_glyphs = 0;
12627 for (i = 0; i < count; ++i) {
12628 int diff;
12629 nk_rune f = range[(i*2)+0];
12630 nk_rune t = range[(i*2)+1];
12631 NK_ASSERT(t >= f);
12632 diff = (int)((t - f) + 1);
12633 total_glyphs += diff;
12634 }
12635 return total_glyphs;
12636 }
12637 NK_API const nk_rune*
12638 nk_font_default_glyph_ranges(void)
12639 {
12640 NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
12641 return ranges;
12642 }
12643 NK_API const nk_rune*
12644 nk_font_chinese_glyph_ranges(void)
12645 {
12646 NK_STORAGE const nk_rune ranges[] = {
12647 0x0020, 0x00FF,
12648 0x3000, 0x30FF,
12649 0x31F0, 0x31FF,
12650 0xFF00, 0xFFEF,
12651 0x4e00, 0x9FAF,
12652 0
12653 };
12654 return ranges;
12655 }
12656 NK_API const nk_rune*
12657 nk_font_cyrillic_glyph_ranges(void)
12658 {
12659 NK_STORAGE const nk_rune ranges[] = {
12660 0x0020, 0x00FF,
12661 0x0400, 0x052F,
12662 0x2DE0, 0x2DFF,
12663 0xA640, 0xA69F,
12664 0
12665 };
12666 return ranges;
12667 }
12668 NK_API const nk_rune*
12669 nk_font_korean_glyph_ranges(void)
12670 {
12671 NK_STORAGE const nk_rune ranges[] = {
12672 0x0020, 0x00FF,
12673 0x3131, 0x3163,
12674 0xAC00, 0xD79D,
12675 0
12676 };
12677 return ranges;
12678 }
12679 NK_INTERN void
12680 nk_font_baker_memory(nk_size *temp, int *glyph_count,
12681 struct nk_font_config *config_list, int count)
12682 {
12683 int range_count = 0;
12684 int total_range_count = 0;
12685 struct nk_font_config *iter, *i;
12686
12687 NK_ASSERT(config_list);
12688 NK_ASSERT(glyph_count);
12689 if (!config_list) {
12690 *temp = 0;
12691 *glyph_count = 0;
12692 return;
12693 }
12694 *glyph_count = 0;
12695 for (iter = config_list; iter; iter = iter->next) {
12696 i = iter;
12697 do {if (!i->range) iter->range = nk_font_default_glyph_ranges();
12698 range_count = nk_range_count(i->range);
12699 total_range_count += range_count;
12700 *glyph_count += nk_range_glyph_count(i->range, range_count);
12701 } while ((i = i->n) != iter);
12702 }
12703 *temp = (nk_size)*glyph_count * sizeof(struct nk_rp_rect);
12704 *temp += (nk_size)total_range_count * sizeof(struct nk_tt_pack_range);
12705 *temp += (nk_size)*glyph_count * sizeof(struct nk_tt_packedchar);
12706 *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
12707 *temp += sizeof(struct nk_font_baker);
12708 *temp += nk_rect_align + nk_range_align + nk_char_align;
12709 *temp += nk_build_align + nk_baker_align;
12710 }
12711 NK_INTERN struct nk_font_baker*
12712 nk_font_baker(void *memory, int glyph_count, int count, struct nk_allocator *alloc)
12713 {
12714 struct nk_font_baker *baker;
12715 if (!memory) return 0;
12716 /* setup baker inside a memory block */
12717 baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
12718 baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
12719 baker->packed_chars = (struct nk_tt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
12720 baker->rects = (struct nk_rp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
12721 baker->ranges = (struct nk_tt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
12722 baker->alloc = *alloc;
12723 return baker;
12724 }
12725 NK_INTERN int
12726 nk_font_bake_pack(struct nk_font_baker *baker,
12727 nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
12728 const struct nk_font_config *config_list, int count,
12729 struct nk_allocator *alloc)
12730 {
12731 NK_STORAGE const nk_size max_height = 1024 * 32;
12732 const struct nk_font_config *config_iter, *it;
12733 int total_glyph_count = 0;
12734 int total_range_count = 0;
12735 (void)total_range_count;
12736 int range_count = 0;
12737 int i = 0;
12738
12739 NK_ASSERT(image_memory);
12740 NK_ASSERT(width);
12741 NK_ASSERT(height);
12742 NK_ASSERT(config_list);
12743 NK_ASSERT(count);
12744 NK_ASSERT(alloc);
12745
12746 if (!image_memory || !width || !height || !config_list || !count) return nk_false;
12747 for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
12748 it = config_iter;
12749 do {range_count = nk_range_count(it->range);
12750 total_range_count += range_count;
12751 total_glyph_count += nk_range_glyph_count(it->range, range_count);
12752 } while ((it = it->n) != config_iter);
12753 }
12754 /* setup font baker from temporary memory */
12755 for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
12756 it = config_iter;
12757 do {if (!nk_tt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
12758 return nk_false;
12759 } while ((it = it->n) != config_iter);
12760 }
12761 *height = 0;
12762 *width = (total_glyph_count > 1000) ? 1024 : 512;
12763 nk_tt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
12764 {
12765 int input_i = 0;
12766 int range_n = 0;
12767 int rect_n = 0;
12768 int char_n = 0;
12769
12770 if (custom) {
12771 /* pack custom user data first so it will be in the upper left corner*/
12772 struct nk_rp_rect custom_space;
12773 nk_zero(&custom_space, sizeof(custom_space));
12774 custom_space.w = (nk_rp_coord)(custom->w);
12775 custom_space.h = (nk_rp_coord)(custom->h);
12776
12777 nk_tt_PackSetOversampling(&baker->spc, 1, 1);
12778 nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, &custom_space, 1);
12779 *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
12780
12781 custom->x = (short)custom_space.x;
12782 custom->y = (short)custom_space.y;
12783 custom->w = (short)custom_space.w;
12784 custom->h = (short)custom_space.h;
12785 }
12786
12787 /* first font pass: pack all glyphs */
12788 for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
12789 config_iter = config_iter->next) {
12790 it = config_iter;
12791 do {int n = 0;
12792 int glyph_count;
12793 const nk_rune *in_range;
12794 const struct nk_font_config *cfg = it;
12795 struct nk_font_bake_data *tmp = &baker->build[input_i++];
12796
12797 /* count glyphs + ranges in current font */
12798 glyph_count = 0; range_count = 0;
12799 for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
12800 glyph_count += (int)(in_range[1] - in_range[0]) + 1;
12801 range_count++;
12802 }
12803
12804 /* setup ranges */
12805 tmp->ranges = baker->ranges + range_n;
12806 tmp->range_count = (nk_rune)range_count;
12807 range_n += range_count;
12808 for (i = 0; i < range_count; ++i) {
12809 in_range = &cfg->range[i * 2];
12810 tmp->ranges[i].font_size = cfg->size;
12811 tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
12812 tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
12813 tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
12814 char_n += tmp->ranges[i].num_chars;
12815 }
12816
12817 /* pack */
12818 tmp->rects = baker->rects + rect_n;
12819 rect_n += glyph_count;
12820 nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
12821 n = nk_tt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
12822 tmp->ranges, (int)tmp->range_count, tmp->rects);
12823 nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, tmp->rects, (int)n);
12824
12825 /* texture height */
12826 for (i = 0; i < n; ++i) {
12827 if (tmp->rects[i].was_packed)
12828 *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
12829 }
12830 } while ((it = it->n) != config_iter);
12831 }
12832 NK_ASSERT(rect_n == total_glyph_count);
12833 NK_ASSERT(char_n == total_glyph_count);
12834 NK_ASSERT(range_n == total_range_count);
12835 }
12836 *height = (int)nk_round_up_pow2((nk_uint)*height);
12837 *image_memory = (nk_size)(*width) * (nk_size)(*height);
12838 return nk_true;
12839 }
12840 NK_INTERN void
12841 nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
12842 struct nk_font_glyph *glyphs, int glyphs_count,
12843 const struct nk_font_config *config_list, int font_count)
12844 {
12845 int input_i = 0;
12846 nk_rune glyph_n = 0;
12847 const struct nk_font_config *config_iter;
12848 const struct nk_font_config *it;
12849
12850 NK_ASSERT(image_memory);
12851 NK_ASSERT(width);
12852 NK_ASSERT(height);
12853 NK_ASSERT(config_list);
12854 NK_ASSERT(baker);
12855 NK_ASSERT(font_count);
12856 NK_ASSERT(glyphs_count);
12857 if (!image_memory || !width || !height || !config_list ||
12858 !font_count || !glyphs || !glyphs_count)
12859 return;
12860
12861 /* second font pass: render glyphs */
12862 nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
12863 baker->spc.pixels = (unsigned char*)image_memory;
12864 baker->spc.height = (int)height;
12865 for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
12866 config_iter = config_iter->next) {
12867 it = config_iter;
12868 do {const struct nk_font_config *cfg = it;
12869 struct nk_font_bake_data *tmp = &baker->build[input_i++];
12870 nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
12871 nk_tt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges,
12872 (int)tmp->range_count, tmp->rects, &baker->alloc);
12873 } while ((it = it->n) != config_iter);
12874 } nk_tt_PackEnd(&baker->spc, &baker->alloc);
12875
12876 /* third pass: setup font and glyphs */
12877 for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
12878 config_iter = config_iter->next) {
12879 it = config_iter;
12880 do {nk_size i = 0;
12881 int char_idx = 0;
12882 nk_rune glyph_count = 0;
12883 const struct nk_font_config *cfg = it;
12884 struct nk_font_bake_data *tmp = &baker->build[input_i++];
12885 struct nk_baked_font *dst_font = cfg->font;
12886
12887 float font_scale = nk_tt_ScaleForPixelHeight(&tmp->info, cfg->size);
12888 int unscaled_ascent, unscaled_descent, unscaled_line_gap;
12889 nk_tt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
12890 &unscaled_line_gap);
12891
12892 /* fill baked font */
12893 if (!cfg->merge_mode) {
12894 dst_font->ranges = cfg->range;
12895 dst_font->height = cfg->size;
12896 dst_font->ascent = ((float)unscaled_ascent * font_scale);
12897 dst_font->descent = ((float)unscaled_descent * font_scale);
12898 dst_font->glyph_offset = glyph_n;
12899 // Need to zero this, or it will carry over from a previous
12900 // bake, and cause a segfault when accessing glyphs[].
12901 dst_font->glyph_count = 0;
12902 }
12903
12904 /* fill own baked font glyph array */
12905 for (i = 0; i < tmp->range_count; ++i) {
12906 struct nk_tt_pack_range *range = &tmp->ranges[i];
12907 for (char_idx = 0; char_idx < range->num_chars; char_idx++)
12908 {
12909 nk_rune codepoint = 0;
12910 float dummy_x = 0, dummy_y = 0;
12911 struct nk_tt_aligned_quad q;
12912 struct nk_font_glyph *glyph;
12913
12914 /* query glyph bounds from stb_truetype */
12915 const struct nk_tt_packedchar *pc = &range->chardata_for_range[char_idx];
12916 if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
12917 codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
12918 nk_tt_GetPackedQuad(range->chardata_for_range, (int)width,
12919 (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
12920
12921 /* fill own glyph type with data */
12922 glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
12923 glyph->codepoint = codepoint;
12924 glyph->x0 = q.x0; glyph->y0 = q.y0;
12925 glyph->x1 = q.x1; glyph->y1 = q.y1;
12926 glyph->y0 += (dst_font->ascent + 0.5f);
12927 glyph->y1 += (dst_font->ascent + 0.5f);
12928 glyph->w = glyph->x1 - glyph->x0 + 0.5f;
12929 glyph->h = glyph->y1 - glyph->y0;
12930
12931 if (cfg->coord_type == NK_COORD_PIXEL) {
12932 glyph->u0 = q.s0 * (float)width;
12933 glyph->v0 = q.t0 * (float)height;
12934 glyph->u1 = q.s1 * (float)width;
12935 glyph->v1 = q.t1 * (float)height;
12936 } else {
12937 glyph->u0 = q.s0;
12938 glyph->v0 = q.t0;
12939 glyph->u1 = q.s1;
12940 glyph->v1 = q.t1;
12941 }
12942 glyph->xadvance = (pc->xadvance + cfg->spacing.x);
12943 if (cfg->pixel_snap)
12944 glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
12945 glyph_count++;
12946 }
12947 }
12948 dst_font->glyph_count += glyph_count;
12949 glyph_n += glyph_count;
12950 } while ((it = it->n) != config_iter);
12951 }
12952 }
12953 NK_INTERN void
12954 nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
12955 struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
12956 int tex_height, char white, char black)
12957 {
12958 nk_byte *pixels;
12959 int y = 0;
12960 int x = 0;
12961 int n = 0;
12962
12963 NK_ASSERT(img_memory);
12964 NK_ASSERT(img_width);
12965 NK_ASSERT(img_height);
12966 NK_ASSERT(texture_data_mask);
12967 NK_UNUSED(tex_height);
12968 if (!img_memory || !img_width || !img_height || !texture_data_mask)
12969 return;
12970
12971 pixels = (nk_byte*)img_memory;
12972 for (y = 0, n = 0; y < tex_height; ++y) {
12973 for (x = 0; x < tex_width; ++x, ++n) {
12974 const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
12975 const int off1 = off0 + 1 + tex_width;
12976 pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
12977 pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
12978 }
12979 }
12980 }
12981 NK_INTERN void
12982 nk_font_bake_convert(void *out_memory, int img_width, int img_height,
12983 const void *in_memory)
12984 {
12985 int n = 0;
12986 nk_rune *dst;
12987 const nk_byte *src;
12988
12989 NK_ASSERT(out_memory);
12990 NK_ASSERT(in_memory);
12991 NK_ASSERT(img_width);
12992 NK_ASSERT(img_height);
12993 if (!out_memory || !in_memory || !img_height || !img_width) return;
12994
12995 dst = (nk_rune*)out_memory;
12996 src = (const nk_byte*)in_memory;
12997 for (n = (int)(img_width * img_height); n > 0; n--)
12998 *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
12999 }
13000
13001 /* -------------------------------------------------------------
13002 *
13003 * FONT
13004 *
13005 * --------------------------------------------------------------*/
13006 NK_INTERN float
13007 nk_font_text_width(nk_handle handle, float height, const char *text, int len)
13008 {
13009 nk_rune unicode;
13010 int text_len = 0;
13011 float text_width = 0;
13012 int glyph_len = 0;
13013 float scale = 0;
13014
13015 struct nk_font *font = (struct nk_font*)handle.ptr;
13016 NK_ASSERT(font);
13017 NK_ASSERT(font->glyphs);
13018 if (!font || !text || !len)
13019 return 0;
13020
13021 scale = height/font->info.height;
13022 glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
13023 if (!glyph_len) return 0;
13024 while (text_len <= (int)len && glyph_len) {
13025 const struct nk_font_glyph *g;
13026 if (unicode == NK_UTF_INVALID) break;
13027
13028 /* query currently drawn glyph information */
13029 g = nk_font_find_glyph(font, unicode);
13030 text_width += g->xadvance * scale;
13031
13032 /* offset next glyph */
13033 glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
13034 text_len += glyph_len;
13035 }
13036 return text_width;
13037 }
13038 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
13039 NK_INTERN void
13040 nk_font_query_font_glyph(nk_handle handle, float height,
13041 struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
13042 {
13043 float scale;
13044 const struct nk_font_glyph *g;
13045 struct nk_font *font;
13046
13047 NK_ASSERT(glyph);
13048 NK_UNUSED(next_codepoint);
13049
13050 font = (struct nk_font*)handle.ptr;
13051 NK_ASSERT(font);
13052 NK_ASSERT(font->glyphs);
13053 if (!font || !glyph)
13054 return;
13055
13056 scale = height/font->info.height;
13057 g = nk_font_find_glyph(font, codepoint);
13058 glyph->width = (g->x1 - g->x0) * scale;
13059 glyph->height = (g->y1 - g->y0) * scale;
13060 glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
13061 glyph->xadvance = (g->xadvance * scale);
13062 glyph->uv[0] = nk_vec2(g->u0, g->v0);
13063 glyph->uv[1] = nk_vec2(g->u1, g->v1);
13064 }
13065 #endif
13066 NK_API const struct nk_font_glyph*
13067 nk_font_find_glyph(struct nk_font *font, nk_rune unicode)
13068 {
13069 int i = 0;
13070 int count;
13071 int total_glyphs = 0;
13072 const struct nk_font_glyph *glyph = 0;
13073 const struct nk_font_config *iter = 0;
13074
13075 NK_ASSERT(font);
13076 NK_ASSERT(font->glyphs);
13077 NK_ASSERT(font->info.ranges);
13078 if (!font || !font->glyphs) return 0;
13079
13080 glyph = font->fallback;
13081 iter = font->config;
13082 do {count = nk_range_count(iter->range);
13083 for (i = 0; i < count; ++i) {
13084 nk_rune f = iter->range[(i*2)+0];
13085 nk_rune t = iter->range[(i*2)+1];
13086 int diff = (int)((t - f) + 1);
13087 if (unicode >= f && unicode <= t)
13088 return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
13089 total_glyphs += diff;
13090 }
13091 } while ((iter = iter->n) != font->config);
13092 return glyph;
13093 }
13094 NK_INTERN void
13095 nk_font_init(struct nk_font *font, float pixel_height,
13096 nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
13097 const struct nk_baked_font *baked_font, nk_handle atlas)
13098 {
13099 struct nk_baked_font baked;
13100 NK_ASSERT(font);
13101 NK_ASSERT(glyphs);
13102 NK_ASSERT(baked_font);
13103 if (!font || !glyphs || !baked_font)
13104 return;
13105
13106 baked = *baked_font;
13107 font->fallback = 0;
13108 font->info = baked;
13109 font->scale = (float)pixel_height / (float)font->info.height;
13110 font->glyphs = &glyphs[baked_font->glyph_offset];
13111 font->texture = atlas;
13112 font->fallback_codepoint = fallback_codepoint;
13113 font->fallback = nk_font_find_glyph(font, fallback_codepoint);
13114
13115 font->handle.height = font->info.height * font->scale;
13116 font->handle.width = nk_font_text_width;
13117 font->handle.userdata.ptr = font;
13118 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
13119 font->handle.query = nk_font_query_font_glyph;
13120 font->handle.texture = font->texture;
13121 #endif
13122 }
13123
13124 /* ---------------------------------------------------------------------------
13125 *
13126 * DEFAULT FONT
13127 *
13128 * ProggyClean.ttf
13129 * Copyright (c) 2004, 2005 Tristan Grimmer
13130 * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
13131 * Download and more information at http://upperbounds.net
13132 *-----------------------------------------------------------------------------*/
13133 #ifdef __clang__
13134 #pragma clang diagnostic push
13135 #pragma clang diagnostic ignored "-Woverlength-strings"
13136 #elif defined(__GNUC__) || defined(__GNUG__)
13137 #pragma GCC diagnostic push
13138 #pragma GCC diagnostic ignored "-Woverlength-strings"
13139 #endif
13140
13141 #ifdef NK_INCLUDE_DEFAULT_FONT
13142
13143 NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
13144 "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
13145 "2*>]b(MC;$jPfY.;h^`IWM9<Lh2TlS+f-s$o6Q<BWH`YiU.xfLq$N;$0iR/GX:U(jcW2p/W*q?-qmnUCI;jHSAiFWM.R*kU@C=GH?a9wp8f$e.-4^Qg1)Q-GL(lf(r/7GrRgwV%MS=C#"
13146 "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#[email protected]<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
13147 "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#[email protected]'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
13148 "kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`&#0j@'DbG&#^$PG.Ll+DNa<XCMKEV*N)LN/N"
13149 "*b=%Q6pia-Xg8I$<MR&,VdJe$<(7G;Ckl'&hF;;$<_=X(b.RS%%)###MPBuuE1V:v&cX&#2m#(&cV]`k9OhLMbn%s$G2,B$BfD3X*sp5#l,$R#]x_X1xKX%b5U*[r5iMfUo9U`N99hG)"
13150 "tm+/Us9pG)XPu`<0s-)WTt(gCRxIg(%6sfh=ktMKn3j)<6<b5Sk_/0(^]AaN#(p/L>&VZ>1i%h1S9u5o@YaaW$e+b<TWFn/Z:Oh(Cx2$lNEoN^e)#CFY@@I;BOQ*sRwZtZxRcU7uW6CX"
13151 "ow0i(?$Q[cjOd[P4d)]>ROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc."
13152 "x]Ip.PH^'/aqUO/$1WxLoW0[iLA<QT;5HKD+@qQ'NQ(3_PLhE48R.qAPSwQ0/WK?Z,[x?-J;jQTWA0X@KJ(_Y8N-:/M74:/-ZpKrUss?d#dZq]DAbkU*JqkL+nwX@@47`5>w=4h(9.`G"
13153 "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?G<Nald$qs]@]L<J7bR*>gv:[7MI2k).'2($5FNP&EQ(,)"
13154 "U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#"
13155 "'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM"
13156 "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0<q-]L_?^)1vw'.,MRsqVr.L;aN&#/EgJ)PBc[-f>+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu"
13157 "Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/"
13158 "/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[K<L"
13159 "%a2E-grWVM3@2=-k22tL]4$##6We'8UJCKE[d_=%wI;'6X-GsLX4j^SgJ$##R*w,vP3wK#iiW&#*h^D&R?jp7+/u&#(AP##XU8c$fSYW-J95_-Dp[g9wcO&#M-h1OcJlc-*vpw0xUX&#"
13160 "OQFKNX@QI'IoPp7nb,QU//MQ&ZDkKP)X<WSVL(68uVl&#c'[0#(s1X&xm$Y%B7*K:eDA323j998GXbA#pwMs-jgD$9QISB-A_(aN4xoFM^@C58D0+Q+q3n0#3U1InDjF682-SjMXJK)("
13161 "h$hxua_K]ul92%'BOU&#BRRh-slg8KDlr:%L71Ka:.A;%YULjDPmL<LYs8i#XwJOYaKPKc1h:'9Ke,g)b),78=I39B;xiY$bgGw-&.Zi9InXDuYa%G*f2Bq7mn9^#p1vv%#(Wi-;/Z5h"
13162 "o;#2:;%d&#x9v68C5g?ntX0X)pT`;%pB3q7mgGN)3%(P8nTd5L7GeA-GL@+%J3u2:(Yf>et`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO"
13163 "j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J<j$UpK<Q4a1]MupW^-"
13164 "sj_$%[HK%'F####QRZJ::Y3EGl4'@%FkiAOg#p[##O`gukTfBHagL<LHw%q&OV0##F=6/:chIm0@eCP8X]:kFI%hl8hgO@RcBhS-@Qb$%+m=hPDLg*%K8ln(wcf3/'DW-$.lR?n[nCH-"
13165 "eXOONTJlh:.RYF%3'p6sq:UIMA945&^HFS87@$EP2iG<-lCO$%c`uKGD3rC$x0BL8aFn--`ke%#HMP'vh1/R&O_J9'um,.<tx[@%wsJk&bUT2`0uMv7gg#qp/ij.L56'hl;.s5CUrxjO"
13166 "M7-##.l+Au'A&O:-T72L]P`&=;ctp'XScX*rU.>-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%"
13167 "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$M<Jnq79VsJW/mWS*PUiq76;]/NM_>hLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]"
13168 "%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et"
13169 "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$<M-SGZ':+Q_k+uvOSLiEo(<aD/K<CCc`'Lx>'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:"
13170 "a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VB<HFF*qL("
13171 "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<[email protected];x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
13172 "nKnw'Ho8C=Y>pqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<<aG/1N$#FX$0V5Y6x'aErI3I$7x%E`v<-BY,)%-?Psf*l?%C3.mM(=/M0:JxG'?"
13173 "7WhH%o'a<-80g0NBxoO(GH<dM]n.+%q@jH?f.UsJ2Ggs&4<-e47&Kl+f//9@`b+?.TeN_&B8Ss?v;^Trk;f#YvJkl&w$]>-+k?'(<S:68tq*WoDfZu';mM?8X[ma8W%*`-=;D.(nc7/;"
13174 ")g:T1=^J$&BRV(-lTmNB6xqB[@0*o.erM*<SWF]u2=st-*(6v>^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M"
13175 "D?@f&1'BW-)Ju<L25gl8uhVm1hL$##*8###'A3/LkKW+(^rWX?5W_8g)a(m&K8P>#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX("
13176 "P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs"
13177 "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
13178 "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aeg<Z'<$#4H)6,>e0jT6'N#(q%.O=?2S]u*(m<-"
13179 "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;[email protected]$m%#QvQS8U@)2Z+3K:AKM5i"
13180 "sZ88+dKQ)W6>J%CL<KE>`.d*(B`-n8D9oK<Up]c$X$(,)M8Zt7/[rdkqTgl-0cuGMv'?>-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P&#9r+$%CE=68>K8r0=dSC%%(@p7"
13181 ".m7jilQ02'0-VWAg<a/''3u.=4L$Y)6k/K:_[3=&jvL<L0C/2'v:^;-DIBW,B4E68:kZ;%?8(Q8BH=kO65BW?xSG&#@uU,DS*,?.+(o(#1vCS8#CHF>TlGW'b)Tq7VT9q^*^$$.:&N@@"
13182 "$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*"
13183 "hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u"
13184 "@-W$U%VEQ/,,>>#)D<h#`)h0:<Q6909ua+&VU%n2:cG3FJ-%@Bj-DgLr`Hw&HAKjKjseK</xKT*)B,N9X3]krc12t'pgTV(Lv-tL[xg_%=M_q7a^x?7Ubd>#%8cY#YZ?=,`Wdxu/ae&#"
13185 "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$s<Eh#c&)q.MXI%#v9ROa5FZO%sF7q7Nwb&#ptUJ:aqJe$Sl68%.D###EC><?-aF&#RNQv>o8lKN%5/$(vdfq7+ebA#"
13186 "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(<c`Q8N)jEIF*+?P2a8g%)$q]o2aH8C&<SibC/q,(e:v;-b#6[$NtDZ84Je2KNvB#$P5?tQ3nt(0"
13187 "d=j.LQf./Ll33+(;q3L-w=8dX$#WF&uIJ@-bfI>%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoF&#4DoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8"
13188 "6e%B/:=>)N4xeW.*wft-;$'58-ESqr<b?UI(_%@[P46>#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#"
13189 "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjL<Lni;''X.`$#8+1GD"
13190 ":k$YUWsbn8ogh6rxZ2Z9]%nd+>V#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#<NEdtg(n'=S1A(Q1/I&4([%dM`,Iu'1:_hL>SfD07&6D<fp8dHM7/g+"
13191 "tlPN9J*rKaPct&?'uBCem^jn%9_K)<,C5K3s=5g&GmJb*[SYq7K;TRLGCsM-$$;S%:Y@r7AK0pprpL<Lrh,q7e/%KWK:50I^+m'vi`3?%Zp+<-d+$L-Sv:@.o19n$s0&39;kn;S%BSq*"
13192 "$3WoJSCLweV[aZ'MQIjO<7;X-X;&+dMLvu#^UsGEC9WEc[X(wI7#2.(F0jV*eZf<-Qv3J-c+J5AlrB#$p(H68LvEA'q3n0#m,[`*8Ft)FcYgEud]CWfm68,(aLA$@EFTgLXoBq/UPlp7"
13193 ":d[/;r_ix=:TF`S5H-b<LI&HY(K=h#)]Lk$K14lVfm:x$H<3^Ql<M`$OhapBnkup'D#L$Pb_`N*g]2e;X/Dtg,bsj&K#2[-:iYr'_wgH)NUIR8a1n#S?Yej'h8^58UbZd+^FKD*T@;6A"
13194 "7aQC[K8d-(v6GI$x:T<&'Gp5Uf>@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-<aN((^7('#Z0wK#5GX@7"
13195 "u][`*S^43933A4rl][`*O4CgLEl]v$1Q3AeF37dbXk,.)vj#x'd`;qgbQR%FW,2(?LO=s%Sc68%NP'##Aotl8x=BE#j1UD([3$M(]UI2LX3RpKN@;/#f'f/&_mt&F)XdF<9t4)Qa.*kT"
13196 "LwQ'(TTB9.xH'>#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5<N?)NBS)QN*_I,?&)2'IM%L3I)X((e/dl2&8'<M"
13197 ":^#M*Q+[T.Xri.LYS3v%fF`68h;b-X[/En'CR.q7E)p'/kle2HM,u;^%OKC-N+Ll%F9CF<Nf'^#t2L,;27W:0O@6##U6W7:$rJfLWHj$#)woqBefIZ.PK<b*t7ed;p*_m;4ExK#h@&]>"
13198 "_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%"
13199 "hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;"
13200 "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmL<LD)F^%[tC'8;+9E#C$g%#5Y>q9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:"
13201 "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3<n-&%H%b<FDj2M<hH=&Eh<2Len$b*aTX=-8QxN)k11IM1c^j%"
13202 "9s<L<NFSo)B?+<-(GxsF,^-Eh@$4dXhN$+#rxK8'je'D7k`e;)2pYwPA'_p9&@^18ml1^[@g4t*[JOa*[=Qp7(qJ_oOL^('7fB&Hq-:sf,sNj8xq^>$U4O]GKx'm9)b@p7YsvK3w^YR-"
13203 "CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*"
13204 "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdF<TddF<9Ah-6&9tWoDlh]&1SpGMq>Ti1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IX<N+T+0MlMBPQ*Vj>SsD<U4JHY"
13205 "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
13206 "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n<bhPmUkMw>%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL<LoNs'6,'85`"
13207 "0?t/'_U59@]ddF<#LdF<eWdF<OuN/45rY<-L@&#+fm>69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdF<gR@2L=FNU-<b[(9c/ML3m;Z[$oF3g)GAWqpARc=<ROu7cL5l;-[A]%/"
13208 "+fsd;l#SafT/f*W]0=O'$(Tb<[)*@e775R-:Yob%g*>l*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj"
13209 "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#<IGe;__.thjZl<%w(Wk2xmp4Q@I#I9,DF]u7-P=.-_:YJ]aS@V"
13210 "?6*C()dOp7:WL,b&3Rg/.cmM9&r^>$(>.Z-I&J(Q0Hd5Q%7Co-b`-c<N(6r@ip+AurK<m86QIth*#v;-OBqi+L7wDE-Ir8K['m+DDSLwK&/.?-V%U_%3:qKNu$_b*B-kp7NaD'QdWQPK"
13211 "Yq[@>P)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8<FfNkgg^oIbah*#8/Qt$F&:K*-(N/'+1vMB,u()-a.VUU*#[e%gAAO(S>WlA2);Sa"
13212 ">gXm8YB`1d@K#n]76-a$U,mF<fX]idqd)<3,]J7JmW4`6]uks=4-72L(jEk+:bJ0M^q-8Dm_Z?0olP1C9Sa&H[d&c$ooQUj]Exd*3ZM@-WGW2%s',B-_M%>%Ul:#/'xoFM9QX-$.QN'>"
13213 "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B</R90;eZ]%Ncq;-Tl]#F>2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I"
13214 "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1<Vc52=u`3^o-n1'g4v58Hj&6_t7$##?M)c<$bgQ_'SY((-xkA#"
13215 "Y(,p'H9rIVY-b,'%bCPF7.J<Up^,(dU1VY*5#WkTU>h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-u<Hp,3@e^9UB1J+ak9-TN/mhKPg+AJYd$"
13216 "MlvAF_jCK*.O-^(63adMT->W%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)"
13217 "i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo"
13218 "1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P"
13219 "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*<h`e-GI7)?OK2A.d7_c)?wQ5AS@DL3r#7fSkgl6-++D:'A,uq7SvlB$pcpH'q3n0#_%dY#xCpr-l<F0NR@-##FEV6NTF6##$l84N1w?AO>'IAO"
13220 "URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#"
13221 ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T<XoIB&hx=T1PcDaB&;HH+-AFr?(m9HZV)FKS8JCw;SD=6[^/DZUL`EUDf]GGlG&>"
13222 "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#<xU?#@.i?#D:%@#HF7@#LRI@#P_[@#Tkn@#Xw*A#]-=A#a9OA#"
13223 "d<F&#*;G##.GY##2Sl##6`($#:l:$#>xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4&#3^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4"
13224 "A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#"
13225 "/QHC#3^ZC#7jmC#;v)D#?,<D#C8ND#GDaD#KPsD#O]/E#g1A5#KA*1#gC17#MGd;#8(02#L-d3#rWM4#Hga1#,<w0#T.j<#O#'2#CYN1#qa^:#_4m3#o@/=#eG8=#t8J5#`+78#4uI-#"
13226 "m3B2#SB[8#Q0@8#i[*9#iOn8#1Nm;#^sN9#qh<9#:=x-#P;K2#$%X9#bC+.#Rg;<#mN=.#MTF.#RZO.#2?)4#Y#(/#[)1/#b;L/#dAU/#0Sv;#lY$0#n`-0#sf60#(F24#wrH0#%/e0#"
13227 "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
13228 "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
13229 "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
13230
13231 #endif /* NK_INCLUDE_DEFAULT_FONT */
13232
13233 #define NK_CURSOR_DATA_W 90
13234 #define NK_CURSOR_DATA_H 27
13235 NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
13236 {
13237 "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
13238 "..- -X.....X- X.X - X.X -X.....X - X.....X"
13239 "--- -XXX.XXX- X...X - X...X -X....X - X....X"
13240 "X - X.X - X.....X - X.....X -X...X - X...X"
13241 "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
13242 "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
13243 "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
13244 "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
13245 "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
13246 "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
13247 "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
13248 "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
13249 "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
13250 "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
13251 "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
13252 "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
13253 "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
13254 "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
13255 "X.X X..X - -X.......X- X.......X - XX XX - "
13256 "XX X..X - - X.....X - X.....X - X.X X.X - "
13257 " X..X - X...X - X...X - X..X X..X - "
13258 " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
13259 "------------ - X - X -X.....................X- "
13260 " ----------------------------------- X...XXXXXXXXXXXXX...X - "
13261 " - X..X X..X - "
13262 " - X.X X.X - "
13263 " - XX XX - "
13264 };
13265
13266 #ifdef __clang__
13267 #pragma clang diagnostic pop
13268 #elif defined(__GNUC__) || defined(__GNUG__)
13269 #pragma GCC diagnostic pop
13270 #endif
13271
13272 NK_GLOBAL unsigned char *nk__barrier;
13273 NK_GLOBAL unsigned char *nk__barrier2;
13274 NK_GLOBAL unsigned char *nk__barrier3;
13275 NK_GLOBAL unsigned char *nk__barrier4;
13276 NK_GLOBAL unsigned char *nk__dout;
13277
13278 NK_INTERN unsigned int
13279 nk_decompress_length(unsigned char *input)
13280 {
13281 return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
13282 }
13283 NK_INTERN void
13284 nk__match(unsigned char *data, unsigned int length)
13285 {
13286 /* INVERSE of memmove... write each byte before copying the next...*/
13287 NK_ASSERT (nk__dout + length <= nk__barrier);
13288 if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
13289 if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
13290 while (length--) *nk__dout++ = *data++;
13291 }
13292 NK_INTERN void
13293 nk__lit(unsigned char *data, unsigned int length)
13294 {
13295 NK_ASSERT (nk__dout + length <= nk__barrier);
13296 if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
13297 if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
13298 NK_MEMCPY(nk__dout, data, length);
13299 nk__dout += length;
13300 }
13301 NK_INTERN unsigned char*
13302 nk_decompress_token(unsigned char *i)
13303 {
13304 #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
13305 #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
13306 #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
13307
13308 if (*i >= 0x20) { /* use fewer if's for cases that expand small */
13309 if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
13310 else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
13311 else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
13312 } else { /* more ifs for cases that expand large, since overhead is amortized */
13313 if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
13314 else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
13315 else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
13316 else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
13317 else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
13318 else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
13319 }
13320 return i;
13321 }
13322 NK_INTERN unsigned int
13323 nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
13324 {
13325 const unsigned long ADLER_MOD = 65521;
13326 unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
13327 unsigned long blocklen, i;
13328
13329 blocklen = buflen % 5552;
13330 while (buflen) {
13331 for (i=0; i + 7 < blocklen; i += 8) {
13332 s1 += buffer[0]; s2 += s1;
13333 s1 += buffer[1]; s2 += s1;
13334 s1 += buffer[2]; s2 += s1;
13335 s1 += buffer[3]; s2 += s1;
13336 s1 += buffer[4]; s2 += s1;
13337 s1 += buffer[5]; s2 += s1;
13338 s1 += buffer[6]; s2 += s1;
13339 s1 += buffer[7]; s2 += s1;
13340 buffer += 8;
13341 }
13342 for (; i < blocklen; ++i) {
13343 s1 += *buffer++; s2 += s1;
13344 }
13345
13346 s1 %= ADLER_MOD; s2 %= ADLER_MOD;
13347 buflen -= (unsigned int)blocklen;
13348 blocklen = 5552;
13349 }
13350 return (unsigned int)(s2 << 16) + (unsigned int)s1;
13351 }
13352 NK_INTERN unsigned int
13353 nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
13354 {
13355 unsigned int olen;
13356 if (nk__in4(0) != 0x57bC0000) return 0;
13357 if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
13358 olen = nk_decompress_length(i);
13359 nk__barrier2 = i;
13360 nk__barrier3 = i+length;
13361 nk__barrier = output + olen;
13362 nk__barrier4 = output;
13363 i += 16;
13364
13365 nk__dout = output;
13366 for (;;) {
13367 unsigned char *old_i = i;
13368 i = nk_decompress_token(i);
13369 if (i == old_i) {
13370 if (*i == 0x05 && i[1] == 0xfa) {
13371 NK_ASSERT(nk__dout == output + olen);
13372 if (nk__dout != output + olen) return 0;
13373 if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
13374 return 0;
13375 return olen;
13376 } else {
13377 NK_ASSERT(0); /* NOTREACHED */
13378 return 0;
13379 }
13380 }
13381 NK_ASSERT(nk__dout <= output + olen);
13382 if (nk__dout > output + olen)
13383 return 0;
13384 }
13385 }
13386 NK_INTERN unsigned int
13387 nk_decode_85_byte(char c)
13388 {
13389 return (unsigned int)((c >= '\\') ? c-36 : c-35);
13390 }
13391 NK_INTERN void
13392 nk_decode_85(unsigned char* dst, const unsigned char* src)
13393 {
13394 while (*src)
13395 {
13396 unsigned int tmp =
13397 nk_decode_85_byte((char)src[0]) +
13398 85 * (nk_decode_85_byte((char)src[1]) +
13399 85 * (nk_decode_85_byte((char)src[2]) +
13400 85 * (nk_decode_85_byte((char)src[3]) +
13401 85 * nk_decode_85_byte((char)src[4]))));
13402
13403 /* we can't assume little-endianess. */
13404 dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
13405 dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
13406 dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
13407 dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
13408
13409 src += 5;
13410 dst += 4;
13411 }
13412 }
13413
13414 /* -------------------------------------------------------------
13415 *
13416 * FONT ATLAS
13417 *
13418 * --------------------------------------------------------------*/
13419 NK_API struct nk_font_config
13420 nk_font_config(float pixel_height)
13421 {
13422 struct nk_font_config cfg;
13423 nk_zero_struct(cfg);
13424 cfg.ttf_blob = 0;
13425 cfg.ttf_size = 0;
13426 cfg.ttf_data_owned_by_atlas = 0;
13427 cfg.size = pixel_height;
13428 cfg.oversample_h = 3;
13429 cfg.oversample_v = 1;
13430 cfg.pixel_snap = 0;
13431 cfg.coord_type = NK_COORD_UV;
13432 cfg.spacing = nk_vec2(0,0);
13433 cfg.range = nk_font_default_glyph_ranges();
13434 cfg.merge_mode = 0;
13435 cfg.fallback_glyph = '?';
13436 cfg.font = 0;
13437 cfg.n = 0;
13438 return cfg;
13439 }
13440 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
13441 NK_API void
13442 nk_font_atlas_init_default(struct nk_font_atlas *atlas)
13443 {
13444 NK_ASSERT(atlas);
13445 if (!atlas) return;
13446 nk_zero_struct(*atlas);
13447 atlas->temporary.userdata.ptr = 0;
13448 atlas->temporary.alloc = nk_malloc;
13449 atlas->temporary.free = nk_mfree;
13450 atlas->permanent.userdata.ptr = 0;
13451 atlas->permanent.alloc = nk_malloc;
13452 atlas->permanent.free = nk_mfree;
13453 }
13454 #endif
13455 NK_API void
13456 nk_font_atlas_init(struct nk_font_atlas *atlas, struct nk_allocator *alloc)
13457 {
13458 NK_ASSERT(atlas);
13459 NK_ASSERT(alloc);
13460 if (!atlas || !alloc) return;
13461 nk_zero_struct(*atlas);
13462 atlas->permanent = *alloc;
13463 atlas->temporary = *alloc;
13464 }
13465 NK_API void
13466 nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
13467 struct nk_allocator *permanent, struct nk_allocator *temporary)
13468 {
13469 NK_ASSERT(atlas);
13470 NK_ASSERT(permanent);
13471 NK_ASSERT(temporary);
13472 if (!atlas || !permanent || !temporary) return;
13473 nk_zero_struct(*atlas);
13474 atlas->permanent = *permanent;
13475 atlas->temporary = *temporary;
13476 }
13477 NK_API void
13478 nk_font_atlas_begin(struct nk_font_atlas *atlas)
13479 {
13480 NK_ASSERT(atlas);
13481 NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
13482 NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
13483 if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
13484 !atlas->temporary.alloc || !atlas->temporary.free) return;
13485 if (atlas->glyphs) {
13486 atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
13487 atlas->glyphs = 0;
13488 }
13489 if (atlas->pixel) {
13490 atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
13491 atlas->pixel = 0;
13492 }
13493 }
13494 NK_API struct nk_font*
13495 nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
13496 {
13497 struct nk_font *font = 0;
13498 struct nk_font_config *cfg;
13499
13500 NK_ASSERT(atlas);
13501 NK_ASSERT(atlas->permanent.alloc);
13502 NK_ASSERT(atlas->permanent.free);
13503 NK_ASSERT(atlas->temporary.alloc);
13504 NK_ASSERT(atlas->temporary.free);
13505
13506 NK_ASSERT(config);
13507 NK_ASSERT(config->ttf_blob);
13508 NK_ASSERT(config->ttf_size);
13509 NK_ASSERT(config->size > 0.0f);
13510
13511 if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
13512 !atlas->permanent.alloc || !atlas->permanent.free ||
13513 !atlas->temporary.alloc || !atlas->temporary.free)
13514 return 0;
13515
13516 /* allocate font config */
13517 cfg = (struct nk_font_config*)
13518 atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
13519 NK_MEMCPY(cfg, config, sizeof(*config));
13520 cfg->n = cfg;
13521 cfg->p = cfg;
13522
13523 if (!config->merge_mode) {
13524 /* insert font config into list */
13525 if (!atlas->config) {
13526 atlas->config = cfg;
13527 cfg->next = 0;
13528 } else {
13529 struct nk_font_config *i = atlas->config;
13530 while (i->next) i = i->next;
13531 i->next = cfg;
13532 cfg->next = 0;
13533 }
13534 /* allocate new font */
13535 font = (struct nk_font*)
13536 atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
13537 NK_ASSERT(font);
13538 nk_zero(font, sizeof(*font));
13539 if (!font) return 0;
13540 font->config = cfg;
13541
13542 /* insert font into list */
13543 if (!atlas->fonts) {
13544 atlas->fonts = font;
13545 font->next = 0;
13546 } else {
13547 struct nk_font *i = atlas->fonts;
13548 while (i->next) i = i->next;
13549 i->next = font;
13550 font->next = 0;
13551 }
13552 cfg->font = &font->info;
13553 } else {
13554 /* extend previously added font */
13555 struct nk_font *f = 0;
13556 struct nk_font_config *c = 0;
13557 NK_ASSERT(atlas->font_num);
13558 f = atlas->fonts;
13559 c = f->config;
13560 cfg->font = &f->info;
13561
13562 cfg->n = c;
13563 cfg->p = c->p;
13564 c->p->n = cfg;
13565 c->p = cfg;
13566 }
13567 /* create own copy of .TTF font blob */
13568 if (!config->ttf_data_owned_by_atlas) {
13569 cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
13570 NK_ASSERT(cfg->ttf_blob);
13571 if (!cfg->ttf_blob) {
13572 atlas->font_num++;
13573 return 0;
13574 }
13575 NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
13576 cfg->ttf_data_owned_by_atlas = 1;
13577 }
13578 atlas->font_num++;
13579 return font;
13580 }
13581 NK_API struct nk_font*
13582 nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
13583 nk_size size, float height, const struct nk_font_config *config)
13584 {
13585 struct nk_font_config cfg;
13586 NK_ASSERT(memory);
13587 NK_ASSERT(size);
13588
13589 NK_ASSERT(atlas);
13590 NK_ASSERT(atlas->temporary.alloc);
13591 NK_ASSERT(atlas->temporary.free);
13592 NK_ASSERT(atlas->permanent.alloc);
13593 NK_ASSERT(atlas->permanent.free);
13594 if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
13595 !atlas->permanent.alloc || !atlas->permanent.free)
13596 return 0;
13597
13598 cfg = (config) ? *config: nk_font_config(height);
13599 cfg.ttf_blob = memory;
13600 cfg.ttf_size = size;
13601 cfg.size = height;
13602 cfg.ttf_data_owned_by_atlas = 0;
13603 return nk_font_atlas_add(atlas, &cfg);
13604 }
13605 #ifdef NK_INCLUDE_STANDARD_IO
13606 NK_API struct nk_font*
13607 nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
13608 float height, const struct nk_font_config *config)
13609 {
13610 nk_size size;
13611 char *memory;
13612 struct nk_font_config cfg;
13613
13614 NK_ASSERT(atlas);
13615 NK_ASSERT(atlas->temporary.alloc);
13616 NK_ASSERT(atlas->temporary.free);
13617 NK_ASSERT(atlas->permanent.alloc);
13618 NK_ASSERT(atlas->permanent.free);
13619
13620 if (!atlas || !file_path) return 0;
13621 memory = nk_file_load(file_path, &size, &atlas->permanent);
13622 if (!memory) return 0;
13623
13624 cfg = (config) ? *config: nk_font_config(height);
13625 cfg.ttf_blob = memory;
13626 cfg.ttf_size = size;
13627 cfg.size = height;
13628 cfg.ttf_data_owned_by_atlas = 1;
13629 return nk_font_atlas_add(atlas, &cfg);
13630 }
13631 #endif
13632 NK_API struct nk_font*
13633 nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
13634 void *compressed_data, nk_size compressed_size, float height,
13635 const struct nk_font_config *config)
13636 {
13637 unsigned int decompressed_size;
13638 void *decompressed_data;
13639 struct nk_font_config cfg;
13640
13641 NK_ASSERT(atlas);
13642 NK_ASSERT(atlas->temporary.alloc);
13643 NK_ASSERT(atlas->temporary.free);
13644 NK_ASSERT(atlas->permanent.alloc);
13645 NK_ASSERT(atlas->permanent.free);
13646
13647 NK_ASSERT(compressed_data);
13648 NK_ASSERT(compressed_size);
13649 if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
13650 !atlas->permanent.alloc || !atlas->permanent.free)
13651 return 0;
13652
13653 decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
13654 decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
13655 NK_ASSERT(decompressed_data);
13656 if (!decompressed_data) return 0;
13657 nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
13658 (unsigned int)compressed_size);
13659
13660 cfg = (config) ? *config: nk_font_config(height);
13661 cfg.ttf_blob = decompressed_data;
13662 cfg.ttf_size = decompressed_size;
13663 cfg.size = height;
13664 cfg.ttf_data_owned_by_atlas = 1;
13665 return nk_font_atlas_add(atlas, &cfg);
13666 }
13667 NK_API struct nk_font*
13668 nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
13669 const char *data_base85, float height, const struct nk_font_config *config)
13670 {
13671 int compressed_size;
13672 void *compressed_data;
13673 struct nk_font *font;
13674
13675 NK_ASSERT(atlas);
13676 NK_ASSERT(atlas->temporary.alloc);
13677 NK_ASSERT(atlas->temporary.free);
13678 NK_ASSERT(atlas->permanent.alloc);
13679 NK_ASSERT(atlas->permanent.free);
13680
13681 NK_ASSERT(data_base85);
13682 if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
13683 !atlas->permanent.alloc || !atlas->permanent.free)
13684 return 0;
13685
13686 compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
13687 compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
13688 NK_ASSERT(compressed_data);
13689 if (!compressed_data) return 0;
13690 nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
13691 font = nk_font_atlas_add_compressed(atlas, compressed_data,
13692 (nk_size)compressed_size, height, config);
13693 atlas->temporary.free(atlas->temporary.userdata, compressed_data);
13694 return font;
13695 }
13696
13697 #ifdef NK_INCLUDE_DEFAULT_FONT
13698 NK_API struct nk_font*
13699 nk_font_atlas_add_default(struct nk_font_atlas *atlas,
13700 float pixel_height, const struct nk_font_config *config)
13701 {
13702 NK_ASSERT(atlas);
13703 NK_ASSERT(atlas->temporary.alloc);
13704 NK_ASSERT(atlas->temporary.free);
13705 NK_ASSERT(atlas->permanent.alloc);
13706 NK_ASSERT(atlas->permanent.free);
13707 return nk_font_atlas_add_compressed_base85(atlas,
13708 nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
13709 }
13710 #endif
13711 NK_API const void*
13712 nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
13713 enum nk_font_atlas_format fmt)
13714 {
13715 int i = 0;
13716 void *tmp = 0;
13717 nk_size tmp_size, img_size;
13718 struct nk_font *font_iter;
13719 struct nk_font_baker *baker;
13720
13721 NK_ASSERT(atlas);
13722 NK_ASSERT(atlas->temporary.alloc);
13723 NK_ASSERT(atlas->temporary.free);
13724 NK_ASSERT(atlas->permanent.alloc);
13725 NK_ASSERT(atlas->permanent.free);
13726
13727 NK_ASSERT(width);
13728 NK_ASSERT(height);
13729 if (!atlas || !width || !height ||
13730 !atlas->temporary.alloc || !atlas->temporary.free ||
13731 !atlas->permanent.alloc || !atlas->permanent.free)
13732 return 0;
13733
13734 #ifdef NK_INCLUDE_DEFAULT_FONT
13735 /* no font added so just use default font */
13736 if (!atlas->font_num)
13737 atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
13738 #endif
13739 NK_ASSERT(atlas->font_num);
13740 if (!atlas->font_num) return 0;
13741
13742 /* allocate temporary baker memory required for the baking process */
13743 nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
13744 tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
13745 NK_ASSERT(tmp);
13746 if (!tmp) goto failed;
13747
13748 /* allocate glyph memory for all fonts */
13749 baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
13750 atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
13751 atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
13752 NK_ASSERT(atlas->glyphs);
13753 if (!atlas->glyphs)
13754 goto failed;
13755
13756 /* pack all glyphs into a tight fit space */
13757 atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
13758 atlas->custom.h = NK_CURSOR_DATA_H + 1;
13759 if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
13760 atlas->config, atlas->font_num, &atlas->temporary))
13761 goto failed;
13762
13763 /* allocate memory for the baked image font atlas */
13764 atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
13765 NK_ASSERT(atlas->pixel);
13766 if (!atlas->pixel)
13767 goto failed;
13768
13769 /* bake glyphs and custom white pixel into image */
13770 nk_font_bake(baker, atlas->pixel, *width, *height,
13771 atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
13772 nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
13773 nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
13774
13775 if (fmt == NK_FONT_ATLAS_RGBA32) {
13776 /* convert alpha8 image into rgba32 image */
13777 void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
13778 (nk_size)(*width * *height * 4));
13779 NK_ASSERT(img_rgba);
13780 if (!img_rgba) goto failed;
13781 nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
13782 atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
13783 atlas->pixel = img_rgba;
13784 }
13785 atlas->tex_width = *width;
13786 atlas->tex_height = *height;
13787
13788 /* initialize each font */
13789 for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
13790 struct nk_font *font = font_iter;
13791 struct nk_font_config *config = font->config;
13792 nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
13793 config->font, nk_handle_ptr(0));
13794 }
13795
13796 /* initialize each cursor */
13797 {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
13798 /* Pos Size Offset */
13799 {{ 0, 3}, {12,19}, { 0, 0}},
13800 {{13, 0}, { 7,16}, { 4, 8}},
13801 {{31, 0}, {23,23}, {11,11}},
13802 {{21, 0}, { 9, 23}, { 5,11}},
13803 {{55,18}, {23, 9}, {11, 5}},
13804 {{73, 0}, {17,17}, { 9, 9}},
13805 {{55, 0}, {17,17}, { 9, 9}}
13806 };
13807 for (i = 0; i < NK_CURSOR_COUNT; ++i) {
13808 struct nk_cursor *cursor = &atlas->cursors[i];
13809 cursor->img.w = (unsigned short)*width;
13810 cursor->img.h = (unsigned short)*height;
13811 cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
13812 cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
13813 cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
13814 cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
13815 cursor->size = nk_cursor_data[i][1];
13816 cursor->offset = nk_cursor_data[i][2];
13817 }}
13818 /* free temporary memory */
13819 atlas->temporary.free(atlas->temporary.userdata, tmp);
13820 return atlas->pixel;
13821
13822 failed:
13823 /* error so cleanup all memory */
13824 if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
13825 if (atlas->glyphs) {
13826 atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
13827 atlas->glyphs = 0;
13828 }
13829 if (atlas->pixel) {
13830 atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
13831 atlas->pixel = 0;
13832 }
13833 return 0;
13834 }
13835 NK_API void
13836 nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
13837 struct nk_draw_null_texture *null)
13838 {
13839 int i = 0;
13840 struct nk_font *font_iter;
13841 NK_ASSERT(atlas);
13842 if (!atlas) {
13843 if (!null) return;
13844 null->texture = texture;
13845 null->uv = nk_vec2(0.5f,0.5f);
13846 }
13847 if (null) {
13848 null->texture = texture;
13849 null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
13850 null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
13851 }
13852 for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
13853 font_iter->texture = texture;
13854 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
13855 font_iter->handle.texture = texture;
13856 #endif
13857 }
13858 for (i = 0; i < NK_CURSOR_COUNT; ++i)
13859 atlas->cursors[i].img.handle = texture;
13860
13861 atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
13862 atlas->pixel = 0;
13863 atlas->tex_width = 0;
13864 atlas->tex_height = 0;
13865 atlas->custom.x = 0;
13866 atlas->custom.y = 0;
13867 atlas->custom.w = 0;
13868 atlas->custom.h = 0;
13869 }
13870 NK_API void
13871 nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
13872 {
13873 NK_ASSERT(atlas);
13874 NK_ASSERT(atlas->temporary.alloc);
13875 NK_ASSERT(atlas->temporary.free);
13876 NK_ASSERT(atlas->permanent.alloc);
13877 NK_ASSERT(atlas->permanent.free);
13878 if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
13879 if (atlas->config) {
13880 struct nk_font_config *iter;
13881 for (iter = atlas->config; iter; iter = iter->next) {
13882 struct nk_font_config *i;
13883 for (i = iter->n; i != iter; i = i->n) {
13884 atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
13885 i->ttf_blob = 0;
13886 }
13887 atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
13888 iter->ttf_blob = 0;
13889 }
13890 }
13891 }
13892 NK_API void
13893 nk_font_atlas_clear(struct nk_font_atlas *atlas)
13894 {
13895 NK_ASSERT(atlas);
13896 NK_ASSERT(atlas->temporary.alloc);
13897 NK_ASSERT(atlas->temporary.free);
13898 NK_ASSERT(atlas->permanent.alloc);
13899 NK_ASSERT(atlas->permanent.free);
13900 if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
13901
13902 if (atlas->config) {
13903 struct nk_font_config *iter, *next;
13904 for (iter = atlas->config; iter; iter = next) {
13905 struct nk_font_config *i, *n;
13906 for (i = iter->n; i != iter; i = n) {
13907 n = i->n;
13908 if (i->ttf_blob)
13909 atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
13910 atlas->permanent.free(atlas->permanent.userdata, i);
13911 }
13912 next = iter->next;
13913 if (i->ttf_blob)
13914 atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
13915 atlas->permanent.free(atlas->permanent.userdata, iter);
13916 }
13917 atlas->config = 0;
13918 }
13919 if (atlas->fonts) {
13920 struct nk_font *iter, *next;
13921 for (iter = atlas->fonts; iter; iter = next) {
13922 next = iter->next;
13923 atlas->permanent.free(atlas->permanent.userdata, iter);
13924 }
13925 atlas->fonts = 0;
13926 }
13927 if (atlas->glyphs)
13928 atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
13929 nk_zero_struct(*atlas);
13930 }
13931 #endif
13932
13933
13934
13935
13936
13937 /* ===============================================================
13938 *
13939 * INPUT
13940 *
13941 * ===============================================================*/
13942 NK_API void
13943 nk_input_begin(struct nk_context *ctx)
13944 {
13945 int i;
13946 struct nk_input *in;
13947 NK_ASSERT(ctx);
13948 if (!ctx) return;
13949 in = &ctx->input;
13950 for (i = 0; i < NK_BUTTON_MAX; ++i)
13951 in->mouse.buttons[i].clicked = 0;
13952
13953 in->keyboard.text_len = 0;
13954 in->mouse.scroll_delta = nk_vec2(0,0);
13955 in->mouse.prev.x = in->mouse.pos.x;
13956 in->mouse.prev.y = in->mouse.pos.y;
13957 in->mouse.delta.x = 0;
13958 in->mouse.delta.y = 0;
13959 for (i = 0; i < NK_KEY_MAX; i++)
13960 in->keyboard.keys[i].clicked = 0;
13961 }
13962 NK_API void
13963 nk_input_end(struct nk_context *ctx)
13964 {
13965 struct nk_input *in;
13966 NK_ASSERT(ctx);
13967 if (!ctx) return;
13968 in = &ctx->input;
13969 if (in->mouse.grab)
13970 in->mouse.grab = 0;
13971 if (in->mouse.ungrab) {
13972 in->mouse.grabbed = 0;
13973 in->mouse.ungrab = 0;
13974 in->mouse.grab = 0;
13975 }
13976 }
13977 NK_API void
13978 nk_input_motion(struct nk_context *ctx, int x, int y)
13979 {
13980 struct nk_input *in;
13981 NK_ASSERT(ctx);
13982 if (!ctx) return;
13983 in = &ctx->input;
13984 in->mouse.pos.x = (float)x;
13985 in->mouse.pos.y = (float)y;
13986 in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
13987 in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
13988 }
13989 NK_API void
13990 nk_input_key(struct nk_context *ctx, enum nk_keys key, int down)
13991 {
13992 struct nk_input *in;
13993 NK_ASSERT(ctx);
13994 if (!ctx) return;
13995 in = &ctx->input;
13996 #ifdef NK_KEYSTATE_BASED_INPUT
13997 if (in->keyboard.keys[key].down != down)
13998 in->keyboard.keys[key].clicked++;
13999 #else
14000 in->keyboard.keys[key].clicked++;
14001 #endif
14002 in->keyboard.keys[key].down = down;
14003 }
14004 NK_API void
14005 nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int down)
14006 {
14007 struct nk_mouse_button *btn;
14008 struct nk_input *in;
14009 NK_ASSERT(ctx);
14010 if (!ctx) return;
14011 in = &ctx->input;
14012 if (in->mouse.buttons[id].down == down) return;
14013
14014 btn = &in->mouse.buttons[id];
14015 btn->clicked_pos.x = (float)x;
14016 btn->clicked_pos.y = (float)y;
14017 btn->down = down;
14018 btn->clicked++;
14019 }
14020 NK_API void
14021 nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
14022 {
14023 NK_ASSERT(ctx);
14024 if (!ctx) return;
14025 ctx->input.mouse.scroll_delta.x += val.x;
14026 ctx->input.mouse.scroll_delta.y += val.y;
14027 }
14028 NK_API void
14029 nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
14030 {
14031 int len = 0;
14032 nk_rune unicode;
14033 struct nk_input *in;
14034
14035 NK_ASSERT(ctx);
14036 if (!ctx) return;
14037 in = &ctx->input;
14038
14039 len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
14040 if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
14041 nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
14042 NK_INPUT_MAX - in->keyboard.text_len);
14043 in->keyboard.text_len += len;
14044 }
14045 }
14046 NK_API void
14047 nk_input_char(struct nk_context *ctx, char c)
14048 {
14049 nk_glyph glyph;
14050 NK_ASSERT(ctx);
14051 if (!ctx) return;
14052 glyph[0] = c;
14053 nk_input_glyph(ctx, glyph);
14054 }
14055 NK_API void
14056 nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
14057 {
14058 nk_glyph rune;
14059 NK_ASSERT(ctx);
14060 if (!ctx) return;
14061 nk_utf_encode(unicode, rune, NK_UTF_SIZE);
14062 nk_input_glyph(ctx, rune);
14063 }
14064 NK_API int
14065 nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
14066 {
14067 const struct nk_mouse_button *btn;
14068 if (!i) return nk_false;
14069 btn = &i->mouse.buttons[id];
14070 return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
14071 }
14072 NK_API int
14073 nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
14074 struct nk_rect b)
14075 {
14076 const struct nk_mouse_button *btn;
14077 if (!i) return nk_false;
14078 btn = &i->mouse.buttons[id];
14079 if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
14080 return nk_false;
14081 return nk_true;
14082 }
14083 NK_API int
14084 nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
14085 struct nk_rect b, int down)
14086 {
14087 const struct nk_mouse_button *btn;
14088 if (!i) return nk_false;
14089 btn = &i->mouse.buttons[id];
14090 return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
14091 }
14092 NK_API int
14093 nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
14094 struct nk_rect b)
14095 {
14096 const struct nk_mouse_button *btn;
14097 if (!i) return nk_false;
14098 btn = &i->mouse.buttons[id];
14099 return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
14100 btn->clicked) ? nk_true : nk_false;
14101 }
14102 NK_API int
14103 nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
14104 struct nk_rect b, int down)
14105 {
14106 const struct nk_mouse_button *btn;
14107 if (!i) return nk_false;
14108 btn = &i->mouse.buttons[id];
14109 return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
14110 btn->clicked) ? nk_true : nk_false;
14111 }
14112 NK_API int
14113 nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
14114 {
14115 int i, down = 0;
14116 for (i = 0; i < NK_BUTTON_MAX; ++i)
14117 down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
14118 return down;
14119 }
14120 NK_API int
14121 nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
14122 {
14123 if (!i) return nk_false;
14124 return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
14125 }
14126 NK_API int
14127 nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
14128 {
14129 if (!i) return nk_false;
14130 return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
14131 }
14132 NK_API int
14133 nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
14134 {
14135 if (!i) return nk_false;
14136 if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
14137 return nk_input_is_mouse_click_in_rect(i, id, rect);
14138 }
14139 NK_API int
14140 nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
14141 {
14142 if (!i) return nk_false;
14143 return i->mouse.buttons[id].down;
14144 }
14145 NK_API int
14146 nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
14147 {
14148 const struct nk_mouse_button *b;
14149 if (!i) return nk_false;
14150 b = &i->mouse.buttons[id];
14151 if (b->down && b->clicked)
14152 return nk_true;
14153 return nk_false;
14154 }
14155 NK_API int
14156 nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
14157 {
14158 if (!i) return nk_false;
14159 return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
14160 }
14161 NK_API int
14162 nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
14163 {
14164 const struct nk_key *k;
14165 if (!i) return nk_false;
14166 k = &i->keyboard.keys[key];
14167 if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
14168 return nk_true;
14169 return nk_false;
14170 }
14171 NK_API int
14172 nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
14173 {
14174 const struct nk_key *k;
14175 if (!i) return nk_false;
14176 k = &i->keyboard.keys[key];
14177 if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
14178 return nk_true;
14179 return nk_false;
14180 }
14181 NK_API int
14182 nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
14183 {
14184 const struct nk_key *k;
14185 if (!i) return nk_false;
14186 k = &i->keyboard.keys[key];
14187 if (k->down) return nk_true;
14188 return nk_false;
14189 }
14190
14191
14192
14193
14194
14195 /* ===============================================================
14196 *
14197 * STYLE
14198 *
14199 * ===============================================================*/
14200 NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
14201 #define NK_COLOR_MAP(NK_COLOR)\
14202 NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
14203 NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
14204 NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
14205 NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
14206 NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
14207 NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
14208 NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
14209 NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
14210 NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
14211 NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
14212 NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
14213 NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
14214 NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
14215 NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
14216 NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
14217 NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
14218 NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
14219 NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
14220 NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
14221 NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
14222 NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
14223 NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
14224 NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT, 255, 0, 0, 255) \
14225 NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
14226 NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
14227 NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER, 120,120,120,255) \
14228 NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE, 150,150,150,255) \
14229 NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
14230
14231 NK_GLOBAL const struct nk_color
14232 nk_default_color_style[NK_COLOR_COUNT] = {
14233 #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
14234 NK_COLOR_MAP(NK_COLOR)
14235 #undef NK_COLOR
14236 };
14237 NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
14238 #define NK_COLOR(a,b,c,d,e) #a,
14239 NK_COLOR_MAP(NK_COLOR)
14240 #undef NK_COLOR
14241 };
14242
14243 NK_API const char*
14244 nk_style_get_color_by_name(enum nk_style_colors c)
14245 {
14246 return nk_color_names[c];
14247 }
14248 NK_API struct nk_style_item
14249 nk_style_item_image(struct nk_image img)
14250 {
14251 struct nk_style_item i;
14252 i.type = NK_STYLE_ITEM_IMAGE;
14253 i.data.image = img;
14254 return i;
14255 }
14256 NK_API struct nk_style_item
14257 nk_style_item_color(struct nk_color col)
14258 {
14259 struct nk_style_item i;
14260 i.type = NK_STYLE_ITEM_COLOR;
14261 i.data.color = col;
14262 return i;
14263 }
14264 NK_API struct nk_style_item
14265 nk_style_item_hide(void)
14266 {
14267 struct nk_style_item i;
14268 i.type = NK_STYLE_ITEM_COLOR;
14269 i.data.color = nk_rgba(0,0,0,0);
14270 return i;
14271 }
14272 NK_API void
14273 nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
14274 {
14275 struct nk_style *style;
14276 struct nk_style_text *text;
14277 struct nk_style_button *button;
14278 struct nk_style_toggle *toggle;
14279 struct nk_style_selectable *select;
14280 struct nk_style_slider *slider;
14281 struct nk_style_progress *prog;
14282 struct nk_style_scrollbar *scroll;
14283 struct nk_style_edit *edit;
14284 struct nk_style_property *property;
14285 struct nk_style_combo *combo;
14286 struct nk_style_chart *chart;
14287 struct nk_style_tab *tab;
14288 struct nk_style_window *win;
14289
14290 NK_ASSERT(ctx);
14291 if (!ctx) return;
14292 style = &ctx->style;
14293 table = (!table) ? nk_default_color_style: table;
14294
14295 /* default text */
14296 text = &style->text;
14297 text->color = table[NK_COLOR_TEXT];
14298 text->padding = nk_vec2(0,0);
14299
14300 /* default button */
14301 button = &style->button;
14302 nk_zero_struct(*button);
14303 button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
14304 button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
14305 button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
14306 button->border_color = table[NK_COLOR_BORDER];
14307 button->text_background = table[NK_COLOR_BUTTON];
14308 button->text_normal = table[NK_COLOR_TEXT];
14309 button->text_hover = table[NK_COLOR_TEXT];
14310 button->text_active = table[NK_COLOR_TEXT];
14311 button->padding = nk_vec2(2.0f,2.0f);
14312 button->image_padding = nk_vec2(0.0f,0.0f);
14313 button->touch_padding = nk_vec2(0.0f, 0.0f);
14314 button->userdata = nk_handle_ptr(0);
14315 button->text_alignment = NK_TEXT_CENTERED;
14316 button->border = 1.0f;
14317 button->rounding = 4.0f;
14318 button->draw_begin = 0;
14319 button->draw_end = 0;
14320
14321 /* contextual button */
14322 button = &style->contextual_button;
14323 nk_zero_struct(*button);
14324 button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
14325 button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
14326 button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
14327 button->border_color = table[NK_COLOR_WINDOW];
14328 button->text_background = table[NK_COLOR_WINDOW];
14329 button->text_normal = table[NK_COLOR_TEXT];
14330 button->text_hover = table[NK_COLOR_TEXT];
14331 button->text_active = table[NK_COLOR_TEXT];
14332 button->padding = nk_vec2(2.0f,2.0f);
14333 button->touch_padding = nk_vec2(0.0f,0.0f);
14334 button->userdata = nk_handle_ptr(0);
14335 button->text_alignment = NK_TEXT_CENTERED;
14336 button->border = 0.0f;
14337 button->rounding = 0.0f;
14338 button->draw_begin = 0;
14339 button->draw_end = 0;
14340
14341 /* menu button */
14342 button = &style->menu_button;
14343 nk_zero_struct(*button);
14344 button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
14345 button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
14346 button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
14347 button->border_color = table[NK_COLOR_WINDOW];
14348 button->text_background = table[NK_COLOR_WINDOW];
14349 button->text_normal = table[NK_COLOR_TEXT];
14350 button->text_hover = table[NK_COLOR_TEXT];
14351 button->text_active = table[NK_COLOR_TEXT];
14352 button->padding = nk_vec2(2.0f,2.0f);
14353 button->touch_padding = nk_vec2(0.0f,0.0f);
14354 button->userdata = nk_handle_ptr(0);
14355 button->text_alignment = NK_TEXT_CENTERED;
14356 button->border = 0.0f;
14357 button->rounding = 1.0f;
14358 button->draw_begin = 0;
14359 button->draw_end = 0;
14360
14361 /* checkbox toggle */
14362 toggle = &style->checkbox;
14363 nk_zero_struct(*toggle);
14364 toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
14365 toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
14366 toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
14367 toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
14368 toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
14369 toggle->userdata = nk_handle_ptr(0);
14370 toggle->text_background = table[NK_COLOR_WINDOW];
14371 toggle->text_normal = table[NK_COLOR_TEXT];
14372 toggle->text_hover = table[NK_COLOR_TEXT];
14373 toggle->text_active = table[NK_COLOR_TEXT];
14374 toggle->padding = nk_vec2(2.0f, 2.0f);
14375 toggle->touch_padding = nk_vec2(0,0);
14376 toggle->border_color = nk_rgba(0,0,0,0);
14377 toggle->border = 0.0f;
14378 toggle->spacing = 4;
14379
14380 /* option toggle */
14381 toggle = &style->option;
14382 nk_zero_struct(*toggle);
14383 toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
14384 toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
14385 toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
14386 toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
14387 toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
14388 toggle->userdata = nk_handle_ptr(0);
14389 toggle->text_background = table[NK_COLOR_WINDOW];
14390 toggle->text_normal = table[NK_COLOR_TEXT];
14391 toggle->text_hover = table[NK_COLOR_TEXT];
14392 toggle->text_active = table[NK_COLOR_TEXT];
14393 toggle->padding = nk_vec2(3.0f, 3.0f);
14394 toggle->touch_padding = nk_vec2(0,0);
14395 toggle->border_color = nk_rgba(0,0,0,0);
14396 toggle->border = 0.0f;
14397 toggle->spacing = 4;
14398
14399 /* selectable */
14400 select = &style->selectable;
14401 nk_zero_struct(*select);
14402 select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
14403 select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
14404 select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
14405 select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
14406 select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
14407 select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
14408 select->text_normal = table[NK_COLOR_TEXT];
14409 select->text_hover = table[NK_COLOR_TEXT];
14410 select->text_pressed = table[NK_COLOR_TEXT];
14411 select->text_normal_active = table[NK_COLOR_TEXT];
14412 select->text_hover_active = table[NK_COLOR_TEXT];
14413 select->text_pressed_active = table[NK_COLOR_TEXT];
14414 select->padding = nk_vec2(2.0f,2.0f);
14415 select->image_padding = nk_vec2(2.0f,2.0f);
14416 select->touch_padding = nk_vec2(0,0);
14417 select->userdata = nk_handle_ptr(0);
14418 select->rounding = 0.0f;
14419 select->draw_begin = 0;
14420 select->draw_end = 0;
14421
14422 /* slider */
14423 slider = &style->slider;
14424 nk_zero_struct(*slider);
14425 slider->normal = nk_style_item_hide();
14426 slider->hover = nk_style_item_hide();
14427 slider->active = nk_style_item_hide();
14428 slider->bar_normal = table[NK_COLOR_SLIDER];
14429 slider->bar_hover = table[NK_COLOR_SLIDER];
14430 slider->bar_active = table[NK_COLOR_SLIDER];
14431 slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
14432 slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
14433 slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
14434 slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
14435 slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
14436 slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
14437 slider->cursor_size = nk_vec2(16,16);
14438 slider->padding = nk_vec2(2,2);
14439 slider->spacing = nk_vec2(2,2);
14440 slider->userdata = nk_handle_ptr(0);
14441 slider->show_buttons = nk_false;
14442 slider->bar_height = 8;
14443 slider->rounding = 0;
14444 slider->draw_begin = 0;
14445 slider->draw_end = 0;
14446
14447 /* slider buttons */
14448 button = &style->slider.inc_button;
14449 button->normal = nk_style_item_color(nk_rgb(40,40,40));
14450 button->hover = nk_style_item_color(nk_rgb(42,42,42));
14451 button->active = nk_style_item_color(nk_rgb(44,44,44));
14452 button->border_color = nk_rgb(65,65,65);
14453 button->text_background = nk_rgb(40,40,40);
14454 button->text_normal = nk_rgb(175,175,175);
14455 button->text_hover = nk_rgb(175,175,175);
14456 button->text_active = nk_rgb(175,175,175);
14457 button->padding = nk_vec2(8.0f,8.0f);
14458 button->touch_padding = nk_vec2(0.0f,0.0f);
14459 button->userdata = nk_handle_ptr(0);
14460 button->text_alignment = NK_TEXT_CENTERED;
14461 button->border = 1.0f;
14462 button->rounding = 0.0f;
14463 button->draw_begin = 0;
14464 button->draw_end = 0;
14465 style->slider.dec_button = style->slider.inc_button;
14466
14467 /* progressbar */
14468 prog = &style->progress;
14469 nk_zero_struct(*prog);
14470 prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
14471 prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
14472 prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
14473 prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
14474 prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
14475 prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
14476 prog->border_color = nk_rgba(0,0,0,0);
14477 prog->cursor_border_color = nk_rgba(0,0,0,0);
14478 prog->userdata = nk_handle_ptr(0);
14479 prog->padding = nk_vec2(4,4);
14480 prog->rounding = 0;
14481 prog->border = 0;
14482 prog->cursor_rounding = 0;
14483 prog->cursor_border = 0;
14484 prog->draw_begin = 0;
14485 prog->draw_end = 0;
14486
14487 /* scrollbars */
14488 scroll = &style->scrollh;
14489 nk_zero_struct(*scroll);
14490 scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
14491 scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
14492 scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
14493 scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
14494 scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
14495 scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
14496 scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
14497 scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
14498 scroll->userdata = nk_handle_ptr(0);
14499 scroll->border_color = table[NK_COLOR_SCROLLBAR];
14500 scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
14501 scroll->padding = nk_vec2(0,0);
14502 scroll->show_buttons = nk_false;
14503 scroll->border = 0;
14504 scroll->rounding = 0;
14505 scroll->border_cursor = 0;
14506 scroll->rounding_cursor = 0;
14507 scroll->draw_begin = 0;
14508 scroll->draw_end = 0;
14509 style->scrollv = style->scrollh;
14510
14511 /* scrollbars buttons */
14512 button = &style->scrollh.inc_button;
14513 button->normal = nk_style_item_color(nk_rgb(40,40,40));
14514 button->hover = nk_style_item_color(nk_rgb(42,42,42));
14515 button->active = nk_style_item_color(nk_rgb(44,44,44));
14516 button->border_color = nk_rgb(65,65,65);
14517 button->text_background = nk_rgb(40,40,40);
14518 button->text_normal = nk_rgb(175,175,175);
14519 button->text_hover = nk_rgb(175,175,175);
14520 button->text_active = nk_rgb(175,175,175);
14521 button->padding = nk_vec2(4.0f,4.0f);
14522 button->touch_padding = nk_vec2(0.0f,0.0f);
14523 button->userdata = nk_handle_ptr(0);
14524 button->text_alignment = NK_TEXT_CENTERED;
14525 button->border = 1.0f;
14526 button->rounding = 0.0f;
14527 button->draw_begin = 0;
14528 button->draw_end = 0;
14529 style->scrollh.dec_button = style->scrollh.inc_button;
14530 style->scrollv.inc_button = style->scrollh.inc_button;
14531 style->scrollv.dec_button = style->scrollh.inc_button;
14532
14533 /* edit */
14534 edit = &style->edit;
14535 nk_zero_struct(*edit);
14536 edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
14537 edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
14538 edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
14539 edit->cursor_normal = table[NK_COLOR_TEXT];
14540 edit->cursor_hover = table[NK_COLOR_TEXT];
14541 edit->cursor_text_normal= table[NK_COLOR_EDIT];
14542 edit->cursor_text_hover = table[NK_COLOR_EDIT];
14543 edit->border_color = table[NK_COLOR_BORDER];
14544 edit->text_normal = table[NK_COLOR_TEXT];
14545 edit->text_hover = table[NK_COLOR_TEXT];
14546 edit->text_active = table[NK_COLOR_TEXT];
14547 edit->selected_normal = table[NK_COLOR_TEXT];
14548 edit->selected_hover = table[NK_COLOR_TEXT];
14549 edit->selected_text_normal = table[NK_COLOR_EDIT];
14550 edit->selected_text_hover = table[NK_COLOR_EDIT];
14551 edit->scrollbar_size = nk_vec2(10,10);
14552 edit->scrollbar = style->scrollv;
14553 edit->padding = nk_vec2(4,4);
14554 edit->row_padding = 2;
14555 edit->cursor_size = 4;
14556 edit->border = 1;
14557 edit->rounding = 0;
14558
14559 /* property */
14560 property = &style->property;
14561 nk_zero_struct(*property);
14562 property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14563 property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14564 property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14565 property->border_color = table[NK_COLOR_BORDER];
14566 property->label_normal = table[NK_COLOR_TEXT];
14567 property->label_hover = table[NK_COLOR_TEXT];
14568 property->label_active = table[NK_COLOR_TEXT];
14569 property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
14570 property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
14571 property->userdata = nk_handle_ptr(0);
14572 property->padding = nk_vec2(4,4);
14573 property->border = 1;
14574 property->rounding = 10;
14575 property->draw_begin = 0;
14576 property->draw_end = 0;
14577
14578 /* property buttons */
14579 button = &style->property.dec_button;
14580 nk_zero_struct(*button);
14581 button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14582 button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14583 button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14584 button->border_color = nk_rgba(0,0,0,0);
14585 button->text_background = table[NK_COLOR_PROPERTY];
14586 button->text_normal = table[NK_COLOR_TEXT];
14587 button->text_hover = table[NK_COLOR_TEXT];
14588 button->text_active = table[NK_COLOR_TEXT];
14589 button->padding = nk_vec2(0.0f,0.0f);
14590 button->touch_padding = nk_vec2(0.0f,0.0f);
14591 button->userdata = nk_handle_ptr(0);
14592 button->text_alignment = NK_TEXT_CENTERED;
14593 button->border = 0.0f;
14594 button->rounding = 0.0f;
14595 button->draw_begin = 0;
14596 button->draw_end = 0;
14597 style->property.inc_button = style->property.dec_button;
14598
14599 /* property edit */
14600 edit = &style->property.edit;
14601 nk_zero_struct(*edit);
14602 edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14603 edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14604 edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
14605 edit->border_color = nk_rgba(0,0,0,0);
14606 edit->cursor_normal = table[NK_COLOR_TEXT];
14607 edit->cursor_hover = table[NK_COLOR_TEXT];
14608 edit->cursor_text_normal= table[NK_COLOR_EDIT];
14609 edit->cursor_text_hover = table[NK_COLOR_EDIT];
14610 edit->text_normal = table[NK_COLOR_TEXT];
14611 edit->text_hover = table[NK_COLOR_TEXT];
14612 edit->text_active = table[NK_COLOR_TEXT];
14613 edit->selected_normal = table[NK_COLOR_TEXT];
14614 edit->selected_hover = table[NK_COLOR_TEXT];
14615 edit->selected_text_normal = table[NK_COLOR_EDIT];
14616 edit->selected_text_hover = table[NK_COLOR_EDIT];
14617 edit->padding = nk_vec2(0,0);
14618 edit->cursor_size = 8;
14619 edit->border = 0;
14620 edit->rounding = 0;
14621
14622 /* chart */
14623 chart = &style->chart;
14624 nk_zero_struct(*chart);
14625 chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
14626 chart->border_color = table[NK_COLOR_BORDER];
14627 chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
14628 chart->color = table[NK_COLOR_CHART_COLOR];
14629 chart->padding = nk_vec2(4,4);
14630 chart->border = 0;
14631 chart->rounding = 0;
14632
14633 /* combo */
14634 combo = &style->combo;
14635 combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
14636 combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
14637 combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
14638 combo->border_color = table[NK_COLOR_BORDER];
14639 combo->label_normal = table[NK_COLOR_TEXT];
14640 combo->label_hover = table[NK_COLOR_TEXT];
14641 combo->label_active = table[NK_COLOR_TEXT];
14642 combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
14643 combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
14644 combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
14645 combo->content_padding = nk_vec2(4,4);
14646 combo->button_padding = nk_vec2(0,4);
14647 combo->spacing = nk_vec2(4,0);
14648 combo->border = 1;
14649 combo->rounding = 0;
14650
14651 /* combo button */
14652 button = &style->combo.button;
14653 nk_zero_struct(*button);
14654 button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
14655 button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
14656 button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
14657 button->border_color = nk_rgba(0,0,0,0);
14658 button->text_background = table[NK_COLOR_COMBO];
14659 button->text_normal = table[NK_COLOR_TEXT];
14660 button->text_hover = table[NK_COLOR_TEXT];
14661 button->text_active = table[NK_COLOR_TEXT];
14662 button->padding = nk_vec2(2.0f,2.0f);
14663 button->touch_padding = nk_vec2(0.0f,0.0f);
14664 button->userdata = nk_handle_ptr(0);
14665 button->text_alignment = NK_TEXT_CENTERED;
14666 button->border = 0.0f;
14667 button->rounding = 0.0f;
14668 button->draw_begin = 0;
14669 button->draw_end = 0;
14670
14671 /* tab */
14672 tab = &style->tab;
14673 tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
14674 tab->border_color = table[NK_COLOR_BORDER];
14675 tab->text = table[NK_COLOR_TEXT];
14676 tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
14677 tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
14678 tab->padding = nk_vec2(4,4);
14679 tab->spacing = nk_vec2(4,4);
14680 tab->indent = 10.0f;
14681 tab->border = 1;
14682 tab->rounding = 0;
14683
14684 /* tab button */
14685 button = &style->tab.tab_minimize_button;
14686 nk_zero_struct(*button);
14687 button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
14688 button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
14689 button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
14690 button->border_color = nk_rgba(0,0,0,0);
14691 button->text_background = table[NK_COLOR_TAB_HEADER];
14692 button->text_normal = table[NK_COLOR_TEXT];
14693 button->text_hover = table[NK_COLOR_TEXT];
14694 button->text_active = table[NK_COLOR_TEXT];
14695 button->padding = nk_vec2(2.0f,2.0f);
14696 button->touch_padding = nk_vec2(0.0f,0.0f);
14697 button->userdata = nk_handle_ptr(0);
14698 button->text_alignment = NK_TEXT_CENTERED;
14699 button->border = 0.0f;
14700 button->rounding = 0.0f;
14701 button->draw_begin = 0;
14702 button->draw_end = 0;
14703 style->tab.tab_maximize_button =*button;
14704
14705 /* node button */
14706 button = &style->tab.node_minimize_button;
14707 nk_zero_struct(*button);
14708 button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
14709 button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
14710 button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
14711 button->border_color = nk_rgba(0,0,0,0);
14712 button->text_background = table[NK_COLOR_TAB_HEADER];
14713 button->text_normal = table[NK_COLOR_TEXT];
14714 button->text_hover = table[NK_COLOR_TEXT];
14715 button->text_active = table[NK_COLOR_TEXT];
14716 button->padding = nk_vec2(2.0f,2.0f);
14717 button->touch_padding = nk_vec2(0.0f,0.0f);
14718 button->userdata = nk_handle_ptr(0);
14719 button->text_alignment = NK_TEXT_CENTERED;
14720 button->border = 0.0f;
14721 button->rounding = 0.0f;
14722 button->draw_begin = 0;
14723 button->draw_end = 0;
14724 style->tab.node_maximize_button =*button;
14725
14726 /* window header */
14727 win = &style->window;
14728 win->header.align = NK_HEADER_RIGHT;
14729 win->header.close_symbol = NK_SYMBOL_X;
14730 win->header.minimize_symbol = NK_SYMBOL_MINUS;
14731 win->header.maximize_symbol = NK_SYMBOL_PLUS;
14732 win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
14733 win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
14734 win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
14735 win->header.label_normal = table[NK_COLOR_TEXT];
14736 win->header.label_hover = table[NK_COLOR_TEXT];
14737 win->header.label_active = table[NK_COLOR_TEXT];
14738 win->header.label_padding = nk_vec2(4,4);
14739 win->header.padding = nk_vec2(4,4);
14740 win->header.spacing = nk_vec2(0,0);
14741
14742 /* window header close button */
14743 button = &style->window.header.close_button;
14744 nk_zero_struct(*button);
14745 button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
14746 button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
14747 button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
14748 button->border_color = nk_rgba(0,0,0,0);
14749 button->text_background = table[NK_COLOR_HEADER];
14750 button->text_normal = table[NK_COLOR_TEXT];
14751 button->text_hover = table[NK_COLOR_TEXT];
14752 button->text_active = table[NK_COLOR_TEXT];
14753 button->padding = nk_vec2(0.0f,0.0f);
14754 button->touch_padding = nk_vec2(0.0f,0.0f);
14755 button->userdata = nk_handle_ptr(0);
14756 button->text_alignment = NK_TEXT_CENTERED;
14757 button->border = 0.0f;
14758 button->rounding = 0.0f;
14759 button->draw_begin = 0;
14760 button->draw_end = 0;
14761
14762 /* window header minimize button */
14763 button = &style->window.header.minimize_button;
14764 nk_zero_struct(*button);
14765 button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
14766 button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
14767 button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
14768 button->border_color = nk_rgba(0,0,0,0);
14769 button->text_background = table[NK_COLOR_HEADER];
14770 button->text_normal = table[NK_COLOR_TEXT];
14771 button->text_hover = table[NK_COLOR_TEXT];
14772 button->text_active = table[NK_COLOR_TEXT];
14773 button->padding = nk_vec2(0.0f,0.0f);
14774 button->touch_padding = nk_vec2(0.0f,0.0f);
14775 button->userdata = nk_handle_ptr(0);
14776 button->text_alignment = NK_TEXT_CENTERED;
14777 button->border = 0.0f;
14778 button->rounding = 0.0f;
14779 button->draw_begin = 0;
14780 button->draw_end = 0;
14781
14782 /* window */
14783 win->background = table[NK_COLOR_WINDOW];
14784 win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
14785 win->border_color = table[NK_COLOR_BORDER];
14786 win->popup_border_color = table[NK_COLOR_BORDER];
14787 win->combo_border_color = table[NK_COLOR_BORDER];
14788 win->contextual_border_color = table[NK_COLOR_BORDER];
14789 win->menu_border_color = table[NK_COLOR_BORDER];
14790 win->group_border_color = table[NK_COLOR_BORDER];
14791 win->tooltip_border_color = table[NK_COLOR_BORDER];
14792 win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
14793
14794 win->rounding = 0.0f;
14795 win->spacing = nk_vec2(4,4);
14796 win->scrollbar_size = nk_vec2(10,10);
14797 win->min_size = nk_vec2(64,64);
14798
14799 win->combo_border = 1.0f;
14800 win->contextual_border = 1.0f;
14801 win->menu_border = 1.0f;
14802 win->group_border = 1.0f;
14803 win->tooltip_border = 1.0f;
14804 win->popup_border = 1.0f;
14805 win->border = 2.0f;
14806 win->min_row_height_padding = 8;
14807
14808 win->padding = nk_vec2(4,4);
14809 win->group_padding = nk_vec2(4,4);
14810 win->popup_padding = nk_vec2(4,4);
14811 win->combo_padding = nk_vec2(4,4);
14812 win->contextual_padding = nk_vec2(4,4);
14813 win->menu_padding = nk_vec2(4,4);
14814 win->tooltip_padding = nk_vec2(4,4);
14815 }
14816 NK_API void
14817 nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
14818 {
14819 struct nk_style *style;
14820 NK_ASSERT(ctx);
14821
14822 if (!ctx) return;
14823 style = &ctx->style;
14824 style->font = font;
14825 ctx->stacks.fonts.head = 0;
14826 if (ctx->current)
14827 nk_layout_reset_min_row_height(ctx);
14828 }
14829 NK_API int
14830 nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
14831 {
14832 struct nk_config_stack_user_font *font_stack;
14833 struct nk_config_stack_user_font_element *element;
14834
14835 NK_ASSERT(ctx);
14836 if (!ctx) return 0;
14837
14838 font_stack = &ctx->stacks.fonts;
14839 NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
14840 if (font_stack->head >= (int)NK_LEN(font_stack->elements))
14841 return 0;
14842
14843 element = &font_stack->elements[font_stack->head++];
14844 element->address = &ctx->style.font;
14845 element->old_value = ctx->style.font;
14846 ctx->style.font = font;
14847 return 1;
14848 }
14849 NK_API int
14850 nk_style_pop_font(struct nk_context *ctx)
14851 {
14852 struct nk_config_stack_user_font *font_stack;
14853 struct nk_config_stack_user_font_element *element;
14854
14855 NK_ASSERT(ctx);
14856 if (!ctx) return 0;
14857
14858 font_stack = &ctx->stacks.fonts;
14859 NK_ASSERT(font_stack->head > 0);
14860 if (font_stack->head < 1)
14861 return 0;
14862
14863 element = &font_stack->elements[--font_stack->head];
14864 *element->address = element->old_value;
14865 return 1;
14866 }
14867 #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
14868 nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
14869 {\
14870 struct nk_config_stack_##type * type_stack;\
14871 struct nk_config_stack_##type##_element *element;\
14872 NK_ASSERT(ctx);\
14873 if (!ctx) return 0;\
14874 type_stack = &ctx->stacks.stack;\
14875 NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
14876 if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
14877 return 0;\
14878 element = &type_stack->elements[type_stack->head++];\
14879 element->address = address;\
14880 element->old_value = *address;\
14881 *address = value;\
14882 return 1;\
14883 }
14884 #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
14885 nk_style_pop_##type(struct nk_context *ctx)\
14886 {\
14887 struct nk_config_stack_##type *type_stack;\
14888 struct nk_config_stack_##type##_element *element;\
14889 NK_ASSERT(ctx);\
14890 if (!ctx) return 0;\
14891 type_stack = &ctx->stacks.stack;\
14892 NK_ASSERT(type_stack->head > 0);\
14893 if (type_stack->head < 1)\
14894 return 0;\
14895 element = &type_stack->elements[--type_stack->head];\
14896 *element->address = element->old_value;\
14897 return 1;\
14898 }
14899 NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
14900 NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
14901 NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
14902 NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
14903 NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
14904
14905 NK_API int NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
14906 NK_API int NK_STYLE_POP_IMPLEMENATION(float,floats)
14907 NK_API int NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
14908 NK_API int NK_STYLE_POP_IMPLEMENATION(flags,flags)
14909 NK_API int NK_STYLE_POP_IMPLEMENATION(color,colors)
14910
14911 NK_API int
14912 nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
14913 {
14914 struct nk_style *style;
14915 NK_ASSERT(ctx);
14916 if (!ctx) return 0;
14917 style = &ctx->style;
14918 if (style->cursors[c]) {
14919 style->cursor_active = style->cursors[c];
14920 return 1;
14921 }
14922 return 0;
14923 }
14924 NK_API void
14925 nk_style_show_cursor(struct nk_context *ctx)
14926 {
14927 ctx->style.cursor_visible = nk_true;
14928 }
14929 NK_API void
14930 nk_style_hide_cursor(struct nk_context *ctx)
14931 {
14932 ctx->style.cursor_visible = nk_false;
14933 }
14934 NK_API void
14935 nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
14936 const struct nk_cursor *c)
14937 {
14938 struct nk_style *style;
14939 NK_ASSERT(ctx);
14940 if (!ctx) return;
14941 style = &ctx->style;
14942 style->cursors[cursor] = c;
14943 }
14944 NK_API void
14945 nk_style_load_all_cursors(struct nk_context *ctx, struct nk_cursor *cursors)
14946 {
14947 int i = 0;
14948 struct nk_style *style;
14949 NK_ASSERT(ctx);
14950 if (!ctx) return;
14951 style = &ctx->style;
14952 for (i = 0; i < NK_CURSOR_COUNT; ++i)
14953 style->cursors[i] = &cursors[i];
14954 style->cursor_visible = nk_true;
14955 }
14956
14957
14958
14959
14960
14961 /* ==============================================================
14962 *
14963 * CONTEXT
14964 *
14965 * ===============================================================*/
14966 NK_INTERN void
14967 nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
14968 {
14969 NK_ASSERT(ctx);
14970 if (!ctx) return;
14971 nk_zero_struct(*ctx);
14972 nk_style_default(ctx);
14973 ctx->seq = 1;
14974 if (font) ctx->style.font = font;
14975 #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
14976 nk_draw_list_init(&ctx->draw_list);
14977 #endif
14978 }
14979 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
14980 NK_API int
14981 nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
14982 {
14983 struct nk_allocator alloc;
14984 alloc.userdata.ptr = 0;
14985 alloc.alloc = nk_malloc;
14986 alloc.free = nk_mfree;
14987 return nk_init(ctx, &alloc, font);
14988 }
14989 #endif
14990 NK_API int
14991 nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
14992 const struct nk_user_font *font)
14993 {
14994 NK_ASSERT(memory);
14995 if (!memory) return 0;
14996 nk_setup(ctx, font);
14997 nk_buffer_init_fixed(&ctx->memory, memory, size);
14998 ctx->use_pool = nk_false;
14999 return 1;
15000 }
15001 NK_API int
15002 nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
15003 struct nk_buffer *pool, const struct nk_user_font *font)
15004 {
15005 NK_ASSERT(cmds);
15006 NK_ASSERT(pool);
15007 if (!cmds || !pool) return 0;
15008
15009 nk_setup(ctx, font);
15010 ctx->memory = *cmds;
15011 if (pool->type == NK_BUFFER_FIXED) {
15012 /* take memory from buffer and alloc fixed pool */
15013 nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
15014 } else {
15015 /* create dynamic pool from buffer allocator */
15016 struct nk_allocator *alloc = &pool->pool;
15017 nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
15018 }
15019 ctx->use_pool = nk_true;
15020 return 1;
15021 }
15022 NK_API int
15023 nk_init(struct nk_context *ctx, struct nk_allocator *alloc,
15024 const struct nk_user_font *font)
15025 {
15026 NK_ASSERT(alloc);
15027 if (!alloc) return 0;
15028 nk_setup(ctx, font);
15029 nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
15030 nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
15031 ctx->use_pool = nk_true;
15032 return 1;
15033 }
15034 #ifdef NK_INCLUDE_COMMAND_USERDATA
15035 NK_API void
15036 nk_set_user_data(struct nk_context *ctx, nk_handle handle)
15037 {
15038 if (!ctx) return;
15039 ctx->userdata = handle;
15040 if (ctx->current)
15041 ctx->current->buffer.userdata = handle;
15042 }
15043 #endif
15044 NK_API void
15045 nk_free(struct nk_context *ctx)
15046 {
15047 NK_ASSERT(ctx);
15048 if (!ctx) return;
15049 nk_buffer_free(&ctx->memory);
15050 if (ctx->use_pool)
15051 nk_pool_free(&ctx->pool);
15052
15053 nk_zero(&ctx->input, sizeof(ctx->input));
15054 nk_zero(&ctx->style, sizeof(ctx->style));
15055 nk_zero(&ctx->memory, sizeof(ctx->memory));
15056
15057 ctx->seq = 0;
15058 ctx->build = 0;
15059 ctx->begin = 0;
15060 ctx->end = 0;
15061 ctx->active = 0;
15062 ctx->current = 0;
15063 ctx->freelist = 0;
15064 ctx->count = 0;
15065 }
15066 NK_API void
15067 nk_clear(struct nk_context *ctx)
15068 {
15069 struct nk_window *iter;
15070 struct nk_window *next;
15071 NK_ASSERT(ctx);
15072
15073 if (!ctx) return;
15074 if (ctx->use_pool)
15075 nk_buffer_clear(&ctx->memory);
15076 else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
15077
15078 ctx->build = 0;
15079 ctx->memory.calls = 0;
15080 ctx->last_widget_state = 0;
15081 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
15082 NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
15083
15084 /* garbage collector */
15085 iter = ctx->begin;
15086 while (iter) {
15087 /* make sure valid minimized windows do not get removed */
15088 if ((iter->flags & NK_WINDOW_MINIMIZED) &&
15089 !(iter->flags & NK_WINDOW_CLOSED) &&
15090 iter->seq == ctx->seq) {
15091 iter = iter->next;
15092 continue;
15093 }
15094 /* remove hotness from hidden or closed windows*/
15095 if (((iter->flags & NK_WINDOW_HIDDEN) ||
15096 (iter->flags & NK_WINDOW_CLOSED)) &&
15097 iter == ctx->active) {
15098 ctx->active = iter->prev;
15099 ctx->end = iter->prev;
15100 if (!ctx->end)
15101 ctx->begin = 0;
15102 if (ctx->active)
15103 ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
15104 }
15105 /* free unused popup windows */
15106 if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
15107 nk_free_window(ctx, iter->popup.win);
15108 iter->popup.win = 0;
15109 }
15110 /* remove unused window state tables */
15111 {struct nk_table *n, *it = iter->tables;
15112 while (it) {
15113 n = it->next;
15114 if (it->seq != ctx->seq) {
15115 nk_remove_table(iter, it);
15116 nk_zero(it, sizeof(union nk_page_data));
15117 nk_free_table(ctx, it);
15118 if (it == iter->tables)
15119 iter->tables = n;
15120 } it = n;
15121 }}
15122 /* window itself is not used anymore so free */
15123 if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
15124 next = iter->next;
15125 nk_remove_window(ctx, iter);
15126 nk_free_window(ctx, iter);
15127 iter = next;
15128 } else iter = iter->next;
15129 }
15130 ctx->seq++;
15131 }
15132 NK_LIB void
15133 nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
15134 {
15135 NK_ASSERT(ctx);
15136 NK_ASSERT(buffer);
15137 if (!ctx || !buffer) return;
15138 buffer->begin = ctx->memory.allocated;
15139 buffer->end = buffer->begin;
15140 buffer->last = buffer->begin;
15141 buffer->clip = nk_null_rect;
15142 }
15143 NK_LIB void
15144 nk_start(struct nk_context *ctx, struct nk_window *win)
15145 {
15146 NK_ASSERT(ctx);
15147 NK_ASSERT(win);
15148 nk_start_buffer(ctx, &win->buffer);
15149 }
15150 NK_LIB void
15151 nk_start_popup(struct nk_context *ctx, struct nk_window *win)
15152 {
15153 struct nk_popup_buffer *buf;
15154 NK_ASSERT(ctx);
15155 NK_ASSERT(win);
15156 if (!ctx || !win) return;
15157
15158 /* save buffer fill state for popup */
15159 buf = &win->popup.buf;
15160 buf->begin = win->buffer.end;
15161 buf->end = win->buffer.end;
15162 buf->parent = win->buffer.last;
15163 buf->last = buf->begin;
15164 buf->active = nk_true;
15165 }
15166 NK_LIB void
15167 nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
15168 {
15169 struct nk_popup_buffer *buf;
15170 NK_ASSERT(ctx);
15171 NK_ASSERT(win);
15172 if (!ctx || !win) return;
15173
15174 buf = &win->popup.buf;
15175 buf->last = win->buffer.last;
15176 buf->end = win->buffer.end;
15177 }
15178 NK_LIB void
15179 nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
15180 {
15181 NK_ASSERT(ctx);
15182 NK_ASSERT(buffer);
15183 if (!ctx || !buffer) return;
15184 buffer->end = ctx->memory.allocated;
15185 }
15186 NK_LIB void
15187 nk_finish(struct nk_context *ctx, struct nk_window *win)
15188 {
15189 struct nk_popup_buffer *buf;
15190 struct nk_command *parent_last;
15191 void *memory;
15192
15193 NK_ASSERT(ctx);
15194 NK_ASSERT(win);
15195 if (!ctx || !win) return;
15196 nk_finish_buffer(ctx, &win->buffer);
15197 if (!win->popup.buf.active) return;
15198
15199 buf = &win->popup.buf;
15200 memory = ctx->memory.memory.ptr;
15201 parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
15202 parent_last->next = buf->end;
15203 }
15204 NK_LIB void
15205 nk_build(struct nk_context *ctx)
15206 {
15207 struct nk_window *it = 0;
15208 struct nk_command *cmd = 0;
15209 nk_byte *buffer = 0;
15210
15211 /* draw cursor overlay */
15212 if (!ctx->style.cursor_active)
15213 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
15214 if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
15215 struct nk_rect mouse_bounds;
15216 const struct nk_cursor *cursor = ctx->style.cursor_active;
15217 nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
15218 nk_start_buffer(ctx, &ctx->overlay);
15219
15220 mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
15221 mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
15222 mouse_bounds.w = cursor->size.x;
15223 mouse_bounds.h = cursor->size.y;
15224
15225 nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
15226 nk_finish_buffer(ctx, &ctx->overlay);
15227 }
15228 /* build one big draw command list out of all window buffers */
15229 it = ctx->begin;
15230 buffer = (nk_byte*)ctx->memory.memory.ptr;
15231 while (it != 0) {
15232 struct nk_window *next = it->next;
15233 if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
15234 it->seq != ctx->seq)
15235 goto cont;
15236
15237 cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
15238 while (next && ((next->buffer.last == next->buffer.begin) ||
15239 (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
15240 next = next->next; /* skip empty command buffers */
15241
15242 if (next) cmd->next = next->buffer.begin;
15243 cont: it = next;
15244 }
15245 /* append all popup draw commands into lists */
15246 it = ctx->begin;
15247 while (it != 0) {
15248 struct nk_window *next = it->next;
15249 struct nk_popup_buffer *buf;
15250 if (!it->popup.buf.active)
15251 goto skip;
15252
15253 buf = &it->popup.buf;
15254 cmd->next = buf->begin;
15255 cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
15256 buf->active = nk_false;
15257 skip: it = next;
15258 }
15259 if (cmd) {
15260 /* append overlay commands */
15261 if (ctx->overlay.end != ctx->overlay.begin)
15262 cmd->next = ctx->overlay.begin;
15263 else cmd->next = ctx->memory.allocated;
15264 }
15265 }
15266 NK_API const struct nk_command*
15267 nk__begin(struct nk_context *ctx)
15268 {
15269 struct nk_window *iter;
15270 nk_byte *buffer;
15271 NK_ASSERT(ctx);
15272 if (!ctx) return 0;
15273 if (!ctx->count) return 0;
15274
15275 buffer = (nk_byte*)ctx->memory.memory.ptr;
15276 if (!ctx->build) {
15277 nk_build(ctx);
15278 ctx->build = nk_true;
15279 }
15280 iter = ctx->begin;
15281 while (iter && ((iter->buffer.begin == iter->buffer.end) ||
15282 (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
15283 iter = iter->next;
15284 if (!iter) return 0;
15285 return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
15286 }
15287
15288 NK_API const struct nk_command*
15289 nk__next(struct nk_context *ctx, const struct nk_command *cmd)
15290 {
15291 nk_byte *buffer;
15292 const struct nk_command *next;
15293 NK_ASSERT(ctx);
15294 if (!ctx || !cmd || !ctx->count) return 0;
15295 if (cmd->next >= ctx->memory.allocated) return 0;
15296 buffer = (nk_byte*)ctx->memory.memory.ptr;
15297 next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
15298 return next;
15299 }
15300
15301
15302
15303
15304
15305
15306 /* ===============================================================
15307 *
15308 * POOL
15309 *
15310 * ===============================================================*/
15311 NK_LIB void
15312 nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
15313 unsigned int capacity)
15314 {
15315 nk_zero(pool, sizeof(*pool));
15316 pool->alloc = *alloc;
15317 pool->capacity = capacity;
15318 pool->type = NK_BUFFER_DYNAMIC;
15319 pool->pages = 0;
15320 }
15321 NK_LIB void
15322 nk_pool_free(struct nk_pool *pool)
15323 {
15324 struct nk_page *iter = pool->pages;
15325 if (!pool) return;
15326 if (pool->type == NK_BUFFER_FIXED) return;
15327 while (iter) {
15328 struct nk_page *next = iter->next;
15329 pool->alloc.free(pool->alloc.userdata, iter);
15330 iter = next;
15331 }
15332 }
15333 NK_LIB void
15334 nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
15335 {
15336 nk_zero(pool, sizeof(*pool));
15337 NK_ASSERT(size >= sizeof(struct nk_page));
15338 if (size < sizeof(struct nk_page)) return;
15339 pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
15340 pool->pages = (struct nk_page*)memory;
15341 pool->type = NK_BUFFER_FIXED;
15342 pool->size = size;
15343 }
15344 NK_LIB struct nk_page_element*
15345 nk_pool_alloc(struct nk_pool *pool)
15346 {
15347 if (!pool->pages || pool->pages->size >= pool->capacity) {
15348 /* allocate new page */
15349 struct nk_page *page;
15350 if (pool->type == NK_BUFFER_FIXED) {
15351 NK_ASSERT(pool->pages);
15352 if (!pool->pages) return 0;
15353 NK_ASSERT(pool->pages->size < pool->capacity);
15354 return 0;
15355 } else {
15356 nk_size size = sizeof(struct nk_page);
15357 size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
15358 page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
15359 page->next = pool->pages;
15360 pool->pages = page;
15361 page->size = 0;
15362 }
15363 } return &pool->pages->win[pool->pages->size++];
15364 }
15365
15366
15367
15368
15369
15370 /* ===============================================================
15371 *
15372 * PAGE ELEMENT
15373 *
15374 * ===============================================================*/
15375 NK_LIB struct nk_page_element*
15376 nk_create_page_element(struct nk_context *ctx)
15377 {
15378 struct nk_page_element *elem;
15379 if (ctx->freelist) {
15380 /* unlink page element from free list */
15381 elem = ctx->freelist;
15382 ctx->freelist = elem->next;
15383 } else if (ctx->use_pool) {
15384 /* allocate page element from memory pool */
15385 elem = nk_pool_alloc(&ctx->pool);
15386 NK_ASSERT(elem);
15387 if (!elem) return 0;
15388 } else {
15389 /* allocate new page element from back of fixed size memory buffer */
15390 NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
15391 NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
15392 elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
15393 NK_ASSERT(elem);
15394 if (!elem) return 0;
15395 }
15396 nk_zero_struct(*elem);
15397 elem->next = 0;
15398 elem->prev = 0;
15399 return elem;
15400 }
15401 NK_LIB void
15402 nk_link_page_element_into_freelist(struct nk_context *ctx,
15403 struct nk_page_element *elem)
15404 {
15405 /* link table into freelist */
15406 if (!ctx->freelist) {
15407 ctx->freelist = elem;
15408 } else {
15409 elem->next = ctx->freelist;
15410 ctx->freelist = elem;
15411 }
15412 }
15413 NK_LIB void
15414 nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
15415 {
15416 /* we have a pool so just add to free list */
15417 if (ctx->use_pool) {
15418 nk_link_page_element_into_freelist(ctx, elem);
15419 return;
15420 }
15421 /* if possible remove last element from back of fixed memory buffer */
15422 {void *elem_end = (void*)(elem + 1);
15423 void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
15424 if (elem_end == buffer_end)
15425 ctx->memory.size -= sizeof(struct nk_page_element);
15426 else nk_link_page_element_into_freelist(ctx, elem);}
15427 }
15428
15429
15430
15431
15432
15433 /* ===============================================================
15434 *
15435 * TABLE
15436 *
15437 * ===============================================================*/
15438 NK_LIB struct nk_table*
15439 nk_create_table(struct nk_context *ctx)
15440 {
15441 struct nk_page_element *elem;
15442 elem = nk_create_page_element(ctx);
15443 if (!elem) return 0;
15444 nk_zero_struct(*elem);
15445 return &elem->data.tbl;
15446 }
15447 NK_LIB void
15448 nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
15449 {
15450 union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
15451 struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
15452 nk_free_page_element(ctx, pe);
15453 }
15454 NK_LIB void
15455 nk_push_table(struct nk_window *win, struct nk_table *tbl)
15456 {
15457 if (!win->tables) {
15458 win->tables = tbl;
15459 tbl->next = 0;
15460 tbl->prev = 0;
15461 tbl->size = 0;
15462 win->table_count = 1;
15463 return;
15464 }
15465 win->tables->prev = tbl;
15466 tbl->next = win->tables;
15467 tbl->prev = 0;
15468 tbl->size = 0;
15469 win->tables = tbl;
15470 win->table_count++;
15471 }
15472 NK_LIB void
15473 nk_remove_table(struct nk_window *win, struct nk_table *tbl)
15474 {
15475 if (win->tables == tbl)
15476 win->tables = tbl->next;
15477 if (tbl->next)
15478 tbl->next->prev = tbl->prev;
15479 if (tbl->prev)
15480 tbl->prev->next = tbl->next;
15481 tbl->next = 0;
15482 tbl->prev = 0;
15483 }
15484 NK_LIB nk_uint*
15485 nk_add_value(struct nk_context *ctx, struct nk_window *win,
15486 nk_hash name, nk_uint value)
15487 {
15488 NK_ASSERT(ctx);
15489 NK_ASSERT(win);
15490 if (!win || !ctx) return 0;
15491 if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
15492 struct nk_table *tbl = nk_create_table(ctx);
15493 NK_ASSERT(tbl);
15494 if (!tbl) return 0;
15495 nk_push_table(win, tbl);
15496 }
15497 win->tables->seq = win->seq;
15498 win->tables->keys[win->tables->size] = name;
15499 win->tables->values[win->tables->size] = value;
15500 return &win->tables->values[win->tables->size++];
15501 }
15502 NK_LIB nk_uint*
15503 nk_find_value(struct nk_window *win, nk_hash name)
15504 {
15505 struct nk_table *iter = win->tables;
15506 while (iter) {
15507 unsigned int i = 0;
15508 unsigned int size = iter->size;
15509 for (i = 0; i < size; ++i) {
15510 if (iter->keys[i] == name) {
15511 iter->seq = win->seq;
15512 return &iter->values[i];
15513 }
15514 } size = NK_VALUE_PAGE_CAPACITY;
15515 iter = iter->next;
15516 }
15517 return 0;
15518 }
15519
15520
15521
15522
15523
15524 /* ===============================================================
15525 *
15526 * PANEL
15527 *
15528 * ===============================================================*/
15529 NK_LIB void*
15530 nk_create_panel(struct nk_context *ctx)
15531 {
15532 struct nk_page_element *elem;
15533 elem = nk_create_page_element(ctx);
15534 if (!elem) return 0;
15535 nk_zero_struct(*elem);
15536 return &elem->data.pan;
15537 }
15538 NK_LIB void
15539 nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
15540 {
15541 union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
15542 struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
15543 nk_free_page_element(ctx, pe);
15544 }
15545 NK_LIB int
15546 nk_panel_has_header(nk_flags flags, const char *title)
15547 {
15548 int active = 0;
15549 active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
15550 active = active || (flags & NK_WINDOW_TITLE);
15551 active = active && !(flags & NK_WINDOW_HIDDEN) && title;
15552 return active;
15553 }
15554 NK_LIB struct nk_vec2
15555 nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
15556 {
15557 switch (type) {
15558 default:
15559 case NK_PANEL_WINDOW: return style->window.padding;
15560 case NK_PANEL_GROUP: return style->window.group_padding;
15561 case NK_PANEL_POPUP: return style->window.popup_padding;
15562 case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
15563 case NK_PANEL_COMBO: return style->window.combo_padding;
15564 case NK_PANEL_MENU: return style->window.menu_padding;
15565 case NK_PANEL_TOOLTIP: return style->window.menu_padding;}
15566 }
15567 NK_LIB float
15568 nk_panel_get_border(const struct nk_style *style, nk_flags flags,
15569 enum nk_panel_type type)
15570 {
15571 if (flags & NK_WINDOW_BORDER) {
15572 switch (type) {
15573 default:
15574 case NK_PANEL_WINDOW: return style->window.border;
15575 case NK_PANEL_GROUP: return style->window.group_border;
15576 case NK_PANEL_POPUP: return style->window.popup_border;
15577 case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
15578 case NK_PANEL_COMBO: return style->window.combo_border;
15579 case NK_PANEL_MENU: return style->window.menu_border;
15580 case NK_PANEL_TOOLTIP: return style->window.menu_border;
15581 }} else return 0;
15582 }
15583 NK_LIB struct nk_color
15584 nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
15585 {
15586 switch (type) {
15587 default:
15588 case NK_PANEL_WINDOW: return style->window.border_color;
15589 case NK_PANEL_GROUP: return style->window.group_border_color;
15590 case NK_PANEL_POPUP: return style->window.popup_border_color;
15591 case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
15592 case NK_PANEL_COMBO: return style->window.combo_border_color;
15593 case NK_PANEL_MENU: return style->window.menu_border_color;
15594 case NK_PANEL_TOOLTIP: return style->window.menu_border_color;}
15595 }
15596 NK_LIB int
15597 nk_panel_is_sub(enum nk_panel_type type)
15598 {
15599 return (type & NK_PANEL_SET_SUB)?1:0;
15600 }
15601 NK_LIB int
15602 nk_panel_is_nonblock(enum nk_panel_type type)
15603 {
15604 return (type & NK_PANEL_SET_NONBLOCK)?1:0;
15605 }
15606 NK_LIB int
15607 nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
15608 {
15609 struct nk_input *in;
15610 struct nk_window *win;
15611 struct nk_panel *layout;
15612 struct nk_command_buffer *out;
15613 const struct nk_style *style;
15614 const struct nk_user_font *font;
15615
15616 struct nk_vec2 scrollbar_size;
15617 struct nk_vec2 panel_padding;
15618
15619 NK_ASSERT(ctx);
15620 NK_ASSERT(ctx->current);
15621 NK_ASSERT(ctx->current->layout);
15622 if (!ctx || !ctx->current || !ctx->current->layout) return 0;
15623 nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
15624 if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
15625 nk_zero(ctx->current->layout, sizeof(struct nk_panel));
15626 ctx->current->layout->type = panel_type;
15627 return 0;
15628 }
15629 /* pull state into local stack */
15630 style = &ctx->style;
15631 font = style->font;
15632 win = ctx->current;
15633 layout = win->layout;
15634 out = &win->buffer;
15635 in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
15636 #ifdef NK_INCLUDE_COMMAND_USERDATA
15637 win->buffer.userdata = ctx->userdata;
15638 #endif
15639 /* pull style configuration into local stack */
15640 scrollbar_size = style->window.scrollbar_size;
15641 panel_padding = nk_panel_get_padding(style, panel_type);
15642
15643 /* window movement */
15644 if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
15645 int left_mouse_down;
15646 int left_mouse_clicked;
15647 int left_mouse_click_in_cursor;
15648
15649 /* calculate draggable window space */
15650 struct nk_rect header;
15651 header.x = win->bounds.x;
15652 header.y = win->bounds.y;
15653 header.w = win->bounds.w;
15654 if (nk_panel_has_header(win->flags, title)) {
15655 header.h = font->height + 2.0f * style->window.header.padding.y;
15656 header.h += 2.0f * style->window.header.label_padding.y;
15657 } else header.h = panel_padding.y;
15658
15659 /* window movement by dragging */
15660 left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
15661 left_mouse_clicked = (int)in->mouse.buttons[NK_BUTTON_LEFT].clicked;
15662 left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
15663 NK_BUTTON_LEFT, header, nk_true);
15664 if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
15665 win->bounds.x = win->bounds.x + in->mouse.delta.x;
15666 win->bounds.y = win->bounds.y + in->mouse.delta.y;
15667 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
15668 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
15669 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
15670 }
15671 }
15672
15673 /* setup panel */
15674 layout->type = panel_type;
15675 layout->flags = win->flags;
15676 layout->bounds = win->bounds;
15677 layout->bounds.x += panel_padding.x;
15678 layout->bounds.w -= 2*panel_padding.x;
15679 if (win->flags & NK_WINDOW_BORDER) {
15680 layout->border = nk_panel_get_border(style, win->flags, panel_type);
15681 layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
15682 } else layout->border = 0;
15683 layout->at_y = layout->bounds.y;
15684 layout->at_x = layout->bounds.x;
15685 layout->max_x = 0;
15686 layout->header_height = 0;
15687 layout->footer_height = 0;
15688 nk_layout_reset_min_row_height(ctx);
15689 layout->row.index = 0;
15690 layout->row.columns = 0;
15691 layout->row.ratio = 0;
15692 layout->row.item_width = 0;
15693 layout->row.tree_depth = 0;
15694 layout->row.height = panel_padding.y;
15695 layout->has_scrolling = nk_true;
15696 if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
15697 layout->bounds.w -= scrollbar_size.x;
15698 if (!nk_panel_is_nonblock(panel_type)) {
15699 layout->footer_height = 0;
15700 if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
15701 layout->footer_height = scrollbar_size.y;
15702 layout->bounds.h -= layout->footer_height;
15703 }
15704
15705 /* panel header */
15706 if (nk_panel_has_header(win->flags, title))
15707 {
15708 struct nk_text text;
15709 struct nk_rect header;
15710 const struct nk_style_item *background = 0;
15711
15712 /* calculate header bounds */
15713 header.x = win->bounds.x;
15714 header.y = win->bounds.y;
15715 header.w = win->bounds.w;
15716 header.h = font->height + 2.0f * style->window.header.padding.y;
15717 header.h += (2.0f * style->window.header.label_padding.y);
15718
15719 /* shrink panel by header */
15720 layout->header_height = header.h;
15721 layout->bounds.y += header.h;
15722 layout->bounds.h -= header.h;
15723 layout->at_y += header.h;
15724
15725 /* select correct header background and text color */
15726 if (ctx->active == win) {
15727 background = &style->window.header.active;
15728 text.text = style->window.header.label_active;
15729 } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
15730 background = &style->window.header.hover;
15731 text.text = style->window.header.label_hover;
15732 } else {
15733 background = &style->window.header.normal;
15734 text.text = style->window.header.label_normal;
15735 }
15736
15737 /* draw header background */
15738 header.h += 1.0f;
15739 if (background->type == NK_STYLE_ITEM_IMAGE) {
15740 text.background = nk_rgba(0,0,0,0);
15741 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
15742 } else {
15743 text.background = background->data.color;
15744 nk_fill_rect(out, header, 0, background->data.color);
15745 }
15746
15747 /* window close button */
15748 {struct nk_rect button;
15749 button.y = header.y + style->window.header.padding.y;
15750 button.h = header.h - 2 * style->window.header.padding.y;
15751 button.w = button.h;
15752 if (win->flags & NK_WINDOW_CLOSABLE) {
15753 nk_flags ws = 0;
15754 if (style->window.header.align == NK_HEADER_RIGHT) {
15755 button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
15756 header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
15757 } else {
15758 button.x = header.x + style->window.header.padding.x;
15759 header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
15760 }
15761
15762 if (nk_do_button_symbol(&ws, &win->buffer, button,
15763 style->window.header.close_symbol, NK_BUTTON_DEFAULT,
15764 &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
15765 {
15766 layout->flags |= NK_WINDOW_HIDDEN;
15767 layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
15768 }
15769 }
15770
15771 /* window minimize button */
15772 if (win->flags & NK_WINDOW_MINIMIZABLE) {
15773 nk_flags ws = 0;
15774 if (style->window.header.align == NK_HEADER_RIGHT) {
15775 button.x = (header.w + header.x) - button.w;
15776 if (!(win->flags & NK_WINDOW_CLOSABLE)) {
15777 button.x -= style->window.header.padding.x;
15778 header.w -= style->window.header.padding.x;
15779 }
15780 header.w -= button.w + style->window.header.spacing.x;
15781 } else {
15782 button.x = header.x;
15783 header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
15784 }
15785 if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
15786 style->window.header.maximize_symbol: style->window.header.minimize_symbol,
15787 NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
15788 layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
15789 layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
15790 layout->flags | NK_WINDOW_MINIMIZED;
15791 }}
15792
15793 {/* window header title */
15794 int text_len = nk_strlen(title);
15795 struct nk_rect label = {0,0,0,0};
15796 float t = font->width(font->userdata, font->height, title, text_len);
15797 text.padding = nk_vec2(0,0);
15798
15799 label.x = header.x + style->window.header.padding.x;
15800 label.x += style->window.header.label_padding.x;
15801 label.y = header.y + style->window.header.label_padding.y;
15802 label.h = font->height + 2 * style->window.header.label_padding.y;
15803 label.w = t + 2 * style->window.header.spacing.x;
15804 label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
15805 nk_widget_text(out, label,(const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
15806 }
15807
15808 /* draw window background */
15809 if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
15810 struct nk_rect body;
15811 body.x = win->bounds.x;
15812 body.w = win->bounds.w;
15813 body.y = (win->bounds.y + layout->header_height);
15814 body.h = (win->bounds.h - layout->header_height);
15815 if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
15816 nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
15817 else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
15818 }
15819
15820 /* set clipping rectangle */
15821 {struct nk_rect clip;
15822 layout->clip = layout->bounds;
15823 nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
15824 layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
15825 nk_push_scissor(out, clip);
15826 layout->clip = clip;}
15827 return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
15828 }
15829 NK_LIB void
15830 nk_panel_end(struct nk_context *ctx)
15831 {
15832 struct nk_input *in;
15833 struct nk_window *window;
15834 struct nk_panel *layout;
15835 const struct nk_style *style;
15836 struct nk_command_buffer *out;
15837
15838 struct nk_vec2 scrollbar_size;
15839 struct nk_vec2 panel_padding;
15840
15841 NK_ASSERT(ctx);
15842 NK_ASSERT(ctx->current);
15843 NK_ASSERT(ctx->current->layout);
15844 if (!ctx || !ctx->current || !ctx->current->layout)
15845 return;
15846
15847 window = ctx->current;
15848 layout = window->layout;
15849 style = &ctx->style;
15850 out = &window->buffer;
15851 in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
15852 if (!nk_panel_is_sub(layout->type))
15853 nk_push_scissor(out, nk_null_rect);
15854
15855 /* cache configuration data */
15856 scrollbar_size = style->window.scrollbar_size;
15857 panel_padding = nk_panel_get_padding(style, layout->type);
15858
15859 /* update the current cursor Y-position to point over the last added widget */
15860 layout->at_y += layout->row.height;
15861
15862 /* dynamic panels */
15863 if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
15864 {
15865 /* update panel height to fit dynamic growth */
15866 struct nk_rect empty_space;
15867 if (layout->at_y < (layout->bounds.y + layout->bounds.h))
15868 layout->bounds.h = layout->at_y - layout->bounds.y;
15869
15870 /* fill top empty space */
15871 empty_space.x = window->bounds.x;
15872 empty_space.y = layout->bounds.y;
15873 empty_space.h = panel_padding.y;
15874 empty_space.w = window->bounds.w;
15875 nk_fill_rect(out, empty_space, 0, style->window.background);
15876
15877 /* fill left empty space */
15878 empty_space.x = window->bounds.x;
15879 empty_space.y = layout->bounds.y;
15880 empty_space.w = panel_padding.x + layout->border;
15881 empty_space.h = layout->bounds.h;
15882 nk_fill_rect(out, empty_space, 0, style->window.background);
15883
15884 /* fill right empty space */
15885 empty_space.x = layout->bounds.x + layout->bounds.w;
15886 empty_space.y = layout->bounds.y;
15887 empty_space.w = panel_padding.x + layout->border;
15888 empty_space.h = layout->bounds.h;
15889 if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
15890 empty_space.w += scrollbar_size.x;
15891 nk_fill_rect(out, empty_space, 0, style->window.background);
15892
15893 /* fill bottom empty space */
15894 if (layout->footer_height > 0) {
15895 empty_space.x = window->bounds.x;
15896 empty_space.y = layout->bounds.y + layout->bounds.h;
15897 empty_space.w = window->bounds.w;
15898 empty_space.h = layout->footer_height;
15899 nk_fill_rect(out, empty_space, 0, style->window.background);
15900 }
15901 }
15902
15903 /* scrollbars */
15904 if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
15905 !(layout->flags & NK_WINDOW_MINIMIZED) &&
15906 window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
15907 {
15908 struct nk_rect scroll;
15909 int scroll_has_scrolling;
15910 float scroll_target;
15911 float scroll_offset;
15912 float scroll_step;
15913 float scroll_inc;
15914
15915 /* mouse wheel scrolling */
15916 if (nk_panel_is_sub(layout->type))
15917 {
15918 /* sub-window mouse wheel scrolling */
15919 struct nk_window *root_window = window;
15920 struct nk_panel *root_panel = window->layout;
15921 while (root_panel->parent)
15922 root_panel = root_panel->parent;
15923 while (root_window->parent)
15924 root_window = root_window->parent;
15925
15926 /* only allow scrolling if parent window is active */
15927 scroll_has_scrolling = 0;
15928 if ((root_window == ctx->active) && layout->has_scrolling) {
15929 /* and panel is being hovered and inside clip rect*/
15930 if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
15931 NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
15932 root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
15933 {
15934 /* deactivate all parent scrolling */
15935 root_panel = window->layout;
15936 while (root_panel->parent) {
15937 root_panel->has_scrolling = nk_false;
15938 root_panel = root_panel->parent;
15939 }
15940 root_panel->has_scrolling = nk_false;
15941 scroll_has_scrolling = nk_true;
15942 }
15943 }
15944 } else if (!nk_panel_is_sub(layout->type)) {
15945 /* window mouse wheel scrolling */
15946 scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
15947 if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
15948 window->scrolled = nk_true;
15949 else window->scrolled = nk_false;
15950 } else scroll_has_scrolling = nk_false;
15951
15952 {
15953 /* vertical scrollbar */
15954 nk_flags state = 0;
15955 scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
15956 scroll.y = layout->bounds.y;
15957 scroll.w = scrollbar_size.x;
15958 scroll.h = layout->bounds.h;
15959
15960 scroll_offset = (float)*layout->offset_y;
15961 scroll_step = scroll.h * 0.10f;
15962 scroll_inc = scroll.h * 0.01f;
15963 scroll_target = (float)(int)(layout->at_y - scroll.y);
15964 scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
15965 scroll_offset, scroll_target, scroll_step, scroll_inc,
15966 &ctx->style.scrollv, in, style->font);
15967 *layout->offset_y = (nk_uint)scroll_offset;
15968 if (in && scroll_has_scrolling)
15969 in->mouse.scroll_delta.y = 0;
15970 }
15971 {
15972 /* horizontal scrollbar */
15973 nk_flags state = 0;
15974 scroll.x = layout->bounds.x;
15975 scroll.y = layout->bounds.y + layout->bounds.h;
15976 scroll.w = layout->bounds.w;
15977 scroll.h = scrollbar_size.y;
15978
15979 scroll_offset = (float)*layout->offset_x;
15980 scroll_target = (float)(int)(layout->max_x - scroll.x);
15981 scroll_step = layout->max_x * 0.05f;
15982 scroll_inc = layout->max_x * 0.005f;
15983 scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
15984 scroll_offset, scroll_target, scroll_step, scroll_inc,
15985 &ctx->style.scrollh, in, style->font);
15986 *layout->offset_x = (nk_uint)scroll_offset;
15987 }
15988 }
15989
15990 /* hide scroll if no user input */
15991 if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
15992 int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
15993 int is_window_hovered = nk_window_is_hovered(ctx);
15994 int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
15995 if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
15996 window->scrollbar_hiding_timer += ctx->delta_time_seconds;
15997 else window->scrollbar_hiding_timer = 0;
15998 } else window->scrollbar_hiding_timer = 0;
15999
16000 /* window border */
16001 if (layout->flags & NK_WINDOW_BORDER)
16002 {
16003 struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
16004 const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
16005 ? (style->window.border + window->bounds.y + layout->header_height)
16006 : ((layout->flags & NK_WINDOW_DYNAMIC)
16007 ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
16008 : (window->bounds.y + window->bounds.h));
16009 struct nk_rect b = window->bounds;
16010 b.h = padding_y - window->bounds.y;
16011 nk_stroke_rect(out, b, 0, layout->border, border_color);
16012 }
16013
16014 /* scaler */
16015 if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
16016 {
16017 /* calculate scaler bounds */
16018 struct nk_rect scaler;
16019 scaler.w = scrollbar_size.x;
16020 scaler.h = scrollbar_size.y;
16021 scaler.y = layout->bounds.y + layout->bounds.h;
16022 if (layout->flags & NK_WINDOW_SCALE_LEFT)
16023 scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
16024 else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
16025 if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
16026 scaler.x -= scaler.w;
16027
16028 /* draw scaler */
16029 {const struct nk_style_item *item = &style->window.scaler;
16030 if (item->type == NK_STYLE_ITEM_IMAGE)
16031 nk_draw_image(out, scaler, &item->data.image, nk_white);
16032 else {
16033 if (layout->flags & NK_WINDOW_SCALE_LEFT) {
16034 nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
16035 scaler.y + scaler.h, scaler.x + scaler.w,
16036 scaler.y + scaler.h, item->data.color);
16037 } else {
16038 nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
16039 scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
16040 }
16041 }}
16042
16043 /* do window scaling */
16044 if (!(window->flags & NK_WINDOW_ROM)) {
16045 struct nk_vec2 window_size = style->window.min_size;
16046 int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
16047 int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
16048 NK_BUTTON_LEFT, scaler, nk_true);
16049
16050 if (left_mouse_down && left_mouse_click_in_scaler) {
16051 float delta_x = in->mouse.delta.x;
16052 if (layout->flags & NK_WINDOW_SCALE_LEFT) {
16053 delta_x = -delta_x;
16054 window->bounds.x += in->mouse.delta.x;
16055 }
16056 /* dragging in x-direction */
16057 if (window->bounds.w + delta_x >= window_size.x) {
16058 if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
16059 window->bounds.w = window->bounds.w + delta_x;
16060 scaler.x += in->mouse.delta.x;
16061 }
16062 }
16063 /* dragging in y-direction (only possible if static window) */
16064 if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
16065 if (window_size.y < window->bounds.h + in->mouse.delta.y) {
16066 if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
16067 window->bounds.h = window->bounds.h + in->mouse.delta.y;
16068 scaler.y += in->mouse.delta.y;
16069 }
16070 }
16071 }
16072 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
16073 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
16074 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
16075 }
16076 }
16077 }
16078 if (!nk_panel_is_sub(layout->type)) {
16079 /* window is hidden so clear command buffer */
16080 if (layout->flags & NK_WINDOW_HIDDEN)
16081 nk_command_buffer_reset(&window->buffer);
16082 /* window is visible and not tab */
16083 else nk_finish(ctx, window);
16084 }
16085
16086 /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
16087 if (layout->flags & NK_WINDOW_REMOVE_ROM) {
16088 layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
16089 layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
16090 }
16091 window->flags = layout->flags;
16092
16093 /* property garbage collector */
16094 if (window->property.active && window->property.old != window->property.seq &&
16095 window->property.active == window->property.prev) {
16096 nk_zero(&window->property, sizeof(window->property));
16097 } else {
16098 window->property.old = window->property.seq;
16099 window->property.prev = window->property.active;
16100 window->property.seq = 0;
16101 }
16102 /* edit garbage collector */
16103 if (window->edit.active && window->edit.old != window->edit.seq &&
16104 window->edit.active == window->edit.prev) {
16105 nk_zero(&window->edit, sizeof(window->edit));
16106 } else {
16107 window->edit.old = window->edit.seq;
16108 window->edit.prev = window->edit.active;
16109 window->edit.seq = 0;
16110 }
16111 /* contextual garbage collector */
16112 if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
16113 window->popup.con_count = 0;
16114 window->popup.con_old = 0;
16115 window->popup.active_con = 0;
16116 } else {
16117 window->popup.con_old = window->popup.con_count;
16118 window->popup.con_count = 0;
16119 }
16120 window->popup.combo_count = 0;
16121 /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
16122 NK_ASSERT(!layout->row.tree_depth);
16123 }
16124
16125
16126
16127
16128
16129 /* ===============================================================
16130 *
16131 * WINDOW
16132 *
16133 * ===============================================================*/
16134 NK_LIB void*
16135 nk_create_window(struct nk_context *ctx)
16136 {
16137 struct nk_page_element *elem;
16138 elem = nk_create_page_element(ctx);
16139 if (!elem) return 0;
16140 elem->data.win.seq = ctx->seq;
16141 return &elem->data.win;
16142 }
16143 NK_LIB void
16144 nk_free_window(struct nk_context *ctx, struct nk_window *win)
16145 {
16146 /* unlink windows from list */
16147 struct nk_table *it = win->tables;
16148 if (win->popup.win) {
16149 nk_free_window(ctx, win->popup.win);
16150 win->popup.win = 0;
16151 }
16152 win->next = 0;
16153 win->prev = 0;
16154
16155 while (it) {
16156 /*free window state tables */
16157 struct nk_table *n = it->next;
16158 nk_remove_table(win, it);
16159 nk_free_table(ctx, it);
16160 if (it == win->tables)
16161 win->tables = n;
16162 it = n;
16163 }
16164
16165 /* link windows into freelist */
16166 {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
16167 struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
16168 nk_free_page_element(ctx, pe);}
16169 }
16170 NK_LIB struct nk_window*
16171 nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
16172 {
16173 struct nk_window *iter;
16174 iter = ctx->begin;
16175 while (iter) {
16176 NK_ASSERT(iter != iter->next);
16177 if (iter->name == hash) {
16178 int max_len = nk_strlen(iter->name_string);
16179 if (!nk_stricmpn(iter->name_string, name, max_len))
16180 return iter;
16181 }
16182 iter = iter->next;
16183 }
16184 return 0;
16185 }
16186 NK_LIB void
16187 nk_insert_window(struct nk_context *ctx, struct nk_window *win,
16188 enum nk_window_insert_location loc)
16189 {
16190 const struct nk_window *iter;
16191 NK_ASSERT(ctx);
16192 NK_ASSERT(win);
16193 if (!win || !ctx) return;
16194
16195 iter = ctx->begin;
16196 while (iter) {
16197 NK_ASSERT(iter != iter->next);
16198 NK_ASSERT(iter != win);
16199 if (iter == win) return;
16200 iter = iter->next;
16201 }
16202
16203 if (!ctx->begin) {
16204 win->next = 0;
16205 win->prev = 0;
16206 ctx->begin = win;
16207 ctx->end = win;
16208 ctx->count = 1;
16209 return;
16210 }
16211 if (loc == NK_INSERT_BACK) {
16212 struct nk_window *end;
16213 end = ctx->end;
16214 end->flags |= NK_WINDOW_ROM;
16215 end->next = win;
16216 win->prev = ctx->end;
16217 win->next = 0;
16218 ctx->end = win;
16219 ctx->active = ctx->end;
16220 ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
16221 } else {
16222 /*ctx->end->flags |= NK_WINDOW_ROM;*/
16223 ctx->begin->prev = win;
16224 win->next = ctx->begin;
16225 win->prev = 0;
16226 ctx->begin = win;
16227 ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
16228 }
16229 ctx->count++;
16230 }
16231 NK_LIB void
16232 nk_remove_window(struct nk_context *ctx, struct nk_window *win)
16233 {
16234 if (win == ctx->begin || win == ctx->end) {
16235 if (win == ctx->begin) {
16236 ctx->begin = win->next;
16237 if (win->next)
16238 win->next->prev = 0;
16239 }
16240 if (win == ctx->end) {
16241 ctx->end = win->prev;
16242 if (win->prev)
16243 win->prev->next = 0;
16244 }
16245 } else {
16246 if (win->next)
16247 win->next->prev = win->prev;
16248 if (win->prev)
16249 win->prev->next = win->next;
16250 }
16251 if (win == ctx->active || !ctx->active) {
16252 ctx->active = ctx->end;
16253 if (ctx->end)
16254 ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
16255 }
16256 win->next = 0;
16257 win->prev = 0;
16258 ctx->count--;
16259 }
16260 NK_API int
16261 nk_begin(struct nk_context *ctx, const char *title,
16262 struct nk_rect bounds, nk_flags flags)
16263 {
16264 return nk_begin_titled(ctx, title, title, bounds, flags);
16265 }
16266 NK_API int
16267 nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
16268 struct nk_rect bounds, nk_flags flags)
16269 {
16270 struct nk_window *win;
16271 struct nk_style *style;
16272 nk_hash name_hash;
16273 int name_len;
16274 int ret = 0;
16275
16276 NK_ASSERT(ctx);
16277 NK_ASSERT(name);
16278 NK_ASSERT(title);
16279 NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
16280 NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
16281 if (!ctx || ctx->current || !title || !name)
16282 return 0;
16283
16284 /* find or create window */
16285 style = &ctx->style;
16286 name_len = (int)nk_strlen(name);
16287 name_hash = nk_murmur_hash(name, (int)name_len, NK_WINDOW_TITLE);
16288 win = nk_find_window(ctx, name_hash, name);
16289 if (!win) {
16290 /* create new window */
16291 nk_size name_length = (nk_size)name_len;
16292 win = (struct nk_window*)nk_create_window(ctx);
16293 NK_ASSERT(win);
16294 if (!win) return 0;
16295
16296 if (flags & NK_WINDOW_BACKGROUND)
16297 nk_insert_window(ctx, win, NK_INSERT_FRONT);
16298 else nk_insert_window(ctx, win, NK_INSERT_BACK);
16299 nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
16300
16301 win->flags = flags;
16302 win->bounds = bounds;
16303 win->name = name_hash;
16304 name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
16305 NK_MEMCPY(win->name_string, name, name_length);
16306 win->name_string[name_length] = 0;
16307 win->popup.win = 0;
16308 if (!ctx->active)
16309 ctx->active = win;
16310 } else {
16311 /* update window */
16312 win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
16313 win->flags |= flags;
16314 if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
16315 win->bounds = bounds;
16316 /* If this assert triggers you either:
16317 *
16318 * I.) Have more than one window with the same name or
16319 * II.) You forgot to actually draw the window.
16320 * More specific you did not call `nk_clear` (nk_clear will be
16321 * automatically called for you if you are using one of the
16322 * provided demo backends). */
16323 NK_ASSERT(win->seq != ctx->seq);
16324 win->seq = ctx->seq;
16325 if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
16326 ctx->active = win;
16327 ctx->end = win;
16328 }
16329 }
16330 if (win->flags & NK_WINDOW_HIDDEN) {
16331 ctx->current = win;
16332 win->layout = 0;
16333 return 0;
16334 } else nk_start(ctx, win);
16335
16336 /* window overlapping */
16337 if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
16338 {
16339 int inpanel, ishovered;
16340 struct nk_window *iter = win;
16341 float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
16342 (2.0f * style->window.header.label_padding.y);
16343 struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
16344 win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
16345
16346 /* activate window if hovered and no other window is overlapping this window */
16347 inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
16348 inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
16349 ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
16350 if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
16351 iter = win->next;
16352 while (iter) {
16353 struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
16354 iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
16355 if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
16356 iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
16357 (!(iter->flags & NK_WINDOW_HIDDEN)))
16358 break;
16359
16360 if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
16361 NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
16362 iter->popup.win->bounds.x, iter->popup.win->bounds.y,
16363 iter->popup.win->bounds.w, iter->popup.win->bounds.h))
16364 break;
16365 iter = iter->next;
16366 }
16367 }
16368
16369 /* activate window if clicked */
16370 if (iter && inpanel && (win != ctx->end)) {
16371 iter = win->next;
16372 while (iter) {
16373 /* try to find a panel with higher priority in the same position */
16374 struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
16375 iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
16376 if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
16377 iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
16378 !(iter->flags & NK_WINDOW_HIDDEN))
16379 break;
16380 if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
16381 NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
16382 iter->popup.win->bounds.x, iter->popup.win->bounds.y,
16383 iter->popup.win->bounds.w, iter->popup.win->bounds.h))
16384 break;
16385 iter = iter->next;
16386 }
16387 }
16388 if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
16389 win->flags |= (nk_flags)NK_WINDOW_ROM;
16390 iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
16391 ctx->active = iter;
16392 if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
16393 /* current window is active in that position so transfer to top
16394 * at the highest priority in stack */
16395 nk_remove_window(ctx, iter);
16396 nk_insert_window(ctx, iter, NK_INSERT_BACK);
16397 }
16398 } else {
16399 if (!iter && ctx->end != win) {
16400 if (!(win->flags & NK_WINDOW_BACKGROUND)) {
16401 /* current window is active in that position so transfer to top
16402 * at the highest priority in stack */
16403 nk_remove_window(ctx, win);
16404 nk_insert_window(ctx, win, NK_INSERT_BACK);
16405 }
16406 win->flags &= ~(nk_flags)NK_WINDOW_ROM;
16407 ctx->active = win;
16408 }
16409 if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
16410 win->flags |= NK_WINDOW_ROM;
16411 }
16412 }
16413 win->layout = (struct nk_panel*)nk_create_panel(ctx);
16414 ctx->current = win;
16415 ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
16416 win->layout->offset_x = &win->scrollbar.x;
16417 win->layout->offset_y = &win->scrollbar.y;
16418 return ret;
16419 }
16420 NK_API void
16421 nk_end(struct nk_context *ctx)
16422 {
16423 struct nk_panel *layout;
16424 NK_ASSERT(ctx);
16425 NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
16426 if (!ctx || !ctx->current)
16427 return;
16428
16429 layout = ctx->current->layout;
16430 if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
16431 ctx->current = 0;
16432 return;
16433 }
16434 nk_panel_end(ctx);
16435 nk_free_panel(ctx, ctx->current->layout);
16436 ctx->current = 0;
16437 }
16438 NK_API struct nk_rect
16439 nk_window_get_bounds(const struct nk_context *ctx)
16440 {
16441 NK_ASSERT(ctx);
16442 NK_ASSERT(ctx->current);
16443 if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
16444 return ctx->current->bounds;
16445 }
16446 NK_API struct nk_vec2
16447 nk_window_get_position(const struct nk_context *ctx)
16448 {
16449 NK_ASSERT(ctx);
16450 NK_ASSERT(ctx->current);
16451 if (!ctx || !ctx->current) return nk_vec2(0,0);
16452 return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
16453 }
16454 NK_API struct nk_vec2
16455 nk_window_get_size(const struct nk_context *ctx)
16456 {
16457 NK_ASSERT(ctx);
16458 NK_ASSERT(ctx->current);
16459 if (!ctx || !ctx->current) return nk_vec2(0,0);
16460 return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
16461 }
16462 NK_API float
16463 nk_window_get_width(const struct nk_context *ctx)
16464 {
16465 NK_ASSERT(ctx);
16466 NK_ASSERT(ctx->current);
16467 if (!ctx || !ctx->current) return 0;
16468 return ctx->current->bounds.w;
16469 }
16470 NK_API float
16471 nk_window_get_height(const struct nk_context *ctx)
16472 {
16473 NK_ASSERT(ctx);
16474 NK_ASSERT(ctx->current);
16475 if (!ctx || !ctx->current) return 0;
16476 return ctx->current->bounds.h;
16477 }
16478 NK_API struct nk_rect
16479 nk_window_get_content_region(struct nk_context *ctx)
16480 {
16481 NK_ASSERT(ctx);
16482 NK_ASSERT(ctx->current);
16483 if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
16484 return ctx->current->layout->clip;
16485 }
16486 NK_API struct nk_vec2
16487 nk_window_get_content_region_min(struct nk_context *ctx)
16488 {
16489 NK_ASSERT(ctx);
16490 NK_ASSERT(ctx->current);
16491 NK_ASSERT(ctx->current->layout);
16492 if (!ctx || !ctx->current) return nk_vec2(0,0);
16493 return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
16494 }
16495 NK_API struct nk_vec2
16496 nk_window_get_content_region_max(struct nk_context *ctx)
16497 {
16498 NK_ASSERT(ctx);
16499 NK_ASSERT(ctx->current);
16500 NK_ASSERT(ctx->current->layout);
16501 if (!ctx || !ctx->current) return nk_vec2(0,0);
16502 return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
16503 ctx->current->layout->clip.y + ctx->current->layout->clip.h);
16504 }
16505 NK_API struct nk_vec2
16506 nk_window_get_content_region_size(struct nk_context *ctx)
16507 {
16508 NK_ASSERT(ctx);
16509 NK_ASSERT(ctx->current);
16510 NK_ASSERT(ctx->current->layout);
16511 if (!ctx || !ctx->current) return nk_vec2(0,0);
16512 return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
16513 }
16514 NK_API struct nk_command_buffer*
16515 nk_window_get_canvas(struct nk_context *ctx)
16516 {
16517 NK_ASSERT(ctx);
16518 NK_ASSERT(ctx->current);
16519 NK_ASSERT(ctx->current->layout);
16520 if (!ctx || !ctx->current) return 0;
16521 return &ctx->current->buffer;
16522 }
16523 NK_API struct nk_panel*
16524 nk_window_get_panel(struct nk_context *ctx)
16525 {
16526 NK_ASSERT(ctx);
16527 NK_ASSERT(ctx->current);
16528 if (!ctx || !ctx->current) return 0;
16529 return ctx->current->layout;
16530 }
16531 NK_API void
16532 nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
16533 {
16534 struct nk_window *win;
16535 NK_ASSERT(ctx);
16536 NK_ASSERT(ctx->current);
16537 if (!ctx || !ctx->current)
16538 return ;
16539 win = ctx->current;
16540 if (offset_x)
16541 *offset_x = win->scrollbar.x;
16542 if (offset_y)
16543 *offset_y = win->scrollbar.y;
16544 }
16545 NK_API int
16546 nk_window_has_focus(const struct nk_context *ctx)
16547 {
16548 NK_ASSERT(ctx);
16549 NK_ASSERT(ctx->current);
16550 NK_ASSERT(ctx->current->layout);
16551 if (!ctx || !ctx->current) return 0;
16552 return ctx->current == ctx->active;
16553 }
16554 NK_API int
16555 nk_window_is_hovered(struct nk_context *ctx)
16556 {
16557 NK_ASSERT(ctx);
16558 NK_ASSERT(ctx->current);
16559 if (!ctx || !ctx->current) return 0;
16560 if(ctx->current->flags & NK_WINDOW_HIDDEN)
16561 return 0;
16562 return nk_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds);
16563 }
16564 NK_API int
16565 nk_window_is_any_hovered(struct nk_context *ctx)
16566 {
16567 struct nk_window *iter;
16568 NK_ASSERT(ctx);
16569 if (!ctx) return 0;
16570 iter = ctx->begin;
16571 while (iter) {
16572 /* check if window is being hovered */
16573 if(!(iter->flags & NK_WINDOW_HIDDEN)) {
16574 /* check if window popup is being hovered */
16575 if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
16576 return 1;
16577
16578 if (iter->flags & NK_WINDOW_MINIMIZED) {
16579 struct nk_rect header = iter->bounds;
16580 header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
16581 if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
16582 return 1;
16583 } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
16584 return 1;
16585 }
16586 }
16587 iter = iter->next;
16588 }
16589 return 0;
16590 }
16591 NK_API int
16592 nk_item_is_any_active(struct nk_context *ctx)
16593 {
16594 int any_hovered = nk_window_is_any_hovered(ctx);
16595 int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
16596 return any_hovered || any_active;
16597 }
16598 NK_API int
16599 nk_window_is_collapsed(struct nk_context *ctx, const char *name)
16600 {
16601 int title_len;
16602 nk_hash title_hash;
16603 struct nk_window *win;
16604 NK_ASSERT(ctx);
16605 if (!ctx) return 0;
16606
16607 title_len = (int)nk_strlen(name);
16608 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16609 win = nk_find_window(ctx, title_hash, name);
16610 if (!win) return 0;
16611 return win->flags & NK_WINDOW_MINIMIZED;
16612 }
16613 NK_API int
16614 nk_window_is_closed(struct nk_context *ctx, const char *name)
16615 {
16616 int title_len;
16617 nk_hash title_hash;
16618 struct nk_window *win;
16619 NK_ASSERT(ctx);
16620 if (!ctx) return 1;
16621
16622 title_len = (int)nk_strlen(name);
16623 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16624 win = nk_find_window(ctx, title_hash, name);
16625 if (!win) return 1;
16626 return (win->flags & NK_WINDOW_CLOSED);
16627 }
16628 NK_API int
16629 nk_window_is_hidden(struct nk_context *ctx, const char *name)
16630 {
16631 int title_len;
16632 nk_hash title_hash;
16633 struct nk_window *win;
16634 NK_ASSERT(ctx);
16635 if (!ctx) return 1;
16636
16637 title_len = (int)nk_strlen(name);
16638 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16639 win = nk_find_window(ctx, title_hash, name);
16640 if (!win) return 1;
16641 return (win->flags & NK_WINDOW_HIDDEN);
16642 }
16643 NK_API int
16644 nk_window_is_active(struct nk_context *ctx, const char *name)
16645 {
16646 int title_len;
16647 nk_hash title_hash;
16648 struct nk_window *win;
16649 NK_ASSERT(ctx);
16650 if (!ctx) return 0;
16651
16652 title_len = (int)nk_strlen(name);
16653 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16654 win = nk_find_window(ctx, title_hash, name);
16655 if (!win) return 0;
16656 return win == ctx->active;
16657 }
16658 NK_API struct nk_window*
16659 nk_window_find(struct nk_context *ctx, const char *name)
16660 {
16661 int title_len;
16662 nk_hash title_hash;
16663 title_len = (int)nk_strlen(name);
16664 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16665 return nk_find_window(ctx, title_hash, name);
16666 }
16667 NK_API void
16668 nk_window_close(struct nk_context *ctx, const char *name)
16669 {
16670 struct nk_window *win;
16671 NK_ASSERT(ctx);
16672 if (!ctx) return;
16673 win = nk_window_find(ctx, name);
16674 if (!win) return;
16675 NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
16676 if (ctx->current == win) return;
16677 win->flags |= NK_WINDOW_HIDDEN;
16678 win->flags |= NK_WINDOW_CLOSED;
16679 }
16680 NK_API void
16681 nk_window_set_bounds(struct nk_context *ctx,
16682 const char *name, struct nk_rect bounds)
16683 {
16684 struct nk_window *win;
16685 NK_ASSERT(ctx);
16686 if (!ctx) return;
16687 win = nk_window_find(ctx, name);
16688 if (!win) return;
16689 NK_ASSERT(ctx->current != win && "You cannot update a currently in procecss window");
16690 win->bounds = bounds;
16691 }
16692 NK_API void
16693 nk_window_set_position(struct nk_context *ctx,
16694 const char *name, struct nk_vec2 pos)
16695 {
16696 struct nk_window *win = nk_window_find(ctx, name);
16697 if (!win) return;
16698 win->bounds.x = pos.x;
16699 win->bounds.y = pos.y;
16700 }
16701 NK_API void
16702 nk_window_set_size(struct nk_context *ctx,
16703 const char *name, struct nk_vec2 size)
16704 {
16705 struct nk_window *win = nk_window_find(ctx, name);
16706 if (!win) return;
16707 win->bounds.w = size.x;
16708 win->bounds.h = size.y;
16709 }
16710 NK_API void
16711 nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
16712 {
16713 struct nk_window *win;
16714 NK_ASSERT(ctx);
16715 NK_ASSERT(ctx->current);
16716 if (!ctx || !ctx->current)
16717 return;
16718 win = ctx->current;
16719 win->scrollbar.x = offset_x;
16720 win->scrollbar.y = offset_y;
16721 }
16722 NK_API void
16723 nk_window_collapse(struct nk_context *ctx, const char *name,
16724 enum nk_collapse_states c)
16725 {
16726 int title_len;
16727 nk_hash title_hash;
16728 struct nk_window *win;
16729 NK_ASSERT(ctx);
16730 if (!ctx) return;
16731
16732 title_len = (int)nk_strlen(name);
16733 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16734 win = nk_find_window(ctx, title_hash, name);
16735 if (!win) return;
16736 if (c == NK_MINIMIZED)
16737 win->flags |= NK_WINDOW_MINIMIZED;
16738 else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
16739 }
16740 NK_API void
16741 nk_window_collapse_if(struct nk_context *ctx, const char *name,
16742 enum nk_collapse_states c, int cond)
16743 {
16744 NK_ASSERT(ctx);
16745 if (!ctx || !cond) return;
16746 nk_window_collapse(ctx, name, c);
16747 }
16748 NK_API void
16749 nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
16750 {
16751 int title_len;
16752 nk_hash title_hash;
16753 struct nk_window *win;
16754 NK_ASSERT(ctx);
16755 if (!ctx) return;
16756
16757 title_len = (int)nk_strlen(name);
16758 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16759 win = nk_find_window(ctx, title_hash, name);
16760 if (!win) return;
16761 if (s == NK_HIDDEN) {
16762 win->flags |= NK_WINDOW_HIDDEN;
16763 } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
16764 }
16765 NK_API void
16766 nk_window_show_if(struct nk_context *ctx, const char *name,
16767 enum nk_show_states s, int cond)
16768 {
16769 NK_ASSERT(ctx);
16770 if (!ctx || !cond) return;
16771 nk_window_show(ctx, name, s);
16772 }
16773
16774 NK_API void
16775 nk_window_set_focus(struct nk_context *ctx, const char *name)
16776 {
16777 int title_len;
16778 nk_hash title_hash;
16779 struct nk_window *win;
16780 NK_ASSERT(ctx);
16781 if (!ctx) return;
16782
16783 title_len = (int)nk_strlen(name);
16784 title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
16785 win = nk_find_window(ctx, title_hash, name);
16786 if (win && ctx->end != win) {
16787 nk_remove_window(ctx, win);
16788 nk_insert_window(ctx, win, NK_INSERT_BACK);
16789 }
16790 ctx->active = win;
16791 }
16792
16793
16794
16795
16796 /* ===============================================================
16797 *
16798 * POPUP
16799 *
16800 * ===============================================================*/
16801 NK_API int
16802 nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
16803 const char *title, nk_flags flags, struct nk_rect rect)
16804 {
16805 struct nk_window *popup;
16806 struct nk_window *win;
16807 struct nk_panel *panel;
16808
16809 int title_len;
16810 nk_hash title_hash;
16811 nk_size allocated;
16812
16813 NK_ASSERT(ctx);
16814 NK_ASSERT(title);
16815 NK_ASSERT(ctx->current);
16816 NK_ASSERT(ctx->current->layout);
16817 if (!ctx || !ctx->current || !ctx->current->layout)
16818 return 0;
16819
16820 win = ctx->current;
16821 panel = win->layout;
16822 NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
16823 (void)panel;
16824 title_len = (int)nk_strlen(title);
16825 title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
16826
16827 popup = win->popup.win;
16828 if (!popup) {
16829 popup = (struct nk_window*)nk_create_window(ctx);
16830 popup->parent = win;
16831 win->popup.win = popup;
16832 win->popup.active = 0;
16833 win->popup.type = NK_PANEL_POPUP;
16834 }
16835
16836 /* make sure we have correct popup */
16837 if (win->popup.name != title_hash) {
16838 if (!win->popup.active) {
16839 nk_zero(popup, sizeof(*popup));
16840 win->popup.name = title_hash;
16841 win->popup.active = 1;
16842 win->popup.type = NK_PANEL_POPUP;
16843 } else return 0;
16844 }
16845
16846 /* popup position is local to window */
16847 ctx->current = popup;
16848 rect.x += win->layout->clip.x;
16849 rect.y += win->layout->clip.y;
16850
16851 /* setup popup data */
16852 popup->parent = win;
16853 popup->bounds = rect;
16854 popup->seq = ctx->seq;
16855 popup->layout = (struct nk_panel*)nk_create_panel(ctx);
16856 popup->flags = flags;
16857 popup->flags |= NK_WINDOW_BORDER;
16858 if (type == NK_POPUP_DYNAMIC)
16859 popup->flags |= NK_WINDOW_DYNAMIC;
16860
16861 popup->buffer = win->buffer;
16862 nk_start_popup(ctx, win);
16863 allocated = ctx->memory.allocated;
16864 nk_push_scissor(&popup->buffer, nk_null_rect);
16865
16866 if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
16867 /* popup is running therefore invalidate parent panels */
16868 struct nk_panel *root;
16869 root = win->layout;
16870 while (root) {
16871 root->flags |= NK_WINDOW_ROM;
16872 root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
16873 root = root->parent;
16874 }
16875 win->popup.active = 1;
16876 popup->layout->offset_x = &popup->scrollbar.x;
16877 popup->layout->offset_y = &popup->scrollbar.y;
16878 popup->layout->parent = win->layout;
16879 return 1;
16880 } else {
16881 /* popup was closed/is invalid so cleanup */
16882 struct nk_panel *root;
16883 root = win->layout;
16884 while (root) {
16885 root->flags |= NK_WINDOW_REMOVE_ROM;
16886 root = root->parent;
16887 }
16888 win->popup.buf.active = 0;
16889 win->popup.active = 0;
16890 ctx->memory.allocated = allocated;
16891 ctx->current = win;
16892 nk_free_panel(ctx, popup->layout);
16893 popup->layout = 0;
16894 return 0;
16895 }
16896 }
16897 NK_LIB int
16898 nk_nonblock_begin(struct nk_context *ctx,
16899 nk_flags flags, struct nk_rect body, struct nk_rect header,
16900 enum nk_panel_type panel_type)
16901 {
16902 struct nk_window *popup;
16903 struct nk_window *win;
16904 struct nk_panel *panel;
16905 int is_active = nk_true;
16906
16907 NK_ASSERT(ctx);
16908 NK_ASSERT(ctx->current);
16909 NK_ASSERT(ctx->current->layout);
16910 if (!ctx || !ctx->current || !ctx->current->layout)
16911 return 0;
16912
16913 /* popups cannot have popups */
16914 win = ctx->current;
16915 panel = win->layout;
16916 NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
16917 (void)panel;
16918 popup = win->popup.win;
16919 if (!popup) {
16920 /* create window for nonblocking popup */
16921 popup = (struct nk_window*)nk_create_window(ctx);
16922 popup->parent = win;
16923 win->popup.win = popup;
16924 win->popup.type = panel_type;
16925 nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
16926 } else {
16927 /* close the popup if user pressed outside or in the header */
16928 int pressed, in_body, in_header;
16929 #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
16930 pressed = nk_input_is_mouse_released(&ctx->input, NK_BUTTON_LEFT);
16931 #else
16932 pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
16933 #endif
16934 in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
16935 in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
16936 if (pressed && (!in_body || in_header))
16937 is_active = nk_false;
16938 }
16939 win->popup.header = header;
16940
16941 if (!is_active) {
16942 /* remove read only mode from all parent panels */
16943 struct nk_panel *root = win->layout;
16944 while (root) {
16945 root->flags |= NK_WINDOW_REMOVE_ROM;
16946 root = root->parent;
16947 }
16948 return is_active;
16949 }
16950 popup->bounds = body;
16951 popup->parent = win;
16952 popup->layout = (struct nk_panel*)nk_create_panel(ctx);
16953 popup->flags = flags;
16954 popup->flags |= NK_WINDOW_BORDER;
16955 popup->flags |= NK_WINDOW_DYNAMIC;
16956 popup->seq = ctx->seq;
16957 win->popup.active = 1;
16958 NK_ASSERT(popup->layout);
16959
16960 nk_start_popup(ctx, win);
16961 popup->buffer = win->buffer;
16962 nk_push_scissor(&popup->buffer, nk_null_rect);
16963 ctx->current = popup;
16964
16965 nk_panel_begin(ctx, 0, panel_type);
16966 win->buffer = popup->buffer;
16967 popup->layout->parent = win->layout;
16968 popup->layout->offset_x = &popup->scrollbar.x;
16969 popup->layout->offset_y = &popup->scrollbar.y;
16970
16971 /* set read only mode to all parent panels */
16972 {struct nk_panel *root;
16973 root = win->layout;
16974 while (root) {
16975 root->flags |= NK_WINDOW_ROM;
16976 root = root->parent;
16977 }}
16978 return is_active;
16979 }
16980 NK_API void
16981 nk_popup_close(struct nk_context *ctx)
16982 {
16983 struct nk_window *popup;
16984 NK_ASSERT(ctx);
16985 if (!ctx || !ctx->current) return;
16986
16987 popup = ctx->current;
16988 NK_ASSERT(popup->parent);
16989 NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
16990 popup->flags |= NK_WINDOW_HIDDEN;
16991 }
16992 NK_API void
16993 nk_popup_end(struct nk_context *ctx)
16994 {
16995 struct nk_window *win;
16996 struct nk_window *popup;
16997
16998 NK_ASSERT(ctx);
16999 NK_ASSERT(ctx->current);
17000 NK_ASSERT(ctx->current->layout);
17001 if (!ctx || !ctx->current || !ctx->current->layout)
17002 return;
17003
17004 popup = ctx->current;
17005 if (!popup->parent) return;
17006 win = popup->parent;
17007 if (popup->flags & NK_WINDOW_HIDDEN) {
17008 struct nk_panel *root;
17009 root = win->layout;
17010 while (root) {
17011 root->flags |= NK_WINDOW_REMOVE_ROM;
17012 root = root->parent;
17013 }
17014 win->popup.active = 0;
17015 }
17016 nk_push_scissor(&popup->buffer, nk_null_rect);
17017 nk_end(ctx);
17018
17019 win->buffer = popup->buffer;
17020 nk_finish_popup(ctx, win);
17021 ctx->current = win;
17022 nk_push_scissor(&win->buffer, win->layout->clip);
17023 }
17024 NK_API void
17025 nk_popup_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
17026 {
17027 struct nk_window *popup;
17028
17029 NK_ASSERT(ctx);
17030 NK_ASSERT(ctx->current);
17031 NK_ASSERT(ctx->current->layout);
17032 if (!ctx || !ctx->current || !ctx->current->layout)
17033 return;
17034
17035 popup = ctx->current;
17036 if (offset_x)
17037 *offset_x = popup->scrollbar.x;
17038 if (offset_y)
17039 *offset_y = popup->scrollbar.y;
17040 }
17041 NK_API void
17042 nk_popup_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
17043 {
17044 struct nk_window *popup;
17045
17046 NK_ASSERT(ctx);
17047 NK_ASSERT(ctx->current);
17048 NK_ASSERT(ctx->current->layout);
17049 if (!ctx || !ctx->current || !ctx->current->layout)
17050 return;
17051
17052 popup = ctx->current;
17053 popup->scrollbar.x = offset_x;
17054 popup->scrollbar.y = offset_y;
17055 }
17056
17057
17058
17059
17060 /* ==============================================================
17061 *
17062 * CONTEXTUAL
17063 *
17064 * ===============================================================*/
17065 NK_API int
17066 nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
17067 struct nk_rect trigger_bounds)
17068 {
17069 struct nk_window *win;
17070 struct nk_window *popup;
17071 struct nk_rect body;
17072
17073 NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
17074 int is_clicked = 0;
17075 int is_open = 0;
17076 int ret = 0;
17077
17078 NK_ASSERT(ctx);
17079 NK_ASSERT(ctx->current);
17080 NK_ASSERT(ctx->current->layout);
17081 if (!ctx || !ctx->current || !ctx->current->layout)
17082 return 0;
17083
17084 win = ctx->current;
17085 ++win->popup.con_count;
17086 if (ctx->current != ctx->active)
17087 return 0;
17088
17089 /* check if currently active contextual is active */
17090 popup = win->popup.win;
17091 is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
17092 is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
17093 if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
17094 return 0;
17095 if (!is_open && win->popup.active_con)
17096 win->popup.active_con = 0;
17097 if ((!is_open && !is_clicked))
17098 return 0;
17099
17100 /* calculate contextual position on click */
17101 win->popup.active_con = win->popup.con_count;
17102 if (is_clicked) {
17103 body.x = ctx->input.mouse.pos.x;
17104 body.y = ctx->input.mouse.pos.y;
17105 } else {
17106 body.x = popup->bounds.x;
17107 body.y = popup->bounds.y;
17108 }
17109 body.w = size.x;
17110 body.h = size.y;
17111
17112 /* start nonblocking contextual popup */
17113 ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
17114 null_rect, NK_PANEL_CONTEXTUAL);
17115 if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
17116 else {
17117 win->popup.active_con = 0;
17118 win->popup.type = NK_PANEL_NONE;
17119 if (win->popup.win)
17120 win->popup.win->flags = 0;
17121 }
17122 return ret;
17123 }
17124 NK_API int
17125 nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
17126 nk_flags alignment)
17127 {
17128 struct nk_window *win;
17129 const struct nk_input *in;
17130 const struct nk_style *style;
17131
17132 struct nk_rect bounds;
17133 enum nk_widget_layout_states state;
17134
17135 NK_ASSERT(ctx);
17136 NK_ASSERT(ctx->current);
17137 NK_ASSERT(ctx->current->layout);
17138 if (!ctx || !ctx->current || !ctx->current->layout)
17139 return 0;
17140
17141 win = ctx->current;
17142 style = &ctx->style;
17143 state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
17144 if (!state) return nk_false;
17145
17146 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17147 if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
17148 text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
17149 nk_contextual_close(ctx);
17150 return nk_true;
17151 }
17152 return nk_false;
17153 }
17154 NK_API int
17155 nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
17156 {
17157 return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
17158 }
17159 NK_API int
17160 nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
17161 const char *text, int len, nk_flags align)
17162 {
17163 struct nk_window *win;
17164 const struct nk_input *in;
17165 const struct nk_style *style;
17166
17167 struct nk_rect bounds;
17168 enum nk_widget_layout_states state;
17169
17170 NK_ASSERT(ctx);
17171 NK_ASSERT(ctx->current);
17172 NK_ASSERT(ctx->current->layout);
17173 if (!ctx || !ctx->current || !ctx->current->layout)
17174 return 0;
17175
17176 win = ctx->current;
17177 style = &ctx->style;
17178 state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
17179 if (!state) return nk_false;
17180
17181 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17182 if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
17183 img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
17184 nk_contextual_close(ctx);
17185 return nk_true;
17186 }
17187 return nk_false;
17188 }
17189 NK_API int
17190 nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
17191 const char *label, nk_flags align)
17192 {
17193 return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
17194 }
17195 NK_API int
17196 nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
17197 const char *text, int len, nk_flags align)
17198 {
17199 struct nk_window *win;
17200 const struct nk_input *in;
17201 const struct nk_style *style;
17202
17203 struct nk_rect bounds;
17204 enum nk_widget_layout_states state;
17205
17206 NK_ASSERT(ctx);
17207 NK_ASSERT(ctx->current);
17208 NK_ASSERT(ctx->current->layout);
17209 if (!ctx || !ctx->current || !ctx->current->layout)
17210 return 0;
17211
17212 win = ctx->current;
17213 style = &ctx->style;
17214 state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
17215 if (!state) return nk_false;
17216
17217 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17218 if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
17219 symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
17220 nk_contextual_close(ctx);
17221 return nk_true;
17222 }
17223 return nk_false;
17224 }
17225 NK_API int
17226 nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
17227 const char *text, nk_flags align)
17228 {
17229 return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
17230 }
17231 NK_API void
17232 nk_contextual_close(struct nk_context *ctx)
17233 {
17234 NK_ASSERT(ctx);
17235 NK_ASSERT(ctx->current);
17236 NK_ASSERT(ctx->current->layout);
17237 if (!ctx || !ctx->current || !ctx->current->layout) return;
17238 nk_popup_close(ctx);
17239 }
17240 NK_API void
17241 nk_contextual_end(struct nk_context *ctx)
17242 {
17243 struct nk_window *popup;
17244 struct nk_panel *panel;
17245 NK_ASSERT(ctx);
17246 NK_ASSERT(ctx->current);
17247 if (!ctx || !ctx->current) return;
17248
17249 popup = ctx->current;
17250 panel = popup->layout;
17251 NK_ASSERT(popup->parent);
17252 NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
17253 if (panel->flags & NK_WINDOW_DYNAMIC) {
17254 /* Close behavior
17255 This is a bit of a hack solution since we do not know before we end our popup
17256 how big it will be. We therefore do not directly know when a
17257 click outside the non-blocking popup must close it at that direct frame.
17258 Instead it will be closed in the next frame.*/
17259 struct nk_rect body = {0,0,0,0};
17260 if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
17261 struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
17262 body = panel->bounds;
17263 body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
17264 body.h = (panel->bounds.y + panel->bounds.h) - body.y;
17265 }
17266 {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
17267 int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
17268 if (pressed && in_body)
17269 popup->flags |= NK_WINDOW_HIDDEN;
17270 }
17271 }
17272 if (popup->flags & NK_WINDOW_HIDDEN)
17273 popup->seq = 0;
17274 nk_popup_end(ctx);
17275 return;
17276 }
17277
17278
17279
17280
17281
17282 /* ===============================================================
17283 *
17284 * MENU
17285 *
17286 * ===============================================================*/
17287 NK_API void
17288 nk_menubar_begin(struct nk_context *ctx)
17289 {
17290 struct nk_panel *layout;
17291 NK_ASSERT(ctx);
17292 NK_ASSERT(ctx->current);
17293 NK_ASSERT(ctx->current->layout);
17294 if (!ctx || !ctx->current || !ctx->current->layout)
17295 return;
17296
17297 layout = ctx->current->layout;
17298 NK_ASSERT(layout->at_y == layout->bounds.y);
17299 /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
17300 If you want a menubar the first nuklear function after `nk_begin` has to be a
17301 `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
17302 widgets (also supports multiple rows).
17303 Example:
17304 if (nk_begin(...)) {
17305 nk_menubar_begin(...);
17306 nk_layout_xxxx(...);
17307 nk_button(...);
17308 nk_layout_xxxx(...);
17309 nk_button(...);
17310 nk_menubar_end(...);
17311 }
17312 nk_end(...);
17313 */
17314 if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
17315 return;
17316
17317 layout->menu.x = layout->at_x;
17318 layout->menu.y = layout->at_y + layout->row.height;
17319 layout->menu.w = layout->bounds.w;
17320 layout->menu.offset.x = *layout->offset_x;
17321 layout->menu.offset.y = *layout->offset_y;
17322 *layout->offset_y = 0;
17323 }
17324 NK_API void
17325 nk_menubar_end(struct nk_context *ctx)
17326 {
17327 struct nk_window *win;
17328 struct nk_panel *layout;
17329 struct nk_command_buffer *out;
17330
17331 NK_ASSERT(ctx);
17332 NK_ASSERT(ctx->current);
17333 NK_ASSERT(ctx->current->layout);
17334 if (!ctx || !ctx->current || !ctx->current->layout)
17335 return;
17336
17337 win = ctx->current;
17338 out = &win->buffer;
17339 layout = win->layout;
17340 if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
17341 return;
17342
17343 layout->menu.h = layout->at_y - layout->menu.y;
17344 layout->bounds.y += layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
17345 layout->bounds.h -= layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
17346
17347 *layout->offset_x = layout->menu.offset.x;
17348 *layout->offset_y = layout->menu.offset.y;
17349 layout->at_y = layout->bounds.y - layout->row.height;
17350
17351 layout->clip.y = layout->bounds.y;
17352 layout->clip.h = layout->bounds.h;
17353 nk_push_scissor(out, layout->clip);
17354 }
17355 NK_INTERN int
17356 nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
17357 const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
17358 {
17359 int is_open = 0;
17360 int is_active = 0;
17361 struct nk_rect body;
17362 struct nk_window *popup;
17363 nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
17364
17365 NK_ASSERT(ctx);
17366 NK_ASSERT(ctx->current);
17367 NK_ASSERT(ctx->current->layout);
17368 if (!ctx || !ctx->current || !ctx->current->layout)
17369 return 0;
17370
17371 body.x = header.x;
17372 body.w = size.x;
17373 body.y = header.y + header.h;
17374 body.h = size.y;
17375
17376 popup = win->popup.win;
17377 is_open = popup ? nk_true : nk_false;
17378 is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
17379 if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
17380 (!is_open && !is_active && !is_clicked)) return 0;
17381 if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
17382 return 0;
17383
17384 win->popup.type = NK_PANEL_MENU;
17385 win->popup.name = hash;
17386 return 1;
17387 }
17388 NK_API int
17389 nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
17390 nk_flags align, struct nk_vec2 size)
17391 {
17392 struct nk_window *win;
17393 const struct nk_input *in;
17394 struct nk_rect header;
17395 int is_clicked = nk_false;
17396 nk_flags state;
17397
17398 NK_ASSERT(ctx);
17399 NK_ASSERT(ctx->current);
17400 NK_ASSERT(ctx->current->layout);
17401 if (!ctx || !ctx->current || !ctx->current->layout)
17402 return 0;
17403
17404 win = ctx->current;
17405 state = nk_widget(&header, ctx);
17406 if (!state) return 0;
17407 in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17408 if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
17409 title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
17410 is_clicked = nk_true;
17411 return nk_menu_begin(ctx, win, title, is_clicked, header, size);
17412 }
17413 NK_API int nk_menu_begin_label(struct nk_context *ctx,
17414 const char *text, nk_flags align, struct nk_vec2 size)
17415 {
17416 return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);
17417 }
17418 NK_API int
17419 nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
17420 struct nk_vec2 size)
17421 {
17422 struct nk_window *win;
17423 struct nk_rect header;
17424 const struct nk_input *in;
17425 int is_clicked = nk_false;
17426 nk_flags state;
17427
17428 NK_ASSERT(ctx);
17429 NK_ASSERT(ctx->current);
17430 NK_ASSERT(ctx->current->layout);
17431 if (!ctx || !ctx->current || !ctx->current->layout)
17432 return 0;
17433
17434 win = ctx->current;
17435 state = nk_widget(&header, ctx);
17436 if (!state) return 0;
17437 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17438 if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
17439 img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
17440 is_clicked = nk_true;
17441 return nk_menu_begin(ctx, win, id, is_clicked, header, size);
17442 }
17443 NK_API int
17444 nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
17445 enum nk_symbol_type sym, struct nk_vec2 size)
17446 {
17447 struct nk_window *win;
17448 const struct nk_input *in;
17449 struct nk_rect header;
17450 int is_clicked = nk_false;
17451 nk_flags state;
17452
17453 NK_ASSERT(ctx);
17454 NK_ASSERT(ctx->current);
17455 NK_ASSERT(ctx->current->layout);
17456 if (!ctx || !ctx->current || !ctx->current->layout)
17457 return 0;
17458
17459 win = ctx->current;
17460 state = nk_widget(&header, ctx);
17461 if (!state) return 0;
17462 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17463 if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
17464 sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
17465 is_clicked = nk_true;
17466 return nk_menu_begin(ctx, win, id, is_clicked, header, size);
17467 }
17468 NK_API int
17469 nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
17470 nk_flags align, struct nk_image img, struct nk_vec2 size)
17471 {
17472 struct nk_window *win;
17473 struct nk_rect header;
17474 const struct nk_input *in;
17475 int is_clicked = nk_false;
17476 nk_flags state;
17477
17478 NK_ASSERT(ctx);
17479 NK_ASSERT(ctx->current);
17480 NK_ASSERT(ctx->current->layout);
17481 if (!ctx || !ctx->current || !ctx->current->layout)
17482 return 0;
17483
17484 win = ctx->current;
17485 state = nk_widget(&header, ctx);
17486 if (!state) return 0;
17487 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17488 if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
17489 header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
17490 ctx->style.font, in))
17491 is_clicked = nk_true;
17492 return nk_menu_begin(ctx, win, title, is_clicked, header, size);
17493 }
17494 NK_API int
17495 nk_menu_begin_image_label(struct nk_context *ctx,
17496 const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
17497 {
17498 return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);
17499 }
17500 NK_API int
17501 nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
17502 nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
17503 {
17504 struct nk_window *win;
17505 struct nk_rect header;
17506 const struct nk_input *in;
17507 int is_clicked = nk_false;
17508 nk_flags state;
17509
17510 NK_ASSERT(ctx);
17511 NK_ASSERT(ctx->current);
17512 NK_ASSERT(ctx->current->layout);
17513 if (!ctx || !ctx->current || !ctx->current->layout)
17514 return 0;
17515
17516 win = ctx->current;
17517 state = nk_widget(&header, ctx);
17518 if (!state) return 0;
17519
17520 in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
17521 if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
17522 header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
17523 ctx->style.font, in)) is_clicked = nk_true;
17524 return nk_menu_begin(ctx, win, title, is_clicked, header, size);
17525 }
17526 NK_API int
17527 nk_menu_begin_symbol_label(struct nk_context *ctx,
17528 const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
17529 {
17530 return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);
17531 }
17532 NK_API int
17533 nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
17534 {
17535 return nk_contextual_item_text(ctx, title, len, align);
17536 }
17537 NK_API int
17538 nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
17539 {
17540 return nk_contextual_item_label(ctx, label, align);
17541 }
17542 NK_API int
17543 nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
17544 const char *label, nk_flags align)
17545 {
17546 return nk_contextual_item_image_label(ctx, img, label, align);
17547 }
17548 NK_API int
17549 nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
17550 const char *text, int len, nk_flags align)
17551 {
17552 return nk_contextual_item_image_text(ctx, img, text, len, align);
17553 }
17554 NK_API int nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
17555 const char *text, int len, nk_flags align)
17556 {
17557 return nk_contextual_item_symbol_text(ctx, sym, text, len, align);
17558 }
17559 NK_API int nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
17560 const char *label, nk_flags align)
17561 {
17562 return nk_contextual_item_symbol_label(ctx, sym, label, align);
17563 }
17564 NK_API void nk_menu_close(struct nk_context *ctx)
17565 {
17566 nk_contextual_close(ctx);
17567 }
17568 NK_API void
17569 nk_menu_end(struct nk_context *ctx)
17570 {
17571 nk_contextual_end(ctx);
17572 }
17573
17574
17575
17576
17577
17578 /* ===============================================================
17579 *
17580 * LAYOUT
17581 *
17582 * ===============================================================*/
17583 NK_API void
17584 nk_layout_set_min_row_height(struct nk_context *ctx, float height)
17585 {
17586 struct nk_window *win;
17587 struct nk_panel *layout;
17588
17589 NK_ASSERT(ctx);
17590 NK_ASSERT(ctx->current);
17591 NK_ASSERT(ctx->current->layout);
17592 if (!ctx || !ctx->current || !ctx->current->layout)
17593 return;
17594
17595 win = ctx->current;
17596 layout = win->layout;
17597 layout->row.min_height = height;
17598 }
17599 NK_API void
17600 nk_layout_reset_min_row_height(struct nk_context *ctx)
17601 {
17602 struct nk_window *win;
17603 struct nk_panel *layout;
17604
17605 NK_ASSERT(ctx);
17606 NK_ASSERT(ctx->current);
17607 NK_ASSERT(ctx->current->layout);
17608 if (!ctx || !ctx->current || !ctx->current->layout)
17609 return;
17610
17611 win = ctx->current;
17612 layout = win->layout;
17613 layout->row.min_height = ctx->style.font->height;
17614 layout->row.min_height += ctx->style.text.padding.y*2;
17615 layout->row.min_height += ctx->style.window.min_row_height_padding*2;
17616 }
17617 NK_LIB float
17618 nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
17619 float total_space, int columns)
17620 {
17621 float panel_padding;
17622 float panel_spacing;
17623 float panel_space;
17624
17625 struct nk_vec2 spacing;
17626 struct nk_vec2 padding;
17627
17628 spacing = style->window.spacing;
17629 padding = nk_panel_get_padding(style, type);
17630
17631 /* calculate the usable panel space */
17632 panel_padding = 2 * padding.x;
17633 panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
17634 panel_space = total_space - panel_padding - panel_spacing;
17635 return panel_space;
17636 }
17637 NK_LIB void
17638 nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
17639 float height, int cols)
17640 {
17641 struct nk_panel *layout;
17642 const struct nk_style *style;
17643 struct nk_command_buffer *out;
17644
17645 struct nk_vec2 item_spacing;
17646 struct nk_color color;
17647
17648 NK_ASSERT(ctx);
17649 NK_ASSERT(ctx->current);
17650 NK_ASSERT(ctx->current->layout);
17651 if (!ctx || !ctx->current || !ctx->current->layout)
17652 return;
17653
17654 /* prefetch some configuration data */
17655 layout = win->layout;
17656 style = &ctx->style;
17657 out = &win->buffer;
17658 color = style->window.background;
17659 item_spacing = style->window.spacing;
17660
17661 /* if one of these triggers you forgot to add an `if` condition around either
17662 a window, group, popup, combobox or contextual menu `begin` and `end` block.
17663 Example:
17664 if (nk_begin(...) {...} nk_end(...); or
17665 if (nk_group_begin(...) { nk_group_end(...);} */
17666 NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
17667 NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
17668 NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
17669
17670 /* update the current row and set the current row layout */
17671 layout->row.index = 0;
17672 layout->at_y += layout->row.height;
17673 layout->row.columns = cols;
17674 if (height == 0.0f)
17675 layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
17676 else layout->row.height = height + item_spacing.y;
17677
17678 layout->row.item_offset = 0;
17679 if (layout->flags & NK_WINDOW_DYNAMIC) {
17680 /* draw background for dynamic panels */
17681 struct nk_rect background;
17682 background.x = win->bounds.x;
17683 background.w = win->bounds.w;
17684 background.y = layout->at_y - 1.0f;
17685 background.h = layout->row.height + 1.0f;
17686 nk_fill_rect(out, background, 0, color);
17687 }
17688 }
17689 NK_LIB void
17690 nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
17691 float height, int cols, int width)
17692 {
17693 /* update the current row and set the current row layout */
17694 struct nk_window *win;
17695 NK_ASSERT(ctx);
17696 NK_ASSERT(ctx->current);
17697 NK_ASSERT(ctx->current->layout);
17698 if (!ctx || !ctx->current || !ctx->current->layout)
17699 return;
17700
17701 win = ctx->current;
17702 nk_panel_layout(ctx, win, height, cols);
17703 if (fmt == NK_DYNAMIC)
17704 win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
17705 else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
17706
17707 win->layout->row.ratio = 0;
17708 win->layout->row.filled = 0;
17709 win->layout->row.item_offset = 0;
17710 win->layout->row.item_width = (float)width;
17711 }
17712 NK_API float
17713 nk_layout_ratio_from_pixel(struct nk_context *ctx, float pixel_width)
17714 {
17715 struct nk_window *win;
17716 NK_ASSERT(ctx);
17717 NK_ASSERT(pixel_width);
17718 if (!ctx || !ctx->current || !ctx->current->layout) return 0;
17719 win = ctx->current;
17720 return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
17721 }
17722 NK_API void
17723 nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
17724 {
17725 nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
17726 }
17727 NK_API void
17728 nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
17729 {
17730 nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
17731 }
17732 NK_API void
17733 nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
17734 float row_height, int cols)
17735 {
17736 struct nk_window *win;
17737 struct nk_panel *layout;
17738
17739 NK_ASSERT(ctx);
17740 NK_ASSERT(ctx->current);
17741 NK_ASSERT(ctx->current->layout);
17742 if (!ctx || !ctx->current || !ctx->current->layout)
17743 return;
17744
17745 win = ctx->current;
17746 layout = win->layout;
17747 nk_panel_layout(ctx, win, row_height, cols);
17748 if (fmt == NK_DYNAMIC)
17749 layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
17750 else layout->row.type = NK_LAYOUT_STATIC_ROW;
17751
17752 layout->row.ratio = 0;
17753 layout->row.filled = 0;
17754 layout->row.item_width = 0;
17755 layout->row.item_offset = 0;
17756 layout->row.columns = cols;
17757 }
17758 NK_API void
17759 nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
17760 {
17761 struct nk_window *win;
17762 struct nk_panel *layout;
17763
17764 NK_ASSERT(ctx);
17765 NK_ASSERT(ctx->current);
17766 NK_ASSERT(ctx->current->layout);
17767 if (!ctx || !ctx->current || !ctx->current->layout)
17768 return;
17769
17770 win = ctx->current;
17771 layout = win->layout;
17772 NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
17773 if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
17774 return;
17775
17776 if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
17777 float ratio = ratio_or_width;
17778 if ((ratio + layout->row.filled) > 1.0f) return;
17779 if (ratio > 0.0f)
17780 layout->row.item_width = NK_SATURATE(ratio);
17781 else layout->row.item_width = 1.0f - layout->row.filled;
17782 } else layout->row.item_width = ratio_or_width;
17783 }
17784 NK_API void
17785 nk_layout_row_end(struct nk_context *ctx)
17786 {
17787 struct nk_window *win;
17788 struct nk_panel *layout;
17789
17790 NK_ASSERT(ctx);
17791 NK_ASSERT(ctx->current);
17792 NK_ASSERT(ctx->current->layout);
17793 if (!ctx || !ctx->current || !ctx->current->layout)
17794 return;
17795
17796 win = ctx->current;
17797 layout = win->layout;
17798 NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
17799 if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
17800 return;
17801 layout->row.item_width = 0;
17802 layout->row.item_offset = 0;
17803 }
17804 NK_API void
17805 nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
17806 float height, int cols, const float *ratio)
17807 {
17808 int i;
17809 int n_undef = 0;
17810 struct nk_window *win;
17811 struct nk_panel *layout;
17812
17813 NK_ASSERT(ctx);
17814 NK_ASSERT(ctx->current);
17815 NK_ASSERT(ctx->current->layout);
17816 if (!ctx || !ctx->current || !ctx->current->layout)
17817 return;
17818
17819 win = ctx->current;
17820 layout = win->layout;
17821 nk_panel_layout(ctx, win, height, cols);
17822 if (fmt == NK_DYNAMIC) {
17823 /* calculate width of undefined widget ratios */
17824 float r = 0;
17825 layout->row.ratio = ratio;
17826 for (i = 0; i < cols; ++i) {
17827 if (ratio[i] < 0.0f)
17828 n_undef++;
17829 else r += ratio[i];
17830 }
17831 r = NK_SATURATE(1.0f - r);
17832 layout->row.type = NK_LAYOUT_DYNAMIC;
17833 layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
17834 } else {
17835 layout->row.ratio = ratio;
17836 layout->row.type = NK_LAYOUT_STATIC;
17837 layout->row.item_width = 0;
17838 layout->row.item_offset = 0;
17839 }
17840 layout->row.item_offset = 0;
17841 layout->row.filled = 0;
17842 }
17843 NK_API void
17844 nk_layout_row_template_begin(struct nk_context *ctx, float height)
17845 {
17846 struct nk_window *win;
17847 struct nk_panel *layout;
17848
17849 NK_ASSERT(ctx);
17850 NK_ASSERT(ctx->current);
17851 NK_ASSERT(ctx->current->layout);
17852 if (!ctx || !ctx->current || !ctx->current->layout)
17853 return;
17854
17855 win = ctx->current;
17856 layout = win->layout;
17857 nk_panel_layout(ctx, win, height, 1);
17858 layout->row.type = NK_LAYOUT_TEMPLATE;
17859 layout->row.columns = 0;
17860 layout->row.ratio = 0;
17861 layout->row.item_width = 0;
17862 layout->row.item_height = 0;
17863 layout->row.item_offset = 0;
17864 layout->row.filled = 0;
17865 layout->row.item.x = 0;
17866 layout->row.item.y = 0;
17867 layout->row.item.w = 0;
17868 layout->row.item.h = 0;
17869 }
17870 NK_API void
17871 nk_layout_row_template_push_dynamic(struct nk_context *ctx)
17872 {
17873 struct nk_window *win;
17874 struct nk_panel *layout;
17875
17876 NK_ASSERT(ctx);
17877 NK_ASSERT(ctx->current);
17878 NK_ASSERT(ctx->current->layout);
17879 if (!ctx || !ctx->current || !ctx->current->layout)
17880 return;
17881
17882 win = ctx->current;
17883 layout = win->layout;
17884 NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
17885 NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
17886 if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
17887 if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
17888 layout->row.templates[layout->row.columns++] = -1.0f;
17889 }
17890 NK_API void
17891 nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
17892 {
17893 struct nk_window *win;
17894 struct nk_panel *layout;
17895
17896 NK_ASSERT(ctx);
17897 NK_ASSERT(ctx->current);
17898 NK_ASSERT(ctx->current->layout);
17899 if (!ctx || !ctx->current || !ctx->current->layout)
17900 return;
17901
17902 win = ctx->current;
17903 layout = win->layout;
17904 NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
17905 NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
17906 if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
17907 if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
17908 layout->row.templates[layout->row.columns++] = -min_width;
17909 }
17910 NK_API void
17911 nk_layout_row_template_push_static(struct nk_context *ctx, float width)
17912 {
17913 struct nk_window *win;
17914 struct nk_panel *layout;
17915
17916 NK_ASSERT(ctx);
17917 NK_ASSERT(ctx->current);
17918 NK_ASSERT(ctx->current->layout);
17919 if (!ctx || !ctx->current || !ctx->current->layout)
17920 return;
17921
17922 win = ctx->current;
17923 layout = win->layout;
17924 NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
17925 NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
17926 if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
17927 if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
17928 layout->row.templates[layout->row.columns++] = width;
17929 }
17930 NK_API void
17931 nk_layout_row_template_end(struct nk_context *ctx)
17932 {
17933 struct nk_window *win;
17934 struct nk_panel *layout;
17935
17936 int i = 0;
17937 int variable_count = 0;
17938 int min_variable_count = 0;
17939 float min_fixed_width = 0.0f;
17940 float total_fixed_width = 0.0f;
17941 float max_variable_width = 0.0f;
17942
17943 NK_ASSERT(ctx);
17944 NK_ASSERT(ctx->current);
17945 NK_ASSERT(ctx->current->layout);
17946 if (!ctx || !ctx->current || !ctx->current->layout)
17947 return;
17948
17949 win = ctx->current;
17950 layout = win->layout;
17951 NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
17952 if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
17953 for (i = 0; i < layout->row.columns; ++i) {
17954 float width = layout->row.templates[i];
17955 if (width >= 0.0f) {
17956 total_fixed_width += width;
17957 min_fixed_width += width;
17958 } else if (width < -1.0f) {
17959 width = -width;
17960 total_fixed_width += width;
17961 max_variable_width = NK_MAX(max_variable_width, width);
17962 variable_count++;
17963 } else {
17964 min_variable_count++;
17965 variable_count++;
17966 }
17967 }
17968 if (variable_count) {
17969 float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
17970 layout->bounds.w, layout->row.columns);
17971 float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
17972 int enough_space = var_width >= max_variable_width;
17973 if (!enough_space)
17974 var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
17975 for (i = 0; i < layout->row.columns; ++i) {
17976 float *width = &layout->row.templates[i];
17977 *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
17978 }
17979 }
17980 }
17981 NK_API void
17982 nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
17983 float height, int widget_count)
17984 {
17985 struct nk_window *win;
17986 struct nk_panel *layout;
17987
17988 NK_ASSERT(ctx);
17989 NK_ASSERT(ctx->current);
17990 NK_ASSERT(ctx->current->layout);
17991 if (!ctx || !ctx->current || !ctx->current->layout)
17992 return;
17993
17994 win = ctx->current;
17995 layout = win->layout;
17996 nk_panel_layout(ctx, win, height, widget_count);
17997 if (fmt == NK_STATIC)
17998 layout->row.type = NK_LAYOUT_STATIC_FREE;
17999 else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
18000
18001 layout->row.ratio = 0;
18002 layout->row.filled = 0;
18003 layout->row.item_width = 0;
18004 layout->row.item_offset = 0;
18005 }
18006 NK_API void
18007 nk_layout_space_end(struct nk_context *ctx)
18008 {
18009 struct nk_window *win;
18010 struct nk_panel *layout;
18011
18012 NK_ASSERT(ctx);
18013 NK_ASSERT(ctx->current);
18014 NK_ASSERT(ctx->current->layout);
18015 if (!ctx || !ctx->current || !ctx->current->layout)
18016 return;
18017
18018 win = ctx->current;
18019 layout = win->layout;
18020 layout->row.item_width = 0;
18021 layout->row.item_height = 0;
18022 layout->row.item_offset = 0;
18023 nk_zero(&layout->row.item, sizeof(layout->row.item));
18024 }
18025 NK_API void
18026 nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
18027 {
18028 struct nk_window *win;
18029 struct nk_panel *layout;
18030
18031 NK_ASSERT(ctx);
18032 NK_ASSERT(ctx->current);
18033 NK_ASSERT(ctx->current->layout);
18034 if (!ctx || !ctx->current || !ctx->current->layout)
18035 return;
18036
18037 win = ctx->current;
18038 layout = win->layout;
18039 layout->row.item = rect;
18040 }
18041 NK_API struct nk_rect
18042 nk_layout_space_bounds(struct nk_context *ctx)
18043 {
18044 struct nk_rect ret;
18045 struct nk_window *win;
18046 struct nk_panel *layout;
18047
18048 NK_ASSERT(ctx);
18049 NK_ASSERT(ctx->current);
18050 NK_ASSERT(ctx->current->layout);
18051 win = ctx->current;
18052 layout = win->layout;
18053
18054 ret.x = layout->clip.x;
18055 ret.y = layout->clip.y;
18056 ret.w = layout->clip.w;
18057 ret.h = layout->row.height;
18058 return ret;
18059 }
18060 NK_API struct nk_rect
18061 nk_layout_widget_bounds(struct nk_context *ctx)
18062 {
18063 struct nk_rect ret;
18064 struct nk_window *win;
18065 struct nk_panel *layout;
18066
18067 NK_ASSERT(ctx);
18068 NK_ASSERT(ctx->current);
18069 NK_ASSERT(ctx->current->layout);
18070 win = ctx->current;
18071 layout = win->layout;
18072
18073 ret.x = layout->at_x;
18074 ret.y = layout->at_y;
18075 ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
18076 ret.h = layout->row.height;
18077 return ret;
18078 }
18079 NK_API struct nk_vec2
18080 nk_layout_space_to_screen(struct nk_context *ctx, struct nk_vec2 ret)
18081 {
18082 struct nk_window *win;
18083 struct nk_panel *layout;
18084
18085 NK_ASSERT(ctx);
18086 NK_ASSERT(ctx->current);
18087 NK_ASSERT(ctx->current->layout);
18088 win = ctx->current;
18089 layout = win->layout;
18090
18091 ret.x += layout->at_x - (float)*layout->offset_x;
18092 ret.y += layout->at_y - (float)*layout->offset_y;
18093 return ret;
18094 }
18095 NK_API struct nk_vec2
18096 nk_layout_space_to_local(struct nk_context *ctx, struct nk_vec2 ret)
18097 {
18098 struct nk_window *win;
18099 struct nk_panel *layout;
18100
18101 NK_ASSERT(ctx);
18102 NK_ASSERT(ctx->current);
18103 NK_ASSERT(ctx->current->layout);
18104 win = ctx->current;
18105 layout = win->layout;
18106
18107 ret.x += -layout->at_x + (float)*layout->offset_x;
18108 ret.y += -layout->at_y + (float)*layout->offset_y;
18109 return ret;
18110 }
18111 NK_API struct nk_rect
18112 nk_layout_space_rect_to_screen(struct nk_context *ctx, struct nk_rect ret)
18113 {
18114 struct nk_window *win;
18115 struct nk_panel *layout;
18116
18117 NK_ASSERT(ctx);
18118 NK_ASSERT(ctx->current);
18119 NK_ASSERT(ctx->current->layout);
18120 win = ctx->current;
18121 layout = win->layout;
18122
18123 ret.x += layout->at_x - (float)*layout->offset_x;
18124 ret.y += layout->at_y - (float)*layout->offset_y;
18125 return ret;
18126 }
18127 NK_API struct nk_rect
18128 nk_layout_space_rect_to_local(struct nk_context *ctx, struct nk_rect ret)
18129 {
18130 struct nk_window *win;
18131 struct nk_panel *layout;
18132
18133 NK_ASSERT(ctx);
18134 NK_ASSERT(ctx->current);
18135 NK_ASSERT(ctx->current->layout);
18136 win = ctx->current;
18137 layout = win->layout;
18138
18139 ret.x += -layout->at_x + (float)*layout->offset_x;
18140 ret.y += -layout->at_y + (float)*layout->offset_y;
18141 return ret;
18142 }
18143 NK_LIB void
18144 nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
18145 {
18146 struct nk_panel *layout = win->layout;
18147 struct nk_vec2 spacing = ctx->style.window.spacing;
18148 const float row_height = layout->row.height - spacing.y;
18149 nk_panel_layout(ctx, win, row_height, layout->row.columns);
18150 }
18151 NK_LIB void
18152 nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
18153 struct nk_window *win, int modify)
18154 {
18155 struct nk_panel *layout;
18156 const struct nk_style *style;
18157
18158 struct nk_vec2 spacing;
18159 struct nk_vec2 padding;
18160
18161 float item_offset = 0;
18162 float item_width = 0;
18163 float item_spacing = 0;
18164 float panel_space = 0;
18165
18166 NK_ASSERT(ctx);
18167 NK_ASSERT(ctx->current);
18168 NK_ASSERT(ctx->current->layout);
18169 if (!ctx || !ctx->current || !ctx->current->layout)
18170 return;
18171
18172 win = ctx->current;
18173 layout = win->layout;
18174 style = &ctx->style;
18175 NK_ASSERT(bounds);
18176
18177 spacing = style->window.spacing;
18178 padding = nk_panel_get_padding(style, layout->type);
18179 panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
18180 layout->bounds.w, layout->row.columns);
18181
18182 #define NK_FRAC(x) (x - (int)x) /* will be used to remove fookin gaps */
18183 /* calculate the width of one item inside the current layout space */
18184 switch (layout->row.type) {
18185 case NK_LAYOUT_DYNAMIC_FIXED: {
18186 /* scaling fixed size widgets item width */
18187 float w = NK_MAX(1.0f,panel_space) / (float)layout->row.columns;
18188 item_offset = (float)layout->row.index * w;
18189 item_width = w + NK_FRAC(item_offset);
18190 item_spacing = (float)layout->row.index * spacing.x;
18191 } break;
18192 case NK_LAYOUT_DYNAMIC_ROW: {
18193 /* scaling single ratio widget width */
18194 float w = layout->row.item_width * panel_space;
18195 item_offset = layout->row.item_offset;
18196 item_width = w + NK_FRAC(item_offset);
18197 item_spacing = 0;
18198
18199 if (modify) {
18200 layout->row.item_offset += w + spacing.x;
18201 layout->row.filled += layout->row.item_width;
18202 layout->row.index = 0;
18203 }
18204 } break;
18205 case NK_LAYOUT_DYNAMIC_FREE: {
18206 /* panel width depended free widget placing */
18207 bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
18208 bounds->x -= (float)*layout->offset_x;
18209 bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
18210 bounds->y -= (float)*layout->offset_y;
18211 bounds->w = layout->bounds.w * layout->row.item.w + NK_FRAC(bounds->x);
18212 bounds->h = layout->row.height * layout->row.item.h + NK_FRAC(bounds->y);
18213 return;
18214 }
18215 case NK_LAYOUT_DYNAMIC: {
18216 /* scaling arrays of panel width ratios for every widget */
18217 float ratio, w;
18218 NK_ASSERT(layout->row.ratio);
18219 ratio = (layout->row.ratio[layout->row.index] < 0) ?
18220 layout->row.item_width : layout->row.ratio[layout->row.index];
18221
18222 w = (ratio * panel_space);
18223 item_spacing = (float)layout->row.index * spacing.x;
18224 item_offset = layout->row.item_offset;
18225 item_width = w + NK_FRAC(item_offset);
18226
18227 if (modify) {
18228 layout->row.item_offset += w;
18229 layout->row.filled += ratio;
18230 }
18231 } break;
18232 case NK_LAYOUT_STATIC_FIXED: {
18233 /* non-scaling fixed widgets item width */
18234 item_width = layout->row.item_width;
18235 item_offset = (float)layout->row.index * item_width;
18236 item_spacing = (float)layout->row.index * spacing.x;
18237 } break;
18238 case NK_LAYOUT_STATIC_ROW: {
18239 /* scaling single ratio widget width */
18240 item_width = layout->row.item_width;
18241 item_offset = layout->row.item_offset;
18242 item_spacing = (float)layout->row.index * spacing.x;
18243 if (modify) layout->row.item_offset += item_width;
18244 } break;
18245 case NK_LAYOUT_STATIC_FREE: {
18246 /* free widget placing */
18247 bounds->x = layout->at_x + layout->row.item.x;
18248 bounds->w = layout->row.item.w;
18249 if (((bounds->x + bounds->w) > layout->max_x) && modify)
18250 layout->max_x = (bounds->x + bounds->w);
18251 bounds->x -= (float)*layout->offset_x;
18252 bounds->y = layout->at_y + layout->row.item.y;
18253 bounds->y -= (float)*layout->offset_y;
18254 bounds->h = layout->row.item.h;
18255 return;
18256 }
18257 case NK_LAYOUT_STATIC: {
18258 /* non-scaling array of panel pixel width for every widget */
18259 item_spacing = (float)layout->row.index * spacing.x;
18260 item_width = layout->row.ratio[layout->row.index];
18261 item_offset = layout->row.item_offset;
18262 if (modify) layout->row.item_offset += item_width;
18263 } break;
18264 case NK_LAYOUT_TEMPLATE: {
18265 /* stretchy row layout with combined dynamic/static widget width*/
18266 float w;
18267 NK_ASSERT(layout->row.index < layout->row.columns);
18268 NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
18269 w = layout->row.templates[layout->row.index];
18270 item_offset = layout->row.item_offset;
18271 item_width = w + NK_FRAC(item_offset);
18272 item_spacing = (float)layout->row.index * spacing.x;
18273 if (modify) layout->row.item_offset += w;
18274 } break;
18275 #undef NK_FRAC
18276 default: NK_ASSERT(0); break;
18277 };
18278
18279 /* set the bounds of the newly allocated widget */
18280 bounds->w = item_width;
18281 bounds->h = layout->row.height - spacing.y;
18282 bounds->y = layout->at_y - (float)*layout->offset_y;
18283 bounds->x = layout->at_x + item_offset + item_spacing + padding.x;
18284 if (((bounds->x + bounds->w) > layout->max_x) && modify)
18285 layout->max_x = bounds->x + bounds->w;
18286 bounds->x -= (float)*layout->offset_x;
18287 }
18288 NK_LIB void
18289 nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
18290 {
18291 struct nk_window *win;
18292 struct nk_panel *layout;
18293
18294 NK_ASSERT(ctx);
18295 NK_ASSERT(ctx->current);
18296 NK_ASSERT(ctx->current->layout);
18297 if (!ctx || !ctx->current || !ctx->current->layout)
18298 return;
18299
18300 /* check if the end of the row has been hit and begin new row if so */
18301 win = ctx->current;
18302 layout = win->layout;
18303 if (layout->row.index >= layout->row.columns)
18304 nk_panel_alloc_row(ctx, win);
18305
18306 /* calculate widget position and size */
18307 nk_layout_widget_space(bounds, ctx, win, nk_true);
18308 layout->row.index++;
18309 }
18310 NK_LIB void
18311 nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx)
18312 {
18313 float y;
18314 int index;
18315 struct nk_window *win;
18316 struct nk_panel *layout;
18317
18318 NK_ASSERT(ctx);
18319 NK_ASSERT(ctx->current);
18320 NK_ASSERT(ctx->current->layout);
18321 if (!ctx || !ctx->current || !ctx->current->layout)
18322 return;
18323
18324 win = ctx->current;
18325 layout = win->layout;
18326 y = layout->at_y;
18327 index = layout->row.index;
18328 if (layout->row.index >= layout->row.columns) {
18329 layout->at_y += layout->row.height;
18330 layout->row.index = 0;
18331 }
18332 nk_layout_widget_space(bounds, ctx, win, nk_false);
18333 if (!layout->row.index) {
18334 bounds->x -= layout->row.item_offset;
18335 }
18336 layout->at_y = y;
18337 layout->row.index = index;
18338 }
18339
18340
18341
18342
18343
18344 /* ===============================================================
18345 *
18346 * TREE
18347 *
18348 * ===============================================================*/
18349 NK_INTERN int
18350 nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
18351 struct nk_image *img, const char *title, enum nk_collapse_states *state)
18352 {
18353 struct nk_window *win;
18354 struct nk_panel *layout;
18355 const struct nk_style *style;
18356 struct nk_command_buffer *out;
18357 const struct nk_input *in;
18358 const struct nk_style_button *button;
18359 enum nk_symbol_type symbol;
18360 float row_height;
18361
18362 struct nk_vec2 item_spacing;
18363 struct nk_rect header = {0,0,0,0};
18364 struct nk_rect sym = {0,0,0,0};
18365 struct nk_text text;
18366
18367 nk_flags ws = 0;
18368 enum nk_widget_layout_states widget_state;
18369
18370 NK_ASSERT(ctx);
18371 NK_ASSERT(ctx->current);
18372 NK_ASSERT(ctx->current->layout);
18373 if (!ctx || !ctx->current || !ctx->current->layout)
18374 return 0;
18375
18376 /* cache some data */
18377 win = ctx->current;
18378 layout = win->layout;
18379 out = &win->buffer;
18380 style = &ctx->style;
18381 item_spacing = style->window.spacing;
18382
18383 /* calculate header bounds and draw background */
18384 row_height = style->font->height + 2 * style->tab.padding.y;
18385 nk_layout_set_min_row_height(ctx, row_height);
18386 nk_layout_row_dynamic(ctx, row_height, 1);
18387 nk_layout_reset_min_row_height(ctx);
18388
18389 widget_state = nk_widget(&header, ctx);
18390 if (type == NK_TREE_TAB) {
18391 const struct nk_style_item *background = &style->tab.background;
18392 if (background->type == NK_STYLE_ITEM_IMAGE) {
18393 nk_draw_image(out, header, &background->data.image, nk_white);
18394 text.background = nk_rgba(0,0,0,0);
18395 } else {
18396 text.background = background->data.color;
18397 nk_fill_rect(out, header, 0, style->tab.border_color);
18398 nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
18399 style->tab.rounding, background->data.color);
18400 }
18401 } else text.background = style->window.background;
18402
18403 /* update node state */
18404 in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
18405 in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
18406 if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
18407 *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
18408
18409 /* select correct button style */
18410 if (*state == NK_MAXIMIZED) {
18411 symbol = style->tab.sym_maximize;
18412 if (type == NK_TREE_TAB)
18413 button = &style->tab.tab_maximize_button;
18414 else button = &style->tab.node_maximize_button;
18415 } else {
18416 symbol = style->tab.sym_minimize;
18417 if (type == NK_TREE_TAB)
18418 button = &style->tab.tab_minimize_button;
18419 else button = &style->tab.node_minimize_button;
18420 }
18421
18422 {/* draw triangle button */
18423 sym.w = sym.h = style->font->height;
18424 sym.y = header.y + style->tab.padding.y;
18425 sym.x = header.x + style->tab.padding.x;
18426 nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
18427 button, 0, style->font);
18428
18429 if (img) {
18430 /* draw optional image icon */
18431 sym.x = sym.x + sym.w + 4 * item_spacing.x;
18432 nk_draw_image(&win->buffer, sym, img, nk_white);
18433 sym.w = style->font->height + style->tab.spacing.x;}
18434 }
18435
18436 {/* draw label */
18437 struct nk_rect label;
18438 header.w = NK_MAX(header.w, sym.w + item_spacing.x);
18439 label.x = sym.x + sym.w + item_spacing.x;
18440 label.y = sym.y;
18441 label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
18442 label.h = style->font->height;
18443 text.text = style->tab.text;
18444 text.padding = nk_vec2(0,0);
18445 nk_widget_text(out, label, title, nk_strlen(title), &text,
18446 NK_TEXT_LEFT, style->font);}
18447
18448 /* increase x-axis cursor widget position pointer */
18449 if (*state == NK_MAXIMIZED) {
18450 layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
18451 layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
18452 layout->bounds.w -= (style->tab.indent + style->window.padding.x);
18453 layout->row.tree_depth++;
18454 return nk_true;
18455 } else return nk_false;
18456 }
18457 NK_INTERN int
18458 nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
18459 struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
18460 const char *hash, int len, int line)
18461 {
18462 struct nk_window *win = ctx->current;
18463 int title_len = 0;
18464 nk_hash tree_hash = 0;
18465 nk_uint *state = 0;
18466
18467 /* retrieve tree state from internal widget state tables */
18468 if (!hash) {
18469 title_len = (int)nk_strlen(title);
18470 tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
18471 } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
18472 state = nk_find_value(win, tree_hash);
18473 if (!state) {
18474 state = nk_add_value(ctx, win, tree_hash, 0);
18475 *state = initial_state;
18476 }
18477 return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
18478 }
18479 NK_API int
18480 nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
18481 const char *title, enum nk_collapse_states *state)
18482 {
18483 return nk_tree_state_base(ctx, type, 0, title, state);
18484 }
18485 NK_API int
18486 nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
18487 struct nk_image img, const char *title, enum nk_collapse_states *state)
18488 {
18489 return nk_tree_state_base(ctx, type, &img, title, state);
18490 }
18491 NK_API void
18492 nk_tree_state_pop(struct nk_context *ctx)
18493 {
18494 struct nk_window *win = 0;
18495 struct nk_panel *layout = 0;
18496
18497 NK_ASSERT(ctx);
18498 NK_ASSERT(ctx->current);
18499 NK_ASSERT(ctx->current->layout);
18500 if (!ctx || !ctx->current || !ctx->current->layout)
18501 return;
18502
18503 win = ctx->current;
18504 layout = win->layout;
18505 layout->at_x -= ctx->style.tab.indent + ctx->style.window.padding.x;
18506 layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
18507 NK_ASSERT(layout->row.tree_depth);
18508 layout->row.tree_depth--;
18509 }
18510 NK_API int
18511 nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
18512 const char *title, enum nk_collapse_states initial_state,
18513 const char *hash, int len, int line)
18514 {
18515 return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
18516 }
18517 NK_API int
18518 nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
18519 struct nk_image img, const char *title, enum nk_collapse_states initial_state,
18520 const char *hash, int len,int seed)
18521 {
18522 return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
18523 }
18524 NK_API void
18525 nk_tree_pop(struct nk_context *ctx)
18526 {
18527 nk_tree_state_pop(ctx);
18528 }
18529 NK_INTERN int
18530 nk_tree_element_image_push_hashed_base(struct nk_context *ctx, enum nk_tree_type type,
18531 struct nk_image *img, const char *title, int title_len,
18532 enum nk_collapse_states *state, int *selected)
18533 {
18534 struct nk_window *win;
18535 struct nk_panel *layout;
18536 const struct nk_style *style;
18537 struct nk_command_buffer *out;
18538 const struct nk_input *in;
18539 const struct nk_style_button *button;
18540 enum nk_symbol_type symbol;
18541 float row_height;
18542 struct nk_vec2 padding;
18543
18544 int text_len;
18545 float text_width;
18546
18547 struct nk_vec2 item_spacing;
18548 struct nk_rect header = {0,0,0,0};
18549 struct nk_rect sym = {0,0,0,0};
18550
18551 nk_flags ws = 0;
18552 enum nk_widget_layout_states widget_state;
18553
18554 NK_ASSERT(ctx);
18555 NK_ASSERT(ctx->current);
18556 NK_ASSERT(ctx->current->layout);
18557 if (!ctx || !ctx->current || !ctx->current->layout)
18558 return 0;
18559
18560 /* cache some data */
18561 win = ctx->current;
18562 layout = win->layout;
18563 out = &win->buffer;
18564 style = &ctx->style;
18565 item_spacing = style->window.spacing;
18566 padding = style->selectable.padding;
18567
18568 /* calculate header bounds and draw background */
18569 row_height = style->font->height + 2 * style->tab.padding.y;
18570 nk_layout_set_min_row_height(ctx, row_height);
18571 nk_layout_row_dynamic(ctx, row_height, 1);
18572 nk_layout_reset_min_row_height(ctx);
18573
18574 widget_state = nk_widget(&header, ctx);
18575 if (type == NK_TREE_TAB) {
18576 const struct nk_style_item *background = &style->tab.background;
18577 if (background->type == NK_STYLE_ITEM_IMAGE) {
18578 nk_draw_image(out, header, &background->data.image, nk_white);
18579 } else {
18580 nk_fill_rect(out, header, 0, style->tab.border_color);
18581 nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
18582 style->tab.rounding, background->data.color);
18583 }
18584 }
18585
18586 in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
18587 in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
18588
18589 /* select correct button style */
18590 if (*state == NK_MAXIMIZED) {
18591 symbol = style->tab.sym_maximize;
18592 if (type == NK_TREE_TAB)
18593 button = &style->tab.tab_maximize_button;
18594 else button = &style->tab.node_maximize_button;
18595 } else {
18596 symbol = style->tab.sym_minimize;
18597 if (type == NK_TREE_TAB)
18598 button = &style->tab.tab_minimize_button;
18599 else button = &style->tab.node_minimize_button;
18600 }
18601 {/* draw triangle button */
18602 sym.w = sym.h = style->font->height;
18603 sym.y = header.y + style->tab.padding.y;
18604 sym.x = header.x + style->tab.padding.x;
18605 if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
18606 *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;}
18607
18608 /* draw label */
18609 {nk_flags dummy = 0;
18610 struct nk_rect label;
18611 /* calculate size of the text and tooltip */
18612 text_len = nk_strlen(title);
18613 text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
18614 text_width += (4 * padding.x);
18615
18616 header.w = NK_MAX(header.w, sym.w + item_spacing.x);
18617 label.x = sym.x + sym.w + item_spacing.x;
18618 label.y = sym.y;
18619 label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
18620 label.h = style->font->height;
18621
18622 if (img) {
18623 nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
18624 selected, img, &style->selectable, in, style->font);
18625 } else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
18626 selected, &style->selectable, in, style->font);
18627 }
18628 /* increase x-axis cursor widget position pointer */
18629 if (*state == NK_MAXIMIZED) {
18630 layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
18631 layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
18632 layout->bounds.w -= (style->tab.indent + style->window.padding.x);
18633 layout->row.tree_depth++;
18634 return nk_true;
18635 } else return nk_false;
18636 }
18637 NK_INTERN int
18638 nk_tree_element_base(struct nk_context *ctx, enum nk_tree_type type,
18639 struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
18640 int *selected, const char *hash, int len, int line)
18641 {
18642 struct nk_window *win = ctx->current;
18643 int title_len = 0;
18644 nk_hash tree_hash = 0;
18645 nk_uint *state = 0;
18646
18647 /* retrieve tree state from internal widget state tables */
18648 if (!hash) {
18649 title_len = (int)nk_strlen(title);
18650 tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
18651 } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
18652 state = nk_find_value(win, tree_hash);
18653 if (!state) {
18654 state = nk_add_value(ctx, win, tree_hash, 0);
18655 *state = initial_state;
18656 } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
18657 nk_strlen(title), (enum nk_collapse_states*)state, selected);
18658 }
18659 NK_API int
18660 nk_tree_element_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
18661 const char *title, enum nk_collapse_states initial_state,
18662 int *selected, const char *hash, int len, int seed)
18663 {
18664 return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
18665 }
18666 NK_API int
18667 nk_tree_element_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
18668 struct nk_image img, const char *title, enum nk_collapse_states initial_state,
18669 int *selected, const char *hash, int len,int seed)
18670 {
18671 return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
18672 }
18673 NK_API void
18674 nk_tree_element_pop(struct nk_context *ctx)
18675 {
18676 nk_tree_state_pop(ctx);
18677 }
18678
18679
18680
18681
18682
18683 /* ===============================================================
18684 *
18685 * GROUP
18686 *
18687 * ===============================================================*/
18688 NK_API int
18689 nk_group_scrolled_offset_begin(struct nk_context *ctx,
18690 nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
18691 {
18692 struct nk_rect bounds;
18693 struct nk_window panel;
18694 struct nk_window *win;
18695
18696 win = ctx->current;
18697 nk_panel_alloc_space(&bounds, ctx);
18698 {const struct nk_rect *c = &win->layout->clip;
18699 if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
18700 !(flags & NK_WINDOW_MOVABLE)) {
18701 return 0;
18702 }}
18703 if (win->flags & NK_WINDOW_ROM)
18704 flags |= NK_WINDOW_ROM;
18705
18706 /* initialize a fake window to create the panel from */
18707 nk_zero(&panel, sizeof(panel));
18708 panel.bounds = bounds;
18709 panel.flags = flags;
18710 panel.scrollbar.x = *x_offset;
18711 panel.scrollbar.y = *y_offset;
18712 panel.buffer = win->buffer;
18713 panel.layout = (struct nk_panel*)nk_create_panel(ctx);
18714 ctx->current = &panel;
18715 nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
18716
18717 win->buffer = panel.buffer;
18718 win->buffer.clip = panel.layout->clip;
18719 panel.layout->offset_x = x_offset;
18720 panel.layout->offset_y = y_offset;
18721 panel.layout->parent = win->layout;
18722 win->layout = panel.layout;
18723
18724 ctx->current = win;
18725 if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
18726 (panel.layout->flags & NK_WINDOW_MINIMIZED))
18727 {
18728 nk_flags f = panel.layout->flags;
18729 nk_group_scrolled_end(ctx);
18730 if (f & NK_WINDOW_CLOSED)
18731 return NK_WINDOW_CLOSED;
18732 if (f & NK_WINDOW_MINIMIZED)
18733 return NK_WINDOW_MINIMIZED;
18734 }
18735 return 1;
18736 }
18737 NK_API void
18738 nk_group_scrolled_end(struct nk_context *ctx)
18739 {
18740 struct nk_window *win;
18741 struct nk_panel *parent;
18742 struct nk_panel *g;
18743
18744 struct nk_rect clip;
18745 struct nk_window pan;
18746 struct nk_vec2 panel_padding;
18747
18748 NK_ASSERT(ctx);
18749 NK_ASSERT(ctx->current);
18750 if (!ctx || !ctx->current)
18751 return;
18752
18753 /* make sure nk_group_begin was called correctly */
18754 NK_ASSERT(ctx->current);
18755 win = ctx->current;
18756 NK_ASSERT(win->layout);
18757 g = win->layout;
18758 NK_ASSERT(g->parent);
18759 parent = g->parent;
18760
18761 /* dummy window */
18762 nk_zero_struct(pan);
18763 panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
18764 pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
18765 pan.bounds.x = g->bounds.x - panel_padding.x;
18766 pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
18767 pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
18768 if (g->flags & NK_WINDOW_BORDER) {
18769 pan.bounds.x -= g->border;
18770 pan.bounds.y -= g->border;
18771 pan.bounds.w += 2*g->border;
18772 pan.bounds.h += 2*g->border;
18773 }
18774 if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
18775 pan.bounds.w += ctx->style.window.scrollbar_size.x;
18776 pan.bounds.h += ctx->style.window.scrollbar_size.y;
18777 }
18778 pan.scrollbar.x = *g->offset_x;
18779 pan.scrollbar.y = *g->offset_y;
18780 pan.flags = g->flags;
18781 pan.buffer = win->buffer;
18782 pan.layout = g;
18783 pan.parent = win;
18784 ctx->current = &pan;
18785
18786 /* make sure group has correct clipping rectangle */
18787 nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
18788 pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
18789 nk_push_scissor(&pan.buffer, clip);
18790 nk_end(ctx);
18791
18792 win->buffer = pan.buffer;
18793 nk_push_scissor(&win->buffer, parent->clip);
18794 ctx->current = win;
18795 win->layout = parent;
18796 g->bounds = pan.bounds;
18797 return;
18798 }
18799 NK_API int
18800 nk_group_scrolled_begin(struct nk_context *ctx,
18801 struct nk_scroll *scroll, const char *title, nk_flags flags)
18802 {
18803 return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);
18804 }
18805 NK_API int
18806 nk_group_begin_titled(struct nk_context *ctx, const char *id,
18807 const char *title, nk_flags flags)
18808 {
18809 int id_len;
18810 nk_hash id_hash;
18811 struct nk_window *win;
18812 nk_uint *x_offset;
18813 nk_uint *y_offset;
18814
18815 NK_ASSERT(ctx);
18816 NK_ASSERT(id);
18817 NK_ASSERT(ctx->current);
18818 NK_ASSERT(ctx->current->layout);
18819 if (!ctx || !ctx->current || !ctx->current->layout || !id)
18820 return 0;
18821
18822 /* find persistent group scrollbar value */
18823 win = ctx->current;
18824 id_len = (int)nk_strlen(id);
18825 id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
18826 x_offset = nk_find_value(win, id_hash);
18827 if (!x_offset) {
18828 x_offset = nk_add_value(ctx, win, id_hash, 0);
18829 y_offset = nk_add_value(ctx, win, id_hash+1, 0);
18830
18831 NK_ASSERT(x_offset);
18832 NK_ASSERT(y_offset);
18833 if (!x_offset || !y_offset) return 0;
18834 *x_offset = *y_offset = 0;
18835 } else y_offset = nk_find_value(win, id_hash+1);
18836 return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
18837 }
18838 NK_API int
18839 nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
18840 {
18841 return nk_group_begin_titled(ctx, title, title, flags);
18842 }
18843 NK_API void
18844 nk_group_end(struct nk_context *ctx)
18845 {
18846 nk_group_scrolled_end(ctx);
18847 }
18848 NK_API void
18849 nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, nk_uint *y_offset)
18850 {
18851 int id_len;
18852 nk_hash id_hash;
18853 struct nk_window *win;
18854 nk_uint *x_offset_ptr;
18855 nk_uint *y_offset_ptr;
18856
18857 NK_ASSERT(ctx);
18858 NK_ASSERT(id);
18859 NK_ASSERT(ctx->current);
18860 NK_ASSERT(ctx->current->layout);
18861 if (!ctx || !ctx->current || !ctx->current->layout || !id)
18862 return;
18863
18864 /* find persistent group scrollbar value */
18865 win = ctx->current;
18866 id_len = (int)nk_strlen(id);
18867 id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
18868 x_offset_ptr = nk_find_value(win, id_hash);
18869 if (!x_offset_ptr) {
18870 x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
18871 y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
18872
18873 NK_ASSERT(x_offset_ptr);
18874 NK_ASSERT(y_offset_ptr);
18875 if (!x_offset_ptr || !y_offset_ptr) return;
18876 *x_offset_ptr = *y_offset_ptr = 0;
18877 } else y_offset_ptr = nk_find_value(win, id_hash+1);
18878 if (x_offset)
18879 *x_offset = *x_offset_ptr;
18880 if (y_offset)
18881 *y_offset = *y_offset_ptr;
18882 }
18883 NK_API void
18884 nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk_uint y_offset)
18885 {
18886 int id_len;
18887 nk_hash id_hash;
18888 struct nk_window *win;
18889 nk_uint *x_offset_ptr;
18890 nk_uint *y_offset_ptr;
18891
18892 NK_ASSERT(ctx);
18893 NK_ASSERT(id);
18894 NK_ASSERT(ctx->current);
18895 NK_ASSERT(ctx->current->layout);
18896 if (!ctx || !ctx->current || !ctx->current->layout || !id)
18897 return;
18898
18899 /* find persistent group scrollbar value */
18900 win = ctx->current;
18901 id_len = (int)nk_strlen(id);
18902 id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
18903 x_offset_ptr = nk_find_value(win, id_hash);
18904 if (!x_offset_ptr) {
18905 x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
18906 y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
18907
18908 NK_ASSERT(x_offset_ptr);
18909 NK_ASSERT(y_offset_ptr);
18910 if (!x_offset_ptr || !y_offset_ptr) return;
18911 *x_offset_ptr = *y_offset_ptr = 0;
18912 } else y_offset_ptr = nk_find_value(win, id_hash+1);
18913 *x_offset_ptr = x_offset;
18914 *y_offset_ptr = y_offset;
18915 }
18916
18917
18918
18919
18920 /* ===============================================================
18921 *
18922 * LIST VIEW
18923 *
18924 * ===============================================================*/
18925 NK_API int
18926 nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
18927 const char *title, nk_flags flags, int row_height, int row_count)
18928 {
18929 int title_len;
18930 nk_hash title_hash;
18931 nk_uint *x_offset;
18932 nk_uint *y_offset;
18933
18934 int result;
18935 struct nk_window *win;
18936 struct nk_panel *layout;
18937 const struct nk_style *style;
18938 struct nk_vec2 item_spacing;
18939
18940 NK_ASSERT(ctx);
18941 NK_ASSERT(view);
18942 NK_ASSERT(title);
18943 if (!ctx || !view || !title) return 0;
18944
18945 win = ctx->current;
18946 style = &ctx->style;
18947 item_spacing = style->window.spacing;
18948 row_height += NK_MAX(0, (int)item_spacing.y);
18949
18950 /* find persistent list view scrollbar offset */
18951 title_len = (int)nk_strlen(title);
18952 title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
18953 x_offset = nk_find_value(win, title_hash);
18954 if (!x_offset) {
18955 x_offset = nk_add_value(ctx, win, title_hash, 0);
18956 y_offset = nk_add_value(ctx, win, title_hash+1, 0);
18957
18958 NK_ASSERT(x_offset);
18959 NK_ASSERT(y_offset);
18960 if (!x_offset || !y_offset) return 0;
18961 *x_offset = *y_offset = 0;
18962 } else y_offset = nk_find_value(win, title_hash+1);
18963 view->scroll_value = *y_offset;
18964 view->scroll_pointer = y_offset;
18965
18966 *y_offset = 0;
18967 result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
18968 win = ctx->current;
18969 layout = win->layout;
18970
18971 view->total_height = row_height * NK_MAX(row_count,1);
18972 view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
18973 view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height),0);
18974 view->count = NK_MIN(view->count, row_count - view->begin);
18975 view->end = view->begin + view->count;
18976 view->ctx = ctx;
18977 return result;
18978 }
18979 NK_API void
18980 nk_list_view_end(struct nk_list_view *view)
18981 {
18982 struct nk_context *ctx;
18983 struct nk_window *win;
18984 struct nk_panel *layout;
18985
18986 NK_ASSERT(view);
18987 NK_ASSERT(view->ctx);
18988 NK_ASSERT(view->scroll_pointer);
18989 if (!view || !view->ctx) return;
18990
18991 ctx = view->ctx;
18992 win = ctx->current;
18993 layout = win->layout;
18994 layout->at_y = layout->bounds.y + (float)view->total_height;
18995 *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
18996 nk_group_end(view->ctx);
18997 }
18998
18999
19000
19001
19002
19003 /* ===============================================================
19004 *
19005 * WIDGET
19006 *
19007 * ===============================================================*/
19008 NK_API struct nk_rect
19009 nk_widget_bounds(struct nk_context *ctx)
19010 {
19011 struct nk_rect bounds;
19012 NK_ASSERT(ctx);
19013 NK_ASSERT(ctx->current);
19014 if (!ctx || !ctx->current)
19015 return nk_rect(0,0,0,0);
19016 nk_layout_peek(&bounds, ctx);
19017 return bounds;
19018 }
19019 NK_API struct nk_vec2
19020 nk_widget_position(struct nk_context *ctx)
19021 {
19022 struct nk_rect bounds;
19023 NK_ASSERT(ctx);
19024 NK_ASSERT(ctx->current);
19025 if (!ctx || !ctx->current)
19026 return nk_vec2(0,0);
19027
19028 nk_layout_peek(&bounds, ctx);
19029 return nk_vec2(bounds.x, bounds.y);
19030 }
19031 NK_API struct nk_vec2
19032 nk_widget_size(struct nk_context *ctx)
19033 {
19034 struct nk_rect bounds;
19035 NK_ASSERT(ctx);
19036 NK_ASSERT(ctx->current);
19037 if (!ctx || !ctx->current)
19038 return nk_vec2(0,0);
19039
19040 nk_layout_peek(&bounds, ctx);
19041 return nk_vec2(bounds.w, bounds.h);
19042 }
19043 NK_API float
19044 nk_widget_width(struct nk_context *ctx)
19045 {
19046 struct nk_rect bounds;
19047 NK_ASSERT(ctx);
19048 NK_ASSERT(ctx->current);
19049 if (!ctx || !ctx->current)
19050 return 0;
19051
19052 nk_layout_peek(&bounds, ctx);
19053 return bounds.w;
19054 }
19055 NK_API float
19056 nk_widget_height(struct nk_context *ctx)
19057 {
19058 struct nk_rect bounds;
19059 NK_ASSERT(ctx);
19060 NK_ASSERT(ctx->current);
19061 if (!ctx || !ctx->current)
19062 return 0;
19063
19064 nk_layout_peek(&bounds, ctx);
19065 return bounds.h;
19066 }
19067 NK_API int
19068 nk_widget_is_hovered(struct nk_context *ctx)
19069 {
19070 struct nk_rect c, v;
19071 struct nk_rect bounds;
19072 NK_ASSERT(ctx);
19073 NK_ASSERT(ctx->current);
19074 if (!ctx || !ctx->current || ctx->active != ctx->current)
19075 return 0;
19076
19077 c = ctx->current->layout->clip;
19078 c.x = (float)((int)c.x);
19079 c.y = (float)((int)c.y);
19080 c.w = (float)((int)c.w);
19081 c.h = (float)((int)c.h);
19082
19083 nk_layout_peek(&bounds, ctx);
19084 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
19085 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
19086 return 0;
19087 return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
19088 }
19089 NK_API int
19090 nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
19091 {
19092 struct nk_rect c, v;
19093 struct nk_rect bounds;
19094 NK_ASSERT(ctx);
19095 NK_ASSERT(ctx->current);
19096 if (!ctx || !ctx->current || ctx->active != ctx->current)
19097 return 0;
19098
19099 c = ctx->current->layout->clip;
19100 c.x = (float)((int)c.x);
19101 c.y = (float)((int)c.y);
19102 c.w = (float)((int)c.w);
19103 c.h = (float)((int)c.h);
19104
19105 nk_layout_peek(&bounds, ctx);
19106 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
19107 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
19108 return 0;
19109 return nk_input_mouse_clicked(&ctx->input, btn, bounds);
19110 }
19111 NK_API int
19112 nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
19113 {
19114 struct nk_rect c, v;
19115 struct nk_rect bounds;
19116 NK_ASSERT(ctx);
19117 NK_ASSERT(ctx->current);
19118 if (!ctx || !ctx->current || ctx->active != ctx->current)
19119 return 0;
19120
19121 c = ctx->current->layout->clip;
19122 c.x = (float)((int)c.x);
19123 c.y = (float)((int)c.y);
19124 c.w = (float)((int)c.w);
19125 c.h = (float)((int)c.h);
19126
19127 nk_layout_peek(&bounds, ctx);
19128 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
19129 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
19130 return 0;
19131 return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
19132 }
19133 NK_API enum nk_widget_layout_states
19134 nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
19135 {
19136 struct nk_rect c, v;
19137 struct nk_window *win;
19138 struct nk_panel *layout;
19139 const struct nk_input *in;
19140
19141 NK_ASSERT(ctx);
19142 NK_ASSERT(ctx->current);
19143 NK_ASSERT(ctx->current->layout);
19144 if (!ctx || !ctx->current || !ctx->current->layout)
19145 return NK_WIDGET_INVALID;
19146
19147 /* allocate space and check if the widget needs to be updated and drawn */
19148 nk_panel_alloc_space(bounds, ctx);
19149 win = ctx->current;
19150 layout = win->layout;
19151 in = &ctx->input;
19152 c = layout->clip;
19153
19154 /* if one of these triggers you forgot to add an `if` condition around either
19155 a window, group, popup, combobox or contextual menu `begin` and `end` block.
19156 Example:
19157 if (nk_begin(...) {...} nk_end(...); or
19158 if (nk_group_begin(...) { nk_group_end(...);} */
19159 NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
19160 NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
19161 NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
19162
19163 /* need to convert to int here to remove floating point errors */
19164 bounds->x = (float)((int)bounds->x);
19165 bounds->y = (float)((int)bounds->y);
19166 bounds->w = (float)((int)bounds->w);
19167 bounds->h = (float)((int)bounds->h);
19168
19169 c.x = (float)((int)c.x);
19170 c.y = (float)((int)c.y);
19171 c.w = (float)((int)c.w);
19172 c.h = (float)((int)c.h);
19173
19174 nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
19175 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
19176 return NK_WIDGET_INVALID;
19177 if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
19178 return NK_WIDGET_ROM;
19179 return NK_WIDGET_VALID;
19180 }
19181 NK_API enum nk_widget_layout_states
19182 nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
19183 struct nk_vec2 item_padding)
19184 {
19185 /* update the bounds to stand without padding */
19186 struct nk_window *win;
19187 struct nk_style *style;
19188 struct nk_panel *layout;
19189 enum nk_widget_layout_states state;
19190 struct nk_vec2 panel_padding;
19191
19192 NK_ASSERT(ctx);
19193 NK_ASSERT(ctx->current);
19194 NK_ASSERT(ctx->current->layout);
19195 if (!ctx || !ctx->current || !ctx->current->layout)
19196 return NK_WIDGET_INVALID;
19197
19198 win = ctx->current;
19199 style = &ctx->style;
19200 layout = win->layout;
19201 state = nk_widget(bounds, ctx);
19202
19203 panel_padding = nk_panel_get_padding(style, layout->type);
19204 if (layout->row.index == 1) {
19205 bounds->w += panel_padding.x;
19206 bounds->x -= panel_padding.x;
19207 } else bounds->x -= item_padding.x;
19208
19209 if (layout->row.index == layout->row.columns)
19210 bounds->w += panel_padding.x;
19211 else bounds->w += item_padding.x;
19212 return state;
19213 }
19214 NK_API void
19215 nk_spacing(struct nk_context *ctx, int cols)
19216 {
19217 struct nk_window *win;
19218 struct nk_panel *layout;
19219 struct nk_rect none;
19220 int i, index, rows;
19221
19222 NK_ASSERT(ctx);
19223 NK_ASSERT(ctx->current);
19224 NK_ASSERT(ctx->current->layout);
19225 if (!ctx || !ctx->current || !ctx->current->layout)
19226 return;
19227
19228 /* spacing over row boundaries */
19229 win = ctx->current;
19230 layout = win->layout;
19231 index = (layout->row.index + cols) % layout->row.columns;
19232 rows = (layout->row.index + cols) / layout->row.columns;
19233 if (rows) {
19234 for (i = 0; i < rows; ++i)
19235 nk_panel_alloc_row(ctx, win);
19236 cols = index;
19237 }
19238 /* non table layout need to allocate space */
19239 if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
19240 layout->row.type != NK_LAYOUT_STATIC_FIXED) {
19241 for (i = 0; i < cols; ++i)
19242 nk_panel_alloc_space(&none, ctx);
19243 } layout->row.index = index;
19244 }
19245
19246
19247
19248
19249
19250 /* ===============================================================
19251 *
19252 * TEXT
19253 *
19254 * ===============================================================*/
19255 NK_LIB void
19256 nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
19257 const char *string, int len, const struct nk_text *t,
19258 nk_flags a, const struct nk_user_font *f)
19259 {
19260 struct nk_rect label;
19261 float text_width;
19262
19263 NK_ASSERT(o);
19264 NK_ASSERT(t);
19265 if (!o || !t) return;
19266
19267 b.h = NK_MAX(b.h, 2 * t->padding.y);
19268 label.x = 0; label.w = 0;
19269 label.y = b.y + t->padding.y;
19270 label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
19271
19272 text_width = f->width(f->userdata, f->height, (const char*)string, len);
19273 text_width += (2.0f * t->padding.x);
19274
19275 /* align in x-axis */
19276 if (a & NK_TEXT_ALIGN_LEFT) {
19277 label.x = b.x + t->padding.x;
19278 label.w = NK_MAX(0, b.w - 2 * t->padding.x);
19279 } else if (a & NK_TEXT_ALIGN_CENTERED) {
19280 label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
19281 label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
19282 label.x = NK_MAX(b.x + t->padding.x, label.x);
19283 label.w = NK_MIN(b.x + b.w, label.x + label.w);
19284 if (label.w >= label.x) label.w -= label.x;
19285 } else if (a & NK_TEXT_ALIGN_RIGHT) {
19286 label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
19287 label.w = (float)text_width + 2 * t->padding.x;
19288 } else return;
19289
19290 /* align in y-axis */
19291 if (a & NK_TEXT_ALIGN_MIDDLE) {
19292 label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
19293 label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
19294 } else if (a & NK_TEXT_ALIGN_BOTTOM) {
19295 label.y = b.y + b.h - f->height;
19296 label.h = f->height;
19297 }
19298 nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
19299 }
19300 NK_LIB void
19301 nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
19302 const char *string, int len, const struct nk_text *t,
19303 const struct nk_user_font *f)
19304 {
19305 float width;
19306 int glyphs = 0;
19307 int fitting = 0;
19308 int done = 0;
19309 struct nk_rect line;
19310 struct nk_text text;
19311 NK_INTERN nk_rune seperator[] = {' '};
19312
19313 NK_ASSERT(o);
19314 NK_ASSERT(t);
19315 if (!o || !t) return;
19316
19317 text.padding = nk_vec2(0,0);
19318 text.background = t->background;
19319 text.text = t->text;
19320
19321 b.w = NK_MAX(b.w, 2 * t->padding.x);
19322 b.h = NK_MAX(b.h, 2 * t->padding.y);
19323 b.h = b.h - 2 * t->padding.y;
19324
19325 line.x = b.x + t->padding.x;
19326 line.y = b.y + t->padding.y;
19327 line.w = b.w - 2 * t->padding.x;
19328 line.h = 2 * t->padding.y + f->height;
19329
19330 fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
19331 while (done < len) {
19332 if (!fitting || line.y + line.h >= (b.y + b.h)) break;
19333 nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
19334 done += fitting;
19335 line.y += f->height + 2 * t->padding.y;
19336 fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
19337 }
19338 }
19339 NK_API void
19340 nk_text_colored(struct nk_context *ctx, const char *str, int len,
19341 nk_flags alignment, struct nk_color color)
19342 {
19343 struct nk_window *win;
19344 const struct nk_style *style;
19345
19346 struct nk_vec2 item_padding;
19347 struct nk_rect bounds;
19348 struct nk_text text;
19349
19350 NK_ASSERT(ctx);
19351 NK_ASSERT(ctx->current);
19352 NK_ASSERT(ctx->current->layout);
19353 if (!ctx || !ctx->current || !ctx->current->layout) return;
19354
19355 win = ctx->current;
19356 style = &ctx->style;
19357 nk_panel_alloc_space(&bounds, ctx);
19358 item_padding = style->text.padding;
19359
19360 text.padding.x = item_padding.x;
19361 text.padding.y = item_padding.y;
19362 text.background = style->window.background;
19363 text.text = color;
19364 nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
19365 }
19366 NK_API void
19367 nk_text_wrap_colored(struct nk_context *ctx, const char *str,
19368 int len, struct nk_color color)
19369 {
19370 struct nk_window *win;
19371 const struct nk_style *style;
19372
19373 struct nk_vec2 item_padding;
19374 struct nk_rect bounds;
19375 struct nk_text text;
19376
19377 NK_ASSERT(ctx);
19378 NK_ASSERT(ctx->current);
19379 NK_ASSERT(ctx->current->layout);
19380 if (!ctx || !ctx->current || !ctx->current->layout) return;
19381
19382 win = ctx->current;
19383 style = &ctx->style;
19384 nk_panel_alloc_space(&bounds, ctx);
19385 item_padding = style->text.padding;
19386
19387 text.padding.x = item_padding.x;
19388 text.padding.y = item_padding.y;
19389 text.background = style->window.background;
19390 text.text = color;
19391 nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
19392 }
19393 #ifdef NK_INCLUDE_STANDARD_VARARGS
19394 NK_API void
19395 nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
19396 struct nk_color color, const char *fmt, ...)
19397 {
19398 va_list args;
19399 va_start(args, fmt);
19400 nk_labelfv_colored(ctx, flags, color, fmt, args);
19401 va_end(args);
19402 }
19403 NK_API void
19404 nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
19405 const char *fmt, ...)
19406 {
19407 va_list args;
19408 va_start(args, fmt);
19409 nk_labelfv_colored_wrap(ctx, color, fmt, args);
19410 va_end(args);
19411 }
19412 NK_API void
19413 nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
19414 {
19415 va_list args;
19416 va_start(args, fmt);
19417 nk_labelfv(ctx, flags, fmt, args);
19418 va_end(args);
19419 }
19420 NK_API void
19421 nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
19422 {
19423 va_list args;
19424 va_start(args, fmt);
19425 nk_labelfv_wrap(ctx, fmt, args);
19426 va_end(args);
19427 }
19428 NK_API void
19429 nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
19430 struct nk_color color, const char *fmt, va_list args)
19431 {
19432 char buf[256];
19433 nk_strfmt(buf, NK_LEN(buf), fmt, args);
19434 nk_label_colored(ctx, buf, flags, color);
19435 }
19436
19437 NK_API void
19438 nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
19439 const char *fmt, va_list args)
19440 {
19441 char buf[256];
19442 nk_strfmt(buf, NK_LEN(buf), fmt, args);
19443 nk_label_colored_wrap(ctx, buf, color);
19444 }
19445
19446 NK_API void
19447 nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
19448 {
19449 char buf[256];
19450 nk_strfmt(buf, NK_LEN(buf), fmt, args);
19451 nk_label(ctx, buf, flags);
19452 }
19453
19454 NK_API void
19455 nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
19456 {
19457 char buf[256];
19458 nk_strfmt(buf, NK_LEN(buf), fmt, args);
19459 nk_label_wrap(ctx, buf);
19460 }
19461
19462 NK_API void
19463 nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
19464 {
19465 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
19466 }
19467 NK_API void
19468 nk_value_int(struct nk_context *ctx, const char *prefix, int value)
19469 {
19470 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
19471 }
19472 NK_API void
19473 nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
19474 {
19475 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
19476 }
19477 NK_API void
19478 nk_value_float(struct nk_context *ctx, const char *prefix, float value)
19479 {
19480 double double_value = (double)value;
19481 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
19482 }
19483 NK_API void
19484 nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
19485 {
19486 nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
19487 }
19488 NK_API void
19489 nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
19490 {
19491 double c[4]; nk_color_dv(c, color);
19492 nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
19493 p, c[0], c[1], c[2], c[3]);
19494 }
19495 NK_API void
19496 nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
19497 {
19498 char hex[16];
19499 nk_color_hex_rgba(hex, color);
19500 nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
19501 }
19502 #endif
19503 NK_API void
19504 nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
19505 {
19506 NK_ASSERT(ctx);
19507 if (!ctx) return;
19508 nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
19509 }
19510 NK_API void
19511 nk_text_wrap(struct nk_context *ctx, const char *str, int len)
19512 {
19513 NK_ASSERT(ctx);
19514 if (!ctx) return;
19515 nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
19516 }
19517 NK_API void
19518 nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
19519 {
19520 nk_text(ctx, str, nk_strlen(str), alignment);
19521 }
19522 NK_API void
19523 nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
19524 struct nk_color color)
19525 {
19526 nk_text_colored(ctx, str, nk_strlen(str), align, color);
19527 }
19528 NK_API void
19529 nk_label_wrap(struct nk_context *ctx, const char *str)
19530 {
19531 nk_text_wrap(ctx, str, nk_strlen(str));
19532 }
19533 NK_API void
19534 nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
19535 {
19536 nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
19537 }
19538
19539
19540
19541
19542
19543 /* ===============================================================
19544 *
19545 * IMAGE
19546 *
19547 * ===============================================================*/
19548 NK_API nk_handle
19549 nk_handle_ptr(void *ptr)
19550 {
19551 nk_handle handle = {0};
19552 handle.ptr = ptr;
19553 return handle;
19554 }
19555 NK_API nk_handle
19556 nk_handle_id(int id)
19557 {
19558 nk_handle handle;
19559 nk_zero_struct(handle);
19560 handle.id = id;
19561 return handle;
19562 }
19563 NK_API struct nk_image
19564 nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
19565 {
19566 struct nk_image s;
19567 nk_zero(&s, sizeof(s));
19568 s.handle.ptr = ptr;
19569 s.w = w; s.h = h;
19570 s.region[0] = (unsigned short)r.x;
19571 s.region[1] = (unsigned short)r.y;
19572 s.region[2] = (unsigned short)r.w;
19573 s.region[3] = (unsigned short)r.h;
19574 return s;
19575 }
19576 NK_API struct nk_image
19577 nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
19578 {
19579 struct nk_image s;
19580 nk_zero(&s, sizeof(s));
19581 s.handle.id = id;
19582 s.w = w; s.h = h;
19583 s.region[0] = (unsigned short)r.x;
19584 s.region[1] = (unsigned short)r.y;
19585 s.region[2] = (unsigned short)r.w;
19586 s.region[3] = (unsigned short)r.h;
19587 return s;
19588 }
19589 NK_API struct nk_image
19590 nk_subimage_handle(nk_handle handle, unsigned short w, unsigned short h,
19591 struct nk_rect r)
19592 {
19593 struct nk_image s;
19594 nk_zero(&s, sizeof(s));
19595 s.handle = handle;
19596 s.w = w; s.h = h;
19597 s.region[0] = (unsigned short)r.x;
19598 s.region[1] = (unsigned short)r.y;
19599 s.region[2] = (unsigned short)r.w;
19600 s.region[3] = (unsigned short)r.h;
19601 return s;
19602 }
19603 NK_API struct nk_image
19604 nk_image_handle(nk_handle handle)
19605 {
19606 struct nk_image s;
19607 nk_zero(&s, sizeof(s));
19608 s.handle = handle;
19609 s.w = 0; s.h = 0;
19610 s.region[0] = 0;
19611 s.region[1] = 0;
19612 s.region[2] = 0;
19613 s.region[3] = 0;
19614 return s;
19615 }
19616 NK_API struct nk_image
19617 nk_image_ptr(void *ptr)
19618 {
19619 struct nk_image s;
19620 nk_zero(&s, sizeof(s));
19621 NK_ASSERT(ptr);
19622 s.handle.ptr = ptr;
19623 s.w = 0; s.h = 0;
19624 s.region[0] = 0;
19625 s.region[1] = 0;
19626 s.region[2] = 0;
19627 s.region[3] = 0;
19628 return s;
19629 }
19630 NK_API struct nk_image
19631 nk_image_id(int id)
19632 {
19633 struct nk_image s;
19634 nk_zero(&s, sizeof(s));
19635 s.handle.id = id;
19636 s.w = 0; s.h = 0;
19637 s.region[0] = 0;
19638 s.region[1] = 0;
19639 s.region[2] = 0;
19640 s.region[3] = 0;
19641 return s;
19642 }
19643 NK_API int
19644 nk_image_is_subimage(const struct nk_image* img)
19645 {
19646 NK_ASSERT(img);
19647 return !(img->w == 0 && img->h == 0);
19648 }
19649 NK_API void
19650 nk_image(struct nk_context *ctx, struct nk_image img)
19651 {
19652 struct nk_window *win;
19653 struct nk_rect bounds;
19654
19655 NK_ASSERT(ctx);
19656 NK_ASSERT(ctx->current);
19657 NK_ASSERT(ctx->current->layout);
19658 if (!ctx || !ctx->current || !ctx->current->layout) return;
19659
19660 win = ctx->current;
19661 if (!nk_widget(&bounds, ctx)) return;
19662 nk_draw_image(&win->buffer, bounds, &img, nk_white);
19663 }
19664 NK_API void
19665 nk_image_color(struct nk_context *ctx, struct nk_image img, struct nk_color col)
19666 {
19667 struct nk_window *win;
19668 struct nk_rect bounds;
19669
19670 NK_ASSERT(ctx);
19671 NK_ASSERT(ctx->current);
19672 NK_ASSERT(ctx->current->layout);
19673 if (!ctx || !ctx->current || !ctx->current->layout) return;
19674
19675 win = ctx->current;
19676 if (!nk_widget(&bounds, ctx)) return;
19677 nk_draw_image(&win->buffer, bounds, &img, col);
19678 }
19679
19680
19681
19682
19683
19684 /* ==============================================================
19685 *
19686 * BUTTON
19687 *
19688 * ===============================================================*/
19689 NK_LIB void
19690 nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
19691 struct nk_rect content, struct nk_color background, struct nk_color foreground,
19692 float border_width, const struct nk_user_font *font)
19693 {
19694 switch (type) {
19695 case NK_SYMBOL_X:
19696 case NK_SYMBOL_UNDERSCORE:
19697 case NK_SYMBOL_PLUS:
19698 case NK_SYMBOL_MINUS: {
19699 /* single character text symbol */
19700 const char *X = (type == NK_SYMBOL_X) ? "x":
19701 (type == NK_SYMBOL_UNDERSCORE) ? "_":
19702 (type == NK_SYMBOL_PLUS) ? "+": "-";
19703 struct nk_text text;
19704 text.padding = nk_vec2(0,0);
19705 text.background = background;
19706 text.text = foreground;
19707 nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
19708 } break;
19709 case NK_SYMBOL_CIRCLE_SOLID:
19710 case NK_SYMBOL_CIRCLE_OUTLINE:
19711 case NK_SYMBOL_RECT_SOLID:
19712 case NK_SYMBOL_RECT_OUTLINE: {
19713 /* simple empty/filled shapes */
19714 if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
19715 nk_fill_rect(out, content, 0, foreground);
19716 if (type == NK_SYMBOL_RECT_OUTLINE)
19717 nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
19718 } else {
19719 nk_fill_circle(out, content, foreground);
19720 if (type == NK_SYMBOL_CIRCLE_OUTLINE)
19721 nk_fill_circle(out, nk_shrink_rect(content, 1), background);
19722 }
19723 } break;
19724 case NK_SYMBOL_TRIANGLE_UP:
19725 case NK_SYMBOL_TRIANGLE_DOWN:
19726 case NK_SYMBOL_TRIANGLE_LEFT:
19727 case NK_SYMBOL_TRIANGLE_RIGHT: {
19728 enum nk_heading heading;
19729 struct nk_vec2 points[3];
19730 heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
19731 (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
19732 (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
19733 nk_triangle_from_direction(points, content, 0, 0, heading);
19734 nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
19735 points[2].x, points[2].y, foreground);
19736 } break;
19737 default:
19738 case NK_SYMBOL_NONE:
19739 case NK_SYMBOL_MAX: break;
19740 }
19741 }
19742 NK_LIB int
19743 nk_button_behavior(nk_flags *state, struct nk_rect r,
19744 const struct nk_input *i, enum nk_button_behavior behavior)
19745 {
19746 int ret = 0;
19747 nk_widget_state_reset(state);
19748 if (!i) return 0;
19749 if (nk_input_is_mouse_hovering_rect(i, r)) {
19750 *state = NK_WIDGET_STATE_HOVERED;
19751 if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
19752 *state = NK_WIDGET_STATE_ACTIVE;
19753 if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
19754 ret = (behavior != NK_BUTTON_DEFAULT) ?
19755 nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
19756 #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
19757 nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
19758 #else
19759 nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
19760 #endif
19761 }
19762 }
19763 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
19764 *state |= NK_WIDGET_STATE_ENTERED;
19765 else if (nk_input_is_mouse_prev_hovering_rect(i, r))
19766 *state |= NK_WIDGET_STATE_LEFT;
19767 return ret;
19768 }
19769 NK_LIB const struct nk_style_item*
19770 nk_draw_button(struct nk_command_buffer *out,
19771 const struct nk_rect *bounds, nk_flags state,
19772 const struct nk_style_button *style)
19773 {
19774 const struct nk_style_item *background;
19775 if (state & NK_WIDGET_STATE_HOVER)
19776 background = &style->hover;
19777 else if (state & NK_WIDGET_STATE_ACTIVED)
19778 background = &style->active;
19779 else background = &style->normal;
19780
19781 if (background->type == NK_STYLE_ITEM_IMAGE) {
19782 nk_draw_image(out, *bounds, &background->data.image, nk_white);
19783 } else {
19784 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
19785 nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
19786 }
19787 return background;
19788 }
19789 NK_LIB int
19790 nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
19791 const struct nk_style_button *style, const struct nk_input *in,
19792 enum nk_button_behavior behavior, struct nk_rect *content)
19793 {
19794 struct nk_rect bounds;
19795 NK_ASSERT(style);
19796 NK_ASSERT(state);
19797 NK_ASSERT(out);
19798 if (!out || !style)
19799 return nk_false;
19800
19801 /* calculate button content space */
19802 content->x = r.x + style->padding.x + style->border + style->rounding;
19803 content->y = r.y + style->padding.y + style->border + style->rounding;
19804 content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
19805 content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
19806
19807 /* execute button behavior */
19808 bounds.x = r.x - style->touch_padding.x;
19809 bounds.y = r.y - style->touch_padding.y;
19810 bounds.w = r.w + 2 * style->touch_padding.x;
19811 bounds.h = r.h + 2 * style->touch_padding.y;
19812 return nk_button_behavior(state, bounds, in, behavior);
19813 }
19814 NK_LIB void
19815 nk_draw_button_text(struct nk_command_buffer *out,
19816 const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
19817 const struct nk_style_button *style, const char *txt, int len,
19818 nk_flags text_alignment, const struct nk_user_font *font)
19819 {
19820 struct nk_text text;
19821 const struct nk_style_item *background;
19822 background = nk_draw_button(out, bounds, state, style);
19823
19824 /* select correct colors/images */
19825 if (background->type == NK_STYLE_ITEM_COLOR)
19826 text.background = background->data.color;
19827 else text.background = style->text_background;
19828 if (state & NK_WIDGET_STATE_HOVER)
19829 text.text = style->text_hover;
19830 else if (state & NK_WIDGET_STATE_ACTIVED)
19831 text.text = style->text_active;
19832 else text.text = style->text_normal;
19833
19834 text.padding = nk_vec2(0,0);
19835 nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
19836 }
19837 NK_LIB int
19838 nk_do_button_text(nk_flags *state,
19839 struct nk_command_buffer *out, struct nk_rect bounds,
19840 const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
19841 const struct nk_style_button *style, const struct nk_input *in,
19842 const struct nk_user_font *font)
19843 {
19844 struct nk_rect content;
19845 int ret = nk_false;
19846
19847 NK_ASSERT(state);
19848 NK_ASSERT(style);
19849 NK_ASSERT(out);
19850 NK_ASSERT(string);
19851 NK_ASSERT(font);
19852 if (!out || !style || !font || !string)
19853 return nk_false;
19854
19855 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
19856 if (style->draw_begin) style->draw_begin(out, style->userdata);
19857 nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
19858 if (style->draw_end) style->draw_end(out, style->userdata);
19859 return ret;
19860 }
19861 NK_LIB void
19862 nk_draw_button_symbol(struct nk_command_buffer *out,
19863 const struct nk_rect *bounds, const struct nk_rect *content,
19864 nk_flags state, const struct nk_style_button *style,
19865 enum nk_symbol_type type, const struct nk_user_font *font)
19866 {
19867 struct nk_color sym, bg;
19868 const struct nk_style_item *background;
19869
19870 /* select correct colors/images */
19871 background = nk_draw_button(out, bounds, state, style);
19872 if (background->type == NK_STYLE_ITEM_COLOR)
19873 bg = background->data.color;
19874 else bg = style->text_background;
19875
19876 if (state & NK_WIDGET_STATE_HOVER)
19877 sym = style->text_hover;
19878 else if (state & NK_WIDGET_STATE_ACTIVED)
19879 sym = style->text_active;
19880 else sym = style->text_normal;
19881 nk_draw_symbol(out, type, *content, bg, sym, 1, font);
19882 }
19883 NK_LIB int
19884 nk_do_button_symbol(nk_flags *state,
19885 struct nk_command_buffer *out, struct nk_rect bounds,
19886 enum nk_symbol_type symbol, enum nk_button_behavior behavior,
19887 const struct nk_style_button *style, const struct nk_input *in,
19888 const struct nk_user_font *font)
19889 {
19890 int ret;
19891 struct nk_rect content;
19892
19893 NK_ASSERT(state);
19894 NK_ASSERT(style);
19895 NK_ASSERT(font);
19896 NK_ASSERT(out);
19897 if (!out || !style || !font || !state)
19898 return nk_false;
19899
19900 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
19901 if (style->draw_begin) style->draw_begin(out, style->userdata);
19902 nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
19903 if (style->draw_end) style->draw_end(out, style->userdata);
19904 return ret;
19905 }
19906 NK_LIB void
19907 nk_draw_button_image(struct nk_command_buffer *out,
19908 const struct nk_rect *bounds, const struct nk_rect *content,
19909 nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
19910 {
19911 nk_draw_button(out, bounds, state, style);
19912 nk_draw_image(out, *content, img, nk_white);
19913 }
19914 NK_LIB int
19915 nk_do_button_image(nk_flags *state,
19916 struct nk_command_buffer *out, struct nk_rect bounds,
19917 struct nk_image img, enum nk_button_behavior b,
19918 const struct nk_style_button *style, const struct nk_input *in)
19919 {
19920 int ret;
19921 struct nk_rect content;
19922
19923 NK_ASSERT(state);
19924 NK_ASSERT(style);
19925 NK_ASSERT(out);
19926 if (!out || !style || !state)
19927 return nk_false;
19928
19929 ret = nk_do_button(state, out, bounds, style, in, b, &content);
19930 content.x += style->image_padding.x;
19931 content.y += style->image_padding.y;
19932 content.w -= 2 * style->image_padding.x;
19933 content.h -= 2 * style->image_padding.y;
19934
19935 if (style->draw_begin) style->draw_begin(out, style->userdata);
19936 nk_draw_button_image(out, &bounds, &content, *state, style, &img);
19937 if (style->draw_end) style->draw_end(out, style->userdata);
19938 return ret;
19939 }
19940 NK_LIB void
19941 nk_draw_button_text_symbol(struct nk_command_buffer *out,
19942 const struct nk_rect *bounds, const struct nk_rect *label,
19943 const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
19944 const char *str, int len, enum nk_symbol_type type,
19945 const struct nk_user_font *font)
19946 {
19947 struct nk_color sym;
19948 struct nk_text text;
19949 const struct nk_style_item *background;
19950
19951 /* select correct background colors/images */
19952 background = nk_draw_button(out, bounds, state, style);
19953 if (background->type == NK_STYLE_ITEM_COLOR)
19954 text.background = background->data.color;
19955 else text.background = style->text_background;
19956
19957 /* select correct text colors */
19958 if (state & NK_WIDGET_STATE_HOVER) {
19959 sym = style->text_hover;
19960 text.text = style->text_hover;
19961 } else if (state & NK_WIDGET_STATE_ACTIVED) {
19962 sym = style->text_active;
19963 text.text = style->text_active;
19964 } else {
19965 sym = style->text_normal;
19966 text.text = style->text_normal;
19967 }
19968
19969 text.padding = nk_vec2(0,0);
19970 nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
19971 nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
19972 }
19973 NK_LIB int
19974 nk_do_button_text_symbol(nk_flags *state,
19975 struct nk_command_buffer *out, struct nk_rect bounds,
19976 enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
19977 enum nk_button_behavior behavior, const struct nk_style_button *style,
19978 const struct nk_user_font *font, const struct nk_input *in)
19979 {
19980 int ret;
19981 struct nk_rect tri = {0,0,0,0};
19982 struct nk_rect content;
19983
19984 NK_ASSERT(style);
19985 NK_ASSERT(out);
19986 NK_ASSERT(font);
19987 if (!out || !style || !font)
19988 return nk_false;
19989
19990 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
19991 tri.y = content.y + (content.h/2) - font->height/2;
19992 tri.w = font->height; tri.h = font->height;
19993 if (align & NK_TEXT_ALIGN_LEFT) {
19994 tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
19995 tri.x = NK_MAX(tri.x, 0);
19996 } else tri.x = content.x + 2 * style->padding.x;
19997
19998 /* draw button */
19999 if (style->draw_begin) style->draw_begin(out, style->userdata);
20000 nk_draw_button_text_symbol(out, &bounds, &content, &tri,
20001 *state, style, str, len, symbol, font);
20002 if (style->draw_end) style->draw_end(out, style->userdata);
20003 return ret;
20004 }
20005 NK_LIB void
20006 nk_draw_button_text_image(struct nk_command_buffer *out,
20007 const struct nk_rect *bounds, const struct nk_rect *label,
20008 const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
20009 const char *str, int len, const struct nk_user_font *font,
20010 const struct nk_image *img)
20011 {
20012 struct nk_text text;
20013 const struct nk_style_item *background;
20014 background = nk_draw_button(out, bounds, state, style);
20015
20016 /* select correct colors */
20017 if (background->type == NK_STYLE_ITEM_COLOR)
20018 text.background = background->data.color;
20019 else text.background = style->text_background;
20020 if (state & NK_WIDGET_STATE_HOVER)
20021 text.text = style->text_hover;
20022 else if (state & NK_WIDGET_STATE_ACTIVED)
20023 text.text = style->text_active;
20024 else text.text = style->text_normal;
20025
20026 text.padding = nk_vec2(0,0);
20027 nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
20028 nk_draw_image(out, *image, img, nk_white);
20029 }
20030 NK_LIB int
20031 nk_do_button_text_image(nk_flags *state,
20032 struct nk_command_buffer *out, struct nk_rect bounds,
20033 struct nk_image img, const char* str, int len, nk_flags align,
20034 enum nk_button_behavior behavior, const struct nk_style_button *style,
20035 const struct nk_user_font *font, const struct nk_input *in)
20036 {
20037 int ret;
20038 struct nk_rect icon;
20039 struct nk_rect content;
20040
20041 NK_ASSERT(style);
20042 NK_ASSERT(state);
20043 NK_ASSERT(font);
20044 NK_ASSERT(out);
20045 if (!out || !font || !style || !str)
20046 return nk_false;
20047
20048 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
20049 icon.y = bounds.y + style->padding.y;
20050 icon.w = icon.h = bounds.h - 2 * style->padding.y;
20051 if (align & NK_TEXT_ALIGN_LEFT) {
20052 icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
20053 icon.x = NK_MAX(icon.x, 0);
20054 } else icon.x = bounds.x + 2 * style->padding.x;
20055
20056 icon.x += style->image_padding.x;
20057 icon.y += style->image_padding.y;
20058 icon.w -= 2 * style->image_padding.x;
20059 icon.h -= 2 * style->image_padding.y;
20060
20061 if (style->draw_begin) style->draw_begin(out, style->userdata);
20062 nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
20063 if (style->draw_end) style->draw_end(out, style->userdata);
20064 return ret;
20065 }
20066 NK_API void
20067 nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
20068 {
20069 NK_ASSERT(ctx);
20070 if (!ctx) return;
20071 ctx->button_behavior = behavior;
20072 }
20073 NK_API int
20074 nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
20075 {
20076 struct nk_config_stack_button_behavior *button_stack;
20077 struct nk_config_stack_button_behavior_element *element;
20078
20079 NK_ASSERT(ctx);
20080 if (!ctx) return 0;
20081
20082 button_stack = &ctx->stacks.button_behaviors;
20083 NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
20084 if (button_stack->head >= (int)NK_LEN(button_stack->elements))
20085 return 0;
20086
20087 element = &button_stack->elements[button_stack->head++];
20088 element->address = &ctx->button_behavior;
20089 element->old_value = ctx->button_behavior;
20090 ctx->button_behavior = behavior;
20091 return 1;
20092 }
20093 NK_API int
20094 nk_button_pop_behavior(struct nk_context *ctx)
20095 {
20096 struct nk_config_stack_button_behavior *button_stack;
20097 struct nk_config_stack_button_behavior_element *element;
20098
20099 NK_ASSERT(ctx);
20100 if (!ctx) return 0;
20101
20102 button_stack = &ctx->stacks.button_behaviors;
20103 NK_ASSERT(button_stack->head > 0);
20104 if (button_stack->head < 1)
20105 return 0;
20106
20107 element = &button_stack->elements[--button_stack->head];
20108 *element->address = element->old_value;
20109 return 1;
20110 }
20111 NK_API int
20112 nk_button_text_styled(struct nk_context *ctx,
20113 const struct nk_style_button *style, const char *title, int len)
20114 {
20115 struct nk_window *win;
20116 struct nk_panel *layout;
20117 const struct nk_input *in;
20118
20119 struct nk_rect bounds;
20120 enum nk_widget_layout_states state;
20121
20122 NK_ASSERT(ctx);
20123 NK_ASSERT(style);
20124 NK_ASSERT(ctx->current);
20125 NK_ASSERT(ctx->current->layout);
20126 if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
20127
20128 win = ctx->current;
20129 layout = win->layout;
20130 state = nk_widget(&bounds, ctx);
20131
20132 if (!state) return 0;
20133 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20134 return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
20135 title, len, style->text_alignment, ctx->button_behavior,
20136 style, in, ctx->style.font);
20137 }
20138 NK_API int
20139 nk_button_text(struct nk_context *ctx, const char *title, int len)
20140 {
20141 NK_ASSERT(ctx);
20142 if (!ctx) return 0;
20143 return nk_button_text_styled(ctx, &ctx->style.button, title, len);
20144 }
20145 NK_API int nk_button_label_styled(struct nk_context *ctx,
20146 const struct nk_style_button *style, const char *title)
20147 {
20148 return nk_button_text_styled(ctx, style, title, nk_strlen(title));
20149 }
20150 NK_API int nk_button_label(struct nk_context *ctx, const char *title)
20151 {
20152 return nk_button_text(ctx, title, nk_strlen(title));
20153 }
20154 NK_API int
20155 nk_button_color(struct nk_context *ctx, struct nk_color color)
20156 {
20157 struct nk_window *win;
20158 struct nk_panel *layout;
20159 const struct nk_input *in;
20160 struct nk_style_button button;
20161
20162 int ret = 0;
20163 struct nk_rect bounds;
20164 struct nk_rect content;
20165 enum nk_widget_layout_states state;
20166
20167 NK_ASSERT(ctx);
20168 NK_ASSERT(ctx->current);
20169 NK_ASSERT(ctx->current->layout);
20170 if (!ctx || !ctx->current || !ctx->current->layout)
20171 return 0;
20172
20173 win = ctx->current;
20174 layout = win->layout;
20175
20176 state = nk_widget(&bounds, ctx);
20177 if (!state) return 0;
20178 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20179
20180 button = ctx->style.button;
20181 button.normal = nk_style_item_color(color);
20182 button.hover = nk_style_item_color(color);
20183 button.active = nk_style_item_color(color);
20184 ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
20185 &button, in, ctx->button_behavior, &content);
20186 nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
20187 return ret;
20188 }
20189 NK_API int
20190 nk_button_symbol_styled(struct nk_context *ctx,
20191 const struct nk_style_button *style, enum nk_symbol_type symbol)
20192 {
20193 struct nk_window *win;
20194 struct nk_panel *layout;
20195 const struct nk_input *in;
20196
20197 struct nk_rect bounds;
20198 enum nk_widget_layout_states state;
20199
20200 NK_ASSERT(ctx);
20201 NK_ASSERT(ctx->current);
20202 NK_ASSERT(ctx->current->layout);
20203 if (!ctx || !ctx->current || !ctx->current->layout)
20204 return 0;
20205
20206 win = ctx->current;
20207 layout = win->layout;
20208 state = nk_widget(&bounds, ctx);
20209 if (!state) return 0;
20210 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20211 return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
20212 symbol, ctx->button_behavior, style, in, ctx->style.font);
20213 }
20214 NK_API int
20215 nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
20216 {
20217 NK_ASSERT(ctx);
20218 if (!ctx) return 0;
20219 return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
20220 }
20221 NK_API int
20222 nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
20223 struct nk_image img)
20224 {
20225 struct nk_window *win;
20226 struct nk_panel *layout;
20227 const struct nk_input *in;
20228
20229 struct nk_rect bounds;
20230 enum nk_widget_layout_states state;
20231
20232 NK_ASSERT(ctx);
20233 NK_ASSERT(ctx->current);
20234 NK_ASSERT(ctx->current->layout);
20235 if (!ctx || !ctx->current || !ctx->current->layout)
20236 return 0;
20237
20238 win = ctx->current;
20239 layout = win->layout;
20240
20241 state = nk_widget(&bounds, ctx);
20242 if (!state) return 0;
20243 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20244 return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
20245 img, ctx->button_behavior, style, in);
20246 }
20247 NK_API int
20248 nk_button_image(struct nk_context *ctx, struct nk_image img)
20249 {
20250 NK_ASSERT(ctx);
20251 if (!ctx) return 0;
20252 return nk_button_image_styled(ctx, &ctx->style.button, img);
20253 }
20254 NK_API int
20255 nk_button_symbol_text_styled(struct nk_context *ctx,
20256 const struct nk_style_button *style, enum nk_symbol_type symbol,
20257 const char *text, int len, nk_flags align)
20258 {
20259 struct nk_window *win;
20260 struct nk_panel *layout;
20261 const struct nk_input *in;
20262
20263 struct nk_rect bounds;
20264 enum nk_widget_layout_states state;
20265
20266 NK_ASSERT(ctx);
20267 NK_ASSERT(ctx->current);
20268 NK_ASSERT(ctx->current->layout);
20269 if (!ctx || !ctx->current || !ctx->current->layout)
20270 return 0;
20271
20272 win = ctx->current;
20273 layout = win->layout;
20274
20275 state = nk_widget(&bounds, ctx);
20276 if (!state) return 0;
20277 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20278 return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
20279 symbol, text, len, align, ctx->button_behavior,
20280 style, ctx->style.font, in);
20281 }
20282 NK_API int
20283 nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
20284 const char* text, int len, nk_flags align)
20285 {
20286 NK_ASSERT(ctx);
20287 if (!ctx) return 0;
20288 return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
20289 }
20290 NK_API int nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
20291 const char *label, nk_flags align)
20292 {
20293 return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
20294 }
20295 NK_API int nk_button_symbol_label_styled(struct nk_context *ctx,
20296 const struct nk_style_button *style, enum nk_symbol_type symbol,
20297 const char *title, nk_flags align)
20298 {
20299 return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
20300 }
20301 NK_API int
20302 nk_button_image_text_styled(struct nk_context *ctx,
20303 const struct nk_style_button *style, struct nk_image img, const char *text,
20304 int len, nk_flags align)
20305 {
20306 struct nk_window *win;
20307 struct nk_panel *layout;
20308 const struct nk_input *in;
20309
20310 struct nk_rect bounds;
20311 enum nk_widget_layout_states state;
20312
20313 NK_ASSERT(ctx);
20314 NK_ASSERT(ctx->current);
20315 NK_ASSERT(ctx->current->layout);
20316 if (!ctx || !ctx->current || !ctx->current->layout)
20317 return 0;
20318
20319 win = ctx->current;
20320 layout = win->layout;
20321
20322 state = nk_widget(&bounds, ctx);
20323 if (!state) return 0;
20324 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20325 return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
20326 bounds, img, text, len, align, ctx->button_behavior,
20327 style, ctx->style.font, in);
20328 }
20329 NK_API int
20330 nk_button_image_text(struct nk_context *ctx, struct nk_image img,
20331 const char *text, int len, nk_flags align)
20332 {
20333 return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
20334 }
20335 NK_API int nk_button_image_label(struct nk_context *ctx, struct nk_image img,
20336 const char *label, nk_flags align)
20337 {
20338 return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
20339 }
20340 NK_API int nk_button_image_label_styled(struct nk_context *ctx,
20341 const struct nk_style_button *style, struct nk_image img,
20342 const char *label, nk_flags text_alignment)
20343 {
20344 return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
20345 }
20346
20347
20348
20349
20350
20351 /* ===============================================================
20352 *
20353 * TOGGLE
20354 *
20355 * ===============================================================*/
20356 NK_LIB int
20357 nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
20358 nk_flags *state, int active)
20359 {
20360 nk_widget_state_reset(state);
20361 if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
20362 *state = NK_WIDGET_STATE_ACTIVE;
20363 active = !active;
20364 }
20365 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
20366 *state |= NK_WIDGET_STATE_ENTERED;
20367 else if (nk_input_is_mouse_prev_hovering_rect(in, select))
20368 *state |= NK_WIDGET_STATE_LEFT;
20369 return active;
20370 }
20371 NK_LIB void
20372 nk_draw_checkbox(struct nk_command_buffer *out,
20373 nk_flags state, const struct nk_style_toggle *style, int active,
20374 const struct nk_rect *label, const struct nk_rect *selector,
20375 const struct nk_rect *cursors, const char *string, int len,
20376 const struct nk_user_font *font)
20377 {
20378 const struct nk_style_item *background;
20379 const struct nk_style_item *cursor;
20380 struct nk_text text;
20381
20382 /* select correct colors/images */
20383 if (state & NK_WIDGET_STATE_HOVER) {
20384 background = &style->hover;
20385 cursor = &style->cursor_hover;
20386 text.text = style->text_hover;
20387 } else if (state & NK_WIDGET_STATE_ACTIVED) {
20388 background = &style->hover;
20389 cursor = &style->cursor_hover;
20390 text.text = style->text_active;
20391 } else {
20392 background = &style->normal;
20393 cursor = &style->cursor_normal;
20394 text.text = style->text_normal;
20395 }
20396
20397 /* draw background and cursor */
20398 if (background->type == NK_STYLE_ITEM_COLOR) {
20399 nk_fill_rect(out, *selector, 0, style->border_color);
20400 nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
20401 } else nk_draw_image(out, *selector, &background->data.image, nk_white);
20402 if (active) {
20403 if (cursor->type == NK_STYLE_ITEM_IMAGE)
20404 nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
20405 else nk_fill_rect(out, *cursors, 0, cursor->data.color);
20406 }
20407
20408 text.padding.x = 0;
20409 text.padding.y = 0;
20410 text.background = style->text_background;
20411 nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
20412 }
20413 NK_LIB void
20414 nk_draw_option(struct nk_command_buffer *out,
20415 nk_flags state, const struct nk_style_toggle *style, int active,
20416 const struct nk_rect *label, const struct nk_rect *selector,
20417 const struct nk_rect *cursors, const char *string, int len,
20418 const struct nk_user_font *font)
20419 {
20420 const struct nk_style_item *background;
20421 const struct nk_style_item *cursor;
20422 struct nk_text text;
20423
20424 /* select correct colors/images */
20425 if (state & NK_WIDGET_STATE_HOVER) {
20426 background = &style->hover;
20427 cursor = &style->cursor_hover;
20428 text.text = style->text_hover;
20429 } else if (state & NK_WIDGET_STATE_ACTIVED) {
20430 background = &style->hover;
20431 cursor = &style->cursor_hover;
20432 text.text = style->text_active;
20433 } else {
20434 background = &style->normal;
20435 cursor = &style->cursor_normal;
20436 text.text = style->text_normal;
20437 }
20438
20439 /* draw background and cursor */
20440 if (background->type == NK_STYLE_ITEM_COLOR) {
20441 nk_fill_circle(out, *selector, style->border_color);
20442 nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
20443 } else nk_draw_image(out, *selector, &background->data.image, nk_white);
20444 if (active) {
20445 if (cursor->type == NK_STYLE_ITEM_IMAGE)
20446 nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
20447 else nk_fill_circle(out, *cursors, cursor->data.color);
20448 }
20449
20450 text.padding.x = 0;
20451 text.padding.y = 0;
20452 text.background = style->text_background;
20453 nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
20454 }
20455 NK_LIB int
20456 nk_do_toggle(nk_flags *state,
20457 struct nk_command_buffer *out, struct nk_rect r,
20458 int *active, const char *str, int len, enum nk_toggle_type type,
20459 const struct nk_style_toggle *style, const struct nk_input *in,
20460 const struct nk_user_font *font)
20461 {
20462 int was_active;
20463 struct nk_rect bounds;
20464 struct nk_rect select;
20465 struct nk_rect cursor;
20466 struct nk_rect label;
20467
20468 NK_ASSERT(style);
20469 NK_ASSERT(out);
20470 NK_ASSERT(font);
20471 if (!out || !style || !font || !active)
20472 return 0;
20473
20474 r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
20475 r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
20476
20477 /* add additional touch padding for touch screen devices */
20478 bounds.x = r.x - style->touch_padding.x;
20479 bounds.y = r.y - style->touch_padding.y;
20480 bounds.w = r.w + 2 * style->touch_padding.x;
20481 bounds.h = r.h + 2 * style->touch_padding.y;
20482
20483 /* calculate the selector space */
20484 select.w = font->height;
20485 select.h = select.w;
20486 select.y = r.y + r.h/2.0f - select.h/2.0f;
20487 select.x = r.x;
20488
20489 /* calculate the bounds of the cursor inside the selector */
20490 cursor.x = select.x + style->padding.x + style->border;
20491 cursor.y = select.y + style->padding.y + style->border;
20492 cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
20493 cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
20494
20495 /* label behind the selector */
20496 label.x = select.x + select.w + style->spacing;
20497 label.y = select.y;
20498 label.w = NK_MAX(r.x + r.w, label.x) - label.x;
20499 label.h = select.w;
20500
20501 /* update selector */
20502 was_active = *active;
20503 *active = nk_toggle_behavior(in, bounds, state, *active);
20504
20505 /* draw selector */
20506 if (style->draw_begin)
20507 style->draw_begin(out, style->userdata);
20508 if (type == NK_TOGGLE_CHECK) {
20509 nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
20510 } else {
20511 nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
20512 }
20513 if (style->draw_end)
20514 style->draw_end(out, style->userdata);
20515 return (was_active != *active);
20516 }
20517 /*----------------------------------------------------------------
20518 *
20519 * CHECKBOX
20520 *
20521 * --------------------------------------------------------------*/
20522 NK_API int
20523 nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
20524 {
20525 struct nk_window *win;
20526 struct nk_panel *layout;
20527 const struct nk_input *in;
20528 const struct nk_style *style;
20529
20530 struct nk_rect bounds;
20531 enum nk_widget_layout_states state;
20532
20533 NK_ASSERT(ctx);
20534 NK_ASSERT(ctx->current);
20535 NK_ASSERT(ctx->current->layout);
20536 if (!ctx || !ctx->current || !ctx->current->layout)
20537 return active;
20538
20539 win = ctx->current;
20540 style = &ctx->style;
20541 layout = win->layout;
20542
20543 state = nk_widget(&bounds, ctx);
20544 if (!state) return active;
20545 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20546 nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
20547 text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
20548 return active;
20549 }
20550 NK_API unsigned int
20551 nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
20552 unsigned int flags, unsigned int value)
20553 {
20554 int old_active;
20555 NK_ASSERT(ctx);
20556 NK_ASSERT(text);
20557 if (!ctx || !text) return flags;
20558 old_active = (int)((flags & value) & value);
20559 if (nk_check_text(ctx, text, len, old_active))
20560 flags |= value;
20561 else flags &= ~value;
20562 return flags;
20563 }
20564 NK_API int
20565 nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
20566 {
20567 int old_val;
20568 NK_ASSERT(ctx);
20569 NK_ASSERT(text);
20570 NK_ASSERT(active);
20571 if (!ctx || !text || !active) return 0;
20572 old_val = *active;
20573 *active = nk_check_text(ctx, text, len, *active);
20574 return old_val != *active;
20575 }
20576 NK_API int
20577 nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
20578 unsigned int *flags, unsigned int value)
20579 {
20580 int active;
20581 NK_ASSERT(ctx);
20582 NK_ASSERT(text);
20583 NK_ASSERT(flags);
20584 if (!ctx || !text || !flags) return 0;
20585
20586 active = (int)((*flags & value) & value);
20587 if (nk_checkbox_text(ctx, text, len, &active)) {
20588 if (active) *flags |= value;
20589 else *flags &= ~value;
20590 return 1;
20591 }
20592 return 0;
20593 }
20594 NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
20595 {
20596 return nk_check_text(ctx, label, nk_strlen(label), active);
20597 }
20598 NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
20599 unsigned int flags, unsigned int value)
20600 {
20601 return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
20602 }
20603 NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
20604 {
20605 return nk_checkbox_text(ctx, label, nk_strlen(label), active);
20606 }
20607 NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
20608 unsigned int *flags, unsigned int value)
20609 {
20610 return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
20611 }
20612 /*----------------------------------------------------------------
20613 *
20614 * OPTION
20615 *
20616 * --------------------------------------------------------------*/
20617 NK_API int
20618 nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
20619 {
20620 struct nk_window *win;
20621 struct nk_panel *layout;
20622 const struct nk_input *in;
20623 const struct nk_style *style;
20624
20625 struct nk_rect bounds;
20626 enum nk_widget_layout_states state;
20627
20628 NK_ASSERT(ctx);
20629 NK_ASSERT(ctx->current);
20630 NK_ASSERT(ctx->current->layout);
20631 if (!ctx || !ctx->current || !ctx->current->layout)
20632 return is_active;
20633
20634 win = ctx->current;
20635 style = &ctx->style;
20636 layout = win->layout;
20637
20638 state = nk_widget(&bounds, ctx);
20639 if (!state) return (int)state;
20640 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20641 nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
20642 text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
20643 return is_active;
20644 }
20645 NK_API int
20646 nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
20647 {
20648 int old_value;
20649 NK_ASSERT(ctx);
20650 NK_ASSERT(text);
20651 NK_ASSERT(active);
20652 if (!ctx || !text || !active) return 0;
20653 old_value = *active;
20654 *active = nk_option_text(ctx, text, len, old_value);
20655 return old_value != *active;
20656 }
20657 NK_API int
20658 nk_option_label(struct nk_context *ctx, const char *label, int active)
20659 {
20660 return nk_option_text(ctx, label, nk_strlen(label), active);
20661 }
20662 NK_API int
20663 nk_radio_label(struct nk_context *ctx, const char *label, int *active)
20664 {
20665 return nk_radio_text(ctx, label, nk_strlen(label), active);
20666 }
20667
20668
20669
20670
20671
20672 /* ===============================================================
20673 *
20674 * SELECTABLE
20675 *
20676 * ===============================================================*/
20677 NK_LIB void
20678 nk_draw_selectable(struct nk_command_buffer *out,
20679 nk_flags state, const struct nk_style_selectable *style, int active,
20680 const struct nk_rect *bounds,
20681 const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym,
20682 const char *string, int len, nk_flags align, const struct nk_user_font *font)
20683 {
20684 const struct nk_style_item *background;
20685 struct nk_text text;
20686 text.padding = style->padding;
20687
20688 /* select correct colors/images */
20689 if (!active) {
20690 if (state & NK_WIDGET_STATE_ACTIVED) {
20691 background = &style->pressed;
20692 text.text = style->text_pressed;
20693 } else if (state & NK_WIDGET_STATE_HOVER) {
20694 background = &style->hover;
20695 text.text = style->text_hover;
20696 } else {
20697 background = &style->normal;
20698 text.text = style->text_normal;
20699 }
20700 } else {
20701 if (state & NK_WIDGET_STATE_ACTIVED) {
20702 background = &style->pressed_active;
20703 text.text = style->text_pressed_active;
20704 } else if (state & NK_WIDGET_STATE_HOVER) {
20705 background = &style->hover_active;
20706 text.text = style->text_hover_active;
20707 } else {
20708 background = &style->normal_active;
20709 text.text = style->text_normal_active;
20710 }
20711 }
20712 /* draw selectable background and text */
20713 if (background->type == NK_STYLE_ITEM_IMAGE) {
20714 nk_draw_image(out, *bounds, &background->data.image, nk_white);
20715 text.background = nk_rgba(0,0,0,0);
20716 } else {
20717 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
20718 text.background = background->data.color;
20719 }
20720 if (icon) {
20721 if (img) nk_draw_image(out, *icon, img, nk_white);
20722 else nk_draw_symbol(out, sym, *icon, text.background, text.text, 1, font);
20723 }
20724 nk_widget_text(out, *bounds, string, len, &text, align, font);
20725 }
20726 NK_LIB int
20727 nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
20728 struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
20729 const struct nk_style_selectable *style, const struct nk_input *in,
20730 const struct nk_user_font *font)
20731 {
20732 int old_value;
20733 struct nk_rect touch;
20734
20735 NK_ASSERT(state);
20736 NK_ASSERT(out);
20737 NK_ASSERT(str);
20738 NK_ASSERT(len);
20739 NK_ASSERT(value);
20740 NK_ASSERT(style);
20741 NK_ASSERT(font);
20742
20743 if (!state || !out || !str || !len || !value || !style || !font) return 0;
20744 old_value = *value;
20745
20746 /* remove padding */
20747 touch.x = bounds.x - style->touch_padding.x;
20748 touch.y = bounds.y - style->touch_padding.y;
20749 touch.w = bounds.w + style->touch_padding.x * 2;
20750 touch.h = bounds.h + style->touch_padding.y * 2;
20751
20752 /* update button */
20753 if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
20754 *value = !(*value);
20755
20756 /* draw selectable */
20757 if (style->draw_begin) style->draw_begin(out, style->userdata);
20758 nk_draw_selectable(out, *state, style, *value, &bounds, 0,0,NK_SYMBOL_NONE, str, len, align, font);
20759 if (style->draw_end) style->draw_end(out, style->userdata);
20760 return old_value != *value;
20761 }
20762 NK_LIB int
20763 nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
20764 struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
20765 const struct nk_image *img, const struct nk_style_selectable *style,
20766 const struct nk_input *in, const struct nk_user_font *font)
20767 {
20768 int old_value;
20769 struct nk_rect touch;
20770 struct nk_rect icon;
20771
20772 NK_ASSERT(state);
20773 NK_ASSERT(out);
20774 NK_ASSERT(str);
20775 NK_ASSERT(len);
20776 NK_ASSERT(value);
20777 NK_ASSERT(style);
20778 NK_ASSERT(font);
20779
20780 if (!state || !out || !str || !len || !value || !style || !font) return 0;
20781 old_value = *value;
20782
20783 /* toggle behavior */
20784 touch.x = bounds.x - style->touch_padding.x;
20785 touch.y = bounds.y - style->touch_padding.y;
20786 touch.w = bounds.w + style->touch_padding.x * 2;
20787 touch.h = bounds.h + style->touch_padding.y * 2;
20788 if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
20789 *value = !(*value);
20790
20791 icon.y = bounds.y + style->padding.y;
20792 icon.w = icon.h = bounds.h - 2 * style->padding.y;
20793 if (align & NK_TEXT_ALIGN_LEFT) {
20794 icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
20795 icon.x = NK_MAX(icon.x, 0);
20796 } else icon.x = bounds.x + 2 * style->padding.x;
20797
20798 icon.x += style->image_padding.x;
20799 icon.y += style->image_padding.y;
20800 icon.w -= 2 * style->image_padding.x;
20801 icon.h -= 2 * style->image_padding.y;
20802
20803 /* draw selectable */
20804 if (style->draw_begin) style->draw_begin(out, style->userdata);
20805 nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, NK_SYMBOL_NONE, str, len, align, font);
20806 if (style->draw_end) style->draw_end(out, style->userdata);
20807 return old_value != *value;
20808 }
20809 NK_LIB int
20810 nk_do_selectable_symbol(nk_flags *state, struct nk_command_buffer *out,
20811 struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
20812 enum nk_symbol_type sym, const struct nk_style_selectable *style,
20813 const struct nk_input *in, const struct nk_user_font *font)
20814 {
20815 int old_value;
20816 struct nk_rect touch;
20817 struct nk_rect icon;
20818
20819 NK_ASSERT(state);
20820 NK_ASSERT(out);
20821 NK_ASSERT(str);
20822 NK_ASSERT(len);
20823 NK_ASSERT(value);
20824 NK_ASSERT(style);
20825 NK_ASSERT(font);
20826
20827 if (!state || !out || !str || !len || !value || !style || !font) return 0;
20828 old_value = *value;
20829
20830 /* toggle behavior */
20831 touch.x = bounds.x - style->touch_padding.x;
20832 touch.y = bounds.y - style->touch_padding.y;
20833 touch.w = bounds.w + style->touch_padding.x * 2;
20834 touch.h = bounds.h + style->touch_padding.y * 2;
20835 if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
20836 *value = !(*value);
20837
20838 icon.y = bounds.y + style->padding.y;
20839 icon.w = icon.h = bounds.h - 2 * style->padding.y;
20840 if (align & NK_TEXT_ALIGN_LEFT) {
20841 icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
20842 icon.x = NK_MAX(icon.x, 0);
20843 } else icon.x = bounds.x + 2 * style->padding.x;
20844
20845 icon.x += style->image_padding.x;
20846 icon.y += style->image_padding.y;
20847 icon.w -= 2 * style->image_padding.x;
20848 icon.h -= 2 * style->image_padding.y;
20849
20850 /* draw selectable */
20851 if (style->draw_begin) style->draw_begin(out, style->userdata);
20852 nk_draw_selectable(out, *state, style, *value, &bounds, &icon, 0, sym, str, len, align, font);
20853 if (style->draw_end) style->draw_end(out, style->userdata);
20854 return old_value != *value;
20855 }
20856
20857 NK_API int
20858 nk_selectable_text(struct nk_context *ctx, const char *str, int len,
20859 nk_flags align, int *value)
20860 {
20861 struct nk_window *win;
20862 struct nk_panel *layout;
20863 const struct nk_input *in;
20864 const struct nk_style *style;
20865
20866 enum nk_widget_layout_states state;
20867 struct nk_rect bounds;
20868
20869 NK_ASSERT(ctx);
20870 NK_ASSERT(value);
20871 NK_ASSERT(ctx->current);
20872 NK_ASSERT(ctx->current->layout);
20873 if (!ctx || !ctx->current || !ctx->current->layout || !value)
20874 return 0;
20875
20876 win = ctx->current;
20877 layout = win->layout;
20878 style = &ctx->style;
20879
20880 state = nk_widget(&bounds, ctx);
20881 if (!state) return 0;
20882 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20883 return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
20884 str, len, align, value, &style->selectable, in, style->font);
20885 }
20886 NK_API int
20887 nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
20888 const char *str, int len, nk_flags align, int *value)
20889 {
20890 struct nk_window *win;
20891 struct nk_panel *layout;
20892 const struct nk_input *in;
20893 const struct nk_style *style;
20894
20895 enum nk_widget_layout_states state;
20896 struct nk_rect bounds;
20897
20898 NK_ASSERT(ctx);
20899 NK_ASSERT(value);
20900 NK_ASSERT(ctx->current);
20901 NK_ASSERT(ctx->current->layout);
20902 if (!ctx || !ctx->current || !ctx->current->layout || !value)
20903 return 0;
20904
20905 win = ctx->current;
20906 layout = win->layout;
20907 style = &ctx->style;
20908
20909 state = nk_widget(&bounds, ctx);
20910 if (!state) return 0;
20911 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20912 return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
20913 str, len, align, value, &img, &style->selectable, in, style->font);
20914 }
20915 NK_API int
20916 nk_selectable_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
20917 const char *str, int len, nk_flags align, int *value)
20918 {
20919 struct nk_window *win;
20920 struct nk_panel *layout;
20921 const struct nk_input *in;
20922 const struct nk_style *style;
20923
20924 enum nk_widget_layout_states state;
20925 struct nk_rect bounds;
20926
20927 NK_ASSERT(ctx);
20928 NK_ASSERT(value);
20929 NK_ASSERT(ctx->current);
20930 NK_ASSERT(ctx->current->layout);
20931 if (!ctx || !ctx->current || !ctx->current->layout || !value)
20932 return 0;
20933
20934 win = ctx->current;
20935 layout = win->layout;
20936 style = &ctx->style;
20937
20938 state = nk_widget(&bounds, ctx);
20939 if (!state) return 0;
20940 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
20941 return nk_do_selectable_symbol(&ctx->last_widget_state, &win->buffer, bounds,
20942 str, len, align, value, sym, &style->selectable, in, style->font);
20943 }
20944 NK_API int
20945 nk_selectable_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
20946 const char *title, nk_flags align, int *value)
20947 {
20948 return nk_selectable_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
20949 }
20950 NK_API int nk_select_text(struct nk_context *ctx, const char *str, int len,
20951 nk_flags align, int value)
20952 {
20953 nk_selectable_text(ctx, str, len, align, &value);return value;
20954 }
20955 NK_API int nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, int *value)
20956 {
20957 return nk_selectable_text(ctx, str, nk_strlen(str), align, value);
20958 }
20959 NK_API int nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
20960 const char *str, nk_flags align, int *value)
20961 {
20962 return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);
20963 }
20964 NK_API int nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, int value)
20965 {
20966 nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;
20967 }
20968 NK_API int nk_select_image_label(struct nk_context *ctx, struct nk_image img,
20969 const char *str, nk_flags align, int value)
20970 {
20971 nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;
20972 }
20973 NK_API int nk_select_image_text(struct nk_context *ctx, struct nk_image img,
20974 const char *str, int len, nk_flags align, int value)
20975 {
20976 nk_selectable_image_text(ctx, img, str, len, align, &value);return value;
20977 }
20978 NK_API int
20979 nk_select_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
20980 const char *title, int title_len, nk_flags align, int value)
20981 {
20982 nk_selectable_symbol_text(ctx, sym, title, title_len, align, &value);return value;
20983 }
20984 NK_API int
20985 nk_select_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
20986 const char *title, nk_flags align, int value)
20987 {
20988 return nk_select_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
20989 }
20990
20991
20992
20993
20994
20995 /* ===============================================================
20996 *
20997 * SLIDER
20998 *
20999 * ===============================================================*/
21000 NK_LIB float
21001 nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
21002 struct nk_rect *visual_cursor, struct nk_input *in,
21003 struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
21004 float slider_step, float slider_steps)
21005 {
21006 int left_mouse_down;
21007 int left_mouse_click_in_cursor;
21008
21009 /* check if visual cursor is being dragged */
21010 nk_widget_state_reset(state);
21011 left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
21012 left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
21013 NK_BUTTON_LEFT, *visual_cursor, nk_true);
21014
21015 if (left_mouse_down && left_mouse_click_in_cursor) {
21016 float ratio = 0;
21017 const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
21018 const float pxstep = bounds.w / slider_steps;
21019
21020 /* only update value if the next slider step is reached */
21021 *state = NK_WIDGET_STATE_ACTIVE;
21022 if (NK_ABS(d) >= pxstep) {
21023 const float steps = (float)((int)(NK_ABS(d) / pxstep));
21024 slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
21025 slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
21026 ratio = (slider_value - slider_min)/slider_step;
21027 logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
21028 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
21029 }
21030 }
21031
21032 /* slider widget state */
21033 if (nk_input_is_mouse_hovering_rect(in, bounds))
21034 *state = NK_WIDGET_STATE_HOVERED;
21035 if (*state & NK_WIDGET_STATE_HOVER &&
21036 !nk_input_is_mouse_prev_hovering_rect(in, bounds))
21037 *state |= NK_WIDGET_STATE_ENTERED;
21038 else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
21039 *state |= NK_WIDGET_STATE_LEFT;
21040 return slider_value;
21041 }
21042 NK_LIB void
21043 nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
21044 const struct nk_style_slider *style, const struct nk_rect *bounds,
21045 const struct nk_rect *visual_cursor, float min, float value, float max)
21046 {
21047 struct nk_rect fill;
21048 struct nk_rect bar;
21049 const struct nk_style_item *background;
21050
21051 /* select correct slider images/colors */
21052 struct nk_color bar_color;
21053 const struct nk_style_item *cursor;
21054
21055 NK_UNUSED(min);
21056 NK_UNUSED(max);
21057 NK_UNUSED(value);
21058
21059 if (state & NK_WIDGET_STATE_ACTIVED) {
21060 background = &style->active;
21061 bar_color = style->bar_active;
21062 cursor = &style->cursor_active;
21063 } else if (state & NK_WIDGET_STATE_HOVER) {
21064 background = &style->hover;
21065 bar_color = style->bar_hover;
21066 cursor = &style->cursor_hover;
21067 } else {
21068 background = &style->normal;
21069 bar_color = style->bar_normal;
21070 cursor = &style->cursor_normal;
21071 }
21072 /* calculate slider background bar */
21073 bar.x = bounds->x;
21074 bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
21075 bar.w = bounds->w;
21076 bar.h = bounds->h/6;
21077
21078 /* filled background bar style */
21079 fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
21080 fill.x = bar.x;
21081 fill.y = bar.y;
21082 fill.h = bar.h;
21083
21084 /* draw background */
21085 if (background->type == NK_STYLE_ITEM_IMAGE) {
21086 nk_draw_image(out, *bounds, &background->data.image, nk_white);
21087 } else {
21088 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
21089 nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
21090 }
21091
21092 /* draw slider bar */
21093 nk_fill_rect(out, bar, style->rounding, bar_color);
21094 nk_fill_rect(out, fill, style->rounding, style->bar_filled);
21095
21096 /* draw cursor */
21097 if (cursor->type == NK_STYLE_ITEM_IMAGE)
21098 nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
21099 else nk_fill_circle(out, *visual_cursor, cursor->data.color);
21100 }
21101 NK_LIB float
21102 nk_do_slider(nk_flags *state,
21103 struct nk_command_buffer *out, struct nk_rect bounds,
21104 float min, float val, float max, float step,
21105 const struct nk_style_slider *style, struct nk_input *in,
21106 const struct nk_user_font *font)
21107 {
21108 float slider_range;
21109 float slider_min;
21110 float slider_max;
21111 float slider_value;
21112 float slider_steps;
21113 float cursor_offset;
21114
21115 struct nk_rect visual_cursor;
21116 struct nk_rect logical_cursor;
21117
21118 NK_ASSERT(style);
21119 NK_ASSERT(out);
21120 if (!out || !style)
21121 return 0;
21122
21123 /* remove padding from slider bounds */
21124 bounds.x = bounds.x + style->padding.x;
21125 bounds.y = bounds.y + style->padding.y;
21126 bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
21127 bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
21128 bounds.w -= 2 * style->padding.x;
21129 bounds.h -= 2 * style->padding.y;
21130
21131 /* optional buttons */
21132 if (style->show_buttons) {
21133 nk_flags ws;
21134 struct nk_rect button;
21135 button.y = bounds.y;
21136 button.w = bounds.h;
21137 button.h = bounds.h;
21138
21139 /* decrement button */
21140 button.x = bounds.x;
21141 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
21142 &style->dec_button, in, font))
21143 val -= step;
21144
21145 /* increment button */
21146 button.x = (bounds.x + bounds.w) - button.w;
21147 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
21148 &style->inc_button, in, font))
21149 val += step;
21150
21151 bounds.x = bounds.x + button.w + style->spacing.x;
21152 bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
21153 }
21154
21155 /* remove one cursor size to support visual cursor */
21156 bounds.x += style->cursor_size.x*0.5f;
21157 bounds.w -= style->cursor_size.x;
21158
21159 /* make sure the provided values are correct */
21160 slider_max = NK_MAX(min, max);
21161 slider_min = NK_MIN(min, max);
21162 slider_value = NK_CLAMP(slider_min, val, slider_max);
21163 slider_range = slider_max - slider_min;
21164 slider_steps = slider_range / step;
21165 cursor_offset = (slider_value - slider_min) / step;
21166
21167 /* calculate cursor
21168 Basically you have two cursors. One for visual representation and interaction
21169 and one for updating the actual cursor value. */
21170 logical_cursor.h = bounds.h;
21171 logical_cursor.w = bounds.w / slider_steps;
21172 logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
21173 logical_cursor.y = bounds.y;
21174
21175 visual_cursor.h = style->cursor_size.y;
21176 visual_cursor.w = style->cursor_size.x;
21177 visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
21178 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
21179
21180 slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
21181 in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
21182 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
21183
21184 /* draw slider */
21185 if (style->draw_begin) style->draw_begin(out, style->userdata);
21186 nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
21187 if (style->draw_end) style->draw_end(out, style->userdata);
21188 return slider_value;
21189 }
21190 NK_API int
21191 nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
21192 float value_step)
21193 {
21194 struct nk_window *win;
21195 struct nk_panel *layout;
21196 struct nk_input *in;
21197 const struct nk_style *style;
21198
21199 int ret = 0;
21200 float old_value;
21201 struct nk_rect bounds;
21202 enum nk_widget_layout_states state;
21203
21204 NK_ASSERT(ctx);
21205 NK_ASSERT(ctx->current);
21206 NK_ASSERT(ctx->current->layout);
21207 NK_ASSERT(value);
21208 if (!ctx || !ctx->current || !ctx->current->layout || !value)
21209 return ret;
21210
21211 win = ctx->current;
21212 style = &ctx->style;
21213 layout = win->layout;
21214
21215 state = nk_widget(&bounds, ctx);
21216 if (!state) return ret;
21217 in = (/*state == NK_WIDGET_ROM || */ layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
21218
21219 old_value = *value;
21220 *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
21221 old_value, max_value, value_step, &style->slider, in, style->font);
21222 return (old_value > *value || old_value < *value);
21223 }
21224 NK_API float
21225 nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
21226 {
21227 nk_slider_float(ctx, min, &val, max, step); return val;
21228 }
21229 NK_API int
21230 nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
21231 {
21232 float value = (float)val;
21233 nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
21234 return (int)value;
21235 }
21236 NK_API int
21237 nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
21238 {
21239 int ret;
21240 float value = (float)*val;
21241 ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
21242 *val = (int)value;
21243 return ret;
21244 }
21245
21246
21247
21248
21249
21250 /* ===============================================================
21251 *
21252 * PROGRESS
21253 *
21254 * ===============================================================*/
21255 NK_LIB nk_size
21256 nk_progress_behavior(nk_flags *state, struct nk_input *in,
21257 struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable)
21258 {
21259 int left_mouse_down = 0;
21260 int left_mouse_click_in_cursor = 0;
21261
21262 nk_widget_state_reset(state);
21263 if (!in || !modifiable) return value;
21264 left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
21265 left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
21266 NK_BUTTON_LEFT, cursor, nk_true);
21267 if (nk_input_is_mouse_hovering_rect(in, r))
21268 *state = NK_WIDGET_STATE_HOVERED;
21269
21270 if (in && left_mouse_down && left_mouse_click_in_cursor) {
21271 if (left_mouse_down && left_mouse_click_in_cursor) {
21272 float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
21273 value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
21274 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
21275 *state |= NK_WIDGET_STATE_ACTIVE;
21276 }
21277 }
21278 /* set progressbar widget state */
21279 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
21280 *state |= NK_WIDGET_STATE_ENTERED;
21281 else if (nk_input_is_mouse_prev_hovering_rect(in, r))
21282 *state |= NK_WIDGET_STATE_LEFT;
21283 return value;
21284 }
21285 NK_LIB void
21286 nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
21287 const struct nk_style_progress *style, const struct nk_rect *bounds,
21288 const struct nk_rect *scursor, nk_size value, nk_size max)
21289 {
21290 const struct nk_style_item *background;
21291 const struct nk_style_item *cursor;
21292
21293 NK_UNUSED(max);
21294 NK_UNUSED(value);
21295
21296 /* select correct colors/images to draw */
21297 if (state & NK_WIDGET_STATE_ACTIVED) {
21298 background = &style->active;
21299 cursor = &style->cursor_active;
21300 } else if (state & NK_WIDGET_STATE_HOVER){
21301 background = &style->hover;
21302 cursor = &style->cursor_hover;
21303 } else {
21304 background = &style->normal;
21305 cursor = &style->cursor_normal;
21306 }
21307
21308 /* draw background */
21309 if (background->type == NK_STYLE_ITEM_COLOR) {
21310 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
21311 nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
21312 } else nk_draw_image(out, *bounds, &background->data.image, nk_white);
21313
21314 /* draw cursor */
21315 if (cursor->type == NK_STYLE_ITEM_COLOR) {
21316 nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
21317 nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
21318 } else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
21319 }
21320 NK_LIB nk_size
21321 nk_do_progress(nk_flags *state,
21322 struct nk_command_buffer *out, struct nk_rect bounds,
21323 nk_size value, nk_size max, int modifiable,
21324 const struct nk_style_progress *style, struct nk_input *in)
21325 {
21326 float prog_scale;
21327 nk_size prog_value;
21328 struct nk_rect cursor;
21329
21330 NK_ASSERT(style);
21331 NK_ASSERT(out);
21332 if (!out || !style) return 0;
21333
21334 /* calculate progressbar cursor */
21335 cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
21336 cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
21337 cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
21338 prog_scale = (float)value / (float)max;
21339
21340 /* update progressbar */
21341 prog_value = NK_MIN(value, max);
21342 prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
21343 cursor.w = cursor.w * prog_scale;
21344
21345 /* draw progressbar */
21346 if (style->draw_begin) style->draw_begin(out, style->userdata);
21347 nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
21348 if (style->draw_end) style->draw_end(out, style->userdata);
21349 return prog_value;
21350 }
21351 NK_API int
21352 nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, int is_modifyable)
21353 {
21354 struct nk_window *win;
21355 struct nk_panel *layout;
21356 const struct nk_style *style;
21357 struct nk_input *in;
21358
21359 struct nk_rect bounds;
21360 enum nk_widget_layout_states state;
21361 nk_size old_value;
21362
21363 NK_ASSERT(ctx);
21364 NK_ASSERT(cur);
21365 NK_ASSERT(ctx->current);
21366 NK_ASSERT(ctx->current->layout);
21367 if (!ctx || !ctx->current || !ctx->current->layout || !cur)
21368 return 0;
21369
21370 win = ctx->current;
21371 style = &ctx->style;
21372 layout = win->layout;
21373 state = nk_widget(&bounds, ctx);
21374 if (!state) return 0;
21375
21376 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
21377 old_value = *cur;
21378 *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
21379 *cur, max, is_modifyable, &style->progress, in);
21380 return (*cur != old_value);
21381 }
21382 NK_API nk_size
21383 nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, int modifyable)
21384 {
21385 nk_progress(ctx, &cur, max, modifyable);
21386 return cur;
21387 }
21388
21389
21390
21391
21392
21393 /* ===============================================================
21394 *
21395 * SCROLLBAR
21396 *
21397 * ===============================================================*/
21398 NK_LIB float
21399 nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
21400 int has_scrolling, const struct nk_rect *scroll,
21401 const struct nk_rect *cursor, const struct nk_rect *empty0,
21402 const struct nk_rect *empty1, float scroll_offset,
21403 float target, float scroll_step, enum nk_orientation o)
21404 {
21405 nk_flags ws = 0;
21406 int left_mouse_down;
21407 int left_mouse_clicked;
21408 int left_mouse_click_in_cursor;
21409 float scroll_delta;
21410
21411 nk_widget_state_reset(state);
21412 if (!in) return scroll_offset;
21413
21414 left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
21415 left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
21416 left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
21417 NK_BUTTON_LEFT, *cursor, nk_true);
21418 if (nk_input_is_mouse_hovering_rect(in, *scroll))
21419 *state = NK_WIDGET_STATE_HOVERED;
21420
21421 scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
21422 if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
21423 /* update cursor by mouse dragging */
21424 float pixel, delta;
21425 *state = NK_WIDGET_STATE_ACTIVE;
21426 if (o == NK_VERTICAL) {
21427 float cursor_y;
21428 pixel = in->mouse.delta.y;
21429 delta = (pixel / scroll->h) * target;
21430 scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
21431 cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
21432 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
21433 } else {
21434 float cursor_x;
21435 pixel = in->mouse.delta.x;
21436 delta = (pixel / scroll->w) * target;
21437 scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
21438 cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
21439 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
21440 }
21441 } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
21442 nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
21443 /* scroll page up by click on empty space or shortcut */
21444 if (o == NK_VERTICAL)
21445 scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
21446 else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
21447 } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
21448 nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
21449 /* scroll page down by click on empty space or shortcut */
21450 if (o == NK_VERTICAL)
21451 scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
21452 else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
21453 } else if (has_scrolling) {
21454 if ((scroll_delta < 0 || (scroll_delta > 0))) {
21455 /* update cursor by mouse scrolling */
21456 scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
21457 if (o == NK_VERTICAL)
21458 scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
21459 else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
21460 } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
21461 /* update cursor to the beginning */
21462 if (o == NK_VERTICAL) scroll_offset = 0;
21463 } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
21464 /* update cursor to the end */
21465 if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
21466 }
21467 }
21468 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
21469 *state |= NK_WIDGET_STATE_ENTERED;
21470 else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
21471 *state |= NK_WIDGET_STATE_LEFT;
21472 return scroll_offset;
21473 }
21474 NK_LIB void
21475 nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
21476 const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
21477 const struct nk_rect *scroll)
21478 {
21479 const struct nk_style_item *background;
21480 const struct nk_style_item *cursor;
21481
21482 /* select correct colors/images to draw */
21483 if (state & NK_WIDGET_STATE_ACTIVED) {
21484 background = &style->active;
21485 cursor = &style->cursor_active;
21486 } else if (state & NK_WIDGET_STATE_HOVER) {
21487 background = &style->hover;
21488 cursor = &style->cursor_hover;
21489 } else {
21490 background = &style->normal;
21491 cursor = &style->cursor_normal;
21492 }
21493
21494 /* draw background */
21495 if (background->type == NK_STYLE_ITEM_COLOR) {
21496 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
21497 nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
21498 } else {
21499 nk_draw_image(out, *bounds, &background->data.image, nk_white);
21500 }
21501
21502 /* draw cursor */
21503 if (cursor->type == NK_STYLE_ITEM_COLOR) {
21504 nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
21505 nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
21506 } else nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
21507 }
21508 NK_LIB float
21509 nk_do_scrollbarv(nk_flags *state,
21510 struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
21511 float offset, float target, float step, float button_pixel_inc,
21512 const struct nk_style_scrollbar *style, struct nk_input *in,
21513 const struct nk_user_font *font)
21514 {
21515 struct nk_rect empty_north;
21516 struct nk_rect empty_south;
21517 struct nk_rect cursor;
21518
21519 float scroll_step;
21520 float scroll_offset;
21521 float scroll_off;
21522 float scroll_ratio;
21523
21524 NK_ASSERT(out);
21525 NK_ASSERT(style);
21526 NK_ASSERT(state);
21527 if (!out || !style) return 0;
21528
21529 scroll.w = NK_MAX(scroll.w, 1);
21530 scroll.h = NK_MAX(scroll.h, 0);
21531 if (target <= scroll.h) return 0;
21532
21533 /* optional scrollbar buttons */
21534 if (style->show_buttons) {
21535 nk_flags ws;
21536 float scroll_h;
21537 struct nk_rect button;
21538
21539 button.x = scroll.x;
21540 button.w = scroll.w;
21541 button.h = scroll.w;
21542
21543 scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
21544 scroll_step = NK_MIN(step, button_pixel_inc);
21545
21546 /* decrement button */
21547 button.y = scroll.y;
21548 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
21549 NK_BUTTON_REPEATER, &style->dec_button, in, font))
21550 offset = offset - scroll_step;
21551
21552 /* increment button */
21553 button.y = scroll.y + scroll.h - button.h;
21554 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
21555 NK_BUTTON_REPEATER, &style->inc_button, in, font))
21556 offset = offset + scroll_step;
21557
21558 scroll.y = scroll.y + button.h;
21559 scroll.h = scroll_h;
21560 }
21561
21562 /* calculate scrollbar constants */
21563 scroll_step = NK_MIN(step, scroll.h);
21564 scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
21565 scroll_ratio = scroll.h / target;
21566 scroll_off = scroll_offset / target;
21567
21568 /* calculate scrollbar cursor bounds */
21569 cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
21570 cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
21571 cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
21572 cursor.x = scroll.x + style->border + style->padding.x;
21573
21574 /* calculate empty space around cursor */
21575 empty_north.x = scroll.x;
21576 empty_north.y = scroll.y;
21577 empty_north.w = scroll.w;
21578 empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
21579
21580 empty_south.x = scroll.x;
21581 empty_south.y = cursor.y + cursor.h;
21582 empty_south.w = scroll.w;
21583 empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
21584
21585 /* update scrollbar */
21586 scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
21587 &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
21588 scroll_off = scroll_offset / target;
21589 cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
21590
21591 /* draw scrollbar */
21592 if (style->draw_begin) style->draw_begin(out, style->userdata);
21593 nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
21594 if (style->draw_end) style->draw_end(out, style->userdata);
21595 return scroll_offset;
21596 }
21597 NK_LIB float
21598 nk_do_scrollbarh(nk_flags *state,
21599 struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
21600 float offset, float target, float step, float button_pixel_inc,
21601 const struct nk_style_scrollbar *style, struct nk_input *in,
21602 const struct nk_user_font *font)
21603 {
21604 struct nk_rect cursor;
21605 struct nk_rect empty_west;
21606 struct nk_rect empty_east;
21607
21608 float scroll_step;
21609 float scroll_offset;
21610 float scroll_off;
21611 float scroll_ratio;
21612
21613 NK_ASSERT(out);
21614 NK_ASSERT(style);
21615 if (!out || !style) return 0;
21616
21617 /* scrollbar background */
21618 scroll.h = NK_MAX(scroll.h, 1);
21619 scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
21620 if (target <= scroll.w) return 0;
21621
21622 /* optional scrollbar buttons */
21623 if (style->show_buttons) {
21624 nk_flags ws;
21625 float scroll_w;
21626 struct nk_rect button;
21627 button.y = scroll.y;
21628 button.w = scroll.h;
21629 button.h = scroll.h;
21630
21631 scroll_w = scroll.w - 2 * button.w;
21632 scroll_step = NK_MIN(step, button_pixel_inc);
21633
21634 /* decrement button */
21635 button.x = scroll.x;
21636 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
21637 NK_BUTTON_REPEATER, &style->dec_button, in, font))
21638 offset = offset - scroll_step;
21639
21640 /* increment button */
21641 button.x = scroll.x + scroll.w - button.w;
21642 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
21643 NK_BUTTON_REPEATER, &style->inc_button, in, font))
21644 offset = offset + scroll_step;
21645
21646 scroll.x = scroll.x + button.w;
21647 scroll.w = scroll_w;
21648 }
21649
21650 /* calculate scrollbar constants */
21651 scroll_step = NK_MIN(step, scroll.w);
21652 scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
21653 scroll_ratio = scroll.w / target;
21654 scroll_off = scroll_offset / target;
21655
21656 /* calculate cursor bounds */
21657 cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
21658 cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
21659 cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
21660 cursor.y = scroll.y + style->border + style->padding.y;
21661
21662 /* calculate empty space around cursor */
21663 empty_west.x = scroll.x;
21664 empty_west.y = scroll.y;
21665 empty_west.w = cursor.x - scroll.x;
21666 empty_west.h = scroll.h;
21667
21668 empty_east.x = cursor.x + cursor.w;
21669 empty_east.y = scroll.y;
21670 empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
21671 empty_east.h = scroll.h;
21672
21673 /* update scrollbar */
21674 scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
21675 &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
21676 scroll_off = scroll_offset / target;
21677 cursor.x = scroll.x + (scroll_off * scroll.w);
21678
21679 /* draw scrollbar */
21680 if (style->draw_begin) style->draw_begin(out, style->userdata);
21681 nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
21682 if (style->draw_end) style->draw_end(out, style->userdata);
21683 return scroll_offset;
21684 }
21685
21686
21687
21688
21689
21690 /* ===============================================================
21691 *
21692 * TEXT EDITOR
21693 *
21694 * ===============================================================*/
21695 /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
21696 struct nk_text_find {
21697 float x,y; /* position of n'th character */
21698 float height; /* height of line */
21699 int first_char, length; /* first char of row, and length */
21700 int prev_first; /*_ first char of previous row */
21701 };
21702
21703 struct nk_text_edit_row {
21704 float x0,x1;
21705 /* starting x location, end x location (allows for align=right, etc) */
21706 float baseline_y_delta;
21707 /* position of baseline relative to previous row's baseline*/
21708 float ymin,ymax;
21709 /* height of row above and below baseline */
21710 int num_chars;
21711 };
21712
21713 /* forward declarations */
21714 NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
21715 NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
21716 NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
21717 #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
21718
21719 NK_INTERN float
21720 nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
21721 const struct nk_user_font *font)
21722 {
21723 int len = 0;
21724 nk_rune unicode = 0;
21725 const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
21726 return font->width(font->userdata, font->height, str, len);
21727 }
21728 NK_INTERN void
21729 nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
21730 int line_start_id, float row_height, const struct nk_user_font *font)
21731 {
21732 int l;
21733 int glyphs = 0;
21734 nk_rune unicode;
21735 const char *remaining;
21736 int len = nk_str_len_char(&edit->string);
21737 const char *end = nk_str_get_const(&edit->string) + len;
21738 const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
21739 const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
21740 text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
21741
21742 r->x0 = 0.0f;
21743 r->x1 = size.x;
21744 r->baseline_y_delta = size.y;
21745 r->ymin = 0.0f;
21746 r->ymax = size.y;
21747 r->num_chars = glyphs;
21748 }
21749 NK_INTERN int
21750 nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
21751 const struct nk_user_font *font, float row_height)
21752 {
21753 struct nk_text_edit_row r;
21754 int n = edit->string.len;
21755 float base_y = 0, prev_x;
21756 int i=0, k;
21757
21758 r.x0 = r.x1 = 0;
21759 r.ymin = r.ymax = 0;
21760 r.num_chars = 0;
21761
21762 /* search rows to find one that straddles 'y' */
21763 while (i < n) {
21764 nk_textedit_layout_row(&r, edit, i, row_height, font);
21765 if (r.num_chars <= 0)
21766 return n;
21767
21768 if (i==0 && y < base_y + r.ymin)
21769 return 0;
21770
21771 if (y < base_y + r.ymax)
21772 break;
21773
21774 i += r.num_chars;
21775 base_y += r.baseline_y_delta;
21776 }
21777
21778 /* below all text, return 'after' last character */
21779 if (i >= n)
21780 return n;
21781
21782 /* check if it's before the beginning of the line */
21783 if (x < r.x0)
21784 return i;
21785
21786 /* check if it's before the end of the line */
21787 if (x < r.x1) {
21788 /* search characters in row for one that straddles 'x' */
21789 k = i;
21790 prev_x = r.x0;
21791 for (i=0; i < r.num_chars; ++i) {
21792 float w = nk_textedit_get_width(edit, k, i, font);
21793 if (x < prev_x+w) {
21794 if (x < prev_x+w/2)
21795 return k+i;
21796 else return k+i+1;
21797 }
21798 prev_x += w;
21799 }
21800 /* shouldn't happen, but if it does, fall through to end-of-line case */
21801 }
21802
21803 /* if the last character is a newline, return that.
21804 * otherwise return 'after' the last character */
21805 if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
21806 return i+r.num_chars-1;
21807 else return i+r.num_chars;
21808 }
21809 NK_LIB void
21810 nk_textedit_click(struct nk_text_edit *state, float x, float y,
21811 const struct nk_user_font *font, float row_height)
21812 {
21813 /* API click: on mouse down, move the cursor to the clicked location,
21814 * and reset the selection */
21815 state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
21816 state->select_start = state->cursor;
21817 state->select_end = state->cursor;
21818 state->has_preferred_x = 0;
21819 }
21820 NK_LIB void
21821 nk_textedit_drag(struct nk_text_edit *state, float x, float y,
21822 const struct nk_user_font *font, float row_height)
21823 {
21824 /* API drag: on mouse drag, move the cursor and selection endpoint
21825 * to the clicked location */
21826 int p = nk_textedit_locate_coord(state, x, y, font, row_height);
21827 if (state->select_start == state->select_end)
21828 state->select_start = state->cursor;
21829 state->cursor = state->select_end = p;
21830 }
21831 NK_INTERN void
21832 nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
21833 int n, int single_line, const struct nk_user_font *font, float row_height)
21834 {
21835 /* find the x/y location of a character, and remember info about the previous
21836 * row in case we get a move-up event (for page up, we'll have to rescan) */
21837 struct nk_text_edit_row r;
21838 int prev_start = 0;
21839 int z = state->string.len;
21840 int i=0, first;
21841
21842 nk_zero_struct(r);
21843 if (n == z) {
21844 /* if it's at the end, then find the last line -- simpler than trying to
21845 explicitly handle this case in the regular code */
21846 nk_textedit_layout_row(&r, state, 0, row_height, font);
21847 if (single_line) {
21848 find->first_char = 0;
21849 find->length = z;
21850 } else {
21851 while (i < z) {
21852 prev_start = i;
21853 i += r.num_chars;
21854 nk_textedit_layout_row(&r, state, i, row_height, font);
21855 }
21856
21857 find->first_char = i;
21858 find->length = r.num_chars;
21859 }
21860 find->x = r.x1;
21861 find->y = r.ymin;
21862 find->height = r.ymax - r.ymin;
21863 find->prev_first = prev_start;
21864 return;
21865 }
21866
21867 /* search rows to find the one that straddles character n */
21868 find->y = 0;
21869
21870 for(;;) {
21871 nk_textedit_layout_row(&r, state, i, row_height, font);
21872 if (n < i + r.num_chars) break;
21873 prev_start = i;
21874 i += r.num_chars;
21875 find->y += r.baseline_y_delta;
21876 }
21877
21878 find->first_char = first = i;
21879 find->length = r.num_chars;
21880 find->height = r.ymax - r.ymin;
21881 find->prev_first = prev_start;
21882
21883 /* now scan to find xpos */
21884 find->x = r.x0;
21885 for (i=0; first+i < n; ++i)
21886 find->x += nk_textedit_get_width(state, first, i, font);
21887 }
21888 NK_INTERN void
21889 nk_textedit_clamp(struct nk_text_edit *state)
21890 {
21891 /* make the selection/cursor state valid if client altered the string */
21892 int n = state->string.len;
21893 if (NK_TEXT_HAS_SELECTION(state)) {
21894 if (state->select_start > n) state->select_start = n;
21895 if (state->select_end > n) state->select_end = n;
21896 /* if clamping forced them to be equal, move the cursor to match */
21897 if (state->select_start == state->select_end)
21898 state->cursor = state->select_start;
21899 }
21900 if (state->cursor > n) state->cursor = n;
21901 }
21902 NK_API void
21903 nk_textedit_delete(struct nk_text_edit *state, int where, int len)
21904 {
21905 /* delete characters while updating undo */
21906 nk_textedit_makeundo_delete(state, where, len);
21907 nk_str_delete_runes(&state->string, where, len);
21908 state->has_preferred_x = 0;
21909 }
21910 NK_API void
21911 nk_textedit_delete_selection(struct nk_text_edit *state)
21912 {
21913 /* delete the section */
21914 nk_textedit_clamp(state);
21915 if (NK_TEXT_HAS_SELECTION(state)) {
21916 if (state->select_start < state->select_end) {
21917 nk_textedit_delete(state, state->select_start,
21918 state->select_end - state->select_start);
21919 state->select_end = state->cursor = state->select_start;
21920 } else {
21921 nk_textedit_delete(state, state->select_end,
21922 state->select_start - state->select_end);
21923 state->select_start = state->cursor = state->select_end;
21924 }
21925 state->has_preferred_x = 0;
21926 }
21927 }
21928 NK_INTERN void
21929 nk_textedit_sortselection(struct nk_text_edit *state)
21930 {
21931 /* canonicalize the selection so start <= end */
21932 if (state->select_end < state->select_start) {
21933 int temp = state->select_end;
21934 state->select_end = state->select_start;
21935 state->select_start = temp;
21936 }
21937 }
21938 NK_INTERN void
21939 nk_textedit_move_to_first(struct nk_text_edit *state)
21940 {
21941 /* move cursor to first character of selection */
21942 if (NK_TEXT_HAS_SELECTION(state)) {
21943 nk_textedit_sortselection(state);
21944 state->cursor = state->select_start;
21945 state->select_end = state->select_start;
21946 state->has_preferred_x = 0;
21947 }
21948 }
21949 NK_INTERN void
21950 nk_textedit_move_to_last(struct nk_text_edit *state)
21951 {
21952 /* move cursor to last character of selection */
21953 if (NK_TEXT_HAS_SELECTION(state)) {
21954 nk_textedit_sortselection(state);
21955 nk_textedit_clamp(state);
21956 state->cursor = state->select_end;
21957 state->select_start = state->select_end;
21958 state->has_preferred_x = 0;
21959 }
21960 }
21961 NK_INTERN int
21962 nk_is_word_boundary( struct nk_text_edit *state, int idx)
21963 {
21964 int len;
21965 nk_rune c;
21966 if (idx <= 0) return 1;
21967 if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
21968 return (c == ' ' || c == '\t' ||c == 0x3000 || c == ',' || c == ';' ||
21969 c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
21970 c == '|');
21971 }
21972 NK_INTERN int
21973 nk_textedit_move_to_word_previous(struct nk_text_edit *state)
21974 {
21975 int c = state->cursor - 1;
21976 while( c >= 0 && !nk_is_word_boundary(state, c))
21977 --c;
21978
21979 if( c < 0 )
21980 c = 0;
21981
21982 return c;
21983 }
21984 NK_INTERN int
21985 nk_textedit_move_to_word_next(struct nk_text_edit *state)
21986 {
21987 const int len = state->string.len;
21988 int c = state->cursor+1;
21989 while( c < len && !nk_is_word_boundary(state, c))
21990 ++c;
21991
21992 if( c > len )
21993 c = len;
21994
21995 return c;
21996 }
21997 NK_INTERN void
21998 nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
21999 {
22000 /* update selection and cursor to match each other */
22001 if (!NK_TEXT_HAS_SELECTION(state))
22002 state->select_start = state->select_end = state->cursor;
22003 else state->cursor = state->select_end;
22004 }
22005 NK_API int
22006 nk_textedit_cut(struct nk_text_edit *state)
22007 {
22008 /* API cut: delete selection */
22009 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
22010 return 0;
22011 if (NK_TEXT_HAS_SELECTION(state)) {
22012 nk_textedit_delete_selection(state); /* implicitly clamps */
22013 state->has_preferred_x = 0;
22014 return 1;
22015 }
22016 return 0;
22017 }
22018 NK_API int
22019 nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
22020 {
22021 /* API paste: replace existing selection with passed-in text */
22022 int glyphs;
22023 const char *text = (const char *) ctext;
22024 if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
22025
22026 /* if there's a selection, the paste should delete it */
22027 nk_textedit_clamp(state);
22028 nk_textedit_delete_selection(state);
22029
22030 /* try to insert the characters */
22031 glyphs = nk_utf_len(ctext, len);
22032 if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
22033 nk_textedit_makeundo_insert(state, state->cursor, glyphs);
22034 state->cursor += len;
22035 state->has_preferred_x = 0;
22036 return 1;
22037 }
22038 /* remove the undo since we didn't actually insert the characters */
22039 if (state->undo.undo_point)
22040 --state->undo.undo_point;
22041 return 0;
22042 }
22043 NK_API void
22044 nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
22045 {
22046 nk_rune unicode;
22047 int glyph_len;
22048 int text_len = 0;
22049
22050 NK_ASSERT(state);
22051 NK_ASSERT(text);
22052 if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
22053
22054 glyph_len = nk_utf_decode(text, &unicode, total_len);
22055 while ((text_len < total_len) && glyph_len)
22056 {
22057 /* don't insert a backward delete, just process the event */
22058 if (unicode == 127) goto next;
22059 /* can't add newline in single-line mode */
22060 if (unicode == '\n' && state->single_line) goto next;
22061 /* filter incoming text */
22062 if (state->filter && !state->filter(state, unicode)) goto next;
22063
22064 if (!NK_TEXT_HAS_SELECTION(state) &&
22065 state->cursor < state->string.len)
22066 {
22067 if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
22068 nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
22069 nk_str_delete_runes(&state->string, state->cursor, 1);
22070 }
22071 if (nk_str_insert_text_utf8(&state->string, state->cursor,
22072 text+text_len, 1))
22073 {
22074 ++state->cursor;
22075 state->has_preferred_x = 0;
22076 }
22077 } else {
22078 nk_textedit_delete_selection(state); /* implicitly clamps */
22079 if (nk_str_insert_text_utf8(&state->string, state->cursor,
22080 text+text_len, 1))
22081 {
22082 nk_textedit_makeundo_insert(state, state->cursor, 1);
22083 ++state->cursor;
22084 state->has_preferred_x = 0;
22085 }
22086 }
22087 next:
22088 text_len += glyph_len;
22089 glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
22090 }
22091 }
22092 NK_LIB void
22093 nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
22094 const struct nk_user_font *font, float row_height)
22095 {
22096 retry:
22097 switch (key)
22098 {
22099 case NK_KEY_NONE:
22100 case NK_KEY_CTRL:
22101 case NK_KEY_ENTER:
22102 case NK_KEY_SHIFT:
22103 case NK_KEY_TAB:
22104 case NK_KEY_COPY:
22105 case NK_KEY_CUT:
22106 case NK_KEY_PASTE:
22107 case NK_KEY_MAX:
22108 default: break;
22109 case NK_KEY_TEXT_UNDO:
22110 nk_textedit_undo(state);
22111 state->has_preferred_x = 0;
22112 break;
22113
22114 case NK_KEY_TEXT_REDO:
22115 nk_textedit_redo(state);
22116 state->has_preferred_x = 0;
22117 break;
22118
22119 case NK_KEY_TEXT_SELECT_ALL:
22120 nk_textedit_select_all(state);
22121 state->has_preferred_x = 0;
22122 break;
22123
22124 case NK_KEY_TEXT_INSERT_MODE:
22125 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
22126 state->mode = NK_TEXT_EDIT_MODE_INSERT;
22127 break;
22128 case NK_KEY_TEXT_REPLACE_MODE:
22129 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
22130 state->mode = NK_TEXT_EDIT_MODE_REPLACE;
22131 break;
22132 case NK_KEY_TEXT_RESET_MODE:
22133 if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
22134 state->mode == NK_TEXT_EDIT_MODE_REPLACE)
22135 state->mode = NK_TEXT_EDIT_MODE_VIEW;
22136 break;
22137
22138 case NK_KEY_LEFT:
22139 if (shift_mod) {
22140 nk_textedit_clamp(state);
22141 nk_textedit_prep_selection_at_cursor(state);
22142 /* move selection left */
22143 if (state->select_end > 0)
22144 --state->select_end;
22145 state->cursor = state->select_end;
22146 state->has_preferred_x = 0;
22147 } else {
22148 /* if currently there's a selection,
22149 * move cursor to start of selection */
22150 if (NK_TEXT_HAS_SELECTION(state))
22151 nk_textedit_move_to_first(state);
22152 else if (state->cursor > 0)
22153 --state->cursor;
22154 state->has_preferred_x = 0;
22155 } break;
22156
22157 case NK_KEY_RIGHT:
22158 if (shift_mod) {
22159 nk_textedit_prep_selection_at_cursor(state);
22160 /* move selection right */
22161 ++state->select_end;
22162 nk_textedit_clamp(state);
22163 state->cursor = state->select_end;
22164 state->has_preferred_x = 0;
22165 } else {
22166 /* if currently there's a selection,
22167 * move cursor to end of selection */
22168 if (NK_TEXT_HAS_SELECTION(state))
22169 nk_textedit_move_to_last(state);
22170 else ++state->cursor;
22171 nk_textedit_clamp(state);
22172 state->has_preferred_x = 0;
22173 } break;
22174
22175 case NK_KEY_TEXT_WORD_LEFT:
22176 if (shift_mod) {
22177 if( !NK_TEXT_HAS_SELECTION( state ) )
22178 nk_textedit_prep_selection_at_cursor(state);
22179 state->cursor = nk_textedit_move_to_word_previous(state);
22180 state->select_end = state->cursor;
22181 nk_textedit_clamp(state );
22182 } else {
22183 if (NK_TEXT_HAS_SELECTION(state))
22184 nk_textedit_move_to_first(state);
22185 else {
22186 state->cursor = nk_textedit_move_to_word_previous(state);
22187 nk_textedit_clamp(state );
22188 }
22189 } break;
22190
22191 case NK_KEY_TEXT_WORD_RIGHT:
22192 if (shift_mod) {
22193 if( !NK_TEXT_HAS_SELECTION( state ) )
22194 nk_textedit_prep_selection_at_cursor(state);
22195 state->cursor = nk_textedit_move_to_word_next(state);
22196 state->select_end = state->cursor;
22197 nk_textedit_clamp(state);
22198 } else {
22199 if (NK_TEXT_HAS_SELECTION(state))
22200 nk_textedit_move_to_last(state);
22201 else {
22202 state->cursor = nk_textedit_move_to_word_next(state);
22203 nk_textedit_clamp(state );
22204 }
22205 } break;
22206
22207 case NK_KEY_DOWN: {
22208 struct nk_text_find find;
22209 struct nk_text_edit_row row;
22210 int i, sel = shift_mod;
22211
22212 if (state->single_line) {
22213 /* on windows, up&down in single-line behave like left&right */
22214 key = NK_KEY_RIGHT;
22215 goto retry;
22216 }
22217
22218 if (sel)
22219 nk_textedit_prep_selection_at_cursor(state);
22220 else if (NK_TEXT_HAS_SELECTION(state))
22221 nk_textedit_move_to_last(state);
22222
22223 /* compute current position of cursor point */
22224 nk_textedit_clamp(state);
22225 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
22226 font, row_height);
22227
22228 /* now find character position down a row */
22229 if (find.length)
22230 {
22231 float x;
22232 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
22233 int start = find.first_char + find.length;
22234
22235 state->cursor = start;
22236 nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
22237 x = row.x0;
22238
22239 for (i=0; i < row.num_chars && x < row.x1; ++i) {
22240 float dx = nk_textedit_get_width(state, start, i, font);
22241 x += dx;
22242 if (x > goal_x)
22243 break;
22244 ++state->cursor;
22245 }
22246 nk_textedit_clamp(state);
22247
22248 state->has_preferred_x = 1;
22249 state->preferred_x = goal_x;
22250 if (sel)
22251 state->select_end = state->cursor;
22252 }
22253 } break;
22254
22255 case NK_KEY_UP: {
22256 struct nk_text_find find;
22257 struct nk_text_edit_row row;
22258 int i, sel = shift_mod;
22259
22260 if (state->single_line) {
22261 /* on windows, up&down become left&right */
22262 key = NK_KEY_LEFT;
22263 goto retry;
22264 }
22265
22266 if (sel)
22267 nk_textedit_prep_selection_at_cursor(state);
22268 else if (NK_TEXT_HAS_SELECTION(state))
22269 nk_textedit_move_to_first(state);
22270
22271 /* compute current position of cursor point */
22272 nk_textedit_clamp(state);
22273 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
22274 font, row_height);
22275
22276 /* can only go up if there's a previous row */
22277 if (find.prev_first != find.first_char) {
22278 /* now find character position up a row */
22279 float x;
22280 float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
22281
22282 state->cursor = find.prev_first;
22283 nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
22284 x = row.x0;
22285
22286 for (i=0; i < row.num_chars && x < row.x1; ++i) {
22287 float dx = nk_textedit_get_width(state, find.prev_first, i, font);
22288 x += dx;
22289 if (x > goal_x)
22290 break;
22291 ++state->cursor;
22292 }
22293 nk_textedit_clamp(state);
22294
22295 state->has_preferred_x = 1;
22296 state->preferred_x = goal_x;
22297 if (sel) state->select_end = state->cursor;
22298 }
22299 } break;
22300
22301 case NK_KEY_DEL:
22302 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
22303 break;
22304 if (NK_TEXT_HAS_SELECTION(state))
22305 nk_textedit_delete_selection(state);
22306 else {
22307 int n = state->string.len;
22308 if (state->cursor < n)
22309 nk_textedit_delete(state, state->cursor, 1);
22310 }
22311 state->has_preferred_x = 0;
22312 break;
22313
22314 case NK_KEY_BACKSPACE:
22315 if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
22316 break;
22317 if (NK_TEXT_HAS_SELECTION(state))
22318 nk_textedit_delete_selection(state);
22319 else {
22320 nk_textedit_clamp(state);
22321 if (state->cursor > 0) {
22322 nk_textedit_delete(state, state->cursor-1, 1);
22323 --state->cursor;
22324 }
22325 }
22326 state->has_preferred_x = 0;
22327 break;
22328
22329 case NK_KEY_TEXT_START:
22330 if (shift_mod) {
22331 nk_textedit_prep_selection_at_cursor(state);
22332 state->cursor = state->select_end = 0;
22333 state->has_preferred_x = 0;
22334 } else {
22335 state->cursor = state->select_start = state->select_end = 0;
22336 state->has_preferred_x = 0;
22337 }
22338 break;
22339
22340 case NK_KEY_TEXT_END:
22341 if (shift_mod) {
22342 nk_textedit_prep_selection_at_cursor(state);
22343 state->cursor = state->select_end = state->string.len;
22344 state->has_preferred_x = 0;
22345 } else {
22346 state->cursor = state->string.len;
22347 state->select_start = state->select_end = 0;
22348 state->has_preferred_x = 0;
22349 }
22350 break;
22351
22352 case NK_KEY_TEXT_LINE_START: {
22353 if (shift_mod) {
22354 struct nk_text_find find;
22355 nk_textedit_clamp(state);
22356 nk_textedit_prep_selection_at_cursor(state);
22357 if (state->string.len && state->cursor == state->string.len)
22358 --state->cursor;
22359 nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
22360 font, row_height);
22361 state->cursor = state->select_end = find.first_char;
22362 state->has_preferred_x = 0;
22363 } else {
22364 struct nk_text_find find;
22365 if (state->string.len && state->cursor == state->string.len)
22366 --state->cursor;
22367 nk_textedit_clamp(state);
22368 nk_textedit_move_to_first(state);
22369 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
22370 font, row_height);
22371 state->cursor = find.first_char;
22372 state->has_preferred_x = 0;
22373 }
22374 } break;
22375
22376 case NK_KEY_TEXT_LINE_END: {
22377 if (shift_mod) {
22378 struct nk_text_find find;
22379 nk_textedit_clamp(state);
22380 nk_textedit_prep_selection_at_cursor(state);
22381 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
22382 font, row_height);
22383 state->has_preferred_x = 0;
22384 state->cursor = find.first_char + find.length;
22385 if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
22386 --state->cursor;
22387 state->select_end = state->cursor;
22388 } else {
22389 struct nk_text_find find;
22390 nk_textedit_clamp(state);
22391 nk_textedit_move_to_first(state);
22392 nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
22393 font, row_height);
22394
22395 state->has_preferred_x = 0;
22396 state->cursor = find.first_char + find.length;
22397 if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
22398 --state->cursor;
22399 }} break;
22400 }
22401 }
22402 NK_INTERN void
22403 nk_textedit_flush_redo(struct nk_text_undo_state *state)
22404 {
22405 state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
22406 state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
22407 }
22408 NK_INTERN void
22409 nk_textedit_discard_undo(struct nk_text_undo_state *state)
22410 {
22411 /* discard the oldest entry in the undo list */
22412 if (state->undo_point > 0) {
22413 /* if the 0th undo state has characters, clean those up */
22414 if (state->undo_rec[0].char_storage >= 0) {
22415 int n = state->undo_rec[0].insert_length, i;
22416 /* delete n characters from all other records */
22417 state->undo_char_point = (short)(state->undo_char_point - n);
22418 NK_MEMCPY(state->undo_char, state->undo_char + n,
22419 (nk_size)state->undo_char_point*sizeof(nk_rune));
22420 for (i=0; i < state->undo_point; ++i) {
22421 if (state->undo_rec[i].char_storage >= 0)
22422 state->undo_rec[i].char_storage = (short)
22423 (state->undo_rec[i].char_storage - n);
22424 }
22425 }
22426 --state->undo_point;
22427 NK_MEMCPY(state->undo_rec, state->undo_rec+1,
22428 (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
22429 }
22430 }
22431 NK_INTERN void
22432 nk_textedit_discard_redo(struct nk_text_undo_state *state)
22433 {
22434 /* discard the oldest entry in the redo list--it's bad if this
22435 ever happens, but because undo & redo have to store the actual
22436 characters in different cases, the redo character buffer can
22437 fill up even though the undo buffer didn't */
22438 nk_size num;
22439 int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
22440 if (state->redo_point <= k) {
22441 /* if the k'th undo state has characters, clean those up */
22442 if (state->undo_rec[k].char_storage >= 0) {
22443 int n = state->undo_rec[k].insert_length, i;
22444 /* delete n characters from all other records */
22445 state->redo_char_point = (short)(state->redo_char_point + n);
22446 num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
22447 NK_MEMCPY(state->undo_char + state->redo_char_point,
22448 state->undo_char + state->redo_char_point-n, num * sizeof(char));
22449 for (i = state->redo_point; i < k; ++i) {
22450 if (state->undo_rec[i].char_storage >= 0) {
22451 state->undo_rec[i].char_storage = (short)
22452 (state->undo_rec[i].char_storage + n);
22453 }
22454 }
22455 }
22456 ++state->redo_point;
22457 num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
22458 if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
22459 state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
22460 }
22461 }
22462 NK_INTERN struct nk_text_undo_record*
22463 nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
22464 {
22465 /* any time we create a new undo record, we discard redo*/
22466 nk_textedit_flush_redo(state);
22467
22468 /* if we have no free records, we have to make room,
22469 * by sliding the existing records down */
22470 if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
22471 nk_textedit_discard_undo(state);
22472
22473 /* if the characters to store won't possibly fit in the buffer,
22474 * we can't undo */
22475 if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
22476 state->undo_point = 0;
22477 state->undo_char_point = 0;
22478 return 0;
22479 }
22480
22481 /* if we don't have enough free characters in the buffer,
22482 * we have to make room */
22483 while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
22484 nk_textedit_discard_undo(state);
22485 return &state->undo_rec[state->undo_point++];
22486 }
22487 NK_INTERN nk_rune*
22488 nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
22489 int insert_len, int delete_len)
22490 {
22491 struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
22492 if (r == 0)
22493 return 0;
22494
22495 r->where = pos;
22496 r->insert_length = (short) insert_len;
22497 r->delete_length = (short) delete_len;
22498
22499 if (insert_len == 0) {
22500 r->char_storage = -1;
22501 return 0;
22502 } else {
22503 r->char_storage = state->undo_char_point;
22504 state->undo_char_point = (short)(state->undo_char_point + insert_len);
22505 return &state->undo_char[r->char_storage];
22506 }
22507 }
22508 NK_API void
22509 nk_textedit_undo(struct nk_text_edit *state)
22510 {
22511 struct nk_text_undo_state *s = &state->undo;
22512 struct nk_text_undo_record u, *r;
22513 if (s->undo_point == 0)
22514 return;
22515
22516 /* we need to do two things: apply the undo record, and create a redo record */
22517 u = s->undo_rec[s->undo_point-1];
22518 r = &s->undo_rec[s->redo_point-1];
22519 r->char_storage = -1;
22520
22521 r->insert_length = u.delete_length;
22522 r->delete_length = u.insert_length;
22523 r->where = u.where;
22524
22525 if (u.delete_length)
22526 {
22527 /* if the undo record says to delete characters, then the redo record will
22528 need to re-insert the characters that get deleted, so we need to store
22529 them.
22530 there are three cases:
22531 - there's enough room to store the characters
22532 - characters stored for *redoing* don't leave room for redo
22533 - characters stored for *undoing* don't leave room for redo
22534 if the last is true, we have to bail */
22535 if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
22536 /* the undo records take up too much character space; there's no space
22537 * to store the redo characters */
22538 r->insert_length = 0;
22539 } else {
22540 int i;
22541 /* there's definitely room to store the characters eventually */
22542 while (s->undo_char_point + u.delete_length > s->redo_char_point) {
22543 /* there's currently not enough room, so discard a redo record */
22544 nk_textedit_discard_redo(s);
22545 /* should never happen: */
22546 if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
22547 return;
22548 }
22549
22550 r = &s->undo_rec[s->redo_point-1];
22551 r->char_storage = (short)(s->redo_char_point - u.delete_length);
22552 s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
22553
22554 /* now save the characters */
22555 for (i=0; i < u.delete_length; ++i)
22556 s->undo_char[r->char_storage + i] =
22557 nk_str_rune_at(&state->string, u.where + i);
22558 }
22559 /* now we can carry out the deletion */
22560 nk_str_delete_runes(&state->string, u.where, u.delete_length);
22561 }
22562
22563 /* check type of recorded action: */
22564 if (u.insert_length) {
22565 /* easy case: was a deletion, so we need to insert n characters */
22566 nk_str_insert_text_runes(&state->string, u.where,
22567 &s->undo_char[u.char_storage], u.insert_length);
22568 s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
22569 }
22570 state->cursor = (short)(u.where + u.insert_length);
22571
22572 s->undo_point--;
22573 s->redo_point--;
22574 }
22575 NK_API void
22576 nk_textedit_redo(struct nk_text_edit *state)
22577 {
22578 struct nk_text_undo_state *s = &state->undo;
22579 struct nk_text_undo_record *u, r;
22580 if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
22581 return;
22582
22583 /* we need to do two things: apply the redo record, and create an undo record */
22584 u = &s->undo_rec[s->undo_point];
22585 r = s->undo_rec[s->redo_point];
22586
22587 /* we KNOW there must be room for the undo record, because the redo record
22588 was derived from an undo record */
22589 u->delete_length = r.insert_length;
22590 u->insert_length = r.delete_length;
22591 u->where = r.where;
22592 u->char_storage = -1;
22593
22594 if (r.delete_length) {
22595 /* the redo record requires us to delete characters, so the undo record
22596 needs to store the characters */
22597 if (s->undo_char_point + u->insert_length > s->redo_char_point) {
22598 u->insert_length = 0;
22599 u->delete_length = 0;
22600 } else {
22601 int i;
22602 u->char_storage = s->undo_char_point;
22603 s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
22604
22605 /* now save the characters */
22606 for (i=0; i < u->insert_length; ++i) {
22607 s->undo_char[u->char_storage + i] =
22608 nk_str_rune_at(&state->string, u->where + i);
22609 }
22610 }
22611 nk_str_delete_runes(&state->string, r.where, r.delete_length);
22612 }
22613
22614 if (r.insert_length) {
22615 /* easy case: need to insert n characters */
22616 nk_str_insert_text_runes(&state->string, r.where,
22617 &s->undo_char[r.char_storage], r.insert_length);
22618 }
22619 state->cursor = r.where + r.insert_length;
22620
22621 s->undo_point++;
22622 s->redo_point++;
22623 }
22624 NK_INTERN void
22625 nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
22626 {
22627 nk_textedit_createundo(&state->undo, where, 0, length);
22628 }
22629 NK_INTERN void
22630 nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
22631 {
22632 int i;
22633 nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
22634 if (p) {
22635 for (i=0; i < length; ++i)
22636 p[i] = nk_str_rune_at(&state->string, where+i);
22637 }
22638 }
22639 NK_INTERN void
22640 nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
22641 int old_length, int new_length)
22642 {
22643 int i;
22644 nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
22645 if (p) {
22646 for (i=0; i < old_length; ++i)
22647 p[i] = nk_str_rune_at(&state->string, where+i);
22648 }
22649 }
22650 NK_LIB void
22651 nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
22652 nk_plugin_filter filter)
22653 {
22654 /* reset the state to default */
22655 state->undo.undo_point = 0;
22656 state->undo.undo_char_point = 0;
22657 state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
22658 state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
22659 state->select_end = state->select_start = 0;
22660 state->cursor = 0;
22661 state->has_preferred_x = 0;
22662 state->preferred_x = 0;
22663 state->cursor_at_end_of_line = 0;
22664 state->initialized = 1;
22665 state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
22666 state->mode = NK_TEXT_EDIT_MODE_VIEW;
22667 state->filter = filter;
22668 state->scrollbar = nk_vec2(0,0);
22669 }
22670 NK_API void
22671 nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
22672 {
22673 NK_ASSERT(state);
22674 NK_ASSERT(memory);
22675 if (!state || !memory || !size) return;
22676 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
22677 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
22678 nk_str_init_fixed(&state->string, memory, size);
22679 }
22680 NK_API void
22681 nk_textedit_init(struct nk_text_edit *state, struct nk_allocator *alloc, nk_size size)
22682 {
22683 NK_ASSERT(state);
22684 NK_ASSERT(alloc);
22685 if (!state || !alloc) return;
22686 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
22687 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
22688 nk_str_init(&state->string, alloc, size);
22689 }
22690 #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
22691 NK_API void
22692 nk_textedit_init_default(struct nk_text_edit *state)
22693 {
22694 NK_ASSERT(state);
22695 if (!state) return;
22696 NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
22697 nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
22698 nk_str_init_default(&state->string);
22699 }
22700 #endif
22701 NK_API void
22702 nk_textedit_select_all(struct nk_text_edit *state)
22703 {
22704 NK_ASSERT(state);
22705 state->select_start = 0;
22706 state->select_end = state->string.len;
22707 }
22708 NK_API void
22709 nk_textedit_free(struct nk_text_edit *state)
22710 {
22711 NK_ASSERT(state);
22712 if (!state) return;
22713 nk_str_free(&state->string);
22714 }
22715
22716
22717
22718
22719
22720 /* ===============================================================
22721 *
22722 * FILTER
22723 *
22724 * ===============================================================*/
22725 NK_API int
22726 nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
22727 {
22728 NK_UNUSED(unicode);
22729 NK_UNUSED(box);
22730 return nk_true;
22731 }
22732 NK_API int
22733 nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
22734 {
22735 NK_UNUSED(box);
22736 if (unicode > 128) return nk_false;
22737 else return nk_true;
22738 }
22739 NK_API int
22740 nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
22741 {
22742 NK_UNUSED(box);
22743 if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
22744 return nk_false;
22745 else return nk_true;
22746 }
22747 NK_API int
22748 nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
22749 {
22750 NK_UNUSED(box);
22751 if ((unicode < '0' || unicode > '9') && unicode != '-')
22752 return nk_false;
22753 else return nk_true;
22754 }
22755 NK_API int
22756 nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
22757 {
22758 NK_UNUSED(box);
22759 if ((unicode < '0' || unicode > '9') &&
22760 (unicode < 'a' || unicode > 'f') &&
22761 (unicode < 'A' || unicode > 'F'))
22762 return nk_false;
22763 else return nk_true;
22764 }
22765 NK_API int
22766 nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
22767 {
22768 NK_UNUSED(box);
22769 if (unicode < '0' || unicode > '7')
22770 return nk_false;
22771 else return nk_true;
22772 }
22773 NK_API int
22774 nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
22775 {
22776 NK_UNUSED(box);
22777 if (unicode != '0' && unicode != '1')
22778 return nk_false;
22779 else return nk_true;
22780 }
22781
22782 /* ===============================================================
22783 *
22784 * EDIT
22785 *
22786 * ===============================================================*/
22787 NK_LIB void
22788 nk_edit_draw_text(struct nk_command_buffer *out,
22789 const struct nk_style_edit *style, float pos_x, float pos_y,
22790 float x_offset, const char *text, int byte_len, float row_height,
22791 const struct nk_user_font *font, struct nk_color background,
22792 struct nk_color foreground, int is_selected)
22793 {
22794 NK_ASSERT(out);
22795 NK_ASSERT(font);
22796 NK_ASSERT(style);
22797 if (!text || !byte_len || !out || !style) return;
22798
22799 {int glyph_len = 0;
22800 nk_rune unicode = 0;
22801 int text_len = 0;
22802 float line_width = 0;
22803 float glyph_width;
22804 const char *line = text;
22805 float line_offset = 0;
22806 int line_count = 0;
22807
22808 struct nk_text txt;
22809 txt.padding = nk_vec2(0,0);
22810 txt.background = background;
22811 txt.text = foreground;
22812
22813 glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
22814 if (!glyph_len) return;
22815 while ((text_len < byte_len) && glyph_len)
22816 {
22817 if (unicode == '\n') {
22818 /* new line separator so draw previous line */
22819 struct nk_rect label;
22820 label.y = pos_y + line_offset;
22821 label.h = row_height;
22822 label.w = line_width;
22823 label.x = pos_x;
22824 if (!line_count)
22825 label.x += x_offset;
22826
22827 if (is_selected) /* selection needs to draw different background color */
22828 nk_fill_rect(out, label, 0, background);
22829 nk_widget_text(out, label, line, (int)((text + text_len) - line),
22830 &txt, NK_TEXT_CENTERED, font);
22831
22832 text_len++;
22833 line_count++;
22834 line_width = 0;
22835 line = text + text_len;
22836 line_offset += row_height;
22837 glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
22838 continue;
22839 }
22840 if (unicode == '\r') {
22841 text_len++;
22842 glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
22843 continue;
22844 }
22845 glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
22846 line_width += (float)glyph_width;
22847 text_len += glyph_len;
22848 glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
22849 continue;
22850 }
22851 if (line_width > 0) {
22852 /* draw last line */
22853 struct nk_rect label;
22854 label.y = pos_y + line_offset;
22855 label.h = row_height;
22856 label.w = line_width;
22857 label.x = pos_x;
22858 if (!line_count)
22859 label.x += x_offset;
22860
22861 if (is_selected)
22862 nk_fill_rect(out, label, 0, background);
22863 nk_widget_text(out, label, line, (int)((text + text_len) - line),
22864 &txt, NK_TEXT_LEFT, font);
22865 }}
22866 }
22867 NK_LIB nk_flags
22868 nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
22869 struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
22870 struct nk_text_edit *edit, const struct nk_style_edit *style,
22871 struct nk_input *in, const struct nk_user_font *font)
22872 {
22873 struct nk_rect area;
22874 nk_flags ret = 0;
22875 float row_height;
22876 char prev_state = 0;
22877 char is_hovered = 0;
22878 char select_all = 0;
22879 char cursor_follow = 0;
22880 struct nk_rect old_clip;
22881 struct nk_rect clip;
22882
22883 NK_ASSERT(state);
22884 NK_ASSERT(out);
22885 NK_ASSERT(style);
22886 if (!state || !out || !style)
22887 return ret;
22888
22889 /* visible text area calculation */
22890 area.x = bounds.x + style->padding.x + style->border;
22891 area.y = bounds.y + style->padding.y + style->border;
22892 area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
22893 area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
22894 if (flags & NK_EDIT_MULTILINE)
22895 area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
22896 row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
22897
22898 /* calculate clipping rectangle */
22899 old_clip = out->clip;
22900 nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
22901
22902 /* update edit state */
22903 prev_state = (char)edit->active;
22904 is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
22905 if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
22906 edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
22907 bounds.x, bounds.y, bounds.w, bounds.h);
22908 }
22909
22910 /* (de)activate text editor */
22911 if (!prev_state && edit->active) {
22912 const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
22913 NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
22914 nk_textedit_clear_state(edit, type, filter);
22915 if (flags & NK_EDIT_AUTO_SELECT)
22916 select_all = nk_true;
22917 if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
22918 edit->cursor = edit->string.len;
22919 in = 0;
22920 }
22921 } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
22922 if (flags & NK_EDIT_READ_ONLY)
22923 edit->mode = NK_TEXT_EDIT_MODE_VIEW;
22924 else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
22925 edit->mode = NK_TEXT_EDIT_MODE_INSERT;
22926
22927 ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
22928 if (prev_state != edit->active)
22929 ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
22930
22931 /* handle user input */
22932 if (edit->active && in)
22933 {
22934 int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
22935 const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
22936 const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
22937
22938 /* mouse click handler */
22939 is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
22940 if (select_all) {
22941 nk_textedit_select_all(edit);
22942 } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
22943 in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
22944 nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
22945 } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
22946 (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
22947 nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
22948 cursor_follow = nk_true;
22949 } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
22950 in->mouse.buttons[NK_BUTTON_RIGHT].down) {
22951 nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
22952 nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
22953 cursor_follow = nk_true;
22954 }
22955
22956 {int i; /* keyboard input */
22957 int old_mode = edit->mode;
22958 for (i = 0; i < NK_KEY_MAX; ++i) {
22959 if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
22960 if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
22961 nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
22962 cursor_follow = nk_true;
22963 }
22964 }
22965 if (old_mode != edit->mode) {
22966 in->keyboard.text_len = 0;
22967 }}
22968
22969 /* text input */
22970 edit->filter = filter;
22971 if (in->keyboard.text_len) {
22972 nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
22973 cursor_follow = nk_true;
22974 in->keyboard.text_len = 0;
22975 }
22976
22977 /* enter key handler */
22978 if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
22979 cursor_follow = nk_true;
22980 if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
22981 nk_textedit_text(edit, "\n", 1);
22982 else if (flags & NK_EDIT_SIG_ENTER)
22983 ret |= NK_EDIT_COMMITED;
22984 else nk_textedit_text(edit, "\n", 1);
22985 }
22986
22987 /* cut & copy handler */
22988 {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
22989 int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
22990 if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
22991 {
22992 int glyph_len;
22993 nk_rune unicode;
22994 const char *text;
22995 int b = edit->select_start;
22996 int e = edit->select_end;
22997
22998 int begin = NK_MIN(b, e);
22999 int end = NK_MAX(b, e);
23000 text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
23001 if (edit->clip.copy)
23002 edit->clip.copy(edit->clip.userdata, text, end - begin);
23003 if (cut && !(flags & NK_EDIT_READ_ONLY)){
23004 nk_textedit_cut(edit);
23005 cursor_follow = nk_true;
23006 }
23007 }}
23008
23009 /* paste handler */
23010 {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
23011 if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
23012 edit->clip.paste(edit->clip.userdata, edit);
23013 cursor_follow = nk_true;
23014 }}
23015
23016 /* tab handler */
23017 {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
23018 if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
23019 nk_textedit_text(edit, " ", 4);
23020 cursor_follow = nk_true;
23021 }}
23022 }
23023
23024 /* set widget state */
23025 if (edit->active)
23026 *state = NK_WIDGET_STATE_ACTIVE;
23027 else nk_widget_state_reset(state);
23028
23029 if (is_hovered)
23030 *state |= NK_WIDGET_STATE_HOVERED;
23031
23032 /* DRAW EDIT */
23033 {const char *text = nk_str_get_const(&edit->string);
23034 int len = nk_str_len_char(&edit->string);
23035
23036 {/* select background colors/images */
23037 const struct nk_style_item *background;
23038 if (*state & NK_WIDGET_STATE_ACTIVED)
23039 background = &style->active;
23040 else if (*state & NK_WIDGET_STATE_HOVER)
23041 background = &style->hover;
23042 else background = &style->normal;
23043
23044 /* draw background frame */
23045 if (background->type == NK_STYLE_ITEM_COLOR) {
23046 nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
23047 nk_fill_rect(out, bounds, style->rounding, background->data.color);
23048 } else nk_draw_image(out, bounds, &background->data.image, nk_white);}
23049
23050 area.w = NK_MAX(0, area.w - style->cursor_size);
23051 if (edit->active)
23052 {
23053 int total_lines = 1;
23054 struct nk_vec2 text_size = nk_vec2(0,0);
23055
23056 /* text pointer positions */
23057 const char *cursor_ptr = 0;
23058 const char *select_begin_ptr = 0;
23059 const char *select_end_ptr = 0;
23060
23061 /* 2D pixel positions */
23062 struct nk_vec2 cursor_pos = nk_vec2(0,0);
23063 struct nk_vec2 selection_offset_start = nk_vec2(0,0);
23064 struct nk_vec2 selection_offset_end = nk_vec2(0,0);
23065
23066 int selection_begin = NK_MIN(edit->select_start, edit->select_end);
23067 int selection_end = NK_MAX(edit->select_start, edit->select_end);
23068
23069 /* calculate total line count + total space + cursor/selection position */
23070 float line_width = 0.0f;
23071 if (text && len)
23072 {
23073 /* utf8 encoding */
23074 float glyph_width;
23075 int glyph_len = 0;
23076 nk_rune unicode = 0;
23077 int text_len = 0;
23078 int glyphs = 0;
23079 int row_begin = 0;
23080
23081 glyph_len = nk_utf_decode(text, &unicode, len);
23082 glyph_width = font->width(font->userdata, font->height, text, glyph_len);
23083 line_width = 0;
23084
23085 /* iterate all lines */
23086 while ((text_len < len) && glyph_len)
23087 {
23088 /* set cursor 2D position and line */
23089 if (!cursor_ptr && glyphs == edit->cursor)
23090 {
23091 int glyph_offset;
23092 struct nk_vec2 out_offset;
23093 struct nk_vec2 row_size;
23094 const char *remaining;
23095
23096 /* calculate 2d position */
23097 cursor_pos.y = (float)(total_lines-1) * row_height;
23098 row_size = nk_text_calculate_text_bounds(font, text+row_begin,
23099 text_len-row_begin, row_height, &remaining,
23100 &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
23101 cursor_pos.x = row_size.x;
23102 cursor_ptr = text + text_len;
23103 }
23104
23105 /* set start selection 2D position and line */
23106 if (!select_begin_ptr && edit->select_start != edit->select_end &&
23107 glyphs == selection_begin)
23108 {
23109 int glyph_offset;
23110 struct nk_vec2 out_offset;
23111 struct nk_vec2 row_size;
23112 const char *remaining;
23113
23114 /* calculate 2d position */
23115 selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
23116 row_size = nk_text_calculate_text_bounds(font, text+row_begin,
23117 text_len-row_begin, row_height, &remaining,
23118 &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
23119 selection_offset_start.x = row_size.x;
23120 select_begin_ptr = text + text_len;
23121 }
23122
23123 /* set end selection 2D position and line */
23124 if (!select_end_ptr && edit->select_start != edit->select_end &&
23125 glyphs == selection_end)
23126 {
23127 int glyph_offset;
23128 struct nk_vec2 out_offset;
23129 struct nk_vec2 row_size;
23130 const char *remaining;
23131
23132 /* calculate 2d position */
23133 selection_offset_end.y = (float)(total_lines-1) * row_height;
23134 row_size = nk_text_calculate_text_bounds(font, text+row_begin,
23135 text_len-row_begin, row_height, &remaining,
23136 &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
23137 selection_offset_end.x = row_size.x;
23138 select_end_ptr = text + text_len;
23139 }
23140 if (unicode == '\n') {
23141 text_size.x = NK_MAX(text_size.x, line_width);
23142 total_lines++;
23143 line_width = 0;
23144 text_len++;
23145 glyphs++;
23146 row_begin = text_len;
23147 glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
23148 glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
23149 continue;
23150 }
23151
23152 glyphs++;
23153 text_len += glyph_len;
23154 line_width += (float)glyph_width;
23155
23156 glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
23157 glyph_width = font->width(font->userdata, font->height,
23158 text+text_len, glyph_len);
23159 continue;
23160 }
23161 text_size.y = (float)total_lines * row_height;
23162
23163 /* handle case when cursor is at end of text buffer */
23164 if (!cursor_ptr && edit->cursor == edit->string.len) {
23165 cursor_pos.x = line_width;
23166 cursor_pos.y = text_size.y - row_height;
23167 }
23168 }
23169 {
23170 /* scrollbar */
23171 if (cursor_follow)
23172 {
23173 /* update scrollbar to follow cursor */
23174 if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
23175 /* horizontal scroll */
23176 const float scroll_increment = area.w * 0.25f;
23177 if (cursor_pos.x < edit->scrollbar.x)
23178 edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
23179 if (cursor_pos.x >= edit->scrollbar.x + area.w)
23180 edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - area.w + scroll_increment);
23181 } else edit->scrollbar.x = 0;
23182
23183 if (flags & NK_EDIT_MULTILINE) {
23184 /* vertical scroll */
23185 if (cursor_pos.y < edit->scrollbar.y)
23186 edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
23187 if (cursor_pos.y >= edit->scrollbar.y + area.h)
23188 edit->scrollbar.y = edit->scrollbar.y + row_height;
23189 } else edit->scrollbar.y = 0;
23190 }
23191
23192 /* scrollbar widget */
23193 if (flags & NK_EDIT_MULTILINE)
23194 {
23195 nk_flags ws;
23196 struct nk_rect scroll;
23197 float scroll_target;
23198 float scroll_offset;
23199 float scroll_step;
23200 float scroll_inc;
23201
23202 scroll = area;
23203 scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
23204 scroll.w = style->scrollbar_size.x;
23205
23206 scroll_offset = edit->scrollbar.y;
23207 scroll_step = scroll.h * 0.10f;
23208 scroll_inc = scroll.h * 0.01f;
23209 scroll_target = text_size.y;
23210 edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
23211 scroll_offset, scroll_target, scroll_step, scroll_inc,
23212 &style->scrollbar, in, font);
23213 }
23214 }
23215
23216 /* draw text */
23217 {struct nk_color background_color;
23218 struct nk_color text_color;
23219 struct nk_color sel_background_color;
23220 struct nk_color sel_text_color;
23221 struct nk_color cursor_color;
23222 struct nk_color cursor_text_color;
23223 const struct nk_style_item *background;
23224 nk_push_scissor(out, clip);
23225
23226 /* select correct colors to draw */
23227 if (*state & NK_WIDGET_STATE_ACTIVED) {
23228 background = &style->active;
23229 text_color = style->text_active;
23230 sel_text_color = style->selected_text_hover;
23231 sel_background_color = style->selected_hover;
23232 cursor_color = style->cursor_hover;
23233 cursor_text_color = style->cursor_text_hover;
23234 } else if (*state & NK_WIDGET_STATE_HOVER) {
23235 background = &style->hover;
23236 text_color = style->text_hover;
23237 sel_text_color = style->selected_text_hover;
23238 sel_background_color = style->selected_hover;
23239 cursor_text_color = style->cursor_text_hover;
23240 cursor_color = style->cursor_hover;
23241 } else {
23242 background = &style->normal;
23243 text_color = style->text_normal;
23244 sel_text_color = style->selected_text_normal;
23245 sel_background_color = style->selected_normal;
23246 cursor_color = style->cursor_normal;
23247 cursor_text_color = style->cursor_text_normal;
23248 }
23249 if (background->type == NK_STYLE_ITEM_IMAGE)
23250 background_color = nk_rgba(0,0,0,0);
23251 else background_color = background->data.color;
23252
23253
23254 if (edit->select_start == edit->select_end) {
23255 /* no selection so just draw the complete text */
23256 const char *begin = nk_str_get_const(&edit->string);
23257 int l = nk_str_len_char(&edit->string);
23258 nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
23259 area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
23260 background_color, text_color, nk_false);
23261 } else {
23262 /* edit has selection so draw 1-3 text chunks */
23263 if (edit->select_start != edit->select_end && selection_begin > 0){
23264 /* draw unselected text before selection */
23265 const char *begin = nk_str_get_const(&edit->string);
23266 NK_ASSERT(select_begin_ptr);
23267 nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
23268 area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
23269 row_height, font, background_color, text_color, nk_false);
23270 }
23271 if (edit->select_start != edit->select_end) {
23272 /* draw selected text */
23273 NK_ASSERT(select_begin_ptr);
23274 if (!select_end_ptr) {
23275 const char *begin = nk_str_get_const(&edit->string);
23276 select_end_ptr = begin + nk_str_len_char(&edit->string);
23277 }
23278 nk_edit_draw_text(out, style,
23279 area.x - edit->scrollbar.x,
23280 area.y + selection_offset_start.y - edit->scrollbar.y,
23281 selection_offset_start.x,
23282 select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
23283 row_height, font, sel_background_color, sel_text_color, nk_true);
23284 }
23285 if ((edit->select_start != edit->select_end &&
23286 selection_end < edit->string.len))
23287 {
23288 /* draw unselected text after selected text */
23289 const char *begin = select_end_ptr;
23290 const char *end = nk_str_get_const(&edit->string) +
23291 nk_str_len_char(&edit->string);
23292 NK_ASSERT(select_end_ptr);
23293 nk_edit_draw_text(out, style,
23294 area.x - edit->scrollbar.x,
23295 area.y + selection_offset_end.y - edit->scrollbar.y,
23296 selection_offset_end.x,
23297 begin, (int)(end - begin), row_height, font,
23298 background_color, text_color, nk_true);
23299 }
23300 }
23301
23302 /* cursor */
23303 if (edit->select_start == edit->select_end)
23304 {
23305 if (edit->cursor >= nk_str_len(&edit->string) ||
23306 (cursor_ptr && *cursor_ptr == '\n')) {
23307 /* draw cursor at end of line */
23308 struct nk_rect cursor;
23309 cursor.w = style->cursor_size;
23310 cursor.h = font->height;
23311 cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
23312 cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
23313 cursor.y -= edit->scrollbar.y;
23314 nk_fill_rect(out, cursor, 0, cursor_color);
23315 } else {
23316 /* draw cursor inside text */
23317 int glyph_len;
23318 struct nk_rect label;
23319 struct nk_text txt;
23320
23321 nk_rune unicode;
23322 NK_ASSERT(cursor_ptr);
23323 glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
23324
23325 label.x = area.x + cursor_pos.x - edit->scrollbar.x;
23326 label.y = area.y + cursor_pos.y - edit->scrollbar.y;
23327 label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
23328 label.h = row_height;
23329
23330 txt.padding = nk_vec2(0,0);
23331 txt.background = cursor_color;;
23332 txt.text = cursor_text_color;
23333 nk_fill_rect(out, label, 0, cursor_color);
23334 nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
23335 }
23336 }}
23337 } else {
23338 /* not active so just draw text */
23339 int l = nk_str_len_char(&edit->string);
23340 const char *begin = nk_str_get_const(&edit->string);
23341
23342 const struct nk_style_item *background;
23343 struct nk_color background_color;
23344 struct nk_color text_color;
23345 nk_push_scissor(out, clip);
23346 if (*state & NK_WIDGET_STATE_ACTIVED) {
23347 background = &style->active;
23348 text_color = style->text_active;
23349 } else if (*state & NK_WIDGET_STATE_HOVER) {
23350 background = &style->hover;
23351 text_color = style->text_hover;
23352 } else {
23353 background = &style->normal;
23354 text_color = style->text_normal;
23355 }
23356 if (background->type == NK_STYLE_ITEM_IMAGE)
23357 background_color = nk_rgba(0,0,0,0);
23358 else background_color = background->data.color;
23359 nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
23360 area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
23361 background_color, text_color, nk_false);
23362 }
23363 nk_push_scissor(out, old_clip);}
23364 return ret;
23365 }
23366 NK_API void
23367 nk_edit_focus(struct nk_context *ctx, nk_flags flags)
23368 {
23369 nk_hash hash;
23370 struct nk_window *win;
23371
23372 NK_ASSERT(ctx);
23373 NK_ASSERT(ctx->current);
23374 if (!ctx || !ctx->current) return;
23375
23376 win = ctx->current;
23377 hash = win->edit.seq;
23378 win->edit.active = nk_true;
23379 win->edit.name = hash;
23380 if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
23381 win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
23382 }
23383 NK_API void
23384 nk_edit_unfocus(struct nk_context *ctx)
23385 {
23386 struct nk_window *win;
23387 NK_ASSERT(ctx);
23388 NK_ASSERT(ctx->current);
23389 if (!ctx || !ctx->current) return;
23390
23391 win = ctx->current;
23392 win->edit.active = nk_false;
23393 win->edit.name = 0;
23394 }
23395 NK_API nk_flags
23396 nk_edit_string(struct nk_context *ctx, nk_flags flags,
23397 char *memory, int *len, int max, nk_plugin_filter filter)
23398 {
23399 nk_hash hash;
23400 nk_flags state;
23401 struct nk_text_edit *edit;
23402 struct nk_window *win;
23403
23404 NK_ASSERT(ctx);
23405 NK_ASSERT(memory);
23406 NK_ASSERT(len);
23407 if (!ctx || !memory || !len)
23408 return 0;
23409
23410 filter = (!filter) ? nk_filter_default: filter;
23411 win = ctx->current;
23412 hash = win->edit.seq;
23413 edit = &ctx->text_edit;
23414 nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
23415 NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
23416
23417 if (win->edit.active && hash == win->edit.name) {
23418 if (flags & NK_EDIT_NO_CURSOR)
23419 edit->cursor = nk_utf_len(memory, *len);
23420 else edit->cursor = win->edit.cursor;
23421 if (!(flags & NK_EDIT_SELECTABLE)) {
23422 edit->select_start = win->edit.cursor;
23423 edit->select_end = win->edit.cursor;
23424 } else {
23425 edit->select_start = win->edit.sel_start;
23426 edit->select_end = win->edit.sel_end;
23427 }
23428 edit->mode = win->edit.mode;
23429 edit->scrollbar.x = (float)win->edit.scrollbar.x;
23430 edit->scrollbar.y = (float)win->edit.scrollbar.y;
23431 edit->active = nk_true;
23432 } else edit->active = nk_false;
23433
23434 max = NK_MAX(1, max);
23435 *len = NK_MIN(*len, max-1);
23436 nk_str_init_fixed(&edit->string, memory, (nk_size)max);
23437 edit->string.buffer.allocated = (nk_size)*len;
23438 edit->string.len = nk_utf_len(memory, *len);
23439 state = nk_edit_buffer(ctx, flags, edit, filter);
23440 *len = (int)edit->string.buffer.allocated;
23441
23442 if (edit->active) {
23443 win->edit.cursor = edit->cursor;
23444 win->edit.sel_start = edit->select_start;
23445 win->edit.sel_end = edit->select_end;
23446 win->edit.mode = edit->mode;
23447 win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
23448 win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
23449 } return state;
23450 }
23451 NK_API nk_flags
23452 nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
23453 struct nk_text_edit *edit, nk_plugin_filter filter)
23454 {
23455 struct nk_window *win;
23456 struct nk_style *style;
23457 struct nk_input *in;
23458
23459 enum nk_widget_layout_states state;
23460 struct nk_rect bounds;
23461
23462 nk_flags ret_flags = 0;
23463 unsigned char prev_state;
23464 nk_hash hash;
23465
23466 /* make sure correct values */
23467 NK_ASSERT(ctx);
23468 NK_ASSERT(edit);
23469 NK_ASSERT(ctx->current);
23470 NK_ASSERT(ctx->current->layout);
23471 if (!ctx || !ctx->current || !ctx->current->layout)
23472 return 0;
23473
23474 win = ctx->current;
23475 style = &ctx->style;
23476 state = nk_widget(&bounds, ctx);
23477 if (!state) return state;
23478 in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
23479
23480 /* check if edit is currently hot item */
23481 hash = win->edit.seq++;
23482 if (win->edit.active && hash == win->edit.name) {
23483 if (flags & NK_EDIT_NO_CURSOR)
23484 edit->cursor = edit->string.len;
23485 if (!(flags & NK_EDIT_SELECTABLE)) {
23486 edit->select_start = edit->cursor;
23487 edit->select_end = edit->cursor;
23488 }
23489 if (flags & NK_EDIT_CLIPBOARD)
23490 edit->clip = ctx->clip;
23491 edit->active = (unsigned char)win->edit.active;
23492 } else edit->active = nk_false;
23493 edit->mode = win->edit.mode;
23494
23495 filter = (!filter) ? nk_filter_default: filter;
23496 prev_state = (unsigned char)edit->active;
23497 in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
23498 ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
23499 filter, edit, &style->edit, in, style->font);
23500
23501 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
23502 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
23503 if (edit->active && prev_state != edit->active) {
23504 /* current edit is now hot */
23505 win->edit.active = nk_true;
23506 win->edit.name = hash;
23507 } else if (prev_state && !edit->active) {
23508 /* current edit is now cold */
23509 win->edit.active = nk_false;
23510 } return ret_flags;
23511 }
23512 NK_API nk_flags
23513 nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
23514 char *buffer, int max, nk_plugin_filter filter)
23515 {
23516 nk_flags result;
23517 int len = nk_strlen(buffer);
23518 result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
23519 buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
23520 return result;
23521 }
23522
23523
23524
23525
23526
23527 /* ===============================================================
23528 *
23529 * PROPERTY
23530 *
23531 * ===============================================================*/
23532 NK_LIB void
23533 nk_drag_behavior(nk_flags *state, const struct nk_input *in,
23534 struct nk_rect drag, struct nk_property_variant *variant,
23535 float inc_per_pixel)
23536 {
23537 int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
23538 int left_mouse_click_in_cursor = in &&
23539 nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
23540
23541 nk_widget_state_reset(state);
23542 if (nk_input_is_mouse_hovering_rect(in, drag))
23543 *state = NK_WIDGET_STATE_HOVERED;
23544
23545 if (left_mouse_down && left_mouse_click_in_cursor) {
23546 float delta, pixels;
23547 pixels = in->mouse.delta.x;
23548 delta = pixels * inc_per_pixel;
23549 switch (variant->kind) {
23550 default: break;
23551 case NK_PROPERTY_INT:
23552 variant->value.i = variant->value.i + (int)delta;
23553 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
23554 break;
23555 case NK_PROPERTY_FLOAT:
23556 variant->value.f = variant->value.f + (float)delta;
23557 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
23558 break;
23559 case NK_PROPERTY_DOUBLE:
23560 variant->value.d = variant->value.d + (double)delta;
23561 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
23562 break;
23563 }
23564 *state = NK_WIDGET_STATE_ACTIVE;
23565 }
23566 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
23567 *state |= NK_WIDGET_STATE_ENTERED;
23568 else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
23569 *state |= NK_WIDGET_STATE_LEFT;
23570 }
23571 NK_LIB void
23572 nk_property_behavior(nk_flags *ws, const struct nk_input *in,
23573 struct nk_rect property, struct nk_rect label, struct nk_rect edit,
23574 struct nk_rect empty, int *state, struct nk_property_variant *variant,
23575 float inc_per_pixel)
23576 {
23577 if (in && *state == NK_PROPERTY_DEFAULT) {
23578 if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
23579 *state = NK_PROPERTY_EDIT;
23580 else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
23581 *state = NK_PROPERTY_DRAG;
23582 else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
23583 *state = NK_PROPERTY_DRAG;
23584 }
23585 if (*state == NK_PROPERTY_DRAG) {
23586 nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
23587 if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
23588 }
23589 }
23590 NK_LIB void
23591 nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
23592 const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
23593 const char *name, int len, const struct nk_user_font *font)
23594 {
23595 struct nk_text text;
23596 const struct nk_style_item *background;
23597
23598 /* select correct background and text color */
23599 if (state & NK_WIDGET_STATE_ACTIVED) {
23600 background = &style->active;
23601 text.text = style->label_active;
23602 } else if (state & NK_WIDGET_STATE_HOVER) {
23603 background = &style->hover;
23604 text.text = style->label_hover;
23605 } else {
23606 background = &style->normal;
23607 text.text = style->label_normal;
23608 }
23609
23610 /* draw background */
23611 if (background->type == NK_STYLE_ITEM_IMAGE) {
23612 nk_draw_image(out, *bounds, &background->data.image, nk_white);
23613 text.background = nk_rgba(0,0,0,0);
23614 } else {
23615 text.background = background->data.color;
23616 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
23617 nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
23618 }
23619
23620 /* draw label */
23621 text.padding = nk_vec2(0,0);
23622 nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
23623 }
23624 NK_LIB void
23625 nk_do_property(nk_flags *ws,
23626 struct nk_command_buffer *out, struct nk_rect property,
23627 const char *name, struct nk_property_variant *variant,
23628 float inc_per_pixel, char *buffer, int *len,
23629 int *state, int *cursor, int *select_begin, int *select_end,
23630 const struct nk_style_property *style,
23631 enum nk_property_filter filter, struct nk_input *in,
23632 const struct nk_user_font *font, struct nk_text_edit *text_edit,
23633 enum nk_button_behavior behavior)
23634 {
23635 const nk_plugin_filter filters[] = {
23636 nk_filter_decimal,
23637 nk_filter_float
23638 };
23639 int active, old;
23640 int num_len = 0, name_len;
23641 char string[NK_MAX_NUMBER_BUFFER];
23642 float size;
23643
23644 char *dst = 0;
23645 int *length;
23646
23647 struct nk_rect left;
23648 struct nk_rect right;
23649 struct nk_rect label;
23650 struct nk_rect edit;
23651 struct nk_rect empty;
23652
23653 /* left decrement button */
23654 left.h = font->height/2;
23655 left.w = left.h;
23656 left.x = property.x + style->border + style->padding.x;
23657 left.y = property.y + style->border + property.h/2.0f - left.h/2;
23658
23659 /* text label */
23660 name_len = nk_strlen(name);
23661 size = font->width(font->userdata, font->height, name, name_len);
23662 label.x = left.x + left.w + style->padding.x;
23663 label.w = (float)size + 2 * style->padding.x;
23664 label.y = property.y + style->border + style->padding.y;
23665 label.h = property.h - (2 * style->border + 2 * style->padding.y);
23666
23667 /* right increment button */
23668 right.y = left.y;
23669 right.w = left.w;
23670 right.h = left.h;
23671 right.x = property.x + property.w - (right.w + style->padding.x);
23672
23673 /* edit */
23674 if (*state == NK_PROPERTY_EDIT) {
23675 size = font->width(font->userdata, font->height, buffer, *len);
23676 size += style->edit.cursor_size;
23677 length = len;
23678 dst = buffer;
23679 } else {
23680 switch (variant->kind) {
23681 default: break;
23682 case NK_PROPERTY_INT:
23683 nk_itoa(string, variant->value.i);
23684 num_len = nk_strlen(string);
23685 break;
23686 case NK_PROPERTY_FLOAT:
23687 NK_DTOA(string, (double)variant->value.f);
23688 num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
23689 break;
23690 case NK_PROPERTY_DOUBLE:
23691 NK_DTOA(string, variant->value.d);
23692 num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
23693 break;
23694 }
23695 size = font->width(font->userdata, font->height, string, num_len);
23696 dst = string;
23697 length = &num_len;
23698 }
23699
23700 edit.w = (float)size + 2 * style->padding.x;
23701 edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
23702 edit.x = right.x - (edit.w + style->padding.x);
23703 edit.y = property.y + style->border;
23704 edit.h = property.h - (2 * style->border);
23705
23706 /* empty left space activator */
23707 empty.w = edit.x - (label.x + label.w);
23708 empty.x = label.x + label.w;
23709 empty.y = property.y;
23710 empty.h = property.h;
23711
23712 /* update property */
23713 old = (*state == NK_PROPERTY_EDIT);
23714 nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
23715
23716 /* draw property */
23717 if (style->draw_begin) style->draw_begin(out, style->userdata);
23718 nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
23719 if (style->draw_end) style->draw_end(out, style->userdata);
23720
23721 /* execute right button */
23722 if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
23723 switch (variant->kind) {
23724 default: break;
23725 case NK_PROPERTY_INT:
23726 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
23727 case NK_PROPERTY_FLOAT:
23728 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
23729 case NK_PROPERTY_DOUBLE:
23730 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
23731 }
23732 }
23733 /* execute left button */
23734 if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
23735 switch (variant->kind) {
23736 default: break;
23737 case NK_PROPERTY_INT:
23738 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
23739 case NK_PROPERTY_FLOAT:
23740 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
23741 case NK_PROPERTY_DOUBLE:
23742 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
23743 }
23744 }
23745 if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
23746 /* property has been activated so setup buffer */
23747 NK_MEMCPY(buffer, dst, (nk_size)*length);
23748 *cursor = nk_utf_len(buffer, *length);
23749 *len = *length;
23750 length = len;
23751 dst = buffer;
23752 active = 0;
23753 } else active = (*state == NK_PROPERTY_EDIT);
23754
23755 /* execute and run text edit field */
23756 nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
23757 text_edit->active = (unsigned char)active;
23758 text_edit->string.len = *length;
23759 text_edit->cursor = NK_CLAMP(0, *cursor, *length);
23760 text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
23761 text_edit->select_end = NK_CLAMP(0,*select_end, *length);
23762 text_edit->string.buffer.allocated = (nk_size)*length;
23763 text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
23764 text_edit->string.buffer.memory.ptr = dst;
23765 text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
23766 text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
23767 nk_do_edit(ws, out, edit, NK_EDIT_FIELD|NK_EDIT_AUTO_SELECT,
23768 filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
23769
23770 *length = text_edit->string.len;
23771 *cursor = text_edit->cursor;
23772 *select_begin = text_edit->select_start;
23773 *select_end = text_edit->select_end;
23774 if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
23775 text_edit->active = nk_false;
23776
23777 if (active && !text_edit->active) {
23778 /* property is now not active so convert edit text to value*/
23779 *state = NK_PROPERTY_DEFAULT;
23780 buffer[*len] = '\0';
23781 switch (variant->kind) {
23782 default: break;
23783 case NK_PROPERTY_INT:
23784 variant->value.i = nk_strtoi(buffer, 0);
23785 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
23786 break;
23787 case NK_PROPERTY_FLOAT:
23788 nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
23789 variant->value.f = nk_strtof(buffer, 0);
23790 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
23791 break;
23792 case NK_PROPERTY_DOUBLE:
23793 nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
23794 variant->value.d = nk_strtod(buffer, 0);
23795 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
23796 break;
23797 }
23798 }
23799 }
23800 NK_LIB struct nk_property_variant
23801 nk_property_variant_int(int value, int min_value, int max_value, int step)
23802 {
23803 struct nk_property_variant result;
23804 result.kind = NK_PROPERTY_INT;
23805 result.value.i = value;
23806 result.min_value.i = min_value;
23807 result.max_value.i = max_value;
23808 result.step.i = step;
23809 return result;
23810 }
23811 NK_LIB struct nk_property_variant
23812 nk_property_variant_float(float value, float min_value, float max_value, float step)
23813 {
23814 struct nk_property_variant result;
23815 result.kind = NK_PROPERTY_FLOAT;
23816 result.value.f = value;
23817 result.min_value.f = min_value;
23818 result.max_value.f = max_value;
23819 result.step.f = step;
23820 return result;
23821 }
23822 NK_LIB struct nk_property_variant
23823 nk_property_variant_double(double value, double min_value, double max_value,
23824 double step)
23825 {
23826 struct nk_property_variant result;
23827 result.kind = NK_PROPERTY_DOUBLE;
23828 result.value.d = value;
23829 result.min_value.d = min_value;
23830 result.max_value.d = max_value;
23831 result.step.d = step;
23832 return result;
23833 }
23834 NK_LIB void
23835 nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
23836 float inc_per_pixel, const enum nk_property_filter filter)
23837 {
23838 struct nk_window *win;
23839 struct nk_panel *layout;
23840 struct nk_input *in;
23841 const struct nk_style *style;
23842
23843 struct nk_rect bounds;
23844 enum nk_widget_layout_states s;
23845
23846 int *state = 0;
23847 nk_hash hash = 0;
23848 char *buffer = 0;
23849 int *len = 0;
23850 int *cursor = 0;
23851 int *select_begin = 0;
23852 int *select_end = 0;
23853 int old_state;
23854
23855 char dummy_buffer[NK_MAX_NUMBER_BUFFER];
23856 int dummy_state = NK_PROPERTY_DEFAULT;
23857 int dummy_length = 0;
23858 int dummy_cursor = 0;
23859 int dummy_select_begin = 0;
23860 int dummy_select_end = 0;
23861
23862 NK_ASSERT(ctx);
23863 NK_ASSERT(ctx->current);
23864 NK_ASSERT(ctx->current->layout);
23865 if (!ctx || !ctx->current || !ctx->current->layout)
23866 return;
23867
23868 win = ctx->current;
23869 layout = win->layout;
23870 style = &ctx->style;
23871 s = nk_widget(&bounds, ctx);
23872 if (!s) return;
23873
23874 /* calculate hash from name */
23875 if (name[0] == '#') {
23876 hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
23877 name++; /* special number hash */
23878 } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
23879
23880 /* check if property is currently hot item */
23881 if (win->property.active && hash == win->property.name) {
23882 buffer = win->property.buffer;
23883 len = &win->property.length;
23884 cursor = &win->property.cursor;
23885 state = &win->property.state;
23886 select_begin = &win->property.select_start;
23887 select_end = &win->property.select_end;
23888 } else {
23889 buffer = dummy_buffer;
23890 len = &dummy_length;
23891 cursor = &dummy_cursor;
23892 state = &dummy_state;
23893 select_begin = &dummy_select_begin;
23894 select_end = &dummy_select_end;
23895 }
23896
23897 /* execute property widget */
23898 old_state = *state;
23899 ctx->text_edit.clip = ctx->clip;
23900 in = ((s == NK_WIDGET_ROM && !win->property.active) ||
23901 layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
23902 nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
23903 variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
23904 select_end, &style->property, filter, in, style->font, &ctx->text_edit,
23905 ctx->button_behavior);
23906
23907 if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
23908 /* current property is now hot */
23909 win->property.active = 1;
23910 NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
23911 win->property.length = *len;
23912 win->property.cursor = *cursor;
23913 win->property.state = *state;
23914 win->property.name = hash;
23915 win->property.select_start = *select_begin;
23916 win->property.select_end = *select_end;
23917 if (*state == NK_PROPERTY_DRAG) {
23918 ctx->input.mouse.grab = nk_true;
23919 ctx->input.mouse.grabbed = nk_true;
23920 }
23921 }
23922 /* check if previously active property is now inactive */
23923 if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
23924 if (old_state == NK_PROPERTY_DRAG) {
23925 ctx->input.mouse.grab = nk_false;
23926 ctx->input.mouse.grabbed = nk_false;
23927 ctx->input.mouse.ungrab = nk_true;
23928 }
23929 win->property.select_start = 0;
23930 win->property.select_end = 0;
23931 win->property.active = 0;
23932 }
23933 }
23934 NK_API void
23935 nk_property_int(struct nk_context *ctx, const char *name,
23936 int min, int *val, int max, int step, float inc_per_pixel)
23937 {
23938 struct nk_property_variant variant;
23939 NK_ASSERT(ctx);
23940 NK_ASSERT(name);
23941 NK_ASSERT(val);
23942
23943 if (!ctx || !ctx->current || !name || !val) return;
23944 variant = nk_property_variant_int(*val, min, max, step);
23945 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
23946 *val = variant.value.i;
23947 }
23948 NK_API void
23949 nk_property_float(struct nk_context *ctx, const char *name,
23950 float min, float *val, float max, float step, float inc_per_pixel)
23951 {
23952 struct nk_property_variant variant;
23953 NK_ASSERT(ctx);
23954 NK_ASSERT(name);
23955 NK_ASSERT(val);
23956
23957 if (!ctx || !ctx->current || !name || !val) return;
23958 variant = nk_property_variant_float(*val, min, max, step);
23959 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
23960 *val = variant.value.f;
23961 }
23962 NK_API void
23963 nk_property_double(struct nk_context *ctx, const char *name,
23964 double min, double *val, double max, double step, float inc_per_pixel)
23965 {
23966 struct nk_property_variant variant;
23967 NK_ASSERT(ctx);
23968 NK_ASSERT(name);
23969 NK_ASSERT(val);
23970
23971 if (!ctx || !ctx->current || !name || !val) return;
23972 variant = nk_property_variant_double(*val, min, max, step);
23973 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
23974 *val = variant.value.d;
23975 }
23976 NK_API int
23977 nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
23978 int max, int step, float inc_per_pixel)
23979 {
23980 struct nk_property_variant variant;
23981 NK_ASSERT(ctx);
23982 NK_ASSERT(name);
23983
23984 if (!ctx || !ctx->current || !name) return val;
23985 variant = nk_property_variant_int(val, min, max, step);
23986 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
23987 val = variant.value.i;
23988 return val;
23989 }
23990 NK_API float
23991 nk_propertyf(struct nk_context *ctx, const char *name, float min,
23992 float val, float max, float step, float inc_per_pixel)
23993 {
23994 struct nk_property_variant variant;
23995 NK_ASSERT(ctx);
23996 NK_ASSERT(name);
23997
23998 if (!ctx || !ctx->current || !name) return val;
23999 variant = nk_property_variant_float(val, min, max, step);
24000 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
24001 val = variant.value.f;
24002 return val;
24003 }
24004 NK_API double
24005 nk_propertyd(struct nk_context *ctx, const char *name, double min,
24006 double val, double max, double step, float inc_per_pixel)
24007 {
24008 struct nk_property_variant variant;
24009 NK_ASSERT(ctx);
24010 NK_ASSERT(name);
24011
24012 if (!ctx || !ctx->current || !name) return val;
24013 variant = nk_property_variant_double(val, min, max, step);
24014 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
24015 val = variant.value.d;
24016 return val;
24017 }
24018
24019
24020
24021
24022
24023 /* ==============================================================
24024 *
24025 * CHART
24026 *
24027 * ===============================================================*/
24028 NK_API int
24029 nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
24030 struct nk_color color, struct nk_color highlight,
24031 int count, float min_value, float max_value)
24032 {
24033 struct nk_window *win;
24034 struct nk_chart *chart;
24035 const struct nk_style *config;
24036 const struct nk_style_chart *style;
24037
24038 const struct nk_style_item *background;
24039 struct nk_rect bounds = {0, 0, 0, 0};
24040
24041 NK_ASSERT(ctx);
24042 NK_ASSERT(ctx->current);
24043 NK_ASSERT(ctx->current->layout);
24044
24045 if (!ctx || !ctx->current || !ctx->current->layout) return 0;
24046 if (!nk_widget(&bounds, ctx)) {
24047 chart = &ctx->current->layout->chart;
24048 nk_zero(chart, sizeof(*chart));
24049 return 0;
24050 }
24051
24052 win = ctx->current;
24053 config = &ctx->style;
24054 chart = &win->layout->chart;
24055 style = &config->chart;
24056
24057 /* setup basic generic chart */
24058 nk_zero(chart, sizeof(*chart));
24059 chart->x = bounds.x + style->padding.x;
24060 chart->y = bounds.y + style->padding.y;
24061 chart->w = bounds.w - 2 * style->padding.x;
24062 chart->h = bounds.h - 2 * style->padding.y;
24063 chart->w = NK_MAX(chart->w, 2 * style->padding.x);
24064 chart->h = NK_MAX(chart->h, 2 * style->padding.y);
24065
24066 /* add first slot into chart */
24067 {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
24068 slot->type = type;
24069 slot->count = count;
24070 slot->color = color;
24071 slot->highlight = highlight;
24072 slot->min = NK_MIN(min_value, max_value);
24073 slot->max = NK_MAX(min_value, max_value);
24074 slot->range = slot->max - slot->min;}
24075
24076 /* draw chart background */
24077 background = &style->background;
24078 if (background->type == NK_STYLE_ITEM_IMAGE) {
24079 nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
24080 } else {
24081 nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
24082 nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
24083 style->rounding, style->background.data.color);
24084 }
24085 return 1;
24086 }
24087 NK_API int
24088 nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
24089 int count, float min_value, float max_value)
24090 {
24091 return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
24092 ctx->style.chart.selected_color, count, min_value, max_value);
24093 }
24094 NK_API void
24095 nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
24096 struct nk_color color, struct nk_color highlight,
24097 int count, float min_value, float max_value)
24098 {
24099 NK_ASSERT(ctx);
24100 NK_ASSERT(ctx->current);
24101 NK_ASSERT(ctx->current->layout);
24102 NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
24103 if (!ctx || !ctx->current || !ctx->current->layout) return;
24104 if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
24105
24106 /* add another slot into the graph */
24107 {struct nk_chart *chart = &ctx->current->layout->chart;
24108 struct nk_chart_slot *slot = &chart->slots[chart->slot++];
24109 slot->type = type;
24110 slot->count = count;
24111 slot->color = color;
24112 slot->highlight = highlight;
24113 slot->min = NK_MIN(min_value, max_value);
24114 slot->max = NK_MAX(min_value, max_value);
24115 slot->range = slot->max - slot->min;}
24116 }
24117 NK_API void
24118 nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
24119 int count, float min_value, float max_value)
24120 {
24121 nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
24122 ctx->style.chart.selected_color, count, min_value, max_value);
24123 }
24124 NK_INTERN nk_flags
24125 nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
24126 struct nk_chart *g, float value, int slot)
24127 {
24128 struct nk_panel *layout = win->layout;
24129 const struct nk_input *i = &ctx->input;
24130 struct nk_command_buffer *out = &win->buffer;
24131
24132 nk_flags ret = 0;
24133 struct nk_vec2 cur;
24134 struct nk_rect bounds;
24135 struct nk_color color;
24136 float step;
24137 float range;
24138 float ratio;
24139
24140 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
24141 step = g->w / (float)g->slots[slot].count;
24142 range = g->slots[slot].max - g->slots[slot].min;
24143 ratio = (value - g->slots[slot].min) / range;
24144
24145 if (g->slots[slot].index == 0) {
24146 /* first data point does not have a connection */
24147 g->slots[slot].last.x = g->x;
24148 g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
24149
24150 bounds.x = g->slots[slot].last.x - 2;
24151 bounds.y = g->slots[slot].last.y - 2;
24152 bounds.w = bounds.h = 4;
24153
24154 color = g->slots[slot].color;
24155 if (!(layout->flags & NK_WINDOW_ROM) &&
24156 NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
24157 ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
24158 ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
24159 i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
24160 color = g->slots[slot].highlight;
24161 }
24162 nk_fill_rect(out, bounds, 0, color);
24163 g->slots[slot].index += 1;
24164 return ret;
24165 }
24166
24167 /* draw a line between the last data point and the new one */
24168 color = g->slots[slot].color;
24169 cur.x = g->x + (float)(step * (float)g->slots[slot].index);
24170 cur.y = (g->y + g->h) - (ratio * (float)g->h);
24171 nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
24172
24173 bounds.x = cur.x - 3;
24174 bounds.y = cur.y - 3;
24175 bounds.w = bounds.h = 6;
24176
24177 /* user selection of current data point */
24178 if (!(layout->flags & NK_WINDOW_ROM)) {
24179 if (nk_input_is_mouse_hovering_rect(i, bounds)) {
24180 ret = NK_CHART_HOVERING;
24181 ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
24182 i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
24183 color = g->slots[slot].highlight;
24184 }
24185 }
24186 nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
24187
24188 /* save current data point position */
24189 g->slots[slot].last.x = cur.x;
24190 g->slots[slot].last.y = cur.y;
24191 g->slots[slot].index += 1;
24192 return ret;
24193 }
24194 NK_INTERN nk_flags
24195 nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
24196 struct nk_chart *chart, float value, int slot)
24197 {
24198 struct nk_command_buffer *out = &win->buffer;
24199 const struct nk_input *in = &ctx->input;
24200 struct nk_panel *layout = win->layout;
24201
24202 float ratio;
24203 nk_flags ret = 0;
24204 struct nk_color color;
24205 struct nk_rect item = {0,0,0,0};
24206
24207 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
24208 if (chart->slots[slot].index >= chart->slots[slot].count)
24209 return nk_false;
24210 if (chart->slots[slot].count) {
24211 float padding = (float)(chart->slots[slot].count-1);
24212 item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
24213 }
24214
24215 /* calculate bounds of current bar chart entry */
24216 color = chart->slots[slot].color;;
24217 item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
24218 if (value >= 0) {
24219 ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
24220 item.y = (chart->y + chart->h) - chart->h * ratio;
24221 } else {
24222 ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
24223 item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
24224 }
24225 item.x = chart->x + ((float)chart->slots[slot].index * item.w);
24226 item.x = item.x + ((float)chart->slots[slot].index);
24227
24228 /* user chart bar selection */
24229 if (!(layout->flags & NK_WINDOW_ROM) &&
24230 NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
24231 ret = NK_CHART_HOVERING;
24232 ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
24233 in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
24234 color = chart->slots[slot].highlight;
24235 }
24236 nk_fill_rect(out, item, 0, color);
24237 chart->slots[slot].index += 1;
24238 return ret;
24239 }
24240 NK_API nk_flags
24241 nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
24242 {
24243 nk_flags flags;
24244 struct nk_window *win;
24245
24246 NK_ASSERT(ctx);
24247 NK_ASSERT(ctx->current);
24248 NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
24249 NK_ASSERT(slot < ctx->current->layout->chart.slot);
24250 if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
24251 if (slot >= ctx->current->layout->chart.slot) return nk_false;
24252
24253 win = ctx->current;
24254 if (win->layout->chart.slot < slot) return nk_false;
24255 switch (win->layout->chart.slots[slot].type) {
24256 case NK_CHART_LINES:
24257 flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
24258 case NK_CHART_COLUMN:
24259 flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
24260 default:
24261 case NK_CHART_MAX:
24262 flags = 0;
24263 }
24264 return flags;
24265 }
24266 NK_API nk_flags
24267 nk_chart_push(struct nk_context *ctx, float value)
24268 {
24269 return nk_chart_push_slot(ctx, value, 0);
24270 }
24271 NK_API void
24272 nk_chart_end(struct nk_context *ctx)
24273 {
24274 struct nk_window *win;
24275 struct nk_chart *chart;
24276
24277 NK_ASSERT(ctx);
24278 NK_ASSERT(ctx->current);
24279 if (!ctx || !ctx->current)
24280 return;
24281
24282 win = ctx->current;
24283 chart = &win->layout->chart;
24284 NK_MEMSET(chart, 0, sizeof(*chart));
24285 return;
24286 }
24287 NK_API void
24288 nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
24289 int count, int offset)
24290 {
24291 int i = 0;
24292 float min_value;
24293 float max_value;
24294
24295 NK_ASSERT(ctx);
24296 NK_ASSERT(values);
24297 if (!ctx || !values || !count) return;
24298
24299 min_value = values[offset];
24300 max_value = values[offset];
24301 for (i = 0; i < count; ++i) {
24302 min_value = NK_MIN(values[i + offset], min_value);
24303 max_value = NK_MAX(values[i + offset], max_value);
24304 }
24305
24306 if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
24307 for (i = 0; i < count; ++i)
24308 nk_chart_push(ctx, values[i + offset]);
24309 nk_chart_end(ctx);
24310 }
24311 }
24312 NK_API void
24313 nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
24314 float(*value_getter)(void* user, int index), int count, int offset)
24315 {
24316 int i = 0;
24317 float min_value;
24318 float max_value;
24319
24320 NK_ASSERT(ctx);
24321 NK_ASSERT(value_getter);
24322 if (!ctx || !value_getter || !count) return;
24323
24324 max_value = min_value = value_getter(userdata, offset);
24325 for (i = 0; i < count; ++i) {
24326 float value = value_getter(userdata, i + offset);
24327 min_value = NK_MIN(value, min_value);
24328 max_value = NK_MAX(value, max_value);
24329 }
24330
24331 if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
24332 for (i = 0; i < count; ++i)
24333 nk_chart_push(ctx, value_getter(userdata, i + offset));
24334 nk_chart_end(ctx);
24335 }
24336 }
24337
24338
24339
24340
24341
24342 /* ==============================================================
24343 *
24344 * COLOR PICKER
24345 *
24346 * ===============================================================*/
24347 NK_LIB int
24348 nk_color_picker_behavior(nk_flags *state,
24349 const struct nk_rect *bounds, const struct nk_rect *matrix,
24350 const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
24351 struct nk_colorf *color, const struct nk_input *in)
24352 {
24353 float hsva[4];
24354 int value_changed = 0;
24355 int hsv_changed = 0;
24356
24357 NK_ASSERT(state);
24358 NK_ASSERT(matrix);
24359 NK_ASSERT(hue_bar);
24360 NK_ASSERT(color);
24361
24362 /* color matrix */
24363 nk_colorf_hsva_fv(hsva, *color);
24364 if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
24365 hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
24366 hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
24367 value_changed = hsv_changed = 1;
24368 }
24369 /* hue bar */
24370 if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
24371 hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
24372 value_changed = hsv_changed = 1;
24373 }
24374 /* alpha bar */
24375 if (alpha_bar) {
24376 if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
24377 hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
24378 value_changed = 1;
24379 }
24380 }
24381 nk_widget_state_reset(state);
24382 if (hsv_changed) {
24383 *color = nk_hsva_colorfv(hsva);
24384 *state = NK_WIDGET_STATE_ACTIVE;
24385 }
24386 if (value_changed) {
24387 color->a = hsva[3];
24388 *state = NK_WIDGET_STATE_ACTIVE;
24389 }
24390 /* set color picker widget state */
24391 if (nk_input_is_mouse_hovering_rect(in, *bounds))
24392 *state = NK_WIDGET_STATE_HOVERED;
24393 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
24394 *state |= NK_WIDGET_STATE_ENTERED;
24395 else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
24396 *state |= NK_WIDGET_STATE_LEFT;
24397 return value_changed;
24398 }
24399 NK_LIB void
24400 nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
24401 const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
24402 struct nk_colorf col)
24403 {
24404 NK_STORAGE const struct nk_color black = {0,0,0,255};
24405 NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
24406 NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
24407
24408 const float crosshair_size = 7.0f;
24409 struct nk_color temp;
24410 float hsva[4];
24411 float line_y;
24412 int i;
24413
24414 NK_ASSERT(o);
24415 NK_ASSERT(matrix);
24416 NK_ASSERT(hue_bar);
24417
24418 /* draw hue bar */
24419 nk_colorf_hsva_fv(hsva, col);
24420 for (i = 0; i < 6; ++i) {
24421 NK_GLOBAL const struct nk_color hue_colors[] = {
24422 {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
24423 {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
24424 };
24425 nk_fill_rect_multi_color(o,
24426 nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
24427 hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
24428 hue_colors[i+1], hue_colors[i+1]);
24429 }
24430 line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
24431 nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
24432 line_y, 1, nk_rgb(255,255,255));
24433
24434 /* draw alpha bar */
24435 if (alpha_bar) {
24436 float alpha = NK_SATURATE(col.a);
24437 line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
24438
24439 nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
24440 nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
24441 line_y, 1, nk_rgb(255,255,255));
24442 }
24443
24444 /* draw color matrix */
24445 temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
24446 nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
24447 nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
24448
24449 /* draw cross-hair */
24450 {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
24451 p.x = (float)(int)(matrix->x + S * matrix->w);
24452 p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
24453 nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
24454 nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
24455 nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
24456 nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
24457 }
24458 NK_LIB int
24459 nk_do_color_picker(nk_flags *state,
24460 struct nk_command_buffer *out, struct nk_colorf *col,
24461 enum nk_color_format fmt, struct nk_rect bounds,
24462 struct nk_vec2 padding, const struct nk_input *in,
24463 const struct nk_user_font *font)
24464 {
24465 int ret = 0;
24466 struct nk_rect matrix;
24467 struct nk_rect hue_bar;
24468 struct nk_rect alpha_bar;
24469 float bar_w;
24470
24471 NK_ASSERT(out);
24472 NK_ASSERT(col);
24473 NK_ASSERT(state);
24474 NK_ASSERT(font);
24475 if (!out || !col || !state || !font)
24476 return ret;
24477
24478 bar_w = font->height;
24479 bounds.x += padding.x;
24480 bounds.y += padding.x;
24481 bounds.w -= 2 * padding.x;
24482 bounds.h -= 2 * padding.y;
24483
24484 matrix.x = bounds.x;
24485 matrix.y = bounds.y;
24486 matrix.h = bounds.h;
24487 matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
24488
24489 hue_bar.w = bar_w;
24490 hue_bar.y = bounds.y;
24491 hue_bar.h = matrix.h;
24492 hue_bar.x = matrix.x + matrix.w + padding.x;
24493
24494 alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
24495 alpha_bar.y = bounds.y;
24496 alpha_bar.w = bar_w;
24497 alpha_bar.h = matrix.h;
24498
24499 ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
24500 (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
24501 nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
24502 return ret;
24503 }
24504 NK_API int
24505 nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
24506 enum nk_color_format fmt)
24507 {
24508 struct nk_window *win;
24509 struct nk_panel *layout;
24510 const struct nk_style *config;
24511 const struct nk_input *in;
24512
24513 enum nk_widget_layout_states state;
24514 struct nk_rect bounds;
24515
24516 NK_ASSERT(ctx);
24517 NK_ASSERT(color);
24518 NK_ASSERT(ctx->current);
24519 NK_ASSERT(ctx->current->layout);
24520 if (!ctx || !ctx->current || !ctx->current->layout || !color)
24521 return 0;
24522
24523 win = ctx->current;
24524 config = &ctx->style;
24525 layout = win->layout;
24526 state = nk_widget(&bounds, ctx);
24527 if (!state) return 0;
24528 in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
24529 return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
24530 nk_vec2(0,0), in, config->font);
24531 }
24532 NK_API struct nk_colorf
24533 nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
24534 enum nk_color_format fmt)
24535 {
24536 nk_color_pick(ctx, &color, fmt);
24537 return color;
24538 }
24539
24540
24541
24542
24543
24544 /* ==============================================================
24545 *
24546 * COMBO
24547 *
24548 * ===============================================================*/
24549 NK_INTERN int
24550 nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
24551 struct nk_vec2 size, int is_clicked, struct nk_rect header)
24552 {
24553 struct nk_window *popup;
24554 int is_open = 0;
24555 int is_active = 0;
24556 struct nk_rect body;
24557 nk_hash hash;
24558
24559 NK_ASSERT(ctx);
24560 NK_ASSERT(ctx->current);
24561 NK_ASSERT(ctx->current->layout);
24562 if (!ctx || !ctx->current || !ctx->current->layout)
24563 return 0;
24564
24565 popup = win->popup.win;
24566 body.x = header.x;
24567 body.w = size.x;
24568 body.y = header.y + header.h-ctx->style.window.combo_border;
24569 body.h = size.y;
24570
24571 hash = win->popup.combo_count++;
24572 is_open = (popup) ? nk_true:nk_false;
24573 is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
24574 if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
24575 (!is_open && !is_active && !is_clicked)) return 0;
24576 if (!nk_nonblock_begin(ctx, 0, body,
24577 (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
24578
24579 win->popup.type = NK_PANEL_COMBO;
24580 win->popup.name = hash;
24581 return 1;
24582 }
24583 NK_API int
24584 nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
24585 struct nk_vec2 size)
24586 {
24587 const struct nk_input *in;
24588 struct nk_window *win;
24589 struct nk_style *style;
24590
24591 enum nk_widget_layout_states s;
24592 int is_clicked = nk_false;
24593 struct nk_rect header;
24594 const struct nk_style_item *background;
24595 struct nk_text text;
24596
24597 NK_ASSERT(ctx);
24598 NK_ASSERT(selected);
24599 NK_ASSERT(ctx->current);
24600 NK_ASSERT(ctx->current->layout);
24601 if (!ctx || !ctx->current || !ctx->current->layout || !selected)
24602 return 0;
24603
24604 win = ctx->current;
24605 style = &ctx->style;
24606 s = nk_widget(&header, ctx);
24607 if (s == NK_WIDGET_INVALID)
24608 return 0;
24609
24610 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
24611 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
24612 is_clicked = nk_true;
24613
24614 /* draw combo box header background and border */
24615 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
24616 background = &style->combo.active;
24617 text.text = style->combo.label_active;
24618 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
24619 background = &style->combo.hover;
24620 text.text = style->combo.label_hover;
24621 } else {
24622 background = &style->combo.normal;
24623 text.text = style->combo.label_normal;
24624 }
24625 if (background->type == NK_STYLE_ITEM_IMAGE) {
24626 text.background = nk_rgba(0,0,0,0);
24627 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
24628 } else {
24629 text.background = background->data.color;
24630 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
24631 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
24632 }
24633 {
24634 /* print currently selected text item */
24635 struct nk_rect label;
24636 struct nk_rect button;
24637 struct nk_rect content;
24638
24639 enum nk_symbol_type sym;
24640 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24641 sym = style->combo.sym_hover;
24642 else if (is_clicked)
24643 sym = style->combo.sym_active;
24644 else sym = style->combo.sym_normal;
24645
24646 /* calculate button */
24647 button.w = header.h - 2 * style->combo.button_padding.y;
24648 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
24649 button.y = header.y + style->combo.button_padding.y;
24650 button.h = button.w;
24651
24652 content.x = button.x + style->combo.button.padding.x;
24653 content.y = button.y + style->combo.button.padding.y;
24654 content.w = button.w - 2 * style->combo.button.padding.x;
24655 content.h = button.h - 2 * style->combo.button.padding.y;
24656
24657 /* draw selected label */
24658 text.padding = nk_vec2(0,0);
24659 label.x = header.x + style->combo.content_padding.x;
24660 label.y = header.y + style->combo.content_padding.y;
24661 label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;;
24662 label.h = header.h - 2 * style->combo.content_padding.y;
24663 nk_widget_text(&win->buffer, label, selected, len, &text,
24664 NK_TEXT_LEFT, ctx->style.font);
24665
24666 /* draw open/close button */
24667 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
24668 &ctx->style.combo.button, sym, style->font);
24669 }
24670 return nk_combo_begin(ctx, win, size, is_clicked, header);
24671 }
24672 NK_API int
24673 nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
24674 {
24675 return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
24676 }
24677 NK_API int
24678 nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
24679 {
24680 struct nk_window *win;
24681 struct nk_style *style;
24682 const struct nk_input *in;
24683
24684 struct nk_rect header;
24685 int is_clicked = nk_false;
24686 enum nk_widget_layout_states s;
24687 const struct nk_style_item *background;
24688
24689 NK_ASSERT(ctx);
24690 NK_ASSERT(ctx->current);
24691 NK_ASSERT(ctx->current->layout);
24692 if (!ctx || !ctx->current || !ctx->current->layout)
24693 return 0;
24694
24695 win = ctx->current;
24696 style = &ctx->style;
24697 s = nk_widget(&header, ctx);
24698 if (s == NK_WIDGET_INVALID)
24699 return 0;
24700
24701 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
24702 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
24703 is_clicked = nk_true;
24704
24705 /* draw combo box header background and border */
24706 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
24707 background = &style->combo.active;
24708 else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24709 background = &style->combo.hover;
24710 else background = &style->combo.normal;
24711
24712 if (background->type == NK_STYLE_ITEM_IMAGE) {
24713 nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
24714 } else {
24715 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
24716 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
24717 }
24718 {
24719 struct nk_rect content;
24720 struct nk_rect button;
24721 struct nk_rect bounds;
24722
24723 enum nk_symbol_type sym;
24724 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24725 sym = style->combo.sym_hover;
24726 else if (is_clicked)
24727 sym = style->combo.sym_active;
24728 else sym = style->combo.sym_normal;
24729
24730 /* calculate button */
24731 button.w = header.h - 2 * style->combo.button_padding.y;
24732 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
24733 button.y = header.y + style->combo.button_padding.y;
24734 button.h = button.w;
24735
24736 content.x = button.x + style->combo.button.padding.x;
24737 content.y = button.y + style->combo.button.padding.y;
24738 content.w = button.w - 2 * style->combo.button.padding.x;
24739 content.h = button.h - 2 * style->combo.button.padding.y;
24740
24741 /* draw color */
24742 bounds.h = header.h - 4 * style->combo.content_padding.y;
24743 bounds.y = header.y + 2 * style->combo.content_padding.y;
24744 bounds.x = header.x + 2 * style->combo.content_padding.x;
24745 bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
24746 nk_fill_rect(&win->buffer, bounds, 0, color);
24747
24748 /* draw open/close button */
24749 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
24750 &ctx->style.combo.button, sym, style->font);
24751 }
24752 return nk_combo_begin(ctx, win, size, is_clicked, header);
24753 }
24754 NK_API int
24755 nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
24756 {
24757 struct nk_window *win;
24758 struct nk_style *style;
24759 const struct nk_input *in;
24760
24761 struct nk_rect header;
24762 int is_clicked = nk_false;
24763 enum nk_widget_layout_states s;
24764 const struct nk_style_item *background;
24765 struct nk_color sym_background;
24766 struct nk_color symbol_color;
24767
24768 NK_ASSERT(ctx);
24769 NK_ASSERT(ctx->current);
24770 NK_ASSERT(ctx->current->layout);
24771 if (!ctx || !ctx->current || !ctx->current->layout)
24772 return 0;
24773
24774 win = ctx->current;
24775 style = &ctx->style;
24776 s = nk_widget(&header, ctx);
24777 if (s == NK_WIDGET_INVALID)
24778 return 0;
24779
24780 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
24781 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
24782 is_clicked = nk_true;
24783
24784 /* draw combo box header background and border */
24785 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
24786 background = &style->combo.active;
24787 symbol_color = style->combo.symbol_active;
24788 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
24789 background = &style->combo.hover;
24790 symbol_color = style->combo.symbol_hover;
24791 } else {
24792 background = &style->combo.normal;
24793 symbol_color = style->combo.symbol_hover;
24794 }
24795
24796 if (background->type == NK_STYLE_ITEM_IMAGE) {
24797 sym_background = nk_rgba(0,0,0,0);
24798 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
24799 } else {
24800 sym_background = background->data.color;
24801 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
24802 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
24803 }
24804 {
24805 struct nk_rect bounds = {0,0,0,0};
24806 struct nk_rect content;
24807 struct nk_rect button;
24808
24809 enum nk_symbol_type sym;
24810 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24811 sym = style->combo.sym_hover;
24812 else if (is_clicked)
24813 sym = style->combo.sym_active;
24814 else sym = style->combo.sym_normal;
24815
24816 /* calculate button */
24817 button.w = header.h - 2 * style->combo.button_padding.y;
24818 button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
24819 button.y = header.y + style->combo.button_padding.y;
24820 button.h = button.w;
24821
24822 content.x = button.x + style->combo.button.padding.x;
24823 content.y = button.y + style->combo.button.padding.y;
24824 content.w = button.w - 2 * style->combo.button.padding.x;
24825 content.h = button.h - 2 * style->combo.button.padding.y;
24826
24827 /* draw symbol */
24828 bounds.h = header.h - 2 * style->combo.content_padding.y;
24829 bounds.y = header.y + style->combo.content_padding.y;
24830 bounds.x = header.x + style->combo.content_padding.x;
24831 bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
24832 nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
24833 1.0f, style->font);
24834
24835 /* draw open/close button */
24836 nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
24837 &ctx->style.combo.button, sym, style->font);
24838 }
24839 return nk_combo_begin(ctx, win, size, is_clicked, header);
24840 }
24841 NK_API int
24842 nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
24843 enum nk_symbol_type symbol, struct nk_vec2 size)
24844 {
24845 struct nk_window *win;
24846 struct nk_style *style;
24847 struct nk_input *in;
24848
24849 struct nk_rect header;
24850 int is_clicked = nk_false;
24851 enum nk_widget_layout_states s;
24852 const struct nk_style_item *background;
24853 struct nk_color symbol_color;
24854 struct nk_text text;
24855
24856 NK_ASSERT(ctx);
24857 NK_ASSERT(ctx->current);
24858 NK_ASSERT(ctx->current->layout);
24859 if (!ctx || !ctx->current || !ctx->current->layout)
24860 return 0;
24861
24862 win = ctx->current;
24863 style = &ctx->style;
24864 s = nk_widget(&header, ctx);
24865 if (!s) return 0;
24866
24867 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
24868 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
24869 is_clicked = nk_true;
24870
24871 /* draw combo box header background and border */
24872 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
24873 background = &style->combo.active;
24874 symbol_color = style->combo.symbol_active;
24875 text.text = style->combo.label_active;
24876 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
24877 background = &style->combo.hover;
24878 symbol_color = style->combo.symbol_hover;
24879 text.text = style->combo.label_hover;
24880 } else {
24881 background = &style->combo.normal;
24882 symbol_color = style->combo.symbol_normal;
24883 text.text = style->combo.label_normal;
24884 }
24885 if (background->type == NK_STYLE_ITEM_IMAGE) {
24886 text.background = nk_rgba(0,0,0,0);
24887 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
24888 } else {
24889 text.background = background->data.color;
24890 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
24891 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
24892 }
24893 {
24894 struct nk_rect content;
24895 struct nk_rect button;
24896 struct nk_rect label;
24897 struct nk_rect image;
24898
24899 enum nk_symbol_type sym;
24900 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24901 sym = style->combo.sym_hover;
24902 else if (is_clicked)
24903 sym = style->combo.sym_active;
24904 else sym = style->combo.sym_normal;
24905
24906 /* calculate button */
24907 button.w = header.h - 2 * style->combo.button_padding.y;
24908 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
24909 button.y = header.y + style->combo.button_padding.y;
24910 button.h = button.w;
24911
24912 content.x = button.x + style->combo.button.padding.x;
24913 content.y = button.y + style->combo.button.padding.y;
24914 content.w = button.w - 2 * style->combo.button.padding.x;
24915 content.h = button.h - 2 * style->combo.button.padding.y;
24916 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
24917 &ctx->style.combo.button, sym, style->font);
24918
24919 /* draw symbol */
24920 image.x = header.x + style->combo.content_padding.x;
24921 image.y = header.y + style->combo.content_padding.y;
24922 image.h = header.h - 2 * style->combo.content_padding.y;
24923 image.w = image.h;
24924 nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
24925 1.0f, style->font);
24926
24927 /* draw label */
24928 text.padding = nk_vec2(0,0);
24929 label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
24930 label.y = header.y + style->combo.content_padding.y;
24931 label.w = (button.x - style->combo.content_padding.x) - label.x;
24932 label.h = header.h - 2 * style->combo.content_padding.y;
24933 nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
24934 }
24935 return nk_combo_begin(ctx, win, size, is_clicked, header);
24936 }
24937 NK_API int
24938 nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
24939 {
24940 struct nk_window *win;
24941 struct nk_style *style;
24942 const struct nk_input *in;
24943
24944 struct nk_rect header;
24945 int is_clicked = nk_false;
24946 enum nk_widget_layout_states s;
24947 const struct nk_style_item *background;
24948
24949 NK_ASSERT(ctx);
24950 NK_ASSERT(ctx->current);
24951 NK_ASSERT(ctx->current->layout);
24952 if (!ctx || !ctx->current || !ctx->current->layout)
24953 return 0;
24954
24955 win = ctx->current;
24956 style = &ctx->style;
24957 s = nk_widget(&header, ctx);
24958 if (s == NK_WIDGET_INVALID)
24959 return 0;
24960
24961 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
24962 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
24963 is_clicked = nk_true;
24964
24965 /* draw combo box header background and border */
24966 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
24967 background = &style->combo.active;
24968 else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24969 background = &style->combo.hover;
24970 else background = &style->combo.normal;
24971
24972 if (background->type == NK_STYLE_ITEM_IMAGE) {
24973 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
24974 } else {
24975 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
24976 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
24977 }
24978 {
24979 struct nk_rect bounds = {0,0,0,0};
24980 struct nk_rect content;
24981 struct nk_rect button;
24982
24983 enum nk_symbol_type sym;
24984 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
24985 sym = style->combo.sym_hover;
24986 else if (is_clicked)
24987 sym = style->combo.sym_active;
24988 else sym = style->combo.sym_normal;
24989
24990 /* calculate button */
24991 button.w = header.h - 2 * style->combo.button_padding.y;
24992 button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
24993 button.y = header.y + style->combo.button_padding.y;
24994 button.h = button.w;
24995
24996 content.x = button.x + style->combo.button.padding.x;
24997 content.y = button.y + style->combo.button.padding.y;
24998 content.w = button.w - 2 * style->combo.button.padding.x;
24999 content.h = button.h - 2 * style->combo.button.padding.y;
25000
25001 /* draw image */
25002 bounds.h = header.h - 2 * style->combo.content_padding.y;
25003 bounds.y = header.y + style->combo.content_padding.y;
25004 bounds.x = header.x + style->combo.content_padding.x;
25005 bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
25006 nk_draw_image(&win->buffer, bounds, &img, nk_white);
25007
25008 /* draw open/close button */
25009 nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
25010 &ctx->style.combo.button, sym, style->font);
25011 }
25012 return nk_combo_begin(ctx, win, size, is_clicked, header);
25013 }
25014 NK_API int
25015 nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
25016 struct nk_image img, struct nk_vec2 size)
25017 {
25018 struct nk_window *win;
25019 struct nk_style *style;
25020 struct nk_input *in;
25021
25022 struct nk_rect header;
25023 int is_clicked = nk_false;
25024 enum nk_widget_layout_states s;
25025 const struct nk_style_item *background;
25026 struct nk_text text;
25027
25028 NK_ASSERT(ctx);
25029 NK_ASSERT(ctx->current);
25030 NK_ASSERT(ctx->current->layout);
25031 if (!ctx || !ctx->current || !ctx->current->layout)
25032 return 0;
25033
25034 win = ctx->current;
25035 style = &ctx->style;
25036 s = nk_widget(&header, ctx);
25037 if (!s) return 0;
25038
25039 in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
25040 if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
25041 is_clicked = nk_true;
25042
25043 /* draw combo box header background and border */
25044 if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
25045 background = &style->combo.active;
25046 text.text = style->combo.label_active;
25047 } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
25048 background = &style->combo.hover;
25049 text.text = style->combo.label_hover;
25050 } else {
25051 background = &style->combo.normal;
25052 text.text = style->combo.label_normal;
25053 }
25054 if (background->type == NK_STYLE_ITEM_IMAGE) {
25055 text.background = nk_rgba(0,0,0,0);
25056 nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
25057 } else {
25058 text.background = background->data.color;
25059 nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
25060 nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
25061 }
25062 {
25063 struct nk_rect content;
25064 struct nk_rect button;
25065 struct nk_rect label;
25066 struct nk_rect image;
25067
25068 enum nk_symbol_type sym;
25069 if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
25070 sym = style->combo.sym_hover;
25071 else if (is_clicked)
25072 sym = style->combo.sym_active;
25073 else sym = style->combo.sym_normal;
25074
25075 /* calculate button */
25076 button.w = header.h - 2 * style->combo.button_padding.y;
25077 button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
25078 button.y = header.y + style->combo.button_padding.y;
25079 button.h = button.w;
25080
25081 content.x = button.x + style->combo.button.padding.x;
25082 content.y = button.y + style->combo.button.padding.y;
25083 content.w = button.w - 2 * style->combo.button.padding.x;
25084 content.h = button.h - 2 * style->combo.button.padding.y;
25085 nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
25086 &ctx->style.combo.button, sym, style->font);
25087
25088 /* draw image */
25089 image.x = header.x + style->combo.content_padding.x;
25090 image.y = header.y + style->combo.content_padding.y;
25091 image.h = header.h - 2 * style->combo.content_padding.y;
25092 image.w = image.h;
25093 nk_draw_image(&win->buffer, image, &img, nk_white);
25094
25095 /* draw label */
25096 text.padding = nk_vec2(0,0);
25097 label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
25098 label.y = header.y + style->combo.content_padding.y;
25099 label.w = (button.x - style->combo.content_padding.x) - label.x;
25100 label.h = header.h - 2 * style->combo.content_padding.y;
25101 nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
25102 }
25103 return nk_combo_begin(ctx, win, size, is_clicked, header);
25104 }
25105 NK_API int
25106 nk_combo_begin_symbol_label(struct nk_context *ctx,
25107 const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
25108 {
25109 return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
25110 }
25111 NK_API int
25112 nk_combo_begin_image_label(struct nk_context *ctx,
25113 const char *selected, struct nk_image img, struct nk_vec2 size)
25114 {
25115 return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
25116 }
25117 NK_API int
25118 nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
25119 {
25120 return nk_contextual_item_text(ctx, text, len, align);
25121 }
25122 NK_API int
25123 nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
25124 {
25125 return nk_contextual_item_label(ctx, label, align);
25126 }
25127 NK_API int
25128 nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
25129 int len, nk_flags alignment)
25130 {
25131 return nk_contextual_item_image_text(ctx, img, text, len, alignment);
25132 }
25133 NK_API int
25134 nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
25135 const char *text, nk_flags alignment)
25136 {
25137 return nk_contextual_item_image_label(ctx, img, text, alignment);
25138 }
25139 NK_API int
25140 nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
25141 const char *text, int len, nk_flags alignment)
25142 {
25143 return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
25144 }
25145 NK_API int
25146 nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
25147 const char *label, nk_flags alignment)
25148 {
25149 return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
25150 }
25151 NK_API void nk_combo_end(struct nk_context *ctx)
25152 {
25153 nk_contextual_end(ctx);
25154 }
25155 NK_API void nk_combo_close(struct nk_context *ctx)
25156 {
25157 nk_contextual_close(ctx);
25158 }
25159 NK_API int
25160 nk_combo(struct nk_context *ctx, const char **items, int count,
25161 int selected, int item_height, struct nk_vec2 size)
25162 {
25163 int i = 0;
25164 int max_height;
25165 struct nk_vec2 item_spacing;
25166 struct nk_vec2 window_padding;
25167
25168 NK_ASSERT(ctx);
25169 NK_ASSERT(items);
25170 NK_ASSERT(ctx->current);
25171 if (!ctx || !items ||!count)
25172 return selected;
25173
25174 item_spacing = ctx->style.window.spacing;
25175 window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
25176 max_height = count * item_height + count * (int)item_spacing.y;
25177 max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
25178 size.y = NK_MIN(size.y, (float)max_height);
25179 if (nk_combo_begin_label(ctx, items[selected], size)) {
25180 nk_layout_row_dynamic(ctx, (float)item_height, 1);
25181 for (i = 0; i < count; ++i) {
25182 if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
25183 selected = i;
25184 }
25185 nk_combo_end(ctx);
25186 }
25187 return selected;
25188 }
25189 NK_API int
25190 nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
25191 int separator, int selected, int count, int item_height, struct nk_vec2 size)
25192 {
25193 int i;
25194 int max_height;
25195 struct nk_vec2 item_spacing;
25196 struct nk_vec2 window_padding;
25197 const char *current_item;
25198 const char *iter;
25199 int length = 0;
25200
25201 NK_ASSERT(ctx);
25202 NK_ASSERT(items_separated_by_separator);
25203 if (!ctx || !items_separated_by_separator)
25204 return selected;
25205
25206 /* calculate popup window */
25207 item_spacing = ctx->style.window.spacing;
25208 window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
25209 max_height = count * item_height + count * (int)item_spacing.y;
25210 max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
25211 size.y = NK_MIN(size.y, (float)max_height);
25212
25213 /* find selected item */
25214 current_item = items_separated_by_separator;
25215 for (i = 0; i < count; ++i) {
25216 iter = current_item;
25217 while (*iter && *iter != separator) iter++;
25218 length = (int)(iter - current_item);
25219 if (i == selected) break;
25220 current_item = iter + 1;
25221 }
25222
25223 if (nk_combo_begin_text(ctx, current_item, length, size)) {
25224 current_item = items_separated_by_separator;
25225 nk_layout_row_dynamic(ctx, (float)item_height, 1);
25226 for (i = 0; i < count; ++i) {
25227 iter = current_item;
25228 while (*iter && *iter != separator) iter++;
25229 length = (int)(iter - current_item);
25230 if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
25231 selected = i;
25232 current_item = current_item + length + 1;
25233 }
25234 nk_combo_end(ctx);
25235 }
25236 return selected;
25237 }
25238 NK_API int
25239 nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
25240 int selected, int count, int item_height, struct nk_vec2 size)
25241 {
25242 return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
25243 }
25244 NK_API int
25245 nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
25246 void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
25247 {
25248 int i;
25249 int max_height;
25250 struct nk_vec2 item_spacing;
25251 struct nk_vec2 window_padding;
25252 const char *item;
25253
25254 NK_ASSERT(ctx);
25255 NK_ASSERT(item_getter);
25256 if (!ctx || !item_getter)
25257 return selected;
25258
25259 /* calculate popup window */
25260 item_spacing = ctx->style.window.spacing;
25261 window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
25262 max_height = count * item_height + count * (int)item_spacing.y;
25263 max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
25264 size.y = NK_MIN(size.y, (float)max_height);
25265
25266 item_getter(userdata, selected, &item);
25267 if (nk_combo_begin_label(ctx, item, size)) {
25268 nk_layout_row_dynamic(ctx, (float)item_height, 1);
25269 for (i = 0; i < count; ++i) {
25270 item_getter(userdata, i, &item);
25271 if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
25272 selected = i;
25273 }
25274 nk_combo_end(ctx);
25275 } return selected;
25276 }
25277 NK_API void
25278 nk_combobox(struct nk_context *ctx, const char **items, int count,
25279 int *selected, int item_height, struct nk_vec2 size)
25280 {
25281 *selected = nk_combo(ctx, items, count, *selected, item_height, size);
25282 }
25283 NK_API void
25284 nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
25285 int *selected, int count, int item_height, struct nk_vec2 size)
25286 {
25287 *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
25288 }
25289 NK_API void
25290 nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
25291 int separator,int *selected, int count, int item_height, struct nk_vec2 size)
25292 {
25293 *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
25294 *selected, count, item_height, size);
25295 }
25296 NK_API void
25297 nk_combobox_callback(struct nk_context *ctx,
25298 void(*item_getter)(void* data, int id, const char **out_text),
25299 void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
25300 {
25301 *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
25302 }
25303
25304
25305
25306
25307
25308 /* ===============================================================
25309 *
25310 * TOOLTIP
25311 *
25312 * ===============================================================*/
25313 NK_API int
25314 nk_tooltip_begin(struct nk_context *ctx, float width)
25315 {
25316 int x,y,w,h;
25317 struct nk_window *win;
25318 const struct nk_input *in;
25319 struct nk_rect bounds;
25320 int ret;
25321
25322 NK_ASSERT(ctx);
25323 NK_ASSERT(ctx->current);
25324 NK_ASSERT(ctx->current->layout);
25325 if (!ctx || !ctx->current || !ctx->current->layout)
25326 return 0;
25327
25328 /* make sure that no nonblocking popup is currently active */
25329 win = ctx->current;
25330 in = &ctx->input;
25331 if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
25332 return 0;
25333
25334 w = nk_iceilf(width);
25335 h = nk_iceilf(nk_null_rect.h);
25336 x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
25337 y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
25338
25339 bounds.x = (float)x;
25340 bounds.y = (float)y;
25341 bounds.w = (float)w;
25342 bounds.h = (float)h;
25343
25344 ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
25345 "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
25346 if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
25347 win->popup.type = NK_PANEL_TOOLTIP;
25348 ctx->current->layout->type = NK_PANEL_TOOLTIP;
25349 return ret;
25350 }
25351
25352 NK_API void
25353 nk_tooltip_end(struct nk_context *ctx)
25354 {
25355 NK_ASSERT(ctx);
25356 NK_ASSERT(ctx->current);
25357 if (!ctx || !ctx->current) return;
25358 ctx->current->seq--;
25359 nk_popup_close(ctx);
25360 nk_popup_end(ctx);
25361 }
25362 NK_API void
25363 nk_tooltip(struct nk_context *ctx, const char *text)
25364 {
25365 const struct nk_style *style;
25366 struct nk_vec2 padding;
25367
25368 int text_len;
25369 float text_width;
25370 float text_height;
25371
25372 NK_ASSERT(ctx);
25373 NK_ASSERT(ctx->current);
25374 NK_ASSERT(ctx->current->layout);
25375 NK_ASSERT(text);
25376 if (!ctx || !ctx->current || !ctx->current->layout || !text)
25377 return;
25378
25379 /* fetch configuration data */
25380 style = &ctx->style;
25381 padding = style->window.padding;
25382
25383 /* calculate size of the text and tooltip */
25384 text_len = nk_strlen(text);
25385 text_width = style->font->width(style->font->userdata,
25386 style->font->height, text, text_len);
25387 text_width += (4 * padding.x);
25388 text_height = (style->font->height + 2 * padding.y);
25389
25390 /* execute tooltip and fill with text */
25391 if (nk_tooltip_begin(ctx, (float)text_width)) {
25392 nk_layout_row_dynamic(ctx, (float)text_height, 1);
25393 nk_text(ctx, text, text_len, NK_TEXT_LEFT);
25394 nk_tooltip_end(ctx);
25395 }
25396 }
25397 #ifdef NK_INCLUDE_STANDARD_VARARGS
25398 NK_API void
25399 nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
25400 {
25401 va_list args;
25402 va_start(args, fmt);
25403 nk_tooltipfv(ctx, fmt, args);
25404 va_end(args);
25405 }
25406 NK_API void
25407 nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
25408 {
25409 char buf[256];
25410 nk_strfmt(buf, NK_LEN(buf), fmt, args);
25411 nk_tooltip(ctx, buf);
25412 }
25413 #endif
25414
25415
25416
25417 #endif /* NK_IMPLEMENTATION */
25418
25419 /*
25420 /// ## License
25421 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
25422 /// ------------------------------------------------------------------------------
25423 /// This software is available under 2 licenses -- choose whichever you prefer.
25424 /// ------------------------------------------------------------------------------
25425 /// ALTERNATIVE A - MIT License
25426 /// Copyright (c) 2016-2018 Micha Mettke
25427 /// Permission is hereby granted, free of charge, to any person obtaining a copy of
25428 /// this software and associated documentation files (the "Software"), to deal in
25429 /// the Software without restriction, including without limitation the rights to
25430 /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
25431 /// of the Software, and to permit persons to whom the Software is furnished to do
25432 /// so, subject to the following conditions:
25433 /// The above copyright notice and this permission notice shall be included in all
25434 /// copies or substantial portions of the Software.
25435 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25436 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25437 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25438 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25439 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25440 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25441 /// SOFTWARE.
25442 /// ------------------------------------------------------------------------------
25443 /// ALTERNATIVE B - Public Domain (www.unlicense.org)
25444 /// This is free and unencumbered software released into the public domain.
25445 /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
25446 /// software, either in source code form or as a compiled binary, for any purpose,
25447 /// commercial or non-commercial, and by any means.
25448 /// In jurisdictions that recognize copyright laws, the author or authors of this
25449 /// software dedicate any and all copyright interest in the software to the public
25450 /// domain. We make this dedication for the benefit of the public at large and to
25451 /// the detriment of our heirs and successors. We intend this dedication to be an
25452 /// overt act of relinquishment in perpetuity of all present and future rights to
25453 /// this software under copyright law.
25454 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25455 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25456 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25457 /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25458 /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25459 /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25460 /// ------------------------------------------------------------------------------
25461 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25462
25463 /// ## Changelog
25464 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
25465 /// [date][x.yy.zz]-[description]
25466 /// -[date]: date on which the change has been pushed
25467 /// -[x.yy.zz]: Numerical version string representation. Each version number on the right
25468 /// resets back to zero if version on the left is incremented.
25469 /// - [x]: Major version with API and library breaking changes
25470 /// - [yy]: Minor version with non-breaking API and library changes
25471 /// - [zz]: Bug fix version with no direct changes to API
25472 ///
25473 /// - 2019/12/10 (4.01.5) - Fix off-by-one error in NK_INTERSECT
25474 /// - 2019/10/09 (4.01.4) - Fix bug for autoscrolling in nk_do_edit
25475 /// - 2019/09/20 (4.01.3) - Fixed a bug wherein combobox cannot be closed by clicking the header
25476 /// when NK_BUTTON_TRIGGER_ON_RELEASE is defined.
25477 /// - 2019/09/10 (4.01.2) - Fixed the nk_cos function, which deviated significantly.
25478 /// - 2019/09/08 (4.01.1) - Fixed a bug wherein re-baking of fonts caused a segmentation
25479 /// fault due to dst_font->glyph_count not being zeroed on subsequent
25480 /// bakes of the same set of fonts.
25481 /// - 2019/06/23 (4.01.0) - Added nk_***_get_scroll and nk_***_set_scroll for groups, windows, and popups.
25482 /// - 2019/06/12 (4.00.3) - Fix panel background drawing bug.
25483 /// - 2018/10/31 (4.00.2) - Added NK_KEYSTATE_BASED_INPUT to "fix" state based backends
25484 /// like GLFW without breaking key repeat behavior on event based.
25485 /// - 2018/04/01 (4.00.1) - Fixed calling `nk_convert` multiple time per single frame.
25486 /// - 2018/04/01 (4.00.0) - BREAKING CHANGE: nk_draw_list_clear no longer tries to
25487 /// clear provided buffers. So make sure to either free
25488 /// or clear each passed buffer after calling nk_convert.
25489 /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior.
25490 /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process.
25491 /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype.
25492 /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug.
25493 /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separed group identifier and title.
25494 /// - 2018/01/07 (3.00.1) - Started to change documentation style.
25495 /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
25496 /// because of conversions between float and byte color representation.
25497 /// Color pickers now use floating point values to represent
25498 /// HSV values. To get back the old behavior I added some additional
25499 /// color conversion functions to cast between nk_color and
25500 /// nk_colorf.
25501 /// - 2017/12/23 (2.00.7) - Fixed small warning.
25502 /// - 2017/12/23 (2.00.7) - Fixed `nk_edit_buffer` behavior if activated to allow input.
25503 /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior.
25504 /// - 2017/12/04 (2.00.6) - Added formated string tooltip widget.
25505 /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag `NK_WINDOW_NO_INPUT`.
25506 /// - 2017/11/15 (2.00.4) - Fixed font merging.
25507 /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions.
25508 /// - 2017/09/14 (2.00.2) - Fixed `nk_edit_buffer` and `nk_edit_focus` behavior.
25509 /// - 2017/09/14 (2.00.1) - Fixed window closing behavior.
25510 /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifing window position and size funtions now
25511 /// require the name of the window and must happen outside the window
25512 /// building process (between function call nk_begin and nk_end).
25513 /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last.
25514 /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows.
25515 /// - 2017/08/27 (1.40.7) - Fixed window background flag.
25516 /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
25517 /// query for widgets.
25518 /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
25519 /// and filled rectangles.
25520 /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
25521 /// process of being destroyed.
25522 /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
25523 /// window instead of directly in table.
25524 /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro.
25525 /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero.
25526 /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
25527 /// comes in effect if you pass in zero was row height argument.
25528 /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
25529 /// how layouting works. From now there will be an internal minimum
25530 /// row height derived from font height. If you need a row smaller than
25531 /// that you can directly set it by `nk_layout_set_min_row_height` and
25532 /// reset the value back by calling `nk_layout_reset_min_row_height.
25533 /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix.
25534 /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a `nk_layout_xxx` function.
25535 /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer.
25536 /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped.
25537 /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundries.
25538 /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space.
25539 /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size.
25540 /// - 2017/05/06 (1.38.0) - Added platform double-click support.
25541 /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends.
25542 /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipboard support.
25543 /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing.
25544 /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error.
25545 /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags.
25546 /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption.
25547 /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows.
25548 /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior.
25549 /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377.
25550 /// - 2017/03/18 (1.34.3) - Fixed long window header titles.
25551 /// - 2017/03/04 (1.34.2) - Fixed text edit filtering.
25552 /// - 2017/03/04 (1.34.1) - Fixed group closable flag.
25553 /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support.
25554 /// - 2017/01/24 (1.33.0) - Added programatic way of remove edit focus.
25555 /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows.
25556 /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows.
25557 /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing.
25558 /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner.
25559 /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
25560 /// dynamic and static widgets.
25561 /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
25562 /// - 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
25563 /// - 2016/12/03 (1.29.1) - Fixed wrapped text with no seperator and C89 error.
25564 /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
25565 /// - 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
25566 /// - 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
25567 /// - 2016/11/19 (1.28.4) - Fixed tooltip flickering.
25568 /// - 2016/11/19 (1.28.3) - Fixed memory leak caused by popup repeated closing.
25569 /// - 2016/11/18 (1.28.2) - Fixed memory leak caused by popup panel allocation.
25570 /// - 2016/11/10 (1.28.1) - Fixed some warnings and C++ error.
25571 /// - 2016/11/10 (1.28.0) - Added additional `nk_button` versions which allows to directly
25572 /// pass in a style struct to change buttons visual.
25573 /// - 2016/11/10 (1.27.0) - Added additional `nk_tree` versions to support external state
25574 /// storage. Just like last the `nk_group` commit the main
25575 /// advantage is that you optionally can minimize nuklears runtime
25576 /// memory consumption or handle hash collisions.
25577 /// - 2016/11/09 (1.26.0) - Added additional `nk_group` version to support external scrollbar
25578 /// offset storage. Main advantage is that you can externalize
25579 /// the memory management for the offset. It could also be helpful
25580 /// if you have a hash collision in `nk_group_begin` but really
25581 /// want the name. In addition I added `nk_list_view` which allows
25582 /// to draw big lists inside a group without actually having to
25583 /// commit the whole list to nuklear (issue #269).
25584 /// - 2016/10/30 (1.25.1) - Fixed clipping rectangle bug inside `nk_draw_list`.
25585 /// - 2016/10/29 (1.25.0) - Pulled `nk_panel` memory management into nuklear and out of
25586 /// the hands of the user. From now on users don't have to care
25587 /// about panels unless they care about some information. If you
25588 /// still need the panel just call `nk_window_get_panel`.
25589 /// - 2016/10/21 (1.24.0) - Changed widget border drawing to stroked rectangle from filled
25590 /// rectangle for less overdraw and widget background transparency.
25591 /// - 2016/10/18 (1.23.0) - Added `nk_edit_focus` for manually edit widget focus control.
25592 /// - 2016/09/29 (1.22.7) - Fixed deduction of basic type in non `<stdint.h>` compilation.
25593 /// - 2016/09/29 (1.22.6) - Fixed edit widget UTF-8 text cursor drawing bug.
25594 /// - 2016/09/28 (1.22.5) - Fixed edit widget UTF-8 text appending/inserting/removing.
25595 /// - 2016/09/28 (1.22.4) - Fixed drawing bug inside edit widgets which offset all text
25596 /// text in every edit widget if one of them is scrolled.
25597 /// - 2016/09/28 (1.22.3) - Fixed small bug in edit widgets if not active. The wrong
25598 /// text length is passed. It should have been in bytes but
25599 /// was passed as glyphes.
25600 /// - 2016/09/20 (1.22.2) - Fixed color button size calculation.
25601 /// - 2016/09/20 (1.22.1) - Fixed some `nk_vsnprintf` behavior bugs and removed `<stdio.h>`
25602 /// again from `NK_INCLUDE_STANDARD_VARARGS`.
25603 /// - 2016/09/18 (1.22.0) - C89 does not support vsnprintf only C99 and newer as well
25604 /// as C++11 and newer. In addition to use vsnprintf you have
25605 /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
25606 /// is not enough. That behavior is now fixed. By default if
25607 /// both varargs as well as stdio is selected I try to use
25608 /// vsnprintf if not possible I will revert to vsprintf. If
25609 /// varargs but not stdio was defined I will use my own function.
25610 /// - 2016/09/15 (1.21.2) - Fixed panel `close` behavior for deeper panel levels.
25611 /// - 2016/09/15 (1.21.1) - Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`.
25612 /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
25613 /// and contextual which prevented closing in y-direction if
25614 /// popup did not reach max height.
25615 /// In addition the height parameter was changed into vec2
25616 /// for width and height to have more control over the popup size.
25617 /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection.
25618 /// - 2016/09/13 (1.20.2) - Fixed slider behavior hopefully for the last time. This time
25619 /// all calculation are correct so no more hackery.
25620 /// - 2016/09/13 (1.20.1) - Internal change to divide window/panel flags into panel flags and types.
25621 /// Suprisinly spend years in C and still happened to confuse types
25622 /// with flags. Probably something to take note.
25623 /// - 2016/09/08 (1.20.0) - Added additional helper function to make it easier to just
25624 /// take the produced buffers from `nk_convert` and unplug the
25625 /// iteration process from `nk_context`. So now you can
25626 /// just use the vertex,element and command buffer + two pointer
25627 /// inside the command buffer retrieved by calls `nk__draw_begin`
25628 /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
25629 /// - 2016/09/08 (1.19.0) - Added additional asserts to make sure every `nk_xxx_begin` call
25630 /// for windows, popups, combobox, menu and contextual is guarded by
25631 /// `if` condition and does not produce false drawing output.
25632 /// - 2016/09/08 (1.18.0) - Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
25633 /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
25634 /// `NK_SYMBOL_RECT_OUTLINE`.
25635 /// - 2016/09/08 (1.17.0) - Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
25636 /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
25637 /// `NK_SYMBOL_CIRCLE_OUTLINE`.
25638 /// - 2016/09/08 (1.16.0) - Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
25639 /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
25640 /// - 2016/09/07 (1.15.3) - Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error.
25641 /// - 2016/09/04 (1.15.2) - Fixed wrong combobox height calculation.
25642 /// - 2016/09/03 (1.15.1) - Fixed gaps inside combo boxes in OpenGL.
25643 /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
25644 /// instead made it user provided. The range of types to convert
25645 /// to is quite limited at the moment, but I would be more than
25646 /// happy to accept PRs to add additional.
25647 /// - 2016/08/30 (1.14.2) - Removed unused variables.
25648 /// - 2016/08/30 (1.14.1) - Fixed C++ build errors.
25649 /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly.
25650 /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables.
25651 /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
25652 /// refrain from using slider with a big number of steps.
25653 /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
25654 /// window was in Read Only Mode.
25655 /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
25656 /// a hack for combo box and menu.
25657 /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
25658 /// it is bugged and causes issues in window selection.
25659 /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
25660 /// determined by the scrollbar size.
25661 /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11.0.
25662 /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection.
25663 /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
25664 /// handling panel padding and panel border.
25665 /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`.
25666 /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups.
25667 /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes.
25668 /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
25669 /// hash collisions. Currently limited to `NK_WINDOW_MAX_NAME`
25670 /// which in term can be redefined if not big enough.
25671 /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code.
25672 /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
25673 /// to account for key press and release happening in one frame.
25674 /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate.
25675 /// - 2016/08/17 (1.09.6) - Removed invalid check for value zero in `nk_propertyx`.
25676 /// - 2016/08/16 (1.09.5) - Fixed ROM mode for deeper levels of popup windows parents.
25677 /// - 2016/08/15 (1.09.4) - Editbox are now still active if enter was pressed with flag
25678 /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
25679 /// typing after commiting.
25680 /// - 2016/08/15 (1.09.4) - Removed redundant code.
25681 /// - 2016/08/15 (1.09.4) - Fixed negative numbers in `nk_strtoi` and remove unused variable.
25682 /// - 2016/08/15 (1.09.3) - Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
25683 /// window only as selected by hovering and not by clicking.
25684 /// - 2016/08/14 (1.09.2) - Fixed a bug in font atlas which caused wrong loading
25685 /// of glyphes for font with multiple ranges.
25686 /// - 2016/08/12 (1.09.1) - Added additional function to check if window is currently
25687 /// hidden and therefore not visible.
25688 /// - 2016/08/12 (1.09.1) - nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
25689 /// instead of the old flag `NK_WINDOW_HIDDEN`.
25690 /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
25691 /// the underlying implementation to not cast to float and instead
25692 /// work directly on the given values.
25693 /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
25694 /// floating pointer number to string conversion for additional
25695 /// precision.
25696 /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
25697 /// string to floating point number conversion for additional
25698 /// precision.
25699 /// - 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
25700 /// - 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
25701 /// to wrong wiget width calculation which results in widgets falsly
25702 /// becomming tagged as not inside window and cannot be accessed.
25703 /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
25704 /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
25705 /// by using `nk_window_show` and closed by either clicking the close
25706 /// icon in a window or by calling `nk_window_close`. Only closed
25707 /// windows get removed at the end of the frame while hidden windows
25708 /// remain.
25709 /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
25710 /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
25711 /// - 2016/08/08 (1.05.4) - Fixed scrollbar auto hiding behavior.
25712 /// - 2016/08/08 (1.05.3) - Fixed wrong panel padding selection in `nk_layout_widget_space`.
25713 /// - 2016/08/07 (1.05.2) - Fixed old bug in dynamic immediate mode layout API, calculating
25714 /// wrong item spacing and panel width.
25715 /// - 2016/08/07 (1.05.1) - Hopefully finally fixed combobox popup drawing bug.
25716 /// - 2016/08/07 (1.05.0) - Split varargs away from `NK_INCLUDE_STANDARD_IO` into own
25717 /// define `NK_INCLUDE_STANDARD_VARARGS` to allow more fine
25718 /// grained controlled over library includes.
25719 /// - 2016/08/06 (1.04.5) - Changed memset calls to `NK_MEMSET`.
25720 /// - 2016/08/04 (1.04.4) - Fixed fast window scaling behavior.
25721 /// - 2016/08/04 (1.04.3) - Fixed window scaling, movement bug which appears if you
25722 /// move/scale a window and another window is behind it.
25723 /// If you are fast enough then the window behind gets activated
25724 /// and the operation is blocked. I now require activating
25725 /// by hovering only if mouse is not pressed.
25726 /// - 2016/08/04 (1.04.2) - Fixed changing fonts.
25727 /// - 2016/08/03 (1.04.1) - Fixed `NK_WINDOW_BACKGROUND` behavior.
25728 /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`.
25729 /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
25730 /// sub windows (combo, menu, ...).
25731 /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor.
25732 /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
25733 /// to be always in the background of the screen.
25734 /// - 2016/08/03 (1.03.2) - Removed invalid assert macro for NK_RGB color picker.
25735 /// - 2016/08/01 (1.03.1) - Added helper macros into header include guard.
25736 /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
25737 /// simplify memory management by removing the need to
25738 /// allocate the pool.
25739 /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
25740 /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
25741 /// seconds without window interaction. To make it work
25742 /// you have to also set a delta time inside the `nk_context`.
25743 /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs.
25744 /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`.
25745 /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument.
25746 /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
25747 /// font atlas memory management by converting pointer
25748 /// arrays for fonts and font configurations to lists.
25749 /// - 2016/07/15 (1.00.0) - Changed button API to use context dependend button
25750 /// behavior instead of passing it for every function call.
25751 /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25752 /// ## Gallery
25753 /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
25754 /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
25755 /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
25756 /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
25757 /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
25758 /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
25759 /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
25760 /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
25761 /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
25762 ///
25763 /// ## Credits
25764 /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
25765 ///
25766 /// Embeds [stb_texedit](https://github.com/nothings/stb/blob/master/stb_textedit.h), [stb_truetype](https://github.com/nothings/stb/blob/master/stb_truetype.h) and [stb_rectpack](https://github.com/nothings/stb/blob/master/stb_rect_pack.h) by Sean Barret (public domain) <br />
25767 /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from [email protected] for documentation generation <br /><br />
25768 /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
25769 ///
25770 /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
25771 /// giving me the inspiration for this library, Casey Muratori for handmade hero
25772 /// and his original immediate mode graphical user interface idea and Sean
25773 /// Barret for his amazing single header libraries which restored my faith
25774 /// in libraries and brought me to create some of my own. Finally Apoorva Joshi
25775 /// for his single header file packer.
25776 */
25777