Start refactoring the UI code into an immediate mode style

This commit is contained in:
2024-01-16 23:12:09 +00:00
parent 60d236c080
commit 353409a5bf
9 changed files with 227 additions and 77 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

@@ -6,14 +6,6 @@
#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;
@@ -32,11 +24,8 @@ union node_data {
};
struct node {
rect_t rect;
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,51 @@
#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;
};
struct ui_ctx {
u64 count;
i64 hovered;
i64 active;
i64 mouse_x;
i64 mouse_y;
bool mouse_down;
bool mouse_up;
const window_t *wnd;
ui_elem_t elements[MAX_UI_ELEMENTS];
};
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);
#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