Skip to content

Commit

Permalink
Merge pull request #112 from Doprez/fix-getcamera
Browse files Browse the repository at this point in the history
Thank you @Doprez
  • Loading branch information
VaclavElias authored Feb 19, 2024
2 parents 6cec3ed + 61ce3d9 commit 8378384
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Stride.CommunityToolkit/Engine/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ public static void AddLightDirectionalGizmo(this Entity entity, GraphicsDevice g
return result;
}

/// <summary>
/// Recursively searches for the first component of the specified type in the entity's children.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public static T? GetComponentInChildren<T>(this Entity entity)
{
var result = entity.OfType<T>().FirstOrDefault();

if (result is null)
{
var children = entity.GetChildren();
foreach(var child in children)
{
result = child.GetComponentInChildren<T>();
if (result != null)
{
return result;
}
}
}

return result;
}

/// <summary>
/// Retrieves all components of the specified type from the entity.
/// </summary>
Expand Down Expand Up @@ -116,6 +142,31 @@ public static void Remove(this Entity entity)
return entity.Scene.Entities.FirstOrDefault(w => w.Name == name);
}

/// <summary>
/// Searches for an entity by name within the top-level entities of the current scene.
/// </summary>
/// <param name="parent">The reference entity used to access the scene.</param>
/// <param name="name">The name of the entity to find.</param>
/// <returns>The first entity matching the specified name, or null if no match is found. This search does not include child entities.</returns>
public static Entity? FindEntityRecursive(this Entity parent, string name)
{
var entities = parent.Scene.Entities;
foreach(var entity in entities)
{
if (entity.Name == name)
{
return entity;
}
var child = entity.FindChild(name);
if(child != null && child.Name == name)
{
return child;
}
}

return null;
}

/// <summary>
/// Tries to retrieve a component of type <typeparamref name="T"/> from the given entity.
/// </summary>
Expand Down
47 changes: 47 additions & 0 deletions src/Stride.CommunityToolkit/Engine/SceneExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Stride.Engine;

namespace Stride.CommunityToolkit.Engine;
public static class SceneExtensions
{

/// <summary>
/// Gets the first camera in the scene.
/// </summary>
/// <param name="scene"></param>
/// <returns></returns>
public static CameraComponent? GetCamera(this Scene scene)
{
var entities = scene.Entities;
CameraComponent? camera = null;
foreach (var entity in entities)
{
camera = entity.GetComponentInChildren<CameraComponent>();
if (camera != null)
{
break;
}
}
return camera;
}

/// <summary>
/// Gets the first camera in the scene with the specified <see cref="Entity"/> name.
/// </summary>
/// <param name="scene"></param>
/// <param name="name"></param>
/// <returns></returns>
public static CameraComponent? GetCamera(this Scene scene, string name)
{
var entities = scene.Entities;
CameraComponent? camera = null;
foreach (var entity in entities)
{
camera = entity.GetComponentInChildren<CameraComponent>();
if (camera != null && camera.Entity.Name == name)
{
break;
}
}
return camera;
}
}
65 changes: 65 additions & 0 deletions src/Stride.CommunityToolkit/Engine/ScriptComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static float DeltaTime(this ScriptComponent scriptComponent)
/// </remarks>
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <returns>The <see cref="CameraComponent"/> named "Main", if found; otherwise, null.</returns>
[Obsolete("Use GetGCCamera instead or use Scene.GetCamera")]
public static CameraComponent? GetCamera(this ScriptComponent scriptComponent)
{
var cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;
Expand All @@ -38,6 +39,29 @@ public static float DeltaTime(this ScriptComponent scriptComponent)
return null;
}

/// <summary>
/// Retrieves the camera named "Main" from the <see cref="GraphicsCompositor"/>. Note that the camera might not be available during the first 2-3 frames.
/// </summary>
/// <remarks>
/// Ensure that the GraphicsCompositor is initialized with cameras; otherwise, this method will fail.
/// </remarks>
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <returns>The <see cref="CameraComponent"/> named "Main", if found; otherwise, null.</returns>
public static CameraComponent? GetGCCamera(this ScriptComponent scriptComponent)
{
var cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;

foreach (var sceneCamera in cameraCollection)
{
if (sceneCamera.Name == "Main")
{
return sceneCamera.Camera;
}
}

return null;
}

/// <summary>
/// Retrieves the camera from the <see cref="GraphicsCompositor"/> with the specified name. Note that the camera might not be available during the first 2-3 frames.
/// </summary>
Expand All @@ -47,6 +71,7 @@ public static float DeltaTime(this ScriptComponent scriptComponent)
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <param name="cameraName">The name of the camera to retrieve.</param>
/// <returns>The <see cref="CameraComponent"/> with the given name, if found; otherwise, null.</returns>
[Obsolete("Use GetGCCamera instead or use Scene.GetCamera")]
public static CameraComponent? GetCamera(this ScriptComponent scriptComponent, string cameraName)
{
var cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;
Expand All @@ -62,6 +87,30 @@ public static float DeltaTime(this ScriptComponent scriptComponent)
return null;
}

/// <summary>
/// Retrieves the camera from the <see cref="GraphicsCompositor"/> with the specified name. Note that the camera might not be available during the first 2-3 frames.
/// </summary>
/// <remarks>
/// Ensure that the GraphicsCompositor is initialized with cameras; otherwise, this method will fail.
/// </remarks>
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <param name="cameraName">The name of the camera to retrieve.</param>
/// <returns>The <see cref="CameraComponent"/> with the given name, if found; otherwise, null.</returns>
public static CameraComponent? GetGCCamera(this ScriptComponent scriptComponent, string cameraName)
{
var cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;

foreach (var sceneCamera in cameraCollection)
{
if (sceneCamera.Name == cameraName)
{
return sceneCamera.Camera;
}
}

return null;
}

/// <summary>
/// Gets the first camera from the <see cref="GraphicsCompositor"/>. Note that the camera might not be available during the first 2-3 frames.
/// </summary>
Expand All @@ -70,10 +119,26 @@ public static float DeltaTime(this ScriptComponent scriptComponent)
/// </remarks>
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <returns>The <see cref="CameraComponent"/> with the given name, if found; otherwise, null.</returns>
[Obsolete("Use GetFirstGCCamera instead or use Scene.GetCamera")]
public static CameraComponent GetFirstCamera(this ScriptComponent scriptComponent)
{
SceneCameraSlotCollection cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;

return cameraCollection[0].Camera;
}

/// <summary>
/// Gets the first camera from the <see cref="GraphicsCompositor"/>. Note that the camera might not be available during the first 2-3 frames.
/// </summary>
/// <remarks>
/// Ensure that the GraphicsCompositor is initialized with cameras; otherwise, this method will fail.
/// </remarks>
/// <param name="scriptComponent">The script component from which to access the GraphicsCompositor.</param>
/// <returns>The <see cref="CameraComponent"/> with the given name, if found; otherwise, null.</returns>
public static CameraComponent GetFirstGCCamera(this ScriptComponent scriptComponent)
{
SceneCameraSlotCollection cameraCollection = scriptComponent.SceneSystem.GraphicsCompositor.Cameras;

return cameraCollection[0].Camera;
}
}

0 comments on commit 8378384

Please sign in to comment.