Skip to content

Commit

Permalink
Fix UI Scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Z2Y committed Sep 23, 2020
1 parent 583d8ad commit db1d39a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
31 changes: 31 additions & 0 deletions city/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions city/core/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions city/core/ui/layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion city/core/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 2 additions & 15 deletions city/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion city/scene/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
Expand Down
3 changes: 2 additions & 1 deletion city/scene/mainmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down

0 comments on commit db1d39a

Please sign in to comment.