67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"runtime"
|
||
|
|
||
|
"golang.org/x/mobile/app"
|
||
|
"golang.org/x/mobile/event/lifecycle"
|
||
|
"golang.org/x/mobile/event/paint"
|
||
|
"golang.org/x/mobile/event/size"
|
||
|
"golang.org/x/mobile/event/touch"
|
||
|
"golang.org/x/mobile/gl"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
app.Main(func(a app.App) {
|
||
|
var glctx gl.Context
|
||
|
for e := range a.Events() {
|
||
|
switch e := a.Filter(e).(type) {
|
||
|
case lifecycle.Event:
|
||
|
switch e.Crosses(lifecycle.StageVisible) {
|
||
|
case lifecycle.CrossOn:
|
||
|
glctx, _ = e.DrawContext.(gl.Context)
|
||
|
onStart()
|
||
|
case lifecycle.CrossOff:
|
||
|
onStop()
|
||
|
glctx = nil
|
||
|
}
|
||
|
case size.Event:
|
||
|
onResize(e)
|
||
|
case paint.Event:
|
||
|
if glctx == nil {
|
||
|
continue
|
||
|
}
|
||
|
onDraw(glctx)
|
||
|
a.Publish()
|
||
|
case touch.Event:
|
||
|
onTouch(e)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func onStart() {
|
||
|
fmt.Println("App started")
|
||
|
}
|
||
|
|
||
|
func onStop() {
|
||
|
fmt.Println("App stopped")
|
||
|
}
|
||
|
|
||
|
func onResize(e size.Event) {
|
||
|
runtime.LockOSThread()
|
||
|
gl.Viewport(0, 0, e.WidthPx, e.HeightPx)
|
||
|
runtime.UnlockOSThread()
|
||
|
}
|
||
|
|
||
|
func onDraw(glctx gl.Context) {
|
||
|
glctx.ClearColor(0.1, 0.1, 0.1, 1)
|
||
|
glctx.Clear(gl.COLOR_BUFFER_BIT)
|
||
|
}
|
||
|
|
||
|
func onTouch(e touch.Event) {
|
||
|
fmt.Printf("Touch event: X: %f, Y: %f\n", e.X, e.Y)
|
||
|
}
|
||
|
|