example/kitchen: include example of affine transforms

Include an example of transforming the entire UI in the example.

Signed-off-by: Viktor <viktor.ogeman@gmail.com>
This commit is contained in:
Viktor
2020-06-20 23:29:53 +02:00
committed by Elias Naur
parent cee045bf92
commit 1a9ae1af69
5 changed files with 42 additions and 8 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ module gioui.org/cmd
go 1.13
require (
gioui.org v0.0.0-20200619103721-0b713032fbff
gioui.org v0.0.0-20200621092036-cee045bf92fb
github.com/chromedp/cdproto v0.0.0-20191114225735-6626966fbae4
github.com/chromedp/chromedp v0.5.2
golang.org/x/image v0.0.0-20200618115811-c13761719519
+2 -2
View File
@@ -1,6 +1,6 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20200619103721-0b713032fbff h1:R2UG6O+TdOkC+S7jeIvcTmrKIAazXn90siDdAWnF4bg=
gioui.org v0.0.0-20200619103721-0b713032fbff/go.mod h1:jiUwifN9cRl/zmco43aAqh0aV+s9GbhG13KcD+gEpkU=
gioui.org v0.0.0-20200621092036-cee045bf92fb h1:7i3eFfl0r6oeZPSQClZysU9gZwwgDScXkbrv1/CBtic=
gioui.org v0.0.0-20200621092036-cee045bf92fb/go.mod h1:jiUwifN9cRl/zmco43aAqh0aV+s9GbhG13KcD+gEpkU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/chromedp/cdproto v0.0.0-20191114225735-6626966fbae4 h1:QD3KxSJ59L2lxG6MXBjNHxiQO2RmxTQ3XcK+wO44WOg=
+1 -1
View File
@@ -3,7 +3,7 @@ module gioui.org/example
go 1.13
require (
gioui.org v0.0.0-20200619103721-0b713032fbff
gioui.org v0.0.0-20200621092036-cee045bf92fb
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72
github.com/google/go-github/v24 v24.0.1
+2 -2
View File
@@ -1,7 +1,7 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20200619103721-0b713032fbff h1:R2UG6O+TdOkC+S7jeIvcTmrKIAazXn90siDdAWnF4bg=
gioui.org v0.0.0-20200619103721-0b713032fbff/go.mod h1:jiUwifN9cRl/zmco43aAqh0aV+s9GbhG13KcD+gEpkU=
gioui.org v0.0.0-20200621092036-cee045bf92fb h1:7i3eFfl0r6oeZPSQClZysU9gZwwgDScXkbrv1/CBtic=
gioui.org v0.0.0-20200621092036-cee045bf92fb/go.mod h1:jiUwifN9cRl/zmco43aAqh0aV+s9GbhG13KcD+gEpkU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
+36 -2
View File
@@ -18,6 +18,7 @@ import (
"gioui.org/app"
"gioui.org/app/headless"
"gioui.org/f32"
"gioui.org/font/gofont"
"gioui.org/io/router"
"gioui.org/io/system"
@@ -127,7 +128,15 @@ func loop(w *app.Window) error {
if *disable {
gtx = gtx.Disabled()
}
kitchen(gtx, th)
if checkbox.Changed() {
if checkbox.Value {
transformTime = e.Now
} else {
transformTime = time.Time{}
}
}
transformedKitchen(gtx, th)
e.Frame(gtx.Ops)
}
case p := <-progressIncrementer:
@@ -140,6 +149,30 @@ func loop(w *app.Window) error {
}
}
func transformedKitchen(gtx layout.Context, th *material.Theme) layout.Dimensions {
if !transformTime.IsZero() {
dt := float32(gtx.Now.Sub(transformTime).Seconds())
angle := dt * .1
op.InvalidateOp{}.Add(gtx.Ops)
defer op.Push(gtx.Ops).Pop()
tr := f32.Affine2D{}
tr = tr.Rotate(f32.Pt(300, 20), -angle)
scale := 1.0 - dt*.5
if scale < 0.5 {
scale = 0.5
}
tr = tr.Scale(f32.Pt(300, 20), f32.Pt(scale, scale))
offset := dt * 50
if offset > 200 {
offset = 200
}
tr = tr.Offset(f32.Pt(0, offset))
op.Affine(tr).Add(gtx.Ops)
}
return kitchen(gtx, th)
}
var (
editor = new(widget.Editor)
lineEditor = &widget.Editor{
@@ -163,6 +196,7 @@ var (
icon *widget.Icon
checkbox = new(widget.Bool)
swtch = new(widget.Bool)
transformTime time.Time
)
type (
@@ -268,7 +302,7 @@ func kitchen(gtx layout.Context, th *material.Theme) layout.Dimensions {
func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(
material.CheckBox(th, checkbox, "Checkbox").Layout,
material.CheckBox(th, checkbox, "Transform").Layout,
),
layout.Rigid(func(gtx C) D {
return layout.Inset{Left: unit.Dp(16)}.Layout(gtx,