Refactor UI code into an immediate mode style (#1)

Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Reviewed-on: #1
This commit is contained in:
2024-01-20 22:22:17 +00:00
parent 60d236c080
commit 63f119c1b2
9 changed files with 247 additions and 175 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

@@ -2,18 +2,11 @@
#define NODES_H
#include "aliases/aliases.h"
#include "ui.h"
#include "window.h"
#define MAX_NODES 1024
#define NODE_WIDTH 70
#define NODE_HEIGHT 20
#define IO_NODE_FILL_COLOUR ((colour_t){.abgr = 0xff2c84b7})
#define IO_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff315c89})
#define OP_NODE_FILL_COLOUR ((colour_t){.abgr = 0xffad6c3a})
#define OP_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff8e4a33})
typedef i32 (*node_func_t)(i32 a, i32 b);
typedef enum node_type node_type_t;
typedef union node_data node_data_t;
@@ -33,10 +26,9 @@ union node_data {
struct node {
rect_t rect;
ui_elem_colours_t colours;
node_type_t type;
node_data_t data;
};
void draw_node(const window_t *wnd, const node_t *node);
#endif // !NODES_H

View File

@@ -1,10 +1,63 @@
#ifndef UI_H
#define UI_H
#include "SDL_events.h"
#include "aliases/aliases.h"
#include "window.h"
#include <stdbool.h>
bool aabb(const rect_t *rect, i32 x, i32 y);
#define MAX_UI_ELEMENTS 4096
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40
#define NODE_WIDTH 70
#define NODE_HEIGHT 20
typedef enum ui_elem_type ui_elem_type_t;
typedef struct ui_elem ui_elem_t;
typedef struct ui_ctx ui_ctx_t;
enum ui_elem_type {
UI_ELEM_NODE,
UI_ELEM_BUTTON,
COUNT_UI_ELEM,
};
struct ui_elem {
u64 id;
rect_t rect;
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 {
u64 count;
i64 hovered;
i64 active;
i32 mouse_x;
i32 mouse_y;
i32 rel_x;
i32 rel_y;
bool mouse_down;
bool mouse_up;
const window_t *wnd;
ui_elem_t elements[MAX_UI_ELEMENTS];
};
void init_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,
const SDL_Event *event);
bool button(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

View File

@@ -63,7 +63,7 @@ void draw_point(const window_t *wnd, point_t p, colour_t colour);
void draw_line(const window_t *wnd, const line_t *ln, colour_t colour);
void draw_triangle(const window_t *wnd, const triangle_t *triangle,
colour_t colour);
void draw_rect(const window_t *wnd, const rect_t *rect, colour_t colour);
void fill_rect(const window_t *wnd, const rect_t *rect, colour_t colour);
void draw_rect(const window_t *wnd, rect_t rect, colour_t colour);
void fill_rect(const window_t *wnd, rect_t rect, colour_t colour);
#endif // !WINDOW_H