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