| 1 | package layout |
| 2 | |
| 3 | import "amadan.net/oficina/slides/internal/slides/deck" |
| 4 | |
| 5 | // A scene block in a picture (scenes and clips spec §3, §8.2, §8.3). |
| 6 | // A picture of a slide is the slide fully built, so a scene block is |
| 7 | // its `to` state: that state's still, or, with none, the panel Slides |
| 8 | // draws in its place (plan call R4), the scene's name over the state's |
| 9 | // description on a panel a shade off the slide. The stylesheet's |
| 10 | // `[data-block="scene"]` and `[data-scene-placeholder]` rules are the |
| 11 | // same numbers: |
| 12 | // |
| 13 | // in a region: a 16:9 box at its width, shrunk like a picture when the |
| 14 | // region is shorter, and the scene the largest 16:9 box that fits it, |
| 15 | // centred; on a canvas, the box |
| 16 | // padding: 1em; gap: 0.5em; font-size: var(--slides-body-s) |
| 17 | // border-radius: 0.6em; a code block's panel colour |
| 18 | const ( |
| 19 | scenePadEm = 1.0 |
| 20 | sceneGapEm = 0.5 |
| 21 | // sceneRatio is a scene block's height for its width in a region: |
| 22 | // the slide's own 16:9. |
| 23 | sceneRatio = 9.0 / 16.0 |
| 24 | ) |
| 25 | |
| 26 | func (e *engine) planScene(b deck.Block, s slot) plan { |
| 27 | // On a canvas the block is its box. Anywhere else it asks for a 16:9 |
| 28 | // box at its width and gives way like a picture to what else the |
| 29 | // region holds (image-full's media included: the stylesheet's |
| 30 | // block-size: 100% there is a picture's, not a scene's). |
| 31 | canvas := e.onCanvas |
| 32 | h, flex := s.h, false |
| 33 | if !canvas || h == 0 { |
| 34 | h, flex = s.w*sceneRatio, !canvas |
| 35 | } |
| 36 | // fit is the scene's own box in the block's: all of it on a canvas, |
| 37 | // the largest 16:9 box that fits, centred, anywhere else. |
| 38 | fit := func(x, y, w, h float64) (float64, float64, float64, float64) { |
| 39 | if canvas { |
| 40 | return x, y, w, h |
| 41 | } |
| 42 | fw := min(w, h/sceneRatio) |
| 43 | fh := fw * sceneRatio |
| 44 | return x + (w-fw)/2, y + (h-fh)/2, fw, fh |
| 45 | } |
| 46 | id, _ := b.Props["scene"].(string) |
| 47 | from, _ := b.Props["from"].(string) |
| 48 | to, _ := b.Props["to"].(string) |
| 49 | sc, known := e.deck.Scenes[id] |
| 50 | shown := sc.Shown(from, to) |
| 51 | |
| 52 | var name, text string |
| 53 | if known { |
| 54 | name = sc.Name |
| 55 | } |
| 56 | if len(shown) > 0 { |
| 57 | st := sc.States[shown[len(shown)-1]] |
| 58 | text = st.Description |
| 59 | if text == "" { |
| 60 | text = st.Name |
| 61 | } |
| 62 | if asset, _, ok := e.deck.StillOf(sc, st.ID); ok { |
| 63 | return plan{h: h, w: s.w, flex: flex, paint: func(x, y, w, h float64) []Op { |
| 64 | x, y, w, h = fit(x, y, w, h) |
| 65 | return []Op{Image{X: x, Y: y, W: w, H: h, Asset: asset, Fit: "contain", Alt: text}} |
| 66 | }} |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | size := e.sc.bodyS() |
| 71 | inset := scenePadEm * size |
| 72 | panel := mix(e.fg, e.bg, mixCodePanel) |
| 73 | return plan{h: h, w: s.w, flex: flex, paint: func(x, y, w, h float64) []Op { |
| 74 | x, y, w, h = fit(x, y, w, h) |
| 75 | // The words wrap at the panel's own width, known only now that |
| 76 | // the region has given the block its height. |
| 77 | var blocks []*textBlock |
| 78 | if name != "" { |
| 79 | st := style{face: e.body, size: size, fill: e.fg} |
| 80 | st.face.Bold = true |
| 81 | blocks = append(blocks, e.wrap(deck.Text{{{T: name}}}, st, w-2*inset, leadBody, 0)) |
| 82 | } |
| 83 | if text != "" { |
| 84 | blocks = append(blocks, e.wrap(deck.Text{{{T: text}}}, style{face: e.body, size: size, fill: e.fg}, w-2*inset, leadBody, 0)) |
| 85 | } |
| 86 | words := 0.0 |
| 87 | for i, tb := range blocks { |
| 88 | if i > 0 { |
| 89 | words += sceneGapEm * size |
| 90 | } |
| 91 | words += tb.h |
| 92 | } |
| 93 | ops := []Op{Rect{X: x, Y: y, W: w, H: h, Fill: panel, Radius: codeRadiusEm * size}} |
| 94 | top := y + (h-words)/2 |
| 95 | for _, tb := range blocks { |
| 96 | ops = append(ops, e.paint(tb, x+inset, top, w-2*inset, Center)...) |
| 97 | top += tb.h + sceneGapEm*size |
| 98 | } |
| 99 | return ops |
| 100 | }} |
| 101 | } |
| 102 | |