Skip to content

Commit

Permalink
UI event setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomvas committed May 24, 2024
1 parent f8f5725 commit ac91489
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Basalt.Raylib/Graphics/RaylibGraphicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ public unsafe void Render()

}

eventBus?.TriggerEvent(BasaltConstants.UiRenderEventKey);

EndDrawing();
//----------------------------------------------------------------------------------
}
Expand Down
15 changes: 15 additions & 0 deletions Basalt/Common/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public virtual void OnPhysicsUpdate()
public virtual void OnCollision(Collider other)
{
}
public virtual void OnUIRender()
{
}

internal void onDestroy()
{
Expand Down Expand Up @@ -99,6 +102,13 @@ public void OnRenderEvent(object? sender, EventArgs args)
OnRender();
}

public void OnUIRenderEvent(object? sender, EventArgs args)
{
if (Entity.Enabled && Enabled)
OnUIRender();
}


private void SubscribeToEvents()
{
var eventbus = Engine.Instance.GetEngineComponent<IEventBus>()!;
Expand All @@ -113,6 +123,10 @@ private void SubscribeToEvents()
// Check if OnPhysicsUpdate was overriden
if (type.GetMethod(nameof(OnPhysicsUpdate)).DeclaringType != typeof(Component))
eventbus.Subscribe(BasaltConstants.PhysicsUpdateEventKey, OnPhysicsUpdateEvent);

// Check if OnUIRender was overriden
if (type.GetMethod(nameof(OnUIRender)).DeclaringType != typeof(Component))
eventbus.Subscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent);
}

private void UnsubscribeFromEvents()
Expand All @@ -122,6 +136,7 @@ private void UnsubscribeFromEvents()
eventbus.Unsubscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent);
eventbus.Unsubscribe(BasaltConstants.RenderEventKey, OnRenderEvent);
eventbus.Unsubscribe(BasaltConstants.PhysicsUpdateEventKey, OnPhysicsUpdateEvent);
eventbus.Unsubscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent);
}


Expand Down
1 change: 1 addition & 0 deletions Basalt/Common/Utils/BasaltConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public static class BasaltConstants
public const string UpdateEventKey = "update";
public const string PhysicsUpdateEventKey = "physics_update";
public const string RenderEventKey = "render";
public const string UiRenderEventKey = "ui_render";
}
}

0 comments on commit ac91489

Please sign in to comment.