You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ugui/ugui.c

399 lines
11 KiB

1 year ago
#include <stdlib.h>
#include <string.h>
#include <errno.h>
1 year ago
#include <err.h>
1 year ago
#include "ugui.h"
1 year ago
1 year ago
#define SALT 0xbabb0cac
#define DEF_SCALE 1.0
1 year ago
#define DEF_PPI 96.0
#define STACK_STEP 64
#define PPI_PPM(ppi, scale) (ppi * scale * 0.03937008)
#define PPI_PPD(ppi, scale) (PPI_PPM(ppi, scale) * 0.3528)
#define IS_VALID_UNIT(u) (u==UG_UNIT_PX||u==UG_UNIT_MM||u==UG_UNIT_PT)
#define UG_ERR(...) err(errno, "__FUNCTION__: " __VA_ARGS__)
#define BETWEEN(x, min, max) (x <= max && x >= min)
1 year ago
// default style
// TODO: fill default style
static const ug_style_t default_style = {
.text = {
.color = RGB_FORMAT(0xffffff),
.alt_color = RGB_FORMAT(0xbbbbbb),
.size = SIZE_PX(16),
.alt_size = SIZE_PX(12),
},
.cnt = {
.bg_color = RGB_FORMAT(0xaaaaaa),
.border.t = SIZE_PX(3),
.border.b = SIZE_PX(3),
.border.l = SIZE_PX(3),
.border.r = SIZE_PX(3),
.titlebar.height = SIZE_PX(20),
.titlebar.bg_color = RGB_FORMAT(0xbababa),
},
1 year ago
};
static const ug_vec2_t max_size = {{10e6}, {10e6}};
1 year ago
static ug_style_t style_cache = {0};
/*=============================================================================*
* Common Functions *
*=============================================================================*/
// grow a stack
#define grow(S) \
{ \
S.items = realloc(S.items, (S.size+STACK_STEP)*sizeof(*(S.items))); \
if(!S.items) \
UG_ERR("Could not allocate stack #S: %s", strerror(errno)); \
memset(&(S.items[S.size]), 0, STACK_STEP*sizeof(*(S.items))); \
S.size += STACK_STEP; \
}
1 year ago
// https://en.wikipedia.org/wiki/Jenkins_hash_function
1 year ago
static ug_id_t hash(const void *data, unsigned int size)
1 year ago
{
if (!size)
return 0;
1 year ago
ug_id_t hash = SALT;
1 year ago
unsigned char *v = (unsigned char *)data;
for (; size; size--) {
hash += v[size-1];
hash += hash << 10;
hash ^= hash >> 6;
}
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}
1 year ago
// update the style cache with the correct sizes in pixels and colors
static void update_style_cache(ug_ctx_t *ctx)
1 year ago
{
1 year ago
const ug_style_t *s = ctx->style;
// TODO: do this shit
(void)s;
}
ug_rect_t rect_to_px(ug_ctx_t *ctx, ug_rect_t rect)
{
float scale = 1.0;
switch (ctx->unit) {
case UG_UNIT_PX:
return rect;
case UG_UNIT_MM:
scale = ctx->ppm;
break;
case UG_UNIT_PT:
scale = ctx->ppd;
break;
}
rect.x *= scale;
rect.y *= scale;
rect.w *= scale;
rect.h *= scale;
return rect;
1 year ago
}
#define mousedown(ctx, btn) (ctx->mouse.press_mask & ctx->mouse.down_mask & btn)
#define mouseup(ctx, btn) (ctx->mouse.press_mask & ~ctx->mouse.down_mask & btn)
1 year ago
/*=============================================================================*
* Context Operations *
*=============================================================================*/
1 year ago
// creates a new context, fills with default values, ctx is ready for ug_start()
ug_ctx_t *ug_new_ctx(void)
{
ug_ctx_t *ctx = malloc(sizeof(ug_ctx_t));
if (!ctx)
err(errno, "__FUNCTION__:" "Could not allocate context: %s", strerror(errno));
memset(ctx, 0, sizeof(ug_ctx_t));
ctx->scale = DEF_SCALE;
1 year ago
ctx->ppi = DEF_PPI;
ctx->ppm = PPI_PPM(DEF_SCALE, DEF_PPI);
ctx->ppd = PPI_PPD(DEF_SCALE, DEF_PPI);
1 year ago
1 year ago
ctx->unit = UG_UNIT_PX;
ctx->style = &default_style;
ctx->style_px = &style_cache;
// TODO: allocate stacks
1 year ago
return ctx;
}
void ug_free_ctx(ug_ctx_t *ctx)
{
if (!ctx) {
warn("__FUNCTION__:" "Trying to free a null context");
return;
}
1 year ago
// TODO: free stacks
1 year ago
free(ctx);
1 year ago
// NOTE: do not free style since the default is statically allocated, let
// the user take care of it instead
1 year ago
}
1 year ago
#define TEST_CTX(ctx) { if (!ctx) return -1; }
1 year ago
1 year ago
int ug_ctx_set_displayinfo(ug_ctx_t *ctx, float scale, float ppi)
1 year ago
{
TEST_CTX(ctx);
1 year ago
if (scale <= 0 || ppi < 20.0)
return -1;
ctx->ppm = PPI_PPM(scale, ppi);
ctx->ppd = PPI_PPM(scale, ppi);
ctx->scale = scale;
ctx->ppi = ppi;
update_style_cache(ctx);
1 year ago
1 year ago
return 0;
1 year ago
}
1 year ago
int ug_ctx_set_drawableregion(ug_ctx_t *ctx, ug_vec2_t size)
1 year ago
{
TEST_CTX(ctx);
1 year ago
if (size.w <= 0 || size.h <= 0)
return -1;
ctx->size.w = size.w;
ctx->size.h = size.h;
1 year ago
1 year ago
// FIXME: do I need to do something like update_container_size() here?
// maybe it is redundant since each frame not every conatiner is
// re-added
return 0;
1 year ago
}
1 year ago
int ug_ctx_set_style(ug_ctx_t *ctx, const ug_style_t *style)
1 year ago
{
TEST_CTX(ctx);
1 year ago
if (!style)
return -1;
// TODO: validate style
1 year ago
1 year ago
ctx->style = style;
update_style_cache(ctx);
return 0;
1 year ago
}
1 year ago
int ug_ctx_set_unit(ug_ctx_t *ctx, ug_unit_t unit)
1 year ago
{
TEST_CTX(ctx);
1 year ago
if (!IS_VALID_UNIT(unit))
return -1;
ctx->unit = unit;
return 0;
1 year ago
}
/*=============================================================================*
1 year ago
* Container Operations *
1 year ago
*=============================================================================*/
1 year ago
// get a new or existing container handle
static ug_container_t *get_container(ug_ctx_t *ctx, ug_id_t id)
1 year ago
{
1 year ago
ug_container_t *c = NULL;
for (int i = 0; i < ctx->cnt_stack.idx; i++) {
if (ctx->cnt_stack.items[i].id == id) {
c = &(ctx->cnt_stack.items[i]);
1 year ago
break;
}
}
// if the container was not already there allocate a new one
if (!c) {
if(ctx->cnt_stack.idx >= ctx->cnt_stack.size)
grow(ctx->cnt_stack);
c = &(ctx->cnt_stack.items[ctx->cnt_stack.idx++]);
1 year ago
}
1 year ago
1 year ago
return c;
1 year ago
}
1 year ago
// update the container dimensions and position according to the context information,
// also handle resizing, moving, ect. if allowed by the container
static void update_container(ug_ctx_t *ctx, ug_container_t *cnt)
1 year ago
{
1 year ago
// if the container was just initialized the unit might not be pixels, this
// is a problem since all mouse events are in pixels and convering back
// and forth accumulates error and in heavy on the cpu
if (cnt->unit != UG_UNIT_PX) {
// FIXME: this takes the unit from the context but it should be fine
cnt->rect = rect_to_px(ctx, cnt->rect);
cnt->unit = UG_UNIT_PX;
}
1 year ago
1 year ago
// recalculate position
// FIXME: this is the right place to do some optimization, what if the
// context didn't change?
// the absoulute position of the container
ug_rect_t rect_abs = cnt->rect;
/*
* Container style:
*
* rect_abs(0,0)
* v
* +-----------------------------------------------+
* | Titlebar |
* +-----------------------------------------------+
* |+---------------------------------------------+|
* ||\ ^ Border Top ^ ||
* || \_ rect(0,0) ||
* || ||
* || ||
* || ||
* || ||
* || ||
* || ||
* || ||
* || < Border Left ||
* || Border Right > ||
* || ||
* || ||
* || ||
* || ||
* |+---------------------------------------------+|
* +-----------------------------------------------+
* ^ Border Bottom ^
*/
1 year ago
const ug_style_t *s = ctx->style_px;
1 year ago
// 0 -> take all the space, <0 -> take absolute
if (rect_abs.w < 0) rect_abs.w = -rect_abs.w;
if (rect_abs.h < 0) rect_abs.h = -rect_abs.h;
if (rect_abs.w == 0) rect_abs.w = ctx->size.w -
s->cnt.border.l.size -
s->cnt.border.r.size ;
if (rect_abs.h == 0) rect_abs.h = ctx->size.h -
s->cnt.border.t.size -
s->cnt.border.b.size ;
if (cnt->flags & UG_CNT_MOVABLE)
rect_abs.h -= s->cnt.titlebar.height.size;
1 year ago
// <0 -> relative to the right margin
if (rect_abs.x < 0) rect_abs.x = ctx->size.x - rect_abs.w + rect_abs.x;
if (rect_abs.y < 0) rect_abs.y = ctx->size.y - rect_abs.h + rect_abs.y;
// if we had focus the frame before, then do shit
// FIXME: if this is a brand new container then do we need to handle user
// inputs, since all inputs lag one frame, then it would make no sense
1 year ago
if (ctx->hover.cnt == cnt->id) {
// mouse pressed handle resize, for simplicity containers can only
// be resized from the bottom and right border
if (mousedown(ctx, UG_BTN_RIGHT)) {
ug_vec2_t mpos = ctx->mouse.pos;
int minx, maxx, miny, maxy;
// handle movable windows
minx = rect_abs.x;
maxx = rect_abs.x + rect_abs.w - s->cnt.border.r.size;
miny = rect_abs.y;
maxy = rect_abs.y + s->cnt.titlebar.height.size;
if (cnt->flags & UG_CNT_MOVABLE &&
BETWEEN(mpos.x, minx, maxx) &&
BETWEEN(mpos.y, miny, maxy)) {
rect_abs.x += ctx->mouse.delta.x;
rect_abs.y += ctx->mouse.delta.y;
cnt->rect.x += ctx->mouse.delta.x;
cnt->rect.y += ctx->mouse.delta.y;
}
// right border resize
minx = rect_abs.x + rect_abs.w - s->cnt.border.r.size;
maxx = rect_abs.x + rect_abs.w;
miny = rect_abs.y;
maxy = rect_abs.y + rect_abs.h;
if (BETWEEN(mpos.x, minx, maxx) && BETWEEN(mpos.y, miny, maxy)) {
rect_abs.w += ctx->mouse.delta.x;
cnt->rect.w += ctx->mouse.delta.x;
}
// bottom border resize
minx = rect_abs.x;
maxx = rect_abs.x + rect_abs.w;
miny = rect_abs.y + rect_abs.h - s->cnt.border.b.size;
maxy = rect_abs.y + rect_abs.h;
if (BETWEEN(mpos.x, minx, maxx) && BETWEEN(mpos.y, miny, maxy)) {
rect_abs.h += ctx->mouse.delta.y;
cnt->rect.h += ctx->mouse.delta.y;
}
}
// TODO: what if I want to close a floating container?
// Maybe add a UG_CNT_CLOSABLE flag?
// TODO: what about scrolling? how do we know if we need to draw
// a scroll bar? Maybe add that information inside the
// container structure
1 year ago
}
1 year ago
1 year ago
// push the appropriate rectangles to the drawing stack
// TODO: ^ This ^
1 year ago
}
1 year ago
// a floating container can be placed anywhere and can be resized, acts like a
// window inside another window
int ug_container_floating(ug_ctx_t *ctx, const char *name, ug_rect_t rect)
{
1 year ago
TEST_CTX(ctx);
1 year ago
// TODO: verify rect
ug_id_t id = name ? hash(name, strlen(name)) : hash(&rect, sizeof(ug_rect_t));
ug_container_t *cnt = get_container(ctx, id);
if (cnt->id) {
// nothing? maybe we can skip updating all dimensions and stuff
} else {
cnt->id = id;
cnt->max_size = max_size;
cnt->rect = rect;
cnt->unit = ctx->unit;
cnt->flags = UG_CNT_MOVABLE |
UG_CNT_RESIZABLE |
UG_CNT_SCROLL_X |
UG_CNT_SCROLL_Y ;
}
update_container(ctx, cnt);
return 0;
}