Implement some preparation code for go2cpp

Updates #744
This commit is contained in:
Hajime Hoshi 2020-11-22 18:22:52 +09:00
parent 912e13071f
commit 5897e1ab7c
5 changed files with 57 additions and 33 deletions

View File

@ -19,7 +19,11 @@ import (
) )
func impl(x, y int) float64 { func impl(x, y int) float64 {
ratio := js.Global().Get("window").Get("devicePixelRatio").Float() window := js.Global().Get("window")
if !window.Truthy() {
return 1
}
ratio := window.Get("devicePixelRatio").Float()
if ratio == 0 { if ratio == 0 {
ratio = 1 ratio = 1
} }

View File

@ -85,6 +85,7 @@ const (
var ( var (
isWebGL2Available = !forceWebGL1 && js.Global().Get("WebGL2RenderingContext").Truthy() isWebGL2Available = !forceWebGL1 && js.Global().Get("WebGL2RenderingContext").Truthy()
needsRestoring_ = !web.IsMobileBrowser() && !js.Global().Get("go2cpp").Truthy()
) )
type contextImpl struct { type contextImpl struct {
@ -95,27 +96,28 @@ type contextImpl struct {
func (c *context) initGL() { func (c *context) initGL() {
c.gl = js.Value{} c.gl = js.Value{}
// TODO: Define id?
doc := js.Global().Get("document")
if !doc.Truthy() {
return
}
canvas := doc.Call("querySelector", "canvas")
attr := js.Global().Get("Object").New()
attr.Set("alpha", true)
attr.Set("premultipliedAlpha", true)
var gl js.Value var gl js.Value
if isWebGL2Available {
gl = canvas.Call("getContext", "webgl2", attr) // TODO: Define id?
} else { if doc := js.Global().Get("document"); doc.Truthy() {
gl = canvas.Call("getContext", "webgl", attr) canvas := doc.Call("querySelector", "canvas")
if jsutil.Equal(gl, js.Null()) { attr := js.Global().Get("Object").New()
gl = canvas.Call("getContext", "experimental-webgl", attr) attr.Set("alpha", true)
attr.Set("premultipliedAlpha", true)
if isWebGL2Available {
gl = canvas.Call("getContext", "webgl2", attr)
} else {
gl = canvas.Call("getContext", "webgl", attr)
if jsutil.Equal(gl, js.Null()) { if jsutil.Equal(gl, js.Null()) {
panic("opengl: getContext failed") gl = canvas.Call("getContext", "experimental-webgl", attr)
if jsutil.Equal(gl, js.Null()) {
panic("opengl: getContext failed")
}
} }
} }
} else if go2cpp := js.Global().Get("go2cpp"); go2cpp.Truthy() {
gl = go2cpp.Get("gl")
} }
c.gl = gl c.gl = gl
@ -493,7 +495,7 @@ func (c *context) flush() {
} }
func (c *context) needsRestoring() bool { func (c *context) needsRestoring() bool {
return !web.IsMobileBrowser() return needsRestoring_
} }
func (c *context) canUsePBO() bool { func (c *context) canUsePBO() bool {

View File

@ -244,6 +244,9 @@ func (i *Input) setMouseCursor(x, y int) {
func (i *Input) UpdateGamepads() { func (i *Input) UpdateGamepads() {
nav := js.Global().Get("navigator") nav := js.Global().Get("navigator")
if !nav.Truthy() {
return
}
if !nav.Get("getGamepads").Truthy() { if !nav.Get("getGamepads").Truthy() {
return return
} }

View File

@ -60,8 +60,8 @@ var (
window = js.Global().Get("window") window = js.Global().Get("window")
document = js.Global().Get("document") document = js.Global().Get("document")
canvas js.Value canvas js.Value
requestAnimationFrame = window.Get("requestAnimationFrame") requestAnimationFrame = js.Global().Get("requestAnimationFrame")
setTimeout = window.Get("setTimeout") setTimeout = js.Global().Get("setTimeout")
) )
func (u *UserInterface) ScreenSizeInFullscreen() (int, int) { func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
@ -134,10 +134,14 @@ func (u *UserInterface) updateSize() {
if u.sizeChanged { if u.sizeChanged {
u.sizeChanged = false u.sizeChanged = false
body := document.Get("body") if document.Truthy() {
bw := body.Get("clientWidth").Float() body := document.Get("body")
bh := body.Get("clientHeight").Float() bw := body.Get("clientWidth").Float()
u.context.Layout(bw, bh) bh := body.Get("clientHeight").Float()
u.context.Layout(bw, bh)
} else {
u.context.Layout(640, 480)
}
} }
} }
@ -250,7 +254,12 @@ func (u *UserInterface) loop(context driver.UIContext) <-chan error {
} }
func init() { func init() {
if jsutil.Equal(document.Get("body"), js.Null()) { // docuemnt is undefined on node.js
if !document.Truthy() {
return
}
if !document.Get("body").Truthy() {
ch := make(chan struct{}) ch := make(chan struct{})
window.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} { window.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
close(ch) close(ch)
@ -400,7 +409,7 @@ func init() {
} }
func (u *UserInterface) Run(context driver.UIContext) error { func (u *UserInterface) Run(context driver.UIContext) error {
if u.initFocused { if u.initFocused && window.Truthy() {
// Do not focus the canvas when the current document is in an iframe. // Do not focus the canvas when the current document is in an iframe.
// Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373). // Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373).
isInIframe := !jsutil.Equal(window.Get("location"), window.Get("parent").Get("location")) isInIframe := !jsutil.Equal(window.Get("location"), window.Get("parent").Get("location"))
@ -417,11 +426,13 @@ func (u *UserInterface) RunWithoutMainLoop(context driver.UIContext) {
} }
func (u *UserInterface) updateScreenSize() { func (u *UserInterface) updateScreenSize() {
body := document.Get("body") if document.Truthy() {
bw := int(body.Get("clientWidth").Float() * u.DeviceScaleFactor()) body := document.Get("body")
bh := int(body.Get("clientHeight").Float() * u.DeviceScaleFactor()) bw := int(body.Get("clientWidth").Float() * u.DeviceScaleFactor())
canvas.Set("width", bw) bh := int(body.Get("clientHeight").Float() * u.DeviceScaleFactor())
canvas.Set("height", bh) canvas.Set("width", bw)
canvas.Set("height", bh)
}
u.sizeChanged = true u.sizeChanged = true
} }

View File

@ -29,7 +29,11 @@ var (
) )
func init() { func init() {
userAgent := js.Global().Get("navigator").Get("userAgent").String() nav := js.Global().Get("navigator")
if !nav.Truthy() {
return
}
userAgent := nav.Get("userAgent").String()
isIOSSafari = strings.Contains(userAgent, "iPhone") || strings.Contains(userAgent, "iPad") isIOSSafari = strings.Contains(userAgent, "iPhone") || strings.Contains(userAgent, "iPad")
isAndroidChrome = strings.Contains(userAgent, "Android") && strings.Contains(userAgent, "Chrome") isAndroidChrome = strings.Contains(userAgent, "Android") && strings.Contains(userAgent, "Chrome")
} }