2019-09-17 10:42:01 -04:00
|
|
|
// +build darwin linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-16 22:26:54 -04:00
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2019-10-07 10:41:51 -04:00
|
|
|
"log"
|
2020-06-16 22:26:54 -04:00
|
|
|
"time"
|
2019-09-17 10:42:01 -04:00
|
|
|
|
2019-10-03 11:32:15 -04:00
|
|
|
"gioui.org/app"
|
2020-06-16 22:26:54 -04:00
|
|
|
"gioui.org/f32"
|
2019-10-18 14:40:35 -04:00
|
|
|
"gioui.org/io/system"
|
2019-10-03 11:32:15 -04:00
|
|
|
"gioui.org/layout"
|
2020-06-11 10:42:43 -04:00
|
|
|
"gioui.org/op"
|
2020-06-16 22:26:54 -04:00
|
|
|
"gioui.org/op/clip"
|
|
|
|
"gioui.org/op/paint"
|
2019-10-03 11:32:15 -04:00
|
|
|
"gioui.org/unit"
|
2019-10-18 14:40:35 -04:00
|
|
|
"gioui.org/widget/material"
|
2019-09-17 10:42:01 -04:00
|
|
|
|
2019-10-31 16:16:56 -04:00
|
|
|
"gioui.org/font/gofont"
|
2019-09-17 10:42:01 -04:00
|
|
|
)
|
|
|
|
|
2020-06-16 22:26:54 -04:00
|
|
|
type vector struct {
|
|
|
|
x, y, z float64
|
|
|
|
}
|
|
|
|
|
2019-09-17 10:42:01 -04:00
|
|
|
var (
|
|
|
|
labchan chan string
|
2020-06-16 22:26:54 -04:00
|
|
|
senschan chan vector
|
2019-09-17 10:42:01 -04:00
|
|
|
)
|
|
|
|
|
2020-06-11 10:42:43 -04:00
|
|
|
type (
|
|
|
|
D = layout.Dimensions
|
|
|
|
C = layout.Context
|
|
|
|
)
|
|
|
|
|
2019-09-17 10:42:01 -04:00
|
|
|
func main() {
|
|
|
|
labchan = make(chan string)
|
2020-06-16 22:26:54 -04:00
|
|
|
senschan = make(chan vector)
|
2019-09-17 10:42:01 -04:00
|
|
|
log.Print("Staring event loop")
|
|
|
|
go eventloop()
|
|
|
|
app.Main()
|
|
|
|
log.Print("App closed")
|
|
|
|
}
|
|
|
|
|
2019-10-18 14:40:35 -04:00
|
|
|
func diffInsets(x, y system.Insets) bool {
|
2019-10-07 10:41:51 -04:00
|
|
|
return x.Top != y.Top ||
|
2019-09-17 10:42:01 -04:00
|
|
|
x.Bottom != y.Bottom ||
|
|
|
|
x.Left != y.Left ||
|
|
|
|
x.Right != y.Right
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventloop() {
|
|
|
|
w := app.NewWindow(
|
2019-10-18 14:40:35 -04:00
|
|
|
app.Size(unit.Dp(400), unit.Dp(400)),
|
|
|
|
app.Title("Hello"))
|
2020-06-16 22:26:54 -04:00
|
|
|
|
2020-06-11 10:42:43 -04:00
|
|
|
th := material.NewTheme(gofont.Collection())
|
|
|
|
var ops op.Ops
|
2019-09-17 10:42:01 -04:00
|
|
|
|
|
|
|
sysinset := &layout.Inset{}
|
2019-10-03 11:32:15 -04:00
|
|
|
margin := layout.UniformInset(unit.Dp(10))
|
2020-06-11 10:42:43 -04:00
|
|
|
labels := []material.LabelStyle{}
|
2019-09-17 10:42:01 -04:00
|
|
|
list := &layout.List{Axis: layout.Vertical}
|
|
|
|
|
2019-10-18 14:40:35 -04:00
|
|
|
resetSysinset := func(x system.Insets) {
|
2019-09-17 10:42:01 -04:00
|
|
|
sysinset.Top = x.Top
|
|
|
|
sysinset.Bottom = x.Bottom
|
|
|
|
sysinset.Left = x.Left
|
|
|
|
sysinset.Right = x.Right
|
|
|
|
}
|
|
|
|
|
2019-10-18 14:40:35 -04:00
|
|
|
go func() {
|
|
|
|
labchan <- "Starting"
|
|
|
|
}()
|
2020-06-16 22:26:54 -04:00
|
|
|
|
|
|
|
var accel vector
|
|
|
|
accel.y = 1
|
|
|
|
|
|
|
|
lablist := func(gtx C) D {
|
|
|
|
return list.Layout(gtx, len(labels), func(gtx C, i int) D {
|
|
|
|
return labels[i].Layout(gtx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
vecview := func(gtx C) D {
|
|
|
|
f := layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceEvenly}
|
|
|
|
return f.Layout(gtx,
|
|
|
|
layout.Rigid(func(gtx C) D {
|
|
|
|
return material.Body1(th, fmt.Sprintf("x = %f", accel.x)).Layout(gtx)
|
|
|
|
}),
|
|
|
|
layout.Rigid(func(gtx C) D {
|
|
|
|
return material.Body1(th, fmt.Sprintf("y = %f", accel.y)).Layout(gtx)
|
|
|
|
}),
|
|
|
|
layout.Rigid(func(gtx C) D {
|
|
|
|
return material.Body1(th, fmt.Sprintf("z = %f", accel.z)).Layout(gtx)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cx, cy, cvx, cvy, cs float32 // circle position
|
|
|
|
cx = 200
|
|
|
|
cy = 200
|
|
|
|
cs = 50 // circle size (radius)
|
|
|
|
|
|
|
|
circle := func(gtx C, width, height float32) D {
|
|
|
|
// clip circle position and bounce off of the edges
|
|
|
|
if (cx < cs) { cx = cs; cvx = (-0.5) * cvx }
|
|
|
|
if (cy < cs) { cy = cs; cvy = (-0.5) * cvy }
|
|
|
|
if (cx > width - cs) { cx = width - cs; cvx = (-0.5) * cvx }
|
|
|
|
if (cy > height - cs) { cy = height - cs; cvy = (-0.5) * cvy }
|
|
|
|
|
|
|
|
blue := color.RGBA{0x3f, 0x51, 0xb5, 0x80}
|
|
|
|
r1 := f32.Rectangle{f32.Point{cx - cs, cy - cs}, f32.Point{cx + cs, cy + cs}}
|
|
|
|
clip.Rect{ Rect: r1, NE: cs, NW: cs, SE: cs, SW: cs}.Op(gtx.Ops).Add(gtx.Ops)
|
|
|
|
paint.ColorOp{Color: blue}.Add(gtx.Ops)
|
|
|
|
paint.PaintOp{Rect: r1}.Add(gtx.Ops)
|
|
|
|
|
|
|
|
var ret D
|
|
|
|
ret.Size.X = int(cs * 2)
|
|
|
|
ret.Size.Y = int(cs * 2)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second / 60)
|
|
|
|
|
|
|
|
var t, told int64
|
|
|
|
t = time.Now().UnixNano()
|
|
|
|
|
2019-09-17 10:42:01 -04:00
|
|
|
for {
|
|
|
|
select {
|
2020-06-16 22:26:54 -04:00
|
|
|
case <- ticker.C:
|
|
|
|
told = t
|
|
|
|
t = time.Now().UnixNano()
|
|
|
|
elapsed := float32((t - told) / 1000000)
|
|
|
|
|
|
|
|
cvx = cvx - float32(accel.x/1000) * elapsed
|
|
|
|
cvy = cvy + float32(accel.y/1000) * elapsed
|
|
|
|
|
|
|
|
cx = cx + cvx * elapsed
|
|
|
|
cy = cy + cvy * elapsed
|
|
|
|
w.Invalidate()
|
|
|
|
|
2019-09-17 10:42:01 -04:00
|
|
|
case x := <-labchan:
|
2020-06-11 10:42:43 -04:00
|
|
|
labels = append(labels, material.Body1(th, x))
|
2020-06-16 22:26:54 -04:00
|
|
|
|
|
|
|
case x := <-senschan:
|
|
|
|
accel = x
|
|
|
|
|
2019-09-17 10:42:01 -04:00
|
|
|
case e := <-w.Events():
|
|
|
|
switch e := e.(type) {
|
2019-10-18 14:40:35 -04:00
|
|
|
case system.DestroyEvent:
|
2019-09-17 10:42:01 -04:00
|
|
|
return
|
2019-10-18 14:40:35 -04:00
|
|
|
case system.FrameEvent:
|
2020-06-11 10:42:43 -04:00
|
|
|
gtx := layout.NewContext(&ops, e)
|
2019-09-17 10:42:01 -04:00
|
|
|
resetSysinset(e.Insets)
|
2020-06-11 10:42:43 -04:00
|
|
|
sysinset.Layout(gtx, func(gtx C) D {
|
2020-06-16 22:26:54 -04:00
|
|
|
ret := margin.Layout(gtx, func(gtx C) D {
|
|
|
|
f1 := layout.Flex{Axis: layout.Vertical}
|
|
|
|
ret := f1.Layout(gtx,
|
|
|
|
layout.Rigid(vecview),
|
|
|
|
layout.Flexed(1, lablist),
|
|
|
|
)
|
|
|
|
circle(gtx, float32(ret.Size.X), float32(ret.Size.Y))
|
|
|
|
return ret
|
2019-10-03 11:32:15 -04:00
|
|
|
})
|
2020-06-16 22:26:54 -04:00
|
|
|
return ret
|
2019-10-03 11:32:15 -04:00
|
|
|
})
|
2019-10-18 14:40:35 -04:00
|
|
|
e.Frame(gtx.Ops)
|
2019-09-17 10:42:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|