mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Improved Editor
- Added window docking - Added MenuBar - Added scene viewport - Added closing and reopening for editor windows
This commit is contained in:
@@ -2,9 +2,12 @@ using GlitchyEditor.EditWindows;
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Events;
|
||||
using GlitchyEngine.ImGui;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.World;
|
||||
using ImGui;
|
||||
using System;
|
||||
using GlitchyEngine.Renderer.Text;
|
||||
|
||||
namespace GlitchyEditor
|
||||
{
|
||||
@@ -14,28 +17,34 @@ namespace GlitchyEditor
|
||||
RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef();
|
||||
|
||||
GraphicsContext _context ~ _?.ReleaseRef();
|
||||
DepthStencilTarget _depthTarget ~ _?.ReleaseRef();
|
||||
DepthStencilTarget _swapchainDepthBuffer ~ _?.ReleaseRef();
|
||||
|
||||
BlendState _alphaBlendState ~ _?.ReleaseRef();
|
||||
BlendState _opaqueBlendState ~ _?.ReleaseRef();
|
||||
|
||||
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")
|
||||
{
|
||||
Application.Get().Window.IsVSync = false;
|
||||
|
||||
|
||||
InitGraphics();
|
||||
InitEcs();
|
||||
InitEditor();
|
||||
}
|
||||
|
||||
private void InitGraphics()
|
||||
{
|
||||
_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);
|
||||
_rasterizerState = new RasterizerState(_context, rsDesc);
|
||||
@@ -47,6 +56,17 @@ namespace GlitchyEditor
|
||||
blendDesc.RenderTarget[0] = .(true, .SourceAlpha, .InvertedSourceAlpha, .Add, .SourceAlpha, .InvertedSourceAlpha, .Add, .All);
|
||||
_alphaBlendState = new BlendState(_context, blendDesc);
|
||||
_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()
|
||||
@@ -61,30 +81,143 @@ namespace GlitchyEditor
|
||||
_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)
|
||||
{
|
||||
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);
|
||||
|
||||
_context.SetRenderTarget(null);
|
||||
_context.SetRenderTarget(_renderTarget2D);
|
||||
_depthTarget.Bind();
|
||||
_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);
|
||||
}
|
||||
|
||||
public override void OnEvent(Event event)
|
||||
{
|
||||
_cameraController.OnEvent(event);
|
||||
|
||||
EventDispatcher dispatcher = EventDispatcher(event);
|
||||
|
||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
|
||||
}
|
||||
|
||||
ImGui.ID _mainDockspaceId;
|
||||
|
||||
private bool OnImGuiRender(ImGuiRenderEvent event)
|
||||
{
|
||||
ImGui.Viewport* viewport = ImGui.GetMainViewport();
|
||||
ImGui.DockSpaceOverViewport(viewport);
|
||||
|
||||
DrawMainMenuBar();
|
||||
|
||||
_editor.SceneViewportWindow.Texture = _renderTarget2D;
|
||||
|
||||
_editor.Update();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user