| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"flag" |
| 21 |
"image" |
| 22 |
"log" |
| 23 |
"math/rand" |
| 24 |
"time" |
| 25 |
|
| 26 |
"golang.org/x/exp/shiny/driver" |
| 27 |
"golang.org/x/exp/shiny/screen" |
| 28 |
"golang.org/x/mobile/event/key" |
| 29 |
"golang.org/x/mobile/event/lifecycle" |
| 30 |
"golang.org/x/mobile/event/mouse" |
| 31 |
"golang.org/x/mobile/event/paint" |
| 32 |
"golang.org/x/mobile/event/size" |
| 33 |
) |
| 34 |
|
| 35 |
var scale = flag.Int("scale", 35, "`percent` to scale images (TODO: a poor design)") |
| 36 |
|
| 37 |
func main() { |
| 38 |
flag.Parse() |
| 39 |
|
| 40 |
rand.Seed(int64(time.Now().Nanosecond())) |
| 41 |
board := NewBoard(9, *scale) |
| 42 |
|
| 43 |
driver.Main(func(s screen.Screen) { |
| 44 |
w, err := s.NewWindow(&screen.NewWindowOptions{ |
| 45 |
Title: "Goban Shiny Example", |
| 46 |
}) |
| 47 |
if err != nil { |
| 48 |
log.Fatal(err) |
| 49 |
} |
| 50 |
defer w.Release() |
| 51 |
|
| 52 |
var b screen.Buffer |
| 53 |
defer func() { |
| 54 |
if b != nil { |
| 55 |
b.Release() |
| 56 |
} |
| 57 |
}() |
| 58 |
|
| 59 |
for { |
| 60 |
switch e := w.NextEvent().(type) { |
| 61 |
case lifecycle.Event: |
| 62 |
if e.To == lifecycle.StageDead { |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
case key.Event: |
| 67 |
if e.Code == key.CodeEscape { |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
case mouse.Event: |
| 72 |
if e.Direction != mouse.DirRelease { |
| 73 |
break |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if e.Modifiers&key.ModControl != 0 { |
| 78 |
e.Button = mouse.ButtonMiddle |
| 79 |
} else if e.Modifiers&key.ModAlt != 0 { |
| 80 |
e.Button = mouse.ButtonRight |
| 81 |
} else if e.Modifiers&key.ModMeta != 0 { |
| 82 |
e.Button = mouse.ButtonMiddle |
| 83 |
} |
| 84 |
|
| 85 |
if board.click(b.RGBA(), int(e.X), int(e.Y), int(e.Button)) { |
| 86 |
w.Send(paint.Event{}) |
| 87 |
} |
| 88 |
|
| 89 |
case paint.Event: |
| 90 |
w.Upload(image.Point{}, b, b.Bounds()) |
| 91 |
w.Publish() |
| 92 |
|
| 93 |
case size.Event: |
| 94 |
|
| 95 |
if b != nil { |
| 96 |
b.Release() |
| 97 |
} |
| 98 |
b, err = s.NewBuffer(e.Size()) |
| 99 |
if err != nil { |
| 100 |
log.Fatal(err) |
| 101 |
} |
| 102 |
render(b.RGBA(), board) |
| 103 |
|
| 104 |
case error: |
| 105 |
log.Print(e) |
| 106 |
} |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
|
| 111 |
func render(m *image.RGBA, board *Board) { |
| 112 |
board.Draw(m) |
| 113 |
} |