Skip to main content

layout_demo/
layout_demo.rs

1//! # Modern Layout Demo
2//!
3//! Showcases the flexbox-inspired layout engine with modern SDF visuals.
4
5use jengine::engine::{Color, Game, jEngine, KeyCode};
6use jengine::renderer::text::Font;
7use jengine::ui::{Alignment, BorderStyle, Padding};
8use jengine::ui::modern::Panel;
9use jengine::ui::widgets::{VStack, HStack, TextWidget, RectWidget, Spacer, Widget};
10use jengine::{DEFAULT_FONT_METADATA, DEFAULT_TILE_H, DEFAULT_TILE_W, DEFAULT_TILESET};
11
12struct LayoutDemo {
13    font_loaded: bool,
14}
15
16impl LayoutDemo {
17    fn new() -> Self {
18        Self { font_loaded: false }
19    }
20}
21
22impl Game for LayoutDemo {
23    fn update(&mut self, engine: &mut jEngine) {
24        if engine.is_key_pressed(KeyCode::Escape) { engine.request_quit(); }
25    }
26
27    fn render(&mut self, engine: &mut jEngine) {
28        if !self.font_loaded {
29            if let Ok(font) = Font::from_mtsdf_json(DEFAULT_FONT_METADATA) {
30                engine.ui.text.set_font(font);
31            }
32            self.font_loaded = true;
33        }
34
35        engine.clear();
36        let sw = engine.grid_width() as f32 * engine.tile_width() as f32;
37        let sh = engine.grid_height() as f32 * engine.tile_height() as f32;
38
39        // Background
40        engine.ui.ui_rect(0.0, 0.0, sw, sh, Color([0.05, 0.05, 0.08, 1.0]));
41
42        // ── Centered Menu ──
43        let mut menu = VStack::new(Alignment::Center)
44            .with_spacing(20.0)
45            .with_padding(Padding::all(40.0))
46            .with_bg(Color([0.08, 0.08, 0.12, 0.9]))
47            .with_border(BorderStyle::Thin, Color([0.3, 0.6, 0.5, 1.0]))
48            .with_radius(16.0)
49            .add(TextWidget { text: "JENGINE".to_string(), size: Some(64.0), color: Some(Color([1.0, 0.9, 0.2, 1.0])) })
50            .add(TextWidget { text: "Modern Layout Demo".to_string(), size: Some(24.0), color: Some(Color([0.4, 0.8, 0.7, 1.0])) })
51            .add(Spacer { size: 20.0, horizontal: false })
52            .add(
53                HStack::new(Alignment::Center)
54                    .with_spacing(15.0)
55                    .add(RectWidget { w: 80.0, h: 2.0, color: Color([0.5, 0.5, 0.5, 1.0]), radius: 1.0 })
56                    .add(TextWidget { text: "v1.0".to_string(), size: Some(14.0), color: Some(Color([0.5, 0.5, 0.5, 1.0])) })
57                    .add(RectWidget { w: 80.0, h: 2.0, color: Color([0.5, 0.5, 0.5, 1.0]), radius: 1.0 })
58            )
59            .add(Spacer { size: 30.0, horizontal: false })
60            .add(TextWidget { text: "> Start Game".to_string(), size: Some(32.0), color: Some(Color::WHITE) })
61            .add(TextWidget { text: "  Options".to_string(), size: Some(32.0), color: Some(Color([0.7, 0.7, 0.7, 1.0])) })
62            .add(TextWidget { text: "  Quit".to_string(), size: Some(32.0), color: Some(Color([0.7, 0.7, 0.7, 1.0])) });
63
64        let (mw, mh) = menu.size(engine);
65        Widget::draw(&mut menu, engine, (sw - mw) * 0.5, (sh - mh) * 0.5, mw, None);
66
67        engine.ui.ui_text(sw * 0.5 - 60.0, sh - 40.0, "[Esc] to Quit", Color([0.4, 0.4, 0.4, 1.0]), Color::TRANSPARENT, Some(16.0));
68    }
69}
70
71fn main() {
72    jEngine::builder()
73        .with_title("jengine — Modern Layout")
74        .with_size(1280, 720)
75        .with_tileset(DEFAULT_TILESET, DEFAULT_TILE_W, DEFAULT_TILE_H)
76        .run(LayoutDemo::new());
77}