Favor switch over if-else chain in event handler.

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

View File

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