2020-09-08 15:58:35 -04:00
|
|
|
package d2util
|
2020-07-11 18:12:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
|
2020-09-08 15:58:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util/assets"
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2020-07-11 18:12:20 -04:00
|
|
|
)
|
|
|
|
|
2020-08-04 22:23:15 -04:00
|
|
|
const (
|
|
|
|
cw = assets.CharWidth
|
|
|
|
ch = assets.CharHeight
|
2020-07-11 18:12:20 -04:00
|
|
|
)
|
|
|
|
|
2020-08-04 22:23:15 -04:00
|
|
|
// NewDebugPrinter creates a new debug printer
|
|
|
|
func NewDebugPrinter() *GlyphPrinter {
|
2020-10-28 14:17:42 -04:00
|
|
|
img := ebiten.NewImageFromImage(assets.CreateTextImage())
|
2020-08-04 22:23:15 -04:00
|
|
|
|
|
|
|
printer := &GlyphPrinter{
|
|
|
|
glyphImageTable: img,
|
|
|
|
glyphs: make(map[rune]*ebiten.Image),
|
|
|
|
}
|
|
|
|
|
|
|
|
return printer
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlyphPrinter uses an image containing glyphs to draw text onto ebiten images
|
|
|
|
type GlyphPrinter struct {
|
|
|
|
glyphImageTable *ebiten.Image
|
|
|
|
glyphs map[rune]*ebiten.Image
|
2020-07-11 18:12:20 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 22:23:15 -04:00
|
|
|
// Print draws the string str on the image on left top corner.
|
2020-07-11 18:12:20 -04:00
|
|
|
//
|
2020-08-04 22:23:15 -04:00
|
|
|
// The available runes are in U+0000 to U+00FF, which is C0 Controls and
|
|
|
|
// Basic Latin and C1 Controls and Latin-1 Supplement.
|
2020-07-11 18:12:20 -04:00
|
|
|
//
|
|
|
|
// DebugPrint always returns nil as of 1.5.0-alpha.
|
2020-08-04 22:23:15 -04:00
|
|
|
func (p *GlyphPrinter) Print(target *ebiten.Image, str string) error {
|
|
|
|
p.PrintAt(target, str, 0, 0)
|
2020-07-11 18:12:20 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:23:15 -04:00
|
|
|
// PrintAt draws the string str on the image at (x, y) position.
|
|
|
|
// The available runes are in U+0000 to U+00FF, which is C0 Controls and
|
|
|
|
// Basic Latin and C1 Controls and Latin-1 Supplement.
|
|
|
|
func (p *GlyphPrinter) PrintAt(target *ebiten.Image, str string, x, y int) {
|
|
|
|
p.drawDebugText(target, str, x, y, false)
|
2020-07-11 18:12:20 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 22:23:15 -04:00
|
|
|
func (p *GlyphPrinter) drawDebugText(target *ebiten.Image, str string, ox, oy int, shadow bool) {
|
2020-07-11 18:12:20 -04:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2020-08-04 22:23:15 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
if shadow {
|
|
|
|
op.ColorM.Scale(0, 0, 0, 0.5)
|
|
|
|
}
|
2020-08-04 22:23:15 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
x := 0
|
|
|
|
y := 0
|
2020-08-04 22:23:15 -04:00
|
|
|
|
|
|
|
w, _ := p.glyphImageTable.Size()
|
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
for _, c := range str {
|
|
|
|
if c == '\n' {
|
|
|
|
x = 0
|
|
|
|
y += ch
|
2020-08-04 22:23:15 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-08-04 22:23:15 -04:00
|
|
|
|
|
|
|
s, ok := p.glyphs[c]
|
2020-07-11 18:12:20 -04:00
|
|
|
if !ok {
|
|
|
|
n := w / cw
|
|
|
|
sx := (int(c) % n) * cw
|
|
|
|
sy := (int(c) / n) * ch
|
2020-08-04 22:23:15 -04:00
|
|
|
rect := image.Rect(sx, sy, sx+cw, sy+ch)
|
|
|
|
s = p.glyphImageTable.SubImage(rect).(*ebiten.Image)
|
|
|
|
p.glyphs[c] = s
|
2020-07-11 18:12:20 -04:00
|
|
|
}
|
2020-08-04 22:23:15 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(float64(x), float64(y))
|
|
|
|
op.GeoM.Translate(float64(ox+1), float64(oy))
|
2020-08-04 22:23:15 -04:00
|
|
|
|
2020-07-11 20:33:20 -04:00
|
|
|
op.CompositeMode = ebiten.CompositeModeLighter
|
2020-10-28 14:17:42 -04:00
|
|
|
target.DrawImage(s, op)
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
x += cw
|
|
|
|
}
|
|
|
|
}
|