UI core boiler plate

This commit is contained in:
2023-12-20 11:25:01 +01:00
parent ebda550bf2
commit 70cc2eae8c
9 changed files with 2072 additions and 2 deletions

67
engine/breeze/ui/ui.c Normal file
View File

@@ -0,0 +1,67 @@
#include "ui.h"
#include "../memory/memory.h"
#include "../util/object_pool.h"
#include "../util/string.h"
#include <stb_ds.h>
typedef struct BzUI {
struct {
BzUIKey key;
BzUIWidget *value;
} *widgetMap;
BzObjectPool *widgets;
BzUIWidget **widgetStack;
BzUIWidget *root;
} BzUI;
BzUIKey bzUIKeyNull() {
return 0;
}
BzUIKey bzUIKeyFromString(const char *str) {
return bzStringDefaultHash(str);
}
BzUI *bzUICreate() {
BzUI *ui = bzAlloc(sizeof(*ui));
ui->widgetMap = NULL;
ui->widgetStack = NULL;
ui->widgets = bzObjectPoolCreate(&(BzObjectPoolDesc) {
.objectsPerPage=128,
.objectSize=sizeof(BzUIWidget),
});
ui->root = bzObjectPool(ui->widgets);
bzMemSet(ui->root, 0, sizeof(*ui->root));
hmput(ui->widgetMap, bzUIKeyFromString("##root"), ui->root);
return ui;
}
void bzUIDestroy(BzUI *ui) {
hmfree(ui->widgetMap);
ui->widgetMap = NULL;
arrfree(ui->widgetStack);
ui->widgetStack = NULL;
bzObjectPoolDestroy(ui->widgets);
ui->widgets = NULL;
bzFree(ui);
}
void bzUIBegin(BzUI *ui, i32 width, i32 height) {
arrsetlen(ui->widgetStack, 0);
arrpush(ui->widgetStack, ui->root);
ui->root->semanticSize[BZ_UI_AXIS_X] = (BzUISize){
.kind = BZ_UI_SIZE_PIXELS,
.value = width
};
ui->root->semanticSize[BZ_UI_AXIS_Y] = (BzUISize) {
.kind = BZ_UI_SIZE_PIXELS,
.value = height
};
}
void bzUIEnd(BzUI *ui) {
}

98
engine/breeze/ui/ui.h Normal file
View File

@@ -0,0 +1,98 @@
#ifndef BREEZE_UI_CORE_H
#define BREEZE_UI_CORE_H
#include "../defines.h"
#include <raylib.h>
typedef enum BzUISizeKind {
BZ_UI_SIZE_NULL,
BZ_UI_SIZE_PIXELS,
BZ_UI_SIZE_FIT,
BZ_UI_SIZE_REL_PARENT,
} BzUISizeKind;
typedef struct BzUISize {
BzUISizeKind kind;
f32 value;
} BzUISize;
typedef enum BzUIAxis {
BZ_UI_AXIS_X,
BZ_UI_AXIS_Y,
BZ_UI_AXIS_COUNT,
} BzUIAxis;
typedef u32 BzUIWidgetFlags;
enum {
BZ_UI_WIDGET_CLICKABLE = (1 << 0),
BZ_UI_WIDGET_VIEW_SCROLL = (1 << 1),
BZ_UI_WIDGET_DRAW_TEXT = (1 << 2),
BZ_UI_WIDGET_DRAW_BORDER = (1 << 3),
};
typedef u32 BzUIKey;
typedef struct BzUIWidget BzUIWidget;
typedef struct BzUIWidget {
BzUIWidget *first;
BzUIWidget *last;
BzUIWidget *next;
BzUIWidget *prev;
BzUIWidget *parent;
// Key+generation info
BzUIKey key;
u64 lastFrameTouchedIndex;
// Per-frame info provided by builders
BzUIWidgetFlags flags;
const char *string;
BzUISize semanticSize[BZ_UI_AXIS_COUNT];
// recomputed every frame
f32 computedRelPosition[BZ_UI_AXIS_COUNT];
f32 computedSize[BZ_UI_AXIS_COUNT];
Rectangle rect;
} BzUIWidget;
typedef struct BzUIResult {
BzUIWidget *widget;
bool pressed;
bool released;
//bool dragging;
bool hovering;
} BzUIResult;
typedef struct BzUIWidgetDesc {
BzUIWidgetFlags flags;
const char *string;
BzUIKey key;
} BzUIWidgetDesc;
typedef struct BzUI BzUI;
BzUIKey bzUIKeyNull();
BzUIKey bzUIKeyFromString(const char *str);
BzUI *bzUICreate();
void bzUIDestroy(BzUI *ui);
void bzUIBegin(BzUI *ui, i32 width, i32 height);
void bzUIEnd(BzUI *ui);
// Widget construction
BzUIWidget *bzUIWidgetMake(const BzUIWidgetDesc *desc);
BzUIWidget *bzUIPushParent(BzUIWidget *widget);
BzUIWidget *bzUIPopParent();
BzUIResult bzUIGetResult(BzUIWidget *widget);
// UI
bool bzUIButton(const char *string);
#endif //BREEZE_UI_CORE_H