mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Change UserInterface to be an interface
This commit is contained in:
parent
d5d9e2c73e
commit
d09bb63f71
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
*~
|
*~
|
||||||
|
*.aar
|
||||||
|
*.apk
|
||||||
|
@ -104,6 +104,7 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// TODO: Use the error value
|
||||||
defer ui.CurrentUI().Terminate()
|
defer ui.CurrentUI().Terminate()
|
||||||
|
|
||||||
frames := 0
|
frames := 0
|
||||||
@ -143,7 +144,9 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ui.CurrentUI().SwapBuffers()
|
if err := ui.CurrentUI().SwapBuffers(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
beforeForUpdate += int64(tt) * int64(time.Second) / int64(fps)
|
||||||
frames++
|
frames++
|
||||||
}
|
}
|
||||||
|
25
internal/ui/ui.go
Normal file
25
internal/ui/ui.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright 2015 Hajime Hoshi
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package ui
|
||||||
|
|
||||||
|
type UserInterface interface {
|
||||||
|
Start(width, height, scale int, title string) error
|
||||||
|
Update() (interface{}, error)
|
||||||
|
SwapBuffers() error
|
||||||
|
Terminate() error
|
||||||
|
ScreenScale() int
|
||||||
|
SetScreenSize(width, height int) bool
|
||||||
|
SetScreenScale(scale int) bool
|
||||||
|
}
|
@ -27,7 +27,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserInterface struct {
|
type userInterface struct {
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
@ -39,9 +39,9 @@ type UserInterface struct {
|
|||||||
sizeChanged bool
|
sizeChanged bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentUI *UserInterface
|
var currentUI *userInterface
|
||||||
|
|
||||||
func CurrentUI() *UserInterface {
|
func CurrentUI() UserInterface {
|
||||||
return currentUI
|
return currentUI
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ func initialize() (*opengl.Context, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
u := &UserInterface{
|
u := &userInterface{
|
||||||
window: window,
|
window: window,
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
sizeChanged: true,
|
sizeChanged: true,
|
||||||
@ -98,7 +98,7 @@ func Main() error {
|
|||||||
return currentUI.main()
|
return currentUI.main()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) main() error {
|
func (u *userInterface) main() error {
|
||||||
// TODO: Check this is done on the main thread.
|
// TODO: Check this is done on the main thread.
|
||||||
for f := range u.funcs {
|
for f := range u.funcs {
|
||||||
f()
|
f()
|
||||||
@ -106,7 +106,7 @@ func (u *UserInterface) main() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) runOnMainThread(f func()) {
|
func (u *userInterface) runOnMainThread(f func()) {
|
||||||
if u.funcs == nil {
|
if u.funcs == nil {
|
||||||
// already closed
|
// already closed
|
||||||
return
|
return
|
||||||
@ -119,7 +119,7 @@ func (u *UserInterface) runOnMainThread(f func()) {
|
|||||||
<-ch
|
<-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
r = u.setScreenSize(width, height, u.scale)
|
r = u.setScreenSize(width, height, u.scale)
|
||||||
@ -127,7 +127,7 @@ func (u *UserInterface) SetScreenSize(width, height int) bool {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenScale(scale int) bool {
|
func (u *userInterface) SetScreenScale(scale int) bool {
|
||||||
r := false
|
r := false
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
r = u.setScreenSize(u.width, u.height, scale)
|
r = u.setScreenSize(u.width, u.height, scale)
|
||||||
@ -135,7 +135,7 @@ func (u *UserInterface) SetScreenScale(scale int) bool {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) ScreenScale() int {
|
func (u *userInterface) ScreenScale() int {
|
||||||
s := 0
|
s := 0
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
s = u.scale
|
s = u.scale
|
||||||
@ -143,7 +143,7 @@ func (u *UserInterface) ScreenScale() int {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Start(width, height, scale int, title string) error {
|
func (u *userInterface) Start(width, height, scale int, title string) error {
|
||||||
var err error
|
var err error
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
m := glfw.GetPrimaryMonitor()
|
m := glfw.GetPrimaryMonitor()
|
||||||
@ -174,20 +174,20 @@ func (u *UserInterface) Start(width, height, scale int, title string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) windowScale() int {
|
func (u *userInterface) windowScale() int {
|
||||||
return u.scale * int(u.deviceScale)
|
return u.scale * int(u.deviceScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) actualScreenScale() int {
|
func (u *userInterface) actualScreenScale() int {
|
||||||
return u.windowScale() * u.framebufferScale
|
return u.windowScale() * u.framebufferScale
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) pollEvents() error {
|
func (u *userInterface) pollEvents() error {
|
||||||
glfw.PollEvents()
|
glfw.PollEvents()
|
||||||
return currentInput.update(u.window, u.windowScale())
|
return currentInput.update(u.window, u.windowScale())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Update() (interface{}, error) {
|
func (u *userInterface) Update() (interface{}, error) {
|
||||||
shouldClose := false
|
shouldClose := false
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
shouldClose = u.window.ShouldClose()
|
shouldClose = u.window.ShouldClose()
|
||||||
@ -237,21 +237,23 @@ func (u *UserInterface) Update() (interface{}, error) {
|
|||||||
return RenderEvent{}, nil
|
return RenderEvent{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Terminate() {
|
func (u *userInterface) Terminate() error {
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
glfw.Terminate()
|
glfw.Terminate()
|
||||||
})
|
})
|
||||||
close(u.funcs)
|
close(u.funcs)
|
||||||
u.funcs = nil
|
u.funcs = nil
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SwapBuffers() {
|
func (u *userInterface) SwapBuffers() error {
|
||||||
u.runOnMainThread(func() {
|
u.runOnMainThread(func() {
|
||||||
u.swapBuffers()
|
u.swapBuffers()
|
||||||
})
|
})
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) swapBuffers() {
|
func (u *userInterface) swapBuffers() {
|
||||||
// The bound framebuffer must be the default one (0) before swapping buffers.
|
// The bound framebuffer must be the default one (0) before swapping buffers.
|
||||||
u.context.BindZeroFramebuffer()
|
u.context.BindZeroFramebuffer()
|
||||||
u.context.RunOnContextThread(func() error {
|
u.context.RunOnContextThread(func() error {
|
||||||
@ -260,7 +262,7 @@ func (u *UserInterface) swapBuffers() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) setScreenSize(width, height, scale int) bool {
|
func (u *userInterface) setScreenSize(width, height, scale int) bool {
|
||||||
if u.width == width && u.height == height && u.scale == scale {
|
if u.width == width && u.height == height && u.scale == scale {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -24,36 +24,19 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenSize(width, height int) bool {
|
|
||||||
return u.setScreenSize(width, height, u.scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenScale(scale int) bool {
|
|
||||||
width, height := u.size()
|
|
||||||
return u.setScreenSize(width, height, scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) ScreenScale() int {
|
|
||||||
return u.scale
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserInterface) ActualScreenScale() int {
|
|
||||||
return u.scale * int(u.deviceScale)
|
|
||||||
}
|
|
||||||
|
|
||||||
var canvas *js.Object
|
var canvas *js.Object
|
||||||
|
|
||||||
type UserInterface struct {
|
type userInterface struct {
|
||||||
scale int
|
scale int
|
||||||
deviceScale float64
|
deviceScale float64
|
||||||
sizeChanged bool
|
sizeChanged bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentUI = &UserInterface{
|
var currentUI = &userInterface{
|
||||||
sizeChanged: true,
|
sizeChanged: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
func CurrentUI() *UserInterface {
|
func CurrentUI() UserInterface {
|
||||||
return currentUI
|
return currentUI
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +63,24 @@ func vsync() {
|
|||||||
<-ch
|
<-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Update() (interface{}, error) {
|
func (u *userInterface) SetScreenSize(width, height int) bool {
|
||||||
|
return u.setScreenSize(width, height, u.scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *userInterface) SetScreenScale(scale int) bool {
|
||||||
|
width, height := u.size()
|
||||||
|
return u.setScreenSize(width, height, scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *userInterface) ScreenScale() int {
|
||||||
|
return u.scale
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *userInterface) ActualScreenScale() int {
|
||||||
|
return u.scale * int(u.deviceScale)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *userInterface) Update() (interface{}, error) {
|
||||||
currentInput.UpdateGamepads()
|
currentInput.UpdateGamepads()
|
||||||
if u.sizeChanged {
|
if u.sizeChanged {
|
||||||
u.sizeChanged = false
|
u.sizeChanged = false
|
||||||
@ -96,15 +96,17 @@ func (u *UserInterface) Update() (interface{}, error) {
|
|||||||
return RenderEvent{}, nil
|
return RenderEvent{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Terminate() {
|
func (u *userInterface) Terminate() error {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SwapBuffers() {
|
func (u *userInterface) SwapBuffers() error {
|
||||||
vsync()
|
vsync()
|
||||||
for !shown() {
|
for !shown() {
|
||||||
vsync()
|
vsync()
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize() (*opengl.Context, error) {
|
func initialize() (*opengl.Context, error) {
|
||||||
@ -252,7 +254,7 @@ func Main() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Start(width, height, scale int, title string) error {
|
func (u *userInterface) Start(width, height, scale int, title string) error {
|
||||||
doc := js.Global.Get("document")
|
doc := js.Global.Get("document")
|
||||||
doc.Set("title", title)
|
doc.Set("title", title)
|
||||||
u.setScreenSize(width, height, scale)
|
u.setScreenSize(width, height, scale)
|
||||||
@ -260,7 +262,7 @@ func (u *UserInterface) Start(width, height, scale int, title string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) size() (width, height int) {
|
func (u *userInterface) size() (width, height int) {
|
||||||
a := int(u.ActualScreenScale())
|
a := int(u.ActualScreenScale())
|
||||||
if a == 0 {
|
if a == 0 {
|
||||||
// a == 0 only on the initial state.
|
// a == 0 only on the initial state.
|
||||||
@ -271,7 +273,7 @@ func (u *UserInterface) size() (width, height int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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 := u.scale
|
s := u.scale
|
||||||
if w == width && h == height && s == scale {
|
if w == width && h == height && s == scale {
|
||||||
|
@ -29,37 +29,37 @@ func Main() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserInterface struct {
|
type userInterface struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CurrentUI() *UserInterface {
|
func CurrentUI() UserInterface {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Start(width, height, scale int, title string) error {
|
func (u *userInterface) Start(width, height, scale int, title string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Terminate() error {
|
func (u *userInterface) Terminate() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) Update() (interface{}, error) {
|
func (u *userInterface) Update() (interface{}, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SwapBuffers() error {
|
func (u *userInterface) SwapBuffers() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenSize(width, height int) bool {
|
func (u *userInterface) SetScreenSize(width, height int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) SetScreenScale(scale int) bool {
|
func (u *userInterface) SetScreenScale(scale int) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserInterface) ScreenScale() int {
|
func (u *userInterface) ScreenScale() int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user