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

✨ Add GLWpfControl to SceneView #243

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions FinalEngine.Editor.Common/FinalEngine.Editor.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
</PackageReference>
<PackageReference Include="System.IO.Abstractions" Version="19.2.29" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FinalEngine.Rendering\FinalEngine.Rendering.csproj" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions FinalEngine.Editor.Common/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Performance", "CA1848:Use the LoggerMessage delegates", Justification = "KISS")]
[assembly: SuppressMessage("Usage", "CA2254:Template should be a static expression", Justification = "KISS")]
16 changes: 16 additions & 0 deletions FinalEngine.Editor.Common/Services/Scenes/ISceneRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="ISceneRenderer.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Scenes;

/// <summary>
/// Defines an interface that renders a scene.
/// </summary>
public interface ISceneRenderer
{
/// <summary>
/// Renders the scene.
/// </summary>
void Render();
}
41 changes: 41 additions & 0 deletions FinalEngine.Editor.Common/Services/Scenes/SceneRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="SceneRenderer.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Editor.Common.Services.Scenes;

using System;
using System.Drawing;
using FinalEngine.Rendering;

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

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

/// <inheritdoc/>
public void Render()
{
this.renderDevice.Clear(Color.FromArgb(255, 30, 30, 30));
}
}
8 changes: 6 additions & 2 deletions FinalEngine.Editor.Desktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace FinalEngine.Editor.Desktop;
using System.Diagnostics;
using System.IO.Abstractions;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging;
using FinalEngine.Editor.Common.Extensions;
using FinalEngine.Editor.Common.Services.Application;
using FinalEngine.Editor.Common.Services.Environment;
using FinalEngine.Editor.Common.Services.Scenes;
using FinalEngine.Editor.Desktop.Services.Actions;
using FinalEngine.Editor.Desktop.Services.Factories.Layout;
using FinalEngine.Editor.Desktop.Views;
Expand All @@ -25,6 +25,8 @@ namespace FinalEngine.Editor.Desktop;
using FinalEngine.Editor.ViewModels.Interactions;
using FinalEngine.Editor.ViewModels.Services.Actions;
using FinalEngine.Editor.ViewModels.Services.Factories.Layout;
using FinalEngine.Rendering;
using FinalEngine.Rendering.OpenGL;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -102,11 +104,13 @@ private static void ConfigureServices(HostBuilderContext context, IServiceCollec
builder.AddConsole().SetMinimumLevel(Debugger.IsAttached ? LogLevel.Debug : LogLevel.Information);
});

services.AddSingleton<IRenderDevice, OpenGLRenderDevice>();

services.AddSingleton<IFileSystem, FileSystem>();

services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);
services.AddSingleton<IApplicationContext, ApplicationContext>();
services.AddSingleton<IEnvironmentContext, EnvironmentContext>();
services.AddSingleton<ISceneRenderer, SceneRenderer>();

services.AddFactory<IProjectExplorerToolViewModel, ProjectExplorerToolViewModel>();
services.AddFactory<ISceneHierarchyToolViewModel, SceneHierarchyToolViewModel>();
Expand Down
2 changes: 2 additions & 0 deletions FinalEngine.Editor.Desktop/FinalEngine.Editor.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="Dirkster.AvalonDock" Version="4.72.0" />
<PackageReference Include="Dirkster.AvalonDock.Themes.VS2013" Version="4.72.0" />
<PackageReference Include="EventBinder" Version="2.5.2" />
<PackageReference Include="GLWpfControl" Version="1.0.0" />
<PackageReference Include="MahApps.Metro" Version="2.4.10" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -43,6 +44,7 @@

<ItemGroup>
<ProjectReference Include="..\FinalEngine.Editor.ViewModels\FinalEngine.Editor.ViewModels.csproj" />
<ProjectReference Include="..\FinalEngine.Rendering.OpenGL\FinalEngine.Rendering.OpenGL.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 16 additions & 1 deletion FinalEngine.Editor.Desktop/Views/Scenes/SceneView.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<UserControl x:Class="FinalEngine.Editor.Desktop.Views.Scenes.SceneView"
<UserControl x:Name="sceneView"
x:Class="FinalEngine.Editor.Desktop.Views.Scenes.SceneView"
softwareantics marked this conversation as resolved.
Show resolved Hide resolved
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:eb="clr-namespace:EventBinder;assembly=EventBinder"
xmlns:vm="clr-namespace:FinalEngine.Editor.ViewModels.Docking.Panes.Scenes;assembly=FinalEngine.Editor.ViewModels"
xmlns:glwpf="clr-namespace:OpenTK.Wpf;assembly=GLWpfControl"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:SceneViewPaneViewModel}">
<Grid>
<glwpf:GLWpfControl x:Name="glWpfControl"
RegisterToEventsDirectly="False"
CanInvokeOnHandledEvents="False"
Render="{eb:EventBinding RenderCommand.Execute, `null`}">
<glwpf:GLWpfControl.Settings>
<glwpf:GLWpfControlSettings MajorVersion="4"
MinorVersion="5"
GraphicsContextFlags="ForwardCompatible"
GraphicsProfile="Core"
RenderContinuously="True" />
</glwpf:GLWpfControl.Settings>
</glwpf:GLWpfControl>
</Grid>
</UserControl>
1 change: 1 addition & 0 deletions FinalEngine.Editor.Desktop/Views/Scenes/SceneView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public partial class SceneView : UserControl
public SceneView()
{
this.InitializeComponent();
this.glWpfControl.Start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@

namespace FinalEngine.Editor.ViewModels.Docking.Panes.Scenes;

using System.Windows.Input;

/// <summary>
/// Defines an interface that represents a model of the scene view pane.
/// </summary>
/// <seealso cref="IPaneViewModel" />
public interface ISceneViewPaneViewModel : IPaneViewModel
{
/// <summary>
/// Gets the render command.
/// </summary>
/// <value>
/// The render command.
/// </value>
/// <remarks>
/// The <see cref="RenderCommand"/> is used to render the currently active scene.
/// </remarks>
ICommand RenderCommand { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace FinalEngine.Editor.ViewModels.Docking.Panes.Scenes;

using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using FinalEngine.Editor.Common.Services.Scenes;
using Microsoft.Extensions.Logging;

/// <summary>
Expand All @@ -14,22 +17,53 @@ namespace FinalEngine.Editor.ViewModels.Docking.Panes.Scenes;
/// <seealso cref="ISceneViewPaneViewModel" />
public sealed class SceneViewPaneViewModel : PaneViewModelBase, ISceneViewPaneViewModel
{
/// <summary>
/// The scene renderer, used to render the current scene.
/// </summary>
private readonly ISceneRenderer sceneRenderer;

/// <summary>
/// The render command, used to render the current scene.
/// </summary>
private ICommand? renderCommand;

/// <summary>
/// Initializes a new instance of the <see cref="SceneViewPaneViewModel"/> class.
/// </summary>
/// <param name="logger">
/// The logger.
/// </param>
public SceneViewPaneViewModel(ILogger<SceneViewPaneViewModel> logger)
/// <param name="sceneRenderer">
/// The scene renderer used to render the currently active scene.
/// </param>
public SceneViewPaneViewModel(
ILogger<SceneViewPaneViewModel> logger,
ISceneRenderer sceneRenderer)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

this.sceneRenderer = sceneRenderer ?? throw new ArgumentNullException(nameof(sceneRenderer));

this.Title = "Scene View";
this.ContentID = "SceneView";

logger.LogInformation($"Initializing {this.Title}...");
}

/// <inheritdoc/>
public ICommand RenderCommand
{
get { return this.renderCommand ??= new RelayCommand(this.Render); }
}

/// <summary>
/// Renders the currently active scene.
/// </summary>
private void Render()
{
this.sceneRenderer.Render();
}
}
2 changes: 1 addition & 1 deletion FinalEngine.Examples.Game/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal static void Main()
var gameTime = new GameTime(120.0d);

resourceManager.RegisterLoader(new SoundResourceLoader(fileSystem));
resourceManager.RegisterLoader(new ShaderResourceLoader(renderDevice.Factory, fileSystem));
resourceManager.RegisterLoader(new ShaderResourceLoader(fileSystem, renderDevice.Factory));
resourceManager.RegisterLoader(new Texture2DResourceLoader(fileSystem, renderDevice.Factory));

displayManager.ChangeResolution(DisplayResolution.HighDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ public sealed class ShaderResourceLoader : ResourceLoaderBase<IShader>
/// <summary>
/// Initializes a new instance of the <see cref="ShaderResourceLoader"/> class.
/// </summary>
/// <param name="factory">
/// The GPU resource factory, used to load shaders into memory.
/// </param>
/// <param name="fileSystem">
/// The file system, used to load shader source code into memory.
/// </param>
/// <param name="factory">
/// The GPU resource factory, used to load shaders into memory.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="factory"/> or <paramref name="fileSystem"/> parameter cannot be null.
/// </exception>
public ShaderResourceLoader(IGPUResourceFactory factory, IFileSystem fileSystem)
public ShaderResourceLoader(IFileSystem fileSystem, IGPUResourceFactory factory)
{
this.factory = factory ?? throw new ArgumentNullException(nameof(factory));
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <copyright file="SceneRendererTests.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>

namespace FinalEngine.Tests.Editor.Common.Services.Rendering;

using System;
using System.Drawing;
using FinalEngine.Editor.Common.Services.Scenes;
using FinalEngine.Rendering;
using Moq;
using NUnit.Framework;

[TestFixture]
public sealed class SceneRendererTests
{
private Mock<IRenderDevice> renderDevice;

private SceneRenderer sceneRenderer;

[Test]
public void ConstructorShouldThrowArgumentNullExceptionWhenRenderDeviceIsNull()
{
// Act and assert
Assert.Throws<ArgumentNullException>(() =>
{
new SceneRenderer(null);
});
}

[Test]
public void RenderShouldInvokeRenderDeviceClearWhenInvoked()
{
// Act
this.sceneRenderer.Render();

// Assert
this.renderDevice.Verify(x => x.Clear(Color.FromArgb(255, 30, 30, 30), 1, 0), Times.Once);
}

[SetUp]
public void Setup()
{
this.renderDevice = new Mock<IRenderDevice>();
this.sceneRenderer = new SceneRenderer(this.renderDevice.Object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace FinalEngine.Tests.Editor.ViewModels.Docking.Panes.Scenes;

using System;
using FinalEngine.Editor.Common.Services.Scenes;
using FinalEngine.Editor.ViewModels.Docking.Panes.Scenes;
using Microsoft.Extensions.Logging;
using Moq;
Expand All @@ -15,6 +16,8 @@ public sealed class SceneViewPaneViewModelTests
{
private Mock<ILogger<SceneViewPaneViewModel>> logger;

private Mock<ISceneRenderer> sceneRenderer;

private SceneViewPaneViewModel viewModel;

[Test]
Expand Down Expand Up @@ -48,14 +51,34 @@ public void ConstructorShouldThrowArgumentNullExceptionWhenLoggerIsNull()
{
Assert.Throws<ArgumentNullException>(() =>
{
new SceneViewPaneViewModel(null);
new SceneViewPaneViewModel(null, this.sceneRenderer.Object);
});
}

[Test]
public void ConstructorShouldThrowArgumentNullExceptionWhenSceneRendererIsNull()
{
Assert.Throws<ArgumentNullException>(() =>
{
new SceneViewPaneViewModel(this.logger.Object, null);
});
}

[Test]
public void RenderCommandExecuteShouldInvokeSceneRendererRenderWhenInvoked()
{
// Act
this.viewModel.RenderCommand.Execute(null);

// Assert
this.sceneRenderer.Verify(x => x.Render(), Times.Once);
}

[SetUp]
public void Setup()
{
this.logger = new Mock<ILogger<SceneViewPaneViewModel>>();
this.viewModel = new SceneViewPaneViewModel(this.logger.Object);
this.sceneRenderer = new Mock<ISceneRenderer>();
this.viewModel = new SceneViewPaneViewModel(this.logger.Object, this.sceneRenderer.Object);
}
}