Debian Codegraph @

shiny/example/basic/main.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build example
6 // +build example
7
8 //
9 // This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
10 // install this example program. Use "go run main.go" to run it or "go install
11 // -tags=example" to install it.
12
13 // Basic is a basic example of a graphical application.
14 package main
15
16 import (
17 "fmt"
18 "image"
19 "image/color"
20 "log"
21 "math"
22
23 "golang.org/x/exp/shiny/driver"
24 "golang.org/x/exp/shiny/imageutil"
25 "golang.org/x/exp/shiny/screen"
26 "golang.org/x/image/math/f64"
27 "golang.org/x/mobile/event/key"
28 "golang.org/x/mobile/event/lifecycle"
29 "golang.org/x/mobile/event/paint"
30 "golang.org/x/mobile/event/size"
31 )
32
33 var (
34 blue0 = color.RGBA{0x00, 0x00, 0x1f, 0xff}
35 blue1 = color.RGBA{0x00, 0x00, 0x3f, 0xff}
36 darkGray = color.RGBA{0x3f, 0x3f, 0x3f, 0xff}
37 green = color.RGBA{0x00, 0x7f, 0x00, 0x7f}
38 red = color.RGBA{0x7f, 0x00, 0x00, 0x7f}
39 yellow = color.RGBA{0x3f, 0x3f, 0x00, 0x3f}
40
41 cos30 = math.Cos(math.Pi / 6)
42 sin30 = math.Sin(math.Pi / 6)
43 )
44
45 func main() {
46 driver.Main(func(s screen.Screen) {
47 w, err := s.NewWindow(&screen.NewWindowOptions{
48 Title: "Basic Shiny Example",
49 })
50 if err != nil {
51 log.Fatal(err)
52 }
53 defer w.Release()
54
55 size0 := image.Point{256, 256}
56 b, err := s.NewBuffer(size0)
57 if err != nil {
58 log.Fatal(err)
59 }
60 defer b.Release()
61 drawGradient(b.RGBA())
62
63 t0, err := s.NewTexture(size0)
64 if err != nil {
65 log.Fatal(err)
66 }
67 defer t0.Release()
68 t0.Upload(image.Point{}, b, b.Bounds())
69
70 size1 := image.Point{32, 20}
71 t1, err := s.NewTexture(size1)
72 if err != nil {
73 log.Fatal(err)
74 }
75 defer t1.Release()
76 t1.Fill(t1.Bounds(), green, screen.Src)
77 t1.Fill(t1.Bounds().Inset(2), red, screen.Over)
78 t1.Fill(t1.Bounds().Inset(4), red, screen.Src)
79
80 var sz size.Event
81 for {
82 e := w.NextEvent()
83
84 // This print message is to help programmers learn what events this
85 // example program generates. A real program shouldn't print such
86 // messages; they're not important to end users.
87 format := "got %#v\n"
88 if _, ok := e.(fmt.Stringer); ok {
89 format = "got %v\n"
90 }
91 fmt.Printf(format, e)
92
93 switch e := e.(type) {
94 case lifecycle.Event:
95 if e.To == lifecycle.StageDead {
96 return
97 }
98
99 case key.Event:
100 if e.Code == key.CodeEscape {
101 return
102 }
103
104 case paint.Event:
105 const inset = 10
106 for _, r := range imageutil.Border(sz.Bounds(), inset) {
107 w.Fill(r, blue0, screen.Src)
108 }
109 w.Fill(sz.Bounds().Inset(inset), blue1, screen.Src)
110 w.Upload(image.Point{20, 0}, b, b.Bounds())
111 w.Fill(image.Rect(50, 50, 350, 120), red, screen.Over)
112
113 // By default, draw the entirety of the texture using the Over
114 // operator. Uncomment one or both of the lines below to see
115 // their different effects.
116 op := screen.Over
117 // op = screen.Src
118 t0Rect := t0.Bounds()
119 // t0Rect = image.Rect(16, 0, 240, 100)
120
121 // Draw the texture t0 twice, as a 1:1 copy and under the
122 // transform src2dst.
123 w.Copy(image.Point{150, 100}, t0, t0Rect, op, nil)
124 src2dst := f64.Aff3{
125 +0.5 * cos30, -1.0 * sin30, 100,
126 +0.5 * sin30, +1.0 * cos30, 200,
127 }
128 w.Draw(src2dst, t0, t0Rect, op, nil)
129 w.DrawUniform(src2dst, yellow, t0Rect.Inset(30), screen.Over, nil)
130
131 // Draw crosses at the transformed corners of t0Rect.
132 for _, sx := range []int{t0Rect.Min.X, t0Rect.Max.X} {
133 for _, sy := range []int{t0Rect.Min.Y, t0Rect.Max.Y} {
134 dx := int(src2dst[0]*float64(sx) + src2dst[1]*float64(sy) + src2dst[2])
135 dy := int(src2dst[3]*float64(sx) + src2dst[4]*float64(sy) + src2dst[5])
136 w.Fill(image.Rect(dx-0, dy-1, dx+1, dy+2), darkGray, screen.Src)
137 w.Fill(image.Rect(dx-1, dy-0, dx+2, dy+1), darkGray, screen.Src)
138 }
139 }
140
141 // Draw t1.
142 w.Copy(image.Point{400, 50}, t1, t1.Bounds(), screen.Src, nil)
143
144 w.Publish()
145
146 case size.Event:
147 sz = e
148
149 case error:
150 log.Print(e)
151 }
152 }
153 })
154 }
155
156 func drawGradient(m *image.RGBA) {
157 b := m.Bounds()
158 for y := b.Min.Y; y < b.Max.Y; y++ {
159 for x := b.Min.X; x < b.Max.X; x++ {
160 if x%64 == 0 || y%64 == 0 {
161 m.SetRGBA(x, y, color.RGBA{0xff, 0xff, 0xff, 0xff})
162 } else if x%64 == 63 || y%64 == 63 {
163 m.SetRGBA(x, y, color.RGBA{0x00, 0x00, 0xff, 0xff})
164 } else {
165 m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
166 }
167 }
168 }
169
170 // Round off the corners.
171 const radius = 64
172 lox := b.Min.X + radius - 1
173 loy := b.Min.Y + radius - 1
174 hix := b.Max.X - radius
175 hiy := b.Max.Y - radius
176 for y := 0; y < radius; y++ {
177 for x := 0; x < radius; x++ {
178 if x*x+y*y <= radius*radius {
179 continue
180 }
181 m.SetRGBA(lox-x, loy-y, color.RGBA{})
182 m.SetRGBA(hix+x, loy-y, color.RGBA{})
183 m.SetRGBA(lox-x, hiy+y, color.RGBA{})
184 m.SetRGBA(hix+x, hiy+y, color.RGBA{})
185 }
186 }
187 }