Merge pull request #609 from paccerdk/FixDemosGrab

Fix missed grab state changes in SDL demos
This commit is contained in:
Rob Loach 2024-03-16 13:48:34 -04:00 committed by GitHub
commit deb529f5dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 70 additions and 45 deletions

View File

@ -139,6 +139,7 @@ main(int argc, char *argv[])
if (evt.type == SDL_QUIT) goto cleanup; if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt); nk_sdl_handle_event(&evt);
} }
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx); nk_input_end(ctx);
/* GUI */ /* GUI */

View File

@ -233,22 +233,27 @@ nk_sdl_font_stash_end(void)
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); 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_API int
nk_sdl_handle_event(SDL_Event *evt) nk_sdl_handle_event(SDL_Event *evt)
{ {
struct nk_context *ctx = &sdl.ctx; 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) switch(evt->type)
{ {
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -149,7 +149,9 @@ int main(int argc, char *argv[])
while (SDL_PollEvent(&evt)) { while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_QUIT) goto cleanup; if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt); nk_sdl_handle_event(&evt);
} nk_input_end(ctx); }
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx);
/* GUI */ /* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),

View File

@ -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_API int
nk_sdl_handle_event(SDL_Event *evt) nk_sdl_handle_event(SDL_Event *evt)
{ {
struct nk_context *ctx = &sdl.ctx; 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) switch(evt->type)
{ {
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -92,6 +92,7 @@ MainLoop(void* loopArg){
if (evt.type == SDL_QUIT) running = nk_false; if (evt.type == SDL_QUIT) running = nk_false;
nk_sdl_handle_event(&evt); nk_sdl_handle_event(&evt);
} }
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx); nk_input_end(ctx);

View File

@ -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_API int
nk_sdl_handle_event(SDL_Event *evt) nk_sdl_handle_event(SDL_Event *evt)
{ {
struct nk_context *ctx = &sdl.ctx; 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) switch(evt->type)
{ {
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */

View File

@ -177,6 +177,7 @@ main(int argc, char *argv[])
if (evt.type == SDL_QUIT) goto cleanup; if (evt.type == SDL_QUIT) goto cleanup;
nk_sdl_handle_event(&evt); nk_sdl_handle_event(&evt);
} }
nk_sdl_handle_grab(); /* optional grabbing behavior */
nk_input_end(ctx); nk_input_end(ctx);
/* GUI */ /* GUI */

View File

@ -264,22 +264,27 @@ nk_sdl_font_stash_end(void)
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle); 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_API int
nk_sdl_handle_event(SDL_Event *evt) nk_sdl_handle_event(SDL_Event *evt)
{ {
struct nk_context *ctx = &sdl.ctx; 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) switch(evt->type)
{ {
case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */ case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */