mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
parent
9c95b4accc
commit
2641f65863
@ -22,23 +22,22 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/image/font"
|
|
||||||
"golang.org/x/image/font/gofont/goregular"
|
"golang.org/x/image/font/gofont/goregular"
|
||||||
"golang.org/x/image/font/opentype"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
"github.com/hajimehoshi/ebiten/v2/text"
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
uiFontSize = 12
|
||||||
lineHeight = 16
|
lineHeight = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
uiImage *ebiten.Image
|
uiImage *ebiten.Image
|
||||||
uiFont font.Face
|
uiFaceSource *text.GoTextFaceSource
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -48,19 +47,14 @@ func init() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
uiImage = ebiten.NewImageFromImage(img)
|
uiImage = ebiten.NewImageFromImage(img)
|
||||||
|
}
|
||||||
|
|
||||||
tt, err := opentype.Parse(goregular.TTF)
|
func init() {
|
||||||
if err != nil {
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
uiFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
|
||||||
Size: 12,
|
|
||||||
DPI: 72,
|
|
||||||
Hinting: font.HintingVertical,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
uiFaceSource = s
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageType int
|
type imageType int
|
||||||
@ -183,12 +177,16 @@ func (b *Button) Draw(dst *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
||||||
|
|
||||||
m := uiFont.Metrics()
|
op := &text.DrawOptions{}
|
||||||
w := font.MeasureString(uiFont, b.Text).Floor()
|
op.GeoM.Translate(float64(b.Rect.Min.X+b.Rect.Max.X)/2, float64(b.Rect.Min.Y+b.Rect.Max.Y)/2)
|
||||||
h := (m.Ascent + m.Descent).Floor()
|
op.ColorScale.ScaleWithColor(color.Black)
|
||||||
x := b.Rect.Min.X + (b.Rect.Dx()-w)/2
|
op.LineHeight = lineHeight
|
||||||
y := b.Rect.Min.Y + (b.Rect.Dy()-h)/2 + m.Ascent.Floor()
|
op.PrimaryAlign = text.AlignCenter
|
||||||
text.Draw(dst, b.Text, uiFont, x, y, color.Black)
|
op.SecondaryAlign = text.AlignCenter
|
||||||
|
text.Draw(dst, b.Text, &text.GoTextFace{
|
||||||
|
Source: uiFaceSource,
|
||||||
|
Size: uiFontSize,
|
||||||
|
}, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) SetOnPressed(f func(b *Button)) {
|
func (b *Button) SetOnPressed(f func(b *Button)) {
|
||||||
@ -285,6 +283,7 @@ func (v *VScrollBar) Draw(dst *ebiten.Image) {
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
textBoxPaddingLeft = 8
|
textBoxPaddingLeft = 8
|
||||||
|
textBoxPaddingTop = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
type TextBox struct {
|
type TextBox struct {
|
||||||
@ -321,7 +320,7 @@ func (t *TextBox) Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TextBox) contentSize() (int, int) {
|
func (t *TextBox) contentSize() (int, int) {
|
||||||
h := len(strings.Split(t.Text, "\n")) * lineHeight
|
h := len(strings.Split(t.Text, "\n"))*lineHeight + textBoxPaddingTop
|
||||||
return t.Rect.Dx(), h
|
return t.Rect.Dx(), h
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,18 +351,17 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.contentBuf.Clear()
|
t.contentBuf.Clear()
|
||||||
m := uiFont.Metrics()
|
textOp := &text.DrawOptions{}
|
||||||
for i, line := range strings.Split(t.Text, "\n") {
|
x := -float64(t.offsetX) + textBoxPaddingLeft
|
||||||
x := -t.offsetX + textBoxPaddingLeft
|
y := -float64(t.offsetY) + textBoxPaddingTop
|
||||||
y := -t.offsetY + i*lineHeight + (lineHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor()
|
textOp.GeoM.Translate(x, y)
|
||||||
if y < -lineHeight {
|
textOp.ColorScale.ScaleWithColor(color.Black)
|
||||||
continue
|
textOp.LineHeight = lineHeight
|
||||||
}
|
text.Draw(t.contentBuf, t.Text, &text.GoTextFace{
|
||||||
if _, h := t.viewSize(); y >= h+lineHeight {
|
Source: uiFaceSource,
|
||||||
continue
|
Size: uiFontSize,
|
||||||
}
|
}, textOp)
|
||||||
text.Draw(t.contentBuf, line, uiFont, x, y, color.Black)
|
|
||||||
}
|
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
|
op.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
|
||||||
dst.DrawImage(t.contentBuf, op)
|
dst.DrawImage(t.contentBuf, op)
|
||||||
@ -389,8 +387,11 @@ type CheckBox struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CheckBox) width() int {
|
func (c *CheckBox) width() int {
|
||||||
w := font.MeasureString(uiFont, c.Text).Floor()
|
w := text.Advance(c.Text, &text.GoTextFace{
|
||||||
return checkboxWidth + checkboxPaddingLeft + w
|
Source: uiFaceSource,
|
||||||
|
Size: uiFontSize,
|
||||||
|
})
|
||||||
|
return checkboxWidth + checkboxPaddingLeft + int(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CheckBox) Update() {
|
func (c *CheckBox) Update() {
|
||||||
@ -423,10 +424,18 @@ func (c *CheckBox) Draw(dst *ebiten.Image) {
|
|||||||
drawNinePatches(dst, r, imageSrcRects[imageTypeCheckBoxMark])
|
drawNinePatches(dst, r, imageSrcRects[imageTypeCheckBoxMark])
|
||||||
}
|
}
|
||||||
|
|
||||||
m := uiFont.Metrics()
|
|
||||||
x := c.X + checkboxWidth + checkboxPaddingLeft
|
x := c.X + checkboxWidth + checkboxPaddingLeft
|
||||||
y := c.Y + (checkboxHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor()
|
y := c.Y + checkboxHeight/2
|
||||||
text.Draw(dst, c.Text, uiFont, x, y, color.Black)
|
op := &text.DrawOptions{}
|
||||||
|
op.GeoM.Translate(float64(x), float64(y))
|
||||||
|
op.ColorScale.ScaleWithColor(color.Black)
|
||||||
|
op.LineHeight = lineHeight
|
||||||
|
op.PrimaryAlign = text.AlignStart
|
||||||
|
op.SecondaryAlign = text.AlignCenter
|
||||||
|
text.Draw(dst, c.Text, &text.GoTextFace{
|
||||||
|
Source: uiFaceSource,
|
||||||
|
Size: uiFontSize,
|
||||||
|
}, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CheckBox) Checked() bool {
|
func (c *CheckBox) Checked() bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user