mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Editor camera and Editor/Runtime scene update separation
This commit is contained in:
@@ -59,6 +59,38 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
var viewportSize = ImGui.GetContentRegionAvail();
|
||||
|
||||
if (_editor.CurrentCamera.[Friend]BindMouse)
|
||||
{
|
||||
var mousePos = ImGui.GetMousePos();
|
||||
var newMousePos = mousePos;
|
||||
var winPos = ImGui.GetWindowPos();
|
||||
var winSize = ImGui.GetWindowSize();
|
||||
|
||||
if (mousePos.x < winPos.x)
|
||||
{
|
||||
newMousePos.x = winPos.x + winSize.x - 1;
|
||||
}
|
||||
else if (mousePos.x > winPos.x + winSize.x)
|
||||
{
|
||||
newMousePos.x = winPos.x + 1;
|
||||
}
|
||||
|
||||
if (mousePos.y < winPos.y)
|
||||
{
|
||||
newMousePos.y = winPos.y + winSize.y - 1;
|
||||
}
|
||||
else if (mousePos.y > winPos.y + winSize.y)
|
||||
{
|
||||
newMousePos.y = winPos.y + 1;
|
||||
}
|
||||
|
||||
if (newMousePos != mousePos)
|
||||
{
|
||||
Input.SetMousePosition(Point((int32)newMousePos.x, (int32)newMousePos.y));
|
||||
_editor.CurrentCamera.[Friend]MouseCooldown = 2;
|
||||
}
|
||||
}
|
||||
|
||||
if(_renderTarget != null)
|
||||
{
|
||||
ImGui.Image(_renderTarget, viewportSize);
|
||||
@@ -157,11 +189,8 @@ namespace GlitchyEditor.EditWindows
|
||||
topLeft.y += cntMin.y;
|
||||
ImGuizmo.SetRect(topLeft.x, topLeft.y, viewportSize.x, viewportSize.y);
|
||||
|
||||
var cameraTransformCmp = _editor.CurrentScene.ActiveCamera.GetComponent<TransformComponent>();
|
||||
var view = cameraTransformCmp.WorldTransform.Invert();
|
||||
|
||||
var cameraCmp = _editor.CurrentCamera.GetComponent<CameraComponent>();
|
||||
var projection = cameraCmp.Camera.Projection;
|
||||
var view = _editor.CurrentCamera.View;
|
||||
var projection = _editor.CurrentCamera.Projection;
|
||||
|
||||
if(_editor.EntityHierarchyWindow.SelectedEntities.Count == 0)
|
||||
return;
|
||||
|
||||
@@ -11,7 +11,6 @@ namespace GlitchyEditor
|
||||
class Editor
|
||||
{
|
||||
private Scene _scene;
|
||||
private Entity _currentCamera;
|
||||
|
||||
private EntityHierarchyWindow _entityHierarchyWindow ~ delete _;
|
||||
private ComponentEditWindow _componentEditWindow ~ delete _;
|
||||
@@ -31,7 +30,7 @@ namespace GlitchyEditor
|
||||
}
|
||||
}
|
||||
|
||||
public Entity CurrentCamera => _currentCamera;
|
||||
public EditorCamera* CurrentCamera { get; set; }
|
||||
|
||||
/// Creates a new editor for the given world
|
||||
public this(Scene scene)
|
||||
@@ -44,17 +43,6 @@ namespace GlitchyEditor
|
||||
|
||||
public void Update()
|
||||
{
|
||||
_currentCamera = _scene.ActiveCamera;
|
||||
if (_currentCamera.IsValid)
|
||||
{
|
||||
var scriptComponent = _currentCamera.GetComponent<NativeScriptComponent>();
|
||||
|
||||
if (var camController = scriptComponent?.Instance as EditorCameraController)
|
||||
{
|
||||
camController.IsEnabled = (SceneViewportWindow.HasFocus && Input.IsMouseButtonPressed(.RightButton));
|
||||
}
|
||||
}
|
||||
|
||||
_entityHierarchyWindow.Show();
|
||||
_componentEditWindow.Show();
|
||||
_sceneViewportWindow.Show();
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.World;
|
||||
|
||||
namespace GlitchyEditor
|
||||
{
|
||||
class EditorCameraController : ScriptableEntity
|
||||
{
|
||||
private float _cameraTranslationSpeed = 2.0f;
|
||||
private float _cameraRotationSpeedX = 0.001f;
|
||||
private float _cameraRotationSpeedY = 0.001f;
|
||||
|
||||
public bool IsEnabled = false;
|
||||
|
||||
protected override void OnUpdate(GameTime gt)
|
||||
{
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
|
||||
Debug.Profiler.ProfileFunction!();
|
||||
|
||||
Vector3 movement = .();
|
||||
|
||||
if(Input.IsKeyPressed(Key.W))
|
||||
movement.Z += 1;
|
||||
if(Input.IsKeyPressed(Key.S))
|
||||
movement.Z -= 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.A))
|
||||
movement.X -= 1;
|
||||
if(Input.IsKeyPressed(Key.D))
|
||||
movement.X += 1;
|
||||
|
||||
if(Input.IsKeyPressed(Key.Space))
|
||||
movement.Y += 1;
|
||||
if(Input.IsKeyPressed(Key.Control))
|
||||
movement.Y -= 1;
|
||||
|
||||
var transformComponent = transform;
|
||||
|
||||
if(movement != .Zero)
|
||||
{
|
||||
movement.Normalize();
|
||||
|
||||
movement *= (float)(gt.FrameTime.TotalSeconds) * _cameraTranslationSpeed;
|
||||
|
||||
Matrix view = transformComponent.WorldTransform.Invert();
|
||||
|
||||
Vector4 delta = Vector4(movement, 1.0f) * view;
|
||||
|
||||
transformComponent.Position = transformComponent.Position + delta.XYZ;
|
||||
}
|
||||
|
||||
// Camera rotation
|
||||
var mouseDelta = Input.GetMouseMovement();
|
||||
|
||||
float rotY = mouseDelta.X * _cameraRotationSpeedX;
|
||||
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
|
||||
|
||||
transformComponent.RotationEuler = transformComponent.RotationEuler + .(rotX, rotY, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,8 +47,7 @@ namespace GlitchyEditor
|
||||
|
||||
SettingsWindow _settingsWindow = new .() ~ delete _;
|
||||
|
||||
Entity _cameraEntity;
|
||||
Entity _otherCameraEntity;
|
||||
EditorCamera _camera ~ _.Dispose();
|
||||
|
||||
class CameraController : ScriptableEntity
|
||||
{
|
||||
@@ -95,6 +94,9 @@ namespace GlitchyEditor
|
||||
|
||||
InitGraphics();
|
||||
|
||||
_camera = EditorCamera(Vector3(3.5f, 1.25f, 2.75f), Quaternion.FromEulerAngles(MathHelper.ToRadians(40), MathHelper.ToRadians(25), 0), MathHelper.ToRadians(75), 0.1f, 1);
|
||||
_camera.RenderTarget = _cameraTarget;
|
||||
|
||||
NewScene();
|
||||
|
||||
InitEditor();
|
||||
@@ -129,10 +131,13 @@ namespace GlitchyEditor
|
||||
{
|
||||
_editor = new Editor(_scene);
|
||||
_editor.SceneViewportWindow.ViewportSizeChangedEvent.Add(new (s, e) => ViewportSizeChanged(s, e));
|
||||
_editor.CurrentCamera = &_camera;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
_camera.Update(gameTime);
|
||||
|
||||
// Clear the swapchain-buffer
|
||||
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
|
||||
@@ -146,7 +151,7 @@ namespace GlitchyEditor
|
||||
RenderCommand.SetBlendState(_alphaBlendState);
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
_scene.Update(gameTime, _viewportTarget);
|
||||
_scene.UpdateEditor(gameTime, _camera, _viewportTarget);
|
||||
|
||||
RenderCommand.UnbindRenderTargets();
|
||||
RenderCommand.SetRenderTarget(null, 0, true);
|
||||
@@ -162,6 +167,7 @@ namespace GlitchyEditor
|
||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
|
||||
dispatcher.Dispatch<KeyPressedEvent>(scope (e) => OnKeyPressed(e));
|
||||
dispatcher.Dispatch<MouseScrolledEvent>(scope (e) => OnMouseScrolled(e));
|
||||
}
|
||||
|
||||
ImGui.ID _mainDockspaceId;
|
||||
@@ -172,18 +178,6 @@ namespace GlitchyEditor
|
||||
{
|
||||
viewer.ViewTexture(_cameraTarget);
|
||||
|
||||
/*ImGui.Begin("Test");
|
||||
|
||||
static bool cameraA = true;
|
||||
|
||||
if (ImGui.Checkbox("Camera A", &cameraA))
|
||||
{
|
||||
_cameraEntity.GetComponent<CameraComponent>().Primary = cameraA;
|
||||
_otherCameraEntity.GetComponent<CameraComponent>().Primary = !cameraA;
|
||||
}
|
||||
|
||||
ImGui.End();*/
|
||||
|
||||
ImGui.Viewport* viewport = ImGui.GetMainViewport();
|
||||
ImGui.DockSpaceOverViewport(viewport);
|
||||
|
||||
@@ -313,34 +307,14 @@ namespace GlitchyEditor
|
||||
}
|
||||
}
|
||||
|
||||
private void PrepareSceneForEditor()
|
||||
{
|
||||
// Create the editor camera
|
||||
{
|
||||
_cameraEntity = _scene.CreateEntity("Editor Camera");
|
||||
// Add EditorComponent so that the engine knows that this is not part of the game.
|
||||
_cameraEntity.AddComponent<EditorComponent>();
|
||||
// Script for controlling the camera.
|
||||
_cameraEntity.AddComponent<NativeScriptComponent>().Bind<EditorCameraController>();
|
||||
|
||||
let camera = _cameraEntity.AddComponent<CameraComponent>();
|
||||
camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f);
|
||||
camera.Primary = true;
|
||||
camera.RenderTarget = _cameraTarget;
|
||||
let transform = _cameraEntity.GetComponent<TransformComponent>();
|
||||
//transform.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
transform.Position = .(3.5f, 1.25f, 2.75f);
|
||||
//transform.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
transform.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(40), 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new scene.
|
||||
private void NewScene()
|
||||
{
|
||||
SceneFilePath = null;
|
||||
|
||||
_camera.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
|
||||
// Create the default light source
|
||||
{
|
||||
var lightNtt = _scene.CreateEntity("Light");
|
||||
@@ -354,8 +328,6 @@ namespace GlitchyEditor
|
||||
}
|
||||
|
||||
TestEntitiesWithModels();
|
||||
|
||||
PrepareSceneForEditor();
|
||||
}
|
||||
|
||||
/// Saves the scene in the file that is was loaded from or saved to last. If there is no such path (i.e. it is a new scene) the save file dialog will open.
|
||||
@@ -402,8 +374,6 @@ namespace GlitchyEditor
|
||||
SceneSerializer serializer = scope .(_scene);
|
||||
serializer.Deserialize(SceneFilePath);
|
||||
|
||||
PrepareSceneForEditor();
|
||||
|
||||
_editor.CurrentScene = _scene;
|
||||
}
|
||||
}
|
||||
@@ -481,6 +451,7 @@ namespace GlitchyEditor
|
||||
_cameraTarget.Resize(sizeX, sizeY);
|
||||
|
||||
_scene.OnViewportResize(sizeX, sizeY);
|
||||
_camera.OnViewportResize(sizeX, sizeY);
|
||||
}
|
||||
|
||||
private bool OnKeyPressed(KeyPressedEvent e)
|
||||
@@ -511,5 +482,13 @@ namespace GlitchyEditor
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool OnMouseScrolled(MouseScrolledEvent e)
|
||||
{
|
||||
if (_camera.OnMouseScrolled(e))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user