Skip to content

Commit

Permalink
Merge branch 'master' into sdl-audio
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Oct 30, 2023
2 parents 90a0dad + f1981a3 commit f75e02a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 29 deletions.
27 changes: 27 additions & 0 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,33 @@ public void TestSetTextSelection()
AddAssert("nothing selected", () => textBox.SelectedText == string.Empty);
}

[Test]
public void TestSelectAll()
{
TextBox textBox = null;

AddStep("add textbox", () =>
{
textBoxes.Add(textBox = new BasicTextBox
{
Size = new Vector2(300, 40),
Text = "initial text",
});
});

AddAssert("select all fails", () => textBox.SelectAll(), () => Is.False);
AddAssert("no text selected", () => textBox.SelectedText, () => Is.EqualTo(string.Empty));

AddStep("click on textbox", () =>
{
InputManager.MoveMouseTo(textBox);
InputManager.Click(MouseButton.Left);
});

AddAssert("select all succeeds", () => textBox.SelectAll(), () => Is.True);
AddAssert("all text selected", () => textBox.SelectedText, () => Is.EqualTo(textBox.Text));
}

private void prependString(InsertableTextBox textBox, string text)
{
InputManager.Keys(PlatformAction.MoveBackwardLine);
Expand Down
19 changes: 16 additions & 3 deletions osu.Framework/Graphics/UserInterface/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ public virtual bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
return true;

case PlatformAction.SelectAll:
selectionStart = 0;
selectionEnd = text.Length;
cursorAndLayout.Invalidate();
SelectAll();
onTextSelectionChanged(TextSelectionType.All, lastSelectionBounds);
return true;

Expand Down Expand Up @@ -362,6 +360,21 @@ public virtual void OnReleased(KeyBindingReleaseEvent<PlatformAction> e)
{
}

/// <summary>
/// Selects all text in this <see cref="TextBox"/>. Focus must be acquired before calling this method.
/// </summary>
/// <returns>Whether text has been selected successfully. Returns <c>false</c> if the text box does not have focus.</returns>
public bool SelectAll()
{
if (!HasFocus)
return false;

selectionStart = 0;
selectionEnd = text.Length;
cursorAndLayout.Invalidate();
return true;
}

/// <summary>
/// Find the word boundary in the backward direction, then return the negative amount of characters.
/// </summary>
Expand Down
13 changes: 4 additions & 9 deletions osu.Framework/Input/Handlers/Tablet/OpenTabletDriverHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,11 @@ private void handleTabletsChanged(object? sender, IEnumerable<TabletReference> t

private void handleDeviceReported(object? sender, IDeviceReport report)
{
switch (report)
{
case ITabletReport tabletReport:
handleTabletReport(tabletReport);
break;
if (report is ITabletReport tabletReport)
handleTabletReport(tabletReport);

case IAuxReport auxiliaryReport:
handleAuxiliaryReport(auxiliaryReport);
break;
}
if (report is IAuxReport auxiliaryReport)
handleAuxiliaryReport(auxiliaryReport);
}

private void updateOutputArea(IWindow window)
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Platform/Display.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class Display : IEquatable<Display>
public Rectangle Bounds { get; }

/// <summary>
/// The available <see cref="DisplayMode"/>s on this display.
/// The available <see cref="DisplayMode"/>s on this display, or empty if the display mode cannot be configured (e.g. mobile displays).
/// </summary>
public DisplayMode[] DisplayModes { get; }

Expand Down
60 changes: 52 additions & 8 deletions osu.Framework/Platform/SDL2Window_Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,35 @@ private void enqueueJoystickButtonInput(JoystickButton button, bool isPressed)

private Point previousPolledPoint = Point.Empty;

private SDLButtonMask pressedButtons;

private void pollMouse()
{
SDL.SDL_GetGlobalMouseState(out int x, out int y);
if (previousPolledPoint.X == x && previousPolledPoint.Y == y)
return;
SDLButtonMask globalButtons = (SDLButtonMask)SDL.SDL_GetGlobalMouseState(out int x, out int y);

previousPolledPoint = new Point(x, y);
if (previousPolledPoint.X != x || previousPolledPoint.Y != y)
{
previousPolledPoint = new Point(x, y);

var pos = WindowMode.Value == Configuration.WindowMode.Windowed ? Position : windowDisplayBounds.Location;
int rx = x - pos.X;
int ry = y - pos.Y;
var pos = WindowMode.Value == Configuration.WindowMode.Windowed ? Position : windowDisplayBounds.Location;
int rx = x - pos.X;
int ry = y - pos.Y;

MouseMove?.Invoke(new Vector2(rx * Scale, ry * Scale));
}

MouseMove?.Invoke(new Vector2(rx * Scale, ry * Scale));
// a button should be released if it was pressed and its current global state differs (its bit in globalButtons is set to 0)
SDLButtonMask buttonsToRelease = pressedButtons & (globalButtons ^ pressedButtons);

// the outer if just optimises for the common case that there are no buttons to release.
if (buttonsToRelease != SDLButtonMask.None)
{
if (buttonsToRelease.HasFlagFast(SDLButtonMask.Left)) MouseUp?.Invoke(MouseButton.Left);
if (buttonsToRelease.HasFlagFast(SDLButtonMask.Middle)) MouseUp?.Invoke(MouseButton.Middle);
if (buttonsToRelease.HasFlagFast(SDLButtonMask.Right)) MouseUp?.Invoke(MouseButton.Right);
if (buttonsToRelease.HasFlagFast(SDLButtonMask.X1)) MouseUp?.Invoke(MouseButton.Button1);
if (buttonsToRelease.HasFlagFast(SDLButtonMask.X2)) MouseUp?.Invoke(MouseButton.Button2);
}
}

public virtual void StartTextInput(bool allowIme) => ScheduleCommand(SDL.SDL_StartTextInput);
Expand Down Expand Up @@ -398,14 +414,18 @@ private void handleMouseWheelEvent(SDL.SDL_MouseWheelEvent evtWheel)
private void handleMouseButtonEvent(SDL.SDL_MouseButtonEvent evtButton)
{
MouseButton button = mouseButtonFromEvent(evtButton.button);
SDLButtonMask mask = (SDLButtonMask)SDL.SDL_BUTTON(evtButton.button);
Debug.Assert(Enum.IsDefined(mask));

switch (evtButton.type)
{
case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN:
pressedButtons |= mask;
MouseDown?.Invoke(button);
break;

case SDL.SDL_EventType.SDL_MOUSEBUTTONUP:
pressedButtons &= ~mask;
MouseUp?.Invoke(button);
break;
}
Expand Down Expand Up @@ -481,6 +501,30 @@ private MouseButton mouseButtonFromEvent(byte button)
}
}

/// <summary>
/// Button mask as returned from <see cref="SDL.SDL_GetGlobalMouseState(out int,out int)"/> and <see cref="SDL.SDL_BUTTON"/>.
/// </summary>
[Flags]
private enum SDLButtonMask
{
None = 0,

/// <see cref="SDL.SDL_BUTTON_LMASK"/>
Left = 1 << 0,

/// <see cref="SDL.SDL_BUTTON_MMASK"/>
Middle = 1 << 1,

/// <see cref="SDL.SDL_BUTTON_RMASK"/>
Right = 1 << 2,

/// <see cref="SDL.SDL_BUTTON_X1MASK"/>
X1 = 1 << 3,

/// <see cref="SDL.SDL_BUTTON_X2MASK"/>
X2 = 1 << 4
}

#endregion

/// <summary>
Expand Down
22 changes: 14 additions & 8 deletions osu.Framework/Platform/SDL2Window_Windowing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,24 +350,30 @@ private static bool tryGetDisplayFromSDL(int displayIndex, [NotNullWhen(true)] o
return false;
}

int numModes = SDL.SDL_GetNumDisplayModes(displayIndex);
DisplayMode[] displayModes = Array.Empty<DisplayMode>();

if (numModes < 0)
if (RuntimeInfo.IsDesktop)
{
Logger.Log($"Failed to get display modes for display at index ({displayIndex}) ({rect.w}x{rect.h}). SDL Error: {SDL.SDL_GetError()} ({numModes})");
display = null;
return false;
}
int numModes = SDL.SDL_GetNumDisplayModes(displayIndex);

if (numModes == 0) Logger.Log($"Display at index ({displayIndex}) ({rect.w}x{rect.h}) has no display modes. Fullscreen might not work.");
if (numModes < 0)
{
Logger.Log($"Failed to get display modes for display at index ({displayIndex}) ({rect.w}x{rect.h}). SDL Error: {SDL.SDL_GetError()} ({numModes})");
display = null;
return false;
}

if (numModes == 0)
Logger.Log($"Display at index ({displayIndex}) ({rect.w}x{rect.h}) has no display modes. Fullscreen might not work.");

var displayModes = Enumerable.Range(0, numModes)
displayModes = Enumerable.Range(0, numModes)
.Select(modeIndex =>
{
SDL.SDL_GetDisplayMode(displayIndex, modeIndex, out var mode);
return mode.ToDisplayMode(displayIndex);
})
.ToArray();
}

display = new Display(displayIndex, SDL.SDL_GetDisplayName(displayIndex), new Rectangle(rect.x, rect.y, rect.w, rect.h), displayModes);
return true;
Expand Down

0 comments on commit f75e02a

Please sign in to comment.