github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/exp/shiny/example/basic/main.go (about)

     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  // +build ignore
     6  //
     7  // This build tag means that "go install golang.org/x/exp/shiny/..." doesn't
     8  // install this example program. Use "go run main.go" to run it.
     9  
    10  // Basic is a basic example of a graphical application.
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"image"
    16  	"image/color"
    17  	"image/draw"
    18  	"log"
    19  	"math"
    20  
    21  	"golang.org/x/exp/shiny/driver"
    22  	"golang.org/x/exp/shiny/screen"
    23  	"golang.org/x/image/math/f64"
    24  	"golang.org/x/mobile/event/key"
    25  	"golang.org/x/mobile/event/paint"
    26  	"golang.org/x/mobile/event/size"
    27  )
    28  
    29  var (
    30  	blue0 = color.RGBA{0x00, 0x00, 0x1f, 0xff}
    31  	blue1 = color.RGBA{0x00, 0x00, 0x3f, 0xff}
    32  )
    33  
    34  func main() {
    35  	driver.Main(func(s screen.Screen) {
    36  		w, err := s.NewWindow(nil)
    37  		if err != nil {
    38  			log.Fatal(err)
    39  		}
    40  		defer w.Release()
    41  
    42  		winSize := image.Point{256, 256}
    43  		b, err := s.NewBuffer(winSize)
    44  		if err != nil {
    45  			log.Fatal(err)
    46  		}
    47  		defer b.Release()
    48  		drawGradient(b.RGBA())
    49  
    50  		t, err := s.NewTexture(winSize)
    51  		if err != nil {
    52  			log.Fatal(err)
    53  		}
    54  		defer t.Release()
    55  		t.Upload(image.Point{}, b, b.Bounds())
    56  
    57  		var sz size.Event
    58  		for e := range w.Events() {
    59  			switch e := e.(type) {
    60  			default:
    61  				// TODO: be more interesting.
    62  				fmt.Printf("got %#v\n", e)
    63  
    64  			case key.Event:
    65  				fmt.Printf("got %v\n", e)
    66  				if e.Code == key.CodeEscape {
    67  					return
    68  				}
    69  
    70  			case paint.Event:
    71  				w.Fill(sz.Bounds(), blue0, draw.Src)
    72  				w.Fill(sz.Bounds().Inset(10), blue1, draw.Src)
    73  				w.Upload(image.Point{}, b, b.Bounds())
    74  				c := math.Cos(math.Pi / 6)
    75  				s := math.Sin(math.Pi / 6)
    76  				w.Draw(f64.Aff3{
    77  					+c, -s, 100,
    78  					+s, +c, 200,
    79  				}, t, t.Bounds(), screen.Over, nil)
    80  				w.Publish()
    81  
    82  			case size.Event:
    83  				sz = e
    84  
    85  			case error:
    86  				log.Print(e)
    87  			}
    88  		}
    89  	})
    90  }
    91  
    92  func drawGradient(m *image.RGBA) {
    93  	b := m.Bounds()
    94  	for y := b.Min.Y; y < b.Max.Y; y++ {
    95  		for x := b.Min.X; x < b.Max.X; x++ {
    96  			if x%64 == 0 || y%64 == 0 {
    97  				m.SetRGBA(x, y, color.RGBA{0xff, 0xff, 0xff, 0xff})
    98  			} else if x%64 == 63 || y%64 == 63 {
    99  				m.SetRGBA(x, y, color.RGBA{0x00, 0x00, 0xff, 0xff})
   100  			} else {
   101  				m.SetRGBA(x, y, color.RGBA{uint8(x), uint8(y), 0x00, 0xff})
   102  			}
   103  		}
   104  	}
   105  }