2016-04-30 18:09:01 +03:00
|
|
|
/*
|
2017-01-15 23:04:51 +03:00
|
|
|
* Nuklear - 1.32.0 - public domain
|
2016-04-30 18:09:01 +03:00
|
|
|
* no warrenty implied; use at your own risk.
|
|
|
|
* authored from 2015-2016 by Micha Mettke
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* ==============================================================
|
|
|
|
*
|
|
|
|
* API
|
|
|
|
*
|
|
|
|
* ===============================================================
|
|
|
|
*/
|
|
|
|
#ifndef NK_GLFW_GL3_H_
|
|
|
|
#define NK_GLFW_GL3_H_
|
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
enum nk_glfw_init_state{
|
|
|
|
NK_GLFW3_DEFAULT=0,
|
|
|
|
NK_GLFW3_INSTALL_CALLBACKS
|
|
|
|
};
|
|
|
|
|
2020-05-08 14:12:55 +03:00
|
|
|
#ifndef NK_GLFW_TEXT_MAX
|
|
|
|
#define NK_GLFW_TEXT_MAX 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct nk_glfw_device {
|
|
|
|
struct nk_buffer cmds;
|
2022-07-31 00:43:46 +03:00
|
|
|
struct nk_draw_null_texture tex_null;
|
2020-05-08 14:12:55 +03:00
|
|
|
GLuint vbo, vao, ebo;
|
|
|
|
GLuint prog;
|
|
|
|
GLuint vert_shdr;
|
|
|
|
GLuint frag_shdr;
|
|
|
|
GLint attrib_pos;
|
|
|
|
GLint attrib_uv;
|
|
|
|
GLint attrib_col;
|
|
|
|
GLint uniform_tex;
|
|
|
|
GLint uniform_proj;
|
|
|
|
GLuint font_tex;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nk_glfw {
|
|
|
|
GLFWwindow *win;
|
|
|
|
int width, height;
|
|
|
|
int display_width, display_height;
|
|
|
|
struct nk_glfw_device ogl;
|
|
|
|
struct nk_context ctx;
|
|
|
|
struct nk_font_atlas atlas;
|
|
|
|
struct nk_vec2 fb_scale;
|
|
|
|
unsigned int text[NK_GLFW_TEXT_MAX];
|
|
|
|
int text_len;
|
|
|
|
struct nk_vec2 scroll;
|
|
|
|
double last_button_click;
|
|
|
|
int is_double_click_down;
|
|
|
|
struct nk_vec2 double_click_pos;
|
|
|
|
};
|
2016-04-30 18:09:01 +03:00
|
|
|
|
2020-04-30 16:17:24 +03:00
|
|
|
NK_API struct nk_context* nk_glfw3_init(struct nk_glfw* glfw, GLFWwindow *win, enum nk_glfw_init_state);
|
|
|
|
NK_API void nk_glfw3_shutdown(struct nk_glfw* glfw);
|
|
|
|
NK_API void nk_glfw3_font_stash_begin(struct nk_glfw* glfw, struct nk_font_atlas **atlas);
|
|
|
|
NK_API void nk_glfw3_font_stash_end(struct nk_glfw* glfw);
|
|
|
|
NK_API void nk_glfw3_new_frame(struct nk_glfw* glfw);
|
|
|
|
NK_API void nk_glfw3_render(struct nk_glfw* glfw, enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
|
|
|
|
|
|
|
|
NK_API void nk_glfw3_device_destroy(struct nk_glfw* glfw);
|
|
|
|
NK_API void nk_glfw3_device_create(struct nk_glfw* glfw);
|
2016-04-30 18:09:01 +03:00
|
|
|
|
|
|
|
NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint);
|
|
|
|
NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff);
|
2017-05-06 19:49:09 +03:00
|
|
|
NK_API void nk_glfw3_mouse_button_callback(GLFWwindow *win, int button, int action, int mods);
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2016-04-30 18:09:01 +03:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* ==============================================================
|
|
|
|
*
|
|
|
|
* IMPLEMENTATION
|
|
|
|
*
|
|
|
|
* ===============================================================
|
|
|
|
*/
|
|
|
|
#ifdef NK_GLFW_GL3_IMPLEMENTATION
|
2024-01-20 15:46:45 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2016-04-30 18:09:01 +03:00
|
|
|
|
2017-05-06 19:49:09 +03:00
|
|
|
#ifndef NK_GLFW_DOUBLE_CLICK_LO
|
|
|
|
#define NK_GLFW_DOUBLE_CLICK_LO 0.02
|
|
|
|
#endif
|
|
|
|
#ifndef NK_GLFW_DOUBLE_CLICK_HI
|
|
|
|
#define NK_GLFW_DOUBLE_CLICK_HI 0.2
|
|
|
|
#endif
|
2016-04-30 18:09:01 +03:00
|
|
|
|
2016-09-02 16:33:20 +03:00
|
|
|
struct nk_glfw_vertex {
|
|
|
|
float position[2];
|
|
|
|
float uv[2];
|
|
|
|
nk_byte col[4];
|
|
|
|
};
|
|
|
|
|
2016-04-19 12:02:22 +03:00
|
|
|
#ifdef __APPLE__
|
2016-04-19 14:06:54 +03:00
|
|
|
#define NK_SHADER_VERSION "#version 150\n"
|
2016-04-19 12:02:22 +03:00
|
|
|
#else
|
|
|
|
#define NK_SHADER_VERSION "#version 300 es\n"
|
|
|
|
#endif
|
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_device_create(struct nk_glfw* glfw)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
GLint status;
|
|
|
|
static const GLchar *vertex_shader =
|
2016-04-19 12:02:22 +03:00
|
|
|
NK_SHADER_VERSION
|
2016-04-11 02:34:59 +03:00
|
|
|
"uniform mat4 ProjMtx;\n"
|
|
|
|
"in vec2 Position;\n"
|
|
|
|
"in vec2 TexCoord;\n"
|
|
|
|
"in vec4 Color;\n"
|
|
|
|
"out vec2 Frag_UV;\n"
|
|
|
|
"out vec4 Frag_Color;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" Frag_UV = TexCoord;\n"
|
|
|
|
" Frag_Color = Color;\n"
|
|
|
|
" gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
|
|
|
|
"}\n";
|
|
|
|
static const GLchar *fragment_shader =
|
2016-04-19 12:02:22 +03:00
|
|
|
NK_SHADER_VERSION
|
2016-04-11 02:34:59 +03:00
|
|
|
"precision mediump float;\n"
|
|
|
|
"uniform sampler2D Texture;\n"
|
|
|
|
"in vec2 Frag_UV;\n"
|
|
|
|
"in vec4 Frag_Color;\n"
|
|
|
|
"out vec4 Out_Color;\n"
|
|
|
|
"void main(){\n"
|
|
|
|
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
|
|
|
|
"}\n";
|
|
|
|
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw_device *dev = &glfw->ogl;
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_buffer_init_default(&dev->cmds);
|
|
|
|
dev->prog = glCreateProgram();
|
|
|
|
dev->vert_shdr = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
dev->frag_shdr = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
glShaderSource(dev->vert_shdr, 1, &vertex_shader, 0);
|
|
|
|
glShaderSource(dev->frag_shdr, 1, &fragment_shader, 0);
|
|
|
|
glCompileShader(dev->vert_shdr);
|
|
|
|
glCompileShader(dev->frag_shdr);
|
|
|
|
glGetShaderiv(dev->vert_shdr, GL_COMPILE_STATUS, &status);
|
|
|
|
assert(status == GL_TRUE);
|
|
|
|
glGetShaderiv(dev->frag_shdr, GL_COMPILE_STATUS, &status);
|
|
|
|
assert(status == GL_TRUE);
|
|
|
|
glAttachShader(dev->prog, dev->vert_shdr);
|
|
|
|
glAttachShader(dev->prog, dev->frag_shdr);
|
|
|
|
glLinkProgram(dev->prog);
|
|
|
|
glGetProgramiv(dev->prog, GL_LINK_STATUS, &status);
|
|
|
|
assert(status == GL_TRUE);
|
|
|
|
|
|
|
|
dev->uniform_tex = glGetUniformLocation(dev->prog, "Texture");
|
|
|
|
dev->uniform_proj = glGetUniformLocation(dev->prog, "ProjMtx");
|
|
|
|
dev->attrib_pos = glGetAttribLocation(dev->prog, "Position");
|
|
|
|
dev->attrib_uv = glGetAttribLocation(dev->prog, "TexCoord");
|
|
|
|
dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
|
|
|
|
|
|
|
|
{
|
|
|
|
/* buffer setup */
|
2016-09-02 16:33:20 +03:00
|
|
|
GLsizei vs = sizeof(struct nk_glfw_vertex);
|
|
|
|
size_t vp = offsetof(struct nk_glfw_vertex, position);
|
|
|
|
size_t vt = offsetof(struct nk_glfw_vertex, uv);
|
|
|
|
size_t vc = offsetof(struct nk_glfw_vertex, col);
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
glGenBuffers(1, &dev->vbo);
|
|
|
|
glGenBuffers(1, &dev->ebo);
|
|
|
|
glGenVertexArrays(1, &dev->vao);
|
|
|
|
|
|
|
|
glBindVertexArray(dev->vao);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray((GLuint)dev->attrib_pos);
|
|
|
|
glEnableVertexAttribArray((GLuint)dev->attrib_uv);
|
|
|
|
glEnableVertexAttribArray((GLuint)dev->attrib_col);
|
|
|
|
|
|
|
|
glVertexAttribPointer((GLuint)dev->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp);
|
|
|
|
glVertexAttribPointer((GLuint)dev->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt);
|
|
|
|
glVertexAttribPointer((GLuint)dev->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NK_INTERN void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_device_upload_atlas(struct nk_glfw* glfw, const void *image, int width, int height)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw_device *dev = &glfw->ogl;
|
2016-04-11 02:34:59 +03:00
|
|
|
glGenTextures(1, &dev->font_tex);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, dev->font_tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
|
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_device_destroy(struct nk_glfw* glfw)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw_device *dev = &glfw->ogl;
|
2016-04-11 02:34:59 +03:00
|
|
|
glDetachShader(dev->prog, dev->vert_shdr);
|
|
|
|
glDetachShader(dev->prog, dev->frag_shdr);
|
|
|
|
glDeleteShader(dev->vert_shdr);
|
|
|
|
glDeleteShader(dev->frag_shdr);
|
|
|
|
glDeleteProgram(dev->prog);
|
|
|
|
glDeleteTextures(1, &dev->font_tex);
|
|
|
|
glDeleteBuffers(1, &dev->vbo);
|
|
|
|
glDeleteBuffers(1, &dev->ebo);
|
|
|
|
nk_buffer_free(&dev->cmds);
|
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_render(struct nk_glfw* glfw, enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw_device *dev = &glfw->ogl;
|
2017-09-01 13:06:49 +03:00
|
|
|
struct nk_buffer vbuf, ebuf;
|
2016-04-11 02:34:59 +03:00
|
|
|
GLfloat ortho[4][4] = {
|
|
|
|
{2.0f, 0.0f, 0.0f, 0.0f},
|
|
|
|
{0.0f,-2.0f, 0.0f, 0.0f},
|
|
|
|
{0.0f, 0.0f,-1.0f, 0.0f},
|
|
|
|
{-1.0f,1.0f, 0.0f, 1.0f},
|
|
|
|
};
|
2020-04-30 16:17:24 +03:00
|
|
|
ortho[0][0] /= (GLfloat)glfw->width;
|
|
|
|
ortho[1][1] /= (GLfloat)glfw->height;
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
/* setup global state */
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendEquation(GL_FUNC_ADD);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
|
|
|
/* setup program */
|
|
|
|
glUseProgram(dev->prog);
|
|
|
|
glUniform1i(dev->uniform_tex, 0);
|
|
|
|
glUniformMatrix4fv(dev->uniform_proj, 1, GL_FALSE, &ortho[0][0]);
|
2020-04-30 16:17:24 +03:00
|
|
|
glViewport(0,0,(GLsizei)glfw->display_width,(GLsizei)glfw->display_height);
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
/* convert from command queue into draw list and draw to screen */
|
|
|
|
const struct nk_draw_command *cmd;
|
|
|
|
void *vertices, *elements;
|
2022-11-24 13:21:37 +03:00
|
|
|
nk_size offset = 0;
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
/* allocate vertex and element buffer */
|
|
|
|
glBindVertexArray(dev->vao);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, max_vertex_buffer, NULL, GL_STREAM_DRAW);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, max_element_buffer, NULL, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
/* load draw vertices & elements directly into vertex + element buffer */
|
|
|
|
vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
|
|
elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
|
|
{
|
2016-09-02 16:33:20 +03:00
|
|
|
/* fill convert configuration */
|
2016-04-11 02:34:59 +03:00
|
|
|
struct nk_convert_config config;
|
2016-09-02 16:33:20 +03:00
|
|
|
static const struct nk_draw_vertex_layout_element vertex_layout[] = {
|
|
|
|
{NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, position)},
|
|
|
|
{NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_glfw_vertex, uv)},
|
|
|
|
{NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_glfw_vertex, col)},
|
|
|
|
{NK_VERTEX_LAYOUT_END}
|
|
|
|
};
|
2021-05-16 23:54:56 +03:00
|
|
|
memset(&config, 0, sizeof(config));
|
2016-09-02 16:33:20 +03:00
|
|
|
config.vertex_layout = vertex_layout;
|
|
|
|
config.vertex_size = sizeof(struct nk_glfw_vertex);
|
|
|
|
config.vertex_alignment = NK_ALIGNOF(struct nk_glfw_vertex);
|
2022-07-31 00:45:12 +03:00
|
|
|
config.tex_null = dev->tex_null;
|
2016-04-11 02:34:59 +03:00
|
|
|
config.circle_segment_count = 22;
|
|
|
|
config.curve_segment_count = 22;
|
|
|
|
config.arc_segment_count = 22;
|
2016-09-02 16:33:20 +03:00
|
|
|
config.global_alpha = 1.0f;
|
|
|
|
config.shape_AA = AA;
|
|
|
|
config.line_AA = AA;
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
/* setup buffers to load vertices and elements */
|
|
|
|
nk_buffer_init_fixed(&vbuf, vertices, (size_t)max_vertex_buffer);
|
|
|
|
nk_buffer_init_fixed(&ebuf, elements, (size_t)max_element_buffer);
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_convert(&glfw->ctx, &dev->cmds, &vbuf, &ebuf, &config);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
|
|
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
|
|
|
|
|
|
|
|
/* iterate over and execute each draw command */
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_draw_foreach(cmd, &glfw->ctx, &dev->cmds)
|
2016-04-28 15:38:44 +03:00
|
|
|
{
|
2016-04-11 02:34:59 +03:00
|
|
|
if (!cmd->elem_count) continue;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
|
2016-04-28 15:38:44 +03:00
|
|
|
glScissor(
|
2020-04-30 16:17:24 +03:00
|
|
|
(GLint)(cmd->clip_rect.x * glfw->fb_scale.x),
|
|
|
|
(GLint)((glfw->height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h)) * glfw->fb_scale.y),
|
|
|
|
(GLint)(cmd->clip_rect.w * glfw->fb_scale.x),
|
|
|
|
(GLint)(cmd->clip_rect.h * glfw->fb_scale.y));
|
2022-11-24 13:21:37 +03:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, (const void*) offset);
|
|
|
|
offset += cmd->elem_count * sizeof(nk_draw_index);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_clear(&glfw->ctx);
|
2020-04-11 16:40:09 +03:00
|
|
|
nk_buffer_clear(&dev->cmds);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 19:24:43 +03:00
|
|
|
/* default OpenGL state */
|
|
|
|
glUseProgram(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
glDisable(GL_BLEND);
|
2016-04-11 02:34:59 +03:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
|
|
|
nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint)
|
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw* glfw = glfwGetWindowUserPointer(win);
|
|
|
|
if (glfw->text_len < NK_GLFW_TEXT_MAX)
|
|
|
|
glfw->text[glfw->text_len++] = codepoint;
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
|
|
|
nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
|
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_glfw* glfw = glfwGetWindowUserPointer(win);
|
|
|
|
(void)xoff;
|
|
|
|
glfw->scroll.x += (float)xoff;
|
|
|
|
glfw->scroll.y += (float)yoff;
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
2017-05-06 19:49:09 +03:00
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_mouse_button_callback(GLFWwindow* win, int button, int action, int mods)
|
2017-05-06 19:49:09 +03:00
|
|
|
{
|
2019-02-27 17:02:44 +03:00
|
|
|
struct nk_glfw* glfw = glfwGetWindowUserPointer(win);
|
2017-05-06 19:49:09 +03:00
|
|
|
double x, y;
|
2021-12-16 22:44:00 +03:00
|
|
|
NK_UNUSED(mods);
|
2017-05-06 19:49:09 +03:00
|
|
|
if (button != GLFW_MOUSE_BUTTON_LEFT) return;
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwGetCursorPos(win, &x, &y);
|
2017-05-06 19:49:09 +03:00
|
|
|
if (action == GLFW_PRESS) {
|
2020-04-30 16:17:24 +03:00
|
|
|
double dt = glfwGetTime() - glfw->last_button_click;
|
2017-08-13 10:55:07 +03:00
|
|
|
if (dt > NK_GLFW_DOUBLE_CLICK_LO && dt < NK_GLFW_DOUBLE_CLICK_HI) {
|
2020-04-30 16:17:24 +03:00
|
|
|
glfw->is_double_click_down = nk_true;
|
|
|
|
glfw->double_click_pos = nk_vec2((float)x, (float)y);
|
2017-08-13 10:55:07 +03:00
|
|
|
}
|
2020-04-30 16:17:24 +03:00
|
|
|
glfw->last_button_click = glfwGetTime();
|
|
|
|
} else glfw->is_double_click_down = nk_false;
|
2017-05-06 19:49:09 +03:00
|
|
|
}
|
|
|
|
|
2016-06-01 14:35:01 +03:00
|
|
|
NK_INTERN void
|
2018-09-15 07:52:10 +03:00
|
|
|
nk_glfw3_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2022-03-22 18:22:22 +03:00
|
|
|
struct nk_glfw* glfw = (struct nk_glfw*)usr.ptr;
|
2020-04-30 16:17:24 +03:00
|
|
|
const char *text = glfwGetClipboardString(glfw->win);
|
2016-04-11 02:34:59 +03:00
|
|
|
if (text) nk_textedit_paste(edit, text, nk_strlen(text));
|
|
|
|
(void)usr;
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:35:01 +03:00
|
|
|
NK_INTERN void
|
2018-09-15 07:52:10 +03:00
|
|
|
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int len)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2022-03-22 18:22:22 +03:00
|
|
|
struct nk_glfw* glfw = (struct nk_glfw*)usr.ptr;
|
2016-04-11 02:34:59 +03:00
|
|
|
char *str = 0;
|
|
|
|
if (!len) return;
|
2016-04-29 15:31:27 +03:00
|
|
|
str = (char*)malloc((size_t)len+1);
|
2016-04-11 02:34:59 +03:00
|
|
|
if (!str) return;
|
|
|
|
memcpy(str, text, (size_t)len);
|
|
|
|
str[len] = '\0';
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwSetClipboardString(glfw->win, str);
|
2016-04-11 02:34:59 +03:00
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
NK_API struct nk_context*
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_init(struct nk_glfw* glfw, GLFWwindow *win, enum nk_glfw_init_state init_state)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwSetWindowUserPointer(win, glfw);
|
|
|
|
glfw->win = win;
|
2016-04-11 02:34:59 +03:00
|
|
|
if (init_state == NK_GLFW3_INSTALL_CALLBACKS) {
|
|
|
|
glfwSetScrollCallback(win, nk_gflw3_scroll_callback);
|
|
|
|
glfwSetCharCallback(win, nk_glfw3_char_callback);
|
2017-05-06 19:49:09 +03:00
|
|
|
glfwSetMouseButtonCallback(win, nk_glfw3_mouse_button_callback);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_init_default(&glfw->ctx, 0);
|
|
|
|
glfw->ctx.clip.copy = nk_glfw3_clipboard_copy;
|
|
|
|
glfw->ctx.clip.paste = nk_glfw3_clipboard_paste;
|
2021-04-08 17:44:06 +03:00
|
|
|
glfw->ctx.clip.userdata = nk_handle_ptr(&glfw);
|
2020-04-30 16:17:24 +03:00
|
|
|
glfw->last_button_click = 0;
|
|
|
|
nk_glfw3_device_create(glfw);
|
2017-08-13 10:55:07 +03:00
|
|
|
|
2020-04-30 16:17:24 +03:00
|
|
|
glfw->is_double_click_down = nk_false;
|
|
|
|
glfw->double_click_pos = nk_vec2(0, 0);
|
2017-08-13 10:55:07 +03:00
|
|
|
|
2020-04-30 16:17:24 +03:00
|
|
|
return &glfw->ctx;
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_font_stash_begin(struct nk_glfw* glfw, struct nk_font_atlas **atlas)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_font_atlas_init_default(&glfw->atlas);
|
|
|
|
nk_font_atlas_begin(&glfw->atlas);
|
|
|
|
*atlas = &glfw->atlas;
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_font_stash_end(struct nk_glfw* glfw)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
const void *image; int w, h;
|
2020-04-30 16:17:24 +03:00
|
|
|
image = nk_font_atlas_bake(&glfw->atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
|
|
|
|
nk_glfw3_device_upload_atlas(glfw, image, w, h);
|
2022-07-31 00:43:46 +03:00
|
|
|
nk_font_atlas_end(&glfw->atlas, nk_handle_id((int)glfw->ogl.font_tex), &glfw->ogl.tex_null);
|
2020-04-30 16:17:24 +03:00
|
|
|
if (glfw->atlas.default_font)
|
|
|
|
nk_style_set_font(&glfw->ctx, &glfw->atlas.default_font->handle);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NK_API void
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_glfw3_new_frame(struct nk_glfw* glfw)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
double x, y;
|
2020-04-30 16:17:24 +03:00
|
|
|
struct nk_context *ctx = &glfw->ctx;
|
|
|
|
struct GLFWwindow *win = glfw->win;
|
2016-04-25 15:07:04 +03:00
|
|
|
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwGetWindowSize(win, &glfw->width, &glfw->height);
|
|
|
|
glfwGetFramebufferSize(win, &glfw->display_width, &glfw->display_height);
|
|
|
|
glfw->fb_scale.x = (float)glfw->display_width/(float)glfw->width;
|
|
|
|
glfw->fb_scale.y = (float)glfw->display_height/(float)glfw->height;
|
2016-04-25 15:07:04 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_begin(ctx);
|
2020-04-30 16:17:24 +03:00
|
|
|
for (i = 0; i < glfw->text_len; ++i)
|
|
|
|
nk_input_unicode(ctx, glfw->text[i]);
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2017-11-27 22:44:13 +03:00
|
|
|
#ifdef NK_GLFW_GL3_MOUSE_GRABBING
|
2016-05-23 15:48:31 +03:00
|
|
|
/* optional grabbing behavior */
|
|
|
|
if (ctx->input.mouse.grab)
|
|
|
|
glfwSetInputMode(glfw.win, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
|
|
|
else if (ctx->input.mouse.ungrab)
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwSetInputMode(glfw->win, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
2017-05-06 19:49:09 +03:00
|
|
|
#endif
|
2016-05-23 15:48:31 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_DEL, glfwGetKey(win, GLFW_KEY_DELETE) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_ENTER, glfwGetKey(win, GLFW_KEY_ENTER) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TAB, glfwGetKey(win, GLFW_KEY_TAB) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_BACKSPACE, glfwGetKey(win, GLFW_KEY_BACKSPACE) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_UP, glfwGetKey(win, GLFW_KEY_UP) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_DOWN, glfwGetKey(win, GLFW_KEY_DOWN) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
|
2016-06-16 21:53:30 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_SCROLL_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_SCROLL_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, glfwGetKey(win, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_SCROLL_UP, glfwGetKey(win, GLFW_KEY_PAGE_UP) == GLFW_PRESS);
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_SHIFT, glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS||
|
|
|
|
glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);
|
|
|
|
|
|
|
|
if (glfwGetKey(win, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
|
2016-06-06 12:25:16 +03:00
|
|
|
glfwGetKey(win, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_COPY, glfwGetKey(win, GLFW_KEY_C) == GLFW_PRESS);
|
2016-12-31 21:14:47 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_PASTE, glfwGetKey(win, GLFW_KEY_V) == GLFW_PRESS);
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_key(ctx, NK_KEY_CUT, glfwGetKey(win, GLFW_KEY_X) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_UNDO, glfwGetKey(win, GLFW_KEY_Z) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_REDO, glfwGetKey(win, GLFW_KEY_R) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, glfwGetKey(win, GLFW_KEY_B) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, glfwGetKey(win, GLFW_KEY_E) == GLFW_PRESS);
|
|
|
|
} else {
|
|
|
|
nk_input_key(ctx, NK_KEY_LEFT, glfwGetKey(win, GLFW_KEY_LEFT) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_RIGHT, glfwGetKey(win, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
|
|
|
nk_input_key(ctx, NK_KEY_COPY, 0);
|
|
|
|
nk_input_key(ctx, NK_KEY_PASTE, 0);
|
|
|
|
nk_input_key(ctx, NK_KEY_CUT, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwGetCursorPos(win, &x, &y);
|
|
|
|
nk_input_motion(ctx, (int)x, (int)y);
|
2017-11-27 22:44:13 +03:00
|
|
|
#ifdef NK_GLFW_GL3_MOUSE_GRABBING
|
2016-06-06 12:25:16 +03:00
|
|
|
if (ctx->input.mouse.grabbed) {
|
2020-04-30 16:17:24 +03:00
|
|
|
glfwSetCursorPos(glfw->win, ctx->input.mouse.prev.x, ctx->input.mouse.prev.y);
|
2016-06-06 12:25:16 +03:00
|
|
|
ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
|
|
|
|
ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
|
|
|
|
}
|
2017-05-06 19:49:09 +03:00
|
|
|
#endif
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_input_button(ctx, NK_BUTTON_LEFT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS);
|
|
|
|
nk_input_button(ctx, NK_BUTTON_MIDDLE, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS);
|
|
|
|
nk_input_button(ctx, NK_BUTTON_RIGHT, (int)x, (int)y, glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS);
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_input_button(ctx, NK_BUTTON_DOUBLE, (int)glfw->double_click_pos.x, (int)glfw->double_click_pos.y, glfw->is_double_click_down);
|
|
|
|
nk_input_scroll(ctx, glfw->scroll);
|
|
|
|
nk_input_end(&glfw->ctx);
|
|
|
|
glfw->text_len = 0;
|
|
|
|
glfw->scroll = nk_vec2(0,0);
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NK_API
|
2020-04-30 16:17:24 +03:00
|
|
|
void nk_glfw3_shutdown(struct nk_glfw* glfw)
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2020-04-30 16:17:24 +03:00
|
|
|
nk_font_atlas_clear(&glfw->atlas);
|
|
|
|
nk_free(&glfw->ctx);
|
|
|
|
nk_glfw3_device_destroy(glfw);
|
2020-06-12 09:03:38 +03:00
|
|
|
memset(glfw, 0, sizeof(*glfw));
|
2016-04-11 02:34:59 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 18:09:01 +03:00
|
|
|
#endif
|