Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Scene View Document #190

Merged
merged 11 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// <copyright file="ServiceCollectionExtensions.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Extensions;

using System;
using System.Diagnostics.CodeAnalysis;
using FinalEngine.Editor.Common.Services.Factories;
using FinalEngine.Editor.Common.Services.Rendering;
using Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Provides extension methods for an <see cref="IServiceCollection"/>.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Extension Methods")]
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds all common services to specified <paramref name="services"/> collection.
/// </summary>
/// <param name="services">
/// The services collection.
/// </param>
/// <returns>
/// The <paramref name="services"/> collection.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="services"/> parameter cannot be null.
/// </exception>
public static IServiceCollection AddCommon(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

services.AddSingleton<ISceneRenderer, SceneRenderer>();

return services;
}

/// <summary>
/// Adds a factory that creates an instance of type <typeparamref name="TViewModel"/> to the specified <paramref name="services"/> collection.
/// </summary>
/// <typeparam name="TViewModel">
/// The type of instance to create.
/// </typeparam>
/// <param name="services">
/// The services collection.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="services"/> parameter cannot be null.
/// </exception>
public static void AddFactory<TViewModel>(this IServiceCollection services)
where TViewModel : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

services.AddTransient<TViewModel>();
services.AddSingleton<Func<TViewModel>>(x =>
{
return () =>
{
return x.GetRequiredService<TViewModel>();
};
});

services.AddSingleton<IFactory<TViewModel>, Factory<TViewModel>>();
}
}
41 changes: 41 additions & 0 deletions FinalEngine.Editor.Common/FinalEngine.Editor.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<AnalysisMode>All</AnalysisMode>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Platforms>x64</Platforms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<NoWarn>CA1848;CA2254</NoWarn>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="..\Styling\StyleCop\Other\stylecop.json" Link="stylecop.json" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SharedAssemblyInfo.cs" Link="Properties\SharedAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FinalEngine.Rendering\FinalEngine.Rendering.csproj" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions FinalEngine.Editor.Common/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// <copyright file="AssemblyInfo.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]
[assembly: AssemblyTitle("FinalEngine.Editor.Common")]
[assembly: AssemblyDescription("A common services library for the Final Engine editor.")]
[assembly: Guid("D8E94A1E-3718-429D-A3F7-40ADAADA636D")]
42 changes: 42 additions & 0 deletions FinalEngine.Editor.Common/Services/Factories/Factory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <copyright file="Factory.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Factories;

using System;

/// <summary>
/// Provides a standard implementation of an <see cref="IFactory{T}"/>.
/// </summary>
/// <typeparam name="T">
/// The type of instance to create.
/// </typeparam>
/// <seealso cref="IFactory{T}"/>
public sealed class Factory<T> : IFactory<T>
{
/// <summary>
/// The function used to create the instance.
/// </summary>
private readonly Func<T> factory;

/// <summary>
/// Initializes a new instance of the <see cref="Factory{T}"/> class.
/// </summary>
/// <param name="factory">
/// The function used to create the instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="factory"/> parameter cannot be null.
/// </exception>
public Factory(Func<T> factory)
{
this.factory = factory ?? throw new ArgumentNullException(nameof(factory));
}

/// <inheritdoc/>
public T Create()
{
return this.factory();
}
}
22 changes: 22 additions & 0 deletions FinalEngine.Editor.Common/Services/Factories/IFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="IFactory.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Factories;

/// <summary>
/// Defines an interface that provides a method to create an instance of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">
/// The type of instance to create.
/// </typeparam>
public interface IFactory<out T>
{
/// <summary>
/// Creates an instance of type <typeparamref name="T"/>.
/// </summary>
/// <returns>
/// The newly created instance of type <typeparamref name="T"/>.
/// </returns>
T Create();
}
27 changes: 27 additions & 0 deletions FinalEngine.Editor.Common/Services/Rendering/ISceneRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <copyright file="ISceneRenderer.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Rendering;

/// <summary>
/// Defines an interface that represents a scene renderer.
/// </summary>
public interface ISceneRenderer
{
/// <summary>
/// Changes the current scene projection the specified <paramref name="projectionWidth"/> and <paramref name="projectionHeight"/>.
/// </summary>
/// <param name="projectionWidth">
/// The width of the projection.
/// </param>
/// <param name="projectionHeight">
/// The height of the projection.
/// </param>
void ChangeProjection(int projectionWidth, int projectionHeight);

/// <summary>
/// Renders the currently active scene.
/// </summary>
void Render();
}
61 changes: 61 additions & 0 deletions FinalEngine.Editor.Common/Services/Rendering/SceneRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// <copyright file="SceneRenderer.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Rendering;

using System;
using System.Drawing;
using System.Numerics;
using FinalEngine.Rendering;
using Microsoft.Extensions.Logging;

/// <summary>
/// Provides a standard implementation of an <see cref="ISceneRenderer"/>.
/// </summary>
/// <seealso cref="ISceneRenderer" />
public sealed class SceneRenderer : ISceneRenderer
{
/// <summary>
/// The logger.
/// </summary>
private readonly ILogger<SceneRenderer> logger;

/// <summary>
/// The render device.
/// </summary>
private readonly IRenderDevice renderDevice;

/// <summary>
/// Initializes a new instance of the <see cref="SceneRenderer"/> class.
/// </summary>
/// <param name="logger">
/// The logger.
/// </param>
/// <param name="renderDevice">
/// The render device.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="logger"/> or <paramref name="renderDevice"/> parameter cannot be null.
/// </exception>
public SceneRenderer(ILogger<SceneRenderer> logger, IRenderDevice renderDevice)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.renderDevice = renderDevice ?? throw new ArgumentNullException(nameof(renderDevice));
}

/// <inheritdoc/>
public void ChangeProjection(int projectionWidth, int projectionHeight)
{
this.logger.LogDebug($"Changing projection to: ({projectionWidth}, {projectionHeight}).");

this.renderDevice.Pipeline.SetUniform("u_projection", Matrix4x4.CreateOrthographicOffCenter(0, projectionWidth, 0, projectionHeight, -1, 1));
this.renderDevice.Rasterizer.SetViewport(new Rectangle(0, 0, projectionWidth, projectionHeight));
}

/// <inheritdoc/>
public void Render()
{
this.renderDevice.Clear(Color.FromArgb(255, 30, 30, 30));
}
}
2 changes: 2 additions & 0 deletions FinalEngine.Editor.Desktop/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Controls.xaml" />
<ResourceDictionary Source="Styles/Docking/ToolStyle.xaml" />
<ResourceDictionary Source="Styles/Docking/PaneStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
Loading