Skip to content

Commit e4c30c2

Browse files
authored
Merge pull request #822 from seanfarley/retina-fix
sdl2: fix retina mouse coordinates
2 parents b8b29ce + 2c19d99 commit e4c30c2

1 file changed

Lines changed: 36 additions & 32 deletions

File tree

frontends/sdl2/main.lisp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@
176176
(display-latin-font display)
177177
(display-cjk-normal-font display)))))
178178

179+
(defmethod scaled-char-width ((display display) x)
180+
(let ((scale-x (round (first (display-scale display)))))
181+
(floor (* scale-x x) (char-width))))
182+
183+
(defmethod scaled-char-height ((display display) y)
184+
(let ((scale-y (round (second (display-scale display)))))
185+
(floor (* scale-y y) (char-height))))
186+
179187
(defmethod update-display ((display display))
180188
(sdl2:render-present (display-renderer display)))
181189

@@ -398,8 +406,7 @@
398406
texture
399407
x
400408
y
401-
(* (display-char-width *display*)
402-
(length string))
409+
(* (char-width) (length string))
403410
height)
404411
(sdl2:destroy-texture texture)
405412
(length string))))
@@ -640,12 +647,12 @@
640647
((eql button sdl2-ffi:+sdl-button-middle+) :button-2)
641648
((eql button 4) :button-4))))
642649
(when button
643-
(let ((pixel-x x)
644-
(pixel-y y)
645-
(char-x (floor x (char-width)))
646-
(char-y (floor y (char-height))))
647-
(lem:send-event (lambda ()
648-
(lem:receive-mouse-button-down char-x char-y pixel-x pixel-y button clicks)))))))
650+
(let ((char-x (scaled-char-width *display* x))
651+
(char-y (scaled-char-height *display* y)))
652+
(lem:send-event
653+
(lambda ()
654+
(lem:receive-mouse-button-down char-x char-y x y button
655+
clicks)))))))
649656

650657
(defun on-mouse-button-up (button x y)
651658
(show-cursor)
@@ -654,37 +661,34 @@
654661
((eql button sdl2-ffi:+sdl-button-right+) :button-3)
655662
((eql button sdl2-ffi:+sdl-button-middle+) :button-2)
656663
((eql button 4) :button-4)))
657-
(pixel-x x)
658-
(pixel-y y)
659-
(char-x (floor x (char-width)))
660-
(char-y (floor y (char-height))))
661-
(lem:send-event (lambda ()
662-
(lem:receive-mouse-button-up char-x char-y pixel-x pixel-y button)))))
664+
(char-x (scaled-char-width *display* x))
665+
(char-y (scaled-char-height *display* y)))
666+
(lem:send-event
667+
(lambda ()
668+
(lem:receive-mouse-button-up char-x char-y x y button)))))
663669

664670
(defun on-mouse-motion (x y state)
665671
(show-cursor)
666672
(let ((button (if (= sdl2-ffi:+sdl-button-lmask+ (logand state sdl2-ffi:+sdl-button-lmask+))
667673
:button-1
668674
nil)))
669-
(let ((pixel-x x)
670-
(pixel-y y)
671-
(char-x (floor x (char-width)))
672-
(char-y (floor y (char-height))))
673-
(lem:send-event (lambda ()
674-
(lem:receive-mouse-motion char-x char-y pixel-x pixel-y button))))))
675+
(let ((char-x (scaled-char-width *display* x))
676+
(char-y (scaled-char-height *display* y)))
677+
(lem:send-event
678+
(lambda ()
679+
(lem:receive-mouse-motion char-x char-y x y button))))))
675680

676681
(defun on-mouse-wheel (wheel-x wheel-y which direction)
677682
(declare (ignore which direction))
678683
(show-cursor)
679684
(multiple-value-bind (x y) (sdl2:mouse-state)
680-
(let ((pixel-x x)
681-
(pixel-y y)
682-
(char-x (floor x (char-width)))
683-
(char-y (floor y (char-height))))
684-
(lem:send-event (lambda ()
685-
(lem:receive-mouse-wheel char-x char-y pixel-x pixel-y wheel-x wheel-y)
686-
(when (= 0 (lem:event-queue-length))
687-
(lem:redraw-display)))))))
685+
(let ((char-x (scaled-char-width *display* x))
686+
(char-y (scaled-char-height *display* y)))
687+
(lem:send-event
688+
(lambda ()
689+
(lem:receive-mouse-wheel char-x char-y x y wheel-x wheel-y)
690+
(when (= 0 (lem:event-queue-length))
691+
(lem:redraw-display)))))))
688692

689693
(defun on-textediting (text)
690694
(handle-textediting (get-platform) text)
@@ -749,7 +753,7 @@
749753
(sdl2:free-surface image)))
750754

751755
(defun adapt-high-dpi-display-scale ()
752-
(with-debug ("adpat-high-dpi-display-scale")
756+
(with-debug ("adapt-high-dpi-display-scale")
753757
(with-renderer ()
754758
(multiple-value-bind (renderer-width renderer-height)
755759
(sdl2:get-renderer-output-size (current-renderer))
@@ -760,7 +764,7 @@
760764
(setf (display-scale *display*) (list scale-x scale-y)))))))
761765

762766
(defun adapt-high-dpi-font-size ()
763-
(with-debug ("adpat-high-dpi-font-size")
767+
(with-debug ("adapt-high-dpi-font-size")
764768
(with-renderer ()
765769
(let ((font-config (display-font-config *display*))
766770
(ratio (round (first (display-scale *display*)))))
@@ -1013,8 +1017,8 @@
10131017
(multiple-value-bind (x y bitmask)
10141018
(sdl2:mouse-state)
10151019
(declare (ignore bitmask))
1016-
(values (floor x (display-char-width *display*))
1017-
(floor y (display-char-height *display*))))))
1020+
(values (scaled-char-width *display* x)
1021+
(scaled-char-height *display* y)))))
10181022

10191023
(defmethod lem-if:get-char-width ((implementation sdl2))
10201024
(char-width))

0 commit comments

Comments
 (0)