|
| 1 | +package spx |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math" |
| 6 | + |
| 7 | + "github.com/goplus/spx/internal/gdi" |
| 8 | + xfont "github.com/goplus/spx/internal/gdi/font" |
| 9 | + "github.com/hajimehoshi/ebiten/v2" |
| 10 | + "golang.org/x/image/colornames" |
| 11 | + "golang.org/x/image/font" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + quotePadding = 5.0 |
| 16 | + quoteLineWidth = 8.0 |
| 17 | + quoteHeadLen = 16.0 |
| 18 | + quoteTextPadding = 3.0 |
| 19 | + quoteBorderRadis = 10.0 |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + quoteMsgFont gdi.Font |
| 24 | + quoteDesFont gdi.Font |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + const dpi = 72 |
| 29 | + quoteMsgFont = xfont.NewDefault(&xfont.Options{ |
| 30 | + Size: 35, |
| 31 | + DPI: dpi, |
| 32 | + Hinting: font.HintingFull, |
| 33 | + }) |
| 34 | + quoteDesFont = xfont.NewDefault(&xfont.Options{ |
| 35 | + Size: 18, |
| 36 | + DPI: dpi, |
| 37 | + Hinting: font.HintingFull, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +type quoter struct { |
| 42 | + sprite *Sprite |
| 43 | + message string |
| 44 | + description string |
| 45 | + |
| 46 | + cachedImg *ebiten.Image |
| 47 | +} |
| 48 | + |
| 49 | +func (p *Sprite) quote_(message, description string) { |
| 50 | + old := p.quoteObj |
| 51 | + if old == nil { |
| 52 | + p.quoteObj = "er{sprite: p, message: message, description: description} |
| 53 | + p.g.addShape(p.quoteObj) |
| 54 | + } else { |
| 55 | + old.message, old.description = message, description |
| 56 | + old.cachedImg = nil |
| 57 | + p.g.activateShape(old) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (p *Sprite) waitStopQuote(secs float64) { |
| 62 | + p.g.Wait(secs) |
| 63 | + p.doStopQuote() |
| 64 | +} |
| 65 | + |
| 66 | +func (p *Sprite) doStopQuote() { |
| 67 | + if p.quoteObj != nil { |
| 68 | + p.g.removeShape(p.quoteObj) |
| 69 | + p.quoteObj = nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (p *quoter) draw(dc drawContext) { |
| 74 | + img := p.getImage() |
| 75 | + if img == nil { |
| 76 | + return |
| 77 | + } |
| 78 | + imgW, imgH := img.Size() |
| 79 | + w, h := dc.Size() |
| 80 | + op := new(ebiten.DrawImageOptions) |
| 81 | + x := p.sprite.x + float64(w)/2 - float64(imgW)/2 |
| 82 | + y := -p.sprite.y - quotePadding - float64(imgH) + float64(h)/2 + float64(imgH)/2 |
| 83 | + op.GeoM.Translate(x, y) |
| 84 | + dc.DrawImage(img, op) |
| 85 | +} |
| 86 | + |
| 87 | +func (p *quoter) getImage() *ebiten.Image { |
| 88 | + if p.cachedImg != nil { |
| 89 | + return p.cachedImg |
| 90 | + } |
| 91 | + bound := p.sprite.getRotatedRect() |
| 92 | + w := math.Max(bound.Size.Height, bound.Size.Width) |
| 93 | + w += quotePadding + quoteLineWidth |
| 94 | + h := w * 1.15 |
| 95 | + quoteHeight := h |
| 96 | + msgRender := gdi.NewTextRender(quoteMsgFont, 135, 2) |
| 97 | + msgRender.AddText(p.message) |
| 98 | + msgW, msgH := msgRender.Size() |
| 99 | + h += float64(msgH / 2) |
| 100 | + desRender := gdi.NewTextRender(quoteDesFont, 135, 2) |
| 101 | + var desW, desH int |
| 102 | + if p.description != "" { |
| 103 | + desRender.AddText((p.description)) |
| 104 | + desW, desH = desRender.Size() |
| 105 | + h += float64(desH + quoteTextPadding) |
| 106 | + } |
| 107 | + |
| 108 | + svg := gdi.NewSVG(int(w), int(h)) |
| 109 | + mainH := int(h) - msgH/2 |
| 110 | + dy := 0.0 |
| 111 | + if p.description != "" { |
| 112 | + dy = float64(desH) + quoteTextPadding |
| 113 | + mainH -= int(dy) |
| 114 | + } |
| 115 | + half := fmt.Sprintf("m 0 %f q 0 %f %f %f h %f q %f %f 0 %f h -%f v %f h %f q %f %f 0 %f h -%f q %f 0 %f %f z", |
| 116 | + dy+quoteBorderRadis, |
| 117 | + |
| 118 | + -quoteBorderRadis, |
| 119 | + quoteBorderRadis, |
| 120 | + -quoteBorderRadis, |
| 121 | + |
| 122 | + quoteHeadLen, |
| 123 | + |
| 124 | + quoteLineWidth/2, |
| 125 | + quoteLineWidth/2, |
| 126 | + quoteLineWidth, |
| 127 | + |
| 128 | + quoteHeadLen+3, |
| 129 | + |
| 130 | + float64(mainH)-2*quoteLineWidth, |
| 131 | + |
| 132 | + quoteHeadLen+3, |
| 133 | + |
| 134 | + quoteLineWidth/2, |
| 135 | + quoteLineWidth/2, |
| 136 | + quoteLineWidth, |
| 137 | + |
| 138 | + quoteHeadLen, |
| 139 | + |
| 140 | + -quoteBorderRadis, |
| 141 | + -quoteBorderRadis, |
| 142 | + -quoteBorderRadis, |
| 143 | + ) |
| 144 | + svg.Def() |
| 145 | + svg.Path(half, `id="quote"`) |
| 146 | + svg.DefEnd() |
| 147 | + // "[" |
| 148 | + style := "fill:rgb(144,169,55);stroke:black;" |
| 149 | + svg.Use(0, 0, "#quote", style) |
| 150 | + // "]" |
| 151 | + svg.Gtransform(fmt.Sprintf("rotate(%.1f %f %f)", 180.0, w/2, quoteHeight/2+dy)) |
| 152 | + svg.Use(0, 0, "#quote", style) |
| 153 | + svg.Gend() |
| 154 | + svg.End() |
| 155 | + |
| 156 | + img, err := svg.ToImage() |
| 157 | + if err != nil { |
| 158 | + panic(err) |
| 159 | + } |
| 160 | + p.cachedImg = ebiten.NewImageFromImage(img) |
| 161 | + msgRender.Draw(p.cachedImg, (int(w)-msgW)/2, int(h)-msgH, colornames.White, 0) |
| 162 | + if p.description != "" { |
| 163 | + desRender.Draw(p.cachedImg, (int(w)-desW)/2, 0, colornames.White, 0) |
| 164 | + } |
| 165 | + return p.cachedImg |
| 166 | +} |
| 167 | + |
| 168 | +func (p *quoter) hit(hc hitContext) (hr hitResult, ok bool) { |
| 169 | + return |
| 170 | +} |
0 commit comments