zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

shiny.go (1363B)


      1 // Copyright 2015 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 //go:build windows
      6 // +build windows
      7 
      8 package app
      9 
     10 import (
     11 	"log"
     12 
     13 	"golang.org/x/exp/shiny/driver/gldriver"
     14 	"golang.org/x/exp/shiny/screen"
     15 	"golang.org/x/mobile/event/lifecycle"
     16 	"golang.org/x/mobile/event/mouse"
     17 	"golang.org/x/mobile/event/touch"
     18 	"golang.org/x/mobile/gl"
     19 )
     20 
     21 func main(f func(a App)) {
     22 	gldriver.Main(func(s screen.Screen) {
     23 		w, err := s.NewWindow(nil)
     24 		if err != nil {
     25 			log.Fatal(err)
     26 		}
     27 		defer w.Release()
     28 
     29 		theApp.glctx = nil
     30 		theApp.worker = nil // handled by shiny
     31 
     32 		go func() {
     33 			for range theApp.publish {
     34 				res := w.Publish()
     35 				theApp.publishResult <- PublishResult{
     36 					BackBufferPreserved: res.BackBufferPreserved,
     37 				}
     38 			}
     39 		}()
     40 
     41 		go f(theApp)
     42 
     43 		for {
     44 			theApp.Send(convertEvent(w.NextEvent()))
     45 		}
     46 	})
     47 }
     48 
     49 func convertEvent(e interface{}) interface{} {
     50 	switch e := e.(type) {
     51 	case lifecycle.Event:
     52 		if theApp.glctx == nil {
     53 			theApp.glctx = e.DrawContext.(gl.Context)
     54 		}
     55 	case mouse.Event:
     56 		te := touch.Event{
     57 			X: e.X,
     58 			Y: e.Y,
     59 		}
     60 		switch e.Direction {
     61 		case mouse.DirNone:
     62 			te.Type = touch.TypeMove
     63 		case mouse.DirPress:
     64 			te.Type = touch.TypeBegin
     65 		case mouse.DirRelease:
     66 			te.Type = touch.TypeEnd
     67 		}
     68 		return te
     69 	}
     70 	return e
     71 }