aboutsummaryrefslogtreecommitdiff
path: root/tests/test_main.c
blob: 087b32ad473a868e714a57269b98ca385c7e3c55 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "saffron_api.h"
#include "saffron_layout.h"
#include "saffron_theme.h"
#include <stdio.h>
#include <saffron.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_render.h>

/* PLEASE don't do this in production!
 * when wrappers are implemented,
 * THEY will make draw functions for you
 * THIS IS A BAD IDEA */
void my_test_draw(SaffronWidget* self, SDL_Renderer* renderer) {
	/* make use of the theme! */
	SaffronTheme* theme = self->theme;
	if (!theme) return; // good luck
	SaffronColor secondary = theme->secondary;
	SaffronColor tertiary = theme->tertiary;
	SDL_SetRenderDrawColor(renderer, secondary.r, secondary.g, secondary.b, secondary.a);
	SDL_FRect rect = {self->x, self->y, self->w, self->h};
	SDL_RenderFillRect(renderer, &rect);

	SDL_SetRenderDrawColor(renderer, tertiary.r, tertiary.g, tertiary.b, tertiary.a);
	SDL_RenderRect(renderer, &rect);
}

/* YET AGAIN DONT DO THIS IN PROD PLEASE
 * this is very lunatic */
void my_test_onclick(SaffronWidget* self) {
	printf("clicked!\n");
}

static void button_click_handler(SaffronButton* self) {
	printf("button clicked!\n");
}

int main(void) {
	saffron_init();

	SaffronWindow* window = saffron_window_new("Saffron Test", 800, 600);

	/* replace the root with our own, because we want horizontal */
	saffron_widget_free(window->root);
	window->root = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_LEFT, SAFFRON_VALIGN_TOP, 5, 5, 0, window->w, window->h);
	window->root->theme = SF_MACRO_DEFAULT_THEME;

	/* i guess IM THE LUNATIC NOW
	 * DEAL WITH IT */
	SaffronWidget* test = saffron_widget_new();
	/* we don't need to set X and Y, the window will layout them automatically */
	test->w = 200;
	test->h = 150;
	test->draw = my_test_draw;
	test->on_click = my_test_onclick;

	/* make a vertical box */
	SaffronWidget* box = (SaffronWidget*)saffron_box_new(SAFFRON_ORIENTATION_VERTICAL, SAFFRON_HALIGN_LEFT, SAFFRON_VALIGN_TOP, 5, 0, 0, 200, 150);

	/* lunatic method 2 */
	SaffronWidget* test2 = saffron_widget_new();
	test2->w = 200;
	test2->h = 72;
	test2->draw = my_test_draw;

	TTF_Font* font = TTF_OpenFont("/usr/share/fonts/fantasque-sans-mono/FantasqueSansMono-Regular.otf", 24);
	SaffronText* test3 = saffron_text_new("Mangoes", font);
	SaffronButton* btn = saffron_button_new_with_text(test3, true, &button_click_handler, 200, 150);

	saffron_widget_add_child(window->root, test);
	saffron_widget_add_child(window->root, box);
	saffron_widget_add_child(box, test2);
	saffron_widget_add_child(window->root, (SaffronWidget*)btn);

	saffron_window_main(window);

	saffron_window_free(window);
	TTF_CloseFont(font);
	saffron_quit();
	return 0;
}