Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions analyzers/disabled.editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
is_global = true

# CA1008: Enums should have zero value
# Some enum doesn't need a zero value
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1008
dotnet_diagnostic.CA1008.severity = none

# CA1033: Interface methods should be callable by child types
# Some childs should not have the parent methods
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1033
Expand Down
2 changes: 1 addition & 1 deletion src/KappaDuck.Quack/Events/CultureChangedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
namespace KappaDuck.Quack.Events;

/// <summary>
/// Represents an event where the user's culture preferences has been changed.
/// Raised when the user's culture preferences has been changed.
/// </summary>
public readonly struct CultureChangedEvent : IEvent;
116 changes: 116 additions & 0 deletions src/KappaDuck.Quack/Events/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace KappaDuck.Quack.Events;
private readonly MouseRemovedEvent _mouseRemovedEvent;
private readonly KeyPressedEvent _keyPressedEvent;
private readonly KeyReleasedEvent _keyReleasedEvent;
private readonly MouseButtonPressedEvent _buttonPressedEvent;
private readonly MouseButtonReleasedEvent _buttonReleasedEvent;
private readonly MouseMovedEvent _mouseMovedEvent;
private readonly MouseWheelEvent _wheelEvent;

/// <summary>
/// Initializes a quit requested event.
Expand Down Expand Up @@ -111,6 +115,46 @@ public Event(KeyReleasedEvent e)
Type = SDL_EventType.KeyUp;
}

/// <summary>
/// Initializes a mouse button pressed event.
/// </summary>
/// <param name="e">The mouse button pressed event.</param>
public Event(MouseButtonPressedEvent e)
{
_buttonPressedEvent = e;
Type = SDL_EventType.MouseButtonDown;
}

/// <summary>
/// Initializes a mouse button released event.
/// </summary>
/// <param name="e">The mouse button released event.</param>
public Event(MouseButtonReleasedEvent e)
{
_buttonReleasedEvent = e;
Type = SDL_EventType.MouseButtonUp;
}

/// <summary>
/// Initializes a mouse moved event.
/// </summary>
/// <param name="e">The mouse moved event.</param>
public Event(MouseMovedEvent e)
{
_mouseMovedEvent = e;
Type = SDL_EventType.MouseMotion;
}

/// <summary>
/// Initializes a mouse wheel event.
/// </summary>
/// <param name="e">The mouse wheel event.</param>
public Event(MouseWheelEvent e)
{
_wheelEvent = e;
Type = SDL_EventType.MouseWheel;
}

internal SDL_EventType Type { get; }

/// <summary>
Expand All @@ -133,6 +177,10 @@ public Event(KeyReleasedEvent e)
SDL_EventType.MouseRemoved => _mouseRemovedEvent,
SDL_EventType.KeyDown => _keyPressedEvent,
SDL_EventType.KeyUp => _keyReleasedEvent,
SDL_EventType.MouseButtonDown => _buttonPressedEvent,
SDL_EventType.MouseButtonUp => _buttonReleasedEvent,
SDL_EventType.MouseMotion => _mouseMovedEvent,
SDL_EventType.MouseWheel => _wheelEvent,
_ => null
};

Expand Down Expand Up @@ -279,4 +327,72 @@ public bool TryGetValue(out KeyReleasedEvent e)
e = _keyReleasedEvent;
return true;
}

/// <summary>
/// Attempts to retrieve this event as a <see cref="MouseButtonPressedEvent"/>.
/// </summary>
/// <param name="e">The mouse button pressed event.</param>
/// <returns><see langword="true"/> if this event holds a <see cref="MouseButtonPressedEvent"/>; otherwise <see langword="false"/></returns>
public bool TryGetValue(out MouseButtonPressedEvent e)
{
if (Type != SDL_EventType.MouseButtonDown)
{
e = default;
return false;
}

e = _buttonPressedEvent;
return true;
}

/// <summary>
/// Attempts to retrieve this event as a <see cref="MouseButtonReleasedEvent"/>.
/// </summary>
/// <param name="e">The mouse button released event.</param>
/// <returns><see langword="true"/> if this event holds a <see cref="MouseButtonReleasedEvent"/>; otherwise <see langword="false"/></returns>
public bool TryGetValue(out MouseButtonReleasedEvent e)
{
if (Type != SDL_EventType.MouseButtonUp)
{
e = default;
return false;
}

e = _buttonReleasedEvent;
return true;
}

/// <summary>
/// Attempts to retrieve this event as a <see cref="MouseMovedEvent"/>.
/// </summary>
/// <param name="e">The mouse moved event.</param>
/// <returns><see langword="true"/> if this event holds a <see cref="MouseMovedEvent"/>; otherwise <see langword="false"/></returns>
public bool TryGetValue(out MouseMovedEvent e)
{
if (Type != SDL_EventType.MouseMotion)
{
e = default;
return false;
}

e = _mouseMovedEvent;
return true;
}

/// <summary>
/// Attempts to retrieve this event as a <see cref="MouseWheelEvent"/>.
/// </summary>
/// <param name="e">The mouse wheel event.</param>
/// <returns><see langword="true"/> if this event holds a <see cref="MouseWheelEvent"/>; otherwise <see langword="false"/></returns>
public bool TryGetValue(out MouseWheelEvent e)
{
if (Type != SDL_EventType.KeyUp)
{
e = default;
return false;
}

e = _wheelEvent;
return true;
}
}
8 changes: 4 additions & 4 deletions src/KappaDuck.Quack/Events/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public static void Enable<TEvent>() where TEvent : IEvent
/// updates the window's recorded size. This lets you selectively drop events as they arrive.
/// </para>
/// <para>
/// Only events that would enter the queue are filtered. Events you <see cref="EventQueue.Push(Event)"/>
/// pass through the filter; events disabled with <see cref="Disable{T}"/> never reach it.
/// Only events that would enter the queue are filtered;
/// events disabled with <see cref="Disable{T}"/> never reach it.
/// </para>
/// <para>
/// The filter may run on a background thread, so keep it fast and thread-safe. The exception is
Expand All @@ -83,7 +83,7 @@ public static EventWatcher Watch(Action<Event> callback)
{
unsafe
{
callback(EventType.Convert(*e));
callback(EventType.Convert(in *e));
}

return true;
Expand All @@ -100,7 +100,7 @@ private static byte OnFilter(void* data, SDL_Event* e)
unsafe
{
SDL_Event native = *e;
Event evt = EventType.Convert(native);
Event evt = EventType.Convert(in native);
return filter(evt) ? (byte)1 : (byte)0;
}
}
Expand Down
54 changes: 8 additions & 46 deletions src/KappaDuck.Quack/Events/EventQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static int Peek(Span<Event> events)
SDLThrowHelper.ThrowIfNegative(count);

for (int i = 0; i < count; i++)
events[i] = EventType.Convert(buffer[i]);
events[i] = EventType.Convert(in buffer[i]);

return count;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public static int Peek<TEvent>(Span<TEvent> events) where TEvent : IEvent
SDLThrowHelper.ThrowIfNegative(count);

for (int i = 0; i < count; i++)
events[i] = (TEvent)EventType.Convert(buffer[i]).Value!;
events[i] = (TEvent)EventType.Convert(in buffer[i]).Value!;

return count;
}
Expand All @@ -116,7 +116,7 @@ public static bool Poll(out Event e)
return false;
}

e = EventType.Convert(native);
e = EventType.Convert(in native);
return true;
}

Expand All @@ -133,44 +133,6 @@ public static bool Poll(out Event e)
/// </remarks>
public static void Pump() => SDL3.PumpEvents();

/// <summary>
/// Adds the specified event to the event queue.
/// </summary>
/// <param name="e">The event to push onto the queue.</param>
/// <returns><see langword="true"/> if the event was pushed; otherwise, <see langword="false"/> if the event was filtered or the event queue being full.</returns>
public static bool Push(Event e)
{
SDL_Event native = EventType.Convert(e);
return SDL3.PushEvent(&native);
}

/// <summary>
/// Adds the events to the event queue.
/// </summary>
/// <remarks>
/// If <paramref name="events"/> is empty, it will return 0.
/// </remarks>
/// <param name="events">The events to push onto the queue.</param>
/// <returns>The number of events successfully pushed onto the queue.</returns>
/// <exception cref="QuackInteropException">Thrown when failing to push events.</exception>
public static int Push(ReadOnlySpan<Event> events)
{
if (events.IsEmpty)
return 0;

Span<SDL_Event> buffer = events.Length <= 32
? stackalloc SDL_Event[events.Length]
: new SDL_Event[events.Length];

for (int i = 0; i < buffer.Length; i++)
buffer[i] = EventType.Convert(events[i]);

int count = SDL3.PeepEvents(buffer, buffer.Length, SDL_EventAction.Add, EventType.None, EventType.End);
SDLThrowHelper.ThrowIfNegative(count);

return count;
}

/// <summary>
/// Runs <paramref name="match"/> over the events currently in the queue, keeping those for
/// which it returns <see langword="true"/> and removing the rest.
Expand All @@ -189,7 +151,7 @@ public static void Retain(Predicate<Event> match)
{
unsafe
{
return match(EventType.Convert(*e));
return match(EventType.Convert(in *e));
}
});
}
Expand Down Expand Up @@ -221,7 +183,7 @@ public static int Retrieve(Span<Event> events)
SDLThrowHelper.ThrowIfNegative(count);

for (int i = 0; i < count; i++)
events[i] = EventType.Convert(buffer[i]);
events[i] = EventType.Convert(in buffer[i]);

return count;
}
Expand Down Expand Up @@ -256,7 +218,7 @@ public static int Retrieve<TEvent>(Span<TEvent> events) where TEvent : IEvent
SDLThrowHelper.ThrowIfNegative(count);

for (int i = 0; i < count; i++)
events[i] = (TEvent)EventType.Convert(buffer[i]).Value!;
events[i] = (TEvent)EventType.Convert(in buffer[i]).Value!;

return count;
}
Expand All @@ -277,13 +239,13 @@ public static bool Wait(out Event e, TimeSpan? timeout = null)
if (!timeout.HasValue || timeout == Timeout.InfiniteTimeSpan)
{
SDL3.WaitEvent(out native);
e = EventType.Convert(native);
e = EventType.Convert(in native);

return e.HasValue;
}

SDL3.WaitEventTimeout(out native, (int)timeout.Value.TotalMilliseconds);
e = EventType.Convert(native);
e = EventType.Convert(in native);

return e.HasValue;
}
Expand Down
Loading