From de7f02949251efcc93c208cfb2e4c797db7de6d5 Mon Sep 17 00:00:00 2001 From: Thiago Menezes Date: Thu, 23 May 2024 23:19:21 -0300 Subject: [PATCH] Add abstract UIComponent class This will serve as the base class for any component that wants to draw UI. Still works as a normal component --- Basalt/Common/Components/Component.cs | 17 ++-------- Basalt/Common/Components/UIComponent.cs | 42 +++++++++++++++++++++++++ Basalt/Types/UIPivot.cs | 15 +++++++++ 3 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 Basalt/Common/Components/UIComponent.cs create mode 100644 Basalt/Types/UIPivot.cs diff --git a/Basalt/Common/Components/Component.cs b/Basalt/Common/Components/Component.cs index 5bb2bbc..3a725bf 100644 --- a/Basalt/Common/Components/Component.cs +++ b/Basalt/Common/Components/Component.cs @@ -59,9 +59,6 @@ public virtual void OnPhysicsUpdate() public virtual void OnCollision(Collider other) { } - public virtual void OnUIRender() - { - } internal void onDestroy() { @@ -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()!; Type type = this.GetType(); @@ -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()!; 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); } diff --git a/Basalt/Common/Components/UIComponent.cs b/Basalt/Common/Components/UIComponent.cs new file mode 100644 index 0000000..a55b515 --- /dev/null +++ b/Basalt/Common/Components/UIComponent.cs @@ -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()!.Subscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent); + } + + private protected override void UnsubscribeFromEvents() + { + base.UnsubscribeFromEvents(); + Engine.Instance.GetEngineComponent()!.Unsubscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent); + } + + + } +} diff --git a/Basalt/Types/UIPivot.cs b/Basalt/Types/UIPivot.cs new file mode 100644 index 0000000..a3361e0 --- /dev/null +++ b/Basalt/Types/UIPivot.cs @@ -0,0 +1,15 @@ +namespace Basalt.Types +{ + public enum UIPivot + { + TopLeft, + TopCenter, + TopRight, + MiddleLeft, + MiddleCenter, + MiddleRight, + BottomLeft, + BottomCenter, + BottomRight + } +}