Skip to content

Commit 14a2f85

Browse files
DrakirusGeertJohan
Drakirus
authored andcommitted
Remove duplicate pixels to screencoordinates calculation
Fixes #101
1 parent 693d0a9 commit 14a2f85

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

example/simpleDemo/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func main() {
3838

3939
options := []flutter.Option{
4040
flutter.ProjectAssetsPath(dir + "/flutter_project/demo/build/flutter_assets"),
41+
// flutter.ForcePixelRatio(1.2),
4142

4243
// This path should not be changed. icudtl.dat is handled by engineDownloader.go
4344
flutter.ApplicationICUDataPath(dir + "/icudtl.dat"),

glfw.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ func glfwCursorPositionCallbackAtPhase(
1818
window *glfw.Window, phase embedder.PointerPhase,
1919
x float64, y float64,
2020
) {
21+
22+
// Pointer events from GLFW are described using screen coordinates.
23+
// We need to provide Flutter with the position in pixels.
2124
width, _ := window.GetSize()
25+
if width == 0 {
26+
fmt.Println("go-flutter: Cannot calculate pointer position in zero-width window.")
27+
return
28+
}
2229
widthPx, _ := window.GetFramebufferSize()
23-
pixelsPerScreenCoordinate := float64(widthPx / width)
30+
pixelsPerScreenCoordinate := float64(widthPx) / float64(width)
31+
2432
event := embedder.PointerEvent{
2533
Phase: phase,
2634
X: x * pixelsPerScreenCoordinate,
@@ -38,19 +46,10 @@ func glfwMouseButtonCallback(window *glfw.Window, key glfw.MouseButton, action g
3846
if key == glfw.MouseButton1 {
3947
x, y := window.GetCursorPos()
4048

41-
// recalculate x and y from screen cordinates to pixels
42-
widthPx, _ := window.GetFramebufferSize()
43-
width, _ := window.GetSize()
44-
pixelsPerScreenCoordinate := float64(widthPx) / float64(width)
45-
x = x * pixelsPerScreenCoordinate
46-
y = y * pixelsPerScreenCoordinate
47-
4849
if action == glfw.Press {
4950
glfwCursorPositionCallbackAtPhase(window, embedder.PointerPhaseDown, x, y)
5051
// TODO(#90): new kHover pointerphase in embedder.h suggests that pos callback is relevant outside "down" as well...
5152
window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) {
52-
x = x * pixelsPerScreenCoordinate
53-
y = y * pixelsPerScreenCoordinate
5453
glfwCursorPositionCallbackAtPhase(window, embedder.PointerPhaseMove, x, y)
5554
})
5655
}

0 commit comments

Comments
 (0)