| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package main |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"image" |
| 19 |
"image/color" |
| 20 |
"image/draw" |
| 21 |
"log" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"golang.org/x/exp/shiny/driver" |
| 25 |
"golang.org/x/exp/shiny/screen" |
| 26 |
"golang.org/x/image/font" |
| 27 |
"golang.org/x/image/font/inconsolata" |
| 28 |
"golang.org/x/image/math/fixed" |
| 29 |
"golang.org/x/mobile/event/key" |
| 30 |
"golang.org/x/mobile/event/lifecycle" |
| 31 |
"golang.org/x/mobile/event/mouse" |
| 32 |
"golang.org/x/mobile/event/paint" |
| 33 |
"golang.org/x/mobile/event/size" |
| 34 |
) |
| 35 |
|
| 36 |
func main() { |
| 37 |
driver.Main(func(s screen.Screen) { |
| 38 |
w, err := s.NewWindow(&screen.NewWindowOptions{ |
| 39 |
Title: "Tile Shiny Example", |
| 40 |
}) |
| 41 |
if err != nil { |
| 42 |
log.Fatal(err) |
| 43 |
} |
| 44 |
defer w.Release() |
| 45 |
|
| 46 |
var ( |
| 47 |
pool = &tilePool{ |
| 48 |
screen: s, |
| 49 |
drawRGBA: drawRGBA, |
| 50 |
m: map[image.Point]*tilePoolEntry{}, |
| 51 |
} |
| 52 |
dragging bool |
| 53 |
paintPending bool |
| 54 |
drag image.Point |
| 55 |
origin image.Point |
| 56 |
sz size.Event |
| 57 |
) |
| 58 |
for { |
| 59 |
switch e := w.NextEvent().(type) { |
| 60 |
case lifecycle.Event: |
| 61 |
if e.To == lifecycle.StageDead { |
| 62 |
return |
| 63 |
} |
| 64 |
|
| 65 |
case key.Event: |
| 66 |
if e.Code == key.CodeEscape { |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
case mouse.Event: |
| 71 |
p := image.Point{X: int(e.X), Y: int(e.Y)} |
| 72 |
if e.Button == mouse.ButtonLeft && e.Direction != mouse.DirNone { |
| 73 |
dragging = e.Direction == mouse.DirPress |
| 74 |
drag = p |
| 75 |
} |
| 76 |
if !dragging { |
| 77 |
break |
| 78 |
} |
| 79 |
origin = origin.Sub(p.Sub(drag)) |
| 80 |
drag = p |
| 81 |
if origin.X < 0 { |
| 82 |
origin.X = 0 |
| 83 |
} |
| 84 |
if origin.Y < 0 { |
| 85 |
origin.Y = 0 |
| 86 |
} |
| 87 |
if !paintPending { |
| 88 |
paintPending = true |
| 89 |
w.Send(paint.Event{}) |
| 90 |
} |
| 91 |
|
| 92 |
case paint.Event: |
| 93 |
generation++ |
| 94 |
var wg sync.WaitGroup |
| 95 |
for y := -(origin.Y & 0xff); y < sz.HeightPx; y += 256 { |
| 96 |
for x := -(origin.X & 0xff); x < sz.WidthPx; x += 256 { |
| 97 |
wg.Add(1) |
| 98 |
go drawTile(&wg, w, pool, origin, x, y) |
| 99 |
} |
| 100 |
} |
| 101 |
wg.Wait() |
| 102 |
w.Publish() |
| 103 |
paintPending = false |
| 104 |
pool.releaseUnused() |
| 105 |
|
| 106 |
case size.Event: |
| 107 |
sz = e |
| 108 |
|
| 109 |
case error: |
| 110 |
log.Print(e) |
| 111 |
} |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
func drawTile(wg *sync.WaitGroup, w screen.Window, pool *tilePool, origin image.Point, x, y int) { |
| 117 |
defer wg.Done() |
| 118 |
tp := image.Point{ |
| 119 |
(x + origin.X) >> 8, |
| 120 |
(y + origin.Y) >> 8, |
| 121 |
} |
| 122 |
tex, err := pool.get(tp) |
| 123 |
if err != nil { |
| 124 |
log.Println(err) |
| 125 |
return |
| 126 |
} |
| 127 |
w.Copy(image.Point{x, y}, tex, tileBounds, screen.Src, nil) |
| 128 |
} |
| 129 |
|
| 130 |
func drawRGBA(m *image.RGBA, tp image.Point) { |
| 131 |
draw.Draw(m, m.Bounds(), image.White, image.Point{}, draw.Src) |
| 132 |
for _, p := range crossPoints { |
| 133 |
m.SetRGBA(p.X, p.Y, crossColor) |
| 134 |
} |
| 135 |
d := font.Drawer{ |
| 136 |
Dst: m, |
| 137 |
Src: image.Black, |
| 138 |
Face: inconsolata.Regular8x16, |
| 139 |
Dot: fixed.Point26_6{ |
| 140 |
Y: inconsolata.Regular8x16.Metrics().Ascent, |
| 141 |
}, |
| 142 |
} |
| 143 |
d.DrawString(fmt.Sprint(tp)) |
| 144 |
} |
| 145 |
|
| 146 |
var ( |
| 147 |
crossColor = color.RGBA{0x7f, 0x00, 0x00, 0xff} |
| 148 |
crossPoints = []image.Point{ |
| 149 |
{0x00, 0xfe}, |
| 150 |
{0x00, 0xff}, |
| 151 |
{0xfe, 0x00}, |
| 152 |
{0xff, 0x00}, |
| 153 |
{0x00, 0x00}, |
| 154 |
{0x01, 0x00}, |
| 155 |
{0x02, 0x00}, |
| 156 |
{0x00, 0x01}, |
| 157 |
{0x00, 0x02}, |
| 158 |
|
| 159 |
{0x80, 0x7f}, |
| 160 |
{0x7f, 0x80}, |
| 161 |
{0x80, 0x80}, |
| 162 |
{0x81, 0x80}, |
| 163 |
{0x80, 0x81}, |
| 164 |
|
| 165 |
{0x80, 0x00}, |
| 166 |
|
| 167 |
{0x00, 0x80}, |
| 168 |
} |
| 169 |
|
| 170 |
generation int |
| 171 |
|
| 172 |
tileSize = image.Point{256, 256} |
| 173 |
tileBounds = image.Rectangle{Max: tileSize} |
| 174 |
) |
| 175 |
|
| 176 |
type tilePoolEntry struct { |
| 177 |
tex screen.Texture |
| 178 |
gen int |
| 179 |
} |
| 180 |
|
| 181 |
type tilePool struct { |
| 182 |
screen screen.Screen |
| 183 |
drawRGBA func(*image.RGBA, image.Point) |
| 184 |
|
| 185 |
mu sync.Mutex |
| 186 |
m map[image.Point]*tilePoolEntry |
| 187 |
} |
| 188 |
|
| 189 |
func (p *tilePool) get(tp image.Point) (screen.Texture, error) { |
| 190 |
p.mu.Lock() |
| 191 |
v, ok := p.m[tp] |
| 192 |
if v != nil { |
| 193 |
v.gen = generation |
| 194 |
} |
| 195 |
p.mu.Unlock() |
| 196 |
|
| 197 |
if ok { |
| 198 |
return v.tex, nil |
| 199 |
} |
| 200 |
tex, err := p.screen.NewTexture(tileSize) |
| 201 |
if err != nil { |
| 202 |
return nil, err |
| 203 |
} |
| 204 |
buf, err := p.screen.NewBuffer(tileSize) |
| 205 |
if err != nil { |
| 206 |
tex.Release() |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
p.drawRGBA(buf.RGBA(), tp) |
| 210 |
tex.Upload(image.Point{}, buf, tileBounds) |
| 211 |
buf.Release() |
| 212 |
|
| 213 |
p.mu.Lock() |
| 214 |
p.m[tp] = &tilePoolEntry{ |
| 215 |
tex: tex, |
| 216 |
gen: generation, |
| 217 |
} |
| 218 |
n := len(p.m) |
| 219 |
p.mu.Unlock() |
| 220 |
|
| 221 |
fmt.Printf("%4d textures; created %v\n", n, tp) |
| 222 |
return tex, nil |
| 223 |
} |
| 224 |
|
| 225 |
func (p *tilePool) releaseUnused() { |
| 226 |
p.mu.Lock() |
| 227 |
defer p.mu.Unlock() |
| 228 |
|
| 229 |
for tp, v := range p.m { |
| 230 |
if v.gen == generation { |
| 231 |
continue |
| 232 |
} |
| 233 |
v.tex.Release() |
| 234 |
delete(p.m, tp) |
| 235 |
fmt.Printf("%4d textures; released %v\n", len(p.m), tp) |
| 236 |
} |
| 237 |
} |