Display listbox text
This commit is contained in:
@@ -2,10 +2,9 @@ package assetstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/fonts"
|
||||
)
|
||||
|
||||
@@ -67,50 +66,32 @@ func (a *AssetStore) Font(name string) (*Font, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FIXME: this violates the ebiten rules for fast drawing. We may need to do the
|
||||
// draw ourselves, with image.Paletted for each glyph to a single ebiten.Image
|
||||
//
|
||||
// FIXME: it'd be great if we didn't have to implement this all by ourselves;
|
||||
// golang.org/x/image/font and github.com/hajimehoshi/ebiten/text are *almost*
|
||||
// sufficient, but don't seem to like it when the glyphs are literal colours
|
||||
// instead of a mask.
|
||||
//
|
||||
// TODO: draw text in a bounding box, multiple lines, etc
|
||||
func (f *Font) DrawLine(text string) (*ebiten.Image, error) {
|
||||
sprites := make([]*Sprite, 0, len(text))
|
||||
// CalculateBounds tries to work out what sort of size the string will be when
|
||||
// rendered
|
||||
func (f *Font) CalculateBounds(text string) image.Rectangle {
|
||||
width := 0
|
||||
height := 0
|
||||
|
||||
for _, r := range text {
|
||||
spr, ok := f.mapping[r]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Font %v does not specify rune %v", f.Name, r)
|
||||
continue // FIXME: we could add the space character or something?
|
||||
}
|
||||
|
||||
width += spr.Rect.Dx()
|
||||
if y := spr.Rect.Dy(); y > height {
|
||||
height = y
|
||||
}
|
||||
|
||||
sprites = append(sprites, spr)
|
||||
}
|
||||
|
||||
img, err := ebiten.NewImage(width, height, ebiten.FilterDefault)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
xOff := 0
|
||||
for _, spr := range sprites {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(xOff), 0)
|
||||
|
||||
xOff += spr.Rect.Dx()
|
||||
|
||||
if err := img.DrawImage(spr.Image, op); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return img, nil
|
||||
return image.Rect(0, 0, width, height)
|
||||
}
|
||||
|
||||
func (f *Font) Glyph(r rune) (*Sprite, error) {
|
||||
glyph, ok := f.mapping[r]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Font %v does not specify rune %v", f.Name, r)
|
||||
}
|
||||
|
||||
return glyph, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user