mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
ui: Remove dataset attributes
This commit is contained in:
parent
fb3bf4e3b4
commit
95485441f4
@ -20,6 +20,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-gl/glfw/v3.1/glfw"
|
"github.com/go-gl/glfw/v3.1/glfw"
|
||||||
@ -38,6 +39,7 @@ type UserInterface struct {
|
|||||||
deviceScale float64
|
deviceScale float64
|
||||||
framebufferScale int
|
framebufferScale int
|
||||||
context *opengl.Context
|
context *opengl.Context
|
||||||
|
m sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentUI *UserInterface
|
var currentUI *UserInterface
|
||||||
|
@ -30,8 +30,7 @@ func Now() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenSize(width, height int) bool {
|
func (u *UserInterface) SetScreenSize(width, height int) bool {
|
||||||
scale := canvas.Get("dataset").Get("ebitenScreenScale").Int()
|
return u.setScreenSize(width, height, u.scale)
|
||||||
return u.setScreenSize(width, height, scale)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenScale(scale int) bool {
|
func (u *UserInterface) SetScreenScale(scale int) bool {
|
||||||
@ -40,16 +39,19 @@ func (u *UserInterface) SetScreenScale(scale int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) ScreenScale() int {
|
func (u *UserInterface) ScreenScale() int {
|
||||||
return canvas.Get("dataset").Get("ebitenScreenScale").Int()
|
return u.scale
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) ActualScreenScale() int {
|
func (u *UserInterface) ActualScreenScale() int {
|
||||||
return canvas.Get("dataset").Get("ebitenActualScreenScale").Int()
|
return u.scale * int(u.deviceScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
var canvas *js.Object
|
var canvas *js.Object
|
||||||
|
|
||||||
type UserInterface struct{}
|
type UserInterface struct {
|
||||||
|
scale int
|
||||||
|
deviceScale float64
|
||||||
|
}
|
||||||
|
|
||||||
var currentUI = &UserInterface{}
|
var currentUI = &UserInterface{}
|
||||||
|
|
||||||
@ -210,7 +212,7 @@ func Init() *opengl.Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setMouseCursorFromEvent(e *js.Object) {
|
func setMouseCursorFromEvent(e *js.Object) {
|
||||||
scale := canvas.Get("dataset").Get("ebitenScreenScale").Int()
|
scale := currentUI.scale
|
||||||
rect := canvas.Call("getBoundingClientRect")
|
rect := canvas.Call("getBoundingClientRect")
|
||||||
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
||||||
x -= rect.Get("left").Int()
|
x -= rect.Get("left").Int()
|
||||||
@ -218,9 +220,8 @@ func setMouseCursorFromEvent(e *js.Object) {
|
|||||||
currentInput.SetMouseCursor(x/scale, y/scale)
|
currentInput.SetMouseCursor(x/scale, y/scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
func devicePixelRatio() int {
|
func devicePixelRatio() float64 {
|
||||||
// TODO: What if ratio is not an integer but a float?
|
ratio := js.Global.Get("window").Get("devicePixelRatio").Float()
|
||||||
ratio := js.Global.Get("window").Get("devicePixelRatio").Int()
|
|
||||||
if ratio == 0 {
|
if ratio == 0 {
|
||||||
ratio = 1
|
ratio = 1
|
||||||
}
|
}
|
||||||
@ -235,8 +236,8 @@ func (u *UserInterface) Start(width, height, scale int, title string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*UserInterface) size() (width, height int) {
|
func (u *UserInterface) size() (width, height int) {
|
||||||
a := canvas.Get("dataset").Get("ebitenActualScreenScale").Int()
|
a := int(u.ActualScreenScale())
|
||||||
if a == 0 {
|
if a == 0 {
|
||||||
// a == 0 only on the initial state.
|
// a == 0 only on the initial state.
|
||||||
return
|
return
|
||||||
@ -248,16 +249,14 @@ func (*UserInterface) size() (width, height int) {
|
|||||||
|
|
||||||
func (u *UserInterface) setScreenSize(width, height, scale int) bool {
|
func (u *UserInterface) setScreenSize(width, height, scale int) bool {
|
||||||
w, h := u.size()
|
w, h := u.size()
|
||||||
s := canvas.Get("dataset").Get("ebitenScreenScale").Int()
|
s := u.scale
|
||||||
if w == width && h == height && s == scale {
|
if w == width && h == height && s == scale {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
u.scale = scale
|
||||||
actualScreenScale := scale * devicePixelRatio()
|
u.deviceScale = devicePixelRatio()
|
||||||
canvas.Set("width", width*actualScreenScale)
|
canvas.Set("width", width*u.ActualScreenScale())
|
||||||
canvas.Set("height", height*actualScreenScale)
|
canvas.Set("height", height*u.ActualScreenScale())
|
||||||
canvas.Get("dataset").Set("ebitenScreenScale", scale)
|
|
||||||
canvas.Get("dataset").Set("ebitenActualScreenScale", actualScreenScale)
|
|
||||||
canvasStyle := canvas.Get("style")
|
canvasStyle := canvas.Get("style")
|
||||||
|
|
||||||
cssWidth := width * scale
|
cssWidth := width * scale
|
||||||
|
Loading…
Reference in New Issue
Block a user