mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-02 22:14:29 +01:00
examples: replace BoundString with MeasureString and Metrics
This commit is contained in:
parent
7791ae3029
commit
569f472475
@ -395,10 +395,9 @@ func (t *Tile) Draw(boardImage *ebiten.Image) {
|
||||
f = mplusNormalFont
|
||||
}
|
||||
|
||||
bound, _ := font.BoundString(f, str)
|
||||
w := (bound.Max.X - bound.Min.X).Ceil()
|
||||
h := (bound.Max.Y - bound.Min.Y).Ceil()
|
||||
x = x + (tileSize-w)/2
|
||||
y = y + (tileSize-h)/2 + h
|
||||
w := font.MeasureString(f, str).Floor()
|
||||
h := (f.Metrics().Ascent + f.Metrics().Descent).Floor()
|
||||
x += (tileSize - w) / 2
|
||||
y += (tileSize-h)/2 + f.Metrics().Ascent.Floor()
|
||||
text.Draw(boardImage, str, f, x, y, tileColor(v))
|
||||
}
|
||||
|
@ -61,8 +61,8 @@ func getArcadeFonts(scale int) font.Face {
|
||||
func textWidth(str string) int {
|
||||
maxW := 0
|
||||
for _, line := range strings.Split(str, "\n") {
|
||||
b, _ := font.BoundString(getArcadeFonts(1), line)
|
||||
w := (b.Max.X - b.Min.X).Ceil()
|
||||
a := font.MeasureString(getArcadeFonts(1), line)
|
||||
w := a.Floor()
|
||||
if maxW < w {
|
||||
maxW = w
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ const (
|
||||
var (
|
||||
uiImage *ebiten.Image
|
||||
uiFont font.Face
|
||||
uiFontMHeight int
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -62,8 +61,6 @@ func init() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b, _, _ := uiFont.GlyphBounds('M')
|
||||
uiFontMHeight = (b.Max.Y - b.Min.Y).Ceil()
|
||||
}
|
||||
|
||||
type imageType int
|
||||
@ -186,10 +183,11 @@ func (b *Button) Draw(dst *ebiten.Image) {
|
||||
}
|
||||
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
||||
|
||||
bounds, _ := font.BoundString(uiFont, b.Text)
|
||||
w := (bounds.Max.X - bounds.Min.X).Ceil()
|
||||
m := uiFont.Metrics()
|
||||
w := font.MeasureString(uiFont, b.Text).Floor()
|
||||
h := (m.Ascent + m.Descent).Floor()
|
||||
x := b.Rect.Min.X + (b.Rect.Dx()-w)/2
|
||||
y := b.Rect.Max.Y - (b.Rect.Dy()-uiFontMHeight)/2
|
||||
y := b.Rect.Min.Y + (b.Rect.Dy()-h)/2 + m.Ascent.Floor()
|
||||
text.Draw(dst, b.Text, uiFont, x, y, color.Black)
|
||||
}
|
||||
|
||||
@ -338,6 +336,8 @@ func (t *TextBox) contentOffset() (int, int) {
|
||||
func (t *TextBox) Draw(dst *ebiten.Image) {
|
||||
drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox])
|
||||
|
||||
// TODO: Use a sub-image of dst instead of an offscreen contentBuf.
|
||||
// Using a sub-image is better in terms of performance.
|
||||
if t.contentBuf != nil {
|
||||
vw, vh := t.viewSize()
|
||||
w, h := t.contentBuf.Bounds().Dx(), t.contentBuf.Bounds().Dy()
|
||||
@ -352,9 +352,10 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
|
||||
}
|
||||
|
||||
t.contentBuf.Clear()
|
||||
m := uiFont.Metrics()
|
||||
for i, line := range strings.Split(t.Text, "\n") {
|
||||
x := -t.offsetX + textBoxPaddingLeft
|
||||
y := -t.offsetY + i*lineHeight + lineHeight - (lineHeight-uiFontMHeight)/2
|
||||
y := -t.offsetY + i*lineHeight + (lineHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor()
|
||||
if y < -lineHeight {
|
||||
continue
|
||||
}
|
||||
@ -388,8 +389,7 @@ type CheckBox struct {
|
||||
}
|
||||
|
||||
func (c *CheckBox) width() int {
|
||||
b, _ := font.BoundString(uiFont, c.Text)
|
||||
w := (b.Max.X - b.Min.X).Ceil()
|
||||
w := font.MeasureString(uiFont, c.Text).Floor()
|
||||
return checkboxWidth + checkboxPaddingLeft + w
|
||||
}
|
||||
|
||||
@ -423,8 +423,9 @@ func (c *CheckBox) Draw(dst *ebiten.Image) {
|
||||
drawNinePatches(dst, r, imageSrcRects[imageTypeCheckBoxMark])
|
||||
}
|
||||
|
||||
m := uiFont.Metrics()
|
||||
x := c.X + checkboxWidth + checkboxPaddingLeft
|
||||
y := (c.Y + 16) - (16-uiFontMHeight)/2
|
||||
y := c.Y + (checkboxHeight-(m.Ascent+m.Descent).Floor())/2 + m.Ascent.Floor()
|
||||
text.Draw(dst, c.Text, uiFont, x, y, color.Black)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user