mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
ui: Refactoring
This commit is contained in:
parent
1953539e22
commit
8371426888
@ -108,51 +108,54 @@ func (u *userInterface) main() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) runOnMainThread(f func()) {
|
func (u *userInterface) runOnMainThread(f func() error) error {
|
||||||
if u.funcs == nil {
|
if u.funcs == nil {
|
||||||
// already closed
|
// already closed
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
|
var err error
|
||||||
u.funcs <- func() {
|
u.funcs <- func() {
|
||||||
f()
|
err = f()
|
||||||
close(ch)
|
close(ch)
|
||||||
}
|
}
|
||||||
<-ch
|
<-ch
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) SetScreenSize(width, height int) bool {
|
func (u *userInterface) SetScreenSize(width, height int) bool {
|
||||||
r := false
|
r := false
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
r = u.setScreenSize(width, height, u.scale)
|
r = u.setScreenSize(width, height, u.scale)
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) SetScreenScale(scale float64) bool {
|
func (u *userInterface) SetScreenScale(scale float64) bool {
|
||||||
r := false
|
r := false
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
r = u.setScreenSize(u.width, u.height, scale)
|
r = u.setScreenSize(u.width, u.height, scale)
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) ScreenScale() float64 {
|
func (u *userInterface) ScreenScale() float64 {
|
||||||
s := 0.0
|
s := 0.0
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
s = u.scale
|
s = u.scale
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) Start(width, height int, scale float64, title string) error {
|
func (u *userInterface) Start(width, height int, scale float64, title string) error {
|
||||||
var err error
|
if err := u.runOnMainThread(func() error {
|
||||||
u.runOnMainThread(func() {
|
|
||||||
m := glfw.GetPrimaryMonitor()
|
m := glfw.GetPrimaryMonitor()
|
||||||
v := m.GetVideoMode()
|
v := m.GetVideoMode()
|
||||||
if !u.setScreenSize(width, height, scale) {
|
if !u.setScreenSize(width, height, scale) {
|
||||||
err = errors.New("ui: Fail to set the screen size")
|
return errors.New("ui: Fail to set the screen size")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
u.window.SetTitle(title)
|
u.window.SetTitle(title)
|
||||||
u.window.Show()
|
u.window.Show()
|
||||||
@ -161,8 +164,11 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er
|
|||||||
x := (v.Width - w) / 2
|
x := (v.Width - w) / 2
|
||||||
y := (v.Height - h) / 3
|
y := (v.Height - h) / 3
|
||||||
u.window.SetPos(x, y)
|
u.window.SetPos(x, y)
|
||||||
})
|
return nil
|
||||||
return err
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) glfwSize() (int, int) {
|
func (u *userInterface) glfwSize() (int, int) {
|
||||||
@ -180,17 +186,18 @@ func (u *userInterface) pollEvents() error {
|
|||||||
|
|
||||||
func (u *userInterface) Update() (interface{}, error) {
|
func (u *userInterface) Update() (interface{}, error) {
|
||||||
shouldClose := false
|
shouldClose := false
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
shouldClose = u.window.ShouldClose()
|
shouldClose = u.window.ShouldClose()
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
if shouldClose {
|
if shouldClose {
|
||||||
return CloseEvent{}, nil
|
return CloseEvent{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var screenSizeEvent *ScreenSizeEvent
|
var screenSizeEvent *ScreenSizeEvent
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
if !u.sizeChanged {
|
if !u.sizeChanged {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
u.sizeChanged = false
|
u.sizeChanged = false
|
||||||
screenSizeEvent = &ScreenSizeEvent{
|
screenSizeEvent = &ScreenSizeEvent{
|
||||||
@ -198,31 +205,29 @@ func (u *userInterface) Update() (interface{}, error) {
|
|||||||
Height: u.height,
|
Height: u.height,
|
||||||
ActualScale: u.actualScreenScale(),
|
ActualScale: u.actualScreenScale(),
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
if screenSizeEvent != nil {
|
if screenSizeEvent != nil {
|
||||||
return *screenSizeEvent, nil
|
return *screenSizeEvent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var ferr error
|
if err := u.runOnMainThread(func() error {
|
||||||
u.runOnMainThread(func() {
|
|
||||||
if err := u.pollEvents(); err != nil {
|
if err := u.pollEvents(); err != nil {
|
||||||
ferr = err
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
for u.window.GetAttrib(glfw.Focused) == 0 {
|
for u.window.GetAttrib(glfw.Focused) == 0 {
|
||||||
// Wait for an arbitrary period to avoid busy loop.
|
// Wait for an arbitrary period to avoid busy loop.
|
||||||
time.Sleep(time.Second / 60)
|
time.Sleep(time.Second / 60)
|
||||||
if err := u.pollEvents(); err != nil {
|
if err := u.pollEvents(); err != nil {
|
||||||
ferr = err
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if u.window.ShouldClose() {
|
if u.window.ShouldClose() {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
return nil
|
||||||
if ferr != nil {
|
}); err != nil {
|
||||||
return nil, ferr
|
return nil, err
|
||||||
}
|
}
|
||||||
// Dummy channel
|
// Dummy channel
|
||||||
ch := make(chan struct{}, 1)
|
ch := make(chan struct{}, 1)
|
||||||
@ -230,8 +235,9 @@ func (u *userInterface) Update() (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) Terminate() error {
|
func (u *userInterface) Terminate() error {
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() error {
|
||||||
glfw.Terminate()
|
glfw.Terminate()
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
close(u.funcs)
|
close(u.funcs)
|
||||||
u.funcs = nil
|
u.funcs = nil
|
||||||
@ -239,11 +245,12 @@ func (u *userInterface) Terminate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) SwapBuffers() error {
|
func (u *userInterface) SwapBuffers() error {
|
||||||
var err error
|
if err := u.runOnMainThread(func() error {
|
||||||
u.runOnMainThread(func() {
|
return u.swapBuffers()
|
||||||
err = u.swapBuffers()
|
}); err != nil {
|
||||||
})
|
return err
|
||||||
return err
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userInterface) swapBuffers() error {
|
func (u *userInterface) swapBuffers() error {
|
||||||
|
Loading…
Reference in New Issue
Block a user