Line data Source code
1 : package uicore
2 :
3 : import (
4 : "image"
5 : "image/color"
6 : "math"
7 : "sync"
8 :
9 : "github.com/hajimehoshi/ebiten/v2"
10 : text "github.com/hajimehoshi/ebiten/v2/text/v2"
11 : "github.com/hajimehoshi/ebiten/v2/vector"
12 : )
13 :
14 : // EbitenCanvas は Canvas を ebiten の描画で満たす本番実装。
15 : type EbitenCanvas struct {
16 : screen *ebiten.Image
17 : }
18 :
19 : var _ Canvas = (*EbitenCanvas)(nil)
20 :
21 : // NewEbitenCanvas は描画先スクリーンを与えて Canvas を作る。
22 : func NewEbitenCanvas(screen *ebiten.Image) *EbitenCanvas {
23 1 : return &EbitenCanvas{screen: screen}
24 1 : }
25 :
26 : // FillRect は EbitenCanvas を実装する。opts に正の Radius があれば四隅を丸めて塗る。
27 : func (e *EbitenCanvas) FillRect(r image.Rectangle, c color.Color, opts RectOptions) {
28 1 : if opts.Radius > 0 {
29 0 : e.drawShape(r.Min, roundedFillShape(r.Dx(), r.Dy(), opts.Radius, c))
30 0 : return
31 0 : }
32 1 : vector.FillRect(e.screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), c, false)
33 : }
34 :
35 : // StrokeRect は EbitenCanvas を実装する。opts に正の Radius があれば四隅を丸めて枠を描く。
36 : func (e *EbitenCanvas) StrokeRect(r image.Rectangle, width int, c color.Color, opts RectOptions) {
37 1 : if opts.Radius > 0 {
38 : // 枠は線幅の半分だけ矩形の外へ出る。画像は線幅ぶん広く焼いてあるので、左上を線幅ぶん戻して重ねる
39 0 : shape := roundedStrokeShape(r.Dx(), r.Dy(), width, opts.Radius, c)
40 0 : e.drawShape(r.Min.Sub(image.Pt(width, width)), shape)
41 0 : return
42 0 : }
43 1 : vector.StrokeRect(e.screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), float32(width), c, false)
44 : }
45 :
46 : // drawShape は焼き済みの形状画像を pos へ素で描く。色は焼き込み済みなので色掛けはしない。
47 : func (e *EbitenCanvas) drawShape(pos image.Point, shape *ebiten.Image) {
48 0 : op := &ebiten.DrawImageOptions{}
49 0 : op.GeoM.Translate(float64(pos.X), float64(pos.Y))
50 0 : e.screen.DrawImage(shape, op)
51 0 : }
52 :
53 : // clampRadius は半径を矩形の短辺の半分までに抑える。小さな矩形へ大きな半径を与えても
54 : // 円弧が交差してパスが壊れないようにする。
55 : func clampRadius(radius, w, h int) float32 {
56 1 : rad := float32(radius)
57 1 : if rad > float32(w)/2 {
58 1 : rad = float32(w) / 2
59 1 : }
60 1 : if rad > float32(h)/2 {
61 1 : rad = float32(h) / 2
62 1 : }
63 1 : return rad
64 : }
65 :
66 : // roundedRectPath は四隅を半径 radius で丸めた矩形のパスを組む。半径は矩形の短辺の半分までに丸める。
67 : func roundedRectPath(r image.Rectangle, radius int) vector.Path {
68 1 : x, y := float32(r.Min.X), float32(r.Min.Y)
69 1 : w, h := float32(r.Dx()), float32(r.Dy())
70 1 : rad := clampRadius(radius, r.Dx(), r.Dy())
71 1 : var p vector.Path
72 1 : p.MoveTo(x+rad, y)
73 1 : p.LineTo(x+w-rad, y)
74 1 : p.ArcTo(x+w, y, x+w, y+rad, rad)
75 1 : p.LineTo(x+w, y+h-rad)
76 1 : p.ArcTo(x+w, y+h, x+w-rad, y+h, rad)
77 1 : p.LineTo(x+rad, y+h)
78 1 : p.ArcTo(x, y+h, x, y+h-rad, rad)
79 1 : p.LineTo(x, y+rad)
80 1 : p.ArcTo(x, y, x+rad, y, rad)
81 1 : p.Close()
82 1 : return p
83 1 : }
84 :
85 : // 角丸は毎フレーム vector.FillPath でラスタライズすると CPU 実描画で重く、パスの分割で
86 : // フレームごとにアロケーションも出る。同じ寸法・色は繰り返し使われるので、形状を色ごと画像へ
87 : // 一度だけ焼き、描画は素の DrawImage で済ませる。
88 : // 焼く画像は Unmanaged にして共有アトラスへ載せない。アトラス配置は生成順に依存し、並行する
89 : // ゴールデンテストで描画結果がぶれる。Unmanaged なら各形状が独立テクスチャで決定的になる。
90 : // 色を描画時に掛けると半透明色で量子化が二重になり直接描画とずれるため、色は焼く時点で塗り込む。
91 : // Draw は単一ゴルーチンだがテストが並行にキャッシュへ触れるので map は mutex で守る。生成も
92 : // ロック内に置き、並行テストで ebiten の画像生成が同時に走らないようにする。二重チェックで
93 : // ロック外へ出すと生成が並行しうるので、あえて直列化する。
94 : // キーはパネルの数種の寸法と theme の色に限られ有限なので、測定キャッシュと違い上限は要らない。
95 : var (
96 : roundedShapeMu sync.Mutex
97 : roundedFillShapes = map[roundedFillKey]*ebiten.Image{}
98 : roundedStrokeShapes = map[roundedStrokeKey]*ebiten.Image{}
99 : )
100 :
101 : type roundedFillKey struct {
102 : w, h, radius int
103 : c color.RGBA
104 : }
105 : type roundedStrokeKey struct {
106 : w, h, width, radius int
107 : c color.RGBA
108 : }
109 :
110 : // roundedFillShape は w×h の角丸塗りを色 c で焼いた画像を返す。左上を原点として描く。
111 : func roundedFillShape(w, h, radius int, c color.Color) *ebiten.Image {
112 1 : rgba, _ := color.RGBAModel.Convert(c).(color.RGBA)
113 1 : key := roundedFillKey{w, h, radius, rgba}
114 1 : roundedShapeMu.Lock()
115 1 : defer roundedShapeMu.Unlock()
116 1 : if img, ok := roundedFillShapes[key]; ok {
117 1 : return img
118 1 : }
119 1 : img := ebiten.NewImageWithOptions(image.Rect(0, 0, w, h), &ebiten.NewImageOptions{Unmanaged: true})
120 1 : p := roundedRectPath(image.Rect(0, 0, w, h), radius)
121 1 : dop := &vector.DrawPathOptions{AntiAlias: true}
122 1 : dop.ColorScale.ScaleWithColor(c)
123 1 : vector.FillPath(img, &p, &vector.FillOptions{}, dop)
124 1 : roundedFillShapes[key] = img
125 1 : return img
126 : }
127 :
128 : // roundedStrokeShape は w×h の角丸枠を色 c で焼いた画像を返す。枠は線幅の半分だけ矩形の外へ
129 : // はみ出すので、線幅ぶん広げた画像に焼く。左上へ合わせるずらしは呼び出し側が線幅から決める。
130 : func roundedStrokeShape(w, h, width, radius int, c color.Color) *ebiten.Image {
131 0 : rgba, _ := color.RGBAModel.Convert(c).(color.RGBA)
132 0 : key := roundedStrokeKey{w, h, width, radius, rgba}
133 0 : roundedShapeMu.Lock()
134 0 : defer roundedShapeMu.Unlock()
135 0 : if img, ok := roundedStrokeShapes[key]; ok {
136 0 : return img
137 0 : }
138 0 : img := ebiten.NewImageWithOptions(image.Rect(0, 0, w+width*2, h+width*2), &ebiten.NewImageOptions{Unmanaged: true})
139 0 : p := roundedRectPath(image.Rect(width, width, width+w, width+h), radius)
140 0 : dop := &vector.DrawPathOptions{AntiAlias: true}
141 0 : dop.ColorScale.ScaleWithColor(c)
142 0 : vector.StrokePath(img, &p, &vector.StrokeOptions{Width: float32(width)}, dop)
143 0 : roundedStrokeShapes[key] = img
144 0 : return img
145 : }
146 :
147 : // FillTriangle は EbitenCanvas を実装する。3頂点の三角形を塗る。text/v2 を通らないのでロックは要らない。
148 : // 頂点が呼び出しごとに変わり形が定まらないので、角丸のような形状キャッシュは持たず毎回パスを組む。
149 : func (e *EbitenCanvas) FillTriangle(p0, p1, p2 [2]float32, c color.Color) {
150 1 : var path vector.Path
151 1 : path.MoveTo(p0[0], p0[1])
152 1 : path.LineTo(p1[0], p1[1])
153 1 : path.LineTo(p2[0], p2[1])
154 1 : path.Close()
155 1 : dop := &vector.DrawPathOptions{AntiAlias: true}
156 1 : dop.ColorScale.ScaleWithColor(c)
157 1 : vector.FillPath(e.screen, &path, &vector.FillOptions{}, dop)
158 1 : }
159 :
160 : // DrawText は EbitenCanvas を実装する。pos を左上として1行を描く。
161 : func (e *EbitenCanvas) DrawText(pos image.Point, s string, face text.Face, c color.Color) {
162 1 : op := &text.DrawOptions{}
163 1 : op.GeoM.Translate(float64(pos.X), float64(pos.Y))
164 1 : op.ColorScale.ScaleWithColor(c)
165 1 : // ebiten text/v2 の共有グリフキャッシュを壊さないよう描画を直列化する。詳細は textMu を参照
166 1 : textMu.Lock()
167 1 : if textObserver != nil {
168 0 : textObserver(face, s)
169 0 : }
170 1 : text.Draw(e.screen, s, face, op)
171 1 : textMu.Unlock()
172 : }
173 :
174 : // textObserver は DrawText が描く文字列を受け取る。textMu で守る
175 : var textObserver func(face text.Face, s string)
176 :
177 : // ObserveText は以後 DrawText が描く文字列を f へ渡す。nil で外す。
178 : // 並列に走る他の描画の文字列も届くので、f はフェイスで自分の描画を選り分ける。
179 : func ObserveText(f func(face text.Face, s string)) {
180 0 : textMu.Lock()
181 0 : textObserver = f
182 0 : textMu.Unlock()
183 0 : }
184 :
185 : // DrawImage は EbitenCanvas を実装する。pos を左上として画像を描く。
186 : func (e *EbitenCanvas) DrawImage(pos image.Point, img *ebiten.Image) {
187 1 : op := &ebiten.DrawImageOptions{}
188 1 : op.GeoM.Translate(float64(pos.X), float64(pos.Y))
189 1 : e.screen.DrawImage(img, op)
190 1 : }
191 :
192 : // DrawImageRect は EbitenCanvas を実装する。dst に収まるよう縦横比を保って縮小し、左寄せ・縦中央で描く。
193 : func (e *EbitenCanvas) DrawImageRect(dst image.Rectangle, img *ebiten.Image) {
194 1 : if img == nil {
195 1 : return
196 1 : }
197 1 : b := img.Bounds()
198 1 : iw, ih := b.Dx(), b.Dy()
199 1 : if iw <= 0 || ih <= 0 || dst.Dx() <= 0 || dst.Dy() <= 0 {
200 1 : return
201 1 : }
202 1 : scale := math.Min(math.Min(float64(dst.Dx())/float64(iw), float64(dst.Dy())/float64(ih)), 1)
203 1 : dh := float64(ih) * scale
204 1 : // 左寄せ・縦中央。アイコンとキーキャップを列の左に揃え、行の高さの中央へ置く
205 1 : ox := float64(dst.Min.X)
206 1 : oy := float64(dst.Min.Y) + (float64(dst.Dy())-dh)/2
207 1 : op := &ebiten.DrawImageOptions{}
208 1 : op.GeoM.Scale(scale, scale)
209 1 : op.GeoM.Translate(ox, oy)
210 1 : // 収まりきらない画像だけがここで縮む。端数倍率の nearest はテクセル境界の画素が
211 : // 揺れるので linear にする。一覧のアイコンは表示サイズで渡ってくるため等倍で通り、
212 : // この分岐には入らない
213 1 : if scale != 1 {
214 1 : op.Filter = ebiten.FilterLinear
215 1 : }
216 1 : e.screen.DrawImage(img, op)
217 : }
218 :
219 : // DrawImageTintedRect は EbitenCanvas を実装する。img を dst いっぱいに引き伸ばし tint を掛けて描く。
220 : func (e *EbitenCanvas) DrawImageTintedRect(dst image.Rectangle, img *ebiten.Image, tint color.Color) {
221 1 : if img == nil {
222 1 : return
223 1 : }
224 1 : b := img.Bounds()
225 1 : if b.Dx() <= 0 || b.Dy() <= 0 || dst.Dx() <= 0 || dst.Dy() <= 0 {
226 1 : return
227 1 : }
228 1 : sx, sy := float64(dst.Dx())/float64(b.Dx()), float64(dst.Dy())/float64(b.Dy())
229 1 : op := &ebiten.DrawImageOptions{}
230 1 : op.GeoM.Scale(sx, sy)
231 1 : op.GeoM.Translate(float64(dst.Min.X), float64(dst.Min.Y))
232 1 : op.ColorScale.ScaleWithColor(tint)
233 1 : // 伸ばす軸がどれも2テクセル以上あるときだけ linear にする。判定の理由は stretchNeedsBlend を参照
234 1 : if stretchNeedsBlend(sx, b.Dx()) && stretchNeedsBlend(sy, b.Dy()) {
235 1 : op.Filter = ebiten.FilterLinear
236 1 : }
237 1 : e.screen.DrawImage(img, op)
238 : }
239 :
240 : // stretchNeedsBlend はその軸を混ぜるべきかを返す。端数倍率の nearest はテクセル境界の
241 : // 画素が揺れるので、混ぜられる軸は linear で混ぜたい。等倍なら境界をまたがず混ぜる必要が無い。
242 : // 1テクセルしか無い軸は混ぜる相手が画像の外側の透明しかなく、端がぼやけるだけなので混ぜない。
243 : func stretchNeedsBlend(scale float64, srcLen int) bool {
244 1 : return scale != 1 && srcLen > 1
245 1 : }
246 :
247 : // DrawNineSlice は EbitenCanvas を実装する。ソースを縦横3分割し、四隅は原寸、辺は片方向、
248 : // 中央は両方向へ伸ばして9セルを dst へ描く。
249 : func (e *EbitenCanvas) DrawNineSlice(dst image.Rectangle, img *ebiten.Image, bx, by [3]int) {
250 1 : if img == nil {
251 1 : return
252 1 : }
253 : // ソースの分割境界
254 1 : sx := [4]int{0, bx[0], bx[0] + bx[1], bx[0] + bx[1] + bx[2]}
255 1 : sy := [4]int{0, by[0], by[0] + by[1], by[0] + by[1] + by[2]}
256 1 : // dst の分割境界。四隅は原寸、中央は残りを埋める
257 1 : midW := max(dst.Dx()-bx[0]-bx[2], 0)
258 1 : midH := max(dst.Dy()-by[0]-by[2], 0)
259 1 : dx := [4]int{dst.Min.X, dst.Min.X + bx[0], dst.Min.X + bx[0] + midW, dst.Max.X}
260 1 : dy := [4]int{dst.Min.Y, dst.Min.Y + by[0], dst.Min.Y + by[0] + midH, dst.Max.Y}
261 1 :
262 1 : for r := range 3 {
263 1 : for c := range 3 {
264 1 : sw, sh := sx[c+1]-sx[c], sy[r+1]-sy[r]
265 1 : dw, dh := dx[c+1]-dx[c], dy[r+1]-dy[r]
266 1 : if sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0 {
267 1 : continue
268 : }
269 1 : sub, ok := img.SubImage(image.Rect(sx[c], sy[r], sx[c+1], sy[r+1])).(*ebiten.Image)
270 1 : if !ok {
271 0 : continue
272 : }
273 1 : ssx, ssy := float64(dw)/float64(sw), float64(dh)/float64(sh)
274 1 : op := &ebiten.DrawImageOptions{}
275 1 : op.GeoM.Scale(ssx, ssy)
276 1 : op.GeoM.Translate(float64(dx[c]), float64(dy[r]))
277 1 : // 四隅は原寸で通り、伸びるのは辺と中央だけ。端数倍率の nearest は
278 : // テクセル境界の画素が揺れるので linear にする
279 1 : if ssx != 1 || ssy != 1 {
280 1 : op.Filter = ebiten.FilterLinear
281 1 : }
282 1 : e.screen.DrawImage(sub, op)
283 : }
284 : }
285 : }
|