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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#include "saffron_theme.h"
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <stdio.h>
#include <SDL3/SDL_events.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <saffron.h>
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, w, h);
window->title = title;
window->w = w;
window->h = h;
memset(window->hooks, 0, sizeof(window->hooks));
window->hook_count = 0;
Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
window->sdl_window = SDL_CreateWindow(title, w, h, flags);
window->renderer = SDL_CreateRenderer(window->sdl_window, NULL);
window->root->theme = SF_MACRO_DEFAULT_THEME;
return window;
}
void saffron_window_free(SaffronWindow* window) {
SDL_DestroyRenderer(window->renderer);
SDL_DestroyWindow(window->sdl_window);
saffron_widget_free(window->root);
free(window);
}
static void handle_mouse_down(SDL_Event* event, SaffronWindow* window) {
if (event->type != SDL_EVENT_MOUSE_BUTTON_DOWN) return;
printf("[Saffron] mouse down!\n");
if (event->button.button == SDL_BUTTON_LEFT) {
printf("[Saffron] left click at %f, %f!\n", event->button.x, event->button.y);
printf("[Saffron] calling hit test at coordinates\n");
SaffronWidget* hit = saffron_widget_hit_test(window->root, (int)event->button.x, (int)event->button.y);
if (hit && hit->on_click) hit->on_click(hit);
}
// TODO: handle right click
}
static void handle_window_resized(SDL_Event* event, SaffronWindow* window) {
if (event->type != SDL_EVENT_WINDOW_RESIZED) return;
printf("[Saffron] window resized!\n");
int new_w, new_h;
SDL_GetWindowSize(window->sdl_window, &new_w, &new_h);
window->w = window->root->w = new_w;
window->h = window->root->h = new_h;
printf("[Saffron] new dimensions: %dx%d\n", new_w, new_h);
printf("[Saffron] recalculating layout on window->root\n");
saffron_box_layout((SaffronBox*)window->root);
}
void saffron_window_main(SaffronWindow *window) {
if (!window) return;
bool running = true;
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) {
running = false;
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
handle_mouse_down(&event, window);
}
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
handle_window_resized(&event, window);
}
for (int i = 0; i < window->hook_count; i++) {
bool consumed = window->hooks[i].callback(&event);
if (consumed) break;
}
}
SDL_SetRenderDrawColor(window->renderer, 0, 0, 0, 255);
SDL_RenderClear(window->renderer);
saffron_widget_draw(window->root, window->renderer);
SDL_RenderPresent(window->renderer);
SDL_Delay(16); // around 60fps or so
}
}
|