🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25417 lines
930 KiB

  1. /*/// # Nuklear
  2. /// ![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
  3. ///
  4. /// ## Contents
  5. /// 1. About section
  6. /// 2. Highlights section
  7. /// 3. Features section
  8. /// 4. Usage section
  9. /// 1. Flags section
  10. /// 2. Constants section
  11. /// 3. Dependencies section
  12. /// 5. Example section
  13. /// 6. API section
  14. /// 1. Context section
  15. /// 2. Input section
  16. /// 3. Drawing section
  17. /// 4. Window section
  18. /// 5. Layouting section
  19. /// 6. Groups section
  20. /// 7. Tree section
  21. /// 8. Properties section
  22. /// 7. License section
  23. /// 8. Changelog section
  24. /// 9. Gallery section
  25. /// 10. Credits section
  26. ///
  27. /// ## About
  28. /// This is a minimal state immediate mode graphical user interface toolkit
  29. /// written in ANSI C and licensed under public domain. It was designed as a simple
  30. /// embeddable user interface for application and does not have any dependencies,
  31. /// a default renderbackend or OS window and input handling but instead provides a very modular
  32. /// library approach by using simple input state for input and draw
  33. /// commands describing primitive shapes as output. So instead of providing a
  34. /// layered library that tries to abstract over a number of platform and
  35. /// render backends it only focuses on the actual UI.
  36. ///
  37. /// ## Highlights
  38. /// - Graphical user interface toolkit
  39. /// - Single header library
  40. /// - Written in C89 (a.k.a. ANSI C or ISO C90)
  41. /// - Small codebase (~18kLOC)
  42. /// - Focus on portability, efficiency and simplicity
  43. /// - No dependencies (not even the standard library if not wanted)
  44. /// - Fully skinnable and customizable
  45. /// - Low memory footprint with total memory control if needed or wanted
  46. /// - UTF-8 support
  47. /// - No global or hidden state
  48. /// - Customizable library modules (you can compile and use only what you need)
  49. /// - Optional font baker and vertex buffer output
  50. ///
  51. /// ## Features
  52. /// - Absolutely no platform dependent code
  53. /// - Memory management control ranging from/to
  54. /// - Ease of use by allocating everything from standard library
  55. /// - Control every byte of memory inside the library
  56. /// - Font handling control ranging from/to
  57. /// - Use your own font implementation for everything
  58. /// - Use this libraries internal font baking and handling API
  59. /// - Drawing output control ranging from/to
  60. /// - Simple shapes for more high level APIs which already have drawing capabilities
  61. /// - Hardware accessible anti-aliased vertex buffer output
  62. /// - Customizable colors and properties ranging from/to
  63. /// - Simple changes to color by filling a simple color table
  64. /// - Complete control with ability to use skinning to decorate widgets
  65. /// - Bendable UI library with widget ranging from/to
  66. /// - Basic widgets like buttons, checkboxes, slider, ...
  67. /// - Advanced widget like abstract comboboxes, contextual menus,...
  68. /// - Compile time configuration to only compile what you need
  69. /// - Subset which can be used if you do not want to link or use the standard library
  70. /// - Can be easily modified to only update on user input instead of frame updates
  71. ///
  72. /// ## Usage
  73. /// This library is self contained in one single header file and can be used either
  74. /// in header only mode or in implementation mode. The header only mode is used
  75. /// by default when included and allows including this header in other headers
  76. /// and does not contain the actual implementation. <br /><br />
  77. ///
  78. /// The implementation mode requires to define the preprocessor macro
  79. /// NK_IMPLEMENTATION in *one* .c/.cpp file before #includeing this file, e.g.:
  80. ///
  81. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~C
  82. /// #define NK_IMPLEMENTATION
  83. /// #include "nuklear.h"
  84. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85. ///
  86. /// Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  87. /// below in header and implementation mode if you want to use additional functionality
  88. /// or need more control over the library.
  89. ///
  90. /// !!! WARNING
  91. /// 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.
  92. ///
  93. /// ### Flags
  94. /// Flag | Description
  95. /// --------------------------------|------------------------------------------
  96. /// NK_PRIVATE | If defined declares all functions as static, so they can only be accessed inside the file that contains the implementation
  97. /// 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.
  98. /// 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.
  99. /// NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
  100. /// NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdio.h> and provide additional functions depending on file loading.
  101. /// 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,...
  102. /// 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.
  103. /// 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
  104. /// 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.
  105. /// 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.
  106. /// 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.
  107. ///
  108. /// !!! WARNING
  109. /// The following flags will pull in the standard C library:
  110. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  111. /// - NK_INCLUDE_STANDARD_IO
  112. /// - NK_INCLUDE_STANDARD_VARARGS
  113. ///
  114. /// !!! WARNING
  115. /// The following flags if defined need to be defined for both header and implementation:
  116. /// - NK_INCLUDE_FIXED_TYPES
  117. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  118. /// - NK_INCLUDE_STANDARD_VARARGS
  119. /// - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  120. /// - NK_INCLUDE_FONT_BAKING
  121. /// - NK_INCLUDE_DEFAULT_FONT
  122. /// - NK_INCLUDE_STANDARD_VARARGS
  123. /// - NK_INCLUDE_COMMAND_USERDATA
  124. ///
  125. /// ### Constants
  126. /// Define | Description
  127. /// --------------------------------|---------------------------------------
  128. /// 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.
  129. /// NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
  130. /// 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.
  131. ///
  132. /// !!! WARNING
  133. /// The following constants if defined need to be defined for both header and implementation:
  134. /// - NK_MAX_NUMBER_BUFFER
  135. /// - NK_BUFFER_DEFAULT_INITIAL_SIZE
  136. /// - NK_INPUT_MAX
  137. ///
  138. /// ### Dependencies
  139. /// Function | Description
  140. /// ------------|---------------------------------------------------------------
  141. /// NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
  142. /// NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
  143. /// NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
  144. /// 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.
  145. /// NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
  146. /// NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
  147. /// 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!).
  148. /// 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!).
  149. /// 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.
  150. ///
  151. /// !!! WARNING
  152. /// The following dependencies will pull in the standard C library if not redefined:
  153. /// - NK_ASSERT
  154. ///
  155. /// !!! WARNING
  156. /// The following dependencies if defined need to be defined for both header and implementation:
  157. /// - NK_ASSERT
  158. ///
  159. /// !!! WARNING
  160. /// The following dependencies if defined need to be defined only for the implementation part:
  161. /// - NK_MEMSET
  162. /// - NK_MEMCPY
  163. /// - NK_SQRT
  164. /// - NK_SIN
  165. /// - NK_COS
  166. /// - NK_STRTOD
  167. /// - NK_DTOA
  168. /// - NK_VSNPRINTF
  169. ///
  170. /// ## Example
  171. ///
  172. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  173. /// // init gui state
  174. /// enum {EASY, HARD};
  175. /// static int op = EASY;
  176. /// static float value = 0.6f;
  177. /// static int i = 20;
  178. /// struct nk_context ctx;
  179. ///
  180. /// nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
  181. /// if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
  182. /// NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
  183. /// // fixed widget pixel width
  184. /// nk_layout_row_static(&ctx, 30, 80, 1);
  185. /// if (nk_button_label(&ctx, "button")) {
  186. /// // event handling
  187. /// }
  188. ///
  189. /// // fixed widget window ratio width
  190. /// nk_layout_row_dynamic(&ctx, 30, 2);
  191. /// if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
  192. /// if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
  193. ///
  194. /// // custom widget pixel width
  195. /// nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
  196. /// {
  197. /// nk_layout_row_push(&ctx, 50);
  198. /// nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
  199. /// nk_layout_row_push(&ctx, 110);
  200. /// nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
  201. /// }
  202. /// nk_layout_row_end(&ctx);
  203. /// }
  204. /// nk_end(&ctx);
  205. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206. ///
  207. /// ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
  208. ///
  209. /// ## API
  210. ///
  211. */
  212. #ifndef NK_NUKLEAR_H_
  213. #define NK_NUKLEAR_H_
  214. #ifdef __cplusplus
  215. extern "C" {
  216. #endif
  217. /*
  218. * ==============================================================
  219. *
  220. * CONSTANTS
  221. *
  222. * ===============================================================
  223. */
  224. #define NK_UNDEFINED (-1.0f)
  225. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  226. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  227. #ifndef NK_INPUT_MAX
  228. #define NK_INPUT_MAX 16
  229. #endif
  230. #ifndef NK_MAX_NUMBER_BUFFER
  231. #define NK_MAX_NUMBER_BUFFER 64
  232. #endif
  233. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  234. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  235. #endif
  236. /*
  237. * ==============================================================
  238. *
  239. * HELPER
  240. *
  241. * ===============================================================
  242. */
  243. #ifndef NK_API
  244. #ifdef NK_PRIVATE
  245. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  246. #define NK_API static inline
  247. #elif defined(__cplusplus)
  248. #define NK_API static inline
  249. #else
  250. #define NK_API static
  251. #endif
  252. #else
  253. #define NK_API extern
  254. #endif
  255. #endif
  256. #define NK_INTERN static
  257. #define NK_STORAGE static
  258. #define NK_GLOBAL static
  259. #define NK_FLAG(x) (1 << (x))
  260. #define NK_STRINGIFY(x) #x
  261. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  262. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  263. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  264. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  265. #ifdef _MSC_VER
  266. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  267. #else
  268. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  269. #endif
  270. #ifndef NK_STATIC_ASSERT
  271. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  272. #endif
  273. #ifndef NK_FILE_LINE
  274. #ifdef _MSC_VER
  275. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  276. #else
  277. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  278. #endif
  279. #endif
  280. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  281. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  282. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  283. #ifdef NK_INCLUDE_STANDARD_VARARGS
  284. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  285. #include <sal.h>
  286. #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
  287. #else
  288. #define NK_PRINTF_FORMAT_STRING
  289. #endif
  290. #if defined(__GNUC__)
  291. #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
  292. #else
  293. #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
  294. #endif
  295. #endif
  296. /*
  297. * ===============================================================
  298. *
  299. * BASIC
  300. *
  301. * ===============================================================
  302. */
  303. #ifdef NK_INCLUDE_FIXED_TYPES
  304. #include <stdint.h>
  305. #define NK_INT8 int8_t
  306. #define NK_UINT8 uint8_t
  307. #define NK_INT16 int16_t
  308. #define NK_UINT16 uint16_t
  309. #define NK_INT32 int32_t
  310. #define NK_UINT32 uint32_t
  311. #define NK_SIZE_TYPE uintptr_t
  312. #define NK_POINTER_TYPE uintptr_t
  313. #else
  314. #ifndef NK_INT8
  315. #define NK_INT8 char
  316. #endif
  317. #ifndef NK_UINT8
  318. #define NK_UINT8 unsigned char
  319. #endif
  320. #ifndef NK_INT16
  321. #define NK_INT16 signed short
  322. #endif
  323. #ifndef NK_UINT16
  324. #define NK_UINT16 unsigned short
  325. #endif
  326. #ifndef NK_INT32
  327. #if defined(_MSC_VER)
  328. #define NK_INT32 __int32
  329. #else
  330. #define NK_INT32 signed int
  331. #endif
  332. #endif
  333. #ifndef NK_UINT32
  334. #if defined(_MSC_VER)
  335. #define NK_UINT32 unsigned __int32
  336. #else
  337. #define NK_UINT32 unsigned int
  338. #endif
  339. #endif
  340. #ifndef NK_SIZE_TYPE
  341. #if defined(_WIN64) && defined(_MSC_VER)
  342. #define NK_SIZE_TYPE unsigned __int64
  343. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  344. #define NK_SIZE_TYPE unsigned __int32
  345. #elif defined(__GNUC__) || defined(__clang__)
  346. #if defined(__x86_64__) || defined(__ppc64__)
  347. #define NK_SIZE_TYPE unsigned long
  348. #else
  349. #define NK_SIZE_TYPE unsigned int
  350. #endif
  351. #else
  352. #define NK_SIZE_TYPE unsigned long
  353. #endif
  354. #endif
  355. #ifndef NK_POINTER_TYPE
  356. #if defined(_WIN64) && defined(_MSC_VER)
  357. #define NK_POINTER_TYPE unsigned __int64
  358. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  359. #define NK_POINTER_TYPE unsigned __int32
  360. #elif defined(__GNUC__) || defined(__clang__)
  361. #if defined(__x86_64__) || defined(__ppc64__)
  362. #define NK_POINTER_TYPE unsigned long
  363. #else
  364. #define NK_POINTER_TYPE unsigned int
  365. #endif
  366. #else
  367. #define NK_POINTER_TYPE unsigned long
  368. #endif
  369. #endif
  370. #endif
  371. typedef NK_INT8 nk_char;
  372. typedef NK_UINT8 nk_uchar;
  373. typedef NK_UINT8 nk_byte;
  374. typedef NK_INT16 nk_short;
  375. typedef NK_UINT16 nk_ushort;
  376. typedef NK_INT32 nk_int;
  377. typedef NK_UINT32 nk_uint;
  378. typedef NK_SIZE_TYPE nk_size;
  379. typedef NK_POINTER_TYPE nk_ptr;
  380. typedef nk_uint nk_hash;
  381. typedef nk_uint nk_flags;
  382. typedef nk_uint nk_rune;
  383. /* Make sure correct type size:
  384. * This will fire with a negative subscript error if the type sizes
  385. * are set incorrectly by the compiler, and compile out if not */
  386. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  387. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  388. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  389. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  390. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  391. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  392. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  393. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  394. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  395. /* ============================================================================
  396. *
  397. * API
  398. *
  399. * =========================================================================== */
  400. struct nk_buffer;
  401. struct nk_allocator;
  402. struct nk_command_buffer;
  403. struct nk_draw_command;
  404. struct nk_convert_config;
  405. struct nk_style_item;
  406. struct nk_text_edit;
  407. struct nk_draw_list;
  408. struct nk_user_font;
  409. struct nk_panel;
  410. struct nk_context;
  411. struct nk_draw_vertex_layout_element;
  412. struct nk_style_button;
  413. struct nk_style_toggle;
  414. struct nk_style_selectable;
  415. struct nk_style_slide;
  416. struct nk_style_progress;
  417. struct nk_style_scrollbar;
  418. struct nk_style_edit;
  419. struct nk_style_property;
  420. struct nk_style_chart;
  421. struct nk_style_combo;
  422. struct nk_style_tab;
  423. struct nk_style_window_header;
  424. struct nk_style_window;
  425. enum {nk_false, nk_true};
  426. struct nk_color {nk_byte r,g,b,a;};
  427. struct nk_colorf {float r,g,b,a;};
  428. struct nk_vec2 {float x,y;};
  429. struct nk_vec2i {short x, y;};
  430. struct nk_rect {float x,y,w,h;};
  431. struct nk_recti {short x,y,w,h;};
  432. typedef char nk_glyph[NK_UTF_SIZE];
  433. typedef union {void *ptr; int id;} nk_handle;
  434. struct nk_image {nk_handle handle;unsigned short w,h;unsigned short region[4];};
  435. struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
  436. struct nk_scroll {nk_uint x, y;};
  437. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  438. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  439. enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
  440. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  441. enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
  442. enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
  443. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  444. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  445. enum nk_color_format {NK_RGB, NK_RGBA};
  446. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  447. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  448. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  449. typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
  450. typedef void (*nk_plugin_free)(nk_handle, void *old);
  451. typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  452. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  453. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  454. struct nk_allocator {
  455. nk_handle userdata;
  456. nk_plugin_alloc alloc;
  457. nk_plugin_free free;
  458. };
  459. enum nk_symbol_type {
  460. NK_SYMBOL_NONE,
  461. NK_SYMBOL_X,
  462. NK_SYMBOL_UNDERSCORE,
  463. NK_SYMBOL_CIRCLE_SOLID,
  464. NK_SYMBOL_CIRCLE_OUTLINE,
  465. NK_SYMBOL_RECT_SOLID,
  466. NK_SYMBOL_RECT_OUTLINE,
  467. NK_SYMBOL_TRIANGLE_UP,
  468. NK_SYMBOL_TRIANGLE_DOWN,
  469. NK_SYMBOL_TRIANGLE_LEFT,
  470. NK_SYMBOL_TRIANGLE_RIGHT,
  471. NK_SYMBOL_PLUS,
  472. NK_SYMBOL_MINUS,
  473. NK_SYMBOL_MAX
  474. };
  475. /* =============================================================================
  476. *
  477. * CONTEXT
  478. *
  479. * =============================================================================*/
  480. /*/// ### Context
  481. /// Contexts are the main entry point and the majestro of nuklear and contain all required state.
  482. /// They are used for window, memory, input, style, stack, commands and time management and need
  483. /// to be passed into all nuklear GUI specific functions.
  484. ///
  485. /// #### Usage
  486. /// To use a context it first has to be initialized which can be achieved by calling
  487. /// one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  488. /// Each takes in a font handle and a specific way of handling memory. Memory control
  489. /// hereby ranges from standard library to just specifying a fixed sized block of memory
  490. /// which nuklear has to manage itself from.
  491. ///
  492. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  493. /// struct nk_context ctx;
  494. /// nk_init_xxx(&ctx, ...);
  495. /// while (1) {
  496. /// // [...]
  497. /// nk_clear(&ctx);
  498. /// }
  499. /// nk_free(&ctx);
  500. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  501. ///
  502. /// #### Reference
  503. /// Function | Description
  504. /// --------------------|-------------------------------------------------------
  505. /// __nk_init_default__ | Initializes context with standard library memory allocation (malloc,free)
  506. /// __nk_init_fixed__ | Initializes context from single fixed size memory block
  507. /// __nk_init__ | Initializes context with memory allocator callbacks for alloc and free
  508. /// __nk_init_custom__ | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  509. /// __nk_clear__ | Called at the end of the frame to reset and prepare the context for the next frame
  510. /// __nk_free__ | Shutdown and free all memory allocated inside the context
  511. /// __nk_set_user_data__| Utility function to pass user data to draw command
  512. */
  513. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  514. /*/// #### nk_init_default
  515. /// Initializes a `nk_context` struct with a default standard library allocator.
  516. /// Should be used if you don't want to be bothered with memory management in nuklear.
  517. ///
  518. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  519. /// int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
  520. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  521. ///
  522. /// Parameter | Description
  523. /// ------------|---------------------------------------------------------------
  524. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  525. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  526. ///
  527. /// Returns either `false(0)` on failure or `true(1)` on success.
  528. ///
  529. */
  530. NK_API int nk_init_default(struct nk_context*, const struct nk_user_font*);
  531. #endif
  532. /*/// #### nk_init_fixed
  533. /// Initializes a `nk_context` struct from single fixed size memory block
  534. /// Should be used if you want complete control over nuklear's memory management.
  535. /// Especially recommended for system with little memory or systems with virtual memory.
  536. /// For the later case you can just allocate for example 16MB of virtual memory
  537. /// and only the required amount of memory will actually be committed.
  538. ///
  539. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  540. /// int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
  541. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  542. ///
  543. /// !!! Warning
  544. /// make sure the passed memory block is aligned correctly for `nk_draw_commands`.
  545. ///
  546. /// Parameter | Description
  547. /// ------------|--------------------------------------------------------------
  548. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  549. /// __memory__ | Must point to a previously allocated memory block
  550. /// __size__ | Must contain the total size of __memory__
  551. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  552. ///
  553. /// Returns either `false(0)` on failure or `true(1)` on success.
  554. */
  555. NK_API int nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  556. /*/// #### nk_init
  557. /// Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  558. /// memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  559. /// interface to nuklear. Can be useful for cases like monitoring memory consumption.
  560. ///
  561. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  562. /// int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
  563. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  564. ///
  565. /// Parameter | Description
  566. /// ------------|---------------------------------------------------------------
  567. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  568. /// __alloc__ | Must point to a previously allocated memory allocator
  569. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  570. ///
  571. /// Returns either `false(0)` on failure or `true(1)` on success.
  572. */
  573. NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  574. /*/// #### nk_init_custom
  575. /// Initializes a `nk_context` struct from two different either fixed or growing
  576. /// buffers. The first buffer is for allocating draw commands while the second buffer is
  577. /// used for allocating windows, panels and state tables.
  578. ///
  579. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  580. /// int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
  581. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  582. ///
  583. /// Parameter | Description
  584. /// ------------|---------------------------------------------------------------
  585. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  586. /// __cmds__ | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  587. /// __pool__ | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  588. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  589. ///
  590. /// Returns either `false(0)` on failure or `true(1)` on success.
  591. */
  592. NK_API int nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  593. /*/// #### nk_clear
  594. /// Resets the context state at the end of the frame. This includes mostly
  595. /// garbage collector tasks like removing windows or table not called and therefore
  596. /// used anymore.
  597. ///
  598. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  599. /// void nk_clear(struct nk_context *ctx);
  600. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  601. ///
  602. /// Parameter | Description
  603. /// ------------|-----------------------------------------------------------
  604. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  605. */
  606. NK_API void nk_clear(struct nk_context*);
  607. /*/// #### nk_free
  608. /// Frees all memory allocated by nuklear. Not needed if context was
  609. /// initialized with `nk_init_fixed`.
  610. ///
  611. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  612. /// void nk_free(struct nk_context *ctx);
  613. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  614. ///
  615. /// Parameter | Description
  616. /// ------------|-----------------------------------------------------------
  617. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  618. */
  619. NK_API void nk_free(struct nk_context*);
  620. #ifdef NK_INCLUDE_COMMAND_USERDATA
  621. /*/// #### nk_set_user_data
  622. /// Sets the currently passed userdata passed down into each draw command.
  623. ///
  624. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  625. /// void nk_set_user_data(struct nk_context *ctx, nk_handle data);
  626. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  627. ///
  628. /// Parameter | Description
  629. /// ------------|--------------------------------------------------------------
  630. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  631. /// __data__ | Handle with either pointer or index to be passed into every draw commands
  632. */
  633. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  634. #endif
  635. /* =============================================================================
  636. *
  637. * INPUT
  638. *
  639. * =============================================================================*/
  640. /*/// ### Input
  641. /// The input API is responsible for holding the current input state composed of
  642. /// mouse, key and text input states.
  643. /// It is worth noting that no direct os or window handling is done in nuklear.
  644. /// Instead all input state has to be provided by platform specific code. This in one hand
  645. /// expects more work from the user and complicates usage but on the other hand
  646. /// provides simple abstraction over a big number of platforms, libraries and other
  647. /// already provided functionality.
  648. ///
  649. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  650. /// nk_input_begin(&ctx);
  651. /// while (GetEvent(&evt)) {
  652. /// if (evt.type == MOUSE_MOVE)
  653. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  654. /// else if (evt.type == [...]) {
  655. /// // [...]
  656. /// }
  657. /// } nk_input_end(&ctx);
  658. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  659. ///
  660. /// #### Usage
  661. /// Input state needs to be provided to nuklear by first calling `nk_input_begin`
  662. /// which resets internal state like delta mouse position and button transistions.
  663. /// After `nk_input_begin` all current input state needs to be provided. This includes
  664. /// mouse motion, button and key pressed and released, text input and scrolling.
  665. /// Both event- or state-based input handling are supported by this API
  666. /// and should work without problems. Finally after all input state has been
  667. /// mirrored `nk_input_end` needs to be called to finish input process.
  668. ///
  669. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  670. /// struct nk_context ctx;
  671. /// nk_init_xxx(&ctx, ...);
  672. /// while (1) {
  673. /// Event evt;
  674. /// nk_input_begin(&ctx);
  675. /// while (GetEvent(&evt)) {
  676. /// if (evt.type == MOUSE_MOVE)
  677. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  678. /// else if (evt.type == [...]) {
  679. /// // [...]
  680. /// }
  681. /// }
  682. /// nk_input_end(&ctx);
  683. /// // [...]
  684. /// nk_clear(&ctx);
  685. /// } nk_free(&ctx);
  686. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  687. ///
  688. /// #### Reference
  689. /// Function | Description
  690. /// --------------------|-------------------------------------------------------
  691. /// __nk_input_begin__ | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  692. /// __nk_input_motion__ | Mirrors mouse cursor position
  693. /// __nk_input_key__ | Mirrors key state with either pressed or released
  694. /// __nk_input_button__ | Mirrors mouse button state with either pressed or released
  695. /// __nk_input_scroll__ | Mirrors mouse scroll values
  696. /// __nk_input_char__ | Adds a single ASCII text character into an internal text buffer
  697. /// __nk_input_glyph__ | Adds a single multi-byte UTF-8 character into an internal text buffer
  698. /// __nk_input_unicode__| Adds a single unicode rune into an internal text buffer
  699. /// __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
  700. */
  701. enum nk_keys {
  702. NK_KEY_NONE,
  703. NK_KEY_SHIFT,
  704. NK_KEY_CTRL,
  705. NK_KEY_DEL,
  706. NK_KEY_ENTER,
  707. NK_KEY_TAB,
  708. NK_KEY_BACKSPACE,
  709. NK_KEY_COPY,
  710. NK_KEY_CUT,
  711. NK_KEY_PASTE,
  712. NK_KEY_UP,
  713. NK_KEY_DOWN,
  714. NK_KEY_LEFT,
  715. NK_KEY_RIGHT,
  716. /* Shortcuts: text field */
  717. NK_KEY_TEXT_INSERT_MODE,
  718. NK_KEY_TEXT_REPLACE_MODE,
  719. NK_KEY_TEXT_RESET_MODE,
  720. NK_KEY_TEXT_LINE_START,
  721. NK_KEY_TEXT_LINE_END,
  722. NK_KEY_TEXT_START,
  723. NK_KEY_TEXT_END,
  724. NK_KEY_TEXT_UNDO,
  725. NK_KEY_TEXT_REDO,
  726. NK_KEY_TEXT_SELECT_ALL,
  727. NK_KEY_TEXT_WORD_LEFT,
  728. NK_KEY_TEXT_WORD_RIGHT,
  729. /* Shortcuts: scrollbar */
  730. NK_KEY_SCROLL_START,
  731. NK_KEY_SCROLL_END,
  732. NK_KEY_SCROLL_DOWN,
  733. NK_KEY_SCROLL_UP,
  734. NK_KEY_MAX
  735. };
  736. enum nk_buttons {
  737. NK_BUTTON_LEFT,
  738. NK_BUTTON_MIDDLE,
  739. NK_BUTTON_RIGHT,
  740. NK_BUTTON_DOUBLE,
  741. NK_BUTTON_MAX
  742. };
  743. /*/// #### nk_input_begin
  744. /// Begins the input mirroring process by resetting text, scroll
  745. /// mouse previous mouse position and movement as well as key state transitions,
  746. ///
  747. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  748. /// void nk_input_begin(struct nk_context*);
  749. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  750. ///
  751. /// Parameter | Description
  752. /// ------------|-----------------------------------------------------------
  753. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  754. */
  755. NK_API void nk_input_begin(struct nk_context*);
  756. /*/// #### nk_input_motion
  757. /// Mirrors current mouse position to nuklear
  758. ///
  759. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  760. /// void nk_input_motion(struct nk_context *ctx, int x, int y);
  761. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  762. ///
  763. /// Parameter | Description
  764. /// ------------|-----------------------------------------------------------
  765. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  766. /// __x__ | Must hold an integer describing the current mouse cursor x-position
  767. /// __y__ | Must hold an integer describing the current mouse cursor y-position
  768. */
  769. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  770. /*/// #### nk_input_key
  771. /// Mirrors state of a specific key to nuklear
  772. ///
  773. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  774. /// void nk_input_key(struct nk_context*, enum nk_keys key, int down);
  775. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  776. ///
  777. /// Parameter | Description
  778. /// ------------|-----------------------------------------------------------
  779. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  780. /// __key__ | Must be any value specified in enum `nk_keys` that needs to be mirrored
  781. /// __down__ | Must be 0 for key is up and 1 for key is down
  782. */
  783. NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
  784. /*/// #### nk_input_button
  785. /// Mirrors the state of a specific mouse button to nuklear
  786. ///
  787. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  788. /// void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, int down);
  789. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  790. ///
  791. /// Parameter | Description
  792. /// ------------|-----------------------------------------------------------
  793. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  794. /// __btn__ | Must be any value specified in enum `nk_buttons` that needs to be mirrored
  795. /// __x__ | Must contain an integer describing mouse cursor x-position on click up/down
  796. /// __y__ | Must contain an integer describing mouse cursor y-position on click up/down
  797. /// __down__ | Must be 0 for key is up and 1 for key is down
  798. */
  799. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
  800. /*/// #### nk_input_scroll
  801. /// Copies the last mouse scroll value to nuklear. Is generally
  802. /// a scroll value. So does not have to come from mouse and could also originate
  803. ///
  804. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  805. /// void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
  806. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  807. ///
  808. /// Parameter | Description
  809. /// ------------|-----------------------------------------------------------
  810. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  811. /// __val__ | vector with both X- as well as Y-scroll value
  812. */
  813. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  814. /*/// #### nk_input_char
  815. /// Copies a single ASCII character into an internal text buffer
  816. /// This is basically a helper function to quickly push ASCII characters into
  817. /// nuklear.
  818. ///
  819. /// !!! Note
  820. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  821. ///
  822. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  823. /// void nk_input_char(struct nk_context *ctx, char c);
  824. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  825. ///
  826. /// Parameter | Description
  827. /// ------------|-----------------------------------------------------------
  828. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  829. /// __c__ | Must be a single ASCII character preferable one that can be printed
  830. */
  831. NK_API void nk_input_char(struct nk_context*, char);
  832. /*/// #### nk_input_glyph
  833. /// Converts an encoded unicode rune into UTF-8 and copies the result into an
  834. /// internal text buffer.
  835. ///
  836. /// !!! Note
  837. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  838. ///
  839. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  840. /// void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
  841. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  842. ///
  843. /// Parameter | Description
  844. /// ------------|-----------------------------------------------------------
  845. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  846. /// __g__ | UTF-32 unicode codepoint
  847. */
  848. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  849. /*/// #### nk_input_unicode
  850. /// Converts a unicode rune into UTF-8 and copies the result
  851. /// into an internal text buffer.
  852. /// !!! Note
  853. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  854. ///
  855. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  856. /// void nk_input_unicode(struct nk_context*, nk_rune rune);
  857. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  858. ///
  859. /// Parameter | Description
  860. /// ------------|-----------------------------------------------------------
  861. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  862. /// __rune__ | UTF-32 unicode codepoint
  863. */
  864. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  865. /*/// #### nk_input_end
  866. /// End the input mirroring process by resetting mouse grabbing
  867. /// state to ensure the mouse cursor is not grabbed indefinitely.///
  868. ///
  869. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  870. /// void nk_input_end(struct nk_context *ctx);
  871. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  872. ///
  873. /// Parameter | Description
  874. /// ------------|-----------------------------------------------------------
  875. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  876. */
  877. NK_API void nk_input_end(struct nk_context*);
  878. /* =============================================================================
  879. *
  880. * DRAWING
  881. *
  882. * =============================================================================*/
  883. /*/// ### Drawing
  884. /// This library was designed to be render backend agnostic so it does
  885. /// not draw anything to screen directly. Instead all drawn shapes, widgets
  886. /// are made of, are buffered into memory and make up a command queue.
  887. /// Each frame therefore fills the command buffer with draw commands
  888. /// that then need to be executed by the user and his own render backend.
  889. /// After that the command buffer needs to be cleared and a new frame can be
  890. /// started. It is probably important to note that the command buffer is the main
  891. /// drawing API and the optional vertex buffer API only takes this format and
  892. /// converts it into a hardware accessible format.
  893. ///
  894. /// #### Usage
  895. /// To draw all draw commands accumulated over a frame you need your own render
  896. /// backend able to draw a number of 2D primitives. This includes at least
  897. /// filled and stroked rectangles, circles, text, lines, triangles and scissors.
  898. /// As soon as this criterion is met you can iterate over each draw command
  899. /// and execute each draw command in a interpreter like fashion:
  900. ///
  901. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  902. /// const struct nk_command *cmd = 0;
  903. /// nk_foreach(cmd, &ctx) {
  904. /// switch (cmd->type) {
  905. /// case NK_COMMAND_LINE:
  906. /// your_draw_line_function(...)
  907. /// break;
  908. /// case NK_COMMAND_RECT
  909. /// your_draw_rect_function(...)
  910. /// break;
  911. /// case //...:
  912. /// //[...]
  913. /// }
  914. /// }
  915. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  916. ///
  917. /// In program flow context draw commands need to be executed after input has been
  918. /// gathered and the complete UI with windows and their contained widgets have
  919. /// been executed and before calling `nk_clear` which frees all previously
  920. /// allocated draw commands.
  921. ///
  922. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  923. /// struct nk_context ctx;
  924. /// nk_init_xxx(&ctx, ...);
  925. /// while (1) {
  926. /// Event evt;
  927. /// nk_input_begin(&ctx);
  928. /// while (GetEvent(&evt)) {
  929. /// if (evt.type == MOUSE_MOVE)
  930. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  931. /// else if (evt.type == [...]) {
  932. /// [...]
  933. /// }
  934. /// }
  935. /// nk_input_end(&ctx);
  936. /// //
  937. /// // [...]
  938. /// //
  939. /// const struct nk_command *cmd = 0;
  940. /// nk_foreach(cmd, &ctx) {
  941. /// switch (cmd->type) {
  942. /// case NK_COMMAND_LINE:
  943. /// your_draw_line_function(...)
  944. /// break;
  945. /// case NK_COMMAND_RECT
  946. /// your_draw_rect_function(...)
  947. /// break;
  948. /// case ...:
  949. /// // [...]
  950. /// }
  951. /// nk_clear(&ctx);
  952. /// }
  953. /// nk_free(&ctx);
  954. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  955. ///
  956. /// You probably noticed that you have to draw all of the UI each frame which is
  957. /// quite wasteful. While the actual UI updating loop is quite fast rendering
  958. /// without actually needing it is not. So there are multiple things you could do.
  959. ///
  960. /// First is only update on input. This of course is only an option if your
  961. /// application only depends on the UI and does not require any outside calculations.
  962. /// If you actually only update on input make sure to update the UI two times each
  963. /// frame and call `nk_clear` directly after the first pass and only draw in
  964. /// the second pass. In addition it is recommended to also add additional timers
  965. /// to make sure the UI is not drawn more than a fixed number of frames per second.
  966. ///
  967. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  968. /// struct nk_context ctx;
  969. /// nk_init_xxx(&ctx, ...);
  970. /// while (1) {
  971. /// // [...wait for input ]
  972. /// // [...do two UI passes ...]
  973. /// do_ui(...)
  974. /// nk_clear(&ctx);
  975. /// do_ui(...)
  976. /// //
  977. /// // draw
  978. /// const struct nk_command *cmd = 0;
  979. /// nk_foreach(cmd, &ctx) {
  980. /// switch (cmd->type) {
  981. /// case NK_COMMAND_LINE:
  982. /// your_draw_line_function(...)
  983. /// break;
  984. /// case NK_COMMAND_RECT
  985. /// your_draw_rect_function(...)
  986. /// break;
  987. /// case ...:
  988. /// //[...]
  989. /// }
  990. /// nk_clear(&ctx);
  991. /// }
  992. /// nk_free(&ctx);
  993. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  994. ///
  995. /// The second probably more applicable trick is to only draw if anything changed.
  996. /// It is not really useful for applications with continuous draw loop but
  997. /// quite useful for desktop applications. To actually get nuklear to only
  998. /// draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  999. /// allocate a memory buffer that will store each unique drawing output.
  1000. /// After each frame you compare the draw command memory inside the library
  1001. /// with your allocated buffer by memcmp. If memcmp detects differences
  1002. /// you have to copy the command buffer into the allocated buffer
  1003. /// and then draw like usual (this example uses fixed memory but you could
  1004. /// use dynamically allocated memory).
  1005. ///
  1006. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1007. /// //[... other defines ...]
  1008. /// #define NK_ZERO_COMMAND_MEMORY
  1009. /// #include "nuklear.h"
  1010. /// //
  1011. /// // setup context
  1012. /// struct nk_context ctx;
  1013. /// void *last = calloc(1,64*1024);
  1014. /// void *buf = calloc(1,64*1024);
  1015. /// nk_init_fixed(&ctx, buf, 64*1024);
  1016. /// //
  1017. /// // loop
  1018. /// while (1) {
  1019. /// // [...input...]
  1020. /// // [...ui...]
  1021. /// void *cmds = nk_buffer_memory(&ctx.memory);
  1022. /// if (memcmp(cmds, last, ctx.memory.allocated)) {
  1023. /// memcpy(last,cmds,ctx.memory.allocated);
  1024. /// const struct nk_command *cmd = 0;
  1025. /// nk_foreach(cmd, &ctx) {
  1026. /// switch (cmd->type) {
  1027. /// case NK_COMMAND_LINE:
  1028. /// your_draw_line_function(...)
  1029. /// break;
  1030. /// case NK_COMMAND_RECT
  1031. /// your_draw_rect_function(...)
  1032. /// break;
  1033. /// case ...:
  1034. /// // [...]
  1035. /// }
  1036. /// }
  1037. /// }
  1038. /// nk_clear(&ctx);
  1039. /// }
  1040. /// nk_free(&ctx);
  1041. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1042. ///
  1043. /// Finally while using draw commands makes sense for higher abstracted platforms like
  1044. /// X11 and Win32 or drawing libraries it is often desirable to use graphics
  1045. /// hardware directly. Therefore it is possible to just define
  1046. /// `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  1047. /// To access the vertex output you first have to convert all draw commands into
  1048. /// vertexes by calling `nk_convert` which takes in your preferred vertex format.
  1049. /// After successfully converting all draw commands just iterate over and execute all
  1050. /// vertex draw commands:
  1051. ///
  1052. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1053. /// // fill configuration
  1054. /// struct nk_convert_config cfg = {};
  1055. /// static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  1056. /// {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  1057. /// {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  1058. /// {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  1059. /// {NK_VERTEX_LAYOUT_END}
  1060. /// };
  1061. /// cfg.shape_AA = NK_ANTI_ALIASING_ON;
  1062. /// cfg.line_AA = NK_ANTI_ALIASING_ON;
  1063. /// cfg.vertex_layout = vertex_layout;
  1064. /// cfg.vertex_size = sizeof(struct your_vertex);
  1065. /// cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  1066. /// cfg.circle_segment_count = 22;
  1067. /// cfg.curve_segment_count = 22;
  1068. /// cfg.arc_segment_count = 22;
  1069. /// cfg.global_alpha = 1.0f;
  1070. /// cfg.null = dev->null;
  1071. /// //
  1072. /// // setup buffers and convert
  1073. /// struct nk_buffer cmds, verts, idx;
  1074. /// nk_buffer_init_default(&cmds);
  1075. /// nk_buffer_init_default(&verts);
  1076. /// nk_buffer_init_default(&idx);
  1077. /// nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  1078. /// //
  1079. /// // draw
  1080. /// nk_draw_foreach(cmd, &ctx, &cmds) {
  1081. /// if (!cmd->elem_count) continue;
  1082. /// //[...]
  1083. /// }
  1084. /// nk_buffer_free(&cms);
  1085. /// nk_buffer_free(&verts);
  1086. /// nk_buffer_free(&idx);
  1087. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1088. ///
  1089. /// #### Reference
  1090. /// Function | Description
  1091. /// --------------------|-------------------------------------------------------
  1092. /// __nk__begin__ | Returns the first draw command in the context draw command list to be drawn
  1093. /// __nk__next__ | Increments the draw command iterator to the next command inside the context draw command list
  1094. /// __nk_foreach__ | Iterates over each draw command inside the context draw command list
  1095. /// __nk_convert__ | Converts from the abstract draw commands list into a hardware accessible vertex format
  1096. /// __nk_draw_begin__ | Returns the first vertex command in the context vertex draw list to be executed
  1097. /// __nk__draw_next__ | Increments the vertex command iterator to the next command inside the context vertex command list
  1098. /// __nk__draw_end__ | Returns the end of the vertex draw list
  1099. /// __nk_draw_foreach__ | Iterates over each vertex draw command inside the vertex draw list
  1100. */
  1101. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  1102. enum nk_convert_result {
  1103. NK_CONVERT_SUCCESS = 0,
  1104. NK_CONVERT_INVALID_PARAM = 1,
  1105. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  1106. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  1107. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  1108. };
  1109. struct nk_draw_null_texture {
  1110. nk_handle texture; /* texture handle to a texture with a white pixel */
  1111. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  1112. };
  1113. struct nk_convert_config {
  1114. float global_alpha; /* global alpha value */
  1115. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  1116. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  1117. unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
  1118. unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
  1119. unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
  1120. struct nk_draw_null_texture null; /* handle to texture with a white pixel for shape drawing */
  1121. const struct nk_draw_vertex_layout_element *vertex_layout; /* describes the vertex output format and packing */
  1122. nk_size vertex_size; /* sizeof one vertex for vertex packing */
  1123. nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
  1124. };
  1125. /*/// #### nk__begin
  1126. /// Returns a draw command list iterator to iterate all draw
  1127. /// commands accumulated over one frame.
  1128. ///
  1129. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1130. /// const struct nk_command* nk__begin(struct nk_context*);
  1131. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1132. ///
  1133. /// Parameter | Description
  1134. /// ------------|-----------------------------------------------------------
  1135. /// __ctx__ | must point to an previously initialized `nk_context` struct at the end of a frame
  1136. ///
  1137. /// Returns draw command pointer pointing to the first command inside the draw command list
  1138. */
  1139. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1140. /*/// #### nk__next
  1141. /// Returns a draw command list iterator to iterate all draw
  1142. ///
  1143. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1144. /// const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1145. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1146. ///
  1147. /// Parameter | Description
  1148. /// ------------|-----------------------------------------------------------
  1149. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1150. /// __cmd__ | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  1151. ///
  1152. /// Returns draw command pointer pointing to the next command inside the draw command list
  1153. */
  1154. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1155. /*/// #### nk_foreach
  1156. /// Iterates over each draw command inside the context draw command list
  1157. ///
  1158. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1159. /// #define nk_foreach(c, ctx)
  1160. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1161. ///
  1162. /// Parameter | Description
  1163. /// ------------|-----------------------------------------------------------
  1164. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1165. /// __cmd__ | Command pointer initialized to NULL
  1166. ///
  1167. /// Returns draw command pointer pointing to the next command inside the draw command list
  1168. */
  1169. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  1170. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1171. /*/// #### nk_convert
  1172. /// Converts all internal draw commands into vertex draw commands and fills
  1173. /// three buffers with vertexes, vertex draw commands and vertex indices. The vertex format
  1174. /// as well as some other configuration values have to be configured by filling out a
  1175. /// `nk_convert_config` struct.
  1176. ///
  1177. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1178. /// nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  1179. // struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1180. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1181. ///
  1182. /// Parameter | Description
  1183. /// ------------|-----------------------------------------------------------
  1184. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1185. /// __cmds__ | Must point to a previously initialized buffer to hold converted vertex draw commands
  1186. /// __vertices__| Must point to a previously initialized buffer to hold all produced vertices
  1187. /// __elements__| Must point to a previously initialized buffer to hold all produced vertex indices
  1188. /// __config__ | Must point to a filled out `nk_config` struct to configure the conversion process
  1189. ///
  1190. /// Returns one of enum nk_convert_result error codes
  1191. ///
  1192. /// Parameter | Description
  1193. /// --------------------------------|-----------------------------------------------------------
  1194. /// NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
  1195. /// NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
  1196. /// NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
  1197. /// NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
  1198. /// NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indicies is full or failed to allocate more memory
  1199. */
  1200. 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*);
  1201. /*/// #### nk__draw_begin
  1202. /// Returns a draw vertex command buffer iterator to iterate each the vertex draw command buffer
  1203. ///
  1204. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1205. /// const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1206. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1207. ///
  1208. /// Parameter | Description
  1209. /// ------------|-----------------------------------------------------------
  1210. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1211. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1212. ///
  1213. /// Returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
  1214. */
  1215. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1216. /*/// #### nk__draw_end
  1217. /// Returns the vertex draw command at the end of the vertex draw command buffer
  1218. ///
  1219. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1220. /// const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
  1221. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1222. ///
  1223. /// Parameter | Description
  1224. /// ------------|-----------------------------------------------------------
  1225. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1226. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1227. ///
  1228. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1229. */
  1230. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  1231. /*/// #### nk__draw_next
  1232. /// Increments the vertex draw command buffer iterator
  1233. ///
  1234. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1235. /// const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1236. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1237. ///
  1238. /// Parameter | Description
  1239. /// ------------|-----------------------------------------------------------
  1240. /// __cmd__ | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  1241. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1242. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1243. ///
  1244. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1245. */
  1246. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1247. /*/// #### nk_draw_foreach
  1248. /// Iterates over each vertex draw command inside a vertex draw command buffer
  1249. ///
  1250. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1251. /// #define nk_draw_foreach(cmd,ctx, b)
  1252. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1253. ///
  1254. /// Parameter | Description
  1255. /// ------------|-----------------------------------------------------------
  1256. /// __cmd__ | `nk_draw_command`iterator set to NULL
  1257. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1258. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1259. */
  1260. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1261. #endif
  1262. /* =============================================================================
  1263. *
  1264. * WINDOW
  1265. *
  1266. * =============================================================================
  1267. /// ### Window
  1268. /// Windows are the main persistent state used inside nuklear and are life time
  1269. /// controlled by simply "retouching" (i.e. calling) each window each frame.
  1270. /// All widgets inside nuklear can only be added inside function pair `nk_begin_xxx`
  1271. /// and `nk_end`. Calling any widgets outside these two functions will result in an
  1272. /// assert in debug or no state change in release mode.<br /><br />
  1273. ///
  1274. /// Each window holds frame persistent state like position, size, flags, state tables,
  1275. /// and some garbage collected internal persistent widget state. Each window
  1276. /// is linked into a window stack list which determines the drawing and overlapping
  1277. /// order. The topmost window thereby is the currently active window.<br /><br />
  1278. ///
  1279. /// To change window position inside the stack occurs either automatically by
  1280. /// user input by being clicked on or programmatically by calling `nk_window_focus`.
  1281. /// Windows by default are visible unless explicitly being defined with flag
  1282. /// `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1283. /// `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1284. /// `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
  1285. ///
  1286. /// #### Usage
  1287. /// To create and keep a window you have to call one of the two `nk_begin_xxx`
  1288. /// functions to start window declarations and `nk_end` at the end. Furthermore it
  1289. /// is recommended to check the return value of `nk_begin_xxx` and only process
  1290. /// widgets inside the window if the value is not 0. Either way you have to call
  1291. /// `nk_end` at the end of window declarations. Furthermore, do not attempt to
  1292. /// nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1293. /// in a segmentation fault.
  1294. ///
  1295. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1296. /// if (nk_begin_xxx(...) {
  1297. /// // [... widgets ...]
  1298. /// }
  1299. /// nk_end(ctx);
  1300. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1301. ///
  1302. /// In the grand concept window and widget declarations need to occur after input
  1303. /// handling and before drawing to screen. Not doing so can result in higher
  1304. /// latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1305. /// is called at the end of the frame. While nuklear's default platform backends
  1306. /// already call `nk_clear` for you if you write your own backend not calling
  1307. /// `nk_clear` can cause asserts or even worse undefined behavior.
  1308. ///
  1309. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1310. /// struct nk_context ctx;
  1311. /// nk_init_xxx(&ctx, ...);
  1312. /// while (1) {
  1313. /// Event evt;
  1314. /// nk_input_begin(&ctx);
  1315. /// while (GetEvent(&evt)) {
  1316. /// if (evt.type == MOUSE_MOVE)
  1317. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1318. /// else if (evt.type == [...]) {
  1319. /// nk_input_xxx(...);
  1320. /// }
  1321. /// }
  1322. /// nk_input_end(&ctx);
  1323. ///
  1324. /// if (nk_begin_xxx(...) {
  1325. /// //[...]
  1326. /// }
  1327. /// nk_end(ctx);
  1328. ///
  1329. /// const struct nk_command *cmd = 0;
  1330. /// nk_foreach(cmd, &ctx) {
  1331. /// case NK_COMMAND_LINE:
  1332. /// your_draw_line_function(...)
  1333. /// break;
  1334. /// case NK_COMMAND_RECT
  1335. /// your_draw_rect_function(...)
  1336. /// break;
  1337. /// case //...:
  1338. /// //[...]
  1339. /// }
  1340. /// nk_clear(&ctx);
  1341. /// }
  1342. /// nk_free(&ctx);
  1343. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1344. ///
  1345. /// #### Reference
  1346. /// Function | Description
  1347. /// ------------------------------------|----------------------------------------
  1348. /// nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1349. /// nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
  1350. /// nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1351. //
  1352. /// nk_window_find | Finds and returns the window with give name
  1353. /// nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
  1354. /// nk_window_get_position | Returns the position of the currently processed window
  1355. /// nk_window_get_size | Returns the size with width and height of the currently processed window
  1356. /// nk_window_get_width | Returns the width of the currently processed window
  1357. /// nk_window_get_height | Returns the height of the currently processed window
  1358. /// nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
  1359. /// nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1360. /// nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1361. /// nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1362. /// nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
  1363. /// nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
  1364. /// nk_window_has_focus | Returns if the currently processed window is currently active
  1365. /// nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
  1366. /// nk_window_is_closed | Returns if the currently processed window was closed
  1367. /// nk_window_is_hidden | Returns if the currently processed window was hidden
  1368. /// nk_window_is_active | Same as nk_window_has_focus for some reason
  1369. /// nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
  1370. /// nk_window_is_any_hovered | Return if any window currently hovered
  1371. /// nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
  1372. //
  1373. /// nk_window_set_bounds | Updates position and size of the currently processed window
  1374. /// nk_window_set_position | Updates position of the currently process window
  1375. /// nk_window_set_size | Updates the size of the currently processed window
  1376. /// nk_window_set_focus | Set the currently processed window as active window
  1377. //
  1378. /// nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
  1379. /// nk_window_collapse | Collapses the window with given window name
  1380. /// nk_window_collapse_if | Collapses the window with given window name if the given condition was met
  1381. /// nk_window_show | Hides a visible or reshows a hidden window
  1382. /// nk_window_show_if | Hides/shows a window depending on condition
  1383. */
  1384. /*
  1385. /// #### nk_panel_flags
  1386. /// Flag | Description
  1387. /// ----------------------------|----------------------------------------
  1388. /// NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
  1389. /// NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
  1390. /// 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
  1391. /// NK_WINDOW_CLOSABLE | Adds a closable icon into the header
  1392. /// NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
  1393. /// NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
  1394. /// NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
  1395. /// 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
  1396. /// NK_WINDOW_BACKGROUND | Always keep window in the background
  1397. /// NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-ottom corner instead right-bottom
  1398. /// NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
  1399. ///
  1400. /// #### nk_collapse_states
  1401. /// State | Description
  1402. /// ----------------|-----------------------------------------------------------
  1403. /// __NK_MINIMIZED__| UI section is collased and not visibile until maximized
  1404. /// __NK_MAXIMIZED__| UI section is extended and visibile until minimized
  1405. /// <br /><br />
  1406. */
  1407. enum nk_panel_flags {
  1408. NK_WINDOW_BORDER = NK_FLAG(0),
  1409. NK_WINDOW_MOVABLE = NK_FLAG(1),
  1410. NK_WINDOW_SCALABLE = NK_FLAG(2),
  1411. NK_WINDOW_CLOSABLE = NK_FLAG(3),
  1412. NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
  1413. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
  1414. NK_WINDOW_TITLE = NK_FLAG(6),
  1415. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
  1416. NK_WINDOW_BACKGROUND = NK_FLAG(8),
  1417. NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
  1418. NK_WINDOW_NO_INPUT = NK_FLAG(10)
  1419. };
  1420. /*/// #### nk_begin
  1421. /// Starts a new window; needs to be called every frame for every
  1422. /// window (unless hidden) or otherwise the window gets removed
  1423. ///
  1424. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1425. /// int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1426. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1427. ///
  1428. /// Parameter | Description
  1429. /// ------------|-----------------------------------------------------------
  1430. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1431. /// __title__ | Window title and identifier. Needs to be persistent over frames to identify the window
  1432. /// __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
  1433. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1434. ///
  1435. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1436. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1437. */
  1438. NK_API int nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1439. /*/// #### nk_begin_titled
  1440. /// Extended window start with separated title and identifier to allow multiple
  1441. /// windows with same name but not title
  1442. ///
  1443. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1444. /// int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1445. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1446. ///
  1447. /// Parameter | Description
  1448. /// ------------|-----------------------------------------------------------
  1449. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1450. /// __name__ | Window identifier. Needs to be persistent over frames to identify the window
  1451. /// __title__ | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1452. /// __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
  1453. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1454. ///
  1455. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1456. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1457. */
  1458. NK_API int nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1459. /*/// #### nk_end
  1460. /// Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1461. /// All widget calls after this functions will result in asserts or no state changes
  1462. ///
  1463. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1464. /// void nk_end(struct nk_context *ctx);
  1465. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1466. ///
  1467. /// Parameter | Description
  1468. /// ------------|-----------------------------------------------------------
  1469. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1470. */
  1471. NK_API void nk_end(struct nk_context *ctx);
  1472. /*/// #### nk_window_find
  1473. /// Finds and returns a window from passed name
  1474. ///
  1475. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1476. /// void nk_end(struct nk_context *ctx);
  1477. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1478. ///
  1479. /// Parameter | Description
  1480. /// ------------|-----------------------------------------------------------
  1481. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1482. /// __name__ | Window identifier
  1483. ///
  1484. /// Returns a `nk_window` struct pointing to the identified window or NULL if
  1485. /// no window with given name was found
  1486. */
  1487. NK_API struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1488. /*/// #### nk_window_get_bounds
  1489. ///
  1490. /// Returns a rectangle with screen position and size of the currently processed window
  1491. /// !!! WARNING
  1492. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1493. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1494. /// struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1495. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1496. ///
  1497. /// Parameter | Description
  1498. /// ------------|-----------------------------------------------------------
  1499. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1500. ///
  1501. /// Returns a `nk_rect` struct with window upper left window position and size
  1502. */
  1503. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1504. /*/// #### nk_window_get_bounds
  1505. ///
  1506. /// Returns the position of the currently processed window.
  1507. /// !!! WARNING
  1508. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1509. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1510. /// struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1511. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1512. ///
  1513. /// Parameter | Description
  1514. /// ------------|-----------------------------------------------------------
  1515. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1516. ///
  1517. /// Returns a `nk_vec2` struct with window upper left position
  1518. */
  1519. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1520. /*/// #### nk_window_get_size
  1521. ///
  1522. /// Returns the size with width and height of the currently processed window.
  1523. /// !!! WARNING
  1524. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1525. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1526. /// struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1527. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1528. ///
  1529. /// Parameter | Description
  1530. /// ------------|-----------------------------------------------------------
  1531. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1532. ///
  1533. /// Returns a `nk_vec2` struct with window width and height
  1534. */
  1535. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  1536. /*/// #### nk_window_get_width
  1537. ///
  1538. /// Returns the width of the currently processed window.
  1539. /// !!! WARNING
  1540. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1541. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1542. /// float nk_window_get_width(const struct nk_context *ctx);
  1543. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1544. ///
  1545. /// Parameter | Description
  1546. /// ------------|-----------------------------------------------------------
  1547. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1548. ///
  1549. /// Returns the current window width
  1550. */
  1551. NK_API float nk_window_get_width(const struct nk_context*);
  1552. /*/// #### nk_window_get_height
  1553. ///
  1554. /// Returns the height of the currently processed window.
  1555. /// !!! WARNING
  1556. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1557. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1558. /// float nk_window_get_height(const struct nk_context *ctx);
  1559. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1560. ///
  1561. /// Parameter | Description
  1562. /// ------------|-----------------------------------------------------------
  1563. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1564. ///
  1565. /// Returns the current window height
  1566. */
  1567. NK_API float nk_window_get_height(const struct nk_context*);
  1568. /*/// #### nk_window_get_panel
  1569. ///
  1570. /// Returns the underlying panel which contains all processing state of the current window.
  1571. /// !!! WARNING
  1572. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1573. /// !!! WARNING
  1574. /// Do not keep the returned panel pointer around it is only valid until `nk_end`
  1575. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1576. /// struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
  1577. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1578. ///
  1579. /// Parameter | Description
  1580. /// ------------|-----------------------------------------------------------
  1581. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1582. ///
  1583. /// Returns a pointer to window internal `nk_panel` state.
  1584. */
  1585. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  1586. /*/// #### nk_window_get_content_region
  1587. ///
  1588. /// Returns the position and size of the currently visible and non-clipped space
  1589. /// inside the currently processed window.
  1590. /// !!! WARNING
  1591. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1592. ///
  1593. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1594. /// struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
  1595. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1596. ///
  1597. /// Parameter | Description
  1598. /// ------------|-----------------------------------------------------------
  1599. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1600. ///
  1601. /// Returns `nk_rect` struct with screen position and size (no scrollbar offset)
  1602. /// of the visible space inside the current window
  1603. */
  1604. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  1605. /*/// #### nk_window_get_content_region_min
  1606. ///
  1607. /// Returns the upper left position of the currently visible and non-clipped
  1608. /// space inside the currently processed window.
  1609. /// !!! WARNING
  1610. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1611. ///
  1612. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1613. /// struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
  1614. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1615. ///
  1616. /// Parameter | Description
  1617. /// ------------|-----------------------------------------------------------
  1618. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1619. ///
  1620. /// returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
  1621. /// of the visible space inside the current window
  1622. */
  1623. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  1624. /*/// #### nk_window_get_content_region_max
  1625. ///
  1626. /// Returns the lower right screen position of the currently visible and
  1627. /// non-clipped space inside the currently processed window.
  1628. /// !!! WARNING
  1629. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1630. ///
  1631. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1632. /// struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
  1633. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1634. ///
  1635. /// Parameter | Description
  1636. /// ------------|-----------------------------------------------------------
  1637. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1638. ///
  1639. /// Returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
  1640. /// of the visible space inside the current window
  1641. */
  1642. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  1643. /*/// #### nk_window_get_content_region_size
  1644. ///
  1645. /// Returns the size of the currently visible and non-clipped space inside the
  1646. /// currently processed window
  1647. /// !!! WARNING
  1648. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1649. ///
  1650. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1651. /// struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
  1652. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1653. ///
  1654. /// Parameter | Description
  1655. /// ------------|-----------------------------------------------------------
  1656. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1657. ///
  1658. /// Returns `nk_vec2` struct with size the visible space inside the current window
  1659. */
  1660. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  1661. /*/// #### nk_window_get_canvas
  1662. /// Returns the draw command buffer. Can be used to draw custom widgets
  1663. /// !!! WARNING
  1664. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1665. /// !!! WARNING
  1666. /// Do not keep the returned command buffer pointer around it is only valid until `nk_end`
  1667. ///
  1668. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1669. /// struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
  1670. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1671. ///
  1672. /// Parameter | Description
  1673. /// ------------|-----------------------------------------------------------
  1674. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1675. ///
  1676. /// Returns a pointer to window internal `nk_command_buffer` struct used as
  1677. /// drawing canvas. Can be used to do custom drawing.
  1678. */
  1679. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  1680. /*/// #### nk_window_has_focus
  1681. /// Returns if the currently processed window is currently active
  1682. /// !!! WARNING
  1683. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1684. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1685. /// int nk_window_has_focus(const struct nk_context *ctx);
  1686. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1687. ///
  1688. /// Parameter | Description
  1689. /// ------------|-----------------------------------------------------------
  1690. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1691. ///
  1692. /// Returns `false(0)` if current window is not active or `true(1)` if it is
  1693. */
  1694. NK_API int nk_window_has_focus(const struct nk_context*);
  1695. /*/// #### nk_window_is_hovered
  1696. /// Return if the current window is being hovered
  1697. /// !!! WARNING
  1698. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1699. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1700. /// int nk_window_is_hovered(struct nk_context *ctx);
  1701. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1702. ///
  1703. /// Parameter | Description
  1704. /// ------------|-----------------------------------------------------------
  1705. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1706. ///
  1707. /// Returns `true(1)` if current window is hovered or `false(0)` otherwise
  1708. */
  1709. NK_API int nk_window_is_hovered(struct nk_context*);
  1710. /*/// #### nk_window_is_collapsed
  1711. /// Returns if the window with given name is currently minimized/collapsed
  1712. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1713. /// int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1714. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1715. ///
  1716. /// Parameter | Description
  1717. /// ------------|-----------------------------------------------------------
  1718. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1719. /// __name__ | Identifier of window you want to check if it is collapsed
  1720. ///
  1721. /// Returns `true(1)` if current window is minimized and `false(0)` if window not
  1722. /// found or is not minimized
  1723. */
  1724. NK_API int nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1725. /*/// #### nk_window_is_closed
  1726. /// Returns if the window with given name was closed by calling `nk_close`
  1727. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1728. /// int nk_window_is_closed(struct nk_context *ctx, const char *name);
  1729. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1730. ///
  1731. /// Parameter | Description
  1732. /// ------------|-----------------------------------------------------------
  1733. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1734. /// __name__ | Identifier of window you want to check if it is closed
  1735. ///
  1736. /// Returns `true(1)` if current window was closed or `false(0)` window not found or not closed
  1737. */
  1738. NK_API int nk_window_is_closed(struct nk_context*, const char*);
  1739. /*/// #### nk_window_is_hidden
  1740. /// Returns if the window with given name is hidden
  1741. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1742. /// int nk_window_is_hidden(struct nk_context *ctx, const char *name);
  1743. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1744. ///
  1745. /// Parameter | Description
  1746. /// ------------|-----------------------------------------------------------
  1747. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1748. /// __name__ | Identifier of window you want to check if it is hidden
  1749. ///
  1750. /// Returns `true(1)` if current window is hidden or `false(0)` window not found or visible
  1751. */
  1752. NK_API int nk_window_is_hidden(struct nk_context*, const char*);
  1753. /*/// #### nk_window_is_active
  1754. /// Same as nk_window_has_focus for some reason
  1755. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1756. /// int nk_window_is_active(struct nk_context *ctx, const char *name);
  1757. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1758. ///
  1759. /// Parameter | Description
  1760. /// ------------|-----------------------------------------------------------
  1761. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1762. /// __name__ | Identifier of window you want to check if it is active
  1763. ///
  1764. /// Returns `true(1)` if current window is active or `false(0)` window not found or not active
  1765. */
  1766. NK_API int nk_window_is_active(struct nk_context*, const char*);
  1767. /*/// #### nk_window_is_any_hovered
  1768. /// Returns if the any window is being hovered
  1769. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1770. /// int nk_window_is_any_hovered(struct nk_context*);
  1771. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1772. ///
  1773. /// Parameter | Description
  1774. /// ------------|-----------------------------------------------------------
  1775. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1776. ///
  1777. /// Returns `true(1)` if any window is hovered or `false(0)` otherwise
  1778. */
  1779. NK_API int nk_window_is_any_hovered(struct nk_context*);
  1780. /*/// #### nk_item_is_any_active
  1781. /// Returns if the any window is being hovered or any widget is currently active.
  1782. /// Can be used to decide if input should be processed by UI or your specific input handling.
  1783. /// Example could be UI and 3D camera to move inside a 3D space.
  1784. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1785. /// int nk_item_is_any_active(struct nk_context*);
  1786. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1787. ///
  1788. /// Parameter | Description
  1789. /// ------------|-----------------------------------------------------------
  1790. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1791. ///
  1792. /// Returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
  1793. */
  1794. NK_API int nk_item_is_any_active(struct nk_context*);
  1795. /*/// #### nk_window_set_bounds
  1796. /// Updates position and size of window with passed in name
  1797. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1798. /// void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1799. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1800. ///
  1801. /// Parameter | Description
  1802. /// ------------|-----------------------------------------------------------
  1803. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1804. /// __name__ | Identifier of the window to modify both position and size
  1805. /// __bounds__ | Must point to a `nk_rect` struct with the new position and size
  1806. */
  1807. NK_API void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1808. /*/// #### nk_window_set_position
  1809. /// Updates position of window with passed name
  1810. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1811. /// void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1812. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1813. ///
  1814. /// Parameter | Description
  1815. /// ------------|-----------------------------------------------------------
  1816. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1817. /// __name__ | Identifier of the window to modify both position
  1818. /// __pos__ | Must point to a `nk_vec2` struct with the new position
  1819. */
  1820. NK_API void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1821. /*/// #### nk_window_set_size
  1822. /// Updates size of window with passed in name
  1823. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1824. /// void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1825. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1826. ///
  1827. /// Parameter | Description
  1828. /// ------------|-----------------------------------------------------------
  1829. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1830. /// __name__ | Identifier of the window to modify both window size
  1831. /// __size__ | Must point to a `nk_vec2` struct with new window size
  1832. */
  1833. NK_API void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1834. /*/// #### nk_window_set_focus
  1835. /// Sets the window with given name as active
  1836. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1837. /// void nk_window_set_focus(struct nk_context*, const char *name);
  1838. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1839. ///
  1840. /// Parameter | Description
  1841. /// ------------|-----------------------------------------------------------
  1842. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1843. /// __name__ | Identifier of the window to set focus on
  1844. */
  1845. NK_API void nk_window_set_focus(struct nk_context*, const char *name);
  1846. /*/// #### nk_window_close
  1847. /// Closes a window and marks it for being freed at the end of the frame
  1848. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1849. /// void nk_window_close(struct nk_context *ctx, const char *name);
  1850. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1851. ///
  1852. /// Parameter | Description
  1853. /// ------------|-----------------------------------------------------------
  1854. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1855. /// __name__ | Identifier of the window to close
  1856. */
  1857. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  1858. /*/// #### nk_window_collapse
  1859. /// Updates collapse state of a window with given name
  1860. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1861. /// void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1862. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1863. ///
  1864. /// Parameter | Description
  1865. /// ------------|-----------------------------------------------------------
  1866. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1867. /// __name__ | Identifier of the window to close
  1868. /// __state__ | value out of nk_collapse_states section
  1869. */
  1870. NK_API void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1871. /*/// #### nk_window_collapse_if
  1872. /// Updates collapse state of a window with given name if given condition is met
  1873. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1874. /// void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1875. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1876. ///
  1877. /// Parameter | Description
  1878. /// ------------|-----------------------------------------------------------
  1879. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1880. /// __name__ | Identifier of the window to either collapse or maximize
  1881. /// __state__ | value out of nk_collapse_states section the window should be put into
  1882. /// __cond__ | condition that has to be met to actually commit the collapse state change
  1883. */
  1884. NK_API void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1885. /*/// #### nk_window_show
  1886. /// updates visibility state of a window with given name
  1887. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1888. /// void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1889. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1890. ///
  1891. /// Parameter | Description
  1892. /// ------------|-----------------------------------------------------------
  1893. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1894. /// __name__ | Identifier of the window to either collapse or maximize
  1895. /// __state__ | state with either visible or hidden to modify the window with
  1896. */
  1897. NK_API void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1898. /*/// #### nk_window_show_if
  1899. /// Updates visibility state of a window with given name if a given condition is met
  1900. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1901. /// void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1902. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1903. ///
  1904. /// Parameter | Description
  1905. /// ------------|-----------------------------------------------------------
  1906. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1907. /// __name__ | Identifier of the window to either hide or show
  1908. /// __state__ | state with either visible or hidden to modify the window with
  1909. /// __cond__ | condition that has to be met to actually commit the visbility state change
  1910. */
  1911. NK_API void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1912. /* =============================================================================
  1913. *
  1914. * LAYOUT
  1915. *
  1916. * =============================================================================
  1917. /// ### Layouting
  1918. /// Layouting in general describes placing widget inside a window with position and size.
  1919. /// While in this particular implementation there are five different APIs for layouting
  1920. /// each with different trade offs between control and ease of use. <br /><br />
  1921. ///
  1922. /// All layouting methods in this library are based around the concept of a row.
  1923. /// A row has a height the window content grows by and a number of columns and each
  1924. /// layouting method specifies how each widget is placed inside the row.
  1925. /// After a row has been allocated by calling a layouting functions and then
  1926. /// filled with widgets will advance an internal pointer over the allocated row. <br /><br />
  1927. ///
  1928. /// To actually define a layout you just call the appropriate layouting function
  1929. /// and each subsequent widget call will place the widget as specified. Important
  1930. /// here is that if you define more widgets then columns defined inside the layout
  1931. /// functions it will allocate the next row without you having to make another layouting <br /><br />
  1932. /// call.
  1933. ///
  1934. /// Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  1935. /// is that you have to define the row height for each. However the row height
  1936. /// often depends on the height of the font. <br /><br />
  1937. ///
  1938. /// To fix that internally nuklear uses a minimum row height that is set to the
  1939. /// height plus padding of currently active font and overwrites the row height
  1940. /// value if zero. <br /><br />
  1941. ///
  1942. /// If you manually want to change the minimum row height then
  1943. /// use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  1944. /// reset it back to be derived from font height. <br /><br />
  1945. ///
  1946. /// Also if you change the font in nuklear it will automatically change the minimum
  1947. /// row height for you and. This means if you change the font but still want
  1948. /// a minimum row height smaller than the font you have to repush your value. <br /><br />
  1949. ///
  1950. /// For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  1951. /// layouting method in combination with a cassowary constraint solver (there are
  1952. /// some versions on github with permissive license model) to take over all control over widget
  1953. /// layouting yourself. However for quick and dirty layouting using all the other layouting
  1954. /// functions should be fine.
  1955. ///
  1956. /// #### Usage
  1957. /// 1. __nk_layout_row_dynamic__<br /><br />
  1958. /// The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  1959. /// widgets with same horizontal space inside the row and dynamically grows
  1960. /// if the owning window grows in width. So the number of columns dictates
  1961. /// the size of each widget dynamically by formula:
  1962. ///
  1963. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1964. /// widget_width = (window_width - padding - spacing) * (1/colum_count)
  1965. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1966. ///
  1967. /// Just like all other layouting APIs if you define more widget than columns this
  1968. /// library will allocate a new row and keep all layouting parameters previously
  1969. /// defined.
  1970. ///
  1971. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1972. /// if (nk_begin_xxx(...) {
  1973. /// // first row with height: 30 composed of two widgets
  1974. /// nk_layout_row_dynamic(&ctx, 30, 2);
  1975. /// nk_widget(...);
  1976. /// nk_widget(...);
  1977. /// //
  1978. /// // second row with same parameter as defined above
  1979. /// nk_widget(...);
  1980. /// nk_widget(...);
  1981. /// //
  1982. /// // third row uses 0 for height which will use auto layouting
  1983. /// nk_layout_row_dynamic(&ctx, 0, 2);
  1984. /// nk_widget(...);
  1985. /// nk_widget(...);
  1986. /// }
  1987. /// nk_end(...);
  1988. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1989. ///
  1990. /// 2. __nk_layout_row_static__<br /><br />
  1991. /// Another easy layouting function is `nk_layout_row_static`. It provides each
  1992. /// widget with same horizontal pixel width inside the row and does not grow
  1993. /// if the owning window scales smaller or bigger.
  1994. ///
  1995. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1996. /// if (nk_begin_xxx(...) {
  1997. /// // first row with height: 30 composed of two widgets with width: 80
  1998. /// nk_layout_row_static(&ctx, 30, 80, 2);
  1999. /// nk_widget(...);
  2000. /// nk_widget(...);
  2001. /// //
  2002. /// // second row with same parameter as defined above
  2003. /// nk_widget(...);
  2004. /// nk_widget(...);
  2005. /// //
  2006. /// // third row uses 0 for height which will use auto layouting
  2007. /// nk_layout_row_static(&ctx, 0, 80, 2);
  2008. /// nk_widget(...);
  2009. /// nk_widget(...);
  2010. /// }
  2011. /// nk_end(...);
  2012. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2013. ///
  2014. /// 3. __nk_layout_row_xxx__<br /><br />
  2015. /// A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  2016. /// `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  2017. /// specify each column pixel or window ratio in a row. It supports either
  2018. /// directly setting per column pixel width or widget window ratio but not
  2019. /// both. Furthermore it is a immediate mode API so each value is directly
  2020. /// pushed before calling a widget. Therefore the layout is not automatically
  2021. /// repeating like the last two layouting functions.
  2022. ///
  2023. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2024. /// if (nk_begin_xxx(...) {
  2025. /// // first row with height: 25 composed of two widgets with width 60 and 40
  2026. /// nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  2027. /// nk_layout_row_push(ctx, 60);
  2028. /// nk_widget(...);
  2029. /// nk_layout_row_push(ctx, 40);
  2030. /// nk_widget(...);
  2031. /// nk_layout_row_end(ctx);
  2032. /// //
  2033. /// // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  2034. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  2035. /// nk_layout_row_push(ctx, 0.25f);
  2036. /// nk_widget(...);
  2037. /// nk_layout_row_push(ctx, 0.75f);
  2038. /// nk_widget(...);
  2039. /// nk_layout_row_end(ctx);
  2040. /// //
  2041. /// // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  2042. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  2043. /// nk_layout_row_push(ctx, 0.25f);
  2044. /// nk_widget(...);
  2045. /// nk_layout_row_push(ctx, 0.75f);
  2046. /// nk_widget(...);
  2047. /// nk_layout_row_end(ctx);
  2048. /// }
  2049. /// nk_end(...);
  2050. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2051. ///
  2052. /// 4. __nk_layout_row__<br /><br />
  2053. /// The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  2054. /// functions. Instead of pushing either pixel or window ratio for every widget
  2055. /// it allows to define it by array. The trade of for less control is that
  2056. /// `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  2057. /// same.
  2058. ///
  2059. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2060. /// if (nk_begin_xxx(...) {
  2061. /// // two rows with height: 30 composed of two widgets with width 60 and 40
  2062. /// const float size[] = {60,40};
  2063. /// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  2064. /// nk_widget(...);
  2065. /// nk_widget(...);
  2066. /// nk_widget(...);
  2067. /// nk_widget(...);
  2068. /// //
  2069. /// // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  2070. /// const float ratio[] = {0.25, 0.75};
  2071. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2072. /// nk_widget(...);
  2073. /// nk_widget(...);
  2074. /// nk_widget(...);
  2075. /// nk_widget(...);
  2076. /// //
  2077. /// // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  2078. /// const float ratio[] = {0.25, 0.75};
  2079. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2080. /// nk_widget(...);
  2081. /// nk_widget(...);
  2082. /// nk_widget(...);
  2083. /// nk_widget(...);
  2084. /// }
  2085. /// nk_end(...);
  2086. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2087. ///
  2088. /// 5. __nk_layout_row_template_xxx__<br /><br />
  2089. /// The most complex and second most flexible API is a simplified flexbox version without
  2090. /// line wrapping and weights for dynamic widgets. It is an immediate mode API but
  2091. /// unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  2092. /// before calling the templated widgets.
  2093. /// The row template layout has three different per widget size specifier. The first
  2094. /// one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
  2095. /// They do not grow if the row grows and will always stay the same.
  2096. /// The second size specifier is `nk_layout_row_template_push_variable`
  2097. /// which defines a minimum widget size but it also can grow if more space is available
  2098. /// not taken by other widgets.
  2099. /// Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
  2100. /// which are completely flexible and unlike variable widgets can even shrink
  2101. /// to zero if not enough space is provided.
  2102. ///
  2103. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2104. /// if (nk_begin_xxx(...) {
  2105. /// // two rows with height: 30 composed of three widgets
  2106. /// nk_layout_row_template_begin(ctx, 30);
  2107. /// nk_layout_row_template_push_dynamic(ctx);
  2108. /// nk_layout_row_template_push_variable(ctx, 80);
  2109. /// nk_layout_row_template_push_static(ctx, 80);
  2110. /// nk_layout_row_template_end(ctx);
  2111. /// //
  2112. /// // first row
  2113. /// nk_widget(...); // dynamic widget can go to zero if not enough space
  2114. /// nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  2115. /// nk_widget(...); // static widget with fixed 80 pixel width
  2116. /// //
  2117. /// // second row same layout
  2118. /// nk_widget(...);
  2119. /// nk_widget(...);
  2120. /// nk_widget(...);
  2121. /// }
  2122. /// nk_end(...);
  2123. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2124. ///
  2125. /// 6. __nk_layout_space_xxx__<br /><br />
  2126. /// Finally the most flexible API directly allows you to place widgets inside the
  2127. /// window. The space layout API is an immediate mode API which does not support
  2128. /// row auto repeat and directly sets position and size of a widget. Position
  2129. /// and size hereby can be either specified as ratio of allocated space or
  2130. /// allocated space local position and pixel size. Since this API is quite
  2131. /// powerful there are a number of utility functions to get the available space
  2132. /// and convert between local allocated space and screen space.
  2133. ///
  2134. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2135. /// if (nk_begin_xxx(...) {
  2136. /// // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2137. /// nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  2138. /// nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  2139. /// nk_widget(...);
  2140. /// nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  2141. /// nk_widget(...);
  2142. /// nk_layout_space_end(ctx);
  2143. /// //
  2144. /// // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2145. /// nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  2146. /// nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  2147. /// nk_widget(...);
  2148. /// nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  2149. /// nk_widget(...);
  2150. /// }
  2151. /// nk_end(...);
  2152. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2153. ///
  2154. /// #### Reference
  2155. /// Function | Description
  2156. /// ----------------------------------------|------------------------------------
  2157. /// nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
  2158. /// nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
  2159. /// nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
  2160. /// nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
  2161. //
  2162. /// nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
  2163. /// nk_layout_row_static | Current layout is divided into n same fixed sized columns
  2164. /// nk_layout_row_begin | Starts a new row with given height and number of columns
  2165. /// nk_layout_row_push | Pushes another column with given size or window ratio
  2166. /// nk_layout_row_end | Finished previously started row
  2167. /// nk_layout_row | Specifies row columns in array as either window ratio or size
  2168. //
  2169. /// nk_layout_row_template_begin | Begins the row template declaration
  2170. /// nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2171. /// nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2172. /// nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
  2173. /// nk_layout_row_template_end | Marks the end of the row template
  2174. //
  2175. /// nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
  2176. /// nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2177. /// nk_layout_space_end | Marks the end of the layouting space
  2178. //
  2179. /// nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
  2180. /// nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
  2181. /// nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
  2182. /// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
  2183. /// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
  2184. */
  2185. /*/// #### nk_layout_set_min_row_height
  2186. /// Sets the currently used minimum row height.
  2187. /// !!! WARNING
  2188. /// The passed height needs to include both your preferred row height
  2189. /// as well as padding. No internal padding is added.
  2190. ///
  2191. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2192. /// void nk_layout_set_min_row_height(struct nk_context*, float height);
  2193. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2194. ///
  2195. /// Parameter | Description
  2196. /// ------------|-----------------------------------------------------------
  2197. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2198. /// __height__ | New minimum row height to be used for auto generating the row height
  2199. */
  2200. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  2201. /*/// #### nk_layout_reset_min_row_height
  2202. /// Reset the currently used minimum row height back to `font_height + text_padding + padding`
  2203. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2204. /// void nk_layout_reset_min_row_height(struct nk_context*);
  2205. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2206. ///
  2207. /// Parameter | Description
  2208. /// ------------|-----------------------------------------------------------
  2209. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2210. */
  2211. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  2212. /*/// #### nk_layout_widget_bounds
  2213. /// Returns the width of the next row allocate by one of the layouting functions
  2214. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2215. /// struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2216. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2217. ///
  2218. /// Parameter | Description
  2219. /// ------------|-----------------------------------------------------------
  2220. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2221. ///
  2222. /// Return `nk_rect` with both position and size of the next row
  2223. */
  2224. NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2225. /*/// #### nk_layout_ratio_from_pixel
  2226. /// Utility functions to calculate window ratio from pixel size
  2227. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2228. /// float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2229. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2230. ///
  2231. /// Parameter | Description
  2232. /// ------------|-----------------------------------------------------------
  2233. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2234. /// __pixel__ | Pixel_width to convert to window ratio
  2235. ///
  2236. /// Returns `nk_rect` with both position and size of the next row
  2237. */
  2238. NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2239. /*/// #### nk_layout_row_dynamic
  2240. /// Sets current row layout to share horizontal space
  2241. /// between @cols number of widgets evenly. Once called all subsequent widget
  2242. /// calls greater than @cols will allocate a new row with same layout.
  2243. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2244. /// void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2245. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2246. ///
  2247. /// Parameter | Description
  2248. /// ------------|-----------------------------------------------------------
  2249. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2250. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2251. /// __columns__ | Number of widget inside row
  2252. */
  2253. NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2254. /*/// #### nk_layout_row_dynamic
  2255. /// Sets current row layout to fill @cols number of widgets
  2256. /// in row with same @item_width horizontal size. Once called all subsequent widget
  2257. /// calls greater than @cols will allocate a new row with same layout.
  2258. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2259. /// void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2260. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2261. ///
  2262. /// Parameter | Description
  2263. /// ------------|-----------------------------------------------------------
  2264. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2265. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2266. /// __width__ | Holds pixel width of each widget in the row
  2267. /// __columns__ | Number of widget inside row
  2268. */
  2269. NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2270. /*/// #### nk_layout_row_begin
  2271. /// Starts a new dynamic or fixed row with given height and columns.
  2272. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2273. /// void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2274. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2275. ///
  2276. /// Parameter | Description
  2277. /// ------------|-----------------------------------------------------------
  2278. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2279. /// __fmt__ | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2280. /// __height__ | holds height of each widget in row or zero for auto layouting
  2281. /// __columns__ | Number of widget inside row
  2282. */
  2283. NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2284. /*/// #### nk_layout_row_push
  2285. /// Specifies either window ratio or width of a single column
  2286. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2287. /// void nk_layout_row_push(struct nk_context*, float value);
  2288. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2289. ///
  2290. /// Parameter | Description
  2291. /// ------------|-----------------------------------------------------------
  2292. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2293. /// __value__ | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
  2294. */
  2295. NK_API void nk_layout_row_push(struct nk_context*, float value);
  2296. /*/// #### nk_layout_row_end
  2297. /// Finished previously started row
  2298. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2299. /// void nk_layout_row_end(struct nk_context*);
  2300. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2301. ///
  2302. /// Parameter | Description
  2303. /// ------------|-----------------------------------------------------------
  2304. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2305. */
  2306. NK_API void nk_layout_row_end(struct nk_context*);
  2307. /*/// #### nk_layout_row
  2308. /// Specifies row columns in array as either window ratio or size
  2309. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2310. /// void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2311. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2312. ///
  2313. /// Parameter | Description
  2314. /// ------------|-----------------------------------------------------------
  2315. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2316. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2317. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2318. /// __columns__ | Number of widget inside row
  2319. */
  2320. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2321. /*/// #### nk_layout_row_template_begin
  2322. /// Begins the row template declaration
  2323. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2324. /// void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2325. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2326. ///
  2327. /// Parameter | Description
  2328. /// ------------|-----------------------------------------------------------
  2329. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2330. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2331. */
  2332. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2333. /*/// #### nk_layout_row_template_push_dynamic
  2334. /// Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2335. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2336. /// void nk_layout_row_template_push_dynamic(struct nk_context*);
  2337. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2338. ///
  2339. /// Parameter | Description
  2340. /// ------------|-----------------------------------------------------------
  2341. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2342. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2343. */
  2344. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  2345. /*/// #### nk_layout_row_template_push_variable
  2346. /// Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2347. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2348. /// void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2349. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2350. ///
  2351. /// Parameter | Description
  2352. /// ------------|-----------------------------------------------------------
  2353. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2354. /// __width__ | Holds the minimum pixel width the next column must always be
  2355. */
  2356. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2357. /*/// #### nk_layout_row_template_push_static
  2358. /// Adds a static column that does not grow and will always have the same size
  2359. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2360. /// void nk_layout_row_template_push_static(struct nk_context*, float width);
  2361. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2362. ///
  2363. /// Parameter | Description
  2364. /// ------------|-----------------------------------------------------------
  2365. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2366. /// __width__ | Holds the absolute pixel width value the next column must be
  2367. */
  2368. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  2369. /*/// #### nk_layout_row_template_end
  2370. /// Marks the end of the row template
  2371. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2372. /// void nk_layout_row_template_end(struct nk_context*);
  2373. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2374. ///
  2375. /// Parameter | Description
  2376. /// ------------|-----------------------------------------------------------
  2377. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2378. */
  2379. NK_API void nk_layout_row_template_end(struct nk_context*);
  2380. /*/// #### nk_layout_space_begin
  2381. /// Begins a new layouting space that allows to specify each widgets position and size.
  2382. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2383. /// void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2384. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2385. ///
  2386. /// Parameter | Description
  2387. /// ------------|-----------------------------------------------------------
  2388. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2389. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2390. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2391. /// __columns__ | Number of widgets inside row
  2392. */
  2393. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2394. /*/// #### nk_layout_space_push
  2395. /// Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2396. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2397. /// void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
  2398. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2399. ///
  2400. /// Parameter | Description
  2401. /// ------------|-----------------------------------------------------------
  2402. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2403. /// __bounds__ | Position and size in laoyut space local coordinates
  2404. */
  2405. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
  2406. /*/// #### nk_layout_space_end
  2407. /// Marks the end of the layout space
  2408. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2409. /// void nk_layout_space_end(struct nk_context*);
  2410. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2411. ///
  2412. /// Parameter | Description
  2413. /// ------------|-----------------------------------------------------------
  2414. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2415. */
  2416. NK_API void nk_layout_space_end(struct nk_context*);
  2417. /*/// #### nk_layout_space_bounds
  2418. /// Utility function to calculate total space allocated for `nk_layout_space`
  2419. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2420. /// struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2421. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2422. ///
  2423. /// Parameter | Description
  2424. /// ------------|-----------------------------------------------------------
  2425. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2426. ///
  2427. /// Returns `nk_rect` holding the total space allocated
  2428. */
  2429. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2430. /*/// #### nk_layout_space_to_screen
  2431. /// Converts vector from nk_layout_space coordinate space into screen space
  2432. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2433. /// struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2434. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2435. ///
  2436. /// Parameter | Description
  2437. /// ------------|-----------------------------------------------------------
  2438. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2439. /// __vec__ | Position to convert from layout space into screen coordinate space
  2440. ///
  2441. /// Returns transformed `nk_vec2` in screen space coordinates
  2442. */
  2443. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2444. /*/// #### nk_layout_space_to_screen
  2445. /// Converts vector from layout space into screen space
  2446. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2447. /// struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2448. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2449. ///
  2450. /// Parameter | Description
  2451. /// ------------|-----------------------------------------------------------
  2452. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2453. /// __vec__ | Position to convert from screen space into layout coordinate space
  2454. ///
  2455. /// Returns transformed `nk_vec2` in layout space coordinates
  2456. */
  2457. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2458. /*/// #### nk_layout_space_rect_to_screen
  2459. /// Converts rectangle from screen space into layout space
  2460. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2461. /// struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2462. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2463. ///
  2464. /// Parameter | Description
  2465. /// ------------|-----------------------------------------------------------
  2466. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2467. /// __bounds__ | Rectangle to convert from layout space into screen space
  2468. ///
  2469. /// Returns transformed `nk_rect` in screen space coordinates
  2470. */
  2471. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2472. /*/// #### nk_layout_space_rect_to_local
  2473. /// Converts rectangle from layout space into screen space
  2474. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2475. /// struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2476. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2477. ///
  2478. /// Parameter | Description
  2479. /// ------------|-----------------------------------------------------------
  2480. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2481. /// __bounds__ | Rectangle to convert from layout space into screen space
  2482. ///
  2483. /// Returns transformed `nk_rect` in layout space coordinates
  2484. */
  2485. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2486. /* =============================================================================
  2487. *
  2488. * GROUP
  2489. *
  2490. * =============================================================================
  2491. /// ### Groups
  2492. /// Groups are basically windows inside windows. They allow to subdivide space
  2493. /// in a window to layout widgets as a group. Almost all more complex widget
  2494. /// layouting requirements can be solved using groups and basic layouting
  2495. /// fuctionality. Groups just like windows are identified by an unique name and
  2496. /// internally keep track of scrollbar offsets by default. However additional
  2497. /// versions are provided to directly manage the scrollbar.
  2498. ///
  2499. /// #### Usage
  2500. /// To create a group you have to call one of the three `nk_group_begin_xxx`
  2501. /// functions to start group declarations and `nk_group_end` at the end. Furthermore it
  2502. /// is required to check the return value of `nk_group_begin_xxx` and only process
  2503. /// widgets inside the window if the value is not 0.
  2504. /// Nesting groups is possible and even encouraged since many layouting schemes
  2505. /// can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
  2506. /// to be only called if the corosponding `nk_group_begin_xxx` call does not return 0:
  2507. ///
  2508. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2509. /// if (nk_group_begin_xxx(ctx, ...) {
  2510. /// // [... widgets ...]
  2511. /// nk_group_end(ctx);
  2512. /// }
  2513. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2514. ///
  2515. /// In the grand concept groups can be called after starting a window
  2516. /// with `nk_begin_xxx` and before calling `nk_end`:
  2517. ///
  2518. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2519. /// struct nk_context ctx;
  2520. /// nk_init_xxx(&ctx, ...);
  2521. /// while (1) {
  2522. /// // Input
  2523. /// Event evt;
  2524. /// nk_input_begin(&ctx);
  2525. /// while (GetEvent(&evt)) {
  2526. /// if (evt.type == MOUSE_MOVE)
  2527. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  2528. /// else if (evt.type == [...]) {
  2529. /// nk_input_xxx(...);
  2530. /// }
  2531. /// }
  2532. /// nk_input_end(&ctx);
  2533. /// //
  2534. /// // Window
  2535. /// if (nk_begin_xxx(...) {
  2536. /// // [...widgets...]
  2537. /// nk_layout_row_dynamic(...);
  2538. /// if (nk_group_begin_xxx(ctx, ...) {
  2539. /// //[... widgets ...]
  2540. /// nk_group_end(ctx);
  2541. /// }
  2542. /// }
  2543. /// nk_end(ctx);
  2544. /// //
  2545. /// // Draw
  2546. /// const struct nk_command *cmd = 0;
  2547. /// nk_foreach(cmd, &ctx) {
  2548. /// switch (cmd->type) {
  2549. /// case NK_COMMAND_LINE:
  2550. /// your_draw_line_function(...)
  2551. /// break;
  2552. /// case NK_COMMAND_RECT
  2553. /// your_draw_rect_function(...)
  2554. /// break;
  2555. /// case ...:
  2556. /// // [...]
  2557. /// }
  2558. // nk_clear(&ctx);
  2559. /// }
  2560. /// nk_free(&ctx);
  2561. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2562. /// #### Reference
  2563. /// Function | Description
  2564. /// --------------------------------|-------------------------------------------
  2565. /// nk_group_begin | Start a new group with internal scrollbar handling
  2566. /// nk_group_begin_titled | Start a new group with separeted name and title and internal scrollbar handling
  2567. /// nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
  2568. /// nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
  2569. /// nk_group_scrolled_begin | Start a new group with manual scrollbar handling
  2570. /// nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
  2571. */
  2572. /*/// #### nk_group_begin
  2573. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2574. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2575. /// int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2576. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2577. ///
  2578. /// Parameter | Description
  2579. /// ------------|-----------------------------------------------------------
  2580. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2581. /// __title__ | Must be an unique identifier for this group that is also used for the group header
  2582. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2583. ///
  2584. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2585. */
  2586. NK_API int nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2587. /*/// #### nk_group_begin_titled
  2588. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2589. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2590. /// int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2591. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2592. ///
  2593. /// Parameter | Description
  2594. /// ------------|-----------------------------------------------------------
  2595. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2596. /// __id__ | Must be an unique identifier for this group
  2597. /// __title__ | Group header title
  2598. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2599. ///
  2600. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2601. */
  2602. NK_API int nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2603. /*/// #### nk_group_end
  2604. /// Ends a widget group
  2605. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2606. /// void nk_group_end(struct nk_context*);
  2607. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2608. ///
  2609. /// Parameter | Description
  2610. /// ------------|-----------------------------------------------------------
  2611. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2612. */
  2613. NK_API void nk_group_end(struct nk_context*);
  2614. /*/// #### nk_group_scrolled_offset_begin
  2615. /// starts a new widget group. requires a previous layouting function to specify
  2616. /// a size. Does not keep track of scrollbar.
  2617. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2618. /// int nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2619. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2620. ///
  2621. /// Parameter | Description
  2622. /// ------------|-----------------------------------------------------------
  2623. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2624. /// __x_offset__| Scrollbar x-offset to offset all widgets inside the group horizontally.
  2625. /// __y_offset__| Scrollbar y-offset to offset all widgets inside the group vertically
  2626. /// __title__ | Window unique group title used to both identify and display in the group header
  2627. /// __flags__ | Window flags from the nk_panel_flags section
  2628. ///
  2629. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2630. */
  2631. 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);
  2632. /*/// #### nk_group_scrolled_begin
  2633. /// Starts a new widget group. requires a previous
  2634. /// layouting function to specify a size. Does not keep track of scrollbar.
  2635. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2636. /// int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2637. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2638. ///
  2639. /// Parameter | Description
  2640. /// ------------|-----------------------------------------------------------
  2641. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2642. /// __off__ | Both x- and y- scroll offset. Allows for manual scrollbar control
  2643. /// __title__ | Window unique group title used to both identify and display in the group header
  2644. /// __flags__ | Window flags from nk_panel_flags section
  2645. ///
  2646. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2647. */
  2648. NK_API int nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2649. /*/// #### nk_group_scrolled_end
  2650. /// Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
  2651. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2652. /// void nk_group_scrolled_end(struct nk_context*);
  2653. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2654. ///
  2655. /// Parameter | Description
  2656. /// ------------|-----------------------------------------------------------
  2657. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2658. */
  2659. NK_API void nk_group_scrolled_end(struct nk_context*);
  2660. /* =============================================================================
  2661. *
  2662. * TREE
  2663. *
  2664. * =============================================================================
  2665. /// ### Tree
  2666. /// Trees represent two different concept. First the concept of a collapsable
  2667. /// UI section that can be either in a hidden or visibile state. They allow the UI
  2668. /// user to selectively minimize the current set of visible UI to comprehend.
  2669. /// The second concept are tree widgets for visual UI representation of trees.<br /><br />
  2670. ///
  2671. /// Trees thereby can be nested for tree representations and multiple nested
  2672. /// collapsable UI sections. All trees are started by calling of the
  2673. /// `nk_tree_xxx_push_tree` functions and ended by calling one of the
  2674. /// `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
  2675. /// and optionally an image to be displayed and the initial collapse state from
  2676. /// the nk_collapse_states section.<br /><br />
  2677. ///
  2678. /// The runtime state of the tree is either stored outside the library by the caller
  2679. /// or inside which requires a unique ID. The unique ID can either be generated
  2680. /// automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
  2681. /// by `__FILE__` and a user provided ID generated for example by loop index with
  2682. /// function `nk_tree_push_id` or completely provided from outside by user with
  2683. /// function `nk_tree_push_hashed`.
  2684. ///
  2685. /// #### Usage
  2686. /// To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
  2687. /// functions to start a collapsable UI section and `nk_tree_xxx_pop` to mark the
  2688. /// end.
  2689. /// Each starting function will either return `false(0)` if the tree is collapsed
  2690. /// or hidden and therefore does not need to be filled with content or `true(1)`
  2691. /// if visible and required to be filled.
  2692. ///
  2693. /// !!! Note
  2694. /// The tree header does not require and layouting function and instead
  2695. /// calculates a auto height based on the currently used font size
  2696. ///
  2697. /// The tree ending functions only need to be called if the tree content is
  2698. /// actually visible. So make sure the tree push function is guarded by `if`
  2699. /// and the pop call is only taken if the tree is visible.
  2700. ///
  2701. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2702. /// if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
  2703. /// nk_layout_row_dynamic(...);
  2704. /// nk_widget(...);
  2705. /// nk_tree_pop(ctx);
  2706. /// }
  2707. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2708. ///
  2709. /// #### Reference
  2710. /// Function | Description
  2711. /// ----------------------------|-------------------------------------------
  2712. /// nk_tree_push | Start a collapsable UI section with internal state management
  2713. /// nk_tree_push_id | Start a collapsable UI section with internal state management callable in a look
  2714. /// nk_tree_push_hashed | Start a collapsable UI section with internal state management with full control over internal unique ID use to store state
  2715. /// nk_tree_image_push | Start a collapsable UI section with image and label header
  2716. /// nk_tree_image_push_id | Start a collapsable UI section with image and label header and internal state management callable in a look
  2717. /// 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
  2718. /// nk_tree_pop | Ends a collapsable UI section
  2719. //
  2720. /// nk_tree_state_push | Start a collapsable UI section with external state management
  2721. /// nk_tree_state_image_push | Start a collapsable UI section with image and label header and external state management
  2722. /// nk_tree_state_pop | Ends a collapsabale UI section
  2723. ///
  2724. /// #### nk_tree_type
  2725. /// Flag | Description
  2726. /// ----------------|----------------------------------------
  2727. /// NK_TREE_NODE | Highlighted tree header to mark a collapsable UI section
  2728. /// NK_TREE_TAB | Non-highighted tree header closer to tree representations
  2729. */
  2730. /*/// #### nk_tree_push
  2731. /// Starts a collapsable UI section with internal state management
  2732. /// !!! WARNING
  2733. /// To keep track of the runtime tree collapsable state this function uses
  2734. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2735. /// to call this function in a loop please use `nk_tree_push_id` or
  2736. /// `nk_tree_push_hashed` instead.
  2737. ///
  2738. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2739. /// #define nk_tree_push(ctx, type, title, state)
  2740. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2741. ///
  2742. /// Parameter | Description
  2743. /// ------------|-----------------------------------------------------------
  2744. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2745. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2746. /// __title__ | Label printed in the tree header
  2747. /// __state__ | Initial tree state value out of nk_collapse_states
  2748. ///
  2749. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2750. */
  2751. #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__)
  2752. /*/// #### nk_tree_push_id
  2753. /// Starts a collapsable UI section with internal state management callable in a look
  2754. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2755. /// #define nk_tree_push_id(ctx, type, title, state, id)
  2756. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2757. ///
  2758. /// Parameter | Description
  2759. /// ------------|-----------------------------------------------------------
  2760. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2761. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2762. /// __title__ | Label printed in the tree header
  2763. /// __state__ | Initial tree state value out of nk_collapse_states
  2764. /// __id__ | Loop counter index if this function is called in a loop
  2765. ///
  2766. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2767. */
  2768. #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)
  2769. /*/// #### nk_tree_push_hashed
  2770. /// Start a collapsable UI section with internal state management with full
  2771. /// control over internal unique ID used to store state
  2772. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2773. /// 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);
  2774. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2775. ///
  2776. /// Parameter | Description
  2777. /// ------------|-----------------------------------------------------------
  2778. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2779. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2780. /// __title__ | Label printed in the tree header
  2781. /// __state__ | Initial tree state value out of nk_collapse_states
  2782. /// __hash__ | Memory block or string to generate the ID from
  2783. /// __len__ | Size of passed memory block or string in __hash__
  2784. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2785. ///
  2786. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2787. */
  2788. 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);
  2789. /*/// #### nk_tree_image_push
  2790. /// Start a collapsable UI section with image and label header
  2791. /// !!! WARNING
  2792. /// To keep track of the runtime tree collapsable state this function uses
  2793. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2794. /// to call this function in a loop please use `nk_tree_image_push_id` or
  2795. /// `nk_tree_image_push_hashed` instead.
  2796. ///
  2797. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2798. /// #define nk_tree_image_push(ctx, type, img, title, state)
  2799. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2800. //
  2801. /// Parameter | Description
  2802. /// ------------|-----------------------------------------------------------
  2803. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2804. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2805. /// __img__ | Image to display inside the header on the left of the label
  2806. /// __title__ | Label printed in the tree header
  2807. /// __state__ | Initial tree state value out of nk_collapse_states
  2808. ///
  2809. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2810. */
  2811. #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__)
  2812. /*/// #### nk_tree_image_push_id
  2813. /// Start a collapsable UI section with image and label header and internal state
  2814. /// management callable in a look
  2815. ///
  2816. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2817. /// #define nk_tree_image_push_id(ctx, type, img, title, state, id)
  2818. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2819. ///
  2820. /// Parameter | Description
  2821. /// ------------|-----------------------------------------------------------
  2822. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2823. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2824. /// __img__ | Image to display inside the header on the left of the label
  2825. /// __title__ | Label printed in the tree header
  2826. /// __state__ | Initial tree state value out of nk_collapse_states
  2827. /// __id__ | Loop counter index if this function is called in a loop
  2828. ///
  2829. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2830. */
  2831. #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)
  2832. /*/// #### nk_tree_image_push_hashed
  2833. /// Start a collapsable UI section with internal state management with full
  2834. /// control over internal unique ID used to store state
  2835. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2836. /// 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);
  2837. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2838. ///
  2839. /// Parameter | Description
  2840. /// ------------|-----------------------------------------------------------
  2841. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2842. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2843. /// __img__ | Image to display inside the header on the left of the label
  2844. /// __title__ | Label printed in the tree header
  2845. /// __state__ | Initial tree state value out of nk_collapse_states
  2846. /// __hash__ | Memory block or string to generate the ID from
  2847. /// __len__ | Size of passed memory block or string in __hash__
  2848. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2849. ///
  2850. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2851. */
  2852. 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);
  2853. /*/// #### nk_tree_pop
  2854. /// Ends a collapsabale UI section
  2855. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2856. /// void nk_tree_pop(struct nk_context*);
  2857. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2858. ///
  2859. /// Parameter | Description
  2860. /// ------------|-----------------------------------------------------------
  2861. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2862. */
  2863. NK_API void nk_tree_pop(struct nk_context*);
  2864. /*/// #### nk_tree_state_push
  2865. /// Start a collapsable UI section with external state management
  2866. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2867. /// int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2868. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2869. ///
  2870. /// Parameter | Description
  2871. /// ------------|-----------------------------------------------------------
  2872. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2873. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2874. /// __title__ | Label printed in the tree header
  2875. /// __state__ | Persistent state to update
  2876. ///
  2877. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2878. */
  2879. NK_API int nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2880. /*/// #### nk_tree_state_image_push
  2881. /// Start a collapsable UI section with image and label header and external state management
  2882. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2883. /// int nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
  2884. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2885. ///
  2886. /// Parameter | Description
  2887. /// ------------|-----------------------------------------------------------
  2888. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2889. /// __img__ | Image to display inside the header on the left of the label
  2890. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2891. /// __title__ | Label printed in the tree header
  2892. /// __state__ | Persistent state to update
  2893. ///
  2894. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2895. */
  2896. 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);
  2897. /*/// #### nk_tree_state_pop
  2898. /// Ends a collapsabale UI section
  2899. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2900. /// void nk_tree_state_pop(struct nk_context*);
  2901. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2902. ///
  2903. /// Parameter | Description
  2904. /// ------------|-----------------------------------------------------------
  2905. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2906. */
  2907. NK_API void nk_tree_state_pop(struct nk_context*);
  2908. /* =============================================================================
  2909. *
  2910. * LIST VIEW
  2911. *
  2912. * ============================================================================= */
  2913. struct nk_list_view {
  2914. /* public: */
  2915. int begin, end, count;
  2916. /* private: */
  2917. int total_height;
  2918. struct nk_context *ctx;
  2919. nk_uint *scroll_pointer;
  2920. nk_uint scroll_value;
  2921. };
  2922. 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);
  2923. NK_API void nk_list_view_end(struct nk_list_view*);
  2924. /* =============================================================================
  2925. *
  2926. * WIDGET
  2927. *
  2928. * ============================================================================= */
  2929. enum nk_widget_layout_states {
  2930. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  2931. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  2932. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  2933. };
  2934. enum nk_widget_states {
  2935. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  2936. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  2937. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  2938. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  2939. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  2940. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  2941. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  2942. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  2943. };
  2944. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  2945. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  2946. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  2947. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  2948. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  2949. NK_API float nk_widget_width(struct nk_context*);
  2950. NK_API float nk_widget_height(struct nk_context*);
  2951. NK_API int nk_widget_is_hovered(struct nk_context*);
  2952. NK_API int nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  2953. NK_API int nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, int down);
  2954. NK_API void nk_spacing(struct nk_context*, int cols);
  2955. /* =============================================================================
  2956. *
  2957. * TEXT
  2958. *
  2959. * ============================================================================= */
  2960. enum nk_text_align {
  2961. NK_TEXT_ALIGN_LEFT = 0x01,
  2962. NK_TEXT_ALIGN_CENTERED = 0x02,
  2963. NK_TEXT_ALIGN_RIGHT = 0x04,
  2964. NK_TEXT_ALIGN_TOP = 0x08,
  2965. NK_TEXT_ALIGN_MIDDLE = 0x10,
  2966. NK_TEXT_ALIGN_BOTTOM = 0x20
  2967. };
  2968. enum nk_text_alignment {
  2969. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  2970. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  2971. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  2972. };
  2973. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  2974. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  2975. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  2976. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  2977. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  2978. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  2979. NK_API void nk_label_wrap(struct nk_context*, const char*);
  2980. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  2981. NK_API void nk_image(struct nk_context*, struct nk_image);
  2982. #ifdef NK_INCLUDE_STANDARD_VARARGS
  2983. NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  2984. 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);
  2985. NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(2);
  2986. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(3);
  2987. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  2988. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  2989. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  2990. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  2991. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  2992. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  2993. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  2994. #endif
  2995. /* =============================================================================
  2996. *
  2997. * BUTTON
  2998. *
  2999. * ============================================================================= */
  3000. NK_API int nk_button_text(struct nk_context*, const char *title, int len);
  3001. NK_API int nk_button_label(struct nk_context*, const char *title);
  3002. NK_API int nk_button_color(struct nk_context*, struct nk_color);
  3003. NK_API int nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  3004. NK_API int nk_button_image(struct nk_context*, struct nk_image img);
  3005. NK_API int nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  3006. NK_API int nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3007. NK_API int nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  3008. NK_API int nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  3009. NK_API int nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
  3010. NK_API int nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
  3011. NK_API int nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  3012. NK_API int nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  3013. 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);
  3014. 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);
  3015. 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);
  3016. 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);
  3017. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  3018. NK_API int nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  3019. NK_API int nk_button_pop_behavior(struct nk_context*);
  3020. /* =============================================================================
  3021. *
  3022. * CHECKBOX
  3023. *
  3024. * ============================================================================= */
  3025. NK_API int nk_check_label(struct nk_context*, const char*, int active);
  3026. NK_API int nk_check_text(struct nk_context*, const char*, int,int active);
  3027. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  3028. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  3029. NK_API int nk_checkbox_label(struct nk_context*, const char*, int *active);
  3030. NK_API int nk_checkbox_text(struct nk_context*, const char*, int, int *active);
  3031. NK_API int nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  3032. NK_API int nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  3033. /* =============================================================================
  3034. *
  3035. * RADIO BUTTON
  3036. *
  3037. * ============================================================================= */
  3038. NK_API int nk_radio_label(struct nk_context*, const char*, int *active);
  3039. NK_API int nk_radio_text(struct nk_context*, const char*, int, int *active);
  3040. NK_API int nk_option_label(struct nk_context*, const char*, int active);
  3041. NK_API int nk_option_text(struct nk_context*, const char*, int, int active);
  3042. /* =============================================================================
  3043. *
  3044. * SELECTABLE
  3045. *
  3046. * ============================================================================= */
  3047. NK_API int nk_selectable_label(struct nk_context*, const char*, nk_flags align, int *value);
  3048. NK_API int nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, int *value);
  3049. NK_API int nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, int *value);
  3050. NK_API int nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, int *value);
  3051. NK_API int nk_select_label(struct nk_context*, const char*, nk_flags align, int value);
  3052. NK_API int nk_select_text(struct nk_context*, const char*, int, nk_flags align, int value);
  3053. NK_API int nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, int value);
  3054. NK_API int nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, int value);
  3055. /* =============================================================================
  3056. *
  3057. * SLIDER
  3058. *
  3059. * ============================================================================= */
  3060. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  3061. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  3062. NK_API int nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  3063. NK_API int nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  3064. /* =============================================================================
  3065. *
  3066. * PROGRESSBAR
  3067. *
  3068. * ============================================================================= */
  3069. NK_API int nk_progress(struct nk_context*, nk_size *cur, nk_size max, int modifyable);
  3070. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, int modifyable);
  3071. /* =============================================================================
  3072. *
  3073. * COLOR PICKER
  3074. *
  3075. * ============================================================================= */
  3076. NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
  3077. NK_API int nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
  3078. /* =============================================================================
  3079. *
  3080. * PROPERTIES
  3081. *
  3082. * =============================================================================
  3083. /// ### Properties
  3084. /// Properties are the main value modification widgets in Nuklear. Changing a value
  3085. /// can be achieved by dragging, adding/removing incremental steps on button click
  3086. /// or by directly typing a number.
  3087. ///
  3088. /// #### Usage
  3089. /// Each property requires a unique name for identifaction that is also used for
  3090. /// displaying a label. If you want to use the same name multiple times make sure
  3091. /// add a '#' before your name. The '#' will not be shown but will generate a
  3092. /// unique ID. Each propery also takes in a minimum and maximum value. If you want
  3093. /// to make use of the complete number range of a type just use the provided
  3094. /// type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
  3095. /// `nk_property_int` and `nk_propertyi`. In additional each property takes in
  3096. /// a increment value that will be added or subtracted if either the increment
  3097. /// decrement button is clicked. Finally there is a value for increment per pixel
  3098. /// dragged that is added or subtracted from the value.
  3099. ///
  3100. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3101. /// int value = 0;
  3102. /// struct nk_context ctx;
  3103. /// nk_init_xxx(&ctx, ...);
  3104. /// while (1) {
  3105. /// // Input
  3106. /// Event evt;
  3107. /// nk_input_begin(&ctx);
  3108. /// while (GetEvent(&evt)) {
  3109. /// if (evt.type == MOUSE_MOVE)
  3110. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  3111. /// else if (evt.type == [...]) {
  3112. /// nk_input_xxx(...);
  3113. /// }
  3114. /// }
  3115. /// nk_input_end(&ctx);
  3116. /// //
  3117. /// // Window
  3118. /// if (nk_begin_xxx(...) {
  3119. /// // Property
  3120. /// nk_layout_row_dynamic(...);
  3121. /// nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
  3122. /// }
  3123. /// nk_end(ctx);
  3124. /// //
  3125. /// // Draw
  3126. /// const struct nk_command *cmd = 0;
  3127. /// nk_foreach(cmd, &ctx) {
  3128. /// switch (cmd->type) {
  3129. /// case NK_COMMAND_LINE:
  3130. /// your_draw_line_function(...)
  3131. /// break;
  3132. /// case NK_COMMAND_RECT
  3133. /// your_draw_rect_function(...)
  3134. /// break;
  3135. /// case ...:
  3136. /// // [...]
  3137. /// }
  3138. // nk_clear(&ctx);
  3139. /// }
  3140. /// nk_free(&ctx);
  3141. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3142. ///
  3143. /// #### Reference
  3144. /// Function | Description
  3145. /// --------------------|-------------------------------------------
  3146. /// nk_property_int | Integer property directly modifing a passed in value
  3147. /// nk_property_float | Float property directly modifing a passed in value
  3148. /// nk_property_double | Double property directly modifing a passed in value
  3149. /// nk_propertyi | Integer property returning the modified int value
  3150. /// nk_propertyf | Float property returning the modified float value
  3151. /// nk_propertyd | Double property returning the modified double value
  3152. ///
  3153. */
  3154. /*/// #### nk_property_int
  3155. /// Integer property directly modifing a passed in value
  3156. /// !!! WARNING
  3157. /// To generate a unique property ID using the same label make sure to insert
  3158. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3159. ///
  3160. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3161. /// void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3162. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3163. ///
  3164. /// Parameter | Description
  3165. /// --------------------|-----------------------------------------------------------
  3166. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3167. /// __name__ | String used both as a label as well as a unique identifier
  3168. /// __min__ | Minimum value not allowed to be underflown
  3169. /// __val__ | Integer pointer to be modified
  3170. /// __max__ | Maximum value not allowed to be overflown
  3171. /// __step__ | Increment added and subtracted on increment and decrement button
  3172. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3173. */
  3174. NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3175. /*/// #### nk_property_float
  3176. /// Float property directly modifing a passed in value
  3177. /// !!! WARNING
  3178. /// To generate a unique property ID using the same label make sure to insert
  3179. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3180. ///
  3181. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3182. /// void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3183. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3184. ///
  3185. /// Parameter | Description
  3186. /// --------------------|-----------------------------------------------------------
  3187. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3188. /// __name__ | String used both as a label as well as a unique identifier
  3189. /// __min__ | Minimum value not allowed to be underflown
  3190. /// __val__ | Float pointer to be modified
  3191. /// __max__ | Maximum value not allowed to be overflown
  3192. /// __step__ | Increment added and subtracted on increment and decrement button
  3193. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3194. */
  3195. NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3196. /*/// #### nk_property_double
  3197. /// Double property directly modifing a passed in value
  3198. /// !!! WARNING
  3199. /// To generate a unique property ID using the same label make sure to insert
  3200. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3201. ///
  3202. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3203. /// void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
  3204. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3205. ///
  3206. /// Parameter | Description
  3207. /// --------------------|-----------------------------------------------------------
  3208. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3209. /// __name__ | String used both as a label as well as a unique identifier
  3210. /// __min__ | Minimum value not allowed to be underflown
  3211. /// __val__ | Double pointer to be modified
  3212. /// __max__ | Maximum value not allowed to be overflown
  3213. /// __step__ | Increment added and subtracted on increment and decrement button
  3214. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3215. */
  3216. NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
  3217. /*/// #### nk_propertyi
  3218. /// Integer property modifing a passed in value and returning the new value
  3219. /// !!! WARNING
  3220. /// To generate a unique property ID using the same label make sure to insert
  3221. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3222. ///
  3223. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3224. /// int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3225. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3226. ///
  3227. /// Parameter | Description
  3228. /// --------------------|-----------------------------------------------------------
  3229. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3230. /// __name__ | String used both as a label as well as a unique identifier
  3231. /// __min__ | Minimum value not allowed to be underflown
  3232. /// __val__ | Current integer value to be modified and returned
  3233. /// __max__ | Maximum value not allowed to be overflown
  3234. /// __step__ | Increment added and subtracted on increment and decrement button
  3235. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3236. ///
  3237. /// Returns the new modified integer value
  3238. */
  3239. NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3240. /*/// #### nk_propertyf
  3241. /// Float property modifing a passed in value and returning the new value
  3242. /// !!! WARNING
  3243. /// To generate a unique property ID using the same label make sure to insert
  3244. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3245. ///
  3246. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3247. /// float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3248. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3249. ///
  3250. /// Parameter | Description
  3251. /// --------------------|-----------------------------------------------------------
  3252. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3253. /// __name__ | String used both as a label as well as a unique identifier
  3254. /// __min__ | Minimum value not allowed to be underflown
  3255. /// __val__ | Current float value to be modified and returned
  3256. /// __max__ | Maximum value not allowed to be overflown
  3257. /// __step__ | Increment added and subtracted on increment and decrement button
  3258. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3259. ///
  3260. /// Returns the new modified float value
  3261. */
  3262. NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3263. /*/// #### nk_propertyd
  3264. /// Float property modifing a passed in value and returning the new value
  3265. /// !!! WARNING
  3266. /// To generate a unique property ID using the same label make sure to insert
  3267. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3268. ///
  3269. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3270. /// float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
  3271. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3272. ///
  3273. /// Parameter | Description
  3274. /// --------------------|-----------------------------------------------------------
  3275. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3276. /// __name__ | String used both as a label as well as a unique identifier
  3277. /// __min__ | Minimum value not allowed to be underflown
  3278. /// __val__ | Current double value to be modified and returned
  3279. /// __max__ | Maximum value not allowed to be overflown
  3280. /// __step__ | Increment added and subtracted on increment and decrement button
  3281. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3282. ///
  3283. /// Returns the new modified double value
  3284. */
  3285. NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
  3286. /* =============================================================================
  3287. *
  3288. * TEXT EDIT
  3289. *
  3290. * ============================================================================= */
  3291. enum nk_edit_flags {
  3292. NK_EDIT_DEFAULT = 0,
  3293. NK_EDIT_READ_ONLY = NK_FLAG(0),
  3294. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  3295. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  3296. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  3297. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  3298. NK_EDIT_SELECTABLE = NK_FLAG(5),
  3299. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  3300. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  3301. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  3302. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  3303. NK_EDIT_MULTILINE = NK_FLAG(10),
  3304. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  3305. };
  3306. enum nk_edit_types {
  3307. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  3308. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
  3309. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
  3310. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
  3311. };
  3312. enum nk_edit_events {
  3313. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  3314. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  3315. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  3316. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  3317. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  3318. };
  3319. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
  3320. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
  3321. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  3322. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  3323. NK_API void nk_edit_unfocus(struct nk_context*);
  3324. /* =============================================================================
  3325. *
  3326. * CHART
  3327. *
  3328. * ============================================================================= */
  3329. NK_API int nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  3330. 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);
  3331. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  3332. 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);
  3333. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  3334. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  3335. NK_API void nk_chart_end(struct nk_context*);
  3336. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  3337. 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);
  3338. /* =============================================================================
  3339. *
  3340. * POPUP
  3341. *
  3342. * ============================================================================= */
  3343. NK_API int nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  3344. NK_API void nk_popup_close(struct nk_context*);
  3345. NK_API void nk_popup_end(struct nk_context*);
  3346. /* =============================================================================
  3347. *
  3348. * COMBOBOX
  3349. *
  3350. * ============================================================================= */
  3351. NK_API int nk_combo(struct nk_context*, const char **items, int count, int selected, int item_height, struct nk_vec2 size);
  3352. 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);
  3353. 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);
  3354. 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);
  3355. NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
  3356. 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);
  3357. 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);
  3358. 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);
  3359. /* =============================================================================
  3360. *
  3361. * ABSTRACT COMBOBOX
  3362. *
  3363. * ============================================================================= */
  3364. NK_API int nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
  3365. NK_API int nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
  3366. NK_API int nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  3367. NK_API int nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  3368. NK_API int nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
  3369. NK_API int nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
  3370. NK_API int nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  3371. NK_API int nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
  3372. NK_API int nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
  3373. NK_API int nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  3374. NK_API int nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  3375. NK_API int nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3376. NK_API int nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  3377. NK_API int nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3378. NK_API int nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3379. NK_API void nk_combo_close(struct nk_context*);
  3380. NK_API void nk_combo_end(struct nk_context*);
  3381. /* =============================================================================
  3382. *
  3383. * CONTEXTUAL
  3384. *
  3385. * ============================================================================= */
  3386. NK_API int nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  3387. NK_API int nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  3388. NK_API int nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  3389. NK_API int nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3390. NK_API int nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3391. NK_API int nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3392. NK_API int nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3393. NK_API void nk_contextual_close(struct nk_context*);
  3394. NK_API void nk_contextual_end(struct nk_context*);
  3395. /* =============================================================================
  3396. *
  3397. * TOOLTIP
  3398. *
  3399. * ============================================================================= */
  3400. NK_API void nk_tooltip(struct nk_context*, const char*);
  3401. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3402. NK_API void nk_tooltipf(struct nk_context*, const char*, ...);
  3403. #endif
  3404. NK_API int nk_tooltip_begin(struct nk_context*, float width);
  3405. NK_API void nk_tooltip_end(struct nk_context*);
  3406. /* =============================================================================
  3407. *
  3408. * MENU
  3409. *
  3410. * ============================================================================= */
  3411. NK_API void nk_menubar_begin(struct nk_context*);
  3412. NK_API void nk_menubar_end(struct nk_context*);
  3413. NK_API int nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  3414. NK_API int nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  3415. NK_API int nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  3416. NK_API int nk_menu_begin_image_text(struct nk_context*, const char*, int,nk_flags align,struct nk_image, struct nk_vec2 size);
  3417. NK_API int nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
  3418. NK_API int nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  3419. 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);
  3420. NK_API int nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align,enum nk_symbol_type, struct nk_vec2 size);
  3421. NK_API int nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  3422. NK_API int nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  3423. NK_API int nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3424. NK_API int nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3425. NK_API int nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3426. NK_API int nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3427. NK_API void nk_menu_close(struct nk_context*);
  3428. NK_API void nk_menu_end(struct nk_context*);
  3429. /* =============================================================================
  3430. *
  3431. * STYLE
  3432. *
  3433. * ============================================================================= */
  3434. enum nk_style_colors {
  3435. NK_COLOR_TEXT,
  3436. NK_COLOR_WINDOW,
  3437. NK_COLOR_HEADER,
  3438. NK_COLOR_BORDER,
  3439. NK_COLOR_BUTTON,
  3440. NK_COLOR_BUTTON_HOVER,
  3441. NK_COLOR_BUTTON_ACTIVE,
  3442. NK_COLOR_TOGGLE,
  3443. NK_COLOR_TOGGLE_HOVER,
  3444. NK_COLOR_TOGGLE_CURSOR,
  3445. NK_COLOR_SELECT,
  3446. NK_COLOR_SELECT_ACTIVE,
  3447. NK_COLOR_SLIDER,
  3448. NK_COLOR_SLIDER_CURSOR,
  3449. NK_COLOR_SLIDER_CURSOR_HOVER,
  3450. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  3451. NK_COLOR_PROPERTY,
  3452. NK_COLOR_EDIT,
  3453. NK_COLOR_EDIT_CURSOR,
  3454. NK_COLOR_COMBO,
  3455. NK_COLOR_CHART,
  3456. NK_COLOR_CHART_COLOR,
  3457. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  3458. NK_COLOR_SCROLLBAR,
  3459. NK_COLOR_SCROLLBAR_CURSOR,
  3460. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  3461. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  3462. NK_COLOR_TAB_HEADER,
  3463. NK_COLOR_COUNT
  3464. };
  3465. enum nk_style_cursor {
  3466. NK_CURSOR_ARROW,
  3467. NK_CURSOR_TEXT,
  3468. NK_CURSOR_MOVE,
  3469. NK_CURSOR_RESIZE_VERTICAL,
  3470. NK_CURSOR_RESIZE_HORIZONTAL,
  3471. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  3472. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  3473. NK_CURSOR_COUNT
  3474. };
  3475. NK_API void nk_style_default(struct nk_context*);
  3476. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  3477. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  3478. NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
  3479. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  3480. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  3481. NK_API int nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  3482. NK_API void nk_style_show_cursor(struct nk_context*);
  3483. NK_API void nk_style_hide_cursor(struct nk_context*);
  3484. NK_API int nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  3485. NK_API int nk_style_push_float(struct nk_context*, float*, float);
  3486. NK_API int nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  3487. NK_API int nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  3488. NK_API int nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  3489. NK_API int nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  3490. NK_API int nk_style_pop_font(struct nk_context*);
  3491. NK_API int nk_style_pop_float(struct nk_context*);
  3492. NK_API int nk_style_pop_vec2(struct nk_context*);
  3493. NK_API int nk_style_pop_style_item(struct nk_context*);
  3494. NK_API int nk_style_pop_flags(struct nk_context*);
  3495. NK_API int nk_style_pop_color(struct nk_context*);
  3496. /* =============================================================================
  3497. *
  3498. * COLOR
  3499. *
  3500. * ============================================================================= */
  3501. NK_API struct nk_color nk_rgb(int r, int g, int b);
  3502. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  3503. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  3504. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  3505. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  3506. NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
  3507. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  3508. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  3509. NK_API struct nk_color nk_rgba_u32(nk_uint);
  3510. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  3511. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  3512. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  3513. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  3514. NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
  3515. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  3516. NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
  3517. NK_API struct nk_colorf nk_hsva_colorfv(float *c);
  3518. NK_API void nk_colorf_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_colorf in);
  3519. NK_API void nk_colorf_hsva_fv(float *hsva, struct nk_colorf in);
  3520. NK_API struct nk_color nk_hsv(int h, int s, int v);
  3521. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  3522. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  3523. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  3524. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  3525. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  3526. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  3527. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  3528. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  3529. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  3530. /* color (conversion nuklear --> user) */
  3531. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  3532. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  3533. NK_API struct nk_colorf nk_color_cf(struct nk_color);
  3534. NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
  3535. NK_API void nk_color_dv(double *rgba_out, struct nk_color);
  3536. NK_API nk_uint nk_color_u32(struct nk_color);
  3537. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  3538. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  3539. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  3540. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  3541. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  3542. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  3543. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  3544. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  3545. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  3546. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  3547. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  3548. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  3549. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  3550. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  3551. /* =============================================================================
  3552. *
  3553. * IMAGE
  3554. *
  3555. * ============================================================================= */
  3556. NK_API nk_handle nk_handle_ptr(void*);
  3557. NK_API nk_handle nk_handle_id(int);
  3558. NK_API struct nk_image nk_image_handle(nk_handle);
  3559. NK_API struct nk_image nk_image_ptr(void*);
  3560. NK_API struct nk_image nk_image_id(int);
  3561. NK_API int nk_image_is_subimage(const struct nk_image* img);
  3562. NK_API struct nk_image nk_subimage_ptr(void*, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3563. NK_API struct nk_image nk_subimage_id(int, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3564. NK_API struct nk_image nk_subimage_handle(nk_handle, unsigned short w, unsigned short h, struct nk_rect sub_region);
  3565. /* =============================================================================
  3566. *
  3567. * MATH
  3568. *
  3569. * ============================================================================= */
  3570. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  3571. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  3572. NK_API struct nk_vec2 nk_vec2(float x, float y);
  3573. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  3574. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  3575. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  3576. NK_API struct nk_rect nk_get_null_rect(void);
  3577. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  3578. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  3579. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  3580. NK_API struct nk_rect nk_rectv(const float *xywh);
  3581. NK_API struct nk_rect nk_rectiv(const int *xywh);
  3582. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  3583. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  3584. /* =============================================================================
  3585. *
  3586. * STRING
  3587. *
  3588. * ============================================================================= */
  3589. NK_API int nk_strlen(const char *str);
  3590. NK_API int nk_stricmp(const char *s1, const char *s2);
  3591. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  3592. NK_API int nk_strtoi(const char *str, const char **endptr);
  3593. NK_API float nk_strtof(const char *str, const char **endptr);
  3594. NK_API double nk_strtod(const char *str, const char **endptr);
  3595. NK_API int nk_strfilter(const char *text, const char *regexp);
  3596. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  3597. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  3598. /* =============================================================================
  3599. *
  3600. * UTF-8
  3601. *
  3602. * ============================================================================= */
  3603. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  3604. NK_API int nk_utf_encode(nk_rune, char*, int);
  3605. NK_API int nk_utf_len(const char*, int byte_len);
  3606. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  3607. /* ===============================================================
  3608. *
  3609. * FONT
  3610. *
  3611. * ===============================================================*/
  3612. /* Font handling in this library was designed to be quite customizable and lets
  3613. you decide what you want to use and what you want to provide. There are three
  3614. different ways to use the font atlas. The first two will use your font
  3615. handling scheme and only requires essential data to run nuklear. The next
  3616. slightly more advanced features is font handling with vertex buffer output.
  3617. Finally the most complex API wise is using nuklear's font baking API.
  3618. 1.) Using your own implementation without vertex buffer output
  3619. --------------------------------------------------------------
  3620. So first up the easiest way to do font handling is by just providing a
  3621. `nk_user_font` struct which only requires the height in pixel of the used
  3622. font and a callback to calculate the width of a string. This way of handling
  3623. fonts is best fitted for using the normal draw shape command API where you
  3624. do all the text drawing yourself and the library does not require any kind
  3625. of deeper knowledge about which font handling mechanism you use.
  3626. IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  3627. over the complete life time! I know this sucks but it is currently the only
  3628. way to switch between fonts.
  3629. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3630. {
  3631. your_font_type *type = handle.ptr;
  3632. float text_width = ...;
  3633. return text_width;
  3634. }
  3635. struct nk_user_font font;
  3636. font.userdata.ptr = &your_font_class_or_struct;
  3637. font.height = your_font_height;
  3638. font.width = your_text_width_calculation;
  3639. struct nk_context ctx;
  3640. nk_init_default(&ctx, &font);
  3641. 2.) Using your own implementation with vertex buffer output
  3642. --------------------------------------------------------------
  3643. While the first approach works fine if you don't want to use the optional
  3644. vertex buffer output it is not enough if you do. To get font handling working
  3645. for these cases you have to provide two additional parameters inside the
  3646. `nk_user_font`. First a texture atlas handle used to draw text as subimages
  3647. of a bigger font atlas texture and a callback to query a character's glyph
  3648. information (offset, size, ...). So it is still possible to provide your own
  3649. font and use the vertex buffer output.
  3650. float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3651. {
  3652. your_font_type *type = handle.ptr;
  3653. float text_width = ...;
  3654. return text_width;
  3655. }
  3656. void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  3657. {
  3658. your_font_type *type = handle.ptr;
  3659. glyph.width = ...;
  3660. glyph.height = ...;
  3661. glyph.xadvance = ...;
  3662. glyph.uv[0].x = ...;
  3663. glyph.uv[0].y = ...;
  3664. glyph.uv[1].x = ...;
  3665. glyph.uv[1].y = ...;
  3666. glyph.offset.x = ...;
  3667. glyph.offset.y = ...;
  3668. }
  3669. struct nk_user_font font;
  3670. font.userdata.ptr = &your_font_class_or_struct;
  3671. font.height = your_font_height;
  3672. font.width = your_text_width_calculation;
  3673. font.query = query_your_font_glyph;
  3674. font.texture.id = your_font_texture;
  3675. struct nk_context ctx;
  3676. nk_init_default(&ctx, &font);
  3677. 3.) Nuklear font baker
  3678. ------------------------------------
  3679. The final approach if you do not have a font handling functionality or don't
  3680. want to use it in this library is by using the optional font baker.
  3681. The font baker APIs can be used to create a font plus font atlas texture
  3682. and can be used with or without the vertex buffer output.
  3683. It still uses the `nk_user_font` struct and the two different approaches
  3684. previously stated still work. The font baker is not located inside
  3685. `nk_context` like all other systems since it can be understood as more of
  3686. an extension to nuklear and does not really depend on any `nk_context` state.
  3687. Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  3688. functions. If you don't care about memory just call the default version
  3689. `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  3690. If you want to control memory allocation but you don't care if the allocated
  3691. memory is temporary and therefore can be freed directly after the baking process
  3692. is over or permanent you can call `nk_font_atlas_init`.
  3693. After successfully initializing the font baker you can add Truetype(.ttf) fonts from
  3694. different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  3695. functions. Adding font will permanently store each font, font config and ttf memory block(!)
  3696. inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  3697. the font baker by for example adding additional fonts you can call
  3698. `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  3699. As soon as you added all fonts you wanted you can now start the baking process
  3700. for every selected glyph to image by calling `nk_font_atlas_bake`.
  3701. The baking process returns image memory, width and height which can be used to
  3702. either create your own image object or upload it to any graphics library.
  3703. No matter which case you finally have to call `nk_font_atlas_end` which
  3704. will free all temporary memory including the font atlas image so make sure
  3705. you created our texture beforehand. `nk_font_atlas_end` requires a handle
  3706. to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  3707. which can be used for the optional vertex output. If you don't want it just
  3708. set the argument to `NULL`.
  3709. At this point you are done and if you don't want to reuse the font atlas you
  3710. can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  3711. memory. Finally if you don't use the font atlas and any of it's fonts anymore
  3712. you need to call `nk_font_atlas_clear` to free all memory still being used.
  3713. struct nk_font_atlas atlas;
  3714. nk_font_atlas_init_default(&atlas);
  3715. nk_font_atlas_begin(&atlas);
  3716. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  3717. nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  3718. const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  3719. nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  3720. struct nk_context ctx;
  3721. nk_init_default(&ctx, &font->handle);
  3722. while (1) {
  3723. }
  3724. nk_font_atlas_clear(&atlas);
  3725. The font baker API is probably the most complex API inside this library and
  3726. I would suggest reading some of my examples `example/` to get a grip on how
  3727. to use the font atlas. There are a number of details I left out. For example
  3728. how to merge fonts, configure a font with `nk_font_config` to use other languages,
  3729. use another texture coordinate format and a lot more:
  3730. struct nk_font_config cfg = nk_font_config(font_pixel_height);
  3731. cfg.merge_mode = nk_false or nk_true;
  3732. cfg.range = nk_font_korean_glyph_ranges();
  3733. cfg.coord_type = NK_COORD_PIXEL;
  3734. nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  3735. */
  3736. struct nk_user_font_glyph;
  3737. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  3738. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  3739. struct nk_user_font_glyph *glyph,
  3740. nk_rune codepoint, nk_rune next_codepoint);
  3741. #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
  3742. struct nk_user_font_glyph {
  3743. struct nk_vec2 uv[2];
  3744. /* texture coordinates */
  3745. struct nk_vec2 offset;
  3746. /* offset between top left and glyph */
  3747. float width, height;
  3748. /* size of the glyph */
  3749. float xadvance;
  3750. /* offset to the next glyph */
  3751. };
  3752. #endif
  3753. struct nk_user_font {
  3754. nk_handle userdata;
  3755. /* user provided font handle */
  3756. float height;
  3757. /* max height of the font */
  3758. nk_text_width_f width;
  3759. /* font string width in pixel callback */
  3760. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3761. nk_query_font_glyph_f query;
  3762. /* font glyph callback to query drawing info */
  3763. nk_handle texture;
  3764. /* texture handle to the used font atlas or texture */
  3765. #endif
  3766. };
  3767. #ifdef NK_INCLUDE_FONT_BAKING
  3768. enum nk_font_coord_type {
  3769. NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
  3770. NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
  3771. };
  3772. struct nk_font;
  3773. struct nk_baked_font {
  3774. float height;
  3775. /* height of the font */
  3776. float ascent, descent;
  3777. /* font glyphs ascent and descent */
  3778. nk_rune glyph_offset;
  3779. /* glyph array offset inside the font glyph baking output array */
  3780. nk_rune glyph_count;
  3781. /* number of glyphs of this font inside the glyph baking array output */
  3782. const nk_rune *ranges;
  3783. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  3784. };
  3785. struct nk_font_config {
  3786. struct nk_font_config *next;
  3787. /* NOTE: only used internally */
  3788. void *ttf_blob;
  3789. /* pointer to loaded TTF file memory block.
  3790. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3791. nk_size ttf_size;
  3792. /* size of the loaded TTF file memory block
  3793. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3794. unsigned char ttf_data_owned_by_atlas;
  3795. /* used inside font atlas: default to: 0*/
  3796. unsigned char merge_mode;
  3797. /* merges this font into the last font */
  3798. unsigned char pixel_snap;
  3799. /* align every character to pixel boundary (if true set oversample (1,1)) */
  3800. unsigned char oversample_v, oversample_h;
  3801. /* rasterize at hight quality for sub-pixel position */
  3802. unsigned char padding[3];
  3803. float size;
  3804. /* baked pixel height of the font */
  3805. enum nk_font_coord_type coord_type;
  3806. /* texture coordinate format with either pixel or UV coordinates */
  3807. struct nk_vec2 spacing;
  3808. /* extra pixel spacing between glyphs */
  3809. const nk_rune *range;
  3810. /* list of unicode ranges (2 values per range, zero terminated) */
  3811. struct nk_baked_font *font;
  3812. /* font to setup in the baking process: NOTE: not needed for font atlas */
  3813. nk_rune fallback_glyph;
  3814. /* fallback glyph to use if a given rune is not found */
  3815. struct nk_font_config *n;
  3816. struct nk_font_config *p;
  3817. };
  3818. struct nk_font_glyph {
  3819. nk_rune codepoint;
  3820. float xadvance;
  3821. float x0, y0, x1, y1, w, h;
  3822. float u0, v0, u1, v1;
  3823. };
  3824. struct nk_font {
  3825. struct nk_font *next;
  3826. struct nk_user_font handle;
  3827. struct nk_baked_font info;
  3828. float scale;
  3829. struct nk_font_glyph *glyphs;
  3830. const struct nk_font_glyph *fallback;
  3831. nk_rune fallback_codepoint;
  3832. nk_handle texture;
  3833. struct nk_font_config *config;
  3834. };
  3835. enum nk_font_atlas_format {
  3836. NK_FONT_ATLAS_ALPHA8,
  3837. NK_FONT_ATLAS_RGBA32
  3838. };
  3839. struct nk_font_atlas {
  3840. void *pixel;
  3841. int tex_width;
  3842. int tex_height;
  3843. struct nk_allocator permanent;
  3844. struct nk_allocator temporary;
  3845. struct nk_recti custom;
  3846. struct nk_cursor cursors[NK_CURSOR_COUNT];
  3847. int glyph_count;
  3848. struct nk_font_glyph *glyphs;
  3849. struct nk_font *default_font;
  3850. struct nk_font *fonts;
  3851. struct nk_font_config *config;
  3852. int font_num;
  3853. };
  3854. /* some language glyph codepoint ranges */
  3855. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  3856. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  3857. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  3858. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  3859. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3860. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  3861. #endif
  3862. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  3863. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator *persistent, struct nk_allocator *transient);
  3864. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  3865. NK_API struct nk_font_config nk_font_config(float pixel_height);
  3866. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  3867. #ifdef NK_INCLUDE_DEFAULT_FONT
  3868. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  3869. #endif
  3870. 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);
  3871. #ifdef NK_INCLUDE_STANDARD_IO
  3872. 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*);
  3873. #endif
  3874. 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*);
  3875. 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);
  3876. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  3877. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  3878. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  3879. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
  3880. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  3881. #endif
  3882. /* ==============================================================
  3883. *
  3884. * MEMORY BUFFER
  3885. *
  3886. * ===============================================================*/
  3887. /* A basic (double)-buffer with linear allocation and resetting as only
  3888. freeing policy. The buffer's main purpose is to control all memory management
  3889. inside the GUI toolkit and still leave memory control as much as possible in
  3890. the hand of the user while also making sure the library is easy to use if
  3891. not as much control is needed.
  3892. In general all memory inside this library can be provided from the user in
  3893. three different ways.
  3894. The first way and the one providing most control is by just passing a fixed
  3895. size memory block. In this case all control lies in the hand of the user
  3896. since he can exactly control where the memory comes from and how much memory
  3897. the library should consume. Of course using the fixed size API removes the
  3898. ability to automatically resize a buffer if not enough memory is provided so
  3899. you have to take over the resizing. While being a fixed sized buffer sounds
  3900. quite limiting, it is very effective in this library since the actual memory
  3901. consumption is quite stable and has a fixed upper bound for a lot of cases.
  3902. If you don't want to think about how much memory the library should allocate
  3903. at all time or have a very dynamic UI with unpredictable memory consumption
  3904. habits but still want control over memory allocation you can use the dynamic
  3905. allocator based API. The allocator consists of two callbacks for allocating
  3906. and freeing memory and optional userdata so you can plugin your own allocator.
  3907. The final and easiest way can be used by defining
  3908. NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  3909. allocation functions malloc and free and takes over complete control over
  3910. memory in this library.
  3911. */
  3912. struct nk_memory_status {
  3913. void *memory;
  3914. unsigned int type;
  3915. nk_size size;
  3916. nk_size allocated;
  3917. nk_size needed;
  3918. nk_size calls;
  3919. };
  3920. enum nk_allocation_type {
  3921. NK_BUFFER_FIXED,
  3922. NK_BUFFER_DYNAMIC
  3923. };
  3924. enum nk_buffer_allocation_type {
  3925. NK_BUFFER_FRONT,
  3926. NK_BUFFER_BACK,
  3927. NK_BUFFER_MAX
  3928. };
  3929. struct nk_buffer_marker {
  3930. int active;
  3931. nk_size offset;
  3932. };
  3933. struct nk_memory {void *ptr;nk_size size;};
  3934. struct nk_buffer {
  3935. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  3936. /* buffer marker to free a buffer to a certain offset */
  3937. struct nk_allocator pool;
  3938. /* allocator callback for dynamic buffers */
  3939. enum nk_allocation_type type;
  3940. /* memory management type */
  3941. struct nk_memory memory;
  3942. /* memory and size of the current memory block */
  3943. float grow_factor;
  3944. /* growing factor for dynamic memory management */
  3945. nk_size allocated;
  3946. /* total amount of memory allocated */
  3947. nk_size needed;
  3948. /* totally consumed memory given that enough memory is present */
  3949. nk_size calls;
  3950. /* number of allocation calls */
  3951. nk_size size;
  3952. /* current size of the buffer */
  3953. };
  3954. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3955. NK_API void nk_buffer_init_default(struct nk_buffer*);
  3956. #endif
  3957. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  3958. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  3959. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  3960. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
  3961. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  3962. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  3963. NK_API void nk_buffer_clear(struct nk_buffer*);
  3964. NK_API void nk_buffer_free(struct nk_buffer*);
  3965. NK_API void *nk_buffer_memory(struct nk_buffer*);
  3966. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  3967. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  3968. /* ==============================================================
  3969. *
  3970. * STRING
  3971. *
  3972. * ===============================================================*/
  3973. /* Basic string buffer which is only used in context with the text editor
  3974. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  3975. * the default string handling method. The only instance you should have any contact
  3976. * with this API is if you interact with an `nk_text_edit` object inside one of the
  3977. * copy and paste functions and even there only for more advanced cases. */
  3978. struct nk_str {
  3979. struct nk_buffer buffer;
  3980. int len; /* in codepoints/runes/glyphs */
  3981. };
  3982. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  3983. NK_API void nk_str_init_default(struct nk_str*);
  3984. #endif
  3985. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  3986. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  3987. NK_API void nk_str_clear(struct nk_str*);
  3988. NK_API void nk_str_free(struct nk_str*);
  3989. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  3990. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  3991. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  3992. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  3993. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  3994. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  3995. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  3996. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  3997. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  3998. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  3999. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  4000. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  4001. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  4002. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  4003. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  4004. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  4005. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  4006. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  4007. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  4008. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  4009. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  4010. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  4011. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  4012. NK_API char *nk_str_get(struct nk_str*);
  4013. NK_API const char *nk_str_get_const(const struct nk_str*);
  4014. NK_API int nk_str_len(struct nk_str*);
  4015. NK_API int nk_str_len_char(struct nk_str*);
  4016. /*===============================================================
  4017. *
  4018. * TEXT EDITOR
  4019. *
  4020. * ===============================================================*/
  4021. /* Editing text in this library is handled by either `nk_edit_string` or
  4022. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  4023. * ways of doing it and a balance between control and ease of use with memory
  4024. * as well as functionality controlled by flags.
  4025. *
  4026. * This library generally allows three different levels of memory control:
  4027. * First of is the most basic way of just providing a simple char array with
  4028. * string length. This method is probably the easiest way of handling simple
  4029. * user text input. Main upside is complete control over memory while the biggest
  4030. * downside in comparison with the other two approaches is missing undo/redo.
  4031. *
  4032. * For UIs that require undo/redo the second way was created. It is based on
  4033. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  4034. * This is mainly useful if you want something more like a text editor but don't want
  4035. * to have a dynamically growing buffer.
  4036. *
  4037. * The final way is using a dynamically growing nk_text_edit struct, which
  4038. * has both a default version if you don't care where memory comes from and an
  4039. * allocator version if you do. While the text editor is quite powerful for its
  4040. * complexity I would not recommend editing gigabytes of data with it.
  4041. * It is rather designed for uses cases which make sense for a GUI library not for
  4042. * an full blown text editor.
  4043. */
  4044. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  4045. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  4046. #endif
  4047. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  4048. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  4049. #endif
  4050. struct nk_text_edit;
  4051. struct nk_clipboard {
  4052. nk_handle userdata;
  4053. nk_plugin_paste paste;
  4054. nk_plugin_copy copy;
  4055. };
  4056. struct nk_text_undo_record {
  4057. int where;
  4058. short insert_length;
  4059. short delete_length;
  4060. short char_storage;
  4061. };
  4062. struct nk_text_undo_state {
  4063. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  4064. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  4065. short undo_point;
  4066. short redo_point;
  4067. short undo_char_point;
  4068. short redo_char_point;
  4069. };
  4070. enum nk_text_edit_type {
  4071. NK_TEXT_EDIT_SINGLE_LINE,
  4072. NK_TEXT_EDIT_MULTI_LINE
  4073. };
  4074. enum nk_text_edit_mode {
  4075. NK_TEXT_EDIT_MODE_VIEW,
  4076. NK_TEXT_EDIT_MODE_INSERT,
  4077. NK_TEXT_EDIT_MODE_REPLACE
  4078. };
  4079. struct nk_text_edit {
  4080. struct nk_clipboard clip;
  4081. struct nk_str string;
  4082. nk_plugin_filter filter;
  4083. struct nk_vec2 scrollbar;
  4084. int cursor;
  4085. int select_start;
  4086. int select_end;
  4087. unsigned char mode;
  4088. unsigned char cursor_at_end_of_line;
  4089. unsigned char initialized;
  4090. unsigned char has_preferred_x;
  4091. unsigned char single_line;
  4092. unsigned char active;
  4093. unsigned char padding1;
  4094. float preferred_x;
  4095. struct nk_text_undo_state undo;
  4096. };
  4097. /* filter function */
  4098. NK_API int nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  4099. NK_API int nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  4100. NK_API int nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  4101. NK_API int nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  4102. NK_API int nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  4103. NK_API int nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  4104. NK_API int nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  4105. /* text editor */
  4106. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4107. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  4108. #endif
  4109. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  4110. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  4111. NK_API void nk_textedit_free(struct nk_text_edit*);
  4112. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  4113. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  4114. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  4115. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  4116. NK_API int nk_textedit_cut(struct nk_text_edit*);
  4117. NK_API int nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  4118. NK_API void nk_textedit_undo(struct nk_text_edit*);
  4119. NK_API void nk_textedit_redo(struct nk_text_edit*);
  4120. /* ===============================================================
  4121. *
  4122. * DRAWING
  4123. *
  4124. * ===============================================================*/
  4125. /* This library was designed to be render backend agnostic so it does
  4126. not draw anything to screen. Instead all drawn shapes, widgets
  4127. are made of, are buffered into memory and make up a command queue.
  4128. Each frame therefore fills the command buffer with draw commands
  4129. that then need to be executed by the user and his own render backend.
  4130. After that the command buffer needs to be cleared and a new frame can be
  4131. started. It is probably important to note that the command buffer is the main
  4132. drawing API and the optional vertex buffer API only takes this format and
  4133. converts it into a hardware accessible format.
  4134. To use the command queue to draw your own widgets you can access the
  4135. command buffer of each window by calling `nk_window_get_canvas` after
  4136. previously having called `nk_begin`:
  4137. void draw_red_rectangle_widget(struct nk_context *ctx)
  4138. {
  4139. struct nk_command_buffer *canvas;
  4140. struct nk_input *input = &ctx->input;
  4141. canvas = nk_window_get_canvas(ctx);
  4142. struct nk_rect space;
  4143. enum nk_widget_layout_states state;
  4144. state = nk_widget(&space, ctx);
  4145. if (!state) return;
  4146. if (state != NK_WIDGET_ROM)
  4147. update_your_widget_by_user_input(...);
  4148. nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  4149. }
  4150. if (nk_begin(...)) {
  4151. nk_layout_row_dynamic(ctx, 25, 1);
  4152. draw_red_rectangle_widget(ctx);
  4153. }
  4154. nk_end(..)
  4155. Important to know if you want to create your own widgets is the `nk_widget`
  4156. call. It allocates space on the panel reserved for this widget to be used,
  4157. but also returns the state of the widget space. If your widget is not seen and does
  4158. not have to be updated it is '0' and you can just return. If it only has
  4159. to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  4160. update and draw your widget. The reason for separating is to only draw and
  4161. update what is actually necessary which is crucial for performance.
  4162. */
  4163. enum nk_command_type {
  4164. NK_COMMAND_NOP,
  4165. NK_COMMAND_SCISSOR,
  4166. NK_COMMAND_LINE,
  4167. NK_COMMAND_CURVE,
  4168. NK_COMMAND_RECT,
  4169. NK_COMMAND_RECT_FILLED,
  4170. NK_COMMAND_RECT_MULTI_COLOR,
  4171. NK_COMMAND_CIRCLE,
  4172. NK_COMMAND_CIRCLE_FILLED,
  4173. NK_COMMAND_ARC,
  4174. NK_COMMAND_ARC_FILLED,
  4175. NK_COMMAND_TRIANGLE,
  4176. NK_COMMAND_TRIANGLE_FILLED,
  4177. NK_COMMAND_POLYGON,
  4178. NK_COMMAND_POLYGON_FILLED,
  4179. NK_COMMAND_POLYLINE,
  4180. NK_COMMAND_TEXT,
  4181. NK_COMMAND_IMAGE,
  4182. NK_COMMAND_CUSTOM
  4183. };
  4184. /* command base and header of every command inside the buffer */
  4185. struct nk_command {
  4186. enum nk_command_type type;
  4187. nk_size next;
  4188. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4189. nk_handle userdata;
  4190. #endif
  4191. };
  4192. struct nk_command_scissor {
  4193. struct nk_command header;
  4194. short x, y;
  4195. unsigned short w, h;
  4196. };
  4197. struct nk_command_line {
  4198. struct nk_command header;
  4199. unsigned short line_thickness;
  4200. struct nk_vec2i begin;
  4201. struct nk_vec2i end;
  4202. struct nk_color color;
  4203. };
  4204. struct nk_command_curve {
  4205. struct nk_command header;
  4206. unsigned short line_thickness;
  4207. struct nk_vec2i begin;
  4208. struct nk_vec2i end;
  4209. struct nk_vec2i ctrl[2];
  4210. struct nk_color color;
  4211. };
  4212. struct nk_command_rect {
  4213. struct nk_command header;
  4214. unsigned short rounding;
  4215. unsigned short line_thickness;
  4216. short x, y;
  4217. unsigned short w, h;
  4218. struct nk_color color;
  4219. };
  4220. struct nk_command_rect_filled {
  4221. struct nk_command header;
  4222. unsigned short rounding;
  4223. short x, y;
  4224. unsigned short w, h;
  4225. struct nk_color color;
  4226. };
  4227. struct nk_command_rect_multi_color {
  4228. struct nk_command header;
  4229. short x, y;
  4230. unsigned short w, h;
  4231. struct nk_color left;
  4232. struct nk_color top;
  4233. struct nk_color bottom;
  4234. struct nk_color right;
  4235. };
  4236. struct nk_command_triangle {
  4237. struct nk_command header;
  4238. unsigned short line_thickness;
  4239. struct nk_vec2i a;
  4240. struct nk_vec2i b;
  4241. struct nk_vec2i c;
  4242. struct nk_color color;
  4243. };
  4244. struct nk_command_triangle_filled {
  4245. struct nk_command header;
  4246. struct nk_vec2i a;
  4247. struct nk_vec2i b;
  4248. struct nk_vec2i c;
  4249. struct nk_color color;
  4250. };
  4251. struct nk_command_circle {
  4252. struct nk_command header;
  4253. short x, y;
  4254. unsigned short line_thickness;
  4255. unsigned short w, h;
  4256. struct nk_color color;
  4257. };
  4258. struct nk_command_circle_filled {
  4259. struct nk_command header;
  4260. short x, y;
  4261. unsigned short w, h;
  4262. struct nk_color color;
  4263. };
  4264. struct nk_command_arc {
  4265. struct nk_command header;
  4266. short cx, cy;
  4267. unsigned short r;
  4268. unsigned short line_thickness;
  4269. float a[2];
  4270. struct nk_color color;
  4271. };
  4272. struct nk_command_arc_filled {
  4273. struct nk_command header;
  4274. short cx, cy;
  4275. unsigned short r;
  4276. float a[2];
  4277. struct nk_color color;
  4278. };
  4279. struct nk_command_polygon {
  4280. struct nk_command header;
  4281. struct nk_color color;
  4282. unsigned short line_thickness;
  4283. unsigned short point_count;
  4284. struct nk_vec2i points[1];
  4285. };
  4286. struct nk_command_polygon_filled {
  4287. struct nk_command header;
  4288. struct nk_color color;
  4289. unsigned short point_count;
  4290. struct nk_vec2i points[1];
  4291. };
  4292. struct nk_command_polyline {
  4293. struct nk_command header;
  4294. struct nk_color color;
  4295. unsigned short line_thickness;
  4296. unsigned short point_count;
  4297. struct nk_vec2i points[1];
  4298. };
  4299. struct nk_command_image {
  4300. struct nk_command header;
  4301. short x, y;
  4302. unsigned short w, h;
  4303. struct nk_image img;
  4304. struct nk_color col;
  4305. };
  4306. typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
  4307. unsigned short w, unsigned short h, nk_handle callback_data);
  4308. struct nk_command_custom {
  4309. struct nk_command header;
  4310. short x, y;
  4311. unsigned short w, h;
  4312. nk_handle callback_data;
  4313. nk_command_custom_callback callback;
  4314. };
  4315. struct nk_command_text {
  4316. struct nk_command header;
  4317. const struct nk_user_font *font;
  4318. struct nk_color background;
  4319. struct nk_color foreground;
  4320. short x, y;
  4321. unsigned short w, h;
  4322. float height;
  4323. int length;
  4324. char string[1];
  4325. };
  4326. enum nk_command_clipping {
  4327. NK_CLIPPING_OFF = nk_false,
  4328. NK_CLIPPING_ON = nk_true
  4329. };
  4330. struct nk_command_buffer {
  4331. struct nk_buffer *base;
  4332. struct nk_rect clip;
  4333. int use_clipping;
  4334. nk_handle userdata;
  4335. nk_size begin, end, last;
  4336. };
  4337. /* shape outlines */
  4338. 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);
  4339. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  4340. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  4341. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  4342. 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);
  4343. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  4344. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float *points, int point_count, float line_thickness, struct nk_color col);
  4345. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  4346. /* filled shades */
  4347. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  4348. 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);
  4349. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  4350. 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);
  4351. 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);
  4352. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  4353. /* misc */
  4354. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  4355. 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);
  4356. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  4357. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  4358. /* ===============================================================
  4359. *
  4360. * INPUT
  4361. *
  4362. * ===============================================================*/
  4363. struct nk_mouse_button {
  4364. int down;
  4365. unsigned int clicked;
  4366. struct nk_vec2 clicked_pos;
  4367. };
  4368. struct nk_mouse {
  4369. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  4370. struct nk_vec2 pos;
  4371. struct nk_vec2 prev;
  4372. struct nk_vec2 delta;
  4373. struct nk_vec2 scroll_delta;
  4374. unsigned char grab;
  4375. unsigned char grabbed;
  4376. unsigned char ungrab;
  4377. };
  4378. struct nk_key {
  4379. int down;
  4380. unsigned int clicked;
  4381. };
  4382. struct nk_keyboard {
  4383. struct nk_key keys[NK_KEY_MAX];
  4384. char text[NK_INPUT_MAX];
  4385. int text_len;
  4386. };
  4387. struct nk_input {
  4388. struct nk_keyboard keyboard;
  4389. struct nk_mouse mouse;
  4390. };
  4391. NK_API int nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  4392. NK_API int nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4393. NK_API int nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, int down);
  4394. NK_API int nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4395. 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);
  4396. NK_API int nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  4397. NK_API int nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  4398. NK_API int nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  4399. NK_API int nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4400. NK_API int nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  4401. NK_API int nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  4402. NK_API int nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  4403. NK_API int nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  4404. NK_API int nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  4405. NK_API int nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  4406. /* ===============================================================
  4407. *
  4408. * DRAW LIST
  4409. *
  4410. * ===============================================================*/
  4411. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4412. /* The optional vertex buffer draw list provides a 2D drawing context
  4413. with antialiasing functionality which takes basic filled or outlined shapes
  4414. or a path and outputs vertexes, elements and draw commands.
  4415. The actual draw list API is not required to be used directly while using this
  4416. library since converting the default library draw command output is done by
  4417. just calling `nk_convert` but I decided to still make this library accessible
  4418. since it can be useful.
  4419. The draw list is based on a path buffering and polygon and polyline
  4420. rendering API which allows a lot of ways to draw 2D content to screen.
  4421. In fact it is probably more powerful than needed but allows even more crazy
  4422. things than this library provides by default.
  4423. */
  4424. typedef nk_ushort nk_draw_index;
  4425. enum nk_draw_list_stroke {
  4426. NK_STROKE_OPEN = nk_false,
  4427. /* build up path has no connection back to the beginning */
  4428. NK_STROKE_CLOSED = nk_true
  4429. /* build up path has a connection back to the beginning */
  4430. };
  4431. enum nk_draw_vertex_layout_attribute {
  4432. NK_VERTEX_POSITION,
  4433. NK_VERTEX_COLOR,
  4434. NK_VERTEX_TEXCOORD,
  4435. NK_VERTEX_ATTRIBUTE_COUNT
  4436. };
  4437. enum nk_draw_vertex_layout_format {
  4438. NK_FORMAT_SCHAR,
  4439. NK_FORMAT_SSHORT,
  4440. NK_FORMAT_SINT,
  4441. NK_FORMAT_UCHAR,
  4442. NK_FORMAT_USHORT,
  4443. NK_FORMAT_UINT,
  4444. NK_FORMAT_FLOAT,
  4445. NK_FORMAT_DOUBLE,
  4446. NK_FORMAT_COLOR_BEGIN,
  4447. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  4448. NK_FORMAT_R16G15B16,
  4449. NK_FORMAT_R32G32B32,
  4450. NK_FORMAT_R8G8B8A8,
  4451. NK_FORMAT_B8G8R8A8,
  4452. NK_FORMAT_R16G15B16A16,
  4453. NK_FORMAT_R32G32B32A32,
  4454. NK_FORMAT_R32G32B32A32_FLOAT,
  4455. NK_FORMAT_R32G32B32A32_DOUBLE,
  4456. NK_FORMAT_RGB32,
  4457. NK_FORMAT_RGBA32,
  4458. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  4459. NK_FORMAT_COUNT
  4460. };
  4461. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  4462. struct nk_draw_vertex_layout_element {
  4463. enum nk_draw_vertex_layout_attribute attribute;
  4464. enum nk_draw_vertex_layout_format format;
  4465. nk_size offset;
  4466. };
  4467. struct nk_draw_command {
  4468. unsigned int elem_count;
  4469. /* number of elements in the current draw batch */
  4470. struct nk_rect clip_rect;
  4471. /* current screen clipping rectangle */
  4472. nk_handle texture;
  4473. /* current texture to set */
  4474. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4475. nk_handle userdata;
  4476. #endif
  4477. };
  4478. struct nk_draw_list {
  4479. struct nk_rect clip_rect;
  4480. struct nk_vec2 circle_vtx[12];
  4481. struct nk_convert_config config;
  4482. struct nk_buffer *buffer;
  4483. struct nk_buffer *vertices;
  4484. struct nk_buffer *elements;
  4485. unsigned int element_count;
  4486. unsigned int vertex_count;
  4487. unsigned int cmd_count;
  4488. nk_size cmd_offset;
  4489. unsigned int path_count;
  4490. unsigned int path_offset;
  4491. enum nk_anti_aliasing line_AA;
  4492. enum nk_anti_aliasing shape_AA;
  4493. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4494. nk_handle userdata;
  4495. #endif
  4496. };
  4497. /* draw list */
  4498. NK_API void nk_draw_list_init(struct nk_draw_list*);
  4499. 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);
  4500. NK_API void nk_draw_list_clear(struct nk_draw_list*);
  4501. /* drawing */
  4502. #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))
  4503. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  4504. 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*);
  4505. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  4506. /* path */
  4507. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  4508. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  4509. 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);
  4510. 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);
  4511. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  4512. 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);
  4513. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  4514. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  4515. /* stroke */
  4516. 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);
  4517. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  4518. 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);
  4519. 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);
  4520. 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);
  4521. 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);
  4522. /* fill */
  4523. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  4524. 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);
  4525. 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);
  4526. 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);
  4527. 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);
  4528. /* misc */
  4529. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  4530. 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);
  4531. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4532. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  4533. #endif
  4534. #endif
  4535. /* ===============================================================
  4536. *
  4537. * GUI
  4538. *
  4539. * ===============================================================*/
  4540. enum nk_style_item_type {
  4541. NK_STYLE_ITEM_COLOR,
  4542. NK_STYLE_ITEM_IMAGE
  4543. };
  4544. union nk_style_item_data {
  4545. struct nk_image image;
  4546. struct nk_color color;
  4547. };
  4548. struct nk_style_item {
  4549. enum nk_style_item_type type;
  4550. union nk_style_item_data data;
  4551. };
  4552. struct nk_style_text {
  4553. struct nk_color color;
  4554. struct nk_vec2 padding;
  4555. };
  4556. struct nk_style_button {
  4557. /* background */
  4558. struct nk_style_item normal;
  4559. struct nk_style_item hover;
  4560. struct nk_style_item active;
  4561. struct nk_color border_color;
  4562. /* text */
  4563. struct nk_color text_background;
  4564. struct nk_color text_normal;
  4565. struct nk_color text_hover;
  4566. struct nk_color text_active;
  4567. nk_flags text_alignment;
  4568. /* properties */
  4569. float border;
  4570. float rounding;
  4571. struct nk_vec2 padding;
  4572. struct nk_vec2 image_padding;
  4573. struct nk_vec2 touch_padding;
  4574. /* optional user callbacks */
  4575. nk_handle userdata;
  4576. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  4577. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  4578. };
  4579. struct nk_style_toggle {
  4580. /* background */
  4581. struct nk_style_item normal;
  4582. struct nk_style_item hover;
  4583. struct nk_style_item active;
  4584. struct nk_color border_color;
  4585. /* cursor */
  4586. struct nk_style_item cursor_normal;
  4587. struct nk_style_item cursor_hover;
  4588. /* text */
  4589. struct nk_color text_normal;
  4590. struct nk_color text_hover;
  4591. struct nk_color text_active;
  4592. struct nk_color text_background;
  4593. nk_flags text_alignment;
  4594. /* properties */
  4595. struct nk_vec2 padding;
  4596. struct nk_vec2 touch_padding;
  4597. float spacing;
  4598. float border;
  4599. /* optional user callbacks */
  4600. nk_handle userdata;
  4601. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4602. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4603. };
  4604. struct nk_style_selectable {
  4605. /* background (inactive) */
  4606. struct nk_style_item normal;
  4607. struct nk_style_item hover;
  4608. struct nk_style_item pressed;
  4609. /* background (active) */
  4610. struct nk_style_item normal_active;
  4611. struct nk_style_item hover_active;
  4612. struct nk_style_item pressed_active;
  4613. /* text color (inactive) */
  4614. struct nk_color text_normal;
  4615. struct nk_color text_hover;
  4616. struct nk_color text_pressed;
  4617. /* text color (active) */
  4618. struct nk_color text_normal_active;
  4619. struct nk_color text_hover_active;
  4620. struct nk_color text_pressed_active;
  4621. struct nk_color text_background;
  4622. nk_flags text_alignment;
  4623. /* properties */
  4624. float rounding;
  4625. struct nk_vec2 padding;
  4626. struct nk_vec2 touch_padding;
  4627. struct nk_vec2 image_padding;
  4628. /* optional user callbacks */
  4629. nk_handle userdata;
  4630. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4631. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4632. };
  4633. struct nk_style_slider {
  4634. /* background */
  4635. struct nk_style_item normal;
  4636. struct nk_style_item hover;
  4637. struct nk_style_item active;
  4638. struct nk_color border_color;
  4639. /* background bar */
  4640. struct nk_color bar_normal;
  4641. struct nk_color bar_hover;
  4642. struct nk_color bar_active;
  4643. struct nk_color bar_filled;
  4644. /* cursor */
  4645. struct nk_style_item cursor_normal;
  4646. struct nk_style_item cursor_hover;
  4647. struct nk_style_item cursor_active;
  4648. /* properties */
  4649. float border;
  4650. float rounding;
  4651. float bar_height;
  4652. struct nk_vec2 padding;
  4653. struct nk_vec2 spacing;
  4654. struct nk_vec2 cursor_size;
  4655. /* optional buttons */
  4656. int show_buttons;
  4657. struct nk_style_button inc_button;
  4658. struct nk_style_button dec_button;
  4659. enum nk_symbol_type inc_symbol;
  4660. enum nk_symbol_type dec_symbol;
  4661. /* optional user callbacks */
  4662. nk_handle userdata;
  4663. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4664. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4665. };
  4666. struct nk_style_progress {
  4667. /* background */
  4668. struct nk_style_item normal;
  4669. struct nk_style_item hover;
  4670. struct nk_style_item active;
  4671. struct nk_color border_color;
  4672. /* cursor */
  4673. struct nk_style_item cursor_normal;
  4674. struct nk_style_item cursor_hover;
  4675. struct nk_style_item cursor_active;
  4676. struct nk_color cursor_border_color;
  4677. /* properties */
  4678. float rounding;
  4679. float border;
  4680. float cursor_border;
  4681. float cursor_rounding;
  4682. struct nk_vec2 padding;
  4683. /* optional user callbacks */
  4684. nk_handle userdata;
  4685. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4686. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4687. };
  4688. struct nk_style_scrollbar {
  4689. /* background */
  4690. struct nk_style_item normal;
  4691. struct nk_style_item hover;
  4692. struct nk_style_item active;
  4693. struct nk_color border_color;
  4694. /* cursor */
  4695. struct nk_style_item cursor_normal;
  4696. struct nk_style_item cursor_hover;
  4697. struct nk_style_item cursor_active;
  4698. struct nk_color cursor_border_color;
  4699. /* properties */
  4700. float border;
  4701. float rounding;
  4702. float border_cursor;
  4703. float rounding_cursor;
  4704. struct nk_vec2 padding;
  4705. /* optional buttons */
  4706. int show_buttons;
  4707. struct nk_style_button inc_button;
  4708. struct nk_style_button dec_button;
  4709. enum nk_symbol_type inc_symbol;
  4710. enum nk_symbol_type dec_symbol;
  4711. /* optional user callbacks */
  4712. nk_handle userdata;
  4713. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4714. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4715. };
  4716. struct nk_style_edit {
  4717. /* background */
  4718. struct nk_style_item normal;
  4719. struct nk_style_item hover;
  4720. struct nk_style_item active;
  4721. struct nk_color border_color;
  4722. struct nk_style_scrollbar scrollbar;
  4723. /* cursor */
  4724. struct nk_color cursor_normal;
  4725. struct nk_color cursor_hover;
  4726. struct nk_color cursor_text_normal;
  4727. struct nk_color cursor_text_hover;
  4728. /* text (unselected) */
  4729. struct nk_color text_normal;
  4730. struct nk_color text_hover;
  4731. struct nk_color text_active;
  4732. /* text (selected) */
  4733. struct nk_color selected_normal;
  4734. struct nk_color selected_hover;
  4735. struct nk_color selected_text_normal;
  4736. struct nk_color selected_text_hover;
  4737. /* properties */
  4738. float border;
  4739. float rounding;
  4740. float cursor_size;
  4741. struct nk_vec2 scrollbar_size;
  4742. struct nk_vec2 padding;
  4743. float row_padding;
  4744. };
  4745. struct nk_style_property {
  4746. /* background */
  4747. struct nk_style_item normal;
  4748. struct nk_style_item hover;
  4749. struct nk_style_item active;
  4750. struct nk_color border_color;
  4751. /* text */
  4752. struct nk_color label_normal;
  4753. struct nk_color label_hover;
  4754. struct nk_color label_active;
  4755. /* symbols */
  4756. enum nk_symbol_type sym_left;
  4757. enum nk_symbol_type sym_right;
  4758. /* properties */
  4759. float border;
  4760. float rounding;
  4761. struct nk_vec2 padding;
  4762. struct nk_style_edit edit;
  4763. struct nk_style_button inc_button;
  4764. struct nk_style_button dec_button;
  4765. /* optional user callbacks */
  4766. nk_handle userdata;
  4767. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4768. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4769. };
  4770. struct nk_style_chart {
  4771. /* colors */
  4772. struct nk_style_item background;
  4773. struct nk_color border_color;
  4774. struct nk_color selected_color;
  4775. struct nk_color color;
  4776. /* properties */
  4777. float border;
  4778. float rounding;
  4779. struct nk_vec2 padding;
  4780. };
  4781. struct nk_style_combo {
  4782. /* background */
  4783. struct nk_style_item normal;
  4784. struct nk_style_item hover;
  4785. struct nk_style_item active;
  4786. struct nk_color border_color;
  4787. /* label */
  4788. struct nk_color label_normal;
  4789. struct nk_color label_hover;
  4790. struct nk_color label_active;
  4791. /* symbol */
  4792. struct nk_color symbol_normal;
  4793. struct nk_color symbol_hover;
  4794. struct nk_color symbol_active;
  4795. /* button */
  4796. struct nk_style_button button;
  4797. enum nk_symbol_type sym_normal;
  4798. enum nk_symbol_type sym_hover;
  4799. enum nk_symbol_type sym_active;
  4800. /* properties */
  4801. float border;
  4802. float rounding;
  4803. struct nk_vec2 content_padding;
  4804. struct nk_vec2 button_padding;
  4805. struct nk_vec2 spacing;
  4806. };
  4807. struct nk_style_tab {
  4808. /* background */
  4809. struct nk_style_item background;
  4810. struct nk_color border_color;
  4811. struct nk_color text;
  4812. /* button */
  4813. struct nk_style_button tab_maximize_button;
  4814. struct nk_style_button tab_minimize_button;
  4815. struct nk_style_button node_maximize_button;
  4816. struct nk_style_button node_minimize_button;
  4817. enum nk_symbol_type sym_minimize;
  4818. enum nk_symbol_type sym_maximize;
  4819. /* properties */
  4820. float border;
  4821. float rounding;
  4822. float indent;
  4823. struct nk_vec2 padding;
  4824. struct nk_vec2 spacing;
  4825. };
  4826. enum nk_style_header_align {
  4827. NK_HEADER_LEFT,
  4828. NK_HEADER_RIGHT
  4829. };
  4830. struct nk_style_window_header {
  4831. /* background */
  4832. struct nk_style_item normal;
  4833. struct nk_style_item hover;
  4834. struct nk_style_item active;
  4835. /* button */
  4836. struct nk_style_button close_button;
  4837. struct nk_style_button minimize_button;
  4838. enum nk_symbol_type close_symbol;
  4839. enum nk_symbol_type minimize_symbol;
  4840. enum nk_symbol_type maximize_symbol;
  4841. /* title */
  4842. struct nk_color label_normal;
  4843. struct nk_color label_hover;
  4844. struct nk_color label_active;
  4845. /* properties */
  4846. enum nk_style_header_align align;
  4847. struct nk_vec2 padding;
  4848. struct nk_vec2 label_padding;
  4849. struct nk_vec2 spacing;
  4850. };
  4851. struct nk_style_window {
  4852. struct nk_style_window_header header;
  4853. struct nk_style_item fixed_background;
  4854. struct nk_color background;
  4855. struct nk_color border_color;
  4856. struct nk_color popup_border_color;
  4857. struct nk_color combo_border_color;
  4858. struct nk_color contextual_border_color;
  4859. struct nk_color menu_border_color;
  4860. struct nk_color group_border_color;
  4861. struct nk_color tooltip_border_color;
  4862. struct nk_style_item scaler;
  4863. float border;
  4864. float combo_border;
  4865. float contextual_border;
  4866. float menu_border;
  4867. float group_border;
  4868. float tooltip_border;
  4869. float popup_border;
  4870. float min_row_height_padding;
  4871. float rounding;
  4872. struct nk_vec2 spacing;
  4873. struct nk_vec2 scrollbar_size;
  4874. struct nk_vec2 min_size;
  4875. struct nk_vec2 padding;
  4876. struct nk_vec2 group_padding;
  4877. struct nk_vec2 popup_padding;
  4878. struct nk_vec2 combo_padding;
  4879. struct nk_vec2 contextual_padding;
  4880. struct nk_vec2 menu_padding;
  4881. struct nk_vec2 tooltip_padding;
  4882. };
  4883. struct nk_style {
  4884. const struct nk_user_font *font;
  4885. const struct nk_cursor *cursors[NK_CURSOR_COUNT];
  4886. const struct nk_cursor *cursor_active;
  4887. struct nk_cursor *cursor_last;
  4888. int cursor_visible;
  4889. struct nk_style_text text;
  4890. struct nk_style_button button;
  4891. struct nk_style_button contextual_button;
  4892. struct nk_style_button menu_button;
  4893. struct nk_style_toggle option;
  4894. struct nk_style_toggle checkbox;
  4895. struct nk_style_selectable selectable;
  4896. struct nk_style_slider slider;
  4897. struct nk_style_progress progress;
  4898. struct nk_style_property property;
  4899. struct nk_style_edit edit;
  4900. struct nk_style_chart chart;
  4901. struct nk_style_scrollbar scrollh;
  4902. struct nk_style_scrollbar scrollv;
  4903. struct nk_style_tab tab;
  4904. struct nk_style_combo combo;
  4905. struct nk_style_window window;
  4906. };
  4907. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  4908. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  4909. NK_API struct nk_style_item nk_style_item_hide(void);
  4910. /*==============================================================
  4911. * PANEL
  4912. * =============================================================*/
  4913. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  4914. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  4915. #endif
  4916. #ifndef NK_CHART_MAX_SLOT
  4917. #define NK_CHART_MAX_SLOT 4
  4918. #endif
  4919. enum nk_panel_type {
  4920. NK_PANEL_WINDOW = NK_FLAG(0),
  4921. NK_PANEL_GROUP = NK_FLAG(1),
  4922. NK_PANEL_POPUP = NK_FLAG(2),
  4923. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  4924. NK_PANEL_COMBO = NK_FLAG(5),
  4925. NK_PANEL_MENU = NK_FLAG(6),
  4926. NK_PANEL_TOOLTIP = NK_FLAG(7)
  4927. };
  4928. enum nk_panel_set {
  4929. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
  4930. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
  4931. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
  4932. };
  4933. struct nk_chart_slot {
  4934. enum nk_chart_type type;
  4935. struct nk_color color;
  4936. struct nk_color highlight;
  4937. float min, max, range;
  4938. int count;
  4939. struct nk_vec2 last;
  4940. int index;
  4941. };
  4942. struct nk_chart {
  4943. int slot;
  4944. float x, y, w, h;
  4945. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  4946. };
  4947. enum nk_panel_row_layout_type {
  4948. NK_LAYOUT_DYNAMIC_FIXED = 0,
  4949. NK_LAYOUT_DYNAMIC_ROW,
  4950. NK_LAYOUT_DYNAMIC_FREE,
  4951. NK_LAYOUT_DYNAMIC,
  4952. NK_LAYOUT_STATIC_FIXED,
  4953. NK_LAYOUT_STATIC_ROW,
  4954. NK_LAYOUT_STATIC_FREE,
  4955. NK_LAYOUT_STATIC,
  4956. NK_LAYOUT_TEMPLATE,
  4957. NK_LAYOUT_COUNT
  4958. };
  4959. struct nk_row_layout {
  4960. enum nk_panel_row_layout_type type;
  4961. int index;
  4962. float height;
  4963. float min_height;
  4964. int columns;
  4965. const float *ratio;
  4966. float item_width;
  4967. float item_height;
  4968. float item_offset;
  4969. float filled;
  4970. struct nk_rect item;
  4971. int tree_depth;
  4972. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  4973. };
  4974. struct nk_popup_buffer {
  4975. nk_size begin;
  4976. nk_size parent;
  4977. nk_size last;
  4978. nk_size end;
  4979. int active;
  4980. };
  4981. struct nk_menu_state {
  4982. float x, y, w, h;
  4983. struct nk_scroll offset;
  4984. };
  4985. struct nk_panel {
  4986. enum nk_panel_type type;
  4987. nk_flags flags;
  4988. struct nk_rect bounds;
  4989. nk_uint *offset_x;
  4990. nk_uint *offset_y;
  4991. float at_x, at_y, max_x;
  4992. float footer_height;
  4993. float header_height;
  4994. float border;
  4995. unsigned int has_scrolling;
  4996. struct nk_rect clip;
  4997. struct nk_menu_state menu;
  4998. struct nk_row_layout row;
  4999. struct nk_chart chart;
  5000. struct nk_command_buffer *buffer;
  5001. struct nk_panel *parent;
  5002. };
  5003. /*==============================================================
  5004. * WINDOW
  5005. * =============================================================*/
  5006. #ifndef NK_WINDOW_MAX_NAME
  5007. #define NK_WINDOW_MAX_NAME 64
  5008. #endif
  5009. struct nk_table;
  5010. enum nk_window_flags {
  5011. NK_WINDOW_PRIVATE = NK_FLAG(11),
  5012. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
  5013. /* special window type growing up in height while being filled to a certain maximum height */
  5014. NK_WINDOW_ROM = NK_FLAG(12),
  5015. /* sets window widgets into a read only mode and does not allow input changes */
  5016. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT,
  5017. /* prevents all interaction caused by input to either window or widgets inside */
  5018. NK_WINDOW_HIDDEN = NK_FLAG(13),
  5019. /* Hides window and stops any window interaction and drawing */
  5020. NK_WINDOW_CLOSED = NK_FLAG(14),
  5021. /* Directly closes and frees the window at the end of the frame */
  5022. NK_WINDOW_MINIMIZED = NK_FLAG(15),
  5023. /* marks the window as minimized */
  5024. NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
  5025. /* Removes read only mode at the end of the window */
  5026. };
  5027. struct nk_popup_state {
  5028. struct nk_window *win;
  5029. enum nk_panel_type type;
  5030. struct nk_popup_buffer buf;
  5031. nk_hash name;
  5032. int active;
  5033. unsigned combo_count;
  5034. unsigned con_count, con_old;
  5035. unsigned active_con;
  5036. struct nk_rect header;
  5037. };
  5038. struct nk_edit_state {
  5039. nk_hash name;
  5040. unsigned int seq;
  5041. unsigned int old;
  5042. int active, prev;
  5043. int cursor;
  5044. int sel_start;
  5045. int sel_end;
  5046. struct nk_scroll scrollbar;
  5047. unsigned char mode;
  5048. unsigned char single_line;
  5049. };
  5050. struct nk_property_state {
  5051. int active, prev;
  5052. char buffer[NK_MAX_NUMBER_BUFFER];
  5053. int length;
  5054. int cursor;
  5055. int select_start;
  5056. int select_end;
  5057. nk_hash name;
  5058. unsigned int seq;
  5059. unsigned int old;
  5060. int state;
  5061. };
  5062. struct nk_window {
  5063. unsigned int seq;
  5064. nk_hash name;
  5065. char name_string[NK_WINDOW_MAX_NAME];
  5066. nk_flags flags;
  5067. struct nk_rect bounds;
  5068. struct nk_scroll scrollbar;
  5069. struct nk_command_buffer buffer;
  5070. struct nk_panel *layout;
  5071. float scrollbar_hiding_timer;
  5072. /* persistent widget state */
  5073. struct nk_property_state property;
  5074. struct nk_popup_state popup;
  5075. struct nk_edit_state edit;
  5076. unsigned int scrolled;
  5077. struct nk_table *tables;
  5078. unsigned int table_count;
  5079. /* window list hooks */
  5080. struct nk_window *next;
  5081. struct nk_window *prev;
  5082. struct nk_window *parent;
  5083. };
  5084. /*==============================================================
  5085. * STACK
  5086. * =============================================================*/
  5087. /* The style modifier stack can be used to temporarily change a
  5088. * property inside `nk_style`. For example if you want a special
  5089. * red button you can temporarily push the old button color onto a stack
  5090. * draw the button with a red color and then you just pop the old color
  5091. * back from the stack:
  5092. *
  5093. * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  5094. * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  5095. * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  5096. * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  5097. *
  5098. * nk_button(...);
  5099. *
  5100. * nk_style_pop_style_item(ctx);
  5101. * nk_style_pop_style_item(ctx);
  5102. * nk_style_pop_style_item(ctx);
  5103. * nk_style_pop_vec2(ctx);
  5104. *
  5105. * Nuklear has a stack for style_items, float properties, vector properties,
  5106. * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  5107. * which can be changed at compile time.
  5108. */
  5109. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  5110. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  5111. #endif
  5112. #ifndef NK_FONT_STACK_SIZE
  5113. #define NK_FONT_STACK_SIZE 8
  5114. #endif
  5115. #ifndef NK_STYLE_ITEM_STACK_SIZE
  5116. #define NK_STYLE_ITEM_STACK_SIZE 16
  5117. #endif
  5118. #ifndef NK_FLOAT_STACK_SIZE
  5119. #define NK_FLOAT_STACK_SIZE 32
  5120. #endif
  5121. #ifndef NK_VECTOR_STACK_SIZE
  5122. #define NK_VECTOR_STACK_SIZE 16
  5123. #endif
  5124. #ifndef NK_FLAGS_STACK_SIZE
  5125. #define NK_FLAGS_STACK_SIZE 32
  5126. #endif
  5127. #ifndef NK_COLOR_STACK_SIZE
  5128. #define NK_COLOR_STACK_SIZE 32
  5129. #endif
  5130. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  5131. struct nk_config_stack_##name##_element {\
  5132. prefix##_##type *address;\
  5133. prefix##_##type old_value;\
  5134. }
  5135. #define NK_CONFIG_STACK(type,size)\
  5136. struct nk_config_stack_##type {\
  5137. int head;\
  5138. struct nk_config_stack_##type##_element elements[size];\
  5139. }
  5140. #define nk_float float
  5141. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  5142. NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
  5143. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  5144. NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
  5145. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  5146. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  5147. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  5148. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  5149. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  5150. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  5151. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  5152. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  5153. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  5154. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  5155. struct nk_configuration_stacks {
  5156. struct nk_config_stack_style_item style_items;
  5157. struct nk_config_stack_float floats;
  5158. struct nk_config_stack_vec2 vectors;
  5159. struct nk_config_stack_flags flags;
  5160. struct nk_config_stack_color colors;
  5161. struct nk_config_stack_user_font fonts;
  5162. struct nk_config_stack_button_behavior button_behaviors;
  5163. };
  5164. /*==============================================================
  5165. * CONTEXT
  5166. * =============================================================*/
  5167. #define NK_VALUE_PAGE_CAPACITY \
  5168. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  5169. struct nk_table {
  5170. unsigned int seq;
  5171. unsigned int size;
  5172. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  5173. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  5174. struct nk_table *next, *prev;
  5175. };
  5176. union nk_page_data {
  5177. struct nk_table tbl;
  5178. struct nk_panel pan;
  5179. struct nk_window win;
  5180. };
  5181. struct nk_page_element {
  5182. union nk_page_data data;
  5183. struct nk_page_element *next;
  5184. struct nk_page_element *prev;
  5185. };
  5186. struct nk_page {
  5187. unsigned int size;
  5188. struct nk_page *next;
  5189. struct nk_page_element win[1];
  5190. };
  5191. struct nk_pool {
  5192. struct nk_allocator alloc;
  5193. enum nk_allocation_type type;
  5194. unsigned int page_count;
  5195. struct nk_page *pages;
  5196. struct nk_page_element *freelist;
  5197. unsigned capacity;
  5198. nk_size size;
  5199. nk_size cap;
  5200. };
  5201. struct nk_context {
  5202. /* public: can be accessed freely */
  5203. struct nk_input input;
  5204. struct nk_style style;
  5205. struct nk_buffer memory;
  5206. struct nk_clipboard clip;
  5207. nk_flags last_widget_state;
  5208. enum nk_button_behavior button_behavior;
  5209. struct nk_configuration_stacks stacks;
  5210. float delta_time_seconds;
  5211. /* private:
  5212. should only be accessed if you
  5213. know what you are doing */
  5214. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  5215. struct nk_draw_list draw_list;
  5216. #endif
  5217. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5218. nk_handle userdata;
  5219. #endif
  5220. /* text editor objects are quite big because of an internal
  5221. * undo/redo stack. Therefore it does not make sense to have one for
  5222. * each window for temporary use cases, so I only provide *one* instance
  5223. * for all windows. This works because the content is cleared anyway */
  5224. struct nk_text_edit text_edit;
  5225. /* draw buffer used for overlay drawing operation like cursor */
  5226. struct nk_command_buffer overlay;
  5227. /* windows */
  5228. int build;
  5229. int use_pool;
  5230. struct nk_pool pool;
  5231. struct nk_window *begin;
  5232. struct nk_window *end;
  5233. struct nk_window *active;
  5234. struct nk_window *current;
  5235. struct nk_page_element *freelist;
  5236. unsigned int count;
  5237. unsigned int seq;
  5238. };
  5239. /* ==============================================================
  5240. * MATH
  5241. * =============================================================== */
  5242. #define NK_PI 3.141592654f
  5243. #define NK_UTF_INVALID 0xFFFD
  5244. #define NK_MAX_FLOAT_PRECISION 2
  5245. #define NK_UNUSED(x) ((void)(x))
  5246. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  5247. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  5248. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  5249. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  5250. #define NK_INBOX(px, py, x, y, w, h)\
  5251. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  5252. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  5253. (!(((x1 > (x0 + w0)) || ((x1 + w1) < x0) || (y1 > (y0 + h0)) || (y1 + h1) < y0)))
  5254. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  5255. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  5256. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  5257. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  5258. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  5259. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  5260. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  5261. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  5262. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  5263. /* ==============================================================
  5264. * ALIGNMENT
  5265. * =============================================================== */
  5266. /* Pointer to Integer type conversion for pointer alignment */
  5267. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  5268. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  5269. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  5270. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  5271. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  5272. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  5273. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  5274. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  5275. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  5276. #else /* generates warning but works */
  5277. # define NK_UINT_TO_PTR(x) ((void*)(x))
  5278. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  5279. #endif
  5280. #define NK_ALIGN_PTR(x, mask)\
  5281. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  5282. #define NK_ALIGN_PTR_BACK(x, mask)\
  5283. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  5284. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  5285. #define NK_CONTAINER_OF(ptr,type,member)\
  5286. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  5287. #ifdef __cplusplus
  5288. }
  5289. #endif
  5290. #ifdef __cplusplus
  5291. template<typename T> struct nk_alignof;
  5292. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  5293. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  5294. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  5295. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  5296. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  5297. #elif defined(_MSC_VER)
  5298. #define NK_ALIGNOF(t) (__alignof(t))
  5299. #else
  5300. #define NK_ALIGNOF(t) ((char*)(&((struct {char c; t _h;}*)0)->_h) - (char*)0)
  5301. #endif
  5302. #endif /* NK_NUKLEAR_H_ */
  5303. /*
  5304. * ==============================================================
  5305. *
  5306. * IMPLEMENTATION
  5307. *
  5308. * ===============================================================
  5309. */
  5310. #ifdef NK_IMPLEMENTATION
  5311. #ifndef NK_POOL_DEFAULT_CAPACITY
  5312. #define NK_POOL_DEFAULT_CAPACITY 16
  5313. #endif
  5314. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  5315. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  5316. #endif
  5317. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  5318. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  5319. #endif
  5320. /* standard library headers */
  5321. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5322. #include <stdlib.h> /* malloc, free */
  5323. #endif
  5324. #ifdef NK_INCLUDE_STANDARD_IO
  5325. #include <stdio.h> /* fopen, fclose,... */
  5326. #endif
  5327. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5328. #include <stdarg.h> /* valist, va_start, va_end, ... */
  5329. #endif
  5330. #ifndef NK_ASSERT
  5331. #include <assert.h>
  5332. #define NK_ASSERT(expr) assert(expr)
  5333. #endif
  5334. #ifndef NK_MEMSET
  5335. #define NK_MEMSET nk_memset
  5336. #endif
  5337. #ifndef NK_MEMCPY
  5338. #define NK_MEMCPY nk_memcopy
  5339. #endif
  5340. #ifndef NK_SQRT
  5341. #define NK_SQRT nk_sqrt
  5342. #endif
  5343. #ifndef NK_SIN
  5344. #define NK_SIN nk_sin
  5345. #endif
  5346. #ifndef NK_COS
  5347. #define NK_COS nk_cos
  5348. #endif
  5349. #ifndef NK_STRTOD
  5350. #define NK_STRTOD nk_strtod
  5351. #endif
  5352. #ifndef NK_DTOA
  5353. #define NK_DTOA nk_dtoa
  5354. #endif
  5355. #define NK_DEFAULT (-1)
  5356. #ifndef NK_VSNPRINTF
  5357. /* If your compiler does support `vsnprintf` I would highly recommend
  5358. * defining this to vsnprintf instead since `vsprintf` is basically
  5359. * unbelievable unsafe and should *NEVER* be used. But I have to support
  5360. * it since C89 only provides this unsafe version. */
  5361. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  5362. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  5363. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  5364. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  5365. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  5366. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  5367. #else
  5368. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  5369. #endif
  5370. #endif
  5371. #define NK_SCHAR_MIN (-127)
  5372. #define NK_SCHAR_MAX 127
  5373. #define NK_UCHAR_MIN 0
  5374. #define NK_UCHAR_MAX 256
  5375. #define NK_SSHORT_MIN (-32767)
  5376. #define NK_SSHORT_MAX 32767
  5377. #define NK_USHORT_MIN 0
  5378. #define NK_USHORT_MAX 65535
  5379. #define NK_SINT_MIN (-2147483647)
  5380. #define NK_SINT_MAX 2147483647
  5381. #define NK_UINT_MIN 0
  5382. #define NK_UINT_MAX 4294967295u
  5383. /* Make sure correct type size:
  5384. * This will fire with a negative subscript error if the type sizes
  5385. * are set incorrectly by the compiler, and compile out if not */
  5386. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  5387. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  5388. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  5389. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  5390. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  5391. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  5392. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  5393. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  5394. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  5395. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  5396. #define NK_FLOAT_PRECISION 0.00000000000001
  5397. NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
  5398. NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
  5399. NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
  5400. NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
  5401. NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
  5402. NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
  5403. /*
  5404. * ==============================================================
  5405. *
  5406. * MATH
  5407. *
  5408. * ===============================================================
  5409. */
  5410. /* Since nuklear is supposed to work on all systems providing floating point
  5411. math without any dependencies I also had to implement my own math functions
  5412. for sqrt, sin and cos. Since the actual highly accurate implementations for
  5413. the standard library functions are quite complex and I do not need high
  5414. precision for my use cases I use approximations.
  5415. Sqrt
  5416. ----
  5417. For square root nuklear uses the famous fast inverse square root:
  5418. https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  5419. slightly tweaked magic constant. While on today's hardware it is
  5420. probably not faster it is still fast and accurate enough for
  5421. nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  5422. Sine/Cosine
  5423. -----------
  5424. All constants inside both function are generated Remez's minimax
  5425. approximations for value range 0...2*PI. The reason why I decided to
  5426. approximate exactly that range is that nuklear only needs sine and
  5427. cosine to generate circles which only requires that exact range.
  5428. In addition I used Remez instead of Taylor for additional precision:
  5429. www.lolengine.net/blog/2011/12/21/better-function-approximations.
  5430. The tool I used to generate constants for both sine and cosine
  5431. (it can actually approximate a lot more functions) can be
  5432. found here: www.lolengine.net/wiki/oss/lolremez
  5433. */
  5434. NK_INTERN float
  5435. nk_inv_sqrt(float number)
  5436. {
  5437. float x2;
  5438. const float threehalfs = 1.5f;
  5439. union {nk_uint i; float f;} conv = {0};
  5440. conv.f = number;
  5441. x2 = number * 0.5f;
  5442. conv.i = 0x5f375A84 - (conv.i >> 1);
  5443. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  5444. return conv.f;
  5445. }
  5446. NK_INTERN float
  5447. nk_sqrt(float x)
  5448. {
  5449. return x * nk_inv_sqrt(x);
  5450. }
  5451. NK_INTERN float
  5452. nk_sin(float x)
  5453. {
  5454. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  5455. NK_STORAGE const float a1 = +1.00086760103908896f;
  5456. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  5457. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  5458. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  5459. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  5460. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  5461. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  5462. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  5463. }
  5464. NK_INTERN float
  5465. nk_cos(float x)
  5466. {
  5467. NK_STORAGE const float a0 = +1.00238601909309722f;
  5468. NK_STORAGE const float a1 = -3.81919947353040024e-2f;
  5469. NK_STORAGE const float a2 = -3.94382342128062756e-1f;
  5470. NK_STORAGE const float a3 = -1.18134036025221444e-1f;
  5471. NK_STORAGE const float a4 = +1.07123798512170878e-1f;
  5472. NK_STORAGE const float a5 = -1.86637164165180873e-2f;
  5473. NK_STORAGE const float a6 = +9.90140908664079833e-4f;
  5474. NK_STORAGE const float a7 = -5.23022132118824778e-14f;
  5475. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  5476. }
  5477. NK_INTERN nk_uint
  5478. nk_round_up_pow2(nk_uint v)
  5479. {
  5480. v--;
  5481. v |= v >> 1;
  5482. v |= v >> 2;
  5483. v |= v >> 4;
  5484. v |= v >> 8;
  5485. v |= v >> 16;
  5486. v++;
  5487. return v;
  5488. }
  5489. NK_API struct nk_rect
  5490. nk_get_null_rect(void)
  5491. {
  5492. return nk_null_rect;
  5493. }
  5494. NK_API struct nk_rect
  5495. nk_rect(float x, float y, float w, float h)
  5496. {
  5497. struct nk_rect r;
  5498. r.x = x; r.y = y;
  5499. r.w = w; r.h = h;
  5500. return r;
  5501. }
  5502. NK_API struct nk_rect
  5503. nk_recti(int x, int y, int w, int h)
  5504. {
  5505. struct nk_rect r;
  5506. r.x = (float)x;
  5507. r.y = (float)y;
  5508. r.w = (float)w;
  5509. r.h = (float)h;
  5510. return r;
  5511. }
  5512. NK_API struct nk_rect
  5513. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  5514. {
  5515. return nk_rect(pos.x, pos.y, size.x, size.y);
  5516. }
  5517. NK_API struct nk_rect
  5518. nk_rectv(const float *r)
  5519. {
  5520. return nk_rect(r[0], r[1], r[2], r[3]);
  5521. }
  5522. NK_API struct nk_rect
  5523. nk_rectiv(const int *r)
  5524. {
  5525. return nk_recti(r[0], r[1], r[2], r[3]);
  5526. }
  5527. NK_API struct nk_vec2
  5528. nk_rect_pos(struct nk_rect r)
  5529. {
  5530. struct nk_vec2 ret;
  5531. ret.x = r.x; ret.y = r.y;
  5532. return ret;
  5533. }
  5534. NK_API struct nk_vec2
  5535. nk_rect_size(struct nk_rect r)
  5536. {
  5537. struct nk_vec2 ret;
  5538. ret.x = r.w; ret.y = r.h;
  5539. return ret;
  5540. }
  5541. NK_INTERN struct nk_rect
  5542. nk_shrink_rect(struct nk_rect r, float amount)
  5543. {
  5544. struct nk_rect res;
  5545. r.w = NK_MAX(r.w, 2 * amount);
  5546. r.h = NK_MAX(r.h, 2 * amount);
  5547. res.x = r.x + amount;
  5548. res.y = r.y + amount;
  5549. res.w = r.w - 2 * amount;
  5550. res.h = r.h - 2 * amount;
  5551. return res;
  5552. }
  5553. NK_INTERN struct nk_rect
  5554. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  5555. {
  5556. r.w = NK_MAX(r.w, 2 * pad.x);
  5557. r.h = NK_MAX(r.h, 2 * pad.y);
  5558. r.x += pad.x; r.y += pad.y;
  5559. r.w -= 2 * pad.x;
  5560. r.h -= 2 * pad.y;
  5561. return r;
  5562. }
  5563. NK_API struct nk_vec2
  5564. nk_vec2(float x, float y)
  5565. {
  5566. struct nk_vec2 ret;
  5567. ret.x = x; ret.y = y;
  5568. return ret;
  5569. }
  5570. NK_API struct nk_vec2
  5571. nk_vec2i(int x, int y)
  5572. {
  5573. struct nk_vec2 ret;
  5574. ret.x = (float)x;
  5575. ret.y = (float)y;
  5576. return ret;
  5577. }
  5578. NK_API struct nk_vec2
  5579. nk_vec2v(const float *v)
  5580. {
  5581. return nk_vec2(v[0], v[1]);
  5582. }
  5583. NK_API struct nk_vec2
  5584. nk_vec2iv(const int *v)
  5585. {
  5586. return nk_vec2i(v[0], v[1]);
  5587. }
  5588. /*
  5589. * ==============================================================
  5590. *
  5591. * UTIL
  5592. *
  5593. * ===============================================================
  5594. */
  5595. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  5596. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  5597. NK_INTERN int nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  5598. NK_INTERN int nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  5599. NK_INTERN int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  5600. NK_INTERN int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  5601. NK_INTERN void*
  5602. nk_memcopy(void *dst0, const void *src0, nk_size length)
  5603. {
  5604. nk_ptr t;
  5605. char *dst = (char*)dst0;
  5606. const char *src = (const char*)src0;
  5607. if (length == 0 || dst == src)
  5608. goto done;
  5609. #define nk_word int
  5610. #define nk_wsize sizeof(nk_word)
  5611. #define nk_wmask (nk_wsize-1)
  5612. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  5613. #define NK_TLOOP1(s) do { s; } while (--t)
  5614. if (dst < src) {
  5615. t = (nk_ptr)src; /* only need low bits */
  5616. if ((t | (nk_ptr)dst) & nk_wmask) {
  5617. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  5618. t = length;
  5619. else
  5620. t = nk_wsize - (t & nk_wmask);
  5621. length -= t;
  5622. NK_TLOOP1(*dst++ = *src++);
  5623. }
  5624. t = length / nk_wsize;
  5625. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  5626. src += nk_wsize; dst += nk_wsize);
  5627. t = length & nk_wmask;
  5628. NK_TLOOP(*dst++ = *src++);
  5629. } else {
  5630. src += length;
  5631. dst += length;
  5632. t = (nk_ptr)src;
  5633. if ((t | (nk_ptr)dst) & nk_wmask) {
  5634. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  5635. t = length;
  5636. else
  5637. t &= nk_wmask;
  5638. length -= t;
  5639. NK_TLOOP1(*--dst = *--src);
  5640. }
  5641. t = length / nk_wsize;
  5642. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  5643. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  5644. t = length & nk_wmask;
  5645. NK_TLOOP(*--dst = *--src);
  5646. }
  5647. #undef nk_word
  5648. #undef nk_wsize
  5649. #undef nk_wmask
  5650. #undef NK_TLOOP
  5651. #undef NK_TLOOP1
  5652. done:
  5653. return (dst0);
  5654. }
  5655. NK_INTERN void
  5656. nk_memset(void *ptr, int c0, nk_size size)
  5657. {
  5658. #define nk_word unsigned
  5659. #define nk_wsize sizeof(nk_word)
  5660. #define nk_wmask (nk_wsize - 1)
  5661. nk_byte *dst = (nk_byte*)ptr;
  5662. unsigned c = 0;
  5663. nk_size t = 0;
  5664. if ((c = (nk_byte)c0) != 0) {
  5665. c = (c << 8) | c; /* at least 16-bits */
  5666. if (sizeof(unsigned int) > 2)
  5667. c = (c << 16) | c; /* at least 32-bits*/
  5668. }
  5669. /* too small of a word count */
  5670. dst = (nk_byte*)ptr;
  5671. if (size < 3 * nk_wsize) {
  5672. while (size--) *dst++ = (nk_byte)c0;
  5673. return;
  5674. }
  5675. /* align destination */
  5676. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  5677. t = nk_wsize -t;
  5678. size -= t;
  5679. do {
  5680. *dst++ = (nk_byte)c0;
  5681. } while (--t != 0);
  5682. }
  5683. /* fill word */
  5684. t = size / nk_wsize;
  5685. do {
  5686. *(nk_word*)((void*)dst) = c;
  5687. dst += nk_wsize;
  5688. } while (--t != 0);
  5689. /* fill trailing bytes */
  5690. t = (size & nk_wmask);
  5691. if (t != 0) {
  5692. do {
  5693. *dst++ = (nk_byte)c0;
  5694. } while (--t != 0);
  5695. }
  5696. #undef nk_word
  5697. #undef nk_wsize
  5698. #undef nk_wmask
  5699. }
  5700. NK_INTERN void
  5701. nk_zero(void *ptr, nk_size size)
  5702. {
  5703. NK_ASSERT(ptr);
  5704. NK_MEMSET(ptr, 0, size);
  5705. }
  5706. NK_API int
  5707. nk_strlen(const char *str)
  5708. {
  5709. int siz = 0;
  5710. NK_ASSERT(str);
  5711. while (str && *str++ != '\0') siz++;
  5712. return siz;
  5713. }
  5714. NK_API int
  5715. nk_strtoi(const char *str, const char **endptr)
  5716. {
  5717. int neg = 1;
  5718. const char *p = str;
  5719. int value = 0;
  5720. NK_ASSERT(str);
  5721. if (!str) return 0;
  5722. /* skip whitespace */
  5723. while (*p == ' ') p++;
  5724. if (*p == '-') {
  5725. neg = -1;
  5726. p++;
  5727. }
  5728. while (*p && *p >= '0' && *p <= '9') {
  5729. value = value * 10 + (int) (*p - '0');
  5730. p++;
  5731. }
  5732. if (endptr)
  5733. *endptr = p;
  5734. return neg*value;
  5735. }
  5736. NK_API double
  5737. nk_strtod(const char *str, const char **endptr)
  5738. {
  5739. double m;
  5740. double neg = 1.0;
  5741. const char *p = str;
  5742. double value = 0;
  5743. double number = 0;
  5744. NK_ASSERT(str);
  5745. if (!str) return 0;
  5746. /* skip whitespace */
  5747. while (*p == ' ') p++;
  5748. if (*p == '-') {
  5749. neg = -1.0;
  5750. p++;
  5751. }
  5752. while (*p && *p != '.' && *p != 'e') {
  5753. value = value * 10.0 + (double) (*p - '0');
  5754. p++;
  5755. }
  5756. if (*p == '.') {
  5757. p++;
  5758. for(m = 0.1; *p && *p != 'e'; p++ ) {
  5759. value = value + (double) (*p - '0') * m;
  5760. m *= 0.1;
  5761. }
  5762. }
  5763. if (*p == 'e') {
  5764. int i, pow, div;
  5765. p++;
  5766. if (*p == '-') {
  5767. div = nk_true;
  5768. p++;
  5769. } else if (*p == '+') {
  5770. div = nk_false;
  5771. p++;
  5772. } else div = nk_false;
  5773. for (pow = 0; *p; p++)
  5774. pow = pow * 10 + (int) (*p - '0');
  5775. for (m = 1.0, i = 0; i < pow; i++)
  5776. m *= 10.0;
  5777. if (div)
  5778. value /= m;
  5779. else value *= m;
  5780. }
  5781. number = value * neg;
  5782. if (endptr)
  5783. *endptr = p;
  5784. return number;
  5785. }
  5786. NK_API float
  5787. nk_strtof(const char *str, const char **endptr)
  5788. {
  5789. float float_value;
  5790. double double_value;
  5791. double_value = NK_STRTOD(str, endptr);
  5792. float_value = (float)double_value;
  5793. return float_value;
  5794. }
  5795. NK_API int
  5796. nk_stricmp(const char *s1, const char *s2)
  5797. {
  5798. nk_int c1,c2,d;
  5799. do {
  5800. c1 = *s1++;
  5801. c2 = *s2++;
  5802. d = c1 - c2;
  5803. while (d) {
  5804. if (c1 <= 'Z' && c1 >= 'A') {
  5805. d += ('a' - 'A');
  5806. if (!d) break;
  5807. }
  5808. if (c2 <= 'Z' && c2 >= 'A') {
  5809. d -= ('a' - 'A');
  5810. if (!d) break;
  5811. }
  5812. return ((d >= 0) << 1) - 1;
  5813. }
  5814. } while (c1);
  5815. return 0;
  5816. }
  5817. NK_API int
  5818. nk_stricmpn(const char *s1, const char *s2, int n)
  5819. {
  5820. int c1,c2,d;
  5821. NK_ASSERT(n >= 0);
  5822. do {
  5823. c1 = *s1++;
  5824. c2 = *s2++;
  5825. if (!n--) return 0;
  5826. d = c1 - c2;
  5827. while (d) {
  5828. if (c1 <= 'Z' && c1 >= 'A') {
  5829. d += ('a' - 'A');
  5830. if (!d) break;
  5831. }
  5832. if (c2 <= 'Z' && c2 >= 'A') {
  5833. d -= ('a' - 'A');
  5834. if (!d) break;
  5835. }
  5836. return ((d >= 0) << 1) - 1;
  5837. }
  5838. } while (c1);
  5839. return 0;
  5840. }
  5841. NK_INTERN int
  5842. nk_str_match_here(const char *regexp, const char *text)
  5843. {
  5844. if (regexp[0] == '\0')
  5845. return 1;
  5846. if (regexp[1] == '*')
  5847. return nk_str_match_star(regexp[0], regexp+2, text);
  5848. if (regexp[0] == '$' && regexp[1] == '\0')
  5849. return *text == '\0';
  5850. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  5851. return nk_str_match_here(regexp+1, text+1);
  5852. return 0;
  5853. }
  5854. NK_INTERN int
  5855. nk_str_match_star(int c, const char *regexp, const char *text)
  5856. {
  5857. do {/* a '* matches zero or more instances */
  5858. if (nk_str_match_here(regexp, text))
  5859. return 1;
  5860. } while (*text != '\0' && (*text++ == c || c == '.'));
  5861. return 0;
  5862. }
  5863. NK_API int
  5864. nk_strfilter(const char *text, const char *regexp)
  5865. {
  5866. /*
  5867. c matches any literal character c
  5868. . matches any single character
  5869. ^ matches the beginning of the input string
  5870. $ matches the end of the input string
  5871. * matches zero or more occurrences of the previous character*/
  5872. if (regexp[0] == '^')
  5873. return nk_str_match_here(regexp+1, text);
  5874. do { /* must look even if string is empty */
  5875. if (nk_str_match_here(regexp, text))
  5876. return 1;
  5877. } while (*text++ != '\0');
  5878. return 0;
  5879. }
  5880. NK_API int
  5881. nk_strmatch_fuzzy_text(const char *str, int str_len,
  5882. const char *pattern, int *out_score)
  5883. {
  5884. /* Returns true if each character in pattern is found sequentially within str
  5885. * if found then out_score is also set. Score value has no intrinsic meaning.
  5886. * Range varies with pattern. Can only compare scores with same search pattern. */
  5887. /* bonus for adjacent matches */
  5888. #define NK_ADJACENCY_BONUS 5
  5889. /* bonus if match occurs after a separator */
  5890. #define NK_SEPARATOR_BONUS 10
  5891. /* bonus if match is uppercase and prev is lower */
  5892. #define NK_CAMEL_BONUS 10
  5893. /* penalty applied for every letter in str before the first match */
  5894. #define NK_LEADING_LETTER_PENALTY (-3)
  5895. /* maximum penalty for leading letters */
  5896. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  5897. /* penalty for every letter that doesn't matter */
  5898. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  5899. /* loop variables */
  5900. int score = 0;
  5901. char const * pattern_iter = pattern;
  5902. int str_iter = 0;
  5903. int prev_matched = nk_false;
  5904. int prev_lower = nk_false;
  5905. /* true so if first letter match gets separator bonus*/
  5906. int prev_separator = nk_true;
  5907. /* use "best" matched letter if multiple string letters match the pattern */
  5908. char const * best_letter = 0;
  5909. int best_letter_score = 0;
  5910. /* loop over strings */
  5911. NK_ASSERT(str);
  5912. NK_ASSERT(pattern);
  5913. if (!str || !str_len || !pattern) return 0;
  5914. while (str_iter < str_len)
  5915. {
  5916. const char pattern_letter = *pattern_iter;
  5917. const char str_letter = str[str_iter];
  5918. int next_match = *pattern_iter != '\0' &&
  5919. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  5920. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  5921. int advanced = next_match && best_letter;
  5922. int pattern_repeat = best_letter && *pattern_iter != '\0';
  5923. pattern_repeat = pattern_repeat &&
  5924. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  5925. if (advanced || pattern_repeat) {
  5926. score += best_letter_score;
  5927. best_letter = 0;
  5928. best_letter_score = 0;
  5929. }
  5930. if (next_match || rematch)
  5931. {
  5932. int new_score = 0;
  5933. /* Apply penalty for each letter before the first pattern match */
  5934. if (pattern_iter == pattern) {
  5935. int count = (int)(&str[str_iter] - str);
  5936. int penalty = NK_LEADING_LETTER_PENALTY * count;
  5937. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  5938. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  5939. score += penalty;
  5940. }
  5941. /* apply bonus for consecutive bonuses */
  5942. if (prev_matched)
  5943. new_score += NK_ADJACENCY_BONUS;
  5944. /* apply bonus for matches after a separator */
  5945. if (prev_separator)
  5946. new_score += NK_SEPARATOR_BONUS;
  5947. /* apply bonus across camel case boundaries */
  5948. if (prev_lower && nk_is_upper(str_letter))
  5949. new_score += NK_CAMEL_BONUS;
  5950. /* update pattern iter IFF the next pattern letter was matched */
  5951. if (next_match)
  5952. ++pattern_iter;
  5953. /* update best letter in str which may be for a "next" letter or a rematch */
  5954. if (new_score >= best_letter_score) {
  5955. /* apply penalty for now skipped letter */
  5956. if (best_letter != 0)
  5957. score += NK_UNMATCHED_LETTER_PENALTY;
  5958. best_letter = &str[str_iter];
  5959. best_letter_score = new_score;
  5960. }
  5961. prev_matched = nk_true;
  5962. } else {
  5963. score += NK_UNMATCHED_LETTER_PENALTY;
  5964. prev_matched = nk_false;
  5965. }
  5966. /* separators should be more easily defined */
  5967. prev_lower = nk_is_lower(str_letter) != 0;
  5968. prev_separator = str_letter == '_' || str_letter == ' ';
  5969. ++str_iter;
  5970. }
  5971. /* apply score for last match */
  5972. if (best_letter)
  5973. score += best_letter_score;
  5974. /* did not match full pattern */
  5975. if (*pattern_iter != '\0')
  5976. return nk_false;
  5977. if (out_score)
  5978. *out_score = score;
  5979. return nk_true;
  5980. }
  5981. NK_API int
  5982. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  5983. {return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);}
  5984. NK_INTERN int
  5985. nk_string_float_limit(char *string, int prec)
  5986. {
  5987. int dot = 0;
  5988. char *c = string;
  5989. while (*c) {
  5990. if (*c == '.') {
  5991. dot = 1;
  5992. c++;
  5993. continue;
  5994. }
  5995. if (dot == (prec+1)) {
  5996. *c = 0;
  5997. break;
  5998. }
  5999. if (dot > 0) dot++;
  6000. c++;
  6001. }
  6002. return (int)(c - string);
  6003. }
  6004. NK_INTERN double
  6005. nk_pow(double x, int n)
  6006. {
  6007. /* check the sign of n */
  6008. double r = 1;
  6009. int plus = n >= 0;
  6010. n = (plus) ? n : -n;
  6011. while (n > 0) {
  6012. if ((n & 1) == 1)
  6013. r *= x;
  6014. n /= 2;
  6015. x *= x;
  6016. }
  6017. return plus ? r : 1.0 / r;
  6018. }
  6019. NK_INTERN int
  6020. nk_ifloord(double x)
  6021. {
  6022. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  6023. return (int)x;
  6024. }
  6025. NK_INTERN int
  6026. nk_ifloorf(float x)
  6027. {
  6028. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  6029. return (int)x;
  6030. }
  6031. NK_INTERN int
  6032. nk_iceilf(float x)
  6033. {
  6034. if (x >= 0) {
  6035. int i = (int)x;
  6036. return (x > i) ? i+1: i;
  6037. } else {
  6038. int t = (int)x;
  6039. float r = x - (float)t;
  6040. return (r > 0.0f) ? t+1: t;
  6041. }
  6042. }
  6043. NK_INTERN int
  6044. nk_log10(double n)
  6045. {
  6046. int neg;
  6047. int ret;
  6048. int exp = 0;
  6049. neg = (n < 0) ? 1 : 0;
  6050. ret = (neg) ? (int)-n : (int)n;
  6051. while ((ret / 10) > 0) {
  6052. ret /= 10;
  6053. exp++;
  6054. }
  6055. if (neg) exp = -exp;
  6056. return exp;
  6057. }
  6058. NK_INTERN void
  6059. nk_strrev_ascii(char *s)
  6060. {
  6061. int len = nk_strlen(s);
  6062. int end = len / 2;
  6063. int i = 0;
  6064. char t;
  6065. for (; i < end; ++i) {
  6066. t = s[i];
  6067. s[i] = s[len - 1 - i];
  6068. s[len -1 - i] = t;
  6069. }
  6070. }
  6071. NK_INTERN char*
  6072. nk_itoa(char *s, long n)
  6073. {
  6074. long i = 0;
  6075. if (n == 0) {
  6076. s[i++] = '0';
  6077. s[i] = 0;
  6078. return s;
  6079. }
  6080. if (n < 0) {
  6081. s[i++] = '-';
  6082. n = -n;
  6083. }
  6084. while (n > 0) {
  6085. s[i++] = (char)('0' + (n % 10));
  6086. n /= 10;
  6087. }
  6088. s[i] = 0;
  6089. if (s[0] == '-')
  6090. ++s;
  6091. nk_strrev_ascii(s);
  6092. return s;
  6093. }
  6094. NK_INTERN char*
  6095. nk_dtoa(char *s, double n)
  6096. {
  6097. int useExp = 0;
  6098. int digit = 0, m = 0, m1 = 0;
  6099. char *c = s;
  6100. int neg = 0;
  6101. NK_ASSERT(s);
  6102. if (!s) return 0;
  6103. if (n == 0.0) {
  6104. s[0] = '0'; s[1] = '\0';
  6105. return s;
  6106. }
  6107. neg = (n < 0);
  6108. if (neg) n = -n;
  6109. /* calculate magnitude */
  6110. m = nk_log10(n);
  6111. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  6112. if (neg) *(c++) = '-';
  6113. /* set up for scientific notation */
  6114. if (useExp) {
  6115. if (m < 0)
  6116. m -= 1;
  6117. n = n / (double)nk_pow(10.0, m);
  6118. m1 = m;
  6119. m = 0;
  6120. }
  6121. if (m < 1.0) {
  6122. m = 0;
  6123. }
  6124. /* convert the number */
  6125. while (n > NK_FLOAT_PRECISION || m >= 0) {
  6126. double weight = nk_pow(10.0, m);
  6127. if (weight > 0) {
  6128. double t = (double)n / weight;
  6129. digit = nk_ifloord(t);
  6130. n -= ((double)digit * weight);
  6131. *(c++) = (char)('0' + (char)digit);
  6132. }
  6133. if (m == 0 && n > 0)
  6134. *(c++) = '.';
  6135. m--;
  6136. }
  6137. if (useExp) {
  6138. /* convert the exponent */
  6139. int i, j;
  6140. *(c++) = 'e';
  6141. if (m1 > 0) {
  6142. *(c++) = '+';
  6143. } else {
  6144. *(c++) = '-';
  6145. m1 = -m1;
  6146. }
  6147. m = 0;
  6148. while (m1 > 0) {
  6149. *(c++) = (char)('0' + (char)(m1 % 10));
  6150. m1 /= 10;
  6151. m++;
  6152. }
  6153. c -= m;
  6154. for (i = 0, j = m-1; i<j; i++, j--) {
  6155. /* swap without temporary */
  6156. c[i] ^= c[j];
  6157. c[j] ^= c[i];
  6158. c[i] ^= c[j];
  6159. }
  6160. c += m;
  6161. }
  6162. *(c) = '\0';
  6163. return s;
  6164. }
  6165. #ifdef NK_INCLUDE_STANDARD_VARARGS
  6166. #ifndef NK_INCLUDE_STANDARD_IO
  6167. static int
  6168. nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
  6169. {
  6170. enum nk_arg_type {
  6171. NK_ARG_TYPE_CHAR,
  6172. NK_ARG_TYPE_SHORT,
  6173. NK_ARG_TYPE_DEFAULT,
  6174. NK_ARG_TYPE_LONG
  6175. };
  6176. enum nk_arg_flags {
  6177. NK_ARG_FLAG_LEFT = 0x01,
  6178. NK_ARG_FLAG_PLUS = 0x02,
  6179. NK_ARG_FLAG_SPACE = 0x04,
  6180. NK_ARG_FLAG_NUM = 0x10,
  6181. NK_ARG_FLAG_ZERO = 0x20
  6182. };
  6183. char number_buffer[NK_MAX_NUMBER_BUFFER];
  6184. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  6185. int precision = NK_DEFAULT;
  6186. int width = NK_DEFAULT;
  6187. nk_flags flag = 0;
  6188. int len = 0;
  6189. int result = -1;
  6190. const char *iter = fmt;
  6191. NK_ASSERT(buf);
  6192. NK_ASSERT(buf_size);
  6193. if (!buf || !buf_size || !fmt) return 0;
  6194. for (iter = fmt; *iter && len < buf_size; iter++) {
  6195. /* copy all non-format characters */
  6196. while (*iter && (*iter != '%') && (len < buf_size))
  6197. buf[len++] = *iter++;
  6198. if (!(*iter) || len >= buf_size) break;
  6199. iter++;
  6200. /* flag arguments */
  6201. while (*iter) {
  6202. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  6203. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  6204. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  6205. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  6206. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  6207. else break;
  6208. iter++;
  6209. }
  6210. /* width argument */
  6211. width = NK_DEFAULT;
  6212. if (*iter >= '1' && *iter <= '9') {
  6213. const char *end;
  6214. width = nk_strtoi(iter, &end);
  6215. if (end == iter)
  6216. width = -1;
  6217. else iter = end;
  6218. } else if (*iter == '*') {
  6219. width = va_arg(args, int);
  6220. iter++;
  6221. }
  6222. /* precision argument */
  6223. precision = NK_DEFAULT;
  6224. if (*iter == '.') {
  6225. iter++;
  6226. if (*iter == '*') {
  6227. precision = va_arg(args, int);
  6228. iter++;
  6229. } else {
  6230. const char *end;
  6231. precision = nk_strtoi(iter, &end);
  6232. if (end == iter)
  6233. precision = -1;
  6234. else iter = end;
  6235. }
  6236. }
  6237. /* length modifier */
  6238. if (*iter == 'h') {
  6239. if (*(iter+1) == 'h') {
  6240. arg_type = NK_ARG_TYPE_CHAR;
  6241. iter++;
  6242. } else arg_type = NK_ARG_TYPE_SHORT;
  6243. iter++;
  6244. } else if (*iter == 'l') {
  6245. arg_type = NK_ARG_TYPE_LONG;
  6246. iter++;
  6247. } else arg_type = NK_ARG_TYPE_DEFAULT;
  6248. /* specifier */
  6249. if (*iter == '%') {
  6250. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6251. NK_ASSERT(precision == NK_DEFAULT);
  6252. NK_ASSERT(width == NK_DEFAULT);
  6253. if (len < buf_size)
  6254. buf[len++] = '%';
  6255. } else if (*iter == 's') {
  6256. /* string */
  6257. const char *str = va_arg(args, const char*);
  6258. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  6259. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6260. NK_ASSERT(precision == NK_DEFAULT);
  6261. NK_ASSERT(width == NK_DEFAULT);
  6262. if (str == buf) return -1;
  6263. while (str && *str && len < buf_size)
  6264. buf[len++] = *str++;
  6265. } else if (*iter == 'n') {
  6266. /* current length callback */
  6267. signed int *n = va_arg(args, int*);
  6268. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6269. NK_ASSERT(precision == NK_DEFAULT);
  6270. NK_ASSERT(width == NK_DEFAULT);
  6271. if (n) *n = len;
  6272. } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  6273. /* signed integer */
  6274. long value = 0;
  6275. const char *num_iter;
  6276. int num_len, num_print, padding;
  6277. int cur_precision = NK_MAX(precision, 1);
  6278. int cur_width = NK_MAX(width, 0);
  6279. /* retrieve correct value type */
  6280. if (arg_type == NK_ARG_TYPE_CHAR)
  6281. value = (signed char)va_arg(args, int);
  6282. else if (arg_type == NK_ARG_TYPE_SHORT)
  6283. value = (signed short)va_arg(args, int);
  6284. else if (arg_type == NK_ARG_TYPE_LONG)
  6285. value = va_arg(args, signed long);
  6286. else if (*iter == 'c')
  6287. value = (unsigned char)va_arg(args, int);
  6288. else value = va_arg(args, signed int);
  6289. /* convert number to string */
  6290. nk_itoa(number_buffer, value);
  6291. num_len = nk_strlen(number_buffer);
  6292. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6293. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6294. padding = NK_MAX(padding-1, 0);
  6295. /* fill left padding up to a total of `width` characters */
  6296. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6297. while (padding-- > 0 && (len < buf_size)) {
  6298. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6299. buf[len++] = '0';
  6300. else buf[len++] = ' ';
  6301. }
  6302. }
  6303. /* copy string value representation into buffer */
  6304. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  6305. buf[len++] = '+';
  6306. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  6307. buf[len++] = ' ';
  6308. /* fill up to precision number of digits with '0' */
  6309. num_print = NK_MAX(cur_precision, num_len);
  6310. while (precision && (num_print > num_len) && (len < buf_size)) {
  6311. buf[len++] = '0';
  6312. num_print--;
  6313. }
  6314. /* copy string value representation into buffer */
  6315. num_iter = number_buffer;
  6316. while (precision && *num_iter && len < buf_size)
  6317. buf[len++] = *num_iter++;
  6318. /* fill right padding up to width characters */
  6319. if (flag & NK_ARG_FLAG_LEFT) {
  6320. while ((padding-- > 0) && (len < buf_size))
  6321. buf[len++] = ' ';
  6322. }
  6323. } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  6324. /* unsigned integer */
  6325. unsigned long value = 0;
  6326. int num_len = 0, num_print, padding = 0;
  6327. int cur_precision = NK_MAX(precision, 1);
  6328. int cur_width = NK_MAX(width, 0);
  6329. unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
  6330. /* print oct/hex/dec value */
  6331. const char *upper_output_format = "0123456789ABCDEF";
  6332. const char *lower_output_format = "0123456789abcdef";
  6333. const char *output_format = (*iter == 'x') ?
  6334. lower_output_format: upper_output_format;
  6335. /* retrieve correct value type */
  6336. if (arg_type == NK_ARG_TYPE_CHAR)
  6337. value = (unsigned char)va_arg(args, int);
  6338. else if (arg_type == NK_ARG_TYPE_SHORT)
  6339. value = (unsigned short)va_arg(args, int);
  6340. else if (arg_type == NK_ARG_TYPE_LONG)
  6341. value = va_arg(args, unsigned long);
  6342. else value = va_arg(args, unsigned int);
  6343. do {
  6344. /* convert decimal number into hex/oct number */
  6345. int digit = output_format[value % base];
  6346. if (num_len < NK_MAX_NUMBER_BUFFER)
  6347. number_buffer[num_len++] = (char)digit;
  6348. value /= base;
  6349. } while (value > 0);
  6350. num_print = NK_MAX(cur_precision, num_len);
  6351. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6352. if (flag & NK_ARG_FLAG_NUM)
  6353. padding = NK_MAX(padding-1, 0);
  6354. /* fill left padding up to a total of `width` characters */
  6355. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6356. while ((padding-- > 0) && (len < buf_size)) {
  6357. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6358. buf[len++] = '0';
  6359. else buf[len++] = ' ';
  6360. }
  6361. }
  6362. /* fill up to precision number of digits */
  6363. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  6364. if ((*iter == 'o') && (len < buf_size)) {
  6365. buf[len++] = '0';
  6366. } else if ((*iter == 'x') && ((len+1) < buf_size)) {
  6367. buf[len++] = '0';
  6368. buf[len++] = 'x';
  6369. } else if ((*iter == 'X') && ((len+1) < buf_size)) {
  6370. buf[len++] = '0';
  6371. buf[len++] = 'X';
  6372. }
  6373. }
  6374. while (precision && (num_print > num_len) && (len < buf_size)) {
  6375. buf[len++] = '0';
  6376. num_print--;
  6377. }
  6378. /* reverse number direction */
  6379. while (num_len > 0) {
  6380. if (precision && (len < buf_size))
  6381. buf[len++] = number_buffer[num_len-1];
  6382. num_len--;
  6383. }
  6384. /* fill right padding up to width characters */
  6385. if (flag & NK_ARG_FLAG_LEFT) {
  6386. while ((padding-- > 0) && (len < buf_size))
  6387. buf[len++] = ' ';
  6388. }
  6389. } else if (*iter == 'f') {
  6390. /* floating point */
  6391. const char *num_iter;
  6392. int cur_precision = (precision < 0) ? 6: precision;
  6393. int prefix, cur_width = NK_MAX(width, 0);
  6394. double value = va_arg(args, double);
  6395. int num_len = 0, frac_len = 0, dot = 0;
  6396. int padding = 0;
  6397. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6398. NK_DTOA(number_buffer, value);
  6399. num_len = nk_strlen(number_buffer);
  6400. /* calculate padding */
  6401. num_iter = number_buffer;
  6402. while (*num_iter && *num_iter != '.')
  6403. num_iter++;
  6404. prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
  6405. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
  6406. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6407. padding = NK_MAX(padding-1, 0);
  6408. /* fill left padding up to a total of `width` characters */
  6409. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6410. while (padding-- > 0 && (len < buf_size)) {
  6411. if (flag & NK_ARG_FLAG_ZERO)
  6412. buf[len++] = '0';
  6413. else buf[len++] = ' ';
  6414. }
  6415. }
  6416. /* copy string value representation into buffer */
  6417. num_iter = number_buffer;
  6418. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  6419. buf[len++] = '+';
  6420. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  6421. buf[len++] = ' ';
  6422. while (*num_iter) {
  6423. if (dot) frac_len++;
  6424. if (len < buf_size)
  6425. buf[len++] = *num_iter;
  6426. if (*num_iter == '.') dot = 1;
  6427. if (frac_len >= cur_precision) break;
  6428. num_iter++;
  6429. }
  6430. /* fill number up to precision */
  6431. while (frac_len < cur_precision) {
  6432. if (!dot && len < buf_size) {
  6433. buf[len++] = '.';
  6434. dot = 1;
  6435. }
  6436. if (len < buf_size)
  6437. buf[len++] = '0';
  6438. frac_len++;
  6439. }
  6440. /* fill right padding up to width characters */
  6441. if (flag & NK_ARG_FLAG_LEFT) {
  6442. while ((padding-- > 0) && (len < buf_size))
  6443. buf[len++] = ' ';
  6444. }
  6445. } else {
  6446. /* Specifier not supported: g,G,e,E,p,z */
  6447. NK_ASSERT(0 && "specifier is not supported!");
  6448. return result;
  6449. }
  6450. }
  6451. buf[(len >= buf_size)?(buf_size-1):len] = 0;
  6452. result = (len >= buf_size)?-1:len;
  6453. return result;
  6454. }
  6455. #endif
  6456. NK_INTERN int
  6457. nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
  6458. {
  6459. int result = -1;
  6460. NK_ASSERT(buf);
  6461. NK_ASSERT(buf_size);
  6462. if (!buf || !buf_size || !fmt) return 0;
  6463. #ifdef NK_INCLUDE_STANDARD_IO
  6464. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  6465. result = (result >= buf_size) ? -1: result;
  6466. buf[buf_size-1] = 0;
  6467. #else
  6468. result = nk_vsnprintf(buf, buf_size, fmt, args);
  6469. #endif
  6470. return result;
  6471. }
  6472. #endif
  6473. NK_API nk_hash
  6474. nk_murmur_hash(const void * key, int len, nk_hash seed)
  6475. {
  6476. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  6477. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  6478. union {const nk_uint *i; const nk_byte *b;} conv = {0};
  6479. const nk_byte *data = (const nk_byte*)key;
  6480. const int nblocks = len/4;
  6481. nk_uint h1 = seed;
  6482. const nk_uint c1 = 0xcc9e2d51;
  6483. const nk_uint c2 = 0x1b873593;
  6484. const nk_byte *tail;
  6485. const nk_uint *blocks;
  6486. nk_uint k1;
  6487. int i;
  6488. /* body */
  6489. if (!key) return 0;
  6490. conv.b = (data + nblocks*4);
  6491. blocks = (const nk_uint*)conv.i;
  6492. for (i = -nblocks; i; ++i) {
  6493. k1 = blocks[i];
  6494. k1 *= c1;
  6495. k1 = NK_ROTL(k1,15);
  6496. k1 *= c2;
  6497. h1 ^= k1;
  6498. h1 = NK_ROTL(h1,13);
  6499. h1 = h1*5+0xe6546b64;
  6500. }
  6501. /* tail */
  6502. tail = (const nk_byte*)(data + nblocks*4);
  6503. k1 = 0;
  6504. switch (len & 3) {
  6505. case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
  6506. case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
  6507. case 1: k1 ^= tail[0];
  6508. k1 *= c1;
  6509. k1 = NK_ROTL(k1,15);
  6510. k1 *= c2;
  6511. h1 ^= k1;
  6512. break;
  6513. default: break;
  6514. }
  6515. /* finalization */
  6516. h1 ^= (nk_uint)len;
  6517. /* fmix32 */
  6518. h1 ^= h1 >> 16;
  6519. h1 *= 0x85ebca6b;
  6520. h1 ^= h1 >> 13;
  6521. h1 *= 0xc2b2ae35;
  6522. h1 ^= h1 >> 16;
  6523. #undef NK_ROTL
  6524. return h1;
  6525. }
  6526. #ifdef NK_INCLUDE_STANDARD_IO
  6527. NK_INTERN char*
  6528. nk_file_load(const char* path, nk_size* siz, struct nk_allocator *alloc)
  6529. {
  6530. char *buf;
  6531. FILE *fd;
  6532. long ret;
  6533. NK_ASSERT(path);
  6534. NK_ASSERT(siz);
  6535. NK_ASSERT(alloc);
  6536. if (!path || !siz || !alloc)
  6537. return 0;
  6538. fd = fopen(path, "rb");
  6539. if (!fd) return 0;
  6540. fseek(fd, 0, SEEK_END);
  6541. ret = ftell(fd);
  6542. if (ret < 0) {
  6543. fclose(fd);
  6544. return 0;
  6545. }
  6546. *siz = (nk_size)ret;
  6547. fseek(fd, 0, SEEK_SET);
  6548. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  6549. NK_ASSERT(buf);
  6550. if (!buf) {
  6551. fclose(fd);
  6552. return 0;
  6553. }
  6554. *siz = (nk_size)fread(buf, 1,*siz, fd);
  6555. fclose(fd);
  6556. return buf;
  6557. }
  6558. #endif
  6559. /*
  6560. * ==============================================================
  6561. *
  6562. * COLOR
  6563. *
  6564. * ===============================================================
  6565. */
  6566. NK_INTERN int
  6567. nk_parse_hex(const char *p, int length)
  6568. {
  6569. int i = 0;
  6570. int len = 0;
  6571. while (len < length) {
  6572. i <<= 4;
  6573. if (p[len] >= 'a' && p[len] <= 'f')
  6574. i += ((p[len] - 'a') + 10);
  6575. else if (p[len] >= 'A' && p[len] <= 'F')
  6576. i += ((p[len] - 'A') + 10);
  6577. else i += (p[len] - '0');
  6578. len++;
  6579. }
  6580. return i;
  6581. }
  6582. NK_API struct nk_color
  6583. nk_rgba(int r, int g, int b, int a)
  6584. {
  6585. struct nk_color ret;
  6586. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  6587. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  6588. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  6589. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  6590. return ret;
  6591. }
  6592. NK_API struct nk_color
  6593. nk_rgb_hex(const char *rgb)
  6594. {
  6595. struct nk_color col;
  6596. const char *c = rgb;
  6597. if (*c == '#') c++;
  6598. col.r = (nk_byte)nk_parse_hex(c, 2);
  6599. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  6600. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  6601. col.a = 255;
  6602. return col;
  6603. }
  6604. NK_API struct nk_color
  6605. nk_rgba_hex(const char *rgb)
  6606. {
  6607. struct nk_color col;
  6608. const char *c = rgb;
  6609. if (*c == '#') c++;
  6610. col.r = (nk_byte)nk_parse_hex(c, 2);
  6611. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  6612. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  6613. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  6614. return col;
  6615. }
  6616. NK_API void
  6617. nk_color_hex_rgba(char *output, struct nk_color col)
  6618. {
  6619. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  6620. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  6621. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  6622. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  6623. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  6624. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  6625. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  6626. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  6627. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  6628. output[8] = '\0';
  6629. #undef NK_TO_HEX
  6630. }
  6631. NK_API void
  6632. nk_color_hex_rgb(char *output, struct nk_color col)
  6633. {
  6634. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  6635. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  6636. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  6637. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  6638. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  6639. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  6640. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  6641. output[6] = '\0';
  6642. #undef NK_TO_HEX
  6643. }
  6644. NK_API struct nk_color
  6645. nk_rgba_iv(const int *c)
  6646. {
  6647. return nk_rgba(c[0], c[1], c[2], c[3]);
  6648. }
  6649. NK_API struct nk_color
  6650. nk_rgba_bv(const nk_byte *c)
  6651. {
  6652. return nk_rgba(c[0], c[1], c[2], c[3]);
  6653. }
  6654. NK_API struct nk_color
  6655. nk_rgb(int r, int g, int b)
  6656. {
  6657. struct nk_color ret;
  6658. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  6659. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  6660. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  6661. ret.a = (nk_byte)255;
  6662. return ret;
  6663. }
  6664. NK_API struct nk_color
  6665. nk_rgb_iv(const int *c)
  6666. {
  6667. return nk_rgb(c[0], c[1], c[2]);
  6668. }
  6669. NK_API struct nk_color
  6670. nk_rgb_bv(const nk_byte* c)
  6671. {
  6672. return nk_rgb(c[0], c[1], c[2]);
  6673. }
  6674. NK_API struct nk_color
  6675. nk_rgba_u32(nk_uint in)
  6676. {
  6677. struct nk_color ret;
  6678. ret.r = (in & 0xFF);
  6679. ret.g = ((in >> 8) & 0xFF);
  6680. ret.b = ((in >> 16) & 0xFF);
  6681. ret.a = (nk_byte)((in >> 24) & 0xFF);
  6682. return ret;
  6683. }
  6684. NK_API struct nk_color
  6685. nk_rgba_f(float r, float g, float b, float a)
  6686. {
  6687. struct nk_color ret;
  6688. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  6689. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  6690. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  6691. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  6692. return ret;
  6693. }
  6694. NK_API struct nk_color
  6695. nk_rgba_fv(const float *c)
  6696. {
  6697. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  6698. }
  6699. NK_API struct nk_color
  6700. nk_rgba_cf(struct nk_colorf c)
  6701. {
  6702. return nk_rgba_f(c.r, c.g, c.b, c.a);
  6703. }
  6704. NK_API struct nk_color
  6705. nk_rgb_f(float r, float g, float b)
  6706. {
  6707. struct nk_color ret;
  6708. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  6709. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  6710. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  6711. ret.a = 255;
  6712. return ret;
  6713. }
  6714. NK_API struct nk_color
  6715. nk_rgb_fv(const float *c)
  6716. {
  6717. return nk_rgb_f(c[0], c[1], c[2]);
  6718. }
  6719. NK_API struct nk_color
  6720. nk_rgb_cf(struct nk_colorf c)
  6721. {
  6722. return nk_rgb_f(c.r, c.g, c.b);
  6723. }
  6724. NK_API struct nk_color
  6725. nk_hsv(int h, int s, int v)
  6726. {
  6727. return nk_hsva(h, s, v, 255);
  6728. }
  6729. NK_API struct nk_color
  6730. nk_hsv_iv(const int *c)
  6731. {
  6732. return nk_hsv(c[0], c[1], c[2]);
  6733. }
  6734. NK_API struct nk_color
  6735. nk_hsv_bv(const nk_byte *c)
  6736. {
  6737. return nk_hsv(c[0], c[1], c[2]);
  6738. }
  6739. NK_API struct nk_color
  6740. nk_hsv_f(float h, float s, float v)
  6741. {
  6742. return nk_hsva_f(h, s, v, 1.0f);
  6743. }
  6744. NK_API struct nk_color
  6745. nk_hsv_fv(const float *c)
  6746. {
  6747. return nk_hsv_f(c[0], c[1], c[2]);
  6748. }
  6749. NK_API struct nk_color
  6750. nk_hsva(int h, int s, int v, int a)
  6751. {
  6752. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  6753. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  6754. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  6755. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  6756. return nk_hsva_f(hf, sf, vf, af);
  6757. }
  6758. NK_API struct nk_color
  6759. nk_hsva_iv(const int *c)
  6760. {
  6761. return nk_hsva(c[0], c[1], c[2], c[3]);
  6762. }
  6763. NK_API struct nk_color
  6764. nk_hsva_bv(const nk_byte *c)
  6765. {
  6766. return nk_hsva(c[0], c[1], c[2], c[3]);
  6767. }
  6768. NK_API struct nk_colorf
  6769. nk_hsva_colorf(float h, float s, float v, float a)
  6770. {
  6771. int i;
  6772. float p, q, t, f;
  6773. struct nk_colorf out = {0,0,0,0};
  6774. if (s <= 0.0f) {
  6775. out.r = v; out.g = v; out.b = v; out.a = a;
  6776. return out;
  6777. }
  6778. h = h / (60.0f/360.0f);
  6779. i = (int)h;
  6780. f = h - (float)i;
  6781. p = v * (1.0f - s);
  6782. q = v * (1.0f - (s * f));
  6783. t = v * (1.0f - s * (1.0f - f));
  6784. switch (i) {
  6785. case 0: default: out.r = v; out.g = t; out.b = p; break;
  6786. case 1: out.r = q; out.g = v; out.b = p; break;
  6787. case 2: out.r = p; out.g = v; out.b = t; break;
  6788. case 3: out.r = p; out.g = q; out.b = v; break;
  6789. case 4: out.r = t; out.g = p; out.b = v; break;
  6790. case 5: out.r = v; out.g = p; out.b = q; break;}
  6791. out.a = a;
  6792. return out;
  6793. }
  6794. NK_API struct nk_colorf
  6795. nk_hsva_colorfv(float *c)
  6796. {
  6797. return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
  6798. }
  6799. NK_API struct nk_color
  6800. nk_hsva_f(float h, float s, float v, float a)
  6801. {
  6802. struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
  6803. return nk_rgba_f(c.r, c.g, c.b, c.a);
  6804. }
  6805. NK_API struct nk_color
  6806. nk_hsva_fv(const float *c)
  6807. {
  6808. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  6809. }
  6810. NK_API nk_uint
  6811. nk_color_u32(struct nk_color in)
  6812. {
  6813. nk_uint out = (nk_uint)in.r;
  6814. out |= ((nk_uint)in.g << 8);
  6815. out |= ((nk_uint)in.b << 16);
  6816. out |= ((nk_uint)in.a << 24);
  6817. return out;
  6818. }
  6819. NK_API void
  6820. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  6821. {
  6822. NK_STORAGE const float s = 1.0f/255.0f;
  6823. *r = (float)in.r * s;
  6824. *g = (float)in.g * s;
  6825. *b = (float)in.b * s;
  6826. *a = (float)in.a * s;
  6827. }
  6828. NK_API void
  6829. nk_color_fv(float *c, struct nk_color in)
  6830. {
  6831. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  6832. }
  6833. NK_API struct nk_colorf
  6834. nk_color_cf(struct nk_color in)
  6835. {
  6836. struct nk_colorf o;
  6837. nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
  6838. return o;
  6839. }
  6840. NK_API void
  6841. nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
  6842. {
  6843. NK_STORAGE const double s = 1.0/255.0;
  6844. *r = (double)in.r * s;
  6845. *g = (double)in.g * s;
  6846. *b = (double)in.b * s;
  6847. *a = (double)in.a * s;
  6848. }
  6849. NK_API void
  6850. nk_color_dv(double *c, struct nk_color in)
  6851. {
  6852. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  6853. }
  6854. NK_API void
  6855. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  6856. {
  6857. float a;
  6858. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  6859. }
  6860. NK_API void
  6861. nk_color_hsv_fv(float *out, struct nk_color in)
  6862. {
  6863. float a;
  6864. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  6865. }
  6866. NK_API void
  6867. nk_colorf_hsva_f(float *out_h, float *out_s,
  6868. float *out_v, float *out_a, struct nk_colorf in)
  6869. {
  6870. float chroma;
  6871. float K = 0.0f;
  6872. if (in.g < in.b) {
  6873. const float t = in.g; in.g = in.b; in.b = t;
  6874. K = -1.f;
  6875. }
  6876. if (in.r < in.g) {
  6877. const float t = in.r; in.r = in.g; in.g = t;
  6878. K = -2.f/6.0f - K;
  6879. }
  6880. chroma = in.r - ((in.g < in.b) ? in.g: in.b);
  6881. *out_h = NK_ABS(K + (in.g - in.b)/(6.0f * chroma + 1e-20f));
  6882. *out_s = chroma / (in.r + 1e-20f);
  6883. *out_v = in.r;
  6884. *out_a = in.a;
  6885. }
  6886. NK_API void
  6887. nk_colorf_hsva_fv(float *hsva, struct nk_colorf in)
  6888. {
  6889. nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
  6890. }
  6891. NK_API void
  6892. nk_color_hsva_f(float *out_h, float *out_s,
  6893. float *out_v, float *out_a, struct nk_color in)
  6894. {
  6895. struct nk_colorf col;
  6896. nk_color_f(&col.r,&col.g,&col.b,&col.a, in);
  6897. nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
  6898. }
  6899. NK_API void
  6900. nk_color_hsva_fv(float *out, struct nk_color in)
  6901. {
  6902. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  6903. }
  6904. NK_API void
  6905. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  6906. int *out_a, struct nk_color in)
  6907. {
  6908. float h,s,v,a;
  6909. nk_color_hsva_f(&h, &s, &v, &a, in);
  6910. *out_h = (nk_byte)(h * 255.0f);
  6911. *out_s = (nk_byte)(s * 255.0f);
  6912. *out_v = (nk_byte)(v * 255.0f);
  6913. *out_a = (nk_byte)(a * 255.0f);
  6914. }
  6915. NK_API void
  6916. nk_color_hsva_iv(int *out, struct nk_color in)
  6917. {
  6918. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  6919. }
  6920. NK_API void
  6921. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  6922. {
  6923. int tmp[4];
  6924. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  6925. out[0] = (nk_byte)tmp[0];
  6926. out[1] = (nk_byte)tmp[1];
  6927. out[2] = (nk_byte)tmp[2];
  6928. out[3] = (nk_byte)tmp[3];
  6929. }
  6930. NK_API void
  6931. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  6932. {
  6933. int tmp[4];
  6934. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  6935. *h = (nk_byte)tmp[0];
  6936. *s = (nk_byte)tmp[1];
  6937. *v = (nk_byte)tmp[2];
  6938. *a = (nk_byte)tmp[3];
  6939. }
  6940. NK_API void
  6941. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  6942. {
  6943. int a;
  6944. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  6945. }
  6946. NK_API void
  6947. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  6948. {
  6949. int tmp[4];
  6950. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  6951. *out_h = (nk_byte)tmp[0];
  6952. *out_s = (nk_byte)tmp[1];
  6953. *out_v = (nk_byte)tmp[2];
  6954. }
  6955. NK_API void
  6956. nk_color_hsv_iv(int *out, struct nk_color in)
  6957. {
  6958. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  6959. }
  6960. NK_API void
  6961. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  6962. {
  6963. int tmp[4];
  6964. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  6965. out[0] = (nk_byte)tmp[0];
  6966. out[1] = (nk_byte)tmp[1];
  6967. out[2] = (nk_byte)tmp[2];
  6968. }
  6969. /*
  6970. * ==============================================================
  6971. *
  6972. * IMAGE
  6973. *
  6974. * ===============================================================
  6975. */
  6976. NK_API nk_handle
  6977. nk_handle_ptr(void *ptr)
  6978. {
  6979. nk_handle handle = {0};
  6980. handle.ptr = ptr;
  6981. return handle;
  6982. }
  6983. NK_API nk_handle
  6984. nk_handle_id(int id)
  6985. {
  6986. nk_handle handle;
  6987. nk_zero_struct(handle);
  6988. handle.id = id;
  6989. return handle;
  6990. }
  6991. NK_API struct nk_image
  6992. nk_subimage_ptr(void *ptr, unsigned short w, unsigned short h, struct nk_rect r)
  6993. {
  6994. struct nk_image s;
  6995. nk_zero(&s, sizeof(s));
  6996. s.handle.ptr = ptr;
  6997. s.w = w; s.h = h;
  6998. s.region[0] = (unsigned short)r.x;
  6999. s.region[1] = (unsigned short)r.y;
  7000. s.region[2] = (unsigned short)r.w;
  7001. s.region[3] = (unsigned short)r.h;
  7002. return s;
  7003. }
  7004. NK_API struct nk_image
  7005. nk_subimage_id(int id, unsigned short w, unsigned short h, struct nk_rect r)
  7006. {
  7007. struct nk_image s;
  7008. nk_zero(&s, sizeof(s));
  7009. s.handle.id = id;
  7010. s.w = w; s.h = h;
  7011. s.region[0] = (unsigned short)r.x;
  7012. s.region[1] = (unsigned short)r.y;
  7013. s.region[2] = (unsigned short)r.w;
  7014. s.region[3] = (unsigned short)r.h;
  7015. return s;
  7016. }
  7017. NK_API struct nk_image
  7018. nk_subimage_handle(nk_handle handle, unsigned short w, unsigned short h,
  7019. struct nk_rect r)
  7020. {
  7021. struct nk_image s;
  7022. nk_zero(&s, sizeof(s));
  7023. s.handle = handle;
  7024. s.w = w; s.h = h;
  7025. s.region[0] = (unsigned short)r.x;
  7026. s.region[1] = (unsigned short)r.y;
  7027. s.region[2] = (unsigned short)r.w;
  7028. s.region[3] = (unsigned short)r.h;
  7029. return s;
  7030. }
  7031. NK_API struct nk_image
  7032. nk_image_handle(nk_handle handle)
  7033. {
  7034. struct nk_image s;
  7035. nk_zero(&s, sizeof(s));
  7036. s.handle = handle;
  7037. s.w = 0; s.h = 0;
  7038. s.region[0] = 0;
  7039. s.region[1] = 0;
  7040. s.region[2] = 0;
  7041. s.region[3] = 0;
  7042. return s;
  7043. }
  7044. NK_API struct nk_image
  7045. nk_image_ptr(void *ptr)
  7046. {
  7047. struct nk_image s;
  7048. nk_zero(&s, sizeof(s));
  7049. NK_ASSERT(ptr);
  7050. s.handle.ptr = ptr;
  7051. s.w = 0; s.h = 0;
  7052. s.region[0] = 0;
  7053. s.region[1] = 0;
  7054. s.region[2] = 0;
  7055. s.region[3] = 0;
  7056. return s;
  7057. }
  7058. NK_API struct nk_image
  7059. nk_image_id(int id)
  7060. {
  7061. struct nk_image s;
  7062. nk_zero(&s, sizeof(s));
  7063. s.handle.id = id;
  7064. s.w = 0; s.h = 0;
  7065. s.region[0] = 0;
  7066. s.region[1] = 0;
  7067. s.region[2] = 0;
  7068. s.region[3] = 0;
  7069. return s;
  7070. }
  7071. NK_API int
  7072. nk_image_is_subimage(const struct nk_image* img)
  7073. {
  7074. NK_ASSERT(img);
  7075. return !(img->w == 0 && img->h == 0);
  7076. }
  7077. NK_INTERN void
  7078. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  7079. float x1, float y1)
  7080. {
  7081. NK_ASSERT(a);
  7082. NK_ASSERT(clip);
  7083. clip->x = NK_MAX(a->x, x0);
  7084. clip->y = NK_MAX(a->y, y0);
  7085. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  7086. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  7087. clip->w = NK_MAX(0, clip->w);
  7088. clip->h = NK_MAX(0, clip->h);
  7089. }
  7090. NK_API void
  7091. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  7092. float pad_x, float pad_y, enum nk_heading direction)
  7093. {
  7094. float w_half, h_half;
  7095. NK_ASSERT(result);
  7096. r.w = NK_MAX(2 * pad_x, r.w);
  7097. r.h = NK_MAX(2 * pad_y, r.h);
  7098. r.w = r.w - 2 * pad_x;
  7099. r.h = r.h - 2 * pad_y;
  7100. r.x = r.x + pad_x;
  7101. r.y = r.y + pad_y;
  7102. w_half = r.w / 2.0f;
  7103. h_half = r.h / 2.0f;
  7104. if (direction == NK_UP) {
  7105. result[0] = nk_vec2(r.x + w_half, r.y);
  7106. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  7107. result[2] = nk_vec2(r.x, r.y + r.h);
  7108. } else if (direction == NK_RIGHT) {
  7109. result[0] = nk_vec2(r.x, r.y);
  7110. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  7111. result[2] = nk_vec2(r.x, r.y + r.h);
  7112. } else if (direction == NK_DOWN) {
  7113. result[0] = nk_vec2(r.x, r.y);
  7114. result[1] = nk_vec2(r.x + r.w, r.y);
  7115. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  7116. } else {
  7117. result[0] = nk_vec2(r.x, r.y + h_half);
  7118. result[1] = nk_vec2(r.x + r.w, r.y);
  7119. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  7120. }
  7121. }
  7122. NK_INTERN int
  7123. nk_text_clamp(const struct nk_user_font *font, const char *text,
  7124. int text_len, float space, int *glyphs, float *text_width,
  7125. nk_rune *sep_list, int sep_count)
  7126. {
  7127. int i = 0;
  7128. int glyph_len = 0;
  7129. float last_width = 0;
  7130. nk_rune unicode = 0;
  7131. float width = 0;
  7132. int len = 0;
  7133. int g = 0;
  7134. float s;
  7135. int sep_len = 0;
  7136. int sep_g = 0;
  7137. float sep_width = 0;
  7138. sep_count = NK_MAX(sep_count,0);
  7139. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7140. while (glyph_len && (width < space) && (len < text_len)) {
  7141. len += glyph_len;
  7142. s = font->width(font->userdata, font->height, text, len);
  7143. for (i = 0; i < sep_count; ++i) {
  7144. if (unicode != sep_list[i]) continue;
  7145. sep_width = last_width = width;
  7146. sep_g = g+1;
  7147. sep_len = len;
  7148. break;
  7149. }
  7150. if (i == sep_count){
  7151. last_width = sep_width = width;
  7152. sep_g = g+1;
  7153. }
  7154. width = s;
  7155. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  7156. g++;
  7157. }
  7158. if (len >= text_len) {
  7159. *glyphs = g;
  7160. *text_width = last_width;
  7161. return len;
  7162. } else {
  7163. *glyphs = sep_g;
  7164. *text_width = sep_width;
  7165. return (!sep_len) ? len: sep_len;
  7166. }
  7167. }
  7168. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  7169. NK_INTERN struct nk_vec2
  7170. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  7171. const char *begin, int byte_len, float row_height, const char **remaining,
  7172. struct nk_vec2 *out_offset, int *glyphs, int op)
  7173. {
  7174. float line_height = row_height;
  7175. struct nk_vec2 text_size = nk_vec2(0,0);
  7176. float line_width = 0.0f;
  7177. float glyph_width;
  7178. int glyph_len = 0;
  7179. nk_rune unicode = 0;
  7180. int text_len = 0;
  7181. if (!begin || byte_len <= 0 || !font)
  7182. return nk_vec2(0,row_height);
  7183. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  7184. if (!glyph_len) return text_size;
  7185. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  7186. *glyphs = 0;
  7187. while ((text_len < byte_len) && glyph_len) {
  7188. if (unicode == '\n') {
  7189. text_size.x = NK_MAX(text_size.x, line_width);
  7190. text_size.y += line_height;
  7191. line_width = 0;
  7192. *glyphs+=1;
  7193. if (op == NK_STOP_ON_NEW_LINE)
  7194. break;
  7195. text_len++;
  7196. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7197. continue;
  7198. }
  7199. if (unicode == '\r') {
  7200. text_len++;
  7201. *glyphs+=1;
  7202. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7203. continue;
  7204. }
  7205. *glyphs = *glyphs + 1;
  7206. text_len += glyph_len;
  7207. line_width += (float)glyph_width;
  7208. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7209. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  7210. continue;
  7211. }
  7212. if (text_size.x < line_width)
  7213. text_size.x = line_width;
  7214. if (out_offset)
  7215. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  7216. if (line_width > 0 || text_size.y == 0.0f)
  7217. text_size.y += line_height;
  7218. if (remaining)
  7219. *remaining = begin+text_len;
  7220. return text_size;
  7221. }
  7222. /* ==============================================================
  7223. *
  7224. * UTF-8
  7225. *
  7226. * ===============================================================*/
  7227. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  7228. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  7229. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  7230. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  7231. NK_INTERN int
  7232. nk_utf_validate(nk_rune *u, int i)
  7233. {
  7234. NK_ASSERT(u);
  7235. if (!u) return 0;
  7236. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  7237. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  7238. *u = NK_UTF_INVALID;
  7239. for (i = 1; *u > nk_utfmax[i]; ++i);
  7240. return i;
  7241. }
  7242. NK_INTERN nk_rune
  7243. nk_utf_decode_byte(char c, int *i)
  7244. {
  7245. NK_ASSERT(i);
  7246. if (!i) return 0;
  7247. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  7248. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  7249. return (nk_byte)(c & ~nk_utfmask[*i]);
  7250. }
  7251. return 0;
  7252. }
  7253. NK_API int
  7254. nk_utf_decode(const char *c, nk_rune *u, int clen)
  7255. {
  7256. int i, j, len, type=0;
  7257. nk_rune udecoded;
  7258. NK_ASSERT(c);
  7259. NK_ASSERT(u);
  7260. if (!c || !u) return 0;
  7261. if (!clen) return 0;
  7262. *u = NK_UTF_INVALID;
  7263. udecoded = nk_utf_decode_byte(c[0], &len);
  7264. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  7265. return 1;
  7266. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  7267. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  7268. if (type != 0)
  7269. return j;
  7270. }
  7271. if (j < len)
  7272. return 0;
  7273. *u = udecoded;
  7274. nk_utf_validate(u, len);
  7275. return len;
  7276. }
  7277. NK_INTERN char
  7278. nk_utf_encode_byte(nk_rune u, int i)
  7279. {
  7280. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  7281. }
  7282. NK_API int
  7283. nk_utf_encode(nk_rune u, char *c, int clen)
  7284. {
  7285. int len, i;
  7286. len = nk_utf_validate(&u, 0);
  7287. if (clen < len || !len || len > NK_UTF_SIZE)
  7288. return 0;
  7289. for (i = len - 1; i != 0; --i) {
  7290. c[i] = nk_utf_encode_byte(u, 0);
  7291. u >>= 6;
  7292. }
  7293. c[0] = nk_utf_encode_byte(u, len);
  7294. return len;
  7295. }
  7296. NK_API int
  7297. nk_utf_len(const char *str, int len)
  7298. {
  7299. const char *text;
  7300. int glyphs = 0;
  7301. int text_len;
  7302. int glyph_len;
  7303. int src_len = 0;
  7304. nk_rune unicode;
  7305. NK_ASSERT(str);
  7306. if (!str || !len) return 0;
  7307. text = str;
  7308. text_len = len;
  7309. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7310. while (glyph_len && src_len < len) {
  7311. glyphs++;
  7312. src_len = src_len + glyph_len;
  7313. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  7314. }
  7315. return glyphs;
  7316. }
  7317. NK_API const char*
  7318. nk_utf_at(const char *buffer, int length, int index,
  7319. nk_rune *unicode, int *len)
  7320. {
  7321. int i = 0;
  7322. int src_len = 0;
  7323. int glyph_len = 0;
  7324. const char *text;
  7325. int text_len;
  7326. NK_ASSERT(buffer);
  7327. NK_ASSERT(unicode);
  7328. NK_ASSERT(len);
  7329. if (!buffer || !unicode || !len) return 0;
  7330. if (index < 0) {
  7331. *unicode = NK_UTF_INVALID;
  7332. *len = 0;
  7333. return 0;
  7334. }
  7335. text = buffer;
  7336. text_len = length;
  7337. glyph_len = nk_utf_decode(text, unicode, text_len);
  7338. while (glyph_len) {
  7339. if (i == index) {
  7340. *len = glyph_len;
  7341. break;
  7342. }
  7343. i++;
  7344. src_len = src_len + glyph_len;
  7345. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7346. }
  7347. if (i != index) return 0;
  7348. return buffer + src_len;
  7349. }
  7350. /* ==============================================================
  7351. *
  7352. * BUFFER
  7353. *
  7354. * ===============================================================*/
  7355. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7356. NK_INTERN void* nk_malloc(nk_handle unused, void *old,nk_size size)
  7357. {NK_UNUSED(unused); NK_UNUSED(old); return malloc(size);}
  7358. NK_INTERN void nk_mfree(nk_handle unused, void *ptr)
  7359. {NK_UNUSED(unused); free(ptr);}
  7360. NK_API void
  7361. nk_buffer_init_default(struct nk_buffer *buffer)
  7362. {
  7363. struct nk_allocator alloc;
  7364. alloc.userdata.ptr = 0;
  7365. alloc.alloc = nk_malloc;
  7366. alloc.free = nk_mfree;
  7367. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  7368. }
  7369. #endif
  7370. NK_API void
  7371. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  7372. nk_size initial_size)
  7373. {
  7374. NK_ASSERT(b);
  7375. NK_ASSERT(a);
  7376. NK_ASSERT(initial_size);
  7377. if (!b || !a || !initial_size) return;
  7378. nk_zero(b, sizeof(*b));
  7379. b->type = NK_BUFFER_DYNAMIC;
  7380. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  7381. b->memory.size = initial_size;
  7382. b->size = initial_size;
  7383. b->grow_factor = 2.0f;
  7384. b->pool = *a;
  7385. }
  7386. NK_API void
  7387. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  7388. {
  7389. NK_ASSERT(b);
  7390. NK_ASSERT(m);
  7391. NK_ASSERT(size);
  7392. if (!b || !m || !size) return;
  7393. nk_zero(b, sizeof(*b));
  7394. b->type = NK_BUFFER_FIXED;
  7395. b->memory.ptr = m;
  7396. b->memory.size = size;
  7397. b->size = size;
  7398. }
  7399. NK_INTERN void*
  7400. nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment,
  7401. enum nk_buffer_allocation_type type)
  7402. {
  7403. void *memory = 0;
  7404. switch (type) {
  7405. default:
  7406. case NK_BUFFER_MAX:
  7407. case NK_BUFFER_FRONT:
  7408. if (align) {
  7409. memory = NK_ALIGN_PTR(unaligned, align);
  7410. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  7411. } else {
  7412. memory = unaligned;
  7413. *alignment = 0;
  7414. }
  7415. break;
  7416. case NK_BUFFER_BACK:
  7417. if (align) {
  7418. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  7419. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  7420. } else {
  7421. memory = unaligned;
  7422. *alignment = 0;
  7423. }
  7424. break;
  7425. }
  7426. return memory;
  7427. }
  7428. NK_INTERN void*
  7429. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  7430. {
  7431. void *temp;
  7432. nk_size buffer_size;
  7433. NK_ASSERT(b);
  7434. NK_ASSERT(size);
  7435. if (!b || !size || !b->pool.alloc || !b->pool.free)
  7436. return 0;
  7437. buffer_size = b->memory.size;
  7438. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  7439. NK_ASSERT(temp);
  7440. if (!temp) return 0;
  7441. *size = capacity;
  7442. if (temp != b->memory.ptr) {
  7443. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  7444. b->pool.free(b->pool.userdata, b->memory.ptr);
  7445. }
  7446. if (b->size == buffer_size) {
  7447. /* no back buffer so just set correct size */
  7448. b->size = capacity;
  7449. return temp;
  7450. } else {
  7451. /* copy back buffer to the end of the new buffer */
  7452. void *dst, *src;
  7453. nk_size back_size;
  7454. back_size = buffer_size - b->size;
  7455. dst = nk_ptr_add(void, temp, capacity - back_size);
  7456. src = nk_ptr_add(void, temp, b->size);
  7457. NK_MEMCPY(dst, src, back_size);
  7458. b->size = capacity - back_size;
  7459. }
  7460. return temp;
  7461. }
  7462. NK_INTERN void*
  7463. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7464. nk_size size, nk_size align)
  7465. {
  7466. int full;
  7467. nk_size alignment;
  7468. void *unaligned;
  7469. void *memory;
  7470. NK_ASSERT(b);
  7471. NK_ASSERT(size);
  7472. if (!b || !size) return 0;
  7473. b->needed += size;
  7474. /* calculate total size with needed alignment + size */
  7475. if (type == NK_BUFFER_FRONT)
  7476. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7477. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7478. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7479. /* check if buffer has enough memory*/
  7480. if (type == NK_BUFFER_FRONT)
  7481. full = ((b->allocated + size + alignment) > b->size);
  7482. else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
  7483. if (full) {
  7484. nk_size capacity;
  7485. if (b->type != NK_BUFFER_DYNAMIC)
  7486. return 0;
  7487. NK_ASSERT(b->pool.alloc && b->pool.free);
  7488. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  7489. return 0;
  7490. /* buffer is full so allocate bigger buffer if dynamic */
  7491. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  7492. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  7493. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  7494. if (!b->memory.ptr) return 0;
  7495. /* align newly allocated pointer */
  7496. if (type == NK_BUFFER_FRONT)
  7497. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7498. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7499. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7500. }
  7501. if (type == NK_BUFFER_FRONT)
  7502. b->allocated += size + alignment;
  7503. else b->size -= (size + alignment);
  7504. b->needed += alignment;
  7505. b->calls++;
  7506. return memory;
  7507. }
  7508. NK_API void
  7509. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7510. const void *memory, nk_size size, nk_size align)
  7511. {
  7512. void *mem = nk_buffer_alloc(b, type, size, align);
  7513. if (!mem) return;
  7514. NK_MEMCPY(mem, memory, size);
  7515. }
  7516. NK_API void
  7517. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7518. {
  7519. NK_ASSERT(buffer);
  7520. if (!buffer) return;
  7521. buffer->marker[type].active = nk_true;
  7522. if (type == NK_BUFFER_BACK)
  7523. buffer->marker[type].offset = buffer->size;
  7524. else buffer->marker[type].offset = buffer->allocated;
  7525. }
  7526. NK_API void
  7527. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  7528. {
  7529. NK_ASSERT(buffer);
  7530. if (!buffer) return;
  7531. if (type == NK_BUFFER_BACK) {
  7532. /* reset back buffer either back to marker or empty */
  7533. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  7534. if (buffer->marker[type].active)
  7535. buffer->size = buffer->marker[type].offset;
  7536. else buffer->size = buffer->memory.size;
  7537. buffer->marker[type].active = nk_false;
  7538. } else {
  7539. /* reset front buffer either back to back marker or empty */
  7540. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  7541. if (buffer->marker[type].active)
  7542. buffer->allocated = buffer->marker[type].offset;
  7543. else buffer->allocated = 0;
  7544. buffer->marker[type].active = nk_false;
  7545. }
  7546. }
  7547. NK_API void
  7548. nk_buffer_clear(struct nk_buffer *b)
  7549. {
  7550. NK_ASSERT(b);
  7551. if (!b) return;
  7552. b->allocated = 0;
  7553. b->size = b->memory.size;
  7554. b->calls = 0;
  7555. b->needed = 0;
  7556. }
  7557. NK_API void
  7558. nk_buffer_free(struct nk_buffer *b)
  7559. {
  7560. NK_ASSERT(b);
  7561. if (!b || !b->memory.ptr) return;
  7562. if (b->type == NK_BUFFER_FIXED) return;
  7563. if (!b->pool.free) return;
  7564. NK_ASSERT(b->pool.free);
  7565. b->pool.free(b->pool.userdata, b->memory.ptr);
  7566. }
  7567. NK_API void
  7568. nk_buffer_info(struct nk_memory_status *s, struct nk_buffer *b)
  7569. {
  7570. NK_ASSERT(b);
  7571. NK_ASSERT(s);
  7572. if (!s || !b) return;
  7573. s->allocated = b->allocated;
  7574. s->size = b->memory.size;
  7575. s->needed = b->needed;
  7576. s->memory = b->memory.ptr;
  7577. s->calls = b->calls;
  7578. }
  7579. NK_API void*
  7580. nk_buffer_memory(struct nk_buffer *buffer)
  7581. {
  7582. NK_ASSERT(buffer);
  7583. if (!buffer) return 0;
  7584. return buffer->memory.ptr;
  7585. }
  7586. NK_API const void*
  7587. nk_buffer_memory_const(const struct nk_buffer *buffer)
  7588. {
  7589. NK_ASSERT(buffer);
  7590. if (!buffer) return 0;
  7591. return buffer->memory.ptr;
  7592. }
  7593. NK_API nk_size
  7594. nk_buffer_total(struct nk_buffer *buffer)
  7595. {
  7596. NK_ASSERT(buffer);
  7597. if (!buffer) return 0;
  7598. return buffer->memory.size;
  7599. }
  7600. /*
  7601. * ==============================================================
  7602. *
  7603. * STRING
  7604. *
  7605. * ===============================================================
  7606. */
  7607. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7608. NK_API void
  7609. nk_str_init_default(struct nk_str *str)
  7610. {
  7611. struct nk_allocator alloc;
  7612. alloc.userdata.ptr = 0;
  7613. alloc.alloc = nk_malloc;
  7614. alloc.free = nk_mfree;
  7615. nk_buffer_init(&str->buffer, &alloc, 32);
  7616. str->len = 0;
  7617. }
  7618. #endif
  7619. NK_API void
  7620. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  7621. {
  7622. nk_buffer_init(&str->buffer, alloc, size);
  7623. str->len = 0;
  7624. }
  7625. NK_API void
  7626. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  7627. {
  7628. nk_buffer_init_fixed(&str->buffer, memory, size);
  7629. str->len = 0;
  7630. }
  7631. NK_API int
  7632. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  7633. {
  7634. char *mem;
  7635. NK_ASSERT(s);
  7636. NK_ASSERT(str);
  7637. if (!s || !str || !len) return 0;
  7638. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7639. if (!mem) return 0;
  7640. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7641. s->len += nk_utf_len(str, len);
  7642. return len;
  7643. }
  7644. NK_API int
  7645. nk_str_append_str_char(struct nk_str *s, const char *str)
  7646. {
  7647. return nk_str_append_text_char(s, str, nk_strlen(str));
  7648. }
  7649. NK_API int
  7650. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  7651. {
  7652. int i = 0;
  7653. int byte_len = 0;
  7654. nk_rune unicode;
  7655. if (!str || !text || !len) return 0;
  7656. for (i = 0; i < len; ++i)
  7657. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7658. nk_str_append_text_char(str, text, byte_len);
  7659. return len;
  7660. }
  7661. NK_API int
  7662. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  7663. {
  7664. int runes = 0;
  7665. int byte_len = 0;
  7666. int num_runes = 0;
  7667. int glyph_len = 0;
  7668. nk_rune unicode;
  7669. if (!str || !text) return 0;
  7670. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7671. while (unicode != '\0' && glyph_len) {
  7672. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7673. byte_len += glyph_len;
  7674. num_runes++;
  7675. }
  7676. nk_str_append_text_char(str, text, byte_len);
  7677. return runes;
  7678. }
  7679. NK_API int
  7680. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  7681. {
  7682. int i = 0;
  7683. int byte_len = 0;
  7684. nk_glyph glyph;
  7685. NK_ASSERT(str);
  7686. if (!str || !text || !len) return 0;
  7687. for (i = 0; i < len; ++i) {
  7688. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  7689. if (!byte_len) break;
  7690. nk_str_append_text_char(str, glyph, byte_len);
  7691. }
  7692. return len;
  7693. }
  7694. NK_API int
  7695. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  7696. {
  7697. int i = 0;
  7698. nk_glyph glyph;
  7699. int byte_len;
  7700. NK_ASSERT(str);
  7701. if (!str || !runes) return 0;
  7702. while (runes[i] != '\0') {
  7703. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7704. nk_str_append_text_char(str, glyph, byte_len);
  7705. i++;
  7706. }
  7707. return i;
  7708. }
  7709. NK_API int
  7710. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  7711. {
  7712. int i;
  7713. void *mem;
  7714. char *src;
  7715. char *dst;
  7716. int copylen;
  7717. NK_ASSERT(s);
  7718. NK_ASSERT(str);
  7719. NK_ASSERT(len >= 0);
  7720. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  7721. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  7722. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  7723. copylen = (int)s->buffer.allocated - pos;
  7724. if (!copylen) {
  7725. nk_str_append_text_char(s, str, len);
  7726. return 1;
  7727. }
  7728. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  7729. if (!mem) return 0;
  7730. /* memmove */
  7731. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  7732. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  7733. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  7734. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  7735. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  7736. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  7737. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  7738. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7739. return 1;
  7740. }
  7741. NK_API int
  7742. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  7743. {
  7744. int glyph_len;
  7745. nk_rune unicode;
  7746. const char *begin;
  7747. const char *buffer;
  7748. NK_ASSERT(str);
  7749. NK_ASSERT(cstr);
  7750. NK_ASSERT(len);
  7751. if (!str || !cstr || !len) return 0;
  7752. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  7753. if (!str->len)
  7754. return nk_str_append_text_char(str, cstr, len);
  7755. buffer = nk_str_get_const(str);
  7756. if (!begin) return 0;
  7757. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  7758. }
  7759. NK_API int
  7760. nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  7761. {
  7762. return nk_str_insert_text_utf8(str, pos, text, len);
  7763. }
  7764. NK_API int
  7765. nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  7766. {
  7767. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  7768. }
  7769. NK_API int
  7770. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  7771. {
  7772. int i = 0;
  7773. int byte_len = 0;
  7774. nk_rune unicode;
  7775. NK_ASSERT(str);
  7776. NK_ASSERT(text);
  7777. if (!str || !text || !len) return 0;
  7778. for (i = 0; i < len; ++i)
  7779. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  7780. nk_str_insert_at_rune(str, pos, text, byte_len);
  7781. return len;
  7782. }
  7783. NK_API int
  7784. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  7785. {
  7786. int runes = 0;
  7787. int byte_len = 0;
  7788. int num_runes = 0;
  7789. int glyph_len = 0;
  7790. nk_rune unicode;
  7791. if (!str || !text) return 0;
  7792. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7793. while (unicode != '\0' && glyph_len) {
  7794. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  7795. byte_len += glyph_len;
  7796. num_runes++;
  7797. }
  7798. nk_str_insert_at_rune(str, pos, text, byte_len);
  7799. return runes;
  7800. }
  7801. NK_API int
  7802. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  7803. {
  7804. int i = 0;
  7805. int byte_len = 0;
  7806. nk_glyph glyph;
  7807. NK_ASSERT(str);
  7808. if (!str || !runes || !len) return 0;
  7809. for (i = 0; i < len; ++i) {
  7810. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7811. if (!byte_len) break;
  7812. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  7813. }
  7814. return len;
  7815. }
  7816. NK_API int
  7817. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  7818. {
  7819. int i = 0;
  7820. nk_glyph glyph;
  7821. int byte_len;
  7822. NK_ASSERT(str);
  7823. if (!str || !runes) return 0;
  7824. while (runes[i] != '\0') {
  7825. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  7826. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  7827. i++;
  7828. }
  7829. return i;
  7830. }
  7831. NK_API void
  7832. nk_str_remove_chars(struct nk_str *s, int len)
  7833. {
  7834. NK_ASSERT(s);
  7835. NK_ASSERT(len >= 0);
  7836. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  7837. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  7838. s->buffer.allocated -= (nk_size)len;
  7839. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7840. }
  7841. NK_API void
  7842. nk_str_remove_runes(struct nk_str *str, int len)
  7843. {
  7844. int index;
  7845. const char *begin;
  7846. const char *end;
  7847. nk_rune unicode;
  7848. NK_ASSERT(str);
  7849. NK_ASSERT(len >= 0);
  7850. if (!str || len < 0) return;
  7851. if (len >= str->len) {
  7852. str->len = 0;
  7853. return;
  7854. }
  7855. index = str->len - len;
  7856. begin = nk_str_at_rune(str, index, &unicode, &len);
  7857. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  7858. nk_str_remove_chars(str, (int)(end-begin)+1);
  7859. }
  7860. NK_API void
  7861. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  7862. {
  7863. NK_ASSERT(s);
  7864. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  7865. (nk_size)(pos + len) > s->buffer.allocated) return;
  7866. if ((nk_size)(pos + len) < s->buffer.allocated) {
  7867. /* memmove */
  7868. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  7869. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  7870. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  7871. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  7872. s->buffer.allocated -= (nk_size)len;
  7873. } else nk_str_remove_chars(s, len);
  7874. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  7875. }
  7876. NK_API void
  7877. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  7878. {
  7879. char *temp;
  7880. nk_rune unicode;
  7881. char *begin;
  7882. char *end;
  7883. int unused;
  7884. NK_ASSERT(s);
  7885. NK_ASSERT(s->len >= pos + len);
  7886. if (s->len < pos + len)
  7887. len = NK_CLAMP(0, (s->len - pos), s->len);
  7888. if (!len) return;
  7889. temp = (char *)s->buffer.memory.ptr;
  7890. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  7891. if (!begin) return;
  7892. s->buffer.memory.ptr = begin;
  7893. end = nk_str_at_rune(s, len, &unicode, &unused);
  7894. s->buffer.memory.ptr = temp;
  7895. if (!end) return;
  7896. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  7897. }
  7898. NK_API char*
  7899. nk_str_at_char(struct nk_str *s, int pos)
  7900. {
  7901. NK_ASSERT(s);
  7902. if (!s || pos > (int)s->buffer.allocated) return 0;
  7903. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  7904. }
  7905. NK_API char*
  7906. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  7907. {
  7908. int i = 0;
  7909. int src_len = 0;
  7910. int glyph_len = 0;
  7911. char *text;
  7912. int text_len;
  7913. NK_ASSERT(str);
  7914. NK_ASSERT(unicode);
  7915. NK_ASSERT(len);
  7916. if (!str || !unicode || !len) return 0;
  7917. if (pos < 0) {
  7918. *unicode = 0;
  7919. *len = 0;
  7920. return 0;
  7921. }
  7922. text = (char*)str->buffer.memory.ptr;
  7923. text_len = (int)str->buffer.allocated;
  7924. glyph_len = nk_utf_decode(text, unicode, text_len);
  7925. while (glyph_len) {
  7926. if (i == pos) {
  7927. *len = glyph_len;
  7928. break;
  7929. }
  7930. i++;
  7931. src_len = src_len + glyph_len;
  7932. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7933. }
  7934. if (i != pos) return 0;
  7935. return text + src_len;
  7936. }
  7937. NK_API const char*
  7938. nk_str_at_char_const(const struct nk_str *s, int pos)
  7939. {
  7940. NK_ASSERT(s);
  7941. if (!s || pos > (int)s->buffer.allocated) return 0;
  7942. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  7943. }
  7944. NK_API const char*
  7945. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  7946. {
  7947. int i = 0;
  7948. int src_len = 0;
  7949. int glyph_len = 0;
  7950. char *text;
  7951. int text_len;
  7952. NK_ASSERT(str);
  7953. NK_ASSERT(unicode);
  7954. NK_ASSERT(len);
  7955. if (!str || !unicode || !len) return 0;
  7956. if (pos < 0) {
  7957. *unicode = 0;
  7958. *len = 0;
  7959. return 0;
  7960. }
  7961. text = (char*)str->buffer.memory.ptr;
  7962. text_len = (int)str->buffer.allocated;
  7963. glyph_len = nk_utf_decode(text, unicode, text_len);
  7964. while (glyph_len) {
  7965. if (i == pos) {
  7966. *len = glyph_len;
  7967. break;
  7968. }
  7969. i++;
  7970. src_len = src_len + glyph_len;
  7971. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7972. }
  7973. if (i != pos) return 0;
  7974. return text + src_len;
  7975. }
  7976. NK_API nk_rune
  7977. nk_str_rune_at(const struct nk_str *str, int pos)
  7978. {
  7979. int len;
  7980. nk_rune unicode = 0;
  7981. nk_str_at_const(str, pos, &unicode, &len);
  7982. return unicode;
  7983. }
  7984. NK_API char*
  7985. nk_str_get(struct nk_str *s)
  7986. {
  7987. NK_ASSERT(s);
  7988. if (!s || !s->len || !s->buffer.allocated) return 0;
  7989. return (char*)s->buffer.memory.ptr;
  7990. }
  7991. NK_API const char*
  7992. nk_str_get_const(const struct nk_str *s)
  7993. {
  7994. NK_ASSERT(s);
  7995. if (!s || !s->len || !s->buffer.allocated) return 0;
  7996. return (const char*)s->buffer.memory.ptr;
  7997. }
  7998. NK_API int
  7999. nk_str_len(struct nk_str *s)
  8000. {
  8001. NK_ASSERT(s);
  8002. if (!s || !s->len || !s->buffer.allocated) return 0;
  8003. return s->len;
  8004. }
  8005. NK_API int
  8006. nk_str_len_char(struct nk_str *s)
  8007. {
  8008. NK_ASSERT(s);
  8009. if (!s || !s->len || !s->buffer.allocated) return 0;
  8010. return (int)s->buffer.allocated;
  8011. }
  8012. NK_API void
  8013. nk_str_clear(struct nk_str *str)
  8014. {
  8015. NK_ASSERT(str);
  8016. nk_buffer_clear(&str->buffer);
  8017. str->len = 0;
  8018. }
  8019. NK_API void
  8020. nk_str_free(struct nk_str *str)
  8021. {
  8022. NK_ASSERT(str);
  8023. nk_buffer_free(&str->buffer);
  8024. str->len = 0;
  8025. }
  8026. /*
  8027. * ==============================================================
  8028. *
  8029. * Command buffer
  8030. *
  8031. * ===============================================================
  8032. */
  8033. NK_INTERN void
  8034. nk_command_buffer_init(struct nk_command_buffer *cmdbuf,
  8035. struct nk_buffer *buffer, enum nk_command_clipping clip)
  8036. {
  8037. NK_ASSERT(cmdbuf);
  8038. NK_ASSERT(buffer);
  8039. if (!cmdbuf || !buffer) return;
  8040. cmdbuf->base = buffer;
  8041. cmdbuf->use_clipping = clip;
  8042. cmdbuf->begin = buffer->allocated;
  8043. cmdbuf->end = buffer->allocated;
  8044. cmdbuf->last = buffer->allocated;
  8045. }
  8046. NK_INTERN void
  8047. nk_command_buffer_reset(struct nk_command_buffer *buffer)
  8048. {
  8049. NK_ASSERT(buffer);
  8050. if (!buffer) return;
  8051. buffer->begin = 0;
  8052. buffer->end = 0;
  8053. buffer->last = 0;
  8054. buffer->clip = nk_null_rect;
  8055. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8056. buffer->userdata.ptr = 0;
  8057. #endif
  8058. }
  8059. NK_INTERN void*
  8060. nk_command_buffer_push(struct nk_command_buffer* b,
  8061. enum nk_command_type t, nk_size size)
  8062. {
  8063. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  8064. struct nk_command *cmd;
  8065. nk_size alignment;
  8066. void *unaligned;
  8067. void *memory;
  8068. NK_ASSERT(b);
  8069. NK_ASSERT(b->base);
  8070. if (!b) return 0;
  8071. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  8072. if (!cmd) return 0;
  8073. /* make sure the offset to the next command is aligned */
  8074. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  8075. unaligned = (nk_byte*)cmd + size;
  8076. memory = NK_ALIGN_PTR(unaligned, align);
  8077. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  8078. #ifdef NK_ZERO_COMMAND_MEMORY
  8079. NK_MEMSET(cmd, 0, size + alignment);
  8080. #endif
  8081. cmd->type = t;
  8082. cmd->next = b->base->allocated + alignment;
  8083. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8084. cmd->userdata = b->userdata;
  8085. #endif
  8086. b->end = cmd->next;
  8087. return cmd;
  8088. }
  8089. NK_API void
  8090. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  8091. {
  8092. struct nk_command_scissor *cmd;
  8093. NK_ASSERT(b);
  8094. if (!b) return;
  8095. b->clip.x = r.x;
  8096. b->clip.y = r.y;
  8097. b->clip.w = r.w;
  8098. b->clip.h = r.h;
  8099. cmd = (struct nk_command_scissor*)
  8100. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  8101. if (!cmd) return;
  8102. cmd->x = (short)r.x;
  8103. cmd->y = (short)r.y;
  8104. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8105. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8106. }
  8107. NK_API void
  8108. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  8109. float x1, float y1, float line_thickness, struct nk_color c)
  8110. {
  8111. struct nk_command_line *cmd;
  8112. NK_ASSERT(b);
  8113. if (!b || line_thickness <= 0) return;
  8114. cmd = (struct nk_command_line*)
  8115. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  8116. if (!cmd) return;
  8117. cmd->line_thickness = (unsigned short)line_thickness;
  8118. cmd->begin.x = (short)x0;
  8119. cmd->begin.y = (short)y0;
  8120. cmd->end.x = (short)x1;
  8121. cmd->end.y = (short)y1;
  8122. cmd->color = c;
  8123. }
  8124. NK_API void
  8125. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  8126. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  8127. float bx, float by, float line_thickness, struct nk_color col)
  8128. {
  8129. struct nk_command_curve *cmd;
  8130. NK_ASSERT(b);
  8131. if (!b || col.a == 0 || line_thickness <= 0) return;
  8132. cmd = (struct nk_command_curve*)
  8133. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  8134. if (!cmd) return;
  8135. cmd->line_thickness = (unsigned short)line_thickness;
  8136. cmd->begin.x = (short)ax;
  8137. cmd->begin.y = (short)ay;
  8138. cmd->ctrl[0].x = (short)ctrl0x;
  8139. cmd->ctrl[0].y = (short)ctrl0y;
  8140. cmd->ctrl[1].x = (short)ctrl1x;
  8141. cmd->ctrl[1].y = (short)ctrl1y;
  8142. cmd->end.x = (short)bx;
  8143. cmd->end.y = (short)by;
  8144. cmd->color = col;
  8145. }
  8146. NK_API void
  8147. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8148. float rounding, float line_thickness, struct nk_color c)
  8149. {
  8150. struct nk_command_rect *cmd;
  8151. NK_ASSERT(b);
  8152. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  8153. if (b->use_clipping) {
  8154. const struct nk_rect *clip = &b->clip;
  8155. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8156. clip->x, clip->y, clip->w, clip->h)) return;
  8157. }
  8158. cmd = (struct nk_command_rect*)
  8159. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  8160. if (!cmd) return;
  8161. cmd->rounding = (unsigned short)rounding;
  8162. cmd->line_thickness = (unsigned short)line_thickness;
  8163. cmd->x = (short)rect.x;
  8164. cmd->y = (short)rect.y;
  8165. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8166. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8167. cmd->color = c;
  8168. }
  8169. NK_API void
  8170. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8171. float rounding, struct nk_color c)
  8172. {
  8173. struct nk_command_rect_filled *cmd;
  8174. NK_ASSERT(b);
  8175. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  8176. if (b->use_clipping) {
  8177. const struct nk_rect *clip = &b->clip;
  8178. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8179. clip->x, clip->y, clip->w, clip->h)) return;
  8180. }
  8181. cmd = (struct nk_command_rect_filled*)
  8182. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  8183. if (!cmd) return;
  8184. cmd->rounding = (unsigned short)rounding;
  8185. cmd->x = (short)rect.x;
  8186. cmd->y = (short)rect.y;
  8187. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8188. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8189. cmd->color = c;
  8190. }
  8191. NK_API void
  8192. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  8193. struct nk_color left, struct nk_color top, struct nk_color right,
  8194. struct nk_color bottom)
  8195. {
  8196. struct nk_command_rect_multi_color *cmd;
  8197. NK_ASSERT(b);
  8198. if (!b || rect.w == 0 || rect.h == 0) return;
  8199. if (b->use_clipping) {
  8200. const struct nk_rect *clip = &b->clip;
  8201. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8202. clip->x, clip->y, clip->w, clip->h)) return;
  8203. }
  8204. cmd = (struct nk_command_rect_multi_color*)
  8205. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  8206. if (!cmd) return;
  8207. cmd->x = (short)rect.x;
  8208. cmd->y = (short)rect.y;
  8209. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8210. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8211. cmd->left = left;
  8212. cmd->top = top;
  8213. cmd->right = right;
  8214. cmd->bottom = bottom;
  8215. }
  8216. NK_API void
  8217. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  8218. float line_thickness, struct nk_color c)
  8219. {
  8220. struct nk_command_circle *cmd;
  8221. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  8222. if (b->use_clipping) {
  8223. const struct nk_rect *clip = &b->clip;
  8224. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8225. return;
  8226. }
  8227. cmd = (struct nk_command_circle*)
  8228. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  8229. if (!cmd) return;
  8230. cmd->line_thickness = (unsigned short)line_thickness;
  8231. cmd->x = (short)r.x;
  8232. cmd->y = (short)r.y;
  8233. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8234. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8235. cmd->color = c;
  8236. }
  8237. NK_API void
  8238. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  8239. {
  8240. struct nk_command_circle_filled *cmd;
  8241. NK_ASSERT(b);
  8242. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  8243. if (b->use_clipping) {
  8244. const struct nk_rect *clip = &b->clip;
  8245. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8246. return;
  8247. }
  8248. cmd = (struct nk_command_circle_filled*)
  8249. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  8250. if (!cmd) return;
  8251. cmd->x = (short)r.x;
  8252. cmd->y = (short)r.y;
  8253. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8254. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8255. cmd->color = c;
  8256. }
  8257. NK_API void
  8258. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8259. float a_min, float a_max, float line_thickness, struct nk_color c)
  8260. {
  8261. struct nk_command_arc *cmd;
  8262. if (!b || c.a == 0 || line_thickness <= 0) return;
  8263. cmd = (struct nk_command_arc*)
  8264. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  8265. if (!cmd) return;
  8266. cmd->line_thickness = (unsigned short)line_thickness;
  8267. cmd->cx = (short)cx;
  8268. cmd->cy = (short)cy;
  8269. cmd->r = (unsigned short)radius;
  8270. cmd->a[0] = a_min;
  8271. cmd->a[1] = a_max;
  8272. cmd->color = c;
  8273. }
  8274. NK_API void
  8275. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8276. float a_min, float a_max, struct nk_color c)
  8277. {
  8278. struct nk_command_arc_filled *cmd;
  8279. NK_ASSERT(b);
  8280. if (!b || c.a == 0) return;
  8281. cmd = (struct nk_command_arc_filled*)
  8282. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  8283. if (!cmd) return;
  8284. cmd->cx = (short)cx;
  8285. cmd->cy = (short)cy;
  8286. cmd->r = (unsigned short)radius;
  8287. cmd->a[0] = a_min;
  8288. cmd->a[1] = a_max;
  8289. cmd->color = c;
  8290. }
  8291. NK_API void
  8292. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8293. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  8294. {
  8295. struct nk_command_triangle *cmd;
  8296. NK_ASSERT(b);
  8297. if (!b || c.a == 0 || line_thickness <= 0) return;
  8298. if (b->use_clipping) {
  8299. const struct nk_rect *clip = &b->clip;
  8300. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8301. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8302. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8303. return;
  8304. }
  8305. cmd = (struct nk_command_triangle*)
  8306. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  8307. if (!cmd) return;
  8308. cmd->line_thickness = (unsigned short)line_thickness;
  8309. cmd->a.x = (short)x0;
  8310. cmd->a.y = (short)y0;
  8311. cmd->b.x = (short)x1;
  8312. cmd->b.y = (short)y1;
  8313. cmd->c.x = (short)x2;
  8314. cmd->c.y = (short)y2;
  8315. cmd->color = c;
  8316. }
  8317. NK_API void
  8318. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8319. float y1, float x2, float y2, struct nk_color c)
  8320. {
  8321. struct nk_command_triangle_filled *cmd;
  8322. NK_ASSERT(b);
  8323. if (!b || c.a == 0) return;
  8324. if (!b) return;
  8325. if (b->use_clipping) {
  8326. const struct nk_rect *clip = &b->clip;
  8327. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8328. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8329. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8330. return;
  8331. }
  8332. cmd = (struct nk_command_triangle_filled*)
  8333. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  8334. if (!cmd) return;
  8335. cmd->a.x = (short)x0;
  8336. cmd->a.y = (short)y0;
  8337. cmd->b.x = (short)x1;
  8338. cmd->b.y = (short)y1;
  8339. cmd->c.x = (short)x2;
  8340. cmd->c.y = (short)y2;
  8341. cmd->color = c;
  8342. }
  8343. NK_API void
  8344. nk_stroke_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8345. float line_thickness, struct nk_color col)
  8346. {
  8347. int i;
  8348. nk_size size = 0;
  8349. struct nk_command_polygon *cmd;
  8350. NK_ASSERT(b);
  8351. if (!b || col.a == 0 || line_thickness <= 0) return;
  8352. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8353. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  8354. if (!cmd) return;
  8355. cmd->color = col;
  8356. cmd->line_thickness = (unsigned short)line_thickness;
  8357. cmd->point_count = (unsigned short)point_count;
  8358. for (i = 0; i < point_count; ++i) {
  8359. cmd->points[i].x = (short)points[i*2];
  8360. cmd->points[i].y = (short)points[i*2+1];
  8361. }
  8362. }
  8363. NK_API void
  8364. nk_fill_polygon(struct nk_command_buffer *b, float *points, int point_count,
  8365. struct nk_color col)
  8366. {
  8367. int i;
  8368. nk_size size = 0;
  8369. struct nk_command_polygon_filled *cmd;
  8370. NK_ASSERT(b);
  8371. if (!b || col.a == 0) return;
  8372. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8373. cmd = (struct nk_command_polygon_filled*)
  8374. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  8375. if (!cmd) return;
  8376. cmd->color = col;
  8377. cmd->point_count = (unsigned short)point_count;
  8378. for (i = 0; i < point_count; ++i) {
  8379. cmd->points[i].x = (short)points[i*2+0];
  8380. cmd->points[i].y = (short)points[i*2+1];
  8381. }
  8382. }
  8383. NK_API void
  8384. nk_stroke_polyline(struct nk_command_buffer *b, float *points, int point_count,
  8385. float line_thickness, struct nk_color col)
  8386. {
  8387. int i;
  8388. nk_size size = 0;
  8389. struct nk_command_polyline *cmd;
  8390. NK_ASSERT(b);
  8391. if (!b || col.a == 0 || line_thickness <= 0) return;
  8392. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8393. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  8394. if (!cmd) return;
  8395. cmd->color = col;
  8396. cmd->point_count = (unsigned short)point_count;
  8397. cmd->line_thickness = (unsigned short)line_thickness;
  8398. for (i = 0; i < point_count; ++i) {
  8399. cmd->points[i].x = (short)points[i*2];
  8400. cmd->points[i].y = (short)points[i*2+1];
  8401. }
  8402. }
  8403. NK_API void
  8404. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  8405. const struct nk_image *img, struct nk_color col)
  8406. {
  8407. struct nk_command_image *cmd;
  8408. NK_ASSERT(b);
  8409. if (!b) return;
  8410. if (b->use_clipping) {
  8411. const struct nk_rect *c = &b->clip;
  8412. 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))
  8413. return;
  8414. }
  8415. cmd = (struct nk_command_image*)
  8416. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  8417. if (!cmd) return;
  8418. cmd->x = (short)r.x;
  8419. cmd->y = (short)r.y;
  8420. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8421. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8422. cmd->img = *img;
  8423. cmd->col = col;
  8424. }
  8425. NK_API void
  8426. nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
  8427. nk_command_custom_callback cb, nk_handle usr)
  8428. {
  8429. struct nk_command_custom *cmd;
  8430. NK_ASSERT(b);
  8431. if (!b) return;
  8432. if (b->use_clipping) {
  8433. const struct nk_rect *c = &b->clip;
  8434. 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))
  8435. return;
  8436. }
  8437. cmd = (struct nk_command_custom*)
  8438. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  8439. if (!cmd) return;
  8440. cmd->x = (short)r.x;
  8441. cmd->y = (short)r.y;
  8442. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8443. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8444. cmd->callback_data = usr;
  8445. cmd->callback = cb;
  8446. }
  8447. NK_API void
  8448. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  8449. const char *string, int length, const struct nk_user_font *font,
  8450. struct nk_color bg, struct nk_color fg)
  8451. {
  8452. float text_width = 0;
  8453. struct nk_command_text *cmd;
  8454. NK_ASSERT(b);
  8455. NK_ASSERT(font);
  8456. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  8457. if (b->use_clipping) {
  8458. const struct nk_rect *c = &b->clip;
  8459. 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))
  8460. return;
  8461. }
  8462. /* make sure text fits inside bounds */
  8463. text_width = font->width(font->userdata, font->height, string, length);
  8464. if (text_width > r.w){
  8465. int glyphs = 0;
  8466. float txt_width = (float)text_width;
  8467. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
  8468. }
  8469. if (!length) return;
  8470. cmd = (struct nk_command_text*)
  8471. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  8472. if (!cmd) return;
  8473. cmd->x = (short)r.x;
  8474. cmd->y = (short)r.y;
  8475. cmd->w = (unsigned short)r.w;
  8476. cmd->h = (unsigned short)r.h;
  8477. cmd->background = bg;
  8478. cmd->foreground = fg;
  8479. cmd->font = font;
  8480. cmd->length = length;
  8481. cmd->height = font->height;
  8482. NK_MEMCPY(cmd->string, string, (nk_size)length);
  8483. cmd->string[length] = '\0';
  8484. }
  8485. /* ==============================================================
  8486. *
  8487. * DRAW LIST
  8488. *
  8489. * ===============================================================*/
  8490. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  8491. NK_API void
  8492. nk_draw_list_init(struct nk_draw_list *list)
  8493. {
  8494. nk_size i = 0;
  8495. NK_ASSERT(list);
  8496. if (!list) return;
  8497. nk_zero(list, sizeof(*list));
  8498. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  8499. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  8500. list->circle_vtx[i].x = (float)NK_COS(a);
  8501. list->circle_vtx[i].y = (float)NK_SIN(a);
  8502. }
  8503. }
  8504. NK_API void
  8505. nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
  8506. struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
  8507. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  8508. {
  8509. NK_ASSERT(canvas);
  8510. NK_ASSERT(config);
  8511. NK_ASSERT(cmds);
  8512. NK_ASSERT(vertices);
  8513. NK_ASSERT(elements);
  8514. if (!canvas || !config || !cmds || !vertices || !elements)
  8515. return;
  8516. canvas->buffer = cmds;
  8517. canvas->config = *config;
  8518. canvas->elements = elements;
  8519. canvas->vertices = vertices;
  8520. canvas->line_AA = line_aa;
  8521. canvas->shape_AA = shape_aa;
  8522. canvas->clip_rect = nk_null_rect;
  8523. }
  8524. NK_API const struct nk_draw_command*
  8525. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8526. {
  8527. nk_byte *memory;
  8528. nk_size offset;
  8529. const struct nk_draw_command *cmd;
  8530. NK_ASSERT(buffer);
  8531. if (!buffer || !buffer->size || !canvas->cmd_count)
  8532. return 0;
  8533. memory = (nk_byte*)buffer->memory.ptr;
  8534. offset = buffer->memory.size - canvas->cmd_offset;
  8535. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8536. return cmd;
  8537. }
  8538. NK_API const struct nk_draw_command*
  8539. nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  8540. {
  8541. nk_size size;
  8542. nk_size offset;
  8543. nk_byte *memory;
  8544. const struct nk_draw_command *end;
  8545. NK_ASSERT(buffer);
  8546. NK_ASSERT(canvas);
  8547. if (!buffer || !canvas)
  8548. return 0;
  8549. memory = (nk_byte*)buffer->memory.ptr;
  8550. size = buffer->memory.size;
  8551. offset = size - canvas->cmd_offset;
  8552. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  8553. end -= (canvas->cmd_count-1);
  8554. return end;
  8555. }
  8556. NK_API const struct nk_draw_command*
  8557. nk__draw_list_next(const struct nk_draw_command *cmd,
  8558. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  8559. {
  8560. const struct nk_draw_command *end;
  8561. NK_ASSERT(buffer);
  8562. NK_ASSERT(canvas);
  8563. if (!cmd || !buffer || !canvas)
  8564. return 0;
  8565. end = nk__draw_list_end(canvas, buffer);
  8566. if (cmd <= end) return 0;
  8567. return (cmd-1);
  8568. }
  8569. NK_API void
  8570. nk_draw_list_clear(struct nk_draw_list *list)
  8571. {
  8572. NK_ASSERT(list);
  8573. if (!list) return;
  8574. if (list->buffer)
  8575. nk_buffer_clear(list->buffer);
  8576. if (list->vertices)
  8577. nk_buffer_clear(list->vertices);
  8578. if (list->elements)
  8579. nk_buffer_clear(list->elements);
  8580. list->element_count = 0;
  8581. list->vertex_count = 0;
  8582. list->cmd_offset = 0;
  8583. list->cmd_count = 0;
  8584. list->path_count = 0;
  8585. list->vertices = 0;
  8586. list->elements = 0;
  8587. list->clip_rect = nk_null_rect;
  8588. }
  8589. NK_INTERN struct nk_vec2*
  8590. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  8591. {
  8592. struct nk_vec2 *points;
  8593. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  8594. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  8595. points = (struct nk_vec2*)
  8596. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  8597. point_size * (nk_size)count, point_align);
  8598. if (!points) return 0;
  8599. if (!list->path_offset) {
  8600. void *memory = nk_buffer_memory(list->buffer);
  8601. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  8602. }
  8603. list->path_count += (unsigned int)count;
  8604. return points;
  8605. }
  8606. NK_INTERN struct nk_vec2
  8607. nk_draw_list_path_last(struct nk_draw_list *list)
  8608. {
  8609. void *memory;
  8610. struct nk_vec2 *point;
  8611. NK_ASSERT(list->path_count);
  8612. memory = nk_buffer_memory(list->buffer);
  8613. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  8614. point += (list->path_count-1);
  8615. return *point;
  8616. }
  8617. NK_INTERN struct nk_draw_command*
  8618. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  8619. nk_handle texture)
  8620. {
  8621. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  8622. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  8623. struct nk_draw_command *cmd;
  8624. NK_ASSERT(list);
  8625. cmd = (struct nk_draw_command*)
  8626. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  8627. if (!cmd) return 0;
  8628. if (!list->cmd_count) {
  8629. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  8630. nk_size total = nk_buffer_total(list->buffer);
  8631. memory = nk_ptr_add(nk_byte, memory, total);
  8632. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  8633. }
  8634. cmd->elem_count = 0;
  8635. cmd->clip_rect = clip;
  8636. cmd->texture = texture;
  8637. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8638. cmd->userdata = list->userdata;
  8639. #endif
  8640. list->cmd_count++;
  8641. list->clip_rect = clip;
  8642. return cmd;
  8643. }
  8644. NK_INTERN struct nk_draw_command*
  8645. nk_draw_list_command_last(struct nk_draw_list *list)
  8646. {
  8647. void *memory;
  8648. nk_size size;
  8649. struct nk_draw_command *cmd;
  8650. NK_ASSERT(list->cmd_count);
  8651. memory = nk_buffer_memory(list->buffer);
  8652. size = nk_buffer_total(list->buffer);
  8653. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  8654. return (cmd - (list->cmd_count-1));
  8655. }
  8656. NK_INTERN void
  8657. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  8658. {
  8659. NK_ASSERT(list);
  8660. if (!list) return;
  8661. if (!list->cmd_count) {
  8662. nk_draw_list_push_command(list, rect, list->config.null.texture);
  8663. } else {
  8664. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8665. if (prev->elem_count == 0)
  8666. prev->clip_rect = rect;
  8667. nk_draw_list_push_command(list, rect, prev->texture);
  8668. }
  8669. }
  8670. NK_INTERN void
  8671. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  8672. {
  8673. NK_ASSERT(list);
  8674. if (!list) return;
  8675. if (!list->cmd_count) {
  8676. nk_draw_list_push_command(list, nk_null_rect, texture);
  8677. } else {
  8678. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  8679. if (prev->elem_count == 0) {
  8680. prev->texture = texture;
  8681. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8682. prev->userdata = list->userdata;
  8683. #endif
  8684. } else if (prev->texture.id != texture.id
  8685. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8686. || prev->userdata.id != list->userdata.id
  8687. #endif
  8688. ) nk_draw_list_push_command(list, prev->clip_rect, texture);
  8689. }
  8690. }
  8691. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8692. NK_API void
  8693. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  8694. {
  8695. list->userdata = userdata;
  8696. }
  8697. #endif
  8698. NK_INTERN void*
  8699. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  8700. {
  8701. void *vtx;
  8702. NK_ASSERT(list);
  8703. if (!list) return 0;
  8704. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  8705. list->config.vertex_size*count, list->config.vertex_alignment);
  8706. if (!vtx) return 0;
  8707. list->vertex_count += (unsigned int)count;
  8708. return vtx;
  8709. }
  8710. NK_INTERN nk_draw_index*
  8711. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  8712. {
  8713. nk_draw_index *ids;
  8714. struct nk_draw_command *cmd;
  8715. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  8716. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  8717. NK_ASSERT(list);
  8718. if (!list) return 0;
  8719. ids = (nk_draw_index*)
  8720. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  8721. if (!ids) return 0;
  8722. cmd = nk_draw_list_command_last(list);
  8723. list->element_count += (unsigned int)count;
  8724. cmd->elem_count += (unsigned int)count;
  8725. return ids;
  8726. }
  8727. NK_INTERN int
  8728. nk_draw_vertex_layout_element_is_end_of_layout(
  8729. const struct nk_draw_vertex_layout_element *element)
  8730. {
  8731. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  8732. element->format == NK_FORMAT_COUNT);
  8733. }
  8734. NK_INTERN void
  8735. nk_draw_vertex_color(void *attr, const float *vals,
  8736. enum nk_draw_vertex_layout_format format)
  8737. {
  8738. /* if this triggers you tried to provide a value format for a color */
  8739. float val[4];
  8740. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  8741. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  8742. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  8743. val[0] = NK_SATURATE(vals[0]);
  8744. val[1] = NK_SATURATE(vals[1]);
  8745. val[2] = NK_SATURATE(vals[2]);
  8746. val[3] = NK_SATURATE(vals[3]);
  8747. switch (format) {
  8748. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  8749. case NK_FORMAT_R8G8B8A8:
  8750. case NK_FORMAT_R8G8B8: {
  8751. struct nk_color col = nk_rgba_fv(val);
  8752. NK_MEMCPY(attr, &col.r, sizeof(col));
  8753. } break;
  8754. case NK_FORMAT_B8G8R8A8: {
  8755. struct nk_color col = nk_rgba_fv(val);
  8756. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  8757. NK_MEMCPY(attr, &bgra, sizeof(bgra));
  8758. } break;
  8759. case NK_FORMAT_R16G15B16: {
  8760. nk_ushort col[3];
  8761. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8762. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8763. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8764. NK_MEMCPY(attr, col, sizeof(col));
  8765. } break;
  8766. case NK_FORMAT_R16G15B16A16: {
  8767. nk_ushort col[4];
  8768. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  8769. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  8770. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  8771. col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
  8772. NK_MEMCPY(attr, col, sizeof(col));
  8773. } break;
  8774. case NK_FORMAT_R32G32B32: {
  8775. nk_uint col[3];
  8776. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8777. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8778. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8779. NK_MEMCPY(attr, col, sizeof(col));
  8780. } break;
  8781. case NK_FORMAT_R32G32B32A32: {
  8782. nk_uint col[4];
  8783. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  8784. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  8785. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  8786. col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
  8787. NK_MEMCPY(attr, col, sizeof(col));
  8788. } break;
  8789. case NK_FORMAT_R32G32B32A32_FLOAT:
  8790. NK_MEMCPY(attr, val, sizeof(float)*4);
  8791. break;
  8792. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  8793. double col[4];
  8794. col[0] = (double)val[0];
  8795. col[1] = (double)val[1];
  8796. col[2] = (double)val[2];
  8797. col[3] = (double)val[3];
  8798. NK_MEMCPY(attr, col, sizeof(col));
  8799. } break;
  8800. case NK_FORMAT_RGB32:
  8801. case NK_FORMAT_RGBA32: {
  8802. struct nk_color col = nk_rgba_fv(val);
  8803. nk_uint color = nk_color_u32(col);
  8804. NK_MEMCPY(attr, &color, sizeof(color));
  8805. } break; }
  8806. }
  8807. NK_INTERN void
  8808. nk_draw_vertex_element(void *dst, const float *values, int value_count,
  8809. enum nk_draw_vertex_layout_format format)
  8810. {
  8811. int value_index;
  8812. void *attribute = dst;
  8813. /* if this triggers you tried to provide a color format for a value */
  8814. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  8815. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  8816. for (value_index = 0; value_index < value_count; ++value_index) {
  8817. switch (format) {
  8818. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  8819. case NK_FORMAT_SCHAR: {
  8820. char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
  8821. NK_MEMCPY(attribute, &value, sizeof(value));
  8822. attribute = (void*)((char*)attribute + sizeof(char));
  8823. } break;
  8824. case NK_FORMAT_SSHORT: {
  8825. nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
  8826. NK_MEMCPY(attribute, &value, sizeof(value));
  8827. attribute = (void*)((char*)attribute + sizeof(value));
  8828. } break;
  8829. case NK_FORMAT_SINT: {
  8830. nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
  8831. NK_MEMCPY(attribute, &value, sizeof(value));
  8832. attribute = (void*)((char*)attribute + sizeof(nk_int));
  8833. } break;
  8834. case NK_FORMAT_UCHAR: {
  8835. unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
  8836. NK_MEMCPY(attribute, &value, sizeof(value));
  8837. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  8838. } break;
  8839. case NK_FORMAT_USHORT: {
  8840. nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
  8841. NK_MEMCPY(attribute, &value, sizeof(value));
  8842. attribute = (void*)((char*)attribute + sizeof(value));
  8843. } break;
  8844. case NK_FORMAT_UINT: {
  8845. nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
  8846. NK_MEMCPY(attribute, &value, sizeof(value));
  8847. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  8848. } break;
  8849. case NK_FORMAT_FLOAT:
  8850. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  8851. attribute = (void*)((char*)attribute + sizeof(float));
  8852. break;
  8853. case NK_FORMAT_DOUBLE: {
  8854. double value = (double)values[value_index];
  8855. NK_MEMCPY(attribute, &value, sizeof(value));
  8856. attribute = (void*)((char*)attribute + sizeof(double));
  8857. } break;
  8858. }
  8859. }
  8860. }
  8861. NK_INTERN void*
  8862. nk_draw_vertex(void *dst, const struct nk_convert_config *config,
  8863. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  8864. {
  8865. void *result = (void*)((char*)dst + config->vertex_size);
  8866. const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
  8867. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  8868. void *address = (void*)((char*)dst + elem_iter->offset);
  8869. switch (elem_iter->attribute) {
  8870. case NK_VERTEX_ATTRIBUTE_COUNT:
  8871. default: NK_ASSERT(0 && "wrong element attribute"); break;
  8872. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  8873. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  8874. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  8875. }
  8876. elem_iter++;
  8877. }
  8878. return result;
  8879. }
  8880. NK_API void
  8881. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  8882. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  8883. float thickness, enum nk_anti_aliasing aliasing)
  8884. {
  8885. nk_size count;
  8886. int thick_line;
  8887. struct nk_colorf col;
  8888. struct nk_colorf col_trans;
  8889. NK_ASSERT(list);
  8890. if (!list || points_count < 2) return;
  8891. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  8892. count = points_count;
  8893. if (!closed) count = points_count-1;
  8894. thick_line = thickness > 1.0f;
  8895. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8896. nk_draw_list_push_userdata(list, list->userdata);
  8897. #endif
  8898. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  8899. nk_color_fv(&col.r, color);
  8900. col_trans = col;
  8901. col_trans.a = 0;
  8902. if (aliasing == NK_ANTI_ALIASING_ON) {
  8903. /* ANTI-ALIASED STROKE */
  8904. const float AA_SIZE = 1.0f;
  8905. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  8906. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  8907. /* allocate vertices and elements */
  8908. nk_size i1 = 0;
  8909. nk_size vertex_offset;
  8910. nk_size index = list->vertex_count;
  8911. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  8912. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  8913. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  8914. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  8915. nk_size size;
  8916. struct nk_vec2 *normals, *temp;
  8917. if (!vtx || !ids) return;
  8918. /* temporary allocate normals + points */
  8919. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  8920. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  8921. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  8922. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  8923. NK_ASSERT(normals);
  8924. if (!normals) return;
  8925. temp = normals + points_count;
  8926. /* make sure vertex pointer is still correct */
  8927. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  8928. /* calculate normals */
  8929. for (i1 = 0; i1 < count; ++i1) {
  8930. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  8931. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  8932. float len;
  8933. /* vec2 inverted length */
  8934. len = nk_vec2_len_sqr(diff);
  8935. if (len != 0.0f)
  8936. len = nk_inv_sqrt(len);
  8937. else len = 1.0f;
  8938. diff = nk_vec2_muls(diff, len);
  8939. normals[i1].x = diff.y;
  8940. normals[i1].y = -diff.x;
  8941. }
  8942. if (!closed)
  8943. normals[points_count-1] = normals[points_count-2];
  8944. if (!thick_line) {
  8945. nk_size idx1, i;
  8946. if (!closed) {
  8947. struct nk_vec2 d;
  8948. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  8949. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  8950. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  8951. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  8952. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  8953. }
  8954. /* fill elements */
  8955. idx1 = index;
  8956. for (i1 = 0; i1 < count; i1++) {
  8957. struct nk_vec2 dm;
  8958. float dmr2;
  8959. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  8960. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  8961. /* average normals */
  8962. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  8963. dmr2 = dm.x * dm.x + dm.y* dm.y;
  8964. if (dmr2 > 0.000001f) {
  8965. float scale = 1.0f/dmr2;
  8966. scale = NK_MIN(100.0f, scale);
  8967. dm = nk_vec2_muls(dm, scale);
  8968. }
  8969. dm = nk_vec2_muls(dm, AA_SIZE);
  8970. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  8971. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  8972. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  8973. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  8974. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  8975. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  8976. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  8977. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  8978. ids += 12;
  8979. idx1 = idx2;
  8980. }
  8981. /* fill vertices */
  8982. for (i = 0; i < points_count; ++i) {
  8983. const struct nk_vec2 uv = list->config.null.uv;
  8984. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  8985. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
  8986. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
  8987. }
  8988. } else {
  8989. nk_size idx1, i;
  8990. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  8991. if (!closed) {
  8992. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  8993. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  8994. temp[0] = nk_vec2_add(points[0], d1);
  8995. temp[1] = nk_vec2_add(points[0], d2);
  8996. temp[2] = nk_vec2_sub(points[0], d2);
  8997. temp[3] = nk_vec2_sub(points[0], d1);
  8998. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  8999. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  9000. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  9001. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  9002. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  9003. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  9004. }
  9005. /* add all elements */
  9006. idx1 = index;
  9007. for (i1 = 0; i1 < count; ++i1) {
  9008. struct nk_vec2 dm_out, dm_in;
  9009. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  9010. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  9011. /* average normals */
  9012. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9013. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  9014. if (dmr2 > 0.000001f) {
  9015. float scale = 1.0f/dmr2;
  9016. scale = NK_MIN(100.0f, scale);
  9017. dm = nk_vec2_muls(dm, scale);
  9018. }
  9019. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  9020. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  9021. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  9022. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  9023. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  9024. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  9025. /* add indexes */
  9026. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  9027. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9028. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  9029. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9030. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9031. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  9032. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  9033. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  9034. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  9035. ids += 18;
  9036. idx1 = idx2;
  9037. }
  9038. /* add vertices */
  9039. for (i = 0; i < points_count; ++i) {
  9040. const struct nk_vec2 uv = list->config.null.uv;
  9041. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
  9042. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
  9043. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
  9044. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
  9045. }
  9046. }
  9047. /* free temporary normals + points */
  9048. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9049. } else {
  9050. /* NON ANTI-ALIASED STROKE */
  9051. nk_size i1 = 0;
  9052. nk_size idx = list->vertex_count;
  9053. const nk_size idx_count = count * 6;
  9054. const nk_size vtx_count = count * 4;
  9055. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9056. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9057. if (!vtx || !ids) return;
  9058. for (i1 = 0; i1 < count; ++i1) {
  9059. float dx, dy;
  9060. const struct nk_vec2 uv = list->config.null.uv;
  9061. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  9062. const struct nk_vec2 p1 = points[i1];
  9063. const struct nk_vec2 p2 = points[i2];
  9064. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  9065. float len;
  9066. /* vec2 inverted length */
  9067. len = nk_vec2_len_sqr(diff);
  9068. if (len != 0.0f)
  9069. len = nk_inv_sqrt(len);
  9070. else len = 1.0f;
  9071. diff = nk_vec2_muls(diff, len);
  9072. /* add vertices */
  9073. dx = diff.x * (thickness * 0.5f);
  9074. dy = diff.y * (thickness * 0.5f);
  9075. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  9076. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  9077. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  9078. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  9079. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  9080. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  9081. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  9082. ids += 6;
  9083. idx += 4;
  9084. }
  9085. }
  9086. }
  9087. NK_API void
  9088. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  9089. const struct nk_vec2 *points, const unsigned int points_count,
  9090. struct nk_color color, enum nk_anti_aliasing aliasing)
  9091. {
  9092. struct nk_colorf col;
  9093. struct nk_colorf col_trans;
  9094. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9095. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9096. NK_ASSERT(list);
  9097. if (!list || points_count < 3) return;
  9098. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9099. nk_draw_list_push_userdata(list, list->userdata);
  9100. #endif
  9101. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9102. nk_color_fv(&col.r, color);
  9103. col_trans = col;
  9104. col_trans.a = 0;
  9105. if (aliasing == NK_ANTI_ALIASING_ON) {
  9106. nk_size i = 0;
  9107. nk_size i0 = 0;
  9108. nk_size i1 = 0;
  9109. const float AA_SIZE = 1.0f;
  9110. nk_size vertex_offset = 0;
  9111. nk_size index = list->vertex_count;
  9112. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  9113. const nk_size vtx_count = (points_count*2);
  9114. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9115. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9116. nk_size size = 0;
  9117. struct nk_vec2 *normals = 0;
  9118. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  9119. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  9120. if (!vtx || !ids) return;
  9121. /* temporary allocate normals */
  9122. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9123. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9124. size = pnt_size * points_count;
  9125. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9126. NK_ASSERT(normals);
  9127. if (!normals) return;
  9128. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9129. /* add elements */
  9130. for (i = 2; i < points_count; i++) {
  9131. ids[0] = (nk_draw_index)(vtx_inner_idx);
  9132. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  9133. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  9134. ids += 3;
  9135. }
  9136. /* compute normals */
  9137. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9138. struct nk_vec2 p0 = points[i0];
  9139. struct nk_vec2 p1 = points[i1];
  9140. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  9141. /* vec2 inverted length */
  9142. float len = nk_vec2_len_sqr(diff);
  9143. if (len != 0.0f)
  9144. len = nk_inv_sqrt(len);
  9145. else len = 1.0f;
  9146. diff = nk_vec2_muls(diff, len);
  9147. normals[i0].x = diff.y;
  9148. normals[i0].y = -diff.x;
  9149. }
  9150. /* add vertices + indexes */
  9151. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9152. const struct nk_vec2 uv = list->config.null.uv;
  9153. struct nk_vec2 n0 = normals[i0];
  9154. struct nk_vec2 n1 = normals[i1];
  9155. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  9156. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  9157. if (dmr2 > 0.000001f) {
  9158. float scale = 1.0f / dmr2;
  9159. scale = NK_MIN(scale, 100.0f);
  9160. dm = nk_vec2_muls(dm, scale);
  9161. }
  9162. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  9163. /* add vertices */
  9164. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  9165. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  9166. /* add indexes */
  9167. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9168. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  9169. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9170. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9171. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  9172. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9173. ids += 6;
  9174. }
  9175. /* free temporary normals + points */
  9176. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9177. } else {
  9178. nk_size i = 0;
  9179. nk_size index = list->vertex_count;
  9180. const nk_size idx_count = (points_count-2)*3;
  9181. const nk_size vtx_count = points_count;
  9182. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9183. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9184. if (!vtx || !ids) return;
  9185. for (i = 0; i < vtx_count; ++i)
  9186. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.null.uv, col);
  9187. for (i = 2; i < points_count; ++i) {
  9188. ids[0] = (nk_draw_index)index;
  9189. ids[1] = (nk_draw_index)(index+ i - 1);
  9190. ids[2] = (nk_draw_index)(index+i);
  9191. ids += 3;
  9192. }
  9193. }
  9194. }
  9195. NK_API void
  9196. nk_draw_list_path_clear(struct nk_draw_list *list)
  9197. {
  9198. NK_ASSERT(list);
  9199. if (!list) return;
  9200. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  9201. list->path_count = 0;
  9202. list->path_offset = 0;
  9203. }
  9204. NK_API void
  9205. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  9206. {
  9207. struct nk_vec2 *points = 0;
  9208. struct nk_draw_command *cmd = 0;
  9209. NK_ASSERT(list);
  9210. if (!list) return;
  9211. if (!list->cmd_count)
  9212. nk_draw_list_add_clip(list, nk_null_rect);
  9213. cmd = nk_draw_list_command_last(list);
  9214. if (cmd && cmd->texture.ptr != list->config.null.texture.ptr)
  9215. nk_draw_list_push_image(list, list->config.null.texture);
  9216. points = nk_draw_list_alloc_path(list, 1);
  9217. if (!points) return;
  9218. points[0] = pos;
  9219. }
  9220. NK_API void
  9221. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  9222. float radius, int a_min, int a_max)
  9223. {
  9224. int a = 0;
  9225. NK_ASSERT(list);
  9226. if (!list) return;
  9227. if (a_min <= a_max) {
  9228. for (a = a_min; a <= a_max; a++) {
  9229. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  9230. const float x = center.x + c.x * radius;
  9231. const float y = center.y + c.y * radius;
  9232. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9233. }
  9234. }
  9235. }
  9236. NK_API void
  9237. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  9238. float radius, float a_min, float a_max, unsigned int segments)
  9239. {
  9240. unsigned int i = 0;
  9241. NK_ASSERT(list);
  9242. if (!list) return;
  9243. if (radius == 0.0f) return;
  9244. /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
  9245. sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
  9246. cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
  9247. Two coordinates (x, y) of a point on a circle centered on
  9248. the origin can be written in polar form as:
  9249. x = r * cos(a)
  9250. y = r * sin(a)
  9251. where r is the radius of the circle,
  9252. a is the angle between (x, y) and the origin.
  9253. This allows us to rotate the coordinates around the
  9254. origin by an angle b using the following transformation:
  9255. x' = r * cos(a + b) = x * cos(b) - y * sin(b)
  9256. y' = r * sin(a + b) = y * cos(b) + x * sin(b)
  9257. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
  9258. */
  9259. {const float d_angle = (a_max - a_min) / (float)segments;
  9260. const float sin_d = (float)NK_SIN(d_angle);
  9261. const float cos_d = (float)NK_COS(d_angle);
  9262. float cx = (float)NK_COS(a_min) * radius;
  9263. float cy = (float)NK_SIN(a_min) * radius;
  9264. for(i = 0; i <= segments; ++i) {
  9265. float new_cx, new_cy;
  9266. const float x = center.x + cx;
  9267. const float y = center.y + cy;
  9268. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9269. new_cx = cx * cos_d - cy * sin_d;
  9270. new_cy = cy * cos_d + cx * sin_d;
  9271. cx = new_cx;
  9272. cy = new_cy;
  9273. }}
  9274. }
  9275. NK_API void
  9276. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  9277. struct nk_vec2 b, float rounding)
  9278. {
  9279. float r;
  9280. NK_ASSERT(list);
  9281. if (!list) return;
  9282. r = rounding;
  9283. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  9284. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  9285. if (r == 0.0f) {
  9286. nk_draw_list_path_line_to(list, a);
  9287. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  9288. nk_draw_list_path_line_to(list, b);
  9289. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  9290. } else {
  9291. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  9292. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  9293. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  9294. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  9295. }
  9296. }
  9297. NK_API void
  9298. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  9299. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  9300. {
  9301. float t_step;
  9302. unsigned int i_step;
  9303. struct nk_vec2 p1;
  9304. NK_ASSERT(list);
  9305. NK_ASSERT(list->path_count);
  9306. if (!list || !list->path_count) return;
  9307. num_segments = NK_MAX(num_segments, 1);
  9308. p1 = nk_draw_list_path_last(list);
  9309. t_step = 1.0f/(float)num_segments;
  9310. for (i_step = 1; i_step <= num_segments; ++i_step) {
  9311. float t = t_step * (float)i_step;
  9312. float u = 1.0f - t;
  9313. float w1 = u*u*u;
  9314. float w2 = 3*u*u*t;
  9315. float w3 = 3*u*t*t;
  9316. float w4 = t * t *t;
  9317. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  9318. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  9319. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  9320. }
  9321. }
  9322. NK_API void
  9323. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  9324. {
  9325. struct nk_vec2 *points;
  9326. NK_ASSERT(list);
  9327. if (!list) return;
  9328. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9329. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  9330. nk_draw_list_path_clear(list);
  9331. }
  9332. NK_API void
  9333. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  9334. enum nk_draw_list_stroke closed, float thickness)
  9335. {
  9336. struct nk_vec2 *points;
  9337. NK_ASSERT(list);
  9338. if (!list) return;
  9339. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9340. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  9341. closed, thickness, list->config.line_AA);
  9342. nk_draw_list_path_clear(list);
  9343. }
  9344. NK_API void
  9345. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  9346. struct nk_vec2 b, struct nk_color col, float thickness)
  9347. {
  9348. NK_ASSERT(list);
  9349. if (!list || !col.a) return;
  9350. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9351. nk_draw_list_path_line_to(list, a);
  9352. nk_draw_list_path_line_to(list, b);
  9353. } else {
  9354. nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
  9355. nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
  9356. }
  9357. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9358. }
  9359. NK_API void
  9360. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  9361. struct nk_color col, float rounding)
  9362. {
  9363. NK_ASSERT(list);
  9364. if (!list || !col.a) return;
  9365. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9366. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9367. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9368. } else {
  9369. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9370. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9371. } nk_draw_list_path_fill(list, col);
  9372. }
  9373. NK_API void
  9374. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  9375. struct nk_color col, float rounding, float thickness)
  9376. {
  9377. NK_ASSERT(list);
  9378. if (!list || !col.a) return;
  9379. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9380. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9381. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9382. } else {
  9383. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9384. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9385. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9386. }
  9387. NK_API void
  9388. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  9389. struct nk_color left, struct nk_color top, struct nk_color right,
  9390. struct nk_color bottom)
  9391. {
  9392. void *vtx;
  9393. struct nk_colorf col_left, col_top;
  9394. struct nk_colorf col_right, col_bottom;
  9395. nk_draw_index *idx;
  9396. nk_draw_index index;
  9397. nk_color_fv(&col_left.r, left);
  9398. nk_color_fv(&col_right.r, right);
  9399. nk_color_fv(&col_top.r, top);
  9400. nk_color_fv(&col_bottom.r, bottom);
  9401. NK_ASSERT(list);
  9402. if (!list) return;
  9403. nk_draw_list_push_image(list, list->config.null.texture);
  9404. index = (nk_draw_index)list->vertex_count;
  9405. vtx = nk_draw_list_alloc_vertices(list, 4);
  9406. idx = nk_draw_list_alloc_elements(list, 6);
  9407. if (!vtx || !idx) return;
  9408. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9409. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9410. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9411. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.null.uv, col_left);
  9412. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.null.uv, col_top);
  9413. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.null.uv, col_right);
  9414. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.null.uv, col_bottom);
  9415. }
  9416. NK_API void
  9417. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9418. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  9419. {
  9420. NK_ASSERT(list);
  9421. if (!list || !col.a) return;
  9422. nk_draw_list_path_line_to(list, a);
  9423. nk_draw_list_path_line_to(list, b);
  9424. nk_draw_list_path_line_to(list, c);
  9425. nk_draw_list_path_fill(list, col);
  9426. }
  9427. NK_API void
  9428. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9429. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  9430. {
  9431. NK_ASSERT(list);
  9432. if (!list || !col.a) return;
  9433. nk_draw_list_path_line_to(list, a);
  9434. nk_draw_list_path_line_to(list, b);
  9435. nk_draw_list_path_line_to(list, c);
  9436. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9437. }
  9438. NK_API void
  9439. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9440. float radius, struct nk_color col, unsigned int segs)
  9441. {
  9442. float a_max;
  9443. NK_ASSERT(list);
  9444. if (!list || !col.a) return;
  9445. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9446. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9447. nk_draw_list_path_fill(list, col);
  9448. }
  9449. NK_API void
  9450. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  9451. float radius, struct nk_color col, unsigned int segs, float thickness)
  9452. {
  9453. float a_max;
  9454. NK_ASSERT(list);
  9455. if (!list || !col.a) return;
  9456. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9457. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9458. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9459. }
  9460. NK_API void
  9461. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  9462. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  9463. struct nk_color col, unsigned int segments, float thickness)
  9464. {
  9465. NK_ASSERT(list);
  9466. if (!list || !col.a) return;
  9467. nk_draw_list_path_line_to(list, p0);
  9468. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  9469. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9470. }
  9471. NK_INTERN void
  9472. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  9473. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  9474. struct nk_color color)
  9475. {
  9476. void *vtx;
  9477. struct nk_vec2 uvb;
  9478. struct nk_vec2 uvd;
  9479. struct nk_vec2 b;
  9480. struct nk_vec2 d;
  9481. struct nk_colorf col;
  9482. nk_draw_index *idx;
  9483. nk_draw_index index;
  9484. NK_ASSERT(list);
  9485. if (!list) return;
  9486. nk_color_fv(&col.r, color);
  9487. uvb = nk_vec2(uvc.x, uva.y);
  9488. uvd = nk_vec2(uva.x, uvc.y);
  9489. b = nk_vec2(c.x, a.y);
  9490. d = nk_vec2(a.x, c.y);
  9491. index = (nk_draw_index)list->vertex_count;
  9492. vtx = nk_draw_list_alloc_vertices(list, 4);
  9493. idx = nk_draw_list_alloc_elements(list, 6);
  9494. if (!vtx || !idx) return;
  9495. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9496. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9497. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9498. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  9499. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  9500. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  9501. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  9502. }
  9503. NK_API void
  9504. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  9505. struct nk_rect rect, struct nk_color color)
  9506. {
  9507. NK_ASSERT(list);
  9508. if (!list) return;
  9509. /* push new command with given texture */
  9510. nk_draw_list_push_image(list, texture.handle);
  9511. if (nk_image_is_subimage(&texture)) {
  9512. /* add region inside of the texture */
  9513. struct nk_vec2 uv[2];
  9514. uv[0].x = (float)texture.region[0]/(float)texture.w;
  9515. uv[0].y = (float)texture.region[1]/(float)texture.h;
  9516. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  9517. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  9518. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9519. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  9520. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9521. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  9522. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  9523. }
  9524. NK_API void
  9525. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  9526. struct nk_rect rect, const char *text, int len, float font_height,
  9527. struct nk_color fg)
  9528. {
  9529. float x = 0;
  9530. int text_len = 0;
  9531. nk_rune unicode = 0;
  9532. nk_rune next = 0;
  9533. int glyph_len = 0;
  9534. int next_glyph_len = 0;
  9535. struct nk_user_font_glyph g;
  9536. NK_ASSERT(list);
  9537. if (!list || !len || !text) return;
  9538. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  9539. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  9540. nk_draw_list_push_image(list, font->texture);
  9541. x = rect.x;
  9542. glyph_len = nk_utf_decode(text, &unicode, len);
  9543. if (!glyph_len) return;
  9544. /* draw every glyph image */
  9545. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  9546. while (text_len < len && glyph_len) {
  9547. float gx, gy, gh, gw;
  9548. float char_width = 0;
  9549. if (unicode == NK_UTF_INVALID) break;
  9550. /* query currently drawn glyph information */
  9551. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  9552. font->query(font->userdata, font_height, &g, unicode,
  9553. (next == NK_UTF_INVALID) ? '\0' : next);
  9554. /* calculate and draw glyph drawing rectangle and image */
  9555. gx = x + g.offset.x;
  9556. gy = rect.y + g.offset.y;
  9557. gw = g.width; gh = g.height;
  9558. char_width = g.xadvance;
  9559. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  9560. g.uv[0], g.uv[1], fg);
  9561. /* offset next glyph */
  9562. text_len += glyph_len;
  9563. x += char_width;
  9564. glyph_len = next_glyph_len;
  9565. unicode = next;
  9566. }
  9567. }
  9568. NK_API nk_flags
  9569. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  9570. struct nk_buffer *vertices, struct nk_buffer *elements,
  9571. const struct nk_convert_config *config)
  9572. {
  9573. nk_flags res = NK_CONVERT_SUCCESS;
  9574. const struct nk_command *cmd;
  9575. NK_ASSERT(ctx);
  9576. NK_ASSERT(cmds);
  9577. NK_ASSERT(vertices);
  9578. NK_ASSERT(elements);
  9579. NK_ASSERT(config);
  9580. NK_ASSERT(config->vertex_layout);
  9581. NK_ASSERT(config->vertex_size);
  9582. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  9583. return NK_CONVERT_INVALID_PARAM;
  9584. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  9585. config->line_AA, config->shape_AA);
  9586. nk_foreach(cmd, ctx)
  9587. {
  9588. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9589. ctx->draw_list.userdata = cmd->userdata;
  9590. #endif
  9591. switch (cmd->type) {
  9592. case NK_COMMAND_NOP: break;
  9593. case NK_COMMAND_SCISSOR: {
  9594. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  9595. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  9596. } break;
  9597. case NK_COMMAND_LINE: {
  9598. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  9599. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  9600. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  9601. } break;
  9602. case NK_COMMAND_CURVE: {
  9603. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  9604. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  9605. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  9606. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  9607. config->curve_segment_count, q->line_thickness);
  9608. } break;
  9609. case NK_COMMAND_RECT: {
  9610. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  9611. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9612. r->color, (float)r->rounding, r->line_thickness);
  9613. } break;
  9614. case NK_COMMAND_RECT_FILLED: {
  9615. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  9616. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9617. r->color, (float)r->rounding);
  9618. } break;
  9619. case NK_COMMAND_RECT_MULTI_COLOR: {
  9620. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  9621. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  9622. r->left, r->top, r->right, r->bottom);
  9623. } break;
  9624. case NK_COMMAND_CIRCLE: {
  9625. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  9626. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9627. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9628. config->circle_segment_count, c->line_thickness);
  9629. } break;
  9630. case NK_COMMAND_CIRCLE_FILLED: {
  9631. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  9632. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  9633. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  9634. config->circle_segment_count);
  9635. } break;
  9636. case NK_COMMAND_ARC: {
  9637. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  9638. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9639. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9640. c->a[0], c->a[1], config->arc_segment_count);
  9641. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  9642. } break;
  9643. case NK_COMMAND_ARC_FILLED: {
  9644. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  9645. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  9646. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  9647. c->a[0], c->a[1], config->arc_segment_count);
  9648. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  9649. } break;
  9650. case NK_COMMAND_TRIANGLE: {
  9651. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  9652. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9653. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  9654. t->line_thickness);
  9655. } break;
  9656. case NK_COMMAND_TRIANGLE_FILLED: {
  9657. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  9658. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  9659. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  9660. } break;
  9661. case NK_COMMAND_POLYGON: {
  9662. int i;
  9663. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  9664. for (i = 0; i < p->point_count; ++i) {
  9665. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9666. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9667. }
  9668. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  9669. } break;
  9670. case NK_COMMAND_POLYGON_FILLED: {
  9671. int i;
  9672. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  9673. for (i = 0; i < p->point_count; ++i) {
  9674. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9675. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9676. }
  9677. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  9678. } break;
  9679. case NK_COMMAND_POLYLINE: {
  9680. int i;
  9681. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  9682. for (i = 0; i < p->point_count; ++i) {
  9683. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  9684. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  9685. }
  9686. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  9687. } break;
  9688. case NK_COMMAND_TEXT: {
  9689. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  9690. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  9691. t->string, t->length, t->height, t->foreground);
  9692. } break;
  9693. case NK_COMMAND_IMAGE: {
  9694. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  9695. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  9696. } break;
  9697. case NK_COMMAND_CUSTOM: {
  9698. const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
  9699. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  9700. } break;
  9701. default: break;
  9702. }
  9703. }
  9704. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
  9705. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
  9706. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
  9707. return res;
  9708. }
  9709. NK_API const struct nk_draw_command*
  9710. nk__draw_begin(const struct nk_context *ctx,
  9711. const struct nk_buffer *buffer)
  9712. {return nk__draw_list_begin(&ctx->draw_list, buffer);}
  9713. NK_API const struct nk_draw_command*
  9714. nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
  9715. {return nk__draw_list_end(&ctx->draw_list, buffer);}
  9716. NK_API const struct nk_draw_command*
  9717. nk__draw_next(const struct nk_draw_command *cmd,
  9718. const struct nk_buffer *buffer, const struct nk_context *ctx)
  9719. {return nk__draw_list_next(cmd, buffer, &ctx->draw_list);}
  9720. #endif
  9721. /*
  9722. * ==============================================================
  9723. *
  9724. * FONT HANDLING
  9725. *
  9726. * ===============================================================
  9727. */
  9728. #ifdef NK_INCLUDE_FONT_BAKING
  9729. /* -------------------------------------------------------------
  9730. *
  9731. * RECT PACK
  9732. *
  9733. * --------------------------------------------------------------*/
  9734. /* stb_rect_pack.h - v0.05 - public domain - rectangle packing */
  9735. /* Sean Barrett 2014 */
  9736. #define NK_RP__MAXVAL 0xffff
  9737. typedef unsigned short nk_rp_coord;
  9738. struct nk_rp_rect {
  9739. /* reserved for your use: */
  9740. int id;
  9741. /* input: */
  9742. nk_rp_coord w, h;
  9743. /* output: */
  9744. nk_rp_coord x, y;
  9745. int was_packed;
  9746. /* non-zero if valid packing */
  9747. }; /* 16 bytes, nominally */
  9748. struct nk_rp_node {
  9749. nk_rp_coord x,y;
  9750. struct nk_rp_node *next;
  9751. };
  9752. struct nk_rp_context {
  9753. int width;
  9754. int height;
  9755. int align;
  9756. int init_mode;
  9757. int heuristic;
  9758. int num_nodes;
  9759. struct nk_rp_node *active_head;
  9760. struct nk_rp_node *free_head;
  9761. struct nk_rp_node extra[2];
  9762. /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  9763. };
  9764. struct nk_rp__findresult {
  9765. int x,y;
  9766. struct nk_rp_node **prev_link;
  9767. };
  9768. enum NK_RP_HEURISTIC {
  9769. NK_RP_HEURISTIC_Skyline_default=0,
  9770. NK_RP_HEURISTIC_Skyline_BL_sortHeight = NK_RP_HEURISTIC_Skyline_default,
  9771. NK_RP_HEURISTIC_Skyline_BF_sortHeight
  9772. };
  9773. enum NK_RP_INIT_STATE{NK_RP__INIT_skyline = 1};
  9774. NK_INTERN void
  9775. nk_rp_setup_allow_out_of_mem(struct nk_rp_context *context, int allow_out_of_mem)
  9776. {
  9777. if (allow_out_of_mem)
  9778. /* if it's ok to run out of memory, then don't bother aligning them; */
  9779. /* this gives better packing, but may fail due to OOM (even though */
  9780. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  9781. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  9782. context->align = 1;
  9783. else {
  9784. /* if it's not ok to run out of memory, then quantize the widths */
  9785. /* so that num_nodes is always enough nodes. */
  9786. /* */
  9787. /* I.e. num_nodes * align >= width */
  9788. /* align >= width / num_nodes */
  9789. /* align = ceil(width/num_nodes) */
  9790. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  9791. }
  9792. }
  9793. NK_INTERN void
  9794. nk_rp_init_target(struct nk_rp_context *context, int width, int height,
  9795. struct nk_rp_node *nodes, int num_nodes)
  9796. {
  9797. int i;
  9798. #ifndef STBRP_LARGE_RECTS
  9799. NK_ASSERT(width <= 0xffff && height <= 0xffff);
  9800. #endif
  9801. for (i=0; i < num_nodes-1; ++i)
  9802. nodes[i].next = &nodes[i+1];
  9803. nodes[i].next = 0;
  9804. context->init_mode = NK_RP__INIT_skyline;
  9805. context->heuristic = NK_RP_HEURISTIC_Skyline_default;
  9806. context->free_head = &nodes[0];
  9807. context->active_head = &context->extra[0];
  9808. context->width = width;
  9809. context->height = height;
  9810. context->num_nodes = num_nodes;
  9811. nk_rp_setup_allow_out_of_mem(context, 0);
  9812. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  9813. context->extra[0].x = 0;
  9814. context->extra[0].y = 0;
  9815. context->extra[0].next = &context->extra[1];
  9816. context->extra[1].x = (nk_rp_coord) width;
  9817. context->extra[1].y = 65535;
  9818. context->extra[1].next = 0;
  9819. }
  9820. /* find minimum y position if it starts at x1 */
  9821. NK_INTERN int
  9822. nk_rp__skyline_find_min_y(struct nk_rp_context *c, struct nk_rp_node *first,
  9823. int x0, int width, int *pwaste)
  9824. {
  9825. struct nk_rp_node *node = first;
  9826. int x1 = x0 + width;
  9827. int min_y, visited_width, waste_area;
  9828. NK_ASSERT(first->x <= x0);
  9829. NK_UNUSED(c);
  9830. NK_ASSERT(node->next->x > x0);
  9831. /* we ended up handling this in the caller for efficiency */
  9832. NK_ASSERT(node->x <= x0);
  9833. min_y = 0;
  9834. waste_area = 0;
  9835. visited_width = 0;
  9836. while (node->x < x1)
  9837. {
  9838. if (node->y > min_y) {
  9839. /* raise min_y higher. */
  9840. /* we've accounted for all waste up to min_y, */
  9841. /* but we'll now add more waste for everything we've visited */
  9842. waste_area += visited_width * (node->y - min_y);
  9843. min_y = node->y;
  9844. /* the first time through, visited_width might be reduced */
  9845. if (node->x < x0)
  9846. visited_width += node->next->x - x0;
  9847. else
  9848. visited_width += node->next->x - node->x;
  9849. } else {
  9850. /* add waste area */
  9851. int under_width = node->next->x - node->x;
  9852. if (under_width + visited_width > width)
  9853. under_width = width - visited_width;
  9854. waste_area += under_width * (min_y - node->y);
  9855. visited_width += under_width;
  9856. }
  9857. node = node->next;
  9858. }
  9859. *pwaste = waste_area;
  9860. return min_y;
  9861. }
  9862. NK_INTERN struct nk_rp__findresult
  9863. nk_rp__skyline_find_best_pos(struct nk_rp_context *c, int width, int height)
  9864. {
  9865. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  9866. struct nk_rp__findresult fr;
  9867. struct nk_rp_node **prev, *node, *tail, **best = 0;
  9868. /* align to multiple of c->align */
  9869. width = (width + c->align - 1);
  9870. width -= width % c->align;
  9871. NK_ASSERT(width % c->align == 0);
  9872. node = c->active_head;
  9873. prev = &c->active_head;
  9874. while (node->x + width <= c->width) {
  9875. int y,waste;
  9876. y = nk_rp__skyline_find_min_y(c, node, node->x, width, &waste);
  9877. /* actually just want to test BL */
  9878. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BL_sortHeight) {
  9879. /* bottom left */
  9880. if (y < best_y) {
  9881. best_y = y;
  9882. best = prev;
  9883. }
  9884. } else {
  9885. /* best-fit */
  9886. if (y + height <= c->height) {
  9887. /* can only use it if it first vertically */
  9888. if (y < best_y || (y == best_y && waste < best_waste)) {
  9889. best_y = y;
  9890. best_waste = waste;
  9891. best = prev;
  9892. }
  9893. }
  9894. }
  9895. prev = &node->next;
  9896. node = node->next;
  9897. }
  9898. best_x = (best == 0) ? 0 : (*best)->x;
  9899. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  9900. /* */
  9901. /* e.g, if fitting */
  9902. /* */
  9903. /* ____________________ */
  9904. /* |____________________| */
  9905. /* */
  9906. /* into */
  9907. /* */
  9908. /* | | */
  9909. /* | ____________| */
  9910. /* |____________| */
  9911. /* */
  9912. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  9913. /* */
  9914. /* This makes BF take about 2x the time */
  9915. if (c->heuristic == NK_RP_HEURISTIC_Skyline_BF_sortHeight)
  9916. {
  9917. tail = c->active_head;
  9918. node = c->active_head;
  9919. prev = &c->active_head;
  9920. /* find first node that's admissible */
  9921. while (tail->x < width)
  9922. tail = tail->next;
  9923. while (tail)
  9924. {
  9925. int xpos = tail->x - width;
  9926. int y,waste;
  9927. NK_ASSERT(xpos >= 0);
  9928. /* find the left position that matches this */
  9929. while (node->next->x <= xpos) {
  9930. prev = &node->next;
  9931. node = node->next;
  9932. }
  9933. NK_ASSERT(node->next->x > xpos && node->x <= xpos);
  9934. y = nk_rp__skyline_find_min_y(c, node, xpos, width, &waste);
  9935. if (y + height < c->height) {
  9936. if (y <= best_y) {
  9937. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  9938. best_x = xpos;
  9939. NK_ASSERT(y <= best_y);
  9940. best_y = y;
  9941. best_waste = waste;
  9942. best = prev;
  9943. }
  9944. }
  9945. }
  9946. tail = tail->next;
  9947. }
  9948. }
  9949. fr.prev_link = best;
  9950. fr.x = best_x;
  9951. fr.y = best_y;
  9952. return fr;
  9953. }
  9954. NK_INTERN struct nk_rp__findresult
  9955. nk_rp__skyline_pack_rectangle(struct nk_rp_context *context, int width, int height)
  9956. {
  9957. /* find best position according to heuristic */
  9958. struct nk_rp__findresult res = nk_rp__skyline_find_best_pos(context, width, height);
  9959. struct nk_rp_node *node, *cur;
  9960. /* bail if: */
  9961. /* 1. it failed */
  9962. /* 2. the best node doesn't fit (we don't always check this) */
  9963. /* 3. we're out of memory */
  9964. if (res.prev_link == 0 || res.y + height > context->height || context->free_head == 0) {
  9965. res.prev_link = 0;
  9966. return res;
  9967. }
  9968. /* on success, create new node */
  9969. node = context->free_head;
  9970. node->x = (nk_rp_coord) res.x;
  9971. node->y = (nk_rp_coord) (res.y + height);
  9972. context->free_head = node->next;
  9973. /* insert the new node into the right starting point, and */
  9974. /* let 'cur' point to the remaining nodes needing to be */
  9975. /* stitched back in */
  9976. cur = *res.prev_link;
  9977. if (cur->x < res.x) {
  9978. /* preserve the existing one, so start testing with the next one */
  9979. struct nk_rp_node *next = cur->next;
  9980. cur->next = node;
  9981. cur = next;
  9982. } else {
  9983. *res.prev_link = node;
  9984. }
  9985. /* from here, traverse cur and free the nodes, until we get to one */
  9986. /* that shouldn't be freed */
  9987. while (cur->next && cur->next->x <= res.x + width) {
  9988. struct nk_rp_node *next = cur->next;
  9989. /* move the current node to the free list */
  9990. cur->next = context->free_head;
  9991. context->free_head = cur;
  9992. cur = next;
  9993. }
  9994. /* stitch the list back in */
  9995. node->next = cur;
  9996. if (cur->x < res.x + width)
  9997. cur->x = (nk_rp_coord) (res.x + width);
  9998. return res;
  9999. }
  10000. NK_INTERN int
  10001. nk_rect_height_compare(const void *a, const void *b)
  10002. {
  10003. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10004. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10005. if (p->h > q->h)
  10006. return -1;
  10007. if (p->h < q->h)
  10008. return 1;
  10009. return (p->w > q->w) ? -1 : (p->w < q->w);
  10010. }
  10011. NK_INTERN int
  10012. nk_rect_original_order(const void *a, const void *b)
  10013. {
  10014. const struct nk_rp_rect *p = (const struct nk_rp_rect *) a;
  10015. const struct nk_rp_rect *q = (const struct nk_rp_rect *) b;
  10016. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  10017. }
  10018. NK_INTERN void
  10019. nk_rp_qsort(struct nk_rp_rect *array, unsigned int len, int(*cmp)(const void*,const void*))
  10020. {
  10021. /* iterative quick sort */
  10022. #define NK_MAX_SORT_STACK 64
  10023. unsigned right, left = 0, stack[NK_MAX_SORT_STACK], pos = 0;
  10024. unsigned seed = len/2 * 69069+1;
  10025. for (;;) {
  10026. for (; left+1 < len; len++) {
  10027. struct nk_rp_rect pivot, tmp;
  10028. if (pos == NK_MAX_SORT_STACK) len = stack[pos = 0];
  10029. pivot = array[left+seed%(len-left)];
  10030. seed = seed * 69069 + 1;
  10031. stack[pos++] = len;
  10032. for (right = left-1;;) {
  10033. while (cmp(&array[++right], &pivot) < 0);
  10034. while (cmp(&pivot, &array[--len]) < 0);
  10035. if (right >= len) break;
  10036. tmp = array[right];
  10037. array[right] = array[len];
  10038. array[len] = tmp;
  10039. }
  10040. }
  10041. if (pos == 0) break;
  10042. left = len;
  10043. len = stack[--pos];
  10044. }
  10045. #undef NK_MAX_SORT_STACK
  10046. }
  10047. NK_INTERN void
  10048. nk_rp_pack_rects(struct nk_rp_context *context, struct nk_rp_rect *rects, int num_rects)
  10049. {
  10050. int i;
  10051. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  10052. for (i=0; i < num_rects; ++i) {
  10053. rects[i].was_packed = i;
  10054. }
  10055. /* sort according to heuristic */
  10056. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_height_compare);
  10057. for (i=0; i < num_rects; ++i) {
  10058. struct nk_rp__findresult fr = nk_rp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  10059. if (fr.prev_link) {
  10060. rects[i].x = (nk_rp_coord) fr.x;
  10061. rects[i].y = (nk_rp_coord) fr.y;
  10062. } else {
  10063. rects[i].x = rects[i].y = NK_RP__MAXVAL;
  10064. }
  10065. }
  10066. /* unsort */
  10067. nk_rp_qsort(rects, (unsigned)num_rects, nk_rect_original_order);
  10068. /* set was_packed flags */
  10069. for (i=0; i < num_rects; ++i)
  10070. rects[i].was_packed = !(rects[i].x == NK_RP__MAXVAL && rects[i].y == NK_RP__MAXVAL);
  10071. }
  10072. /*
  10073. * ==============================================================
  10074. *
  10075. * TRUETYPE
  10076. *
  10077. * ===============================================================
  10078. */
  10079. /* stb_truetype.h - v1.07 - public domain */
  10080. #define NK_TT_MAX_OVERSAMPLE 8
  10081. #define NK_TT__OVER_MASK (NK_TT_MAX_OVERSAMPLE-1)
  10082. struct nk_tt_bakedchar {
  10083. unsigned short x0,y0,x1,y1;
  10084. /* coordinates of bbox in bitmap */
  10085. float xoff,yoff,xadvance;
  10086. };
  10087. struct nk_tt_aligned_quad{
  10088. float x0,y0,s0,t0; /* top-left */
  10089. float x1,y1,s1,t1; /* bottom-right */
  10090. };
  10091. struct nk_tt_packedchar {
  10092. unsigned short x0,y0,x1,y1;
  10093. /* coordinates of bbox in bitmap */
  10094. float xoff,yoff,xadvance;
  10095. float xoff2,yoff2;
  10096. };
  10097. struct nk_tt_pack_range {
  10098. float font_size;
  10099. int first_unicode_codepoint_in_range;
  10100. /* if non-zero, then the chars are continuous, and this is the first codepoint */
  10101. int *array_of_unicode_codepoints;
  10102. /* if non-zero, then this is an array of unicode codepoints */
  10103. int num_chars;
  10104. struct nk_tt_packedchar *chardata_for_range; /* output */
  10105. unsigned char h_oversample, v_oversample;
  10106. /* don't set these, they're used internally */
  10107. };
  10108. struct nk_tt_pack_context {
  10109. void *pack_info;
  10110. int width;
  10111. int height;
  10112. int stride_in_bytes;
  10113. int padding;
  10114. unsigned int h_oversample, v_oversample;
  10115. unsigned char *pixels;
  10116. void *nodes;
  10117. };
  10118. struct nk_tt_fontinfo {
  10119. const unsigned char* data; /* pointer to .ttf file */
  10120. int fontstart;/* offset of start of font */
  10121. int numGlyphs;/* number of glyphs, needed for range checking */
  10122. int loca,head,glyf,hhea,hmtx,kern; /* table locations as offset from start of .ttf */
  10123. int index_map; /* a cmap mapping for our chosen character encoding */
  10124. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  10125. };
  10126. enum {
  10127. NK_TT_vmove=1,
  10128. NK_TT_vline,
  10129. NK_TT_vcurve
  10130. };
  10131. struct nk_tt_vertex {
  10132. short x,y,cx,cy;
  10133. unsigned char type,padding;
  10134. };
  10135. struct nk_tt__bitmap{
  10136. int w,h,stride;
  10137. unsigned char *pixels;
  10138. };
  10139. struct nk_tt__hheap_chunk {
  10140. struct nk_tt__hheap_chunk *next;
  10141. };
  10142. struct nk_tt__hheap {
  10143. struct nk_allocator alloc;
  10144. struct nk_tt__hheap_chunk *head;
  10145. void *first_free;
  10146. int num_remaining_in_head_chunk;
  10147. };
  10148. struct nk_tt__edge {
  10149. float x0,y0, x1,y1;
  10150. int invert;
  10151. };
  10152. struct nk_tt__active_edge {
  10153. struct nk_tt__active_edge *next;
  10154. float fx,fdx,fdy;
  10155. float direction;
  10156. float sy;
  10157. float ey;
  10158. };
  10159. struct nk_tt__point {float x,y;};
  10160. #define NK_TT_MACSTYLE_DONTCARE 0
  10161. #define NK_TT_MACSTYLE_BOLD 1
  10162. #define NK_TT_MACSTYLE_ITALIC 2
  10163. #define NK_TT_MACSTYLE_UNDERSCORE 4
  10164. #define NK_TT_MACSTYLE_NONE 8
  10165. /* <= not same as 0, this makes us check the bitfield is 0 */
  10166. enum { /* platformID */
  10167. NK_TT_PLATFORM_ID_UNICODE =0,
  10168. NK_TT_PLATFORM_ID_MAC =1,
  10169. NK_TT_PLATFORM_ID_ISO =2,
  10170. NK_TT_PLATFORM_ID_MICROSOFT =3
  10171. };
  10172. enum { /* encodingID for NK_TT_PLATFORM_ID_UNICODE */
  10173. NK_TT_UNICODE_EID_UNICODE_1_0 =0,
  10174. NK_TT_UNICODE_EID_UNICODE_1_1 =1,
  10175. NK_TT_UNICODE_EID_ISO_10646 =2,
  10176. NK_TT_UNICODE_EID_UNICODE_2_0_BMP=3,
  10177. NK_TT_UNICODE_EID_UNICODE_2_0_FULL=4
  10178. };
  10179. enum { /* encodingID for NK_TT_PLATFORM_ID_MICROSOFT */
  10180. NK_TT_MS_EID_SYMBOL =0,
  10181. NK_TT_MS_EID_UNICODE_BMP =1,
  10182. NK_TT_MS_EID_SHIFTJIS =2,
  10183. NK_TT_MS_EID_UNICODE_FULL =10
  10184. };
  10185. enum { /* encodingID for NK_TT_PLATFORM_ID_MAC; same as Script Manager codes */
  10186. NK_TT_MAC_EID_ROMAN =0, NK_TT_MAC_EID_ARABIC =4,
  10187. NK_TT_MAC_EID_JAPANESE =1, NK_TT_MAC_EID_HEBREW =5,
  10188. NK_TT_MAC_EID_CHINESE_TRAD =2, NK_TT_MAC_EID_GREEK =6,
  10189. NK_TT_MAC_EID_KOREAN =3, NK_TT_MAC_EID_RUSSIAN =7
  10190. };
  10191. enum { /* languageID for NK_TT_PLATFORM_ID_MICROSOFT; same as LCID... */
  10192. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  10193. NK_TT_MS_LANG_ENGLISH =0x0409, NK_TT_MS_LANG_ITALIAN =0x0410,
  10194. NK_TT_MS_LANG_CHINESE =0x0804, NK_TT_MS_LANG_JAPANESE =0x0411,
  10195. NK_TT_MS_LANG_DUTCH =0x0413, NK_TT_MS_LANG_KOREAN =0x0412,
  10196. NK_TT_MS_LANG_FRENCH =0x040c, NK_TT_MS_LANG_RUSSIAN =0x0419,
  10197. NK_TT_MS_LANG_GERMAN =0x0407, NK_TT_MS_LANG_SPANISH =0x0409,
  10198. NK_TT_MS_LANG_HEBREW =0x040d, NK_TT_MS_LANG_SWEDISH =0x041D
  10199. };
  10200. enum { /* languageID for NK_TT_PLATFORM_ID_MAC */
  10201. NK_TT_MAC_LANG_ENGLISH =0 , NK_TT_MAC_LANG_JAPANESE =11,
  10202. NK_TT_MAC_LANG_ARABIC =12, NK_TT_MAC_LANG_KOREAN =23,
  10203. NK_TT_MAC_LANG_DUTCH =4 , NK_TT_MAC_LANG_RUSSIAN =32,
  10204. NK_TT_MAC_LANG_FRENCH =1 , NK_TT_MAC_LANG_SPANISH =6 ,
  10205. NK_TT_MAC_LANG_GERMAN =2 , NK_TT_MAC_LANG_SWEDISH =5 ,
  10206. NK_TT_MAC_LANG_HEBREW =10, NK_TT_MAC_LANG_CHINESE_SIMPLIFIED =33,
  10207. NK_TT_MAC_LANG_ITALIAN =3 , NK_TT_MAC_LANG_CHINESE_TRAD =19
  10208. };
  10209. #define nk_ttBYTE(p) (* (const nk_byte *) (p))
  10210. #define nk_ttCHAR(p) (* (const char *) (p))
  10211. #if defined(NK_BIGENDIAN) && !defined(NK_ALLOW_UNALIGNED_TRUETYPE)
  10212. #define nk_ttUSHORT(p) (* (nk_ushort *) (p))
  10213. #define nk_ttSHORT(p) (* (nk_short *) (p))
  10214. #define nk_ttULONG(p) (* (nk_uint *) (p))
  10215. #define nk_ttLONG(p) (* (nk_int *) (p))
  10216. #else
  10217. static nk_ushort nk_ttUSHORT(const nk_byte *p) { return (nk_ushort)(p[0]*256 + p[1]); }
  10218. static nk_short nk_ttSHORT(const nk_byte *p) { return (nk_short)(p[0]*256 + p[1]); }
  10219. static nk_uint nk_ttULONG(const nk_byte *p) { return (nk_uint)((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]); }
  10220. #endif
  10221. #define nk_tt_tag4(p,c0,c1,c2,c3)\
  10222. ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  10223. #define nk_tt_tag(p,str) nk_tt_tag4(p,str[0],str[1],str[2],str[3])
  10224. NK_INTERN int nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10225. int glyph_index, struct nk_tt_vertex **pvertices);
  10226. NK_INTERN nk_uint
  10227. nk_tt__find_table(const nk_byte *data, nk_uint fontstart, const char *tag)
  10228. {
  10229. /* @OPTIMIZE: binary search */
  10230. nk_int num_tables = nk_ttUSHORT(data+fontstart+4);
  10231. nk_uint tabledir = fontstart + 12;
  10232. nk_int i;
  10233. for (i = 0; i < num_tables; ++i) {
  10234. nk_uint loc = tabledir + (nk_uint)(16*i);
  10235. if (nk_tt_tag(data+loc+0, tag))
  10236. return nk_ttULONG(data+loc+8);
  10237. }
  10238. return 0;
  10239. }
  10240. NK_INTERN int
  10241. nk_tt_InitFont(struct nk_tt_fontinfo *info, const unsigned char *data2, int fontstart)
  10242. {
  10243. nk_uint cmap, t;
  10244. nk_int i,numTables;
  10245. const nk_byte *data = (const nk_byte *) data2;
  10246. info->data = data;
  10247. info->fontstart = fontstart;
  10248. cmap = nk_tt__find_table(data, (nk_uint)fontstart, "cmap"); /* required */
  10249. info->loca = (int)nk_tt__find_table(data, (nk_uint)fontstart, "loca"); /* required */
  10250. info->head = (int)nk_tt__find_table(data, (nk_uint)fontstart, "head"); /* required */
  10251. info->glyf = (int)nk_tt__find_table(data, (nk_uint)fontstart, "glyf"); /* required */
  10252. info->hhea = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hhea"); /* required */
  10253. info->hmtx = (int)nk_tt__find_table(data, (nk_uint)fontstart, "hmtx"); /* required */
  10254. info->kern = (int)nk_tt__find_table(data, (nk_uint)fontstart, "kern"); /* not required */
  10255. if (!cmap || !info->loca || !info->head || !info->glyf || !info->hhea || !info->hmtx)
  10256. return 0;
  10257. t = nk_tt__find_table(data, (nk_uint)fontstart, "maxp");
  10258. if (t) info->numGlyphs = nk_ttUSHORT(data+t+4);
  10259. else info->numGlyphs = 0xffff;
  10260. /* find a cmap encoding table we understand *now* to avoid searching */
  10261. /* later. (todo: could make this installable) */
  10262. /* the same regardless of glyph. */
  10263. numTables = nk_ttUSHORT(data + cmap + 2);
  10264. info->index_map = 0;
  10265. for (i=0; i < numTables; ++i)
  10266. {
  10267. nk_uint encoding_record = cmap + 4 + 8 * (nk_uint)i;
  10268. /* find an encoding we understand: */
  10269. switch(nk_ttUSHORT(data+encoding_record)) {
  10270. case NK_TT_PLATFORM_ID_MICROSOFT:
  10271. switch (nk_ttUSHORT(data+encoding_record+2)) {
  10272. case NK_TT_MS_EID_UNICODE_BMP:
  10273. case NK_TT_MS_EID_UNICODE_FULL:
  10274. /* MS/Unicode */
  10275. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10276. break;
  10277. default: break;
  10278. } break;
  10279. case NK_TT_PLATFORM_ID_UNICODE:
  10280. /* Mac/iOS has these */
  10281. /* all the encodingIDs are unicode, so we don't bother to check it */
  10282. info->index_map = (int)(cmap + nk_ttULONG(data+encoding_record+4));
  10283. break;
  10284. default: break;
  10285. }
  10286. }
  10287. if (info->index_map == 0)
  10288. return 0;
  10289. info->indexToLocFormat = nk_ttUSHORT(data+info->head + 50);
  10290. return 1;
  10291. }
  10292. NK_INTERN int
  10293. nk_tt_FindGlyphIndex(const struct nk_tt_fontinfo *info, int unicode_codepoint)
  10294. {
  10295. const nk_byte *data = info->data;
  10296. nk_uint index_map = (nk_uint)info->index_map;
  10297. nk_ushort format = nk_ttUSHORT(data + index_map + 0);
  10298. if (format == 0) { /* apple byte encoding */
  10299. nk_int bytes = nk_ttUSHORT(data + index_map + 2);
  10300. if (unicode_codepoint < bytes-6)
  10301. return nk_ttBYTE(data + index_map + 6 + unicode_codepoint);
  10302. return 0;
  10303. } else if (format == 6) {
  10304. nk_uint first = nk_ttUSHORT(data + index_map + 6);
  10305. nk_uint count = nk_ttUSHORT(data + index_map + 8);
  10306. if ((nk_uint) unicode_codepoint >= first && (nk_uint) unicode_codepoint < first+count)
  10307. return nk_ttUSHORT(data + index_map + 10 + (unicode_codepoint - (int)first)*2);
  10308. return 0;
  10309. } else if (format == 2) {
  10310. NK_ASSERT(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  10311. return 0;
  10312. } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  10313. nk_ushort segcount = nk_ttUSHORT(data+index_map+6) >> 1;
  10314. nk_ushort searchRange = nk_ttUSHORT(data+index_map+8) >> 1;
  10315. nk_ushort entrySelector = nk_ttUSHORT(data+index_map+10);
  10316. nk_ushort rangeShift = nk_ttUSHORT(data+index_map+12) >> 1;
  10317. /* do a binary search of the segments */
  10318. nk_uint endCount = index_map + 14;
  10319. nk_uint search = endCount;
  10320. if (unicode_codepoint > 0xffff)
  10321. return 0;
  10322. /* they lie from endCount .. endCount + segCount */
  10323. /* but searchRange is the nearest power of two, so... */
  10324. if (unicode_codepoint >= nk_ttUSHORT(data + search + rangeShift*2))
  10325. search += (nk_uint)(rangeShift*2);
  10326. /* now decrement to bias correctly to find smallest */
  10327. search -= 2;
  10328. while (entrySelector) {
  10329. nk_ushort end;
  10330. searchRange >>= 1;
  10331. end = nk_ttUSHORT(data + search + searchRange*2);
  10332. if (unicode_codepoint > end)
  10333. search += (nk_uint)(searchRange*2);
  10334. --entrySelector;
  10335. }
  10336. search += 2;
  10337. {
  10338. nk_ushort offset, start;
  10339. nk_ushort item = (nk_ushort) ((search - endCount) >> 1);
  10340. NK_ASSERT(unicode_codepoint <= nk_ttUSHORT(data + endCount + 2*item));
  10341. start = nk_ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
  10342. if (unicode_codepoint < start)
  10343. return 0;
  10344. offset = nk_ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
  10345. if (offset == 0)
  10346. return (nk_ushort) (unicode_codepoint + nk_ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
  10347. return nk_ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
  10348. }
  10349. } else if (format == 12 || format == 13) {
  10350. nk_uint ngroups = nk_ttULONG(data+index_map+12);
  10351. nk_int low,high;
  10352. low = 0; high = (nk_int)ngroups;
  10353. /* Binary search the right group. */
  10354. while (low < high) {
  10355. nk_int mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
  10356. nk_uint start_char = nk_ttULONG(data+index_map+16+mid*12);
  10357. nk_uint end_char = nk_ttULONG(data+index_map+16+mid*12+4);
  10358. if ((nk_uint) unicode_codepoint < start_char)
  10359. high = mid;
  10360. else if ((nk_uint) unicode_codepoint > end_char)
  10361. low = mid+1;
  10362. else {
  10363. nk_uint start_glyph = nk_ttULONG(data+index_map+16+mid*12+8);
  10364. if (format == 12)
  10365. return (int)start_glyph + (int)unicode_codepoint - (int)start_char;
  10366. else /* format == 13 */
  10367. return (int)start_glyph;
  10368. }
  10369. }
  10370. return 0; /* not found */
  10371. }
  10372. /* @TODO */
  10373. NK_ASSERT(0);
  10374. return 0;
  10375. }
  10376. NK_INTERN void
  10377. nk_tt_setvertex(struct nk_tt_vertex *v, nk_byte type, nk_int x, nk_int y, nk_int cx, nk_int cy)
  10378. {
  10379. v->type = type;
  10380. v->x = (nk_short) x;
  10381. v->y = (nk_short) y;
  10382. v->cx = (nk_short) cx;
  10383. v->cy = (nk_short) cy;
  10384. }
  10385. NK_INTERN int
  10386. nk_tt__GetGlyfOffset(const struct nk_tt_fontinfo *info, int glyph_index)
  10387. {
  10388. int g1,g2;
  10389. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  10390. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  10391. if (info->indexToLocFormat == 0) {
  10392. g1 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  10393. g2 = info->glyf + nk_ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  10394. } else {
  10395. g1 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4);
  10396. g2 = info->glyf + (int)nk_ttULONG (info->data + info->loca + glyph_index * 4 + 4);
  10397. }
  10398. return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
  10399. }
  10400. NK_INTERN int
  10401. nk_tt_GetGlyphBox(const struct nk_tt_fontinfo *info, int glyph_index,
  10402. int *x0, int *y0, int *x1, int *y1)
  10403. {
  10404. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10405. if (g < 0) return 0;
  10406. if (x0) *x0 = nk_ttSHORT(info->data + g + 2);
  10407. if (y0) *y0 = nk_ttSHORT(info->data + g + 4);
  10408. if (x1) *x1 = nk_ttSHORT(info->data + g + 6);
  10409. if (y1) *y1 = nk_ttSHORT(info->data + g + 8);
  10410. return 1;
  10411. }
  10412. NK_INTERN int
  10413. nk_tt__close_shape(struct nk_tt_vertex *vertices, int num_vertices, int was_off,
  10414. int start_off, nk_int sx, nk_int sy, nk_int scx, nk_int scy, nk_int cx, nk_int cy)
  10415. {
  10416. if (start_off) {
  10417. if (was_off)
  10418. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
  10419. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, sx,sy,scx,scy);
  10420. } else {
  10421. if (was_off)
  10422. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve,sx,sy,cx,cy);
  10423. else
  10424. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline,sx,sy,0,0);
  10425. }
  10426. return num_vertices;
  10427. }
  10428. NK_INTERN int
  10429. nk_tt_GetGlyphShape(const struct nk_tt_fontinfo *info, struct nk_allocator *alloc,
  10430. int glyph_index, struct nk_tt_vertex **pvertices)
  10431. {
  10432. nk_short numberOfContours;
  10433. const nk_byte *endPtsOfContours;
  10434. const nk_byte *data = info->data;
  10435. struct nk_tt_vertex *vertices=0;
  10436. int num_vertices=0;
  10437. int g = nk_tt__GetGlyfOffset(info, glyph_index);
  10438. *pvertices = 0;
  10439. if (g < 0) return 0;
  10440. numberOfContours = nk_ttSHORT(data + g);
  10441. if (numberOfContours > 0) {
  10442. nk_byte flags=0,flagcount;
  10443. nk_int ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
  10444. nk_int x,y,cx,cy,sx,sy, scx,scy;
  10445. const nk_byte *points;
  10446. endPtsOfContours = (data + g + 10);
  10447. ins = nk_ttUSHORT(data + g + 10 + numberOfContours * 2);
  10448. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  10449. n = 1+nk_ttUSHORT(endPtsOfContours + numberOfContours*2-2);
  10450. m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
  10451. vertices = (struct nk_tt_vertex *)alloc->alloc(alloc->userdata, 0, (nk_size)m * sizeof(vertices[0]));
  10452. if (vertices == 0)
  10453. return 0;
  10454. next_move = 0;
  10455. flagcount=0;
  10456. /* in first pass, we load uninterpreted data into the allocated array */
  10457. /* above, shifted to the end of the array so we won't overwrite it when */
  10458. /* we create our final data starting from the front */
  10459. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  10460. /* first load flags */
  10461. for (i=0; i < n; ++i) {
  10462. if (flagcount == 0) {
  10463. flags = *points++;
  10464. if (flags & 8)
  10465. flagcount = *points++;
  10466. } else --flagcount;
  10467. vertices[off+i].type = flags;
  10468. }
  10469. /* now load x coordinates */
  10470. x=0;
  10471. for (i=0; i < n; ++i) {
  10472. flags = vertices[off+i].type;
  10473. if (flags & 2) {
  10474. nk_short dx = *points++;
  10475. x += (flags & 16) ? dx : -dx; /* ??? */
  10476. } else {
  10477. if (!(flags & 16)) {
  10478. x = x + (nk_short) (points[0]*256 + points[1]);
  10479. points += 2;
  10480. }
  10481. }
  10482. vertices[off+i].x = (nk_short) x;
  10483. }
  10484. /* now load y coordinates */
  10485. y=0;
  10486. for (i=0; i < n; ++i) {
  10487. flags = vertices[off+i].type;
  10488. if (flags & 4) {
  10489. nk_short dy = *points++;
  10490. y += (flags & 32) ? dy : -dy; /* ??? */
  10491. } else {
  10492. if (!(flags & 32)) {
  10493. y = y + (nk_short) (points[0]*256 + points[1]);
  10494. points += 2;
  10495. }
  10496. }
  10497. vertices[off+i].y = (nk_short) y;
  10498. }
  10499. /* now convert them to our format */
  10500. num_vertices=0;
  10501. sx = sy = cx = cy = scx = scy = 0;
  10502. for (i=0; i < n; ++i)
  10503. {
  10504. flags = vertices[off+i].type;
  10505. x = (nk_short) vertices[off+i].x;
  10506. y = (nk_short) vertices[off+i].y;
  10507. if (next_move == i) {
  10508. if (i != 0)
  10509. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10510. /* now start the new one */
  10511. start_off = !(flags & 1);
  10512. if (start_off) {
  10513. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  10514. /* where we can start, and we need to save some state for when we wraparound. */
  10515. scx = x;
  10516. scy = y;
  10517. if (!(vertices[off+i+1].type & 1)) {
  10518. /* next point is also a curve point, so interpolate an on-point curve */
  10519. sx = (x + (nk_int) vertices[off+i+1].x) >> 1;
  10520. sy = (y + (nk_int) vertices[off+i+1].y) >> 1;
  10521. } else {
  10522. /* otherwise just use the next point as our start point */
  10523. sx = (nk_int) vertices[off+i+1].x;
  10524. sy = (nk_int) vertices[off+i+1].y;
  10525. ++i; /* we're using point i+1 as the starting point, so skip it */
  10526. }
  10527. } else {
  10528. sx = x;
  10529. sy = y;
  10530. }
  10531. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vmove,sx,sy,0,0);
  10532. was_off = 0;
  10533. next_move = 1 + nk_ttUSHORT(endPtsOfContours+j*2);
  10534. ++j;
  10535. } else {
  10536. if (!(flags & 1))
  10537. { /* if it's a curve */
  10538. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  10539. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
  10540. cx = x;
  10541. cy = y;
  10542. was_off = 1;
  10543. } else {
  10544. if (was_off)
  10545. nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vcurve, x,y, cx, cy);
  10546. else nk_tt_setvertex(&vertices[num_vertices++], NK_TT_vline, x,y,0,0);
  10547. was_off = 0;
  10548. }
  10549. }
  10550. }
  10551. num_vertices = nk_tt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  10552. } else if (numberOfContours == -1) {
  10553. /* Compound shapes. */
  10554. int more = 1;
  10555. const nk_byte *comp = data + g + 10;
  10556. num_vertices = 0;
  10557. vertices = 0;
  10558. while (more)
  10559. {
  10560. nk_ushort flags, gidx;
  10561. int comp_num_verts = 0, i;
  10562. struct nk_tt_vertex *comp_verts = 0, *tmp = 0;
  10563. float mtx[6] = {1,0,0,1,0,0}, m, n;
  10564. flags = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10565. gidx = (nk_ushort)nk_ttSHORT(comp); comp+=2;
  10566. if (flags & 2) { /* XY values */
  10567. if (flags & 1) { /* shorts */
  10568. mtx[4] = nk_ttSHORT(comp); comp+=2;
  10569. mtx[5] = nk_ttSHORT(comp); comp+=2;
  10570. } else {
  10571. mtx[4] = nk_ttCHAR(comp); comp+=1;
  10572. mtx[5] = nk_ttCHAR(comp); comp+=1;
  10573. }
  10574. } else {
  10575. /* @TODO handle matching point */
  10576. NK_ASSERT(0);
  10577. }
  10578. if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
  10579. mtx[0] = mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10580. mtx[1] = mtx[2] = 0;
  10581. } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  10582. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10583. mtx[1] = mtx[2] = 0;
  10584. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10585. } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
  10586. mtx[0] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10587. mtx[1] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10588. mtx[2] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10589. mtx[3] = nk_ttSHORT(comp)/16384.0f; comp+=2;
  10590. }
  10591. /* Find transformation scales. */
  10592. m = (float) NK_SQRT(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
  10593. n = (float) NK_SQRT(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
  10594. /* Get indexed glyph. */
  10595. comp_num_verts = nk_tt_GetGlyphShape(info, alloc, gidx, &comp_verts);
  10596. if (comp_num_verts > 0)
  10597. {
  10598. /* Transform vertices. */
  10599. for (i = 0; i < comp_num_verts; ++i) {
  10600. struct nk_tt_vertex* v = &comp_verts[i];
  10601. short x,y;
  10602. x=v->x; y=v->y;
  10603. v->x = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10604. v->y = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10605. x=v->cx; y=v->cy;
  10606. v->cx = (short)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  10607. v->cy = (short)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  10608. }
  10609. /* Append vertices. */
  10610. tmp = (struct nk_tt_vertex*)alloc->alloc(alloc->userdata, 0,
  10611. (nk_size)(num_vertices+comp_num_verts)*sizeof(struct nk_tt_vertex));
  10612. if (!tmp) {
  10613. if (vertices) alloc->free(alloc->userdata, vertices);
  10614. if (comp_verts) alloc->free(alloc->userdata, comp_verts);
  10615. return 0;
  10616. }
  10617. if (num_vertices > 0) NK_MEMCPY(tmp, vertices, (nk_size)num_vertices*sizeof(struct nk_tt_vertex));
  10618. NK_MEMCPY(tmp+num_vertices, comp_verts, (nk_size)comp_num_verts*sizeof(struct nk_tt_vertex));
  10619. if (vertices) alloc->free(alloc->userdata,vertices);
  10620. vertices = tmp;
  10621. alloc->free(alloc->userdata,comp_verts);
  10622. num_vertices += comp_num_verts;
  10623. }
  10624. /* More components ? */
  10625. more = flags & (1<<5);
  10626. }
  10627. } else if (numberOfContours < 0) {
  10628. /* @TODO other compound variations? */
  10629. NK_ASSERT(0);
  10630. } else {
  10631. /* numberOfCounters == 0, do nothing */
  10632. }
  10633. *pvertices = vertices;
  10634. return num_vertices;
  10635. }
  10636. NK_INTERN void
  10637. nk_tt_GetGlyphHMetrics(const struct nk_tt_fontinfo *info, int glyph_index,
  10638. int *advanceWidth, int *leftSideBearing)
  10639. {
  10640. nk_ushort numOfLongHorMetrics = nk_ttUSHORT(info->data+info->hhea + 34);
  10641. if (glyph_index < numOfLongHorMetrics) {
  10642. if (advanceWidth)
  10643. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index);
  10644. if (leftSideBearing)
  10645. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
  10646. } else {
  10647. if (advanceWidth)
  10648. *advanceWidth = nk_ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
  10649. if (leftSideBearing)
  10650. *leftSideBearing = nk_ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
  10651. }
  10652. }
  10653. NK_INTERN void
  10654. nk_tt_GetFontVMetrics(const struct nk_tt_fontinfo *info,
  10655. int *ascent, int *descent, int *lineGap)
  10656. {
  10657. if (ascent ) *ascent = nk_ttSHORT(info->data+info->hhea + 4);
  10658. if (descent) *descent = nk_ttSHORT(info->data+info->hhea + 6);
  10659. if (lineGap) *lineGap = nk_ttSHORT(info->data+info->hhea + 8);
  10660. }
  10661. NK_INTERN float
  10662. nk_tt_ScaleForPixelHeight(const struct nk_tt_fontinfo *info, float height)
  10663. {
  10664. int fheight = nk_ttSHORT(info->data + info->hhea + 4) - nk_ttSHORT(info->data + info->hhea + 6);
  10665. return (float) height / (float)fheight;
  10666. }
  10667. NK_INTERN float
  10668. nk_tt_ScaleForMappingEmToPixels(const struct nk_tt_fontinfo *info, float pixels)
  10669. {
  10670. int unitsPerEm = nk_ttUSHORT(info->data + info->head + 18);
  10671. return pixels / (float)unitsPerEm;
  10672. }
  10673. /*-------------------------------------------------------------
  10674. * antialiasing software rasterizer
  10675. * --------------------------------------------------------------*/
  10676. NK_INTERN void
  10677. nk_tt_GetGlyphBitmapBoxSubpixel(const struct nk_tt_fontinfo *font,
  10678. int glyph, float scale_x, float scale_y,float shift_x, float shift_y,
  10679. int *ix0, int *iy0, int *ix1, int *iy1)
  10680. {
  10681. int x0,y0,x1,y1;
  10682. if (!nk_tt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
  10683. /* e.g. space character */
  10684. if (ix0) *ix0 = 0;
  10685. if (iy0) *iy0 = 0;
  10686. if (ix1) *ix1 = 0;
  10687. if (iy1) *iy1 = 0;
  10688. } else {
  10689. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  10690. if (ix0) *ix0 = nk_ifloorf((float)x0 * scale_x + shift_x);
  10691. if (iy0) *iy0 = nk_ifloorf((float)-y1 * scale_y + shift_y);
  10692. if (ix1) *ix1 = nk_iceilf ((float)x1 * scale_x + shift_x);
  10693. if (iy1) *iy1 = nk_iceilf ((float)-y0 * scale_y + shift_y);
  10694. }
  10695. }
  10696. NK_INTERN void
  10697. nk_tt_GetGlyphBitmapBox(const struct nk_tt_fontinfo *font, int glyph,
  10698. float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1)
  10699. {
  10700. nk_tt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
  10701. }
  10702. /*-------------------------------------------------------------
  10703. * Rasterizer
  10704. * --------------------------------------------------------------*/
  10705. NK_INTERN void*
  10706. nk_tt__hheap_alloc(struct nk_tt__hheap *hh, nk_size size)
  10707. {
  10708. if (hh->first_free) {
  10709. void *p = hh->first_free;
  10710. hh->first_free = * (void **) p;
  10711. return p;
  10712. } else {
  10713. if (hh->num_remaining_in_head_chunk == 0) {
  10714. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  10715. struct nk_tt__hheap_chunk *c = (struct nk_tt__hheap_chunk *)
  10716. hh->alloc.alloc(hh->alloc.userdata, 0,
  10717. sizeof(struct nk_tt__hheap_chunk) + size * (nk_size)count);
  10718. if (c == 0) return 0;
  10719. c->next = hh->head;
  10720. hh->head = c;
  10721. hh->num_remaining_in_head_chunk = count;
  10722. }
  10723. --hh->num_remaining_in_head_chunk;
  10724. return (char *) (hh->head) + size * (nk_size)hh->num_remaining_in_head_chunk;
  10725. }
  10726. }
  10727. NK_INTERN void
  10728. nk_tt__hheap_free(struct nk_tt__hheap *hh, void *p)
  10729. {
  10730. *(void **) p = hh->first_free;
  10731. hh->first_free = p;
  10732. }
  10733. NK_INTERN void
  10734. nk_tt__hheap_cleanup(struct nk_tt__hheap *hh)
  10735. {
  10736. struct nk_tt__hheap_chunk *c = hh->head;
  10737. while (c) {
  10738. struct nk_tt__hheap_chunk *n = c->next;
  10739. hh->alloc.free(hh->alloc.userdata, c);
  10740. c = n;
  10741. }
  10742. }
  10743. NK_INTERN struct nk_tt__active_edge*
  10744. nk_tt__new_active(struct nk_tt__hheap *hh, struct nk_tt__edge *e,
  10745. int off_x, float start_point)
  10746. {
  10747. struct nk_tt__active_edge *z = (struct nk_tt__active_edge *)
  10748. nk_tt__hheap_alloc(hh, sizeof(*z));
  10749. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  10750. /*STBTT_assert(e->y0 <= start_point); */
  10751. if (!z) return z;
  10752. z->fdx = dxdy;
  10753. z->fdy = (dxdy != 0) ? (1/dxdy): 0;
  10754. z->fx = e->x0 + dxdy * (start_point - e->y0);
  10755. z->fx -= (float)off_x;
  10756. z->direction = e->invert ? 1.0f : -1.0f;
  10757. z->sy = e->y0;
  10758. z->ey = e->y1;
  10759. z->next = 0;
  10760. return z;
  10761. }
  10762. NK_INTERN void
  10763. nk_tt__handle_clipped_edge(float *scanline, int x, struct nk_tt__active_edge *e,
  10764. float x0, float y0, float x1, float y1)
  10765. {
  10766. if (y0 == y1) return;
  10767. NK_ASSERT(y0 < y1);
  10768. NK_ASSERT(e->sy <= e->ey);
  10769. if (y0 > e->ey) return;
  10770. if (y1 < e->sy) return;
  10771. if (y0 < e->sy) {
  10772. x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
  10773. y0 = e->sy;
  10774. }
  10775. if (y1 > e->ey) {
  10776. x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
  10777. y1 = e->ey;
  10778. }
  10779. if (x0 == x) NK_ASSERT(x1 <= x+1);
  10780. else if (x0 == x+1) NK_ASSERT(x1 >= x);
  10781. else if (x0 <= x) NK_ASSERT(x1 <= x);
  10782. else if (x0 >= x+1) NK_ASSERT(x1 >= x+1);
  10783. else NK_ASSERT(x1 >= x && x1 <= x+1);
  10784. if (x0 <= x && x1 <= x)
  10785. scanline[x] += e->direction * (y1-y0);
  10786. else if (x0 >= x+1 && x1 >= x+1);
  10787. else {
  10788. NK_ASSERT(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
  10789. /* coverage = 1 - average x position */
  10790. scanline[x] += (float)e->direction * (float)(y1-y0) * (1.0f-((x0-(float)x)+(x1-(float)x))/2.0f);
  10791. }
  10792. }
  10793. NK_INTERN void
  10794. nk_tt__fill_active_edges_new(float *scanline, float *scanline_fill, int len,
  10795. struct nk_tt__active_edge *e, float y_top)
  10796. {
  10797. float y_bottom = y_top+1;
  10798. while (e)
  10799. {
  10800. /* brute force every pixel */
  10801. /* compute intersection points with top & bottom */
  10802. NK_ASSERT(e->ey >= y_top);
  10803. if (e->fdx == 0) {
  10804. float x0 = e->fx;
  10805. if (x0 < len) {
  10806. if (x0 >= 0) {
  10807. nk_tt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
  10808. nk_tt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
  10809. } else {
  10810. nk_tt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
  10811. }
  10812. }
  10813. } else {
  10814. float x0 = e->fx;
  10815. float dx = e->fdx;
  10816. float xb = x0 + dx;
  10817. float x_top, x_bottom;
  10818. float y0,y1;
  10819. float dy = e->fdy;
  10820. NK_ASSERT(e->sy <= y_bottom && e->ey >= y_top);
  10821. /* compute endpoints of line segment clipped to this scanline (if the */
  10822. /* line segment starts on this scanline. x0 is the intersection of the */
  10823. /* line with y_top, but that may be off the line segment. */
  10824. if (e->sy > y_top) {
  10825. x_top = x0 + dx * (e->sy - y_top);
  10826. y0 = e->sy;
  10827. } else {
  10828. x_top = x0;
  10829. y0 = y_top;
  10830. }
  10831. if (e->ey < y_bottom) {
  10832. x_bottom = x0 + dx * (e->ey - y_top);
  10833. y1 = e->ey;
  10834. } else {
  10835. x_bottom = xb;
  10836. y1 = y_bottom;
  10837. }
  10838. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len)
  10839. {
  10840. /* from here on, we don't have to range check x values */
  10841. if ((int) x_top == (int) x_bottom) {
  10842. float height;
  10843. /* simple case, only spans one pixel */
  10844. int x = (int) x_top;
  10845. height = y1 - y0;
  10846. NK_ASSERT(x >= 0 && x < len);
  10847. scanline[x] += e->direction * (1.0f-(((float)x_top - (float)x) + ((float)x_bottom-(float)x))/2.0f) * (float)height;
  10848. scanline_fill[x] += e->direction * (float)height; /* everything right of this pixel is filled */
  10849. } else {
  10850. int x,x1,x2;
  10851. float y_crossing, step, sign, area;
  10852. /* covers 2+ pixels */
  10853. if (x_top > x_bottom)
  10854. {
  10855. /* flip scanline vertically; signed area is the same */
  10856. float t;
  10857. y0 = y_bottom - (y0 - y_top);
  10858. y1 = y_bottom - (y1 - y_top);
  10859. t = y0; y0 = y1; y1 = t;
  10860. t = x_bottom; x_bottom = x_top; x_top = t;
  10861. dx = -dx;
  10862. dy = -dy;
  10863. t = x0; x0 = xb; xb = t;
  10864. }
  10865. x1 = (int) x_top;
  10866. x2 = (int) x_bottom;
  10867. /* compute intersection with y axis at x1+1 */
  10868. y_crossing = ((float)x1+1 - (float)x0) * (float)dy + (float)y_top;
  10869. sign = e->direction;
  10870. /* area of the rectangle covered from y0..y_crossing */
  10871. area = sign * (y_crossing-y0);
  10872. /* area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) */
  10873. scanline[x1] += area * (1.0f-((float)((float)x_top - (float)x1)+(float)(x1+1-x1))/2.0f);
  10874. step = sign * dy;
  10875. for (x = x1+1; x < x2; ++x) {
  10876. scanline[x] += area + step/2;
  10877. area += step;
  10878. }
  10879. y_crossing += (float)dy * (float)(x2 - (x1+1));
  10880. scanline[x2] += area + sign * (1.0f-((float)(x2-x2)+((float)x_bottom-(float)x2))/2.0f) * (y1-y_crossing);
  10881. scanline_fill[x2] += sign * (y1-y0);
  10882. }
  10883. }
  10884. else
  10885. {
  10886. /* if edge goes outside of box we're drawing, we require */
  10887. /* clipping logic. since this does not match the intended use */
  10888. /* of this library, we use a different, very slow brute */
  10889. /* force implementation */
  10890. int x;
  10891. for (x=0; x < len; ++x)
  10892. {
  10893. /* cases: */
  10894. /* */
  10895. /* there can be up to two intersections with the pixel. any intersection */
  10896. /* with left or right edges can be handled by splitting into two (or three) */
  10897. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  10898. /* */
  10899. /* the old way of doing this found the intersections with the left & right edges, */
  10900. /* then used some simple logic to produce up to three segments in sorted order */
  10901. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  10902. /* across the x border, then the corresponding y position might not be distinct */
  10903. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  10904. /* that, we need to explicitly produce segments based on x positions. */
  10905. /* rename variables to clear pairs */
  10906. float ya = y_top;
  10907. float x1 = (float) (x);
  10908. float x2 = (float) (x+1);
  10909. float x3 = xb;
  10910. float y3 = y_bottom;
  10911. float yb,y2;
  10912. yb = ((float)x - x0) / dx + y_top;
  10913. y2 = ((float)x+1 - x0) / dx + y_top;
  10914. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  10915. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  10916. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x2,y2);
  10917. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  10918. } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  10919. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  10920. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x1,yb);
  10921. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  10922. } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  10923. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  10924. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  10925. } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  10926. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x1,yb);
  10927. nk_tt__handle_clipped_edge(scanline,x,e, x1,yb, x3,y3);
  10928. } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  10929. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  10930. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  10931. } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  10932. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x2,y2);
  10933. nk_tt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  10934. } else { /* one segment */
  10935. nk_tt__handle_clipped_edge(scanline,x,e, x0,ya, x3,y3);
  10936. }
  10937. }
  10938. }
  10939. }
  10940. e = e->next;
  10941. }
  10942. }
  10943. /* directly AA rasterize edges w/o supersampling */
  10944. NK_INTERN void
  10945. nk_tt__rasterize_sorted_edges(struct nk_tt__bitmap *result, struct nk_tt__edge *e,
  10946. int n, int vsubsample, int off_x, int off_y, struct nk_allocator *alloc)
  10947. {
  10948. struct nk_tt__hheap hh;
  10949. struct nk_tt__active_edge *active = 0;
  10950. int y,j=0, i;
  10951. float scanline_data[129], *scanline, *scanline2;
  10952. NK_UNUSED(vsubsample);
  10953. nk_zero_struct(hh);
  10954. hh.alloc = *alloc;
  10955. if (result->w > 64)
  10956. scanline = (float *) alloc->alloc(alloc->userdata,0, (nk_size)(result->w*2+1) * sizeof(float));
  10957. else scanline = scanline_data;
  10958. scanline2 = scanline + result->w;
  10959. y = off_y;
  10960. e[n].y0 = (float) (off_y + result->h) + 1;
  10961. while (j < result->h)
  10962. {
  10963. /* find center of pixel for this scanline */
  10964. float scan_y_top = (float)y + 0.0f;
  10965. float scan_y_bottom = (float)y + 1.0f;
  10966. struct nk_tt__active_edge **step = &active;
  10967. NK_MEMSET(scanline , 0, (nk_size)result->w*sizeof(scanline[0]));
  10968. NK_MEMSET(scanline2, 0, (nk_size)(result->w+1)*sizeof(scanline[0]));
  10969. /* update all active edges; */
  10970. /* remove all active edges that terminate before the top of this scanline */
  10971. while (*step) {
  10972. struct nk_tt__active_edge * z = *step;
  10973. if (z->ey <= scan_y_top) {
  10974. *step = z->next; /* delete from list */
  10975. NK_ASSERT(z->direction);
  10976. z->direction = 0;
  10977. nk_tt__hheap_free(&hh, z);
  10978. } else {
  10979. step = &((*step)->next); /* advance through list */
  10980. }
  10981. }
  10982. /* insert all edges that start before the bottom of this scanline */
  10983. while (e->y0 <= scan_y_bottom) {
  10984. if (e->y0 != e->y1) {
  10985. struct nk_tt__active_edge *z = nk_tt__new_active(&hh, e, off_x, scan_y_top);
  10986. if (z != 0) {
  10987. NK_ASSERT(z->ey >= scan_y_top);
  10988. /* insert at front */
  10989. z->next = active;
  10990. active = z;
  10991. }
  10992. }
  10993. ++e;
  10994. }
  10995. /* now process all active edges */
  10996. if (active)
  10997. nk_tt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
  10998. {
  10999. float sum = 0;
  11000. for (i=0; i < result->w; ++i) {
  11001. float k;
  11002. int m;
  11003. sum += scanline2[i];
  11004. k = scanline[i] + sum;
  11005. k = (float) NK_ABS(k) * 255.0f + 0.5f;
  11006. m = (int) k;
  11007. if (m > 255) m = 255;
  11008. result->pixels[j*result->stride + i] = (unsigned char) m;
  11009. }
  11010. }
  11011. /* advance all the edges */
  11012. step = &active;
  11013. while (*step) {
  11014. struct nk_tt__active_edge *z = *step;
  11015. z->fx += z->fdx; /* advance to position for current scanline */
  11016. step = &((*step)->next); /* advance through list */
  11017. }
  11018. ++y;
  11019. ++j;
  11020. }
  11021. nk_tt__hheap_cleanup(&hh);
  11022. if (scanline != scanline_data)
  11023. alloc->free(alloc->userdata, scanline);
  11024. }
  11025. #define NK_TT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  11026. NK_INTERN void
  11027. nk_tt__sort_edges_ins_sort(struct nk_tt__edge *p, int n)
  11028. {
  11029. int i,j;
  11030. for (i=1; i < n; ++i) {
  11031. struct nk_tt__edge t = p[i], *a = &t;
  11032. j = i;
  11033. while (j > 0) {
  11034. struct nk_tt__edge *b = &p[j-1];
  11035. int c = NK_TT__COMPARE(a,b);
  11036. if (!c) break;
  11037. p[j] = p[j-1];
  11038. --j;
  11039. }
  11040. if (i != j)
  11041. p[j] = t;
  11042. }
  11043. }
  11044. NK_INTERN void
  11045. nk_tt__sort_edges_quicksort(struct nk_tt__edge *p, int n)
  11046. {
  11047. /* threshold for transitioning to insertion sort */
  11048. while (n > 12) {
  11049. struct nk_tt__edge t;
  11050. int c01,c12,c,m,i,j;
  11051. /* compute median of three */
  11052. m = n >> 1;
  11053. c01 = NK_TT__COMPARE(&p[0],&p[m]);
  11054. c12 = NK_TT__COMPARE(&p[m],&p[n-1]);
  11055. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  11056. if (c01 != c12) {
  11057. /* otherwise, we'll need to swap something else to middle */
  11058. int z;
  11059. c = NK_TT__COMPARE(&p[0],&p[n-1]);
  11060. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  11061. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  11062. z = (c == c12) ? 0 : n-1;
  11063. t = p[z];
  11064. p[z] = p[m];
  11065. p[m] = t;
  11066. }
  11067. /* now p[m] is the median-of-three */
  11068. /* swap it to the beginning so it won't move around */
  11069. t = p[0];
  11070. p[0] = p[m];
  11071. p[m] = t;
  11072. /* partition loop */
  11073. i=1;
  11074. j=n-1;
  11075. for(;;) {
  11076. /* handling of equality is crucial here */
  11077. /* for sentinels & efficiency with duplicates */
  11078. for (;;++i) {
  11079. if (!NK_TT__COMPARE(&p[i], &p[0])) break;
  11080. }
  11081. for (;;--j) {
  11082. if (!NK_TT__COMPARE(&p[0], &p[j])) break;
  11083. }
  11084. /* make sure we haven't crossed */
  11085. if (i >= j) break;
  11086. t = p[i];
  11087. p[i] = p[j];
  11088. p[j] = t;
  11089. ++i;
  11090. --j;
  11091. }
  11092. /* recurse on smaller side, iterate on larger */
  11093. if (j < (n-i)) {
  11094. nk_tt__sort_edges_quicksort(p,j);
  11095. p = p+i;
  11096. n = n-i;
  11097. } else {
  11098. nk_tt__sort_edges_quicksort(p+i, n-i);
  11099. n = j;
  11100. }
  11101. }
  11102. }
  11103. NK_INTERN void
  11104. nk_tt__sort_edges(struct nk_tt__edge *p, int n)
  11105. {
  11106. nk_tt__sort_edges_quicksort(p, n);
  11107. nk_tt__sort_edges_ins_sort(p, n);
  11108. }
  11109. NK_INTERN void
  11110. nk_tt__rasterize(struct nk_tt__bitmap *result, struct nk_tt__point *pts,
  11111. int *wcount, int windings, float scale_x, float scale_y,
  11112. float shift_x, float shift_y, int off_x, int off_y, int invert,
  11113. struct nk_allocator *alloc)
  11114. {
  11115. float y_scale_inv = invert ? -scale_y : scale_y;
  11116. struct nk_tt__edge *e;
  11117. int n,i,j,k,m;
  11118. int vsubsample = 1;
  11119. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  11120. /* now we have to blow out the windings into explicit edge lists */
  11121. n = 0;
  11122. for (i=0; i < windings; ++i)
  11123. n += wcount[i];
  11124. e = (struct nk_tt__edge*)
  11125. alloc->alloc(alloc->userdata, 0,(sizeof(*e) * (nk_size)(n+1)));
  11126. if (e == 0) return;
  11127. n = 0;
  11128. m=0;
  11129. for (i=0; i < windings; ++i)
  11130. {
  11131. struct nk_tt__point *p = pts + m;
  11132. m += wcount[i];
  11133. j = wcount[i]-1;
  11134. for (k=0; k < wcount[i]; j=k++) {
  11135. int a=k,b=j;
  11136. /* skip the edge if horizontal */
  11137. if (p[j].y == p[k].y)
  11138. continue;
  11139. /* add edge from j to k to the list */
  11140. e[n].invert = 0;
  11141. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  11142. e[n].invert = 1;
  11143. a=j,b=k;
  11144. }
  11145. e[n].x0 = p[a].x * scale_x + shift_x;
  11146. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * (float)vsubsample;
  11147. e[n].x1 = p[b].x * scale_x + shift_x;
  11148. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * (float)vsubsample;
  11149. ++n;
  11150. }
  11151. }
  11152. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  11153. /*STBTT_sort(e, n, sizeof(e[0]), nk_tt__edge_compare); */
  11154. nk_tt__sort_edges(e, n);
  11155. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  11156. nk_tt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, alloc);
  11157. alloc->free(alloc->userdata, e);
  11158. }
  11159. NK_INTERN void
  11160. nk_tt__add_point(struct nk_tt__point *points, int n, float x, float y)
  11161. {
  11162. if (!points) return; /* during first pass, it's unallocated */
  11163. points[n].x = x;
  11164. points[n].y = y;
  11165. }
  11166. NK_INTERN int
  11167. nk_tt__tesselate_curve(struct nk_tt__point *points, int *num_points,
  11168. float x0, float y0, float x1, float y1, float x2, float y2,
  11169. float objspace_flatness_squared, int n)
  11170. {
  11171. /* tesselate until threshold p is happy...
  11172. * @TODO warped to compensate for non-linear stretching */
  11173. /* midpoint */
  11174. float mx = (x0 + 2*x1 + x2)/4;
  11175. float my = (y0 + 2*y1 + y2)/4;
  11176. /* versus directly drawn line */
  11177. float dx = (x0+x2)/2 - mx;
  11178. float dy = (y0+y2)/2 - my;
  11179. if (n > 16) /* 65536 segments on one curve better be enough! */
  11180. return 1;
  11181. /* half-pixel error allowed... need to be smaller if AA */
  11182. if (dx*dx+dy*dy > objspace_flatness_squared) {
  11183. nk_tt__tesselate_curve(points, num_points, x0,y0,
  11184. (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
  11185. nk_tt__tesselate_curve(points, num_points, mx,my,
  11186. (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
  11187. } else {
  11188. nk_tt__add_point(points, *num_points,x2,y2);
  11189. *num_points = *num_points+1;
  11190. }
  11191. return 1;
  11192. }
  11193. /* returns number of contours */
  11194. NK_INTERN struct nk_tt__point*
  11195. nk_tt_FlattenCurves(struct nk_tt_vertex *vertices, int num_verts,
  11196. float objspace_flatness, int **contour_lengths, int *num_contours,
  11197. struct nk_allocator *alloc)
  11198. {
  11199. struct nk_tt__point *points=0;
  11200. int num_points=0;
  11201. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  11202. int i;
  11203. int n=0;
  11204. int start=0;
  11205. int pass;
  11206. /* count how many "moves" there are to get the contour count */
  11207. for (i=0; i < num_verts; ++i)
  11208. if (vertices[i].type == NK_TT_vmove) ++n;
  11209. *num_contours = n;
  11210. if (n == 0) return 0;
  11211. *contour_lengths = (int *)
  11212. alloc->alloc(alloc->userdata,0, (sizeof(**contour_lengths) * (nk_size)n));
  11213. if (*contour_lengths == 0) {
  11214. *num_contours = 0;
  11215. return 0;
  11216. }
  11217. /* make two passes through the points so we don't need to realloc */
  11218. for (pass=0; pass < 2; ++pass)
  11219. {
  11220. float x=0,y=0;
  11221. if (pass == 1) {
  11222. points = (struct nk_tt__point *)
  11223. alloc->alloc(alloc->userdata,0, (nk_size)num_points * sizeof(points[0]));
  11224. if (points == 0) goto error;
  11225. }
  11226. num_points = 0;
  11227. n= -1;
  11228. for (i=0; i < num_verts; ++i)
  11229. {
  11230. switch (vertices[i].type) {
  11231. case NK_TT_vmove:
  11232. /* start the next contour */
  11233. if (n >= 0)
  11234. (*contour_lengths)[n] = num_points - start;
  11235. ++n;
  11236. start = num_points;
  11237. x = vertices[i].x, y = vertices[i].y;
  11238. nk_tt__add_point(points, num_points++, x,y);
  11239. break;
  11240. case NK_TT_vline:
  11241. x = vertices[i].x, y = vertices[i].y;
  11242. nk_tt__add_point(points, num_points++, x, y);
  11243. break;
  11244. case NK_TT_vcurve:
  11245. nk_tt__tesselate_curve(points, &num_points, x,y,
  11246. vertices[i].cx, vertices[i].cy,
  11247. vertices[i].x, vertices[i].y,
  11248. objspace_flatness_squared, 0);
  11249. x = vertices[i].x, y = vertices[i].y;
  11250. break;
  11251. default: break;
  11252. }
  11253. }
  11254. (*contour_lengths)[n] = num_points - start;
  11255. }
  11256. return points;
  11257. error:
  11258. alloc->free(alloc->userdata, points);
  11259. alloc->free(alloc->userdata, *contour_lengths);
  11260. *contour_lengths = 0;
  11261. *num_contours = 0;
  11262. return 0;
  11263. }
  11264. NK_INTERN void
  11265. nk_tt_Rasterize(struct nk_tt__bitmap *result, float flatness_in_pixels,
  11266. struct nk_tt_vertex *vertices, int num_verts,
  11267. float scale_x, float scale_y, float shift_x, float shift_y,
  11268. int x_off, int y_off, int invert, struct nk_allocator *alloc)
  11269. {
  11270. float scale = scale_x > scale_y ? scale_y : scale_x;
  11271. int winding_count, *winding_lengths;
  11272. struct nk_tt__point *windings = nk_tt_FlattenCurves(vertices, num_verts,
  11273. flatness_in_pixels / scale, &winding_lengths, &winding_count, alloc);
  11274. NK_ASSERT(alloc);
  11275. if (windings) {
  11276. nk_tt__rasterize(result, windings, winding_lengths, winding_count,
  11277. scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, alloc);
  11278. alloc->free(alloc->userdata, winding_lengths);
  11279. alloc->free(alloc->userdata, windings);
  11280. }
  11281. }
  11282. NK_INTERN void
  11283. nk_tt_MakeGlyphBitmapSubpixel(const struct nk_tt_fontinfo *info, unsigned char *output,
  11284. int out_w, int out_h, int out_stride, float scale_x, float scale_y,
  11285. float shift_x, float shift_y, int glyph, struct nk_allocator *alloc)
  11286. {
  11287. int ix0,iy0;
  11288. struct nk_tt_vertex *vertices;
  11289. int num_verts = nk_tt_GetGlyphShape(info, alloc, glyph, &vertices);
  11290. struct nk_tt__bitmap gbm;
  11291. nk_tt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x,
  11292. shift_y, &ix0,&iy0,0,0);
  11293. gbm.pixels = output;
  11294. gbm.w = out_w;
  11295. gbm.h = out_h;
  11296. gbm.stride = out_stride;
  11297. if (gbm.w && gbm.h)
  11298. nk_tt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y,
  11299. shift_x, shift_y, ix0,iy0, 1, alloc);
  11300. alloc->free(alloc->userdata, vertices);
  11301. }
  11302. /*-------------------------------------------------------------
  11303. * Bitmap baking
  11304. * --------------------------------------------------------------*/
  11305. NK_INTERN int
  11306. nk_tt_PackBegin(struct nk_tt_pack_context *spc, unsigned char *pixels,
  11307. int pw, int ph, int stride_in_bytes, int padding, struct nk_allocator *alloc)
  11308. {
  11309. int num_nodes = pw - padding;
  11310. struct nk_rp_context *context = (struct nk_rp_context *)
  11311. alloc->alloc(alloc->userdata,0, sizeof(*context));
  11312. struct nk_rp_node *nodes = (struct nk_rp_node*)
  11313. alloc->alloc(alloc->userdata,0, (sizeof(*nodes ) * (nk_size)num_nodes));
  11314. if (context == 0 || nodes == 0) {
  11315. if (context != 0) alloc->free(alloc->userdata, context);
  11316. if (nodes != 0) alloc->free(alloc->userdata, nodes);
  11317. return 0;
  11318. }
  11319. spc->width = pw;
  11320. spc->height = ph;
  11321. spc->pixels = pixels;
  11322. spc->pack_info = context;
  11323. spc->nodes = nodes;
  11324. spc->padding = padding;
  11325. spc->stride_in_bytes = (stride_in_bytes != 0) ? stride_in_bytes : pw;
  11326. spc->h_oversample = 1;
  11327. spc->v_oversample = 1;
  11328. nk_rp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
  11329. if (pixels)
  11330. NK_MEMSET(pixels, 0, (nk_size)(pw*ph)); /* background of 0 around pixels */
  11331. return 1;
  11332. }
  11333. NK_INTERN void
  11334. nk_tt_PackEnd(struct nk_tt_pack_context *spc, struct nk_allocator *alloc)
  11335. {
  11336. alloc->free(alloc->userdata, spc->nodes);
  11337. alloc->free(alloc->userdata, spc->pack_info);
  11338. }
  11339. NK_INTERN void
  11340. nk_tt_PackSetOversampling(struct nk_tt_pack_context *spc,
  11341. unsigned int h_oversample, unsigned int v_oversample)
  11342. {
  11343. NK_ASSERT(h_oversample <= NK_TT_MAX_OVERSAMPLE);
  11344. NK_ASSERT(v_oversample <= NK_TT_MAX_OVERSAMPLE);
  11345. if (h_oversample <= NK_TT_MAX_OVERSAMPLE)
  11346. spc->h_oversample = h_oversample;
  11347. if (v_oversample <= NK_TT_MAX_OVERSAMPLE)
  11348. spc->v_oversample = v_oversample;
  11349. }
  11350. NK_INTERN void
  11351. nk_tt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11352. int kernel_width)
  11353. {
  11354. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11355. int safe_w = w - kernel_width;
  11356. int j;
  11357. for (j=0; j < h; ++j)
  11358. {
  11359. int i;
  11360. unsigned int total;
  11361. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11362. total = 0;
  11363. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11364. switch (kernel_width) {
  11365. case 2:
  11366. for (i=0; i <= safe_w; ++i) {
  11367. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11368. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11369. pixels[i] = (unsigned char) (total / 2);
  11370. }
  11371. break;
  11372. case 3:
  11373. for (i=0; i <= safe_w; ++i) {
  11374. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11375. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11376. pixels[i] = (unsigned char) (total / 3);
  11377. }
  11378. break;
  11379. case 4:
  11380. for (i=0; i <= safe_w; ++i) {
  11381. total += (unsigned int)pixels[i] - buffer[i & NK_TT__OVER_MASK];
  11382. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11383. pixels[i] = (unsigned char) (total / 4);
  11384. }
  11385. break;
  11386. case 5:
  11387. for (i=0; i <= safe_w; ++i) {
  11388. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11389. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11390. pixels[i] = (unsigned char) (total / 5);
  11391. }
  11392. break;
  11393. default:
  11394. for (i=0; i <= safe_w; ++i) {
  11395. total += (unsigned int)(pixels[i] - buffer[i & NK_TT__OVER_MASK]);
  11396. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i];
  11397. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11398. }
  11399. break;
  11400. }
  11401. for (; i < w; ++i) {
  11402. NK_ASSERT(pixels[i] == 0);
  11403. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11404. pixels[i] = (unsigned char) (total / (unsigned int)kernel_width);
  11405. }
  11406. pixels += stride_in_bytes;
  11407. }
  11408. }
  11409. NK_INTERN void
  11410. nk_tt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes,
  11411. int kernel_width)
  11412. {
  11413. unsigned char buffer[NK_TT_MAX_OVERSAMPLE];
  11414. int safe_h = h - kernel_width;
  11415. int j;
  11416. for (j=0; j < w; ++j)
  11417. {
  11418. int i;
  11419. unsigned int total;
  11420. NK_MEMSET(buffer, 0, (nk_size)kernel_width);
  11421. total = 0;
  11422. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  11423. switch (kernel_width) {
  11424. case 2:
  11425. for (i=0; i <= safe_h; ++i) {
  11426. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11427. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11428. pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
  11429. }
  11430. break;
  11431. case 3:
  11432. for (i=0; i <= safe_h; ++i) {
  11433. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11434. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11435. pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
  11436. }
  11437. break;
  11438. case 4:
  11439. for (i=0; i <= safe_h; ++i) {
  11440. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11441. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11442. pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
  11443. }
  11444. break;
  11445. case 5:
  11446. for (i=0; i <= safe_h; ++i) {
  11447. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11448. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11449. pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
  11450. }
  11451. break;
  11452. default:
  11453. for (i=0; i <= safe_h; ++i) {
  11454. total += (unsigned int)(pixels[i*stride_in_bytes] - buffer[i & NK_TT__OVER_MASK]);
  11455. buffer[(i+kernel_width) & NK_TT__OVER_MASK] = pixels[i*stride_in_bytes];
  11456. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11457. }
  11458. break;
  11459. }
  11460. for (; i < h; ++i) {
  11461. NK_ASSERT(pixels[i*stride_in_bytes] == 0);
  11462. total -= (unsigned int)(buffer[i & NK_TT__OVER_MASK]);
  11463. pixels[i*stride_in_bytes] = (unsigned char) (total / (unsigned int)kernel_width);
  11464. }
  11465. pixels += 1;
  11466. }
  11467. }
  11468. NK_INTERN float
  11469. nk_tt__oversample_shift(int oversample)
  11470. {
  11471. if (!oversample)
  11472. return 0.0f;
  11473. /* The prefilter is a box filter of width "oversample", */
  11474. /* which shifts phase by (oversample - 1)/2 pixels in */
  11475. /* oversampled space. We want to shift in the opposite */
  11476. /* direction to counter this. */
  11477. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  11478. }
  11479. /* rects array must be big enough to accommodate all characters in the given ranges */
  11480. NK_INTERN int
  11481. nk_tt_PackFontRangesGatherRects(struct nk_tt_pack_context *spc,
  11482. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11483. int num_ranges, struct nk_rp_rect *rects)
  11484. {
  11485. int i,j,k;
  11486. k = 0;
  11487. for (i=0; i < num_ranges; ++i) {
  11488. float fh = ranges[i].font_size;
  11489. float scale = (fh > 0) ? nk_tt_ScaleForPixelHeight(info, fh):
  11490. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11491. ranges[i].h_oversample = (unsigned char) spc->h_oversample;
  11492. ranges[i].v_oversample = (unsigned char) spc->v_oversample;
  11493. for (j=0; j < ranges[i].num_chars; ++j) {
  11494. int x0,y0,x1,y1;
  11495. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11496. ranges[i].first_unicode_codepoint_in_range + j :
  11497. ranges[i].array_of_unicode_codepoints[j];
  11498. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11499. nk_tt_GetGlyphBitmapBoxSubpixel(info,glyph, scale * (float)spc->h_oversample,
  11500. scale * (float)spc->v_oversample, 0,0, &x0,&y0,&x1,&y1);
  11501. rects[k].w = (nk_rp_coord) (x1-x0 + spc->padding + (int)spc->h_oversample-1);
  11502. rects[k].h = (nk_rp_coord) (y1-y0 + spc->padding + (int)spc->v_oversample-1);
  11503. ++k;
  11504. }
  11505. }
  11506. return k;
  11507. }
  11508. NK_INTERN int
  11509. nk_tt_PackFontRangesRenderIntoRects(struct nk_tt_pack_context *spc,
  11510. struct nk_tt_fontinfo *info, struct nk_tt_pack_range *ranges,
  11511. int num_ranges, struct nk_rp_rect *rects, struct nk_allocator *alloc)
  11512. {
  11513. int i,j,k, return_value = 1;
  11514. /* save current values */
  11515. int old_h_over = (int)spc->h_oversample;
  11516. int old_v_over = (int)spc->v_oversample;
  11517. /* rects array must be big enough to accommodate all characters in the given ranges */
  11518. k = 0;
  11519. for (i=0; i < num_ranges; ++i)
  11520. {
  11521. float fh = ranges[i].font_size;
  11522. float recip_h,recip_v,sub_x,sub_y;
  11523. float scale = fh > 0 ? nk_tt_ScaleForPixelHeight(info, fh):
  11524. nk_tt_ScaleForMappingEmToPixels(info, -fh);
  11525. spc->h_oversample = ranges[i].h_oversample;
  11526. spc->v_oversample = ranges[i].v_oversample;
  11527. recip_h = 1.0f / (float)spc->h_oversample;
  11528. recip_v = 1.0f / (float)spc->v_oversample;
  11529. sub_x = nk_tt__oversample_shift((int)spc->h_oversample);
  11530. sub_y = nk_tt__oversample_shift((int)spc->v_oversample);
  11531. for (j=0; j < ranges[i].num_chars; ++j)
  11532. {
  11533. struct nk_rp_rect *r = &rects[k];
  11534. if (r->was_packed)
  11535. {
  11536. struct nk_tt_packedchar *bc = &ranges[i].chardata_for_range[j];
  11537. int advance, lsb, x0,y0,x1,y1;
  11538. int codepoint = ranges[i].first_unicode_codepoint_in_range ?
  11539. ranges[i].first_unicode_codepoint_in_range + j :
  11540. ranges[i].array_of_unicode_codepoints[j];
  11541. int glyph = nk_tt_FindGlyphIndex(info, codepoint);
  11542. nk_rp_coord pad = (nk_rp_coord) spc->padding;
  11543. /* pad on left and top */
  11544. r->x = (nk_rp_coord)((int)r->x + (int)pad);
  11545. r->y = (nk_rp_coord)((int)r->y + (int)pad);
  11546. r->w = (nk_rp_coord)((int)r->w - (int)pad);
  11547. r->h = (nk_rp_coord)((int)r->h - (int)pad);
  11548. nk_tt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  11549. nk_tt_GetGlyphBitmapBox(info, glyph, scale * (float)spc->h_oversample,
  11550. (scale * (float)spc->v_oversample), &x0,&y0,&x1,&y1);
  11551. nk_tt_MakeGlyphBitmapSubpixel(info, spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11552. (int)(r->w - spc->h_oversample+1), (int)(r->h - spc->v_oversample+1),
  11553. spc->stride_in_bytes, scale * (float)spc->h_oversample,
  11554. scale * (float)spc->v_oversample, 0,0, glyph, alloc);
  11555. if (spc->h_oversample > 1)
  11556. nk_tt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11557. r->w, r->h, spc->stride_in_bytes, (int)spc->h_oversample);
  11558. if (spc->v_oversample > 1)
  11559. nk_tt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  11560. r->w, r->h, spc->stride_in_bytes, (int)spc->v_oversample);
  11561. bc->x0 = (nk_ushort) r->x;
  11562. bc->y0 = (nk_ushort) r->y;
  11563. bc->x1 = (nk_ushort) (r->x + r->w);
  11564. bc->y1 = (nk_ushort) (r->y + r->h);
  11565. bc->xadvance = scale * (float)advance;
  11566. bc->xoff = (float) x0 * recip_h + sub_x;
  11567. bc->yoff = (float) y0 * recip_v + sub_y;
  11568. bc->xoff2 = ((float)x0 + r->w) * recip_h + sub_x;
  11569. bc->yoff2 = ((float)y0 + r->h) * recip_v + sub_y;
  11570. } else {
  11571. return_value = 0; /* if any fail, report failure */
  11572. }
  11573. ++k;
  11574. }
  11575. }
  11576. /* restore original values */
  11577. spc->h_oversample = (unsigned int)old_h_over;
  11578. spc->v_oversample = (unsigned int)old_v_over;
  11579. return return_value;
  11580. }
  11581. NK_INTERN void
  11582. nk_tt_GetPackedQuad(struct nk_tt_packedchar *chardata, int pw, int ph,
  11583. int char_index, float *xpos, float *ypos, struct nk_tt_aligned_quad *q,
  11584. int align_to_integer)
  11585. {
  11586. float ipw = 1.0f / (float)pw, iph = 1.0f / (float)ph;
  11587. struct nk_tt_packedchar *b = (struct nk_tt_packedchar*)(chardata + char_index);
  11588. if (align_to_integer) {
  11589. int tx = nk_ifloorf((*xpos + b->xoff) + 0.5f);
  11590. int ty = nk_ifloorf((*ypos + b->yoff) + 0.5f);
  11591. float x = (float)tx;
  11592. float y = (float)ty;
  11593. q->x0 = x;
  11594. q->y0 = y;
  11595. q->x1 = x + b->xoff2 - b->xoff;
  11596. q->y1 = y + b->yoff2 - b->yoff;
  11597. } else {
  11598. q->x0 = *xpos + b->xoff;
  11599. q->y0 = *ypos + b->yoff;
  11600. q->x1 = *xpos + b->xoff2;
  11601. q->y1 = *ypos + b->yoff2;
  11602. }
  11603. q->s0 = b->x0 * ipw;
  11604. q->t0 = b->y0 * iph;
  11605. q->s1 = b->x1 * ipw;
  11606. q->t1 = b->y1 * iph;
  11607. *xpos += b->xadvance;
  11608. }
  11609. /* -------------------------------------------------------------
  11610. *
  11611. * FONT BAKING
  11612. *
  11613. * --------------------------------------------------------------*/
  11614. struct nk_font_bake_data {
  11615. struct nk_tt_fontinfo info;
  11616. struct nk_rp_rect *rects;
  11617. struct nk_tt_pack_range *ranges;
  11618. nk_rune range_count;
  11619. };
  11620. struct nk_font_baker {
  11621. struct nk_allocator alloc;
  11622. struct nk_tt_pack_context spc;
  11623. struct nk_font_bake_data *build;
  11624. struct nk_tt_packedchar *packed_chars;
  11625. struct nk_rp_rect *rects;
  11626. struct nk_tt_pack_range *ranges;
  11627. };
  11628. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct nk_rp_rect);
  11629. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(struct nk_tt_pack_range);
  11630. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(struct nk_tt_packedchar);
  11631. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  11632. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  11633. NK_INTERN int
  11634. nk_range_count(const nk_rune *range)
  11635. {
  11636. const nk_rune *iter = range;
  11637. NK_ASSERT(range);
  11638. if (!range) return 0;
  11639. while (*(iter++) != 0);
  11640. return (iter == range) ? 0 : (int)((iter - range)/2);
  11641. }
  11642. NK_INTERN int
  11643. nk_range_glyph_count(const nk_rune *range, int count)
  11644. {
  11645. int i = 0;
  11646. int total_glyphs = 0;
  11647. for (i = 0; i < count; ++i) {
  11648. int diff;
  11649. nk_rune f = range[(i*2)+0];
  11650. nk_rune t = range[(i*2)+1];
  11651. NK_ASSERT(t >= f);
  11652. diff = (int)((t - f) + 1);
  11653. total_glyphs += diff;
  11654. }
  11655. return total_glyphs;
  11656. }
  11657. NK_API const nk_rune*
  11658. nk_font_default_glyph_ranges(void)
  11659. {
  11660. NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
  11661. return ranges;
  11662. }
  11663. NK_API const nk_rune*
  11664. nk_font_chinese_glyph_ranges(void)
  11665. {
  11666. NK_STORAGE const nk_rune ranges[] = {
  11667. 0x0020, 0x00FF,
  11668. 0x3000, 0x30FF,
  11669. 0x31F0, 0x31FF,
  11670. 0xFF00, 0xFFEF,
  11671. 0x4e00, 0x9FAF,
  11672. 0
  11673. };
  11674. return ranges;
  11675. }
  11676. NK_API const nk_rune*
  11677. nk_font_cyrillic_glyph_ranges(void)
  11678. {
  11679. NK_STORAGE const nk_rune ranges[] = {
  11680. 0x0020, 0x00FF,
  11681. 0x0400, 0x052F,
  11682. 0x2DE0, 0x2DFF,
  11683. 0xA640, 0xA69F,
  11684. 0
  11685. };
  11686. return ranges;
  11687. }
  11688. NK_API const nk_rune*
  11689. nk_font_korean_glyph_ranges(void)
  11690. {
  11691. NK_STORAGE const nk_rune ranges[] = {
  11692. 0x0020, 0x00FF,
  11693. 0x3131, 0x3163,
  11694. 0xAC00, 0xD79D,
  11695. 0
  11696. };
  11697. return ranges;
  11698. }
  11699. NK_INTERN void
  11700. nk_font_baker_memory(nk_size *temp, int *glyph_count,
  11701. struct nk_font_config *config_list, int count)
  11702. {
  11703. int range_count = 0;
  11704. int total_range_count = 0;
  11705. struct nk_font_config *iter, *i;
  11706. NK_ASSERT(config_list);
  11707. NK_ASSERT(glyph_count);
  11708. if (!config_list) {
  11709. *temp = 0;
  11710. *glyph_count = 0;
  11711. return;
  11712. }
  11713. *glyph_count = 0;
  11714. for (iter = config_list; iter; iter = iter->next) {
  11715. i = iter;
  11716. do {if (!i->range) iter->range = nk_font_default_glyph_ranges();
  11717. range_count = nk_range_count(i->range);
  11718. total_range_count += range_count;
  11719. *glyph_count += nk_range_glyph_count(i->range, range_count);
  11720. } while ((i = i->n) != iter);
  11721. }
  11722. *temp = (nk_size)*glyph_count * sizeof(struct nk_rp_rect);
  11723. *temp += (nk_size)total_range_count * sizeof(struct nk_tt_pack_range);
  11724. *temp += (nk_size)*glyph_count * sizeof(struct nk_tt_packedchar);
  11725. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  11726. *temp += sizeof(struct nk_font_baker);
  11727. *temp += nk_rect_align + nk_range_align + nk_char_align;
  11728. *temp += nk_build_align + nk_baker_align;
  11729. }
  11730. NK_INTERN struct nk_font_baker*
  11731. nk_font_baker(void *memory, int glyph_count, int count, struct nk_allocator *alloc)
  11732. {
  11733. struct nk_font_baker *baker;
  11734. if (!memory) return 0;
  11735. /* setup baker inside a memory block */
  11736. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  11737. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  11738. baker->packed_chars = (struct nk_tt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  11739. baker->rects = (struct nk_rp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  11740. baker->ranges = (struct nk_tt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  11741. baker->alloc = *alloc;
  11742. return baker;
  11743. }
  11744. NK_INTERN int
  11745. nk_font_bake_pack(struct nk_font_baker *baker,
  11746. nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
  11747. const struct nk_font_config *config_list, int count,
  11748. struct nk_allocator *alloc)
  11749. {
  11750. NK_STORAGE const nk_size max_height = 1024 * 32;
  11751. const struct nk_font_config *config_iter, *it;
  11752. int total_glyph_count = 0;
  11753. int total_range_count = 0;
  11754. int range_count = 0;
  11755. int i = 0;
  11756. NK_ASSERT(image_memory);
  11757. NK_ASSERT(width);
  11758. NK_ASSERT(height);
  11759. NK_ASSERT(config_list);
  11760. NK_ASSERT(count);
  11761. NK_ASSERT(alloc);
  11762. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  11763. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11764. it = config_iter;
  11765. do {range_count = nk_range_count(it->range);
  11766. total_range_count += range_count;
  11767. total_glyph_count += nk_range_glyph_count(it->range, range_count);
  11768. } while ((it = it->n) != config_iter);
  11769. }
  11770. /* setup font baker from temporary memory */
  11771. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  11772. it = config_iter;
  11773. do {if (!nk_tt_InitFont(&baker->build[i++].info, (const unsigned char*)it->ttf_blob, 0))
  11774. return nk_false;
  11775. } while ((it = it->n) != config_iter);
  11776. }
  11777. *height = 0;
  11778. *width = (total_glyph_count > 1000) ? 1024 : 512;
  11779. nk_tt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
  11780. {
  11781. int input_i = 0;
  11782. int range_n = 0;
  11783. int rect_n = 0;
  11784. int char_n = 0;
  11785. if (custom) {
  11786. /* pack custom user data first so it will be in the upper left corner*/
  11787. struct nk_rp_rect custom_space;
  11788. nk_zero(&custom_space, sizeof(custom_space));
  11789. custom_space.w = (nk_rp_coord)(custom->w);
  11790. custom_space.h = (nk_rp_coord)(custom->h);
  11791. nk_tt_PackSetOversampling(&baker->spc, 1, 1);
  11792. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, &custom_space, 1);
  11793. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  11794. custom->x = (short)custom_space.x;
  11795. custom->y = (short)custom_space.y;
  11796. custom->w = (short)custom_space.w;
  11797. custom->h = (short)custom_space.h;
  11798. }
  11799. /* first font pass: pack all glyphs */
  11800. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  11801. config_iter = config_iter->next) {
  11802. it = config_iter;
  11803. do {int n = 0;
  11804. int glyph_count;
  11805. const nk_rune *in_range;
  11806. const struct nk_font_config *cfg = it;
  11807. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  11808. /* count glyphs + ranges in current font */
  11809. glyph_count = 0; range_count = 0;
  11810. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  11811. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  11812. range_count++;
  11813. }
  11814. /* setup ranges */
  11815. tmp->ranges = baker->ranges + range_n;
  11816. tmp->range_count = (nk_rune)range_count;
  11817. range_n += range_count;
  11818. for (i = 0; i < range_count; ++i) {
  11819. in_range = &cfg->range[i * 2];
  11820. tmp->ranges[i].font_size = cfg->size;
  11821. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  11822. tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
  11823. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  11824. char_n += tmp->ranges[i].num_chars;
  11825. }
  11826. /* pack */
  11827. tmp->rects = baker->rects + rect_n;
  11828. rect_n += glyph_count;
  11829. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  11830. n = nk_tt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  11831. tmp->ranges, (int)tmp->range_count, tmp->rects);
  11832. nk_rp_pack_rects((struct nk_rp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  11833. /* texture height */
  11834. for (i = 0; i < n; ++i) {
  11835. if (tmp->rects[i].was_packed)
  11836. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  11837. }
  11838. } while ((it = it->n) != config_iter);
  11839. }
  11840. NK_ASSERT(rect_n == total_glyph_count);
  11841. NK_ASSERT(char_n == total_glyph_count);
  11842. NK_ASSERT(range_n == total_range_count);
  11843. }
  11844. *height = (int)nk_round_up_pow2((nk_uint)*height);
  11845. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  11846. return nk_true;
  11847. }
  11848. NK_INTERN void
  11849. nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
  11850. struct nk_font_glyph *glyphs, int glyphs_count,
  11851. const struct nk_font_config *config_list, int font_count)
  11852. {
  11853. int input_i = 0;
  11854. nk_rune glyph_n = 0;
  11855. const struct nk_font_config *config_iter;
  11856. const struct nk_font_config *it;
  11857. NK_ASSERT(image_memory);
  11858. NK_ASSERT(width);
  11859. NK_ASSERT(height);
  11860. NK_ASSERT(config_list);
  11861. NK_ASSERT(baker);
  11862. NK_ASSERT(font_count);
  11863. NK_ASSERT(glyphs_count);
  11864. if (!image_memory || !width || !height || !config_list ||
  11865. !font_count || !glyphs || !glyphs_count)
  11866. return;
  11867. /* second font pass: render glyphs */
  11868. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  11869. baker->spc.pixels = (unsigned char*)image_memory;
  11870. baker->spc.height = (int)height;
  11871. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  11872. config_iter = config_iter->next) {
  11873. it = config_iter;
  11874. do {const struct nk_font_config *cfg = it;
  11875. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  11876. nk_tt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  11877. nk_tt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges,
  11878. (int)tmp->range_count, tmp->rects, &baker->alloc);
  11879. } while ((it = it->n) != config_iter);
  11880. } nk_tt_PackEnd(&baker->spc, &baker->alloc);
  11881. /* third pass: setup font and glyphs */
  11882. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  11883. config_iter = config_iter->next) {
  11884. it = config_iter;
  11885. do {nk_size i = 0;
  11886. int char_idx = 0;
  11887. nk_rune glyph_count = 0;
  11888. const struct nk_font_config *cfg = it;
  11889. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  11890. struct nk_baked_font *dst_font = cfg->font;
  11891. float font_scale = nk_tt_ScaleForPixelHeight(&tmp->info, cfg->size);
  11892. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  11893. nk_tt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  11894. &unscaled_line_gap);
  11895. /* fill baked font */
  11896. if (!cfg->merge_mode) {
  11897. dst_font->ranges = cfg->range;
  11898. dst_font->height = cfg->size;
  11899. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  11900. dst_font->descent = ((float)unscaled_descent * font_scale);
  11901. dst_font->glyph_offset = glyph_n;
  11902. }
  11903. /* fill own baked font glyph array */
  11904. for (i = 0; i < tmp->range_count; ++i) {
  11905. struct nk_tt_pack_range *range = &tmp->ranges[i];
  11906. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  11907. {
  11908. nk_rune codepoint = 0;
  11909. float dummy_x = 0, dummy_y = 0;
  11910. struct nk_tt_aligned_quad q;
  11911. struct nk_font_glyph *glyph;
  11912. /* query glyph bounds from stb_truetype */
  11913. const struct nk_tt_packedchar *pc = &range->chardata_for_range[char_idx];
  11914. if (!pc->x0 && !pc->x1 && !pc->y0 && !pc->y1) continue;
  11915. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  11916. nk_tt_GetPackedQuad(range->chardata_for_range, (int)width,
  11917. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  11918. /* fill own glyph type with data */
  11919. glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
  11920. glyph->codepoint = codepoint;
  11921. glyph->x0 = q.x0; glyph->y0 = q.y0;
  11922. glyph->x1 = q.x1; glyph->y1 = q.y1;
  11923. glyph->y0 += (dst_font->ascent + 0.5f);
  11924. glyph->y1 += (dst_font->ascent + 0.5f);
  11925. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  11926. glyph->h = glyph->y1 - glyph->y0;
  11927. if (cfg->coord_type == NK_COORD_PIXEL) {
  11928. glyph->u0 = q.s0 * (float)width;
  11929. glyph->v0 = q.t0 * (float)height;
  11930. glyph->u1 = q.s1 * (float)width;
  11931. glyph->v1 = q.t1 * (float)height;
  11932. } else {
  11933. glyph->u0 = q.s0;
  11934. glyph->v0 = q.t0;
  11935. glyph->u1 = q.s1;
  11936. glyph->v1 = q.t1;
  11937. }
  11938. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  11939. if (cfg->pixel_snap)
  11940. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  11941. glyph_count++;
  11942. }
  11943. }
  11944. dst_font->glyph_count += glyph_count;
  11945. glyph_n += glyph_count;
  11946. } while ((it = it->n) != config_iter);
  11947. }
  11948. }
  11949. NK_INTERN void
  11950. nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
  11951. struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
  11952. int tex_height, char white, char black)
  11953. {
  11954. nk_byte *pixels;
  11955. int y = 0;
  11956. int x = 0;
  11957. int n = 0;
  11958. NK_ASSERT(img_memory);
  11959. NK_ASSERT(img_width);
  11960. NK_ASSERT(img_height);
  11961. NK_ASSERT(texture_data_mask);
  11962. NK_UNUSED(tex_height);
  11963. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  11964. return;
  11965. pixels = (nk_byte*)img_memory;
  11966. for (y = 0, n = 0; y < tex_height; ++y) {
  11967. for (x = 0; x < tex_width; ++x, ++n) {
  11968. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  11969. const int off1 = off0 + 1 + tex_width;
  11970. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  11971. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  11972. }
  11973. }
  11974. }
  11975. NK_INTERN void
  11976. nk_font_bake_convert(void *out_memory, int img_width, int img_height,
  11977. const void *in_memory)
  11978. {
  11979. int n = 0;
  11980. nk_rune *dst;
  11981. const nk_byte *src;
  11982. NK_ASSERT(out_memory);
  11983. NK_ASSERT(in_memory);
  11984. NK_ASSERT(img_width);
  11985. NK_ASSERT(img_height);
  11986. if (!out_memory || !in_memory || !img_height || !img_width) return;
  11987. dst = (nk_rune*)out_memory;
  11988. src = (const nk_byte*)in_memory;
  11989. for (n = (int)(img_width * img_height); n > 0; n--)
  11990. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  11991. }
  11992. /* -------------------------------------------------------------
  11993. *
  11994. * FONT
  11995. *
  11996. * --------------------------------------------------------------*/
  11997. NK_INTERN float
  11998. nk_font_text_width(nk_handle handle, float height, const char *text, int len)
  11999. {
  12000. nk_rune unicode;
  12001. int text_len = 0;
  12002. float text_width = 0;
  12003. int glyph_len = 0;
  12004. float scale = 0;
  12005. struct nk_font *font = (struct nk_font*)handle.ptr;
  12006. NK_ASSERT(font);
  12007. NK_ASSERT(font->glyphs);
  12008. if (!font || !text || !len)
  12009. return 0;
  12010. scale = height/font->info.height;
  12011. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  12012. if (!glyph_len) return 0;
  12013. while (text_len <= (int)len && glyph_len) {
  12014. const struct nk_font_glyph *g;
  12015. if (unicode == NK_UTF_INVALID) break;
  12016. /* query currently drawn glyph information */
  12017. g = nk_font_find_glyph(font, unicode);
  12018. text_width += g->xadvance * scale;
  12019. /* offset next glyph */
  12020. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  12021. text_len += glyph_len;
  12022. }
  12023. return text_width;
  12024. }
  12025. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12026. NK_INTERN void
  12027. nk_font_query_font_glyph(nk_handle handle, float height,
  12028. struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  12029. {
  12030. float scale;
  12031. const struct nk_font_glyph *g;
  12032. struct nk_font *font;
  12033. NK_ASSERT(glyph);
  12034. NK_UNUSED(next_codepoint);
  12035. font = (struct nk_font*)handle.ptr;
  12036. NK_ASSERT(font);
  12037. NK_ASSERT(font->glyphs);
  12038. if (!font || !glyph)
  12039. return;
  12040. scale = height/font->info.height;
  12041. g = nk_font_find_glyph(font, codepoint);
  12042. glyph->width = (g->x1 - g->x0) * scale;
  12043. glyph->height = (g->y1 - g->y0) * scale;
  12044. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  12045. glyph->xadvance = (g->xadvance * scale);
  12046. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  12047. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  12048. }
  12049. #endif
  12050. NK_API const struct nk_font_glyph*
  12051. nk_font_find_glyph(struct nk_font *font, nk_rune unicode)
  12052. {
  12053. int i = 0;
  12054. int count;
  12055. int total_glyphs = 0;
  12056. const struct nk_font_glyph *glyph = 0;
  12057. const struct nk_font_config *iter = 0;
  12058. NK_ASSERT(font);
  12059. NK_ASSERT(font->glyphs);
  12060. NK_ASSERT(font->info.ranges);
  12061. if (!font || !font->glyphs) return 0;
  12062. glyph = font->fallback;
  12063. iter = font->config;
  12064. do {count = nk_range_count(iter->range);
  12065. for (i = 0; i < count; ++i) {
  12066. nk_rune f = iter->range[(i*2)+0];
  12067. nk_rune t = iter->range[(i*2)+1];
  12068. int diff = (int)((t - f) + 1);
  12069. if (unicode >= f && unicode <= t)
  12070. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  12071. total_glyphs += diff;
  12072. }
  12073. } while ((iter = iter->n) != font->config);
  12074. return glyph;
  12075. }
  12076. NK_INTERN void
  12077. nk_font_init(struct nk_font *font, float pixel_height,
  12078. nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
  12079. const struct nk_baked_font *baked_font, nk_handle atlas)
  12080. {
  12081. struct nk_baked_font baked;
  12082. NK_ASSERT(font);
  12083. NK_ASSERT(glyphs);
  12084. NK_ASSERT(baked_font);
  12085. if (!font || !glyphs || !baked_font)
  12086. return;
  12087. baked = *baked_font;
  12088. font->fallback = 0;
  12089. font->info = baked;
  12090. font->scale = (float)pixel_height / (float)font->info.height;
  12091. font->glyphs = &glyphs[baked_font->glyph_offset];
  12092. font->texture = atlas;
  12093. font->fallback_codepoint = fallback_codepoint;
  12094. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  12095. font->handle.height = font->info.height * font->scale;
  12096. font->handle.width = nk_font_text_width;
  12097. font->handle.userdata.ptr = font;
  12098. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12099. font->handle.query = nk_font_query_font_glyph;
  12100. font->handle.texture = font->texture;
  12101. #endif
  12102. }
  12103. /* ---------------------------------------------------------------------------
  12104. *
  12105. * DEFAULT FONT
  12106. *
  12107. * ProggyClean.ttf
  12108. * Copyright (c) 2004, 2005 Tristan Grimmer
  12109. * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  12110. * Download and more information at http://upperbounds.net
  12111. *-----------------------------------------------------------------------------*/
  12112. #ifdef NK_INCLUDE_DEFAULT_FONT
  12113. #ifdef __clang__
  12114. #pragma clang diagnostic push
  12115. #pragma clang diagnostic ignored "-Woverlength-strings"
  12116. #elif defined(__GNUC__) || defined(__GNUG__)
  12117. #pragma GCC diagnostic push
  12118. #pragma GCC diagnostic ignored "-Woverlength-strings"
  12119. #endif
  12120. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
  12121. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  12122. "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#"
  12123. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#0X@U.a<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  12124. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  12125. "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"
  12126. "*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)"
  12127. "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"
  12128. "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."
  12129. "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"
  12130. "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(,)"
  12131. "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#"
  12132. "'/###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"
  12133. "_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"
  12134. "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/"
  12135. "/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"
  12136. "%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&#"
  12137. "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)("
  12138. "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"
  12139. "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"
  12140. "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^-"
  12141. "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-"
  12142. "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"
  12143. "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%"
  12144. "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]"
  12145. "%(?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"
  12146. "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:"
  12147. "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("
  12148. "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<iaQjO@.kLg;x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  12149. "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'?"
  12150. "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/;"
  12151. ")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"
  12152. "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("
  12153. "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"
  12154. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  12155. "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<-"
  12156. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i"
  12157. "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"
  12158. ".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@@"
  12159. "$&)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*"
  12160. "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"
  12161. "@-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&#"
  12162. "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#"
  12163. "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"
  12164. "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"
  12165. "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&#"
  12166. "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"
  12167. ":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+"
  12168. "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*"
  12169. "$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"
  12170. ":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"
  12171. "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"
  12172. "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"
  12173. "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"
  12174. ":^#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@&]>"
  12175. "_>@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%"
  12176. "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;"
  12177. "^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:"
  12178. "+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%"
  12179. "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-"
  12180. "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*"
  12181. "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"
  12182. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  12183. "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`"
  12184. "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]%/"
  12185. "+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"
  12186. "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"
  12187. "?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"
  12188. "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"
  12189. ">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'>"
  12190. "[%$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"
  12191. "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#"
  12192. "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$"
  12193. "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,)"
  12194. "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"
  12195. "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"
  12196. "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"
  12197. "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(#"
  12198. ";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&>"
  12199. "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#"
  12200. "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"
  12201. "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#"
  12202. "/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-#"
  12203. "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#"
  12204. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  12205. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  12206. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  12207. #endif /* NK_INCLUDE_DEFAULT_FONT */
  12208. #define NK_CURSOR_DATA_W 90
  12209. #define NK_CURSOR_DATA_H 27
  12210. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  12211. {
  12212. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  12213. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  12214. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  12215. "X - X.X - X.....X - X.....X -X...X - X...X"
  12216. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  12217. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  12218. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  12219. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  12220. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  12221. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  12222. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  12223. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  12224. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  12225. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  12226. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  12227. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  12228. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  12229. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  12230. "X.X X..X - -X.......X- X.......X - XX XX - "
  12231. "XX X..X - - X.....X - X.....X - X.X X.X - "
  12232. " X..X - X...X - X...X - X..X X..X - "
  12233. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  12234. "------------ - X - X -X.....................X- "
  12235. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  12236. " - X..X X..X - "
  12237. " - X.X X.X - "
  12238. " - XX XX - "
  12239. };
  12240. #ifdef __clang__
  12241. #pragma clang diagnostic pop
  12242. #elif defined(__GNUC__) || defined(__GNUG__)
  12243. #pragma GCC diagnostic pop
  12244. #endif
  12245. NK_INTERN unsigned int
  12246. nk_decompress_length(unsigned char *input)
  12247. {
  12248. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  12249. }
  12250. NK_GLOBAL unsigned char *nk__barrier;
  12251. NK_GLOBAL unsigned char *nk__barrier2;
  12252. NK_GLOBAL unsigned char *nk__barrier3;
  12253. NK_GLOBAL unsigned char *nk__barrier4;
  12254. NK_GLOBAL unsigned char *nk__dout;
  12255. NK_INTERN void
  12256. nk__match(unsigned char *data, unsigned int length)
  12257. {
  12258. /* INVERSE of memmove... write each byte before copying the next...*/
  12259. NK_ASSERT (nk__dout + length <= nk__barrier);
  12260. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12261. if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
  12262. while (length--) *nk__dout++ = *data++;
  12263. }
  12264. NK_INTERN void
  12265. nk__lit(unsigned char *data, unsigned int length)
  12266. {
  12267. NK_ASSERT (nk__dout + length <= nk__barrier);
  12268. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  12269. if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
  12270. NK_MEMCPY(nk__dout, data, length);
  12271. nk__dout += length;
  12272. }
  12273. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  12274. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  12275. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  12276. NK_INTERN unsigned char*
  12277. nk_decompress_token(unsigned char *i)
  12278. {
  12279. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  12280. if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  12281. else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
  12282. else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  12283. } else { /* more ifs for cases that expand large, since overhead is amortized */
  12284. if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
  12285. else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
  12286. else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  12287. else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  12288. else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
  12289. else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
  12290. }
  12291. return i;
  12292. }
  12293. NK_INTERN unsigned int
  12294. nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  12295. {
  12296. const unsigned long ADLER_MOD = 65521;
  12297. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  12298. unsigned long blocklen, i;
  12299. blocklen = buflen % 5552;
  12300. while (buflen) {
  12301. for (i=0; i + 7 < blocklen; i += 8) {
  12302. s1 += buffer[0]; s2 += s1;
  12303. s1 += buffer[1]; s2 += s1;
  12304. s1 += buffer[2]; s2 += s1;
  12305. s1 += buffer[3]; s2 += s1;
  12306. s1 += buffer[4]; s2 += s1;
  12307. s1 += buffer[5]; s2 += s1;
  12308. s1 += buffer[6]; s2 += s1;
  12309. s1 += buffer[7]; s2 += s1;
  12310. buffer += 8;
  12311. }
  12312. for (; i < blocklen; ++i) {
  12313. s1 += *buffer++; s2 += s1;
  12314. }
  12315. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  12316. buflen -= (unsigned int)blocklen;
  12317. blocklen = 5552;
  12318. }
  12319. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  12320. }
  12321. NK_INTERN unsigned int
  12322. nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  12323. {
  12324. unsigned int olen;
  12325. if (nk__in4(0) != 0x57bC0000) return 0;
  12326. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  12327. olen = nk_decompress_length(i);
  12328. nk__barrier2 = i;
  12329. nk__barrier3 = i+length;
  12330. nk__barrier = output + olen;
  12331. nk__barrier4 = output;
  12332. i += 16;
  12333. nk__dout = output;
  12334. for (;;) {
  12335. unsigned char *old_i = i;
  12336. i = nk_decompress_token(i);
  12337. if (i == old_i) {
  12338. if (*i == 0x05 && i[1] == 0xfa) {
  12339. NK_ASSERT(nk__dout == output + olen);
  12340. if (nk__dout != output + olen) return 0;
  12341. if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
  12342. return 0;
  12343. return olen;
  12344. } else {
  12345. NK_ASSERT(0); /* NOTREACHED */
  12346. return 0;
  12347. }
  12348. }
  12349. NK_ASSERT(nk__dout <= output + olen);
  12350. if (nk__dout > output + olen)
  12351. return 0;
  12352. }
  12353. }
  12354. NK_INTERN unsigned int
  12355. nk_decode_85_byte(char c)
  12356. { return (unsigned int)((c >= '\\') ? c-36 : c-35); }
  12357. NK_INTERN void
  12358. nk_decode_85(unsigned char* dst, const unsigned char* src)
  12359. {
  12360. while (*src)
  12361. {
  12362. unsigned int tmp =
  12363. nk_decode_85_byte((char)src[0]) +
  12364. 85 * (nk_decode_85_byte((char)src[1]) +
  12365. 85 * (nk_decode_85_byte((char)src[2]) +
  12366. 85 * (nk_decode_85_byte((char)src[3]) +
  12367. 85 * nk_decode_85_byte((char)src[4]))));
  12368. /* we can't assume little-endianess. */
  12369. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  12370. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  12371. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  12372. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  12373. src += 5;
  12374. dst += 4;
  12375. }
  12376. }
  12377. /* -------------------------------------------------------------
  12378. *
  12379. * FONT ATLAS
  12380. *
  12381. * --------------------------------------------------------------*/
  12382. NK_API struct nk_font_config
  12383. nk_font_config(float pixel_height)
  12384. {
  12385. struct nk_font_config cfg;
  12386. nk_zero_struct(cfg);
  12387. cfg.ttf_blob = 0;
  12388. cfg.ttf_size = 0;
  12389. cfg.ttf_data_owned_by_atlas = 0;
  12390. cfg.size = pixel_height;
  12391. cfg.oversample_h = 3;
  12392. cfg.oversample_v = 1;
  12393. cfg.pixel_snap = 0;
  12394. cfg.coord_type = NK_COORD_UV;
  12395. cfg.spacing = nk_vec2(0,0);
  12396. cfg.range = nk_font_default_glyph_ranges();
  12397. cfg.merge_mode = 0;
  12398. cfg.fallback_glyph = '?';
  12399. cfg.font = 0;
  12400. cfg.n = 0;
  12401. return cfg;
  12402. }
  12403. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  12404. NK_API void
  12405. nk_font_atlas_init_default(struct nk_font_atlas *atlas)
  12406. {
  12407. NK_ASSERT(atlas);
  12408. if (!atlas) return;
  12409. nk_zero_struct(*atlas);
  12410. atlas->temporary.userdata.ptr = 0;
  12411. atlas->temporary.alloc = nk_malloc;
  12412. atlas->temporary.free = nk_mfree;
  12413. atlas->permanent.userdata.ptr = 0;
  12414. atlas->permanent.alloc = nk_malloc;
  12415. atlas->permanent.free = nk_mfree;
  12416. }
  12417. #endif
  12418. NK_API void
  12419. nk_font_atlas_init(struct nk_font_atlas *atlas, struct nk_allocator *alloc)
  12420. {
  12421. NK_ASSERT(atlas);
  12422. NK_ASSERT(alloc);
  12423. if (!atlas || !alloc) return;
  12424. nk_zero_struct(*atlas);
  12425. atlas->permanent = *alloc;
  12426. atlas->temporary = *alloc;
  12427. }
  12428. NK_API void
  12429. nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
  12430. struct nk_allocator *permanent, struct nk_allocator *temporary)
  12431. {
  12432. NK_ASSERT(atlas);
  12433. NK_ASSERT(permanent);
  12434. NK_ASSERT(temporary);
  12435. if (!atlas || !permanent || !temporary) return;
  12436. nk_zero_struct(*atlas);
  12437. atlas->permanent = *permanent;
  12438. atlas->temporary = *temporary;
  12439. }
  12440. NK_API void
  12441. nk_font_atlas_begin(struct nk_font_atlas *atlas)
  12442. {
  12443. NK_ASSERT(atlas);
  12444. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  12445. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  12446. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  12447. !atlas->temporary.alloc || !atlas->temporary.free) return;
  12448. if (atlas->glyphs) {
  12449. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12450. atlas->glyphs = 0;
  12451. }
  12452. if (atlas->pixel) {
  12453. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  12454. atlas->pixel = 0;
  12455. }
  12456. }
  12457. NK_API struct nk_font*
  12458. nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
  12459. {
  12460. struct nk_font *font = 0;
  12461. struct nk_font_config *cfg;
  12462. NK_ASSERT(atlas);
  12463. NK_ASSERT(atlas->permanent.alloc);
  12464. NK_ASSERT(atlas->permanent.free);
  12465. NK_ASSERT(atlas->temporary.alloc);
  12466. NK_ASSERT(atlas->temporary.free);
  12467. NK_ASSERT(config);
  12468. NK_ASSERT(config->ttf_blob);
  12469. NK_ASSERT(config->ttf_size);
  12470. NK_ASSERT(config->size > 0.0f);
  12471. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
  12472. !atlas->permanent.alloc || !atlas->permanent.free ||
  12473. !atlas->temporary.alloc || !atlas->temporary.free)
  12474. return 0;
  12475. /* allocate font config */
  12476. cfg = (struct nk_font_config*)
  12477. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
  12478. NK_MEMCPY(cfg, config, sizeof(*config));
  12479. cfg->n = cfg;
  12480. cfg->p = cfg;
  12481. if (!config->merge_mode) {
  12482. /* insert font config into list */
  12483. if (!atlas->config) {
  12484. atlas->config = cfg;
  12485. cfg->next = 0;
  12486. } else {
  12487. struct nk_font_config *i = atlas->config;
  12488. while (i->next) i = i->next;
  12489. i->next = cfg;
  12490. cfg->next = 0;
  12491. }
  12492. /* allocate new font */
  12493. font = (struct nk_font*)
  12494. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
  12495. NK_ASSERT(font);
  12496. nk_zero(font, sizeof(*font));
  12497. if (!font) return 0;
  12498. font->config = cfg;
  12499. /* insert font into list */
  12500. if (!atlas->fonts) {
  12501. atlas->fonts = font;
  12502. font->next = 0;
  12503. } else {
  12504. struct nk_font *i = atlas->fonts;
  12505. while (i->next) i = i->next;
  12506. i->next = font;
  12507. font->next = 0;
  12508. }
  12509. cfg->font = &font->info;
  12510. } else {
  12511. /* extend previously added font */
  12512. struct nk_font *f = 0;
  12513. struct nk_font_config *c = 0;
  12514. NK_ASSERT(atlas->font_num);
  12515. f = atlas->fonts;
  12516. c = f->config;
  12517. cfg->font = &f->info;
  12518. cfg->n = c;
  12519. cfg->p = c->p;
  12520. c->p->n = cfg;
  12521. c->p = cfg;
  12522. }
  12523. /* create own copy of .TTF font blob */
  12524. if (!config->ttf_data_owned_by_atlas) {
  12525. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
  12526. NK_ASSERT(cfg->ttf_blob);
  12527. if (!cfg->ttf_blob) {
  12528. atlas->font_num++;
  12529. return 0;
  12530. }
  12531. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  12532. cfg->ttf_data_owned_by_atlas = 1;
  12533. }
  12534. atlas->font_num++;
  12535. return font;
  12536. }
  12537. NK_API struct nk_font*
  12538. nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
  12539. nk_size size, float height, const struct nk_font_config *config)
  12540. {
  12541. struct nk_font_config cfg;
  12542. NK_ASSERT(memory);
  12543. NK_ASSERT(size);
  12544. NK_ASSERT(atlas);
  12545. NK_ASSERT(atlas->temporary.alloc);
  12546. NK_ASSERT(atlas->temporary.free);
  12547. NK_ASSERT(atlas->permanent.alloc);
  12548. NK_ASSERT(atlas->permanent.free);
  12549. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  12550. !atlas->permanent.alloc || !atlas->permanent.free)
  12551. return 0;
  12552. cfg = (config) ? *config: nk_font_config(height);
  12553. cfg.ttf_blob = memory;
  12554. cfg.ttf_size = size;
  12555. cfg.size = height;
  12556. cfg.ttf_data_owned_by_atlas = 0;
  12557. return nk_font_atlas_add(atlas, &cfg);
  12558. }
  12559. #ifdef NK_INCLUDE_STANDARD_IO
  12560. NK_API struct nk_font*
  12561. nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
  12562. float height, const struct nk_font_config *config)
  12563. {
  12564. nk_size size;
  12565. char *memory;
  12566. struct nk_font_config cfg;
  12567. NK_ASSERT(atlas);
  12568. NK_ASSERT(atlas->temporary.alloc);
  12569. NK_ASSERT(atlas->temporary.free);
  12570. NK_ASSERT(atlas->permanent.alloc);
  12571. NK_ASSERT(atlas->permanent.free);
  12572. if (!atlas || !file_path) return 0;
  12573. memory = nk_file_load(file_path, &size, &atlas->permanent);
  12574. if (!memory) return 0;
  12575. cfg = (config) ? *config: nk_font_config(height);
  12576. cfg.ttf_blob = memory;
  12577. cfg.ttf_size = size;
  12578. cfg.size = height;
  12579. cfg.ttf_data_owned_by_atlas = 1;
  12580. return nk_font_atlas_add(atlas, &cfg);
  12581. }
  12582. #endif
  12583. NK_API struct nk_font*
  12584. nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
  12585. void *compressed_data, nk_size compressed_size, float height,
  12586. const struct nk_font_config *config)
  12587. {
  12588. unsigned int decompressed_size;
  12589. void *decompressed_data;
  12590. struct nk_font_config cfg;
  12591. NK_ASSERT(atlas);
  12592. NK_ASSERT(atlas->temporary.alloc);
  12593. NK_ASSERT(atlas->temporary.free);
  12594. NK_ASSERT(atlas->permanent.alloc);
  12595. NK_ASSERT(atlas->permanent.free);
  12596. NK_ASSERT(compressed_data);
  12597. NK_ASSERT(compressed_size);
  12598. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  12599. !atlas->permanent.alloc || !atlas->permanent.free)
  12600. return 0;
  12601. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  12602. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
  12603. NK_ASSERT(decompressed_data);
  12604. if (!decompressed_data) return 0;
  12605. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  12606. (unsigned int)compressed_size);
  12607. cfg = (config) ? *config: nk_font_config(height);
  12608. cfg.ttf_blob = decompressed_data;
  12609. cfg.ttf_size = decompressed_size;
  12610. cfg.size = height;
  12611. cfg.ttf_data_owned_by_atlas = 1;
  12612. return nk_font_atlas_add(atlas, &cfg);
  12613. }
  12614. NK_API struct nk_font*
  12615. nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
  12616. const char *data_base85, float height, const struct nk_font_config *config)
  12617. {
  12618. int compressed_size;
  12619. void *compressed_data;
  12620. struct nk_font *font;
  12621. NK_ASSERT(atlas);
  12622. NK_ASSERT(atlas->temporary.alloc);
  12623. NK_ASSERT(atlas->temporary.free);
  12624. NK_ASSERT(atlas->permanent.alloc);
  12625. NK_ASSERT(atlas->permanent.free);
  12626. NK_ASSERT(data_base85);
  12627. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  12628. !atlas->permanent.alloc || !atlas->permanent.free)
  12629. return 0;
  12630. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  12631. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
  12632. NK_ASSERT(compressed_data);
  12633. if (!compressed_data) return 0;
  12634. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  12635. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  12636. (nk_size)compressed_size, height, config);
  12637. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  12638. return font;
  12639. }
  12640. #ifdef NK_INCLUDE_DEFAULT_FONT
  12641. NK_API struct nk_font*
  12642. nk_font_atlas_add_default(struct nk_font_atlas *atlas,
  12643. float pixel_height, const struct nk_font_config *config)
  12644. {
  12645. NK_ASSERT(atlas);
  12646. NK_ASSERT(atlas->temporary.alloc);
  12647. NK_ASSERT(atlas->temporary.free);
  12648. NK_ASSERT(atlas->permanent.alloc);
  12649. NK_ASSERT(atlas->permanent.free);
  12650. return nk_font_atlas_add_compressed_base85(atlas,
  12651. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  12652. }
  12653. #endif
  12654. NK_API const void*
  12655. nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
  12656. enum nk_font_atlas_format fmt)
  12657. {
  12658. int i = 0;
  12659. void *tmp = 0;
  12660. nk_size tmp_size, img_size;
  12661. struct nk_font *font_iter;
  12662. struct nk_font_baker *baker;
  12663. NK_ASSERT(atlas);
  12664. NK_ASSERT(atlas->temporary.alloc);
  12665. NK_ASSERT(atlas->temporary.free);
  12666. NK_ASSERT(atlas->permanent.alloc);
  12667. NK_ASSERT(atlas->permanent.free);
  12668. NK_ASSERT(width);
  12669. NK_ASSERT(height);
  12670. if (!atlas || !width || !height ||
  12671. !atlas->temporary.alloc || !atlas->temporary.free ||
  12672. !atlas->permanent.alloc || !atlas->permanent.free)
  12673. return 0;
  12674. #ifdef NK_INCLUDE_DEFAULT_FONT
  12675. /* no font added so just use default font */
  12676. if (!atlas->font_num)
  12677. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  12678. #endif
  12679. NK_ASSERT(atlas->font_num);
  12680. if (!atlas->font_num) return 0;
  12681. /* allocate temporary baker memory required for the baking process */
  12682. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  12683. tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
  12684. NK_ASSERT(tmp);
  12685. if (!tmp) goto failed;
  12686. /* allocate glyph memory for all fonts */
  12687. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  12688. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  12689. atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
  12690. NK_ASSERT(atlas->glyphs);
  12691. if (!atlas->glyphs)
  12692. goto failed;
  12693. /* pack all glyphs into a tight fit space */
  12694. atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
  12695. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  12696. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  12697. atlas->config, atlas->font_num, &atlas->temporary))
  12698. goto failed;
  12699. /* allocate memory for the baked image font atlas */
  12700. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
  12701. NK_ASSERT(atlas->pixel);
  12702. if (!atlas->pixel)
  12703. goto failed;
  12704. /* bake glyphs and custom white pixel into image */
  12705. nk_font_bake(baker, atlas->pixel, *width, *height,
  12706. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  12707. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  12708. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  12709. if (fmt == NK_FONT_ATLAS_RGBA32) {
  12710. /* convert alpha8 image into rgba32 image */
  12711. void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
  12712. (nk_size)(*width * *height * 4));
  12713. NK_ASSERT(img_rgba);
  12714. if (!img_rgba) goto failed;
  12715. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  12716. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12717. atlas->pixel = img_rgba;
  12718. }
  12719. atlas->tex_width = *width;
  12720. atlas->tex_height = *height;
  12721. /* initialize each font */
  12722. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12723. struct nk_font *font = font_iter;
  12724. struct nk_font_config *config = font->config;
  12725. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  12726. config->font, nk_handle_ptr(0));
  12727. }
  12728. /* initialize each cursor */
  12729. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  12730. /* Pos Size Offset */
  12731. {{ 0, 3}, {12,19}, { 0, 0}},
  12732. {{13, 0}, { 7,16}, { 4, 8}},
  12733. {{31, 0}, {23,23}, {11,11}},
  12734. {{21, 0}, { 9, 23}, { 5,11}},
  12735. {{55,18}, {23, 9}, {11, 5}},
  12736. {{73, 0}, {17,17}, { 9, 9}},
  12737. {{55, 0}, {17,17}, { 9, 9}}
  12738. };
  12739. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  12740. struct nk_cursor *cursor = &atlas->cursors[i];
  12741. cursor->img.w = (unsigned short)*width;
  12742. cursor->img.h = (unsigned short)*height;
  12743. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  12744. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  12745. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  12746. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  12747. cursor->size = nk_cursor_data[i][1];
  12748. cursor->offset = nk_cursor_data[i][2];
  12749. }}
  12750. /* free temporary memory */
  12751. atlas->temporary.free(atlas->temporary.userdata, tmp);
  12752. return atlas->pixel;
  12753. failed:
  12754. /* error so cleanup all memory */
  12755. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  12756. if (atlas->glyphs) {
  12757. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12758. atlas->glyphs = 0;
  12759. }
  12760. if (atlas->pixel) {
  12761. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12762. atlas->pixel = 0;
  12763. }
  12764. return 0;
  12765. }
  12766. NK_API void
  12767. nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
  12768. struct nk_draw_null_texture *null)
  12769. {
  12770. int i = 0;
  12771. struct nk_font *font_iter;
  12772. NK_ASSERT(atlas);
  12773. if (!atlas) {
  12774. if (!null) return;
  12775. null->texture = texture;
  12776. null->uv = nk_vec2(0.5f,0.5f);
  12777. }
  12778. if (null) {
  12779. null->texture = texture;
  12780. null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
  12781. null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
  12782. }
  12783. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  12784. font_iter->texture = texture;
  12785. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  12786. font_iter->handle.texture = texture;
  12787. #endif
  12788. }
  12789. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  12790. atlas->cursors[i].img.handle = texture;
  12791. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  12792. atlas->pixel = 0;
  12793. atlas->tex_width = 0;
  12794. atlas->tex_height = 0;
  12795. atlas->custom.x = 0;
  12796. atlas->custom.y = 0;
  12797. atlas->custom.w = 0;
  12798. atlas->custom.h = 0;
  12799. }
  12800. NK_API void
  12801. nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
  12802. {
  12803. NK_ASSERT(atlas);
  12804. NK_ASSERT(atlas->temporary.alloc);
  12805. NK_ASSERT(atlas->temporary.free);
  12806. NK_ASSERT(atlas->permanent.alloc);
  12807. NK_ASSERT(atlas->permanent.free);
  12808. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  12809. if (atlas->config) {
  12810. struct nk_font_config *iter;
  12811. for (iter = atlas->config; iter; iter = iter->next) {
  12812. struct nk_font_config *i;
  12813. for (i = iter->n; i != iter; i = i->n) {
  12814. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  12815. i->ttf_blob = 0;
  12816. }
  12817. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  12818. iter->ttf_blob = 0;
  12819. }
  12820. }
  12821. }
  12822. NK_API void
  12823. nk_font_atlas_clear(struct nk_font_atlas *atlas)
  12824. {
  12825. NK_ASSERT(atlas);
  12826. NK_ASSERT(atlas->temporary.alloc);
  12827. NK_ASSERT(atlas->temporary.free);
  12828. NK_ASSERT(atlas->permanent.alloc);
  12829. NK_ASSERT(atlas->permanent.free);
  12830. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  12831. if (atlas->config) {
  12832. struct nk_font_config *iter, *next;
  12833. for (iter = atlas->config; iter; iter = next) {
  12834. struct nk_font_config *i, *n;
  12835. for (i = iter->n; i != iter; i = n) {
  12836. n = i->n;
  12837. if (i->ttf_blob)
  12838. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  12839. atlas->permanent.free(atlas->permanent.userdata, i);
  12840. }
  12841. next = iter->next;
  12842. if (i->ttf_blob)
  12843. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  12844. atlas->permanent.free(atlas->permanent.userdata, iter);
  12845. }
  12846. atlas->config = 0;
  12847. }
  12848. if (atlas->fonts) {
  12849. struct nk_font *iter, *next;
  12850. for (iter = atlas->fonts; iter; iter = next) {
  12851. next = iter->next;
  12852. atlas->permanent.free(atlas->permanent.userdata, iter);
  12853. }
  12854. atlas->fonts = 0;
  12855. }
  12856. if (atlas->glyphs)
  12857. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  12858. nk_zero_struct(*atlas);
  12859. }
  12860. #endif
  12861. /* ==============================================================
  12862. *
  12863. * INPUT
  12864. *
  12865. * ===============================================================*/
  12866. NK_API void
  12867. nk_input_begin(struct nk_context *ctx)
  12868. {
  12869. int i;
  12870. struct nk_input *in;
  12871. NK_ASSERT(ctx);
  12872. if (!ctx) return;
  12873. in = &ctx->input;
  12874. for (i = 0; i < NK_BUTTON_MAX; ++i)
  12875. in->mouse.buttons[i].clicked = 0;
  12876. in->keyboard.text_len = 0;
  12877. in->mouse.scroll_delta = nk_vec2(0,0);
  12878. in->mouse.prev.x = in->mouse.pos.x;
  12879. in->mouse.prev.y = in->mouse.pos.y;
  12880. in->mouse.delta.x = 0;
  12881. in->mouse.delta.y = 0;
  12882. for (i = 0; i < NK_KEY_MAX; i++)
  12883. in->keyboard.keys[i].clicked = 0;
  12884. }
  12885. NK_API void
  12886. nk_input_end(struct nk_context *ctx)
  12887. {
  12888. struct nk_input *in;
  12889. NK_ASSERT(ctx);
  12890. if (!ctx) return;
  12891. in = &ctx->input;
  12892. if (in->mouse.grab)
  12893. in->mouse.grab = 0;
  12894. if (in->mouse.ungrab) {
  12895. in->mouse.grabbed = 0;
  12896. in->mouse.ungrab = 0;
  12897. in->mouse.grab = 0;
  12898. }
  12899. }
  12900. NK_API void
  12901. nk_input_motion(struct nk_context *ctx, int x, int y)
  12902. {
  12903. struct nk_input *in;
  12904. NK_ASSERT(ctx);
  12905. if (!ctx) return;
  12906. in = &ctx->input;
  12907. in->mouse.pos.x = (float)x;
  12908. in->mouse.pos.y = (float)y;
  12909. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  12910. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  12911. }
  12912. NK_API void
  12913. nk_input_key(struct nk_context *ctx, enum nk_keys key, int down)
  12914. {
  12915. struct nk_input *in;
  12916. NK_ASSERT(ctx);
  12917. if (!ctx) return;
  12918. in = &ctx->input;
  12919. if (in->keyboard.keys[key].down != down)
  12920. in->keyboard.keys[key].clicked++;
  12921. in->keyboard.keys[key].down = down;
  12922. }
  12923. NK_API void
  12924. nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int down)
  12925. {
  12926. struct nk_mouse_button *btn;
  12927. struct nk_input *in;
  12928. NK_ASSERT(ctx);
  12929. if (!ctx) return;
  12930. in = &ctx->input;
  12931. if (in->mouse.buttons[id].down == down) return;
  12932. btn = &in->mouse.buttons[id];
  12933. btn->clicked_pos.x = (float)x;
  12934. btn->clicked_pos.y = (float)y;
  12935. btn->down = down;
  12936. btn->clicked++;
  12937. }
  12938. NK_API void
  12939. nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
  12940. {
  12941. NK_ASSERT(ctx);
  12942. if (!ctx) return;
  12943. ctx->input.mouse.scroll_delta.x += val.x;
  12944. ctx->input.mouse.scroll_delta.y += val.y;
  12945. }
  12946. NK_API void
  12947. nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
  12948. {
  12949. int len = 0;
  12950. nk_rune unicode;
  12951. struct nk_input *in;
  12952. NK_ASSERT(ctx);
  12953. if (!ctx) return;
  12954. in = &ctx->input;
  12955. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  12956. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  12957. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  12958. NK_INPUT_MAX - in->keyboard.text_len);
  12959. in->keyboard.text_len += len;
  12960. }
  12961. }
  12962. NK_API void
  12963. nk_input_char(struct nk_context *ctx, char c)
  12964. {
  12965. nk_glyph glyph;
  12966. NK_ASSERT(ctx);
  12967. if (!ctx) return;
  12968. glyph[0] = c;
  12969. nk_input_glyph(ctx, glyph);
  12970. }
  12971. NK_API void
  12972. nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
  12973. {
  12974. nk_glyph rune;
  12975. NK_ASSERT(ctx);
  12976. if (!ctx) return;
  12977. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  12978. nk_input_glyph(ctx, rune);
  12979. }
  12980. NK_API int
  12981. nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
  12982. {
  12983. const struct nk_mouse_button *btn;
  12984. if (!i) return nk_false;
  12985. btn = &i->mouse.buttons[id];
  12986. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  12987. }
  12988. NK_API int
  12989. nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  12990. struct nk_rect b)
  12991. {
  12992. const struct nk_mouse_button *btn;
  12993. if (!i) return nk_false;
  12994. btn = &i->mouse.buttons[id];
  12995. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  12996. return nk_false;
  12997. return nk_true;
  12998. }
  12999. NK_API int
  13000. nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13001. struct nk_rect b, int down)
  13002. {
  13003. const struct nk_mouse_button *btn;
  13004. if (!i) return nk_false;
  13005. btn = &i->mouse.buttons[id];
  13006. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  13007. }
  13008. NK_API int
  13009. nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  13010. struct nk_rect b)
  13011. {
  13012. const struct nk_mouse_button *btn;
  13013. if (!i) return nk_false;
  13014. btn = &i->mouse.buttons[id];
  13015. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  13016. btn->clicked) ? nk_true : nk_false;
  13017. }
  13018. NK_API int
  13019. nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  13020. struct nk_rect b, int down)
  13021. {
  13022. const struct nk_mouse_button *btn;
  13023. if (!i) return nk_false;
  13024. btn = &i->mouse.buttons[id];
  13025. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  13026. btn->clicked) ? nk_true : nk_false;
  13027. }
  13028. NK_API int
  13029. nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
  13030. {
  13031. int i, down = 0;
  13032. for (i = 0; i < NK_BUTTON_MAX; ++i)
  13033. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  13034. return down;
  13035. }
  13036. NK_API int
  13037. nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13038. {
  13039. if (!i) return nk_false;
  13040. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  13041. }
  13042. NK_API int
  13043. nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  13044. {
  13045. if (!i) return nk_false;
  13046. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  13047. }
  13048. NK_API int
  13049. nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
  13050. {
  13051. if (!i) return nk_false;
  13052. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  13053. return nk_input_is_mouse_click_in_rect(i, id, rect);
  13054. }
  13055. NK_API int
  13056. nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
  13057. {
  13058. if (!i) return nk_false;
  13059. return i->mouse.buttons[id].down;
  13060. }
  13061. NK_API int
  13062. nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
  13063. {
  13064. const struct nk_mouse_button *b;
  13065. if (!i) return nk_false;
  13066. b = &i->mouse.buttons[id];
  13067. if (b->down && b->clicked)
  13068. return nk_true;
  13069. return nk_false;
  13070. }
  13071. NK_API int
  13072. nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
  13073. {
  13074. if (!i) return nk_false;
  13075. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  13076. }
  13077. NK_API int
  13078. nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
  13079. {
  13080. const struct nk_key *k;
  13081. if (!i) return nk_false;
  13082. k = &i->keyboard.keys[key];
  13083. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  13084. return nk_true;
  13085. return nk_false;
  13086. }
  13087. NK_API int
  13088. nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
  13089. {
  13090. const struct nk_key *k;
  13091. if (!i) return nk_false;
  13092. k = &i->keyboard.keys[key];
  13093. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  13094. return nk_true;
  13095. return nk_false;
  13096. }
  13097. NK_API int
  13098. nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
  13099. {
  13100. const struct nk_key *k;
  13101. if (!i) return nk_false;
  13102. k = &i->keyboard.keys[key];
  13103. if (k->down) return nk_true;
  13104. return nk_false;
  13105. }
  13106. /*
  13107. * ==============================================================
  13108. *
  13109. * TEXT EDITOR
  13110. *
  13111. * ===============================================================
  13112. */
  13113. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  13114. struct nk_text_find {
  13115. float x,y; /* position of n'th character */
  13116. float height; /* height of line */
  13117. int first_char, length; /* first char of row, and length */
  13118. int prev_first; /*_ first char of previous row */
  13119. };
  13120. struct nk_text_edit_row {
  13121. float x0,x1;
  13122. /* starting x location, end x location (allows for align=right, etc) */
  13123. float baseline_y_delta;
  13124. /* position of baseline relative to previous row's baseline*/
  13125. float ymin,ymax;
  13126. /* height of row above and below baseline */
  13127. int num_chars;
  13128. };
  13129. /* forward declarations */
  13130. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  13131. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  13132. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  13133. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  13134. NK_INTERN float
  13135. nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
  13136. const struct nk_user_font *font)
  13137. {
  13138. int len = 0;
  13139. nk_rune unicode = 0;
  13140. const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  13141. return font->width(font->userdata, font->height, str, len);
  13142. }
  13143. NK_INTERN void
  13144. nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
  13145. int line_start_id, float row_height, const struct nk_user_font *font)
  13146. {
  13147. int l;
  13148. int glyphs = 0;
  13149. nk_rune unicode;
  13150. const char *remaining;
  13151. int len = nk_str_len_char(&edit->string);
  13152. const char *end = nk_str_get_const(&edit->string) + len;
  13153. const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  13154. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  13155. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  13156. r->x0 = 0.0f;
  13157. r->x1 = size.x;
  13158. r->baseline_y_delta = size.y;
  13159. r->ymin = 0.0f;
  13160. r->ymax = size.y;
  13161. r->num_chars = glyphs;
  13162. }
  13163. NK_INTERN int
  13164. nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
  13165. const struct nk_user_font *font, float row_height)
  13166. {
  13167. struct nk_text_edit_row r;
  13168. int n = edit->string.len;
  13169. float base_y = 0, prev_x;
  13170. int i=0, k;
  13171. r.x0 = r.x1 = 0;
  13172. r.ymin = r.ymax = 0;
  13173. r.num_chars = 0;
  13174. /* search rows to find one that straddles 'y' */
  13175. while (i < n) {
  13176. nk_textedit_layout_row(&r, edit, i, row_height, font);
  13177. if (r.num_chars <= 0)
  13178. return n;
  13179. if (i==0 && y < base_y + r.ymin)
  13180. return 0;
  13181. if (y < base_y + r.ymax)
  13182. break;
  13183. i += r.num_chars;
  13184. base_y += r.baseline_y_delta;
  13185. }
  13186. /* below all text, return 'after' last character */
  13187. if (i >= n)
  13188. return n;
  13189. /* check if it's before the beginning of the line */
  13190. if (x < r.x0)
  13191. return i;
  13192. /* check if it's before the end of the line */
  13193. if (x < r.x1) {
  13194. /* search characters in row for one that straddles 'x' */
  13195. k = i;
  13196. prev_x = r.x0;
  13197. for (i=0; i < r.num_chars; ++i) {
  13198. float w = nk_textedit_get_width(edit, k, i, font);
  13199. if (x < prev_x+w) {
  13200. if (x < prev_x+w/2)
  13201. return k+i;
  13202. else return k+i+1;
  13203. }
  13204. prev_x += w;
  13205. }
  13206. /* shouldn't happen, but if it does, fall through to end-of-line case */
  13207. }
  13208. /* if the last character is a newline, return that.
  13209. * otherwise return 'after' the last character */
  13210. if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
  13211. return i+r.num_chars-1;
  13212. else return i+r.num_chars;
  13213. }
  13214. NK_INTERN void
  13215. nk_textedit_click(struct nk_text_edit *state, float x, float y,
  13216. const struct nk_user_font *font, float row_height)
  13217. {
  13218. /* API click: on mouse down, move the cursor to the clicked location,
  13219. * and reset the selection */
  13220. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  13221. state->select_start = state->cursor;
  13222. state->select_end = state->cursor;
  13223. state->has_preferred_x = 0;
  13224. }
  13225. NK_INTERN void
  13226. nk_textedit_drag(struct nk_text_edit *state, float x, float y,
  13227. const struct nk_user_font *font, float row_height)
  13228. {
  13229. /* API drag: on mouse drag, move the cursor and selection endpoint
  13230. * to the clicked location */
  13231. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  13232. if (state->select_start == state->select_end)
  13233. state->select_start = state->cursor;
  13234. state->cursor = state->select_end = p;
  13235. }
  13236. NK_INTERN void
  13237. nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
  13238. int n, int single_line, const struct nk_user_font *font, float row_height)
  13239. {
  13240. /* find the x/y location of a character, and remember info about the previous
  13241. * row in case we get a move-up event (for page up, we'll have to rescan) */
  13242. struct nk_text_edit_row r;
  13243. int prev_start = 0;
  13244. int z = state->string.len;
  13245. int i=0, first;
  13246. nk_zero_struct(r);
  13247. if (n == z) {
  13248. /* if it's at the end, then find the last line -- simpler than trying to
  13249. explicitly handle this case in the regular code */
  13250. nk_textedit_layout_row(&r, state, 0, row_height, font);
  13251. if (single_line) {
  13252. find->first_char = 0;
  13253. find->length = z;
  13254. } else {
  13255. while (i < z) {
  13256. prev_start = i;
  13257. i += r.num_chars;
  13258. nk_textedit_layout_row(&r, state, i, row_height, font);
  13259. }
  13260. find->first_char = i;
  13261. find->length = r.num_chars;
  13262. }
  13263. find->x = r.x1;
  13264. find->y = r.ymin;
  13265. find->height = r.ymax - r.ymin;
  13266. find->prev_first = prev_start;
  13267. return;
  13268. }
  13269. /* search rows to find the one that straddles character n */
  13270. find->y = 0;
  13271. for(;;) {
  13272. nk_textedit_layout_row(&r, state, i, row_height, font);
  13273. if (n < i + r.num_chars) break;
  13274. prev_start = i;
  13275. i += r.num_chars;
  13276. find->y += r.baseline_y_delta;
  13277. }
  13278. find->first_char = first = i;
  13279. find->length = r.num_chars;
  13280. find->height = r.ymax - r.ymin;
  13281. find->prev_first = prev_start;
  13282. /* now scan to find xpos */
  13283. find->x = r.x0;
  13284. for (i=0; first+i < n; ++i)
  13285. find->x += nk_textedit_get_width(state, first, i, font);
  13286. }
  13287. NK_INTERN void
  13288. nk_textedit_clamp(struct nk_text_edit *state)
  13289. {
  13290. /* make the selection/cursor state valid if client altered the string */
  13291. int n = state->string.len;
  13292. if (NK_TEXT_HAS_SELECTION(state)) {
  13293. if (state->select_start > n) state->select_start = n;
  13294. if (state->select_end > n) state->select_end = n;
  13295. /* if clamping forced them to be equal, move the cursor to match */
  13296. if (state->select_start == state->select_end)
  13297. state->cursor = state->select_start;
  13298. }
  13299. if (state->cursor > n) state->cursor = n;
  13300. }
  13301. NK_API void
  13302. nk_textedit_delete(struct nk_text_edit *state, int where, int len)
  13303. {
  13304. /* delete characters while updating undo */
  13305. nk_textedit_makeundo_delete(state, where, len);
  13306. nk_str_delete_runes(&state->string, where, len);
  13307. state->has_preferred_x = 0;
  13308. }
  13309. NK_API void
  13310. nk_textedit_delete_selection(struct nk_text_edit *state)
  13311. {
  13312. /* delete the section */
  13313. nk_textedit_clamp(state);
  13314. if (NK_TEXT_HAS_SELECTION(state)) {
  13315. if (state->select_start < state->select_end) {
  13316. nk_textedit_delete(state, state->select_start,
  13317. state->select_end - state->select_start);
  13318. state->select_end = state->cursor = state->select_start;
  13319. } else {
  13320. nk_textedit_delete(state, state->select_end,
  13321. state->select_start - state->select_end);
  13322. state->select_start = state->cursor = state->select_end;
  13323. }
  13324. state->has_preferred_x = 0;
  13325. }
  13326. }
  13327. NK_INTERN void
  13328. nk_textedit_sortselection(struct nk_text_edit *state)
  13329. {
  13330. /* canonicalize the selection so start <= end */
  13331. if (state->select_end < state->select_start) {
  13332. int temp = state->select_end;
  13333. state->select_end = state->select_start;
  13334. state->select_start = temp;
  13335. }
  13336. }
  13337. NK_INTERN void
  13338. nk_textedit_move_to_first(struct nk_text_edit *state)
  13339. {
  13340. /* move cursor to first character of selection */
  13341. if (NK_TEXT_HAS_SELECTION(state)) {
  13342. nk_textedit_sortselection(state);
  13343. state->cursor = state->select_start;
  13344. state->select_end = state->select_start;
  13345. state->has_preferred_x = 0;
  13346. }
  13347. }
  13348. NK_INTERN void
  13349. nk_textedit_move_to_last(struct nk_text_edit *state)
  13350. {
  13351. /* move cursor to last character of selection */
  13352. if (NK_TEXT_HAS_SELECTION(state)) {
  13353. nk_textedit_sortselection(state);
  13354. nk_textedit_clamp(state);
  13355. state->cursor = state->select_end;
  13356. state->select_start = state->select_end;
  13357. state->has_preferred_x = 0;
  13358. }
  13359. }
  13360. NK_INTERN int
  13361. nk_is_word_boundary( struct nk_text_edit *state, int idx)
  13362. {
  13363. int len;
  13364. nk_rune c;
  13365. if (idx <= 0) return 1;
  13366. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  13367. return (c == ' ' || c == '\t' ||c == 0x3000 || c == ',' || c == ';' ||
  13368. c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
  13369. c == '|');
  13370. }
  13371. NK_INTERN int
  13372. nk_textedit_move_to_word_previous(struct nk_text_edit *state)
  13373. {
  13374. int c = state->cursor - 1;
  13375. while( c >= 0 && !nk_is_word_boundary(state, c))
  13376. --c;
  13377. if( c < 0 )
  13378. c = 0;
  13379. return c;
  13380. }
  13381. NK_INTERN int
  13382. nk_textedit_move_to_word_next(struct nk_text_edit *state)
  13383. {
  13384. const int len = state->string.len;
  13385. int c = state->cursor+1;
  13386. while( c < len && !nk_is_word_boundary(state, c))
  13387. ++c;
  13388. if( c > len )
  13389. c = len;
  13390. return c;
  13391. }
  13392. NK_INTERN void
  13393. nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
  13394. {
  13395. /* update selection and cursor to match each other */
  13396. if (!NK_TEXT_HAS_SELECTION(state))
  13397. state->select_start = state->select_end = state->cursor;
  13398. else state->cursor = state->select_end;
  13399. }
  13400. NK_API int
  13401. nk_textedit_cut(struct nk_text_edit *state)
  13402. {
  13403. /* API cut: delete selection */
  13404. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  13405. return 0;
  13406. if (NK_TEXT_HAS_SELECTION(state)) {
  13407. nk_textedit_delete_selection(state); /* implicitly clamps */
  13408. state->has_preferred_x = 0;
  13409. return 1;
  13410. }
  13411. return 0;
  13412. }
  13413. NK_API int
  13414. nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
  13415. {
  13416. /* API paste: replace existing selection with passed-in text */
  13417. int glyphs;
  13418. const char *text = (const char *) ctext;
  13419. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  13420. /* if there's a selection, the paste should delete it */
  13421. nk_textedit_clamp(state);
  13422. nk_textedit_delete_selection(state);
  13423. /* try to insert the characters */
  13424. glyphs = nk_utf_len(ctext, len);
  13425. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  13426. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  13427. state->cursor += len;
  13428. state->has_preferred_x = 0;
  13429. return 1;
  13430. }
  13431. /* remove the undo since we didn't actually insert the characters */
  13432. if (state->undo.undo_point)
  13433. --state->undo.undo_point;
  13434. return 0;
  13435. }
  13436. NK_API void
  13437. nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
  13438. {
  13439. nk_rune unicode;
  13440. int glyph_len;
  13441. int text_len = 0;
  13442. NK_ASSERT(state);
  13443. NK_ASSERT(text);
  13444. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  13445. glyph_len = nk_utf_decode(text, &unicode, total_len);
  13446. while ((text_len < total_len) && glyph_len)
  13447. {
  13448. /* don't insert a backward delete, just process the event */
  13449. if (unicode == 127) goto next;
  13450. /* can't add newline in single-line mode */
  13451. if (unicode == '\n' && state->single_line) goto next;
  13452. /* filter incoming text */
  13453. if (state->filter && !state->filter(state, unicode)) goto next;
  13454. if (!NK_TEXT_HAS_SELECTION(state) &&
  13455. state->cursor < state->string.len)
  13456. {
  13457. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  13458. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  13459. nk_str_delete_runes(&state->string, state->cursor, 1);
  13460. }
  13461. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  13462. text+text_len, 1))
  13463. {
  13464. ++state->cursor;
  13465. state->has_preferred_x = 0;
  13466. }
  13467. } else {
  13468. nk_textedit_delete_selection(state); /* implicitly clamps */
  13469. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  13470. text+text_len, 1))
  13471. {
  13472. nk_textedit_makeundo_insert(state, state->cursor, 1);
  13473. ++state->cursor;
  13474. state->has_preferred_x = 0;
  13475. }
  13476. }
  13477. next:
  13478. text_len += glyph_len;
  13479. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
  13480. }
  13481. }
  13482. NK_INTERN void
  13483. nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
  13484. const struct nk_user_font *font, float row_height)
  13485. {
  13486. retry:
  13487. switch (key)
  13488. {
  13489. case NK_KEY_NONE:
  13490. case NK_KEY_CTRL:
  13491. case NK_KEY_ENTER:
  13492. case NK_KEY_SHIFT:
  13493. case NK_KEY_TAB:
  13494. case NK_KEY_COPY:
  13495. case NK_KEY_CUT:
  13496. case NK_KEY_PASTE:
  13497. case NK_KEY_MAX:
  13498. default: break;
  13499. case NK_KEY_TEXT_UNDO:
  13500. nk_textedit_undo(state);
  13501. state->has_preferred_x = 0;
  13502. break;
  13503. case NK_KEY_TEXT_REDO:
  13504. nk_textedit_redo(state);
  13505. state->has_preferred_x = 0;
  13506. break;
  13507. case NK_KEY_TEXT_SELECT_ALL:
  13508. nk_textedit_select_all(state);
  13509. state->has_preferred_x = 0;
  13510. break;
  13511. case NK_KEY_TEXT_INSERT_MODE:
  13512. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  13513. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  13514. break;
  13515. case NK_KEY_TEXT_REPLACE_MODE:
  13516. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  13517. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  13518. break;
  13519. case NK_KEY_TEXT_RESET_MODE:
  13520. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  13521. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  13522. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  13523. break;
  13524. case NK_KEY_LEFT:
  13525. if (shift_mod) {
  13526. nk_textedit_clamp(state);
  13527. nk_textedit_prep_selection_at_cursor(state);
  13528. /* move selection left */
  13529. if (state->select_end > 0)
  13530. --state->select_end;
  13531. state->cursor = state->select_end;
  13532. state->has_preferred_x = 0;
  13533. } else {
  13534. /* if currently there's a selection,
  13535. * move cursor to start of selection */
  13536. if (NK_TEXT_HAS_SELECTION(state))
  13537. nk_textedit_move_to_first(state);
  13538. else if (state->cursor > 0)
  13539. --state->cursor;
  13540. state->has_preferred_x = 0;
  13541. } break;
  13542. case NK_KEY_RIGHT:
  13543. if (shift_mod) {
  13544. nk_textedit_prep_selection_at_cursor(state);
  13545. /* move selection right */
  13546. ++state->select_end;
  13547. nk_textedit_clamp(state);
  13548. state->cursor = state->select_end;
  13549. state->has_preferred_x = 0;
  13550. } else {
  13551. /* if currently there's a selection,
  13552. * move cursor to end of selection */
  13553. if (NK_TEXT_HAS_SELECTION(state))
  13554. nk_textedit_move_to_last(state);
  13555. else ++state->cursor;
  13556. nk_textedit_clamp(state);
  13557. state->has_preferred_x = 0;
  13558. } break;
  13559. case NK_KEY_TEXT_WORD_LEFT:
  13560. if (shift_mod) {
  13561. if( !NK_TEXT_HAS_SELECTION( state ) )
  13562. nk_textedit_prep_selection_at_cursor(state);
  13563. state->cursor = nk_textedit_move_to_word_previous(state);
  13564. state->select_end = state->cursor;
  13565. nk_textedit_clamp(state );
  13566. } else {
  13567. if (NK_TEXT_HAS_SELECTION(state))
  13568. nk_textedit_move_to_first(state);
  13569. else {
  13570. state->cursor = nk_textedit_move_to_word_previous(state);
  13571. nk_textedit_clamp(state );
  13572. }
  13573. } break;
  13574. case NK_KEY_TEXT_WORD_RIGHT:
  13575. if (shift_mod) {
  13576. if( !NK_TEXT_HAS_SELECTION( state ) )
  13577. nk_textedit_prep_selection_at_cursor(state);
  13578. state->cursor = nk_textedit_move_to_word_next(state);
  13579. state->select_end = state->cursor;
  13580. nk_textedit_clamp(state);
  13581. } else {
  13582. if (NK_TEXT_HAS_SELECTION(state))
  13583. nk_textedit_move_to_last(state);
  13584. else {
  13585. state->cursor = nk_textedit_move_to_word_next(state);
  13586. nk_textedit_clamp(state );
  13587. }
  13588. } break;
  13589. case NK_KEY_DOWN: {
  13590. struct nk_text_find find;
  13591. struct nk_text_edit_row row;
  13592. int i, sel = shift_mod;
  13593. if (state->single_line) {
  13594. /* on windows, up&down in single-line behave like left&right */
  13595. key = NK_KEY_RIGHT;
  13596. goto retry;
  13597. }
  13598. if (sel)
  13599. nk_textedit_prep_selection_at_cursor(state);
  13600. else if (NK_TEXT_HAS_SELECTION(state))
  13601. nk_textedit_move_to_last(state);
  13602. /* compute current position of cursor point */
  13603. nk_textedit_clamp(state);
  13604. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  13605. font, row_height);
  13606. /* now find character position down a row */
  13607. if (find.length)
  13608. {
  13609. float x;
  13610. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  13611. int start = find.first_char + find.length;
  13612. state->cursor = start;
  13613. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  13614. x = row.x0;
  13615. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  13616. float dx = nk_textedit_get_width(state, start, i, font);
  13617. x += dx;
  13618. if (x > goal_x)
  13619. break;
  13620. ++state->cursor;
  13621. }
  13622. nk_textedit_clamp(state);
  13623. state->has_preferred_x = 1;
  13624. state->preferred_x = goal_x;
  13625. if (sel)
  13626. state->select_end = state->cursor;
  13627. }
  13628. } break;
  13629. case NK_KEY_UP: {
  13630. struct nk_text_find find;
  13631. struct nk_text_edit_row row;
  13632. int i, sel = shift_mod;
  13633. if (state->single_line) {
  13634. /* on windows, up&down become left&right */
  13635. key = NK_KEY_LEFT;
  13636. goto retry;
  13637. }
  13638. if (sel)
  13639. nk_textedit_prep_selection_at_cursor(state);
  13640. else if (NK_TEXT_HAS_SELECTION(state))
  13641. nk_textedit_move_to_first(state);
  13642. /* compute current position of cursor point */
  13643. nk_textedit_clamp(state);
  13644. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  13645. font, row_height);
  13646. /* can only go up if there's a previous row */
  13647. if (find.prev_first != find.first_char) {
  13648. /* now find character position up a row */
  13649. float x;
  13650. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  13651. state->cursor = find.prev_first;
  13652. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  13653. x = row.x0;
  13654. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  13655. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  13656. x += dx;
  13657. if (x > goal_x)
  13658. break;
  13659. ++state->cursor;
  13660. }
  13661. nk_textedit_clamp(state);
  13662. state->has_preferred_x = 1;
  13663. state->preferred_x = goal_x;
  13664. if (sel) state->select_end = state->cursor;
  13665. }
  13666. } break;
  13667. case NK_KEY_DEL:
  13668. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  13669. break;
  13670. if (NK_TEXT_HAS_SELECTION(state))
  13671. nk_textedit_delete_selection(state);
  13672. else {
  13673. int n = state->string.len;
  13674. if (state->cursor < n)
  13675. nk_textedit_delete(state, state->cursor, 1);
  13676. }
  13677. state->has_preferred_x = 0;
  13678. break;
  13679. case NK_KEY_BACKSPACE:
  13680. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  13681. break;
  13682. if (NK_TEXT_HAS_SELECTION(state))
  13683. nk_textedit_delete_selection(state);
  13684. else {
  13685. nk_textedit_clamp(state);
  13686. if (state->cursor > 0) {
  13687. nk_textedit_delete(state, state->cursor-1, 1);
  13688. --state->cursor;
  13689. }
  13690. }
  13691. state->has_preferred_x = 0;
  13692. break;
  13693. case NK_KEY_TEXT_START:
  13694. if (shift_mod) {
  13695. nk_textedit_prep_selection_at_cursor(state);
  13696. state->cursor = state->select_end = 0;
  13697. state->has_preferred_x = 0;
  13698. } else {
  13699. state->cursor = state->select_start = state->select_end = 0;
  13700. state->has_preferred_x = 0;
  13701. }
  13702. break;
  13703. case NK_KEY_TEXT_END:
  13704. if (shift_mod) {
  13705. nk_textedit_prep_selection_at_cursor(state);
  13706. state->cursor = state->select_end = state->string.len;
  13707. state->has_preferred_x = 0;
  13708. } else {
  13709. state->cursor = state->string.len;
  13710. state->select_start = state->select_end = 0;
  13711. state->has_preferred_x = 0;
  13712. }
  13713. break;
  13714. case NK_KEY_TEXT_LINE_START: {
  13715. if (shift_mod) {
  13716. struct nk_text_find find;
  13717. nk_textedit_clamp(state);
  13718. nk_textedit_prep_selection_at_cursor(state);
  13719. if (state->string.len && state->cursor == state->string.len)
  13720. --state->cursor;
  13721. nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
  13722. font, row_height);
  13723. state->cursor = state->select_end = find.first_char;
  13724. state->has_preferred_x = 0;
  13725. } else {
  13726. struct nk_text_find find;
  13727. if (state->string.len && state->cursor == state->string.len)
  13728. --state->cursor;
  13729. nk_textedit_clamp(state);
  13730. nk_textedit_move_to_first(state);
  13731. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  13732. font, row_height);
  13733. state->cursor = find.first_char;
  13734. state->has_preferred_x = 0;
  13735. }
  13736. } break;
  13737. case NK_KEY_TEXT_LINE_END: {
  13738. if (shift_mod) {
  13739. struct nk_text_find find;
  13740. nk_textedit_clamp(state);
  13741. nk_textedit_prep_selection_at_cursor(state);
  13742. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  13743. font, row_height);
  13744. state->has_preferred_x = 0;
  13745. state->cursor = find.first_char + find.length;
  13746. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  13747. --state->cursor;
  13748. state->select_end = state->cursor;
  13749. } else {
  13750. struct nk_text_find find;
  13751. nk_textedit_clamp(state);
  13752. nk_textedit_move_to_first(state);
  13753. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  13754. font, row_height);
  13755. state->has_preferred_x = 0;
  13756. state->cursor = find.first_char + find.length;
  13757. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  13758. --state->cursor;
  13759. }} break;
  13760. }
  13761. }
  13762. NK_INTERN void
  13763. nk_textedit_flush_redo(struct nk_text_undo_state *state)
  13764. {
  13765. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  13766. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  13767. }
  13768. NK_INTERN void
  13769. nk_textedit_discard_undo(struct nk_text_undo_state *state)
  13770. {
  13771. /* discard the oldest entry in the undo list */
  13772. if (state->undo_point > 0) {
  13773. /* if the 0th undo state has characters, clean those up */
  13774. if (state->undo_rec[0].char_storage >= 0) {
  13775. int n = state->undo_rec[0].insert_length, i;
  13776. /* delete n characters from all other records */
  13777. state->undo_char_point = (short)(state->undo_char_point - n);
  13778. NK_MEMCPY(state->undo_char, state->undo_char + n,
  13779. (nk_size)state->undo_char_point*sizeof(nk_rune));
  13780. for (i=0; i < state->undo_point; ++i) {
  13781. if (state->undo_rec[i].char_storage >= 0)
  13782. state->undo_rec[i].char_storage = (short)
  13783. (state->undo_rec[i].char_storage - n);
  13784. }
  13785. }
  13786. --state->undo_point;
  13787. NK_MEMCPY(state->undo_rec, state->undo_rec+1,
  13788. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  13789. }
  13790. }
  13791. NK_INTERN void
  13792. nk_textedit_discard_redo(struct nk_text_undo_state *state)
  13793. {
  13794. /* discard the oldest entry in the redo list--it's bad if this
  13795. ever happens, but because undo & redo have to store the actual
  13796. characters in different cases, the redo character buffer can
  13797. fill up even though the undo buffer didn't */
  13798. nk_size num;
  13799. int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
  13800. if (state->redo_point <= k) {
  13801. /* if the k'th undo state has characters, clean those up */
  13802. if (state->undo_rec[k].char_storage >= 0) {
  13803. int n = state->undo_rec[k].insert_length, i;
  13804. /* delete n characters from all other records */
  13805. state->redo_char_point = (short)(state->redo_char_point + n);
  13806. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  13807. NK_MEMCPY(state->undo_char + state->redo_char_point,
  13808. state->undo_char + state->redo_char_point-n, num * sizeof(char));
  13809. for (i = state->redo_point; i < k; ++i) {
  13810. if (state->undo_rec[i].char_storage >= 0) {
  13811. state->undo_rec[i].char_storage = (short)
  13812. (state->undo_rec[i].char_storage + n);
  13813. }
  13814. }
  13815. }
  13816. ++state->redo_point;
  13817. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  13818. if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
  13819. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  13820. }
  13821. }
  13822. NK_INTERN struct nk_text_undo_record*
  13823. nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
  13824. {
  13825. /* any time we create a new undo record, we discard redo*/
  13826. nk_textedit_flush_redo(state);
  13827. /* if we have no free records, we have to make room,
  13828. * by sliding the existing records down */
  13829. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  13830. nk_textedit_discard_undo(state);
  13831. /* if the characters to store won't possibly fit in the buffer,
  13832. * we can't undo */
  13833. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  13834. state->undo_point = 0;
  13835. state->undo_char_point = 0;
  13836. return 0;
  13837. }
  13838. /* if we don't have enough free characters in the buffer,
  13839. * we have to make room */
  13840. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  13841. nk_textedit_discard_undo(state);
  13842. return &state->undo_rec[state->undo_point++];
  13843. }
  13844. NK_INTERN nk_rune*
  13845. nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
  13846. int insert_len, int delete_len)
  13847. {
  13848. struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
  13849. if (r == 0)
  13850. return 0;
  13851. r->where = pos;
  13852. r->insert_length = (short) insert_len;
  13853. r->delete_length = (short) delete_len;
  13854. if (insert_len == 0) {
  13855. r->char_storage = -1;
  13856. return 0;
  13857. } else {
  13858. r->char_storage = state->undo_char_point;
  13859. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  13860. return &state->undo_char[r->char_storage];
  13861. }
  13862. }
  13863. NK_API void
  13864. nk_textedit_undo(struct nk_text_edit *state)
  13865. {
  13866. struct nk_text_undo_state *s = &state->undo;
  13867. struct nk_text_undo_record u, *r;
  13868. if (s->undo_point == 0)
  13869. return;
  13870. /* we need to do two things: apply the undo record, and create a redo record */
  13871. u = s->undo_rec[s->undo_point-1];
  13872. r = &s->undo_rec[s->redo_point-1];
  13873. r->char_storage = -1;
  13874. r->insert_length = u.delete_length;
  13875. r->delete_length = u.insert_length;
  13876. r->where = u.where;
  13877. if (u.delete_length)
  13878. {
  13879. /* if the undo record says to delete characters, then the redo record will
  13880. need to re-insert the characters that get deleted, so we need to store
  13881. them.
  13882. there are three cases:
  13883. - there's enough room to store the characters
  13884. - characters stored for *redoing* don't leave room for redo
  13885. - characters stored for *undoing* don't leave room for redo
  13886. if the last is true, we have to bail */
  13887. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  13888. /* the undo records take up too much character space; there's no space
  13889. * to store the redo characters */
  13890. r->insert_length = 0;
  13891. } else {
  13892. int i;
  13893. /* there's definitely room to store the characters eventually */
  13894. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  13895. /* there's currently not enough room, so discard a redo record */
  13896. nk_textedit_discard_redo(s);
  13897. /* should never happen: */
  13898. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  13899. return;
  13900. }
  13901. r = &s->undo_rec[s->redo_point-1];
  13902. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  13903. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  13904. /* now save the characters */
  13905. for (i=0; i < u.delete_length; ++i)
  13906. s->undo_char[r->char_storage + i] =
  13907. nk_str_rune_at(&state->string, u.where + i);
  13908. }
  13909. /* now we can carry out the deletion */
  13910. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  13911. }
  13912. /* check type of recorded action: */
  13913. if (u.insert_length) {
  13914. /* easy case: was a deletion, so we need to insert n characters */
  13915. nk_str_insert_text_runes(&state->string, u.where,
  13916. &s->undo_char[u.char_storage], u.insert_length);
  13917. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  13918. }
  13919. state->cursor = (short)(u.where + u.insert_length);
  13920. s->undo_point--;
  13921. s->redo_point--;
  13922. }
  13923. NK_API void
  13924. nk_textedit_redo(struct nk_text_edit *state)
  13925. {
  13926. struct nk_text_undo_state *s = &state->undo;
  13927. struct nk_text_undo_record *u, r;
  13928. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  13929. return;
  13930. /* we need to do two things: apply the redo record, and create an undo record */
  13931. u = &s->undo_rec[s->undo_point];
  13932. r = s->undo_rec[s->redo_point];
  13933. /* we KNOW there must be room for the undo record, because the redo record
  13934. was derived from an undo record */
  13935. u->delete_length = r.insert_length;
  13936. u->insert_length = r.delete_length;
  13937. u->where = r.where;
  13938. u->char_storage = -1;
  13939. if (r.delete_length) {
  13940. /* the redo record requires us to delete characters, so the undo record
  13941. needs to store the characters */
  13942. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  13943. u->insert_length = 0;
  13944. u->delete_length = 0;
  13945. } else {
  13946. int i;
  13947. u->char_storage = s->undo_char_point;
  13948. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  13949. /* now save the characters */
  13950. for (i=0; i < u->insert_length; ++i) {
  13951. s->undo_char[u->char_storage + i] =
  13952. nk_str_rune_at(&state->string, u->where + i);
  13953. }
  13954. }
  13955. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  13956. }
  13957. if (r.insert_length) {
  13958. /* easy case: need to insert n characters */
  13959. nk_str_insert_text_runes(&state->string, r.where,
  13960. &s->undo_char[r.char_storage], r.insert_length);
  13961. }
  13962. state->cursor = r.where + r.insert_length;
  13963. s->undo_point++;
  13964. s->redo_point++;
  13965. }
  13966. NK_INTERN void
  13967. nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
  13968. {
  13969. nk_textedit_createundo(&state->undo, where, 0, length);
  13970. }
  13971. NK_INTERN void
  13972. nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
  13973. {
  13974. int i;
  13975. nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
  13976. if (p) {
  13977. for (i=0; i < length; ++i)
  13978. p[i] = nk_str_rune_at(&state->string, where+i);
  13979. }
  13980. }
  13981. NK_INTERN void
  13982. nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
  13983. int old_length, int new_length)
  13984. {
  13985. int i;
  13986. nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  13987. if (p) {
  13988. for (i=0; i < old_length; ++i)
  13989. p[i] = nk_str_rune_at(&state->string, where+i);
  13990. }
  13991. }
  13992. NK_INTERN void
  13993. nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
  13994. nk_plugin_filter filter)
  13995. {
  13996. /* reset the state to default */
  13997. state->undo.undo_point = 0;
  13998. state->undo.undo_char_point = 0;
  13999. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  14000. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  14001. state->select_end = state->select_start = 0;
  14002. state->cursor = 0;
  14003. state->has_preferred_x = 0;
  14004. state->preferred_x = 0;
  14005. state->cursor_at_end_of_line = 0;
  14006. state->initialized = 1;
  14007. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  14008. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  14009. state->filter = filter;
  14010. state->scrollbar = nk_vec2(0,0);
  14011. }
  14012. NK_API void
  14013. nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
  14014. {
  14015. NK_ASSERT(state);
  14016. NK_ASSERT(memory);
  14017. if (!state || !memory || !size) return;
  14018. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  14019. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  14020. nk_str_init_fixed(&state->string, memory, size);
  14021. }
  14022. NK_API void
  14023. nk_textedit_init(struct nk_text_edit *state, struct nk_allocator *alloc, nk_size size)
  14024. {
  14025. NK_ASSERT(state);
  14026. NK_ASSERT(alloc);
  14027. if (!state || !alloc) return;
  14028. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  14029. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  14030. nk_str_init(&state->string, alloc, size);
  14031. }
  14032. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  14033. NK_API void
  14034. nk_textedit_init_default(struct nk_text_edit *state)
  14035. {
  14036. NK_ASSERT(state);
  14037. if (!state) return;
  14038. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  14039. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  14040. nk_str_init_default(&state->string);
  14041. }
  14042. #endif
  14043. NK_API void
  14044. nk_textedit_select_all(struct nk_text_edit *state)
  14045. {
  14046. NK_ASSERT(state);
  14047. state->select_start = 0;
  14048. state->select_end = state->string.len;
  14049. }
  14050. NK_API void
  14051. nk_textedit_free(struct nk_text_edit *state)
  14052. {
  14053. NK_ASSERT(state);
  14054. if (!state) return;
  14055. nk_str_free(&state->string);
  14056. }
  14057. /* ===============================================================
  14058. *
  14059. * TEXT WIDGET
  14060. *
  14061. * ===============================================================*/
  14062. #define nk_widget_state_reset(s)\
  14063. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  14064. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  14065. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  14066. struct nk_text {
  14067. struct nk_vec2 padding;
  14068. struct nk_color background;
  14069. struct nk_color text;
  14070. };
  14071. NK_INTERN void
  14072. nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
  14073. const char *string, int len, const struct nk_text *t,
  14074. nk_flags a, const struct nk_user_font *f)
  14075. {
  14076. struct nk_rect label;
  14077. float text_width;
  14078. NK_ASSERT(o);
  14079. NK_ASSERT(t);
  14080. if (!o || !t) return;
  14081. b.h = NK_MAX(b.h, 2 * t->padding.y);
  14082. label.x = 0; label.w = 0;
  14083. label.y = b.y + t->padding.y;
  14084. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  14085. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  14086. text_width += (2.0f * t->padding.x);
  14087. /* align in x-axis */
  14088. if (a & NK_TEXT_ALIGN_LEFT) {
  14089. label.x = b.x + t->padding.x;
  14090. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  14091. } else if (a & NK_TEXT_ALIGN_CENTERED) {
  14092. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  14093. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  14094. label.x = NK_MAX(b.x + t->padding.x, label.x);
  14095. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  14096. if (label.w >= label.x) label.w -= label.x;
  14097. } else if (a & NK_TEXT_ALIGN_RIGHT) {
  14098. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  14099. label.w = (float)text_width + 2 * t->padding.x;
  14100. } else return;
  14101. /* align in y-axis */
  14102. if (a & NK_TEXT_ALIGN_MIDDLE) {
  14103. label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
  14104. label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
  14105. } else if (a & NK_TEXT_ALIGN_BOTTOM) {
  14106. label.y = b.y + b.h - f->height;
  14107. label.h = f->height;
  14108. }
  14109. nk_draw_text(o, label, (const char*)string,
  14110. len, f, t->background, t->text);
  14111. }
  14112. NK_INTERN void
  14113. nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
  14114. const char *string, int len, const struct nk_text *t,
  14115. const struct nk_user_font *f)
  14116. {
  14117. float width;
  14118. int glyphs = 0;
  14119. int fitting = 0;
  14120. int done = 0;
  14121. struct nk_rect line;
  14122. struct nk_text text;
  14123. NK_INTERN nk_rune seperator[] = {' '};
  14124. NK_ASSERT(o);
  14125. NK_ASSERT(t);
  14126. if (!o || !t) return;
  14127. text.padding = nk_vec2(0,0);
  14128. text.background = t->background;
  14129. text.text = t->text;
  14130. b.w = NK_MAX(b.w, 2 * t->padding.x);
  14131. b.h = NK_MAX(b.h, 2 * t->padding.y);
  14132. b.h = b.h - 2 * t->padding.y;
  14133. line.x = b.x + t->padding.x;
  14134. line.y = b.y + t->padding.y;
  14135. line.w = b.w - 2 * t->padding.x;
  14136. line.h = 2 * t->padding.y + f->height;
  14137. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  14138. while (done < len) {
  14139. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  14140. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  14141. done += fitting;
  14142. line.y += f->height + 2 * t->padding.y;
  14143. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  14144. }
  14145. }
  14146. /* ===============================================================
  14147. *
  14148. * BUTTON
  14149. *
  14150. * ===============================================================*/
  14151. NK_INTERN void
  14152. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  14153. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  14154. float border_width, const struct nk_user_font *font)
  14155. {
  14156. switch (type) {
  14157. case NK_SYMBOL_X:
  14158. case NK_SYMBOL_UNDERSCORE:
  14159. case NK_SYMBOL_PLUS:
  14160. case NK_SYMBOL_MINUS: {
  14161. /* single character text symbol */
  14162. const char *X = (type == NK_SYMBOL_X) ? "x":
  14163. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  14164. (type == NK_SYMBOL_PLUS) ? "+": "-";
  14165. struct nk_text text;
  14166. text.padding = nk_vec2(0,0);
  14167. text.background = background;
  14168. text.text = foreground;
  14169. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  14170. } break;
  14171. case NK_SYMBOL_CIRCLE_SOLID:
  14172. case NK_SYMBOL_CIRCLE_OUTLINE:
  14173. case NK_SYMBOL_RECT_SOLID:
  14174. case NK_SYMBOL_RECT_OUTLINE: {
  14175. /* simple empty/filled shapes */
  14176. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  14177. nk_fill_rect(out, content, 0, foreground);
  14178. if (type == NK_SYMBOL_RECT_OUTLINE)
  14179. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  14180. } else {
  14181. nk_fill_circle(out, content, foreground);
  14182. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  14183. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  14184. }
  14185. } break;
  14186. case NK_SYMBOL_TRIANGLE_UP:
  14187. case NK_SYMBOL_TRIANGLE_DOWN:
  14188. case NK_SYMBOL_TRIANGLE_LEFT:
  14189. case NK_SYMBOL_TRIANGLE_RIGHT: {
  14190. enum nk_heading heading;
  14191. struct nk_vec2 points[3];
  14192. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  14193. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  14194. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  14195. nk_triangle_from_direction(points, content, 0, 0, heading);
  14196. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  14197. points[2].x, points[2].y, foreground);
  14198. } break;
  14199. default:
  14200. case NK_SYMBOL_NONE:
  14201. case NK_SYMBOL_MAX: break;
  14202. }
  14203. }
  14204. NK_INTERN int
  14205. nk_button_behavior(nk_flags *state, struct nk_rect r,
  14206. const struct nk_input *i, enum nk_button_behavior behavior)
  14207. {
  14208. int ret = 0;
  14209. nk_widget_state_reset(state);
  14210. if (!i) return 0;
  14211. if (nk_input_is_mouse_hovering_rect(i, r)) {
  14212. *state = NK_WIDGET_STATE_HOVERED;
  14213. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  14214. *state = NK_WIDGET_STATE_ACTIVE;
  14215. if (nk_input_has_mouse_click_in_rect(i, NK_BUTTON_LEFT, r)) {
  14216. ret = (behavior != NK_BUTTON_DEFAULT) ?
  14217. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  14218. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  14219. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  14220. #else
  14221. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  14222. #endif
  14223. }
  14224. }
  14225. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  14226. *state |= NK_WIDGET_STATE_ENTERED;
  14227. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  14228. *state |= NK_WIDGET_STATE_LEFT;
  14229. return ret;
  14230. }
  14231. NK_INTERN const struct nk_style_item*
  14232. nk_draw_button(struct nk_command_buffer *out,
  14233. const struct nk_rect *bounds, nk_flags state,
  14234. const struct nk_style_button *style)
  14235. {
  14236. const struct nk_style_item *background;
  14237. if (state & NK_WIDGET_STATE_HOVER)
  14238. background = &style->hover;
  14239. else if (state & NK_WIDGET_STATE_ACTIVED)
  14240. background = &style->active;
  14241. else background = &style->normal;
  14242. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14243. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  14244. } else {
  14245. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  14246. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  14247. }
  14248. return background;
  14249. }
  14250. NK_INTERN int
  14251. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  14252. const struct nk_style_button *style, const struct nk_input *in,
  14253. enum nk_button_behavior behavior, struct nk_rect *content)
  14254. {
  14255. struct nk_rect bounds;
  14256. NK_ASSERT(style);
  14257. NK_ASSERT(state);
  14258. NK_ASSERT(out);
  14259. if (!out || !style)
  14260. return nk_false;
  14261. /* calculate button content space */
  14262. content->x = r.x + style->padding.x + style->border + style->rounding;
  14263. content->y = r.y + style->padding.y + style->border + style->rounding;
  14264. content->w = r.w - (2 * style->padding.x + style->border + style->rounding*2);
  14265. content->h = r.h - (2 * style->padding.y + style->border + style->rounding*2);
  14266. /* execute button behavior */
  14267. bounds.x = r.x - style->touch_padding.x;
  14268. bounds.y = r.y - style->touch_padding.y;
  14269. bounds.w = r.w + 2 * style->touch_padding.x;
  14270. bounds.h = r.h + 2 * style->touch_padding.y;
  14271. return nk_button_behavior(state, bounds, in, behavior);
  14272. }
  14273. NK_INTERN void
  14274. nk_draw_button_text(struct nk_command_buffer *out,
  14275. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  14276. const struct nk_style_button *style, const char *txt, int len,
  14277. nk_flags text_alignment, const struct nk_user_font *font)
  14278. {
  14279. struct nk_text text;
  14280. const struct nk_style_item *background;
  14281. background = nk_draw_button(out, bounds, state, style);
  14282. /* select correct colors/images */
  14283. if (background->type == NK_STYLE_ITEM_COLOR)
  14284. text.background = background->data.color;
  14285. else text.background = style->text_background;
  14286. if (state & NK_WIDGET_STATE_HOVER)
  14287. text.text = style->text_hover;
  14288. else if (state & NK_WIDGET_STATE_ACTIVED)
  14289. text.text = style->text_active;
  14290. else text.text = style->text_normal;
  14291. text.padding = nk_vec2(0,0);
  14292. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  14293. }
  14294. NK_INTERN int
  14295. nk_do_button_text(nk_flags *state,
  14296. struct nk_command_buffer *out, struct nk_rect bounds,
  14297. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  14298. const struct nk_style_button *style, const struct nk_input *in,
  14299. const struct nk_user_font *font)
  14300. {
  14301. struct nk_rect content;
  14302. int ret = nk_false;
  14303. NK_ASSERT(state);
  14304. NK_ASSERT(style);
  14305. NK_ASSERT(out);
  14306. NK_ASSERT(string);
  14307. NK_ASSERT(font);
  14308. if (!out || !style || !font || !string)
  14309. return nk_false;
  14310. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  14311. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14312. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  14313. if (style->draw_end) style->draw_end(out, style->userdata);
  14314. return ret;
  14315. }
  14316. NK_INTERN void
  14317. nk_draw_button_symbol(struct nk_command_buffer *out,
  14318. const struct nk_rect *bounds, const struct nk_rect *content,
  14319. nk_flags state, const struct nk_style_button *style,
  14320. enum nk_symbol_type type, const struct nk_user_font *font)
  14321. {
  14322. struct nk_color sym, bg;
  14323. const struct nk_style_item *background;
  14324. /* select correct colors/images */
  14325. background = nk_draw_button(out, bounds, state, style);
  14326. if (background->type == NK_STYLE_ITEM_COLOR)
  14327. bg = background->data.color;
  14328. else bg = style->text_background;
  14329. if (state & NK_WIDGET_STATE_HOVER)
  14330. sym = style->text_hover;
  14331. else if (state & NK_WIDGET_STATE_ACTIVED)
  14332. sym = style->text_active;
  14333. else sym = style->text_normal;
  14334. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  14335. }
  14336. NK_INTERN int
  14337. nk_do_button_symbol(nk_flags *state,
  14338. struct nk_command_buffer *out, struct nk_rect bounds,
  14339. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  14340. const struct nk_style_button *style, const struct nk_input *in,
  14341. const struct nk_user_font *font)
  14342. {
  14343. int ret;
  14344. struct nk_rect content;
  14345. NK_ASSERT(state);
  14346. NK_ASSERT(style);
  14347. NK_ASSERT(font);
  14348. NK_ASSERT(out);
  14349. if (!out || !style || !font || !state)
  14350. return nk_false;
  14351. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  14352. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14353. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  14354. if (style->draw_end) style->draw_end(out, style->userdata);
  14355. return ret;
  14356. }
  14357. NK_INTERN void
  14358. nk_draw_button_image(struct nk_command_buffer *out,
  14359. const struct nk_rect *bounds, const struct nk_rect *content,
  14360. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  14361. {
  14362. nk_draw_button(out, bounds, state, style);
  14363. nk_draw_image(out, *content, img, nk_white);
  14364. }
  14365. NK_INTERN int
  14366. nk_do_button_image(nk_flags *state,
  14367. struct nk_command_buffer *out, struct nk_rect bounds,
  14368. struct nk_image img, enum nk_button_behavior b,
  14369. const struct nk_style_button *style, const struct nk_input *in)
  14370. {
  14371. int ret;
  14372. struct nk_rect content;
  14373. NK_ASSERT(state);
  14374. NK_ASSERT(style);
  14375. NK_ASSERT(out);
  14376. if (!out || !style || !state)
  14377. return nk_false;
  14378. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  14379. content.x += style->image_padding.x;
  14380. content.y += style->image_padding.y;
  14381. content.w -= 2 * style->image_padding.x;
  14382. content.h -= 2 * style->image_padding.y;
  14383. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14384. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  14385. if (style->draw_end) style->draw_end(out, style->userdata);
  14386. return ret;
  14387. }
  14388. NK_INTERN void
  14389. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  14390. const struct nk_rect *bounds, const struct nk_rect *label,
  14391. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  14392. const char *str, int len, enum nk_symbol_type type,
  14393. const struct nk_user_font *font)
  14394. {
  14395. struct nk_color sym;
  14396. struct nk_text text;
  14397. const struct nk_style_item *background;
  14398. /* select correct background colors/images */
  14399. background = nk_draw_button(out, bounds, state, style);
  14400. if (background->type == NK_STYLE_ITEM_COLOR)
  14401. text.background = background->data.color;
  14402. else text.background = style->text_background;
  14403. /* select correct text colors */
  14404. if (state & NK_WIDGET_STATE_HOVER) {
  14405. sym = style->text_hover;
  14406. text.text = style->text_hover;
  14407. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  14408. sym = style->text_active;
  14409. text.text = style->text_active;
  14410. } else {
  14411. sym = style->text_normal;
  14412. text.text = style->text_normal;
  14413. }
  14414. text.padding = nk_vec2(0,0);
  14415. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  14416. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  14417. }
  14418. NK_INTERN int
  14419. nk_do_button_text_symbol(nk_flags *state,
  14420. struct nk_command_buffer *out, struct nk_rect bounds,
  14421. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  14422. enum nk_button_behavior behavior, const struct nk_style_button *style,
  14423. const struct nk_user_font *font, const struct nk_input *in)
  14424. {
  14425. int ret;
  14426. struct nk_rect tri = {0,0,0,0};
  14427. struct nk_rect content;
  14428. NK_ASSERT(style);
  14429. NK_ASSERT(out);
  14430. NK_ASSERT(font);
  14431. if (!out || !style || !font)
  14432. return nk_false;
  14433. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  14434. tri.y = content.y + (content.h/2) - font->height/2;
  14435. tri.w = font->height; tri.h = font->height;
  14436. if (align & NK_TEXT_ALIGN_LEFT) {
  14437. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  14438. tri.x = NK_MAX(tri.x, 0);
  14439. } else tri.x = content.x + 2 * style->padding.x;
  14440. /* draw button */
  14441. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14442. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  14443. *state, style, str, len, symbol, font);
  14444. if (style->draw_end) style->draw_end(out, style->userdata);
  14445. return ret;
  14446. }
  14447. NK_INTERN void
  14448. nk_draw_button_text_image(struct nk_command_buffer *out,
  14449. const struct nk_rect *bounds, const struct nk_rect *label,
  14450. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  14451. const char *str, int len, const struct nk_user_font *font,
  14452. const struct nk_image *img)
  14453. {
  14454. struct nk_text text;
  14455. const struct nk_style_item *background;
  14456. background = nk_draw_button(out, bounds, state, style);
  14457. /* select correct colors */
  14458. if (background->type == NK_STYLE_ITEM_COLOR)
  14459. text.background = background->data.color;
  14460. else text.background = style->text_background;
  14461. if (state & NK_WIDGET_STATE_HOVER)
  14462. text.text = style->text_hover;
  14463. else if (state & NK_WIDGET_STATE_ACTIVED)
  14464. text.text = style->text_active;
  14465. else text.text = style->text_normal;
  14466. text.padding = nk_vec2(0,0);
  14467. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  14468. nk_draw_image(out, *image, img, nk_white);
  14469. }
  14470. NK_INTERN int
  14471. nk_do_button_text_image(nk_flags *state,
  14472. struct nk_command_buffer *out, struct nk_rect bounds,
  14473. struct nk_image img, const char* str, int len, nk_flags align,
  14474. enum nk_button_behavior behavior, const struct nk_style_button *style,
  14475. const struct nk_user_font *font, const struct nk_input *in)
  14476. {
  14477. int ret;
  14478. struct nk_rect icon;
  14479. struct nk_rect content;
  14480. NK_ASSERT(style);
  14481. NK_ASSERT(state);
  14482. NK_ASSERT(font);
  14483. NK_ASSERT(out);
  14484. if (!out || !font || !style || !str)
  14485. return nk_false;
  14486. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  14487. icon.y = bounds.y + style->padding.y;
  14488. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  14489. if (align & NK_TEXT_ALIGN_LEFT) {
  14490. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  14491. icon.x = NK_MAX(icon.x, 0);
  14492. } else icon.x = bounds.x + 2 * style->padding.x;
  14493. icon.x += style->image_padding.x;
  14494. icon.y += style->image_padding.y;
  14495. icon.w -= 2 * style->image_padding.x;
  14496. icon.h -= 2 * style->image_padding.y;
  14497. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14498. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  14499. if (style->draw_end) style->draw_end(out, style->userdata);
  14500. return ret;
  14501. }
  14502. /* ===============================================================
  14503. *
  14504. * TOGGLE
  14505. *
  14506. * ===============================================================*/
  14507. enum nk_toggle_type {
  14508. NK_TOGGLE_CHECK,
  14509. NK_TOGGLE_OPTION
  14510. };
  14511. NK_INTERN int
  14512. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  14513. nk_flags *state, int active)
  14514. {
  14515. nk_widget_state_reset(state);
  14516. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  14517. *state = NK_WIDGET_STATE_ACTIVE;
  14518. active = !active;
  14519. }
  14520. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  14521. *state |= NK_WIDGET_STATE_ENTERED;
  14522. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  14523. *state |= NK_WIDGET_STATE_LEFT;
  14524. return active;
  14525. }
  14526. NK_INTERN void
  14527. nk_draw_checkbox(struct nk_command_buffer *out,
  14528. nk_flags state, const struct nk_style_toggle *style, int active,
  14529. const struct nk_rect *label, const struct nk_rect *selector,
  14530. const struct nk_rect *cursors, const char *string, int len,
  14531. const struct nk_user_font *font)
  14532. {
  14533. const struct nk_style_item *background;
  14534. const struct nk_style_item *cursor;
  14535. struct nk_text text;
  14536. /* select correct colors/images */
  14537. if (state & NK_WIDGET_STATE_HOVER) {
  14538. background = &style->hover;
  14539. cursor = &style->cursor_hover;
  14540. text.text = style->text_hover;
  14541. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  14542. background = &style->hover;
  14543. cursor = &style->cursor_hover;
  14544. text.text = style->text_active;
  14545. } else {
  14546. background = &style->normal;
  14547. cursor = &style->cursor_normal;
  14548. text.text = style->text_normal;
  14549. }
  14550. /* draw background and cursor */
  14551. if (background->type == NK_STYLE_ITEM_COLOR) {
  14552. nk_fill_rect(out, *selector, 0, style->border_color);
  14553. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
  14554. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  14555. if (active) {
  14556. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  14557. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  14558. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  14559. }
  14560. text.padding.x = 0;
  14561. text.padding.y = 0;
  14562. text.background = style->text_background;
  14563. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  14564. }
  14565. NK_INTERN void
  14566. nk_draw_option(struct nk_command_buffer *out,
  14567. nk_flags state, const struct nk_style_toggle *style, int active,
  14568. const struct nk_rect *label, const struct nk_rect *selector,
  14569. const struct nk_rect *cursors, const char *string, int len,
  14570. const struct nk_user_font *font)
  14571. {
  14572. const struct nk_style_item *background;
  14573. const struct nk_style_item *cursor;
  14574. struct nk_text text;
  14575. /* select correct colors/images */
  14576. if (state & NK_WIDGET_STATE_HOVER) {
  14577. background = &style->hover;
  14578. cursor = &style->cursor_hover;
  14579. text.text = style->text_hover;
  14580. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  14581. background = &style->hover;
  14582. cursor = &style->cursor_hover;
  14583. text.text = style->text_active;
  14584. } else {
  14585. background = &style->normal;
  14586. cursor = &style->cursor_normal;
  14587. text.text = style->text_normal;
  14588. }
  14589. /* draw background and cursor */
  14590. if (background->type == NK_STYLE_ITEM_COLOR) {
  14591. nk_fill_circle(out, *selector, style->border_color);
  14592. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
  14593. } else nk_draw_image(out, *selector, &background->data.image, nk_white);
  14594. if (active) {
  14595. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  14596. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  14597. else nk_fill_circle(out, *cursors, cursor->data.color);
  14598. }
  14599. text.padding.x = 0;
  14600. text.padding.y = 0;
  14601. text.background = style->text_background;
  14602. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  14603. }
  14604. NK_INTERN int
  14605. nk_do_toggle(nk_flags *state,
  14606. struct nk_command_buffer *out, struct nk_rect r,
  14607. int *active, const char *str, int len, enum nk_toggle_type type,
  14608. const struct nk_style_toggle *style, const struct nk_input *in,
  14609. const struct nk_user_font *font)
  14610. {
  14611. int was_active;
  14612. struct nk_rect bounds;
  14613. struct nk_rect select;
  14614. struct nk_rect cursor;
  14615. struct nk_rect label;
  14616. NK_ASSERT(style);
  14617. NK_ASSERT(out);
  14618. NK_ASSERT(font);
  14619. if (!out || !style || !font || !active)
  14620. return 0;
  14621. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  14622. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  14623. /* add additional touch padding for touch screen devices */
  14624. bounds.x = r.x - style->touch_padding.x;
  14625. bounds.y = r.y - style->touch_padding.y;
  14626. bounds.w = r.w + 2 * style->touch_padding.x;
  14627. bounds.h = r.h + 2 * style->touch_padding.y;
  14628. /* calculate the selector space */
  14629. select.w = font->height;
  14630. select.h = select.w;
  14631. select.y = r.y + r.h/2.0f - select.h/2.0f;
  14632. select.x = r.x;
  14633. /* calculate the bounds of the cursor inside the selector */
  14634. cursor.x = select.x + style->padding.x + style->border;
  14635. cursor.y = select.y + style->padding.y + style->border;
  14636. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  14637. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  14638. /* label behind the selector */
  14639. label.x = select.x + select.w + style->spacing;
  14640. label.y = select.y;
  14641. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  14642. label.h = select.w;
  14643. /* update selector */
  14644. was_active = *active;
  14645. *active = nk_toggle_behavior(in, bounds, state, *active);
  14646. /* draw selector */
  14647. if (style->draw_begin)
  14648. style->draw_begin(out, style->userdata);
  14649. if (type == NK_TOGGLE_CHECK) {
  14650. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  14651. } else {
  14652. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  14653. }
  14654. if (style->draw_end)
  14655. style->draw_end(out, style->userdata);
  14656. return (was_active != *active);
  14657. }
  14658. /* ===============================================================
  14659. *
  14660. * SELECTABLE
  14661. *
  14662. * ===============================================================*/
  14663. NK_INTERN void
  14664. nk_draw_selectable(struct nk_command_buffer *out,
  14665. nk_flags state, const struct nk_style_selectable *style, int active,
  14666. const struct nk_rect *bounds, const struct nk_rect *icon, const struct nk_image *img,
  14667. const char *string, int len, nk_flags align, const struct nk_user_font *font)
  14668. {
  14669. const struct nk_style_item *background;
  14670. struct nk_text text;
  14671. text.padding = style->padding;
  14672. /* select correct colors/images */
  14673. if (!active) {
  14674. if (state & NK_WIDGET_STATE_ACTIVED) {
  14675. background = &style->pressed;
  14676. text.text = style->text_pressed;
  14677. } else if (state & NK_WIDGET_STATE_HOVER) {
  14678. background = &style->hover;
  14679. text.text = style->text_hover;
  14680. } else {
  14681. background = &style->normal;
  14682. text.text = style->text_normal;
  14683. }
  14684. } else {
  14685. if (state & NK_WIDGET_STATE_ACTIVED) {
  14686. background = &style->pressed_active;
  14687. text.text = style->text_pressed_active;
  14688. } else if (state & NK_WIDGET_STATE_HOVER) {
  14689. background = &style->hover_active;
  14690. text.text = style->text_hover_active;
  14691. } else {
  14692. background = &style->normal_active;
  14693. text.text = style->text_normal_active;
  14694. }
  14695. }
  14696. /* draw selectable background and text */
  14697. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14698. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  14699. text.background = nk_rgba(0,0,0,0);
  14700. } else {
  14701. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  14702. text.background = background->data.color;
  14703. }
  14704. if (img && icon) nk_draw_image(out, *icon, img, nk_white);
  14705. nk_widget_text(out, *bounds, string, len, &text, align, font);
  14706. }
  14707. NK_INTERN int
  14708. nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
  14709. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  14710. const struct nk_style_selectable *style, const struct nk_input *in,
  14711. const struct nk_user_font *font)
  14712. {
  14713. int old_value;
  14714. struct nk_rect touch;
  14715. NK_ASSERT(state);
  14716. NK_ASSERT(out);
  14717. NK_ASSERT(str);
  14718. NK_ASSERT(len);
  14719. NK_ASSERT(value);
  14720. NK_ASSERT(style);
  14721. NK_ASSERT(font);
  14722. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  14723. old_value = *value;
  14724. /* remove padding */
  14725. touch.x = bounds.x - style->touch_padding.x;
  14726. touch.y = bounds.y - style->touch_padding.y;
  14727. touch.w = bounds.w + style->touch_padding.x * 2;
  14728. touch.h = bounds.h + style->touch_padding.y * 2;
  14729. /* update button */
  14730. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  14731. *value = !(*value);
  14732. /* draw selectable */
  14733. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14734. nk_draw_selectable(out, *state, style, *value, &bounds, 0,0, str, len, align, font);
  14735. if (style->draw_end) style->draw_end(out, style->userdata);
  14736. return old_value != *value;
  14737. }
  14738. NK_INTERN int
  14739. nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
  14740. struct nk_rect bounds, const char *str, int len, nk_flags align, int *value,
  14741. const struct nk_image *img, const struct nk_style_selectable *style,
  14742. const struct nk_input *in, const struct nk_user_font *font)
  14743. {
  14744. int old_value;
  14745. struct nk_rect touch;
  14746. struct nk_rect icon;
  14747. NK_ASSERT(state);
  14748. NK_ASSERT(out);
  14749. NK_ASSERT(str);
  14750. NK_ASSERT(len);
  14751. NK_ASSERT(value);
  14752. NK_ASSERT(style);
  14753. NK_ASSERT(font);
  14754. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  14755. old_value = *value;
  14756. /* toggle behavior */
  14757. touch.x = bounds.x - style->touch_padding.x;
  14758. touch.y = bounds.y - style->touch_padding.y;
  14759. touch.w = bounds.w + style->touch_padding.x * 2;
  14760. touch.h = bounds.h + style->touch_padding.y * 2;
  14761. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  14762. *value = !(*value);
  14763. icon.y = bounds.y + style->padding.y;
  14764. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  14765. if (align & NK_TEXT_ALIGN_LEFT) {
  14766. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  14767. icon.x = NK_MAX(icon.x, 0);
  14768. } else icon.x = bounds.x + 2 * style->padding.x;
  14769. icon.x += style->image_padding.x;
  14770. icon.y += style->image_padding.y;
  14771. icon.w -= 2 * style->image_padding.x;
  14772. icon.h -= 2 * style->image_padding.y;
  14773. /* draw selectable */
  14774. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14775. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, str, len, align, font);
  14776. if (style->draw_end) style->draw_end(out, style->userdata);
  14777. return old_value != *value;
  14778. }
  14779. /* ===============================================================
  14780. *
  14781. * SLIDER
  14782. *
  14783. * ===============================================================*/
  14784. NK_INTERN float
  14785. nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
  14786. struct nk_rect *visual_cursor, struct nk_input *in,
  14787. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  14788. float slider_step, float slider_steps)
  14789. {
  14790. int left_mouse_down;
  14791. int left_mouse_click_in_cursor;
  14792. /* check if visual cursor is being dragged */
  14793. nk_widget_state_reset(state);
  14794. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  14795. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  14796. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  14797. if (left_mouse_down && left_mouse_click_in_cursor) {
  14798. float ratio = 0;
  14799. const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
  14800. const float pxstep = bounds.w / slider_steps;
  14801. /* only update value if the next slider step is reached */
  14802. *state = NK_WIDGET_STATE_ACTIVE;
  14803. if (NK_ABS(d) >= pxstep) {
  14804. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  14805. slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
  14806. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  14807. ratio = (slider_value - slider_min)/slider_step;
  14808. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  14809. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  14810. }
  14811. }
  14812. /* slider widget state */
  14813. if (nk_input_is_mouse_hovering_rect(in, bounds))
  14814. *state = NK_WIDGET_STATE_HOVERED;
  14815. if (*state & NK_WIDGET_STATE_HOVER &&
  14816. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  14817. *state |= NK_WIDGET_STATE_ENTERED;
  14818. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  14819. *state |= NK_WIDGET_STATE_LEFT;
  14820. return slider_value;
  14821. }
  14822. NK_INTERN void
  14823. nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
  14824. const struct nk_style_slider *style, const struct nk_rect *bounds,
  14825. const struct nk_rect *visual_cursor, float min, float value, float max)
  14826. {
  14827. struct nk_rect fill;
  14828. struct nk_rect bar;
  14829. const struct nk_style_item *background;
  14830. /* select correct slider images/colors */
  14831. struct nk_color bar_color;
  14832. const struct nk_style_item *cursor;
  14833. NK_UNUSED(min);
  14834. NK_UNUSED(max);
  14835. NK_UNUSED(value);
  14836. if (state & NK_WIDGET_STATE_ACTIVED) {
  14837. background = &style->active;
  14838. bar_color = style->bar_active;
  14839. cursor = &style->cursor_active;
  14840. } else if (state & NK_WIDGET_STATE_HOVER) {
  14841. background = &style->hover;
  14842. bar_color = style->bar_hover;
  14843. cursor = &style->cursor_hover;
  14844. } else {
  14845. background = &style->normal;
  14846. bar_color = style->bar_normal;
  14847. cursor = &style->cursor_normal;
  14848. }
  14849. /* calculate slider background bar */
  14850. bar.x = bounds->x;
  14851. bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
  14852. bar.w = bounds->w;
  14853. bar.h = bounds->h/6;
  14854. /* filled background bar style */
  14855. fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
  14856. fill.x = bar.x;
  14857. fill.y = bar.y;
  14858. fill.h = bar.h;
  14859. /* draw background */
  14860. if (background->type == NK_STYLE_ITEM_IMAGE) {
  14861. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  14862. } else {
  14863. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  14864. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  14865. }
  14866. /* draw slider bar */
  14867. nk_fill_rect(out, bar, style->rounding, bar_color);
  14868. nk_fill_rect(out, fill, style->rounding, style->bar_filled);
  14869. /* draw cursor */
  14870. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  14871. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
  14872. else nk_fill_circle(out, *visual_cursor, cursor->data.color);
  14873. }
  14874. NK_INTERN float
  14875. nk_do_slider(nk_flags *state,
  14876. struct nk_command_buffer *out, struct nk_rect bounds,
  14877. float min, float val, float max, float step,
  14878. const struct nk_style_slider *style, struct nk_input *in,
  14879. const struct nk_user_font *font)
  14880. {
  14881. float slider_range;
  14882. float slider_min;
  14883. float slider_max;
  14884. float slider_value;
  14885. float slider_steps;
  14886. float cursor_offset;
  14887. struct nk_rect visual_cursor;
  14888. struct nk_rect logical_cursor;
  14889. NK_ASSERT(style);
  14890. NK_ASSERT(out);
  14891. if (!out || !style)
  14892. return 0;
  14893. /* remove padding from slider bounds */
  14894. bounds.x = bounds.x + style->padding.x;
  14895. bounds.y = bounds.y + style->padding.y;
  14896. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  14897. bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
  14898. bounds.w -= 2 * style->padding.x;
  14899. bounds.h -= 2 * style->padding.y;
  14900. /* optional buttons */
  14901. if (style->show_buttons) {
  14902. nk_flags ws;
  14903. struct nk_rect button;
  14904. button.y = bounds.y;
  14905. button.w = bounds.h;
  14906. button.h = bounds.h;
  14907. /* decrement button */
  14908. button.x = bounds.x;
  14909. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  14910. &style->dec_button, in, font))
  14911. val -= step;
  14912. /* increment button */
  14913. button.x = (bounds.x + bounds.w) - button.w;
  14914. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  14915. &style->inc_button, in, font))
  14916. val += step;
  14917. bounds.x = bounds.x + button.w + style->spacing.x;
  14918. bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
  14919. }
  14920. /* remove one cursor size to support visual cursor */
  14921. bounds.x += style->cursor_size.x*0.5f;
  14922. bounds.w -= style->cursor_size.x;
  14923. /* make sure the provided values are correct */
  14924. slider_max = NK_MAX(min, max);
  14925. slider_min = NK_MIN(min, max);
  14926. slider_value = NK_CLAMP(slider_min, val, slider_max);
  14927. slider_range = slider_max - slider_min;
  14928. slider_steps = slider_range / step;
  14929. cursor_offset = (slider_value - slider_min) / step;
  14930. /* calculate cursor
  14931. Basically you have two cursors. One for visual representation and interaction
  14932. and one for updating the actual cursor value. */
  14933. logical_cursor.h = bounds.h;
  14934. logical_cursor.w = bounds.w / slider_steps;
  14935. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  14936. logical_cursor.y = bounds.y;
  14937. visual_cursor.h = style->cursor_size.y;
  14938. visual_cursor.w = style->cursor_size.x;
  14939. visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
  14940. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  14941. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  14942. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  14943. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  14944. /* draw slider */
  14945. if (style->draw_begin) style->draw_begin(out, style->userdata);
  14946. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  14947. if (style->draw_end) style->draw_end(out, style->userdata);
  14948. return slider_value;
  14949. }
  14950. /* ===============================================================
  14951. *
  14952. * PROGRESSBAR
  14953. *
  14954. * ===============================================================*/
  14955. NK_INTERN nk_size
  14956. nk_progress_behavior(nk_flags *state, struct nk_input *in,
  14957. struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, int modifiable)
  14958. {
  14959. int left_mouse_down = 0;
  14960. int left_mouse_click_in_cursor = 0;
  14961. nk_widget_state_reset(state);
  14962. if (!in || !modifiable) return value;
  14963. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  14964. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  14965. NK_BUTTON_LEFT, cursor, nk_true);
  14966. if (nk_input_is_mouse_hovering_rect(in, r))
  14967. *state = NK_WIDGET_STATE_HOVERED;
  14968. if (in && left_mouse_down && left_mouse_click_in_cursor) {
  14969. if (left_mouse_down && left_mouse_click_in_cursor) {
  14970. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
  14971. value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
  14972. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
  14973. *state |= NK_WIDGET_STATE_ACTIVE;
  14974. }
  14975. }
  14976. /* set progressbar widget state */
  14977. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  14978. *state |= NK_WIDGET_STATE_ENTERED;
  14979. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  14980. *state |= NK_WIDGET_STATE_LEFT;
  14981. return value;
  14982. }
  14983. NK_INTERN void
  14984. nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
  14985. const struct nk_style_progress *style, const struct nk_rect *bounds,
  14986. const struct nk_rect *scursor, nk_size value, nk_size max)
  14987. {
  14988. const struct nk_style_item *background;
  14989. const struct nk_style_item *cursor;
  14990. NK_UNUSED(max);
  14991. NK_UNUSED(value);
  14992. /* select correct colors/images to draw */
  14993. if (state & NK_WIDGET_STATE_ACTIVED) {
  14994. background = &style->active;
  14995. cursor = &style->cursor_active;
  14996. } else if (state & NK_WIDGET_STATE_HOVER){
  14997. background = &style->hover;
  14998. cursor = &style->cursor_hover;
  14999. } else {
  15000. background = &style->normal;
  15001. cursor = &style->cursor_normal;
  15002. }
  15003. /* draw background */
  15004. if (background->type == NK_STYLE_ITEM_COLOR) {
  15005. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  15006. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  15007. } else nk_draw_image(out, *bounds, &background->data.image, nk_white);
  15008. /* draw cursor */
  15009. if (cursor->type == NK_STYLE_ITEM_COLOR) {
  15010. nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
  15011. nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
  15012. } else nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
  15013. }
  15014. NK_INTERN nk_size
  15015. nk_do_progress(nk_flags *state,
  15016. struct nk_command_buffer *out, struct nk_rect bounds,
  15017. nk_size value, nk_size max, int modifiable,
  15018. const struct nk_style_progress *style, struct nk_input *in)
  15019. {
  15020. float prog_scale;
  15021. nk_size prog_value;
  15022. struct nk_rect cursor;
  15023. NK_ASSERT(style);
  15024. NK_ASSERT(out);
  15025. if (!out || !style) return 0;
  15026. /* calculate progressbar cursor */
  15027. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  15028. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  15029. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  15030. prog_scale = (float)value / (float)max;
  15031. /* update progressbar */
  15032. prog_value = NK_MIN(value, max);
  15033. prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
  15034. cursor.w = cursor.w * prog_scale;
  15035. /* draw progressbar */
  15036. if (style->draw_begin) style->draw_begin(out, style->userdata);
  15037. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  15038. if (style->draw_end) style->draw_end(out, style->userdata);
  15039. return prog_value;
  15040. }
  15041. /* ===============================================================
  15042. *
  15043. * SCROLLBAR
  15044. *
  15045. * ===============================================================*/
  15046. NK_INTERN float
  15047. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  15048. int has_scrolling, const struct nk_rect *scroll,
  15049. const struct nk_rect *cursor, const struct nk_rect *empty0,
  15050. const struct nk_rect *empty1, float scroll_offset,
  15051. float target, float scroll_step, enum nk_orientation o)
  15052. {
  15053. nk_flags ws = 0;
  15054. int left_mouse_down;
  15055. int left_mouse_click_in_cursor;
  15056. float scroll_delta;
  15057. nk_widget_state_reset(state);
  15058. if (!in) return scroll_offset;
  15059. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  15060. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  15061. NK_BUTTON_LEFT, *cursor, nk_true);
  15062. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  15063. *state = NK_WIDGET_STATE_HOVERED;
  15064. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  15065. if (left_mouse_down && left_mouse_click_in_cursor) {
  15066. /* update cursor by mouse dragging */
  15067. float pixel, delta;
  15068. *state = NK_WIDGET_STATE_ACTIVE;
  15069. if (o == NK_VERTICAL) {
  15070. float cursor_y;
  15071. pixel = in->mouse.delta.y;
  15072. delta = (pixel / scroll->h) * target;
  15073. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  15074. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  15075. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  15076. } else {
  15077. float cursor_x;
  15078. pixel = in->mouse.delta.x;
  15079. delta = (pixel / scroll->w) * target;
  15080. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  15081. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  15082. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  15083. }
  15084. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  15085. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  15086. /* scroll page up by click on empty space or shortcut */
  15087. if (o == NK_VERTICAL)
  15088. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  15089. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  15090. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  15091. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  15092. /* scroll page down by click on empty space or shortcut */
  15093. if (o == NK_VERTICAL)
  15094. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  15095. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  15096. } else if (has_scrolling) {
  15097. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  15098. /* update cursor by mouse scrolling */
  15099. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  15100. if (o == NK_VERTICAL)
  15101. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  15102. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  15103. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  15104. /* update cursor to the beginning */
  15105. if (o == NK_VERTICAL) scroll_offset = 0;
  15106. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  15107. /* update cursor to the end */
  15108. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  15109. }
  15110. }
  15111. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  15112. *state |= NK_WIDGET_STATE_ENTERED;
  15113. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  15114. *state |= NK_WIDGET_STATE_LEFT;
  15115. return scroll_offset;
  15116. }
  15117. NK_INTERN void
  15118. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  15119. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  15120. const struct nk_rect *scroll)
  15121. {
  15122. const struct nk_style_item *background;
  15123. const struct nk_style_item *cursor;
  15124. /* select correct colors/images to draw */
  15125. if (state & NK_WIDGET_STATE_ACTIVED) {
  15126. background = &style->active;
  15127. cursor = &style->cursor_active;
  15128. } else if (state & NK_WIDGET_STATE_HOVER) {
  15129. background = &style->hover;
  15130. cursor = &style->cursor_hover;
  15131. } else {
  15132. background = &style->normal;
  15133. cursor = &style->cursor_normal;
  15134. }
  15135. /* draw background */
  15136. if (background->type == NK_STYLE_ITEM_COLOR) {
  15137. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  15138. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  15139. } else {
  15140. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  15141. }
  15142. /* draw cursor */
  15143. if (background->type == NK_STYLE_ITEM_COLOR) {
  15144. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  15145. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  15146. } else nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  15147. }
  15148. NK_INTERN float
  15149. nk_do_scrollbarv(nk_flags *state,
  15150. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  15151. float offset, float target, float step, float button_pixel_inc,
  15152. const struct nk_style_scrollbar *style, struct nk_input *in,
  15153. const struct nk_user_font *font)
  15154. {
  15155. struct nk_rect empty_north;
  15156. struct nk_rect empty_south;
  15157. struct nk_rect cursor;
  15158. float scroll_step;
  15159. float scroll_offset;
  15160. float scroll_off;
  15161. float scroll_ratio;
  15162. NK_ASSERT(out);
  15163. NK_ASSERT(style);
  15164. NK_ASSERT(state);
  15165. if (!out || !style) return 0;
  15166. scroll.w = NK_MAX(scroll.w, 1);
  15167. scroll.h = NK_MAX(scroll.h, 0);
  15168. if (target <= scroll.h) return 0;
  15169. /* optional scrollbar buttons */
  15170. if (style->show_buttons) {
  15171. nk_flags ws;
  15172. float scroll_h;
  15173. struct nk_rect button;
  15174. button.x = scroll.x;
  15175. button.w = scroll.w;
  15176. button.h = scroll.w;
  15177. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  15178. scroll_step = NK_MIN(step, button_pixel_inc);
  15179. /* decrement button */
  15180. button.y = scroll.y;
  15181. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  15182. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  15183. offset = offset - scroll_step;
  15184. /* increment button */
  15185. button.y = scroll.y + scroll.h - button.h;
  15186. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  15187. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  15188. offset = offset + scroll_step;
  15189. scroll.y = scroll.y + button.h;
  15190. scroll.h = scroll_h;
  15191. }
  15192. /* calculate scrollbar constants */
  15193. scroll_step = NK_MIN(step, scroll.h);
  15194. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  15195. scroll_ratio = scroll.h / target;
  15196. scroll_off = scroll_offset / target;
  15197. /* calculate scrollbar cursor bounds */
  15198. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  15199. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  15200. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  15201. cursor.x = scroll.x + style->border + style->padding.x;
  15202. /* calculate empty space around cursor */
  15203. empty_north.x = scroll.x;
  15204. empty_north.y = scroll.y;
  15205. empty_north.w = scroll.w;
  15206. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  15207. empty_south.x = scroll.x;
  15208. empty_south.y = cursor.y + cursor.h;
  15209. empty_south.w = scroll.w;
  15210. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  15211. /* update scrollbar */
  15212. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  15213. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  15214. scroll_off = scroll_offset / target;
  15215. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  15216. /* draw scrollbar */
  15217. if (style->draw_begin) style->draw_begin(out, style->userdata);
  15218. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  15219. if (style->draw_end) style->draw_end(out, style->userdata);
  15220. return scroll_offset;
  15221. }
  15222. NK_INTERN float
  15223. nk_do_scrollbarh(nk_flags *state,
  15224. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  15225. float offset, float target, float step, float button_pixel_inc,
  15226. const struct nk_style_scrollbar *style, struct nk_input *in,
  15227. const struct nk_user_font *font)
  15228. {
  15229. struct nk_rect cursor;
  15230. struct nk_rect empty_west;
  15231. struct nk_rect empty_east;
  15232. float scroll_step;
  15233. float scroll_offset;
  15234. float scroll_off;
  15235. float scroll_ratio;
  15236. NK_ASSERT(out);
  15237. NK_ASSERT(style);
  15238. if (!out || !style) return 0;
  15239. /* scrollbar background */
  15240. scroll.h = NK_MAX(scroll.h, 1);
  15241. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  15242. if (target <= scroll.w) return 0;
  15243. /* optional scrollbar buttons */
  15244. if (style->show_buttons) {
  15245. nk_flags ws;
  15246. float scroll_w;
  15247. struct nk_rect button;
  15248. button.y = scroll.y;
  15249. button.w = scroll.h;
  15250. button.h = scroll.h;
  15251. scroll_w = scroll.w - 2 * button.w;
  15252. scroll_step = NK_MIN(step, button_pixel_inc);
  15253. /* decrement button */
  15254. button.x = scroll.x;
  15255. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  15256. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  15257. offset = offset - scroll_step;
  15258. /* increment button */
  15259. button.x = scroll.x + scroll.w - button.w;
  15260. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  15261. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  15262. offset = offset + scroll_step;
  15263. scroll.x = scroll.x + button.w;
  15264. scroll.w = scroll_w;
  15265. }
  15266. /* calculate scrollbar constants */
  15267. scroll_step = NK_MIN(step, scroll.w);
  15268. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  15269. scroll_ratio = scroll.w / target;
  15270. scroll_off = scroll_offset / target;
  15271. /* calculate cursor bounds */
  15272. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  15273. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  15274. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  15275. cursor.y = scroll.y + style->border + style->padding.y;
  15276. /* calculate empty space around cursor */
  15277. empty_west.x = scroll.x;
  15278. empty_west.y = scroll.y;
  15279. empty_west.w = cursor.x - scroll.x;
  15280. empty_west.h = scroll.h;
  15281. empty_east.x = cursor.x + cursor.w;
  15282. empty_east.y = scroll.y;
  15283. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  15284. empty_east.h = scroll.h;
  15285. /* update scrollbar */
  15286. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  15287. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  15288. scroll_off = scroll_offset / target;
  15289. cursor.x = scroll.x + (scroll_off * scroll.w);
  15290. /* draw scrollbar */
  15291. if (style->draw_begin) style->draw_begin(out, style->userdata);
  15292. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  15293. if (style->draw_end) style->draw_end(out, style->userdata);
  15294. return scroll_offset;
  15295. }
  15296. /* ===============================================================
  15297. *
  15298. * FILTER
  15299. *
  15300. * ===============================================================*/
  15301. NK_API int nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
  15302. {(void)unicode;NK_UNUSED(box);return nk_true;}
  15303. NK_API int
  15304. nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
  15305. {
  15306. NK_UNUSED(box);
  15307. if (unicode > 128) return nk_false;
  15308. else return nk_true;
  15309. }
  15310. NK_API int
  15311. nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
  15312. {
  15313. NK_UNUSED(box);
  15314. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  15315. return nk_false;
  15316. else return nk_true;
  15317. }
  15318. NK_API int
  15319. nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
  15320. {
  15321. NK_UNUSED(box);
  15322. if ((unicode < '0' || unicode > '9') && unicode != '-')
  15323. return nk_false;
  15324. else return nk_true;
  15325. }
  15326. NK_API int
  15327. nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
  15328. {
  15329. NK_UNUSED(box);
  15330. if ((unicode < '0' || unicode > '9') &&
  15331. (unicode < 'a' || unicode > 'f') &&
  15332. (unicode < 'A' || unicode > 'F'))
  15333. return nk_false;
  15334. else return nk_true;
  15335. }
  15336. NK_API int
  15337. nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
  15338. {
  15339. NK_UNUSED(box);
  15340. if (unicode < '0' || unicode > '7')
  15341. return nk_false;
  15342. else return nk_true;
  15343. }
  15344. NK_API int
  15345. nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
  15346. {
  15347. NK_UNUSED(box);
  15348. if (unicode != '0' && unicode != '1')
  15349. return nk_false;
  15350. else return nk_true;
  15351. }
  15352. /* ===============================================================
  15353. *
  15354. * EDIT
  15355. *
  15356. * ===============================================================*/
  15357. NK_INTERN void
  15358. nk_edit_draw_text(struct nk_command_buffer *out,
  15359. const struct nk_style_edit *style, float pos_x, float pos_y,
  15360. float x_offset, const char *text, int byte_len, float row_height,
  15361. const struct nk_user_font *font, struct nk_color background,
  15362. struct nk_color foreground, int is_selected)
  15363. {
  15364. NK_ASSERT(out);
  15365. NK_ASSERT(font);
  15366. NK_ASSERT(style);
  15367. if (!text || !byte_len || !out || !style) return;
  15368. {int glyph_len = 0;
  15369. nk_rune unicode = 0;
  15370. int text_len = 0;
  15371. float line_width = 0;
  15372. float glyph_width;
  15373. const char *line = text;
  15374. float line_offset = 0;
  15375. int line_count = 0;
  15376. struct nk_text txt;
  15377. txt.padding = nk_vec2(0,0);
  15378. txt.background = background;
  15379. txt.text = foreground;
  15380. glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
  15381. if (!glyph_len) return;
  15382. while ((text_len < byte_len) && glyph_len)
  15383. {
  15384. if (unicode == '\n') {
  15385. /* new line separator so draw previous line */
  15386. struct nk_rect label;
  15387. label.y = pos_y + line_offset;
  15388. label.h = row_height;
  15389. label.w = line_width;
  15390. label.x = pos_x;
  15391. if (!line_count)
  15392. label.x += x_offset;
  15393. if (is_selected) /* selection needs to draw different background color */
  15394. nk_fill_rect(out, label, 0, background);
  15395. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  15396. &txt, NK_TEXT_CENTERED, font);
  15397. text_len++;
  15398. line_count++;
  15399. line_width = 0;
  15400. line = text + text_len;
  15401. line_offset += row_height;
  15402. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
  15403. continue;
  15404. }
  15405. if (unicode == '\r') {
  15406. text_len++;
  15407. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  15408. continue;
  15409. }
  15410. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  15411. line_width += (float)glyph_width;
  15412. text_len += glyph_len;
  15413. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  15414. continue;
  15415. }
  15416. if (line_width > 0) {
  15417. /* draw last line */
  15418. struct nk_rect label;
  15419. label.y = pos_y + line_offset;
  15420. label.h = row_height;
  15421. label.w = line_width;
  15422. label.x = pos_x;
  15423. if (!line_count)
  15424. label.x += x_offset;
  15425. if (is_selected)
  15426. nk_fill_rect(out, label, 0, background);
  15427. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  15428. &txt, NK_TEXT_LEFT, font);
  15429. }}
  15430. }
  15431. NK_INTERN nk_flags
  15432. nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
  15433. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  15434. struct nk_text_edit *edit, const struct nk_style_edit *style,
  15435. struct nk_input *in, const struct nk_user_font *font)
  15436. {
  15437. struct nk_rect area;
  15438. nk_flags ret = 0;
  15439. float row_height;
  15440. char prev_state = 0;
  15441. char is_hovered = 0;
  15442. char select_all = 0;
  15443. char cursor_follow = 0;
  15444. struct nk_rect old_clip;
  15445. struct nk_rect clip;
  15446. NK_ASSERT(state);
  15447. NK_ASSERT(out);
  15448. NK_ASSERT(style);
  15449. if (!state || !out || !style)
  15450. return ret;
  15451. /* visible text area calculation */
  15452. area.x = bounds.x + style->padding.x + style->border;
  15453. area.y = bounds.y + style->padding.y + style->border;
  15454. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  15455. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  15456. if (flags & NK_EDIT_MULTILINE)
  15457. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  15458. row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
  15459. /* calculate clipping rectangle */
  15460. old_clip = out->clip;
  15461. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  15462. /* update edit state */
  15463. prev_state = (char)edit->active;
  15464. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
  15465. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  15466. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  15467. bounds.x, bounds.y, bounds.w, bounds.h);
  15468. }
  15469. /* (de)activate text editor */
  15470. if (!prev_state && edit->active) {
  15471. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  15472. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
  15473. nk_textedit_clear_state(edit, type, filter);
  15474. if (flags & NK_EDIT_AUTO_SELECT)
  15475. select_all = nk_true;
  15476. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  15477. edit->cursor = edit->string.len;
  15478. in = 0;
  15479. }
  15480. } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  15481. if (flags & NK_EDIT_READ_ONLY)
  15482. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  15483. else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  15484. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  15485. ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
  15486. if (prev_state != edit->active)
  15487. ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
  15488. /* handle user input */
  15489. if (edit->active && in)
  15490. {
  15491. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  15492. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  15493. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  15494. /* mouse click handler */
  15495. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  15496. if (select_all) {
  15497. nk_textedit_select_all(edit);
  15498. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  15499. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  15500. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  15501. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  15502. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  15503. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  15504. cursor_follow = nk_true;
  15505. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  15506. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  15507. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  15508. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  15509. cursor_follow = nk_true;
  15510. }
  15511. {int i; /* keyboard input */
  15512. int old_mode = edit->mode;
  15513. for (i = 0; i < NK_KEY_MAX; ++i) {
  15514. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  15515. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  15516. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  15517. cursor_follow = nk_true;
  15518. }
  15519. }
  15520. if (old_mode != edit->mode) {
  15521. in->keyboard.text_len = 0;
  15522. }}
  15523. /* text input */
  15524. edit->filter = filter;
  15525. if (in->keyboard.text_len) {
  15526. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  15527. cursor_follow = nk_true;
  15528. in->keyboard.text_len = 0;
  15529. }
  15530. /* enter key handler */
  15531. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  15532. cursor_follow = nk_true;
  15533. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  15534. nk_textedit_text(edit, "\n", 1);
  15535. else if (flags & NK_EDIT_SIG_ENTER)
  15536. ret |= NK_EDIT_COMMITED;
  15537. else nk_textedit_text(edit, "\n", 1);
  15538. }
  15539. /* cut & copy handler */
  15540. {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
  15541. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  15542. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  15543. {
  15544. int glyph_len;
  15545. nk_rune unicode;
  15546. const char *text;
  15547. int b = edit->select_start;
  15548. int e = edit->select_end;
  15549. int begin = NK_MIN(b, e);
  15550. int end = NK_MAX(b, e);
  15551. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  15552. if (edit->clip.copy)
  15553. edit->clip.copy(edit->clip.userdata, text, end - begin);
  15554. if (cut && !(flags & NK_EDIT_READ_ONLY)){
  15555. nk_textedit_cut(edit);
  15556. cursor_follow = nk_true;
  15557. }
  15558. }}
  15559. /* paste handler */
  15560. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  15561. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  15562. edit->clip.paste(edit->clip.userdata, edit);
  15563. cursor_follow = nk_true;
  15564. }}
  15565. /* tab handler */
  15566. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  15567. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  15568. nk_textedit_text(edit, " ", 4);
  15569. cursor_follow = nk_true;
  15570. }}
  15571. }
  15572. /* set widget state */
  15573. if (edit->active)
  15574. *state = NK_WIDGET_STATE_ACTIVE;
  15575. else nk_widget_state_reset(state);
  15576. if (is_hovered)
  15577. *state |= NK_WIDGET_STATE_HOVERED;
  15578. /* DRAW EDIT */
  15579. {const char *text = nk_str_get_const(&edit->string);
  15580. int len = nk_str_len_char(&edit->string);
  15581. {/* select background colors/images */
  15582. const struct nk_style_item *background;
  15583. if (*state & NK_WIDGET_STATE_ACTIVED)
  15584. background = &style->active;
  15585. else if (*state & NK_WIDGET_STATE_HOVER)
  15586. background = &style->hover;
  15587. else background = &style->normal;
  15588. /* draw background frame */
  15589. if (background->type == NK_STYLE_ITEM_COLOR) {
  15590. nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
  15591. nk_fill_rect(out, bounds, style->rounding, background->data.color);
  15592. } else nk_draw_image(out, bounds, &background->data.image, nk_white);}
  15593. area.w = NK_MAX(0, area.w - style->cursor_size);
  15594. if (edit->active)
  15595. {
  15596. int total_lines = 1;
  15597. struct nk_vec2 text_size = nk_vec2(0,0);
  15598. /* text pointer positions */
  15599. const char *cursor_ptr = 0;
  15600. const char *select_begin_ptr = 0;
  15601. const char *select_end_ptr = 0;
  15602. /* 2D pixel positions */
  15603. struct nk_vec2 cursor_pos = nk_vec2(0,0);
  15604. struct nk_vec2 selection_offset_start = nk_vec2(0,0);
  15605. struct nk_vec2 selection_offset_end = nk_vec2(0,0);
  15606. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  15607. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  15608. /* calculate total line count + total space + cursor/selection position */
  15609. float line_width = 0.0f;
  15610. if (text && len)
  15611. {
  15612. /* utf8 encoding */
  15613. float glyph_width;
  15614. int glyph_len = 0;
  15615. nk_rune unicode = 0;
  15616. int text_len = 0;
  15617. int glyphs = 0;
  15618. int row_begin = 0;
  15619. glyph_len = nk_utf_decode(text, &unicode, len);
  15620. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  15621. line_width = 0;
  15622. /* iterate all lines */
  15623. while ((text_len < len) && glyph_len)
  15624. {
  15625. /* set cursor 2D position and line */
  15626. if (!cursor_ptr && glyphs == edit->cursor)
  15627. {
  15628. int glyph_offset;
  15629. struct nk_vec2 out_offset;
  15630. struct nk_vec2 row_size;
  15631. const char *remaining;
  15632. /* calculate 2d position */
  15633. cursor_pos.y = (float)(total_lines-1) * row_height;
  15634. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  15635. text_len-row_begin, row_height, &remaining,
  15636. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  15637. cursor_pos.x = row_size.x;
  15638. cursor_ptr = text + text_len;
  15639. }
  15640. /* set start selection 2D position and line */
  15641. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  15642. glyphs == selection_begin)
  15643. {
  15644. int glyph_offset;
  15645. struct nk_vec2 out_offset;
  15646. struct nk_vec2 row_size;
  15647. const char *remaining;
  15648. /* calculate 2d position */
  15649. selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
  15650. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  15651. text_len-row_begin, row_height, &remaining,
  15652. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  15653. selection_offset_start.x = row_size.x;
  15654. select_begin_ptr = text + text_len;
  15655. }
  15656. /* set end selection 2D position and line */
  15657. if (!select_end_ptr && edit->select_start != edit->select_end &&
  15658. glyphs == selection_end)
  15659. {
  15660. int glyph_offset;
  15661. struct nk_vec2 out_offset;
  15662. struct nk_vec2 row_size;
  15663. const char *remaining;
  15664. /* calculate 2d position */
  15665. selection_offset_end.y = (float)(total_lines-1) * row_height;
  15666. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  15667. text_len-row_begin, row_height, &remaining,
  15668. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  15669. selection_offset_end.x = row_size.x;
  15670. select_end_ptr = text + text_len;
  15671. }
  15672. if (unicode == '\n') {
  15673. text_size.x = NK_MAX(text_size.x, line_width);
  15674. total_lines++;
  15675. line_width = 0;
  15676. text_len++;
  15677. glyphs++;
  15678. row_begin = text_len;
  15679. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  15680. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  15681. continue;
  15682. }
  15683. glyphs++;
  15684. text_len += glyph_len;
  15685. line_width += (float)glyph_width;
  15686. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  15687. glyph_width = font->width(font->userdata, font->height,
  15688. text+text_len, glyph_len);
  15689. continue;
  15690. }
  15691. text_size.y = (float)total_lines * row_height;
  15692. /* handle case when cursor is at end of text buffer */
  15693. if (!cursor_ptr && edit->cursor == edit->string.len) {
  15694. cursor_pos.x = line_width;
  15695. cursor_pos.y = text_size.y - row_height;
  15696. }
  15697. }
  15698. {
  15699. /* scrollbar */
  15700. if (cursor_follow)
  15701. {
  15702. /* update scrollbar to follow cursor */
  15703. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  15704. /* horizontal scroll */
  15705. const float scroll_increment = area.w * 0.25f;
  15706. if (cursor_pos.x < edit->scrollbar.x)
  15707. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  15708. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  15709. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x);
  15710. } else edit->scrollbar.x = 0;
  15711. if (flags & NK_EDIT_MULTILINE) {
  15712. /* vertical scroll */
  15713. if (cursor_pos.y < edit->scrollbar.y)
  15714. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
  15715. if (cursor_pos.y >= edit->scrollbar.y + area.h)
  15716. edit->scrollbar.y = edit->scrollbar.y + row_height;
  15717. } else edit->scrollbar.y = 0;
  15718. }
  15719. /* scrollbar widget */
  15720. if (flags & NK_EDIT_MULTILINE)
  15721. {
  15722. nk_flags ws;
  15723. struct nk_rect scroll;
  15724. float scroll_target;
  15725. float scroll_offset;
  15726. float scroll_step;
  15727. float scroll_inc;
  15728. scroll = area;
  15729. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  15730. scroll.w = style->scrollbar_size.x;
  15731. scroll_offset = edit->scrollbar.y;
  15732. scroll_step = scroll.h * 0.10f;
  15733. scroll_inc = scroll.h * 0.01f;
  15734. scroll_target = text_size.y;
  15735. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
  15736. scroll_offset, scroll_target, scroll_step, scroll_inc,
  15737. &style->scrollbar, in, font);
  15738. }
  15739. }
  15740. /* draw text */
  15741. {struct nk_color background_color;
  15742. struct nk_color text_color;
  15743. struct nk_color sel_background_color;
  15744. struct nk_color sel_text_color;
  15745. struct nk_color cursor_color;
  15746. struct nk_color cursor_text_color;
  15747. const struct nk_style_item *background;
  15748. nk_push_scissor(out, clip);
  15749. /* select correct colors to draw */
  15750. if (*state & NK_WIDGET_STATE_ACTIVED) {
  15751. background = &style->active;
  15752. text_color = style->text_active;
  15753. sel_text_color = style->selected_text_hover;
  15754. sel_background_color = style->selected_hover;
  15755. cursor_color = style->cursor_hover;
  15756. cursor_text_color = style->cursor_text_hover;
  15757. } else if (*state & NK_WIDGET_STATE_HOVER) {
  15758. background = &style->hover;
  15759. text_color = style->text_hover;
  15760. sel_text_color = style->selected_text_hover;
  15761. sel_background_color = style->selected_hover;
  15762. cursor_text_color = style->cursor_text_hover;
  15763. cursor_color = style->cursor_hover;
  15764. } else {
  15765. background = &style->normal;
  15766. text_color = style->text_normal;
  15767. sel_text_color = style->selected_text_normal;
  15768. sel_background_color = style->selected_normal;
  15769. cursor_color = style->cursor_normal;
  15770. cursor_text_color = style->cursor_text_normal;
  15771. }
  15772. if (background->type == NK_STYLE_ITEM_IMAGE)
  15773. background_color = nk_rgba(0,0,0,0);
  15774. else background_color = background->data.color;
  15775. if (edit->select_start == edit->select_end) {
  15776. /* no selection so just draw the complete text */
  15777. const char *begin = nk_str_get_const(&edit->string);
  15778. int l = nk_str_len_char(&edit->string);
  15779. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  15780. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  15781. background_color, text_color, nk_false);
  15782. } else {
  15783. /* edit has selection so draw 1-3 text chunks */
  15784. if (edit->select_start != edit->select_end && selection_begin > 0){
  15785. /* draw unselected text before selection */
  15786. const char *begin = nk_str_get_const(&edit->string);
  15787. NK_ASSERT(select_begin_ptr);
  15788. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  15789. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  15790. row_height, font, background_color, text_color, nk_false);
  15791. }
  15792. if (edit->select_start != edit->select_end) {
  15793. /* draw selected text */
  15794. NK_ASSERT(select_begin_ptr);
  15795. if (!select_end_ptr) {
  15796. const char *begin = nk_str_get_const(&edit->string);
  15797. select_end_ptr = begin + nk_str_len_char(&edit->string);
  15798. }
  15799. nk_edit_draw_text(out, style,
  15800. area.x - edit->scrollbar.x,
  15801. area.y + selection_offset_start.y - edit->scrollbar.y,
  15802. selection_offset_start.x,
  15803. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  15804. row_height, font, sel_background_color, sel_text_color, nk_true);
  15805. }
  15806. if ((edit->select_start != edit->select_end &&
  15807. selection_end < edit->string.len))
  15808. {
  15809. /* draw unselected text after selected text */
  15810. const char *begin = select_end_ptr;
  15811. const char *end = nk_str_get_const(&edit->string) +
  15812. nk_str_len_char(&edit->string);
  15813. NK_ASSERT(select_end_ptr);
  15814. nk_edit_draw_text(out, style,
  15815. area.x - edit->scrollbar.x,
  15816. area.y + selection_offset_end.y - edit->scrollbar.y,
  15817. selection_offset_end.x,
  15818. begin, (int)(end - begin), row_height, font,
  15819. background_color, text_color, nk_true);
  15820. }
  15821. }
  15822. /* cursor */
  15823. if (edit->select_start == edit->select_end)
  15824. {
  15825. if (edit->cursor >= nk_str_len(&edit->string) ||
  15826. (cursor_ptr && *cursor_ptr == '\n')) {
  15827. /* draw cursor at end of line */
  15828. struct nk_rect cursor;
  15829. cursor.w = style->cursor_size;
  15830. cursor.h = font->height;
  15831. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  15832. cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
  15833. cursor.y -= edit->scrollbar.y;
  15834. nk_fill_rect(out, cursor, 0, cursor_color);
  15835. } else {
  15836. /* draw cursor inside text */
  15837. int glyph_len;
  15838. struct nk_rect label;
  15839. struct nk_text txt;
  15840. nk_rune unicode;
  15841. NK_ASSERT(cursor_ptr);
  15842. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  15843. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  15844. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  15845. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  15846. label.h = row_height;
  15847. txt.padding = nk_vec2(0,0);
  15848. txt.background = cursor_color;;
  15849. txt.text = cursor_text_color;
  15850. nk_fill_rect(out, label, 0, cursor_color);
  15851. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  15852. }
  15853. }}
  15854. } else {
  15855. /* not active so just draw text */
  15856. int l = nk_str_len_char(&edit->string);
  15857. const char *begin = nk_str_get_const(&edit->string);
  15858. const struct nk_style_item *background;
  15859. struct nk_color background_color;
  15860. struct nk_color text_color;
  15861. nk_push_scissor(out, clip);
  15862. if (*state & NK_WIDGET_STATE_ACTIVED) {
  15863. background = &style->active;
  15864. text_color = style->text_active;
  15865. } else if (*state & NK_WIDGET_STATE_HOVER) {
  15866. background = &style->hover;
  15867. text_color = style->text_hover;
  15868. } else {
  15869. background = &style->normal;
  15870. text_color = style->text_normal;
  15871. }
  15872. if (background->type == NK_STYLE_ITEM_IMAGE)
  15873. background_color = nk_rgba(0,0,0,0);
  15874. else background_color = background->data.color;
  15875. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  15876. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  15877. background_color, text_color, nk_false);
  15878. }
  15879. nk_push_scissor(out, old_clip);}
  15880. return ret;
  15881. }
  15882. /* ===============================================================
  15883. *
  15884. * PROPERTY
  15885. *
  15886. * ===============================================================*/
  15887. enum nk_property_status {
  15888. NK_PROPERTY_DEFAULT,
  15889. NK_PROPERTY_EDIT,
  15890. NK_PROPERTY_DRAG
  15891. };
  15892. enum nk_property_filter {
  15893. NK_FILTER_INT,
  15894. NK_FILTER_FLOAT
  15895. };
  15896. enum nk_property_kind {
  15897. NK_PROPERTY_INT,
  15898. NK_PROPERTY_FLOAT,
  15899. NK_PROPERTY_DOUBLE
  15900. };
  15901. union nk_property {
  15902. int i;
  15903. float f;
  15904. double d;
  15905. };
  15906. struct nk_property_variant {
  15907. enum nk_property_kind kind;
  15908. union nk_property value;
  15909. union nk_property min_value;
  15910. union nk_property max_value;
  15911. union nk_property step;
  15912. };
  15913. NK_INTERN void
  15914. nk_drag_behavior(nk_flags *state, const struct nk_input *in,
  15915. struct nk_rect drag, struct nk_property_variant *variant,
  15916. float inc_per_pixel)
  15917. {
  15918. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  15919. int left_mouse_click_in_cursor = in &&
  15920. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  15921. nk_widget_state_reset(state);
  15922. if (nk_input_is_mouse_hovering_rect(in, drag))
  15923. *state = NK_WIDGET_STATE_HOVERED;
  15924. if (left_mouse_down && left_mouse_click_in_cursor) {
  15925. float delta, pixels;
  15926. pixels = in->mouse.delta.x;
  15927. delta = pixels * inc_per_pixel;
  15928. switch (variant->kind) {
  15929. default: break;
  15930. case NK_PROPERTY_INT:
  15931. variant->value.i = variant->value.i + (int)delta;
  15932. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  15933. break;
  15934. case NK_PROPERTY_FLOAT:
  15935. variant->value.f = variant->value.f + (float)delta;
  15936. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  15937. break;
  15938. case NK_PROPERTY_DOUBLE:
  15939. variant->value.d = variant->value.d + (double)delta;
  15940. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  15941. break;
  15942. }
  15943. *state = NK_WIDGET_STATE_ACTIVE;
  15944. }
  15945. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  15946. *state |= NK_WIDGET_STATE_ENTERED;
  15947. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  15948. *state |= NK_WIDGET_STATE_LEFT;
  15949. }
  15950. NK_INTERN void
  15951. nk_property_behavior(nk_flags *ws, const struct nk_input *in,
  15952. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  15953. struct nk_rect empty, int *state, struct nk_property_variant *variant,
  15954. float inc_per_pixel)
  15955. {
  15956. if (in && *state == NK_PROPERTY_DEFAULT) {
  15957. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  15958. *state = NK_PROPERTY_EDIT;
  15959. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  15960. *state = NK_PROPERTY_DRAG;
  15961. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  15962. *state = NK_PROPERTY_DRAG;
  15963. }
  15964. if (*state == NK_PROPERTY_DRAG) {
  15965. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  15966. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  15967. }
  15968. }
  15969. NK_INTERN void
  15970. nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
  15971. const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
  15972. const char *name, int len, const struct nk_user_font *font)
  15973. {
  15974. struct nk_text text;
  15975. const struct nk_style_item *background;
  15976. /* select correct background and text color */
  15977. if (state & NK_WIDGET_STATE_ACTIVED) {
  15978. background = &style->active;
  15979. text.text = style->label_active;
  15980. } else if (state & NK_WIDGET_STATE_HOVER) {
  15981. background = &style->hover;
  15982. text.text = style->label_hover;
  15983. } else {
  15984. background = &style->normal;
  15985. text.text = style->label_normal;
  15986. }
  15987. /* draw background */
  15988. if (background->type == NK_STYLE_ITEM_IMAGE) {
  15989. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  15990. text.background = nk_rgba(0,0,0,0);
  15991. } else {
  15992. text.background = background->data.color;
  15993. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  15994. nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
  15995. }
  15996. /* draw label */
  15997. text.padding = nk_vec2(0,0);
  15998. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  15999. }
  16000. NK_INTERN void
  16001. nk_do_property(nk_flags *ws,
  16002. struct nk_command_buffer *out, struct nk_rect property,
  16003. const char *name, struct nk_property_variant *variant,
  16004. float inc_per_pixel, char *buffer, int *len,
  16005. int *state, int *cursor, int *select_begin, int *select_end,
  16006. const struct nk_style_property *style,
  16007. enum nk_property_filter filter, struct nk_input *in,
  16008. const struct nk_user_font *font, struct nk_text_edit *text_edit,
  16009. enum nk_button_behavior behavior)
  16010. {
  16011. const nk_plugin_filter filters[] = {
  16012. nk_filter_decimal,
  16013. nk_filter_float
  16014. };
  16015. int active, old;
  16016. int num_len, name_len;
  16017. char string[NK_MAX_NUMBER_BUFFER];
  16018. float size;
  16019. char *dst = 0;
  16020. int *length;
  16021. struct nk_rect left;
  16022. struct nk_rect right;
  16023. struct nk_rect label;
  16024. struct nk_rect edit;
  16025. struct nk_rect empty;
  16026. /* left decrement button */
  16027. left.h = font->height/2;
  16028. left.w = left.h;
  16029. left.x = property.x + style->border + style->padding.x;
  16030. left.y = property.y + style->border + property.h/2.0f - left.h/2;
  16031. /* text label */
  16032. name_len = nk_strlen(name);
  16033. size = font->width(font->userdata, font->height, name, name_len);
  16034. label.x = left.x + left.w + style->padding.x;
  16035. label.w = (float)size + 2 * style->padding.x;
  16036. label.y = property.y + style->border + style->padding.y;
  16037. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  16038. /* right increment button */
  16039. right.y = left.y;
  16040. right.w = left.w;
  16041. right.h = left.h;
  16042. right.x = property.x + property.w - (right.w + style->padding.x);
  16043. /* edit */
  16044. if (*state == NK_PROPERTY_EDIT) {
  16045. size = font->width(font->userdata, font->height, buffer, *len);
  16046. size += style->edit.cursor_size;
  16047. length = len;
  16048. dst = buffer;
  16049. } else {
  16050. switch (variant->kind) {
  16051. default: break;
  16052. case NK_PROPERTY_INT:
  16053. nk_itoa(string, variant->value.i);
  16054. num_len = nk_strlen(string);
  16055. break;
  16056. case NK_PROPERTY_FLOAT:
  16057. NK_DTOA(string, (double)variant->value.f);
  16058. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  16059. break;
  16060. case NK_PROPERTY_DOUBLE:
  16061. NK_DTOA(string, variant->value.d);
  16062. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  16063. break;
  16064. }
  16065. size = font->width(font->userdata, font->height, string, num_len);
  16066. dst = string;
  16067. length = &num_len;
  16068. }
  16069. edit.w = (float)size + 2 * style->padding.x;
  16070. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  16071. edit.x = right.x - (edit.w + style->padding.x);
  16072. edit.y = property.y + style->border;
  16073. edit.h = property.h - (2 * style->border);
  16074. /* empty left space activator */
  16075. empty.w = edit.x - (label.x + label.w);
  16076. empty.x = label.x + label.w;
  16077. empty.y = property.y;
  16078. empty.h = property.h;
  16079. /* update property */
  16080. old = (*state == NK_PROPERTY_EDIT);
  16081. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  16082. /* draw property */
  16083. if (style->draw_begin) style->draw_begin(out, style->userdata);
  16084. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  16085. if (style->draw_end) style->draw_end(out, style->userdata);
  16086. /* execute right button */
  16087. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  16088. switch (variant->kind) {
  16089. default: break;
  16090. case NK_PROPERTY_INT:
  16091. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  16092. case NK_PROPERTY_FLOAT:
  16093. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  16094. case NK_PROPERTY_DOUBLE:
  16095. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  16096. }
  16097. }
  16098. /* execute left button */
  16099. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  16100. switch (variant->kind) {
  16101. default: break;
  16102. case NK_PROPERTY_INT:
  16103. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  16104. case NK_PROPERTY_FLOAT:
  16105. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  16106. case NK_PROPERTY_DOUBLE:
  16107. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  16108. }
  16109. }
  16110. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  16111. /* property has been activated so setup buffer */
  16112. NK_MEMCPY(buffer, dst, (nk_size)*length);
  16113. *cursor = nk_utf_len(buffer, *length);
  16114. *len = *length;
  16115. length = len;
  16116. dst = buffer;
  16117. active = 0;
  16118. } else active = (*state == NK_PROPERTY_EDIT);
  16119. /* execute and run text edit field */
  16120. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  16121. text_edit->active = (unsigned char)active;
  16122. text_edit->string.len = *length;
  16123. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  16124. text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
  16125. text_edit->select_end = NK_CLAMP(0,*select_end, *length);
  16126. text_edit->string.buffer.allocated = (nk_size)*length;
  16127. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  16128. text_edit->string.buffer.memory.ptr = dst;
  16129. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  16130. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  16131. nk_do_edit(ws, out, edit, NK_EDIT_FIELD|NK_EDIT_AUTO_SELECT,
  16132. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
  16133. *length = text_edit->string.len;
  16134. *cursor = text_edit->cursor;
  16135. *select_begin = text_edit->select_start;
  16136. *select_end = text_edit->select_end;
  16137. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  16138. text_edit->active = nk_false;
  16139. if (active && !text_edit->active) {
  16140. /* property is now not active so convert edit text to value*/
  16141. *state = NK_PROPERTY_DEFAULT;
  16142. buffer[*len] = '\0';
  16143. switch (variant->kind) {
  16144. default: break;
  16145. case NK_PROPERTY_INT:
  16146. variant->value.i = nk_strtoi(buffer, 0);
  16147. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  16148. break;
  16149. case NK_PROPERTY_FLOAT:
  16150. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  16151. variant->value.f = nk_strtof(buffer, 0);
  16152. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  16153. break;
  16154. case NK_PROPERTY_DOUBLE:
  16155. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  16156. variant->value.d = nk_strtod(buffer, 0);
  16157. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  16158. break;
  16159. }
  16160. }
  16161. }
  16162. /* ===============================================================
  16163. *
  16164. * COLOR PICKER
  16165. *
  16166. * ===============================================================*/
  16167. NK_INTERN int
  16168. nk_color_picker_behavior(nk_flags *state,
  16169. const struct nk_rect *bounds, const struct nk_rect *matrix,
  16170. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  16171. struct nk_colorf *color, const struct nk_input *in)
  16172. {
  16173. float hsva[4];
  16174. int value_changed = 0;
  16175. int hsv_changed = 0;
  16176. NK_ASSERT(state);
  16177. NK_ASSERT(matrix);
  16178. NK_ASSERT(hue_bar);
  16179. NK_ASSERT(color);
  16180. /* color matrix */
  16181. nk_colorf_hsva_fv(hsva, *color);
  16182. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  16183. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
  16184. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
  16185. value_changed = hsv_changed = 1;
  16186. }
  16187. /* hue bar */
  16188. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  16189. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
  16190. value_changed = hsv_changed = 1;
  16191. }
  16192. /* alpha bar */
  16193. if (alpha_bar) {
  16194. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  16195. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
  16196. value_changed = 1;
  16197. }
  16198. }
  16199. nk_widget_state_reset(state);
  16200. if (hsv_changed) {
  16201. *color = nk_hsva_colorfv(hsva);
  16202. *state = NK_WIDGET_STATE_ACTIVE;
  16203. }
  16204. if (value_changed) {
  16205. color->a = hsva[3];
  16206. *state = NK_WIDGET_STATE_ACTIVE;
  16207. }
  16208. /* set color picker widget state */
  16209. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  16210. *state = NK_WIDGET_STATE_HOVERED;
  16211. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  16212. *state |= NK_WIDGET_STATE_ENTERED;
  16213. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  16214. *state |= NK_WIDGET_STATE_LEFT;
  16215. return value_changed;
  16216. }
  16217. NK_INTERN void
  16218. nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
  16219. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  16220. struct nk_colorf col)
  16221. {
  16222. NK_STORAGE const struct nk_color black = {0,0,0,255};
  16223. NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
  16224. NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
  16225. const float crosshair_size = 7.0f;
  16226. struct nk_color temp;
  16227. float hsva[4];
  16228. float line_y;
  16229. int i;
  16230. NK_ASSERT(o);
  16231. NK_ASSERT(matrix);
  16232. NK_ASSERT(hue_bar);
  16233. /* draw hue bar */
  16234. nk_colorf_hsva_fv(hsva, col);
  16235. for (i = 0; i < 6; ++i) {
  16236. NK_GLOBAL const struct nk_color hue_colors[] = {
  16237. {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
  16238. {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
  16239. };
  16240. nk_fill_rect_multi_color(o,
  16241. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
  16242. hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  16243. hue_colors[i+1], hue_colors[i+1]);
  16244. }
  16245. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  16246. nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
  16247. line_y, 1, nk_rgb(255,255,255));
  16248. /* draw alpha bar */
  16249. if (alpha_bar) {
  16250. float alpha = NK_SATURATE(col.a);
  16251. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  16252. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  16253. nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
  16254. line_y, 1, nk_rgb(255,255,255));
  16255. }
  16256. /* draw color matrix */
  16257. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  16258. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  16259. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  16260. /* draw cross-hair */
  16261. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  16262. p.x = (float)(int)(matrix->x + S * matrix->w);
  16263. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  16264. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
  16265. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
  16266. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
  16267. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
  16268. }
  16269. NK_INTERN int
  16270. nk_do_color_picker(nk_flags *state,
  16271. struct nk_command_buffer *out, struct nk_colorf *col,
  16272. enum nk_color_format fmt, struct nk_rect bounds,
  16273. struct nk_vec2 padding, const struct nk_input *in,
  16274. const struct nk_user_font *font)
  16275. {
  16276. int ret = 0;
  16277. struct nk_rect matrix;
  16278. struct nk_rect hue_bar;
  16279. struct nk_rect alpha_bar;
  16280. float bar_w;
  16281. NK_ASSERT(out);
  16282. NK_ASSERT(col);
  16283. NK_ASSERT(state);
  16284. NK_ASSERT(font);
  16285. if (!out || !col || !state || !font)
  16286. return ret;
  16287. bar_w = font->height;
  16288. bounds.x += padding.x;
  16289. bounds.y += padding.x;
  16290. bounds.w -= 2 * padding.x;
  16291. bounds.h -= 2 * padding.y;
  16292. matrix.x = bounds.x;
  16293. matrix.y = bounds.y;
  16294. matrix.h = bounds.h;
  16295. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  16296. hue_bar.w = bar_w;
  16297. hue_bar.y = bounds.y;
  16298. hue_bar.h = matrix.h;
  16299. hue_bar.x = matrix.x + matrix.w + padding.x;
  16300. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  16301. alpha_bar.y = bounds.y;
  16302. alpha_bar.w = bar_w;
  16303. alpha_bar.h = matrix.h;
  16304. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  16305. (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
  16306. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
  16307. return ret;
  16308. }
  16309. /* ==============================================================
  16310. *
  16311. * STYLE
  16312. *
  16313. * ===============================================================*/
  16314. NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
  16315. #define NK_COLOR_MAP(NK_COLOR)\
  16316. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  16317. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  16318. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  16319. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  16320. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  16321. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  16322. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  16323. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  16324. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  16325. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  16326. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  16327. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  16328. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  16329. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  16330. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  16331. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  16332. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  16333. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  16334. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  16335. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  16336. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  16337. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  16338. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT,255, 0, 0, 255) \
  16339. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  16340. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  16341. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER,120,120,120,255) \
  16342. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,150,150,150,255) \
  16343. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
  16344. NK_GLOBAL const struct nk_color
  16345. nk_default_color_style[NK_COLOR_COUNT] = {
  16346. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  16347. NK_COLOR_MAP(NK_COLOR)
  16348. #undef NK_COLOR
  16349. };
  16350. NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
  16351. #define NK_COLOR(a,b,c,d,e) #a,
  16352. NK_COLOR_MAP(NK_COLOR)
  16353. #undef NK_COLOR
  16354. };
  16355. NK_API const char *nk_style_get_color_by_name(enum nk_style_colors c)
  16356. {return nk_color_names[c];}
  16357. NK_API struct nk_style_item nk_style_item_image(struct nk_image img)
  16358. {struct nk_style_item i; i.type = NK_STYLE_ITEM_IMAGE; i.data.image = img; return i;}
  16359. NK_API struct nk_style_item nk_style_item_color(struct nk_color col)
  16360. {struct nk_style_item i; i.type = NK_STYLE_ITEM_COLOR; i.data.color = col; return i;}
  16361. NK_API struct nk_style_item nk_style_item_hide(void)
  16362. {struct nk_style_item i; i.type = NK_STYLE_ITEM_COLOR; i.data.color = nk_rgba(0,0,0,0); return i;}
  16363. NK_API void
  16364. nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
  16365. {
  16366. struct nk_style *style;
  16367. struct nk_style_text *text;
  16368. struct nk_style_button *button;
  16369. struct nk_style_toggle *toggle;
  16370. struct nk_style_selectable *select;
  16371. struct nk_style_slider *slider;
  16372. struct nk_style_progress *prog;
  16373. struct nk_style_scrollbar *scroll;
  16374. struct nk_style_edit *edit;
  16375. struct nk_style_property *property;
  16376. struct nk_style_combo *combo;
  16377. struct nk_style_chart *chart;
  16378. struct nk_style_tab *tab;
  16379. struct nk_style_window *win;
  16380. NK_ASSERT(ctx);
  16381. if (!ctx) return;
  16382. style = &ctx->style;
  16383. table = (!table) ? nk_default_color_style: table;
  16384. /* default text */
  16385. text = &style->text;
  16386. text->color = table[NK_COLOR_TEXT];
  16387. text->padding = nk_vec2(0,0);
  16388. /* default button */
  16389. button = &style->button;
  16390. nk_zero_struct(*button);
  16391. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  16392. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  16393. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  16394. button->border_color = table[NK_COLOR_BORDER];
  16395. button->text_background = table[NK_COLOR_BUTTON];
  16396. button->text_normal = table[NK_COLOR_TEXT];
  16397. button->text_hover = table[NK_COLOR_TEXT];
  16398. button->text_active = table[NK_COLOR_TEXT];
  16399. button->padding = nk_vec2(2.0f,2.0f);
  16400. button->image_padding = nk_vec2(0.0f,0.0f);
  16401. button->touch_padding = nk_vec2(0.0f, 0.0f);
  16402. button->userdata = nk_handle_ptr(0);
  16403. button->text_alignment = NK_TEXT_CENTERED;
  16404. button->border = 1.0f;
  16405. button->rounding = 4.0f;
  16406. button->draw_begin = 0;
  16407. button->draw_end = 0;
  16408. /* contextual button */
  16409. button = &style->contextual_button;
  16410. nk_zero_struct(*button);
  16411. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16412. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  16413. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  16414. button->border_color = table[NK_COLOR_WINDOW];
  16415. button->text_background = table[NK_COLOR_WINDOW];
  16416. button->text_normal = table[NK_COLOR_TEXT];
  16417. button->text_hover = table[NK_COLOR_TEXT];
  16418. button->text_active = table[NK_COLOR_TEXT];
  16419. button->padding = nk_vec2(2.0f,2.0f);
  16420. button->touch_padding = nk_vec2(0.0f,0.0f);
  16421. button->userdata = nk_handle_ptr(0);
  16422. button->text_alignment = NK_TEXT_CENTERED;
  16423. button->border = 0.0f;
  16424. button->rounding = 0.0f;
  16425. button->draw_begin = 0;
  16426. button->draw_end = 0;
  16427. /* menu button */
  16428. button = &style->menu_button;
  16429. nk_zero_struct(*button);
  16430. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16431. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16432. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16433. button->border_color = table[NK_COLOR_WINDOW];
  16434. button->text_background = table[NK_COLOR_WINDOW];
  16435. button->text_normal = table[NK_COLOR_TEXT];
  16436. button->text_hover = table[NK_COLOR_TEXT];
  16437. button->text_active = table[NK_COLOR_TEXT];
  16438. button->padding = nk_vec2(2.0f,2.0f);
  16439. button->touch_padding = nk_vec2(0.0f,0.0f);
  16440. button->userdata = nk_handle_ptr(0);
  16441. button->text_alignment = NK_TEXT_CENTERED;
  16442. button->border = 0.0f;
  16443. button->rounding = 1.0f;
  16444. button->draw_begin = 0;
  16445. button->draw_end = 0;
  16446. /* checkbox toggle */
  16447. toggle = &style->checkbox;
  16448. nk_zero_struct(*toggle);
  16449. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  16450. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  16451. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  16452. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  16453. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  16454. toggle->userdata = nk_handle_ptr(0);
  16455. toggle->text_background = table[NK_COLOR_WINDOW];
  16456. toggle->text_normal = table[NK_COLOR_TEXT];
  16457. toggle->text_hover = table[NK_COLOR_TEXT];
  16458. toggle->text_active = table[NK_COLOR_TEXT];
  16459. toggle->padding = nk_vec2(2.0f, 2.0f);
  16460. toggle->touch_padding = nk_vec2(0,0);
  16461. toggle->border_color = nk_rgba(0,0,0,0);
  16462. toggle->border = 0.0f;
  16463. toggle->spacing = 4;
  16464. /* option toggle */
  16465. toggle = &style->option;
  16466. nk_zero_struct(*toggle);
  16467. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  16468. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  16469. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  16470. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  16471. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  16472. toggle->userdata = nk_handle_ptr(0);
  16473. toggle->text_background = table[NK_COLOR_WINDOW];
  16474. toggle->text_normal = table[NK_COLOR_TEXT];
  16475. toggle->text_hover = table[NK_COLOR_TEXT];
  16476. toggle->text_active = table[NK_COLOR_TEXT];
  16477. toggle->padding = nk_vec2(3.0f, 3.0f);
  16478. toggle->touch_padding = nk_vec2(0,0);
  16479. toggle->border_color = nk_rgba(0,0,0,0);
  16480. toggle->border = 0.0f;
  16481. toggle->spacing = 4;
  16482. /* selectable */
  16483. select = &style->selectable;
  16484. nk_zero_struct(*select);
  16485. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  16486. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  16487. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  16488. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  16489. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  16490. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  16491. select->text_normal = table[NK_COLOR_TEXT];
  16492. select->text_hover = table[NK_COLOR_TEXT];
  16493. select->text_pressed = table[NK_COLOR_TEXT];
  16494. select->text_normal_active = table[NK_COLOR_TEXT];
  16495. select->text_hover_active = table[NK_COLOR_TEXT];
  16496. select->text_pressed_active = table[NK_COLOR_TEXT];
  16497. select->padding = nk_vec2(2.0f,2.0f);
  16498. select->touch_padding = nk_vec2(0,0);
  16499. select->userdata = nk_handle_ptr(0);
  16500. select->rounding = 0.0f;
  16501. select->draw_begin = 0;
  16502. select->draw_end = 0;
  16503. /* slider */
  16504. slider = &style->slider;
  16505. nk_zero_struct(*slider);
  16506. slider->normal = nk_style_item_hide();
  16507. slider->hover = nk_style_item_hide();
  16508. slider->active = nk_style_item_hide();
  16509. slider->bar_normal = table[NK_COLOR_SLIDER];
  16510. slider->bar_hover = table[NK_COLOR_SLIDER];
  16511. slider->bar_active = table[NK_COLOR_SLIDER];
  16512. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  16513. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  16514. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  16515. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  16516. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  16517. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  16518. slider->cursor_size = nk_vec2(16,16);
  16519. slider->padding = nk_vec2(2,2);
  16520. slider->spacing = nk_vec2(2,2);
  16521. slider->userdata = nk_handle_ptr(0);
  16522. slider->show_buttons = nk_false;
  16523. slider->bar_height = 8;
  16524. slider->rounding = 0;
  16525. slider->draw_begin = 0;
  16526. slider->draw_end = 0;
  16527. /* slider buttons */
  16528. button = &style->slider.inc_button;
  16529. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  16530. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  16531. button->active = nk_style_item_color(nk_rgb(44,44,44));
  16532. button->border_color = nk_rgb(65,65,65);
  16533. button->text_background = nk_rgb(40,40,40);
  16534. button->text_normal = nk_rgb(175,175,175);
  16535. button->text_hover = nk_rgb(175,175,175);
  16536. button->text_active = nk_rgb(175,175,175);
  16537. button->padding = nk_vec2(8.0f,8.0f);
  16538. button->touch_padding = nk_vec2(0.0f,0.0f);
  16539. button->userdata = nk_handle_ptr(0);
  16540. button->text_alignment = NK_TEXT_CENTERED;
  16541. button->border = 1.0f;
  16542. button->rounding = 0.0f;
  16543. button->draw_begin = 0;
  16544. button->draw_end = 0;
  16545. style->slider.dec_button = style->slider.inc_button;
  16546. /* progressbar */
  16547. prog = &style->progress;
  16548. nk_zero_struct(*prog);
  16549. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  16550. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  16551. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  16552. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  16553. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  16554. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  16555. prog->border_color = nk_rgba(0,0,0,0);
  16556. prog->cursor_border_color = nk_rgba(0,0,0,0);
  16557. prog->userdata = nk_handle_ptr(0);
  16558. prog->padding = nk_vec2(4,4);
  16559. prog->rounding = 0;
  16560. prog->border = 0;
  16561. prog->cursor_rounding = 0;
  16562. prog->cursor_border = 0;
  16563. prog->draw_begin = 0;
  16564. prog->draw_end = 0;
  16565. /* scrollbars */
  16566. scroll = &style->scrollh;
  16567. nk_zero_struct(*scroll);
  16568. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  16569. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  16570. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  16571. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  16572. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  16573. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  16574. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  16575. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  16576. scroll->userdata = nk_handle_ptr(0);
  16577. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  16578. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  16579. scroll->padding = nk_vec2(0,0);
  16580. scroll->show_buttons = nk_false;
  16581. scroll->border = 0;
  16582. scroll->rounding = 0;
  16583. scroll->border_cursor = 0;
  16584. scroll->rounding_cursor = 0;
  16585. scroll->draw_begin = 0;
  16586. scroll->draw_end = 0;
  16587. style->scrollv = style->scrollh;
  16588. /* scrollbars buttons */
  16589. button = &style->scrollh.inc_button;
  16590. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  16591. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  16592. button->active = nk_style_item_color(nk_rgb(44,44,44));
  16593. button->border_color = nk_rgb(65,65,65);
  16594. button->text_background = nk_rgb(40,40,40);
  16595. button->text_normal = nk_rgb(175,175,175);
  16596. button->text_hover = nk_rgb(175,175,175);
  16597. button->text_active = nk_rgb(175,175,175);
  16598. button->padding = nk_vec2(4.0f,4.0f);
  16599. button->touch_padding = nk_vec2(0.0f,0.0f);
  16600. button->userdata = nk_handle_ptr(0);
  16601. button->text_alignment = NK_TEXT_CENTERED;
  16602. button->border = 1.0f;
  16603. button->rounding = 0.0f;
  16604. button->draw_begin = 0;
  16605. button->draw_end = 0;
  16606. style->scrollh.dec_button = style->scrollh.inc_button;
  16607. style->scrollv.inc_button = style->scrollh.inc_button;
  16608. style->scrollv.dec_button = style->scrollh.inc_button;
  16609. /* edit */
  16610. edit = &style->edit;
  16611. nk_zero_struct(*edit);
  16612. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  16613. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  16614. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  16615. edit->cursor_normal = table[NK_COLOR_TEXT];
  16616. edit->cursor_hover = table[NK_COLOR_TEXT];
  16617. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  16618. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  16619. edit->border_color = table[NK_COLOR_BORDER];
  16620. edit->text_normal = table[NK_COLOR_TEXT];
  16621. edit->text_hover = table[NK_COLOR_TEXT];
  16622. edit->text_active = table[NK_COLOR_TEXT];
  16623. edit->selected_normal = table[NK_COLOR_TEXT];
  16624. edit->selected_hover = table[NK_COLOR_TEXT];
  16625. edit->selected_text_normal = table[NK_COLOR_EDIT];
  16626. edit->selected_text_hover = table[NK_COLOR_EDIT];
  16627. edit->scrollbar_size = nk_vec2(10,10);
  16628. edit->scrollbar = style->scrollv;
  16629. edit->padding = nk_vec2(4,4);
  16630. edit->row_padding = 2;
  16631. edit->cursor_size = 4;
  16632. edit->border = 1;
  16633. edit->rounding = 0;
  16634. /* property */
  16635. property = &style->property;
  16636. nk_zero_struct(*property);
  16637. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16638. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16639. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16640. property->border_color = table[NK_COLOR_BORDER];
  16641. property->label_normal = table[NK_COLOR_TEXT];
  16642. property->label_hover = table[NK_COLOR_TEXT];
  16643. property->label_active = table[NK_COLOR_TEXT];
  16644. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  16645. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  16646. property->userdata = nk_handle_ptr(0);
  16647. property->padding = nk_vec2(4,4);
  16648. property->border = 1;
  16649. property->rounding = 10;
  16650. property->draw_begin = 0;
  16651. property->draw_end = 0;
  16652. /* property buttons */
  16653. button = &style->property.dec_button;
  16654. nk_zero_struct(*button);
  16655. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16656. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16657. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16658. button->border_color = nk_rgba(0,0,0,0);
  16659. button->text_background = table[NK_COLOR_PROPERTY];
  16660. button->text_normal = table[NK_COLOR_TEXT];
  16661. button->text_hover = table[NK_COLOR_TEXT];
  16662. button->text_active = table[NK_COLOR_TEXT];
  16663. button->padding = nk_vec2(0.0f,0.0f);
  16664. button->touch_padding = nk_vec2(0.0f,0.0f);
  16665. button->userdata = nk_handle_ptr(0);
  16666. button->text_alignment = NK_TEXT_CENTERED;
  16667. button->border = 0.0f;
  16668. button->rounding = 0.0f;
  16669. button->draw_begin = 0;
  16670. button->draw_end = 0;
  16671. style->property.inc_button = style->property.dec_button;
  16672. /* property edit */
  16673. edit = &style->property.edit;
  16674. nk_zero_struct(*edit);
  16675. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16676. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16677. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  16678. edit->border_color = nk_rgba(0,0,0,0);
  16679. edit->cursor_normal = table[NK_COLOR_TEXT];
  16680. edit->cursor_hover = table[NK_COLOR_TEXT];
  16681. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  16682. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  16683. edit->text_normal = table[NK_COLOR_TEXT];
  16684. edit->text_hover = table[NK_COLOR_TEXT];
  16685. edit->text_active = table[NK_COLOR_TEXT];
  16686. edit->selected_normal = table[NK_COLOR_TEXT];
  16687. edit->selected_hover = table[NK_COLOR_TEXT];
  16688. edit->selected_text_normal = table[NK_COLOR_EDIT];
  16689. edit->selected_text_hover = table[NK_COLOR_EDIT];
  16690. edit->padding = nk_vec2(0,0);
  16691. edit->cursor_size = 8;
  16692. edit->border = 0;
  16693. edit->rounding = 0;
  16694. /* chart */
  16695. chart = &style->chart;
  16696. nk_zero_struct(*chart);
  16697. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  16698. chart->border_color = table[NK_COLOR_BORDER];
  16699. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  16700. chart->color = table[NK_COLOR_CHART_COLOR];
  16701. chart->padding = nk_vec2(4,4);
  16702. chart->border = 0;
  16703. chart->rounding = 0;
  16704. /* combo */
  16705. combo = &style->combo;
  16706. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  16707. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  16708. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  16709. combo->border_color = table[NK_COLOR_BORDER];
  16710. combo->label_normal = table[NK_COLOR_TEXT];
  16711. combo->label_hover = table[NK_COLOR_TEXT];
  16712. combo->label_active = table[NK_COLOR_TEXT];
  16713. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  16714. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  16715. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  16716. combo->content_padding = nk_vec2(4,4);
  16717. combo->button_padding = nk_vec2(0,4);
  16718. combo->spacing = nk_vec2(4,0);
  16719. combo->border = 1;
  16720. combo->rounding = 0;
  16721. /* combo button */
  16722. button = &style->combo.button;
  16723. nk_zero_struct(*button);
  16724. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  16725. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  16726. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  16727. button->border_color = nk_rgba(0,0,0,0);
  16728. button->text_background = table[NK_COLOR_COMBO];
  16729. button->text_normal = table[NK_COLOR_TEXT];
  16730. button->text_hover = table[NK_COLOR_TEXT];
  16731. button->text_active = table[NK_COLOR_TEXT];
  16732. button->padding = nk_vec2(2.0f,2.0f);
  16733. button->touch_padding = nk_vec2(0.0f,0.0f);
  16734. button->userdata = nk_handle_ptr(0);
  16735. button->text_alignment = NK_TEXT_CENTERED;
  16736. button->border = 0.0f;
  16737. button->rounding = 0.0f;
  16738. button->draw_begin = 0;
  16739. button->draw_end = 0;
  16740. /* tab */
  16741. tab = &style->tab;
  16742. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  16743. tab->border_color = table[NK_COLOR_BORDER];
  16744. tab->text = table[NK_COLOR_TEXT];
  16745. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  16746. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  16747. tab->padding = nk_vec2(4,4);
  16748. tab->spacing = nk_vec2(4,4);
  16749. tab->indent = 10.0f;
  16750. tab->border = 1;
  16751. tab->rounding = 0;
  16752. /* tab button */
  16753. button = &style->tab.tab_minimize_button;
  16754. nk_zero_struct(*button);
  16755. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  16756. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  16757. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  16758. button->border_color = nk_rgba(0,0,0,0);
  16759. button->text_background = table[NK_COLOR_TAB_HEADER];
  16760. button->text_normal = table[NK_COLOR_TEXT];
  16761. button->text_hover = table[NK_COLOR_TEXT];
  16762. button->text_active = table[NK_COLOR_TEXT];
  16763. button->padding = nk_vec2(2.0f,2.0f);
  16764. button->touch_padding = nk_vec2(0.0f,0.0f);
  16765. button->userdata = nk_handle_ptr(0);
  16766. button->text_alignment = NK_TEXT_CENTERED;
  16767. button->border = 0.0f;
  16768. button->rounding = 0.0f;
  16769. button->draw_begin = 0;
  16770. button->draw_end = 0;
  16771. style->tab.tab_maximize_button =*button;
  16772. /* node button */
  16773. button = &style->tab.node_minimize_button;
  16774. nk_zero_struct(*button);
  16775. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16776. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16777. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16778. button->border_color = nk_rgba(0,0,0,0);
  16779. button->text_background = table[NK_COLOR_TAB_HEADER];
  16780. button->text_normal = table[NK_COLOR_TEXT];
  16781. button->text_hover = table[NK_COLOR_TEXT];
  16782. button->text_active = table[NK_COLOR_TEXT];
  16783. button->padding = nk_vec2(2.0f,2.0f);
  16784. button->touch_padding = nk_vec2(0.0f,0.0f);
  16785. button->userdata = nk_handle_ptr(0);
  16786. button->text_alignment = NK_TEXT_CENTERED;
  16787. button->border = 0.0f;
  16788. button->rounding = 0.0f;
  16789. button->draw_begin = 0;
  16790. button->draw_end = 0;
  16791. style->tab.node_maximize_button =*button;
  16792. /* window header */
  16793. win = &style->window;
  16794. win->header.align = NK_HEADER_RIGHT;
  16795. win->header.close_symbol = NK_SYMBOL_X;
  16796. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  16797. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  16798. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  16799. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  16800. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  16801. win->header.label_normal = table[NK_COLOR_TEXT];
  16802. win->header.label_hover = table[NK_COLOR_TEXT];
  16803. win->header.label_active = table[NK_COLOR_TEXT];
  16804. win->header.label_padding = nk_vec2(4,4);
  16805. win->header.padding = nk_vec2(4,4);
  16806. win->header.spacing = nk_vec2(0,0);
  16807. /* window header close button */
  16808. button = &style->window.header.close_button;
  16809. nk_zero_struct(*button);
  16810. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  16811. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  16812. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  16813. button->border_color = nk_rgba(0,0,0,0);
  16814. button->text_background = table[NK_COLOR_HEADER];
  16815. button->text_normal = table[NK_COLOR_TEXT];
  16816. button->text_hover = table[NK_COLOR_TEXT];
  16817. button->text_active = table[NK_COLOR_TEXT];
  16818. button->padding = nk_vec2(0.0f,0.0f);
  16819. button->touch_padding = nk_vec2(0.0f,0.0f);
  16820. button->userdata = nk_handle_ptr(0);
  16821. button->text_alignment = NK_TEXT_CENTERED;
  16822. button->border = 0.0f;
  16823. button->rounding = 0.0f;
  16824. button->draw_begin = 0;
  16825. button->draw_end = 0;
  16826. /* window header minimize button */
  16827. button = &style->window.header.minimize_button;
  16828. nk_zero_struct(*button);
  16829. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  16830. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  16831. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  16832. button->border_color = nk_rgba(0,0,0,0);
  16833. button->text_background = table[NK_COLOR_HEADER];
  16834. button->text_normal = table[NK_COLOR_TEXT];
  16835. button->text_hover = table[NK_COLOR_TEXT];
  16836. button->text_active = table[NK_COLOR_TEXT];
  16837. button->padding = nk_vec2(0.0f,0.0f);
  16838. button->touch_padding = nk_vec2(0.0f,0.0f);
  16839. button->userdata = nk_handle_ptr(0);
  16840. button->text_alignment = NK_TEXT_CENTERED;
  16841. button->border = 0.0f;
  16842. button->rounding = 0.0f;
  16843. button->draw_begin = 0;
  16844. button->draw_end = 0;
  16845. /* window */
  16846. win->background = table[NK_COLOR_WINDOW];
  16847. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  16848. win->border_color = table[NK_COLOR_BORDER];
  16849. win->popup_border_color = table[NK_COLOR_BORDER];
  16850. win->combo_border_color = table[NK_COLOR_BORDER];
  16851. win->contextual_border_color = table[NK_COLOR_BORDER];
  16852. win->menu_border_color = table[NK_COLOR_BORDER];
  16853. win->group_border_color = table[NK_COLOR_BORDER];
  16854. win->tooltip_border_color = table[NK_COLOR_BORDER];
  16855. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  16856. win->rounding = 0.0f;
  16857. win->spacing = nk_vec2(4,4);
  16858. win->scrollbar_size = nk_vec2(10,10);
  16859. win->min_size = nk_vec2(64,64);
  16860. win->combo_border = 1.0f;
  16861. win->contextual_border = 1.0f;
  16862. win->menu_border = 1.0f;
  16863. win->group_border = 1.0f;
  16864. win->tooltip_border = 1.0f;
  16865. win->popup_border = 1.0f;
  16866. win->border = 2.0f;
  16867. win->min_row_height_padding = 8;
  16868. win->padding = nk_vec2(4,4);
  16869. win->group_padding = nk_vec2(4,4);
  16870. win->popup_padding = nk_vec2(4,4);
  16871. win->combo_padding = nk_vec2(4,4);
  16872. win->contextual_padding = nk_vec2(4,4);
  16873. win->menu_padding = nk_vec2(4,4);
  16874. win->tooltip_padding = nk_vec2(4,4);
  16875. }
  16876. NK_API void
  16877. nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
  16878. {
  16879. struct nk_style *style;
  16880. NK_ASSERT(ctx);
  16881. if (!ctx) return;
  16882. style = &ctx->style;
  16883. style->font = font;
  16884. ctx->stacks.fonts.head = 0;
  16885. if (ctx->current)
  16886. nk_layout_reset_min_row_height(ctx);
  16887. }
  16888. NK_API int
  16889. nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
  16890. {
  16891. struct nk_config_stack_user_font *font_stack;
  16892. struct nk_config_stack_user_font_element *element;
  16893. NK_ASSERT(ctx);
  16894. if (!ctx) return 0;
  16895. font_stack = &ctx->stacks.fonts;
  16896. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  16897. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  16898. return 0;
  16899. element = &font_stack->elements[font_stack->head++];
  16900. element->address = &ctx->style.font;
  16901. element->old_value = ctx->style.font;
  16902. ctx->style.font = font;
  16903. return 1;
  16904. }
  16905. NK_API int
  16906. nk_style_pop_font(struct nk_context *ctx)
  16907. {
  16908. struct nk_config_stack_user_font *font_stack;
  16909. struct nk_config_stack_user_font_element *element;
  16910. NK_ASSERT(ctx);
  16911. if (!ctx) return 0;
  16912. font_stack = &ctx->stacks.fonts;
  16913. NK_ASSERT(font_stack->head > 0);
  16914. if (font_stack->head < 1)
  16915. return 0;
  16916. element = &font_stack->elements[--font_stack->head];
  16917. *element->address = element->old_value;
  16918. return 1;
  16919. }
  16920. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  16921. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  16922. {\
  16923. struct nk_config_stack_##type * type_stack;\
  16924. struct nk_config_stack_##type##_element *element;\
  16925. NK_ASSERT(ctx);\
  16926. if (!ctx) return 0;\
  16927. type_stack = &ctx->stacks.stack;\
  16928. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  16929. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  16930. return 0;\
  16931. element = &type_stack->elements[type_stack->head++];\
  16932. element->address = address;\
  16933. element->old_value = *address;\
  16934. *address = value;\
  16935. return 1;\
  16936. }
  16937. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  16938. nk_style_pop_##type(struct nk_context *ctx)\
  16939. {\
  16940. struct nk_config_stack_##type *type_stack;\
  16941. struct nk_config_stack_##type##_element *element;\
  16942. NK_ASSERT(ctx);\
  16943. if (!ctx) return 0;\
  16944. type_stack = &ctx->stacks.stack;\
  16945. NK_ASSERT(type_stack->head > 0);\
  16946. if (type_stack->head < 1)\
  16947. return 0;\
  16948. element = &type_stack->elements[--type_stack->head];\
  16949. *element->address = element->old_value;\
  16950. return 1;\
  16951. }
  16952. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  16953. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
  16954. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  16955. NK_API int NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
  16956. NK_API int NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
  16957. NK_API int NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  16958. NK_API int NK_STYLE_POP_IMPLEMENATION(float,floats)
  16959. NK_API int NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  16960. NK_API int NK_STYLE_POP_IMPLEMENATION(flags,flags)
  16961. NK_API int NK_STYLE_POP_IMPLEMENATION(color,colors)
  16962. NK_API int
  16963. nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
  16964. {
  16965. struct nk_style *style;
  16966. NK_ASSERT(ctx);
  16967. if (!ctx) return 0;
  16968. style = &ctx->style;
  16969. if (style->cursors[c]) {
  16970. style->cursor_active = style->cursors[c];
  16971. return 1;
  16972. }
  16973. return 0;
  16974. }
  16975. NK_API void
  16976. nk_style_show_cursor(struct nk_context *ctx)
  16977. {
  16978. ctx->style.cursor_visible = nk_true;
  16979. }
  16980. NK_API void
  16981. nk_style_hide_cursor(struct nk_context *ctx)
  16982. {
  16983. ctx->style.cursor_visible = nk_false;
  16984. }
  16985. NK_API void
  16986. nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
  16987. const struct nk_cursor *c)
  16988. {
  16989. struct nk_style *style;
  16990. NK_ASSERT(ctx);
  16991. if (!ctx) return;
  16992. style = &ctx->style;
  16993. style->cursors[cursor] = c;
  16994. }
  16995. NK_API void
  16996. nk_style_load_all_cursors(struct nk_context *ctx, struct nk_cursor *cursors)
  16997. {
  16998. int i = 0;
  16999. struct nk_style *style;
  17000. NK_ASSERT(ctx);
  17001. if (!ctx) return;
  17002. style = &ctx->style;
  17003. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  17004. style->cursors[i] = &cursors[i];
  17005. style->cursor_visible = nk_true;
  17006. }
  17007. /* ===============================================================
  17008. *
  17009. * POOL
  17010. *
  17011. * ===============================================================*/
  17012. NK_INTERN void
  17013. nk_pool_init(struct nk_pool *pool, struct nk_allocator *alloc,
  17014. unsigned int capacity)
  17015. {
  17016. nk_zero(pool, sizeof(*pool));
  17017. pool->alloc = *alloc;
  17018. pool->capacity = capacity;
  17019. pool->type = NK_BUFFER_DYNAMIC;
  17020. pool->pages = 0;
  17021. }
  17022. NK_INTERN void
  17023. nk_pool_free(struct nk_pool *pool)
  17024. {
  17025. struct nk_page *iter = pool->pages;
  17026. if (!pool) return;
  17027. if (pool->type == NK_BUFFER_FIXED) return;
  17028. while (iter) {
  17029. struct nk_page *next = iter->next;
  17030. pool->alloc.free(pool->alloc.userdata, iter);
  17031. iter = next;
  17032. }
  17033. }
  17034. NK_INTERN void
  17035. nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
  17036. {
  17037. nk_zero(pool, sizeof(*pool));
  17038. NK_ASSERT(size >= sizeof(struct nk_page));
  17039. if (size < sizeof(struct nk_page)) return;
  17040. pool->capacity = (unsigned)(size - sizeof(struct nk_page)) / sizeof(struct nk_page_element);
  17041. pool->pages = (struct nk_page*)memory;
  17042. pool->type = NK_BUFFER_FIXED;
  17043. pool->size = size;
  17044. }
  17045. NK_INTERN struct nk_page_element*
  17046. nk_pool_alloc(struct nk_pool *pool)
  17047. {
  17048. if (!pool->pages || pool->pages->size >= pool->capacity) {
  17049. /* allocate new page */
  17050. struct nk_page *page;
  17051. if (pool->type == NK_BUFFER_FIXED) {
  17052. if (!pool->pages) {
  17053. NK_ASSERT(pool->pages);
  17054. return 0;
  17055. }
  17056. NK_ASSERT(pool->pages->size < pool->capacity);
  17057. return 0;
  17058. } else {
  17059. nk_size size = sizeof(struct nk_page);
  17060. size += NK_POOL_DEFAULT_CAPACITY * sizeof(union nk_page_data);
  17061. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
  17062. page->next = pool->pages;
  17063. pool->pages = page;
  17064. page->size = 0;
  17065. }
  17066. }
  17067. return &pool->pages->win[pool->pages->size++];
  17068. }
  17069. /* ===============================================================
  17070. *
  17071. * CONTEXT
  17072. *
  17073. * ===============================================================*/
  17074. NK_INTERN void* nk_create_window(struct nk_context *ctx);
  17075. NK_INTERN void nk_remove_window(struct nk_context*, struct nk_window*);
  17076. NK_INTERN void nk_free_window(struct nk_context *ctx, struct nk_window *win);
  17077. NK_INTERN void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
  17078. NK_INTERN void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
  17079. NK_INTERN void* nk_create_panel(struct nk_context *ctx);
  17080. NK_INTERN void nk_free_panel(struct nk_context*, struct nk_panel *pan);
  17081. NK_INTERN void
  17082. nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
  17083. {
  17084. NK_ASSERT(ctx);
  17085. if (!ctx) return;
  17086. nk_zero_struct(*ctx);
  17087. nk_style_default(ctx);
  17088. ctx->seq = 1;
  17089. if (font) ctx->style.font = font;
  17090. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17091. nk_draw_list_init(&ctx->draw_list);
  17092. #endif
  17093. }
  17094. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  17095. NK_API int
  17096. nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
  17097. {
  17098. struct nk_allocator alloc;
  17099. alloc.userdata.ptr = 0;
  17100. alloc.alloc = nk_malloc;
  17101. alloc.free = nk_mfree;
  17102. return nk_init(ctx, &alloc, font);
  17103. }
  17104. #endif
  17105. NK_API int
  17106. nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
  17107. const struct nk_user_font *font)
  17108. {
  17109. NK_ASSERT(memory);
  17110. if (!memory) return 0;
  17111. nk_setup(ctx, font);
  17112. nk_buffer_init_fixed(&ctx->memory, memory, size);
  17113. ctx->use_pool = nk_false;
  17114. return 1;
  17115. }
  17116. NK_API int
  17117. nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
  17118. struct nk_buffer *pool, const struct nk_user_font *font)
  17119. {
  17120. NK_ASSERT(cmds);
  17121. NK_ASSERT(pool);
  17122. if (!cmds || !pool) return 0;
  17123. nk_setup(ctx, font);
  17124. ctx->memory = *cmds;
  17125. if (pool->type == NK_BUFFER_FIXED) {
  17126. /* take memory from buffer and alloc fixed pool */
  17127. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  17128. } else {
  17129. /* create dynamic pool from buffer allocator */
  17130. struct nk_allocator *alloc = &pool->pool;
  17131. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17132. }
  17133. ctx->use_pool = nk_true;
  17134. return 1;
  17135. }
  17136. NK_API int
  17137. nk_init(struct nk_context *ctx, struct nk_allocator *alloc,
  17138. const struct nk_user_font *font)
  17139. {
  17140. NK_ASSERT(alloc);
  17141. if (!alloc) return 0;
  17142. nk_setup(ctx, font);
  17143. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  17144. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17145. ctx->use_pool = nk_true;
  17146. return 1;
  17147. }
  17148. #ifdef NK_INCLUDE_COMMAND_USERDATA
  17149. NK_API void
  17150. nk_set_user_data(struct nk_context *ctx, nk_handle handle)
  17151. {
  17152. if (!ctx) return;
  17153. ctx->userdata = handle;
  17154. if (ctx->current)
  17155. ctx->current->buffer.userdata = handle;
  17156. }
  17157. #endif
  17158. NK_API void
  17159. nk_free(struct nk_context *ctx)
  17160. {
  17161. NK_ASSERT(ctx);
  17162. if (!ctx) return;
  17163. nk_buffer_free(&ctx->memory);
  17164. if (ctx->use_pool)
  17165. nk_pool_free(&ctx->pool);
  17166. nk_zero(&ctx->input, sizeof(ctx->input));
  17167. nk_zero(&ctx->style, sizeof(ctx->style));
  17168. nk_zero(&ctx->memory, sizeof(ctx->memory));
  17169. ctx->seq = 0;
  17170. ctx->build = 0;
  17171. ctx->begin = 0;
  17172. ctx->end = 0;
  17173. ctx->active = 0;
  17174. ctx->current = 0;
  17175. ctx->freelist = 0;
  17176. ctx->count = 0;
  17177. }
  17178. NK_API void
  17179. nk_clear(struct nk_context *ctx)
  17180. {
  17181. struct nk_window *iter;
  17182. struct nk_window *next;
  17183. NK_ASSERT(ctx);
  17184. if (!ctx) return;
  17185. if (ctx->use_pool)
  17186. nk_buffer_clear(&ctx->memory);
  17187. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  17188. ctx->build = 0;
  17189. ctx->memory.calls = 0;
  17190. ctx->last_widget_state = 0;
  17191. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17192. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  17193. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17194. nk_draw_list_clear(&ctx->draw_list);
  17195. #endif
  17196. /* garbage collector */
  17197. iter = ctx->begin;
  17198. while (iter) {
  17199. /* make sure valid minimized windows do not get removed */
  17200. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  17201. !(iter->flags & NK_WINDOW_CLOSED) &&
  17202. iter->seq == ctx->seq) {
  17203. iter = iter->next;
  17204. continue;
  17205. }
  17206. /* remove hotness from hidden or closed windows*/
  17207. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  17208. (iter->flags & NK_WINDOW_CLOSED)) &&
  17209. iter == ctx->active) {
  17210. ctx->active = iter->prev;
  17211. ctx->end = iter->prev;
  17212. if (!ctx->end)
  17213. ctx->begin = 0;
  17214. if (ctx->active)
  17215. ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
  17216. }
  17217. /* free unused popup windows */
  17218. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  17219. nk_free_window(ctx, iter->popup.win);
  17220. iter->popup.win = 0;
  17221. }
  17222. /* remove unused window state tables */
  17223. {struct nk_table *n, *it = iter->tables;
  17224. while (it) {
  17225. n = it->next;
  17226. if (it->seq != ctx->seq) {
  17227. nk_remove_table(iter, it);
  17228. nk_zero(it, sizeof(union nk_page_data));
  17229. nk_free_table(ctx, it);
  17230. if (it == iter->tables)
  17231. iter->tables = n;
  17232. } it = n;
  17233. }}
  17234. /* window itself is not used anymore so free */
  17235. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  17236. next = iter->next;
  17237. nk_remove_window(ctx, iter);
  17238. nk_free_window(ctx, iter);
  17239. iter = next;
  17240. } else iter = iter->next;
  17241. }
  17242. ctx->seq++;
  17243. }
  17244. /* ----------------------------------------------------------------
  17245. *
  17246. * BUFFERING
  17247. *
  17248. * ---------------------------------------------------------------*/
  17249. NK_INTERN void
  17250. nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  17251. {
  17252. NK_ASSERT(ctx);
  17253. NK_ASSERT(buffer);
  17254. if (!ctx || !buffer) return;
  17255. buffer->begin = ctx->memory.allocated;
  17256. buffer->end = buffer->begin;
  17257. buffer->last = buffer->begin;
  17258. buffer->clip = nk_null_rect;
  17259. }
  17260. NK_INTERN void
  17261. nk_start(struct nk_context *ctx, struct nk_window *win)
  17262. {
  17263. NK_ASSERT(ctx);
  17264. NK_ASSERT(win);
  17265. nk_start_buffer(ctx, &win->buffer);
  17266. }
  17267. NK_INTERN void
  17268. nk_start_popup(struct nk_context *ctx, struct nk_window *win)
  17269. {
  17270. struct nk_popup_buffer *buf;
  17271. NK_ASSERT(ctx);
  17272. NK_ASSERT(win);
  17273. if (!ctx || !win) return;
  17274. /* save buffer fill state for popup */
  17275. buf = &win->popup.buf;
  17276. buf->begin = win->buffer.end;
  17277. buf->end = win->buffer.end;
  17278. buf->parent = win->buffer.last;
  17279. buf->last = buf->begin;
  17280. buf->active = nk_true;
  17281. }
  17282. NK_INTERN void
  17283. nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
  17284. {
  17285. struct nk_popup_buffer *buf;
  17286. NK_ASSERT(ctx);
  17287. NK_ASSERT(win);
  17288. if (!ctx || !win) return;
  17289. buf = &win->popup.buf;
  17290. buf->last = win->buffer.last;
  17291. buf->end = win->buffer.end;
  17292. }
  17293. NK_INTERN void
  17294. nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  17295. {
  17296. NK_ASSERT(ctx);
  17297. NK_ASSERT(buffer);
  17298. if (!ctx || !buffer) return;
  17299. buffer->end = ctx->memory.allocated;
  17300. }
  17301. NK_INTERN void
  17302. nk_finish(struct nk_context *ctx, struct nk_window *win)
  17303. {
  17304. struct nk_popup_buffer *buf;
  17305. struct nk_command *parent_last;
  17306. void *memory;
  17307. NK_ASSERT(ctx);
  17308. NK_ASSERT(win);
  17309. if (!ctx || !win) return;
  17310. nk_finish_buffer(ctx, &win->buffer);
  17311. if (!win->popup.buf.active) return;
  17312. buf = &win->popup.buf;
  17313. memory = ctx->memory.memory.ptr;
  17314. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  17315. parent_last->next = buf->end;
  17316. }
  17317. NK_INTERN void
  17318. nk_build(struct nk_context *ctx)
  17319. {
  17320. struct nk_window *it = 0;
  17321. struct nk_command *cmd = 0;
  17322. nk_byte *buffer = 0;
  17323. /* draw cursor overlay */
  17324. if (!ctx->style.cursor_active)
  17325. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17326. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  17327. struct nk_rect mouse_bounds;
  17328. const struct nk_cursor *cursor = ctx->style.cursor_active;
  17329. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  17330. nk_start_buffer(ctx, &ctx->overlay);
  17331. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  17332. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  17333. mouse_bounds.w = cursor->size.x;
  17334. mouse_bounds.h = cursor->size.y;
  17335. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  17336. nk_finish_buffer(ctx, &ctx->overlay);
  17337. }
  17338. /* build one big draw command list out of all window buffers */
  17339. it = ctx->begin;
  17340. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17341. while (it != 0) {
  17342. struct nk_window *next = it->next;
  17343. if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
  17344. it->seq != ctx->seq)
  17345. goto cont;
  17346. cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
  17347. while (next && ((next->buffer.last == next->buffer.begin) ||
  17348. (next->flags & NK_WINDOW_HIDDEN)))
  17349. next = next->next; /* skip empty command buffers */
  17350. if (next) cmd->next = next->buffer.begin;
  17351. cont: it = next;
  17352. }
  17353. /* append all popup draw commands into lists */
  17354. it = ctx->begin;
  17355. while (it != 0) {
  17356. struct nk_window *next = it->next;
  17357. struct nk_popup_buffer *buf;
  17358. if (!it->popup.buf.active)
  17359. goto skip;
  17360. buf = &it->popup.buf;
  17361. cmd->next = buf->begin;
  17362. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  17363. buf->active = nk_false;
  17364. skip: it = next;
  17365. }
  17366. if (cmd) {
  17367. /* append overlay commands */
  17368. if (ctx->overlay.end != ctx->overlay.begin)
  17369. cmd->next = ctx->overlay.begin;
  17370. else cmd->next = ctx->memory.allocated;
  17371. }
  17372. }
  17373. NK_API const struct nk_command*
  17374. nk__begin(struct nk_context *ctx)
  17375. {
  17376. struct nk_window *iter;
  17377. nk_byte *buffer;
  17378. NK_ASSERT(ctx);
  17379. if (!ctx) return 0;
  17380. if (!ctx->count) return 0;
  17381. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17382. if (!ctx->build) {
  17383. nk_build(ctx);
  17384. ctx->build = nk_true;
  17385. }
  17386. iter = ctx->begin;
  17387. while (iter && ((iter->buffer.begin == iter->buffer.end) ||
  17388. (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
  17389. iter = iter->next;
  17390. if (!iter) return 0;
  17391. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  17392. }
  17393. NK_API const struct nk_command*
  17394. nk__next(struct nk_context *ctx, const struct nk_command *cmd)
  17395. {
  17396. nk_byte *buffer;
  17397. const struct nk_command *next;
  17398. NK_ASSERT(ctx);
  17399. if (!ctx || !cmd || !ctx->count) return 0;
  17400. if (cmd->next >= ctx->memory.allocated) return 0;
  17401. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17402. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  17403. return next;
  17404. }
  17405. /* ----------------------------------------------------------------
  17406. *
  17407. * PANEL
  17408. *
  17409. * ---------------------------------------------------------------*/
  17410. static int
  17411. nk_panel_has_header(nk_flags flags, const char *title)
  17412. {
  17413. int active = 0;
  17414. active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
  17415. active = active || (flags & NK_WINDOW_TITLE);
  17416. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  17417. return active;
  17418. }
  17419. NK_INTERN struct nk_vec2
  17420. nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
  17421. {
  17422. switch (type) {
  17423. default:
  17424. case NK_PANEL_WINDOW: return style->window.padding;
  17425. case NK_PANEL_GROUP: return style->window.group_padding;
  17426. case NK_PANEL_POPUP: return style->window.popup_padding;
  17427. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  17428. case NK_PANEL_COMBO: return style->window.combo_padding;
  17429. case NK_PANEL_MENU: return style->window.menu_padding;
  17430. case NK_PANEL_TOOLTIP: return style->window.menu_padding;
  17431. }
  17432. }
  17433. NK_INTERN float
  17434. nk_panel_get_border(const struct nk_style *style, nk_flags flags,
  17435. enum nk_panel_type type)
  17436. {
  17437. if (flags & NK_WINDOW_BORDER) {
  17438. switch (type) {
  17439. default:
  17440. case NK_PANEL_WINDOW: return style->window.border;
  17441. case NK_PANEL_GROUP: return style->window.group_border;
  17442. case NK_PANEL_POPUP: return style->window.popup_border;
  17443. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  17444. case NK_PANEL_COMBO: return style->window.combo_border;
  17445. case NK_PANEL_MENU: return style->window.menu_border;
  17446. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  17447. }} else return 0;
  17448. }
  17449. NK_INTERN struct nk_color
  17450. nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
  17451. {
  17452. switch (type) {
  17453. default:
  17454. case NK_PANEL_WINDOW: return style->window.border_color;
  17455. case NK_PANEL_GROUP: return style->window.group_border_color;
  17456. case NK_PANEL_POPUP: return style->window.popup_border_color;
  17457. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  17458. case NK_PANEL_COMBO: return style->window.combo_border_color;
  17459. case NK_PANEL_MENU: return style->window.menu_border_color;
  17460. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;
  17461. }
  17462. }
  17463. NK_INTERN int
  17464. nk_panel_is_sub(enum nk_panel_type type)
  17465. {
  17466. return (type & NK_PANEL_SET_SUB)?1:0;
  17467. }
  17468. NK_INTERN int
  17469. nk_panel_is_nonblock(enum nk_panel_type type)
  17470. {
  17471. return (type & NK_PANEL_SET_NONBLOCK)?1:0;
  17472. }
  17473. NK_INTERN int
  17474. nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
  17475. {
  17476. struct nk_input *in;
  17477. struct nk_window *win;
  17478. struct nk_panel *layout;
  17479. struct nk_command_buffer *out;
  17480. const struct nk_style *style;
  17481. const struct nk_user_font *font;
  17482. struct nk_vec2 scrollbar_size;
  17483. struct nk_vec2 panel_padding;
  17484. NK_ASSERT(ctx);
  17485. NK_ASSERT(ctx->current);
  17486. NK_ASSERT(ctx->current->layout);
  17487. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  17488. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  17489. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  17490. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  17491. ctx->current->layout->type = panel_type;
  17492. return 0;
  17493. }
  17494. /* pull state into local stack */
  17495. style = &ctx->style;
  17496. font = style->font;
  17497. win = ctx->current;
  17498. layout = win->layout;
  17499. out = &win->buffer;
  17500. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
  17501. #ifdef NK_INCLUDE_COMMAND_USERDATA
  17502. win->buffer.userdata = ctx->userdata;
  17503. #endif
  17504. /* pull style configuration into local stack */
  17505. scrollbar_size = style->window.scrollbar_size;
  17506. panel_padding = nk_panel_get_padding(style, panel_type);
  17507. /* window movement */
  17508. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  17509. int left_mouse_down;
  17510. int left_mouse_click_in_cursor;
  17511. /* calculate draggable window space */
  17512. struct nk_rect header;
  17513. header.x = win->bounds.x;
  17514. header.y = win->bounds.y;
  17515. header.w = win->bounds.w;
  17516. if (nk_panel_has_header(win->flags, title)) {
  17517. header.h = font->height + 2.0f * style->window.header.padding.y;
  17518. header.h += 2.0f * style->window.header.label_padding.y;
  17519. } else header.h = panel_padding.y;
  17520. /* window movement by dragging */
  17521. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  17522. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  17523. NK_BUTTON_LEFT, header, nk_true);
  17524. if (left_mouse_down && left_mouse_click_in_cursor) {
  17525. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  17526. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  17527. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  17528. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  17529. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  17530. }
  17531. }
  17532. /* setup panel */
  17533. layout->type = panel_type;
  17534. layout->flags = win->flags;
  17535. layout->bounds = win->bounds;
  17536. layout->bounds.x += panel_padding.x;
  17537. layout->bounds.w -= 2*panel_padding.x;
  17538. if (win->flags & NK_WINDOW_BORDER) {
  17539. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  17540. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  17541. } else layout->border = 0;
  17542. layout->at_y = layout->bounds.y;
  17543. layout->at_x = layout->bounds.x;
  17544. layout->max_x = 0;
  17545. layout->header_height = 0;
  17546. layout->footer_height = 0;
  17547. nk_layout_reset_min_row_height(ctx);
  17548. layout->row.index = 0;
  17549. layout->row.columns = 0;
  17550. layout->row.ratio = 0;
  17551. layout->row.item_width = 0;
  17552. layout->row.tree_depth = 0;
  17553. layout->row.height = panel_padding.y;
  17554. layout->has_scrolling = nk_true;
  17555. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  17556. layout->bounds.w -= scrollbar_size.x;
  17557. if (!nk_panel_is_nonblock(panel_type)) {
  17558. layout->footer_height = 0;
  17559. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  17560. layout->footer_height = scrollbar_size.y;
  17561. layout->bounds.h -= layout->footer_height;
  17562. }
  17563. /* panel header */
  17564. if (nk_panel_has_header(win->flags, title))
  17565. {
  17566. struct nk_text text;
  17567. struct nk_rect header;
  17568. const struct nk_style_item *background = 0;
  17569. /* calculate header bounds */
  17570. header.x = win->bounds.x;
  17571. header.y = win->bounds.y;
  17572. header.w = win->bounds.w;
  17573. header.h = font->height + 2.0f * style->window.header.padding.y;
  17574. header.h += (2.0f * style->window.header.label_padding.y);
  17575. /* shrink panel by header */
  17576. layout->header_height = header.h;
  17577. layout->bounds.y += header.h;
  17578. layout->bounds.h -= header.h;
  17579. layout->at_y += header.h;
  17580. /* select correct header background and text color */
  17581. if (ctx->active == win) {
  17582. background = &style->window.header.active;
  17583. text.text = style->window.header.label_active;
  17584. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  17585. background = &style->window.header.hover;
  17586. text.text = style->window.header.label_hover;
  17587. } else {
  17588. background = &style->window.header.normal;
  17589. text.text = style->window.header.label_normal;
  17590. }
  17591. /* draw header background */
  17592. header.h += 1.0f;
  17593. if (background->type == NK_STYLE_ITEM_IMAGE) {
  17594. text.background = nk_rgba(0,0,0,0);
  17595. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  17596. } else {
  17597. text.background = background->data.color;
  17598. nk_fill_rect(out, header, 0, background->data.color);
  17599. }
  17600. /* window close button */
  17601. {struct nk_rect button;
  17602. button.y = header.y + style->window.header.padding.y;
  17603. button.h = header.h - 2 * style->window.header.padding.y;
  17604. button.w = button.h;
  17605. if (win->flags & NK_WINDOW_CLOSABLE) {
  17606. nk_flags ws = 0;
  17607. if (style->window.header.align == NK_HEADER_RIGHT) {
  17608. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  17609. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  17610. } else {
  17611. button.x = header.x + style->window.header.padding.x;
  17612. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  17613. }
  17614. if (nk_do_button_symbol(&ws, &win->buffer, button,
  17615. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  17616. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  17617. {
  17618. layout->flags |= NK_WINDOW_HIDDEN;
  17619. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  17620. }
  17621. }
  17622. /* window minimize button */
  17623. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  17624. nk_flags ws = 0;
  17625. if (style->window.header.align == NK_HEADER_RIGHT) {
  17626. button.x = (header.w + header.x) - button.w;
  17627. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  17628. button.x -= style->window.header.padding.x;
  17629. header.w -= style->window.header.padding.x;
  17630. }
  17631. header.w -= button.w + style->window.header.spacing.x;
  17632. } else {
  17633. button.x = header.x;
  17634. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  17635. }
  17636. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
  17637. style->window.header.maximize_symbol: style->window.header.minimize_symbol,
  17638. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  17639. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  17640. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
  17641. layout->flags | NK_WINDOW_MINIMIZED;
  17642. }}
  17643. {/* window header title */
  17644. int text_len = nk_strlen(title);
  17645. struct nk_rect label = {0,0,0,0};
  17646. float t = font->width(font->userdata, font->height, title, text_len);
  17647. text.padding = nk_vec2(0,0);
  17648. label.x = header.x + style->window.header.padding.x;
  17649. label.x += style->window.header.label_padding.x;
  17650. label.y = header.y + style->window.header.label_padding.y;
  17651. label.h = font->height + 2 * style->window.header.label_padding.y;
  17652. label.w = t + 2 * style->window.header.spacing.x;
  17653. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  17654. nk_widget_text(out, label,(const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
  17655. }
  17656. /* draw window background */
  17657. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  17658. struct nk_rect body;
  17659. body.x = win->bounds.x;
  17660. body.w = win->bounds.w;
  17661. body.y = (win->bounds.y + layout->header_height);
  17662. body.h = (win->bounds.h - layout->header_height);
  17663. if (style->window.fixed_background.type == NK_STYLE_ITEM_IMAGE)
  17664. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  17665. else nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
  17666. }
  17667. /* set clipping rectangle */
  17668. {struct nk_rect clip;
  17669. layout->clip = layout->bounds;
  17670. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  17671. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  17672. nk_push_scissor(out, clip);
  17673. layout->clip = clip;}
  17674. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  17675. }
  17676. NK_INTERN void
  17677. nk_panel_end(struct nk_context *ctx)
  17678. {
  17679. struct nk_input *in;
  17680. struct nk_window *window;
  17681. struct nk_panel *layout;
  17682. const struct nk_style *style;
  17683. struct nk_command_buffer *out;
  17684. struct nk_vec2 scrollbar_size;
  17685. struct nk_vec2 panel_padding;
  17686. NK_ASSERT(ctx);
  17687. NK_ASSERT(ctx->current);
  17688. NK_ASSERT(ctx->current->layout);
  17689. if (!ctx || !ctx->current || !ctx->current->layout)
  17690. return;
  17691. window = ctx->current;
  17692. layout = window->layout;
  17693. style = &ctx->style;
  17694. out = &window->buffer;
  17695. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
  17696. if (!nk_panel_is_sub(layout->type))
  17697. nk_push_scissor(out, nk_null_rect);
  17698. /* cache configuration data */
  17699. scrollbar_size = style->window.scrollbar_size;
  17700. panel_padding = nk_panel_get_padding(style, layout->type);
  17701. /* update the current cursor Y-position to point over the last added widget */
  17702. layout->at_y += layout->row.height;
  17703. /* dynamic panels */
  17704. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  17705. {
  17706. /* update panel height to fit dynamic growth */
  17707. struct nk_rect empty_space;
  17708. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  17709. layout->bounds.h = layout->at_y - layout->bounds.y;
  17710. /* fill top empty space */
  17711. empty_space.x = window->bounds.x;
  17712. empty_space.y = layout->bounds.y;
  17713. empty_space.h = panel_padding.y;
  17714. empty_space.w = window->bounds.w;
  17715. nk_fill_rect(out, empty_space, 0, style->window.background);
  17716. /* fill left empty space */
  17717. empty_space.x = window->bounds.x;
  17718. empty_space.y = layout->bounds.y;
  17719. empty_space.w = panel_padding.x + layout->border;
  17720. empty_space.h = layout->bounds.h;
  17721. nk_fill_rect(out, empty_space, 0, style->window.background);
  17722. /* fill right empty space */
  17723. empty_space.x = layout->bounds.x + layout->bounds.w - layout->border;
  17724. empty_space.y = layout->bounds.y;
  17725. empty_space.w = panel_padding.x + layout->border;
  17726. empty_space.h = layout->bounds.h;
  17727. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  17728. empty_space.w += scrollbar_size.x;
  17729. nk_fill_rect(out, empty_space, 0, style->window.background);
  17730. /* fill bottom empty space */
  17731. if (*layout->offset_x != 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR)) {
  17732. empty_space.x = window->bounds.x;
  17733. empty_space.y = layout->bounds.y + layout->bounds.h;
  17734. empty_space.w = window->bounds.w;
  17735. empty_space.h = scrollbar_size.y;
  17736. nk_fill_rect(out, empty_space, 0, style->window.background);
  17737. }
  17738. }
  17739. /* scrollbars */
  17740. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  17741. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  17742. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  17743. {
  17744. struct nk_rect scroll;
  17745. int scroll_has_scrolling;
  17746. float scroll_target;
  17747. float scroll_offset;
  17748. float scroll_step;
  17749. float scroll_inc;
  17750. /* mouse wheel scrolling */
  17751. if (nk_panel_is_sub(layout->type))
  17752. {
  17753. /* sub-window mouse wheel scrolling */
  17754. struct nk_window *root_window = window;
  17755. struct nk_panel *root_panel = window->layout;
  17756. while (root_panel->parent)
  17757. root_panel = root_panel->parent;
  17758. while (root_window->parent)
  17759. root_window = root_window->parent;
  17760. /* only allow scrolling if parent window is active */
  17761. scroll_has_scrolling = 0;
  17762. if ((root_window == ctx->active) && layout->has_scrolling) {
  17763. /* and panel is being hovered and inside clip rect*/
  17764. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  17765. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  17766. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  17767. {
  17768. /* deactivate all parent scrolling */
  17769. root_panel = window->layout;
  17770. while (root_panel->parent) {
  17771. root_panel->has_scrolling = nk_false;
  17772. root_panel = root_panel->parent;
  17773. }
  17774. root_panel->has_scrolling = nk_false;
  17775. scroll_has_scrolling = nk_true;
  17776. }
  17777. }
  17778. } else if (!nk_panel_is_sub(layout->type)) {
  17779. /* window mouse wheel scrolling */
  17780. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  17781. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  17782. window->scrolled = nk_true;
  17783. else window->scrolled = nk_false;
  17784. } else scroll_has_scrolling = nk_false;
  17785. {
  17786. /* vertical scrollbar */
  17787. nk_flags state = 0;
  17788. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  17789. scroll.y = layout->bounds.y;
  17790. scroll.w = scrollbar_size.x;
  17791. scroll.h = layout->bounds.h;
  17792. scroll_offset = (float)*layout->offset_y;
  17793. scroll_step = scroll.h * 0.10f;
  17794. scroll_inc = scroll.h * 0.01f;
  17795. scroll_target = (float)(int)(layout->at_y - scroll.y);
  17796. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  17797. scroll_offset, scroll_target, scroll_step, scroll_inc,
  17798. &ctx->style.scrollv, in, style->font);
  17799. *layout->offset_y = (nk_uint)scroll_offset;
  17800. if (in && scroll_has_scrolling)
  17801. in->mouse.scroll_delta.y = 0;
  17802. }
  17803. {
  17804. /* horizontal scrollbar */
  17805. nk_flags state = 0;
  17806. scroll.x = layout->bounds.x;
  17807. scroll.y = layout->bounds.y + layout->bounds.h;
  17808. scroll.w = layout->bounds.w;
  17809. scroll.h = scrollbar_size.y;
  17810. scroll_offset = (float)*layout->offset_x;
  17811. scroll_target = (float)(int)(layout->max_x - scroll.x);
  17812. scroll_step = layout->max_x * 0.05f;
  17813. scroll_inc = layout->max_x * 0.005f;
  17814. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  17815. scroll_offset, scroll_target, scroll_step, scroll_inc,
  17816. &ctx->style.scrollh, in, style->font);
  17817. *layout->offset_x = (nk_uint)scroll_offset;
  17818. }
  17819. }
  17820. /* hide scroll if no user input */
  17821. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  17822. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  17823. int is_window_hovered = nk_window_is_hovered(ctx);
  17824. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  17825. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  17826. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  17827. else window->scrollbar_hiding_timer = 0;
  17828. } else window->scrollbar_hiding_timer = 0;
  17829. /* window border */
  17830. if (layout->flags & NK_WINDOW_BORDER)
  17831. {
  17832. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  17833. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
  17834. ? (style->window.border + window->bounds.y + layout->header_height)
  17835. : ((layout->flags & NK_WINDOW_DYNAMIC)
  17836. ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
  17837. : (window->bounds.y + window->bounds.h));
  17838. struct nk_rect b = window->bounds;
  17839. b.h = padding_y - window->bounds.y;
  17840. nk_stroke_rect(out, b, 0, layout->border, border_color);
  17841. }
  17842. /* scaler */
  17843. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  17844. {
  17845. /* calculate scaler bounds */
  17846. struct nk_rect scaler;
  17847. scaler.w = scrollbar_size.x;
  17848. scaler.h = scrollbar_size.y;
  17849. scaler.y = layout->bounds.y + layout->bounds.h;
  17850. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  17851. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  17852. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  17853. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  17854. scaler.x -= scaler.w;
  17855. /* draw scaler */
  17856. {const struct nk_style_item *item = &style->window.scaler;
  17857. if (item->type == NK_STYLE_ITEM_IMAGE)
  17858. nk_draw_image(out, scaler, &item->data.image, nk_white);
  17859. else {
  17860. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  17861. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  17862. scaler.y + scaler.h, scaler.x + scaler.w,
  17863. scaler.y + scaler.h, item->data.color);
  17864. } else {
  17865. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  17866. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  17867. }
  17868. }}
  17869. /* do window scaling */
  17870. if (!(window->flags & NK_WINDOW_ROM)) {
  17871. struct nk_vec2 window_size = style->window.min_size;
  17872. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  17873. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  17874. NK_BUTTON_LEFT, scaler, nk_true);
  17875. if (left_mouse_down && left_mouse_click_in_scaler) {
  17876. float delta_x = in->mouse.delta.x;
  17877. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  17878. delta_x = -delta_x;
  17879. window->bounds.x += in->mouse.delta.x;
  17880. }
  17881. /* dragging in x-direction */
  17882. if (window->bounds.w + delta_x >= window_size.x) {
  17883. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  17884. window->bounds.w = window->bounds.w + delta_x;
  17885. scaler.x += in->mouse.delta.x;
  17886. }
  17887. }
  17888. /* dragging in y-direction (only possible if static window) */
  17889. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  17890. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  17891. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  17892. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  17893. scaler.y += in->mouse.delta.y;
  17894. }
  17895. }
  17896. }
  17897. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  17898. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
  17899. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
  17900. }
  17901. }
  17902. }
  17903. if (!nk_panel_is_sub(layout->type)) {
  17904. /* window is hidden so clear command buffer */
  17905. if (layout->flags & NK_WINDOW_HIDDEN)
  17906. nk_command_buffer_reset(&window->buffer);
  17907. /* window is visible and not tab */
  17908. else nk_finish(ctx, window);
  17909. }
  17910. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  17911. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  17912. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  17913. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  17914. }
  17915. window->flags = layout->flags;
  17916. /* property garbage collector */
  17917. if (window->property.active && window->property.old != window->property.seq &&
  17918. window->property.active == window->property.prev) {
  17919. nk_zero(&window->property, sizeof(window->property));
  17920. } else {
  17921. window->property.old = window->property.seq;
  17922. window->property.prev = window->property.active;
  17923. window->property.seq = 0;
  17924. }
  17925. /* edit garbage collector */
  17926. if (window->edit.active && window->edit.old != window->edit.seq &&
  17927. window->edit.active == window->edit.prev) {
  17928. nk_zero(&window->edit, sizeof(window->edit));
  17929. } else {
  17930. window->edit.old = window->edit.seq;
  17931. window->edit.prev = window->edit.active;
  17932. window->edit.seq = 0;
  17933. }
  17934. /* contextual garbage collector */
  17935. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  17936. window->popup.con_count = 0;
  17937. window->popup.con_old = 0;
  17938. window->popup.active_con = 0;
  17939. } else {
  17940. window->popup.con_old = window->popup.con_count;
  17941. window->popup.con_count = 0;
  17942. }
  17943. window->popup.combo_count = 0;
  17944. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  17945. NK_ASSERT(!layout->row.tree_depth);
  17946. }
  17947. /* ----------------------------------------------------------------
  17948. *
  17949. * PAGE ELEMENT
  17950. *
  17951. * ---------------------------------------------------------------*/
  17952. NK_INTERN struct nk_page_element*
  17953. nk_create_page_element(struct nk_context *ctx)
  17954. {
  17955. struct nk_page_element *elem;
  17956. if (ctx->freelist) {
  17957. /* unlink page element from free list */
  17958. elem = ctx->freelist;
  17959. ctx->freelist = elem->next;
  17960. } else if (ctx->use_pool) {
  17961. /* allocate page element from memory pool */
  17962. elem = nk_pool_alloc(&ctx->pool);
  17963. NK_ASSERT(elem);
  17964. if (!elem) return 0;
  17965. } else {
  17966. /* allocate new page element from back of fixed size memory buffer */
  17967. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  17968. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  17969. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  17970. NK_ASSERT(elem);
  17971. if (!elem) return 0;
  17972. }
  17973. nk_zero_struct(*elem);
  17974. elem->next = 0;
  17975. elem->prev = 0;
  17976. return elem;
  17977. }
  17978. NK_INTERN void
  17979. nk_link_page_element_into_freelist(struct nk_context *ctx,
  17980. struct nk_page_element *elem)
  17981. {
  17982. /* link table into freelist */
  17983. if (!ctx->freelist) {
  17984. ctx->freelist = elem;
  17985. } else {
  17986. elem->next = ctx->freelist;
  17987. ctx->freelist = elem;
  17988. }
  17989. }
  17990. NK_INTERN void
  17991. nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
  17992. {
  17993. /* we have a pool so just add to free list */
  17994. if (ctx->use_pool) {
  17995. nk_link_page_element_into_freelist(ctx, elem);
  17996. return;
  17997. }
  17998. /* if possible remove last element from back of fixed memory buffer */
  17999. {void *elem_end = (void*)(elem + 1);
  18000. void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  18001. if (elem_end == buffer_end)
  18002. ctx->memory.size -= sizeof(struct nk_page_element);
  18003. else nk_link_page_element_into_freelist(ctx, elem);}
  18004. }
  18005. /* ----------------------------------------------------------------
  18006. *
  18007. * PANEL
  18008. *
  18009. * ---------------------------------------------------------------*/
  18010. NK_INTERN void*
  18011. nk_create_panel(struct nk_context *ctx)
  18012. {
  18013. struct nk_page_element *elem;
  18014. elem = nk_create_page_element(ctx);
  18015. if (!elem) return 0;
  18016. nk_zero_struct(*elem);
  18017. return &elem->data.pan;
  18018. }
  18019. NK_INTERN void
  18020. nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
  18021. {
  18022. union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  18023. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18024. nk_free_page_element(ctx, pe);
  18025. }
  18026. /* ----------------------------------------------------------------
  18027. *
  18028. * TABLES
  18029. *
  18030. * ---------------------------------------------------------------*/
  18031. NK_INTERN struct nk_table*
  18032. nk_create_table(struct nk_context *ctx)
  18033. {
  18034. struct nk_page_element *elem;
  18035. elem = nk_create_page_element(ctx);
  18036. if (!elem) return 0;
  18037. nk_zero_struct(*elem);
  18038. return &elem->data.tbl;
  18039. }
  18040. NK_INTERN void
  18041. nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
  18042. {
  18043. union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  18044. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18045. nk_free_page_element(ctx, pe);
  18046. }
  18047. NK_INTERN void
  18048. nk_push_table(struct nk_window *win, struct nk_table *tbl)
  18049. {
  18050. if (!win->tables) {
  18051. win->tables = tbl;
  18052. tbl->next = 0;
  18053. tbl->prev = 0;
  18054. tbl->size = 0;
  18055. win->table_count = 1;
  18056. return;
  18057. }
  18058. win->tables->prev = tbl;
  18059. tbl->next = win->tables;
  18060. tbl->prev = 0;
  18061. tbl->size = 0;
  18062. win->tables = tbl;
  18063. win->table_count++;
  18064. }
  18065. NK_INTERN void
  18066. nk_remove_table(struct nk_window *win, struct nk_table *tbl)
  18067. {
  18068. if (win->tables == tbl)
  18069. win->tables = tbl->next;
  18070. if (tbl->next)
  18071. tbl->next->prev = tbl->prev;
  18072. if (tbl->prev)
  18073. tbl->prev->next = tbl->next;
  18074. tbl->next = 0;
  18075. tbl->prev = 0;
  18076. }
  18077. NK_INTERN nk_uint*
  18078. nk_add_value(struct nk_context *ctx, struct nk_window *win,
  18079. nk_hash name, nk_uint value)
  18080. {
  18081. NK_ASSERT(ctx);
  18082. NK_ASSERT(win);
  18083. if (!win || !ctx) return 0;
  18084. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  18085. struct nk_table *tbl = nk_create_table(ctx);
  18086. NK_ASSERT(tbl);
  18087. if (!tbl) return 0;
  18088. nk_push_table(win, tbl);
  18089. }
  18090. win->tables->seq = win->seq;
  18091. win->tables->keys[win->tables->size] = name;
  18092. win->tables->values[win->tables->size] = value;
  18093. return &win->tables->values[win->tables->size++];
  18094. }
  18095. NK_INTERN nk_uint*
  18096. nk_find_value(struct nk_window *win, nk_hash name)
  18097. {
  18098. struct nk_table *iter = win->tables;
  18099. while (iter) {
  18100. unsigned int i = 0;
  18101. unsigned int size = iter->size;
  18102. for (i = 0; i < size; ++i) {
  18103. if (iter->keys[i] == name) {
  18104. iter->seq = win->seq;
  18105. return &iter->values[i];
  18106. }
  18107. } size = NK_VALUE_PAGE_CAPACITY;
  18108. iter = iter->next;
  18109. }
  18110. return 0;
  18111. }
  18112. /* ----------------------------------------------------------------
  18113. *
  18114. * WINDOW
  18115. *
  18116. * ---------------------------------------------------------------*/
  18117. NK_INTERN void*
  18118. nk_create_window(struct nk_context *ctx)
  18119. {
  18120. struct nk_page_element *elem;
  18121. elem = nk_create_page_element(ctx);
  18122. if (!elem) return 0;
  18123. elem->data.win.seq = ctx->seq;
  18124. return &elem->data.win;
  18125. }
  18126. NK_INTERN void
  18127. nk_free_window(struct nk_context *ctx, struct nk_window *win)
  18128. {
  18129. /* unlink windows from list */
  18130. struct nk_table *it = win->tables;
  18131. if (win->popup.win) {
  18132. nk_free_window(ctx, win->popup.win);
  18133. win->popup.win = 0;
  18134. }
  18135. win->next = 0;
  18136. win->prev = 0;
  18137. while (it) {
  18138. /*free window state tables */
  18139. struct nk_table *n = it->next;
  18140. nk_remove_table(win, it);
  18141. nk_free_table(ctx, it);
  18142. if (it == win->tables)
  18143. win->tables = n;
  18144. it = n;
  18145. }
  18146. /* link windows into freelist */
  18147. {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  18148. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18149. nk_free_page_element(ctx, pe);}
  18150. }
  18151. NK_INTERN struct nk_window*
  18152. nk_find_window(struct nk_context *ctx, nk_hash hash, const char *name)
  18153. {
  18154. struct nk_window *iter;
  18155. iter = ctx->begin;
  18156. while (iter) {
  18157. NK_ASSERT(iter != iter->next);
  18158. if (iter->name == hash) {
  18159. int max_len = nk_strlen(iter->name_string);
  18160. if (!nk_stricmpn(iter->name_string, name, max_len))
  18161. return iter;
  18162. }
  18163. iter = iter->next;
  18164. }
  18165. return 0;
  18166. }
  18167. enum nk_window_insert_location {
  18168. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  18169. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  18170. };
  18171. NK_INTERN void
  18172. nk_insert_window(struct nk_context *ctx, struct nk_window *win,
  18173. enum nk_window_insert_location loc)
  18174. {
  18175. const struct nk_window *iter;
  18176. NK_ASSERT(ctx);
  18177. NK_ASSERT(win);
  18178. if (!win || !ctx) return;
  18179. iter = ctx->begin;
  18180. while (iter) {
  18181. NK_ASSERT(iter != iter->next);
  18182. NK_ASSERT(iter != win);
  18183. if (iter == win) return;
  18184. iter = iter->next;
  18185. }
  18186. if (!ctx->begin) {
  18187. win->next = 0;
  18188. win->prev = 0;
  18189. ctx->begin = win;
  18190. ctx->end = win;
  18191. ctx->count = 1;
  18192. return;
  18193. }
  18194. if (loc == NK_INSERT_BACK) {
  18195. struct nk_window *end;
  18196. end = ctx->end;
  18197. end->flags |= NK_WINDOW_ROM;
  18198. end->next = win;
  18199. win->prev = ctx->end;
  18200. win->next = 0;
  18201. ctx->end = win;
  18202. ctx->active = ctx->end;
  18203. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18204. } else {
  18205. /*ctx->end->flags |= NK_WINDOW_ROM;*/
  18206. ctx->begin->prev = win;
  18207. win->next = ctx->begin;
  18208. win->prev = 0;
  18209. ctx->begin = win;
  18210. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18211. }
  18212. ctx->count++;
  18213. }
  18214. NK_INTERN void
  18215. nk_remove_window(struct nk_context *ctx, struct nk_window *win)
  18216. {
  18217. if (win == ctx->begin || win == ctx->end) {
  18218. if (win == ctx->begin) {
  18219. ctx->begin = win->next;
  18220. if (win->next)
  18221. win->next->prev = 0;
  18222. }
  18223. if (win == ctx->end) {
  18224. ctx->end = win->prev;
  18225. if (win->prev)
  18226. win->prev->next = 0;
  18227. }
  18228. } else {
  18229. if (win->next)
  18230. win->next->prev = win->prev;
  18231. if (win->prev)
  18232. win->prev->next = win->next;
  18233. }
  18234. if (win == ctx->active || !ctx->active) {
  18235. ctx->active = ctx->end;
  18236. if (ctx->end)
  18237. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18238. }
  18239. win->next = 0;
  18240. win->prev = 0;
  18241. ctx->count--;
  18242. }
  18243. NK_API int
  18244. nk_begin(struct nk_context *ctx, const char *title,
  18245. struct nk_rect bounds, nk_flags flags)
  18246. {
  18247. return nk_begin_titled(ctx, title, title, bounds, flags);
  18248. }
  18249. NK_API int
  18250. nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
  18251. struct nk_rect bounds, nk_flags flags)
  18252. {
  18253. struct nk_window *win;
  18254. struct nk_style *style;
  18255. nk_hash title_hash;
  18256. int title_len;
  18257. int ret = 0;
  18258. NK_ASSERT(ctx);
  18259. NK_ASSERT(name);
  18260. NK_ASSERT(title);
  18261. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  18262. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  18263. if (!ctx || ctx->current || !title || !name)
  18264. return 0;
  18265. /* find or create window */
  18266. style = &ctx->style;
  18267. title_len = (int)nk_strlen(name);
  18268. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18269. win = nk_find_window(ctx, title_hash, name);
  18270. if (!win) {
  18271. /* create new window */
  18272. nk_size name_length = (nk_size)nk_strlen(name);
  18273. win = (struct nk_window*)nk_create_window(ctx);
  18274. NK_ASSERT(win);
  18275. if (!win) return 0;
  18276. if (flags & NK_WINDOW_BACKGROUND)
  18277. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  18278. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  18279. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  18280. win->flags = flags;
  18281. win->bounds = bounds;
  18282. win->name = title_hash;
  18283. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
  18284. NK_MEMCPY(win->name_string, name, name_length);
  18285. win->name_string[name_length] = 0;
  18286. win->popup.win = 0;
  18287. if (!ctx->active)
  18288. ctx->active = win;
  18289. } else {
  18290. /* update window */
  18291. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
  18292. win->flags |= flags;
  18293. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  18294. win->bounds = bounds;
  18295. /* If this assert triggers you either:
  18296. *
  18297. * I.) Have more than one window with the same name or
  18298. * II.) You forgot to actually draw the window.
  18299. * More specific you did not call `nk_clear` (nk_clear will be
  18300. * automatically called for you if you are using one of the
  18301. * provided demo backends). */
  18302. NK_ASSERT(win->seq != ctx->seq);
  18303. win->seq = ctx->seq;
  18304. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
  18305. ctx->active = win;
  18306. ctx->end = win;
  18307. }
  18308. }
  18309. if (win->flags & NK_WINDOW_HIDDEN) {
  18310. ctx->current = win;
  18311. win->layout = 0;
  18312. return 0;
  18313. } else nk_start(ctx, win);
  18314. /* window overlapping */
  18315. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  18316. {
  18317. int inpanel, ishovered;
  18318. struct nk_window *iter = win;
  18319. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  18320. (2.0f * style->window.header.label_padding.y);
  18321. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
  18322. win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  18323. /* activate window if hovered and no other window is overlapping this window */
  18324. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  18325. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  18326. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  18327. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  18328. iter = win->next;
  18329. while (iter) {
  18330. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  18331. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  18332. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18333. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  18334. (!(iter->flags & NK_WINDOW_HIDDEN)))
  18335. break;
  18336. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  18337. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18338. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  18339. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  18340. break;
  18341. iter = iter->next;
  18342. }
  18343. }
  18344. /* activate window if clicked */
  18345. if (iter && inpanel && (win != ctx->end)) {
  18346. iter = win->next;
  18347. while (iter) {
  18348. /* try to find a panel with higher priority in the same position */
  18349. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  18350. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  18351. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  18352. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  18353. !(iter->flags & NK_WINDOW_HIDDEN))
  18354. break;
  18355. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  18356. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18357. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  18358. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  18359. break;
  18360. iter = iter->next;
  18361. }
  18362. }
  18363. if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
  18364. win->flags |= (nk_flags)NK_WINDOW_ROM;
  18365. iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18366. ctx->active = iter;
  18367. if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
  18368. /* current window is active in that position so transfer to top
  18369. * at the highest priority in stack */
  18370. nk_remove_window(ctx, iter);
  18371. nk_insert_window(ctx, iter, NK_INSERT_BACK);
  18372. }
  18373. } else {
  18374. if (!iter && ctx->end != win) {
  18375. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  18376. /* current window is active in that position so transfer to top
  18377. * at the highest priority in stack */
  18378. nk_remove_window(ctx, win);
  18379. nk_insert_window(ctx, win, NK_INSERT_BACK);
  18380. }
  18381. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18382. ctx->active = win;
  18383. }
  18384. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  18385. win->flags |= NK_WINDOW_ROM;
  18386. }
  18387. }
  18388. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  18389. ctx->current = win;
  18390. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  18391. win->layout->offset_x = &win->scrollbar.x;
  18392. win->layout->offset_y = &win->scrollbar.y;
  18393. return ret;
  18394. }
  18395. NK_API void
  18396. nk_end(struct nk_context *ctx)
  18397. {
  18398. struct nk_panel *layout;
  18399. NK_ASSERT(ctx);
  18400. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  18401. if (!ctx || !ctx->current)
  18402. return;
  18403. layout = ctx->current->layout;
  18404. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  18405. ctx->current = 0;
  18406. return;
  18407. }
  18408. nk_panel_end(ctx);
  18409. nk_free_panel(ctx, ctx->current->layout);
  18410. ctx->current = 0;
  18411. }
  18412. NK_API struct nk_rect
  18413. nk_window_get_bounds(const struct nk_context *ctx)
  18414. {
  18415. NK_ASSERT(ctx);
  18416. NK_ASSERT(ctx->current);
  18417. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  18418. return ctx->current->bounds;
  18419. }
  18420. NK_API struct nk_vec2
  18421. nk_window_get_position(const struct nk_context *ctx)
  18422. {
  18423. NK_ASSERT(ctx);
  18424. NK_ASSERT(ctx->current);
  18425. if (!ctx || !ctx->current) return nk_vec2(0,0);
  18426. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  18427. }
  18428. NK_API struct nk_vec2
  18429. nk_window_get_size(const struct nk_context *ctx)
  18430. {
  18431. NK_ASSERT(ctx);
  18432. NK_ASSERT(ctx->current);
  18433. if (!ctx || !ctx->current) return nk_vec2(0,0);
  18434. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  18435. }
  18436. NK_API float
  18437. nk_window_get_width(const struct nk_context *ctx)
  18438. {
  18439. NK_ASSERT(ctx);
  18440. NK_ASSERT(ctx->current);
  18441. if (!ctx || !ctx->current) return 0;
  18442. return ctx->current->bounds.w;
  18443. }
  18444. NK_API float
  18445. nk_window_get_height(const struct nk_context *ctx)
  18446. {
  18447. NK_ASSERT(ctx);
  18448. NK_ASSERT(ctx->current);
  18449. if (!ctx || !ctx->current) return 0;
  18450. return ctx->current->bounds.h;
  18451. }
  18452. NK_API struct nk_rect
  18453. nk_window_get_content_region(struct nk_context *ctx)
  18454. {
  18455. NK_ASSERT(ctx);
  18456. NK_ASSERT(ctx->current);
  18457. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  18458. return ctx->current->layout->clip;
  18459. }
  18460. NK_API struct nk_vec2
  18461. nk_window_get_content_region_min(struct nk_context *ctx)
  18462. {
  18463. NK_ASSERT(ctx);
  18464. NK_ASSERT(ctx->current);
  18465. NK_ASSERT(ctx->current->layout);
  18466. if (!ctx || !ctx->current) return nk_vec2(0,0);
  18467. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  18468. }
  18469. NK_API struct nk_vec2
  18470. nk_window_get_content_region_max(struct nk_context *ctx)
  18471. {
  18472. NK_ASSERT(ctx);
  18473. NK_ASSERT(ctx->current);
  18474. NK_ASSERT(ctx->current->layout);
  18475. if (!ctx || !ctx->current) return nk_vec2(0,0);
  18476. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  18477. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  18478. }
  18479. NK_API struct nk_vec2
  18480. nk_window_get_content_region_size(struct nk_context *ctx)
  18481. {
  18482. NK_ASSERT(ctx);
  18483. NK_ASSERT(ctx->current);
  18484. NK_ASSERT(ctx->current->layout);
  18485. if (!ctx || !ctx->current) return nk_vec2(0,0);
  18486. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  18487. }
  18488. NK_API struct nk_command_buffer*
  18489. nk_window_get_canvas(struct nk_context *ctx)
  18490. {
  18491. NK_ASSERT(ctx);
  18492. NK_ASSERT(ctx->current);
  18493. NK_ASSERT(ctx->current->layout);
  18494. if (!ctx || !ctx->current) return 0;
  18495. return &ctx->current->buffer;
  18496. }
  18497. NK_API struct nk_panel*
  18498. nk_window_get_panel(struct nk_context *ctx)
  18499. {
  18500. NK_ASSERT(ctx);
  18501. NK_ASSERT(ctx->current);
  18502. if (!ctx || !ctx->current) return 0;
  18503. return ctx->current->layout;
  18504. }
  18505. NK_API int
  18506. nk_window_has_focus(const struct nk_context *ctx)
  18507. {
  18508. NK_ASSERT(ctx);
  18509. NK_ASSERT(ctx->current);
  18510. NK_ASSERT(ctx->current->layout);
  18511. if (!ctx || !ctx->current) return 0;
  18512. return ctx->current == ctx->active;
  18513. }
  18514. NK_API int
  18515. nk_window_is_hovered(struct nk_context *ctx)
  18516. {
  18517. NK_ASSERT(ctx);
  18518. NK_ASSERT(ctx->current);
  18519. if (!ctx || !ctx->current) return 0;
  18520. if(ctx->current->flags & NK_WINDOW_HIDDEN)
  18521. return 0;
  18522. return nk_input_is_mouse_hovering_rect(&ctx->input, ctx->current->bounds);
  18523. }
  18524. NK_API int
  18525. nk_window_is_any_hovered(struct nk_context *ctx)
  18526. {
  18527. struct nk_window *iter;
  18528. NK_ASSERT(ctx);
  18529. if (!ctx) return 0;
  18530. iter = ctx->begin;
  18531. while (iter) {
  18532. /* check if window is being hovered */
  18533. if(!(iter->flags & NK_WINDOW_HIDDEN)) {
  18534. /* check if window popup is being hovered */
  18535. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  18536. return 1;
  18537. if (iter->flags & NK_WINDOW_MINIMIZED) {
  18538. struct nk_rect header = iter->bounds;
  18539. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  18540. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  18541. return 1;
  18542. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  18543. return 1;
  18544. }
  18545. }
  18546. iter = iter->next;
  18547. }
  18548. return 0;
  18549. }
  18550. NK_API int
  18551. nk_item_is_any_active(struct nk_context *ctx)
  18552. {
  18553. int any_hovered = nk_window_is_any_hovered(ctx);
  18554. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  18555. return any_hovered || any_active;
  18556. }
  18557. NK_API int
  18558. nk_window_is_collapsed(struct nk_context *ctx, const char *name)
  18559. {
  18560. int title_len;
  18561. nk_hash title_hash;
  18562. struct nk_window *win;
  18563. NK_ASSERT(ctx);
  18564. if (!ctx) return 0;
  18565. title_len = (int)nk_strlen(name);
  18566. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18567. win = nk_find_window(ctx, title_hash, name);
  18568. if (!win) return 0;
  18569. return win->flags & NK_WINDOW_MINIMIZED;
  18570. }
  18571. NK_API int
  18572. nk_window_is_closed(struct nk_context *ctx, const char *name)
  18573. {
  18574. int title_len;
  18575. nk_hash title_hash;
  18576. struct nk_window *win;
  18577. NK_ASSERT(ctx);
  18578. if (!ctx) return 1;
  18579. title_len = (int)nk_strlen(name);
  18580. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18581. win = nk_find_window(ctx, title_hash, name);
  18582. if (!win) return 1;
  18583. return (win->flags & NK_WINDOW_CLOSED);
  18584. }
  18585. NK_API int
  18586. nk_window_is_hidden(struct nk_context *ctx, const char *name)
  18587. {
  18588. int title_len;
  18589. nk_hash title_hash;
  18590. struct nk_window *win;
  18591. NK_ASSERT(ctx);
  18592. if (!ctx) return 1;
  18593. title_len = (int)nk_strlen(name);
  18594. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18595. win = nk_find_window(ctx, title_hash, name);
  18596. if (!win) return 1;
  18597. return (win->flags & NK_WINDOW_HIDDEN);
  18598. }
  18599. NK_API int
  18600. nk_window_is_active(struct nk_context *ctx, const char *name)
  18601. {
  18602. int title_len;
  18603. nk_hash title_hash;
  18604. struct nk_window *win;
  18605. NK_ASSERT(ctx);
  18606. if (!ctx) return 0;
  18607. title_len = (int)nk_strlen(name);
  18608. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18609. win = nk_find_window(ctx, title_hash, name);
  18610. if (!win) return 0;
  18611. return win == ctx->active;
  18612. }
  18613. NK_API struct nk_window*
  18614. nk_window_find(struct nk_context *ctx, const char *name)
  18615. {
  18616. int title_len;
  18617. nk_hash title_hash;
  18618. title_len = (int)nk_strlen(name);
  18619. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18620. return nk_find_window(ctx, title_hash, name);
  18621. }
  18622. NK_API void
  18623. nk_window_close(struct nk_context *ctx, const char *name)
  18624. {
  18625. struct nk_window *win;
  18626. NK_ASSERT(ctx);
  18627. if (!ctx) return;
  18628. win = nk_window_find(ctx, name);
  18629. if (!win) return;
  18630. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  18631. if (ctx->current == win) return;
  18632. win->flags |= NK_WINDOW_HIDDEN;
  18633. win->flags |= NK_WINDOW_CLOSED;
  18634. }
  18635. NK_API void
  18636. nk_window_set_bounds(struct nk_context *ctx,
  18637. const char *name, struct nk_rect bounds)
  18638. {
  18639. struct nk_window *win;
  18640. NK_ASSERT(ctx);
  18641. if (!ctx) return;
  18642. win = nk_window_find(ctx, name);
  18643. if (!win) return;
  18644. NK_ASSERT(ctx->current != win && "You cannot update a currently in procecss window");
  18645. win->bounds = bounds;
  18646. }
  18647. NK_API void
  18648. nk_window_set_position(struct nk_context *ctx,
  18649. const char *name, struct nk_vec2 pos)
  18650. {
  18651. struct nk_window *win = nk_window_find(ctx, name);
  18652. if (!win) return;
  18653. win->bounds.x = pos.x;
  18654. win->bounds.y = pos.y;
  18655. }
  18656. NK_API void
  18657. nk_window_set_size(struct nk_context *ctx,
  18658. const char *name, struct nk_vec2 size)
  18659. {
  18660. struct nk_window *win = nk_window_find(ctx, name);
  18661. if (!win) return;
  18662. win->bounds.w = size.x;
  18663. win->bounds.h = size.y;
  18664. }
  18665. NK_API void
  18666. nk_window_collapse(struct nk_context *ctx, const char *name,
  18667. enum nk_collapse_states c)
  18668. {
  18669. int title_len;
  18670. nk_hash title_hash;
  18671. struct nk_window *win;
  18672. NK_ASSERT(ctx);
  18673. if (!ctx) return;
  18674. title_len = (int)nk_strlen(name);
  18675. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18676. win = nk_find_window(ctx, title_hash, name);
  18677. if (!win) return;
  18678. if (c == NK_MINIMIZED)
  18679. win->flags |= NK_WINDOW_MINIMIZED;
  18680. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  18681. }
  18682. NK_API void
  18683. nk_window_collapse_if(struct nk_context *ctx, const char *name,
  18684. enum nk_collapse_states c, int cond)
  18685. {
  18686. NK_ASSERT(ctx);
  18687. if (!ctx || !cond) return;
  18688. nk_window_collapse(ctx, name, c);
  18689. }
  18690. NK_API void
  18691. nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
  18692. {
  18693. int title_len;
  18694. nk_hash title_hash;
  18695. struct nk_window *win;
  18696. NK_ASSERT(ctx);
  18697. if (!ctx) return;
  18698. title_len = (int)nk_strlen(name);
  18699. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18700. win = nk_find_window(ctx, title_hash, name);
  18701. if (!win) return;
  18702. if (s == NK_HIDDEN) {
  18703. win->flags |= NK_WINDOW_HIDDEN;
  18704. } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  18705. }
  18706. NK_API void
  18707. nk_window_show_if(struct nk_context *ctx, const char *name,
  18708. enum nk_show_states s, int cond)
  18709. {
  18710. NK_ASSERT(ctx);
  18711. if (!ctx || !cond) return;
  18712. nk_window_show(ctx, name, s);
  18713. }
  18714. NK_API void
  18715. nk_window_set_focus(struct nk_context *ctx, const char *name)
  18716. {
  18717. int title_len;
  18718. nk_hash title_hash;
  18719. struct nk_window *win;
  18720. NK_ASSERT(ctx);
  18721. if (!ctx) return;
  18722. title_len = (int)nk_strlen(name);
  18723. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  18724. win = nk_find_window(ctx, title_hash, name);
  18725. if (win && ctx->end != win) {
  18726. nk_remove_window(ctx, win);
  18727. nk_insert_window(ctx, win, NK_INSERT_BACK);
  18728. }
  18729. ctx->active = win;
  18730. }
  18731. /*----------------------------------------------------------------
  18732. *
  18733. * MENUBAR
  18734. *
  18735. * --------------------------------------------------------------*/
  18736. NK_API void
  18737. nk_menubar_begin(struct nk_context *ctx)
  18738. {
  18739. struct nk_panel *layout;
  18740. NK_ASSERT(ctx);
  18741. NK_ASSERT(ctx->current);
  18742. NK_ASSERT(ctx->current->layout);
  18743. if (!ctx || !ctx->current || !ctx->current->layout)
  18744. return;
  18745. layout = ctx->current->layout;
  18746. NK_ASSERT(layout->at_y == layout->bounds.y);
  18747. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  18748. If you want a menubar the first nuklear function after `nk_begin` has to be a
  18749. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  18750. widgets (also supports multiple rows).
  18751. Example:
  18752. if (nk_begin(...)) {
  18753. nk_menubar_begin(...);
  18754. nk_layout_xxxx(...);
  18755. nk_button(...);
  18756. nk_layout_xxxx(...);
  18757. nk_button(...);
  18758. nk_menubar_end(...);
  18759. }
  18760. nk_end(...);
  18761. */
  18762. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  18763. return;
  18764. layout->menu.x = layout->at_x;
  18765. layout->menu.y = layout->at_y + layout->row.height;
  18766. layout->menu.w = layout->bounds.w;
  18767. layout->menu.offset.x = *layout->offset_x;
  18768. layout->menu.offset.y = *layout->offset_y;
  18769. *layout->offset_y = 0;
  18770. }
  18771. NK_API void
  18772. nk_menubar_end(struct nk_context *ctx)
  18773. {
  18774. struct nk_window *win;
  18775. struct nk_panel *layout;
  18776. struct nk_command_buffer *out;
  18777. NK_ASSERT(ctx);
  18778. NK_ASSERT(ctx->current);
  18779. NK_ASSERT(ctx->current->layout);
  18780. if (!ctx || !ctx->current || !ctx->current->layout)
  18781. return;
  18782. win = ctx->current;
  18783. out = &win->buffer;
  18784. layout = win->layout;
  18785. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  18786. return;
  18787. layout->menu.h = layout->at_y - layout->menu.y;
  18788. layout->bounds.y += layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  18789. layout->bounds.h -= layout->menu.h + ctx->style.window.spacing.y + layout->row.height;
  18790. *layout->offset_x = layout->menu.offset.x;
  18791. *layout->offset_y = layout->menu.offset.y;
  18792. layout->at_y = layout->bounds.y - layout->row.height;
  18793. layout->clip.y = layout->bounds.y;
  18794. layout->clip.h = layout->bounds.h;
  18795. nk_push_scissor(out, layout->clip);
  18796. }
  18797. /* -------------------------------------------------------------
  18798. *
  18799. * LAYOUT
  18800. *
  18801. * --------------------------------------------------------------*/
  18802. NK_API void
  18803. nk_layout_set_min_row_height(struct nk_context *ctx, float height)
  18804. {
  18805. struct nk_window *win;
  18806. struct nk_panel *layout;
  18807. NK_ASSERT(ctx);
  18808. NK_ASSERT(ctx->current);
  18809. NK_ASSERT(ctx->current->layout);
  18810. if (!ctx || !ctx->current || !ctx->current->layout)
  18811. return;
  18812. win = ctx->current;
  18813. layout = win->layout;
  18814. layout->row.min_height = height;
  18815. }
  18816. NK_API void
  18817. nk_layout_reset_min_row_height(struct nk_context *ctx)
  18818. {
  18819. struct nk_window *win;
  18820. struct nk_panel *layout;
  18821. NK_ASSERT(ctx);
  18822. NK_ASSERT(ctx->current);
  18823. NK_ASSERT(ctx->current->layout);
  18824. if (!ctx || !ctx->current || !ctx->current->layout)
  18825. return;
  18826. win = ctx->current;
  18827. layout = win->layout;
  18828. layout->row.min_height = ctx->style.font->height;
  18829. layout->row.min_height += ctx->style.text.padding.y*2;
  18830. layout->row.min_height += ctx->style.window.min_row_height_padding*2;
  18831. }
  18832. NK_INTERN float
  18833. nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
  18834. float total_space, int columns)
  18835. {
  18836. float panel_padding;
  18837. float panel_spacing;
  18838. float panel_space;
  18839. struct nk_vec2 spacing;
  18840. struct nk_vec2 padding;
  18841. spacing = style->window.spacing;
  18842. padding = nk_panel_get_padding(style, type);
  18843. /* calculate the usable panel space */
  18844. panel_padding = 2 * padding.x;
  18845. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  18846. panel_space = total_space - panel_padding - panel_spacing;
  18847. return panel_space;
  18848. }
  18849. NK_INTERN void
  18850. nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
  18851. float height, int cols)
  18852. {
  18853. struct nk_panel *layout;
  18854. const struct nk_style *style;
  18855. struct nk_command_buffer *out;
  18856. struct nk_vec2 item_spacing;
  18857. struct nk_color color;
  18858. NK_ASSERT(ctx);
  18859. NK_ASSERT(ctx->current);
  18860. NK_ASSERT(ctx->current->layout);
  18861. if (!ctx || !ctx->current || !ctx->current->layout)
  18862. return;
  18863. /* prefetch some configuration data */
  18864. layout = win->layout;
  18865. style = &ctx->style;
  18866. out = &win->buffer;
  18867. color = style->window.background;
  18868. item_spacing = style->window.spacing;
  18869. /* if one of these triggers you forgot to add an `if` condition around either
  18870. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  18871. Example:
  18872. if (nk_begin(...) {...} nk_end(...); or
  18873. if (nk_group_begin(...) { nk_group_end(...);} */
  18874. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  18875. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  18876. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  18877. /* update the current row and set the current row layout */
  18878. layout->row.index = 0;
  18879. layout->at_y += layout->row.height;
  18880. layout->row.columns = cols;
  18881. if (height == 0.0f)
  18882. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  18883. else layout->row.height = height + item_spacing.y;
  18884. layout->row.item_offset = 0;
  18885. if (layout->flags & NK_WINDOW_DYNAMIC) {
  18886. /* draw background for dynamic panels */
  18887. struct nk_rect background;
  18888. background.x = win->bounds.x;
  18889. background.w = win->bounds.w;
  18890. background.y = layout->at_y - 1.0f;
  18891. background.h = layout->row.height + 1.0f;
  18892. nk_fill_rect(out, background, 0, color);
  18893. }
  18894. }
  18895. NK_INTERN void
  18896. nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
  18897. float height, int cols, int width)
  18898. {
  18899. /* update the current row and set the current row layout */
  18900. struct nk_window *win;
  18901. NK_ASSERT(ctx);
  18902. NK_ASSERT(ctx->current);
  18903. NK_ASSERT(ctx->current->layout);
  18904. if (!ctx || !ctx->current || !ctx->current->layout)
  18905. return;
  18906. win = ctx->current;
  18907. nk_panel_layout(ctx, win, height, cols);
  18908. if (fmt == NK_DYNAMIC)
  18909. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  18910. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  18911. win->layout->row.ratio = 0;
  18912. win->layout->row.filled = 0;
  18913. win->layout->row.item_offset = 0;
  18914. win->layout->row.item_width = (float)width;
  18915. }
  18916. NK_API float
  18917. nk_layout_ratio_from_pixel(struct nk_context *ctx, float pixel_width)
  18918. {
  18919. struct nk_window *win;
  18920. NK_ASSERT(ctx);
  18921. NK_ASSERT(pixel_width);
  18922. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  18923. win = ctx->current;
  18924. return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
  18925. }
  18926. NK_API void
  18927. nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
  18928. {
  18929. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  18930. }
  18931. NK_API void
  18932. nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
  18933. {
  18934. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  18935. }
  18936. NK_API void
  18937. nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  18938. float row_height, int cols)
  18939. {
  18940. struct nk_window *win;
  18941. struct nk_panel *layout;
  18942. NK_ASSERT(ctx);
  18943. NK_ASSERT(ctx->current);
  18944. NK_ASSERT(ctx->current->layout);
  18945. if (!ctx || !ctx->current || !ctx->current->layout)
  18946. return;
  18947. win = ctx->current;
  18948. layout = win->layout;
  18949. nk_panel_layout(ctx, win, row_height, cols);
  18950. if (fmt == NK_DYNAMIC)
  18951. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  18952. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  18953. layout->row.ratio = 0;
  18954. layout->row.filled = 0;
  18955. layout->row.item_width = 0;
  18956. layout->row.item_offset = 0;
  18957. layout->row.columns = cols;
  18958. }
  18959. NK_API void
  18960. nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
  18961. {
  18962. struct nk_window *win;
  18963. struct nk_panel *layout;
  18964. NK_ASSERT(ctx);
  18965. NK_ASSERT(ctx->current);
  18966. NK_ASSERT(ctx->current->layout);
  18967. if (!ctx || !ctx->current || !ctx->current->layout)
  18968. return;
  18969. win = ctx->current;
  18970. layout = win->layout;
  18971. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  18972. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  18973. return;
  18974. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  18975. float ratio = ratio_or_width;
  18976. if ((ratio + layout->row.filled) > 1.0f) return;
  18977. if (ratio > 0.0f)
  18978. layout->row.item_width = NK_SATURATE(ratio);
  18979. else layout->row.item_width = 1.0f - layout->row.filled;
  18980. } else layout->row.item_width = ratio_or_width;
  18981. }
  18982. NK_API void
  18983. nk_layout_row_end(struct nk_context *ctx)
  18984. {
  18985. struct nk_window *win;
  18986. struct nk_panel *layout;
  18987. NK_ASSERT(ctx);
  18988. NK_ASSERT(ctx->current);
  18989. NK_ASSERT(ctx->current->layout);
  18990. if (!ctx || !ctx->current || !ctx->current->layout)
  18991. return;
  18992. win = ctx->current;
  18993. layout = win->layout;
  18994. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  18995. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  18996. return;
  18997. layout->row.item_width = 0;
  18998. layout->row.item_offset = 0;
  18999. }
  19000. NK_API void
  19001. nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
  19002. float height, int cols, const float *ratio)
  19003. {
  19004. int i;
  19005. int n_undef = 0;
  19006. struct nk_window *win;
  19007. struct nk_panel *layout;
  19008. NK_ASSERT(ctx);
  19009. NK_ASSERT(ctx->current);
  19010. NK_ASSERT(ctx->current->layout);
  19011. if (!ctx || !ctx->current || !ctx->current->layout)
  19012. return;
  19013. win = ctx->current;
  19014. layout = win->layout;
  19015. nk_panel_layout(ctx, win, height, cols);
  19016. if (fmt == NK_DYNAMIC) {
  19017. /* calculate width of undefined widget ratios */
  19018. float r = 0;
  19019. layout->row.ratio = ratio;
  19020. for (i = 0; i < cols; ++i) {
  19021. if (ratio[i] < 0.0f)
  19022. n_undef++;
  19023. else r += ratio[i];
  19024. }
  19025. r = NK_SATURATE(1.0f - r);
  19026. layout->row.type = NK_LAYOUT_DYNAMIC;
  19027. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
  19028. } else {
  19029. layout->row.ratio = ratio;
  19030. layout->row.type = NK_LAYOUT_STATIC;
  19031. layout->row.item_width = 0;
  19032. layout->row.item_offset = 0;
  19033. }
  19034. layout->row.item_offset = 0;
  19035. layout->row.filled = 0;
  19036. }
  19037. NK_API void
  19038. nk_layout_row_template_begin(struct nk_context *ctx, float height)
  19039. {
  19040. struct nk_window *win;
  19041. struct nk_panel *layout;
  19042. NK_ASSERT(ctx);
  19043. NK_ASSERT(ctx->current);
  19044. NK_ASSERT(ctx->current->layout);
  19045. if (!ctx || !ctx->current || !ctx->current->layout)
  19046. return;
  19047. win = ctx->current;
  19048. layout = win->layout;
  19049. nk_panel_layout(ctx, win, height, 1);
  19050. layout->row.type = NK_LAYOUT_TEMPLATE;
  19051. layout->row.columns = 0;
  19052. layout->row.ratio = 0;
  19053. layout->row.item_width = 0;
  19054. layout->row.item_height = 0;
  19055. layout->row.item_offset = 0;
  19056. layout->row.filled = 0;
  19057. layout->row.item.x = 0;
  19058. layout->row.item.y = 0;
  19059. layout->row.item.w = 0;
  19060. layout->row.item.h = 0;
  19061. }
  19062. NK_API void
  19063. nk_layout_row_template_push_dynamic(struct nk_context *ctx)
  19064. {
  19065. struct nk_window *win;
  19066. struct nk_panel *layout;
  19067. NK_ASSERT(ctx);
  19068. NK_ASSERT(ctx->current);
  19069. NK_ASSERT(ctx->current->layout);
  19070. if (!ctx || !ctx->current || !ctx->current->layout)
  19071. return;
  19072. win = ctx->current;
  19073. layout = win->layout;
  19074. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  19075. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  19076. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  19077. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  19078. layout->row.templates[layout->row.columns++] = -1.0f;
  19079. }
  19080. NK_API void
  19081. nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
  19082. {
  19083. struct nk_window *win;
  19084. struct nk_panel *layout;
  19085. NK_ASSERT(ctx);
  19086. NK_ASSERT(ctx->current);
  19087. NK_ASSERT(ctx->current->layout);
  19088. if (!ctx || !ctx->current || !ctx->current->layout)
  19089. return;
  19090. win = ctx->current;
  19091. layout = win->layout;
  19092. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  19093. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  19094. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  19095. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  19096. layout->row.templates[layout->row.columns++] = -min_width;
  19097. }
  19098. NK_API void
  19099. nk_layout_row_template_push_static(struct nk_context *ctx, float width)
  19100. {
  19101. struct nk_window *win;
  19102. struct nk_panel *layout;
  19103. NK_ASSERT(ctx);
  19104. NK_ASSERT(ctx->current);
  19105. NK_ASSERT(ctx->current->layout);
  19106. if (!ctx || !ctx->current || !ctx->current->layout)
  19107. return;
  19108. win = ctx->current;
  19109. layout = win->layout;
  19110. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  19111. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  19112. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  19113. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  19114. layout->row.templates[layout->row.columns++] = width;
  19115. }
  19116. NK_API void
  19117. nk_layout_row_template_end(struct nk_context *ctx)
  19118. {
  19119. struct nk_window *win;
  19120. struct nk_panel *layout;
  19121. int i = 0;
  19122. int variable_count = 0;
  19123. int min_variable_count = 0;
  19124. float min_fixed_width = 0.0f;
  19125. float total_fixed_width = 0.0f;
  19126. float max_variable_width = 0.0f;
  19127. NK_ASSERT(ctx);
  19128. NK_ASSERT(ctx->current);
  19129. NK_ASSERT(ctx->current->layout);
  19130. if (!ctx || !ctx->current || !ctx->current->layout)
  19131. return;
  19132. win = ctx->current;
  19133. layout = win->layout;
  19134. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  19135. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  19136. for (i = 0; i < layout->row.columns; ++i) {
  19137. float width = layout->row.templates[i];
  19138. if (width >= 0.0f) {
  19139. total_fixed_width += width;
  19140. min_fixed_width += width;
  19141. } else if (width < -1.0f) {
  19142. width = -width;
  19143. total_fixed_width += width;
  19144. max_variable_width = NK_MAX(max_variable_width, width);
  19145. variable_count++;
  19146. } else {
  19147. min_variable_count++;
  19148. variable_count++;
  19149. }
  19150. }
  19151. if (variable_count) {
  19152. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  19153. layout->bounds.w, layout->row.columns);
  19154. float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
  19155. int enough_space = var_width >= max_variable_width;
  19156. if (!enough_space)
  19157. var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
  19158. for (i = 0; i < layout->row.columns; ++i) {
  19159. float *width = &layout->row.templates[i];
  19160. *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
  19161. }
  19162. }
  19163. }
  19164. NK_API void
  19165. nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  19166. float height, int widget_count)
  19167. {
  19168. struct nk_window *win;
  19169. struct nk_panel *layout;
  19170. NK_ASSERT(ctx);
  19171. NK_ASSERT(ctx->current);
  19172. NK_ASSERT(ctx->current->layout);
  19173. if (!ctx || !ctx->current || !ctx->current->layout)
  19174. return;
  19175. win = ctx->current;
  19176. layout = win->layout;
  19177. nk_panel_layout(ctx, win, height, widget_count);
  19178. if (fmt == NK_STATIC)
  19179. layout->row.type = NK_LAYOUT_STATIC_FREE;
  19180. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  19181. layout->row.ratio = 0;
  19182. layout->row.filled = 0;
  19183. layout->row.item_width = 0;
  19184. layout->row.item_offset = 0;
  19185. }
  19186. NK_API void
  19187. nk_layout_space_end(struct nk_context *ctx)
  19188. {
  19189. struct nk_window *win;
  19190. struct nk_panel *layout;
  19191. NK_ASSERT(ctx);
  19192. NK_ASSERT(ctx->current);
  19193. NK_ASSERT(ctx->current->layout);
  19194. if (!ctx || !ctx->current || !ctx->current->layout)
  19195. return;
  19196. win = ctx->current;
  19197. layout = win->layout;
  19198. layout->row.item_width = 0;
  19199. layout->row.item_height = 0;
  19200. layout->row.item_offset = 0;
  19201. nk_zero(&layout->row.item, sizeof(layout->row.item));
  19202. }
  19203. NK_API void
  19204. nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
  19205. {
  19206. struct nk_window *win;
  19207. struct nk_panel *layout;
  19208. NK_ASSERT(ctx);
  19209. NK_ASSERT(ctx->current);
  19210. NK_ASSERT(ctx->current->layout);
  19211. if (!ctx || !ctx->current || !ctx->current->layout)
  19212. return;
  19213. win = ctx->current;
  19214. layout = win->layout;
  19215. layout->row.item = rect;
  19216. }
  19217. NK_API struct nk_rect
  19218. nk_layout_space_bounds(struct nk_context *ctx)
  19219. {
  19220. struct nk_rect ret;
  19221. struct nk_window *win;
  19222. struct nk_panel *layout;
  19223. NK_ASSERT(ctx);
  19224. NK_ASSERT(ctx->current);
  19225. NK_ASSERT(ctx->current->layout);
  19226. win = ctx->current;
  19227. layout = win->layout;
  19228. ret.x = layout->clip.x;
  19229. ret.y = layout->clip.y;
  19230. ret.w = layout->clip.w;
  19231. ret.h = layout->row.height;
  19232. return ret;
  19233. }
  19234. NK_API struct nk_rect
  19235. nk_layout_widget_bounds(struct nk_context *ctx)
  19236. {
  19237. struct nk_rect ret;
  19238. struct nk_window *win;
  19239. struct nk_panel *layout;
  19240. NK_ASSERT(ctx);
  19241. NK_ASSERT(ctx->current);
  19242. NK_ASSERT(ctx->current->layout);
  19243. win = ctx->current;
  19244. layout = win->layout;
  19245. ret.x = layout->at_x;
  19246. ret.y = layout->at_y;
  19247. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
  19248. ret.h = layout->row.height;
  19249. return ret;
  19250. }
  19251. NK_API struct nk_vec2
  19252. nk_layout_space_to_screen(struct nk_context *ctx, struct nk_vec2 ret)
  19253. {
  19254. struct nk_window *win;
  19255. struct nk_panel *layout;
  19256. NK_ASSERT(ctx);
  19257. NK_ASSERT(ctx->current);
  19258. NK_ASSERT(ctx->current->layout);
  19259. win = ctx->current;
  19260. layout = win->layout;
  19261. ret.x += layout->at_x - (float)*layout->offset_x;
  19262. ret.y += layout->at_y - (float)*layout->offset_y;
  19263. return ret;
  19264. }
  19265. NK_API struct nk_vec2
  19266. nk_layout_space_to_local(struct nk_context *ctx, struct nk_vec2 ret)
  19267. {
  19268. struct nk_window *win;
  19269. struct nk_panel *layout;
  19270. NK_ASSERT(ctx);
  19271. NK_ASSERT(ctx->current);
  19272. NK_ASSERT(ctx->current->layout);
  19273. win = ctx->current;
  19274. layout = win->layout;
  19275. ret.x += -layout->at_x + (float)*layout->offset_x;
  19276. ret.y += -layout->at_y + (float)*layout->offset_y;
  19277. return ret;
  19278. }
  19279. NK_API struct nk_rect
  19280. nk_layout_space_rect_to_screen(struct nk_context *ctx, struct nk_rect ret)
  19281. {
  19282. struct nk_window *win;
  19283. struct nk_panel *layout;
  19284. NK_ASSERT(ctx);
  19285. NK_ASSERT(ctx->current);
  19286. NK_ASSERT(ctx->current->layout);
  19287. win = ctx->current;
  19288. layout = win->layout;
  19289. ret.x += layout->at_x - (float)*layout->offset_x;
  19290. ret.y += layout->at_y - (float)*layout->offset_y;
  19291. return ret;
  19292. }
  19293. NK_API struct nk_rect
  19294. nk_layout_space_rect_to_local(struct nk_context *ctx, struct nk_rect ret)
  19295. {
  19296. struct nk_window *win;
  19297. struct nk_panel *layout;
  19298. NK_ASSERT(ctx);
  19299. NK_ASSERT(ctx->current);
  19300. NK_ASSERT(ctx->current->layout);
  19301. win = ctx->current;
  19302. layout = win->layout;
  19303. ret.x += -layout->at_x + (float)*layout->offset_x;
  19304. ret.y += -layout->at_y + (float)*layout->offset_y;
  19305. return ret;
  19306. }
  19307. NK_INTERN void
  19308. nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
  19309. {
  19310. struct nk_panel *layout = win->layout;
  19311. struct nk_vec2 spacing = ctx->style.window.spacing;
  19312. const float row_height = layout->row.height - spacing.y;
  19313. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  19314. }
  19315. NK_INTERN void
  19316. nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
  19317. struct nk_window *win, int modify)
  19318. {
  19319. struct nk_panel *layout;
  19320. const struct nk_style *style;
  19321. struct nk_vec2 spacing;
  19322. struct nk_vec2 padding;
  19323. float item_offset = 0;
  19324. float item_width = 0;
  19325. float item_spacing = 0;
  19326. float panel_space = 0;
  19327. NK_ASSERT(ctx);
  19328. NK_ASSERT(ctx->current);
  19329. NK_ASSERT(ctx->current->layout);
  19330. if (!ctx || !ctx->current || !ctx->current->layout)
  19331. return;
  19332. win = ctx->current;
  19333. layout = win->layout;
  19334. style = &ctx->style;
  19335. NK_ASSERT(bounds);
  19336. spacing = style->window.spacing;
  19337. padding = nk_panel_get_padding(style, layout->type);
  19338. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  19339. layout->bounds.w, layout->row.columns);
  19340. /* calculate the width of one item inside the current layout space */
  19341. switch (layout->row.type) {
  19342. case NK_LAYOUT_DYNAMIC_FIXED: {
  19343. /* scaling fixed size widgets item width */
  19344. item_width = NK_MAX(1.0f,panel_space) / (float)layout->row.columns;
  19345. item_offset = (float)layout->row.index * item_width;
  19346. item_spacing = (float)layout->row.index * spacing.x;
  19347. } break;
  19348. case NK_LAYOUT_DYNAMIC_ROW: {
  19349. /* scaling single ratio widget width */
  19350. item_width = layout->row.item_width * panel_space;
  19351. item_offset = layout->row.item_offset;
  19352. item_spacing = 0;
  19353. if (modify) {
  19354. layout->row.item_offset += item_width + spacing.x;
  19355. layout->row.filled += layout->row.item_width;
  19356. layout->row.index = 0;
  19357. }
  19358. } break;
  19359. case NK_LAYOUT_DYNAMIC_FREE: {
  19360. /* panel width depended free widget placing */
  19361. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  19362. bounds->x -= (float)*layout->offset_x;
  19363. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  19364. bounds->y -= (float)*layout->offset_y;
  19365. bounds->w = layout->bounds.w * layout->row.item.w;
  19366. bounds->h = layout->row.height * layout->row.item.h;
  19367. return;
  19368. } break;
  19369. case NK_LAYOUT_DYNAMIC: {
  19370. /* scaling arrays of panel width ratios for every widget */
  19371. float ratio;
  19372. NK_ASSERT(layout->row.ratio);
  19373. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  19374. layout->row.item_width : layout->row.ratio[layout->row.index];
  19375. item_spacing = (float)layout->row.index * spacing.x;
  19376. item_width = (ratio * panel_space);
  19377. item_offset = layout->row.item_offset;
  19378. if (modify) {
  19379. layout->row.item_offset += item_width;
  19380. layout->row.filled += ratio;
  19381. }
  19382. } break;
  19383. case NK_LAYOUT_STATIC_FIXED: {
  19384. /* non-scaling fixed widgets item width */
  19385. item_width = layout->row.item_width;
  19386. item_offset = (float)layout->row.index * item_width;
  19387. item_spacing = (float)layout->row.index * spacing.x;
  19388. } break;
  19389. case NK_LAYOUT_STATIC_ROW: {
  19390. /* scaling single ratio widget width */
  19391. item_width = layout->row.item_width;
  19392. item_offset = layout->row.item_offset;
  19393. item_spacing = (float)layout->row.index * spacing.x;
  19394. if (modify) layout->row.item_offset += item_width;
  19395. } break;
  19396. case NK_LAYOUT_STATIC_FREE: {
  19397. /* free widget placing */
  19398. bounds->x = layout->at_x + layout->row.item.x;
  19399. bounds->w = layout->row.item.w;
  19400. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  19401. layout->max_x = (bounds->x + bounds->w);
  19402. bounds->x -= (float)*layout->offset_x;
  19403. bounds->y = layout->at_y + layout->row.item.y;
  19404. bounds->y -= (float)*layout->offset_y;
  19405. bounds->h = layout->row.item.h;
  19406. return;
  19407. } break;
  19408. case NK_LAYOUT_STATIC: {
  19409. /* non-scaling array of panel pixel width for every widget */
  19410. item_spacing = (float)layout->row.index * spacing.x;
  19411. item_width = layout->row.ratio[layout->row.index];
  19412. item_offset = layout->row.item_offset;
  19413. if (modify) layout->row.item_offset += item_width;
  19414. } break;
  19415. case NK_LAYOUT_TEMPLATE: {
  19416. /* stretchy row layout with combined dynamic/static widget width*/
  19417. NK_ASSERT(layout->row.index < layout->row.columns);
  19418. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  19419. item_width = layout->row.templates[layout->row.index];
  19420. item_offset = layout->row.item_offset;
  19421. item_spacing = (float)layout->row.index * spacing.x;
  19422. if (modify) layout->row.item_offset += item_width;
  19423. } break;
  19424. default: NK_ASSERT(0); break;
  19425. };
  19426. /* set the bounds of the newly allocated widget */
  19427. bounds->w = item_width;
  19428. bounds->h = layout->row.height - spacing.y;
  19429. bounds->y = layout->at_y - (float)*layout->offset_y;
  19430. bounds->x = layout->at_x + item_offset + item_spacing + padding.x;
  19431. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  19432. layout->max_x = bounds->x + bounds->w;
  19433. bounds->x -= (float)*layout->offset_x;
  19434. }
  19435. NK_INTERN void
  19436. nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
  19437. {
  19438. struct nk_window *win;
  19439. struct nk_panel *layout;
  19440. NK_ASSERT(ctx);
  19441. NK_ASSERT(ctx->current);
  19442. NK_ASSERT(ctx->current->layout);
  19443. if (!ctx || !ctx->current || !ctx->current->layout)
  19444. return;
  19445. /* check if the end of the row has been hit and begin new row if so */
  19446. win = ctx->current;
  19447. layout = win->layout;
  19448. if (layout->row.index >= layout->row.columns)
  19449. nk_panel_alloc_row(ctx, win);
  19450. /* calculate widget position and size */
  19451. nk_layout_widget_space(bounds, ctx, win, nk_true);
  19452. layout->row.index++;
  19453. }
  19454. NK_INTERN void
  19455. nk_layout_peek(struct nk_rect *bounds, struct nk_context *ctx)
  19456. {
  19457. float y;
  19458. int index;
  19459. struct nk_window *win;
  19460. struct nk_panel *layout;
  19461. NK_ASSERT(ctx);
  19462. NK_ASSERT(ctx->current);
  19463. NK_ASSERT(ctx->current->layout);
  19464. if (!ctx || !ctx->current || !ctx->current->layout)
  19465. return;
  19466. win = ctx->current;
  19467. layout = win->layout;
  19468. y = layout->at_y;
  19469. index = layout->row.index;
  19470. if (layout->row.index >= layout->row.columns) {
  19471. layout->at_y += layout->row.height;
  19472. layout->row.index = 0;
  19473. }
  19474. nk_layout_widget_space(bounds, ctx, win, nk_false);
  19475. if (!layout->row.index) {
  19476. bounds->x -= layout->row.item_offset;
  19477. }
  19478. layout->at_y = y;
  19479. layout->row.index = index;
  19480. }
  19481. NK_INTERN int
  19482. nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
  19483. struct nk_image *img, const char *title, enum nk_collapse_states *state)
  19484. {
  19485. struct nk_window *win;
  19486. struct nk_panel *layout;
  19487. const struct nk_style *style;
  19488. struct nk_command_buffer *out;
  19489. const struct nk_input *in;
  19490. const struct nk_style_button *button;
  19491. enum nk_symbol_type symbol;
  19492. float row_height;
  19493. struct nk_vec2 item_spacing;
  19494. struct nk_rect header = {0,0,0,0};
  19495. struct nk_rect sym = {0,0,0,0};
  19496. struct nk_text text;
  19497. nk_flags ws = 0;
  19498. enum nk_widget_layout_states widget_state;
  19499. NK_ASSERT(ctx);
  19500. NK_ASSERT(ctx->current);
  19501. NK_ASSERT(ctx->current->layout);
  19502. if (!ctx || !ctx->current || !ctx->current->layout)
  19503. return 0;
  19504. /* cache some data */
  19505. win = ctx->current;
  19506. layout = win->layout;
  19507. out = &win->buffer;
  19508. style = &ctx->style;
  19509. item_spacing = style->window.spacing;
  19510. /* calculate header bounds and draw background */
  19511. row_height = style->font->height + 2 * style->tab.padding.y;
  19512. nk_layout_set_min_row_height(ctx, row_height);
  19513. nk_layout_row_dynamic(ctx, row_height, 1);
  19514. nk_layout_reset_min_row_height(ctx);
  19515. widget_state = nk_widget(&header, ctx);
  19516. if (type == NK_TREE_TAB) {
  19517. const struct nk_style_item *background = &style->tab.background;
  19518. if (background->type == NK_STYLE_ITEM_IMAGE) {
  19519. nk_draw_image(out, header, &background->data.image, nk_white);
  19520. text.background = nk_rgba(0,0,0,0);
  19521. } else {
  19522. text.background = background->data.color;
  19523. nk_fill_rect(out, header, 0, style->tab.border_color);
  19524. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  19525. style->tab.rounding, background->data.color);
  19526. }
  19527. } else text.background = style->window.background;
  19528. /* update node state */
  19529. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  19530. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  19531. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  19532. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  19533. /* select correct button style */
  19534. if (*state == NK_MAXIMIZED) {
  19535. symbol = style->tab.sym_maximize;
  19536. if (type == NK_TREE_TAB)
  19537. button = &style->tab.tab_maximize_button;
  19538. else button = &style->tab.node_maximize_button;
  19539. } else {
  19540. symbol = style->tab.sym_minimize;
  19541. if (type == NK_TREE_TAB)
  19542. button = &style->tab.tab_minimize_button;
  19543. else button = &style->tab.node_minimize_button;
  19544. }
  19545. {/* draw triangle button */
  19546. sym.w = sym.h = style->font->height;
  19547. sym.y = header.y + style->tab.padding.y;
  19548. sym.x = header.x + style->tab.padding.x;
  19549. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  19550. button, 0, style->font);
  19551. if (img) {
  19552. /* draw optional image icon */
  19553. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  19554. nk_draw_image(&win->buffer, sym, img, nk_white);
  19555. sym.w = style->font->height + style->tab.spacing.x;}
  19556. }
  19557. {/* draw label */
  19558. struct nk_rect label;
  19559. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  19560. label.x = sym.x + sym.w + item_spacing.x;
  19561. label.y = sym.y;
  19562. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  19563. label.h = style->font->height;
  19564. text.text = style->tab.text;
  19565. text.padding = nk_vec2(0,0);
  19566. nk_widget_text(out, label, title, nk_strlen(title), &text,
  19567. NK_TEXT_LEFT, style->font);}
  19568. /* increase x-axis cursor widget position pointer */
  19569. if (*state == NK_MAXIMIZED) {
  19570. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  19571. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  19572. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  19573. layout->row.tree_depth++;
  19574. return nk_true;
  19575. } else return nk_false;
  19576. }
  19577. NK_INTERN int
  19578. nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
  19579. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  19580. const char *hash, int len, int line)
  19581. {
  19582. struct nk_window *win = ctx->current;
  19583. int title_len = 0;
  19584. nk_hash tree_hash = 0;
  19585. nk_uint *state = 0;
  19586. /* retrieve tree state from internal widget state tables */
  19587. if (!hash) {
  19588. title_len = (int)nk_strlen(title);
  19589. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  19590. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  19591. state = nk_find_value(win, tree_hash);
  19592. if (!state) {
  19593. state = nk_add_value(ctx, win, tree_hash, 0);
  19594. *state = initial_state;
  19595. }
  19596. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  19597. }
  19598. NK_API int
  19599. nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
  19600. const char *title, enum nk_collapse_states *state)
  19601. {return nk_tree_state_base(ctx, type, 0, title, state);}
  19602. NK_API int
  19603. nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
  19604. struct nk_image img, const char *title, enum nk_collapse_states *state)
  19605. {return nk_tree_state_base(ctx, type, &img, title, state);}
  19606. NK_API void
  19607. nk_tree_state_pop(struct nk_context *ctx)
  19608. {
  19609. struct nk_window *win = 0;
  19610. struct nk_panel *layout = 0;
  19611. NK_ASSERT(ctx);
  19612. NK_ASSERT(ctx->current);
  19613. NK_ASSERT(ctx->current->layout);
  19614. if (!ctx || !ctx->current || !ctx->current->layout)
  19615. return;
  19616. win = ctx->current;
  19617. layout = win->layout;
  19618. layout->at_x -= ctx->style.tab.indent + ctx->style.window.padding.x;
  19619. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  19620. NK_ASSERT(layout->row.tree_depth);
  19621. layout->row.tree_depth--;
  19622. }
  19623. NK_API int
  19624. nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  19625. const char *title, enum nk_collapse_states initial_state,
  19626. const char *hash, int len, int line)
  19627. {return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);}
  19628. NK_API int
  19629. nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  19630. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  19631. const char *hash, int len,int seed)
  19632. {return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);}
  19633. NK_API void
  19634. nk_tree_pop(struct nk_context *ctx)
  19635. {nk_tree_state_pop(ctx);}
  19636. /*----------------------------------------------------------------
  19637. *
  19638. * WIDGETS
  19639. *
  19640. * --------------------------------------------------------------*/
  19641. NK_API struct nk_rect
  19642. nk_widget_bounds(struct nk_context *ctx)
  19643. {
  19644. struct nk_rect bounds;
  19645. NK_ASSERT(ctx);
  19646. NK_ASSERT(ctx->current);
  19647. if (!ctx || !ctx->current)
  19648. return nk_rect(0,0,0,0);
  19649. nk_layout_peek(&bounds, ctx);
  19650. return bounds;
  19651. }
  19652. NK_API struct nk_vec2
  19653. nk_widget_position(struct nk_context *ctx)
  19654. {
  19655. struct nk_rect bounds;
  19656. NK_ASSERT(ctx);
  19657. NK_ASSERT(ctx->current);
  19658. if (!ctx || !ctx->current)
  19659. return nk_vec2(0,0);
  19660. nk_layout_peek(&bounds, ctx);
  19661. return nk_vec2(bounds.x, bounds.y);
  19662. }
  19663. NK_API struct nk_vec2
  19664. nk_widget_size(struct nk_context *ctx)
  19665. {
  19666. struct nk_rect bounds;
  19667. NK_ASSERT(ctx);
  19668. NK_ASSERT(ctx->current);
  19669. if (!ctx || !ctx->current)
  19670. return nk_vec2(0,0);
  19671. nk_layout_peek(&bounds, ctx);
  19672. return nk_vec2(bounds.w, bounds.h);
  19673. }
  19674. NK_API float
  19675. nk_widget_width(struct nk_context *ctx)
  19676. {
  19677. struct nk_rect bounds;
  19678. NK_ASSERT(ctx);
  19679. NK_ASSERT(ctx->current);
  19680. if (!ctx || !ctx->current)
  19681. return 0;
  19682. nk_layout_peek(&bounds, ctx);
  19683. return bounds.w;
  19684. }
  19685. NK_API float
  19686. nk_widget_height(struct nk_context *ctx)
  19687. {
  19688. struct nk_rect bounds;
  19689. NK_ASSERT(ctx);
  19690. NK_ASSERT(ctx->current);
  19691. if (!ctx || !ctx->current)
  19692. return 0;
  19693. nk_layout_peek(&bounds, ctx);
  19694. return bounds.h;
  19695. }
  19696. NK_API int
  19697. nk_widget_is_hovered(struct nk_context *ctx)
  19698. {
  19699. struct nk_rect c, v;
  19700. struct nk_rect bounds;
  19701. NK_ASSERT(ctx);
  19702. NK_ASSERT(ctx->current);
  19703. if (!ctx || !ctx->current || ctx->active != ctx->current)
  19704. return 0;
  19705. c = ctx->current->layout->clip;
  19706. c.x = (float)((int)c.x);
  19707. c.y = (float)((int)c.y);
  19708. c.w = (float)((int)c.w);
  19709. c.h = (float)((int)c.h);
  19710. nk_layout_peek(&bounds, ctx);
  19711. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  19712. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  19713. return 0;
  19714. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  19715. }
  19716. NK_API int
  19717. nk_widget_is_mouse_clicked(struct nk_context *ctx, enum nk_buttons btn)
  19718. {
  19719. struct nk_rect c, v;
  19720. struct nk_rect bounds;
  19721. NK_ASSERT(ctx);
  19722. NK_ASSERT(ctx->current);
  19723. if (!ctx || !ctx->current || ctx->active != ctx->current)
  19724. return 0;
  19725. c = ctx->current->layout->clip;
  19726. c.x = (float)((int)c.x);
  19727. c.y = (float)((int)c.y);
  19728. c.w = (float)((int)c.w);
  19729. c.h = (float)((int)c.h);
  19730. nk_layout_peek(&bounds, ctx);
  19731. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  19732. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  19733. return 0;
  19734. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  19735. }
  19736. NK_API int
  19737. nk_widget_has_mouse_click_down(struct nk_context *ctx, enum nk_buttons btn, int down)
  19738. {
  19739. struct nk_rect c, v;
  19740. struct nk_rect bounds;
  19741. NK_ASSERT(ctx);
  19742. NK_ASSERT(ctx->current);
  19743. if (!ctx || !ctx->current || ctx->active != ctx->current)
  19744. return 0;
  19745. c = ctx->current->layout->clip;
  19746. c.x = (float)((int)c.x);
  19747. c.y = (float)((int)c.y);
  19748. c.w = (float)((int)c.w);
  19749. c.h = (float)((int)c.h);
  19750. nk_layout_peek(&bounds, ctx);
  19751. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  19752. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  19753. return 0;
  19754. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  19755. }
  19756. NK_API enum nk_widget_layout_states
  19757. nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
  19758. {
  19759. struct nk_rect c, v;
  19760. struct nk_window *win;
  19761. struct nk_panel *layout;
  19762. const struct nk_input *in;
  19763. NK_ASSERT(ctx);
  19764. NK_ASSERT(ctx->current);
  19765. NK_ASSERT(ctx->current->layout);
  19766. if (!ctx || !ctx->current || !ctx->current->layout)
  19767. return NK_WIDGET_INVALID;
  19768. /* allocate space and check if the widget needs to be updated and drawn */
  19769. nk_panel_alloc_space(bounds, ctx);
  19770. win = ctx->current;
  19771. layout = win->layout;
  19772. in = &ctx->input;
  19773. c = layout->clip;
  19774. /* if one of these triggers you forgot to add an `if` condition around either
  19775. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  19776. Example:
  19777. if (nk_begin(...) {...} nk_end(...); or
  19778. if (nk_group_begin(...) { nk_group_end(...);} */
  19779. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  19780. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  19781. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  19782. /* need to convert to int here to remove floating point errors */
  19783. bounds->x = (float)((int)bounds->x);
  19784. bounds->y = (float)((int)bounds->y);
  19785. bounds->w = (float)((int)bounds->w);
  19786. bounds->h = (float)((int)bounds->h);
  19787. c.x = (float)((int)c.x);
  19788. c.y = (float)((int)c.y);
  19789. c.w = (float)((int)c.w);
  19790. c.h = (float)((int)c.h);
  19791. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  19792. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  19793. return NK_WIDGET_INVALID;
  19794. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  19795. return NK_WIDGET_ROM;
  19796. return NK_WIDGET_VALID;
  19797. }
  19798. NK_API enum nk_widget_layout_states
  19799. nk_widget_fitting(struct nk_rect *bounds, struct nk_context *ctx,
  19800. struct nk_vec2 item_padding)
  19801. {
  19802. /* update the bounds to stand without padding */
  19803. struct nk_window *win;
  19804. struct nk_style *style;
  19805. struct nk_panel *layout;
  19806. enum nk_widget_layout_states state;
  19807. struct nk_vec2 panel_padding;
  19808. NK_ASSERT(ctx);
  19809. NK_ASSERT(ctx->current);
  19810. NK_ASSERT(ctx->current->layout);
  19811. if (!ctx || !ctx->current || !ctx->current->layout)
  19812. return NK_WIDGET_INVALID;
  19813. win = ctx->current;
  19814. style = &ctx->style;
  19815. layout = win->layout;
  19816. state = nk_widget(bounds, ctx);
  19817. panel_padding = nk_panel_get_padding(style, layout->type);
  19818. if (layout->row.index == 1) {
  19819. bounds->w += panel_padding.x;
  19820. bounds->x -= panel_padding.x;
  19821. } else bounds->x -= item_padding.x;
  19822. if (layout->row.index == layout->row.columns)
  19823. bounds->w += panel_padding.x;
  19824. else bounds->w += item_padding.x;
  19825. return state;
  19826. }
  19827. /*----------------------------------------------------------------
  19828. *
  19829. * MISC
  19830. *
  19831. * --------------------------------------------------------------*/
  19832. NK_API void
  19833. nk_spacing(struct nk_context *ctx, int cols)
  19834. {
  19835. struct nk_window *win;
  19836. struct nk_panel *layout;
  19837. struct nk_rect none;
  19838. int i, index, rows;
  19839. NK_ASSERT(ctx);
  19840. NK_ASSERT(ctx->current);
  19841. NK_ASSERT(ctx->current->layout);
  19842. if (!ctx || !ctx->current || !ctx->current->layout)
  19843. return;
  19844. /* spacing over row boundaries */
  19845. win = ctx->current;
  19846. layout = win->layout;
  19847. index = (layout->row.index + cols) % layout->row.columns;
  19848. rows = (layout->row.index + cols) / layout->row.columns;
  19849. if (rows) {
  19850. for (i = 0; i < rows; ++i)
  19851. nk_panel_alloc_row(ctx, win);
  19852. cols = index;
  19853. }
  19854. /* non table layout need to allocate space */
  19855. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  19856. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  19857. for (i = 0; i < cols; ++i)
  19858. nk_panel_alloc_space(&none, ctx);
  19859. }
  19860. layout->row.index = index;
  19861. }
  19862. /*----------------------------------------------------------------
  19863. *
  19864. * TEXT
  19865. *
  19866. * --------------------------------------------------------------*/
  19867. NK_API void
  19868. nk_text_colored(struct nk_context *ctx, const char *str, int len,
  19869. nk_flags alignment, struct nk_color color)
  19870. {
  19871. struct nk_window *win;
  19872. const struct nk_style *style;
  19873. struct nk_vec2 item_padding;
  19874. struct nk_rect bounds;
  19875. struct nk_text text;
  19876. NK_ASSERT(ctx);
  19877. NK_ASSERT(ctx->current);
  19878. NK_ASSERT(ctx->current->layout);
  19879. if (!ctx || !ctx->current || !ctx->current->layout) return;
  19880. win = ctx->current;
  19881. style = &ctx->style;
  19882. nk_panel_alloc_space(&bounds, ctx);
  19883. item_padding = style->text.padding;
  19884. text.padding.x = item_padding.x;
  19885. text.padding.y = item_padding.y;
  19886. text.background = style->window.background;
  19887. text.text = color;
  19888. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  19889. }
  19890. NK_API void
  19891. nk_text_wrap_colored(struct nk_context *ctx, const char *str,
  19892. int len, struct nk_color color)
  19893. {
  19894. struct nk_window *win;
  19895. const struct nk_style *style;
  19896. struct nk_vec2 item_padding;
  19897. struct nk_rect bounds;
  19898. struct nk_text text;
  19899. NK_ASSERT(ctx);
  19900. NK_ASSERT(ctx->current);
  19901. NK_ASSERT(ctx->current->layout);
  19902. if (!ctx || !ctx->current || !ctx->current->layout) return;
  19903. win = ctx->current;
  19904. style = &ctx->style;
  19905. nk_panel_alloc_space(&bounds, ctx);
  19906. item_padding = style->text.padding;
  19907. text.padding.x = item_padding.x;
  19908. text.padding.y = item_padding.y;
  19909. text.background = style->window.background;
  19910. text.text = color;
  19911. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  19912. }
  19913. #ifdef NK_INCLUDE_STANDARD_VARARGS
  19914. NK_API void
  19915. nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
  19916. struct nk_color color, const char *fmt, ...)
  19917. {
  19918. char buf[256];
  19919. va_list args;
  19920. va_start(args, fmt);
  19921. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  19922. nk_label_colored(ctx, buf, flags, color);
  19923. va_end(args);
  19924. }
  19925. NK_API void
  19926. nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
  19927. const char *fmt, ...)
  19928. {
  19929. char buf[256];
  19930. va_list args;
  19931. va_start(args, fmt);
  19932. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  19933. nk_label_colored_wrap(ctx, buf, color);
  19934. va_end(args);
  19935. }
  19936. NK_API void
  19937. nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
  19938. {
  19939. char buf[256];
  19940. va_list args;
  19941. va_start(args, fmt);
  19942. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  19943. nk_label(ctx, buf, flags);
  19944. va_end(args);
  19945. }
  19946. NK_API void
  19947. nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
  19948. {
  19949. char buf[256];
  19950. va_list args;
  19951. va_start(args, fmt);
  19952. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  19953. nk_label_wrap(ctx, buf);
  19954. va_end(args);
  19955. }
  19956. NK_API void
  19957. nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
  19958. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));}
  19959. NK_API void
  19960. nk_value_int(struct nk_context *ctx, const char *prefix, int value)
  19961. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);}
  19962. NK_API void
  19963. nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
  19964. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);}
  19965. NK_API void
  19966. nk_value_float(struct nk_context *ctx, const char *prefix, float value)
  19967. {
  19968. double double_value = (double)value;
  19969. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  19970. }
  19971. NK_API void
  19972. nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
  19973. {nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);}
  19974. NK_API void
  19975. nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
  19976. {
  19977. double c[4]; nk_color_dv(c, color);
  19978. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  19979. p, c[0], c[1], c[2], c[3]);
  19980. }
  19981. NK_API void
  19982. nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
  19983. {
  19984. char hex[16];
  19985. nk_color_hex_rgba(hex, color);
  19986. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  19987. }
  19988. #endif
  19989. NK_API void
  19990. nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
  19991. {
  19992. NK_ASSERT(ctx);
  19993. if (!ctx) return;
  19994. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  19995. }
  19996. NK_API void
  19997. nk_text_wrap(struct nk_context *ctx, const char *str, int len)
  19998. {
  19999. NK_ASSERT(ctx);
  20000. if (!ctx) return;
  20001. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  20002. }
  20003. NK_API void
  20004. nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
  20005. {nk_text(ctx, str, nk_strlen(str), alignment);}
  20006. NK_API void
  20007. nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
  20008. struct nk_color color)
  20009. {nk_text_colored(ctx, str, nk_strlen(str), align, color);}
  20010. NK_API void
  20011. nk_label_wrap(struct nk_context *ctx, const char *str)
  20012. {nk_text_wrap(ctx, str, nk_strlen(str));}
  20013. NK_API void
  20014. nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
  20015. {nk_text_wrap_colored(ctx, str, nk_strlen(str), color);}
  20016. NK_API void
  20017. nk_image(struct nk_context *ctx, struct nk_image img)
  20018. {
  20019. struct nk_window *win;
  20020. struct nk_rect bounds;
  20021. NK_ASSERT(ctx);
  20022. NK_ASSERT(ctx->current);
  20023. NK_ASSERT(ctx->current->layout);
  20024. if (!ctx || !ctx->current || !ctx->current->layout) return;
  20025. win = ctx->current;
  20026. if (!nk_widget(&bounds, ctx)) return;
  20027. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  20028. }
  20029. /*----------------------------------------------------------------
  20030. *
  20031. * BUTTON
  20032. *
  20033. * --------------------------------------------------------------*/
  20034. NK_API void
  20035. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  20036. {
  20037. NK_ASSERT(ctx);
  20038. if (!ctx) return;
  20039. ctx->button_behavior = behavior;
  20040. }
  20041. NK_API int
  20042. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  20043. {
  20044. struct nk_config_stack_button_behavior *button_stack;
  20045. struct nk_config_stack_button_behavior_element *element;
  20046. NK_ASSERT(ctx);
  20047. if (!ctx) return 0;
  20048. button_stack = &ctx->stacks.button_behaviors;
  20049. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  20050. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  20051. return 0;
  20052. element = &button_stack->elements[button_stack->head++];
  20053. element->address = &ctx->button_behavior;
  20054. element->old_value = ctx->button_behavior;
  20055. ctx->button_behavior = behavior;
  20056. return 1;
  20057. }
  20058. NK_API int
  20059. nk_button_pop_behavior(struct nk_context *ctx)
  20060. {
  20061. struct nk_config_stack_button_behavior *button_stack;
  20062. struct nk_config_stack_button_behavior_element *element;
  20063. NK_ASSERT(ctx);
  20064. if (!ctx) return 0;
  20065. button_stack = &ctx->stacks.button_behaviors;
  20066. NK_ASSERT(button_stack->head > 0);
  20067. if (button_stack->head < 1)
  20068. return 0;
  20069. element = &button_stack->elements[--button_stack->head];
  20070. *element->address = element->old_value;
  20071. return 1;
  20072. }
  20073. NK_API int
  20074. nk_button_text_styled(struct nk_context *ctx,
  20075. const struct nk_style_button *style, const char *title, int len)
  20076. {
  20077. struct nk_window *win;
  20078. struct nk_panel *layout;
  20079. const struct nk_input *in;
  20080. struct nk_rect bounds;
  20081. enum nk_widget_layout_states state;
  20082. NK_ASSERT(ctx);
  20083. NK_ASSERT(style);
  20084. NK_ASSERT(ctx->current);
  20085. NK_ASSERT(ctx->current->layout);
  20086. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  20087. win = ctx->current;
  20088. layout = win->layout;
  20089. state = nk_widget(&bounds, ctx);
  20090. if (!state) return 0;
  20091. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20092. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  20093. title, len, style->text_alignment, ctx->button_behavior,
  20094. style, in, ctx->style.font);
  20095. }
  20096. NK_API int
  20097. nk_button_text(struct nk_context *ctx, const char *title, int len)
  20098. {
  20099. NK_ASSERT(ctx);
  20100. if (!ctx) return 0;
  20101. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  20102. }
  20103. NK_API int nk_button_label_styled(struct nk_context *ctx,
  20104. const struct nk_style_button *style, const char *title)
  20105. {return nk_button_text_styled(ctx, style, title, nk_strlen(title));}
  20106. NK_API int nk_button_label(struct nk_context *ctx, const char *title)
  20107. {return nk_button_text(ctx, title, nk_strlen(title));}
  20108. NK_API int
  20109. nk_button_color(struct nk_context *ctx, struct nk_color color)
  20110. {
  20111. struct nk_window *win;
  20112. struct nk_panel *layout;
  20113. const struct nk_input *in;
  20114. struct nk_style_button button;
  20115. int ret = 0;
  20116. struct nk_rect bounds;
  20117. struct nk_rect content;
  20118. enum nk_widget_layout_states state;
  20119. NK_ASSERT(ctx);
  20120. NK_ASSERT(ctx->current);
  20121. NK_ASSERT(ctx->current->layout);
  20122. if (!ctx || !ctx->current || !ctx->current->layout)
  20123. return 0;
  20124. win = ctx->current;
  20125. layout = win->layout;
  20126. state = nk_widget(&bounds, ctx);
  20127. if (!state) return 0;
  20128. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20129. button = ctx->style.button;
  20130. button.normal = nk_style_item_color(color);
  20131. button.hover = nk_style_item_color(color);
  20132. button.active = nk_style_item_color(color);
  20133. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  20134. &button, in, ctx->button_behavior, &content);
  20135. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  20136. return ret;
  20137. }
  20138. NK_API int
  20139. nk_button_symbol_styled(struct nk_context *ctx,
  20140. const struct nk_style_button *style, enum nk_symbol_type symbol)
  20141. {
  20142. struct nk_window *win;
  20143. struct nk_panel *layout;
  20144. const struct nk_input *in;
  20145. struct nk_rect bounds;
  20146. enum nk_widget_layout_states state;
  20147. NK_ASSERT(ctx);
  20148. NK_ASSERT(ctx->current);
  20149. NK_ASSERT(ctx->current->layout);
  20150. if (!ctx || !ctx->current || !ctx->current->layout)
  20151. return 0;
  20152. win = ctx->current;
  20153. layout = win->layout;
  20154. state = nk_widget(&bounds, ctx);
  20155. if (!state) return 0;
  20156. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20157. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  20158. symbol, ctx->button_behavior, style, in, ctx->style.font);
  20159. }
  20160. NK_API int
  20161. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  20162. {
  20163. NK_ASSERT(ctx);
  20164. if (!ctx) return 0;
  20165. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  20166. }
  20167. NK_API int
  20168. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  20169. struct nk_image img)
  20170. {
  20171. struct nk_window *win;
  20172. struct nk_panel *layout;
  20173. const struct nk_input *in;
  20174. struct nk_rect bounds;
  20175. enum nk_widget_layout_states state;
  20176. NK_ASSERT(ctx);
  20177. NK_ASSERT(ctx->current);
  20178. NK_ASSERT(ctx->current->layout);
  20179. if (!ctx || !ctx->current || !ctx->current->layout)
  20180. return 0;
  20181. win = ctx->current;
  20182. layout = win->layout;
  20183. state = nk_widget(&bounds, ctx);
  20184. if (!state) return 0;
  20185. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20186. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  20187. img, ctx->button_behavior, style, in);
  20188. }
  20189. NK_API int
  20190. nk_button_image(struct nk_context *ctx, struct nk_image img)
  20191. {
  20192. NK_ASSERT(ctx);
  20193. if (!ctx) return 0;
  20194. return nk_button_image_styled(ctx, &ctx->style.button, img);
  20195. }
  20196. NK_API int
  20197. nk_button_symbol_text_styled(struct nk_context *ctx,
  20198. const struct nk_style_button *style, enum nk_symbol_type symbol,
  20199. const char *text, int len, nk_flags align)
  20200. {
  20201. struct nk_window *win;
  20202. struct nk_panel *layout;
  20203. const struct nk_input *in;
  20204. struct nk_rect bounds;
  20205. enum nk_widget_layout_states state;
  20206. NK_ASSERT(ctx);
  20207. NK_ASSERT(ctx->current);
  20208. NK_ASSERT(ctx->current->layout);
  20209. if (!ctx || !ctx->current || !ctx->current->layout)
  20210. return 0;
  20211. win = ctx->current;
  20212. layout = win->layout;
  20213. state = nk_widget(&bounds, ctx);
  20214. if (!state) return 0;
  20215. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20216. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  20217. symbol, text, len, align, ctx->button_behavior,
  20218. style, ctx->style.font, in);
  20219. }
  20220. NK_API int
  20221. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  20222. const char* text, int len, nk_flags align)
  20223. {
  20224. NK_ASSERT(ctx);
  20225. if (!ctx) return 0;
  20226. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  20227. }
  20228. NK_API int nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  20229. const char *label, nk_flags align)
  20230. {return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);}
  20231. NK_API int nk_button_symbol_label_styled(struct nk_context *ctx,
  20232. const struct nk_style_button *style, enum nk_symbol_type symbol,
  20233. const char *title, nk_flags align)
  20234. {return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);}
  20235. NK_API int
  20236. nk_button_image_text_styled(struct nk_context *ctx,
  20237. const struct nk_style_button *style, struct nk_image img, const char *text,
  20238. int len, nk_flags align)
  20239. {
  20240. struct nk_window *win;
  20241. struct nk_panel *layout;
  20242. const struct nk_input *in;
  20243. struct nk_rect bounds;
  20244. enum nk_widget_layout_states state;
  20245. NK_ASSERT(ctx);
  20246. NK_ASSERT(ctx->current);
  20247. NK_ASSERT(ctx->current->layout);
  20248. if (!ctx || !ctx->current || !ctx->current->layout)
  20249. return 0;
  20250. win = ctx->current;
  20251. layout = win->layout;
  20252. state = nk_widget(&bounds, ctx);
  20253. if (!state) return 0;
  20254. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20255. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  20256. bounds, img, text, len, align, ctx->button_behavior,
  20257. style, ctx->style.font, in);
  20258. }
  20259. NK_API int
  20260. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  20261. const char *text, int len, nk_flags align)
  20262. {return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);}
  20263. NK_API int nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  20264. const char *label, nk_flags align)
  20265. {return nk_button_image_text(ctx, img, label, nk_strlen(label), align);}
  20266. NK_API int nk_button_image_label_styled(struct nk_context *ctx,
  20267. const struct nk_style_button *style, struct nk_image img,
  20268. const char *label, nk_flags text_alignment)
  20269. {return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);}
  20270. /*----------------------------------------------------------------
  20271. *
  20272. * SELECTABLE
  20273. *
  20274. * --------------------------------------------------------------*/
  20275. NK_API int
  20276. nk_selectable_text(struct nk_context *ctx, const char *str, int len,
  20277. nk_flags align, int *value)
  20278. {
  20279. struct nk_window *win;
  20280. struct nk_panel *layout;
  20281. const struct nk_input *in;
  20282. const struct nk_style *style;
  20283. enum nk_widget_layout_states state;
  20284. struct nk_rect bounds;
  20285. NK_ASSERT(ctx);
  20286. NK_ASSERT(value);
  20287. NK_ASSERT(ctx->current);
  20288. NK_ASSERT(ctx->current->layout);
  20289. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  20290. return 0;
  20291. win = ctx->current;
  20292. layout = win->layout;
  20293. style = &ctx->style;
  20294. state = nk_widget(&bounds, ctx);
  20295. if (!state) return 0;
  20296. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20297. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  20298. str, len, align, value, &style->selectable, in, style->font);
  20299. }
  20300. NK_API int
  20301. nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
  20302. const char *str, int len, nk_flags align, int *value)
  20303. {
  20304. struct nk_window *win;
  20305. struct nk_panel *layout;
  20306. const struct nk_input *in;
  20307. const struct nk_style *style;
  20308. enum nk_widget_layout_states state;
  20309. struct nk_rect bounds;
  20310. NK_ASSERT(ctx);
  20311. NK_ASSERT(value);
  20312. NK_ASSERT(ctx->current);
  20313. NK_ASSERT(ctx->current->layout);
  20314. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  20315. return 0;
  20316. win = ctx->current;
  20317. layout = win->layout;
  20318. style = &ctx->style;
  20319. state = nk_widget(&bounds, ctx);
  20320. if (!state) return 0;
  20321. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20322. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  20323. str, len, align, value, &img, &style->selectable, in, style->font);
  20324. }
  20325. NK_API int nk_select_text(struct nk_context *ctx, const char *str, int len,
  20326. nk_flags align, int value)
  20327. {nk_selectable_text(ctx, str, len, align, &value);return value;}
  20328. NK_API int nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, int *value)
  20329. {return nk_selectable_text(ctx, str, nk_strlen(str), align, value);}
  20330. NK_API int nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
  20331. const char *str, nk_flags align, int *value)
  20332. {return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);}
  20333. NK_API int nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, int value)
  20334. {nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;}
  20335. NK_API int nk_select_image_label(struct nk_context *ctx, struct nk_image img,
  20336. const char *str, nk_flags align, int value)
  20337. {nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;}
  20338. NK_API int nk_select_image_text(struct nk_context *ctx, struct nk_image img,
  20339. const char *str, int len, nk_flags align, int value)
  20340. {nk_selectable_image_text(ctx, img, str, len, align, &value);return value;}
  20341. /*----------------------------------------------------------------
  20342. *
  20343. * CHECKBOX
  20344. *
  20345. * --------------------------------------------------------------*/
  20346. NK_API int
  20347. nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
  20348. {
  20349. struct nk_window *win;
  20350. struct nk_panel *layout;
  20351. const struct nk_input *in;
  20352. const struct nk_style *style;
  20353. struct nk_rect bounds;
  20354. enum nk_widget_layout_states state;
  20355. NK_ASSERT(ctx);
  20356. NK_ASSERT(ctx->current);
  20357. NK_ASSERT(ctx->current->layout);
  20358. if (!ctx || !ctx->current || !ctx->current->layout)
  20359. return active;
  20360. win = ctx->current;
  20361. style = &ctx->style;
  20362. layout = win->layout;
  20363. state = nk_widget(&bounds, ctx);
  20364. if (!state) return active;
  20365. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20366. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  20367. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
  20368. return active;
  20369. }
  20370. NK_API unsigned int
  20371. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  20372. unsigned int flags, unsigned int value)
  20373. {
  20374. int old_active;
  20375. NK_ASSERT(ctx);
  20376. NK_ASSERT(text);
  20377. if (!ctx || !text) return flags;
  20378. old_active = (int)((flags & value) & value);
  20379. if (nk_check_text(ctx, text, len, old_active))
  20380. flags |= value;
  20381. else flags &= ~value;
  20382. return flags;
  20383. }
  20384. NK_API int
  20385. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
  20386. {
  20387. int old_val;
  20388. NK_ASSERT(ctx);
  20389. NK_ASSERT(text);
  20390. NK_ASSERT(active);
  20391. if (!ctx || !text || !active) return 0;
  20392. old_val = *active;
  20393. *active = nk_check_text(ctx, text, len, *active);
  20394. return old_val != *active;
  20395. }
  20396. NK_API int
  20397. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  20398. unsigned int *flags, unsigned int value)
  20399. {
  20400. int active;
  20401. NK_ASSERT(ctx);
  20402. NK_ASSERT(text);
  20403. NK_ASSERT(flags);
  20404. if (!ctx || !text || !flags) return 0;
  20405. active = (int)((*flags & value) & value);
  20406. if (nk_checkbox_text(ctx, text, len, &active)) {
  20407. if (active) *flags |= value;
  20408. else *flags &= ~value;
  20409. return 1;
  20410. }
  20411. return 0;
  20412. }
  20413. NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
  20414. {return nk_check_text(ctx, label, nk_strlen(label), active);}
  20415. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  20416. unsigned int flags, unsigned int value)
  20417. {return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);}
  20418. NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
  20419. {return nk_checkbox_text(ctx, label, nk_strlen(label), active);}
  20420. NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  20421. unsigned int *flags, unsigned int value)
  20422. {return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);}
  20423. /*----------------------------------------------------------------
  20424. *
  20425. * OPTION
  20426. *
  20427. * --------------------------------------------------------------*/
  20428. NK_API int
  20429. nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
  20430. {
  20431. struct nk_window *win;
  20432. struct nk_panel *layout;
  20433. const struct nk_input *in;
  20434. const struct nk_style *style;
  20435. struct nk_rect bounds;
  20436. enum nk_widget_layout_states state;
  20437. NK_ASSERT(ctx);
  20438. NK_ASSERT(ctx->current);
  20439. NK_ASSERT(ctx->current->layout);
  20440. if (!ctx || !ctx->current || !ctx->current->layout)
  20441. return is_active;
  20442. win = ctx->current;
  20443. style = &ctx->style;
  20444. layout = win->layout;
  20445. state = nk_widget(&bounds, ctx);
  20446. if (!state) return state;
  20447. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20448. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  20449. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
  20450. return is_active;
  20451. }
  20452. NK_API int
  20453. nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
  20454. {
  20455. int old_value;
  20456. NK_ASSERT(ctx);
  20457. NK_ASSERT(text);
  20458. NK_ASSERT(active);
  20459. if (!ctx || !text || !active) return 0;
  20460. old_value = *active;
  20461. *active = nk_option_text(ctx, text, len, old_value);
  20462. return old_value != *active;
  20463. }
  20464. NK_API int
  20465. nk_option_label(struct nk_context *ctx, const char *label, int active)
  20466. {return nk_option_text(ctx, label, nk_strlen(label), active);}
  20467. NK_API int
  20468. nk_radio_label(struct nk_context *ctx, const char *label, int *active)
  20469. {return nk_radio_text(ctx, label, nk_strlen(label), active);}
  20470. /*----------------------------------------------------------------
  20471. *
  20472. * SLIDER
  20473. *
  20474. * --------------------------------------------------------------*/
  20475. NK_API int
  20476. nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  20477. float value_step)
  20478. {
  20479. struct nk_window *win;
  20480. struct nk_panel *layout;
  20481. struct nk_input *in;
  20482. const struct nk_style *style;
  20483. int ret = 0;
  20484. float old_value;
  20485. struct nk_rect bounds;
  20486. enum nk_widget_layout_states state;
  20487. NK_ASSERT(ctx);
  20488. NK_ASSERT(ctx->current);
  20489. NK_ASSERT(ctx->current->layout);
  20490. NK_ASSERT(value);
  20491. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  20492. return ret;
  20493. win = ctx->current;
  20494. style = &ctx->style;
  20495. layout = win->layout;
  20496. state = nk_widget(&bounds, ctx);
  20497. if (!state) return ret;
  20498. in = (/*state == NK_WIDGET_ROM || */ layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20499. old_value = *value;
  20500. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  20501. old_value, max_value, value_step, &style->slider, in, style->font);
  20502. return (old_value > *value || old_value < *value);
  20503. }
  20504. NK_API float
  20505. nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
  20506. {
  20507. nk_slider_float(ctx, min, &val, max, step); return val;
  20508. }
  20509. NK_API int
  20510. nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
  20511. {
  20512. float value = (float)val;
  20513. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  20514. return (int)value;
  20515. }
  20516. NK_API int
  20517. nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
  20518. {
  20519. int ret;
  20520. float value = (float)*val;
  20521. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  20522. *val = (int)value;
  20523. return ret;
  20524. }
  20525. /*----------------------------------------------------------------
  20526. *
  20527. * PROGRESSBAR
  20528. *
  20529. * --------------------------------------------------------------*/
  20530. NK_API int
  20531. nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, int is_modifyable)
  20532. {
  20533. struct nk_window *win;
  20534. struct nk_panel *layout;
  20535. const struct nk_style *style;
  20536. struct nk_input *in;
  20537. struct nk_rect bounds;
  20538. enum nk_widget_layout_states state;
  20539. nk_size old_value;
  20540. NK_ASSERT(ctx);
  20541. NK_ASSERT(cur);
  20542. NK_ASSERT(ctx->current);
  20543. NK_ASSERT(ctx->current->layout);
  20544. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  20545. return 0;
  20546. win = ctx->current;
  20547. style = &ctx->style;
  20548. layout = win->layout;
  20549. state = nk_widget(&bounds, ctx);
  20550. if (!state) return 0;
  20551. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20552. old_value = *cur;
  20553. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  20554. *cur, max, is_modifyable, &style->progress, in);
  20555. return (*cur != old_value);
  20556. }
  20557. NK_API nk_size nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, int modifyable)
  20558. {nk_progress(ctx, &cur, max, modifyable);return cur;}
  20559. /*----------------------------------------------------------------
  20560. *
  20561. * EDIT
  20562. *
  20563. * --------------------------------------------------------------*/
  20564. NK_API void
  20565. nk_edit_focus(struct nk_context *ctx, nk_flags flags)
  20566. {
  20567. nk_hash hash;
  20568. struct nk_window *win;
  20569. NK_ASSERT(ctx);
  20570. NK_ASSERT(ctx->current);
  20571. if (!ctx || !ctx->current) return;
  20572. win = ctx->current;
  20573. hash = win->edit.seq;
  20574. win->edit.active = nk_true;
  20575. win->edit.name = hash;
  20576. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  20577. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  20578. }
  20579. NK_API void
  20580. nk_edit_unfocus(struct nk_context *ctx)
  20581. {
  20582. struct nk_window *win;
  20583. NK_ASSERT(ctx);
  20584. NK_ASSERT(ctx->current);
  20585. if (!ctx || !ctx->current) return;
  20586. win = ctx->current;
  20587. win->edit.active = nk_false;
  20588. win->edit.name = 0;
  20589. }
  20590. NK_API nk_flags
  20591. nk_edit_string(struct nk_context *ctx, nk_flags flags,
  20592. char *memory, int *len, int max, nk_plugin_filter filter)
  20593. {
  20594. nk_hash hash;
  20595. nk_flags state;
  20596. struct nk_text_edit *edit;
  20597. struct nk_window *win;
  20598. NK_ASSERT(ctx);
  20599. NK_ASSERT(memory);
  20600. NK_ASSERT(len);
  20601. if (!ctx || !memory || !len)
  20602. return 0;
  20603. filter = (!filter) ? nk_filter_default: filter;
  20604. win = ctx->current;
  20605. hash = win->edit.seq;
  20606. edit = &ctx->text_edit;
  20607. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
  20608. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
  20609. if (win->edit.active && hash == win->edit.name) {
  20610. if (flags & NK_EDIT_NO_CURSOR)
  20611. edit->cursor = nk_utf_len(memory, *len);
  20612. else edit->cursor = win->edit.cursor;
  20613. if (!(flags & NK_EDIT_SELECTABLE)) {
  20614. edit->select_start = win->edit.cursor;
  20615. edit->select_end = win->edit.cursor;
  20616. } else {
  20617. edit->select_start = win->edit.sel_start;
  20618. edit->select_end = win->edit.sel_end;
  20619. }
  20620. edit->mode = win->edit.mode;
  20621. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  20622. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  20623. edit->active = nk_true;
  20624. } else edit->active = nk_false;
  20625. max = NK_MAX(1, max);
  20626. *len = NK_MIN(*len, max-1);
  20627. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  20628. edit->string.buffer.allocated = (nk_size)*len;
  20629. edit->string.len = nk_utf_len(memory, *len);
  20630. state = nk_edit_buffer(ctx, flags, edit, filter);
  20631. *len = (int)edit->string.buffer.allocated;
  20632. if (edit->active) {
  20633. win->edit.cursor = edit->cursor;
  20634. win->edit.sel_start = edit->select_start;
  20635. win->edit.sel_end = edit->select_end;
  20636. win->edit.mode = edit->mode;
  20637. win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
  20638. win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
  20639. } return state;
  20640. }
  20641. NK_API nk_flags
  20642. nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
  20643. struct nk_text_edit *edit, nk_plugin_filter filter)
  20644. {
  20645. struct nk_window *win;
  20646. struct nk_style *style;
  20647. struct nk_input *in;
  20648. enum nk_widget_layout_states state;
  20649. struct nk_rect bounds;
  20650. nk_flags ret_flags = 0;
  20651. unsigned char prev_state;
  20652. nk_hash hash;
  20653. /* make sure correct values */
  20654. NK_ASSERT(ctx);
  20655. NK_ASSERT(edit);
  20656. NK_ASSERT(ctx->current);
  20657. NK_ASSERT(ctx->current->layout);
  20658. if (!ctx || !ctx->current || !ctx->current->layout)
  20659. return 0;
  20660. win = ctx->current;
  20661. style = &ctx->style;
  20662. state = nk_widget(&bounds, ctx);
  20663. if (!state) return state;
  20664. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20665. /* check if edit is currently hot item */
  20666. hash = win->edit.seq++;
  20667. if (win->edit.active && hash == win->edit.name) {
  20668. if (flags & NK_EDIT_NO_CURSOR)
  20669. edit->cursor = edit->string.len;
  20670. if (!(flags & NK_EDIT_SELECTABLE)) {
  20671. edit->select_start = edit->cursor;
  20672. edit->select_end = edit->cursor;
  20673. }
  20674. if (flags & NK_EDIT_CLIPBOARD)
  20675. edit->clip = ctx->clip;
  20676. edit->active = (unsigned char)win->edit.active;
  20677. } else edit->active = nk_false;
  20678. edit->mode = win->edit.mode;
  20679. filter = (!filter) ? nk_filter_default: filter;
  20680. prev_state = (unsigned char)edit->active;
  20681. in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
  20682. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  20683. filter, edit, &style->edit, in, style->font);
  20684. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  20685. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  20686. if (edit->active && prev_state != edit->active) {
  20687. /* current edit is now hot */
  20688. win->edit.active = nk_true;
  20689. win->edit.name = hash;
  20690. } else if (prev_state && !edit->active) {
  20691. /* current edit is now cold */
  20692. win->edit.active = nk_false;
  20693. } return ret_flags;
  20694. }
  20695. NK_API nk_flags
  20696. nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
  20697. char *buffer, int max, nk_plugin_filter filter)
  20698. {
  20699. nk_flags result;
  20700. int len = nk_strlen(buffer);
  20701. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  20702. buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
  20703. return result;
  20704. }
  20705. /*----------------------------------------------------------------
  20706. *
  20707. * PROPERTY
  20708. *
  20709. * --------------------------------------------------------------*/
  20710. NK_INTERN struct nk_property_variant
  20711. nk_property_variant_int(int value, int min_value, int max_value, int step)
  20712. {
  20713. struct nk_property_variant result;
  20714. result.kind = NK_PROPERTY_INT;
  20715. result.value.i = value;
  20716. result.min_value.i = min_value;
  20717. result.max_value.i = max_value;
  20718. result.step.i = step;
  20719. return result;
  20720. }
  20721. NK_INTERN struct nk_property_variant
  20722. nk_property_variant_float(float value, float min_value, float max_value, float step)
  20723. {
  20724. struct nk_property_variant result;
  20725. result.kind = NK_PROPERTY_FLOAT;
  20726. result.value.f = value;
  20727. result.min_value.f = min_value;
  20728. result.max_value.f = max_value;
  20729. result.step.f = step;
  20730. return result;
  20731. }
  20732. NK_INTERN struct nk_property_variant
  20733. nk_property_variant_double(double value, double min_value, double max_value,
  20734. double step)
  20735. {
  20736. struct nk_property_variant result;
  20737. result.kind = NK_PROPERTY_DOUBLE;
  20738. result.value.d = value;
  20739. result.min_value.d = min_value;
  20740. result.max_value.d = max_value;
  20741. result.step.d = step;
  20742. return result;
  20743. }
  20744. NK_INTERN void
  20745. nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
  20746. float inc_per_pixel, const enum nk_property_filter filter)
  20747. {
  20748. struct nk_window *win;
  20749. struct nk_panel *layout;
  20750. struct nk_input *in;
  20751. const struct nk_style *style;
  20752. struct nk_rect bounds;
  20753. enum nk_widget_layout_states s;
  20754. int *state = 0;
  20755. nk_hash hash = 0;
  20756. char *buffer = 0;
  20757. int *len = 0;
  20758. int *cursor = 0;
  20759. int *select_begin = 0;
  20760. int *select_end = 0;
  20761. int old_state;
  20762. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  20763. int dummy_state = NK_PROPERTY_DEFAULT;
  20764. int dummy_length = 0;
  20765. int dummy_cursor = 0;
  20766. int dummy_select_begin = 0;
  20767. int dummy_select_end = 0;
  20768. NK_ASSERT(ctx);
  20769. NK_ASSERT(ctx->current);
  20770. NK_ASSERT(ctx->current->layout);
  20771. if (!ctx || !ctx->current || !ctx->current->layout)
  20772. return;
  20773. win = ctx->current;
  20774. layout = win->layout;
  20775. style = &ctx->style;
  20776. s = nk_widget(&bounds, ctx);
  20777. if (!s) return;
  20778. /* calculate hash from name */
  20779. if (name[0] == '#') {
  20780. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  20781. name++; /* special number hash */
  20782. } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  20783. /* check if property is currently hot item */
  20784. if (win->property.active && hash == win->property.name) {
  20785. buffer = win->property.buffer;
  20786. len = &win->property.length;
  20787. cursor = &win->property.cursor;
  20788. state = &win->property.state;
  20789. select_begin = &win->property.select_start;
  20790. select_end = &win->property.select_end;
  20791. } else {
  20792. buffer = dummy_buffer;
  20793. len = &dummy_length;
  20794. cursor = &dummy_cursor;
  20795. state = &dummy_state;
  20796. select_begin = &dummy_select_begin;
  20797. select_end = &dummy_select_end;
  20798. }
  20799. /* execute property widget */
  20800. old_state = *state;
  20801. ctx->text_edit.clip = ctx->clip;
  20802. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  20803. layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20804. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  20805. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  20806. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  20807. ctx->button_behavior);
  20808. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  20809. /* current property is now hot */
  20810. win->property.active = 1;
  20811. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  20812. win->property.length = *len;
  20813. win->property.cursor = *cursor;
  20814. win->property.state = *state;
  20815. win->property.name = hash;
  20816. win->property.select_start = *select_begin;
  20817. win->property.select_end = *select_end;
  20818. if (*state == NK_PROPERTY_DRAG) {
  20819. ctx->input.mouse.grab = nk_true;
  20820. ctx->input.mouse.grabbed = nk_true;
  20821. }
  20822. }
  20823. /* check if previously active property is now inactive */
  20824. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  20825. if (old_state == NK_PROPERTY_DRAG) {
  20826. ctx->input.mouse.grab = nk_false;
  20827. ctx->input.mouse.grabbed = nk_false;
  20828. ctx->input.mouse.ungrab = nk_true;
  20829. }
  20830. win->property.select_start = 0;
  20831. win->property.select_end = 0;
  20832. win->property.active = 0;
  20833. }
  20834. }
  20835. NK_API void
  20836. nk_property_int(struct nk_context *ctx, const char *name,
  20837. int min, int *val, int max, int step, float inc_per_pixel)
  20838. {
  20839. struct nk_property_variant variant;
  20840. NK_ASSERT(ctx);
  20841. NK_ASSERT(name);
  20842. NK_ASSERT(val);
  20843. if (!ctx || !ctx->current || !name || !val) return;
  20844. variant = nk_property_variant_int(*val, min, max, step);
  20845. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  20846. *val = variant.value.i;
  20847. }
  20848. NK_API void
  20849. nk_property_float(struct nk_context *ctx, const char *name,
  20850. float min, float *val, float max, float step, float inc_per_pixel)
  20851. {
  20852. struct nk_property_variant variant;
  20853. NK_ASSERT(ctx);
  20854. NK_ASSERT(name);
  20855. NK_ASSERT(val);
  20856. if (!ctx || !ctx->current || !name || !val) return;
  20857. variant = nk_property_variant_float(*val, min, max, step);
  20858. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  20859. *val = variant.value.f;
  20860. }
  20861. NK_API void
  20862. nk_property_double(struct nk_context *ctx, const char *name,
  20863. double min, double *val, double max, double step, float inc_per_pixel)
  20864. {
  20865. struct nk_property_variant variant;
  20866. NK_ASSERT(ctx);
  20867. NK_ASSERT(name);
  20868. NK_ASSERT(val);
  20869. if (!ctx || !ctx->current || !name || !val) return;
  20870. variant = nk_property_variant_double(*val, min, max, step);
  20871. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  20872. *val = variant.value.d;
  20873. }
  20874. NK_API int
  20875. nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
  20876. int max, int step, float inc_per_pixel)
  20877. {
  20878. struct nk_property_variant variant;
  20879. NK_ASSERT(ctx);
  20880. NK_ASSERT(name);
  20881. if (!ctx || !ctx->current || !name) return val;
  20882. variant = nk_property_variant_int(val, min, max, step);
  20883. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  20884. val = variant.value.i;
  20885. return val;
  20886. }
  20887. NK_API float
  20888. nk_propertyf(struct nk_context *ctx, const char *name, float min,
  20889. float val, float max, float step, float inc_per_pixel)
  20890. {
  20891. struct nk_property_variant variant;
  20892. NK_ASSERT(ctx);
  20893. NK_ASSERT(name);
  20894. if (!ctx || !ctx->current || !name) return val;
  20895. variant = nk_property_variant_float(val, min, max, step);
  20896. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  20897. val = variant.value.f;
  20898. return val;
  20899. }
  20900. NK_API double
  20901. nk_propertyd(struct nk_context *ctx, const char *name, double min,
  20902. double val, double max, double step, float inc_per_pixel)
  20903. {
  20904. struct nk_property_variant variant;
  20905. NK_ASSERT(ctx);
  20906. NK_ASSERT(name);
  20907. if (!ctx || !ctx->current || !name) return val;
  20908. variant = nk_property_variant_double(val, min, max, step);
  20909. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  20910. val = variant.value.d;
  20911. return val;
  20912. }
  20913. /*----------------------------------------------------------------
  20914. *
  20915. * COLOR PICKER
  20916. *
  20917. * --------------------------------------------------------------*/
  20918. NK_API int
  20919. nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
  20920. enum nk_color_format fmt)
  20921. {
  20922. struct nk_window *win;
  20923. struct nk_panel *layout;
  20924. const struct nk_style *config;
  20925. const struct nk_input *in;
  20926. enum nk_widget_layout_states state;
  20927. struct nk_rect bounds;
  20928. NK_ASSERT(ctx);
  20929. NK_ASSERT(color);
  20930. NK_ASSERT(ctx->current);
  20931. NK_ASSERT(ctx->current->layout);
  20932. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  20933. return 0;
  20934. win = ctx->current;
  20935. config = &ctx->style;
  20936. layout = win->layout;
  20937. state = nk_widget(&bounds, ctx);
  20938. if (!state) return 0;
  20939. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20940. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  20941. nk_vec2(0,0), in, config->font);
  20942. }
  20943. NK_API struct nk_colorf
  20944. nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
  20945. enum nk_color_format fmt)
  20946. {
  20947. nk_color_pick(ctx, &color, fmt);
  20948. return color;
  20949. }
  20950. /* -------------------------------------------------------------
  20951. *
  20952. * CHART
  20953. *
  20954. * --------------------------------------------------------------*/
  20955. NK_API int
  20956. nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
  20957. struct nk_color color, struct nk_color highlight,
  20958. int count, float min_value, float max_value)
  20959. {
  20960. struct nk_window *win;
  20961. struct nk_chart *chart;
  20962. const struct nk_style *config;
  20963. const struct nk_style_chart *style;
  20964. const struct nk_style_item *background;
  20965. struct nk_rect bounds = {0, 0, 0, 0};
  20966. NK_ASSERT(ctx);
  20967. NK_ASSERT(ctx->current);
  20968. NK_ASSERT(ctx->current->layout);
  20969. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  20970. if (!nk_widget(&bounds, ctx)) {
  20971. chart = &ctx->current->layout->chart;
  20972. nk_zero(chart, sizeof(*chart));
  20973. return 0;
  20974. }
  20975. win = ctx->current;
  20976. config = &ctx->style;
  20977. chart = &win->layout->chart;
  20978. style = &config->chart;
  20979. /* setup basic generic chart */
  20980. nk_zero(chart, sizeof(*chart));
  20981. chart->x = bounds.x + style->padding.x;
  20982. chart->y = bounds.y + style->padding.y;
  20983. chart->w = bounds.w - 2 * style->padding.x;
  20984. chart->h = bounds.h - 2 * style->padding.y;
  20985. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  20986. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  20987. /* add first slot into chart */
  20988. {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  20989. slot->type = type;
  20990. slot->count = count;
  20991. slot->color = color;
  20992. slot->highlight = highlight;
  20993. slot->min = NK_MIN(min_value, max_value);
  20994. slot->max = NK_MAX(min_value, max_value);
  20995. slot->range = slot->max - slot->min;}
  20996. /* draw chart background */
  20997. background = &style->background;
  20998. if (background->type == NK_STYLE_ITEM_IMAGE) {
  20999. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
  21000. } else {
  21001. nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
  21002. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  21003. style->rounding, style->background.data.color);
  21004. }
  21005. return 1;
  21006. }
  21007. NK_API int
  21008. nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
  21009. int count, float min_value, float max_value)
  21010. {return nk_chart_begin_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);}
  21011. NK_API void
  21012. nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
  21013. struct nk_color color, struct nk_color highlight,
  21014. int count, float min_value, float max_value)
  21015. {
  21016. NK_ASSERT(ctx);
  21017. NK_ASSERT(ctx->current);
  21018. NK_ASSERT(ctx->current->layout);
  21019. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  21020. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21021. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  21022. /* add another slot into the graph */
  21023. {struct nk_chart *chart = &ctx->current->layout->chart;
  21024. struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  21025. slot->type = type;
  21026. slot->count = count;
  21027. slot->color = color;
  21028. slot->highlight = highlight;
  21029. slot->min = NK_MIN(min_value, max_value);
  21030. slot->max = NK_MAX(min_value, max_value);
  21031. slot->range = slot->max - slot->min;}
  21032. }
  21033. NK_API void
  21034. nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
  21035. int count, float min_value, float max_value)
  21036. {nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color, ctx->style.chart.selected_color, count, min_value, max_value);}
  21037. NK_INTERN nk_flags
  21038. nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
  21039. struct nk_chart *g, float value, int slot)
  21040. {
  21041. struct nk_panel *layout = win->layout;
  21042. const struct nk_input *i = &ctx->input;
  21043. struct nk_command_buffer *out = &win->buffer;
  21044. nk_flags ret = 0;
  21045. struct nk_vec2 cur;
  21046. struct nk_rect bounds;
  21047. struct nk_color color;
  21048. float step;
  21049. float range;
  21050. float ratio;
  21051. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  21052. step = g->w / (float)g->slots[slot].count;
  21053. range = g->slots[slot].max - g->slots[slot].min;
  21054. ratio = (value - g->slots[slot].min) / range;
  21055. if (g->slots[slot].index == 0) {
  21056. /* first data point does not have a connection */
  21057. g->slots[slot].last.x = g->x;
  21058. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  21059. bounds.x = g->slots[slot].last.x - 2;
  21060. bounds.y = g->slots[slot].last.y - 2;
  21061. bounds.w = bounds.h = 4;
  21062. color = g->slots[slot].color;
  21063. if (!(layout->flags & NK_WINDOW_ROM) &&
  21064. NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
  21065. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  21066. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  21067. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  21068. color = g->slots[slot].highlight;
  21069. }
  21070. nk_fill_rect(out, bounds, 0, color);
  21071. g->slots[slot].index += 1;
  21072. return ret;
  21073. }
  21074. /* draw a line between the last data point and the new one */
  21075. color = g->slots[slot].color;
  21076. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  21077. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  21078. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  21079. bounds.x = cur.x - 3;
  21080. bounds.y = cur.y - 3;
  21081. bounds.w = bounds.h = 6;
  21082. /* user selection of current data point */
  21083. if (!(layout->flags & NK_WINDOW_ROM)) {
  21084. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  21085. ret = NK_CHART_HOVERING;
  21086. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  21087. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  21088. color = g->slots[slot].highlight;
  21089. }
  21090. }
  21091. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  21092. /* save current data point position */
  21093. g->slots[slot].last.x = cur.x;
  21094. g->slots[slot].last.y = cur.y;
  21095. g->slots[slot].index += 1;
  21096. return ret;
  21097. }
  21098. NK_INTERN nk_flags
  21099. nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
  21100. struct nk_chart *chart, float value, int slot)
  21101. {
  21102. struct nk_command_buffer *out = &win->buffer;
  21103. const struct nk_input *in = &ctx->input;
  21104. struct nk_panel *layout = win->layout;
  21105. float ratio;
  21106. nk_flags ret = 0;
  21107. struct nk_color color;
  21108. struct nk_rect item = {0,0,0,0};
  21109. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  21110. if (chart->slots[slot].index >= chart->slots[slot].count)
  21111. return nk_false;
  21112. if (chart->slots[slot].count) {
  21113. float padding = (float)(chart->slots[slot].count-1);
  21114. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  21115. }
  21116. /* calculate bounds of current bar chart entry */
  21117. color = chart->slots[slot].color;;
  21118. item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
  21119. if (value >= 0) {
  21120. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  21121. item.y = (chart->y + chart->h) - chart->h * ratio;
  21122. } else {
  21123. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  21124. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  21125. }
  21126. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  21127. item.x = item.x + ((float)chart->slots[slot].index);
  21128. /* user chart bar selection */
  21129. if (!(layout->flags & NK_WINDOW_ROM) &&
  21130. NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
  21131. ret = NK_CHART_HOVERING;
  21132. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  21133. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  21134. color = chart->slots[slot].highlight;
  21135. }
  21136. nk_fill_rect(out, item, 0, color);
  21137. chart->slots[slot].index += 1;
  21138. return ret;
  21139. }
  21140. NK_API nk_flags
  21141. nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
  21142. {
  21143. nk_flags flags;
  21144. struct nk_window *win;
  21145. NK_ASSERT(ctx);
  21146. NK_ASSERT(ctx->current);
  21147. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  21148. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  21149. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  21150. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  21151. win = ctx->current;
  21152. if (win->layout->chart.slot < slot) return nk_false;
  21153. switch (win->layout->chart.slots[slot].type) {
  21154. case NK_CHART_LINES:
  21155. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  21156. case NK_CHART_COLUMN:
  21157. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  21158. default:
  21159. case NK_CHART_MAX:
  21160. flags = 0;
  21161. }
  21162. return flags;
  21163. }
  21164. NK_API nk_flags
  21165. nk_chart_push(struct nk_context *ctx, float value)
  21166. {return nk_chart_push_slot(ctx, value, 0);}
  21167. NK_API void
  21168. nk_chart_end(struct nk_context *ctx)
  21169. {
  21170. struct nk_window *win;
  21171. struct nk_chart *chart;
  21172. NK_ASSERT(ctx);
  21173. NK_ASSERT(ctx->current);
  21174. if (!ctx || !ctx->current)
  21175. return;
  21176. win = ctx->current;
  21177. chart = &win->layout->chart;
  21178. NK_MEMSET(chart, 0, sizeof(*chart));
  21179. return;
  21180. }
  21181. NK_API void
  21182. nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
  21183. int count, int offset)
  21184. {
  21185. int i = 0;
  21186. float min_value;
  21187. float max_value;
  21188. NK_ASSERT(ctx);
  21189. NK_ASSERT(values);
  21190. if (!ctx || !values || !count) return;
  21191. min_value = values[offset];
  21192. max_value = values[offset];
  21193. for (i = 0; i < count; ++i) {
  21194. min_value = NK_MIN(values[i + offset], min_value);
  21195. max_value = NK_MAX(values[i + offset], max_value);
  21196. }
  21197. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  21198. for (i = 0; i < count; ++i)
  21199. nk_chart_push(ctx, values[i + offset]);
  21200. nk_chart_end(ctx);
  21201. }
  21202. }
  21203. NK_API void
  21204. nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
  21205. float(*value_getter)(void* user, int index), int count, int offset)
  21206. {
  21207. int i = 0;
  21208. float min_value;
  21209. float max_value;
  21210. NK_ASSERT(ctx);
  21211. NK_ASSERT(value_getter);
  21212. if (!ctx || !value_getter || !count) return;
  21213. max_value = min_value = value_getter(userdata, offset);
  21214. for (i = 0; i < count; ++i) {
  21215. float value = value_getter(userdata, i + offset);
  21216. min_value = NK_MIN(value, min_value);
  21217. max_value = NK_MAX(value, max_value);
  21218. }
  21219. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  21220. for (i = 0; i < count; ++i)
  21221. nk_chart_push(ctx, value_getter(userdata, i + offset));
  21222. nk_chart_end(ctx);
  21223. }
  21224. }
  21225. /* -------------------------------------------------------------
  21226. *
  21227. * GROUP
  21228. *
  21229. * --------------------------------------------------------------*/
  21230. NK_API int
  21231. nk_group_scrolled_offset_begin(struct nk_context *ctx,
  21232. nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
  21233. {
  21234. struct nk_rect bounds;
  21235. struct nk_window panel;
  21236. struct nk_window *win;
  21237. win = ctx->current;
  21238. nk_panel_alloc_space(&bounds, ctx);
  21239. {const struct nk_rect *c = &win->layout->clip;
  21240. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  21241. !(flags & NK_WINDOW_MOVABLE)) {
  21242. return 0;
  21243. }}
  21244. if (win->flags & NK_WINDOW_ROM)
  21245. flags |= NK_WINDOW_ROM;
  21246. /* initialize a fake window to create the panel from */
  21247. nk_zero(&panel, sizeof(panel));
  21248. panel.bounds = bounds;
  21249. panel.flags = flags;
  21250. panel.scrollbar.x = *x_offset;
  21251. panel.scrollbar.y = *y_offset;
  21252. panel.buffer = win->buffer;
  21253. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  21254. ctx->current = &panel;
  21255. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
  21256. win->buffer = panel.buffer;
  21257. win->buffer.clip = panel.layout->clip;
  21258. panel.layout->offset_x = x_offset;
  21259. panel.layout->offset_y = y_offset;
  21260. panel.layout->parent = win->layout;
  21261. win->layout = panel.layout;
  21262. ctx->current = win;
  21263. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  21264. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  21265. {
  21266. nk_flags f = panel.layout->flags;
  21267. nk_group_scrolled_end(ctx);
  21268. if (f & NK_WINDOW_CLOSED)
  21269. return NK_WINDOW_CLOSED;
  21270. if (f & NK_WINDOW_MINIMIZED)
  21271. return NK_WINDOW_MINIMIZED;
  21272. }
  21273. return 1;
  21274. }
  21275. NK_API void
  21276. nk_group_scrolled_end(struct nk_context *ctx)
  21277. {
  21278. struct nk_window *win;
  21279. struct nk_panel *parent;
  21280. struct nk_panel *g;
  21281. struct nk_rect clip;
  21282. struct nk_window pan;
  21283. struct nk_vec2 panel_padding;
  21284. NK_ASSERT(ctx);
  21285. NK_ASSERT(ctx->current);
  21286. if (!ctx || !ctx->current)
  21287. return;
  21288. /* make sure nk_group_begin was called correctly */
  21289. NK_ASSERT(ctx->current);
  21290. win = ctx->current;
  21291. NK_ASSERT(win->layout);
  21292. g = win->layout;
  21293. NK_ASSERT(g->parent);
  21294. parent = g->parent;
  21295. /* dummy window */
  21296. nk_zero_struct(pan);
  21297. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  21298. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  21299. pan.bounds.x = g->bounds.x - panel_padding.x;
  21300. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  21301. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  21302. if (g->flags & NK_WINDOW_BORDER) {
  21303. pan.bounds.x -= g->border;
  21304. pan.bounds.y -= g->border;
  21305. pan.bounds.w += 2*g->border;
  21306. pan.bounds.h += 2*g->border;
  21307. }
  21308. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  21309. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  21310. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  21311. }
  21312. pan.scrollbar.x = *g->offset_x;
  21313. pan.scrollbar.y = *g->offset_y;
  21314. pan.flags = g->flags;
  21315. pan.buffer = win->buffer;
  21316. pan.layout = g;
  21317. pan.parent = win;
  21318. ctx->current = &pan;
  21319. /* make sure group has correct clipping rectangle */
  21320. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  21321. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  21322. nk_push_scissor(&pan.buffer, clip);
  21323. nk_end(ctx);
  21324. win->buffer = pan.buffer;
  21325. nk_push_scissor(&win->buffer, parent->clip);
  21326. ctx->current = win;
  21327. win->layout = parent;
  21328. g->bounds = pan.bounds;
  21329. return;
  21330. }
  21331. NK_API int
  21332. nk_group_scrolled_begin(struct nk_context *ctx,
  21333. struct nk_scroll *scroll, const char *title, nk_flags flags)
  21334. {return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);}
  21335. NK_API int
  21336. nk_group_begin_titled(struct nk_context *ctx, const char *id,
  21337. const char *title, nk_flags flags)
  21338. {
  21339. int id_len;
  21340. nk_hash id_hash;
  21341. struct nk_window *win;
  21342. nk_uint *x_offset;
  21343. nk_uint *y_offset;
  21344. NK_ASSERT(ctx);
  21345. NK_ASSERT(id);
  21346. NK_ASSERT(ctx->current);
  21347. NK_ASSERT(ctx->current->layout);
  21348. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21349. return 0;
  21350. /* find persistent group scrollbar value */
  21351. win = ctx->current;
  21352. id_len = (int)nk_strlen(id);
  21353. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21354. x_offset = nk_find_value(win, id_hash);
  21355. if (!x_offset) {
  21356. x_offset = nk_add_value(ctx, win, id_hash, 0);
  21357. y_offset = nk_add_value(ctx, win, id_hash+1, 0);
  21358. NK_ASSERT(x_offset);
  21359. NK_ASSERT(y_offset);
  21360. if (!x_offset || !y_offset) return 0;
  21361. *x_offset = *y_offset = 0;
  21362. } else y_offset = nk_find_value(win, id_hash+1);
  21363. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21364. }
  21365. NK_API int
  21366. nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
  21367. {
  21368. return nk_group_begin_titled(ctx, title, title, flags);
  21369. }
  21370. NK_API void
  21371. nk_group_end(struct nk_context *ctx)
  21372. {nk_group_scrolled_end(ctx);}
  21373. NK_API int
  21374. nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
  21375. const char *title, nk_flags flags, int row_height, int row_count)
  21376. {
  21377. int title_len;
  21378. nk_hash title_hash;
  21379. nk_uint *x_offset;
  21380. nk_uint *y_offset;
  21381. int result;
  21382. struct nk_window *win;
  21383. struct nk_panel *layout;
  21384. const struct nk_style *style;
  21385. struct nk_vec2 item_spacing;
  21386. NK_ASSERT(ctx);
  21387. NK_ASSERT(view);
  21388. NK_ASSERT(title);
  21389. if (!ctx || !view || !title) return 0;
  21390. win = ctx->current;
  21391. style = &ctx->style;
  21392. item_spacing = style->window.spacing;
  21393. row_height += NK_MAX(0, (int)item_spacing.y);
  21394. /* find persistent list view scrollbar offset */
  21395. title_len = (int)nk_strlen(title);
  21396. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  21397. x_offset = nk_find_value(win, title_hash);
  21398. if (!x_offset) {
  21399. x_offset = nk_add_value(ctx, win, title_hash, 0);
  21400. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  21401. NK_ASSERT(x_offset);
  21402. NK_ASSERT(y_offset);
  21403. if (!x_offset || !y_offset) return 0;
  21404. *x_offset = *y_offset = 0;
  21405. } else y_offset = nk_find_value(win, title_hash+1);
  21406. view->scroll_value = *y_offset;
  21407. view->scroll_pointer = y_offset;
  21408. *y_offset = 0;
  21409. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21410. win = ctx->current;
  21411. layout = win->layout;
  21412. view->total_height = row_height * NK_MAX(row_count,1);
  21413. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  21414. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height), 0);
  21415. view->end = view->begin + view->count;
  21416. view->ctx = ctx;
  21417. return result;
  21418. }
  21419. NK_API void
  21420. nk_list_view_end(struct nk_list_view *view)
  21421. {
  21422. struct nk_context *ctx;
  21423. struct nk_window *win;
  21424. struct nk_panel *layout;
  21425. NK_ASSERT(view);
  21426. NK_ASSERT(view->ctx);
  21427. NK_ASSERT(view->scroll_pointer);
  21428. if (!view || !view->ctx) return;
  21429. ctx = view->ctx;
  21430. win = ctx->current;
  21431. layout = win->layout;
  21432. layout->at_y = layout->bounds.y + (float)view->total_height;
  21433. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  21434. nk_group_end(view->ctx);
  21435. }
  21436. /* --------------------------------------------------------------
  21437. *
  21438. * POPUP
  21439. *
  21440. * --------------------------------------------------------------*/
  21441. NK_API int
  21442. nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
  21443. const char *title, nk_flags flags, struct nk_rect rect)
  21444. {
  21445. struct nk_window *popup;
  21446. struct nk_window *win;
  21447. struct nk_panel *panel;
  21448. int title_len;
  21449. nk_hash title_hash;
  21450. nk_size allocated;
  21451. NK_ASSERT(ctx);
  21452. NK_ASSERT(title);
  21453. NK_ASSERT(ctx->current);
  21454. NK_ASSERT(ctx->current->layout);
  21455. if (!ctx || !ctx->current || !ctx->current->layout)
  21456. return 0;
  21457. win = ctx->current;
  21458. panel = win->layout;
  21459. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  21460. (void)panel;
  21461. title_len = (int)nk_strlen(title);
  21462. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  21463. popup = win->popup.win;
  21464. if (!popup) {
  21465. popup = (struct nk_window*)nk_create_window(ctx);
  21466. popup->parent = win;
  21467. win->popup.win = popup;
  21468. win->popup.active = 0;
  21469. win->popup.type = NK_PANEL_POPUP;
  21470. }
  21471. /* make sure we have correct popup */
  21472. if (win->popup.name != title_hash) {
  21473. if (!win->popup.active) {
  21474. nk_zero(popup, sizeof(*popup));
  21475. win->popup.name = title_hash;
  21476. win->popup.active = 1;
  21477. win->popup.type = NK_PANEL_POPUP;
  21478. } else return 0;
  21479. }
  21480. /* popup position is local to window */
  21481. ctx->current = popup;
  21482. rect.x += win->layout->clip.x;
  21483. rect.y += win->layout->clip.y;
  21484. /* setup popup data */
  21485. popup->parent = win;
  21486. popup->bounds = rect;
  21487. popup->seq = ctx->seq;
  21488. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  21489. popup->flags = flags;
  21490. popup->flags |= NK_WINDOW_BORDER;
  21491. if (type == NK_POPUP_DYNAMIC)
  21492. popup->flags |= NK_WINDOW_DYNAMIC;
  21493. popup->buffer = win->buffer;
  21494. nk_start_popup(ctx, win);
  21495. allocated = ctx->memory.allocated;
  21496. nk_push_scissor(&popup->buffer, nk_null_rect);
  21497. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  21498. /* popup is running therefore invalidate parent panels */
  21499. struct nk_panel *root;
  21500. root = win->layout;
  21501. while (root) {
  21502. root->flags |= NK_WINDOW_ROM;
  21503. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  21504. root = root->parent;
  21505. }
  21506. win->popup.active = 1;
  21507. popup->layout->offset_x = &popup->scrollbar.x;
  21508. popup->layout->offset_y = &popup->scrollbar.y;
  21509. popup->layout->parent = win->layout;
  21510. return 1;
  21511. } else {
  21512. /* popup was closed/is invalid so cleanup */
  21513. struct nk_panel *root;
  21514. root = win->layout;
  21515. while (root) {
  21516. root->flags |= NK_WINDOW_REMOVE_ROM;
  21517. root = root->parent;
  21518. }
  21519. win->popup.buf.active = 0;
  21520. win->popup.active = 0;
  21521. ctx->memory.allocated = allocated;
  21522. ctx->current = win;
  21523. nk_free_panel(ctx, popup->layout);
  21524. popup->layout = 0;
  21525. return 0;
  21526. }
  21527. }
  21528. NK_INTERN int
  21529. nk_nonblock_begin(struct nk_context *ctx,
  21530. nk_flags flags, struct nk_rect body, struct nk_rect header,
  21531. enum nk_panel_type panel_type)
  21532. {
  21533. struct nk_window *popup;
  21534. struct nk_window *win;
  21535. struct nk_panel *panel;
  21536. int is_active = nk_true;
  21537. NK_ASSERT(ctx);
  21538. NK_ASSERT(ctx->current);
  21539. NK_ASSERT(ctx->current->layout);
  21540. if (!ctx || !ctx->current || !ctx->current->layout)
  21541. return 0;
  21542. /* popups cannot have popups */
  21543. win = ctx->current;
  21544. panel = win->layout;
  21545. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
  21546. (void)panel;
  21547. popup = win->popup.win;
  21548. if (!popup) {
  21549. /* create window for nonblocking popup */
  21550. popup = (struct nk_window*)nk_create_window(ctx);
  21551. popup->parent = win;
  21552. win->popup.win = popup;
  21553. win->popup.type = panel_type;
  21554. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  21555. } else {
  21556. /* close the popup if user pressed outside or in the header */
  21557. int pressed, in_body, in_header;
  21558. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  21559. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  21560. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  21561. if (pressed && (!in_body || in_header))
  21562. is_active = nk_false;
  21563. }
  21564. win->popup.header = header;
  21565. if (!is_active) {
  21566. /* remove read only mode from all parent panels */
  21567. struct nk_panel *root = win->layout;
  21568. while (root) {
  21569. root->flags |= NK_WINDOW_REMOVE_ROM;
  21570. root = root->parent;
  21571. }
  21572. return is_active;
  21573. }
  21574. popup->bounds = body;
  21575. popup->parent = win;
  21576. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  21577. popup->flags = flags;
  21578. popup->flags |= NK_WINDOW_BORDER;
  21579. popup->flags |= NK_WINDOW_DYNAMIC;
  21580. popup->seq = ctx->seq;
  21581. win->popup.active = 1;
  21582. NK_ASSERT(popup->layout);
  21583. nk_start_popup(ctx, win);
  21584. popup->buffer = win->buffer;
  21585. nk_push_scissor(&popup->buffer, nk_null_rect);
  21586. ctx->current = popup;
  21587. nk_panel_begin(ctx, 0, panel_type);
  21588. win->buffer = popup->buffer;
  21589. popup->layout->parent = win->layout;
  21590. popup->layout->offset_x = &popup->scrollbar.x;
  21591. popup->layout->offset_y = &popup->scrollbar.y;
  21592. /* set read only mode to all parent panels */
  21593. {struct nk_panel *root;
  21594. root = win->layout;
  21595. while (root) {
  21596. root->flags |= NK_WINDOW_ROM;
  21597. root = root->parent;
  21598. }}
  21599. return is_active;
  21600. }
  21601. NK_API void
  21602. nk_popup_close(struct nk_context *ctx)
  21603. {
  21604. struct nk_window *popup;
  21605. NK_ASSERT(ctx);
  21606. if (!ctx || !ctx->current) return;
  21607. popup = ctx->current;
  21608. NK_ASSERT(popup->parent);
  21609. NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
  21610. popup->flags |= NK_WINDOW_HIDDEN;
  21611. }
  21612. NK_API void
  21613. nk_popup_end(struct nk_context *ctx)
  21614. {
  21615. struct nk_window *win;
  21616. struct nk_window *popup;
  21617. NK_ASSERT(ctx);
  21618. NK_ASSERT(ctx->current);
  21619. NK_ASSERT(ctx->current->layout);
  21620. if (!ctx || !ctx->current || !ctx->current->layout)
  21621. return;
  21622. popup = ctx->current;
  21623. if (!popup->parent) return;
  21624. win = popup->parent;
  21625. if (popup->flags & NK_WINDOW_HIDDEN) {
  21626. struct nk_panel *root;
  21627. root = win->layout;
  21628. while (root) {
  21629. root->flags |= NK_WINDOW_REMOVE_ROM;
  21630. root = root->parent;
  21631. }
  21632. win->popup.active = 0;
  21633. }
  21634. nk_push_scissor(&popup->buffer, nk_null_rect);
  21635. nk_end(ctx);
  21636. win->buffer = popup->buffer;
  21637. nk_finish_popup(ctx, win);
  21638. ctx->current = win;
  21639. nk_push_scissor(&win->buffer, win->layout->clip);
  21640. }
  21641. /* -------------------------------------------------------------
  21642. *
  21643. * TOOLTIP
  21644. *
  21645. * -------------------------------------------------------------- */
  21646. NK_API int
  21647. nk_tooltip_begin(struct nk_context *ctx, float width)
  21648. {
  21649. int x,y,w,h;
  21650. struct nk_window *win;
  21651. const struct nk_input *in;
  21652. struct nk_rect bounds;
  21653. int ret;
  21654. NK_ASSERT(ctx);
  21655. NK_ASSERT(ctx->current);
  21656. NK_ASSERT(ctx->current->layout);
  21657. if (!ctx || !ctx->current || !ctx->current->layout)
  21658. return 0;
  21659. /* make sure that no nonblocking popup is currently active */
  21660. win = ctx->current;
  21661. in = &ctx->input;
  21662. if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
  21663. return 0;
  21664. w = nk_iceilf(width);
  21665. h = nk_iceilf(nk_null_rect.h);
  21666. x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
  21667. y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
  21668. bounds.x = (float)x;
  21669. bounds.y = (float)y;
  21670. bounds.w = (float)w;
  21671. bounds.h = (float)h;
  21672. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  21673. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
  21674. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  21675. win->popup.type = NK_PANEL_TOOLTIP;
  21676. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  21677. return ret;
  21678. }
  21679. NK_API void
  21680. nk_tooltip_end(struct nk_context *ctx)
  21681. {
  21682. NK_ASSERT(ctx);
  21683. NK_ASSERT(ctx->current);
  21684. if (!ctx || !ctx->current) return;
  21685. ctx->current->seq--;
  21686. nk_popup_close(ctx);
  21687. nk_popup_end(ctx);
  21688. }
  21689. NK_API void
  21690. nk_tooltip(struct nk_context *ctx, const char *text)
  21691. {
  21692. const struct nk_style *style;
  21693. struct nk_vec2 padding;
  21694. int text_len;
  21695. float text_width;
  21696. float text_height;
  21697. NK_ASSERT(ctx);
  21698. NK_ASSERT(ctx->current);
  21699. NK_ASSERT(ctx->current->layout);
  21700. NK_ASSERT(text);
  21701. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  21702. return;
  21703. /* fetch configuration data */
  21704. style = &ctx->style;
  21705. padding = style->window.padding;
  21706. /* calculate size of the text and tooltip */
  21707. text_len = nk_strlen(text);
  21708. text_width = style->font->width(style->font->userdata,
  21709. style->font->height, text, text_len);
  21710. text_width += (4 * padding.x);
  21711. text_height = (style->font->height + 2 * padding.y);
  21712. /* execute tooltip and fill with text */
  21713. if (nk_tooltip_begin(ctx, (float)text_width)) {
  21714. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  21715. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  21716. nk_tooltip_end(ctx);
  21717. }
  21718. }
  21719. #ifdef NK_INCLUDE_STANDARD_VARARGS
  21720. NK_API void
  21721. nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
  21722. {
  21723. char buf[256];
  21724. va_list args;
  21725. va_start(args, fmt);
  21726. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21727. va_end(args);
  21728. nk_tooltip(ctx, buf);
  21729. }
  21730. #endif
  21731. /* -------------------------------------------------------------
  21732. *
  21733. * CONTEXTUAL
  21734. *
  21735. * -------------------------------------------------------------- */
  21736. NK_API int
  21737. nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
  21738. struct nk_rect trigger_bounds)
  21739. {
  21740. struct nk_window *win;
  21741. struct nk_window *popup;
  21742. struct nk_rect body;
  21743. NK_STORAGE const struct nk_rect null_rect = {0,0,0,0};
  21744. int is_clicked = 0;
  21745. int is_active = 0;
  21746. int is_open = 0;
  21747. int ret = 0;
  21748. NK_ASSERT(ctx);
  21749. NK_ASSERT(ctx->current);
  21750. NK_ASSERT(ctx->current->layout);
  21751. if (!ctx || !ctx->current || !ctx->current->layout)
  21752. return 0;
  21753. win = ctx->current;
  21754. ++win->popup.con_count;
  21755. /* check if currently active contextual is active */
  21756. popup = win->popup.win;
  21757. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  21758. is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
  21759. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  21760. return 0;
  21761. if ((is_clicked && is_open && !is_active) || (!is_open && !is_active && !is_clicked))
  21762. return 0;
  21763. /* calculate contextual position on click */
  21764. win->popup.active_con = win->popup.con_count;
  21765. if (is_clicked) {
  21766. body.x = ctx->input.mouse.pos.x;
  21767. body.y = ctx->input.mouse.pos.y;
  21768. } else {
  21769. body.x = popup->bounds.x;
  21770. body.y = popup->bounds.y;
  21771. }
  21772. body.w = size.x;
  21773. body.h = size.y;
  21774. /* start nonblocking contextual popup */
  21775. ret = nk_nonblock_begin(ctx, flags|NK_WINDOW_NO_SCROLLBAR, body,
  21776. null_rect, NK_PANEL_CONTEXTUAL);
  21777. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  21778. else {
  21779. win->popup.active_con = 0;
  21780. if (win->popup.win)
  21781. win->popup.win->flags = 0;
  21782. }
  21783. return ret;
  21784. }
  21785. NK_API int
  21786. nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
  21787. nk_flags alignment)
  21788. {
  21789. struct nk_window *win;
  21790. const struct nk_input *in;
  21791. const struct nk_style *style;
  21792. struct nk_rect bounds;
  21793. enum nk_widget_layout_states state;
  21794. NK_ASSERT(ctx);
  21795. NK_ASSERT(ctx->current);
  21796. NK_ASSERT(ctx->current->layout);
  21797. if (!ctx || !ctx->current || !ctx->current->layout)
  21798. return 0;
  21799. win = ctx->current;
  21800. style = &ctx->style;
  21801. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  21802. if (!state) return nk_false;
  21803. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21804. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  21805. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  21806. nk_contextual_close(ctx);
  21807. return nk_true;
  21808. }
  21809. return nk_false;
  21810. }
  21811. NK_API int nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  21812. {return nk_contextual_item_text(ctx, label, nk_strlen(label), align);}
  21813. NK_API int
  21814. nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
  21815. const char *text, int len, nk_flags align)
  21816. {
  21817. struct nk_window *win;
  21818. const struct nk_input *in;
  21819. const struct nk_style *style;
  21820. struct nk_rect bounds;
  21821. enum nk_widget_layout_states state;
  21822. NK_ASSERT(ctx);
  21823. NK_ASSERT(ctx->current);
  21824. NK_ASSERT(ctx->current->layout);
  21825. if (!ctx || !ctx->current || !ctx->current->layout)
  21826. return 0;
  21827. win = ctx->current;
  21828. style = &ctx->style;
  21829. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  21830. if (!state) return nk_false;
  21831. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21832. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  21833. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
  21834. nk_contextual_close(ctx);
  21835. return nk_true;
  21836. }
  21837. return nk_false;
  21838. }
  21839. NK_API int nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
  21840. const char *label, nk_flags align)
  21841. {return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);}
  21842. NK_API int
  21843. nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  21844. const char *text, int len, nk_flags align)
  21845. {
  21846. struct nk_window *win;
  21847. const struct nk_input *in;
  21848. const struct nk_style *style;
  21849. struct nk_rect bounds;
  21850. enum nk_widget_layout_states state;
  21851. NK_ASSERT(ctx);
  21852. NK_ASSERT(ctx->current);
  21853. NK_ASSERT(ctx->current->layout);
  21854. if (!ctx || !ctx->current || !ctx->current->layout)
  21855. return 0;
  21856. win = ctx->current;
  21857. style = &ctx->style;
  21858. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  21859. if (!state) return nk_false;
  21860. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  21861. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  21862. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  21863. nk_contextual_close(ctx);
  21864. return nk_true;
  21865. }
  21866. return nk_false;
  21867. }
  21868. NK_API int nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  21869. const char *text, nk_flags align)
  21870. {return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);}
  21871. NK_API void
  21872. nk_contextual_close(struct nk_context *ctx)
  21873. {
  21874. NK_ASSERT(ctx);
  21875. NK_ASSERT(ctx->current);
  21876. NK_ASSERT(ctx->current->layout);
  21877. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21878. nk_popup_close(ctx);
  21879. }
  21880. NK_API void
  21881. nk_contextual_end(struct nk_context *ctx)
  21882. {
  21883. struct nk_window *popup;
  21884. struct nk_panel *panel;
  21885. NK_ASSERT(ctx);
  21886. NK_ASSERT(ctx->current);
  21887. if (!ctx || !ctx->current) return;
  21888. popup = ctx->current;
  21889. panel = popup->layout;
  21890. NK_ASSERT(popup->parent);
  21891. NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
  21892. if (panel->flags & NK_WINDOW_DYNAMIC) {
  21893. /* Close behavior
  21894. This is a bit of a hack solution since we do not know before we end our popup
  21895. how big it will be. We therefore do not directly know when a
  21896. click outside the non-blocking popup must close it at that direct frame.
  21897. Instead it will be closed in the next frame.*/
  21898. struct nk_rect body = {0,0,0,0};
  21899. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  21900. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  21901. body = panel->bounds;
  21902. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  21903. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  21904. }
  21905. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  21906. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  21907. if (pressed && in_body)
  21908. popup->flags |= NK_WINDOW_HIDDEN;
  21909. }
  21910. }
  21911. if (popup->flags & NK_WINDOW_HIDDEN)
  21912. popup->seq = 0;
  21913. nk_popup_end(ctx);
  21914. return;
  21915. }
  21916. /* -------------------------------------------------------------
  21917. *
  21918. * COMBO
  21919. *
  21920. * --------------------------------------------------------------*/
  21921. NK_INTERN int
  21922. nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
  21923. struct nk_vec2 size, int is_clicked, struct nk_rect header)
  21924. {
  21925. struct nk_window *popup;
  21926. int is_open = 0;
  21927. int is_active = 0;
  21928. struct nk_rect body;
  21929. nk_hash hash;
  21930. NK_ASSERT(ctx);
  21931. NK_ASSERT(ctx->current);
  21932. NK_ASSERT(ctx->current->layout);
  21933. if (!ctx || !ctx->current || !ctx->current->layout)
  21934. return 0;
  21935. popup = win->popup.win;
  21936. body.x = header.x;
  21937. body.w = size.x;
  21938. body.y = header.y + header.h-ctx->style.window.combo_border;
  21939. body.h = size.y;
  21940. hash = win->popup.combo_count++;
  21941. is_open = (popup) ? nk_true:nk_false;
  21942. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  21943. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  21944. (!is_open && !is_active && !is_clicked)) return 0;
  21945. if (!nk_nonblock_begin(ctx, 0, body,
  21946. (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
  21947. win->popup.type = NK_PANEL_COMBO;
  21948. win->popup.name = hash;
  21949. return 1;
  21950. }
  21951. NK_API int
  21952. nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
  21953. struct nk_vec2 size)
  21954. {
  21955. const struct nk_input *in;
  21956. struct nk_window *win;
  21957. struct nk_style *style;
  21958. enum nk_widget_layout_states s;
  21959. int is_clicked = nk_false;
  21960. struct nk_rect header;
  21961. const struct nk_style_item *background;
  21962. struct nk_text text;
  21963. NK_ASSERT(ctx);
  21964. NK_ASSERT(selected);
  21965. NK_ASSERT(ctx->current);
  21966. NK_ASSERT(ctx->current->layout);
  21967. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  21968. return 0;
  21969. win = ctx->current;
  21970. style = &ctx->style;
  21971. s = nk_widget(&header, ctx);
  21972. if (s == NK_WIDGET_INVALID)
  21973. return 0;
  21974. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  21975. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  21976. is_clicked = nk_true;
  21977. /* draw combo box header background and border */
  21978. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  21979. background = &style->combo.active;
  21980. text.text = style->combo.label_active;
  21981. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  21982. background = &style->combo.hover;
  21983. text.text = style->combo.label_hover;
  21984. } else {
  21985. background = &style->combo.normal;
  21986. text.text = style->combo.label_normal;
  21987. }
  21988. if (background->type == NK_STYLE_ITEM_IMAGE) {
  21989. text.background = nk_rgba(0,0,0,0);
  21990. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  21991. } else {
  21992. text.background = background->data.color;
  21993. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  21994. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  21995. }
  21996. {
  21997. /* print currently selected text item */
  21998. struct nk_rect label;
  21999. struct nk_rect button;
  22000. struct nk_rect content;
  22001. enum nk_symbol_type sym;
  22002. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22003. sym = style->combo.sym_hover;
  22004. else if (is_clicked)
  22005. sym = style->combo.sym_active;
  22006. else sym = style->combo.sym_normal;
  22007. /* calculate button */
  22008. button.w = header.h - 2 * style->combo.button_padding.y;
  22009. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22010. button.y = header.y + style->combo.button_padding.y;
  22011. button.h = button.w;
  22012. content.x = button.x + style->combo.button.padding.x;
  22013. content.y = button.y + style->combo.button.padding.y;
  22014. content.w = button.w - 2 * style->combo.button.padding.x;
  22015. content.h = button.h - 2 * style->combo.button.padding.y;
  22016. /* draw selected label */
  22017. text.padding = nk_vec2(0,0);
  22018. label.x = header.x + style->combo.content_padding.x;
  22019. label.y = header.y + style->combo.content_padding.y;
  22020. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;;
  22021. label.h = header.h - 2 * style->combo.content_padding.y;
  22022. nk_widget_text(&win->buffer, label, selected, len, &text,
  22023. NK_TEXT_LEFT, ctx->style.font);
  22024. /* draw open/close button */
  22025. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22026. &ctx->style.combo.button, sym, style->font);
  22027. }
  22028. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22029. }
  22030. NK_API int nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
  22031. {return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);}
  22032. NK_API int
  22033. nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
  22034. {
  22035. struct nk_window *win;
  22036. struct nk_style *style;
  22037. const struct nk_input *in;
  22038. struct nk_rect header;
  22039. int is_clicked = nk_false;
  22040. enum nk_widget_layout_states s;
  22041. const struct nk_style_item *background;
  22042. NK_ASSERT(ctx);
  22043. NK_ASSERT(ctx->current);
  22044. NK_ASSERT(ctx->current->layout);
  22045. if (!ctx || !ctx->current || !ctx->current->layout)
  22046. return 0;
  22047. win = ctx->current;
  22048. style = &ctx->style;
  22049. s = nk_widget(&header, ctx);
  22050. if (s == NK_WIDGET_INVALID)
  22051. return 0;
  22052. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22053. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22054. is_clicked = nk_true;
  22055. /* draw combo box header background and border */
  22056. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  22057. background = &style->combo.active;
  22058. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22059. background = &style->combo.hover;
  22060. else background = &style->combo.normal;
  22061. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22062. nk_draw_image(&win->buffer, header, &background->data.image,nk_white);
  22063. } else {
  22064. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22065. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22066. }
  22067. {
  22068. struct nk_rect content;
  22069. struct nk_rect button;
  22070. struct nk_rect bounds;
  22071. enum nk_symbol_type sym;
  22072. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22073. sym = style->combo.sym_hover;
  22074. else if (is_clicked)
  22075. sym = style->combo.sym_active;
  22076. else sym = style->combo.sym_normal;
  22077. /* calculate button */
  22078. button.w = header.h - 2 * style->combo.button_padding.y;
  22079. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22080. button.y = header.y + style->combo.button_padding.y;
  22081. button.h = button.w;
  22082. content.x = button.x + style->combo.button.padding.x;
  22083. content.y = button.y + style->combo.button.padding.y;
  22084. content.w = button.w - 2 * style->combo.button.padding.x;
  22085. content.h = button.h - 2 * style->combo.button.padding.y;
  22086. /* draw color */
  22087. bounds.h = header.h - 4 * style->combo.content_padding.y;
  22088. bounds.y = header.y + 2 * style->combo.content_padding.y;
  22089. bounds.x = header.x + 2 * style->combo.content_padding.x;
  22090. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  22091. nk_fill_rect(&win->buffer, bounds, 0, color);
  22092. /* draw open/close button */
  22093. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22094. &ctx->style.combo.button, sym, style->font);
  22095. }
  22096. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22097. }
  22098. NK_API int
  22099. nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  22100. {
  22101. struct nk_window *win;
  22102. struct nk_style *style;
  22103. const struct nk_input *in;
  22104. struct nk_rect header;
  22105. int is_clicked = nk_false;
  22106. enum nk_widget_layout_states s;
  22107. const struct nk_style_item *background;
  22108. struct nk_color sym_background;
  22109. struct nk_color symbol_color;
  22110. NK_ASSERT(ctx);
  22111. NK_ASSERT(ctx->current);
  22112. NK_ASSERT(ctx->current->layout);
  22113. if (!ctx || !ctx->current || !ctx->current->layout)
  22114. return 0;
  22115. win = ctx->current;
  22116. style = &ctx->style;
  22117. s = nk_widget(&header, ctx);
  22118. if (s == NK_WIDGET_INVALID)
  22119. return 0;
  22120. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22121. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22122. is_clicked = nk_true;
  22123. /* draw combo box header background and border */
  22124. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22125. background = &style->combo.active;
  22126. symbol_color = style->combo.symbol_active;
  22127. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22128. background = &style->combo.hover;
  22129. symbol_color = style->combo.symbol_hover;
  22130. } else {
  22131. background = &style->combo.normal;
  22132. symbol_color = style->combo.symbol_hover;
  22133. }
  22134. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22135. sym_background = nk_rgba(0,0,0,0);
  22136. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22137. } else {
  22138. sym_background = background->data.color;
  22139. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22140. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22141. }
  22142. {
  22143. struct nk_rect bounds = {0,0,0,0};
  22144. struct nk_rect content;
  22145. struct nk_rect button;
  22146. enum nk_symbol_type sym;
  22147. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22148. sym = style->combo.sym_hover;
  22149. else if (is_clicked)
  22150. sym = style->combo.sym_active;
  22151. else sym = style->combo.sym_normal;
  22152. /* calculate button */
  22153. button.w = header.h - 2 * style->combo.button_padding.y;
  22154. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  22155. button.y = header.y + style->combo.button_padding.y;
  22156. button.h = button.w;
  22157. content.x = button.x + style->combo.button.padding.x;
  22158. content.y = button.y + style->combo.button.padding.y;
  22159. content.w = button.w - 2 * style->combo.button.padding.x;
  22160. content.h = button.h - 2 * style->combo.button.padding.y;
  22161. /* draw symbol */
  22162. bounds.h = header.h - 2 * style->combo.content_padding.y;
  22163. bounds.y = header.y + style->combo.content_padding.y;
  22164. bounds.x = header.x + style->combo.content_padding.x;
  22165. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  22166. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  22167. 1.0f, style->font);
  22168. /* draw open/close button */
  22169. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  22170. &ctx->style.combo.button, sym, style->font);
  22171. }
  22172. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22173. }
  22174. NK_API int
  22175. nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
  22176. enum nk_symbol_type symbol, struct nk_vec2 size)
  22177. {
  22178. struct nk_window *win;
  22179. struct nk_style *style;
  22180. struct nk_input *in;
  22181. struct nk_rect header;
  22182. int is_clicked = nk_false;
  22183. enum nk_widget_layout_states s;
  22184. const struct nk_style_item *background;
  22185. struct nk_color symbol_color;
  22186. struct nk_text text;
  22187. NK_ASSERT(ctx);
  22188. NK_ASSERT(ctx->current);
  22189. NK_ASSERT(ctx->current->layout);
  22190. if (!ctx || !ctx->current || !ctx->current->layout)
  22191. return 0;
  22192. win = ctx->current;
  22193. style = &ctx->style;
  22194. s = nk_widget(&header, ctx);
  22195. if (!s) return 0;
  22196. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22197. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22198. is_clicked = nk_true;
  22199. /* draw combo box header background and border */
  22200. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22201. background = &style->combo.active;
  22202. symbol_color = style->combo.symbol_active;
  22203. text.text = style->combo.label_active;
  22204. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22205. background = &style->combo.hover;
  22206. symbol_color = style->combo.symbol_hover;
  22207. text.text = style->combo.label_hover;
  22208. } else {
  22209. background = &style->combo.normal;
  22210. symbol_color = style->combo.symbol_normal;
  22211. text.text = style->combo.label_normal;
  22212. }
  22213. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22214. text.background = nk_rgba(0,0,0,0);
  22215. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22216. } else {
  22217. text.background = background->data.color;
  22218. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22219. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22220. }
  22221. {
  22222. struct nk_rect content;
  22223. struct nk_rect button;
  22224. struct nk_rect label;
  22225. struct nk_rect image;
  22226. enum nk_symbol_type sym;
  22227. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22228. sym = style->combo.sym_hover;
  22229. else if (is_clicked)
  22230. sym = style->combo.sym_active;
  22231. else sym = style->combo.sym_normal;
  22232. /* calculate button */
  22233. button.w = header.h - 2 * style->combo.button_padding.y;
  22234. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22235. button.y = header.y + style->combo.button_padding.y;
  22236. button.h = button.w;
  22237. content.x = button.x + style->combo.button.padding.x;
  22238. content.y = button.y + style->combo.button.padding.y;
  22239. content.w = button.w - 2 * style->combo.button.padding.x;
  22240. content.h = button.h - 2 * style->combo.button.padding.y;
  22241. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22242. &ctx->style.combo.button, sym, style->font);
  22243. /* draw symbol */
  22244. image.x = header.x + style->combo.content_padding.x;
  22245. image.y = header.y + style->combo.content_padding.y;
  22246. image.h = header.h - 2 * style->combo.content_padding.y;
  22247. image.w = image.h;
  22248. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  22249. 1.0f, style->font);
  22250. /* draw label */
  22251. text.padding = nk_vec2(0,0);
  22252. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  22253. label.y = header.y + style->combo.content_padding.y;
  22254. label.w = (button.x - style->combo.content_padding.x) - label.x;
  22255. label.h = header.h - 2 * style->combo.content_padding.y;
  22256. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  22257. }
  22258. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22259. }
  22260. NK_API int
  22261. nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
  22262. {
  22263. struct nk_window *win;
  22264. struct nk_style *style;
  22265. const struct nk_input *in;
  22266. struct nk_rect header;
  22267. int is_clicked = nk_false;
  22268. enum nk_widget_layout_states s;
  22269. const struct nk_style_item *background;
  22270. NK_ASSERT(ctx);
  22271. NK_ASSERT(ctx->current);
  22272. NK_ASSERT(ctx->current->layout);
  22273. if (!ctx || !ctx->current || !ctx->current->layout)
  22274. return 0;
  22275. win = ctx->current;
  22276. style = &ctx->style;
  22277. s = nk_widget(&header, ctx);
  22278. if (s == NK_WIDGET_INVALID)
  22279. return 0;
  22280. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22281. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22282. is_clicked = nk_true;
  22283. /* draw combo box header background and border */
  22284. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  22285. background = &style->combo.active;
  22286. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22287. background = &style->combo.hover;
  22288. else background = &style->combo.normal;
  22289. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22290. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22291. } else {
  22292. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22293. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22294. }
  22295. {
  22296. struct nk_rect bounds = {0,0,0,0};
  22297. struct nk_rect content;
  22298. struct nk_rect button;
  22299. enum nk_symbol_type sym;
  22300. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22301. sym = style->combo.sym_hover;
  22302. else if (is_clicked)
  22303. sym = style->combo.sym_active;
  22304. else sym = style->combo.sym_normal;
  22305. /* calculate button */
  22306. button.w = header.h - 2 * style->combo.button_padding.y;
  22307. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  22308. button.y = header.y + style->combo.button_padding.y;
  22309. button.h = button.w;
  22310. content.x = button.x + style->combo.button.padding.x;
  22311. content.y = button.y + style->combo.button.padding.y;
  22312. content.w = button.w - 2 * style->combo.button.padding.x;
  22313. content.h = button.h - 2 * style->combo.button.padding.y;
  22314. /* draw image */
  22315. bounds.h = header.h - 2 * style->combo.content_padding.y;
  22316. bounds.y = header.y + style->combo.content_padding.y;
  22317. bounds.x = header.x + style->combo.content_padding.x;
  22318. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  22319. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  22320. /* draw open/close button */
  22321. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  22322. &ctx->style.combo.button, sym, style->font);
  22323. }
  22324. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22325. }
  22326. NK_API int
  22327. nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
  22328. struct nk_image img, struct nk_vec2 size)
  22329. {
  22330. struct nk_window *win;
  22331. struct nk_style *style;
  22332. struct nk_input *in;
  22333. struct nk_rect header;
  22334. int is_clicked = nk_false;
  22335. enum nk_widget_layout_states s;
  22336. const struct nk_style_item *background;
  22337. struct nk_text text;
  22338. NK_ASSERT(ctx);
  22339. NK_ASSERT(ctx->current);
  22340. NK_ASSERT(ctx->current->layout);
  22341. if (!ctx || !ctx->current || !ctx->current->layout)
  22342. return 0;
  22343. win = ctx->current;
  22344. style = &ctx->style;
  22345. s = nk_widget(&header, ctx);
  22346. if (!s) return 0;
  22347. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM)? 0: &ctx->input;
  22348. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  22349. is_clicked = nk_true;
  22350. /* draw combo box header background and border */
  22351. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  22352. background = &style->combo.active;
  22353. text.text = style->combo.label_active;
  22354. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  22355. background = &style->combo.hover;
  22356. text.text = style->combo.label_hover;
  22357. } else {
  22358. background = &style->combo.normal;
  22359. text.text = style->combo.label_normal;
  22360. }
  22361. if (background->type == NK_STYLE_ITEM_IMAGE) {
  22362. text.background = nk_rgba(0,0,0,0);
  22363. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  22364. } else {
  22365. text.background = background->data.color;
  22366. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  22367. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  22368. }
  22369. {
  22370. struct nk_rect content;
  22371. struct nk_rect button;
  22372. struct nk_rect label;
  22373. struct nk_rect image;
  22374. enum nk_symbol_type sym;
  22375. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  22376. sym = style->combo.sym_hover;
  22377. else if (is_clicked)
  22378. sym = style->combo.sym_active;
  22379. else sym = style->combo.sym_normal;
  22380. /* calculate button */
  22381. button.w = header.h - 2 * style->combo.button_padding.y;
  22382. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  22383. button.y = header.y + style->combo.button_padding.y;
  22384. button.h = button.w;
  22385. content.x = button.x + style->combo.button.padding.x;
  22386. content.y = button.y + style->combo.button.padding.y;
  22387. content.w = button.w - 2 * style->combo.button.padding.x;
  22388. content.h = button.h - 2 * style->combo.button.padding.y;
  22389. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  22390. &ctx->style.combo.button, sym, style->font);
  22391. /* draw image */
  22392. image.x = header.x + style->combo.content_padding.x;
  22393. image.y = header.y + style->combo.content_padding.y;
  22394. image.h = header.h - 2 * style->combo.content_padding.y;
  22395. image.w = image.h;
  22396. nk_draw_image(&win->buffer, image, &img, nk_white);
  22397. /* draw label */
  22398. text.padding = nk_vec2(0,0);
  22399. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  22400. label.y = header.y + style->combo.content_padding.y;
  22401. label.w = (button.x - style->combo.content_padding.x) - label.x;
  22402. label.h = header.h - 2 * style->combo.content_padding.y;
  22403. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  22404. }
  22405. return nk_combo_begin(ctx, win, size, is_clicked, header);
  22406. }
  22407. NK_API int nk_combo_begin_symbol_label(struct nk_context *ctx,
  22408. const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
  22409. {return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);}
  22410. NK_API int nk_combo_begin_image_label(struct nk_context *ctx,
  22411. const char *selected, struct nk_image img, struct nk_vec2 size)
  22412. {return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);}
  22413. NK_API int nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
  22414. {return nk_contextual_item_text(ctx, text, len, align);}
  22415. NK_API int nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  22416. {return nk_contextual_item_label(ctx, label, align);}
  22417. NK_API int nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
  22418. int len, nk_flags alignment)
  22419. {return nk_contextual_item_image_text(ctx, img, text, len, alignment);}
  22420. NK_API int nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
  22421. const char *text, nk_flags alignment)
  22422. {return nk_contextual_item_image_label(ctx, img, text, alignment);}
  22423. NK_API int nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  22424. const char *text, int len, nk_flags alignment)
  22425. {return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);}
  22426. NK_API int nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  22427. const char *label, nk_flags alignment)
  22428. {return nk_contextual_item_symbol_label(ctx, sym, label, alignment);}
  22429. NK_API void nk_combo_end(struct nk_context *ctx)
  22430. {nk_contextual_end(ctx);}
  22431. NK_API void nk_combo_close(struct nk_context *ctx)
  22432. {nk_contextual_close(ctx);}
  22433. NK_API int
  22434. nk_combo(struct nk_context *ctx, const char **items, int count,
  22435. int selected, int item_height, struct nk_vec2 size)
  22436. {
  22437. int i = 0;
  22438. int max_height;
  22439. struct nk_vec2 item_spacing;
  22440. struct nk_vec2 window_padding;
  22441. NK_ASSERT(ctx);
  22442. NK_ASSERT(items);
  22443. NK_ASSERT(ctx->current);
  22444. if (!ctx || !items ||!count)
  22445. return selected;
  22446. item_spacing = ctx->style.window.spacing;
  22447. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  22448. max_height = count * item_height + count * (int)item_spacing.y;
  22449. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  22450. size.y = NK_MIN(size.y, (float)max_height);
  22451. if (nk_combo_begin_label(ctx, items[selected], size)) {
  22452. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  22453. for (i = 0; i < count; ++i) {
  22454. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  22455. selected = i;
  22456. }
  22457. nk_combo_end(ctx);
  22458. }
  22459. return selected;
  22460. }
  22461. NK_API int
  22462. nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  22463. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  22464. {
  22465. int i;
  22466. int max_height;
  22467. struct nk_vec2 item_spacing;
  22468. struct nk_vec2 window_padding;
  22469. const char *current_item;
  22470. const char *iter;
  22471. int length = 0;
  22472. NK_ASSERT(ctx);
  22473. NK_ASSERT(items_separated_by_separator);
  22474. if (!ctx || !items_separated_by_separator)
  22475. return selected;
  22476. /* calculate popup window */
  22477. item_spacing = ctx->style.window.spacing;
  22478. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  22479. max_height = count * item_height + count * (int)item_spacing.y;
  22480. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  22481. size.y = NK_MIN(size.y, (float)max_height);
  22482. /* find selected item */
  22483. current_item = items_separated_by_separator;
  22484. for (i = 0; i < count; ++i) {
  22485. iter = current_item;
  22486. while (*iter && *iter != separator) iter++;
  22487. length = (int)(iter - current_item);
  22488. if (i == selected) break;
  22489. current_item = iter + 1;
  22490. }
  22491. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  22492. current_item = items_separated_by_separator;
  22493. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  22494. for (i = 0; i < count; ++i) {
  22495. iter = current_item;
  22496. while (*iter && *iter != separator) iter++;
  22497. length = (int)(iter - current_item);
  22498. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  22499. selected = i;
  22500. current_item = current_item + length + 1;
  22501. }
  22502. nk_combo_end(ctx);
  22503. }
  22504. return selected;
  22505. }
  22506. NK_API int
  22507. nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  22508. int selected, int count, int item_height, struct nk_vec2 size)
  22509. {return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);}
  22510. NK_API int
  22511. nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
  22512. void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
  22513. {
  22514. int i;
  22515. int max_height;
  22516. struct nk_vec2 item_spacing;
  22517. struct nk_vec2 window_padding;
  22518. const char *item;
  22519. NK_ASSERT(ctx);
  22520. NK_ASSERT(item_getter);
  22521. if (!ctx || !item_getter)
  22522. return selected;
  22523. /* calculate popup window */
  22524. item_spacing = ctx->style.window.spacing;
  22525. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  22526. max_height = count * item_height + count * (int)item_spacing.y;
  22527. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  22528. size.y = NK_MIN(size.y, (float)max_height);
  22529. item_getter(userdata, selected, &item);
  22530. if (nk_combo_begin_label(ctx, item, size)) {
  22531. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  22532. for (i = 0; i < count; ++i) {
  22533. item_getter(userdata, i, &item);
  22534. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  22535. selected = i;
  22536. }
  22537. nk_combo_end(ctx);
  22538. }
  22539. return selected;
  22540. }
  22541. NK_API void nk_combobox(struct nk_context *ctx, const char **items, int count,
  22542. int *selected, int item_height, struct nk_vec2 size)
  22543. {*selected = nk_combo(ctx, items, count, *selected, item_height, size);}
  22544. NK_API void nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  22545. int *selected, int count, int item_height, struct nk_vec2 size)
  22546. {*selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);}
  22547. NK_API void nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  22548. int separator,int *selected, int count, int item_height, struct nk_vec2 size)
  22549. {*selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  22550. *selected, count, item_height, size);}
  22551. NK_API void nk_combobox_callback(struct nk_context *ctx,
  22552. void(*item_getter)(void* data, int id, const char **out_text),
  22553. void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
  22554. {*selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);}
  22555. /*
  22556. * -------------------------------------------------------------
  22557. *
  22558. * MENU
  22559. *
  22560. * --------------------------------------------------------------
  22561. */
  22562. NK_INTERN int
  22563. nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
  22564. const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  22565. {
  22566. int is_open = 0;
  22567. int is_active = 0;
  22568. struct nk_rect body;
  22569. struct nk_window *popup;
  22570. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  22571. NK_ASSERT(ctx);
  22572. NK_ASSERT(ctx->current);
  22573. NK_ASSERT(ctx->current->layout);
  22574. if (!ctx || !ctx->current || !ctx->current->layout)
  22575. return 0;
  22576. body.x = header.x;
  22577. body.w = size.x;
  22578. body.y = header.y + header.h;
  22579. body.h = size.y;
  22580. popup = win->popup.win;
  22581. is_open = popup ? nk_true : nk_false;
  22582. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  22583. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  22584. (!is_open && !is_active && !is_clicked)) return 0;
  22585. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  22586. return 0;
  22587. win->popup.type = NK_PANEL_MENU;
  22588. win->popup.name = hash;
  22589. return 1;
  22590. }
  22591. NK_API int
  22592. nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
  22593. nk_flags align, struct nk_vec2 size)
  22594. {
  22595. struct nk_window *win;
  22596. const struct nk_input *in;
  22597. struct nk_rect header;
  22598. int is_clicked = nk_false;
  22599. nk_flags state;
  22600. NK_ASSERT(ctx);
  22601. NK_ASSERT(ctx->current);
  22602. NK_ASSERT(ctx->current->layout);
  22603. if (!ctx || !ctx->current || !ctx->current->layout)
  22604. return 0;
  22605. win = ctx->current;
  22606. state = nk_widget(&header, ctx);
  22607. if (!state) return 0;
  22608. in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22609. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  22610. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  22611. is_clicked = nk_true;
  22612. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  22613. }
  22614. NK_API int nk_menu_begin_label(struct nk_context *ctx,
  22615. const char *text, nk_flags align, struct nk_vec2 size)
  22616. {return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);}
  22617. NK_API int
  22618. nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
  22619. struct nk_vec2 size)
  22620. {
  22621. struct nk_window *win;
  22622. struct nk_rect header;
  22623. const struct nk_input *in;
  22624. int is_clicked = nk_false;
  22625. nk_flags state;
  22626. NK_ASSERT(ctx);
  22627. NK_ASSERT(ctx->current);
  22628. NK_ASSERT(ctx->current->layout);
  22629. if (!ctx || !ctx->current || !ctx->current->layout)
  22630. return 0;
  22631. win = ctx->current;
  22632. state = nk_widget(&header, ctx);
  22633. if (!state) return 0;
  22634. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22635. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  22636. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  22637. is_clicked = nk_true;
  22638. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  22639. }
  22640. NK_API int
  22641. nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
  22642. enum nk_symbol_type sym, struct nk_vec2 size)
  22643. {
  22644. struct nk_window *win;
  22645. const struct nk_input *in;
  22646. struct nk_rect header;
  22647. int is_clicked = nk_false;
  22648. nk_flags state;
  22649. NK_ASSERT(ctx);
  22650. NK_ASSERT(ctx->current);
  22651. NK_ASSERT(ctx->current->layout);
  22652. if (!ctx || !ctx->current || !ctx->current->layout)
  22653. return 0;
  22654. win = ctx->current;
  22655. state = nk_widget(&header, ctx);
  22656. if (!state) return 0;
  22657. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22658. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  22659. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  22660. is_clicked = nk_true;
  22661. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  22662. }
  22663. NK_API int
  22664. nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
  22665. nk_flags align, struct nk_image img, struct nk_vec2 size)
  22666. {
  22667. struct nk_window *win;
  22668. struct nk_rect header;
  22669. const struct nk_input *in;
  22670. int is_clicked = nk_false;
  22671. nk_flags state;
  22672. NK_ASSERT(ctx);
  22673. NK_ASSERT(ctx->current);
  22674. NK_ASSERT(ctx->current->layout);
  22675. if (!ctx || !ctx->current || !ctx->current->layout)
  22676. return 0;
  22677. win = ctx->current;
  22678. state = nk_widget(&header, ctx);
  22679. if (!state) return 0;
  22680. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22681. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  22682. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  22683. ctx->style.font, in))
  22684. is_clicked = nk_true;
  22685. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  22686. }
  22687. NK_API int nk_menu_begin_image_label(struct nk_context *ctx,
  22688. const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  22689. {return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);}
  22690. NK_API int
  22691. nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
  22692. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  22693. {
  22694. struct nk_window *win;
  22695. struct nk_rect header;
  22696. const struct nk_input *in;
  22697. int is_clicked = nk_false;
  22698. nk_flags state;
  22699. NK_ASSERT(ctx);
  22700. NK_ASSERT(ctx->current);
  22701. NK_ASSERT(ctx->current->layout);
  22702. if (!ctx || !ctx->current || !ctx->current->layout)
  22703. return 0;
  22704. win = ctx->current;
  22705. state = nk_widget(&header, ctx);
  22706. if (!state) return 0;
  22707. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22708. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  22709. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  22710. ctx->style.font, in)) is_clicked = nk_true;
  22711. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  22712. }
  22713. NK_API int nk_menu_begin_symbol_label(struct nk_context *ctx,
  22714. const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
  22715. {return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);}
  22716. NK_API int nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
  22717. {return nk_contextual_item_text(ctx, title, len, align);}
  22718. NK_API int nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  22719. {return nk_contextual_item_label(ctx, label, align);}
  22720. NK_API int nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
  22721. const char *label, nk_flags align)
  22722. {return nk_contextual_item_image_label(ctx, img, label, align);}
  22723. NK_API int nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
  22724. const char *text, int len, nk_flags align)
  22725. {return nk_contextual_item_image_text(ctx, img, text, len, align);}
  22726. NK_API int nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  22727. const char *text, int len, nk_flags align)
  22728. {return nk_contextual_item_symbol_text(ctx, sym, text, len, align);}
  22729. NK_API int nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  22730. const char *label, nk_flags align)
  22731. {return nk_contextual_item_symbol_label(ctx, sym, label, align);}
  22732. NK_API void nk_menu_close(struct nk_context *ctx)
  22733. {nk_contextual_close(ctx);}
  22734. NK_API void
  22735. nk_menu_end(struct nk_context *ctx)
  22736. {nk_contextual_end(ctx);}
  22737. #endif /* NK_IMPLEMENTATION */
  22738. /*
  22739. /// ## License
  22740. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  22741. /// ------------------------------------------------------------------------------
  22742. /// This software is available under 2 licenses -- choose whichever you prefer.
  22743. /// ------------------------------------------------------------------------------
  22744. /// ALTERNATIVE A - MIT License
  22745. /// Copyright (c) 2016-2018 Micha Mettke
  22746. /// Permission is hereby granted, free of charge, to any person obtaining a copy of
  22747. /// this software and associated documentation files (the "Software"), to deal in
  22748. /// the Software without restriction, including without limitation the rights to
  22749. /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  22750. /// of the Software, and to permit persons to whom the Software is furnished to do
  22751. /// so, subject to the following conditions:
  22752. /// The above copyright notice and this permission notice shall be included in all
  22753. /// copies or substantial portions of the Software.
  22754. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22755. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22756. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22757. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22758. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22759. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22760. /// SOFTWARE.
  22761. /// ------------------------------------------------------------------------------
  22762. /// ALTERNATIVE B - Public Domain (www.unlicense.org)
  22763. /// This is free and unencumbered software released into the public domain.
  22764. /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  22765. /// software, either in source code form or as a compiled binary, for any purpose,
  22766. /// commercial or non-commercial, and by any means.
  22767. /// In jurisdictions that recognize copyright laws, the author or authors of this
  22768. /// software dedicate any and all copyright interest in the software to the public
  22769. /// domain. We make this dedication for the benefit of the public at large and to
  22770. /// the detriment of our heirs and successors. We intend this dedication to be an
  22771. /// overt act of relinquishment in perpetuity of all present and future rights to
  22772. /// this software under copyright law.
  22773. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22774. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22775. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22776. /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22777. /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22778. /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22779. /// ------------------------------------------------------------------------------
  22780. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22781. /// ## Changelog
  22782. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  22783. /// [date][x.yy.zz]-[description]
  22784. /// -[date]: date on which the change has been pushed
  22785. /// -[x.yy.zz]: Numerical version string representation. Each version number on the right
  22786. /// resets back to zero if version on the left is incremented.
  22787. /// - [x]: Major version with API and library breaking
  22788. /// - [yy]: Minor version with non-breaking API and library changes
  22789. /// - [zz]: Bug fix version with no direct changes to API
  22790. ///
  22791. /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior
  22792. /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process
  22793. /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype
  22794. /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug
  22795. /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separed group identifier and title
  22796. /// - 2018/01/07 (3.00.1) - Started to change documentation style
  22797. /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
  22798. /// because of conversions between float and byte color representation.
  22799. /// Color pickers now use floating point values to represent
  22800. /// HSV values. To get back the old behavior I added some additional
  22801. /// color conversion functions to cast between nk_color and
  22802. /// nk_colorf.
  22803. /// - 2017/12/23 (2.00.7) - Fixed small warning
  22804. /// - 2017/12/23 (2.00.7) - Fixed nk_edit_buffer behavior if activated to allow input
  22805. /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior
  22806. /// - 2017/12/04 (2.00.6) - Added formated string tooltip widget
  22807. /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag NK_WINDOW_NO_INPUT
  22808. /// - 2017/11/15 (2.00.4) - Fixed font merging
  22809. /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions
  22810. /// - 2017/09/14 (2.00.2) - Fixed nk_edit_buffer and nk_edit_focus behavior
  22811. /// - 2017/09/14 (2.00.1) - Fixed window closing behavior
  22812. /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifing window position and size funtions now
  22813. /// require the name of the window and must happen outside the window
  22814. /// building process (between function call nk_begin and nk_end).
  22815. /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last
  22816. /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows
  22817. /// - 2017/08/27 (1.40.7) - Fixed window background flag
  22818. /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
  22819. /// query for widgets
  22820. /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
  22821. /// and filled rectangles
  22822. /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
  22823. /// process of being destroyed.
  22824. /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
  22825. /// window instead of directly in table.
  22826. /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro
  22827. /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero
  22828. /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
  22829. /// comes in effect if you pass in zero was row height argument
  22830. /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
  22831. /// how layouting works. From now there will be an internal minimum
  22832. /// row height derived from font height. If you need a row smaller than
  22833. /// that you can directly set it by `nk_layout_set_min_row_height` and
  22834. /// reset the value back by calling `nk_layout_reset_min_row_height.
  22835. /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix
  22836. /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a nk_layout_xxx function
  22837. /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer
  22838. /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped
  22839. /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundries
  22840. /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space
  22841. /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size
  22842. /// - 2017/05/06 (1.38.0) - Added platform double-click support
  22843. /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends
  22844. /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipbard support
  22845. /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing
  22846. /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error
  22847. /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags
  22848. /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption
  22849. /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows
  22850. /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior
  22851. /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377
  22852. /// - 2017/03/18 (1.34.3) - Fixed long window header titles
  22853. /// - 2017/03/04 (1.34.2) - Fixed text edit filtering
  22854. /// - 2017/03/04 (1.34.1) - Fixed group closable flag
  22855. /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support
  22856. /// - 2017/01/24 (1.33.0) - Added programatic way of remove edit focus
  22857. /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows
  22858. /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows
  22859. /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing
  22860. /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner
  22861. /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
  22862. /// dynamic and static widgets.
  22863. /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit
  22864. /// - 2016/12/31 (1.29.2)- Fixed closing window bug of minimized windows
  22865. /// - 2016/12/03 (1.29.1)- Fixed wrapped text with no seperator and C89 error
  22866. /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters
  22867. /// - 2016/11/22 (1.28.6)- Fixed window minimized closing bug
  22868. /// - 2016/11/19 (1.28.5)- Fixed abstract combo box closing behavior
  22869. /// - 2016/11/19 (1.28.4)- Fixed tooltip flickering
  22870. /// - 2016/11/19 (1.28.3)- Fixed memory leak caused by popup repeated closing
  22871. /// - 2016/11/18 (1.28.2)- Fixed memory leak caused by popup panel allocation
  22872. /// - 2016/11/10 (1.28.1)- Fixed some warnings and C++ error
  22873. /// - 2016/11/10 (1.28.0)- Added additional `nk_button` versions which allows to directly
  22874. /// pass in a style struct to change buttons visual.
  22875. /// - 2016/11/10 (1.27.0)- Added additional 'nk_tree' versions to support external state
  22876. /// storage. Just like last the `nk_group` commit the main
  22877. /// advantage is that you optionally can minimize nuklears runtime
  22878. /// memory consumption or handle hash collisions.
  22879. /// - 2016/11/09 (1.26.0)- Added additional `nk_group` version to support external scrollbar
  22880. /// offset storage. Main advantage is that you can externalize
  22881. /// the memory management for the offset. It could also be helpful
  22882. /// if you have a hash collision in `nk_group_begin` but really
  22883. /// want the name. In addition I added `nk_list_view` which allows
  22884. /// to draw big lists inside a group without actually having to
  22885. /// commit the whole list to nuklear (issue #269).
  22886. /// - 2016/10/30 (1.25.1)- Fixed clipping rectangle bug inside `nk_draw_list`
  22887. /// - 2016/10/29 (1.25.0)- Pulled `nk_panel` memory management into nuklear and out of
  22888. /// the hands of the user. From now on users don't have to care
  22889. /// about panels unless they care about some information. If you
  22890. /// still need the panel just call `nk_window_get_panel`.
  22891. /// - 2016/10/21 (1.24.0)- Changed widget border drawing to stroked rectangle from filled
  22892. /// rectangle for less overdraw and widget background transparency.
  22893. /// - 2016/10/18 (1.23.0)- Added `nk_edit_focus` for manually edit widget focus control
  22894. /// - 2016/09/29 (1.22.7)- Fixed deduction of basic type in non `<stdint.h>` compilation
  22895. /// - 2016/09/29 (1.22.6)- Fixed edit widget UTF-8 text cursor drawing bug
  22896. /// - 2016/09/28 (1.22.5)- Fixed edit widget UTF-8 text appending/inserting/removing
  22897. /// - 2016/09/28 (1.22.4)- Fixed drawing bug inside edit widgets which offset all text
  22898. /// text in every edit widget if one of them is scrolled.
  22899. /// - 2016/09/28 (1.22.3)- Fixed small bug in edit widgets if not active. The wrong
  22900. /// text length is passed. It should have been in bytes but
  22901. /// was passed as glyphes.
  22902. /// - 2016/09/20 (1.22.2)- Fixed color button size calculation
  22903. /// - 2016/09/20 (1.22.1)- Fixed some `nk_vsnprintf` behavior bugs and removed
  22904. /// `<stdio.h>` again from `NK_INCLUDE_STANDARD_VARARGS`.
  22905. /// - 2016/09/18 (1.22.0)- C89 does not support vsnprintf only C99 and newer as well
  22906. /// as C++11 and newer. In addition to use vsnprintf you have
  22907. /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
  22908. /// is not enough. That behavior is now fixed. By default if
  22909. /// both varargs as well as stdio is selected I try to use
  22910. /// vsnprintf if not possible I will revert to vsprintf. If
  22911. /// varargs but not stdio was defined I will use my own function.
  22912. /// - 2016/09/15 (1.21.2)- Fixed panel `close` behavior for deeper panel levels
  22913. /// - 2016/09/15 (1.21.1)- Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`
  22914. /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
  22915. /// and contextual which prevented closing in y-direction if
  22916. /// popup did not reach max height.
  22917. /// In addition the height parameter was changed into vec2
  22918. /// for width and height to have more control over the popup size.
  22919. /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection
  22920. /// - 2016/09/13 (1.20.2)- Fixed slider behavior hopefully for the last time. This time
  22921. /// all calculation are correct so no more hackery.
  22922. /// - 2016/09/13 (1.20.1)- Internal change to divide window/panel flags into panel flags and types.
  22923. /// Suprisinly spend years in C and still happened to confuse types
  22924. /// with flags. Probably something to take note.
  22925. /// - 2016/09/08 (1.20.0)- Added additional helper function to make it easier to just
  22926. /// take the produced buffers from `nk_convert` and unplug the
  22927. /// iteration process from `nk_context`. So now you can
  22928. /// just use the vertex,element and command buffer + two pointer
  22929. /// inside the command buffer retrieved by calls `nk__draw_begin`
  22930. /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
  22931. /// - 2016/09/08 (1.19.0)- Added additional asserts to make sure every `nk_xxx_begin` call
  22932. /// for windows, popups, combobox, menu and contextual is guarded by
  22933. /// `if` condition and does not produce false drawing output.
  22934. /// - 2016/09/08 (1.18.0)- Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
  22935. /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
  22936. /// `NK_SYMBOL_RECT_OUTLINE`.
  22937. /// - 2016/09/08 (1.17.0)- Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
  22938. /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
  22939. /// `NK_SYMBOL_CIRCLE_OUTLINE`.
  22940. /// - 2016/09/08 (1.16.0)- Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
  22941. /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
  22942. /// - 2016/09/07 (1.15.3)- Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error
  22943. /// - 2016/09/04 (1.15.2)- Fixed wrong combobox height calculation
  22944. /// - 2016/09/03 (1.15.1)- Fixed gaps inside combo boxes in OpenGL
  22945. /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
  22946. /// instead made it user provided. The range of types to convert
  22947. /// to is quite limited at the moment, but I would be more than
  22948. /// happy to accept PRs to add additional.
  22949. /// - 2016/08/30 (1.14.2) - Removed unused variables
  22950. /// - 2016/08/30 (1.14.1) - Fixed C++ build errors
  22951. /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly
  22952. /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables
  22953. /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
  22954. /// refrain from using slider with a big number of steps.
  22955. /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
  22956. /// window was in Read Only Mode.
  22957. /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
  22958. /// a hack for combo box and menu.
  22959. /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
  22960. /// it is bugged and causes issues in window selection.
  22961. /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
  22962. /// determined by the scrollbar size
  22963. /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11
  22964. /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection
  22965. /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
  22966. /// handling panel padding and panel border.
  22967. /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`
  22968. /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups
  22969. /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes
  22970. /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
  22971. /// hash collisions. Currently limited to NK_WINDOW_MAX_NAME
  22972. /// which in term can be redefined if not big enough.
  22973. /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code
  22974. /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
  22975. /// to account for key press and release happening in one frame.
  22976. /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate
  22977. /// - 2016/08/17 (1.09.6)- Removed invalid check for value zero in nk_propertyx
  22978. /// - 2016/08/16 (1.09.5)- Fixed ROM mode for deeper levels of popup windows parents.
  22979. /// - 2016/08/15 (1.09.4)- Editbox are now still active if enter was pressed with flag
  22980. /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
  22981. /// typing after commiting.
  22982. /// - 2016/08/15 (1.09.4)- Removed redundant code
  22983. /// - 2016/08/15 (1.09.4)- Fixed negative numbers in `nk_strtoi` and remove unused variable
  22984. /// - 2016/08/15 (1.09.3)- Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
  22985. /// window only as selected by hovering and not by clicking.
  22986. /// - 2016/08/14 (1.09.2)- Fixed a bug in font atlas which caused wrong loading
  22987. /// of glyphes for font with multiple ranges.
  22988. /// - 2016/08/12 (1.09.1)- Added additional function to check if window is currently
  22989. /// hidden and therefore not visible.
  22990. /// - 2016/08/12 (1.09.1)- nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
  22991. /// instead of the old flag `NK_WINDOW_HIDDEN`
  22992. /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
  22993. /// the underlying implementation to not cast to float and instead
  22994. /// work directly on the given values.
  22995. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  22996. /// floating pointer number to string conversion for additional
  22997. /// precision.
  22998. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  22999. /// string to floating point number conversion for additional
  23000. /// precision.
  23001. /// - 2016/08/08 (1.07.2)- Fixed compiling error without define NK_INCLUDE_FIXED_TYPE
  23002. /// - 2016/08/08 (1.07.1)- Fixed possible floating point error inside `nk_widget` leading
  23003. /// to wrong wiget width calculation which results in widgets falsly
  23004. /// becomming tagged as not inside window and cannot be accessed.
  23005. /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
  23006. /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
  23007. /// by using `nk_window_show` and closed by either clicking the close
  23008. /// icon in a window or by calling `nk_window_close`. Only closed
  23009. /// windows get removed at the end of the frame while hidden windows
  23010. /// remain.
  23011. /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
  23012. /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
  23013. /// - 2016/08/08 (1.05.4)- Fixed scrollbar auto hiding behavior
  23014. /// - 2016/08/08 (1.05.3)- Fixed wrong panel padding selection in `nk_layout_widget_space`
  23015. /// - 2016/08/07 (1.05.2)- Fixed old bug in dynamic immediate mode layout API, calculating
  23016. /// wrong item spacing and panel width.
  23017. ///- 2016/08/07 (1.05.1)- Hopefully finally fixed combobox popup drawing bug
  23018. ///- 2016/08/07 (1.05.0) - Split varargs away from NK_INCLUDE_STANDARD_IO into own
  23019. /// define NK_INCLUDE_STANDARD_VARARGS to allow more fine
  23020. /// grained controlled over library includes.
  23021. /// - 2016/08/06 (1.04.5)- Changed memset calls to NK_MEMSET
  23022. /// - 2016/08/04 (1.04.4)- Fixed fast window scaling behavior
  23023. /// - 2016/08/04 (1.04.3)- Fixed window scaling, movement bug which appears if you
  23024. /// move/scale a window and another window is behind it.
  23025. /// If you are fast enough then the window behind gets activated
  23026. /// and the operation is blocked. I now require activating
  23027. /// by hovering only if mouse is not pressed.
  23028. /// - 2016/08/04 (1.04.2)- Fixed changing fonts
  23029. /// - 2016/08/03 (1.04.1)- Fixed `NK_WINDOW_BACKGROUND` behavior
  23030. /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`
  23031. /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
  23032. /// sub windows (combo, menu, ...)
  23033. /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor
  23034. /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
  23035. /// to be always in the background of the screen
  23036. /// - 2016/08/03 (1.03.2)- Removed invalid assert macro for NK_RGB color picker
  23037. /// - 2016/08/01 (1.03.1)- Added helper macros into header include guard
  23038. /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
  23039. /// simplify memory management by removing the need to
  23040. /// allocate the pool.
  23041. /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
  23042. /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
  23043. /// seconds without window interaction. To make it work
  23044. /// you have to also set a delta time inside the `nk_context`.
  23045. /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs
  23046. /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`
  23047. /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument
  23048. /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
  23049. /// font atlas memory management by converting pointer
  23050. /// arrays for fonts and font configurations to lists.
  23051. /// - 2016/07/15 (1.00.0) - Changed button API to use context dependend button
  23052. /// behavior instead of passing it for every function call.
  23053. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23054. ///
  23055. /// ## Gallery
  23056. /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
  23057. /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
  23058. /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
  23059. /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
  23060. /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
  23061. /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
  23062. /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
  23063. /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
  23064. /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
  23065. ///
  23066. /// ## Credits
  23067. /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
  23068. ///
  23069. /// 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 />
  23070. /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from r-lyeh@github.com for documentation generation <br /><br />
  23071. /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
  23072. ///
  23073. /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
  23074. /// giving me the inspiration for this library, Casey Muratori for handmade hero
  23075. /// and his original immediate mode graphical user interface idea and Sean
  23076. /// Barret for his amazing single header libraries which restored my faith
  23077. /// in libraries and brought me to create some of my own.
  23078. ///
  23079. */