Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adjusts the camera / mouse properly for high dpi devices
  • Loading branch information
Noofbiz committed May 15, 2018
1 parent fd49fd5 commit 9e8857f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions axis.go
Expand Up @@ -80,11 +80,11 @@ func (am *AxisMouse) Value() float32 {
var diff float32

if am.direction == AxisMouseHori {
diff = (Input.Mouse.X - am.old + (ResizeXOffset / (2 * GetGlobalScale().X)))
am.old = (Input.Mouse.X + (ResizeXOffset / (2 * GetGlobalScale().X)))
diff = (Input.Mouse.X - am.old + (ResizeXOffset / (2 * GetGlobalScale().X * CanvasScale())))
am.old = (Input.Mouse.X + (ResizeXOffset / (2 * GetGlobalScale().X * CanvasScale())))
} else {
diff = (Input.Mouse.Y - am.old + (ResizeYOffset / (2 * GetGlobalScale().Y)))
am.old = (Input.Mouse.Y + (ResizeYOffset / (2 * GetGlobalScale().Y)))
diff = (Input.Mouse.Y - am.old + (ResizeYOffset / (2 * GetGlobalScale().Y * CanvasScale())))
am.old = (Input.Mouse.Y + (ResizeYOffset / (2 * GetGlobalScale().Y * CanvasScale())))
}

return diff
Expand Down
2 changes: 1 addition & 1 deletion common/camera.go
Expand Up @@ -348,7 +348,7 @@ type EntityScroller struct {

// New adjusts CameraBounds to the bounds of EntityScroller.
func (c *EntityScroller) New(*ecs.World) {
offsetX, offsetY := engo.CanvasWidth()/2, engo.CanvasHeight()/2
offsetX, offsetY := engo.GameWidth()/2, engo.GameWidth()/2

CameraBounds.Min.X = c.TrackingBounds.Min.X + (offsetX / engo.GetGlobalScale().X)
CameraBounds.Min.Y = c.TrackingBounds.Min.Y + (offsetY / engo.GetGlobalScale().Y)
Expand Down
4 changes: 2 additions & 2 deletions common/mouse.go
Expand Up @@ -179,8 +179,8 @@ func (m *MouseSystem) Update(dt float32) {
// Translate Mouse.X and Mouse.Y into "game coordinates"
switch engo.CurrentBackEnd {
case engo.BackEndGLFW:
m.mouseX = engo.Input.Mouse.X*m.camera.Z()*(engo.GameWidth()/engo.CanvasWidth()) + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z())/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z()*(engo.GameHeight()/engo.CanvasHeight()) + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z())/engo.GetGlobalScale().Y
m.mouseX = engo.Input.Mouse.X*m.camera.Z() + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z())/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z() + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z())/engo.GetGlobalScale().Y
case engo.BackEndMobile:
m.mouseX = engo.Input.Mouse.X*m.camera.Z() + (m.camera.X()-(engo.GameWidth()/2)*m.camera.Z()+(engo.ResizeXOffset/2))/engo.GetGlobalScale().X
m.mouseY = engo.Input.Mouse.Y*m.camera.Z() + (m.camera.Y()-(engo.GameHeight()/2)*m.camera.Z()+(engo.ResizeYOffset/2))/engo.GetGlobalScale().Y
Expand Down
2 changes: 1 addition & 1 deletion demos/adventure/adventure.go
Expand Up @@ -159,7 +159,7 @@ func (scene *DefaultScene) Setup(u engo.Updater) {
spriteSheet := common.NewSpritesheetFromFile(model, width, height)

hero := scene.CreateHero(
engo.Point{engo.CanvasWidth() / 2, engo.CanvasHeight() / 2},
engo.Point{engo.GameWidth() / 2, engo.GameHeight() / 2},
spriteSheet,
)

Expand Down
6 changes: 3 additions & 3 deletions engo_glfw.go
Expand Up @@ -136,15 +136,15 @@ func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
})

window.SetCursorPosCallback(func(window *glfw.Window, x, y float64) {
Input.Mouse.X, Input.Mouse.Y = float32(x)*scale/opts.GlobalScale.X, float32(y)*scale/opts.GlobalScale.Y
Input.Mouse.X, Input.Mouse.Y = float32(x)/opts.GlobalScale.X, float32(y)/opts.GlobalScale.Y
if Input.Mouse.Action != Release && Input.Mouse.Action != Press {
Input.Mouse.Action = Move
}
})

window.SetMouseButtonCallback(func(window *glfw.Window, b glfw.MouseButton, a glfw.Action, m glfw.ModifierKey) {
x, y := window.GetCursorPos()
Input.Mouse.X, Input.Mouse.Y = float32(x)*scale/opts.GlobalScale.X, float32(y)*scale/opts.GlobalScale.Y
Input.Mouse.X, Input.Mouse.Y = float32(x)/(opts.GlobalScale.X), float32(y)/(opts.GlobalScale.Y)

// this is only valid because we use an internal structure that is
// 100% compatible with glfw3.h
Expand Down Expand Up @@ -287,7 +287,7 @@ Outer:
// CursorPos returns the current cursor position
func CursorPos() (x, y float32) {
w, h := window.GetCursorPos()
return float32(w) * scale, float32(h) * scale
return float32(w), float32(h)
}

// WindowSize gets the current window size
Expand Down
16 changes: 8 additions & 8 deletions engo_js.go
Expand Up @@ -43,8 +43,8 @@ func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {
canvas := document.CreateElement("canvas").(*dom.HTMLCanvasElement)

devicePixelRatio = js.Global.Get("devicePixelRatio").Float()
canvas.Width = int(float64(width)*devicePixelRatio + 0.5) // Nearest non-negative int.
canvas.Height = int(float64(height)*devicePixelRatio + 0.5) // Nearest non-negative int.
canvas.Width = int(float64(width) + 0.5) // Nearest non-negative int.
canvas.Height = int(float64(height) + 0.5) // Nearest non-negative int.
canvas.Style().SetProperty("width", fmt.Sprintf("%vpx", width), "")
canvas.Style().SetProperty("height", fmt.Sprintf("%vpx", height), "")
log.Println("devicePixelRatio", devicePixelRatio)
Expand Down Expand Up @@ -111,22 +111,22 @@ func CreateWindow(title string, width, height int, fullscreen bool, msaa int) {

w.AddEventListener("mousemove", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)*devicePixelRatio) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)*devicePixelRatio) / opts.GlobalScale.Y
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
//Mouse.Action = MOVE
})

w.AddEventListener("mousedown", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)*devicePixelRatio) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)*devicePixelRatio) / opts.GlobalScale.Y
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
Input.Mouse.Action = Press
})

w.AddEventListener("mouseup", false, func(ev dom.Event) {
mm := ev.(*dom.MouseEvent)
Input.Mouse.X = float32(float64(mm.ClientX)*devicePixelRatio) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)*devicePixelRatio) / opts.GlobalScale.Y
Input.Mouse.X = float32(float64(mm.ClientX)) / opts.GlobalScale.X
Input.Mouse.Y = float32(float64(mm.ClientY)) / opts.GlobalScale.Y
Input.Mouse.Action = Release
})
}
Expand Down

0 comments on commit 9e8857f

Please sign in to comment.