123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115openTsdlopenB_utilsmoduleTheme=B_thememoduleTrigger=B_triggermoduleDraw=B_drawmoduleE=Sdl.Event(* mouse position. If the mouse goes over a second window, the new origin
immediately shifts *)(* it doesn't work for touchscreen (only the first touch, not motion) *)letpos()=Draw.dpi_unscale_pos@@Trigger.mouse_pos()(* the mouse position registered in the mouse_motion event *)(* Inutile (et inutilisé): apparemment ça ne change rien *)letmotion_posev=matchTrigger.event_kindevwith|`Mouse_motion->letx=E.(getevmouse_motion_x)inlety=E.(getevmouse_motion_y)inletx,y=Draw.dpi_unscale_pos(x,y)inlet()=if!debugthenletpx,py=pos()inif(x,y)<>(px,py)thenprintddebug_event" ! Mouse_pos (%d,%d) <> Motion_pos (%d,%d) ! "pxpyxyinSome(x,y)|_->None(* not used *)letbutton_posev=matchTrigger.event_kindevwith|`Mouse_motion|`Mouse_button_down|`Mouse_button_up->letx=E.(getevmouse_button_x)inlety=E.(getevmouse_button_y)inDraw.dpi_unscale_pos(x,y)|_->failwith"WRONG EVENT"(* return the SDL window where the mouse focus is. If not, return the last
one. TODO check if window does exist (was not destroyed) *)letget_window=letlast_win=refNoneinfun()->do_option(Sdl.get_mouse_focus())(funwin->last_win:=Somewin);!last_win(* physical pixels *)letcompute_finger_posev=(* WARNING as of tsdl version??? this is now normalized in 0..1 *)letfx=E.(getevtouch_finger_x)inletfy=E.(getevtouch_finger_y)inmatchget_window()with|None->failwith"Cannot find window for finger position"(* TODO don't fail for this? *)|Somewin->letw,h=Draw.get_window_sizewininfx*.floatw,fy*.floath(* logical pixels *)letfinger_posev=matchTrigger.event_kindevwith|`Finger_down|`Finger_up|`Finger_motion->letx,y=compute_finger_posevinTheme.unscale_fx,Theme.unscale_fy|_->failwith"WRONG EVENT"(* Guess where the pointer is (in physical pixels), trying mouse first and then
touch *)(* TODO retrieve also from mouse_at_rest *)letpointer_physical_posev=matchTrigger.event_kindevwith|`Mouse_motion|`Mouse_button_down|`Mouse_button_up->letx=E.(getevmouse_button_x)inlety=E.(getevmouse_button_y)inDraw.dpi_rescale(x,y)|`Finger_down|`Finger_up|`Finger_motion->letx,y=compute_finger_posevin(roundx,roundy)|_->beginprintddebug_error"The event for pointer_pos should be a mouse or touch event";Trigger.mouse_pos()end(* in logical pixels *)letpointer_posev=Draw.unscale_pos(pointer_physical_posev)(* The mouse_pos with respect to the given window, using window position if
necessary. *)letwindow_pos=letx'=ref0inlety'=ref0infunwindow->letx,y=pos()inmatchSdl.get_mouse_focus()with|None->(!x',!y')|Somew->ifSdl.get_window_idw=Sdl.get_window_idwindowthen(x':=x;y':=y;(x,y))elseletx0,y0=Draw.get_window_positionwindowinletx1,y1=Draw.get_window_positionwin(x':=x1-x0+x;y':=y1-y0+y;(!x',!y'))letwheel_yev=round(50.*.!Theme.mouse_wheel_scale*.floatE.(getevmouse_wheel_y))letset_wheel_speeds=Theme.mouse_wheel_scale:=s