mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
UI Tweaks, ChildEnumerator, ComponentAdded-Handlers
This commit is contained in:
@@ -107,7 +107,7 @@ namespace GlitchyEngine
|
||||
class ImGuiSettings
|
||||
{
|
||||
[Setting("UI", "Font Size"), BonInclude]
|
||||
public int32 FontSize = 16;
|
||||
public int32 FontSize = 14;
|
||||
|
||||
[Setting("UI", "Font name"), BonInclude]
|
||||
public readonly String FontName = new .("Fonts/CascadiaCode.ttf") ~ delete _;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using internal GlitchyEngine.World;
|
||||
|
||||
@@ -24,13 +25,53 @@ namespace GlitchyEngine.World
|
||||
_scene = scene;
|
||||
}
|
||||
|
||||
public ChildEnumerator EnumerateChildren => .(this);
|
||||
|
||||
public bool IsValid => _entity.IsValid;
|
||||
|
||||
public Entity? Parent
|
||||
{
|
||||
get
|
||||
{
|
||||
var cmp = GetComponent<TransformComponent>();
|
||||
|
||||
if (cmp.Parent == .InvalidEntity)
|
||||
return null;
|
||||
|
||||
return .(cmp.Parent, _scene);
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
var cmp = GetComponent<TransformComponent>();
|
||||
cmp.Parent = .InvalidEntity;
|
||||
}
|
||||
else
|
||||
{
|
||||
Entity parent = value.Value;
|
||||
|
||||
if (parent.Scene != _scene)
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var cmp = GetComponent<TransformComponent>();
|
||||
cmp.Parent = parent._entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public T* AddComponent<T>(T value = T()) where T: struct, new
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component.");
|
||||
|
||||
return _scene._ecsWorld.AssignComponent<T>(_entity, value);
|
||||
T* component = _scene._ecsWorld.AssignComponent<T>(_entity, value);
|
||||
|
||||
_scene.[Friend]OnComponentAdded(this, typeof(T), component);
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
public T* GetComponent<T>() where T: struct, new
|
||||
@@ -51,5 +92,41 @@ namespace GlitchyEngine.World
|
||||
|
||||
_scene._ecsWorld.RemoveComponent<T>(_entity);
|
||||
}
|
||||
|
||||
public struct ChildEnumerator : IEnumerator<Entity>, IDisposable
|
||||
{
|
||||
private WorldEnumerator<TransformComponent> _transformEnum;
|
||||
|
||||
private EcsEntity _entity;
|
||||
private Entity _currentChild;
|
||||
|
||||
public this(Entity entity)
|
||||
{
|
||||
_entity = entity.Handle;
|
||||
_transformEnum = entity.Scene._ecsWorld.Enumerate<TransformComponent>();
|
||||
_currentChild = .(.InvalidEntity, entity.Scene);
|
||||
}
|
||||
|
||||
public Entity Current => _currentChild;
|
||||
|
||||
public Result<Entity> GetNext() mut
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
(EcsEntity entity, TransformComponent* transform) = Try!(_transformEnum.GetNext());
|
||||
|
||||
if (transform.Parent == _entity)
|
||||
{
|
||||
_currentChild.[Friend]_entity = entity;
|
||||
return .Ok(_currentChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_transformEnum.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.Renderer;
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
@@ -9,6 +10,8 @@ namespace GlitchyEngine.World
|
||||
class Scene
|
||||
{
|
||||
internal EcsWorld _ecsWorld = new .() ~ delete _;
|
||||
|
||||
private Dictionary<Type, function void(Entity entity, Type componentType, void* component)> _onComponentAddedHandlers = new .() ~ delete _;
|
||||
|
||||
public this()
|
||||
{
|
||||
@@ -20,6 +23,11 @@ namespace GlitchyEngine.World
|
||||
v.Sprite = new Texture2D("Textures/rocket.png");
|
||||
v.Sprite.SamplerState = SamplerStateManager.PointClamp;
|
||||
|
||||
_onComponentAddedHandlers.Add(typeof(CameraComponent), (e, t, c) => {
|
||||
CameraComponent* cameraComponent = (.)c;
|
||||
|
||||
cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight);
|
||||
});
|
||||
}
|
||||
|
||||
public ~this()
|
||||
@@ -68,6 +76,7 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new Entity with the given name.
|
||||
public Entity CreateEntity(String name = "")
|
||||
{
|
||||
Entity entity = Entity(_ecsWorld.NewEntity(), this);
|
||||
@@ -79,8 +88,20 @@ namespace GlitchyEngine.World
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void DestroyEntity(Entity entity)
|
||||
/** Deletes the given entity.
|
||||
* @param entity The entity to delete.
|
||||
* @param destroyChildren If set to true all children of entity will be destroyed.
|
||||
*/
|
||||
public void DestroyEntity(Entity entity, bool destroyChildren = false)
|
||||
{
|
||||
if (destroyChildren)
|
||||
{
|
||||
for (Entity child in entity.EnumerateChildren)
|
||||
{
|
||||
DestroyEntity(child, true);
|
||||
}
|
||||
}
|
||||
|
||||
_ecsWorld.RemoveEntity(entity.Handle);
|
||||
}
|
||||
|
||||
@@ -100,5 +121,13 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnComponentAdded(Entity entity, Type componentType, void* component)
|
||||
{
|
||||
if (_onComponentAddedHandlers.TryGetValue(componentType, let handler))
|
||||
{
|
||||
handler(entity, componentType, component);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user