Skip to main content

yog_book/
renderer.rs

1//! Book renderer — ties together yog-ui layout, yog-gfx GPU pipeline,
2//! SVG icon rasterization, and custom font rendering.
3
4use std::collections::HashMap;
5use yog_gfx::{GfxContext, gl, core::{DrawMode, DataType, blend}};
6use yog_ui::{widget, FlexDir, Align, UiRoot};
7
8use crate::{Book, BookPage};
9use crate::state::BookViewState;
10use crate::theme::BookTheme;
11use crate::font::{BookFontRegistry, FontAtlas};
12use crate::svg;
13
14// Patchouli-compatible book texture layout (512×256 sprite sheet).
15// UV coordinates are in pixels on a 512×256 sheet.
16// The full open-book background occupies 272×180 at UV (0,0).
17pub const BOOK_TEX_W: f32   = 512.0;
18pub const BOOK_TEX_H: f32   = 256.0;
19// Open-book dimensions in texture-space
20pub const BK_W: f32         = 272.0;
21pub const BK_H: f32         = 180.0;
22// Per-page dimensions and offsets (in book-local coords)
23pub const PAGE_W: f32       = 116.0;
24pub const PAGE_H: f32       = 156.0;
25pub const TOP_PAD: f32      = 18.0;   // vertical space above page text area
26pub const LEFT_X: f32       = 15.0;   // left page X inside book
27pub const RIGHT_X: f32      = 141.0;  // right page X inside book
28// Separator strip UV origin on the sprite sheet
29pub const SEP_U: f32        = 140.0;
30pub const SEP_V: f32        = 180.0;
31pub const SEP_W: f32        = 110.0;
32pub const SEP_H: f32        = 3.0;
33
34// ── Vertex for the custom 2D shader (pos + uv + color) ───────────────────────
35
36#[repr(C)]
37#[derive(Copy, Clone)]
38struct Vert { x: f32, y: f32, u: f32, v: f32, r: f32, g: f32, b: f32, a: f32 }
39
40fn rgba_f(c: u32) -> (f32, f32, f32, f32) {
41    let a = ((c >> 24) & 0xFF) as f32 / 255.0;
42    let r = ((c >> 16) & 0xFF) as f32 / 255.0;
43    let g = ((c >>  8) & 0xFF) as f32 / 255.0;
44    let b = ( c        & 0xFF) as f32 / 255.0;
45    (r, g, b, a)
46}
47
48fn quad(verts: &mut Vec<Vert>, x: f32, y: f32, w: f32, h: f32,
49        u0: f32, v0: f32, u1: f32, v1: f32, color: u32) {
50    let (r, g, b, a) = rgba_f(color);
51    let p = [
52        Vert { x,        y,        u: u0, v: v0, r, g, b, a },
53        Vert { x: x+w,   y,        u: u1, v: v0, r, g, b, a },
54        Vert { x,        y: y+h,   u: u0, v: v1, r, g, b, a },
55        Vert { x: x+w,   y: y+h,   u: u1, v: v1, r, g, b, a },
56    ];
57    // two triangles: 0,1,2  1,3,2
58    verts.extend_from_slice(&[p[0], p[1], p[2], p[1], p[3], p[2]]);
59}
60
61// ── GLSL shaders ─────────────────────────────────────────────────────────────
62
63const VERT: &str = r#"
64#version 330 core
65layout(location = 0) in vec2 aPos;
66layout(location = 1) in vec2 aUv;
67layout(location = 2) in vec4 aCol;
68out vec2 fUv;
69out vec4 fCol;
70uniform vec2 uScreen;
71void main() {
72    fUv  = aUv;
73    fCol = aCol;
74    vec2 ndc = aPos / uScreen * 2.0 - vec2(1.0);
75    gl_Position = vec4(ndc.x, -ndc.y, 0.0, 1.0);
76}
77"#;
78
79const FRAG: &str = r#"
80#version 330 core
81in vec2 fUv;
82in vec4 fCol;
83out vec4 outColor;
84uniform sampler2D uTex;
85uniform int uMode;  // 0 = solid color, 1 = RGBA texture, 2 = alpha-only (font)
86void main() {
87    if (uMode == 0) {
88        outColor = fCol;
89    } else if (uMode == 1) {
90        outColor = texture(uTex, fUv) * fCol;
91    } else {
92        float alpha = texture(uTex, fUv).a;
93        outColor = vec4(fCol.rgb, fCol.a * alpha);
94    }
95}
96"#;
97
98// ── Embedded textures ─────────────────────────────────────────────────────────
99
100static BOOK_PNG:     &[u8] = include_bytes!("../assets/book_brown.png");
101static CRAFTING_PNG: &[u8] = include_bytes!("../assets/crafting.png");
102
103fn decode_png(data: &[u8]) -> Option<(Vec<u8>, u32, u32)> {
104    use png::Decoder;
105    let decoder = Decoder::new(std::io::Cursor::new(data));
106    let mut reader = decoder.read_info().ok()?;
107    let mut buf = vec![0u8; reader.output_buffer_size()];
108    let info = reader.next_frame(&mut buf).ok()?;
109    let rgba = match info.color_type {
110        png::ColorType::Rgba => buf[..info.buffer_size()].to_vec(),
111        png::ColorType::Rgb  => {
112            let rgb = &buf[..info.buffer_size()];
113            let mut out = Vec::with_capacity(rgb.len() / 3 * 4);
114            for px in rgb.chunks(3) { out.extend_from_slice(px); out.push(255); }
115            out
116        }
117        _ => return None,
118    };
119    Some((rgba, info.width, info.height))
120}
121
122// ── GL resource cache ─────────────────────────────────────────────────────────
123
124struct BookGl {
125    prog:         gl::ShaderProgram,
126    vao:          gl::VertexArray,
127    vbo:          gl::Buffer,
128    book_tex:     Option<(gl::Texture, u32, u32)>,
129    crafting_tex: Option<(gl::Texture, u32, u32)>,
130    svg_tex:      HashMap<u64, (gl::Texture, u32, u32)>,
131    font_atlas:   HashMap<u64, (gl::Texture, FontAtlas)>,
132}
133
134impl BookGl {
135    fn init(ctx: &GfxContext) -> Option<Self> {
136        let prog = ctx.create_shader(VERT, FRAG).ok()?;
137        let vbo  = ctx.create_buffer();
138        let vao  = ctx.create_vao();
139
140        const STRIDE: u32 = 32; // 8 × f32
141        vao.attrib(ctx, &vbo, 0, 2, DataType::F32, false, STRIDE, 0);  // pos
142        vao.attrib(ctx, &vbo, 1, 2, DataType::F32, false, STRIDE, 8);  // uv
143        vao.attrib(ctx, &vbo, 2, 4, DataType::F32, false, STRIDE, 16); // col
144
145        let load = |data: &[u8]| decode_png(data).map(|(rgba, w, h)| {
146            let tex = ctx.create_texture_rgba(w, h, &rgba, true);
147            (tex, w, h)
148        });
149        let book_tex     = load(BOOK_PNG);
150        let crafting_tex = load(CRAFTING_PNG);
151
152        Some(BookGl { prog, vao, vbo, book_tex, crafting_tex, svg_tex: HashMap::new(), font_atlas: HashMap::new() })
153    }
154
155    fn svg_tex(&mut self, ctx: &GfxContext, hash: u64, data: &str, w: u32, h: u32)
156        -> Option<&(gl::Texture, u32, u32)>
157    {
158        if !self.svg_tex.contains_key(&hash) {
159            let pixels = svg::rasterize(data, w, h)?;
160            let tex = ctx.create_texture_rgba(w, h, &pixels, true);
161            self.svg_tex.insert(hash, (tex, w, h));
162        }
163        self.svg_tex.get(&hash)
164    }
165
166    fn flush(&self, ctx: &GfxContext, verts: &[Vert]) {
167        if verts.is_empty() { return; }
168        unsafe { self.vbo.upload(ctx, verts, true); }
169        ctx.draw_arrays(&self.vao, &self.prog, DrawMode::Triangles, 0, verts.len() as u32);
170    }
171}
172
173// ── Draw helpers ──────────────────────────────────────────────────────────────
174
175impl BookGl {
176    fn begin_frame(&self, ctx: &GfxContext, sw: f32, sh: f32) {
177        ctx.set_blend(true, blend::SRC_ALPHA, blend::ONE_MINUS_SRC_ALPHA);
178        ctx.set_depth(false, false);
179        self.prog.uniform_2f(ctx, "uScreen", sw, sh);
180        self.prog.uniform_1i(ctx, "uTex", 0);
181    }
182
183    /// Blit a subrect of a sprite sheet at an arbitrary screen position.
184    fn draw_book_sprite(&self, ctx: &GfxContext, spr: &BgSprite) {
185        let sheet = match spr.sheet {
186            SpriteSheet::Book     => &self.book_tex,
187            SpriteSheet::Crafting => &self.crafting_tex,
188        };
189        let Some((tex, tw, th)) = sheet else { return };
190        let u0 = spr.u  / *tw as f32;
191        let v0 = spr.v  / *th as f32;
192        let u1 = (spr.u + spr.uw) / *tw as f32;
193        let v1 = (spr.v + spr.uh) / *th as f32;
194        self.prog.uniform_1i(ctx, "uMode", 1);
195        ctx.bind_texture(0, tex);
196        let mut v = Vec::with_capacity(6);
197        quad(&mut v, spr.x, spr.y, spr.w, spr.h, u0, v0, u1, v1, 0xFF_FFFFFF);
198        self.flush(ctx, &v);
199    }
200
201    /// Blit the open-book background from the embedded book texture.
202    fn draw_book_bg(&self, ctx: &GfxContext, bx: f32, by: f32, bw: f32, bh: f32) {
203        let Some((tex, tw, th)) = &self.book_tex else { return };
204        let su = BK_W / *tw as f32;
205        let sv = BK_H / *th as f32;
206        self.prog.uniform_1i(ctx, "uMode", 1);
207        ctx.bind_texture(0, tex);
208        let mut v = Vec::with_capacity(6);
209        quad(&mut v, bx, by, bw, bh, 0.0, 0.0, su, sv, 0xFF_FFFFFF);
210        self.flush(ctx, &v);
211    }
212
213    fn draw_svg(&mut self, ctx: &GfxContext, data: &str, x: f32, y: f32, w: f32, h: f32) {
214        let hash = svg::svg_hash(data);
215        let tw = w as u32; let th = h as u32;
216        if let Some(&(tex, _, _)) = self.svg_tex(ctx, hash, data, tw, th) {
217            self.prog.uniform_1i(ctx, "uMode", 1);
218            ctx.bind_texture(0, &tex);
219            let mut v = Vec::with_capacity(6);
220            quad(&mut v, x, y, w, h, 0.0, 0.0, 1.0, 1.0, 0xFF_FFFFFF);
221            self.flush(ctx, &v);
222        }
223    }
224
225    fn draw_text_custom(&mut self, ctx: &GfxContext, ttf: &[u8], size_px: f32,
226                        text: &str, mut x: f32, y: f32, color: u32) {
227        let hash = font_hash(ttf);
228        if !self.font_atlas.contains_key(&hash) {
229            if let Some(atlas) = FontAtlas::build(ttf, size_px) {
230                let tex = ctx.create_texture_rgba(
231                    atlas.atlas_size, atlas.atlas_size, &atlas.pixels, true);
232                self.font_atlas.insert(hash, (tex, atlas));
233            }
234        }
235        // Get raw pointers so the borrow of self.font_atlas ends before we call
236        // self.prog / self.flush, which also borrow fields of self.
237        let ptrs = self.font_atlas.get(&hash)
238            .map(|(t, a)| (t as *const gl::Texture, a as *const FontAtlas));
239        if let Some((tex_ptr, atlas_ptr)) = ptrs {
240            // SAFETY: we hold &mut self; font_atlas is not mutated below.
241            let tex   = unsafe { &*tex_ptr   };
242            let atlas = unsafe { &*atlas_ptr };
243            self.prog.uniform_1i(ctx, "uMode", 2);
244            ctx.bind_texture(0, tex);
245            let baseline = y + size_px;
246            let mut verts = Vec::new();
247            for ch in text.chars() {
248                if let Some(g) = atlas.glyphs.get(&ch) {
249                    if g.width > 0 {
250                        let gx = x + g.xoff;
251                        let gy = baseline - g.yoff - g.height as f32;
252                        quad(&mut verts, gx, gy, g.width as f32, g.height as f32,
253                             g.u0, g.v0, g.u1, g.v1, color);
254                    }
255                    x += g.advance;
256                }
257            }
258            self.flush(ctx, &verts);
259        }
260    }
261}
262
263fn font_hash(data: &[u8]) -> u64 {
264    use std::hash::{Hash, Hasher};
265    let mut h = std::collections::hash_map::DefaultHasher::new();
266    data.hash(&mut h);
267    h.finish()
268}
269
270// ── Background sprite (texture blit, drawn before yog-ui) ────────────────────
271
272#[derive(Clone, Copy, PartialEq)]
273pub(crate) enum SpriteSheet { Book, Crafting }
274
275/// A subrect blit from one of the embedded sprite sheets, drawn before yog-ui.
276#[derive(Clone)]
277pub(crate) struct BgSprite {
278    pub sheet: SpriteSheet,
279    /// Source rect in pixels on the sheet.
280    pub u: f32, pub v: f32, pub uw: f32, pub uh: f32,
281    /// Destination on screen.
282    pub x: f32, pub y: f32, pub w: f32, pub h: f32,
283}
284
285// ── Pending overlay draw commands ─────────────────────────────────────────────
286
287#[derive(Clone)]
288pub(crate) enum OverlayCmd {
289    Svg  { data: String, x: f32, y: f32, w: f32, h: f32 },
290    Text { text: String, font: crate::font::BookFont, x: f32, y: f32, color: u32 },
291}
292
293// ── BookRenderer ──────────────────────────────────────────────────────────────
294
295pub struct BookRenderer {
296    pub book:  Book,
297    pub state: BookViewState,
298    pub theme: BookTheme,
299    fonts:        BookFontRegistry,
300    gl:           Option<BookGl>,
301    pub ui:       Option<UiRoot>,
302    bg_sprites:   Vec<BgSprite>,
303    overlays:     Vec<OverlayCmd>,
304    dirty:   bool,
305    last_sw: f32,
306    last_sh: f32,
307}
308
309impl BookRenderer {
310    pub fn new(book: Book) -> Self {
311        let theme = BookTheme::default().with_nameplate(&book.nameplate_color);
312        Self {
313            book,
314            state: BookViewState::default(),
315            theme,
316            fonts: BookFontRegistry::default(),
317            gl: None,
318            ui: None,
319            bg_sprites: Vec::new(),
320            overlays: Vec::new(),
321            dirty: true,
322            last_sw: 0.0,
323            last_sh: 0.0,
324        }
325    }
326
327    pub fn handle_event(&mut self, ev: &str) {
328        if self.state.handle(ev, &self.book) {
329            self.dirty = true;
330        }
331    }
332
333    /// Register a custom TTF/OTF font for use in `BookPage::CustomText`.
334    pub fn register_font(&mut self, id: impl Into<String>, ttf: Vec<u8>) {
335        self.fonts.register(id, ttf);
336    }
337
338    pub fn render(&mut self, ctx: &GfxContext, sw: f32, sh: f32) {
339        // Lazy GL init.
340        if self.gl.is_none() {
341            self.gl = BookGl::init(ctx);
342        }
343
344        // Render at Patchouli-native size (272×180 GUI pixels). Scale DOWN only
345        // if the screen is smaller; never scale up (MC font is designed for 1×).
346        let scale = ((sw - 16.0) / BK_W).min((sh - 16.0) / BK_H).min(1.0).max(0.4);
347        let bw = BK_W * scale;
348        let bh = BK_H * scale;
349        let bx = (sw - bw) / 2.0;
350        let by = (sh - bh) / 2.0;
351
352        // Rebuild widget tree if dirty or screen resized.
353        if self.dirty || sw != self.last_sw || sh != self.last_sh {
354            let (root, bg_sprites, overlays) = build_ui(&self.book, &self.state, &self.theme, sw, sh, bx, by, bw, bh);
355            self.ui = Some(root);
356            self.bg_sprites = bg_sprites;
357            self.overlays = overlays;
358            self.dirty = false;
359            self.last_sw = sw;
360            self.last_sh = sh;
361        }
362
363        // 1. Draw book background texture first.
364        if let Some(gl) = &mut self.gl {
365            gl.begin_frame(ctx, sw, sh);
366            gl.draw_book_bg(ctx, bx, by, bw, bh);
367        }
368
369        // 1b. Background sprites (nameplate banner, separator lines) from book texture.
370        if let Some(gl) = &mut self.gl {
371            for spr in &self.bg_sprites {
372                gl.draw_book_sprite(ctx, spr);
373            }
374        }
375
376        // 2. yog-ui: text, buttons, icons (transparent BG — texture is already there).
377        if let Some(ui) = &mut self.ui {
378            if ui.needs_layout { ui.layout(sw, sh); }
379            ui.render(ctx);
380        }
381
382        // 3. Custom GL overlays (SVG icons, custom font text).
383        if let Some(gl) = &mut self.gl {
384            gl.begin_frame(ctx, sw, sh);
385            for ov in self.overlays.clone() {
386                match ov {
387                    OverlayCmd::Svg  { data, x, y, w, h } =>
388                        gl.draw_svg(ctx, &data, x, y, w, h),
389                    OverlayCmd::Text { text, font, x, y, color } => {
390                        if let Some(ttf) = self.fonts.get(&font.font_id) {
391                            gl.draw_text_custom(ctx, ttf, font.size_px, &text, x, y, color);
392                        }
393                    }
394                }
395            }
396        }
397    }
398}
399
400// ── UI builder ────────────────────────────────────────────────────────────────
401
402/// Build the yog-ui widget tree + bg sprites + overlay commands for the current book state.
403/// `bx/by/bw/bh` are the screen-space book rect (same values used to blit the bg texture).
404fn build_ui(book: &Book, state: &BookViewState, theme: &BookTheme,
405             sw: f32, sh: f32,
406             bx: f32, by: f32, bw: f32, bh: f32) -> (UiRoot, Vec<BgSprite>, Vec<OverlayCmd>) {
407    let _ = (sw, sh);
408    let mut bg_sprites: Vec<BgSprite> = Vec::new();
409    let mut overlays:   Vec<OverlayCmd> = Vec::new();
410
411    // Scale from Patchouli's fixed BK_W×BK_H coordinate space → actual bw×bh.
412    let sx = bw / BK_W;
413    let sy = bh / BK_H;
414
415    // In book-local: left page starts at x=LEFT_X, right at RIGHT_X, both y=TOP_PAD.
416    let pw = PAGE_W * sx;
417    let ph = PAGE_H * sy;
418    let spine_gap = (RIGHT_X - LEFT_X - PAGE_W) * sx; // ≈10px scaled
419
420    // Content x/y in screen space (for SVG overlay positioning).
421    let lx = bx + LEFT_X  * sx;
422    let rx = bx + RIGHT_X * sx;
423    let py = by + TOP_PAD  * sy;
424
425    if state.at_home {
426        // Nameplate banner: book-local (-8, 12) from LEFT_PAGE_X, size 140×31.
427        bg_sprites.push(BgSprite {
428            sheet: SpriteSheet::Book,
429            u: 0.0, v: 180.0, uw: 140.0, uh: 31.0,
430            x: bx + (LEFT_X - 8.0) * sx,
431            y: by + 12.0 * sy,
432            w: 140.0 * sx,
433            h: 31.0 * sy,
434        });
435        // Separator below "Categories" header on right page (TOP_PAD + 12 book-local)
436        bg_sprites.push(BgSprite {
437            sheet: SpriteSheet::Book,
438            u: SEP_U, v: SEP_V, uw: SEP_W, uh: SEP_H,
439            x: rx,
440            y: by + (TOP_PAD + 12.0) * sy,
441            w: SEP_W * sx,
442            h: (SEP_H * sy).max(1.0),
443        });
444    }
445
446    let (left_page, right_page) = if state.at_home {
447        let l = build_landing_left(book, theme, pw, ph, lx, py, sx, sy);
448        let r = build_categories_right(book, state, theme, pw, ph, rx, py, sx, sy, &mut overlays);
449        (l, r)
450    } else {
451        let l = build_entry_left(book, state, theme, pw, ph, lx, py, sx, sy, &mut bg_sprites, &mut overlays);
452        let r = build_entries_right(book, state, theme, pw, ph, rx, py, sx, sy);
453        (l, r)
454    };
455
456    // Root row spans the full book width.
457    // padding-left = LEFT_X*sx offsets both pages from the left cover edge.
458    // padding-top  = TOP_PAD*sy offsets from the header banner.
459    // spine_gap spacer sits between the two pages.
460    let root_widget = widget::panel(FlexDir::Row)
461        .w(bw).h(bh)
462        .padding(TOP_PAD * sy, 0.0, 0.0, LEFT_X * sx)
463        .child(left_page)
464        .child(widget::spacer().w(spine_gap))  // spine gap
465        .child(right_page);
466
467    // Outer shell: positions the book on screen via a full-screen transparent Row.
468    // We use a Column + Row arrangement to apply top/left offsets without margin support.
469    let outer = widget::panel(FlexDir::Column)
470        .w(bw).h(bh)
471        .child(root_widget);
472
473    // Screen-level container: positions book at (bx, by).
474    // bx offset via padding, by offset via padding-top.
475    // Note: layout computes from (0,0), so we pass bx/by into the UiRoot manually
476    // by wrapping in a full-screen panel with the right padding.
477    let screen_root = widget::panel(FlexDir::Row)
478        .padding(by, 0.0, 0.0, bx)
479        .child(outer);
480
481    let ui = UiRoot::new(&book.id, screen_root);
482    (ui, bg_sprites, overlays)
483}
484
485// ── Left page: landing (home view) ───────────────────────────────────────────
486
487fn build_landing_left(book: &Book, theme: &BookTheme,
488                       page_w: f32, page_h: f32, _lx: f32, _py: f32,
489                       _sx: f32, sy: f32)
490    -> widget::Widget
491{
492    // The nameplate banner occupies book-local y=12..43.
493    // Page area starts at y=TOP_PAD=18 → visible banner in page-widget: y=0..(43-18)*sy.
494    let nameplate_h = (43.0 - TOP_PAD) * sy;
495    let gap         = 4.0;
496    let pad_top     = 2.0;
497
498    let mut col = widget::panel(FlexDir::Column)
499        .w(page_w).h(page_h)
500        .padding(pad_top, 6.0, 4.0, 4.0)
501        .gap(gap);
502
503    // Book title on nameplate sprite.
504    col = col.child(widget::label(&book.name).color(theme.nameplate).h(10.0));
505
506    // Optional author subtitle ("by …") — mirrors Patchouli's subtitle field.
507    let spacer_h = if let Some(author) = &book.author {
508        col = col.child(
509            widget::label(format!("by {}", author)).color(theme.nameplate).h(8.0)
510        );
511        (nameplate_h - 10.0 - gap - 8.0 - gap).max(0.0)
512    } else {
513        (nameplate_h - 10.0 - gap).max(0.0)
514    };
515    col = col.child(widget::spacer().h(spacer_h));
516
517    // Landing text paragraphs.
518    for para in book.landing_text.split('\n') {
519        if para.is_empty() {
520            col = col.child(widget::spacer().h(3.0));
521        } else {
522            col = col.child(widget::label(para).color(theme.text));
523        }
524    }
525
526    // Progress bar at bottom: entry count across all categories.
527    let total = book.entries.len();
528    col = col.child(widget::spacer().flex(1.0));
529    col = col.child(
530        widget::spacer().h(3.0).bg(theme.border)   // thin separator line
531    );
532    col = col.child(
533        widget::label(format!("{} entries", total))
534            .color(theme.nav).h(9.0).align(Align::Center)
535    );
536    col
537}
538
539// ── Right page: category list (home view) ────────────────────────────────────
540
541fn build_categories_right(
542    book: &Book, state: &BookViewState, theme: &BookTheme,
543    page_w: f32, page_h: f32,
544    _rx: f32, _py: f32,
545    sx: f32, sy: f32,
546    overlays: &mut Vec<OverlayCmd>,
547) -> widget::Widget {
548    let _ = overlays;
549
550    // Patchouli right-page layout (landing):
551    //   "Categories" header at TOP_PADDING, centered over RIGHT_PAGE_X..RIGHT_PAGE_X+PAGE_WIDTH
552    //   Separator at TOP_PADDING+12 (drawn as bg sprite, not here)
553    //   4-column 24×24 icon grid starting at (RIGHT_PAGE_X+10, TOP_PADDING+25)
554    //   In page-local terms: left-pad=10, top-pad=(TOP_PAD+25-TOP_PAD)=25, cell=24
555    let cell_w = 24.0 * sx;
556    let cell_h = 24.0 * sy;
557    let grid_pad_left = 10.0 * sx;
558    // TOP_PADDING+12 is where separator is; TOP_PADDING+25 is where the grid starts.
559    // In page-widget space (y=0 = book-local TOP_PAD): grid starts at 25*sy.
560    let grid_top = 25.0 * sy;
561    // Space above grid = grid_top (separator region = 12*sy to 25*sy)
562    let header_h  = 12.0 * sy; // "Categories" label
563    let sep_gap   = (grid_top - header_h).max(0.0); // gap between label and grid
564
565    let mut col = widget::panel(FlexDir::Column)
566        .w(page_w).h(page_h)
567        .padding(0.0, 4.0, 4.0, 0.0)
568        .gap(0.0);
569
570    // Header: "Categories" centered
571    col = col.child(
572        widget::label("Categories").color(theme.divider).h(header_h)
573    );
574    // Gap where the separator sprite sits
575    col = col.child(widget::spacer().h(sep_gap));
576
577    // 4-column icon grid
578    let cats = &book.categories;
579    let mut row_i = 0usize;
580    loop {
581        let row_start = row_i * 4;
582        if row_start >= cats.len() { break; }
583        let mut row = widget::panel(FlexDir::Row)
584            .h(cell_h)
585            .gap(0.0)
586            .padding(0.0, 0.0, 0.0, grid_pad_left);
587        for col_i in 0..4 {
588            let idx = row_start + col_i;
589            if idx >= cats.len() { break; }
590            let cat = &cats[idx];
591            let selected = !state.at_home && idx == state.cat;
592            let bg = if selected { theme.nav_selected_bg } else { 0 };
593
594            let icon_w = 16.0 * sx;
595            let icon_h = 16.0 * sy;
596            let pad_xy = (cell_w - icon_w) / 2.0;
597
598            let mut cell = widget::panel(FlexDir::Column)
599                .w(cell_w).h(cell_h).bg(bg)
600                .on_click(format!("cat:{}", idx))
601                .id(format!("book_cat_{}", idx))
602                .padding(pad_xy, pad_xy, pad_xy, pad_xy);
603            cell = if let Some(icon_id) = &cat.icon {
604                cell.child(item_icon_widget(icon_id).w(icon_w).h(icon_h))
605            } else {
606                cell.child(widget::spacer().w(icon_w).h(icon_h))
607            };
608            row = row.child(cell);
609        }
610        col = col.child(row);
611        row_i += 1;
612    }
613    col
614}
615
616// ── Left page: entry content (entry view) ────────────────────────────────────
617
618fn build_entry_left(
619    book: &Book, state: &BookViewState, theme: &BookTheme,
620    page_w: f32, page_h: f32,
621    ox: f32, oy: f32,
622    sx: f32, sy: f32,
623    bg_sprites: &mut Vec<BgSprite>,
624    overlays: &mut Vec<OverlayCmd>,
625) -> widget::Widget {
626    let entry      = state.current_entry(book);
627    let page       = entry.and_then(|e| e.pages.get(state.page));
628    let page_count = state.page_count(book);
629    let title_text = entry.map(|e| e.name.as_str()).unwrap_or("");
630
631    // Sprite separator after title.
632    // Layout: padding-top=4, title h=14, gap=3 → separator at oy+21.
633    let pad_top  = 4.0;
634    let title_h  = 14.0;
635    let gap      = 3.0;
636    let sep_h_px = (SEP_H * sy).max(2.0);
637    let sep_y    = oy + pad_top + title_h + gap;
638    bg_sprites.push(BgSprite {
639        sheet: SpriteSheet::Book,
640        u: SEP_U, v: SEP_V, uw: SEP_W, uh: SEP_H,
641        x: ox, y: sep_y,
642        w: SEP_W * sx, h: sep_h_px,
643    });
644
645    // Body starts after separator + another gap.
646    let body_oy = sep_y + sep_h_px + gap;
647
648    let page_label = format!("{}/{}", state.page + 1, page_count);
649    let nav = widget::panel(FlexDir::Row)
650        .h(16.0).gap(4.0)
651        .padding(0.0, 2.0, 0.0, 2.0)
652        .child(widget::button("◀").w(16.0).h(12.0).color(theme.nav)
653            .on_click("prev_page").id("prev_page"))
654        .child(widget::label(&page_label).color(theme.nav).flex(1.0).align(Align::Center))
655        .child(widget::button("⌂").w(16.0).h(12.0).color(theme.nav)
656            .on_click("home").id("book_home"))
657        .child(widget::button("▶").w(16.0).h(12.0).color(theme.nav)
658            .on_click("next_page").id("next_page"));
659
660    let page_body = build_page(page, state.page, theme, bg_sprites, overlays, ox, body_oy, sx, sy);
661
662    widget::panel(FlexDir::Column)
663        .w(page_w).h(page_h)
664        .padding(pad_top, 6.0, 4.0, 4.0)
665        .gap(gap)
666        .child(widget::label(title_text).color(theme.title).h(title_h).align(Align::Center))
667        .child(widget::spacer().h(sep_h_px))   // space for sprite separator
668        .child(page_body)
669        .child(nav)
670}
671
672// ── Right page: entry list for selected category (entry view) ─────────────────
673
674fn build_entries_right(
675    book: &Book, state: &BookViewState, theme: &BookTheme,
676    page_w: f32, page_h: f32, _rx: f32, _py: f32,
677    _sx: f32, _sy: f32,
678) -> widget::Widget {
679    let entries  = state.entries_visible(book);
680    let cat_name = book.categories.get(state.cat).map(|c| c.name.as_str()).unwrap_or("Entries");
681    let spread_count = state.list_spread_count(book);
682
683    let mut col = widget::panel(FlexDir::Column)
684        .w(page_w).h(page_h)
685        .padding(4.0, 6.0, 4.0, 4.0)
686        .gap(1.0);
687
688    col = col.child(widget::label(cat_name).color(theme.divider).h(11.0));
689    col = col.child(widget::spacer().h(2.0));
690
691    let abs_start = state.list_spread_start();
692    for (i, entry) in entries.iter().enumerate() {
693        let abs_i    = abs_start + i;
694        let selected = abs_i == state.entry;
695        let bg    = if selected { theme.nav_selected_bg } else { 0 };
696        let color = if selected { theme.nav_selected } else { theme.nav };
697
698        let mut row = widget::panel(FlexDir::Row)
699            .h(14.0).gap(4.0).bg(bg)
700            .on_click(format!("entry:{}", abs_i))
701            .id(format!("book_entry_{}", abs_i))
702            .padding(1.0, 2.0, 1.0, 2.0);
703
704        if let Some(icon_id) = &entry.icon {
705            row = row.child(item_icon_widget(icon_id));
706        } else {
707            row = row.child(widget::spacer().w(14.0));
708        }
709        row = row.child(widget::label(&entry.name).color(color).flex(1.0));
710        col = col.child(row);
711    }
712
713    // List spread navigation — only shown when there's more than one spread.
714    if spread_count > 1 {
715        col = col.child(widget::spacer().flex(1.0));
716        let spread_label = format!("{}/{}", state.list_spread + 1, spread_count);
717        col = col.child(
718            widget::panel(FlexDir::Row).h(14.0).gap(2.0)
719                .child(widget::button("◀").w(14.0).h(12.0).color(theme.nav)
720                    .on_click("prev_list").id("prev_list"))
721                .child(widget::label(&spread_label).color(theme.nav)
722                    .flex(1.0).align(Align::Center))
723                .child(widget::button("▶").w(14.0).h(12.0).color(theme.nav)
724                    .on_click("next_list").id("next_list"))
725        );
726    }
727    col
728}
729
730/// Bare 16×16 item icon widget from an item ID ("ns:item_name" or "ns:item/name").
731fn item_icon_widget(item_id: &str) -> widget::Widget {
732    // Normalize "ns:item/name" or "ns:block/name" → "ns:textures/item/name.png"
733    // Normalize "ns:name" → "ns:textures/item/name.png"
734    let tex = if let Some((ns, path)) = item_id.split_once(':') {
735        if path.starts_with("item/") || path.starts_with("block/") {
736            format!("{ns}:textures/{path}.png")
737        } else {
738            format!("{ns}:textures/item/{path}.png")
739        }
740    } else {
741        format!("minecraft:textures/item/{item_id}.png")
742    };
743    widget::mc_image(&tex, 16.0, 16.0)
744}
745
746// ── Page content builder ──────────────────────────────────────────────────────
747
748/// Build the content widget for a single page.
749/// `ox/oy` = screen top-left of the page body area.
750/// `sx/sy` = book-local-to-screen scale factors.
751fn build_page(
752    page: Option<&BookPage>,
753    page_num: usize,
754    theme: &BookTheme,
755    bg_sprites: &mut Vec<BgSprite>,
756    overlays: &mut Vec<OverlayCmd>,
757    ox: f32, oy: f32,
758    sx: f32, sy: f32,
759) -> widget::Widget {
760    let mut col = widget::panel(FlexDir::Column).flex(1.0).gap(4.0);
761
762    let Some(page) = page else {
763        return col.child(widget::label("No entries yet.").color(theme.nav));
764    };
765
766    match page {
767        BookPage::Text { text, title } => {
768            // On non-first pages, show an optional section title + separator.
769            if page_num > 0 {
770                if let Some(t) = title {
771                    let sep_h_px = (SEP_H * sy).max(2.0);
772                    col = col.child(widget::label(t.as_str()).color(theme.title)
773                        .h(12.0).align(Align::Center));
774                    bg_sprites.push(BgSprite {
775                        sheet: SpriteSheet::Book,
776                        u: SEP_U, v: SEP_V, uw: SEP_W, uh: SEP_H,
777                        x: ox, y: oy + 12.0 + 3.0,
778                        w: SEP_W * sx, h: sep_h_px,
779                    });
780                    col = col.child(widget::spacer().h(sep_h_px));
781                }
782            }
783            for para in text.split('\n') {
784                col = col.child(widget::label(para).color(theme.text));
785            }
786        }
787
788        BookPage::Spotlight { item, title, text } => {
789            // Crafting-box frame from crafting.png sprite sheet.
790            // Source: u=0, v=102 (=128-26), w=66, h=26 on 128×256 sheet.
791            // Destination (page-body-local): x = PAGE_W/2 - 33 = 25, y = 10.
792            let box_w   = 66.0 * sx;
793            let box_h   = 26.0 * sy;
794            let box_x   = ox + (PAGE_W / 2.0 - 33.0) * sx;
795            let box_y   = oy + 10.0 * sy;
796            bg_sprites.push(BgSprite {
797                sheet: SpriteSheet::Crafting,
798                u: 0.0, v: 102.0, uw: 66.0, uh: 26.0,
799                x: box_x, y: box_y, w: box_w, h: box_h,
800            });
801
802            // Item title above the box (page-body y=0).
803            let item_name = title.as_deref()
804                .or(item.name.as_deref())
805                .unwrap_or(item.id.as_str());
806            col = col.child(widget::label(item_name).color(theme.title)
807                .h(10.0).align(Align::Center));
808
809            // Spacer to box top (y=10 book-local) minus title.
810            col = col.child(widget::spacer().h((10.0 * sy - 10.0 - 4.0).max(0.0)));
811
812            // Item icon centered in box (page-body y=15, x=PAGE_W/2-8).
813            let icon_size = 16.0 * sx.min(sy);
814            col = col.child(
815                widget::panel(FlexDir::Row).h(icon_size)
816                    .child(widget::spacer().flex(1.0))
817                    .child(item_icon_widget(&item.id).w(icon_size).h(icon_size))
818                    .child(widget::spacer().flex(1.0))
819            );
820
821            // Spacer for the rest of the box below the icon.
822            let icon_end = 15.0 * sy + icon_size;
823            let box_end  = 10.0 * sy + box_h;
824            col = col.child(widget::spacer().h((box_end - icon_end + 4.0).max(0.0)));
825
826            if let Some(t) = text {
827                col = col.child(widget::label(t.as_str()).color(theme.text));
828            }
829        }
830
831        BookPage::Crafting { recipe_id, text } => {
832            col = col.child(
833                widget::label(format!("[Crafting: {}]", recipe_id)).color(theme.nav)
834            );
835            if let Some(t) = text {
836                col = col.child(widget::label(t.as_str()).color(theme.text));
837            }
838        }
839
840        BookPage::Smelting { recipe_id, text } => {
841            col = col.child(
842                widget::label(format!("[Smelting: {}]", recipe_id)).color(theme.nav)
843            );
844            if let Some(t) = text {
845                col = col.child(widget::label(t.as_str()).color(theme.text));
846            }
847        }
848
849        BookPage::Image { texture, title, text, .. } => {
850            if let Some(t) = title {
851                col = col.child(widget::label(t.as_str()).color(theme.title));
852            }
853            col = col.child(widget::mc_image(texture, 80.0, 80.0));
854            if let Some(t) = text {
855                col = col.child(widget::label(t.as_str()).color(theme.text));
856            }
857        }
858
859        BookPage::Svg { data, title, text } => {
860            if let Some(t) = title {
861                col = col.child(widget::label(t.as_str()).color(theme.title));
862            }
863            overlays.push(OverlayCmd::Svg { data: data.clone(), x: ox, y: oy, w: 64.0, h: 64.0 });
864            col = col.child(widget::spacer().h(68.0));
865            if let Some(t) = text {
866                col = col.child(widget::label(t.as_str()).color(theme.text));
867            }
868        }
869
870        BookPage::CustomText { text, font, color } => {
871            overlays.push(OverlayCmd::Text {
872                text: text.clone(), font: font.clone(), x: ox, y: oy, color: *color,
873            });
874            col = col.child(widget::spacer().h(font.size_px * 1.5));
875        }
876
877        BookPage::Relations { entries, text } => {
878            if let Some(t) = text {
879                col = col.child(widget::label(t.as_str()).color(theme.text));
880            }
881            col = col.child(widget::label("See also:").color(theme.title));
882            for e in entries {
883                col = col.child(widget::label(format!("• {}", e)).color(theme.nav));
884            }
885        }
886
887        BookPage::Entity { entity_type, name, text } => {
888            let display = name.as_deref().unwrap_or(entity_type.as_str());
889            col = col.child(widget::label(display).color(theme.title));
890            if let Some(t) = text {
891                col = col.child(widget::label(t.as_str()).color(theme.text));
892            }
893        }
894
895        BookPage::Pattern { op_id, input, output, text, .. } => {
896            col = col.child(widget::label(op_id.as_str()).color(theme.title));
897            col = col.child(
898                widget::label(format!("{} → {}", input, output)).color(theme.nav)
899            );
900            if !text.is_empty() {
901                col = col.child(widget::label(text.as_str()).color(theme.text));
902            }
903        }
904
905        BookPage::Empty => {}
906    }
907
908    col
909}