This commit is contained in:
2026-05-25 20:35:04 +02:00
parent e4a7cc9177
commit 9fa1d200ef
26 changed files with 1826 additions and 516 deletions
+53 -47
View File
@@ -7,68 +7,74 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector"
)
// speechBubble holds the currently-spoken line. The Say action sets it and
// the engine draws it above the speaker (or centered at screen top if the
// speaker is unknown).
type speechBubble struct {
speaker string
lines []string
pos Point
active bool
// SpeechBubble renders the line set by the Say action above the speaking
// character (or at FallbackY if the speaker has no on-screen position).
type SpeechBubble struct {
Name string
MaxWidth int
Padding int
OffsetY int
FallbackY int
}
func (s *speechBubble) set(g *Game, speaker, text string) {
s.speaker = speaker
s.lines = wrapText(text, 200)
s.active = true
// position above the speaker, if we know where they are
if c, ok := g.runtimeChar(speaker); ok {
s.pos = Point{X: c.pos.X, Y: c.pos.Y - c.def.H - 4}
} else {
s.pos = Point{X: 160, Y: 14}
}
}
func (s *SpeechBubble) GetName() string { return s.Name }
func (s *SpeechBubble) Tick(ctx *UICtx) {}
func (s *speechBubble) clear() {
s.active = false
s.speaker = ""
s.lines = nil
}
func (s *speechBubble) tick(dt float64) { _ = dt }
func (s *speechBubble) draw(dst *ebiten.Image, g *Game) {
if !s.active || len(s.lines) == 0 {
func (s *SpeechBubble) Draw(dst *ebiten.Image, ctx *UICtx) {
g := ctx.Game
sp := g.speech
if !sp.Active {
return
}
maxW := 0
for _, ln := range s.lines {
if w := textWidth(ln); w > maxW {
maxW = w
maxW := s.MaxWidth
if maxW <= 0 {
maxW = 200
}
pad := s.Padding
if pad <= 0 {
pad = 3
}
lines := wrapText(sp.Text, maxW)
w := 0
for _, ln := range lines {
if t := textWidth(ln); t > w {
w = t
}
}
pad := 3
w := maxW + pad*2
h := len(s.lines)*16 + pad*2
x := int(s.pos.X) - w/2
y := int(s.pos.Y) - h
w += pad * 2
h := len(lines)*16 + pad*2
// position above speaker if known
cx, cy := g.Width/2, s.FallbackY
if cy == 0 {
cy = 14
}
if c, ok := g.runtimeChar(sp.Speaker); ok {
cx = int(c.pos.X)
cy = int(c.pos.Y-c.def.H) - s.OffsetY
}
x := cx - w/2
y := cy - h
if x < 2 {
x = 2
}
if x+w > 318 {
x = 318 - w
if x+w > g.Width-2 {
x = g.Width - 2 - w
}
if y < 2 {
y = 2
}
vector.DrawFilledRect(dst, float32(x), float32(y), float32(w), float32(h), color.RGBA{0, 0, 0, 200}, false)
speechColor := color.RGBA{255, 255, 255, 255}
if c, ok := g.CharacterManager.Get(s.speaker); ok {
if sc, ok := c.SpeechColor.(color.RGBA); ok {
speechColor = sc
th := g.Theme()
vector.DrawFilledRect(dst, float32(x), float32(y), float32(w), float32(h), th.SpeechBubbleBG, false)
textCol := th.SpeechDefaultText
if c, ok := g.CharacterManager.Get(sp.Speaker); ok {
if rgba, ok := c.SpeechColor.(color.RGBA); ok && rgba.A > 0 {
textCol = rgba
}
}
for k, ln := range s.lines {
drawText(dst, ln, x+pad, y+pad+k*16-2, speechColor)
for i, ln := range lines {
g.DrawText(dst, ln, x+pad, y+pad+i*16-2, textCol)
}
}