Switch to using if statements instead of switch in event handling code

This commit is contained in:
Abdelrahman Said 2024-02-25 22:42:07 +00:00
parent 29bcfabda9
commit 547764764c
2 changed files with 19 additions and 29 deletions

View File

@ -88,16 +88,16 @@ i32 run_main_loop(void) {
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
&(comp.event)); &(comp.event));
switch (comp.event.type) { if (comp.event.type == SDL_QUIT) {
case SDL_QUIT:
comp.running = false; comp.running = false;
break; }
case SDL_WINDOWEVENT:
switch (comp.event.window.event) { if (comp.event.type == SDL_WINDOWEVENT) {
case SDL_WINDOWEVENT_CLOSE: if (comp.event.window.event == SDL_WINDOWEVENT_CLOSE) {
comp.running = false; comp.running = false;
break; }
case SDL_WINDOWEVENT_ENTER: {
if (comp.event.window.event == SDL_WINDOWEVENT_ENTER) {
u32 id = comp.event.window.windowID; u32 id = comp.event.window.windowID;
window *wnd = NULL; window *wnd = NULL;
@ -111,25 +111,18 @@ i32 run_main_loop(void) {
} }
} }
if (!wnd) { if (wnd) {
break; SDL_RaiseWindow(wnd->window);
} }
SDL_RaiseWindow(wnd->window);
break;
}
} }
}
break; if (comp.event.type == SDL_DROPFILE) {
case SDL_DROPFILE:
if (comp.event.drop.windowID == main_window->id) { if (comp.event.drop.windowID == main_window->id) {
node_data data = (node_data){.path = comp.event.drop.file}; node_data data = (node_data){.path = comp.event.drop.file};
add_node(&comp, NODE_TYPE_IO, data, IO_INPUT_COUNT, comp.ctx.mouse_x, add_node(&comp, NODE_TYPE_IO, data, IO_INPUT_COUNT, comp.ctx.mouse_x,
comp.ctx.mouse_y, io_node_colours); comp.ctx.mouse_y, io_node_colours);
break;
} }
} }
} }

View File

@ -26,34 +26,31 @@ void reset_ui_ctx(ui_ctx *ctx) {
} }
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) { void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
switch (event->type) { if (event->type == SDL_MOUSEMOTION) {
case SDL_MOUSEMOTION:
if (wnd->id == event->motion.windowID) { if (wnd->id == event->motion.windowID) {
ctx->mouse_x = event->motion.x; ctx->mouse_x = event->motion.x;
ctx->mouse_y = event->motion.y; ctx->mouse_y = event->motion.y;
ctx->rel_x += event->motion.xrel; ctx->rel_x += event->motion.xrel;
ctx->rel_y += event->motion.yrel; ctx->rel_y += event->motion.yrel;
ctx->wnd = wnd; ctx->wnd = wnd;
break;
} }
case SDL_MOUSEBUTTONDOWN: }
if (event->type == SDL_MOUSEBUTTONDOWN) {
if (wnd->id == event->button.windowID) { if (wnd->id == event->button.windowID) {
ctx->mouse_x = event->button.x; ctx->mouse_x = event->button.x;
ctx->mouse_y = event->button.y; ctx->mouse_y = event->button.y;
ctx->mouse_down = true; ctx->mouse_down = true;
ctx->wnd = wnd; ctx->wnd = wnd;
break;
} }
case SDL_MOUSEBUTTONUP: }
if (event->type == SDL_MOUSEBUTTONUP) {
if (wnd->id == event->button.windowID) { if (wnd->id == event->button.windowID) {
ctx->mouse_x = event->button.x; ctx->mouse_x = event->button.x;
ctx->mouse_y = event->button.y; ctx->mouse_y = event->button.y;
ctx->mouse_up = true; ctx->mouse_up = true;
ctx->wnd = wnd; ctx->wnd = wnd;
break;
} }
} }
} }