rand/cmd/rand-gui/android.go

202 lines
4.4 KiB
Go
Raw Normal View History

2019-01-09 18:21:20 -05:00
//+build !darwin
package main
import (
"time"
"github.com/xlab/android-go/android"
"github.com/xlab/android-go/app"
gl "github.com/xlab/android-go/gles31"
//"github.com/golang-ui/nuklear/nk"
"gitlab.wow.st/gmp/nuklear/nk"
)
const (
datadir = "/storage/emulated/0/Android/data/st.wow.gitlab.gmp.commute/"
fontSize = 64
)
func platformInit() {
gl.Enable(gl.CULL_FACE)
gl.Disable(gl.DEPTH_TEST)
app.SetLogTag("Commute")
}
func mkWin() {
}
func Terminate() {
return
}
func PollEvents() {
return
}
func GetSize() (width, height int) {
handle := nk.NkPlatformDisplayHandle()
return handle.Width, handle.Height
}
func SwapBuffers() {
}
var (
nativeWindowEvents chan app.NativeWindowEvent
inputQueueEvents chan app.InputQueueEvent
inputQueueChan chan *android.InputQueue
backPressed bool
)
func eventloop() {
nativeWindowEvents = make(chan app.NativeWindowEvent, 1)
inputQueueEvents = make(chan app.InputQueueEvent, 1)
inputQueueChan = make(chan *android.InputQueue, 1)
app.Main(appmain)
log(Info,"app.Main returned")
}
func appmain(a app.NativeActivity) {
log(Info,"appmain() start")
fpsTicker := time.NewTicker(frametime)
a.HandleNativeWindowEvents(nativeWindowEvents)
a.HandleInputQueueEvents(inputQueueEvents)
log(Info,"appmain() launching init thread")
inputs := make(chan struct{},64)
go app.HandleInputQueues(inputQueueChan, func() {
a.InputQueueHandled()
}, func(ev *android.InputEvent) {
switch android.InputEventGetType(ev) {
case android.InputEventTypeKey:
key := android.KeyEventGetKeyCode(ev)
action := android.KeyEventGetAction(ev)
meta := android.KeyEventGetMetaState(ev)
switch action {
case android.KeyEventActionDown:
if key == android.KeycodeBack {
backPressed = true
inputs<- struct{}{}
return
}
fallthrough
case android.KeyEventActionUp:
nk.NkPlatformInput(nil, &nk.PlatformKeyEvent{
Activity: a.NativeActivity(),
Action: action,
KeyCode: key,
MetaState: meta,
})
inputs<- struct{}{}
}
case android.InputEventTypeMotion:
action := android.MotionEventGetAction(ev)
switch action {
case android.MotionEventActionDown,
android.MotionEventActionMove,
android.MotionEventActionUp:
x := android.MotionEventGetX(ev, 0)
y := android.MotionEventGetY(ev, 0)
nk.NkPlatformInput(&nk.PlatformTouchEvent{
Action: action,
X: int32(x),
Y: int32(y),
}, nil)
inputs<- struct{}{}
}
}
})
a.InitDone()
log(Info,"appmain() starting event loop")
for {
select {
case event := <-a.LifecycleEvents():
log(Info,"appmain(): lifecycle ", event)
case event := <-inputQueueEvents:
log(Debug,"appmain(): inputQueueEvent ", event)
switch event.Kind {
case app.QueueCreated:
inputQueueChan <- event.Queue
case app.QueueDestroyed:
inputQueueChan <- nil
}
if ctx != nil {
gfxMain()
}
case <-fpsTicker.C:
log(DebugGfx,"appmain(): fpsTicker")
if ctx != nil {
log(DebugGfx,"gfxMain()")
fpsTicker.Stop()
gfxMain()
fpsTicker = newTicker()
}
case <-inputs:
log(Debug,"appmain(): inputs queue")
if ctx != nil {
fpsTicker.Stop()
gfxMain()
fpsTicker = newFastTicker()
}
case event := <-nativeWindowEvents:
log(Info,"appmain(): nativeWindowEvents", event)
switch event.Kind {
case app.NativeWindowRedrawNeeded:
log(Info,"appmain(): redraw")
fpsTicker.Stop()
gfxMain()
a.NativeWindowRedrawDone()
fpsTicker = newTicker()
case app.NativeWindowCreated:
log(Info,"appmain(): window created")
ctx = nk.NkPlatformInit(event.Window, nk.PlatformInstallCallbacks)
log(Info,"appmain(): init returned")
if ctx == nil {
log(Fatal,"Nuklear failed to init")
}
loadFont()
fpsTicker = newTicker()
log(Info,"appmain(): done with create window")
case app.NativeWindowDestroyed:
log(Info,"appmain(): window destroyed")
//refresh = true
fpsTicker.Stop()
nk.NkPlatformShutdown()
Drain:
for {
select {
case <-inputs:
default:
break Drain
}
}
}
}
backPressed = false
}
log(Info,"appmain: returning")
}
func newFastTicker() *time.Ticker {
return time.NewTicker(time.Second / 60)
}
func newTicker() *time.Ticker {
if y != lastY {
return time.NewTicker(time.Second / 60)
} else {
return time.NewTicker(time.Second)
}
}
func BackButton(string) bool {
return backPressed
}
func Clip(x string) {
}