ui: Reduce functions in UserInterface

This commit is contained in:
Hajime Hoshi 2016-09-02 23:38:02 +09:00
parent 128dcaa342
commit 6164ecb9fc
6 changed files with 23 additions and 27 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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
View File

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