1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
//
// This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
// install this example program. Use "go run main.go" to run it.
// Basic is a basic example of a graphical application.
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"log"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
"golang.org/x/image/math/f64"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
func main() {
driver.Main(func(s screen.Screen) {
w, err := s.NewWindow(nil)
if err != nil {
log.Fatal(err)
}
defer w.Release()
winSize := image.Point{256, 256}
b, err := s.NewBuffer(winSize)
if err != nil {
log.Fatal(err)
}
defer b.Release()
drawGradient(b.RGBA())
t, err := s.NewTexture(winSize)
if err != nil {
log.Fatal(err)
}
defer t.Release()
t.Upload(image.Point{}, b, b.Bounds(), w)
var sz size.Event
for e := range w.Events() {
switch e := e.(type) {
default:
// TODO: be more interesting.
fmt.Printf("got %#v\n", e)
case key.Event:
fmt.Printf("got %v\n", e)
if e.Code == key.CodeEscape {
return
}
case paint.Event:
wBounds := image.Rectangle{Max: image.Point{sz.WidthPx, sz.HeightPx}}
w.Fill(wBounds, color.RGBA{0x00, 0x00, 0x3f, 0xff}, draw.Src)
w.Upload(image.Point{}, b, b.Bounds(), w)
w.Draw(f64.Aff3{
1, 0, 100,
0, 1, 200,
}, t, t.Bounds(), screen.Over, nil)
w.EndPaint(e)
case screen.UploadedEvent:
// No-op.
case size.Event:
sz = e
case error:
log.Print(e)
}
}
})
}
func drawGradient(m *image.RGBA) {
b := m.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
}
}
}
|