From db1d39a851e26d5898de31320f63dfe4bfadc173 Mon Sep 17 00:00:00 2001 From: Z2Y Date: Wed, 23 Sep 2020 16:58:57 +0800 Subject: [PATCH] Fix UI Scale --- city/config/config.go | 31 +++++++++++++++++++++++++++++++ city/core/control/control.go | 4 ++-- city/core/ui/layout/layout.go | 5 +++-- city/core/ui/ui.go | 4 +++- city/game.go | 17 ++--------------- city/scene/game.go | 3 ++- city/scene/mainmenu.go | 3 ++- 7 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 city/config/config.go diff --git a/city/config/config.go b/city/config/config.go new file mode 100644 index 0000000..27dcf5f --- /dev/null +++ b/city/config/config.go @@ -0,0 +1,31 @@ +package config + +import ( + "github.com/EngoEngine/engo" +) + +var ( + GameWidth = float32(667) + GameHeight = float32(375) + + windowScale = engo.Point{X: 1, Y: 1} +) + +func GetSafeScale(width, height float32) float32 { + gameRatio := float32(GameHeight) / float32(GameWidth) + safeHeight := width * gameRatio + if safeHeight > height { + return height / float32(GameHeight) + } + return width / float32(GameWidth) +} + +func UpdateWindowScale(engo.Message) { + curW, curH := engo.GameWidth(), engo.GameHeight() + windowScale.X = engo.WindowWidth() / curW + windowScale.Y = engo.WindowHeight() / curH +} + +func WindowScale() engo.Point { + return windowScale +} diff --git a/city/core/control/control.go b/city/core/control/control.go index c21dba9..42080c8 100644 --- a/city/core/control/control.go +++ b/city/core/control/control.go @@ -111,8 +111,8 @@ func (c *ControlSystem) followHero() { func (c *ControlSystem) routeToMousePoint() { var ( - mouseX = engo.Input.Mouse.X*c.camera.Z() + (c.camera.X()-(engo.GameWidth()/2)*c.camera.Z()+(engo.ResizeXOffset/2))/engo.GetGlobalScale().X - mouseY = engo.Input.Mouse.Y*c.camera.Z() + (c.camera.Y()-(engo.GameHeight()/2)*c.camera.Z()+(engo.ResizeYOffset/2))/engo.GetGlobalScale().Y + mouseX = ((engo.Input.Mouse.X * c.camera.Z() * engo.GameWidth() / engo.WindowWidth()) + (c.camera.X()-(engo.GameWidth()/2)*c.camera.Z())/engo.GetGlobalScale().X) + mouseY = ((engo.Input.Mouse.Y * c.camera.Z() * engo.GameHeight() / engo.WindowHeight()) + (c.camera.Y()-(engo.GameHeight()/2)*c.camera.Z())/engo.GetGlobalScale().Y) ) if engo.Input.Mouse.Action == engo.Press { diff --git a/city/core/ui/layout/layout.go b/city/core/ui/layout/layout.go index 13c6d64..27e96bb 100644 --- a/city/core/ui/layout/layout.go +++ b/city/core/ui/layout/layout.go @@ -2,6 +2,7 @@ package layout import ( "github.com/EngoEngine/engo" + "github.com/Z2Y/trpgo/city/config" ) func AlignCenter(parent, child engo.AABB) engo.Point { @@ -26,11 +27,11 @@ func AlignRightTop(parent engo.AABB, child engo.AABB, right, top float32) engo.P } func AlignToWorldCenter(child engo.AABB) engo.Point { - p := engo.AABB{Max: engo.Point{X: engo.WindowWidth() / engo.GetGlobalScale().X, Y: engo.WindowHeight() / engo.GetGlobalScale().Y}} + p := engo.AABB{Max: engo.Point{X: float32(config.GameWidth), Y: float32(config.GameHeight)}} return AlignCenter(p, child) } func AlignToWorldRightBottom(child engo.AABB, right, bottom float32) engo.Point { - p := engo.AABB{Max: engo.Point{X: engo.WindowWidth() / engo.GetGlobalScale().X, Y: engo.WindowHeight() / engo.GetGlobalScale().Y}} + p := engo.AABB{Max: engo.Point{X: float32(config.GameWidth), Y: float32(config.GameHeight)}} return AlignRightBottom(p, child, right, bottom) } diff --git a/city/core/ui/ui.go b/city/core/ui/ui.go index 78ea7bd..a65c6c8 100644 --- a/city/core/ui/ui.go +++ b/city/core/ui/ui.go @@ -7,6 +7,7 @@ import ( "github.com/EngoEngine/ecs" "github.com/EngoEngine/engo" "github.com/EngoEngine/engo/common" + "github.com/Z2Y/trpgo/city/config" "github.com/Z2Y/trpgo/city/core/input" ) @@ -130,6 +131,7 @@ func (ui *UISystem) New(world *ecs.World) { ui.Remove(msg.Target.BasicEntity) } }) + engo.Mailbox.Listen("WindowResizeMessage", config.UpdateWindowScale) } func (*UISystem) Priority() int { @@ -212,7 +214,7 @@ func (ui *UISystem) getUIZIndexOffset() float32 { } func (ui *UISystem) update() { - curPos := engo.Point{X: engo.Input.Mouse.X, Y: engo.Input.Mouse.Y} + curPos := engo.Point{X: engo.Input.Mouse.X / config.WindowScale().X, Y: engo.Input.Mouse.Y / config.WindowScale().Y} updateZindex, zIndexOffset := ui.shouldUpdateIndex(), float32(0) resetInput := false diff --git a/city/game.go b/city/game.go index 916bd5b..ba6d7ab 100644 --- a/city/game.go +++ b/city/game.go @@ -2,25 +2,12 @@ package city import ( "github.com/EngoEngine/engo" + "github.com/Z2Y/trpgo/city/config" "github.com/Z2Y/trpgo/city/scene" ) -var ( - GameWidth = 667 - GameHeight = 375 -) - -func getSafeScale(width, height float32) float32 { - gameRatio := float32(GameHeight) / float32(GameWidth) - safeHeight := width * gameRatio - if safeHeight > height { - return height / float32(GameHeight) - } - return width / float32(GameWidth) -} - func Start(width, height int) { - gameScale := getSafeScale(float32(width), float32(height)) + gameScale := config.GetSafeScale(float32(width), float32(height)) opts := engo.RunOptions{ Title: "trpgo", Width: width, diff --git a/city/scene/game.go b/city/scene/game.go index e56de42..f9e416d 100644 --- a/city/scene/game.go +++ b/city/scene/game.go @@ -9,6 +9,7 @@ import ( "github.com/EngoEngine/engo/common" "github.com/Z2Y/trpgo/city/asset" + "github.com/Z2Y/trpgo/city/config" "github.com/Z2Y/trpgo/city/core" "github.com/Z2Y/trpgo/city/core/control" "github.com/Z2Y/trpgo/city/core/ui" @@ -95,7 +96,7 @@ func (g *Game) SetupUI(w *ecs.World) { log.Println("系统 clicked") panel := ui.NewPanel(ui.Panel{Image: asset.LoadedSubSprite("blue_panel.png"), UIBasic: ui.UIBasic{Width: 400, Height: 300}}) - modal := ui.NewModal(ui.Modal{Content: panel, UIBasic: ui.UIBasic{Width: engo.WindowWidth() / engo.GetGlobalScale().X, Height: engo.WindowHeight() / engo.GetGlobalScale().Y}}) + modal := ui.NewModal(ui.Modal{Content: panel, UIBasic: ui.UIBasic{Width: config.GameWidth, Height: config.GameHeight}}) w.AddEntity(modal) }) } diff --git a/city/scene/mainmenu.go b/city/scene/mainmenu.go index f7e5bc8..b2d2b3f 100644 --- a/city/scene/mainmenu.go +++ b/city/scene/mainmenu.go @@ -2,6 +2,7 @@ package scene import ( "github.com/Z2Y/trpgo/city/asset" + "github.com/Z2Y/trpgo/city/config" "github.com/Z2Y/trpgo/city/core/ui" "github.com/Z2Y/trpgo/city/core/ui/layout" @@ -27,7 +28,7 @@ func (s *MainMenu) Setup(u engo.Updater) { w := u.(*ecs.World) common.SetBackground(background) - engo.Window.SetAspectRatio(667, 375) + engo.Window.SetAspectRatio(int(config.GameWidth), int(config.GameHeight)) ui.SetDefaultFont("font/CN.ttf", 24) w.AddSystem(&common.RenderSystem{}) w.AddSystemInterface(&ui.UISystem{}, ui.UIEntityFace, nil)