proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
//! Screen-space UI layer — bypasses the 3D camera and renders in pixel coordinates.
//!
//! The UI layer renders AFTER the 3D scene and post-processing but BEFORE the
//! final composite.  UI elements are pixel-perfect, unaffected by bloom or
//! distortion, and positioned in screen coordinates: (0,0) = top-left.
//!
//! # Architecture
//!
//! ```text
//! 3D scene → PostFx (bloom, CA, grain) → UI Layer (ortho, no FX) → screen
//! ```
//!
//! The UI layer collects draw commands each frame via `UiLayer::draw_*` methods,
//! then flushes them all in one pass via `UiLayerRenderer`.

use glam::{Vec2, Vec3, Vec4, Mat4};
use std::collections::VecDeque;

// ── Draw Commands ───────────────────────────────────────────────────────────

/// A single UI draw command, queued and executed in order.
#[derive(Clone, Debug)]
pub enum UiDrawCommand {
    Text {
        text: String,
        x: f32,
        y: f32,
        scale: f32,
        color: Vec4,
        emission: f32,
        alignment: TextAlign,
    },
    Rect {
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        color: Vec4,
        filled: bool,
    },
    Panel {
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        border: BorderStyle,
        fill_color: Vec4,
        border_color: Vec4,
    },
    Bar {
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        fill_pct: f32,
        fill_color: Vec4,
        bg_color: Vec4,
        ghost_pct: Option<f32>,
        ghost_color: Vec4,
    },
    Sprite {
        lines: Vec<String>,
        x: f32,
        y: f32,
        color: Vec4,
    },
}

/// Text alignment.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum TextAlign {
    #[default]
    Left,
    Center,
    Right,
}

/// Border drawing styles for panels.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BorderStyle {
    /// Single line: ┌─┐│└─┘
    Single,
    /// Double line: ╔═╗║╚═╝
    Double,
    /// Rounded corners: ╭─╮│╰─╯
    Rounded,
    /// Heavy line: ┏━┓┃┗━┛
    Heavy,
    /// Dashed: ┌╌┐╎└╌┘
    Dashed,
}

impl BorderStyle {
    /// Get the 8 border characters: [top-left, top, top-right, left, right, bottom-left, bottom, bottom-right]
    pub fn chars(&self) -> [char; 8] {
        match self {
            BorderStyle::Single  => ['', '', '', '', '', '', '', ''],
            BorderStyle::Double  => ['', '', '', '', '', '', '', ''],
            BorderStyle::Rounded => ['', '', '', '', '', '', '', ''],
            BorderStyle::Heavy   => ['', '', '', '', '', '', '', ''],
            BorderStyle::Dashed  => ['', '', '', '', '', '', '', ''],
        }
    }
}

// ── UiLayer ─────────────────────────────────────────────────────────────────

/// The screen-space UI layer.  Collects draw commands each frame, then renders
/// them all in a single pass with an orthographic projection.
pub struct UiLayer {
    /// Screen dimensions (updated on resize).
    pub screen_width: f32,
    pub screen_height: f32,
    /// Character cell dimensions in screen pixels.
    pub char_width: f32,
    pub char_height: f32,
    /// Queued draw commands for this frame.
    draw_queue: Vec<UiDrawCommand>,
    /// Whether the UI layer is enabled.
    pub enabled: bool,
}

impl UiLayer {
    pub fn new(screen_width: f32, screen_height: f32) -> Self {
        Self {
            screen_width,
            screen_height,
            char_width: 10.0,
            char_height: 18.0,
            draw_queue: Vec::with_capacity(256),
            enabled: true,
        }
    }

    /// Update screen dimensions (call on resize).
    pub fn resize(&mut self, width: f32, height: f32) {
        self.screen_width = width;
        self.screen_height = height;
    }

    /// Set the character cell size in screen pixels.
    pub fn set_char_size(&mut self, width: f32, height: f32) {
        self.char_width = width;
        self.char_height = height;
    }

    /// Clear all queued commands. Call at the start of each frame.
    pub fn begin_frame(&mut self) {
        self.draw_queue.clear();
    }

    /// Get the orthographic projection matrix for this UI layer.
    /// Maps (0,0) at top-left to (screen_width, screen_height) at bottom-right.
    pub fn projection(&self) -> Mat4 {
        Mat4::orthographic_rh_gl(
            0.0,
            self.screen_width,
            self.screen_height,
            0.0,
            -1.0,
            1.0,
        )
    }

    /// Get the draw queue for rendering.
    pub fn draw_queue(&self) -> &[UiDrawCommand] {
        &self.draw_queue
    }

    /// Number of pending draw commands.
    pub fn command_count(&self) -> usize {
        self.draw_queue.len()
    }

    // ── Drawing API ─────────────────────────────────────────────────────────

    /// Draw text at screen coordinates.
    pub fn draw_text(&mut self, x: f32, y: f32, text: &str, scale: f32, color: Vec4) {
        self.draw_queue.push(UiDrawCommand::Text {
            text: text.to_string(),
            x, y, scale,
            color,
            emission: 0.0,
            alignment: TextAlign::Left,
        });
    }

    /// Draw text with emission (for bloom-capable UI text).
    pub fn draw_text_glowing(&mut self, x: f32, y: f32, text: &str, scale: f32, color: Vec4, emission: f32) {
        self.draw_queue.push(UiDrawCommand::Text {
            text: text.to_string(),
            x, y, scale,
            color,
            emission,
            alignment: TextAlign::Left,
        });
    }

    /// Draw text with alignment.
    pub fn draw_text_aligned(&mut self, x: f32, y: f32, text: &str, scale: f32, color: Vec4, align: TextAlign) {
        self.draw_queue.push(UiDrawCommand::Text {
            text: text.to_string(),
            x, y, scale,
            color,
            emission: 0.0,
            alignment: align,
        });
    }

    /// Draw centered text (centers horizontally at the given y).
    pub fn draw_centered_text(&mut self, y: f32, text: &str, scale: f32, color: Vec4) {
        self.draw_text_aligned(self.screen_width / 2.0, y, text, scale, color, TextAlign::Center);
    }

    /// Draw word-wrapped text within a max width (in pixels).
    pub fn draw_wrapped_text(&mut self, x: f32, y: f32, max_width: f32, text: &str, scale: f32, color: Vec4) {
        let char_w = self.char_width * scale;
        let max_chars = (max_width / char_w.max(1.0)) as usize;
        let lines = wrap_text_ui(text, max_chars);
        let line_h = self.char_height * scale;
        for (i, line) in lines.iter().enumerate() {
            self.draw_text(x, y + i as f32 * line_h, line, scale, color);
        }
    }

    /// Measure text dimensions in screen pixels.
    pub fn measure_text(&self, text: &str, scale: f32) -> (f32, f32) {
        let lines: Vec<&str> = text.lines().collect();
        let max_cols = lines.iter().map(|l| l.chars().count()).max().unwrap_or(0);
        let width = max_cols as f32 * self.char_width * scale;
        let height = lines.len() as f32 * self.char_height * scale;
        (width, height)
    }

    /// Draw a filled or outlined rectangle.
    pub fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32, color: Vec4, filled: bool) {
        self.draw_queue.push(UiDrawCommand::Rect {
            x, y, w, h, color, filled,
        });
    }

    /// Draw a panel with a border and optional fill.
    pub fn draw_panel(
        &mut self,
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        border: BorderStyle,
        fill_color: Vec4,
        border_color: Vec4,
    ) {
        self.draw_queue.push(UiDrawCommand::Panel {
            x, y, w, h, border, fill_color, border_color,
        });
    }

    /// Draw a progress bar using █ and ░ characters.
    pub fn draw_bar(
        &mut self,
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        fill_pct: f32,
        fill_color: Vec4,
        bg_color: Vec4,
    ) {
        self.draw_queue.push(UiDrawCommand::Bar {
            x, y, w, h,
            fill_pct: fill_pct.clamp(0.0, 1.0),
            fill_color,
            bg_color,
            ghost_pct: None,
            ghost_color: Vec4::ZERO,
        });
    }

    /// Draw a progress bar with a ghost bar (recent damage indicator).
    pub fn draw_bar_with_ghost(
        &mut self,
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        fill_pct: f32,
        fill_color: Vec4,
        bg_color: Vec4,
        ghost_pct: f32,
        ghost_color: Vec4,
    ) {
        self.draw_queue.push(UiDrawCommand::Bar {
            x, y, w, h,
            fill_pct: fill_pct.clamp(0.0, 1.0),
            fill_color,
            bg_color,
            ghost_pct: Some(ghost_pct.clamp(0.0, 1.0)),
            ghost_color,
        });
    }

    /// Draw multi-line ASCII art sprite.
    pub fn draw_sprite(&mut self, x: f32, y: f32, lines: &[&str], color: Vec4) {
        self.draw_queue.push(UiDrawCommand::Sprite {
            lines: lines.iter().map(|s| s.to_string()).collect(),
            x, y, color,
        });
    }
}

// ── Word wrapping for UI ────────────────────────────────────────────────────

fn wrap_text_ui(text: &str, max_chars: usize) -> Vec<String> {
    if max_chars == 0 {
        return vec![text.to_string()];
    }
    let mut lines = Vec::new();
    for paragraph in text.split('\n') {
        if paragraph.is_empty() {
            lines.push(String::new());
            continue;
        }
        let words: Vec<&str> = paragraph.split_whitespace().collect();
        let mut line = String::new();
        for word in words {
            if line.is_empty() {
                if word.len() > max_chars {
                    let mut w = word;
                    while w.len() > max_chars {
                        lines.push(w[..max_chars].to_string());
                        w = &w[max_chars..];
                    }
                    line = w.to_string();
                } else {
                    line = word.to_string();
                }
            } else if line.len() + 1 + word.len() <= max_chars {
                line.push(' ');
                line.push_str(word);
            } else {
                lines.push(std::mem::take(&mut line));
                line = word.to_string();
            }
        }
        if !line.is_empty() {
            lines.push(line);
        }
    }
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ui_layer_projection_is_orthographic() {
        let ui = UiLayer::new(1280.0, 800.0);
        let proj = ui.projection();
        // Top-left (0,0) should map to (-1, 1) in clip space.
        let tl = proj * Vec4::new(0.0, 0.0, 0.0, 1.0);
        assert!((tl.x / tl.w - (-1.0)).abs() < 0.01);
        assert!((tl.y / tl.w - 1.0).abs() < 0.01);
    }

    #[test]
    fn ui_layer_draw_and_clear() {
        let mut ui = UiLayer::new(1280.0, 800.0);
        ui.draw_text(0.0, 0.0, "Hello", 1.0, Vec4::ONE);
        assert_eq!(ui.command_count(), 1);
        ui.begin_frame();
        assert_eq!(ui.command_count(), 0);
    }

    #[test]
    fn measure_text_single_line() {
        let ui = UiLayer::new(1280.0, 800.0);
        let (w, h) = ui.measure_text("Hello", 1.0);
        assert_eq!(w, 5.0 * ui.char_width);
        assert_eq!(h, ui.char_height);
    }

    #[test]
    fn measure_text_multi_line() {
        let ui = UiLayer::new(1280.0, 800.0);
        let (_, h) = ui.measure_text("Line1\nLine2\nLine3", 1.0);
        assert_eq!(h, 3.0 * ui.char_height);
    }

    #[test]
    fn border_style_chars() {
        let chars = BorderStyle::Single.chars();
        assert_eq!(chars[0], '');
        assert_eq!(chars[7], '');
    }

    #[test]
    fn wrap_text_ui_basic() {
        let lines = wrap_text_ui("Hello world foo bar", 10);
        for l in &lines {
            assert!(l.len() <= 10, "Line too long: '{}'", l);
        }
    }

    #[test]
    fn bar_pct_clamped() {
        let mut ui = UiLayer::new(1280.0, 800.0);
        ui.draw_bar(0.0, 0.0, 100.0, 10.0, 1.5, Vec4::ONE, Vec4::ZERO);
        if let UiDrawCommand::Bar { fill_pct, .. } = &ui.draw_queue()[0] {
            assert_eq!(*fill_pct, 1.0);
        }
    }
}