DJUI: added ability for x/y/w/h values to be relative to parent's size

This commit is contained in:
MysterD 2021-06-18 16:16:41 -07:00
parent 038c1135b3
commit 368157fbcd
6 changed files with 71 additions and 92 deletions

View File

@ -20,53 +20,37 @@ void djui_render(void) {
gDjuiRoot = djui_root_create();
struct DjuiRect* imageContainer = djui_rect_create(&gDjuiRoot->base);
imageContainer->base.x = 32;
imageContainer->base.y = 32;
imageContainer->base.width = 64;
imageContainer->base.height = 64;
djui_base_set_location(&imageContainer->base, 32, 32);
djui_base_set_size(&imageContainer->base, 64, 64);
sDjuiImage = djui_image_create(&imageContainer->base, texture16x16, 16, 16);
sDjuiImage->base.x = 16;
sDjuiImage->base.y = 16;
sDjuiImage->base.width = 32;
sDjuiImage->base.height = 32;
sDjuiImage->base.color.r = 255;
djui_base_set_location(&sDjuiImage->base, 16, 16);
djui_base_set_size(&sDjuiImage->base, 32, 32);
djui_base_set_color(&sDjuiImage->base, 255, 255, 255, 255);
//////////////
sDjuiRect = djui_rect_create(&gDjuiRoot->base);
sDjuiRect->base.x = 64;
sDjuiRect->base.y = 64;
sDjuiRect->base.width = 188;
sDjuiRect->base.height = 64;
sDjuiRect->base.color.a = 200;
sDjuiRect->base.hAlign = DJUI_HALIGN_LEFT;
sDjuiRect->base.vAlign = DJUI_VALIGN_BOTTOM;
djui_base_set_location(&sDjuiRect->base, 64, 64);
djui_base_set_size(&sDjuiRect->base, 188, 64);
djui_base_set_color(&sDjuiRect->base, 255, 255, 255, 200);
djui_base_set_alignment(&sDjuiRect->base, DJUI_HALIGN_LEFT, DJUI_VALIGN_BOTTOM);
sDjuiRect2 = djui_rect_create(&sDjuiRect->base);
sDjuiRect2->base.x = 0;
sDjuiRect2->base.y = 0;
//sDjuiRect2->base.color.r = 0;
sDjuiRect2->base.hAlign = DJUI_HALIGN_CENTER;
sDjuiRect2->base.vAlign = DJUI_VALIGN_CENTER;
sDjuiRect2->base.color.a = 255;
sDjuiRect2->base.width = 188 - 8;
sDjuiRect2->base.height = 64 - 8;
djui_base_set_location(&sDjuiRect2->base, 0, 0);
djui_base_set_size(&sDjuiRect2->base, 188 - 8, 64 - 8);
djui_base_set_alignment(&sDjuiRect2->base, DJUI_HALIGN_CENTER, DJUI_VALIGN_CENTER);
sDjuiText = djui_text_create(&sDjuiRect2->base, "Host");
sDjuiText->base.color.r = 111;
sDjuiText->base.color.g = 111;
sDjuiText->base.color.b = 111;
sDjuiText->fontSize = 2;
sDjuiText->base.color.a = 255;
sDjuiText->base.width = 188 - 8;
sDjuiText->base.height = 64 - 8;
sDjuiText->base.x = 0;
sDjuiText->base.y = 0;
sDjuiText->textHAlign = DJUI_HALIGN_CENTER;
sDjuiText->textVAlign = DJUI_VALIGN_CENTER;
djui_base_set_location(&sDjuiText->base, 0, 0);
djui_base_set_size(&sDjuiText->base, 188 - 8, 64 - 8);
djui_base_set_color(&sDjuiText->base, 111, 111, 111, 255);
djui_text_set_font_size(sDjuiText, 2);
djui_text_set_alignment(sDjuiText, DJUI_HALIGN_CENTER, DJUI_VALIGN_CENTER);
sDjuiButton = djui_button_create(&gDjuiRoot->base, "button");
djui_base_set_alignment(&sDjuiButton->base, DJUI_HALIGN_RIGHT, DJUI_VALIGN_BOTTOM);
djui_base_set_location(&sDjuiButton->base, 64, 64);
}
djui_base_render(&gDjuiRoot->base);
@ -74,16 +58,22 @@ void djui_render(void) {
if (sDjuiRect2 != NULL) {
static u32 sTimer = 0;
sTimer++;
sDjuiImage->base.x = 16.0f + cos((sTimer) / 10.0f) * 24.0f;
sDjuiImage->base.y = 16.0f + sin((sTimer) / 31.0f) * 24.0f;
sDjuiImage->base.color.r = 127.0 + sin((sTimer) / 13.0f) * 127.0f;
sDjuiImage->base.color.g = 127.0 + sin((sTimer) / 17.0f) * 127.0f;
sDjuiImage->base.color.b = 127.0 + sin((sTimer) / 23.0f) * 127.0f;
djui_base_set_location(&sDjuiImage->base,
16.0f + cos((sTimer) / 10.0f) * 24.0f,
16.0f + sin((sTimer) / 31.0f) * 24.0f);
sDjuiRect2->base.x = 32.0f + cos((sTimer) / 10.0f) * 64.0f;
sDjuiRect2->base.y = 32.0f + sin((sTimer) / 31.0f) * 64.0f;
djui_base_set_color(&sDjuiImage->base,
127.0f + sin((sTimer) / 13.0f) * 127.0f,
127.0f + sin((sTimer) / 17.0f) * 127.0f,
127.0f + sin((sTimer) / 23.0f) * 127.0f,
255);
sDjuiButton->base.width = 200 + cos((sTimer) / 10.0f) * 64.0f;
sDjuiButton->base.height = 64 + sin((sTimer) / 10.0f) * 16.0f;
djui_base_set_location(&sDjuiRect2->base,
32.0f + cos((sTimer) / 10.0f) * 64.0f,
32.0f + sin((sTimer) / 31.0f) * 64.0f);
djui_base_set_size(&sDjuiButton->base,
200.0f + cos((sTimer) / 10.0f) * 64.0f,
64.0f + sin((sTimer) / 10.0f) * 16.0f);
}
}

View File

@ -1,26 +1,28 @@
#include <string.h>
#include "djui.h"
////////////////
////////////////
// properties //
////////////////
void djui_base_set_location(struct DjuiBase* base, f32 x, f32 y) {
base->x = x;
base->y = y;
base->x.value = x;
base->y.value = y;
}
void djui_base_set_location_type(struct DjuiBase* base, enum DjuiScreenValueType xType, enum DjuiScreenValueType yType) {
base->x.type = xType;
base->y.type = yType;
}
void djui_base_set_size(struct DjuiBase* base, f32 width, f32 height) {
base->width = width;
base->height = height;
base->widthFill = false;
base->heightFill = false;
base->width.value = width;
base->height.value = height;
}
void djui_base_set_size_fill(struct DjuiBase* base, f32 widthScale, f32 heightScale) {
base->width = widthScale;
base->height = heightScale;
base->widthFill = true;
base->heightFill = true;
void djui_base_set_size_type(struct DjuiBase* base, f32 widthType, f32 heightType) {
base->width.type = widthType;
base->height.type = heightType;
}
void djui_base_set_color(struct DjuiBase* base, u8 r, u8 g, u8 b, u8 a) {
@ -65,10 +67,11 @@ void djui_base_compute(struct DjuiBase* base) {
struct DjuiBase* parent = base->parent;
struct DjuiBaseRect* comp = &base->comp;
f32 x = base->x;
f32 y = base->y;
f32 width = base->widthFill ? (parent->comp.width - x) : base->width;
f32 height = base->heightFill ? (parent->comp.height - y) : base->height;
f32 x = (base->x.type == DJUI_SVT_RELATIVE) ? parent->comp.width * base->x.value : base->x.value;
f32 y = (base->y.type == DJUI_SVT_RELATIVE) ? parent->comp.height * base->y.value : base->y.value;
f32 width = (base->width.type == DJUI_SVT_RELATIVE) ? parent->comp.width * base->width.value : base->width.value;
f32 height = (base->height.type == DJUI_SVT_RELATIVE) ? parent->comp.height * base->height.value : base->height.value;
// horizontal alignment
if (base->hAlign == DJUI_HALIGN_CENTER) {
@ -188,21 +191,11 @@ void djui_base_destroy(struct DjuiBase* base) {
}
void djui_base_init(struct DjuiBase* parent, struct DjuiBase* base, void(*render)(struct DjuiBase*), void (*destroy)(struct DjuiBase*)) {
memset(base, 0, sizeof(struct DjuiBase));
base->parent = parent;
base->child = NULL;
base->visible = true;
base->x = 0;
base->y = 0;
base->width = 64;
base->height = 64;
base->widthFill = false;
base->heightFill = false;
base->color.r = 255;
base->color.g = 255;
base->color.b = 255;
base->color.a = 255;
base->hAlign = DJUI_HALIGN_LEFT;
base->vAlign = DJUI_VALIGN_TOP;
djui_base_set_size(base, 64, 64);
djui_base_set_color(base, 255, 255, 255, 255);
base->render = render;
base->destroy = destroy;
djui_base_add_child(parent, base);

View File

@ -20,12 +20,10 @@ struct DjuiBase {
struct DjuiBase* parent;
struct DjuiBaseChild* child;
bool visible;
f32 x;
f32 y;
f32 width;
f32 height;
bool widthFill;
bool heightFill;
struct DjuiScreenValue x;
struct DjuiScreenValue y;
struct DjuiScreenValue width;
struct DjuiScreenValue height;
struct DjuiColor color;
enum DjuiHAlign hAlign;
enum DjuiVAlign vAlign;
@ -36,8 +34,9 @@ struct DjuiBase {
};
void djui_base_set_location(struct DjuiBase* base, f32 x, f32 y);
void djui_base_set_location_type(struct DjuiBase* base, enum DjuiScreenValueType xType, enum DjuiScreenValueType yType);
void djui_base_set_size(struct DjuiBase* base, f32 width, f32 height);
void djui_base_set_size_fill(struct DjuiBase* base, f32 widthScale, f32 heightScale);
void djui_base_set_size_type(struct DjuiBase* base, f32 widthType, f32 heightType);
void djui_base_set_color(struct DjuiBase* base, u8 r, u8 g, u8 b, u8 a);
void djui_base_set_alignment(struct DjuiBase* base, enum DjuiHAlign hAlign, enum DjuiVAlign vAlign);

View File

@ -13,12 +13,14 @@ struct DjuiButton* djui_button_create(struct DjuiBase* parent, const char* messa
djui_base_set_size(base, 200, 64);
struct DjuiRect* rect = djui_rect_create(&button->base);
djui_base_set_size_fill(&rect->base, 1.0f, 1.0f);
djui_base_set_size_type(&rect->base, DJUI_SVT_RELATIVE, DJUI_SVT_RELATIVE);
djui_base_set_size(&rect->base, 1.0f, 1.0f);
djui_base_set_color(&rect->base, 225, 225, 225, 255);
button->rect = rect;
struct DjuiText* text = djui_text_create(&rect->base, message);
djui_base_set_size_fill(&text->base, 1.0f, 1.0f);
djui_base_set_size_type(&text->base, DJUI_SVT_RELATIVE, DJUI_SVT_RELATIVE);
djui_base_set_size(&text->base, 1.0f, 1.0f);
djui_base_set_color(&text->base, 11, 11, 11, 255);
djui_text_set_alignment(text, DJUI_HALIGN_CENTER, DJUI_VALIGN_CENTER);
button->text = text;

View File

@ -8,10 +8,8 @@ static void djui_root_render(struct DjuiBase* base) {
wm_api->get_dimensions(&windowWidth, &windowHeight);
// fill the screen
base->x = 0;
base->y = 0;
base->width = windowWidth;
base->height = windowHeight;
djui_base_set_location(base, 0, 0);
djui_base_set_size(base, windowWidth, windowHeight);
// compute base
djui_base_compute(base);
@ -32,11 +30,9 @@ struct DjuiRoot* djui_root_create(void) {
u32 windowWidth, windowHeight;
wm_api->get_dimensions(&windowWidth, &windowHeight);
base->x = 0;
base->y = 0;
base->width = windowWidth;
base->height = windowHeight;
base->color.a = 0;
djui_base_set_location(base, 0, 0);
djui_base_set_size(base, windowWidth, windowHeight);
djui_base_set_color(base, 0, 0, 0, 0);
return root;
}

View File

@ -46,7 +46,6 @@ static void djui_text_translate(f32 x, f32 y) {
static void djui_text_render_char(struct DjuiText* text, u8 d) {
struct DjuiBaseRect* comp = &text->base.comp;
struct DjuiBaseRect* clip = &text->base.clip;
f32 dX = comp->x + sTextRenderX * text->fontSize;
f32 dY = comp->y + sTextRenderY * text->fontSize;