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 {
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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -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")
}