compositor-test/src/compositor.c

199 lines
4.8 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;
struct compositor {
window windows[MAX_WINDOWS];
u32 active_window;
SDL_Event event;
bool running;
i64 node_hovered;
u64 count;
node *nodes;
bool move_node;
ui_ctx ctx;
};
void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
ui_elem_colours colours);
i32 run_main_loop(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
return EXIT_FAILURE;
}
compositor comp = {0};
init_ui_ctx(&(comp.ctx));
comp.nodes = (node *)malloc(sizeof(node) * MAX_NODES);
window *main_window = &(comp.windows[0]);
window *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 bg_colour = {.abgr = 0xffffffff};
ui_elem_colours button_colours = (ui_elem_colours){
.fill = (colour){.abgr = 0xff89a83c},
.border = (colour){.abgr = 0xff768432},
};
ui_elem_colours io_node_colours = (ui_elem_colours){
.fill = (colour){.abgr = 0xff2c84b7},
.border = (colour){.abgr = 0xff315c89},
};
ui_elem_colours op_node_colours = (ui_elem_colours){
.fill = (colour){.abgr = 0xffad6c3a},
.border = (colour){.abgr = 0xff8e4a33},
};
i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
quad qd = (quad){
.p0 = (point){140, 40},
.p1 = (point){190, 40},
.p2 = (point){170, 200},
.p3 = (point){210, 200},
};
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 *wnd = NULL;
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
window *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 data = (node_data){.path = comp.event.drop.file};
add_node(&comp, NODEYPE_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 rect = {
.topleft.x = toolbox_button_x,
.topleft.y = i * (BUTTON_HEIGHT + 20) + 30,
.w = BUTTON_WIDTH,
.h = BUTTON_HEIGHT,
};
if (ui_button(toolbox, &(comp.ctx), rect, button_colours)) {
node_data data = (node_data){.func = ops[i]};
add_node(&comp, NODEYPE_OP, data, comp.ctx.mouse_x, comp.ctx.mouse_y,
op_node_colours);
}
}
for (u64 i = 0; i < comp.count; ++i) {
comp.nodes[i].rec = ui_node(main_window, &(comp.ctx), comp.nodes[i].rec,
comp.nodes[i].colours);
}
qd = ui_quad(main_window, &(comp.ctx), qd, op_node_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 *comp, node_type type, node_data data, i32 x, i32 y,
ui_elem_colours colours) {
if (comp->count + 1 >= MAX_NODES) {
return;
}
comp->nodes[(comp->count)++] = (node){
.rec =
(rect){
.topleft.x = x,
.topleft.y = y,
.w = NODE_WIDTH,
.h = NODE_HEIGHT,
},
.colours = colours,
.type = type,
.data.path = data.path,
};
}