From 547764764c323f2b42ec1a32cc51e374c3155b3f Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 25 Feb 2024 22:42:07 +0000 Subject: [PATCH] Switch to using if statements instead of switch in event handling code --- src/compositor.c | 31 ++++++++++++------------------- src/ui.c | 17 +++++++---------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/compositor.c b/src/compositor.c index 4cf23ac..2032c48 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -88,16 +88,16 @@ i32 run_main_loop(void) { handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), &(comp.event)); - switch (comp.event.type) { - case SDL_QUIT: + if (comp.event.type == SDL_QUIT) { comp.running = false; - break; - case SDL_WINDOWEVENT: - switch (comp.event.window.event) { - case SDL_WINDOWEVENT_CLOSE: + } + + if (comp.event.type == SDL_WINDOWEVENT) { + if (comp.event.window.event == SDL_WINDOWEVENT_CLOSE) { comp.running = false; - break; - case SDL_WINDOWEVENT_ENTER: { + } + + if (comp.event.window.event == SDL_WINDOWEVENT_ENTER) { u32 id = comp.event.window.windowID; window *wnd = NULL; @@ -111,25 +111,18 @@ i32 run_main_loop(void) { } } - if (!wnd) { - break; + if (wnd) { + SDL_RaiseWindow(wnd->window); } - - SDL_RaiseWindow(wnd->window); - - break; - } } + } - break; - case SDL_DROPFILE: + if (comp.event.type == SDL_DROPFILE) { if (comp.event.drop.windowID == main_window->id) { node_data data = (node_data){.path = comp.event.drop.file}; add_node(&comp, NODE_TYPE_IO, data, IO_INPUT_COUNT, comp.ctx.mouse_x, comp.ctx.mouse_y, io_node_colours); - - break; } } } diff --git a/src/ui.c b/src/ui.c index ebb095a..0eb0099 100644 --- a/src/ui.c +++ b/src/ui.c @@ -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) { - switch (event->type) { - case SDL_MOUSEMOTION: + if (event->type == SDL_MOUSEMOTION) { if (wnd->id == event->motion.windowID) { ctx->mouse_x = event->motion.x; ctx->mouse_y = event->motion.y; ctx->rel_x += event->motion.xrel; ctx->rel_y += event->motion.yrel; ctx->wnd = wnd; - - break; } - case SDL_MOUSEBUTTONDOWN: + } + + if (event->type == SDL_MOUSEBUTTONDOWN) { if (wnd->id == event->button.windowID) { ctx->mouse_x = event->button.x; ctx->mouse_y = event->button.y; ctx->mouse_down = true; ctx->wnd = wnd; - - break; } - case SDL_MOUSEBUTTONUP: + } + + if (event->type == SDL_MOUSEBUTTONUP) { if (wnd->id == event->button.windowID) { ctx->mouse_x = event->button.x; ctx->mouse_y = event->button.y; ctx->mouse_up = true; ctx->wnd = wnd; - - break; } } }