Improved Editor

- Added window docking
- Added MenuBar
- Added scene viewport
- Added closing and reopening for editor windows
This commit is contained in:
Simon Lübeß
2021-09-17 13:05:15 +02:00
parent 45d298b755
commit 0b1ec9a837
5 changed files with 260 additions and 18 deletions
@@ -7,16 +7,18 @@ namespace GlitchyEditor.EditWindows
{ {
class ComponentEditWindow class ComponentEditWindow
{ {
public const String s_WindowTitle = "Components";
private Editor _editor; private Editor _editor;
private bool _show = true; private bool _open = true;
public Editor Editor => _editor; public Editor Editor => _editor;
public bool Show public bool Open
{ {
get => _show; get => _open;
set => _show = value; set => _open = value;
} }
public this(Editor editor) public this(Editor editor)
@@ -26,10 +28,10 @@ namespace GlitchyEditor.EditWindows
public void Show() public void Show()
{ {
if(!_show) if(!_open)
return; return;
if(!ImGui.Begin("Components")) if(!ImGui.Begin(s_WindowTitle, &_open, .None))
{ {
ImGui.End(); ImGui.End();
return; return;
@@ -12,19 +12,21 @@ namespace GlitchyEditor.EditWindows
/// A window for viewing and editing the scene hierarchy /// A window for viewing and editing the scene hierarchy
class EntityHierarchyWindow class EntityHierarchyWindow
{ {
public const String s_WindowTitle = "Entity Hierarchy";
private Editor _editor; private Editor _editor;
/// Buffer for the entity search string. /// Buffer for the entity search string.
private char8[64] _entitySearchChars; private char8[64] _entitySearchChars;
private bool _show = true; private bool _open = true;
public Editor Editor => _editor; public Editor Editor => _editor;
public bool Show public bool Open
{ {
get => _show; get => _open;
set => _show = value; set => _open = value;
} }
public this(Editor editor) public this(Editor editor)
@@ -34,10 +36,10 @@ namespace GlitchyEditor.EditWindows
public void Show() public void Show()
{ {
if(!_show) if(!_open)
return; return;
if(!ImGui.Begin("Entity Hierarchy", null, .MenuBar)) if(!ImGui.Begin(s_WindowTitle, &_open, .MenuBar))
{ {
ImGui.End(); ImGui.End();
return; return;
@@ -0,0 +1,99 @@
using System;
using ImGui;
using GlitchyEngine.Renderer;
using GlitchyEngine.Math;
using GlitchyEngine;
namespace GlitchyEditor.EditWindows
{
class SceneViewportWindow
{
public const String s_WindowTitle = "Scene";
private bool _open = true;
private Texture2D _texture ~ _?.ReleaseRef();
private bool _hasFocus;
public Event<EventHandler<Vector2>> ViewportSizeChangedEvent ~ _.Dispose();
public bool Open
{
get => _open;
set => _open = value;
}
public bool HasFocus
{
get => _hasFocus;
}
public Texture2D Texture
{
get => _texture;
set
{
if(_texture == value)
return;
_texture?.ReleaseRef();
_texture = value;
_texture?.AddRef();
}
}
public this()
{
}
private ImGui.Vec2 oldViewportSize;
Texture2D _lastTexture;
public void Show()
{
// Hold a reference to the texture until the next frame.
// This is a workaround for the issue that changing the window size will release the texture
_lastTexture?.ReleaseRef();
_lastTexture = _texture;
_lastTexture.AddRef();
if(!_open)
return;
ImGui.PushStyleVar(.WindowPadding, ImGui.Vec2(1, 1));
defer ImGui.PopStyleVar();
if(!ImGui.Begin(s_WindowTitle, &_open, .NoScrollbar))
{
ImGui.End();
return;
}
if(ImGui.IsWindowHovered() && Input.IsMouseButtonPressing(.RightButton))
{
var currentWindow = ImGui.GetCurrentWindow();
ImGui.FocusWindow(currentWindow);
}
_hasFocus = ImGui.IsWindowFocused();
var viewportSize = ImGui.GetContentRegionAvail();
if(_lastTexture != null)
{
ImGui.Image(_lastTexture, viewportSize);
}
ImGui.End();
if(oldViewportSize != viewportSize)
{
ViewportSizeChangedEvent.Invoke(this, *(Vector2*)&viewportSize);
oldViewportSize = viewportSize;
}
}
}
}
+6
View File
@@ -13,6 +13,7 @@ namespace GlitchyEditor
private EntityHierarchyWindow _entityHierarchyWindow = new .(this) ~ delete _; private EntityHierarchyWindow _entityHierarchyWindow = new .(this) ~ delete _;
private ComponentEditWindow _componentEditWindow = new .(this) ~ delete _; private ComponentEditWindow _componentEditWindow = new .(this) ~ delete _;
private SceneViewportWindow _sceneViewportWindow = new .() ~ delete _;
private List<Entity> _selectedEntities = new .() ~ delete _; private List<Entity> _selectedEntities = new .() ~ delete _;
@@ -20,6 +21,10 @@ namespace GlitchyEditor
public List<Entity> SelectedEntities => _selectedEntities; public List<Entity> SelectedEntities => _selectedEntities;
public EntityHierarchyWindow EntityHierarchyWindow => _entityHierarchyWindow;
public ComponentEditWindow ComponentEditWindow => _componentEditWindow;
public SceneViewportWindow SceneViewportWindow => _sceneViewportWindow;
/// Creates a new editor for the given world /// Creates a new editor for the given world
public this(EcsWorld world) public this(EcsWorld world)
{ {
@@ -30,6 +35,7 @@ namespace GlitchyEditor
{ {
_entityHierarchyWindow.Show(); _entityHierarchyWindow.Show();
_componentEditWindow.Show(); _componentEditWindow.Show();
_sceneViewportWindow.Show();
} }
/// Creates a new entity with a transform component. /// Creates a new entity with a transform component.
+138 -5
View File
@@ -2,9 +2,12 @@ using GlitchyEditor.EditWindows;
using GlitchyEngine; using GlitchyEngine;
using GlitchyEngine.Events; using GlitchyEngine.Events;
using GlitchyEngine.ImGui; using GlitchyEngine.ImGui;
using GlitchyEngine.Math;
using GlitchyEngine.Renderer; using GlitchyEngine.Renderer;
using GlitchyEngine.World; using GlitchyEngine.World;
using ImGui; using ImGui;
using System;
using GlitchyEngine.Renderer.Text;
namespace GlitchyEditor namespace GlitchyEditor
{ {
@@ -14,14 +17,19 @@ namespace GlitchyEditor
RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef(); RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef();
GraphicsContext _context ~ _?.ReleaseRef(); GraphicsContext _context ~ _?.ReleaseRef();
DepthStencilTarget _depthTarget ~ _?.ReleaseRef(); DepthStencilTarget _swapchainDepthBuffer ~ _?.ReleaseRef();
BlendState _alphaBlendState ~ _?.ReleaseRef(); BlendState _alphaBlendState ~ _?.ReleaseRef();
BlendState _opaqueBlendState ~ _?.ReleaseRef(); BlendState _opaqueBlendState ~ _?.ReleaseRef();
EcsWorld _world = new EcsWorld() ~ delete _; EcsWorld _world = new EcsWorld() ~ delete _;
Editor _editor = new Editor(_world) ~ delete _; Editor _editor ~ delete _;
PerspectiveCameraController _cameraController ~ delete _;
RenderTarget2D _renderTarget2D ~ _?.ReleaseRef();
DepthStencilTarget _depthTarget ~ _?.ReleaseRef();
public this() : base("Example") public this() : base("Example")
{ {
@@ -29,13 +37,14 @@ namespace GlitchyEditor
InitGraphics(); InitGraphics();
InitEcs(); InitEcs();
InitEditor();
} }
private void InitGraphics() private void InitGraphics()
{ {
_context = Application.Get().Window.Context..AddRef(); _context = Application.Get().Window.Context..AddRef();
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height); _swapchainDepthBuffer = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
RasterizerStateDescription rsDesc = .(.Solid, .Back, true); RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
_rasterizerState = new RasterizerState(_context, rsDesc); _rasterizerState = new RasterizerState(_context, rsDesc);
@@ -47,6 +56,17 @@ namespace GlitchyEditor
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All); blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
_alphaBlendState = new BlendState(_context, blendDesc); _alphaBlendState = new BlendState(_context, blendDesc);
_opaqueBlendState = new BlendState(_context, .Default); _opaqueBlendState = new BlendState(_context, .Default);
_renderTarget2D = new RenderTarget2D(_context, 100, 100, .R8G8B8A8_UNorm);
_depthTarget = new DepthStencilTarget(_context, 100, 100, .D32_Float);
SamplerStateDescription desc = .();
SamplerState sampler = new SamplerState(_context, desc);
_renderTarget2D.SamplerState = sampler;
sampler.ReleaseRef();
} }
private void InitEcs() private void InitEcs()
@@ -61,30 +81,143 @@ namespace GlitchyEditor
_world.Register<AnimationComponent>(); _world.Register<AnimationComponent>();
} }
private void InitEditor()
{
_editor = new Editor(_world);
_editor.SceneViewportWindow.ViewportSizeChangedEvent.Add(new (s, e) => ViewportSizeChanged(s, e));
_cameraController = new .(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
_cameraController.CameraPosition = .(0, 0, -5);
_cameraController.TranslationSpeed = 10;
_editor.[Friend]CreateEntityWithTransform();
}
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f)); if(_editor.SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton))
_cameraController.Update(gameTime);
TransformSystem.Update(_world);
RenderCommand.Clear(_renderTarget2D, .(0.2f, 0.2f, 0.2f));
_depthTarget.Clear(1.0f, 0, .Depth); _depthTarget.Clear(1.0f, 0, .Depth);
_context.SetRenderTarget(null); _context.SetRenderTarget(_renderTarget2D);
_depthTarget.Bind(); _depthTarget.Bind();
_context.BindRenderTargets(); _context.BindRenderTargets();
_context.SetViewport(Viewport(0, 0, _renderTarget2D.Width, _renderTarget2D.Height));
_opaqueBlendState.Bind();
Renderer.BeginScene(_cameraController.Camera);
DebugRenderer.Render(_world);
Renderer.EndScene();
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
_swapchainDepthBuffer.Clear(1.0f, 0, .Depth);
_context.SetRenderTarget(null);
_swapchainDepthBuffer.Bind();
_context.BindRenderTargets();
_context.SetViewport(_context.SwapChain.BackbufferViewport); _context.SetViewport(_context.SwapChain.BackbufferViewport);
} }
public override void OnEvent(Event event) public override void OnEvent(Event event)
{ {
_cameraController.OnEvent(event);
EventDispatcher dispatcher = EventDispatcher(event); EventDispatcher dispatcher = EventDispatcher(event);
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e)); dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
} }
ImGui.ID _mainDockspaceId;
private bool OnImGuiRender(ImGuiRenderEvent event) private bool OnImGuiRender(ImGuiRenderEvent event)
{ {
ImGui.Viewport* viewport = ImGui.GetMainViewport();
ImGui.DockSpaceOverViewport(viewport);
DrawMainMenuBar();
_editor.SceneViewportWindow.Texture = _renderTarget2D;
_editor.Update(); _editor.Update();
return false; return false;
} }
private void DrawMainMenuBar()
{
ImGui.BeginMainMenuBar();
if(ImGui.BeginMenu("File", false))
{
ImGui.EndMenu();
}
if(ImGui.BeginMenu("View", true))
{
if(ImGui.MenuItem(EntityHierarchyWindow.s_WindowTitle))
{
_editor.EntityHierarchyWindow.Open = true;
}
if(ImGui.MenuItem(ComponentEditWindow.s_WindowTitle))
{
_editor.ComponentEditWindow.Open = true;
}
if(ImGui.MenuItem(SceneViewportWindow.s_WindowTitle))
{
_editor.SceneViewportWindow.Open = true;
}
ImGui.EndMenu();
}
ImGui.EndMainMenuBar();
}
private bool OnWindowResize(WindowResizeEvent e)
{
_swapchainDepthBuffer.ReleaseRef();
_swapchainDepthBuffer = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
return false;
}
private void ViewportSizeChanged(Object sender, Vector2 viewportSize)
{
uint32 sizeX = (uint32)viewportSize.X;
uint32 sizeY = (uint32)viewportSize.Y;
if(sizeX == 0 || sizeY == 0)
return;
var sampler = _renderTarget2D.SamplerState;
_renderTarget2D.ReleaseRef();
_depthTarget.ReleaseRef();
_renderTarget2D = new RenderTarget2D(_context, sizeX, sizeY, .R8G8B8A8_UNorm);
_editor.SceneViewportWindow.Texture = _renderTarget2D;
_depthTarget = new DepthStencilTarget(_context, sizeX, sizeY, .D32_Float);
_cameraController.AspectRatio = (float)sizeX / (float)sizeY;
_renderTarget2D.SamplerState = sampler;
}
} }
} }