2020-07-29 17:19:08 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-15 14:01:24 +01:00
|
|
|
"bytes"
|
2020-07-29 17:19:08 +02:00
|
|
|
"image/color"
|
|
|
|
"log"
|
2021-07-27 10:01:36 +02:00
|
|
|
"math"
|
2020-07-29 17:19:08 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2024-08-22 16:36:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
2024-08-22 16:36:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2023-11-11 11:04:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2022-10-21 08:23:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2020-07-29 17:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
|
|
|
)
|
|
|
|
|
2022-04-03 17:18:35 +02:00
|
|
|
const sampleText = ` The quick brown fox jumps
|
2020-07-29 17:19:08 +02:00
|
|
|
over the lazy dog.`
|
|
|
|
|
|
|
|
var (
|
2023-11-15 14:01:24 +01:00
|
|
|
mplusFaceSource *text.GoTextFaceSource
|
|
|
|
mplusNormalFace *text.GoTextFace
|
|
|
|
mplusBigFace *text.GoTextFace
|
2020-07-29 17:19:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-11-15 14:01:24 +01:00
|
|
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
|
2020-07-29 17:19:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-11-15 14:01:24 +01:00
|
|
|
mplusFaceSource = s
|
2020-07-29 17:19:08 +02:00
|
|
|
|
2023-11-15 14:01:24 +01:00
|
|
|
mplusNormalFace = &text.GoTextFace{
|
|
|
|
Source: mplusFaceSource,
|
|
|
|
Size: 24,
|
2020-10-03 16:12:39 +02:00
|
|
|
}
|
2023-11-15 14:01:24 +01:00
|
|
|
mplusBigFace = &text.GoTextFace{
|
|
|
|
Source: mplusFaceSource,
|
|
|
|
Size: 32,
|
2020-10-03 16:12:39 +02:00
|
|
|
}
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Game struct {
|
2024-08-22 16:36:20 +02:00
|
|
|
glyphs []text.Glyph
|
|
|
|
showOrigins bool
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2021-11-11 16:54:15 +01:00
|
|
|
// Initialize the glyphs for special (colorful) rendering.
|
2023-12-05 11:44:32 +01:00
|
|
|
if len(g.glyphs) == 0 {
|
2023-11-13 15:15:54 +01:00
|
|
|
op := &text.LayoutOptions{}
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = mplusNormalFace.Size * 1.5
|
2023-12-05 11:44:32 +01:00
|
|
|
g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op)
|
2023-06-10 12:21:01 +02:00
|
|
|
}
|
2024-08-22 16:36:20 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyO) {
|
|
|
|
g.showOrigins = !g.showOrigins
|
|
|
|
}
|
2023-11-11 11:04:13 +01:00
|
|
|
return nil
|
2023-06-10 12:21:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2024-08-22 16:36:20 +02:00
|
|
|
ebitenutil.DebugPrint(screen, "Press O to show/hide origins")
|
|
|
|
|
2020-07-29 17:19:08 +02:00
|
|
|
gray := color.RGBA{0x80, 0x80, 0x80, 0xff}
|
|
|
|
|
|
|
|
{
|
2023-11-12 17:36:04 +01:00
|
|
|
const x, y = 20, 20
|
2023-12-02 06:26:17 +01:00
|
|
|
w, h := text.Measure(sampleText, mplusNormalFace, mplusNormalFace.Size*1.5)
|
2023-11-11 11:04:13 +01:00
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
|
|
|
op.GeoM.Translate(x, y)
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = mplusNormalFace.Size * 1.5
|
2023-11-11 11:04:13 +01:00
|
|
|
text.Draw(screen, sampleText, mplusNormalFace, op)
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
{
|
2023-11-12 17:36:04 +01:00
|
|
|
const x, y = 20, 120
|
2023-12-02 06:26:17 +01:00
|
|
|
w, h := text.Measure(sampleText, mplusBigFace, mplusBigFace.Size*1.5)
|
2023-11-11 11:04:13 +01:00
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
|
|
|
op.GeoM.Translate(x, y)
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = mplusBigFace.Size * 1.5
|
2023-11-11 11:04:13 +01:00
|
|
|
text.Draw(screen, sampleText, mplusBigFace, op)
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
2021-07-27 10:01:36 +02:00
|
|
|
{
|
2023-11-12 17:36:04 +01:00
|
|
|
const x, y = 20, 220
|
2023-11-11 11:04:13 +01:00
|
|
|
op := &text.DrawOptions{}
|
2021-07-27 10:01:36 +02:00
|
|
|
op.GeoM.Rotate(math.Pi / 4)
|
|
|
|
op.GeoM.Translate(x, y)
|
|
|
|
op.Filter = ebiten.FilterLinear
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = mplusNormalFace.Size * 1.5
|
2023-11-11 11:04:13 +01:00
|
|
|
text.Draw(screen, sampleText, mplusNormalFace, op)
|
2021-07-27 10:01:36 +02:00
|
|
|
}
|
2021-09-12 15:53:41 +02:00
|
|
|
{
|
2023-11-12 17:36:04 +01:00
|
|
|
const x, y = 160, 220
|
2023-11-16 17:17:29 +01:00
|
|
|
const lineSpacingInPixels = 80
|
|
|
|
w, h := text.Measure(sampleText, mplusBigFace, lineSpacingInPixels)
|
2023-11-11 11:04:13 +01:00
|
|
|
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
|
|
|
|
op := &text.DrawOptions{}
|
2023-11-13 14:58:19 +01:00
|
|
|
// Add the width as the text rendering region's upper-right position comes to (0, 0)
|
|
|
|
// when the horizontal alignment is right. The alignment is specified later (PrimaryAlign).
|
|
|
|
op.GeoM.Translate(x+w, y)
|
2023-12-24 07:06:16 +01:00
|
|
|
op.LineSpacing = lineSpacingInPixels
|
2023-11-13 14:58:19 +01:00
|
|
|
// The primary alignment for the left-to-right direction is a horizontal alignment, and the end means the right.
|
|
|
|
op.PrimaryAlign = text.AlignEnd
|
2023-11-11 11:04:13 +01:00
|
|
|
text.Draw(screen, sampleText, mplusBigFace, op)
|
2021-09-12 15:53:41 +02:00
|
|
|
}
|
2021-11-11 16:54:15 +01:00
|
|
|
{
|
2023-11-13 15:15:54 +01:00
|
|
|
const x, y = 240, 360
|
2021-11-11 16:54:15 +01:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2023-12-05 11:44:32 +01:00
|
|
|
// g.glyphs is initialized by text.AppendGlyphs.
|
2021-11-11 16:54:15 +01:00
|
|
|
// You can customize how to render each glyph.
|
|
|
|
// In this example, multiple colors are used to render glyphs.
|
2023-12-05 11:44:32 +01:00
|
|
|
for i, gl := range g.glyphs {
|
2023-12-05 12:01:25 +01:00
|
|
|
if gl.Image == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-13 15:15:54 +01:00
|
|
|
op.GeoM.Reset()
|
|
|
|
op.GeoM.Translate(x, y)
|
2023-12-05 11:44:32 +01:00
|
|
|
op.GeoM.Translate(gl.X, gl.Y)
|
2023-11-13 15:15:54 +01:00
|
|
|
op.ColorScale.Reset()
|
|
|
|
r := float32(1)
|
|
|
|
if i%3 == 0 {
|
|
|
|
r = 0.5
|
|
|
|
}
|
|
|
|
g := float32(1)
|
|
|
|
if i%3 == 1 {
|
|
|
|
g = 0.5
|
|
|
|
}
|
|
|
|
b := float32(1)
|
|
|
|
if i%3 == 2 {
|
|
|
|
b = 0.5
|
2021-11-11 16:54:15 +01:00
|
|
|
}
|
2023-11-13 15:15:54 +01:00
|
|
|
op.ColorScale.Scale(r, g, b, 1)
|
2023-12-05 11:44:32 +01:00
|
|
|
screen.DrawImage(gl.Image, op)
|
2021-11-11 16:54:15 +01:00
|
|
|
}
|
2024-08-22 16:36:20 +02:00
|
|
|
|
|
|
|
if g.showOrigins {
|
|
|
|
for _, gl := range g.glyphs {
|
|
|
|
vector.DrawFilledCircle(screen, x+float32(gl.OriginX), y+float32(gl.OriginY), 2, color.RGBA{0xff, 0, 0, 0xff}, true)
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 16:54:15 +01:00
|
|
|
}
|
2020-07-29 17:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Text (Ebitengine Demo)")
|
2020-07-29 17:19:08 +02:00
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|