diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/saffron_layout.c | 29 | ||||
| -rw-r--r-- | src/saffron_widget.c | 2 | ||||
| -rw-r--r-- | src/saffron_window.c | 12 |
3 files changed, 37 insertions, 6 deletions
diff --git a/src/saffron_layout.c b/src/saffron_layout.c index c714bfa..62c7de2 100644 --- a/src/saffron_layout.c +++ b/src/saffron_layout.c @@ -1,5 +1,6 @@ #include "saffron_layout.h" #include "saffron_api.h" +#include "saffron_widget.h" #include <SDL3/SDL_video.h> #include <stdio.h> #include <stdlib.h> @@ -7,12 +8,14 @@ #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) { +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; @@ -36,6 +39,28 @@ void saffron_box_layout(SaffronBox* box) { int inner_w = content_w - (box->padding * 2); int inner_h = content_h - (box->padding * 2); - /* TODO actually make it layout */ + 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; } diff --git a/src/saffron_widget.c b/src/saffron_widget.c index 8612557..779253b 100644 --- a/src/saffron_widget.c +++ b/src/saffron_widget.c @@ -20,6 +20,8 @@ void saffron_widget_init(SaffronWidget* widget) { widget->width_mode = SAFFRON_SIZE_FIXED; widget->height_mode = SAFFRON_SIZE_FIXED; widget->pixel_perfect = false; + + widget->type = SAFFRON_WIDGET_UNKNOWN; } SaffronWidget* saffron_widget_new(void) { diff --git a/src/saffron_window.c b/src/saffron_window.c index 74742fa..5687f16 100644 --- a/src/saffron_window.c +++ b/src/saffron_window.c @@ -9,10 +9,10 @@ SaffronWindow* saffron_window_new(const char* title, int w, int h) { SaffronWindow* window = malloc(sizeof(SaffronWindow)); - window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 10, 10, 0); + window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 10, 10, 0, w, h); window->title = title; - window->w = window->root->w = w; - window->h = window->root->h = h; + window->w = w; + window->h = h; Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; window->sdl_window = SDL_CreateWindow(title, w, h, flags); @@ -61,9 +61,13 @@ void saffron_window_main(SaffronWindow *window) { if (!window) return; bool running = true; - printf("[Saffron] window running!\n"); + printf("[Saffron] window starting!\n"); SDL_Event event; + printf("[Saffron] calculating layout on window->root\n"); + saffron_box_layout((SaffronBox*)window->root); + + printf("[Saffron] starting window mainloop\n"); while (running) { while (SDL_PollEvent(&event)) { if (event.type == SDL_EVENT_QUIT) { |
