Compare commits

..

No commits in common. "0ac799cec890dbe2e3ddda2e791ad8d9e57bb625" and "d114cfce996abc8b315e76a8ead60fc7d74280b5" have entirely different histories.

9 changed files with 8 additions and 136 deletions

View File

@ -1,16 +0,0 @@
#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,6 +37,7 @@ struct node {
node_data_t data; 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); void draw_node(const window_t *wnd, const node_t *node);
#endif // !NODES_H #endif // !NODES_H

View File

@ -1,28 +0,0 @@
#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

View File

@ -1,10 +0,0 @@
#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

View File

@ -1,10 +0,0 @@
#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,15 +1,11 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "button.h"
#include "nodes.h" #include "nodes.h"
#include "ops.h"
#include "ui.h"
#include "window.h" #include "window.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define MAX_WINDOWS 2 #define MAX_WINDOWS 2
@ -24,15 +20,10 @@ struct compositor {
bool running; bool running;
u64 mouse_x; u64 mouse_x;
u64 mouse_y; 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;
bool move_node; 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, void add_node(compositor_t *comp, node_type_t type, node_data_t data, i32 x,
@ -66,16 +57,6 @@ i32 run_main_loop(void) {
colour_t bg_colour = {.abgr = 0xffffffff}; 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 (comp.running) {
while (SDL_PollEvent(&(comp.event))) { while (SDL_PollEvent(&(comp.event))) {
switch (comp.event.type) { switch (comp.event.type) {
@ -94,15 +75,6 @@ i32 run_main_loop(void) {
if (comp.event.button.windowID == main_window->id) { if (comp.event.button.windowID == main_window->id) {
if (comp.node_hovered != -1) { if (comp.node_hovered != -1) {
comp.move_node = true; 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; break;
@ -110,18 +82,8 @@ i32 run_main_loop(void) {
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
comp.move_node = false; 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; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
comp.button_hovered = -1;
if (comp.event.motion.windowID == main_window->id) { if (comp.event.motion.windowID == main_window->id) {
comp.mouse_x = comp.event.motion.x; comp.mouse_x = comp.event.motion.x;
comp.mouse_y = comp.event.motion.y; comp.mouse_y = comp.event.motion.y;
@ -138,26 +100,15 @@ i32 run_main_loop(void) {
comp.node_hovered = -1; comp.node_hovered = -1;
for (u64 i = comp.count - 1; i >= 0; --i) { for (u64 i = comp.count - 1; i >= 0; --i) {
rect_t *rect = &(comp.nodes[i].rect); node_t *node = &(comp.nodes[i]);
if (aabb(rect, comp.mouse_x, comp.mouse_y)) { if (aabb(node, comp.mouse_x, comp.mouse_y)) {
comp.node_hovered = i; comp.node_hovered = i;
break; 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; break;
} }
case SDL_DROPFILE: case SDL_DROPFILE:
@ -180,11 +131,6 @@ i32 run_main_loop(void) {
draw_node(main_window, node); 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) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i])); swap_buffers(&(comp.windows[i]));
} }

View File

@ -22,6 +22,11 @@ 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) { void draw_node(const window_t *wnd, const node_t *node) {
fill_rect(wnd, &(node->rect), colours[node->type].fill); fill_rect(wnd, &(node->rect), colours[node->type].fill);
draw_rect(wnd, &(node->rect), colours[node->type].border); draw_rect(wnd, &(node->rect), colours[node->type].border);

View File

@ -1,10 +0,0 @@
#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; }

View File

@ -1,6 +0,0 @@
#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;
}