zenthra-widgets 0.1.2

Immediate-mode UI widgets (buttons, text fields, lists) for the Zenthra UI framework
Documentation
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::ui::{DrawCommand, ImageDraw, Ui};
use zenthra_core::{Color, EdgeInsets, Id, Rect, Role, SemanticNode, ImageSource, ObjectFit};
use zenthra_render::ImageInstance;

pub struct ImageBuilder<'u, 'a> {
    ui: &'u mut Ui<'a>,
    id: Id,
    source: ImageSource,

    // Sizing & Position
    pos: Option<(f32, f32)>,
    width: Option<f32>,
    height: Option<f32>,
    max_width: Option<f32>,
    max_height: Option<f32>,
    aspect_ratio: Option<f32>,
    original_size: bool,

    // Spacing
    padding: EdgeInsets,
    margin: EdgeInsets,

    // Appearance (Idle State)
    fit: ObjectFit,
    radius: [f32; 4],
    border_color: Color,
    border_width: f32,
    bg: Color,
    opacity: f32,
    grayscale: f32,
    shadow_color: Color,
    shadow_offset: [f32; 2],
    shadow_blur: f32,
    shadow_opacity: f32,

    // Interaction Styling (Hover & Active States)
    hover_opacity: Option<f32>,
    hover_grayscale: Option<f32>,
    hover_border: Option<Color>,
    active_opacity: Option<f32>,

    // Advanced Controls
    internal_scale: [f32; 2],
    internal_offset: [f32; 2],
    rotation: [f32; 3], // (x, y, z)
    flip_h: bool,
    flip_v: bool,

    // Framework Mechanics
    cursor: crate::text::CursorIcon,
    render_mode: Option<zenthra_core::RenderMode>,
}

impl<'u, 'a> ImageBuilder<'u, 'a> {
    pub fn new(ui: &'u mut Ui<'a>, source: ImageSource) -> Self {
        let id = ui.id();
        Self {
            ui,
            id,
            source,
            pos: None,
            width: None,
            height: None,
            max_width: None,
            max_height: None,
            padding: EdgeInsets::ZERO,
            margin: EdgeInsets::ZERO,
            fit: ObjectFit::Contain,
            radius: [0.0; 4],
            border_color: Color::TRANSPARENT,
            border_width: 0.0,
            bg: Color::TRANSPARENT,
            opacity: 1.0,
            grayscale: 0.0,
            shadow_color: Color::TRANSPARENT,
            shadow_offset: [0.0, 0.0],
            shadow_blur: 0.0,
            shadow_opacity: 1.0,
            hover_opacity: None,
            hover_grayscale: None,
            hover_border: None,
            active_opacity: None,
            aspect_ratio: None,
            original_size: false,
            internal_scale: [1.0, 1.0],
            internal_offset: [0.0; 2],
            rotation: [0.0; 3],
            flip_h: false,
            flip_v: false,
            cursor: crate::text::CursorIcon::Default,
            render_mode: None,
        }
    }

    pub fn pos(mut self, x: f32, y: f32) -> Self {
        self.pos = Some((x, y));
        self
    }
    pub fn width(mut self, w: f32) -> Self {
        self.width = Some(w);
        self
    }
    pub fn height(mut self, h: f32) -> Self {
        self.height = Some(h);
        self
    }
    pub fn size(mut self, w: f32, h: f32) -> Self {
        self.width = Some(w);
        self.height = Some(h);
        self
    }
    pub fn max_width(mut self, w: f32) -> Self {
        self.max_width = Some(w);
        self
    }
    pub fn max_height(mut self, h: f32) -> Self {
        self.max_height = Some(h);
        self
    }
    pub fn aspect_ratio(mut self, ratio: f32) -> Self {
        self.aspect_ratio = Some(ratio);
        self
    }
    pub fn original_size(mut self) -> Self {
        self.original_size = true;
        self
    }

    // Padding
    pub fn padding(mut self, t: f32, r: f32, b: f32, l: f32) -> Self {
        self.padding = EdgeInsets { top: t, right: r, bottom: b, left: l };
        self
    }
    pub fn padding_all(mut self, p: f32) -> Self {
        self.padding = EdgeInsets { top: p, right: p, bottom: p, left: p };
        self
    }
    pub fn padding_x(mut self, p: f32) -> Self {
        self.padding.left = p;
        self.padding.right = p;
        self
    }
    pub fn padding_y(mut self, p: f32) -> Self {
        self.padding.top = p;
        self.padding.bottom = p;
        self
    }
    pub fn padding_top(mut self, p: f32) -> Self {
        self.padding.top = p;
        self
    }
    pub fn padding_bottom(mut self, p: f32) -> Self {
        self.padding.bottom = p;
        self
    }
    pub fn padding_left(mut self, p: f32) -> Self {
        self.padding.left = p;
        self
    }
    pub fn padding_right(mut self, p: f32) -> Self {
        self.padding.right = p;
        self
    }

    // Margin
    pub fn margin(mut self, t: f32, r: f32, b: f32, l: f32) -> Self {
        self.margin = EdgeInsets { top: t, right: r, bottom: b, left: l };
        self
    }
    pub fn margin_x(mut self, m: f32) -> Self {
        self.margin.left = m;
        self.margin.right = m;
        self
    }
    pub fn margin_y(mut self, m: f32) -> Self {
        self.margin.top = m;
        self.margin.bottom = m;
        self
    }
    pub fn margin_top(mut self, m: f32) -> Self {
        self.margin.top = m;
        self
    }
    pub fn margin_bottom(mut self, m: f32) -> Self {
        self.margin.bottom = m;
        self
    }
    pub fn margin_left(mut self, m: f32) -> Self {
        self.margin.left = m;
        self
    }
    pub fn margin_right(mut self, m: f32) -> Self {
        self.margin.right = m;
        self
    }

    // Appearance
    pub fn fit(mut self, fit: ObjectFit) -> Self {
        self.fit = fit;
        self
    }
    pub fn border_radius(mut self, radius: f32) -> Self {
        self.radius = [radius; 4] ;
        self
    }

    pub fn radius(mut self, tl: f32, tr: f32, br: f32, bl: f32) -> Self {
        self.radius = [tl, tr, br, bl];
        self
    }

    pub fn radius_all(mut self, r: f32) -> Self {
        self.radius = [r; 4];
        self
    }

    pub fn radius_top(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self.radius[1] = r;
        self
    }

    pub fn radius_bottom(mut self, r: f32) -> Self {
        self.radius[2] = r;
        self.radius[3] = r;
        self
    }

    pub fn radius_top_left(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self
    }

    pub fn radius_top_right(mut self, r: f32) -> Self {
        self.radius[1] = r;
        self
    }

    pub fn radius_bottom_right(mut self, r: f32) -> Self {
        self.radius[2] = r;
        self
    }

    pub fn radius_bottom_left(mut self, r: f32) -> Self {
        self.radius[3] = r;
        self
    }

    pub fn radius_left(mut self, r: f32) -> Self {
        self.radius[0] = r;
        self.radius[3] = r;
        self
    }

    pub fn radius_right(mut self, r: f32) -> Self {
        self.radius[1] = r;
        self.radius[2] = r;
        self
    }
    pub fn border(mut self, color: Color, width: f32) -> Self {
        self.border_color = color;
        self.border_width = width;
        self
    }
    pub fn bg(mut self, color: Color) -> Self {
        self.bg = color;
        self
    }
    pub fn opacity(mut self, alpha: f32) -> Self {
        self.opacity = alpha;
        self
    }
    pub fn grayscale(mut self, amount: f32) -> Self {
        self.grayscale = amount;
        self
    }
    pub fn shadow(mut self, color: Color, x: f32, y: f32, blur: f32) -> Self {
        self.shadow_color = color;
        self.shadow_offset = [x, y];
        self.shadow_blur = blur;
        self
    }
    pub fn shadow_opacity(mut self, opacity: f32) -> Self {
        self.shadow_opacity = opacity;
        self
    }

    // Advanced Controls
    pub fn scale(mut self, s: f32) -> Self {
        self.internal_scale = [s, s];
        self
    }
    pub fn scale_x(mut self, x: f32) -> Self {
        self.internal_scale[0] = x;
        self
    }
    pub fn scale_y(mut self, y: f32) -> Self {
        self.internal_scale[1] = y;
        self
    }
    pub fn zoom(mut self, z: f32) -> Self {
        self.internal_scale = [z, z];
        self
    }
    pub fn offset(mut self, x: f32, y: f32) -> Self {
        self.internal_offset = [x, y];
        self
    }
    pub fn rotate(mut self, x: f32, y: f32, z: f32) -> Self {
        self.rotation = [x.to_radians(), y.to_radians(), z.to_radians()];
        self
    }
    pub fn rotate_x(mut self, x: f32) -> Self {
        self.rotation[0] = x.to_radians();
        self
    }
    pub fn rotate_y(mut self, y: f32) -> Self {
        self.rotation[1] = y.to_radians();
        self
    }
    pub fn rotate_z(mut self, z: f32) -> Self {
        self.rotation[2] = z.to_radians();
        self
    }
    pub fn flip_h(mut self, flip: bool) -> Self {
        self.flip_h = flip;
        self
    }
    pub fn flip_v(mut self, flip: bool) -> Self {
        self.flip_v = flip;
        self
    }

    // Interaction Styling
    pub fn hover_opacity(mut self, alpha: f32) -> Self {
        self.hover_opacity = Some(alpha);
        self
    }
    pub fn hover_grayscale(mut self, amount: f32) -> Self {
        self.hover_grayscale = Some(amount);
        self
    }
    pub fn hover_border(mut self, color: Color) -> Self {
        self.hover_border = Some(color);
        self
    }
    pub fn active_opacity(mut self, alpha: f32) -> Self {
        self.active_opacity = Some(alpha);
        self
    }

    // Framework
    pub fn id(mut self, id: impl std::hash::Hash) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        use std::hash::{Hash, Hasher};
        id.hash(&mut hasher);
        if let Some(parent) = self.ui.semantic_stack.last() {
            parent.hash(&mut hasher);
        }
        self.id = zenthra_core::Id::from_u64(hasher.finish());
        self
    }
    pub fn cursor(mut self, c: crate::text::CursorIcon) -> Self {
        self.cursor = c;
        self
    }
    pub fn continuous(mut self) -> Self {
        self.render_mode = Some(zenthra_core::RenderMode::Continuous);
        self
    }
    pub fn static_mode(mut self) -> Self {
        self.render_mode = Some(zenthra_core::RenderMode::Static);
        self
    }

    pub fn show(self) -> zenthra_core::Response {
        if let Some(mode) = self.render_mode {
            self.ui.render_mode_stack.push(mode);
        }

        let start_x = self.pos.map(|p| p.0).unwrap_or(self.ui.cursor_x);
        let start_y = self.pos.map(|p| p.1).unwrap_or(self.ui.cursor_y);

        let (orig_w, orig_h) = self.ui.image_sizes.get(&self.source)
            .map(|(w, h)| (*w as f32, *h as f32))
            .unwrap_or((100.0, 100.0));

        let ratio = self.aspect_ratio.unwrap_or(orig_w / orig_h);

        let (final_w, final_h) = if self.original_size {
            (orig_w, orig_h)
        } else {
            match (self.width, self.height) {
                (Some(w), Some(h)) => (w, h),
                (Some(w), None) => (w, w / ratio),
                (None, Some(h)) => (h * ratio, h),
                (None, None) => {
                    if orig_w > self.ui.available_width && self.ui.available_width > 0.0 {
                        (self.ui.available_width, self.ui.available_width / ratio)
                    } else {
                        (orig_w, orig_h)
                    }
                }
            }
        };

        let w = if let Some(mw) = self.max_width { final_w.min(mw) } else { final_w };
        let h = if let Some(mh) = self.max_height { final_h.min(mh) } else { final_h };

        let draw_start = self.ui.draws.len();

        let local_x = start_x + self.margin.left;
        let local_y = start_y + self.margin.top;

        let is_hovered = self.ui.is_hovered(self.id, local_x + self.ui.offset_x, local_y + self.ui.offset_y, w, h);

        let is_pressed = is_hovered && self.ui.mouse_down;
        let clicked = self.ui.clicked && is_hovered;

        // Interaction state overrides
        let current_opacity = if is_pressed {
            self.active_opacity.unwrap_or(self.opacity)
        } else if is_hovered {
            self.hover_opacity.unwrap_or(self.opacity)
        } else {
            self.opacity
        };

        let current_grayscale = if is_hovered && !is_pressed {
            self.hover_grayscale.unwrap_or(self.grayscale)
        } else {
            self.grayscale
        };

        let current_border_color = if is_hovered && !is_pressed {
            self.hover_border.unwrap_or(self.border_color)
        } else {
            self.border_color
        };

        let shadow_color_arr = {
            let mut c = self.shadow_color;
            c.a *= self.shadow_opacity;
            c.to_array()
        };

        let uv_rect = if w <= 0.0 || h <= 0.0 || orig_w <= 0.0 || orig_h <= 0.0 {
            [0.0, 0.0, 1.0, 1.0]
        } else {
            let r_t = orig_w / orig_h;
            let r_w = w / h;
            let base_uv = match self.fit {
                ObjectFit::Fill => [0.0, 0.0, 1.0, 1.0],
                ObjectFit::Cover => {
                    if r_t > r_w {
                        let u_size = r_w / r_t;
                        let u_start = (1.0 - u_size) / 2.0;
                        [u_start, 0.0, u_size, 1.0]
                    } else {
                        let v_size = r_t / r_w;
                        let v_start = (1.0 - v_size) / 2.0;
                        [0.0, v_start, 1.0, v_size]
                    }
                }
                ObjectFit::Contain => {
                    if r_t > r_w {
                        let v_size = r_t / r_w;
                        let v_start = -0.5 * (v_size - 1.0);
                        [0.0, v_start, 1.0, v_size]
                    } else {
                        let u_size = r_w / r_t;
                        let u_start = -0.5 * (u_size - 1.0);
                        [u_start, 0.0, u_size, 1.0]
                    }
                }
                ObjectFit::None => {
                    let u_size = w / orig_w;
                    let u_start = -0.5 * (u_size - 1.0);
                    let v_size = h / orig_h;
                    let v_start = -0.5 * (v_size - 1.0);
                    [u_start, v_start, u_size, v_size]
                }
                ObjectFit::ScaleDown => {
                    if orig_w > w || orig_h > h {
                        // Behave like Contain
                        if r_t > r_w {
                            let v_size = r_t / r_w;
                            let v_start = -0.5 * (v_size - 1.0);
                            [0.0, v_start, 1.0, v_size]
                        } else {
                            let u_size = r_w / r_t;
                            let u_start = -0.5 * (u_size - 1.0);
                            [u_start, 0.0, u_size, 1.0]
                        }
                    } else {
                        // Behave like None
                        let u_size = w / orig_w;
                        let u_start = -0.5 * (u_size - 1.0);
                        let v_size = h / orig_h;
                        let v_start = -0.5 * (v_size - 1.0);
                        [u_start, v_start, u_size, v_size]
                    }
                }
            };

            let scale_x = self.internal_scale[0].max(0.001);
            let scale_y = self.internal_scale[1].max(0.001);

            let new_u_size = base_uv[2] / scale_x;
            let new_v_size = base_uv[3] / scale_y;

            let new_u_start = base_uv[0] + (base_uv[2] - new_u_size) / 2.0;
            let new_v_start = base_uv[1] + (base_uv[3] - new_v_size) / 2.0;

            let final_u_start = new_u_start - (self.internal_offset[0] / w) * new_u_size;
            let final_v_start = new_v_start - (self.internal_offset[1] / h) * new_v_size;

            [final_u_start, final_v_start, new_u_size, new_v_size]
        };

        self.ui.draws.push(DrawCommand::Image(ImageDraw {
            source: self.source.clone(),
            fit: self.fit,
            internal_scale: self.internal_scale,
            internal_offset: self.internal_offset,
            instance: ImageInstance {
                pos: [local_x, local_y],
                size: [w, h],
                radius: self.radius,
                border_width: self.border_width,
                border_color: current_border_color.to_array(),
                shadow_color: shadow_color_arr,
                shadow_offset: self.shadow_offset,
                shadow_blur: self.shadow_blur,
                clip_rect: [0.0, 0.0, 9999.0, 9999.0], // Managed by containers
                grayscale: current_grayscale,
                brightness: 1.0,
                opacity: current_opacity,
                uv_rect,
                bg_color: self.bg.to_array(),
                rotation: self.rotation,
                flip: [if self.flip_h { -1.0 } else { 1.0 }, if self.flip_v { -1.0 } else { 1.0 }],
            },
        }));

        let total_w = w + self.margin.horizontal();
        let total_h = h + self.margin.vertical();

        self.ui.register_semantic(
            SemanticNode::new(self.id, Role::Image, Rect::new(start_x, start_y, total_w, total_h))
        );

        self.ui.record_layout(self.id, Rect::new(start_x, start_y, total_w, total_h));
        
        // Only advance cursor if not absolutely positioned
        if self.pos.is_none() {
            self.ui.advance(total_w, total_h, draw_start);
        }

        if self.render_mode.is_some() {
            self.ui.render_mode_stack.pop();
        }

        zenthra_core::Response {
            clicked,
            hovered: is_hovered,
            pressed: is_pressed,
        }
    }
}