DJUI: Added padding to DjuiBase

This commit is contained in:
MysterD 2021-06-19 01:10:24 -07:00
parent f27a6b2469
commit b80cc50cd0
5 changed files with 67 additions and 10 deletions

View file

@ -21,12 +21,14 @@ void djui_render(void) {
struct DjuiRect* imageContainer = djui_rect_create(&gDjuiRoot->base);
djui_base_set_location(&imageContainer->base, 32, 32);
djui_base_set_size(&imageContainer->base, 64, 64);
djui_base_set_size(&imageContainer->base, 128, 128);
djui_base_set_padding(&imageContainer->base, 32, 32, 32, 32);
sDjuiImage = djui_image_create(&imageContainer->base, texture16x16, 16, 16);
djui_base_set_location(&sDjuiImage->base, 16, 16);
djui_base_set_location(&sDjuiImage->base, 0, 0);
djui_base_set_size(&sDjuiImage->base, 32, 32);
djui_base_set_color(&sDjuiImage->base, 255, 255, 255, 255);
djui_base_set_size_type(&sDjuiImage->base, DJUI_SVT_RELATIVE, DJUI_SVT_RELATIVE);
djui_base_set_size(&sDjuiImage->base, 1.0f, 1.0f);
//////////////
@ -58,9 +60,9 @@ void djui_render(void) {
if (sDjuiRect2 != NULL) {
static u32 sTimer = 0;
sTimer++;
djui_base_set_location(&sDjuiImage->base,
16.0f + cos((sTimer) / 10.0f) * 24.0f,
16.0f + sin((sTimer) / 31.0f) * 24.0f);
/*djui_base_set_location(&sDjuiImage->base,
0.0f + cos((sTimer) / 30.0f) * 128.0f,
0.0f + fabs(sin((sTimer) / 30.0f)) * 128.0f);*/
djui_base_set_color(&sDjuiImage->base,
127.0f + sin((sTimer) / 13.0f) * 127.0f,

View file

@ -20,7 +20,7 @@ void djui_base_set_size(struct DjuiBase* base, f32 width, f32 height) {
base->height.value = height;
}
void djui_base_set_size_type(struct DjuiBase* base, f32 widthType, f32 heightType) {
void djui_base_set_size_type(struct DjuiBase* base, enum DjuiScreenValueType widthType, enum DjuiScreenValueType heightType) {
base->width.type = widthType;
base->height.type = heightType;
}
@ -47,6 +47,20 @@ void djui_base_set_border_color(struct DjuiBase* base, u8 r, u8 g, u8 b, u8 a) {
base->borderColor.a = a;
}
void djui_base_set_padding(struct DjuiBase* base, f32 top, f32 right, f32 bottom, f32 left) {
base->padding.top.value = top;
base->padding.right.value = right;
base->padding.bottom.value = bottom;
base->padding.left.value = left;
}
void djui_base_set_padding_type(struct DjuiBase* base, enum DjuiScreenValueType topType, enum DjuiScreenValueType rightType, enum DjuiScreenValueType bottomType, enum DjuiScreenValueType leftType) {
base->padding.top.type = topType;
base->padding.right.type = rightType;
base->padding.bottom.type = bottomType;
base->padding.left.type = leftType;
}
void djui_base_set_alignment(struct DjuiBase* base, enum DjuiHAlign hAlign, enum DjuiVAlign vAlign) {
base->hAlign = hAlign;
base->vAlign = vAlign;
@ -78,6 +92,21 @@ static void djui_base_clip(struct DjuiBase* base) {
clip->height = fmin(clip->height, (parent->clip.y + parent->clip.height) - clip->y);
}
static void djui_base_add_padding(struct DjuiBase* base) {
struct DjuiBaseRect* comp = &base->comp;
struct DjuiBaseRect* parentComp = &base->comp;
f32 tPad = (base->padding.top.type == DJUI_SVT_RELATIVE) ? parentComp->height * base->padding.top.value : base->padding.top.value;
f32 rPad = (base->padding.right.type == DJUI_SVT_RELATIVE) ? parentComp->width * base->padding.right.value : base->padding.right.value;
f32 bPad = (base->padding.bottom.type == DJUI_SVT_RELATIVE) ? parentComp->height * base->padding.bottom.value : base->padding.bottom.value;
f32 lPad = (base->padding.left.type == DJUI_SVT_RELATIVE) ? parentComp->width * base->padding.left.value : base->padding.left.value;
comp->x += lPad;
comp->y += tPad;
comp->height -= tPad + bPad;
comp->width -= lPad + rPad;
}
void djui_base_compute(struct DjuiBase* base) {
struct DjuiBase* parent = base->parent;
struct DjuiBaseRect* comp = &base->comp;
@ -114,6 +143,7 @@ void djui_base_compute(struct DjuiBase* base) {
comp->width = width;
comp->height = height;
//djui_base_add_padding(base);
djui_base_clip(base);
}
@ -183,8 +213,11 @@ static void djui_base_render_border(struct DjuiBase* base) {
struct DjuiBaseRect* clip = &base->clip;
struct DjuiBaseRect savedComp = base->comp;
f32 xBorderWidth = fmin(base->borderWidth.value, savedComp.width / 2.0f);
f32 yBorderWidth = fmin(base->borderWidth.value, savedComp.height / 2.0f);
f32 xBorderWidth = (base->borderWidth.type == DJUI_SVT_RELATIVE) ? (savedComp.width * base->borderWidth.value) : base->borderWidth.value;
f32 yBorderWidth = (base->borderWidth.type == DJUI_SVT_RELATIVE) ? (savedComp.height * base->borderWidth.value) : base->borderWidth.value;
xBorderWidth = fmin(xBorderWidth, savedComp.width / 2.0f);
yBorderWidth = fmin(yBorderWidth, savedComp.height / 2.0f);
comp->x += base->borderWidth.value;
comp->y += base->borderWidth.value;
@ -234,6 +267,8 @@ void djui_base_render(struct DjuiBase* base) {
base->render(base);
}
djui_base_add_padding(base);
// render all children
struct DjuiBaseChild* child = base->child;
while (child != NULL) {

View file

@ -15,6 +15,14 @@ struct DjuiBaseChild {
struct DjuiBaseChild* next;
};
#pragma pack(1)
struct DjuiBasePadding {
struct DjuiScreenValue top;
struct DjuiScreenValue right;
struct DjuiScreenValue bottom;
struct DjuiScreenValue left;
};
#pragma pack(1)
struct DjuiBase {
struct DjuiBase* parent;
@ -27,6 +35,7 @@ struct DjuiBase {
struct DjuiColor color;
struct DjuiScreenValue borderWidth;
struct DjuiColor borderColor;
struct DjuiBasePadding padding;
enum DjuiHAlign hAlign;
enum DjuiVAlign vAlign;
struct DjuiBaseRect comp;
@ -38,11 +47,13 @@ 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_type(struct DjuiBase* base, f32 widthType, f32 heightType);
void djui_base_set_size_type(struct DjuiBase* base, enum DjuiScreenValueType widthType, enum DjuiScreenValueType heightType);
void djui_base_set_color(struct DjuiBase* base, u8 r, u8 g, u8 b, u8 a);
void djui_base_set_border_width(struct DjuiBase* base, f32 width);
void djui_base_set_border_width_type(struct DjuiBase* base, enum DjuiScreenValueType widthType);
void djui_base_set_border_color(struct DjuiBase* base, u8 r, u8 g, u8 b, u8 a);
void djui_base_set_padding(struct DjuiBase* base, f32 top, f32 right, f32 bottom, f32 left);
void djui_base_set_padding_type(struct DjuiBase* base, enum DjuiScreenValueType topType, enum DjuiScreenValueType rightType, enum DjuiScreenValueType bottomType, enum DjuiScreenValueType leftType);
void djui_base_set_alignment(struct DjuiBase* base, enum DjuiHAlign hAlign, enum DjuiVAlign vAlign);
void djui_base_compute(struct DjuiBase* base);

View file

@ -22,6 +22,13 @@ void djui_text_set_font_size(struct DjuiText* text, f32 fontSize) {
text->fontSize = fontSize;
}
void djui_text_set_drop_shadow(struct DjuiText* text, f32 r, f32 g, f32 b, f32 a) {
text->dropShadow.r = r;
text->dropShadow.g = g;
text->dropShadow.b = b;
text->dropShadow.a = a;
}
void djui_text_set_alignment(struct DjuiText* text, enum DjuiHAlign hAlign, enum DjuiVAlign vAlign) {
text->textHAlign = hAlign;
text->textVAlign = vAlign;

View file

@ -6,12 +6,14 @@ struct DjuiText {
struct DjuiBase base;
char* message;
f32 fontSize;
struct DjuiColor dropShadow;
enum DjuiHAlign textHAlign;
enum DjuiVAlign textVAlign;
};
void djui_text_set_text(struct DjuiText* text, const char* message);
void djui_text_set_font_size(struct DjuiText* text, f32 fontSize);
void djui_text_set_drop_shadow(struct DjuiText* text, f32 r, f32 g, f32 b, f32 a);
void djui_text_set_alignment(struct DjuiText* text, enum DjuiHAlign hAlign, enum DjuiVAlign vAlign);
struct DjuiText* djui_text_create(struct DjuiBase* parent, const char* message);