Favor switch over if-else chain in event handler.

This commit is contained in:
lib-omar 2022-01-29 14:18:07 -05:00 committed by GitHub
parent 43b5e82748
commit 811dbacd12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -344,89 +344,98 @@ 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;
if (evt->type == SDL_KEYUP || evt->type == SDL_KEYDOWN) {
/* key events */ /* optional grabbing behavior */
int down = evt->type == SDL_KEYDOWN; if (ctx->input.mouse.grab) {
const Uint8* state = SDL_GetKeyboardState(0); SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_Keycode sym = evt->key.keysym.sym; ctx->input.mouse.grab = 0;
if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) } else if (ctx->input.mouse.ungrab) {
nk_input_key(ctx, NK_KEY_SHIFT, down); int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
else if (sym == SDLK_DELETE) SDL_SetRelativeMouseMode(SDL_FALSE);
nk_input_key(ctx, NK_KEY_DEL, down); SDL_WarpMouseInWindow(sdl.win, x, y);
else if (sym == SDLK_RETURN) ctx->input.mouse.ungrab = 0;
nk_input_key(ctx, NK_KEY_ENTER, down); }
else if (sym == SDLK_TAB)
nk_input_key(ctx, NK_KEY_TAB, down); switch(evt->type)
else if (sym == SDLK_BACKSPACE) {
nk_input_key(ctx, NK_KEY_BACKSPACE, down); case SDL_KEYUP: /* KEYUP & KEYDOWN share same routine */
else if (sym == SDLK_HOME) { case SDL_KEYDOWN:
nk_input_key(ctx, NK_KEY_TEXT_START, down); {
nk_input_key(ctx, NK_KEY_SCROLL_START, down); int down = evt->type == SDL_KEYDOWN;
} else if (sym == SDLK_END) { const Uint8* state = SDL_GetKeyboardState(0);
nk_input_key(ctx, NK_KEY_TEXT_END, down); switch(evt->key.keysym.sym)
nk_input_key(ctx, NK_KEY_SCROLL_END, down); {
} else if (sym == SDLK_PAGEDOWN) { case SDLK_RSHIFT: /* RSHIFT & LSHIFT share same routine */
nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); case SDLK_LSHIFT: nk_input_key(ctx, NK_KEY_SHIFT, down); break;
} else if (sym == SDLK_PAGEUP) { case SDLK_DELETE: nk_input_key(ctx, NK_KEY_DEL, down); break;
nk_input_key(ctx, NK_KEY_SCROLL_UP, down); case SDLK_RETURN: nk_input_key(ctx, NK_KEY_ENTER, down); break;
} else if (sym == SDLK_z) case SDLK_TAB: nk_input_key(ctx, NK_KEY_TAB, down); break;
nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); case SDLK_BACKSPACE: nk_input_key(ctx, NK_KEY_BACKSPACE, down); break;
else if (sym == SDLK_r) case SDLK_HOME: nk_input_key(ctx, NK_KEY_TEXT_START, down);
nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); nk_input_key(ctx, NK_KEY_SCROLL_START, down); break;
else if (sym == SDLK_c) case SDLK_END: nk_input_key(ctx, NK_KEY_TEXT_END, down);
nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); nk_input_key(ctx, NK_KEY_SCROLL_END, down); break;
else if (sym == SDLK_v) case SDLK_PAGEDOWN: nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down); break;
nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); case SDLK_PAGEUP: nk_input_key(ctx, NK_KEY_SCROLL_UP, down); break;
else if (sym == SDLK_x) case SDLK_z: nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && state[SDL_SCANCODE_LCTRL]); break;
nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); case SDLK_r: nk_input_key(ctx, NK_KEY_TEXT_REDO, down && state[SDL_SCANCODE_LCTRL]); break;
else if (sym == SDLK_b) case SDLK_c: nk_input_key(ctx, NK_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); break;
nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); case SDLK_v: nk_input_key(ctx, NK_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); break;
else if (sym == SDLK_e) case SDLK_x: nk_input_key(ctx, NK_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); break;
nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); case SDLK_b: nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && state[SDL_SCANCODE_LCTRL]); break;
else if (sym == SDLK_UP) case SDLK_e: nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && state[SDL_SCANCODE_LCTRL]); break;
nk_input_key(ctx, NK_KEY_UP, down); case SDLK_UP: nk_input_key(ctx, NK_KEY_UP, down); break;
else if (sym == SDLK_DOWN) case SDLK_DOWN: nk_input_key(ctx, NK_KEY_DOWN, down); break;
nk_input_key(ctx, NK_KEY_DOWN, down); case SDLK_LEFT:
else if (sym == SDLK_LEFT) { if (state[SDL_SCANCODE_LCTRL])
if (state[SDL_SCANCODE_LCTRL]) nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); else nk_input_key(ctx, NK_KEY_LEFT, down);
else nk_input_key(ctx, NK_KEY_LEFT, down); break;
} else if (sym == SDLK_RIGHT) { case SDLK_RIGHT:
if (state[SDL_SCANCODE_LCTRL]) if (state[SDL_SCANCODE_LCTRL])
nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
else nk_input_key(ctx, NK_KEY_RIGHT, down); else nk_input_key(ctx, NK_KEY_RIGHT, down);
} else return 0; break;
return 1; }
} else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { }
/* mouse button */ return 1;
int down = evt->type == SDL_MOUSEBUTTONDOWN;
const int x = evt->button.x, y = evt->button.y; case SDL_MOUSEBUTTONUP: /* MOUSEBUTTONUP & MOUSEBUTTONDOWN share same routine */
if (evt->button.button == SDL_BUTTON_LEFT) { case SDL_MOUSEBUTTONDOWN:
if (evt->button.clicks > 1) {
nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down); int down = evt->type == SDL_MOUSEBUTTONDOWN;
nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); const int x = evt->button.x, y = evt->button.y;
} else if (evt->button.button == SDL_BUTTON_MIDDLE) switch(evt->button.button)
nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); {
else if (evt->button.button == SDL_BUTTON_RIGHT) case SDL_BUTTON_LEFT:
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); if (evt->button.clicks > 1)
return 1; nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, down);
} else if (evt->type == SDL_MOUSEMOTION) { nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); break;
/* mouse motion */ case SDL_BUTTON_MIDDLE: nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); break;
if (ctx->input.mouse.grabbed) { case SDL_BUTTON_RIGHT: nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down); break;
int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y; }
nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel); }
} else nk_input_motion(ctx, evt->motion.x, evt->motion.y); return 1;
return 1;
} else if (evt->type == SDL_TEXTINPUT) { case SDL_MOUSEMOTION:
/* text input */ if (ctx->input.mouse.grabbed) {
nk_glyph glyph; int x = (int)ctx->input.mouse.prev.x, y = (int)ctx->input.mouse.prev.y;
memcpy(glyph, evt->text.text, NK_UTF_SIZE); nk_input_motion(ctx, x + evt->motion.xrel, y + evt->motion.yrel);
nk_input_glyph(ctx, glyph); }
return 1; else nk_input_motion(ctx, evt->motion.x, evt->motion.y);
} else if (evt->type == SDL_MOUSEWHEEL) { return 1;
/* mouse wheel */
nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y)); case SDL_TEXTINPUT:
return 1; {
nk_glyph glyph;
memcpy(glyph, evt->text.text, NK_UTF_SIZE);
nk_input_glyph(ctx, glyph);
}
return 1;
case SDL_MOUSEWHEEL:
nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y));
return 1;
} }
return 0; return 0;
} }