aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_layout.c
blob: 62c7de2fccaae6e10be1e50682aa78c6fcdfa2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "saffron_layout.h"
#include "saffron_api.h"
#include "saffron_widget.h"
#include <SDL3/SDL_video.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <saffron.h>

SaffronBox* saffron_box_new(SaffronOrientation orientation, SaffronHorizontalAlignment halign, SaffronVerticalAlignment valign, int spacing, int padding, int margin, int width, int height) {
	SaffronBox* box = malloc(sizeof(SaffronBox));
	if (!box) return NULL;

	saffron_widget_init((SaffronWidget*)box);

	((SaffronWidget*)box)->type = SAFFRON_WIDGET_BOX;

	box->orientation = orientation;
	box->halign = halign;
	box->valign = valign;
	box->spacing = spacing;
	box->padding = padding;
	box->margin = margin;

	return box;
}

void saffron_box_layout(SaffronBox* box) {
	if (!box || ((SaffronWidget*)box)->child_count == 0) return;

	int content_x = ((SaffronWidget*)box)->x + box->margin;
	int content_y = ((SaffronWidget*)box)->y + box->margin;
	int content_w = ((SaffronWidget*)box)->w - (box->margin * 2);
	int content_h = ((SaffronWidget*)box)->h - (box->margin * 2);

	int inner_x = content_x + box->padding;
	int inner_y = content_y + box->padding;
	int inner_w = content_w - (box->padding * 2);
	int inner_h = content_h - (box->padding * 2);

	int x_offset = inner_x;
	int y_offset = inner_y;

	/* TODO make it account for stretch and stuff */
	for (int i = 0; i < ((SaffronWidget*)box)->child_count; i++) {
		SaffronWidget* child = ((SaffronWidget*)box)->children[i];

		if (child->pixel_perfect) continue;

		if (box->orientation == SAFFRON_ORIENTATION_HORIZONTAL) {
			child->x = x_offset;
			child->y = y_offset;
			x_offset += child->w + box->spacing;
		} else {
			child->x = x_offset;
			child->y = y_offset;
			y_offset += child->h + box->spacing;
		}

		if (child->type == SAFFRON_WIDGET_BOX) {
			saffron_box_layout((SaffronBox*)child);
		}
	}
	return;
}