~wrnr/gio unlisted

57901d59fb6a1a394e9ab30b0790d4e17920532b — Werner Laurensse 6 months ago e540f23 master
example/gesture/gesture.go: Example of pinch gesture
Signed-off-by: Werner Laurensse
1 files changed, 90 insertions(+), 0 deletions(-)

A example/gesture/gesture.go
A example/gesture/gesture.go => example/gesture/gesture.go +90 -0
@@ 0,0 1,90 @@
package main

import (
	"gioui.org/f32"
	"gioui.org/io/pointer"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"image"
	"image/color"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
)

var (
	maroon = color.RGBA{127, 0, 0, 255}
)

func main() {
	go func() {
		w := app.NewWindow()
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func loop(w *app.Window) error {
	var ops op.Ops
	magnification := float32(1)
	rotation := float32(0)
	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			cons := gtx.Constraints
			width, height := cons.Max.X/2, cons.Max.Y/2
			//center := f32.Point{X: float32(width)/2, Y: float32(height)/2}

			tr := f32.Affine2D{}.
				Scale(f32.Point{}, f32.Pt(magnification, magnification)).
				Rotate(f32.Point{}, rotation)
			op.Affine(tr).Add(gtx.Ops)

			drawRect(width, height, gtx.Ops)

			pointer.Rect(image.Rectangle{Max: cons.Max}).Add(gtx.Ops)
			for _, e := range gtx.Events(w) {
				e, ok := e.(pointer.Event)
				if !ok {
					continue
				}
				switch e.Type {
				case pointer.Pinch:
					magnification += e.Magnification
				case pointer.Rotate:
					rotation += e.Rotation
				}
			}
			pointer.InputOp{Tag: w, Grab: false, Types: pointer.Pinch | pointer.Rotate}.Add(gtx.Ops)
			e.Frame(gtx.Ops)
		}
	}
}

func drawRect(w, h int, ops *op.Ops) {
	defer op.Push(ops).Pop()
	p1 := f32.Pt(0, 0)
	p2 := f32.Pt(float32(w), float32(h))
	var path clip.Path
	path.Begin(ops)
	path.Move(p1)
	path.Line(f32.Point{X: p2.X, Y: 0})
	path.Line(f32.Point{X: 0, Y: p2.Y})
	path.Line(f32.Point{X: -p2.X, Y: 0})
	path.Line(f32.Point{X: 0, Y: -p2.Y})
	path.End().Add(ops)
	box := f32.Rectangle{Min: p1, Max: p2}
	paint.ColorOp{Color: maroon}.Add(ops)
	paint.PaintOp{Rect: box}.Add(ops)
}