Compare commits
24 Commits
10ba3d642d
...
main
Author | SHA1 | Date | |
---|---|---|---|
f419378183 | |||
46a4802ce6 | |||
4ed86611b5 | |||
|
b447e9d5a1 | ||
|
65d69bd570 | ||
9570f31dc7 | |||
547764764c | |||
29bcfabda9 | |||
f03dad33a2 | |||
8078a72deb | |||
f2faa56e5f | |||
a68d6997a5 | |||
373e48216d | |||
7b7a913c46 | |||
bc6bb1afdc | |||
b847f87ee5 | |||
4610561eff | |||
8501eb787e | |||
050bb355d0 | |||
a86b025f7d | |||
2d31233a1e | |||
30986e3c99 | |||
84cdb87a19 | |||
84873f1e98 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.cache
|
||||
compile_commands.json
|
||||
main
|
||||
*.dSYM
|
||||
|
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,3 +1,3 @@
|
||||
[submodule "intern/aliases"]
|
||||
path = intern/aliases
|
||||
url = https://git.thewizardapprentice.com/abdelrahman/c-cpp-aliases.git
|
||||
[submodule "intern/wizapp"]
|
||||
path = intern/wizapp
|
||||
url = https://git.thewizardapprentice.com/abdelrahman/wizapp-stdlib.git
|
||||
|
18
compile
18
compile
@@ -1,9 +1,21 @@
|
||||
#!/bin/bash
|
||||
|
||||
CC=clang
|
||||
CFLAGS="-g -Wall -Iinclude -Iintern $(pkg-config --cflags sdl2)"
|
||||
CFLAGS="-g -Wall $(pkg-config --cflags sdl2)"
|
||||
LIBS="$(pkg-config --libs sdl2) -lm"
|
||||
SRC=src/*.c
|
||||
INCLUDE="\
|
||||
-Iinclude \
|
||||
-Iintern/wizapp/aliases \
|
||||
-Iintern/wizapp/cpath/include \
|
||||
-Iintern/wizapp/dstr/include \
|
||||
$(find intern/wizapp/mem/include -type d | xargs -I{} echo -n "-I{} ") \
|
||||
"
|
||||
SRC="\
|
||||
intern/wizapp/cpath/src/*.c \
|
||||
intern/wizapp/dstr/src/*.c \
|
||||
intern/wizapp/mem/src/*/*.c \
|
||||
src/*.c \
|
||||
"
|
||||
OUT=main
|
||||
|
||||
(set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT)
|
||||
(set -x ; $CC $CFLAGS $INCLUDE $LIBS $SRC -o $OUT)
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#ifndef COMPOSITOR_H
|
||||
#define COMPOSITOR_H
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
|
||||
i32 run_main_loop(void);
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#ifndef MATH_UTILS_H
|
||||
#define MATH_UTILS_H
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
|
||||
#define square(x) (x * x)
|
||||
#define absolute(x) (x < 0.0 ? x * -1.0 : x)
|
||||
|
@@ -1,21 +1,27 @@
|
||||
#ifndef NODES_H
|
||||
#define NODES_H
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "ui.h"
|
||||
|
||||
#define MAX_NODES 1024
|
||||
#define IO_INPUT_COUNT 0
|
||||
#define OP_INPUT_COUNT 2
|
||||
#define EMPTY_NODE 0
|
||||
#define NODE_START 1
|
||||
#define CONNECTION_START 1
|
||||
|
||||
typedef i32 (*node_func)(i32 a, i32 b);
|
||||
typedef enum node_type node_type;
|
||||
typedef union node_data node_data;
|
||||
typedef struct node node;
|
||||
typedef struct noodle noodle;
|
||||
|
||||
enum node_type {
|
||||
NODEYPE_IO,
|
||||
NODEYPE_OP,
|
||||
NODE_TYPE_IO,
|
||||
NODE_TYPE_OP,
|
||||
|
||||
COUNT_NODEYPES,
|
||||
COUNT_NODE_TYPES,
|
||||
};
|
||||
|
||||
union node_data {
|
||||
@@ -23,11 +29,21 @@ union node_data {
|
||||
node_func func;
|
||||
};
|
||||
|
||||
struct noodle {
|
||||
line ln;
|
||||
u64 connected_node;
|
||||
u64 connection_idx;
|
||||
};
|
||||
|
||||
struct node {
|
||||
ui_node_elem node;
|
||||
rect rec;
|
||||
ui_elem_colours colours;
|
||||
node_type type;
|
||||
node_data data;
|
||||
u64 inputs;
|
||||
u64 connected;
|
||||
noodle *noodles;
|
||||
noodle **connected_noodles;
|
||||
};
|
||||
|
||||
#endif // !NODES_H
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#ifndef COMP_OPS_H
|
||||
#define COMP_OPS_H
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "nodes.h"
|
||||
|
||||
enum comp_ops {
|
||||
@@ -18,7 +18,7 @@ i32 comp_sub(i32 a, i32 b);
|
||||
i32 comp_mul(i32 a, i32 b);
|
||||
i32 comp_div(i32 a, i32 b);
|
||||
|
||||
INTERNAL node_func ops[COUNT_COMP_OPS] = {
|
||||
internal node_func ops[COUNT_COMP_OPS] = {
|
||||
[COMP_OP_ADD] = comp_add,
|
||||
[COMP_OP_SUB] = comp_sub,
|
||||
[COMP_OP_MUL] = comp_mul,
|
||||
|
28
include/ui.h
28
include/ui.h
@@ -2,7 +2,7 @@
|
||||
#define UI_H
|
||||
|
||||
#include "SDL_events.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "window.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -14,12 +14,17 @@
|
||||
#define NODE_WIDTH 70
|
||||
#define NODE_HEIGHT 20
|
||||
|
||||
#define DEFAULT_NOODLE_LENGTH 60
|
||||
|
||||
typedef enum ui_elem_type ui_elem_type;
|
||||
typedef struct ui_elem ui_elem;
|
||||
typedef struct ui_ctx ui_ctx;
|
||||
typedef struct ui_elem_colours ui_elem_colours;
|
||||
typedef enum noodle_action noodle_action;
|
||||
|
||||
enum ui_elem_type {
|
||||
UI_ELEM_NODE,
|
||||
UI_ELEM_NOODLE,
|
||||
UI_ELEM_BUTTON,
|
||||
|
||||
COUNT_UI_ELEM,
|
||||
@@ -27,20 +32,22 @@ enum ui_elem_type {
|
||||
|
||||
struct ui_elem {
|
||||
u64 id;
|
||||
rect rect;
|
||||
union {
|
||||
rect rec;
|
||||
line ln;
|
||||
};
|
||||
ui_elem_type type;
|
||||
};
|
||||
|
||||
typedef struct ui_elem_colours ui_elem_colours;
|
||||
struct ui_elem_colours {
|
||||
colour fill;
|
||||
colour border;
|
||||
};
|
||||
|
||||
typedef struct ui_node_elem ui_node_elem;
|
||||
struct ui_node_elem {
|
||||
line noodle;
|
||||
rect rec;
|
||||
enum noodle_action {
|
||||
NOODLE_ACTION_NONE,
|
||||
NOODLE_ACTION_DRAGGING,
|
||||
NOODLE_ACTION_RELEASED,
|
||||
};
|
||||
|
||||
struct ui_ctx {
|
||||
@@ -54,6 +61,7 @@ struct ui_ctx {
|
||||
bool mouse_down;
|
||||
bool mouse_up;
|
||||
const window *wnd;
|
||||
ui_elem elements[MAX_UI_ELEMENTS];
|
||||
};
|
||||
|
||||
void init_ui_ctx(ui_ctx *ctx);
|
||||
@@ -61,7 +69,9 @@ void reset_ui_ctx(ui_ctx *ctx);
|
||||
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event);
|
||||
bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
|
||||
ui_elem_colours colours);
|
||||
ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
ui_elem_colours colours);
|
||||
bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
||||
ui_elem_colours colours);
|
||||
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours, rect parent_node);
|
||||
|
||||
#endif // !UI_H
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
@@ -80,5 +80,7 @@ void draw_rect(const window *wnd, rect rec, colour colour);
|
||||
void fill_triangle(const window *wnd, triangle triangle, colour colour);
|
||||
void fill_quad(const window *wnd, quad qd, colour colour);
|
||||
void fill_rect(const window *wnd, rect rec, colour colour);
|
||||
line line_from_origin(point origin, f64 angle, i32 line_length);
|
||||
bool aabb(rect rec, i32 x, i32 y);
|
||||
|
||||
#endif // !WINDOW_H
|
||||
|
Submodule intern/aliases deleted from f95f3aa499
1
intern/wizapp
Submodule
1
intern/wizapp
Submodule
Submodule intern/wizapp added at 7948d3fd1a
273
src/compositor.c
273
src/compositor.c
@@ -1,4 +1,5 @@
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_arena.h"
|
||||
#include "nodes.h"
|
||||
#include "ops.h"
|
||||
#include "ui.h"
|
||||
@@ -15,21 +16,25 @@
|
||||
#define WINDOW_WIDTH 1280
|
||||
#define WINDOW_HEIGHT 720
|
||||
|
||||
#define ARENA_CAPACITY 1 * 1024 * 1024
|
||||
|
||||
typedef struct compositor compositor;
|
||||
struct compositor {
|
||||
Arena *arena;
|
||||
window windows[MAX_WINDOWS];
|
||||
u32 active_window;
|
||||
SDL_Event event;
|
||||
bool running;
|
||||
i64 node_hovered;
|
||||
u64 count;
|
||||
node *nodes;
|
||||
bool move_node;
|
||||
node *back_nodes;
|
||||
ui_ctx ctx;
|
||||
};
|
||||
|
||||
void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
|
||||
ui_elem_colours colours);
|
||||
internal void add_node(compositor *comp, node_type type, node_data data,
|
||||
u64 inputs, i32 x, i32 y, ui_elem_colours colours);
|
||||
internal void update_node_graph(compositor *comp, const window *wnd);
|
||||
internal void draw_node_graph(compositor *comp, const window *wnd);
|
||||
|
||||
i32 run_main_loop(void) {
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
@@ -39,8 +44,11 @@ i32 run_main_loop(void) {
|
||||
compositor comp = {0};
|
||||
|
||||
init_ui_ctx(&(comp.ctx));
|
||||
mem_arena_init(&comp.arena, ARENA_CAPACITY);
|
||||
|
||||
comp.nodes = (node *)malloc(sizeof(node) * MAX_NODES);
|
||||
comp.nodes = (node *)mem_arena_alloc(comp.arena, sizeof(node) * MAX_NODES);
|
||||
comp.back_nodes =
|
||||
(node *)mem_arena_alloc(comp.arena, sizeof(node) * MAX_NODES);
|
||||
|
||||
window *main_window = &(comp.windows[0]);
|
||||
window *toolbox = &(comp.windows[1]);
|
||||
@@ -81,16 +89,16 @@ i32 run_main_loop(void) {
|
||||
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
|
||||
&(comp.event));
|
||||
|
||||
switch (comp.event.type) {
|
||||
case SDL_QUIT:
|
||||
if (comp.event.type == SDL_QUIT) {
|
||||
comp.running = false;
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (comp.event.window.event) {
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
}
|
||||
|
||||
if (comp.event.type == SDL_WINDOWEVENT) {
|
||||
if (comp.event.window.event == SDL_WINDOWEVENT_CLOSE) {
|
||||
comp.running = false;
|
||||
break;
|
||||
case SDL_WINDOWEVENT_ENTER: {
|
||||
}
|
||||
|
||||
if (comp.event.window.event == SDL_WINDOWEVENT_ENTER) {
|
||||
u32 id = comp.event.window.windowID;
|
||||
window *wnd = NULL;
|
||||
|
||||
@@ -104,29 +112,24 @@ i32 run_main_loop(void) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!wnd) {
|
||||
break;
|
||||
if (wnd) {
|
||||
SDL_RaiseWindow(wnd->window);
|
||||
}
|
||||
|
||||
SDL_RaiseWindow(wnd->window);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case SDL_DROPFILE:
|
||||
if (comp.event.type == 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;
|
||||
add_node(&comp, NODE_TYPE_IO, data, IO_INPUT_COUNT, comp.ctx.mouse_x,
|
||||
comp.ctx.mouse_y, io_node_colours);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_node_graph(&comp, main_window);
|
||||
|
||||
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
|
||||
clear_window(&(comp.windows[i]), bg_colour);
|
||||
}
|
||||
@@ -142,15 +145,12 @@ i32 run_main_loop(void) {
|
||||
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);
|
||||
add_node(&comp, NODE_TYPE_OP, data, OP_INPUT_COUNT, comp.ctx.mouse_x,
|
||||
comp.ctx.mouse_y, op_node_colours);
|
||||
}
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < comp.count; ++i) {
|
||||
comp.nodes[i].node = ui_node(main_window, &(comp.ctx), comp.nodes[i].node,
|
||||
comp.nodes[i].colours);
|
||||
}
|
||||
draw_node_graph(&comp, main_window);
|
||||
|
||||
for (u64 i = 0; i < MAX_WINDOWS; ++i) {
|
||||
swap_buffers(&(comp.windows[i]));
|
||||
@@ -165,29 +165,208 @@ i32 run_main_loop(void) {
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
mem_arena_free(&comp.arena);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void add_node(compositor *comp, node_type type, node_data data, i32 x, i32 y,
|
||||
ui_elem_colours colours) {
|
||||
internal void add_node(compositor *comp, node_type type, node_data data,
|
||||
u64 inputs, i32 x, i32 y, ui_elem_colours colours) {
|
||||
if (comp->count + 1 >= MAX_NODES) {
|
||||
return;
|
||||
}
|
||||
|
||||
comp->nodes[(comp->count)++] = (node){
|
||||
.node =
|
||||
(ui_node_elem){
|
||||
.rec =
|
||||
(rect){
|
||||
.topleft.x = x,
|
||||
.topleft.y = y,
|
||||
.w = NODE_WIDTH,
|
||||
.h = NODE_HEIGHT,
|
||||
},
|
||||
.noodle = (line){0},
|
||||
},
|
||||
u64 alloc_size = inputs * sizeof(noodle);
|
||||
noodle *noodles = mem_arena_alloc(comp->arena, alloc_size);
|
||||
noodle *back_noodles = mem_arena_alloc(comp->arena, alloc_size);
|
||||
if (!noodles || !back_noodles) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 connected_alloc_size = MAX_NODES * sizeof(noodle *);
|
||||
noodle **connected_noodles =
|
||||
mem_arena_alloc(comp->arena, connected_alloc_size);
|
||||
noodle **connected_back_noodles =
|
||||
mem_arena_alloc(comp->arena, connected_alloc_size);
|
||||
if (!connected_noodles || !connected_back_noodles) {
|
||||
return;
|
||||
}
|
||||
|
||||
rect rec = (rect){
|
||||
.topleft.x = x,
|
||||
.topleft.y = y,
|
||||
.w = NODE_WIDTH,
|
||||
.h = NODE_HEIGHT,
|
||||
};
|
||||
|
||||
u64 idx = ++(comp->count);
|
||||
|
||||
comp->nodes[idx] = comp->back_nodes[idx] = (node){
|
||||
.rec = rec,
|
||||
.colours = colours,
|
||||
.type = type,
|
||||
.data.path = data.path,
|
||||
.inputs = inputs,
|
||||
.connected = 0,
|
||||
.noodles = noodles,
|
||||
.connected_noodles = connected_noodles,
|
||||
};
|
||||
|
||||
comp->back_nodes[idx].noodles = back_noodles;
|
||||
comp->back_nodes[idx].connected_noodles = connected_back_noodles;
|
||||
}
|
||||
|
||||
internal void update_node_graph(compositor *comp, const window *wnd) {
|
||||
for (u64 i = NODE_START; i <= comp->count; ++i) {
|
||||
node *node_elem = &(comp->nodes[i]);
|
||||
const node *back_node = &(comp->back_nodes[i]);
|
||||
|
||||
node_elem->rec = back_node->rec;
|
||||
node_elem->connected = back_node->connected;
|
||||
|
||||
for (u64 j = 0; j < node_elem->inputs; ++j) {
|
||||
noodle *ndl = &(node_elem->noodles[j]);
|
||||
const noodle *back_ndl = &(back_node->noodles[j]);
|
||||
|
||||
*ndl = *back_ndl;
|
||||
}
|
||||
|
||||
for (u64 j = CONNECTION_START; j <= back_node->connected; ++j) {
|
||||
node_elem->connected_noodles[j] = back_node->connected_noodles[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void draw_node_graph(compositor *comp, const window *wnd) {
|
||||
for (u64 i = NODE_START; i <= comp->count; ++i) {
|
||||
node *node_elem = &(comp->nodes[i]);
|
||||
node *back_node = &(comp->back_nodes[i]);
|
||||
|
||||
f64 angle = 90.0;
|
||||
f64 angle_delta = 25.0;
|
||||
i64 delta_multiplier = node_elem->inputs % 2 == 0 ? -1 : 0;
|
||||
|
||||
for (u64 j = 0; j < node_elem->inputs; ++j) {
|
||||
f64 new_angle = angle + angle_delta * delta_multiplier;
|
||||
noodle *ndl = &(node_elem->noodles[j]);
|
||||
noodle *back_ndl = &(back_node->noodles[j]);
|
||||
|
||||
if (ndl->ln.p0.x == ndl->ln.p1.x && ndl->ln.p0.y == ndl->ln.p1.y) {
|
||||
point origin = {node_elem->rec.topleft.x + node_elem->rec.w / 2,
|
||||
node_elem->rec.topleft.y + node_elem->rec.h / 2};
|
||||
|
||||
back_ndl->ln =
|
||||
line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
|
||||
}
|
||||
|
||||
switch (ui_noodle(wnd, &(comp->ctx), ndl->ln, node_elem->colours,
|
||||
node_elem->rec)) {
|
||||
case NOODLE_ACTION_DRAGGING:
|
||||
back_ndl->ln.p0.x += comp->ctx.rel_x;
|
||||
back_ndl->ln.p0.y += comp->ctx.rel_y;
|
||||
break;
|
||||
case NOODLE_ACTION_RELEASED: {
|
||||
bool connected = false;
|
||||
bool disconnect = false;
|
||||
|
||||
for (u64 k = NODE_START; k <= comp->count; ++k) {
|
||||
if (k == i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const node *nd = &(comp->nodes[k]);
|
||||
node *back_node = &(comp->back_nodes[k]);
|
||||
|
||||
if (aabb(nd->rec, comp->ctx.mouse_x, comp->ctx.mouse_y)) {
|
||||
point p0 = {nd->rec.topleft.x + nd->rec.w / 2,
|
||||
nd->rec.topleft.y + nd->rec.h / 2};
|
||||
|
||||
back_ndl->ln.p0 = p0;
|
||||
|
||||
connected = true;
|
||||
disconnect = back_ndl->connected_node != EMPTY_NODE &&
|
||||
back_ndl->connected_node != k;
|
||||
|
||||
if (back_ndl->connected_node != k) {
|
||||
back_ndl->connected_node = k;
|
||||
u64 idx = ++(back_node->connected);
|
||||
back_ndl->connection_idx = idx;
|
||||
back_node->connected_noodles[idx] = back_ndl;
|
||||
}
|
||||
|
||||
break;
|
||||
} else {
|
||||
if (back_ndl->connected_node != EMPTY_NODE) {
|
||||
disconnect = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (disconnect) {
|
||||
u64 conntection_idx = back_ndl->connection_idx;
|
||||
u64 node_idx = back_ndl->connected_node;
|
||||
|
||||
node *connected_node = &(comp->back_nodes[node_idx]);
|
||||
|
||||
if (conntection_idx == connected_node->connected) {
|
||||
connected_node->connected_noodles[conntection_idx] = NULL;
|
||||
} else {
|
||||
connected_node->connected_noodles[conntection_idx] =
|
||||
connected_node->connected_noodles[connected_node->connected];
|
||||
connected_node->connected_noodles[connected_node->connected] = NULL;
|
||||
|
||||
connected_node->connected_noodles[conntection_idx]->connection_idx =
|
||||
conntection_idx;
|
||||
}
|
||||
|
||||
connected_node->connected -= 1;
|
||||
}
|
||||
|
||||
if (!connected) {
|
||||
back_ndl->ln.p0 = ndl->ln.p1;
|
||||
back_ndl->connected_node = EMPTY_NODE;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (delta_multiplier > 0) {
|
||||
angle = new_angle;
|
||||
}
|
||||
|
||||
if (delta_multiplier == 0) {
|
||||
delta_multiplier = -1;
|
||||
} else {
|
||||
delta_multiplier *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ui_node(wnd, &(comp->ctx), node_elem->rec, node_elem->colours)) {
|
||||
back_node->rec.topleft.x += comp->ctx.rel_x;
|
||||
back_node->rec.topleft.y += comp->ctx.rel_y;
|
||||
|
||||
for (u64 j = 0; j < node_elem->inputs; ++j) {
|
||||
noodle *ndl = &(node_elem->noodles[j]);
|
||||
noodle *back_ndl = &(back_node->noodles[j]);
|
||||
|
||||
if (ndl->connected_node == EMPTY_NODE) {
|
||||
back_ndl->ln.p0.x += comp->ctx.rel_x;
|
||||
back_ndl->ln.p0.y += comp->ctx.rel_y;
|
||||
}
|
||||
|
||||
back_ndl->ln.p1.x += comp->ctx.rel_x;
|
||||
back_ndl->ln.p1.y += comp->ctx.rel_y;
|
||||
}
|
||||
|
||||
for (u64 j = CONNECTION_START; j <= back_node->connected; ++j) {
|
||||
noodle *ndl = back_node->connected_noodles[j];
|
||||
|
||||
ndl->ln.p0.x += comp->ctx.rel_x;
|
||||
ndl->ln.p0.y += comp->ctx.rel_y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "compositor.h"
|
||||
|
||||
i32 main(void) { return run_main_loop(); }
|
||||
|
@@ -1,5 +1,5 @@
|
||||
#include "ops.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
|
||||
i32 comp_add(i32 a, i32 b) { return a + b; }
|
||||
|
||||
|
141
src/ui.c
141
src/ui.c
@@ -1,6 +1,6 @@
|
||||
#include "ui.h"
|
||||
#include "SDL_events.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "math_utils.h"
|
||||
#include "window.h"
|
||||
#include <math.h>
|
||||
@@ -8,12 +8,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NOODLE_HALF_WIDTH 2
|
||||
#define DEFAULT_NOODLE_LENGTH 60
|
||||
|
||||
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours);
|
||||
INTERNAL bool aabb(rect rec, i32 x, i32 y);
|
||||
INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length);
|
||||
internal u64 get_id(ui_ctx *ctx);
|
||||
|
||||
void init_ui_ctx(ui_ctx *ctx) {
|
||||
*ctx = (ui_ctx){0};
|
||||
@@ -30,34 +26,31 @@ void reset_ui_ctx(ui_ctx *ctx) {
|
||||
}
|
||||
|
||||
void handle_ui_events(const window *wnd, ui_ctx *ctx, const SDL_Event *event) {
|
||||
switch (event->type) {
|
||||
case SDL_MOUSEMOTION:
|
||||
if (event->type == SDL_MOUSEMOTION) {
|
||||
if (wnd->id == event->motion.windowID) {
|
||||
ctx->mouse_x = event->motion.x;
|
||||
ctx->mouse_y = event->motion.y;
|
||||
ctx->rel_x += event->motion.xrel;
|
||||
ctx->rel_y += event->motion.yrel;
|
||||
ctx->wnd = wnd;
|
||||
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
}
|
||||
|
||||
if (event->type == SDL_MOUSEBUTTONDOWN) {
|
||||
if (wnd->id == event->button.windowID) {
|
||||
ctx->mouse_x = event->button.x;
|
||||
ctx->mouse_y = event->button.y;
|
||||
ctx->mouse_down = true;
|
||||
ctx->wnd = wnd;
|
||||
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
}
|
||||
|
||||
if (event->type == SDL_MOUSEBUTTONUP) {
|
||||
if (wnd->id == event->button.windowID) {
|
||||
ctx->mouse_x = event->button.x;
|
||||
ctx->mouse_y = event->button.y;
|
||||
ctx->mouse_up = true;
|
||||
ctx->wnd = wnd;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +61,12 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rec,
|
||||
return false;
|
||||
}
|
||||
|
||||
u64 id = (ctx->count)++;
|
||||
u64 id = get_id(ctx);
|
||||
ctx->elements[id] = (ui_elem){
|
||||
.id = id,
|
||||
.rec = rec,
|
||||
.type = UI_ELEM_BUTTON,
|
||||
};
|
||||
|
||||
fill_rect(wnd, rec, colours.fill);
|
||||
draw_rect(wnd, rec, colours.border);
|
||||
@@ -95,52 +93,40 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rec,
|
||||
return false;
|
||||
}
|
||||
|
||||
ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
ui_elem_colours colours) {
|
||||
bool ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
||||
ui_elem_colours colours) {
|
||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||
return (ui_node_elem){0};
|
||||
return false;
|
||||
}
|
||||
|
||||
u64 id = (ctx->count)++;
|
||||
u64 id = get_id(ctx);
|
||||
ctx->elements[id] = (ui_elem){
|
||||
.id = id,
|
||||
.rec = rect,
|
||||
.type = UI_ELEM_NODE,
|
||||
};
|
||||
|
||||
line ln;
|
||||
if (node.noodle.p0.x == node.noodle.p1.x &&
|
||||
node.noodle.p0.y == node.noodle.p1.y) {
|
||||
ln = line_from_origin((point){node.rec.topleft.x + node.rec.w / 2,
|
||||
node.rec.topleft.y + node.rec.h / 2},
|
||||
90.0, DEFAULT_NOODLE_LENGTH);
|
||||
} else {
|
||||
ln = node.noodle;
|
||||
}
|
||||
|
||||
node.noodle = ui_noodle(wnd, ctx, ln, colours);
|
||||
|
||||
fill_rect(wnd, node.rec, colours.fill);
|
||||
draw_rect(wnd, node.rec, colours.border);
|
||||
fill_rect(wnd, rect, colours.fill);
|
||||
draw_rect(wnd, rect, colours.border);
|
||||
|
||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
||||
return node;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ctx->mouse_up) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
ctx->rel_x = ctx->rel_y = 0;
|
||||
return node;
|
||||
if (ctx->hovered == ctx->active && ctx->hovered == id) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ctx->hovered == id && ctx->active == id) {
|
||||
node.rec.topleft.x += ctx->rel_x;
|
||||
node.rec.topleft.y += ctx->rel_y;
|
||||
node.noodle.p0.x += ctx->rel_x;
|
||||
node.noodle.p0.y += ctx->rel_y;
|
||||
node.noodle.p1.x += ctx->rel_x;
|
||||
node.noodle.p1.y += ctx->rel_y;
|
||||
|
||||
return node;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!aabb(node.rec, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return node;
|
||||
if (!aabb(rect, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->hovered = id;
|
||||
@@ -149,16 +135,21 @@ ui_node_elem ui_node(const window *wnd, ui_ctx *ctx, ui_node_elem node,
|
||||
ctx->active = id;
|
||||
}
|
||||
|
||||
return node;
|
||||
return false;
|
||||
}
|
||||
|
||||
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours) {
|
||||
noodle_action ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ui_elem_colours colours, rect parent_node) {
|
||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||
return (line){0};
|
||||
return NOODLE_ACTION_NONE;
|
||||
}
|
||||
|
||||
u64 id = (ctx->count)++;
|
||||
u64 id = get_id(ctx);
|
||||
ctx->elements[id] = (ui_elem){
|
||||
.id = id,
|
||||
.ln = ln,
|
||||
.type = UI_ELEM_NOODLE,
|
||||
};
|
||||
|
||||
bool horizontal = ln.p0.y == ln.p1.y;
|
||||
|
||||
@@ -226,24 +217,25 @@ line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
}
|
||||
|
||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
||||
return ln;
|
||||
return NOODLE_ACTION_NONE;
|
||||
}
|
||||
|
||||
if (ctx->mouse_up) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
ctx->rel_x = ctx->rel_y = 0;
|
||||
return ln;
|
||||
if (ctx->hovered == ctx->active && ctx->hovered == id) {
|
||||
ctx->hovered = ctx->active = -1;
|
||||
return NOODLE_ACTION_RELEASED;
|
||||
}
|
||||
|
||||
return NOODLE_ACTION_NONE;
|
||||
}
|
||||
|
||||
if (ctx->hovered == id && ctx->active == id) {
|
||||
ln.p0.x += ctx->rel_x;
|
||||
ln.p0.y += ctx->rel_y;
|
||||
|
||||
return ln;
|
||||
return NOODLE_ACTION_DRAGGING;
|
||||
}
|
||||
|
||||
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return ln;
|
||||
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y) ||
|
||||
aabb(parent_node, ctx->mouse_x, ctx->mouse_y)) {
|
||||
return NOODLE_ACTION_NONE;
|
||||
}
|
||||
|
||||
ctx->hovered = id;
|
||||
@@ -252,23 +244,10 @@ line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||
ctx->active = id;
|
||||
}
|
||||
|
||||
return ln;
|
||||
return NOODLE_ACTION_NONE;
|
||||
}
|
||||
|
||||
INTERNAL bool aabb(rect rec, i32 x, i32 y) {
|
||||
return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y &&
|
||||
y <= rec.topleft.y + rec.h;
|
||||
}
|
||||
|
||||
INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length) {
|
||||
f64 rad = radians(angle);
|
||||
f64 direction = angle / absolute(angle) * -1;
|
||||
|
||||
i32 adjacent = line_length * cos(rad) * direction; // dx
|
||||
i32 opposite = line_length * sin(rad) * direction; // dy
|
||||
|
||||
return (line){
|
||||
(point){origin.x + adjacent, origin.y + opposite},
|
||||
origin,
|
||||
};
|
||||
internal u64 get_id(ui_ctx *ctx) {
|
||||
// This will always keep the 0 slot empty
|
||||
return ++(ctx->count);
|
||||
}
|
||||
|
24
src/window.c
24
src/window.c
@@ -1,12 +1,12 @@
|
||||
#include "window.h"
|
||||
#include "aliases/aliases.h"
|
||||
#include "aliases.h"
|
||||
#include "math_utils.h"
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
INTERNAL inline bool inside_triangle(triangle tri, point p);
|
||||
internal inline bool inside_triangle(triangle tri, point p);
|
||||
|
||||
bool init_window(window *wnd, const char *title, u32 width, u32 height, i32 x,
|
||||
i32 y) {
|
||||
@@ -162,7 +162,25 @@ void fill_quad(const window *wnd, quad qd, colour colour) {
|
||||
fill_triangle(wnd, t1, colour);
|
||||
}
|
||||
|
||||
INTERNAL inline bool inside_triangle(triangle tri, point p) {
|
||||
line line_from_origin(point origin, f64 angle, i32 line_length) {
|
||||
f64 rad = radians(angle);
|
||||
f64 direction = angle / absolute(angle) * -1;
|
||||
|
||||
i32 adjacent = line_length * cos(rad) * direction; // dx
|
||||
i32 opposite = line_length * sin(rad) * direction; // dy
|
||||
|
||||
return (line){
|
||||
(point){origin.x + adjacent, origin.y + opposite},
|
||||
origin,
|
||||
};
|
||||
}
|
||||
|
||||
bool aabb(rect rec, i32 x, i32 y) {
|
||||
return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y &&
|
||||
y <= rec.topleft.y + rec.h;
|
||||
}
|
||||
|
||||
internal inline bool inside_triangle(triangle tri, point p) {
|
||||
// Based on the following video:
|
||||
// https://www.youtube.com/watch?v=HYAgJN3x4GA
|
||||
f32 cy_min_ay = tri.p2.y - tri.p0.y;
|
||||
|
Reference in New Issue
Block a user