overview.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /* nuklear - v1.00 - public domain */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <math.h>
  10. #include <time.h>
  11. #include <limits.h>
  12. #include <GL/glew.h>
  13. #include <GL/gl.h>
  14. #include <GL/glu.h>
  15. #include <GLFW/glfw3.h>
  16. #define NK_INCLUDE_FIXED_TYPES
  17. #define NK_INCLUDE_STANDARD_IO
  18. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  19. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  20. #define NK_INCLUDE_FONT_BAKING
  21. #define NK_INCLUDE_DEFAULT_FONT
  22. #define NK_IMPLEMENTATION
  23. #include "../nuklear.h"
  24. /* macros */
  25. #define WINDOW_WIDTH 800
  26. #define WINDOW_HEIGHT 600
  27. #define MAX_VERTEX_MEMORY 512 * 1024
  28. #define MAX_ELEMENT_MEMORY 128 * 1024
  29. #define UNUSED(a) (void)a
  30. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  31. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  32. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  33. /* ===============================================================
  34. *
  35. * GUI
  36. *
  37. * ===============================================================*/
  38. static int
  39. overview_window(struct nk_context *ctx)
  40. {
  41. struct nk_panel menu;
  42. /* window flags */
  43. static int show_menu = nk_true;
  44. static int titlebar = nk_true;
  45. static int border = nk_true;
  46. static int resize = nk_true;
  47. static int moveable = nk_true;
  48. static int no_scrollbar = nk_false;
  49. static nk_flags window_flags = 0;
  50. static int minimizable = nk_true;
  51. static int close = nk_true;
  52. /* popups */
  53. static enum nk_style_header_align header_align = NK_HEADER_RIGHT;
  54. static int show_app_about = nk_false;
  55. struct nk_panel layout;
  56. /* window flags */
  57. window_flags = 0;
  58. ctx->style.window.header.align = header_align;
  59. if (border) window_flags |= NK_WINDOW_BORDER;
  60. if (resize) window_flags |= NK_WINDOW_SCALABLE;
  61. if (moveable) window_flags |= NK_WINDOW_MOVABLE;
  62. if (no_scrollbar) window_flags |= NK_WINDOW_NO_SCROLLBAR;
  63. if (minimizable) window_flags |= NK_WINDOW_MINIMIZABLE;
  64. if (close) window_flags |= NK_WINDOW_CLOSABLE;
  65. if (nk_begin(ctx, &layout, "Demo", nk_rect(10, 10, 400, 750), window_flags))
  66. {
  67. if (show_menu)
  68. {
  69. /* menubar */
  70. enum menu_states {MENU_DEFAULT, MENU_WINDOWS};
  71. static enum menu_states menu_state = MENU_DEFAULT;
  72. static nk_size mprog = 60;
  73. static int mslider = 10;
  74. static int mcheck = nk_true;
  75. nk_menubar_begin(ctx);
  76. nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  77. nk_layout_row_push(ctx, 45);
  78. if (nk_menu_begin_label(ctx, &menu, "MENU", NK_TEXT_LEFT, 120))
  79. {
  80. static size_t prog = 40;
  81. static int slider = 10;
  82. static int check = nk_true;
  83. nk_layout_row_dynamic(ctx, 25, 1);
  84. if (nk_menu_item_label(ctx, "Hide", NK_TEXT_LEFT))
  85. show_menu = nk_false;
  86. if (nk_menu_item_label(ctx, "About", NK_TEXT_LEFT))
  87. show_app_about = nk_true;
  88. nk_progress(ctx, &prog, 100, NK_MODIFIABLE);
  89. nk_slider_int(ctx, 0, &slider, 16, 1);
  90. nk_checkbox_label(ctx, "check", &check);
  91. nk_menu_end(ctx);
  92. }
  93. nk_layout_row_push(ctx, 70);
  94. nk_progress(ctx, &mprog, 100, NK_MODIFIABLE);
  95. nk_slider_int(ctx, 0, &mslider, 16, 1);
  96. nk_checkbox_label(ctx, "check", &mcheck);
  97. nk_menubar_end(ctx);
  98. }
  99. if (show_app_about)
  100. {
  101. /* about popup */
  102. struct nk_panel popup;
  103. static struct nk_rect s = {20, 100, 300, 190};
  104. if (nk_popup_begin(ctx, &popup, NK_POPUP_STATIC, "About", NK_WINDOW_CLOSABLE, s))
  105. {
  106. nk_layout_row_dynamic(ctx, 20, 1);
  107. nk_label(ctx, "Nuklear", NK_TEXT_LEFT);
  108. nk_label(ctx, "By Micha Mettke", NK_TEXT_LEFT);
  109. nk_label(ctx, "nuklear is licensed under the public domain License.", NK_TEXT_LEFT);
  110. nk_popup_end(ctx);
  111. } else show_app_about = nk_false;
  112. }
  113. /* window flags */
  114. if (nk_tree_push(ctx, NK_TREE_TAB, "Window", NK_MINIMIZED)) {
  115. nk_layout_row_dynamic(ctx, 30, 2);
  116. nk_checkbox_label(ctx, "Titlebar", &titlebar);
  117. nk_checkbox_label(ctx, "Menu", &show_menu);
  118. nk_checkbox_label(ctx, "Border", &border);
  119. nk_checkbox_label(ctx, "Resizable", &resize);
  120. nk_checkbox_label(ctx, "Moveable", &moveable);
  121. nk_checkbox_label(ctx, "No Scrollbar", &no_scrollbar);
  122. nk_checkbox_label(ctx, "Minimizable", &minimizable);
  123. nk_checkbox_label(ctx, "Closeable", &close);
  124. nk_tree_pop(ctx);
  125. }
  126. if (nk_tree_push(ctx, NK_TREE_TAB, "Widgets", NK_MINIMIZED))
  127. {
  128. enum options {A,B,C};
  129. static int checkbox;
  130. static int option;
  131. if (nk_tree_push(ctx, NK_TREE_NODE, "Text", NK_MINIMIZED))
  132. {
  133. /* Text Widgets */
  134. nk_layout_row_dynamic(ctx, 20, 1);
  135. nk_label(ctx, "Label aligned left", NK_TEXT_LEFT);
  136. nk_label(ctx, "Label aligned centered", NK_TEXT_CENTERED);
  137. nk_label(ctx, "Label aligned right", NK_TEXT_RIGHT);
  138. nk_label_colored(ctx, "Blue text", NK_TEXT_LEFT, nk_rgb(0,0,255));
  139. nk_label_colored(ctx, "Yellow text", NK_TEXT_LEFT, nk_rgb(255,255,0));
  140. nk_text(ctx, "Text without /0", 15, NK_TEXT_RIGHT);
  141. nk_layout_row_static(ctx, 100, 200, 1);
  142. nk_label_wrap(ctx, "This is a very long line to hopefully get this text to be wrapped into multiple lines to show line wrapping");
  143. nk_layout_row_dynamic(ctx, 100, 1);
  144. nk_label_wrap(ctx, "This is another long text to show dynamic window changes on multiline text");
  145. nk_tree_pop(ctx);
  146. }
  147. if (nk_tree_push(ctx, NK_TREE_NODE, "Button", NK_MINIMIZED))
  148. {
  149. /* Buttons Widgets */
  150. nk_layout_row_static(ctx, 30, 100, 3);
  151. if (nk_button_label(ctx, "Button", NK_BUTTON_DEFAULT))
  152. fprintf(stdout, "Button pressed!\n");
  153. if (nk_button_label(ctx, "Repeater", NK_BUTTON_REPEATER))
  154. fprintf(stdout, "Repeater is being pressed!\n");
  155. nk_button_color(ctx, nk_rgb(0,0,255), NK_BUTTON_DEFAULT);
  156. nk_layout_row_static(ctx, 20, 20, 8);
  157. nk_button_symbol(ctx, NK_SYMBOL_CIRCLE, NK_BUTTON_DEFAULT);
  158. nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_FILLED, NK_BUTTON_DEFAULT);
  159. nk_button_symbol(ctx, NK_SYMBOL_RECT, NK_BUTTON_DEFAULT);
  160. nk_button_symbol(ctx, NK_SYMBOL_RECT_FILLED, NK_BUTTON_DEFAULT);
  161. nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_UP, NK_BUTTON_DEFAULT);
  162. nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_DOWN, NK_BUTTON_DEFAULT);
  163. nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT, NK_BUTTON_DEFAULT);
  164. nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT, NK_BUTTON_DEFAULT);
  165. nk_layout_row_static(ctx, 30, 100, 2);
  166. nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_LEFT, "prev", NK_TEXT_RIGHT, NK_BUTTON_DEFAULT);
  167. nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_RIGHT, "next", NK_TEXT_LEFT, NK_BUTTON_DEFAULT);
  168. nk_tree_pop(ctx);
  169. }
  170. if (nk_tree_push(ctx, NK_TREE_NODE, "Basic", NK_MINIMIZED))
  171. {
  172. /* Basic widgets */
  173. static int int_slider = 5;
  174. static float float_slider = 2.5f;
  175. static size_t prog_value = 40;
  176. static float property_float = 2;
  177. static int property_int = 10;
  178. static int property_neg = 10;
  179. static float range_float_min = 0;
  180. static float range_float_max = 100;
  181. static float range_float_value = 50;
  182. static int range_int_min = 0;
  183. static int range_int_value = 2048;
  184. static int range_int_max = 4096;
  185. static const float ratio[] = {120, 150};
  186. nk_layout_row_static(ctx, 30, 100, 1);
  187. nk_checkbox_label(ctx, "Checkbox", &checkbox);
  188. nk_layout_row_static(ctx, 30, 80, 3);
  189. option = nk_option_label(ctx, "optionA", option == A) ? A : option;
  190. option = nk_option_label(ctx, "optionB", option == B) ? B : option;
  191. option = nk_option_label(ctx, "optionC", option == C) ? C : option;
  192. nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  193. nk_labelf(ctx, NK_TEXT_LEFT, "Slider int");
  194. nk_slider_int(ctx, 0, &int_slider, 10, 1);
  195. nk_label(ctx, "Slider float", NK_TEXT_LEFT);
  196. nk_slider_float(ctx, 0, &float_slider, 5.0, 0.5f);
  197. nk_labelf(ctx, NK_TEXT_LEFT, "Progressbar" , prog_value);
  198. nk_progress(ctx, &prog_value, 100, NK_MODIFIABLE);
  199. nk_layout_row(ctx, NK_STATIC, 25, 2, ratio);
  200. nk_label(ctx, "Property float:", NK_TEXT_LEFT);
  201. nk_property_float(ctx, "Float:", 0, &property_float, 64.0f, 0.1f, 0.2f);
  202. nk_label(ctx, "Property int:", NK_TEXT_LEFT);
  203. nk_property_int(ctx, "Int:", 0, &property_int, 100.0f, 1, 1);
  204. nk_label(ctx, "Property neg:", NK_TEXT_LEFT);
  205. nk_property_int(ctx, "Neg:", -10, &property_neg, 10, 1, 1);
  206. nk_layout_row_dynamic(ctx, 25, 1);
  207. nk_label(ctx, "Range:", NK_TEXT_LEFT);
  208. nk_layout_row_dynamic(ctx, 25, 3);
  209. nk_property_float(ctx, "#min:", 0, &range_float_min, range_float_max, 1.0f, 0.2f);
  210. nk_property_float(ctx, "#float:", range_float_min, &range_float_value, range_float_max, 1.0f, 0.2f);
  211. nk_property_float(ctx, "#max:", range_float_min, &range_float_max, 100, 1.0f, 0.2f);
  212. nk_property_int(ctx, "#min:", INT_MIN, &range_int_min, range_int_max, 1, 10);
  213. nk_property_int(ctx, "#neg:", range_int_min, &range_int_value, range_int_max, 1, 10);
  214. nk_property_int(ctx, "#max:", range_int_min, &range_int_max, INT_MAX, 1, 10);
  215. nk_tree_pop(ctx);
  216. }
  217. if (nk_tree_push(ctx, NK_TREE_NODE, "Selectable", NK_MINIMIZED))
  218. {
  219. if (nk_tree_push(ctx, NK_TREE_NODE, "List", NK_MINIMIZED))
  220. {
  221. static int selected[4] = {nk_false, nk_false, nk_true, nk_false};
  222. nk_layout_row_static(ctx, 18, 100, 1);
  223. nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[0]);
  224. nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[1]);
  225. nk_label(ctx, "Not Selectable", NK_TEXT_LEFT);
  226. nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[2]);
  227. nk_selectable_label(ctx, "Selectable", NK_TEXT_LEFT, &selected[3]);
  228. nk_tree_pop(ctx);
  229. }
  230. if (nk_tree_push(ctx, NK_TREE_NODE, "Grid", NK_MINIMIZED))
  231. {
  232. int i;
  233. static int selected[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
  234. nk_layout_row_static(ctx, 50, 50, 4);
  235. for (i = 0; i < 16; ++i) {
  236. if (nk_selectable_label(ctx, "Z", NK_TEXT_CENTERED, &selected[i])) {
  237. int x = (i % 4), y = i / 4;
  238. if (x > 0) selected[i - 1] ^= 1;
  239. if (x < 3) selected[i + 1] ^= 1;
  240. if (y > 0) selected[i - 4] ^= 1;
  241. if (y < 3) selected[i + 4] ^= 1;
  242. }
  243. }
  244. nk_tree_pop(ctx);
  245. }
  246. nk_tree_pop(ctx);
  247. }
  248. if (nk_tree_push(ctx, NK_TREE_NODE, "Combo", NK_MINIMIZED))
  249. {
  250. /* Combobox Widgets
  251. * In this library comboboxes are not limited to being a popup
  252. * list of selectable text. Instead it is a abstract concept of
  253. * having something that is *selected* or displayed, a popup window
  254. * which opens if something needs to be modified and the content
  255. * of the popup which causes the *selected* or displayed value to
  256. * change or if wanted close the combobox.
  257. *
  258. * While strange at first handling comboboxes in a abstract way
  259. * solves the problem of overloaded window content. For example
  260. * changing a color value requires 4 value modifier (slider, property,...)
  261. * for RGBA then you need a label and ways to display the current color.
  262. * If you want to go fancy you even add rgb and hsv ratio boxes.
  263. * While fine for one color if you have a lot of them it because
  264. * tedious to look at and quite wasteful in space. You could add
  265. * a popup which modifies the color but this does not solve the
  266. * fact that it still requires a lot of cluttered space to do.
  267. *
  268. * In these kind of instance abstract comboboxes are quite handy. All
  269. * value modifiers are hidden inside the combobox popup and only
  270. * the color is shown if not open. This combines the clarity of the
  271. * popup with the ease of use of just using the space for modifiers.
  272. *
  273. * Other instances are for example time and especially date picker,
  274. * which only show the currently activated time/data and hide the
  275. * selection logic inside the combobox popup.
  276. */
  277. static float chart_selection = 8.0f;
  278. static int current_weapon = 0;
  279. static int check_values[5];
  280. static float position[3];
  281. static struct nk_color combo_color = {130, 50, 50, 255};
  282. static struct nk_color combo_color2 = {130, 180, 50, 255};
  283. static size_t prog_a = 20, prog_b = 40, prog_c = 10, prog_d = 90;
  284. static const char *weapons[] = {"Fist","Pistol","Shotgun","Plasma","BFG"};
  285. char buffer[64];
  286. size_t sum = 0;
  287. struct nk_panel combo;
  288. /* default combobox */
  289. nk_layout_row_static(ctx, 25, 200, 1);
  290. current_weapon = nk_combo(ctx, weapons, LEN(weapons), current_weapon, 25);
  291. /* slider color combobox */
  292. if (nk_combo_begin_color(ctx, &combo, combo_color, 200)) {
  293. float ratios[] = {0.15f, 0.85f};
  294. nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratios);
  295. nk_label(ctx, "R:", NK_TEXT_LEFT);
  296. combo_color.r = (nk_byte)nk_slide_int(ctx, 0, combo_color.r, 255, 5);
  297. nk_label(ctx, "G:", NK_TEXT_LEFT);
  298. combo_color.g = (nk_byte)nk_slide_int(ctx, 0, combo_color.g, 255, 5);
  299. nk_label(ctx, "B:", NK_TEXT_LEFT);
  300. combo_color.b = (nk_byte)nk_slide_int(ctx, 0, combo_color.b, 255, 5);
  301. nk_label(ctx, "A:", NK_TEXT_LEFT);
  302. combo_color.a = (nk_byte)nk_slide_int(ctx, 0, combo_color.a , 255, 5);
  303. nk_combo_end(ctx);
  304. }
  305. /* complex color combobox */
  306. if (nk_combo_begin_color(ctx, &combo, combo_color2, 400)) {
  307. enum color_mode {COL_RGB, COL_HSV};
  308. static int col_mode = COL_RGB;
  309. #ifndef DEMO_DO_NOT_USE_COLOR_PICKER
  310. nk_layout_row_dynamic(ctx, 120, 1);
  311. combo_color2 = nk_color_picker(ctx, combo_color2, NK_RGBA);
  312. #endif
  313. nk_layout_row_dynamic(ctx, 25, 2);
  314. col_mode = nk_option_label(ctx, "RGB", col_mode == COL_RGB) ? COL_RGB : col_mode;
  315. col_mode = nk_option_label(ctx, "HSV", col_mode == COL_HSV) ? COL_HSV : col_mode;
  316. nk_layout_row_dynamic(ctx, 25, 1);
  317. if (col_mode == COL_RGB) {
  318. combo_color2.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, combo_color2.r, 255, 1,1);
  319. combo_color2.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, combo_color2.g, 255, 1,1);
  320. combo_color2.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, combo_color2.b, 255, 1,1);
  321. combo_color2.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, combo_color2.a, 255, 1,1);
  322. } else {
  323. nk_byte tmp[4];
  324. nk_color_hsva_bv(tmp, combo_color2);
  325. tmp[0] = (nk_byte)nk_propertyi(ctx, "#H:", 0, tmp[0], 255, 1,1);
  326. tmp[1] = (nk_byte)nk_propertyi(ctx, "#S:", 0, tmp[1], 255, 1,1);
  327. tmp[2] = (nk_byte)nk_propertyi(ctx, "#V:", 0, tmp[2], 255, 1,1);
  328. tmp[3] = (nk_byte)nk_propertyi(ctx, "#A:", 0, tmp[3], 255, 1,1);
  329. combo_color2 = nk_hsva_bv(tmp);
  330. }
  331. nk_combo_end(ctx);
  332. }
  333. /* progressbar combobox */
  334. sum = prog_a + prog_b + prog_c + prog_d;
  335. sprintf(buffer, "%lu", sum);
  336. if (nk_combo_begin_label(ctx, &combo, buffer, 200)) {
  337. nk_layout_row_dynamic(ctx, 30, 1);
  338. nk_progress(ctx, &prog_a, 100, NK_MODIFIABLE);
  339. nk_progress(ctx, &prog_b, 100, NK_MODIFIABLE);
  340. nk_progress(ctx, &prog_c, 100, NK_MODIFIABLE);
  341. nk_progress(ctx, &prog_d, 100, NK_MODIFIABLE);
  342. nk_combo_end(ctx);
  343. }
  344. /* checkbox combobox */
  345. sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]);
  346. sprintf(buffer, "%lu", sum);
  347. if (nk_combo_begin_label(ctx, &combo, buffer, 200)) {
  348. nk_layout_row_dynamic(ctx, 30, 1);
  349. nk_checkbox_label(ctx, weapons[0], &check_values[0]);
  350. nk_checkbox_label(ctx, weapons[1], &check_values[1]);
  351. nk_checkbox_label(ctx, weapons[2], &check_values[2]);
  352. nk_checkbox_label(ctx, weapons[3], &check_values[3]);
  353. nk_combo_end(ctx);
  354. }
  355. /* complex text combobox */
  356. sprintf(buffer, "%.2f, %.2f, %.2f", position[0], position[1],position[2]);
  357. if (nk_combo_begin_label(ctx, &combo, buffer, 200)) {
  358. nk_layout_row_dynamic(ctx, 25, 1);
  359. nk_property_float(ctx, "#X:", -1024.0f, &position[0], 1024.0f, 1,0.5f);
  360. nk_property_float(ctx, "#Y:", -1024.0f, &position[1], 1024.0f, 1,0.5f);
  361. nk_property_float(ctx, "#Z:", -1024.0f, &position[2], 1024.0f, 1,0.5f);
  362. nk_combo_end(ctx);
  363. }
  364. /* chart combobox */
  365. sprintf(buffer, "%.1f", chart_selection);
  366. if (nk_combo_begin_label(ctx, &combo, buffer, 250)) {
  367. size_t i = 0;
  368. static const float values[]={26.0f,13.0f,30.0f,15.0f,25.0f,10.0f,20.0f,40.0f, 12.0f, 8.0f, 22.0f, 28.0f, 5.0f};
  369. nk_layout_row_dynamic(ctx, 150, 1);
  370. nk_chart_begin(ctx, NK_CHART_COLUMN, LEN(values), 0, 50);
  371. for (i = 0; i < LEN(values); ++i) {
  372. nk_flags res = nk_chart_push(ctx, values[i]);
  373. if (res & NK_CHART_CLICKED) {
  374. chart_selection = values[i];
  375. nk_combo_close(ctx);
  376. }
  377. }
  378. nk_chart_end(ctx);
  379. nk_combo_end(ctx);
  380. }
  381. {
  382. static int time_selected = 0;
  383. static int date_selected = 0;
  384. static struct tm sel_time;
  385. static struct tm sel_date;
  386. if (!time_selected || !date_selected) {
  387. /* keep time and date updated if nothing is selected */
  388. time_t cur_time = time(0);
  389. struct tm *n = localtime(&cur_time);
  390. if (!time_selected)
  391. memcpy(&sel_time, n, sizeof(struct tm));
  392. if (!date_selected)
  393. memcpy(&sel_date, n, sizeof(struct tm));
  394. }
  395. /* time combobox */
  396. sprintf(buffer, "%02d:%02d:%02d", sel_time.tm_hour, sel_time.tm_min, sel_time.tm_sec);
  397. if (nk_combo_begin_label(ctx, &combo, buffer, 250)) {
  398. time_selected = 1;
  399. nk_layout_row_dynamic(ctx, 25, 1);
  400. sel_time.tm_sec = nk_propertyi(ctx, "#S:", 0, sel_time.tm_sec, 60, 1, 1);
  401. sel_time.tm_min = nk_propertyi(ctx, "#M:", 0, sel_time.tm_min, 60, 1, 1);
  402. sel_time.tm_hour = nk_propertyi(ctx, "#H:", 0, sel_time.tm_hour, 23, 1, 1);
  403. nk_combo_end(ctx);
  404. }
  405. /* date combobox */
  406. nk_layout_row_static(ctx, 25, 350, 1);
  407. sprintf(buffer, "%02d-%02d-%02d", sel_date.tm_mday, sel_date.tm_mon+1, sel_date.tm_year+1900);
  408. if (nk_combo_begin_label(ctx, &combo, buffer, 400))
  409. {
  410. int i = 0;
  411. const char *month[] = {"January", "February", "March", "Apil", "May", "June", "July", "August", "September", "Ocotober", "November", "December"};
  412. const char *week_days[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
  413. const int month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  414. int year = sel_date.tm_year+1900;
  415. int leap_year = (!(year % 4) && ((year % 100))) || !(year % 400);
  416. int days = (sel_date.tm_mon == 1) ?
  417. month_days[sel_date.tm_mon] + leap_year:
  418. month_days[sel_date.tm_mon];
  419. /* header with month and year */
  420. date_selected = 1;
  421. nk_layout_row_begin(ctx, NK_DYNAMIC, 20, 3);
  422. nk_layout_row_push(ctx, 0.05f);
  423. if (nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT, NK_BUTTON_DEFAULT)) {
  424. if (sel_date.tm_mon == 0) {
  425. sel_date.tm_mon = 11;
  426. sel_date.tm_year = MAX(0, sel_date.tm_year-1);
  427. } else sel_date.tm_mon--;
  428. }
  429. nk_layout_row_push(ctx, 0.9f);
  430. sprintf(buffer, "%s %d", month[sel_date.tm_mon], year);
  431. nk_label(ctx, buffer, NK_TEXT_CENTERED);
  432. nk_layout_row_push(ctx, 0.05f);
  433. if (nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT, NK_BUTTON_DEFAULT)) {
  434. if (sel_date.tm_mon == 11) {
  435. sel_date.tm_mon = 0;
  436. sel_date.tm_year++;
  437. } else sel_date.tm_mon++;
  438. }
  439. nk_layout_row_end(ctx);
  440. /* good old week day formula (double because precision) */
  441. {int year_n = (sel_date.tm_mon < 2) ? year-1: year;
  442. int y = year_n % 100;
  443. int c = year_n / 100;
  444. int y4 = (int)((float)y / 4);
  445. int c4 = (int)((float)c / 4);
  446. int m = (int)(2.6 * (double)(((sel_date.tm_mon + 10) % 12) + 1) - 0.2);
  447. int week_day = (((1 + m + y + y4 + c4 - 2 * c) % 7) + 7) % 7;
  448. /* weekdays */
  449. nk_layout_row_dynamic(ctx, 35, 7);
  450. for (i = 0; i < (int)LEN(week_days); ++i)
  451. nk_label(ctx, week_days[i], NK_TEXT_CENTERED);
  452. /* days */
  453. if (week_day > 0) nk_spacing(ctx, week_day);
  454. for (i = 1; i <= days; ++i) {
  455. sprintf(buffer, "%d", i);
  456. if (nk_button_label(ctx, buffer, NK_BUTTON_DEFAULT)) {
  457. sel_date.tm_mday = i;
  458. nk_combo_close(ctx);
  459. }
  460. }}
  461. nk_combo_end(ctx);
  462. }
  463. }
  464. nk_tree_pop(ctx);
  465. }
  466. if (nk_tree_push(ctx, NK_TREE_NODE, "Input", NK_MINIMIZED))
  467. {
  468. static const float ratio[] = {120, 150};
  469. static char field_buffer[64];
  470. static char text[9][64];
  471. static int text_len[9];
  472. static char box_buffer[512];
  473. static int field_len;
  474. static int box_len;
  475. nk_flags active;
  476. nk_layout_row(ctx, NK_STATIC, 25, 2, ratio);
  477. nk_label(ctx, "Default:", NK_TEXT_LEFT);
  478. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[0], &text_len[0], 64, nk_filter_default);
  479. nk_label(ctx, "Int:", NK_TEXT_LEFT);
  480. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[1], &text_len[1], 64, nk_filter_decimal);
  481. nk_label(ctx, "Float:", NK_TEXT_LEFT);
  482. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[2], &text_len[2], 64, nk_filter_float);
  483. nk_label(ctx, "Hex:", NK_TEXT_LEFT);
  484. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[4], &text_len[4], 64, nk_filter_hex);
  485. nk_label(ctx, "Octal:", NK_TEXT_LEFT);
  486. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[5], &text_len[5], 64, nk_filter_oct);
  487. nk_label(ctx, "Binary:", NK_TEXT_LEFT);
  488. nk_edit_string(ctx, NK_EDIT_SIMPLE, text[6], &text_len[6], 64, nk_filter_binary);
  489. nk_label(ctx, "Password:", NK_TEXT_LEFT);
  490. {
  491. int i = 0;
  492. int old_len = text_len[8];
  493. char buffer[64];
  494. for (i = 0; i < text_len[8]; ++i) buffer[i] = '*';
  495. nk_edit_string(ctx, NK_EDIT_FIELD, buffer, &text_len[8], 64, nk_filter_default);
  496. if (old_len < text_len[8])
  497. memcpy(&text[8][old_len], &buffer[old_len], (nk_size)(text_len[8] - old_len));
  498. }
  499. nk_label(ctx, "Field:", NK_TEXT_LEFT);
  500. nk_edit_string(ctx, NK_EDIT_FIELD, field_buffer, &field_len, 64, nk_filter_default);
  501. nk_label(ctx, "Box:", NK_TEXT_LEFT);
  502. nk_layout_row_static(ctx, 180, 278, 1);
  503. nk_edit_string(ctx, NK_EDIT_BOX, box_buffer, &box_len, 512, nk_filter_default);
  504. nk_layout_row(ctx, NK_STATIC, 25, 2, ratio);
  505. active = nk_edit_string(ctx, NK_EDIT_FIELD|NK_EDIT_SIG_ENTER, text[7], &text_len[7], 64, nk_filter_ascii);
  506. if (nk_button_label(ctx, "Submit", NK_BUTTON_DEFAULT) ||
  507. (active & NK_EDIT_COMMITED))
  508. {
  509. text[7][text_len[7]] = '\n';
  510. text_len[7]++;
  511. memcpy(&box_buffer[box_len], &text[7], (nk_size)text_len[7]);
  512. box_len += text_len[7];
  513. text_len[7] = 0;
  514. }
  515. nk_layout_row_end(ctx);
  516. nk_tree_pop(ctx);
  517. }
  518. nk_tree_pop(ctx);
  519. }
  520. if (nk_tree_push(ctx, NK_TREE_TAB, "Chart", NK_MINIMIZED))
  521. {
  522. /* Chart Widgets
  523. * This library has two different rather simple charts. The line and the
  524. * column chart. Both provide a simple way of visualizing values and
  525. * have a retain mode and immedidate mode API version. For the retain
  526. * mode version `nk_plot` and `nk_plot_function` you either provide
  527. * an array or a callback to call to handle drawing the graph.
  528. * For the immediate mode version you start by calling `nk_chart_begin`
  529. * and need to provide min and max values for scaling on the Y-axis.
  530. * and then call `nk_chart_push` to push values into the chart.
  531. * Finally `nk_chart_end` needs to be called to end the process. */
  532. float id = 0;
  533. static int col_index = -1;
  534. static int line_index = -1;
  535. float step = (2*3.141592654f) / 32;
  536. int i;
  537. int index = -1;
  538. struct nk_rect bounds;
  539. /* line chart */
  540. id = 0;
  541. index = -1;
  542. nk_layout_row_dynamic(ctx, 100, 1);
  543. bounds = nk_widget_bounds(ctx);
  544. if (nk_chart_begin(ctx, NK_CHART_LINES, 32, -1.0f, 1.0f)) {
  545. for (i = 0; i < 32; ++i) {
  546. nk_flags res = nk_chart_push(ctx, (float)cos(id));
  547. if (res & NK_CHART_HOVERING)
  548. index = (int)i;
  549. if (res & NK_CHART_CLICKED)
  550. line_index = (int)i;
  551. id += step;
  552. }
  553. nk_chart_end(ctx);
  554. }
  555. if (index != -1) {
  556. char buffer[NK_MAX_NUMBER_BUFFER];
  557. float val = (float)cos((float)index*step);
  558. sprintf(buffer, "Value: %.2f", val);
  559. nk_tooltip(ctx, buffer);
  560. }
  561. if (line_index != -1) {
  562. nk_layout_row_dynamic(ctx, 20, 1);
  563. nk_labelf(ctx, NK_TEXT_LEFT, "Selected value: %.2f", (float)cos((float)index*step));
  564. }
  565. /* column chart */
  566. nk_layout_row_dynamic(ctx, 100, 1);
  567. bounds = nk_widget_bounds(ctx);
  568. if (nk_chart_begin(ctx, NK_CHART_COLUMN, 32, 0.0f, 1.0f)) {
  569. for (i = 0; i < 32; ++i) {
  570. nk_flags res = nk_chart_push(ctx, (float)fabs(sin(id)));
  571. if (res & NK_CHART_HOVERING)
  572. index = (int)i;
  573. if (res & NK_CHART_CLICKED)
  574. col_index = (int)i;
  575. id += step;
  576. }
  577. nk_chart_end(ctx);
  578. }
  579. if (index != -1) {
  580. char buffer[NK_MAX_NUMBER_BUFFER];
  581. sprintf(buffer, "Value: %.2f", (float)fabs(sin(step * (float)index)));
  582. nk_tooltip(ctx, buffer);
  583. }
  584. if (col_index != -1) {
  585. nk_layout_row_dynamic(ctx, 20, 1);
  586. nk_labelf(ctx, NK_TEXT_LEFT, "Selected value: %.2f", (float)fabs(sin(step * (float)col_index)));
  587. }
  588. nk_tree_pop(ctx);
  589. }
  590. if (nk_tree_push(ctx, NK_TREE_TAB, "Popup", NK_MINIMIZED))
  591. {
  592. static struct nk_color color = {255,0,0, 255};
  593. static int select[4];
  594. static int popup_active;
  595. const struct nk_input *in = &ctx->input;
  596. struct nk_rect bounds;
  597. /* menu contextual */
  598. nk_layout_row_static(ctx, 30, 150, 1);
  599. bounds = nk_widget_bounds(ctx);
  600. nk_label(ctx, "Right click me for menu", NK_TEXT_LEFT);
  601. if (nk_contextual_begin(ctx, &menu, 0, nk_vec2(100, 300), bounds)) {
  602. static size_t prog = 40;
  603. static int slider = 10;
  604. nk_layout_row_dynamic(ctx, 25, 1);
  605. nk_checkbox_label(ctx, "Menu", &show_menu);
  606. nk_progress(ctx, &prog, 100, NK_MODIFIABLE);
  607. nk_slider_int(ctx, 0, &slider, 16, 1);
  608. if (nk_contextual_item_label(ctx, "About", NK_TEXT_CENTERED))
  609. show_app_about = nk_true;
  610. nk_selectable_label(ctx, select[0]?"Unselect":"Select", NK_TEXT_LEFT, &select[0]);
  611. nk_selectable_label(ctx, select[1]?"Unselect":"Select", NK_TEXT_LEFT, &select[1]);
  612. nk_selectable_label(ctx, select[2]?"Unselect":"Select", NK_TEXT_LEFT, &select[2]);
  613. nk_selectable_label(ctx, select[3]?"Unselect":"Select", NK_TEXT_LEFT, &select[3]);
  614. nk_contextual_end(ctx);
  615. }
  616. /* color contextual */
  617. nk_layout_row_begin(ctx, NK_STATIC, 30, 2);
  618. nk_layout_row_push(ctx, 100);
  619. nk_label(ctx, "Right Click here:", NK_TEXT_LEFT);
  620. nk_layout_row_push(ctx, 50);
  621. bounds = nk_widget_bounds(ctx);
  622. nk_button_color(ctx, color, NK_BUTTON_DEFAULT);
  623. nk_layout_row_end(ctx);
  624. if (nk_contextual_begin(ctx, &menu, 0, nk_vec2(350, 60), bounds)) {
  625. nk_layout_row_dynamic(ctx, 30, 4);
  626. color.r = (nk_byte)nk_propertyi(ctx, "#r", 0, color.r, 255, 1, 1);
  627. color.g = (nk_byte)nk_propertyi(ctx, "#g", 0, color.g, 255, 1, 1);
  628. color.b = (nk_byte)nk_propertyi(ctx, "#b", 0, color.b, 255, 1, 1);
  629. color.a = (nk_byte)nk_propertyi(ctx, "#a", 0, color.a, 255, 1, 1);
  630. nk_contextual_end(ctx);
  631. }
  632. /* popup */
  633. nk_layout_row_begin(ctx, NK_STATIC, 30, 2);
  634. nk_layout_row_push(ctx, 100);
  635. nk_label(ctx, "Popup:", NK_TEXT_LEFT);
  636. nk_layout_row_push(ctx, 50);
  637. if (nk_button_label(ctx, "Popup", NK_BUTTON_DEFAULT))
  638. popup_active = 1;
  639. nk_layout_row_end(ctx);
  640. if (popup_active)
  641. {
  642. static struct nk_rect s = {20, 100, 220, 150};
  643. if (nk_popup_begin(ctx, &menu, NK_POPUP_STATIC, "Error", NK_WINDOW_DYNAMIC, s))
  644. {
  645. nk_layout_row_dynamic(ctx, 25, 1);
  646. nk_label(ctx, "A terrible error as occured", NK_TEXT_LEFT);
  647. nk_layout_row_dynamic(ctx, 25, 2);
  648. if (nk_button_label(ctx, "OK", NK_BUTTON_DEFAULT)) {
  649. popup_active = 0;
  650. nk_popup_close(ctx);
  651. }
  652. if (nk_button_label(ctx, "Cancel", NK_BUTTON_DEFAULT)) {
  653. popup_active = 0;
  654. nk_popup_close(ctx);
  655. }
  656. nk_popup_end(ctx);
  657. } else popup_active = nk_false;
  658. }
  659. /* tooltip */
  660. nk_layout_row_static(ctx, 30, 150, 1);
  661. bounds = nk_widget_bounds(ctx);
  662. nk_label(ctx, "Hover me for tooltip", NK_TEXT_LEFT);
  663. if (nk_input_is_mouse_hovering_rect(in, bounds))
  664. nk_tooltip(ctx, "This is a tooltip");
  665. nk_tree_pop(ctx);
  666. }
  667. if (nk_tree_push(ctx, NK_TREE_TAB, "Layout", NK_MINIMIZED))
  668. {
  669. if (nk_tree_push(ctx, NK_TREE_NODE, "Widget", NK_MINIMIZED))
  670. {
  671. float ratio_two[] = {0.2f, 0.6f, 0.2f};
  672. float width_two[] = {100, 200, 50};
  673. nk_layout_row_dynamic(ctx, 30, 1);
  674. nk_label(ctx, "Dynamic fixed column layout with generated position and size:", NK_TEXT_LEFT);
  675. nk_layout_row_dynamic(ctx, 30, 3);
  676. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  677. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  678. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  679. nk_layout_row_dynamic(ctx, 30, 1);
  680. nk_label(ctx, "static fixed column layout with generated position and size:", NK_TEXT_LEFT);
  681. nk_layout_row_static(ctx, 30, 100, 3);
  682. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  683. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  684. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  685. nk_layout_row_dynamic(ctx, 30, 1);
  686. nk_label(ctx, "Dynamic array-based custom column layout with generated position and custom size:",NK_TEXT_LEFT);
  687. nk_layout_row(ctx, NK_DYNAMIC, 30, 3, ratio_two);
  688. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  689. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  690. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  691. nk_layout_row_dynamic(ctx, 30, 1);
  692. nk_label(ctx, "Static array-based custom column layout with generated position and custom size:",NK_TEXT_LEFT );
  693. nk_layout_row(ctx, NK_STATIC, 30, 3, width_two);
  694. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  695. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  696. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  697. nk_layout_row_dynamic(ctx, 30, 1);
  698. nk_label(ctx, "Dynamic immediate mode custom column layout with generated position and custom size:",NK_TEXT_LEFT);
  699. nk_layout_row_begin(ctx, NK_DYNAMIC, 30, 3);
  700. nk_layout_row_push(ctx, 0.2f);
  701. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  702. nk_layout_row_push(ctx, 0.6f);
  703. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  704. nk_layout_row_push(ctx, 0.2f);
  705. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  706. nk_layout_row_end(ctx);
  707. nk_layout_row_dynamic(ctx, 30, 1);
  708. nk_label(ctx, "Static immmediate mode custom column layout with generated position and custom size:", NK_TEXT_LEFT);
  709. nk_layout_row_begin(ctx, NK_STATIC, 30, 3);
  710. nk_layout_row_push(ctx, 100);
  711. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  712. nk_layout_row_push(ctx, 200);
  713. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  714. nk_layout_row_push(ctx, 50);
  715. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  716. nk_layout_row_end(ctx);
  717. nk_layout_row_dynamic(ctx, 30, 1);
  718. nk_label(ctx, "Static free space with custom position and custom size:", NK_TEXT_LEFT);
  719. nk_layout_space_begin(ctx, NK_STATIC, 120, 4);
  720. nk_layout_space_push(ctx, nk_rect(100, 0, 100, 30));
  721. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  722. nk_layout_space_push(ctx, nk_rect(0, 15, 100, 30));
  723. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  724. nk_layout_space_push(ctx, nk_rect(200, 15, 100, 30));
  725. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  726. nk_layout_space_push(ctx, nk_rect(100, 30, 100, 30));
  727. nk_button_label(ctx, "button", NK_BUTTON_DEFAULT);
  728. nk_layout_space_end(ctx);
  729. nk_tree_pop(ctx);
  730. }
  731. if (nk_tree_push(ctx, NK_TREE_NODE, "Group", NK_MINIMIZED))
  732. {
  733. static int group_titlebar = nk_false;
  734. static int group_border = nk_true;
  735. static int group_no_scrollbar = nk_false;
  736. static int group_width = 320;
  737. static int group_height = 200;
  738. struct nk_panel tab;
  739. nk_flags group_flags = 0;
  740. if (group_border) group_flags |= NK_WINDOW_BORDER;
  741. if (group_no_scrollbar) group_flags |= NK_WINDOW_NO_SCROLLBAR;
  742. if (group_titlebar) group_flags |= NK_WINDOW_TITLE;
  743. nk_layout_row_dynamic(ctx, 30, 3);
  744. nk_checkbox_label(ctx, "Titlebar", &group_titlebar);
  745. nk_checkbox_label(ctx, "Border", &group_border);
  746. nk_checkbox_label(ctx, "No Scrollbar", &group_no_scrollbar);
  747. nk_layout_row_begin(ctx, NK_STATIC, 22, 2);
  748. nk_layout_row_push(ctx, 50);
  749. nk_label(ctx, "size:", NK_TEXT_LEFT);
  750. nk_layout_row_push(ctx, 130);
  751. nk_property_int(ctx, "#Width:", 100, &group_width, 500, 10, 1);
  752. nk_layout_row_push(ctx, 130);
  753. nk_property_int(ctx, "#Height:", 100, &group_height, 500, 10, 1);
  754. nk_layout_row_end(ctx);
  755. nk_layout_row_static(ctx, (float)group_height, group_width, 2);
  756. if (nk_group_begin(ctx, &tab, "Group", group_flags)) {
  757. int i = 0;
  758. static int selected[16];
  759. nk_layout_row_static(ctx, 18, 100, 1);
  760. for (i = 0; i < 16; ++i)
  761. nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
  762. nk_group_end(ctx);
  763. }
  764. nk_tree_pop(ctx);
  765. }
  766. if (nk_tree_push(ctx, NK_TREE_NODE, "Simple", NK_MINIMIZED))
  767. {
  768. struct nk_panel tab;
  769. nk_layout_row_dynamic(ctx, 300, 2);
  770. if (nk_group_begin(ctx, &tab, "Group_Without_Border", 0)) {
  771. int i = 0;
  772. char buffer[64];
  773. nk_layout_row_static(ctx, 18, 150, 1);
  774. for (i = 0; i < 64; ++i) {
  775. sprintf(buffer, "0x%02x", i);
  776. nk_labelf(ctx, NK_TEXT_LEFT, "%s: scrollable region", buffer);
  777. }
  778. nk_group_end(ctx);
  779. }
  780. if (nk_group_begin(ctx, &tab, "Group_With_Border", NK_WINDOW_BORDER)) {
  781. int i = 0;
  782. char buffer[64];
  783. nk_layout_row_dynamic(ctx, 25, 2);
  784. for (i = 0; i < 64; ++i) {
  785. sprintf(buffer, "%08d", ((((i%7)*10)^32))+(64+(i%2)*2));
  786. nk_button_label(ctx, buffer, NK_BUTTON_DEFAULT);
  787. }
  788. nk_group_end(ctx);
  789. }
  790. nk_tree_pop(ctx);
  791. }
  792. if (nk_tree_push(ctx, NK_TREE_NODE, "Complex", NK_MINIMIZED))
  793. {
  794. int i;
  795. struct nk_panel tab;
  796. nk_layout_space_begin(ctx, NK_STATIC, 500, 64);
  797. nk_layout_space_push(ctx, nk_rect(0,0,150,500));
  798. if (nk_group_begin(ctx, &tab, "Group_left", NK_WINDOW_BORDER)) {
  799. static int selected[32];
  800. nk_layout_row_static(ctx, 18, 100, 1);
  801. for (i = 0; i < 32; ++i)
  802. nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
  803. nk_group_end(ctx);
  804. }
  805. nk_layout_space_push(ctx, nk_rect(160,0,150,240));
  806. if (nk_group_begin(ctx, &tab, "Group_top", NK_WINDOW_BORDER)) {
  807. nk_layout_row_dynamic(ctx, 25, 1);
  808. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  809. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  810. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  811. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  812. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  813. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  814. nk_group_end(ctx);
  815. }
  816. nk_layout_space_push(ctx, nk_rect(160,250,150,250));
  817. if (nk_group_begin(ctx, &tab, "Group_buttom", NK_WINDOW_BORDER)) {
  818. nk_layout_row_dynamic(ctx, 25, 1);
  819. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  820. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  821. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  822. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  823. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  824. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  825. nk_group_end(ctx);
  826. }
  827. nk_layout_space_push(ctx, nk_rect(320,0,150,150));
  828. if (nk_group_begin(ctx, &tab, "Group_right_top", NK_WINDOW_BORDER)) {
  829. static int selected[4];
  830. nk_layout_row_static(ctx, 18, 100, 1);
  831. for (i = 0; i < 4; ++i)
  832. nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
  833. nk_group_end(ctx);
  834. }
  835. nk_layout_space_push(ctx, nk_rect(320,160,150,150));
  836. if (nk_group_begin(ctx, &tab, "Group_right_center", NK_WINDOW_BORDER)) {
  837. static int selected[4];
  838. nk_layout_row_static(ctx, 18, 100, 1);
  839. for (i = 0; i < 4; ++i)
  840. nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
  841. nk_group_end(ctx);
  842. }
  843. nk_layout_space_push(ctx, nk_rect(320,320,150,150));
  844. if (nk_group_begin(ctx, &tab, "Group_right_bottom", NK_WINDOW_BORDER)) {
  845. static int selected[4];
  846. nk_layout_row_static(ctx, 18, 100, 1);
  847. for (i = 0; i < 4; ++i)
  848. nk_selectable_label(ctx, (selected[i]) ? "Selected": "Unselected", NK_TEXT_CENTERED, &selected[i]);
  849. nk_group_end(ctx);
  850. }
  851. nk_layout_space_end(ctx);
  852. nk_tree_pop(ctx);
  853. }
  854. if (nk_tree_push(ctx, NK_TREE_NODE, "Splitter", NK_MINIMIZED))
  855. {
  856. const struct nk_input *in = &ctx->input;
  857. nk_layout_row_static(ctx, 20, 320, 1);
  858. nk_label(ctx, "Use slider and spinner to change tile size", NK_TEXT_LEFT);
  859. nk_label(ctx, "Drag the space between tiles to change tile ratio", NK_TEXT_LEFT);
  860. if (nk_tree_push(ctx, NK_TREE_NODE, "Vertical", NK_MINIMIZED))
  861. {
  862. static float a = 100, b = 100, c = 100;
  863. struct nk_rect bounds;
  864. struct nk_panel sub;
  865. float row_layout[5];
  866. row_layout[0] = a;
  867. row_layout[1] = 8;
  868. row_layout[2] = b;
  869. row_layout[3] = 8;
  870. row_layout[4] = c;
  871. /* header */
  872. nk_layout_row_static(ctx, 30, 100, 2);
  873. nk_label(ctx, "left:", NK_TEXT_LEFT);
  874. nk_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f);
  875. nk_label(ctx, "middle:", NK_TEXT_LEFT);
  876. nk_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f);
  877. nk_label(ctx, "right:", NK_TEXT_LEFT);
  878. nk_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f);
  879. /* tiles */
  880. nk_layout_row(ctx, NK_STATIC, 200, 5, row_layout);
  881. /* left space */
  882. if (nk_group_begin(ctx, &sub, "left", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) {
  883. nk_layout_row_dynamic(ctx, 25, 1);
  884. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  885. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  886. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  887. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  888. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  889. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  890. nk_group_end(ctx);
  891. }
  892. /* scaler */
  893. bounds = nk_widget_bounds(ctx);
  894. nk_spacing(ctx, 1);
  895. if ((nk_input_is_mouse_hovering_rect(in, bounds) ||
  896. nk_input_is_mouse_prev_hovering_rect(in, bounds)) &&
  897. nk_input_is_mouse_down(in, NK_BUTTON_LEFT))
  898. {
  899. a = row_layout[0] + in->mouse.delta.x;
  900. b = row_layout[2] - in->mouse.delta.x;
  901. }
  902. /* middle space */
  903. if (nk_group_begin(ctx, &sub, "center", NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) {
  904. nk_layout_row_dynamic(ctx, 25, 1);
  905. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  906. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  907. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  908. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  909. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  910. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  911. nk_group_end(ctx);
  912. }
  913. /* scaler */
  914. bounds = nk_widget_bounds(ctx);
  915. nk_spacing(ctx, 1);
  916. if ((nk_input_is_mouse_hovering_rect(in, bounds) ||
  917. nk_input_is_mouse_prev_hovering_rect(in, bounds)) &&
  918. nk_input_is_mouse_down(in, NK_BUTTON_LEFT))
  919. {
  920. b = (row_layout[2] + in->mouse.delta.x);
  921. c = (row_layout[4] - in->mouse.delta.x);
  922. }
  923. /* right space */
  924. if (nk_group_begin(ctx, &sub, "right", NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) {
  925. nk_layout_row_dynamic(ctx, 25, 1);
  926. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  927. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  928. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  929. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  930. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  931. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  932. nk_group_end(ctx);
  933. }
  934. nk_tree_pop(ctx);
  935. }
  936. if (nk_tree_push(ctx, NK_TREE_NODE, "Horizontal", NK_MINIMIZED))
  937. {
  938. static float a = 100, b = 100, c = 100;
  939. struct nk_panel sub;
  940. struct nk_rect bounds;
  941. /* header */
  942. nk_layout_row_static(ctx, 30, 100, 2);
  943. nk_label(ctx, "top:", NK_TEXT_LEFT);
  944. nk_slider_float(ctx, 10.0f, &a, 200.0f, 10.0f);
  945. nk_label(ctx, "middle:", NK_TEXT_LEFT);
  946. nk_slider_float(ctx, 10.0f, &b, 200.0f, 10.0f);
  947. nk_label(ctx, "bottom:", NK_TEXT_LEFT);
  948. nk_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f);
  949. /* top space */
  950. nk_layout_row_dynamic(ctx, a, 1);
  951. if (nk_group_begin(ctx, &sub, "top", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER)) {
  952. nk_layout_row_dynamic(ctx, 25, 3);
  953. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  954. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  955. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  956. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  957. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  958. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  959. nk_group_end(ctx);
  960. }
  961. /* scaler */
  962. nk_layout_row_dynamic(ctx, 8, 1);
  963. bounds = nk_widget_bounds(ctx);
  964. nk_spacing(ctx, 1);
  965. if ((nk_input_is_mouse_hovering_rect(in, bounds) ||
  966. nk_input_is_mouse_prev_hovering_rect(in, bounds)) &&
  967. nk_input_is_mouse_down(in, NK_BUTTON_LEFT))
  968. {
  969. a = a + in->mouse.delta.y;
  970. b = b - in->mouse.delta.y;
  971. }
  972. /* middle space */
  973. nk_layout_row_dynamic(ctx, b, 1);
  974. if (nk_group_begin(ctx, &sub, "middle", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER)) {
  975. nk_layout_row_dynamic(ctx, 25, 3);
  976. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  977. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  978. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  979. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  980. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  981. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  982. nk_group_end(ctx);
  983. }
  984. {
  985. /* scaler */
  986. nk_layout_row_dynamic(ctx, 8, 1);
  987. bounds = nk_widget_bounds(ctx);
  988. if ((nk_input_is_mouse_hovering_rect(in, bounds) ||
  989. nk_input_is_mouse_prev_hovering_rect(in, bounds)) &&
  990. nk_input_is_mouse_down(in, NK_BUTTON_LEFT))
  991. {
  992. b = b + in->mouse.delta.y;
  993. c = c - in->mouse.delta.y;
  994. }
  995. }
  996. /* bottom space */
  997. nk_layout_row_dynamic(ctx, c, 1);
  998. if (nk_group_begin(ctx, &sub, "bottom", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER)) {
  999. nk_layout_row_dynamic(ctx, 25, 3);
  1000. nk_button_label(ctx, "#FFAA", NK_BUTTON_DEFAULT);
  1001. nk_button_label(ctx, "#FFBB", NK_BUTTON_DEFAULT);
  1002. nk_button_label(ctx, "#FFCC", NK_BUTTON_DEFAULT);
  1003. nk_button_label(ctx, "#FFDD", NK_BUTTON_DEFAULT);
  1004. nk_button_label(ctx, "#FFEE", NK_BUTTON_DEFAULT);
  1005. nk_button_label(ctx, "#FFFF", NK_BUTTON_DEFAULT);
  1006. nk_group_end(ctx);
  1007. }
  1008. nk_tree_pop(ctx);
  1009. }
  1010. nk_tree_pop(ctx);
  1011. }
  1012. nk_tree_pop(ctx);
  1013. }
  1014. }
  1015. nk_end(ctx);
  1016. return !nk_window_is_closed(ctx, "Demo");
  1017. }
  1018. /* ===============================================================
  1019. *
  1020. * DEVICE
  1021. *
  1022. * ===============================================================*/
  1023. struct device {
  1024. struct nk_buffer cmds;
  1025. struct nk_draw_null_texture null;
  1026. GLuint vbo, vao, ebo;
  1027. GLuint prog;
  1028. GLuint vert_shdr;
  1029. GLuint frag_shdr;
  1030. GLint attrib_pos;
  1031. GLint attrib_uv;
  1032. GLint attrib_col;
  1033. GLint uniform_tex;
  1034. GLint uniform_proj;
  1035. GLuint font_tex;
  1036. };
  1037. static void
  1038. device_init(struct device *dev)
  1039. {
  1040. GLint status;
  1041. static const GLchar *vertex_shader =
  1042. "#version 300 es\n"
  1043. "uniform mat4 ProjMtx;\n"
  1044. "in vec2 Position;\n"
  1045. "in vec2 TexCoord;\n"
  1046. "in vec4 Color;\n"
  1047. "out vec2 Frag_UV;\n"
  1048. "out vec4 Frag_Color;\n"
  1049. "void main() {\n"
  1050. " Frag_UV = TexCoord;\n"
  1051. " Frag_Color = Color;\n"
  1052. " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
  1053. "}\n";
  1054. static const GLchar *fragment_shader =
  1055. "#version 300 es\n"
  1056. "precision mediump float;\n"
  1057. "uniform sampler2D Texture;\n"
  1058. "in vec2 Frag_UV;\n"
  1059. "in vec4 Frag_Color;\n"
  1060. "out vec4 Out_Color;\n"
  1061. "void main(){\n"
  1062. " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
  1063. "}\n";
  1064. nk_buffer_init_default(&dev->cmds);
  1065. dev->prog = glCreateProgram();
  1066. dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
  1067. dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
  1068. glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
  1069. glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0);
  1070. glCompileShader(dev->vert_shdr);
  1071. glCompileShader(dev->frag_shdr);
  1072. glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status);
  1073. assert(status == GL_TRUE);
  1074. glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status);
  1075. assert(status == GL_TRUE);
  1076. glAttachShader(dev->prog, dev->vert_shdr);
  1077. glAttachShader(dev->prog, dev->frag_shdr);
  1078. glLinkProgram(dev->prog);
  1079. glGetProgramiv(dev->prog, GL_LINK_STATUS, &status);
  1080. assert(status == GL_TRUE);
  1081. dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture");
  1082. dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx");
  1083. dev->attrib_pos = glGetAttribLocation(dev->prog, "Position");
  1084. dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord");
  1085. dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
  1086. {
  1087. /* buffer setup */
  1088. GLsizei vs = sizeof(struct nk_draw_vertex);
  1089. size_t vp = offsetof(struct nk_draw_vertex, position);
  1090. size_t vt = offsetof(struct nk_draw_vertex, uv);
  1091. size_t vc = offsetof(struct nk_draw_vertex, col);
  1092. glGenBuffers(1, &dev->vbo);
  1093. glGenBuffers(1, &dev->ebo);
  1094. glGenVertexArrays(1, &dev->vao);
  1095. glBindVertexArray(dev->vao);
  1096. glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
  1097. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
  1098. glEnableVertexAttribArray((GLuint)dev->attrib_pos);
  1099. glEnableVertexAttribArray((GLuint)dev->attrib_uv);
  1100. glEnableVertexAttribArray((GLuint)dev->attrib_col);
  1101. glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp);
  1102. glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt);
  1103. glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc);
  1104. }
  1105. glBindTexture(GL_TEXTURE_2D, 0);
  1106. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1107. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  1108. glBindVertexArray(0);
  1109. }
  1110. static void
  1111. device_upload_atlas(struct device *dev, const void *image, int width, int height)
  1112. {
  1113. glGenTextures(1, &dev->font_tex);
  1114. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  1115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1117. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  1118. GL_RGBA, GL_UNSIGNED_BYTE, image);
  1119. }
  1120. static void
  1121. device_shutdown(struct device *dev)
  1122. {
  1123. glDetachShader(dev->prog, dev->vert_shdr);
  1124. glDetachShader(dev->prog, dev->frag_shdr);
  1125. glDeleteShader(dev->vert_shdr);
  1126. glDeleteShader(dev->frag_shdr);
  1127. glDeleteProgram(dev->prog);
  1128. glDeleteTextures(1, &dev->font_tex);
  1129. glDeleteBuffers(1, &dev->vbo);
  1130. glDeleteBuffers(1, &dev->ebo);
  1131. nk_buffer_free(&dev->cmds);
  1132. }
  1133. static void
  1134. device_draw(struct device *dev, struct nk_context *ctx, int width, int height,
  1135. enum nk_anti_aliasing AA)
  1136. {
  1137. GLint last_prog, last_tex;
  1138. GLint last_ebo, last_vbo, last_vao;
  1139. GLfloat ortho[4][4] = {
  1140. {2.0f, 0.0f, 0.0f, 0.0f},
  1141. {0.0f,-2.0f, 0.0f, 0.0f},
  1142. {0.0f, 0.0f,-1.0f, 0.0f},
  1143. {-1.0f,1.0f, 0.0f, 1.0f},
  1144. };
  1145. ortho[0][0] /= (GLfloat)width;
  1146. ortho[1][1] /= (GLfloat)height;
  1147. /* save previous opengl state */
  1148. glGetIntegerv(GL_CURRENT_PROGRAM, &last_prog);
  1149. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_tex);
  1150. glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_vao);
  1151. glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_ebo);
  1152. glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vbo);
  1153. /* setup global state */
  1154. glEnable(GL_BLEND);
  1155. glBlendEquation(GL_FUNC_ADD);
  1156. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1157. glDisable(GL_CULL_FACE);
  1158. glDisable(GL_DEPTH_TEST);
  1159. glEnable(GL_SCISSOR_TEST);
  1160. glActiveTexture(GL_TEXTURE0);
  1161. /* setup program */
  1162. glUseProgram(dev->prog);
  1163. glUniform1i(dev->uniform_tex, 0);
  1164. glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]);
  1165. {
  1166. /* convert from command queue into draw list and draw to screen */
  1167. const struct nk_draw_command *cmd;
  1168. void *vertices, *elements;
  1169. const nk_draw_index *offset = NULL;
  1170. /* allocate vertex and element buffer */
  1171. glBindVertexArray(dev->vao);
  1172. glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
  1173. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
  1174. glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_MEMORY, NULL, GL_STREAM_DRAW);
  1175. glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW);
  1176. /* load draw vertices & elements directly into vertex + element buffer */
  1177. vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  1178. elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
  1179. {
  1180. /* fill converting configuration */
  1181. struct nk_convert_config config;
  1182. memset(&config, 0, sizeof(config));
  1183. config.global_alpha = 1.0f;
  1184. config.shape_AA = AA;
  1185. config.line_AA = AA;
  1186. config.circle_segment_count = 22;
  1187. config.curve_segment_count = 22;
  1188. config.arc_segment_count = 22;
  1189. config.null = dev->null;
  1190. /* setup buffers to load vertices and elements */
  1191. {struct nk_buffer vbuf, ebuf;
  1192. nk_buffer_init_fixed(&vbuf, vertices, MAX_VERTEX_MEMORY);
  1193. nk_buffer_init_fixed(&ebuf, elements, MAX_ELEMENT_MEMORY);
  1194. nk_convert(ctx, &dev->cmds, &vbuf, &ebuf, &config);}
  1195. }
  1196. glUnmapBuffer(GL_ARRAY_BUFFER);
  1197. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  1198. /* iterate over and execute each draw command */
  1199. nk_draw_foreach(cmd, ctx, &dev->cmds) {
  1200. if (!cmd->elem_count) continue;
  1201. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  1202. glScissor((GLint)cmd->clip_rect.x,
  1203. height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h),
  1204. (GLint)cmd->clip_rect.w, (GLint)cmd->clip_rect.h);
  1205. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  1206. offset += cmd->elem_count;
  1207. }
  1208. nk_clear(ctx);
  1209. }
  1210. /* restore old state */
  1211. glUseProgram((GLuint)last_prog);
  1212. glBindTexture(GL_TEXTURE_2D, (GLuint)last_tex);
  1213. glBindBuffer(GL_ARRAY_BUFFER, (GLuint)last_vbo);
  1214. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)last_ebo);
  1215. glBindVertexArray((GLuint)last_vao);
  1216. glDisable(GL_SCISSOR_TEST);
  1217. }
  1218. /* glfw callbacks (I don't know if there is a easier way to access text and scroll )*/
  1219. static void error_callback(int e, const char *d){printf("Error %d: %s\n", e, d);}
  1220. static void text_input(GLFWwindow *win, unsigned int codepoint)
  1221. {nk_input_unicode((struct nk_context*)glfwGetWindowUserPointer(win), codepoint);}
  1222. static void scroll_input(GLFWwindow *win, double _, double yoff)
  1223. {UNUSED(_);nk_input_scroll((struct nk_context*)glfwGetWindowUserPointer(win), (float)yoff);}
  1224. int main(int argc, char *argv[])
  1225. {
  1226. /* Platform */
  1227. static GLFWwindow *win;
  1228. int width = 0, height = 0;
  1229. /* GUI */
  1230. struct device device;
  1231. struct nk_context ctx;
  1232. struct nk_font *font;
  1233. struct nk_font_atlas atlas;
  1234. /* GLFW */
  1235. glfwSetErrorCallback(error_callback);
  1236. if (!glfwInit()) {
  1237. fprintf(stdout, "[GFLW] failed to init!\n");
  1238. exit(1);
  1239. }
  1240. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  1241. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  1242. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  1243. #ifdef __APPLE__
  1244. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  1245. #endif
  1246. win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
  1247. glfwMakeContextCurrent(win);
  1248. glfwSetWindowUserPointer(win, &ctx);
  1249. glfwSetCharCallback(win, text_input);
  1250. glfwSetScrollCallback(win, scroll_input);
  1251. /* OpenGL */
  1252. glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
  1253. glewExperimental = 1;
  1254. if (glewInit() != GLEW_OK) {
  1255. fprintf(stderr, "Failed to setup GLEW\n");
  1256. exit(1);
  1257. }
  1258. {/* GUI */
  1259. device_init(&device);
  1260. {const void *image; int w, h;
  1261. const char *font_path = (argc > 1) ? argv[1]: 0;
  1262. nk_font_atlas_init_default(&atlas);
  1263. nk_font_atlas_begin(&atlas);
  1264. if (font_path) font = nk_font_atlas_add_from_file(&atlas, font_path, 14.0f, NULL);
  1265. else font = nk_font_atlas_add_default(&atlas, 14.0f, NULL);
  1266. image = nk_font_atlas_bake(&atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  1267. device_upload_atlas(&device, image, w, h);
  1268. nk_font_atlas_end(&atlas, nk_handle_id((int)device.font_tex), &device.null);}
  1269. nk_init_default(&ctx, &font->handle);}
  1270. while (!glfwWindowShouldClose(win))
  1271. {
  1272. /* Input */
  1273. {double x, y;
  1274. nk_input_begin(&ctx);
  1275. glfwPollEvents();
  1276. nk_input_key(&ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
  1277. nk_input_key(&ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
  1278. nk_input_key(&ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
  1279. nk_input_key(&ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
  1280. nk_input_key(&ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
  1281. nk_input_key(&ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
  1282. nk_input_key(&ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
  1283. nk_input_key(&ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
  1284. if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
  1285. glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL)) {
  1286. nk_input_key(&ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
  1287. nk_input_key(&ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_P) == GLFW_PRESS);
  1288. nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
  1289. nk_input_key(&ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
  1290. nk_input_key(&ctx, NK_KEY_SHIFT, 1);
  1291. } else {
  1292. nk_input_key(&ctx, NK_KEY_COPY, 0);
  1293. nk_input_key(&ctx, NK_KEY_PASTE, 0);
  1294. nk_input_key(&ctx, NK_KEY_CUT, 0);
  1295. nk_input_key(&ctx, NK_KEY_SHIFT, 0);
  1296. }
  1297. glfwGetCursorPos(win, &x, &y);
  1298. nk_input_motion(&ctx, (int)x, (int)y);
  1299. nk_input_button(&ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
  1300. nk_input_button(&ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
  1301. nk_input_button(&ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
  1302. nk_input_end(&ctx);}
  1303. /* GUI */
  1304. if (!overview_window(&ctx)) break;
  1305. /* Draw */
  1306. glfwGetWindowSize(win, &width, &height);
  1307. glViewport(0, 0, width, height);
  1308. glClear(GL_COLOR_BUFFER_BIT);
  1309. glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
  1310. device_draw(&device, &ctx, width, height, NK_ANTI_ALIASING_ON);
  1311. glfwSwapBuffers(win);
  1312. }
  1313. nk_font_atlas_clear(&atlas);
  1314. nk_free(&ctx);
  1315. device_shutdown(&device);
  1316. glfwTerminate();
  1317. return 0;
  1318. }