mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-09 01:14:28 +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
|
f = mplusNormalFont
|
||||||
}
|
}
|
||||||
|
|
||||||
bound, _ := font.BoundString(f, str)
|
w := font.MeasureString(f, str).Floor()
|
||||||
w := (bound.Max.X - bound.Min.X).Ceil()
|
h := (f.Metrics().Ascent + f.Metrics().Descent).Floor()
|
||||||
h := (bound.Max.Y - bound.Min.Y).Ceil()
|
x += (tileSize - w) / 2
|
||||||
x = x + (tileSize-w)/2
|
y += (tileSize-h)/2 + f.Metrics().Ascent.Floor()
|
||||||
y = y + (tileSize-h)/2 + h
|
|
||||||
text.Draw(boardImage, str, f, x, y, tileColor(v))
|
text.Draw(boardImage, str, f, x, y, tileColor(v))
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,8 @@ func getArcadeFonts(scale int) font.Face {
|
|||||||
func textWidth(str string) int {
|
func textWidth(str string) int {
|
||||||
maxW := 0
|
maxW := 0
|
||||||
for _, line := range strings.Split(str, "\n") {
|
for _, line := range strings.Split(str, "\n") {
|
||||||
b, _ := font.BoundString(getArcadeFonts(1), line)
|
a := font.MeasureString(getArcadeFonts(1), line)
|
||||||
w := (b.Max.X - b.Min.X).Ceil()
|
w := a.Floor()
|
||||||
if maxW < w {
|
if maxW < w {
|
||||||
maxW = w
|
maxW = w
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
uiImage *ebiten.Image
|
uiImage *ebiten.Image
|
||||||
uiFont font.Face
|
uiFont font.Face
|
||||||
uiFontMHeight int
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -62,8 +61,6 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
b, _, _ := uiFont.GlyphBounds('M')
|
|
||||||
uiFontMHeight = (b.Max.Y - b.Min.Y).Ceil()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageType int
|
type imageType int
|
||||||
@ -186,10 +183,11 @@ func (b *Button) Draw(dst *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
drawNinePatches(dst, b.Rect, imageSrcRects[t])
|
||||||
|
|
||||||
bounds, _ := font.BoundString(uiFont, b.Text)
|
m := uiFont.Metrics()
|
||||||
w := (bounds.Max.X - bounds.Min.X).Ceil()
|
w := font.MeasureString(uiFont, b.Text).Floor()
|
||||||
|
h := (m.Ascent + m.Descent).Floor()
|
||||||
x := b.Rect.Min.X + (b.Rect.Dx()-w)/2
|
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)
|
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) {
|
func (t *TextBox) Draw(dst *ebiten.Image) {
|
||||||
drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox])
|
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 {
|
if t.contentBuf != nil {
|
||||||
vw, vh := t.viewSize()
|
vw, vh := t.viewSize()
|
||||||
w, h := t.contentBuf.Bounds().Dx(), t.contentBuf.Bounds().Dy()
|
w, h := t.contentBuf.Bounds().Dx(), t.contentBuf.Bounds().Dy()
|
||||||
@ -352,9 +352,10 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.contentBuf.Clear()
|
t.contentBuf.Clear()
|
||||||
|
m := uiFont.Metrics()
|
||||||
for i, line := range strings.Split(t.Text, "\n") {
|
for i, line := range strings.Split(t.Text, "\n") {
|
||||||
x := -t.offsetX + textBoxPaddingLeft
|
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 {
|
if y < -lineHeight {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -388,8 +389,7 @@ type CheckBox struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CheckBox) width() int {
|
func (c *CheckBox) width() int {
|
||||||
b, _ := font.BoundString(uiFont, c.Text)
|
w := font.MeasureString(uiFont, c.Text).Floor()
|
||||||
w := (b.Max.X - b.Min.X).Ceil()
|
|
||||||
return checkboxWidth + checkboxPaddingLeft + w
|
return checkboxWidth + checkboxPaddingLeft + w
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,8 +423,9 @@ 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 + 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)
|
text.Draw(dst, c.Text, uiFont, x, y, color.Black)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user