A game framework written with osu! in mind.

Remove unnecessary redirection methods when triggering SDL window events

+31 -80
+1 -1
osu.Framework/Platform/MacOS/MacOSWindow.cs
··· 56 float scrollingDeltaX = Cocoa.SendFloat(theEvent, sel_scrollingdeltax); 57 float scrollingDeltaY = Cocoa.SendFloat(theEvent, sel_scrollingdeltay); 58 59 - ScheduleEvent(() => OnMouseWheel(new Vector2(scrollingDeltaX * scale_factor, scrollingDeltaY * scale_factor), true)); 60 } 61 } 62 }
··· 56 float scrollingDeltaX = Cocoa.SendFloat(theEvent, sel_scrollingdeltax); 57 float scrollingDeltaY = Cocoa.SendFloat(theEvent, sel_scrollingdeltay); 58 59 + ScheduleEvent(() => TriggerMouseWheel(new Vector2(scrollingDeltaX * scale_factor, scrollingDeltaY * scale_factor), true)); 60 } 61 } 62 }
+30 -79
osu.Framework/Platform/SDL2DesktopWindow.cs
··· 144 if (value.Equals(size)) return; 145 146 size = value; 147 - ScheduleEvent(() => OnResized()); 148 } 149 } 150 ··· 385 updateCursorVisibility(!evt.NewValue.HasFlagFast(CursorState.Hidden)); 386 updateCursorConfined(evt.NewValue.HasFlagFast(CursorState.Confined)); 387 }; 388 - 389 - cursorInWindow.ValueChanged += evt => 390 - { 391 - if (evt.NewValue) 392 - OnMouseEntered(); 393 - else 394 - OnMouseLeft(); 395 - }; 396 } 397 398 /// <summary> ··· 465 466 eventScheduler.Update(); 467 468 - OnUpdate(); 469 } 470 471 - OnExited(); 472 473 if (SDLWindowHandle != IntPtr.Zero) 474 SDL.SDL_DestroyWindow(SDLWindowHandle); ··· 500 /// </summary> 501 public void RequestClose() => ScheduleEvent(() => 502 { 503 - if (!OnExitRequested()) 504 Close(); 505 }); 506 ··· 530 { 531 // SDL reports axis values in the range short.MinValue to short.MaxValue, so we scale and clamp it to the range of -1f to 1f 532 var clamped = Math.Clamp((float)axisValue / short.MaxValue, -1f, 1f); 533 - ScheduleEvent(() => OnJoystickAxisChanged(new JoystickAxis(axisSource, clamped))); 534 } 535 536 private void enqueueJoystickButtonInput(JoystickButton button, bool isPressed) 537 { 538 if (isPressed) 539 - ScheduleEvent(() => OnJoystickButtonDown(button)); 540 else 541 - ScheduleEvent(() => OnJoystickButtonUp(button)); 542 } 543 544 /// <summary> ··· 577 var rx = x - pos.X; 578 var ry = y - pos.Y; 579 580 - ScheduleEvent(() => OnMouseMove(new Vector2(rx * Scale, ry * Scale))); 581 } 582 583 #region SDL Event Handling ··· 699 case SDL.SDL_EventType.SDL_DROPFILE: 700 var str = SDL.UTF8_ToManaged(evtDrop.file, true); 701 if (str != null) 702 - ScheduleEvent(() => OnDragDrop(str)); 703 704 break; 705 } ··· 816 } 817 818 private void handleMouseWheelEvent(SDL.SDL_MouseWheelEvent evtWheel) => 819 - ScheduleEvent(() => OnMouseWheel(new Vector2(evtWheel.x, evtWheel.y), false)); 820 821 private void handleMouseButtonEvent(SDL.SDL_MouseButtonEvent evtButton) 822 { ··· 825 switch (evtButton.type) 826 { 827 case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: 828 - ScheduleEvent(() => OnMouseDown(button)); 829 break; 830 831 case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: 832 - ScheduleEvent(() => OnMouseUp(button)); 833 break; 834 } 835 } ··· 837 private void handleMouseMotionEvent(SDL.SDL_MouseMotionEvent evtMotion) 838 { 839 if (SDL.SDL_GetRelativeMouseMode() == SDL.SDL_bool.SDL_FALSE) 840 - ScheduleEvent(() => OnMouseMove(new Vector2(evtMotion.x * Scale, evtMotion.y * Scale))); 841 else 842 - ScheduleEvent(() => OnMouseMoveRelative(new Vector2(evtMotion.xrel * Scale, evtMotion.yrel * Scale))); 843 } 844 845 private unsafe void handleTextInputEvent(SDL.SDL_TextInputEvent evtText) ··· 851 string text = Marshal.PtrToStringUTF8(ptr) ?? ""; 852 853 foreach (char c in text) 854 - ScheduleEvent(() => OnKeyTyped(c)); 855 } 856 857 private void handleTextEditingEvent(SDL.SDL_TextEditingEvent evtEdit) ··· 868 switch (evtKey.type) 869 { 870 case SDL.SDL_EventType.SDL_KEYDOWN: 871 - ScheduleEvent(() => OnKeyDown(key)); 872 break; 873 874 case SDL.SDL_EventType.SDL_KEYUP: 875 - ScheduleEvent(() => OnKeyUp(key)); 876 break; 877 } 878 } ··· 883 884 switch (evtWindow.windowEvent) 885 { 886 - case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN: 887 - ScheduleEvent(OnShown); 888 - break; 889 - 890 - case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN: 891 - ScheduleEvent(OnHidden); 892 - break; 893 - 894 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED: 895 // explicitly requery as there are occasions where what SDL has provided us with is not up-to-date. 896 SDL.SDL_GetWindowPosition(SDLWindowHandle, out int x, out int y); ··· 900 { 901 position = newPosition; 902 storeWindowPositionToConfig(); 903 - ScheduleEvent(() => OnMoved(newPosition)); 904 } 905 906 break; ··· 914 915 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER: 916 cursorInWindow.Value = true; 917 - ScheduleEvent(OnMouseEntered); 918 break; 919 920 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE: 921 cursorInWindow.Value = false; 922 - ScheduleEvent(OnMouseLeft); 923 break; 924 925 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: 926 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: 927 - ScheduleEvent(OnFocusGained); 928 break; 929 930 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: 931 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: 932 - ScheduleEvent(OnFocusLost); 933 break; 934 935 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: ··· 963 964 if (windowState != stateBefore) 965 { 966 - ScheduleEvent(() => OnWindowStateChanged(windowState)); 967 updateMaximisedState(); 968 } 969 ··· 973 { 974 displayIndex = newDisplayIndex; 975 currentDisplay = Displays.ElementAtOrDefault(displayIndex) ?? PrimaryDisplay; 976 - ScheduleEvent(() => OnDisplayChanged(currentDisplay)); 977 } 978 } 979 ··· 1093 1094 return currentDisplay.Bounds.Size; 1095 } 1096 - 1097 - protected void OnHidden() { } 1098 - 1099 - protected void OnShown() { } 1100 - 1101 - protected void OnWindowStateChanged(WindowState state) => WindowStateChanged?.Invoke(state); 1102 - 1103 - protected void OnDisplayChanged(Display display) => CurrentDisplayBindable.Value = display; 1104 - 1105 - protected void OnFocusGained() => Focused = true; 1106 - 1107 - protected void OnFocusLost() => Focused = false; 1108 1109 private MouseButton mouseButtonFromEvent(byte button) 1110 { ··· 1338 public event Action<WindowState> WindowStateChanged; 1339 1340 /// <summary> 1341 - /// Invoked when the user attempts to close the window. 1342 /// </summary> 1343 public event Func<bool> ExitRequested; 1344 ··· 1367 /// </summary> 1368 public event Action<Vector2, bool> MouseWheel; 1369 1370 /// <summary> 1371 /// Invoked when the user moves the mouse cursor within the window. 1372 /// </summary> ··· 1421 /// Invoked when the user drops a file into the window. 1422 /// </summary> 1423 public event Action<string> DragDrop; 1424 - 1425 - #endregion 1426 - 1427 - #region Event Invocation 1428 - 1429 - protected void OnUpdate() => Update?.Invoke(); 1430 - 1431 - protected void OnResized() => Resized?.Invoke(); 1432 - 1433 - protected bool OnExitRequested() => ExitRequested?.Invoke() ?? false; 1434 - protected void OnExited() => Exited?.Invoke(); 1435 - protected void OnMouseEntered() => MouseEntered?.Invoke(); 1436 - protected void OnMouseLeft() => MouseLeft?.Invoke(); 1437 - protected void OnMoved(Point point) => Moved?.Invoke(point); 1438 - protected void OnMouseWheel(Vector2 delta, bool precise) => MouseWheel?.Invoke(delta, precise); 1439 - protected void OnMouseMove(Vector2 position) => MouseMove?.Invoke(position); 1440 - protected void OnMouseMoveRelative(Vector2 position) => MouseMoveRelative?.Invoke(position); 1441 - protected void OnMouseDown(MouseButton button) => MouseDown?.Invoke(button); 1442 - protected void OnMouseUp(MouseButton button) => MouseUp?.Invoke(button); 1443 - protected void OnKeyDown(Key key) => KeyDown?.Invoke(key); 1444 - protected void OnKeyUp(Key key) => KeyUp?.Invoke(key); 1445 - protected void OnKeyTyped(char c) => KeyTyped?.Invoke(c); 1446 - protected void OnJoystickAxisChanged(JoystickAxis axis) => JoystickAxisChanged?.Invoke(axis); 1447 - protected void OnJoystickButtonDown(JoystickButton button) => JoystickButtonDown?.Invoke(button); 1448 - protected void OnJoystickButtonUp(JoystickButton button) => JoystickButtonUp?.Invoke(button); 1449 - protected void OnDragDrop(string file) => DragDrop?.Invoke(file); 1450 1451 #endregion 1452
··· 144 if (value.Equals(size)) return; 145 146 size = value; 147 + ScheduleEvent(() => Resized?.Invoke()); 148 } 149 } 150 ··· 385 updateCursorVisibility(!evt.NewValue.HasFlagFast(CursorState.Hidden)); 386 updateCursorConfined(evt.NewValue.HasFlagFast(CursorState.Confined)); 387 }; 388 } 389 390 /// <summary> ··· 457 458 eventScheduler.Update(); 459 460 + Update?.Invoke(); 461 } 462 463 + Exited?.Invoke(); 464 465 if (SDLWindowHandle != IntPtr.Zero) 466 SDL.SDL_DestroyWindow(SDLWindowHandle); ··· 492 /// </summary> 493 public void RequestClose() => ScheduleEvent(() => 494 { 495 + if (ExitRequested?.Invoke() != true) 496 Close(); 497 }); 498 ··· 522 { 523 // SDL reports axis values in the range short.MinValue to short.MaxValue, so we scale and clamp it to the range of -1f to 1f 524 var clamped = Math.Clamp((float)axisValue / short.MaxValue, -1f, 1f); 525 + ScheduleEvent(() => JoystickAxisChanged?.Invoke(new JoystickAxis(axisSource, clamped))); 526 } 527 528 private void enqueueJoystickButtonInput(JoystickButton button, bool isPressed) 529 { 530 if (isPressed) 531 + ScheduleEvent(() => JoystickButtonDown?.Invoke(button)); 532 else 533 + ScheduleEvent(() => JoystickButtonUp?.Invoke(button)); 534 } 535 536 /// <summary> ··· 569 var rx = x - pos.X; 570 var ry = y - pos.Y; 571 572 + ScheduleEvent(() => MouseMove?.Invoke(new Vector2(rx * Scale, ry * Scale))); 573 } 574 575 #region SDL Event Handling ··· 691 case SDL.SDL_EventType.SDL_DROPFILE: 692 var str = SDL.UTF8_ToManaged(evtDrop.file, true); 693 if (str != null) 694 + ScheduleEvent(() => DragDrop?.Invoke(str)); 695 696 break; 697 } ··· 808 } 809 810 private void handleMouseWheelEvent(SDL.SDL_MouseWheelEvent evtWheel) => 811 + ScheduleEvent(() => TriggerMouseWheel(new Vector2(evtWheel.x, evtWheel.y), false)); 812 813 private void handleMouseButtonEvent(SDL.SDL_MouseButtonEvent evtButton) 814 { ··· 817 switch (evtButton.type) 818 { 819 case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: 820 + ScheduleEvent(() => MouseDown?.Invoke(button)); 821 break; 822 823 case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: 824 + ScheduleEvent(() => MouseUp?.Invoke(button)); 825 break; 826 } 827 } ··· 829 private void handleMouseMotionEvent(SDL.SDL_MouseMotionEvent evtMotion) 830 { 831 if (SDL.SDL_GetRelativeMouseMode() == SDL.SDL_bool.SDL_FALSE) 832 + ScheduleEvent(() => MouseMove?.Invoke(new Vector2(evtMotion.x * Scale, evtMotion.y * Scale))); 833 else 834 + ScheduleEvent(() => MouseMoveRelative?.Invoke(new Vector2(evtMotion.xrel * Scale, evtMotion.yrel * Scale))); 835 } 836 837 private unsafe void handleTextInputEvent(SDL.SDL_TextInputEvent evtText) ··· 843 string text = Marshal.PtrToStringUTF8(ptr) ?? ""; 844 845 foreach (char c in text) 846 + ScheduleEvent(() => KeyTyped?.Invoke(c)); 847 } 848 849 private void handleTextEditingEvent(SDL.SDL_TextEditingEvent evtEdit) ··· 860 switch (evtKey.type) 861 { 862 case SDL.SDL_EventType.SDL_KEYDOWN: 863 + ScheduleEvent(() => KeyDown?.Invoke(key)); 864 break; 865 866 case SDL.SDL_EventType.SDL_KEYUP: 867 + ScheduleEvent(() => KeyUp?.Invoke(key)); 868 break; 869 } 870 } ··· 875 876 switch (evtWindow.windowEvent) 877 { 878 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED: 879 // explicitly requery as there are occasions where what SDL has provided us with is not up-to-date. 880 SDL.SDL_GetWindowPosition(SDLWindowHandle, out int x, out int y); ··· 884 { 885 position = newPosition; 886 storeWindowPositionToConfig(); 887 + ScheduleEvent(() => Moved?.Invoke(newPosition)); 888 } 889 890 break; ··· 898 899 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER: 900 cursorInWindow.Value = true; 901 + ScheduleEvent(() => MouseEntered?.Invoke()); 902 break; 903 904 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE: 905 cursorInWindow.Value = false; 906 + ScheduleEvent(() => MouseLeft?.Invoke()); 907 break; 908 909 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: 910 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: 911 + ScheduleEvent(() => Focused = true); 912 break; 913 914 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: 915 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: 916 + ScheduleEvent(() => Focused = false); 917 break; 918 919 case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: ··· 947 948 if (windowState != stateBefore) 949 { 950 + ScheduleEvent(() => WindowStateChanged?.Invoke(windowState)); 951 updateMaximisedState(); 952 } 953 ··· 957 { 958 displayIndex = newDisplayIndex; 959 currentDisplay = Displays.ElementAtOrDefault(displayIndex) ?? PrimaryDisplay; 960 + ScheduleEvent(() => 961 + { 962 + CurrentDisplayBindable.Value = currentDisplay; 963 + }); 964 } 965 } 966 ··· 1080 1081 return currentDisplay.Bounds.Size; 1082 } 1083 1084 private MouseButton mouseButtonFromEvent(byte button) 1085 { ··· 1313 public event Action<WindowState> WindowStateChanged; 1314 1315 /// <summary> 1316 + /// Invoked when the user attempts to close the window. Return value of true will cancel exit. 1317 /// </summary> 1318 public event Func<bool> ExitRequested; 1319 ··· 1342 /// </summary> 1343 public event Action<Vector2, bool> MouseWheel; 1344 1345 + protected void TriggerMouseWheel(Vector2 delta, bool precise) => MouseWheel?.Invoke(delta, precise); 1346 + 1347 /// <summary> 1348 /// Invoked when the user moves the mouse cursor within the window. 1349 /// </summary> ··· 1398 /// Invoked when the user drops a file into the window. 1399 /// </summary> 1400 public event Action<string> DragDrop; 1401 1402 #endregion 1403