mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Reduce functions in UserInterface
This commit is contained in:
parent
128dcaa342
commit
6164ecb9fc
@ -21,9 +21,6 @@ type GraphicsContext interface {
|
||||
|
||||
type UserInterface interface {
|
||||
Run(width, height int, scale float64, title string, g GraphicsContext) error
|
||||
ScreenScale() float64
|
||||
SetScreenSize(width, height int) (bool, error)
|
||||
SetScreenScale(scale float64) (bool, error)
|
||||
}
|
||||
|
||||
type RegularTermination struct {
|
||||
|
@ -77,14 +77,10 @@ func initialize() error {
|
||||
}
|
||||
|
||||
func Run(ch <-chan error) error {
|
||||
return currentUI.run(ch)
|
||||
}
|
||||
|
||||
func (u *userInterface) run(ch <-chan error) error {
|
||||
// TODO: Check this is done on the main thread.
|
||||
for {
|
||||
select {
|
||||
case f := <-u.funcs:
|
||||
case f := <-currentUI.funcs:
|
||||
f()
|
||||
case err := <-ch:
|
||||
// ch returns a value not only when an error occur but also it is closed.
|
||||
@ -108,7 +104,8 @@ func (u *userInterface) runOnMainThread(f func() error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenSize(width, height int) (bool, error) {
|
||||
func SetScreenSize(width, height int) (bool, error) {
|
||||
u := currentUI
|
||||
r := false
|
||||
if err := u.runOnMainThread(func() error {
|
||||
var err error
|
||||
@ -123,7 +120,8 @@ func (u *userInterface) SetScreenSize(width, height int) (bool, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenScale(scale float64) (bool, error) {
|
||||
func SetScreenScale(scale float64) (bool, error) {
|
||||
u := currentUI
|
||||
r := false
|
||||
if err := u.runOnMainThread(func() error {
|
||||
var err error
|
||||
@ -138,7 +136,8 @@ func (u *userInterface) SetScreenScale(scale float64) (bool, error) {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (u *userInterface) ScreenScale() float64 {
|
||||
func ScreenScale() float64 {
|
||||
u := currentUI
|
||||
s := 0.0
|
||||
_ = u.runOnMainThread(func() error {
|
||||
s = u.scale
|
||||
|
@ -48,17 +48,17 @@ func shown() bool {
|
||||
return !js.Global.Get("document").Get("hidden").Bool()
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenSize(width, height int) (bool, error) {
|
||||
return u.setScreenSize(width, height, u.scale), nil
|
||||
func SetScreenSize(width, height int) (bool, error) {
|
||||
return currentUI.setScreenSize(width, height, currentUI.scale), nil
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenScale(scale float64) (bool, error) {
|
||||
width, height := u.size()
|
||||
return u.setScreenSize(width, height, scale), nil
|
||||
func SetScreenScale(scale float64) (bool, error) {
|
||||
width, height := currentUI.size()
|
||||
return currentUI.setScreenSize(width, height, scale), nil
|
||||
}
|
||||
|
||||
func (u *userInterface) ScreenScale() float64 {
|
||||
return u.scale
|
||||
func ScreenScale() float64 {
|
||||
return currentUI.scale
|
||||
}
|
||||
|
||||
func (u *userInterface) ActualScreenScale() float64 {
|
||||
|
@ -101,18 +101,18 @@ func (u *userInterface) update(g GraphicsContext) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenSize(width, height int) (bool, error) {
|
||||
func SetScreenSize(width, height int) (bool, error) {
|
||||
// TODO: Implement
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (u *userInterface) SetScreenScale(scale float64) (bool, error) {
|
||||
func SetScreenScale(scale float64) (bool, error) {
|
||||
// TODO: Implement
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (u *userInterface) ScreenScale() float64 {
|
||||
return u.scale
|
||||
func ScreenScale() float64 {
|
||||
return currentUI.scale
|
||||
}
|
||||
|
||||
func (u *userInterface) actualScreenScale() float64 {
|
||||
|
@ -41,8 +41,8 @@ func (t touch) ID() int {
|
||||
|
||||
func (t touch) Position() (int, int) {
|
||||
// TODO: Is this OK to adjust the position here?
|
||||
return int(float64(t.position.x) / ui.CurrentUI().ScreenScale()),
|
||||
int(float64(t.position.y) / ui.CurrentUI().ScreenScale())
|
||||
return int(float64(t.position.x) / ui.ScreenScale()),
|
||||
int(float64(t.position.y) / ui.ScreenScale())
|
||||
}
|
||||
|
||||
func updateTouches() {
|
||||
|
6
run.go
6
run.go
@ -118,7 +118,7 @@ func SetScreenSize(width, height int) {
|
||||
if width <= 0 || height <= 0 {
|
||||
panic("ebiten: width and height must be positive")
|
||||
}
|
||||
if _, err := ui.CurrentUI().SetScreenSize(width, height); err != nil {
|
||||
if _, err := ui.SetScreenSize(width, height); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -130,7 +130,7 @@ func SetScreenScale(scale float64) {
|
||||
if scale <= 0 {
|
||||
panic("ebiten: scale must be positive")
|
||||
}
|
||||
if _, err := ui.CurrentUI().SetScreenScale(scale); err != nil {
|
||||
if _, err := ui.SetScreenScale(scale); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -139,5 +139,5 @@ func SetScreenScale(scale float64) {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func ScreenScale() float64 {
|
||||
return ui.CurrentUI().ScreenScale()
|
||||
return ui.ScreenScale()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user