| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | |
| | | 6 | | using static SDL2.SDL_error; |
| | | 7 | | using static SDL2.SDL_events; |
| | | 8 | | using static SDL2.SDL_video; |
| | | 9 | | |
| | | 10 | | namespace Imagini |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents an event manager. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class EventManager |
| | | 16 | | { |
| | | 17 | | private const uint GLOBAL_QUEUE_ID = 0; |
| | | 18 | | |
| | 1 | 19 | | static EventManager() => Lifecycle.TryInitialize(); |
| | 0 | 20 | | private static Dictionary<uint, EventQueue> _queues = |
| | 0 | 21 | | new Dictionary<uint, EventQueue>() { |
| | 0 | 22 | | { GLOBAL_QUEUE_ID, new EventQueue() } |
| | 0 | 23 | | }; |
| | | 24 | | |
| | | 25 | | internal static EventQueue CreateQueueFor(Window window) |
| | | 26 | | { |
| | 69 | 27 | | var id = window.ID; |
| | 69 | 28 | | if (_queues.ContainsKey(id)) return _queues[id]; |
| | 69 | 29 | | var queue = new EventQueue(); |
| | 69 | 30 | | _queues.Add(id, queue); |
| | 69 | 31 | | return queue; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static void DeleteQueueFor(Window window) => |
| | 69 | 35 | | _queues.Remove(window.ID); |
| | | 36 | | |
| | 102 | 37 | | internal static EventQueue GlobalQueue => _queues[GLOBAL_QUEUE_ID]; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gathers all available events and distributes them to the |
| | | 41 | | /// corresponding queues. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="suppressGlobalProcessing"> |
| | | 44 | | /// If true, global event queue will not be processed after calling this method. |
| | | 45 | | /// </param> |
| | | 46 | | public unsafe static void Poll(bool suppressGlobalProcessing = false) |
| | | 47 | | { |
| | 440 | 48 | | while (SDL_PollEvent(out SDL_Event @event) != 0) |
| | | 49 | | { |
| | 338 | 50 | | byte* data = (byte*)&@event; |
| | 338 | 51 | | switch ((SDL_EventType)@event.type) |
| | | 52 | | { |
| | | 53 | | // uint32 type, uint32 timestamp, uint32 windowID |
| | | 54 | | case SDL_EventType.SDL_WINDOWEVENT: |
| | | 55 | | case SDL_EventType.SDL_KEYDOWN: |
| | | 56 | | case SDL_EventType.SDL_KEYUP: |
| | | 57 | | case SDL_EventType.SDL_TEXTEDITING: |
| | | 58 | | case SDL_EventType.SDL_TEXTINPUT: |
| | | 59 | | case SDL_EventType.SDL_MOUSEMOTION: |
| | | 60 | | case SDL_EventType.SDL_MOUSEBUTTONDOWN: |
| | | 61 | | case SDL_EventType.SDL_MOUSEBUTTONUP: |
| | | 62 | | case SDL_EventType.SDL_MOUSEWHEEL: |
| | | 63 | | case SDL_EventType.SDL_USEREVENT: |
| | 320 | 64 | | var windowID = (uint)*(data + 8); |
| | 320 | 65 | | PushTo(windowID, @event); |
| | 320 | 66 | | break; |
| | | 67 | | case SDL_EventType.SDL_JOYAXISMOTION: |
| | | 68 | | case SDL_EventType.SDL_JOYBALLMOTION: |
| | | 69 | | case SDL_EventType.SDL_JOYHATMOTION: |
| | | 70 | | case SDL_EventType.SDL_JOYBUTTONDOWN: |
| | | 71 | | case SDL_EventType.SDL_JOYBUTTONUP: |
| | | 72 | | case SDL_EventType.SDL_CONTROLLERAXISMOTION: |
| | | 73 | | case SDL_EventType.SDL_CONTROLLERBUTTONDOWN: |
| | | 74 | | case SDL_EventType.SDL_CONTROLLERBUTTONUP: |
| | | 75 | | case SDL_EventType.SDL_FINGERDOWN: |
| | | 76 | | case SDL_EventType.SDL_FINGERUP: |
| | | 77 | | case SDL_EventType.SDL_FINGERMOTION: |
| | | 78 | | case SDL_EventType.SDL_DOLLARGESTURE: |
| | | 79 | | case SDL_EventType.SDL_DOLLARRECORD: |
| | | 80 | | case SDL_EventType.SDL_MULTIGESTURE: |
| | 11 | 81 | | PushToCurrent(@event); |
| | 11 | 82 | | break; |
| | | 83 | | case SDL_EventType.SDL_DROPFILE: |
| | 0 | 84 | | windowID = (uint)*(data + 8 + IntPtr.Size); |
| | 0 | 85 | | PushTo(windowID, @event); |
| | 0 | 86 | | break; |
| | | 87 | | default: |
| | 7 | 88 | | PushToGlobal(@event); |
| | 7 | 89 | | break; |
| | | 90 | | } |
| | | 91 | | } |
| | 102 | 92 | | if (!suppressGlobalProcessing) |
| | 101 | 93 | | GlobalQueue.ProcessAll(Events.Global); |
| | 102 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Pushes an event onto the poll queue. |
| | | 98 | | /// </summary> |
| | | 99 | | public static void Push(CommonEventArgs e) |
| | | 100 | | { |
| | 26 | 101 | | var _e = e.AsEvent(); |
| | 26 | 102 | | if (SDL_PushEvent(ref _e) < 0) |
| | 0 | 103 | | throw new ImaginiException($"Unable to push event: {SDL_GetError()}"); |
| | 26 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Updates the event queue and internal input device state. |
| | | 108 | | /// </summary> |
| | 6 | 109 | | public static void Pump() => SDL_PumpEvents(); |
| | | 110 | | |
| | | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 112 | | private static void PushTo(uint windowId, SDL_Event @event) |
| | | 113 | | { |
| | 331 | 114 | | if (_queues.ContainsKey(windowId)) |
| | 82 | 115 | | _queues[windowId].Enqueue(@event); |
| | 331 | 116 | | } |
| | | 117 | | |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 119 | | private static void PushToCurrent(SDL_Event @event) |
| | | 120 | | { |
| | 11 | 121 | | var current = Window.Current; |
| | 11 | 122 | | if (current == null) return; |
| | 11 | 123 | | PushTo(current.ID, @event); |
| | 11 | 124 | | } |
| | | 125 | | |
| | | 126 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 127 | | private static void PushToGlobal(SDL_Event @event) => |
| | 7 | 128 | | _queues[GLOBAL_QUEUE_ID].Enqueue(@event); |
| | | 129 | | |
| | | 130 | | internal class EventQueue |
| | | 131 | | { |
| | 70 | 132 | | private Queue<SDL_Event> _events = new Queue<SDL_Event>(32); |
| | 4 | 133 | | public IReadOnlyCollection<SDL_Event> Events => _events; |
| | | 134 | | |
| | 89 | 135 | | internal void Enqueue(SDL_Event @event) => _events.Enqueue(@event); |
| | | 136 | | |
| | 76 | 137 | | public void ProcessNext(Events events) => events.Process(_events.Dequeue()); |
| | | 138 | | |
| | | 139 | | public void ProcessAll(Events events) |
| | | 140 | | { |
| | 276 | 141 | | while (_events.Count > 0) |
| | 76 | 142 | | ProcessNext(events); |
| | 200 | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |