Compare commits

...

5 Commits

9 changed files with 136 additions and 8 deletions

16
include/button.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef BUTTON_H
#define BUTTON_H
#include "window.h"
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40
typedef struct button button_t;
struct button {
rect_t rect;
};
void draw_button(const window_t *wnd, const button_t *button);
#endif // !BUTTON_H

View File

@ -37,7 +37,6 @@ struct node {
node_data_t data;
};
bool aabb(const node_t *node, i32 x, i32 y);
void draw_node(const window_t *wnd, const node_t *node);
#endif // !NODES_H

28
include/ops.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef COMP_OPS_H
#define COMP_OPS_H
#include "aliases/aliases.h"
#include "nodes.h"
enum comp_ops {
COMP_OP_ADD,
COMP_OP_SUB,
COMP_OP_MUL,
COMP_OP_DIV,
COUNT_COMP_OPS,
};
i32 comp_add(i32 a, i32 b);
i32 comp_sub(i32 a, i32 b);
i32 comp_mul(i32 a, i32 b);
i32 comp_div(i32 a, i32 b);
INTERNAL node_func_t ops[COUNT_COMP_OPS] = {
[COMP_OP_ADD] = comp_add,
[COMP_OP_SUB] = comp_sub,
[COMP_OP_MUL] = comp_mul,
[COMP_OP_DIV] = comp_div,
};
#endif // !COMP_OPS_H

10
include/ui.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef UI_H
#define UI_H
#include "aliases/aliases.h"
#include "window.h"
#include <stdbool.h>
bool aabb(const rect_t *rect, i32 x, i32 y);
#endif // !UI_H

10
src/button.c Normal file
View File

@ -0,0 +1,10 @@
#include "button.h"
#include "window.h"
#define BUTTON_FILL_COLOUR ((colour_t){.abgr = 0xff89a83c})
#define BUTTON_BORDER_COLOUR ((colour_t){.abgr = 0xff768432})
void draw_button(const window_t *wnd, const button_t *button) {
fill_rect(wnd, &(button->rect), BUTTON_FILL_COLOUR);
draw_rect(wnd, &(button->rect), BUTTON_BORDER_COLOUR);
}

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]));
}

View File

@ -22,11 +22,6 @@ INTERNAL node_colours_t colours[COUNT_NODE_TYPES] = {
},
};
bool aabb(const node_t *node, i32 x, i32 y) {
return x > node->rect.topleft.x && x <= node->rect.topleft.x + node->rect.w &&
y > node->rect.topleft.y && y <= node->rect.topleft.y + node->rect.h;
}
void draw_node(const window_t *wnd, const node_t *node) {
fill_rect(wnd, &(node->rect), colours[node->type].fill);
draw_rect(wnd, &(node->rect), colours[node->type].border);

10
src/ops.c Normal file
View File

@ -0,0 +1,10 @@
#include "ops.h"
#include "aliases/aliases.h"
i32 comp_add(i32 a, i32 b) { return a + b; }
i32 comp_sub(i32 a, i32 b) { return a - b; }
i32 comp_mul(i32 a, i32 b) { return a * b; }
i32 comp_div(i32 a, i32 b) { return a / b; }

6
src/ui.c Normal file
View File

@ -0,0 +1,6 @@
#include "ui.h"
bool aabb(const rect_t *rect, i32 x, i32 y) {
return x > rect->topleft.x && x <= rect->topleft.x + rect->w &&
y > rect->topleft.y && y <= rect->topleft.y + rect->h;
}