Add initial toolbox support

This commit is contained in:
Abdelrahman Said 2024-01-15 22:38:58 +00:00
parent 9d438d7347
commit 0ac799cec8

View File

@ -1,11 +1,15 @@
#include "aliases/aliases.h"
#include "button.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 <stdio.h>
#include <stdlib.h>
#define MAX_WINDOWS 2
@ -20,10 +24,15 @@ struct compositor {
bool running;
u64 mouse_x;
u64 mouse_y;
u64 last_clicked_mouse_x;
u64 last_clicked_mouse_y;
i64 node_hovered;
u64 count;
node_t *nodes;
bool move_node;
button_t buttons[COUNT_COMP_OPS];
i64 button_hovered;
i64 button_clicked;
};
void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
@ -57,6 +66,16 @@ i32 run_main_loop(void) {
colour_t bg_colour = {.abgr = 0xffffffff};
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) {
@ -75,6 +94,15 @@ i32 run_main_loop(void) {
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;
@ -82,8 +110,18 @@ i32 run_main_loop(void) {
case SDL_MOUSEBUTTONUP:
comp.move_node = false;
if (comp.event.button.windowID == toolbox->id) {
if (comp.button_hovered == comp.button_clicked) {
printf("%d\n", ops[comp.button_hovered](10, 5));
}
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;
@ -100,15 +138,26 @@ i32 run_main_loop(void) {
comp.node_hovered = -1;
for (u64 i = comp.count - 1; i >= 0; --i) {
node_t *node = &(comp.nodes[i]);
rect_t *rect = &(comp.nodes[i].rect);
if (aabb(node, comp.mouse_x, comp.mouse_y)) {
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:
@ -131,6 +180,11 @@ i32 run_main_loop(void) {
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]));
}