compositor-test/src/compositor.c
Abdelrahman Said 63f119c1b2 Refactor UI code into an immediate mode style (#1)
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Reviewed-on: #1
2024-01-20 22:22:17 +00:00

190 lines
4.7 KiB
C

#include "aliases/aliases.h"
#include "nodes.h"
#include "ops.h"
#include "ui.h"
#include "window.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX_WINDOWS 2
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
typedef struct compositor compositor_t;
struct compositor {
window_t windows[MAX_WINDOWS];
u32 active_window;
SDL_Event event;
bool running;
i64 node_hovered;
u64 count;
node_t *nodes;
bool move_node;
ui_ctx_t ctx;
};
void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
i32 y, ui_elem_colours_t colours);
i32 run_main_loop(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
return EXIT_FAILURE;
}
compositor_t comp = {0};
init_ui_ctx(&(comp.ctx));
comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
window_t *main_window = &(comp.windows[0]);
window_t *toolbox = &(comp.windows[1]);
if (!init_window(main_window, "Compositor", WINDOW_WIDTH, WINDOW_HEIGHT, -1,
-1)) {
SDL_Quit();
return EXIT_FAILURE;
}
u32 toolbox_window_width = WINDOW_WIDTH / 7;
init_window(toolbox, "Toolbox", toolbox_window_width, WINDOW_HEIGHT,
main_window->x - toolbox_window_width, -1);
comp.running = true;
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
colour_t bg_colour = {.abgr = 0xffffffff};
ui_elem_colours_t button_colours = (ui_elem_colours_t){
.fill = (colour_t){.abgr = 0xff89a83c},
.border = (colour_t){.abgr = 0xff768432},
};
ui_elem_colours_t io_node_colours = (ui_elem_colours_t){
.fill = (colour_t){.abgr = 0xff2c84b7},
.border = (colour_t){.abgr = 0xff315c89},
};
ui_elem_colours_t op_node_colours = (ui_elem_colours_t){
.fill = (colour_t){.abgr = 0xffad6c3a},
.border = (colour_t){.abgr = 0xff8e4a33},
};
i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
while (comp.running) {
while (SDL_PollEvent(&(comp.event))) {
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
&(comp.event));
switch (comp.event.type) {
case SDL_QUIT:
comp.running = false;
break;
case SDL_WINDOWEVENT:
switch (comp.event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
comp.running = false;
break;
case SDL_WINDOWEVENT_ENTER: {
u32 id = comp.event.window.windowID;
window_t *wnd = NULL;
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
window_t *window = &(comp.windows[i]);
if (id == window->id) {
comp.active_window = id;
wnd = window;
break;
}
}
if (!wnd) {
break;
}
SDL_RaiseWindow(wnd->window);
break;
}
}
break;
case SDL_DROPFILE:
if (comp.event.drop.windowID == main_window->id) {
node_data_t data = (node_data_t){.path = comp.event.drop.file};
add_node(&comp, NODE_TYPE_IO, data, comp.ctx.mouse_x,
comp.ctx.mouse_y, io_node_colours);
break;
}
}
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
clear_window(&(comp.windows[i]), bg_colour);
}
for (u64 i = 0; i < COUNT_COMP_OPS; ++i) {
rect_t rect = {
.topleft.x = toolbox_button_x,
.topleft.y = i * (BUTTON_HEIGHT + 20) + 30,
.w = BUTTON_WIDTH,
.h = BUTTON_HEIGHT,
};
if (button(toolbox, &(comp.ctx), rect, button_colours)) {
node_data_t data = (node_data_t){.func = ops[i]};
add_node(&comp, NODE_TYPE_OP, data, comp.ctx.mouse_x, comp.ctx.mouse_y,
op_node_colours);
}
}
for (u64 i = 0; i < comp.count; ++i) {
comp.nodes[i].rect = node(main_window, &(comp.ctx), comp.nodes[i].rect,
comp.nodes[i].colours);
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i]));
}
reset_ui_ctx(&(comp.ctx));
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
cleanup_window(&(comp.windows[i]));
}
SDL_Quit();
return EXIT_SUCCESS;
}
void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
i32 y, ui_elem_colours_t colours) {
if (comp->count + 1 >= MAX_NODES) {
return;
}
comp->nodes[(comp->count)++] = (node_t){
.rect =
(rect_t){
.topleft.x = x,
.topleft.y = y,
.w = NODE_WIDTH,
.h = NODE_HEIGHT,
},
.colours = colours,
.type = type,
.data.path = data.path,
};
}