Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event Bus rewriting #12

Merged
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Event/MainWindowClosedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Event;

public readonly struct MainWindowClosedEvent(WindowRegistration registration)
public readonly struct MainWindowClosedEvent(WindowRegistration registration) : IEventArgs
{
public readonly WindowRegistration Registration = registration;
}
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Event/WindowClosed.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Event;

public readonly struct WindowClosedEvent(WindowRegistration registration)
public readonly struct WindowClosedEvent(WindowRegistration registration) : IEventArgs
{
public readonly WindowRegistration Registration = registration;
}
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Event/WindowFocusChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Event;

public readonly record struct WindowFocusChangedEvent(WindowRegistration Registration, bool Focused);
public readonly record struct WindowFocusChangedEvent(WindowRegistration Registration, bool Focused) : IEventArgs;
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void OnLoad()
_logger.EngineInfo("Loaded");
}

private void OnFrameUpdate(UpdateFrameEvent args)
private void OnFrameUpdate(ref UpdateFrameEvent args)
{
#if DEBUG
_windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime} | cPos: {_cameraManager.MainCamera?.Position ?? null} | cRot: {_cameraManager.MainCamera?.Rotation ?? null}");
Expand All @@ -74,7 +74,7 @@ private void OnFrameUpdate(UpdateFrameEvent args)
_cameraManager.UpdateInput(_cameraManager.MainCamera, args.DeltaSeconds);
}

private void OnFrameRender(RenderFrameEvent args)
private void OnFrameRender(ref RenderFrameEvent args)
{
BatchClear();

Expand Down
6 changes: 3 additions & 3 deletions Hypercube.Client/Graphics/Rendering/Renderer.Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public void DestroyWindow(WindowRegistration registration)

public void CloseWindow(WindowRegistration registration)
{
_eventBus.Invoke(new WindowClosedEvent(registration));
_eventBus.Raise(new WindowClosedEvent(registration));

if (registration.Id == _mainWindowId)
{
_eventBus.Invoke(new MainWindowClosedEvent(registration));
_eventBus.Raise(new MainWindowClosedEvent(registration));
return;
}

Expand Down Expand Up @@ -84,6 +84,6 @@ private bool InitMainWindow(ContextInfo? context, WindowCreateSettings settings)

public void OnFocusChanged(WindowRegistration window, bool focused)
{
_eventBus.Invoke(new WindowFocusChangedEvent(window, focused));
_eventBus.Raise(new WindowFocusChangedEvent(window, focused));
}
}
15 changes: 8 additions & 7 deletions Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Hypercube.Client.Graphics.Windows.Manager;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Resources.Manager;
using Hypercube.Shared.Runtimes.Event;
Expand All @@ -17,7 +18,7 @@

namespace Hypercube.Client.Graphics.Rendering;

public sealed partial class Renderer : IRenderer, IPostInject
public sealed partial class Renderer : IRenderer, IPostInject, IEventSubscriber
{
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly ITextureManager _textureManager = default!;
Expand Down Expand Up @@ -68,19 +69,19 @@ public sealed partial class Renderer : IRenderer, IPostInject

public void PostInject()
{
_eventBus.Subscribe<RuntimeInitializationEvent>(OnInitialization);
_eventBus.Subscribe<RuntimeStartupEvent>(OnStartup);
_eventBus.Subscribe<UpdateFrameEvent>(OnFrameUpdate);
_eventBus.Subscribe<RenderFrameEvent>(OnFrameRender);
_eventBus.Subscribe<RuntimeInitializationEvent>(this, OnInitialization);
_eventBus.Subscribe<RuntimeStartupEvent>(this, OnStartup);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnFrameUpdate);
_eventBus.Subscribe<RenderFrameEvent>(this, OnFrameRender);
}

private void OnInitialization(RuntimeInitializationEvent args)
private void OnInitialization(ref RuntimeInitializationEvent args)
{
_windowManager = CreateWindowManager();
_bindingsContext = new BindingsContext(_windowManager);
}

private void OnStartup(RuntimeStartupEvent args)
private void OnStartup(ref RuntimeStartupEvent args)
{
_currentThread = Thread.CurrentThread;
_logger.EngineInfo($"Working thread {_currentThread.Name}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ private void OnWindowFocusChanged(Window* window, bool focused)

_renderer.OnFocusChanged(registration, focused);
}
private void OnWindowFocused(Window* window, bool focused)
{
if (!TryGetWindow(window, out var registration))
return;

_renderer.OnFocusChanged(registration, focused);
}

}
8 changes: 4 additions & 4 deletions Hypercube.Client/Runtimes/Loop/RuntimeLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public void Run()
_timing.StartFrame();

var deltaTime = (float)_timing.RealFrameTime.TotalSeconds;
_eventBus.Invoke(new InputFrameEvent(deltaTime));
_eventBus.Invoke(new TickFrameEvent(deltaTime));
_eventBus.Invoke(new UpdateFrameEvent(deltaTime));
_eventBus.Invoke(new RenderFrameEvent(deltaTime));
_eventBus.Raise(new InputFrameEvent(deltaTime));
_eventBus.Raise(new TickFrameEvent(deltaTime));
_eventBus.Raise(new UpdateFrameEvent(deltaTime));
_eventBus.Raise(new RenderFrameEvent(deltaTime));
}
}

Expand Down
17 changes: 8 additions & 9 deletions Hypercube.Client/Runtimes/Runtime.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Hypercube.Client.Graphics;
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Runtimes.Loop;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Runtimes.Event;

namespace Hypercube.Client.Runtimes;

public sealed partial class Runtime(DependenciesContainer dependenciesContainer) : IPostInject
public sealed partial class Runtime(DependenciesContainer dependenciesContainer) : IPostInject, IEventSubscriber
{
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly IRuntimeLoop _loop = default!;
Expand All @@ -18,7 +17,7 @@ public sealed partial class Runtime(DependenciesContainer dependenciesContainer)

public void PostInject()
{
_eventBus.Subscribe<MainWindowClosedEvent>(OnMainWindowClosed);
_eventBus.Subscribe<MainWindowClosedEvent>(this, OnMainWindowClosed);
}

/// <summary>
Expand All @@ -43,24 +42,24 @@ private void Shutdown(string? reason = null)
reason = reason is null ? "Shutting down" : $"Shutting down, reason: {reason}";

_logger.EngineInfo(reason);
_eventBus.Invoke(new RuntimeShutdownEvent(reason));
_eventBus.Raise(new RuntimeShutdownEvent(reason));
_loop.Shutdown();
}

private void RunLoop()
{
_logger.EngineInfo("Startup");
_eventBus.Invoke(new RuntimeStartupEvent());
_eventBus.Raise(new RuntimeStartupEvent());
_loop.Run();
}

private void Initialize()
{
_logger.EngineInfo("Initialize");
_eventBus.Invoke(new RuntimeInitializationEvent());
_eventBus.Raise(new RuntimeInitializationEvent());
}

private void OnMainWindowClosed(MainWindowClosedEvent obj)
private void OnMainWindowClosed(ref MainWindowClosedEvent obj)
{
Shutdown("Main window closed");
}
Expand Down
22 changes: 18 additions & 4 deletions Hypercube.Shared/Entities/Realisation/EventBus/EntitiesEventBus.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
namespace Hypercube.Shared.Entities.Realisation.EventBus;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.EventBus.Handlers;

namespace Hypercube.Shared.Entities.Realisation.EventBus;

public sealed class EntitiesEventBus : IEntitiesEventBus
{
public void Subscribe<T>(Action<T> callback)
public void Unsubscribe<T>(IEventSubscriber subscriber) where T : IEventArgs
{
throw new NotImplementedException();
}

public void Subscribe<T>(IEventSubscriber subscriber, EventRefHandler<T> refHandler) where T : IEventArgs
{
throw new NotImplementedException();
}

public void Raise(IEventArgs receiver)
{
throw new NotImplementedException();
}

public void Unsubscribe<T>(Action<T> callback)
public void Raise<T>(ref T receiver) where T : IEventArgs
{
throw new NotImplementedException();
}

public void Invoke<T>(T signal)
public void Raise<T>(T receiver) where T : IEventArgs
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hypercube.Shared.Entities.Realisation.Components;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Shared.Entities.Realisation.Events;

public readonly record struct ComponentAdded(EntityUid EntityUid, IComponent Component);
public readonly record struct ComponentAdded(EntityUid EntityUid, IComponent Component) : IEventArgs;
6 changes: 4 additions & 2 deletions Hypercube.Shared/Entities/Realisation/Events/EntityAdded.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace Hypercube.Shared.Entities.Realisation.Events;
using Hypercube.Shared.EventBus.Events;

public readonly record struct EntityAdded(EntityUid EntityUid);
namespace Hypercube.Shared.Entities.Realisation.Events;

public readonly record struct EntityAdded(EntityUid EntityUid) : IEventArgs;
6 changes: 4 additions & 2 deletions Hypercube.Shared/Entities/Realisation/Events/EntityRemoved.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace Hypercube.Shared.Entities.Realisation.Events;
using Hypercube.Shared.EventBus.Events;

public readonly record struct EntityRemoved(EntityUid EntityUid);
namespace Hypercube.Shared.Entities.Realisation.Events;

public readonly record struct EntityRemoved(EntityUid EntityUid) : IEventArgs;
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
using Hypercube.Shared.Entities.Realisation.Components;
using Hypercube.Shared.Entities.Realisation.Events;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Runtimes.Event;
using Hypercube.Shared.Utilities.Helpers;

namespace Hypercube.Shared.Entities.Realisation.Manager;

public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostInject
public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostInject, IEventSubscriber
{
private static readonly Type BaseComponentType = typeof(IComponent);

Expand All @@ -20,10 +21,10 @@ public sealed class EntitiesComponentManager : IEntitiesComponentManager, IPostI

public void PostInject()
{
_eventBus.Subscribe<RuntimeInitializationEvent>(OnInitialized);
_eventBus.Subscribe<RuntimeInitializationEvent>(this, OnInitialized);
}

private void OnInitialized(RuntimeInitializationEvent args)
private void OnInitialized(ref RuntimeInitializationEvent args)
{
_components = ReflectionHelper.GetAllInstantiableSubclassOf(BaseComponentType);

Expand Down Expand Up @@ -72,7 +73,7 @@ private object AddComponent(EntityUid entityUid, Type type)
var instance = (IComponent)constructor.Invoke(Array.Empty<object>()) ?? throw new NullReferenceException();

components.Add(entityUid, instance);
_eventBus.Invoke(new ComponentAdded(entityUid, instance));
_eventBus.Raise(new ComponentAdded(entityUid, instance));

return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public EntityUid Create(string name, SceneCoordinates coordinates)
var transformComponent = _entitiesComponentManager.AddComponent<TransformComponent>(newEntity);

_entities.Add(newEntity);
_eventBus.Invoke(new EntityAdded(newEntity));
_eventBus.Raise(new EntityAdded(newEntity));

return newEntity;
}

public void Delete(EntityUid entityUid)
{
_entities.Remove(entityUid);
_eventBus.Invoke(new EntityRemoved(entityUid));
_eventBus.Raise(new EntityRemoved(entityUid));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Entities.Realisation.Systems;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.EventBus.Events;
using Hypercube.Shared.Runtimes.Event;
using Hypercube.Shared.Runtimes.Loop.Event;
using Hypercube.Shared.Utilities.Helpers;

namespace Hypercube.Shared.Entities.Realisation.Manager;

public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject
public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject, IEventSubscriber
{
[Dependency] private readonly IEventBus _eventBus = default!;

Expand All @@ -19,14 +20,14 @@ public class EntitiesSystemManager : IEntitiesSystemManager, IPostInject

public void PostInject()
{
_eventBus.Subscribe<RuntimeInitializationEvent>(OnInitialization);
_eventBus.Subscribe<RuntimeStartupEvent>(OnStartup);
_eventBus.Subscribe<RuntimeInitializationEvent>(this, OnInitialization);
_eventBus.Subscribe<RuntimeStartupEvent>(this, OnStartup);

_eventBus.Subscribe<UpdateFrameEvent>(OnFrameUpdate);
_eventBus.Subscribe<RuntimeShutdownEvent>(OnShutdown);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnFrameUpdate);
_eventBus.Subscribe<RuntimeShutdownEvent>(this, OnShutdown);
}

private void OnInitialization(RuntimeInitializationEvent args)
private void OnInitialization(ref RuntimeInitializationEvent args)
{
// Auto creating all systems
var types = GetAllSystemTypes();
Expand All @@ -45,23 +46,23 @@ private void OnInitialization(RuntimeInitializationEvent args)
_system = system.ToFrozenSet();
}

private void OnStartup(RuntimeStartupEvent args)
private void OnStartup(ref RuntimeStartupEvent args)
{
foreach (var instance in _system)
{
instance.Initialize();
}
}

private void OnFrameUpdate(UpdateFrameEvent args)
private void OnFrameUpdate(ref UpdateFrameEvent args)
{
foreach (var instance in _system)
{
instance.FrameUpdate(args);
}
}

private void OnShutdown(RuntimeShutdownEvent args)
private void OnShutdown(ref RuntimeShutdownEvent args)
{
foreach (var instance in _system)
{
Expand Down
Loading
Loading