Skip to content

Commit

Permalink
Add abstract UIComponent class
Browse files Browse the repository at this point in the history
This will serve as the base class for any component that wants to draw UI. Still works as a normal component
  • Loading branch information
thiagomvas committed May 24, 2024
1 parent ac91489 commit de7f029
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
17 changes: 2 additions & 15 deletions Basalt/Common/Components/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public virtual void OnPhysicsUpdate()
public virtual void OnCollision(Collider other)
{
}
public virtual void OnUIRender()
{
}

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

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


private void SubscribeToEvents()
private virtual protected void SubscribeToEvents()
{
var eventbus = Engine.Instance.GetEngineComponent<IEventBus>()!;
Type type = this.GetType();
Expand All @@ -123,20 +115,15 @@ 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()
private virtual protected void UnsubscribeFromEvents()
{
var eventbus = Engine.Instance.GetEngineComponent<IEventBus>()!;
eventbus.Unsubscribe(BasaltConstants.StartEventKey, OnStartEvent);
eventbus.Unsubscribe(BasaltConstants.UpdateEventKey, OnUpdateEvent);
eventbus.Unsubscribe(BasaltConstants.RenderEventKey, OnRenderEvent);
eventbus.Unsubscribe(BasaltConstants.PhysicsUpdateEventKey, OnPhysicsUpdateEvent);
eventbus.Unsubscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent);
}


Expand Down
42 changes: 42 additions & 0 deletions Basalt/Common/Components/UIComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Basalt.Common.Entities;
using Basalt.Common.Utils;
using Basalt.Core.Common.Abstractions.Engine;
using Basalt.Types;
using System.Numerics;

namespace Basalt.Common.Components
{
public abstract class UIComponent : Component
{
public UIPivot Pivot { get; set; }
public Vector3 Offset { get; set; }
public Vector3 Scale { get; set; }
protected UIComponent(Entity entity) : base(entity)
{
}

public virtual void OnUIRender()
{
}

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

private protected override void SubscribeToEvents()
{
base.SubscribeToEvents();
Engine.Instance.GetEngineComponent<IEventBus>()!.Subscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent);
}

private protected override void UnsubscribeFromEvents()
{
base.UnsubscribeFromEvents();
Engine.Instance.GetEngineComponent<IEventBus>()!.Unsubscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent);
}


}
}
15 changes: 15 additions & 0 deletions Basalt/Types/UIPivot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Basalt.Types
{
public enum UIPivot
{
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottomCenter,
BottomRight
}
}

0 comments on commit de7f029

Please sign in to comment.