mittens-engine 0.7.0

A Vulkan and OpenXR scene engine with ECS, reactive signals, and Meow Meow scripting
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
use mittens_engine::{engine, utils};

#[path = "example_util/mod.rs"]
mod example_util;

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

fn measure_text_bounds(text: &str, wrap_at: usize) -> (usize, usize) {
    const TAB_WIDTH: usize = 4;

    let mut col: usize = 0;
    let mut line_count: usize = 0;
    let mut row: usize = 0;
    let mut max_col: usize = 0;

    for ch in text.chars() {
        if ch == '\n' {
            max_col = max_col.max(col);
            row += 1;
            col = 0;
            line_count = 0;
            continue;
        }

        if wrap_at != 0 && line_count >= wrap_at {
            max_col = max_col.max(col);
            row += 1;
            col = 0;
            line_count = 0;
        }

        let adv = if ch == '\t' { TAB_WIDTH } else { 1 };
        col += adv;
        line_count += adv;
        max_col = max_col.max(col);
    }

    // Keep a minimum 1x1 so empty text still gets a panel.
    (max_col.max(1), (row + 1).max(1))
}

fn collect_rs_files(dir: &Path, out: &mut Vec<PathBuf>) {
    let Ok(read_dir) = std::fs::read_dir(dir) else {
        return;
    };

    for entry in read_dir.flatten() {
        let path = entry.path();
        if path.is_dir() {
            collect_rs_files(&path, out);
            continue;
        }
        if path.extension().and_then(|s| s.to_str()) == Some("rs") {
            out.push(path);
        }
    }
}

fn main() {
    mittens_engine::example_support::ensure_model_assets();
    utils::logger::init();

    // Usage:
    //   cargo run --example folder-text -- [folder] [spacing]
    // Defaults:
    //   folder=src  spacing=0.3
    let mut args = std::env::args().skip(1);
    let folder = args.next().unwrap_or_else(|| "src".to_string());
    let spacing: f32 = args
        .next()
        .and_then(|s| s.parse::<f32>().ok())
        .unwrap_or(1.0);

    // Safety caps: this demo can explode the ECS if we load huge projects.
    const MAX_FILES: usize = 42;
    const MAX_CHARS_PER_FILE: usize = 10_000;
    const WRAP_AT: usize = 90;
    const TEXT_SCALE: f32 = 0.01;

    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let scan_root = {
        let p = PathBuf::from(&folder);
        if p.is_absolute() {
            p
        } else {
            // Resolve relative paths from the crate root, not the process CWD.
            manifest_dir.join(p)
        }
    };
    eprintln!("[folder-text] cwd={:?}", cwd);
    eprintln!("[folder-text] manifest_dir={:?}", manifest_dir);
    eprintln!("[folder-text] scan_root={:?}", scan_root);

    let mut files = Vec::new();
    collect_rs_files(&scan_root, &mut files);
    files.sort();

    if files.is_empty() {
        eprintln!(
            "[folder-text] No .rs files found under folder={:?} (resolved={:?})",
            folder, scan_root
        );
    }

    let files: Vec<PathBuf> = files.into_iter().take(MAX_FILES).collect();
    eprintln!(
        "[folder-text] Loaded {} file(s) from {:?} (spacing={})",
        files.len(),
        folder,
        spacing
    );

    #[derive(Debug, Clone)]
    struct FileEntry {
        path: PathBuf,
        display_text: String,
        max_cols: usize,
        rows: usize,
    }

    // Group files by folder.
    // Layout:
    // - each folder becomes a column along +X
    // - within a folder, files stack along +Y (8 high)
    // - after 8, additional files start a new stack further along +Z
    let mut files_by_folder: BTreeMap<PathBuf, Vec<FileEntry>> = BTreeMap::new();
    for path in files {
        let Ok(content) = std::fs::read_to_string(&path) else {
            eprintln!("[folder-text] Failed to read {:?}", path);
            continue;
        };

        let mut display_text = format!(
            "// {}\n\n{}",
            path.strip_prefix(&manifest_dir).unwrap_or(&path).display(),
            content
        );

        if display_text.chars().count() > MAX_CHARS_PER_FILE {
            display_text = display_text
                .chars()
                .take(MAX_CHARS_PER_FILE)
                .collect::<String>();
            display_text.push_str("\n\n// ... truncated ...\n");
        }

        let (max_cols, rows) = measure_text_bounds(&display_text, WRAP_AT);

        let folder_key = path.parent().unwrap_or(&scan_root).to_path_buf();
        files_by_folder
            .entry(folder_key)
            .or_default()
            .push(FileEntry {
                path,
                display_text,
                max_cols,
                rows,
            });
    }

    let column_count = files_by_folder.len().max(1);

    let world = engine::ecs::World::default();
    let mut universe = engine::Universe::new(world);

    let bg_r = 0.25;
    let bg_g = 0.25;
    let bg_b = 0.25;
    let background = universe
        .world
        .add_component(engine::ecs::component::BackgroundColorComponent::new());
    let background_c = universe
        .world
        .add_component(engine::ecs::component::ColorComponent::rgba(
            bg_r, bg_g, bg_b, 1.00,
        ));
    let _ = universe.world.add_child(background, background_c);
    universe.add(background);

    let ambient = universe
        .world
        .add_component(engine::ecs::component::AmbientLightComponent::rgb(
            bg_r, bg_g, bg_b,
        ));
    universe.add(ambient);

    // Input-driven camera rig.
    // Topology: I { T { C3D } }
    let input = universe
        .world
        .add_component(engine::ecs::component::InputComponent::new().with_speed(1.5));

    // Center the camera horizontally over the spawned columns.
    let center_x = if column_count <= 1 {
        0.0
    } else {
        ((column_count - 1) as f32) * spacing * 0.5
    };

    // Bring camera closer: these text blocks are tiny.
    let rig_transform = universe.world.add_component(
        engine::ecs::component::TransformComponent::new().with_position(center_x, 0.0, 1.2),
    );
    let camera3d = universe
        .world
        .add_component(engine::ecs::component::Camera3DComponent::new());
    let input_mode = universe.world.add_component(
        engine::ecs::component::InputTransformModeComponent::forward_z().with_roll_axis_y(),
    );
    let _ = universe.attach(input, input_mode);
    let _ = universe.attach(input, rig_transform);
    let _ = universe.attach(rig_transform, camera3d);

    // Topology: I { T { C3D } } — add a small camera-attached controls hint.
    example_util::spawn_desktop_camera_controls_hint(&mut universe, rig_transform);
    universe.add(input);

    // Big floor plane under everything.
    // RenderableComponent::square() is an XY quad facing +Z; rotate it to XZ facing +Y.
    let max_stacks = files_by_folder
        .values()
        .map(|v| (v.len() + 7) / 8)
        .max()
        .unwrap_or(1)
        .max(1);
    let total_width = (column_count as f32) * spacing;
    let total_depth = (max_stacks as f32) * spacing;
    let floor_w = total_width.max(10.0);
    let floor_h = total_depth.max(10.0);
    let floor_transform = universe.world.add_component(
        engine::ecs::component::TransformComponent::new()
            .with_position(0.0, -2.0, 0.0)
            .with_rotation_euler(-std::f32::consts::FRAC_PI_2, 0.0, 0.0)
            .with_scale(floor_w, floor_h, 1.0),
    );
    let floor_renderable = universe
        .world
        .add_component(engine::ecs::component::RenderableComponent::square());
    let floor_color = universe
        .world
        .add_component(engine::ecs::component::ColorComponent::rgba(
            0.88, 0.88, 0.88, 1.0,
        ));
    let _ = universe.attach(floor_transform, floor_renderable);
    let _ = universe.attach(floor_renderable, floor_color);
    universe.add(floor_transform);

    // 4 red cubes around the perimeter of the world (easy visual anchors).
    fn spawn_red_cube(universe: &mut engine::Universe, x: f32, y: f32, z: f32, s: f32) {
        let t = universe.world.add_component(
            engine::ecs::component::TransformComponent::new()
                .with_position(x, y, z)
                .with_scale(s, s, s),
        );
        let r = universe
            .world
            .add_component(engine::ecs::component::RenderableComponent::cube());
        let c = universe
            .world
            .add_component(engine::ecs::component::ColorComponent::rgba(
                1.0, 0.0, 0.0, 1.0,
            ));

        let light_transform = universe.world.add_component(
            engine::ecs::component::TransformComponent::new().with_position(0.0, s * 0.75, 0.0),
        );
        let light = universe.world.add_component(
            engine::ecs::component::PointLightComponent::new()
                .with_intensity(1.5)
                .with_distance(20.0)
                .with_color(1.0, 0.0, 0.0),
        );

        let _ = universe.attach(t, r);
        let _ = universe.attach(r, c);
        let _ = universe.attach(t, light_transform);
        let _ = universe.attach(light_transform, light);

        universe.add(t);
    }

    let p = 1.5;
    let s = 0.25;
    spawn_red_cube(&mut universe, -p, -p, 0.0, s);
    spawn_red_cube(&mut universe, p, -p, 0.0, s);
    spawn_red_cube(&mut universe, -p, p, 0.0, s);
    spawn_red_cube(&mut universe, p, p, 0.0, s);

    let start_x = -center_x;
    let stack_depth = spacing;
    let row_gap_world = 0.35;

    // One global point light at the top of the overall text "tower".
    let pad_y = 4.0;
    let global_max_panel_h_world = files_by_folder
        .values()
        .flat_map(|v| v.iter())
        .map(|e| ((e.rows as f32) + pad_y) * TEXT_SCALE)
        .fold(0.0_f32, f32::max)
        .max(0.6);
    let global_slot_h_world = global_max_panel_h_world + row_gap_world;
    let tower_top_y = (7.0 * global_slot_h_world) + 4.0;
    let tower_center_z = total_depth * 0.5;

    let tower_light_transform = universe.world.add_component(
        engine::ecs::component::TransformComponent::new().with_position(
            0.0,
            tower_top_y,
            tower_center_z,
        ),
    );
    let tower_light = universe.world.add_component(
        engine::ecs::component::PointLightComponent::new()
            .with_intensity(2.0)
            .with_distance(120.0)
            .with_color(1.0, 1.0, 1.0),
    );
    let _ = universe.attach(tower_light_transform, tower_light);
    universe.add(tower_light_transform);

    for (col_idx, (_folder, mut entries)) in files_by_folder.into_iter().enumerate() {
        entries.sort_by(|a, b| a.path.cmp(&b.path));

        // Slot height (world units) based on the tallest panel in this folder.
        let pad_y = 4.0;
        let max_panel_h_world = entries
            .iter()
            .map(|e| ((e.rows as f32) + pad_y) * TEXT_SCALE)
            .fold(0.0_f32, f32::max)
            .max(0.6);
        let slot_h_world = max_panel_h_world + row_gap_world;

        let col_x = start_x + (col_idx as f32) * spacing;

        for (i, entry) in entries.into_iter().enumerate() {
            let row = (i % 8) as f32;
            let stack = (i / 8) as f32;
            let file_y = row * slot_h_world;
            let file_z = stack * stack_depth;

            let file_group = universe.world.add_component(
                engine::ecs::component::TransformComponent::new()
                    .with_position(col_x, file_y, file_z),
            );

            // Text subtree (tiny scale).
            let file_root = universe.world.add_component(
                engine::ecs::component::TransformComponent::new()
                    .with_position(0.0, 1.0, 0.0)
                    .with_scale(TEXT_SCALE, TEXT_SCALE, 1.0)
                    .with_rotation_euler(0.0, std::f32::consts::PI / 6.0, 0.0),
            );
            let _ = universe.attach(file_group, file_root);

            // Background panel behind the text.
            // Size is based on wrapped line count (matches TextSystem's strict wrapping behavior).
            let (max_cols, rows) = (entry.max_cols, entry.rows);
            let pad_x = 4.0;
            let pad_y = 4.0;
            let w = (max_cols as f32) + pad_x;
            let h = (rows as f32) + pad_y;

            // Text glyph quads are centered at integer (col,row) positions and are 1x1, so the
            // text's AABB (in text-space) is roughly:
            //   x: [-0.5, max_cols-0.5]
            //   y: [-(rows-1)-0.5, 0.5]
            // Centering the background quad at those midpoints keeps it aligned as text grows.
            let bg_x = (max_cols as f32 - 1.0) * 0.5;
            let bg_y = -((rows as f32 - 1.0) * 0.5);

            let bg_transform = universe.world.add_component(
                engine::ecs::component::TransformComponent::new()
                    .with_position(bg_x, bg_y, -0.05)
                    .with_scale(w, h, 1.0),
            );
            let bg_renderable = universe
                .world
                .add_component(engine::ecs::component::RenderableComponent::square());
            let bg_quant = universe.world.add_component(
                engine::ecs::component::LightQuantizationComponent::steps(5.0),
            );
            let bg_color =
                universe
                    .world
                    .add_component(engine::ecs::component::ColorComponent::rgba(
                        0.2, 0.2, 0.2, 1.0,
                    ));
            let _ = universe.attach(file_root, bg_transform);
            let _ = universe.attach(bg_transform, bg_renderable);
            let _ = universe.attach(bg_renderable, bg_quant);
            let _ = universe.attach(bg_renderable, bg_color);

            let text =
                universe
                    .world
                    .add_component(engine::ecs::component::TextComponent::with_wrap(
                        entry.display_text,
                        WRAP_AT,
                    ));
            let cutout = universe
                .world
                .add_component(engine::ecs::component::TransparentCutoutComponent::new());
            let filtering = universe.world.add_component(
                engine::ecs::component::TextureFilteringComponent::nearest_magnification(),
            );
            // let color = universe
            //     .world
            //     .add_component(engine::ecs::component::ColorComponent::rgba(0.7, 0.7, 1.0, 1.0));
            let emissive = universe
                .world
                .add_component(engine::ecs::component::EmissiveComponent::on());
            let _ = universe.attach(file_root, text);
            let _ = universe.attach(text, cutout);
            let _ = universe.attach(text, filtering);
            //let _ = universe.world.add_child(text, color);
            let _ = universe.attach(text, emissive);

            universe.add(file_group);
        }
    }

    // Process init-time registrations (Text expands into glyph subtrees here).
    universe.systems.process_commands(
        &mut universe.world,
        &mut universe.visuals,
        &mut universe.render_assets,
        &mut universe.command_queue,
    );

    engine::Windowing::run_app(universe).expect("Windowing failed");
}