🛠️🐜 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.

386 lines
14 KiB

  1. /*
  2. * Nuklear - v1.00 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2016 by Micha Mettke
  5. */
  6. /*
  7. * ==============================================================
  8. *
  9. * API
  10. *
  11. * ===============================================================
  12. */
  13. #ifndef NK_BTGUI_GL2_H_
  14. #define NK_BTGUI_GL2_H_
  15. enum nk_btgui_init_state{
  16. NK_BTGUI3_DEFAULT = 0,
  17. NK_BTGUI3_INSTALL_CALLBACKS
  18. };
  19. struct nk_btgui_vertex {
  20. float position[2];
  21. float uv[2];
  22. nk_byte col[4];
  23. };
  24. NK_API struct nk_context* nk_btgui_init(b3gDefaultOpenGLWindow *win, enum nk_btgui_init_state);
  25. NK_API void nk_btgui_font_stash_begin(struct nk_font_atlas **atlas);
  26. NK_API void nk_btgui_font_stash_end(void);
  27. NK_API void nk_btgui_update_mouse_pos(int x, int y);
  28. NK_API void nk_btgui_update_mouse_state(int left_button_state, int middle_button_state, int right_button_state);
  29. NK_API void nk_btgui_new_frame(void);
  30. NK_API void nk_btgui_render(enum nk_anti_aliasing , int max_vertex_buffer, int max_element_buffer);
  31. NK_API void nk_btgui_shutdown(void);
  32. NK_API void nk_btgui_char_callback(b3gDefaultOpenGLWindow *win, unsigned int codepoint);
  33. NK_API void nk_btgui_scroll_callback(b3gDefaultOpenGLWindow *win, double xoff, double yoff);
  34. #endif
  35. /*
  36. * ==============================================================
  37. *
  38. * IMPLEMENTATION
  39. *
  40. * ===============================================================
  41. */
  42. #ifdef NK_BTGUI_GL2_IMPLEMENTATION
  43. #ifndef NK_BTGUI_TEXT_MAX
  44. #define NK_BTGUI_TEXT_MAX 256
  45. #endif
  46. struct nk_btgui_device {
  47. struct nk_buffer cmds;
  48. struct nk_draw_null_texture null;
  49. GLuint font_tex;
  50. };
  51. static struct nk_btgui {
  52. b3gDefaultOpenGLWindow *win;
  53. int width, height;
  54. int display_width, display_height;
  55. struct nk_btgui_device ogl;
  56. struct nk_context ctx;
  57. struct nk_font_atlas atlas;
  58. struct nk_vec2 fb_scale;
  59. unsigned int text[NK_BTGUI_TEXT_MAX];
  60. int text_len;
  61. float scroll;
  62. int left_button_state;
  63. int middle_button_state;
  64. int right_button_state;
  65. int mouse_pos[2];
  66. } btgui;
  67. NK_INTERN void
  68. nk_btgui_device_upload_atlas(const void *image, int width, int height)
  69. {
  70. struct nk_btgui_device *dev = &btgui.ogl;
  71. glGenTextures(1, &dev->font_tex);
  72. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  73. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  74. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  75. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  76. GL_RGBA, GL_UNSIGNED_BYTE, image);
  77. assert(glGetError() == GL_NO_ERROR);
  78. }
  79. NK_API void
  80. nk_btgui_update_mouse_state(int left_button_state, int middle_button_state, int right_button_state)
  81. {
  82. btgui.left_button_state = left_button_state;
  83. btgui.middle_button_state = middle_button_state;
  84. btgui.right_button_state = right_button_state;
  85. }
  86. NK_API void
  87. nk_btgui_update_mouse_pos(int x, int y)
  88. {
  89. btgui.mouse_pos[0] = x;
  90. btgui.mouse_pos[1] = y;
  91. }
  92. NK_API void
  93. nk_btgui_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
  94. {
  95. /* setup global state */
  96. struct nk_btgui_device *dev = &btgui.ogl;
  97. glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  98. assert(glGetError() == GL_NO_ERROR);
  99. glDisable(GL_CULL_FACE);
  100. glDisable(GL_DEPTH_TEST);
  101. glEnable(GL_SCISSOR_TEST);
  102. glEnable(GL_BLEND);
  103. glEnable(GL_TEXTURE_2D);
  104. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  105. /* setup viewport/project */
  106. glViewport(0,0,(GLsizei)btgui.display_width,(GLsizei)btgui.display_height);
  107. glMatrixMode(GL_PROJECTION);
  108. glPushMatrix();
  109. glLoadIdentity();
  110. glOrtho(0.0f, btgui.width, btgui.height, 0.0f, -1.0f, 1.0f);
  111. glMatrixMode(GL_MODELVIEW);
  112. glPushMatrix();
  113. glLoadIdentity();
  114. glEnableClientState(GL_VERTEX_ARRAY);
  115. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  116. glEnableClientState(GL_COLOR_ARRAY);
  117. {
  118. GLsizei vs = sizeof(struct nk_btgui_vertex);
  119. size_t vp = offsetof(struct nk_btgui_vertex, position);
  120. size_t vt = offsetof(struct nk_btgui_vertex, uv);
  121. size_t vc = offsetof(struct nk_btgui_vertex, col);
  122. /* convert from command queue into draw list and draw to screen */
  123. const struct nk_draw_command *cmd;
  124. const nk_draw_index *offset = NULL;
  125. struct nk_buffer vbuf, ebuf;
  126. /* fill converting configuration */
  127. struct nk_convert_config config;
  128. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  129. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_btgui_vertex, position)},
  130. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_btgui_vertex, uv)},
  131. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_btgui_vertex, col)},
  132. {NK_VERTEX_LAYOUT_END}
  133. };
  134. NK_MEMSET(&config, 0, sizeof(config));
  135. config.vertex_layout = vertex_layout;
  136. config.vertex_size = sizeof(struct nk_btgui_vertex);
  137. config.vertex_alignment = NK_ALIGNOF(struct nk_btgui_vertex);
  138. config.null = dev->null;
  139. config.circle_segment_count = 22;
  140. config.curve_segment_count = 22;
  141. config.arc_segment_count = 22;
  142. config.global_alpha = 1.0f;
  143. config.shape_AA = AA;
  144. config.line_AA = AA;
  145. /* convert shapes into vertexes */
  146. nk_buffer_init_default(&vbuf);
  147. nk_buffer_init_default(&ebuf);
  148. nk_convert(&btgui.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  149. /* setup vertex buffer pointer */
  150. {const void *vertices = nk_buffer_memory_const(&vbuf);
  151. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  152. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  153. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
  154. /* iterate over and execute each draw command */
  155. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  156. nk_draw_foreach(cmd, &btgui.ctx, &dev->cmds)
  157. {
  158. if (!cmd->elem_count) continue;
  159. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  160. glScissor(
  161. (GLint)(cmd->clip_rect.x * btgui.fb_scale.x),
  162. (GLint)((btgui.height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * btgui.fb_scale.y),
  163. (GLint)(cmd->clip_rect.w * btgui.fb_scale.x),
  164. (GLint)(cmd->clip_rect.h * btgui.fb_scale.y));
  165. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  166. offset += cmd->elem_count;
  167. }
  168. nk_clear(&btgui.ctx);
  169. nk_buffer_free(&vbuf);
  170. nk_buffer_free(&ebuf);
  171. }
  172. assert(glGetError() == GL_NO_ERROR);
  173. /* default OpenGL state */
  174. glDisableClientState(GL_VERTEX_ARRAY);
  175. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  176. glDisableClientState(GL_COLOR_ARRAY);
  177. assert(glGetError() == GL_NO_ERROR);
  178. glDisable(GL_CULL_FACE);
  179. glDisable(GL_DEPTH_TEST);
  180. glDisable(GL_SCISSOR_TEST);
  181. glDisable(GL_BLEND);
  182. glDisable(GL_TEXTURE_2D);
  183. glBindTexture(GL_TEXTURE_2D, 0);
  184. glMatrixMode(GL_MODELVIEW);
  185. glPopMatrix();
  186. glMatrixMode(GL_PROJECTION);
  187. glPopMatrix();
  188. glPopAttrib();
  189. assert(glGetError() == GL_NO_ERROR);
  190. }
  191. #if 0 // @todo
  192. NK_API void
  193. nk_btgui_char_callback(b3gDefaultOpenGLWindow *win, unsigned int codepoint)
  194. {
  195. (void)win;
  196. if (btgui.text_len < NK_BTGUI_TEXT_MAX)
  197. btgui.text[btgui.text_len++] = codepoint;
  198. }
  199. NK_API void
  200. nk_gflw3_scroll_callback(b3gDefaultOpenGLWindow *win, double xoff, double yoff)
  201. {
  202. (void)win; (void)xoff;
  203. btgui.scroll += (float)yoff;
  204. }
  205. static void
  206. nk_btgui_clipbard_paste(nk_handle usr, struct nk_text_edit *edit)
  207. {
  208. const char *text = btguiGetClipboardString(btgui.win);
  209. if (text) nk_textedit_paste(edit, text, nk_strlen(text));
  210. (void)usr;
  211. }
  212. static void
  213. nk_btgui_clipbard_copy(nk_handle usr, const char *text, int len)
  214. {
  215. char *str = 0;
  216. (void)usr;
  217. if (!len) return;
  218. str = (char*)malloc((size_t)len+1);
  219. if (!str) return;
  220. memcpy(str, text, (size_t)len);
  221. str[len] = '\0';
  222. btguiSetClipboardString(btgui.win, str);
  223. free(str);
  224. }
  225. #endif
  226. NK_API struct nk_context*
  227. nk_btgui_init(b3gDefaultOpenGLWindow *win, enum nk_btgui_init_state init_state)
  228. {
  229. btgui.win = win;
  230. if (init_state == NK_BTGUI3_INSTALL_CALLBACKS) {
  231. //btguiSetScrollCallback(win, nk_gflw3_scroll_callback);
  232. //btguiSetCharCallback(win, nk_btgui_char_callback);
  233. }
  234. nk_init_default(&btgui.ctx, 0);
  235. //btgui.ctx.clip.copy = nk_btgui_clipbard_copy;
  236. //btgui.ctx.clip.paste = nk_btgui_clipbard_paste;
  237. //btgui.ctx.clip.userdata = nk_handle_ptr(0);
  238. nk_buffer_init_default(&btgui.ogl.cmds);
  239. return &btgui.ctx;
  240. }
  241. NK_API void
  242. nk_btgui_font_stash_begin(struct nk_font_atlas **atlas)
  243. {
  244. nk_font_atlas_init_default(&btgui.atlas);
  245. nk_font_atlas_begin(&btgui.atlas);
  246. *atlas = &btgui.atlas;
  247. }
  248. NK_API void
  249. nk_btgui_font_stash_end(void)
  250. {
  251. const void *image; int w, h;
  252. image = nk_font_atlas_bake(&btgui.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  253. nk_btgui_device_upload_atlas(image, w, h);
  254. nk_font_atlas_end(&btgui.atlas, nk_handle_id((int)btgui.ogl.font_tex), &btgui.ogl.null);
  255. if (btgui.atlas.default_font)
  256. nk_style_set_font(&btgui.ctx, &btgui.atlas.default_font->handle);
  257. }
  258. NK_API void
  259. nk_btgui_new_frame(void)
  260. {
  261. int i;
  262. //double x, y;
  263. struct nk_context *ctx = &btgui.ctx;
  264. b3gDefaultOpenGLWindow *win = btgui.win;
  265. btgui.width = win->getWidth();
  266. btgui.height = win->getHeight();
  267. float fbscale = 1.0;
  268. #ifdef __APPLE__
  269. //fbscale = win->getRetinaScale();
  270. #endif
  271. btgui.display_width = fbscale * btgui.width;
  272. btgui.display_height = fbscale * btgui.height;
  273. btgui.fb_scale.x = (float)btgui.display_width/(float)btgui.width;
  274. btgui.fb_scale.y = (float)btgui.display_height/(float)btgui.height;
  275. // HACK
  276. btgui.fb_scale.x = 1.0;
  277. btgui.fb_scale.y = 1.0;
  278. nk_input_begin(ctx);
  279. #if 0
  280. for (i = 0; i < btgui.text_len; ++i)
  281. nk_input_unicode(ctx, btgui.text[i]);
  282. nk_input_key(ctx, NK_KEY_DEL, btguiGetKey(win, BTGUI_KEY_DELETE) == BTGUI_PRESS);
  283. nk_input_key(ctx, NK_KEY_ENTER, btguiGetKey(win, BTGUI_KEY_ENTER) == BTGUI_PRESS);
  284. nk_input_key(ctx, NK_KEY_TAB, btguiGetKey(win, BTGUI_KEY_TAB) == BTGUI_PRESS);
  285. nk_input_key(ctx, NK_KEY_BACKSPACE, btguiGetKey(win, BTGUI_KEY_BACKSPACE) == BTGUI_PRESS);
  286. nk_input_key(ctx, NK_KEY_UP, btguiGetKey(win, BTGUI_KEY_UP) == BTGUI_PRESS);
  287. nk_input_key(ctx, NK_KEY_DOWN, btguiGetKey(win, BTGUI_KEY_DOWN) == BTGUI_PRESS);
  288. nk_input_key(ctx, NK_KEY_TEXT_START, btguiGetKey(win, BTGUI_KEY_HOME) == BTGUI_PRESS);
  289. nk_input_key(ctx, NK_KEY_TEXT_END, btguiGetKey(win, BTGUI_KEY_END) == BTGUI_PRESS);
  290. nk_input_key(ctx, NK_KEY_SHIFT, btguiGetKey(win, BTGUI_KEY_LEFT_SHIFT) == BTGUI_PRESS||
  291. btguiGetKey(win, BTGUI_KEY_RIGHT_SHIFT) == BTGUI_PRESS);
  292. if (btguiGetKey(win, BTGUI_KEY_LEFT_CONTROL) == BTGUI_PRESS ||
  293. btguiGetKey(win, BTGUI_KEY_RIGHT_CONTROL)) {
  294. nk_input_key(ctx, NK_KEY_COPY, btguiGetKey(win, BTGUI_KEY_C) == BTGUI_PRESS);
  295. nk_input_key(ctx, NK_KEY_PASTE, btguiGetKey(win, BTGUI_KEY_P) == BTGUI_PRESS);
  296. nk_input_key(ctx, NK_KEY_CUT, btguiGetKey(win, BTGUI_KEY_X) == BTGUI_PRESS);
  297. nk_input_key(ctx, NK_KEY_TEXT_UNDO, btguiGetKey(win, BTGUI_KEY_Z) == BTGUI_PRESS);
  298. nk_input_key(ctx, NK_KEY_TEXT_REDO, btguiGetKey(win, BTGUI_KEY_R) == BTGUI_PRESS);
  299. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, btguiGetKey(win, BTGUI_KEY_LEFT) == BTGUI_PRESS);
  300. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, btguiGetKey(win, BTGUI_KEY_RIGHT) == BTGUI_PRESS);
  301. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, btguiGetKey(win, BTGUI_KEY_B) == BTGUI_PRESS);
  302. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, btguiGetKey(win, BTGUI_KEY_E) == BTGUI_PRESS);
  303. } else {
  304. nk_input_key(ctx, NK_KEY_LEFT, btguiGetKey(win, BTGUI_KEY_LEFT) == BTGUI_PRESS);
  305. nk_input_key(ctx, NK_KEY_RIGHT, btguiGetKey(win, BTGUI_KEY_RIGHT) == BTGUI_PRESS);
  306. nk_input_key(ctx, NK_KEY_COPY, 0);
  307. nk_input_key(ctx, NK_KEY_PASTE, 0);
  308. nk_input_key(ctx, NK_KEY_CUT, 0);
  309. nk_input_key(ctx, NK_KEY_SHIFT, 0);
  310. }
  311. btguiGetCursorPos(win, &x, &y);
  312. nk_input_motion(ctx, (int)x, (int)y);
  313. nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, btguiGetMouseButton(win, BTGUI_MOUSE_BUTTON_LEFT) == BTGUI_PRESS);
  314. nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, btguiGetMouseButton(win, BTGUI_MOUSE_BUTTON_MIDDLE) == BTGUI_PRESS);
  315. nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, btguiGetMouseButton(win, BTGUI_MOUSE_BUTTON_RIGHT) == BTGUI_PRESS);
  316. nk_input_scroll(ctx, btgui.scroll);
  317. #endif
  318. nk_input_motion(ctx, btgui.mouse_pos[0], btgui.mouse_pos[1]);
  319. nk_input_button(ctx, NK_BUTTON_LEFT, btgui.mouse_pos[0], btgui.mouse_pos[1], btgui.left_button_state);
  320. nk_input_button(ctx, NK_BUTTON_MIDDLE, btgui.mouse_pos[0], btgui.mouse_pos[1], btgui.middle_button_state);
  321. nk_input_button(ctx, NK_BUTTON_RIGHT, btgui.mouse_pos[0], btgui.mouse_pos[1], btgui.right_button_state);
  322. nk_input_end(&btgui.ctx);
  323. btgui.text_len = 0;
  324. btgui.scroll = 0;
  325. }
  326. NK_API
  327. void nk_btgui_shutdown(void)
  328. {
  329. struct nk_btgui_device *dev = &btgui.ogl;
  330. nk_font_atlas_clear(&btgui.atlas);
  331. nk_free(&btgui.ctx);
  332. glDeleteTextures(1, &dev->font_tex);
  333. nk_buffer_free(&dev->cmds);
  334. memset(&btgui, 0, sizeof(btgui));
  335. }
  336. #endif