2020-09-08 15:58:35 -04:00
|
|
|
package d2util
|
2020-07-11 18:12:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2020-07-11 18:12:20 -04:00
|
|
|
|
2020-09-08 15:58:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util/assets"
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-07-11 18:12:20 -04:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
// DebugPrinter is the global debug printer
|
|
|
|
var DebugPrinter = NewDebugPrinter() //nolint:gochecknoglobals // currently global by design
|
|
|
|
|
|
|
|
// NewDebugPrinter creates a new debug printer
|
|
|
|
func NewDebugPrinter() *GlyphPrinter {
|
|
|
|
img, err := ebiten.NewImageFromImage(assets.CreateTextImage(), ebiten.FilterDefault)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-23 13:30:54 -04:00
|
|
|
err := target.DrawImage(s, op)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-11 18:12:20 -04:00
|
|
|
x += cw
|
|
|
|
}
|
|
|
|
}
|