Compare commits

...

4 Commits

5 changed files with 68 additions and 189 deletions

View File

@ -2,6 +2,7 @@
#define NODES_H #define NODES_H
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "ui.h"
#include "window.h" #include "window.h"
#define MAX_NODES 1024 #define MAX_NODES 1024
@ -24,6 +25,8 @@ union node_data {
}; };
struct node { struct node {
rect_t rect;
ui_elem_colours_t colours;
node_type_t type; node_type_t type;
node_data_t data; node_data_t data;
}; };

View File

@ -31,6 +31,12 @@ struct ui_elem {
ui_elem_type_t type; ui_elem_type_t type;
}; };
typedef struct ui_elem_colours ui_elem_colours_t;
struct ui_elem_colours {
colour_t fill;
colour_t border;
};
struct ui_ctx { struct ui_ctx {
u64 count; u64 count;
i64 hovered; i64 hovered;
@ -49,7 +55,9 @@ void init_ui_ctx(ui_ctx_t *ctx);
void reset_ui_ctx(ui_ctx_t *ctx); void reset_ui_ctx(ui_ctx_t *ctx);
void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx, void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
const SDL_Event *event); const SDL_Event *event);
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect); ui_elem_colours_t colours);
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
ui_elem_colours_t colours);
#endif // !UI_H #endif // !UI_H

View File

@ -21,10 +21,6 @@ struct compositor {
u32 active_window; u32 active_window;
SDL_Event event; SDL_Event event;
bool running; bool running;
u64 mouse_x;
u64 mouse_y;
u64 last_clicked_mouse_x;
u64 last_clicked_mouse_y;
i64 node_hovered; i64 node_hovered;
u64 count; u64 count;
node_t *nodes; node_t *nodes;
@ -33,7 +29,7 @@ struct compositor {
}; };
void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
i32 y); i32 y, ui_elem_colours_t colours);
i32 run_main_loop(void) { i32 run_main_loop(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@ -65,6 +61,20 @@ i32 run_main_loop(void) {
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
colour_t bg_colour = {.abgr = 0xffffffff}; 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 (comp.running) {
while (SDL_PollEvent(&(comp.event))) { while (SDL_PollEvent(&(comp.event))) {
@ -105,6 +115,15 @@ i32 run_main_loop(void) {
} }
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;
}
} }
} }
@ -112,6 +131,27 @@ i32 run_main_loop(void) {
clear_window(&(comp.windows[i]), bg_colour); 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) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i])); swap_buffers(&(comp.windows[i]));
} }
@ -119,156 +159,6 @@ i32 run_main_loop(void) {
reset_ui_ctx(&(comp.ctx)); reset_ui_ctx(&(comp.ctx));
} }
#if 0
i32 button_x = (toolbox->width - BUTTON_WIDTH) / 2;
for (u64 i = 0; i < COUNT_COMP_OPS; ++i) {
comp.buttons[i] = (button_t){.rect = (rect_t){
.topleft.x = button_x,
.topleft.y = i * (BUTTON_HEIGHT + 20) + 30,
.w = BUTTON_WIDTH,
.h = BUTTON_HEIGHT,
}};
}
while (comp.running) {
while (SDL_PollEvent(&(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) {
wnd = window;
break;
}
}
if (!wnd) {
break;
}
SDL_RaiseWindow(wnd->window);
break;
}
}
break;
case SDL_MOUSEBUTTONDOWN:
if (comp.event.button.windowID == main_window->id) {
if (comp.node_hovered != -1) {
comp.move_node = true;
}
comp.last_clicked_mouse_x = comp.event.button.x;
comp.last_clicked_mouse_y = comp.event.button.y;
break;
} else if (comp.event.button.windowID == toolbox->id) {
if (comp.button_hovered != -1) {
comp.button_clicked = comp.button_hovered;
}
break;
}
case SDL_MOUSEBUTTONUP:
comp.move_node = false;
if (comp.event.button.windowID == toolbox->id) {
if (comp.button_hovered >= 0 &&
comp.button_hovered == comp.button_clicked) {
add_node(&comp, NODE_TYPE_OP,
(node_data_t){.func = ops[comp.button_hovered]},
comp.last_clicked_mouse_x, comp.last_clicked_mouse_y);
}
comp.button_clicked = -1;
}
break;
case SDL_MOUSEMOTION:
comp.button_hovered = -1;
if (comp.event.motion.windowID == main_window->id) {
comp.mouse_x = comp.event.motion.x;
comp.mouse_y = comp.event.motion.y;
if (comp.move_node) {
i32 dx = comp.event.motion.xrel;
i32 dy = comp.event.motion.yrel;
node_t *node = &(comp.nodes[comp.node_hovered]);
node->rect.topleft.x += dx;
node->rect.topleft.y += dy;
} else {
comp.node_hovered = -1;
for (u64 i = comp.count - 1; i >= 0; --i) {
rect_t *rect = &(comp.nodes[i].rect);
if (aabb(rect, comp.mouse_x, comp.mouse_y)) {
comp.node_hovered = i;
break;
}
}
}
break;
} else if (comp.event.motion.windowID == toolbox->id) {
for (u64 i = COUNT_COMP_OPS - 1; i >= 0; --i) {
rect_t *rect = &(comp.buttons[i].rect);
if (aabb(rect, comp.event.motion.x, comp.event.motion.y)) {
comp.button_hovered = i;
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.mouse_x, comp.mouse_y);
break;
}
}
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
clear_window(&(comp.windows[i]), bg_colour);
}
for (u64 i = 0; i < comp.count; ++i) {
node_t *node = &(comp.nodes[i]);
draw_node(main_window, node);
}
for (u64 i = 0; i < COUNT_COMP_OPS; ++i) {
button_t *button = &(comp.buttons[i]);
draw_button(toolbox, button);
}
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i]));
}
}
#endif
for (u64 i = 0; i < MAX_WINDOWS; ++i) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
cleanup_window(&(comp.windows[i])); cleanup_window(&(comp.windows[i]));
} }
@ -278,9 +168,8 @@ i32 run_main_loop(void) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#if 0
void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x, void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
i32 y) { i32 y, ui_elem_colours_t colours) {
if (comp->count + 1 >= MAX_NODES) { if (comp->count + 1 >= MAX_NODES) {
return; return;
} }
@ -293,8 +182,8 @@ void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
.w = NODE_WIDTH, .w = NODE_WIDTH,
.h = NODE_HEIGHT, .h = NODE_HEIGHT,
}, },
.colours = colours,
.type = type, .type = type,
.data.path = data.path, .data.path = data.path,
}; };
} }
#endif

View File

@ -1,4 +0,0 @@
#include "nodes.h"
#include "aliases/aliases.h"
#include "window.h"
#include <stdbool.h>

View File

@ -3,25 +3,6 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "window.h" #include "window.h"
typedef struct ui_elem_colours ui_elem_colours_t;
struct ui_elem_colours {
colour_t fill;
colour_t border;
};
INTERNAL ui_elem_colours_t colours[COUNT_UI_ELEM] = {
[UI_ELEM_NODE] =
(ui_elem_colours_t){
.fill = (colour_t){.abgr = 0xff2c84b7},
.border = (colour_t){.abgr = 0xff315c89},
},
[UI_ELEM_BUTTON] =
(ui_elem_colours_t){
.fill = (colour_t){.abgr = 0xff89a83c},
.border = (colour_t){.abgr = 0xff768432},
},
};
bool aabb(const ui_elem_t *elem, i32 x, i32 y) { bool aabb(const ui_elem_t *elem, i32 x, i32 y) {
return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w && return x > elem->rect.topleft.x && x <= elem->rect.topleft.x + elem->rect.w &&
y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h; y > elem->rect.topleft.y && y <= elem->rect.topleft.y + elem->rect.h;
@ -71,7 +52,8 @@ void handle_ui_events(const window_t *wnd, ui_ctx_t *ctx,
} }
} }
bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
ui_elem_colours_t colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return false; return false;
} }
@ -82,8 +64,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
.type = UI_ELEM_BUTTON, .type = UI_ELEM_BUTTON,
}; };
fill_rect(wnd, rect, colours[UI_ELEM_BUTTON].fill); fill_rect(wnd, rect, colours.fill);
draw_rect(wnd, rect, colours[UI_ELEM_BUTTON].border); draw_rect(wnd, rect, colours.border);
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
return false; return false;
@ -107,7 +89,8 @@ bool button(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
return false; return false;
} }
rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) { rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect,
ui_elem_colours_t colours) {
if (ctx->count + 1 >= MAX_UI_ELEMENTS) { if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
return (rect_t){0}; return (rect_t){0};
} }
@ -118,8 +101,8 @@ rect_t node(const window_t *wnd, ui_ctx_t *ctx, rect_t rect) {
.type = UI_ELEM_NODE, .type = UI_ELEM_NODE,
}; };
fill_rect(wnd, rect, colours[UI_ELEM_NODE].fill); fill_rect(wnd, rect, colours.fill);
draw_rect(wnd, rect, colours[UI_ELEM_NODE].border); draw_rect(wnd, rect, colours.border);
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) { if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != elem.id)) {
return rect; return rect;