diff --git a/demo/sdl_opengl2/main.c b/demo/sdl_opengl2/main.c index 45c51d2..438f2d3 100644 --- a/demo/sdl_opengl2/main.c +++ b/demo/sdl_opengl2/main.c @@ -139,6 +139,7 @@ main(int argc, char *argv[]) if (evt.type == SDL_QUIT) goto cleanup; nk_sdl_handle_event(&evt); } + nk_sdl_handle_grab(); /* optional grabbing behavior */ nk_input_end(ctx); /* GUI */ diff --git a/demo/sdl_opengl2/nuklear_sdl_gl2.h b/demo/sdl_opengl2/nuklear_sdl_gl2.h index 819723c..fbc7ec7 100644 --- a/demo/sdl_opengl2/nuklear_sdl_gl2.h +++ b/demo/sdl_opengl2/nuklear_sdl_gl2.h @@ -233,22 +233,27 @@ nk_sdl_font_stash_end(void) nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); } +NK_API void +nk_sdl_handle_grab(void) +{ + struct nk_context *ctx = &sdl.ctx; + if (ctx->input.mouse.grab) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } else if (ctx->input.mouse.ungrab) { + /* better support for older SDL by setting mode first; causes an extra mouse motion event */ + SDL_SetRelativeMouseMode(SDL_FALSE); + SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + } else if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + } +} + NK_API int nk_sdl_handle_event(SDL_Event *evt) { struct nk_context *ctx = &sdl.ctx; - /* optional grabbing behavior */ - if (ctx->input.mouse.grab) { - SDL_SetRelativeMouseMode(SDL_TRUE); - ctx->input.mouse.grab = 0; - } else if (ctx->input.mouse.ungrab) { - int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; - SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, x, y); - ctx->input.mouse.ungrab = 0; - } - switch(evt->type) { case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ diff --git a/demo/sdl_opengl3/main.c b/demo/sdl_opengl3/main.c index e039a2f..44c8c74 100644 --- a/demo/sdl_opengl3/main.c +++ b/demo/sdl_opengl3/main.c @@ -149,7 +149,9 @@ int main(int argc, char *argv[]) while (SDL_PollEvent(&evt)) { if (evt.type == SDL_QUIT) goto cleanup; nk_sdl_handle_event(&evt); - } nk_input_end(ctx); + } + nk_sdl_handle_grab(); /* optional grabbing behavior */ + nk_input_end(ctx); /* GUI */ if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), diff --git a/demo/sdl_opengl3/nuklear_sdl_gl3.h b/demo/sdl_opengl3/nuklear_sdl_gl3.h index 4e7df6b..6c9d4b1 100644 --- a/demo/sdl_opengl3/nuklear_sdl_gl3.h +++ b/demo/sdl_opengl3/nuklear_sdl_gl3.h @@ -342,22 +342,27 @@ nk_sdl_font_stash_end(void) } +NK_API void +nk_sdl_handle_grab(void) +{ + struct nk_context *ctx = &sdl.ctx; + if (ctx->input.mouse.grab) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } else if (ctx->input.mouse.ungrab) { + /* better support for older SDL by setting mode first; causes an extra mouse motion event */ + SDL_SetRelativeMouseMode(SDL_FALSE); + SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + } else if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + } +} + NK_API int nk_sdl_handle_event(SDL_Event *evt) { struct nk_context *ctx = &sdl.ctx; - /* optional grabbing behavior */ - if (ctx->input.mouse.grab) { - SDL_SetRelativeMouseMode(SDL_TRUE); - ctx->input.mouse.grab = 0; - } else if (ctx->input.mouse.ungrab) { - int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; - SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, x, y); - ctx->input.mouse.ungrab = 0; - } - switch(evt->type) { case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ diff --git a/demo/sdl_opengles2/main.c b/demo/sdl_opengles2/main.c index b114159..9ef90a7 100644 --- a/demo/sdl_opengles2/main.c +++ b/demo/sdl_opengles2/main.c @@ -92,6 +92,7 @@ MainLoop(void* loopArg){ if (evt.type == SDL_QUIT) running = nk_false; nk_sdl_handle_event(&evt); } + nk_sdl_handle_grab(); /* optional grabbing behavior */ nk_input_end(ctx); diff --git a/demo/sdl_opengles2/nuklear_sdl_gles2.h b/demo/sdl_opengles2/nuklear_sdl_gles2.h index 39a444d..6f46d52 100644 --- a/demo/sdl_opengles2/nuklear_sdl_gles2.h +++ b/demo/sdl_opengles2/nuklear_sdl_gles2.h @@ -342,22 +342,27 @@ nk_sdl_font_stash_end(void) } +NK_API void +nk_sdl_handle_grab(void) +{ + struct nk_context *ctx = &sdl.ctx; + if (ctx->input.mouse.grab) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } else if (ctx->input.mouse.ungrab) { + /* better support for older SDL by setting mode first; causes an extra mouse motion event */ + SDL_SetRelativeMouseMode(SDL_FALSE); + SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + } else if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + } +} + NK_API int nk_sdl_handle_event(SDL_Event *evt) { struct nk_context *ctx = &sdl.ctx; - /* optional grabbing behavior */ - if (ctx->input.mouse.grab) { - SDL_SetRelativeMouseMode(SDL_TRUE); - ctx->input.mouse.grab = 0; - } else if (ctx->input.mouse.ungrab) { - int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; - SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, x, y); - ctx->input.mouse.ungrab = 0; - } - switch(evt->type) { case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ diff --git a/demo/sdl_renderer/main.c b/demo/sdl_renderer/main.c index 940ea84..a4a0f28 100644 --- a/demo/sdl_renderer/main.c +++ b/demo/sdl_renderer/main.c @@ -177,6 +177,7 @@ main(int argc, char *argv[]) if (evt.type == SDL_QUIT) goto cleanup; nk_sdl_handle_event(&evt); } + nk_sdl_handle_grab(); /* optional grabbing behavior */ nk_input_end(ctx); /* GUI */ diff --git a/demo/sdl_renderer/nuklear_sdl_renderer.h b/demo/sdl_renderer/nuklear_sdl_renderer.h index fbea3cf..fb8596f 100644 --- a/demo/sdl_renderer/nuklear_sdl_renderer.h +++ b/demo/sdl_renderer/nuklear_sdl_renderer.h @@ -264,22 +264,27 @@ nk_sdl_font_stash_end(void) nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); } +NK_API void +nk_sdl_handle_grab(void) +{ + struct nk_context *ctx = &sdl.ctx; + if (ctx->input.mouse.grab) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } else if (ctx->input.mouse.ungrab) { + /* better support for older SDL by setting mode first; causes an extra mouse motion event */ + SDL_SetRelativeMouseMode(SDL_FALSE); + SDL_WarpMouseInWindow(sdl.win, (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y); + } else if (ctx->input.mouse.grabbed) { + ctx->input.mouse.pos.x = ctx->input.mouse.prev.x; + ctx->input.mouse.pos.y = ctx->input.mouse.prev.y; + } +} + NK_API int nk_sdl_handle_event(SDL_Event *evt) { struct nk_context *ctx = &sdl.ctx; - /* optional grabbing behavior */ - if (ctx->input.mouse.grab) { - SDL_SetRelativeMouseMode(SDL_TRUE); - ctx->input.mouse.grab = 0; - } else if (ctx->input.mouse.ungrab) { - int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; - SDL_SetRelativeMouseMode(SDL_FALSE); - SDL_WarpMouseInWindow(sdl.win, x, y); - ctx->input.mouse.ungrab = 0; - } - switch(evt->type) { case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */