From 5897e1ab7cc721edd99ce4a40cfbeee78f2095d9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 22 Nov 2020 18:22:52 +0900 Subject: [PATCH] Implement some preparation code for go2cpp Updates #744 --- internal/devicescale/impl_js.go | 6 +++- internal/graphicsdriver/opengl/context_js.go | 38 ++++++++++---------- internal/uidriver/js/input_js.go | 3 ++ internal/uidriver/js/ui_js.go | 37 ++++++++++++------- internal/web/web_js.go | 6 +++- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/internal/devicescale/impl_js.go b/internal/devicescale/impl_js.go index 8acca5ed7..3f7c3db0f 100644 --- a/internal/devicescale/impl_js.go +++ b/internal/devicescale/impl_js.go @@ -19,7 +19,11 @@ import ( ) 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 { ratio = 1 } diff --git a/internal/graphicsdriver/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go index 3b42ee01d..b6b9fc813 100644 --- a/internal/graphicsdriver/opengl/context_js.go +++ b/internal/graphicsdriver/opengl/context_js.go @@ -85,6 +85,7 @@ const ( var ( isWebGL2Available = !forceWebGL1 && js.Global().Get("WebGL2RenderingContext").Truthy() + needsRestoring_ = !web.IsMobileBrowser() && !js.Global().Get("go2cpp").Truthy() ) type contextImpl struct { @@ -95,27 +96,28 @@ type contextImpl struct { func (c *context) initGL() { 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 - if isWebGL2Available { - gl = canvas.Call("getContext", "webgl2", attr) - } else { - gl = canvas.Call("getContext", "webgl", attr) - if jsutil.Equal(gl, js.Null()) { - gl = canvas.Call("getContext", "experimental-webgl", attr) + + // TODO: Define id? + if doc := js.Global().Get("document"); doc.Truthy() { + canvas := doc.Call("querySelector", "canvas") + attr := js.Global().Get("Object").New() + 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()) { - 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 @@ -493,7 +495,7 @@ func (c *context) flush() { } func (c *context) needsRestoring() bool { - return !web.IsMobileBrowser() + return needsRestoring_ } func (c *context) canUsePBO() bool { diff --git a/internal/uidriver/js/input_js.go b/internal/uidriver/js/input_js.go index 2d797f7e7..ae8bea681 100644 --- a/internal/uidriver/js/input_js.go +++ b/internal/uidriver/js/input_js.go @@ -244,6 +244,9 @@ func (i *Input) setMouseCursor(x, y int) { func (i *Input) UpdateGamepads() { nav := js.Global().Get("navigator") + if !nav.Truthy() { + return + } if !nav.Get("getGamepads").Truthy() { return } diff --git a/internal/uidriver/js/ui_js.go b/internal/uidriver/js/ui_js.go index beea7a868..ac09c25af 100644 --- a/internal/uidriver/js/ui_js.go +++ b/internal/uidriver/js/ui_js.go @@ -60,8 +60,8 @@ var ( window = js.Global().Get("window") document = js.Global().Get("document") canvas js.Value - requestAnimationFrame = window.Get("requestAnimationFrame") - setTimeout = window.Get("setTimeout") + requestAnimationFrame = js.Global().Get("requestAnimationFrame") + setTimeout = js.Global().Get("setTimeout") ) func (u *UserInterface) ScreenSizeInFullscreen() (int, int) { @@ -134,10 +134,14 @@ func (u *UserInterface) updateSize() { if u.sizeChanged { u.sizeChanged = false - body := document.Get("body") - bw := body.Get("clientWidth").Float() - bh := body.Get("clientHeight").Float() - u.context.Layout(bw, bh) + if document.Truthy() { + body := document.Get("body") + bw := body.Get("clientWidth").Float() + 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() { - 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{}) window.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} { close(ch) @@ -400,7 +409,7 @@ func init() { } 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. // 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")) @@ -417,11 +426,13 @@ func (u *UserInterface) RunWithoutMainLoop(context driver.UIContext) { } func (u *UserInterface) updateScreenSize() { - body := document.Get("body") - bw := int(body.Get("clientWidth").Float() * u.DeviceScaleFactor()) - bh := int(body.Get("clientHeight").Float() * u.DeviceScaleFactor()) - canvas.Set("width", bw) - canvas.Set("height", bh) + if document.Truthy() { + body := document.Get("body") + bw := int(body.Get("clientWidth").Float() * u.DeviceScaleFactor()) + bh := int(body.Get("clientHeight").Float() * u.DeviceScaleFactor()) + canvas.Set("width", bw) + canvas.Set("height", bh) + } u.sizeChanged = true } diff --git a/internal/web/web_js.go b/internal/web/web_js.go index 149074165..793107a50 100644 --- a/internal/web/web_js.go +++ b/internal/web/web_js.go @@ -29,7 +29,11 @@ var ( ) 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") isAndroidChrome = strings.Contains(userAgent, "Android") && strings.Contains(userAgent, "Chrome") }