Skip to content

Commit

Permalink
Merge pull request #609 from paccerdk/FixDemosGrab
Browse files Browse the repository at this point in the history
Fix missed grab state changes in SDL demos
  • Loading branch information
RobLoach authored Mar 16, 2024
2 parents 8e5c9f7 + 56ab9d9 commit deb529f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 33 deletions.
1 change: 1 addition & 0 deletions demo/sdl_opengl2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
21 changes: 13 additions & 8 deletions demo/sdl_opengl2/nuklear_sdl_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,26 @@ nk_sdl_font_stash_end(void)
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
}

NK_API int
nk_sdl_handle_event(SDL_Event *evt)
NK_API void
nk_sdl_handle_grab(void)
{
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;
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
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;

switch(evt->type)
{
Expand Down
4 changes: 3 additions & 1 deletion demo/sdl_opengl3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 13 additions & 8 deletions demo/sdl_opengl3/nuklear_sdl_gl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,26 @@ nk_sdl_font_stash_end(void)

}

NK_API int
nk_sdl_handle_event(SDL_Event *evt)
NK_API void
nk_sdl_handle_grab(void)
{
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;
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
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;

switch(evt->type)
{
Expand Down
1 change: 1 addition & 0 deletions demo/sdl_opengles2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
21 changes: 13 additions & 8 deletions demo/sdl_opengles2/nuklear_sdl_gles2.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,26 @@ nk_sdl_font_stash_end(void)

}

NK_API int
nk_sdl_handle_event(SDL_Event *evt)
NK_API void
nk_sdl_handle_grab(void)
{
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;
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
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;

switch(evt->type)
{
Expand Down
1 change: 1 addition & 0 deletions demo/sdl_renderer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
21 changes: 13 additions & 8 deletions demo/sdl_renderer/nuklear_sdl_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,26 @@ nk_sdl_font_stash_end(void)
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
}

NK_API int
nk_sdl_handle_event(SDL_Event *evt)
NK_API void
nk_sdl_handle_grab(void)
{
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;
/* better support for older SDL by setting mode first; causes an extra mouse motion event */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_WarpMouseInWindow(sdl.win, x, y);
ctx->input.mouse.ungrab = 0;
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;

switch(evt->type)
{
Expand Down

0 comments on commit deb529f

Please sign in to comment.