@tty-pt/lamina 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,426 @@
1
+ #ifndef LAM_UI_H
2
+ #define LAM_UI_H
3
+
4
+ /**
5
+ * @file lam-ui.h
6
+ * @brief Public header for the liblamina UI system.
7
+ *
8
+ * Declares the API for liblamina’s immediate-mode
9
+ * layout and rendering system. Provides a small,
10
+ * CSS-like model for composing 2D interface
11
+ * elements (divs) with flexible layout and style.
12
+ */
13
+
14
+ #include "lam.h"
15
+ #include <stdint.h>
16
+ #include <stddef.h>
17
+
18
+ #include <ttypt/corm.h>
19
+
20
+ #ifdef __cplusplus
21
+ extern "C" {
22
+ #endif
23
+
24
+ /**
25
+ * @brief Assign a style field on a div.
26
+ *
27
+ * Convenience macro to set a single field of a style
28
+ * struct by name, without manually calculating offsets.
29
+ *
30
+ * @param[in] div Target div.
31
+ * @param[in] field Field name inside lam_ui_style_t.
32
+ * @param[in] val Field value to assign.
33
+ */
34
+ #define LAM_UI_STYLE(div, field, val) \
35
+ do { \
36
+ lam_ui_style_t dummy; \
37
+ dummy.field = val; \
38
+ lam_ui_style_set(div, offsetof(lam_ui_style_t, field), &dummy.field, sizeof(dummy.field)); \
39
+ } while (0)
40
+
41
+ /** @defgroup lam_ui_types liblamina UI types
42
+ * @brief Fundamental type definitions.
43
+ * @{
44
+ */
45
+
46
+ /**
47
+ * @brief Automatic layout value.
48
+ *
49
+ * Used for style properties like `top`, `left`, `flex_basis`,
50
+ * etc., to indicate that the layout engine should
51
+ * determine the value automatically.
52
+ */
53
+ static const int32_t LAM_UI_AUTO = -1;
54
+
55
+ /**
56
+ * @brief Layout direction for container elements (CSS: flex-direction).
57
+ */
58
+ typedef enum {
59
+ LAM_UI_COLUMN,
60
+ LAM_UI_ROW,
61
+ } lam_ui_flex_direction_t;
62
+
63
+ /**
64
+ * @brief Positioning mode for elements (CSS: position).
65
+ */
66
+ typedef enum {
67
+ LAM_UI_POSITION_RELATIVE,
68
+ LAM_UI_POSITION_ABSOLUTE,
69
+ } lam_ui_position_mode_t;
70
+
71
+ /**
72
+ * @brief Display mode for visibility control (CSS: display).
73
+ */
74
+ typedef enum {
75
+ LAM_UI_DISPLAY_BLOCK,
76
+ LAM_UI_DISPLAY_INLINE,
77
+ LAM_UI_DISPLAY_FLEX,
78
+ LAM_UI_DISPLAY_NONE,
79
+ } lam_ui_display_mode_t;
80
+
81
+ /**
82
+ * @brief Alignment along cross axis (CSS: align-items, align-self).
83
+ */
84
+ typedef enum {
85
+ LAM_UI_ALIGN_STRETCH = 0,
86
+ LAM_UI_ALIGN_FLEX_START,
87
+ LAM_UI_ALIGN_CENTER,
88
+ LAM_UI_ALIGN_FLEX_END,
89
+ LAM_UI_ALIGN_AUTO = -1,
90
+ } lam_ui_align_mode_t;
91
+
92
+ /**
93
+ * @brief Alignment of items along the main axis (CSS: justify-content).
94
+ */
95
+ typedef enum {
96
+ LAM_UI_JUSTIFY_FLEX_START = 0,
97
+ LAM_UI_JUSTIFY_CENTER,
98
+ LAM_UI_JUSTIFY_FLEX_END,
99
+ LAM_UI_JUSTIFY_SPACE_BETWEEN,
100
+ LAM_UI_JUSTIFY_SPACE_AROUND
101
+ } lam_ui_justify_content_mode_t;
102
+
103
+ /**
104
+ * @brief Text alignment mode (CSS: text-align).
105
+ */
106
+ typedef enum {
107
+ LAM_UI_TEXT_ALIGN_LEFT = 0,
108
+ LAM_UI_TEXT_ALIGN_CENTER,
109
+ LAM_UI_TEXT_ALIGN_RIGHT
110
+ } lam_ui_text_align_t;
111
+
112
+ /**
113
+ * @brief White-space handling (CSS: white-space).
114
+ */
115
+ typedef enum {
116
+ LAM_UI_WS_NORMAL = 0,
117
+ LAM_UI_WS_NOWRAP,
118
+ LAM_UI_WS_PRE,
119
+ LAM_UI_WS_PRE_WRAP,
120
+ LAM_UI_WS_PRE_LINE
121
+ } lam_ui_white_space_t;
122
+
123
+ /**
124
+ * @brief Word breaking behavior (CSS: word-break).
125
+ */
126
+ typedef enum {
127
+ LAM_UI_WB_NORMAL = 0,
128
+ LAM_UI_WB_BREAK_ALL,
129
+ LAM_UI_WB_KEEP_ALL,
130
+ LAM_UI_WB_BREAK_WORD
131
+ } lam_ui_word_break_t;
132
+
133
+ /**
134
+ * @brief Overflow handling for the vertical axis.
135
+ */
136
+ typedef enum {
137
+ LAM_UI_OVERFLOW_HIDDEN = 0,
138
+ LAM_UI_OVERFLOW_VISIBLE,
139
+ } lam_ui_overflow_t;
140
+
141
+ /**
142
+ * @brief Visual style definition for a UI element.
143
+ *
144
+ * Represents colors, borders, fonts and layout
145
+ * metrics. Styles may be defined globally in a
146
+ * stylesheet and applied by class name.
147
+ */
148
+ typedef struct {
149
+ uint32_t background_color;
150
+ uint32_t border_color;
151
+ uint32_t border_width;
152
+ uint32_t background_image_ref;
153
+ uint32_t font_family_ref;
154
+ uint32_t font_size;
155
+ uint32_t color;
156
+
157
+ uint32_t padding_left;
158
+ uint32_t padding_right;
159
+ uint32_t padding_top;
160
+ uint32_t padding_bottom;
161
+
162
+ /* positioning */
163
+ lam_ui_position_mode_t position;
164
+ int32_t top;
165
+ int32_t left;
166
+ int32_t right;
167
+ int32_t bottom;
168
+
169
+ lam_ui_display_mode_t display;
170
+
171
+ /* flexbox layout */
172
+ lam_ui_flex_direction_t flex_direction;
173
+ float flex_grow;
174
+ float flex_shrink;
175
+ int32_t flex_basis;
176
+
177
+ /* flex alignment properties */
178
+ lam_ui_align_mode_t align_items;
179
+ lam_ui_align_mode_t align_self;
180
+ lam_ui_justify_content_mode_t justify_content;
181
+
182
+ uint32_t border_radius_top_left;
183
+ uint32_t border_radius_top_right;
184
+ uint32_t border_radius_bottom_right;
185
+ uint32_t border_radius_bottom_left;
186
+
187
+ uint32_t box_shadow_color;
188
+ float box_shadow_blur;
189
+ float box_shadow_offset_x;
190
+ float box_shadow_offset_y;
191
+
192
+ lam_ui_text_align_t text_align;
193
+ lam_ui_white_space_t white_space;
194
+ lam_ui_word_break_t word_break;
195
+
196
+ int32_t width, height;
197
+
198
+ lam_ui_overflow_t overflow_y;
199
+ } lam_ui_style_t;
200
+
201
+ /**
202
+ * @brief Opaque UI element.
203
+ *
204
+ * Each div represents a node in the layout tree.
205
+ */
206
+ typedef struct lam_ui_div lam_ui_div_t;
207
+
208
+ /** @} */
209
+
210
+ /** @defgroup lam_ui_core liblamina UI initialization and stylesheet
211
+ * @brief Core setup and stylesheet management.
212
+ * @{
213
+ */
214
+
215
+ /**
216
+ * @brief Initialize the UI subsystem.
217
+ */
218
+ void lam_ui_init(uint32_t screen_w, uint32_t screen_h);
219
+
220
+ /**
221
+ * @brief Initialize (or reset) a stylesheet.
222
+ */
223
+ uint32_t lam_ui_stylesheet_init(void);
224
+
225
+ /**
226
+ * @brief Add a new class rule to a stylesheet.
227
+ */
228
+ static inline void lam_ui_stylesheet_add(uint32_t ss,
229
+ const char *class_name,
230
+ const lam_ui_style_t *style)
231
+ {
232
+ corm_put(ss, class_name, style);
233
+ }
234
+
235
+ /** @} */
236
+
237
+ /** @defgroup lam_ui_tree liblamina UI element tree
238
+ * @brief Element creation and configuration.
239
+ * @{
240
+ */
241
+
242
+ /**
243
+ * @brief Create a new UI element.
244
+ */
245
+ lam_ui_div_t *lam_ui_new(lam_ui_div_t *parent, lam_ui_style_t *style);
246
+
247
+ /**
248
+ * @brief Create a flexbox container element with display: flex and direction.
249
+ *
250
+ * @param[in] parent Parent element or NULL for root.
251
+ * @param[in] dir Flex direction (LAM_UI_ROW or LAM_UI_COLUMN).
252
+ * @return Pointer to new element.
253
+ */
254
+ lam_ui_div_t *lam_ui_flex(lam_ui_div_t *parent, lam_ui_flex_direction_t dir);
255
+
256
+ /**
257
+ * @brief Create a sized box element with background color.
258
+ *
259
+ * @param[in] parent Parent element or NULL for root.
260
+ * @param[in] w,h Width and height in pixels (or LAM_UI_AUTO).
261
+ * @param[in] bg_color Background color (packed RGBA).
262
+ * @return Pointer to new element.
263
+ */
264
+ lam_ui_div_t *lam_ui_box(lam_ui_div_t *parent, int32_t w, int32_t h, uint32_t bg_color);
265
+
266
+ /**
267
+ * @brief Create a text label element with font family and text color.
268
+ *
269
+ * @param[in] parent Parent element or NULL for root.
270
+ * @param[in] text UTF-8 text string.
271
+ * @param[in] font_ref Font handle (from lam_font_open).
272
+ * @param[in] color Text color (packed RGBA).
273
+ * @return Pointer to new element.
274
+ */
275
+ lam_ui_div_t *lam_ui_label(lam_ui_div_t *parent, const char *text, uint32_t font_ref, uint32_t color);
276
+
277
+ /**
278
+ * @brief Set explicit width and height dimensions on a div.
279
+ */
280
+ void lam_ui_set_size(lam_ui_div_t *div, int32_t w, int32_t h);
281
+
282
+ /**
283
+ * @brief Set background color on a div.
284
+ */
285
+ void lam_ui_set_bg(lam_ui_div_t *div, uint32_t color);
286
+
287
+ /**
288
+ * @brief Set padding on all four edges of a div.
289
+ */
290
+ void lam_ui_set_padding(lam_ui_div_t *div, uint32_t left, uint32_t top, uint32_t right, uint32_t bottom);
291
+
292
+ /**
293
+ * @brief Set flex properties (grow, shrink, basis) on a div.
294
+ */
295
+ void lam_ui_set_flex(lam_ui_div_t *div, float grow, float shrink, int32_t basis);
296
+
297
+ /**
298
+ * @brief Set alignment properties on a flex container.
299
+ */
300
+ void lam_ui_set_align(lam_ui_div_t *div, lam_ui_align_mode_t align_items, lam_ui_justify_content_mode_t justify_content);
301
+
302
+ /**
303
+ * @brief Assign a CSS-like class to a div.
304
+ */
305
+ void lam_ui_class(lam_ui_div_t *div, const char *class_name);
306
+
307
+ /**
308
+ * @brief Assign text content to a div.
309
+ */
310
+ void lam_ui_text(lam_ui_div_t *div, const char *text);
311
+
312
+ /**
313
+ * @brief Query overflow continuation text.
314
+ */
315
+ const char *lam_ui_overflow(const lam_ui_div_t *div);
316
+
317
+ /** @} */
318
+
319
+ /** @defgroup lam_ui_layout liblamina UI layout and rendering
320
+ * @brief Applying styles, computing geometry and drawing.
321
+ * @{
322
+ */
323
+
324
+ /**
325
+ * @brief Apply stylesheet rules and compute final styles.
326
+ */
327
+ void lam_ui_apply_styles(lam_ui_div_t *root, uint32_t ss);
328
+
329
+ /**
330
+ * @brief Compute layout positions and sizes.
331
+ */
332
+ void lam_ui_layout(lam_ui_div_t *root);
333
+
334
+ /**
335
+ * @brief Render the element tree.
336
+ */
337
+ void lam_ui_render(lam_ui_div_t *root);
338
+
339
+ /**
340
+ * @brief Recursively free a UI tree.
341
+ */
342
+ void lam_ui_clear(lam_ui_div_t *root);
343
+
344
+ /**
345
+ * @brief Reset a style struct to its default values.
346
+ */
347
+ void lam_ui_style_reset(lam_ui_style_t *s);
348
+
349
+ /**
350
+ * @brief Draw a rectangle with rounded corners and optional border.
351
+ */
352
+ void lam_border_radius(uint32_t background_color,
353
+ uint32_t border_color,
354
+ int32_t x, int32_t y,
355
+ uint32_t w, uint32_t h,
356
+ float tl, float tr,
357
+ float br, float bl,
358
+ float border_width);
359
+
360
+ /**
361
+ * @brief Draw a blurred drop shadow behind a rounded box.
362
+ */
363
+ void lam_box_shadow(uint32_t color,
364
+ int32_t x, int32_t y,
365
+ uint32_t w, uint32_t h,
366
+ float tl, float tr, float br, float bl,
367
+ float blur_radius, float offset_x, float offset_y);
368
+
369
+ /**
370
+ * @brief Mark a div as dirty so its cached render is rebuilt.
371
+ */
372
+ void lam_ui_mark_dirty(lam_ui_div_t *d);
373
+
374
+ /**
375
+ * @brief Set an inline style value by byte offset.
376
+ */
377
+ void lam_ui_style_set(lam_ui_div_t *root, size_t offset,
378
+ const void *value, size_t size);
379
+
380
+ /**
381
+ * @brief Get computed X position of a div after layout.
382
+ */
383
+ int32_t lam_ui_get_x(const lam_ui_div_t *div);
384
+
385
+ /**
386
+ * @brief Get computed Y position of a div after layout.
387
+ */
388
+ int32_t lam_ui_get_y(const lam_ui_div_t *div);
389
+
390
+ /**
391
+ * @brief Get computed width of a div after layout.
392
+ */
393
+ int32_t lam_ui_get_width(const lam_ui_div_t *div);
394
+
395
+ /**
396
+ * @brief Get computed height of a div after layout.
397
+ */
398
+ int32_t lam_ui_get_height(const lam_ui_div_t *div);
399
+
400
+ /**
401
+ * @brief Get content width of a div after layout.
402
+ */
403
+ uint32_t lam_ui_get_content_width(const lam_ui_div_t *div);
404
+
405
+ /**
406
+ * @brief Get content height of a div after layout.
407
+ */
408
+ uint32_t lam_ui_get_content_height(const lam_ui_div_t *div);
409
+
410
+ /**
411
+ * @brief Get text content of a div.
412
+ */
413
+ const char *lam_ui_get_text(const lam_ui_div_t *div);
414
+
415
+ /**
416
+ * @brief Get class name of a div.
417
+ */
418
+ const char *lam_ui_get_class(const lam_ui_div_t *div);
419
+
420
+ /** @} */
421
+
422
+ #ifdef __cplusplus
423
+ }
424
+ #endif
425
+
426
+ #endif /* LAM_UI_H */