This commit is contained in:
Simon Lübeß
2023-03-25 00:41:34 +01:00
parent d54cdf43ee
commit ed952155b6
6 changed files with 99 additions and 244 deletions
@@ -14,6 +14,8 @@ namespace GlitchyEditor.EditWindows
class ContentBrowserWindow : EditorWindow class ContentBrowserWindow : EditorWindow
{ {
public const String s_WindowTitle = "Content Browser";
private append String _currentDirectory = .(); private append String _currentDirectory = .();
private append String _selectedFile = .(); private append String _selectedFile = .();
@@ -40,7 +42,7 @@ namespace GlitchyEditor.EditWindows
_currentDirectory.Set(_manager.ContentDirectory); _currentDirectory.Set(_manager.ContentDirectory);
} }
if(!ImGui.Begin("Content Browser", &_open, .None)) if(!ImGui.Begin(s_WindowTitle, &_open, .None))
{ {
ImGui.End(); ImGui.End();
return; return;
@@ -9,7 +9,7 @@ using GlitchyEngine.Events;
namespace GlitchyEditor.EditWindows namespace GlitchyEditor.EditWindows
{ {
class SceneViewportWindow : EditorWindow class EditorViewportWindow : EditorWindow
{ {
public const String s_WindowTitle = "Scene"; public const String s_WindowTitle = "Scene";
@@ -10,6 +10,8 @@ namespace GlitchyEditor.EditWindows;
class PropertiesWindow : EditorWindow class PropertiesWindow : EditorWindow
{ {
public const String s_WindowTitle = "Properties";
private AssetPropertiesEditor _currentPropertiesEditor ~ delete _; private AssetPropertiesEditor _currentPropertiesEditor ~ delete _;
private bool _lockCurrentAsset; private bool _lockCurrentAsset;
@@ -28,7 +30,7 @@ class PropertiesWindow : EditorWindow
protected override void InternalShow() protected override void InternalShow()
{ {
defer { ImGui.End(); } defer { ImGui.End(); }
if(!ImGui.Begin("Properties", &_open, .None)) if(!ImGui.Begin(s_WindowTitle, &_open, .None))
return; return;
// TODO: make a little button in title bar? // TODO: make a little button in title bar?
+3 -3
View File
@@ -17,7 +17,7 @@ namespace GlitchyEditor
private EntityHierarchyWindow _entityHierarchyWindow ~ delete _; private EntityHierarchyWindow _entityHierarchyWindow ~ delete _;
private ComponentEditWindow _componentEditWindow ~ delete _; private ComponentEditWindow _componentEditWindow ~ delete _;
private SceneViewportWindow _sceneViewportWindow ~ delete _; private EditorViewportWindow _sceneViewportWindow ~ delete _;
private GameViewportWindow _gameViewportWindow ~ delete _; private GameViewportWindow _gameViewportWindow ~ delete _;
private ContentBrowserWindow _contentBrowserWindow ~ delete _; private ContentBrowserWindow _contentBrowserWindow ~ delete _;
private PropertiesWindow _propertiesWindow ~ delete _; private PropertiesWindow _propertiesWindow ~ delete _;
@@ -39,7 +39,7 @@ namespace GlitchyEditor
public EntityHierarchyWindow EntityHierarchyWindow => _entityHierarchyWindow; public EntityHierarchyWindow EntityHierarchyWindow => _entityHierarchyWindow;
public ComponentEditWindow ComponentEditWindow => _componentEditWindow; public ComponentEditWindow ComponentEditWindow => _componentEditWindow;
public SceneViewportWindow SceneViewportWindow => _sceneViewportWindow; public EditorViewportWindow SceneViewportWindow => _sceneViewportWindow;
public GameViewportWindow GameViewportWindow => _gameViewportWindow; public GameViewportWindow GameViewportWindow => _gameViewportWindow;
public ContentBrowserWindow ContentBrowserWindow => _contentBrowserWindow; public ContentBrowserWindow ContentBrowserWindow => _contentBrowserWindow;
public PropertiesWindow PropertiesWindow => _propertiesWindow; public PropertiesWindow PropertiesWindow => _propertiesWindow;
@@ -62,7 +62,7 @@ namespace GlitchyEditor
private void InitWindows() private void InitWindows()
{ {
_sceneViewportWindow = new SceneViewportWindow(this); _sceneViewportWindow = new EditorViewportWindow(this);
_gameViewportWindow = new GameViewportWindow(this); _gameViewportWindow = new GameViewportWindow(this);
_entityHierarchyWindow = new EntityHierarchyWindow(this, _scene); _entityHierarchyWindow = new EntityHierarchyWindow(this, _scene);
_componentEditWindow = new ComponentEditWindow(_entityHierarchyWindow); _componentEditWindow = new ComponentEditWindow(_entityHierarchyWindow);
+89 -230
View File
@@ -18,34 +18,40 @@ namespace GlitchyEditor
{ {
class EditorLayer : Layer class EditorLayer : Layer
{ {
enum SceneState
{
Edit,
Play,
Simulate
}
RasterizerState _rasterizerState ~ _?.ReleaseRef(); RasterizerState _rasterizerState ~ _?.ReleaseRef();
RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef(); RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef();
// TODO: we shouldn't hold a reference to the context
GraphicsContext _context ~ _.ReleaseRef(); GraphicsContext _context ~ _.ReleaseRef();
BlendState _alphaBlendState ~ _.ReleaseRef(); BlendState _alphaBlendState ~ _.ReleaseRef();
BlendState _opaqueBlendState ~ _.ReleaseRef(); BlendState _opaqueBlendState ~ _.ReleaseRef();
DepthStencilState _depthStencilState ~ _.ReleaseRef(); DepthStencilState _depthStencilState ~ _.ReleaseRef();
/// Reference to the scene that is currently being played and worked on.
Scene _activeScene ~ _?.ReleaseRef(); Scene _activeScene ~ _?.ReleaseRef();
/**
* Referece to the editor scene.
* We hold a reference to the editor scene because we need it in order
* to restore the original state once we stop the game/simulation.
* Before starting the simulation the editor scene will be copied and
* the reference in _activeScene will be replaced with the new scene.
*/
Scene _editorScene ~ _?.ReleaseRef(); Scene _editorScene ~ _?.ReleaseRef();
SceneRenderer _gameSceneRenderer ~ delete _; SceneRenderer _gameSceneRenderer ~ delete _;
SceneRenderer _editorSceneRenderer ~ delete _; SceneRenderer _editorSceneRenderer ~ delete _;
String _sceneFilePath = new String() ~ delete _; /// Path of the current scene.
append String _sceneFilePath = .();
public String SceneFilePath
{
get => _sceneFilePath;
set
{
_sceneFilePath.Clear();
if (value != null)
_sceneFilePath.Append(value);
}
}
Editor _editor ~ delete _; Editor _editor ~ delete _;
@@ -57,29 +63,30 @@ namespace GlitchyEditor
EditorCamera _camera ~ _.Dispose(); EditorCamera _camera ~ _.Dispose();
/*Texture2D _editorIcons ~ _.ReleaseRef();
SubTexture2D _iconDirectionalLight ~ _.ReleaseRef();
SubTexture2D _iconCamera ~ _.ReleaseRef();*/
EditorIcons _editorIcons ~ _.ReleaseRef(); EditorIcons _editorIcons ~ _.ReleaseRef();
EditorContentManager _contentManager; EditorContentManager _contentManager;
enum SceneState
{
Edit,
Play,
Simulate
}
SceneState _sceneState = .Edit; SceneState _sceneState = .Edit;
bool _isPaused = false; bool _isPaused = false;
/// Gets or sets the path of the current scene.
public StringView SceneFilePath
{
get => _sceneFilePath;
set
{
_sceneFilePath.Clear();
public this(EditorContentManager contentManager) : base("Example") if (!value.IsWhiteSpace)
_sceneFilePath.Append(value);
}
}
public this(EditorContentManager contentManager) : base("Editor")
{ {
Application.Get().Window.IsVSync = false; Application.Get().Window.IsVSync = false;
//InitContentManager();
_contentManager = contentManager; _contentManager = contentManager;
InitGraphics(); InitGraphics();
@@ -98,31 +105,6 @@ namespace GlitchyEditor
NewScene(); NewScene();
} }
/*private void InitContentManager()
{
_contentManager = new EditorContentManager();
_contentManager.RegisterAssetLoader<EditorTextureAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<EditorTextureAssetLoader>(".png", ".dds");
_contentManager.SetAssetPropertiesEditor<EditorTextureAssetLoader>(=> TextureAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<ModelAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<ModelAssetLoader>(".glb", ".gltf");
_contentManager.SetAssetPropertiesEditor<ModelAssetLoader>(=> ModelAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<MaterialAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<MaterialAssetLoader>(".mat");
_contentManager.SetAssetPropertiesEditor<MaterialAssetLoader>(=> MaterialAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<EffectAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<EffectAssetLoader>(".hlsl");
_contentManager.SetAssetPropertiesEditor<EffectAssetLoader>(=> EffectAssetPropertiesEditor.Factory);
_contentManager.SetContentDirectory("./content");
// Todo: Sketchy...
Application.Get().[Friend]_contentManager = _contentManager;
}*/
private void InitGraphics() private void InitGraphics()
{ {
_context = Application.Get().Window.Context..AddRef(); _context = Application.Get().Window.Context..AddRef();
@@ -172,11 +154,6 @@ namespace GlitchyEditor
ContentBrowserWindow.s_FolderTexture = _editorIcons.Folder; ContentBrowserWindow.s_FolderTexture = _editorIcons.Folder;
ContentBrowserWindow.s_FileTexture = _editorIcons.File; ContentBrowserWindow.s_FileTexture = _editorIcons.File;
/*_editorIcons = new Texture2D("Textures/EditorIcons.dds");
_editorIcons.SamplerState = SamplerStateManager.AnisotropicClamp;
_iconDirectionalLight = .CreateFromGrid(_editorIcons, .(0, 0), .(64, 64));
_iconCamera = .CreateFromGrid(_editorIcons, .(1, 0), .(64, 64));*/
} }
private void InitEditor() private void InitEditor()
@@ -396,16 +373,15 @@ namespace GlitchyEditor
float totalWidth = size * 3 + padding * 4; float totalWidth = size * 3 + padding * 4;
float penX = centerX - totalWidth / 2; ImGui.SameLine();
ImGui.SetCursorPosX(centerX - totalWidth / 2);
ImGui.SameLine(penX);
if (ImGui.ImageButton(_editorIcons.Play, .(size, size), .Zero, .Ones, 0)) if (ImGui.ImageButton(_editorIcons.Play, .(size, size), .Zero, .Ones, 0))
OnScenePlay(); OnScenePlay();
ImGui.AttachTooltip("Play the game."); ImGui.AttachTooltip("Play the game.");
ImGui.SameLine(penX += size + 2 * padding); ImGui.SameLine();
ImGui.PushID(1); ImGui.PushID(1);
@@ -425,7 +401,8 @@ namespace GlitchyEditor
ImGui.PushID(2); ImGui.PushID(2);
ImGui.SameLine(penX += size + 2 * padding); //ImGui.SameLine(penX += size + 2 * padding);
ImGui.SameLine();
if (ImGui.ImageButton(_editorIcons.Pause, .(size, size), .Zero, .Ones, 0)) if (ImGui.ImageButton(_editorIcons.Pause, .(size, size), .Zero, .Ones, 0))
_isPaused = !_isPaused; _isPaused = !_isPaused;
@@ -439,7 +416,8 @@ namespace GlitchyEditor
{ {
// Display the buttons for play/simulation state // Display the buttons for play/simulation state
ImGui.SameLine(centerX - size - padding); ImGui.SameLine();
ImGui.SetCursorPosX(centerX - size - padding);
SubTexture2D pauseButtonIcon = _isPaused ? _editorIcons.Play : _editorIcons.Pause; SubTexture2D pauseButtonIcon = _isPaused ? _editorIcons.Play : _editorIcons.Pause;
@@ -453,7 +431,7 @@ namespace GlitchyEditor
ImGui.AttachTooltip(_isPaused ? "Resume" : "Pause"); ImGui.AttachTooltip(_isPaused ? "Resume" : "Pause");
ImGui.SameLine(centerX + padding); ImGui.SameLine();
ImGui.PushID(1); ImGui.PushID(1);
if (ImGui.ImageButton(_editorIcons.Stop, .(size, size), .Zero, .Ones, 0)) if (ImGui.ImageButton(_editorIcons.Stop, .(size, size), .Zero, .Ones, 0))
@@ -538,134 +516,6 @@ namespace GlitchyEditor
GameViewportSizeChanged(null, _editor.GameViewportWindow.ViewportSize); GameViewportSizeChanged(null, _editor.GameViewportWindow.ViewportSize);
} }
// Just for testing
private void TestEntitiesWithModels()
{
/*{
var lightNtt = _scene.CreateEntity("My Sexy Sun 2");
let transform = lightNtt.GetComponent<TransformComponent>();
transform.Position = .(0, 0, 0);
transform.RotationEuler = .(MathHelper.ToRadians(70), MathHelper.ToRadians(-30), 0);
let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}
{
var lightNtt = _scene.CreateEntity("My Sexy Sun 3");
let transform = lightNtt.GetComponent<TransformComponent>();
transform.Position = .(0, 0, 0);
transform.RotationEuler = .(MathHelper.ToRadians(20), MathHelper.ToRadians(-55), 0);
let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}
{
var cameraNtt = _scene.CreateEntity("My Camera");
let transform = cameraNtt.GetComponent<TransformComponent>();
transform.Position = .(5, 5, -5);
transform.RotationEuler = .(MathHelper.ToRadians(45), MathHelper.ToRadians(-45), 0);
let camera = cameraNtt.AddComponent<CameraComponent>();
camera.Primary = true;
camera.Camera.SetPerspective(MathHelper.ToRadians(45), 0.1f, 10.0f);
camera.RenderTarget = _cameraTarget;
}
var fxLib = Application.Get().EffectLibrary;
using (Effect myEffect = fxLib.Load("content/Shaders/myEffect.hlsl"))
/*using (Texture2D albedo = new Texture2D("Textures/White.png", true))
using (Texture2D normal = new Texture2D("Textures/White.png"))
using (Texture2D rough = new Texture2D("Textures/White.png"))
using (Texture2D metal = new Texture2D("Textures/White.png"))*/
using (Texture2D albedo = new Texture2D("Textures/TestMat/rustediron2_albedo.png", true))
using (Texture2D normal = new Texture2D("Textures/TestMat/rustediron2_normal.png"))
using (Texture2D rough = new Texture2D("Textures/TestMat/rustediron2_roughness.png"))
using (Texture2D metal = new Texture2D("Textures/TestMat/rustediron2_metallic.png"))
{
albedo.SamplerState = SamplerStateManager.AnisotropicWrap;
normal.SamplerState = SamplerStateManager.AnisotropicWrap;
rough.SamplerState = SamplerStateManager.AnisotropicWrap;
metal.SamplerState = SamplerStateManager.AnisotropicWrap;
List<AnimationClip> clips = scope .();
using (Material mat = new .(myEffect))
{
mat.SetTexture("AlbedoTexture", albedo);
mat.SetTexture("NormalTexture", normal);
mat.SetTexture("MetallicTexture", metal);
mat.SetTexture("RoughnessTexture", rough);
mat.SetVariable("AlbedoColor", ColorRGBA.White);
mat.SetVariable("NormalScaling", Vector2.One);
mat.SetVariable("MetallicFactor", 1.0f);
mat.SetVariable("RoughnessFactor", 1.0f);
// TODO: completely wrong!
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1");
Entity entity = .(e, _scene);
var transform = entity.GetComponent<TransformComponent>();
transform.Position = .(5, 0, 5);
}
/*using (Material mat = new .(myEffect))
{
mat.SetTexture("AlbedoTexture", albedo);
mat.SetTexture("NormalTexture", normal);
mat.SetTexture("MetallicTexture", metal);
mat.SetTexture("RoughnessTexture", rough);
//mat.SetVariable("BaseColor", Vector4(1, 1, 0, 1));
//mat.SetVariable("LightDir", Vector3(0, 1, 0).Normalized());
ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
}*/
ClearAndReleaseItems!(clips);
}
using (Effect myEffect = fxLib.Get("myEffect"))
using (Texture2D white = new Texture2D("Textures/White.png"))
using (Texture2D normal = new Texture2D("Textures/DefaultNormal.png"))
{
white.SamplerState = SamplerStateManager.PointClamp;
normal.SamplerState = SamplerStateManager.PointClamp;
List<AnimationClip> clips = scope .();
for (int x < 10)
for (int y < 10)
{
using (Material mat = new .(myEffect))
{
mat.SetTexture("AlbedoTexture", white);
mat.SetTexture("NormalTexture", normal);
mat.SetTexture("MetallicTexture", white);
mat.SetTexture("RoughnessTexture", white);
mat.SetVariable("AlbedoColor", Vector4(1, 0, 0, 1));
mat.SetVariable("NormalScaling", Vector2(1.0f));
mat.SetVariable("RoughnessFactor", (x + 1) / 10.0f);
mat.SetVariable("MetallicFactor", y / 9.0f);
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", mat, _scene.[Friend]_ecsWorld, clips, scope $"Sphere {x} {y}");
Entity entity = .(e, _scene);
var transform = entity.GetComponent<TransformComponent>();
transform.Position = .(x * 1.5f, y * 1.5f, 0);
}
}
ClearAndReleaseItems!(clips);
}*/
}
/// Creates a new scene. /// Creates a new scene.
private void NewScene() private void NewScene()
{ {
@@ -673,43 +523,50 @@ namespace GlitchyEditor
SceneFilePath = null; SceneFilePath = null;
Scene newScene = new Scene();
_camera.Position = .(-1.5f, 1.5f, -2.5f);
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
// Create a default camera
{
let cameraEntity = newScene.CreateEntity("Camera");
let transform = cameraEntity.Transform;
transform.Position = Vector3(0, 2, -5);
transform.RotationEuler = Vector3(0, MathHelper.ToRadians(25), 0);
let camera = cameraEntity.AddComponent<CameraComponent>();
camera.Primary = true;
camera.Camera.ProjectionType = .InfinitePerspective;
camera.Camera.PerspectiveFovY = MathHelper.ToRadians(75);
camera.Camera.PerspectiveNearPlane = 0.1f;
}
// Create a default light source
{
let lightEntity = newScene.CreateEntity("Light");
let transform = lightEntity.Transform;
transform.Position = .(-3, 4, -1.5f);
transform.RotationEuler = .(MathHelper.ToRadians(20), MathHelper.ToRadians(75), MathHelper.ToRadians(20));
let light = lightEntity.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}
_editorScene.ReleaseRef(); _editorScene.ReleaseRef();
_editorScene = new Scene(); _editorScene = newScene;
_editor.CurrentScene = _editorScene; _editor.CurrentScene = _editorScene;
var vpSize = _editor.SceneViewportWindow.ViewportSize; var vpSize = _editor.SceneViewportWindow.ViewportSize;
_editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y); _editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
SetReference!(_activeScene, _editorScene); SetReference!(_activeScene, _editorScene);
_camera.Position = .(-1.5f, 1.5f, -2.5f);
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
// Create default camera
/*{
let camEntity = _scene.CreateEntity("Camera");
let transform = camEntity.Transform;
transform.Position =
}*/
/*// Create the default light source
{
let lightNtt = _scene.CreateEntity("Light");
let transform = lightNtt.Transform;
transform.Position = .(0, 0, 0);
transform.RotationEuler = .(MathHelper.ToRadians(45), MathHelper.ToRadians(-100), 0);
let light = lightNtt.AddComponent<LightComponent>();
light.SceneLight.Illuminance = 10.0f;
light.SceneLight.Color = .(1.0f, 0.95f, 0.8f);
}
TestEntitiesWithModels();*/
} }
/// 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. /// 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.
private void SaveScene() private void SaveScene()
{ {
if (String.IsNullOrWhiteSpace(SceneFilePath)) if (SceneFilePath.IsWhiteSpace)
{ {
SaveSceneAs(); SaveSceneAs();
return; return;
@@ -799,25 +656,27 @@ namespace GlitchyEditor
if(ImGui.BeginMenu("View", true)) if(ImGui.BeginMenu("View", true))
{ {
if(ImGui.MenuItem(EntityHierarchyWindow.s_WindowTitle))
{
_editor.EntityHierarchyWindow.Open = true;
}
if(ImGui.MenuItem(ComponentEditWindow.s_WindowTitle)) if(ImGui.MenuItem(ComponentEditWindow.s_WindowTitle))
{
_editor.ComponentEditWindow.Open = true; _editor.ComponentEditWindow.Open = true;
}
if(ImGui.MenuItem(SceneViewportWindow.s_WindowTitle)) if(ImGui.MenuItem(ContentBrowserWindow.s_WindowTitle))
{ _editor.ComponentEditWindow.Open = true;
if(ImGui.MenuItem(EditorViewportWindow.s_WindowTitle))
_editor.SceneViewportWindow.Open = true; _editor.SceneViewportWindow.Open = true;
}
if(ImGui.MenuItem(EntityHierarchyWindow.s_WindowTitle))
_editor.EntityHierarchyWindow.Open = true;
if(ImGui.MenuItem(GameViewportWindow.s_WindowTitle))
_editor.GameViewportWindow.Open = true;
if(ImGui.MenuItem(GameViewportWindow.s_WindowTitle))
_editor.GameViewportWindow.Open = true;
ImGui.EndMenu(); ImGui.EndMenu();
} }
ImGui.EndMainMenuBar(); ImGui.EndMainMenuBar();
} }
-8
View File
@@ -183,14 +183,6 @@ class SceneRenderer
Renderer2D.DrawCircle(transform.WorldTransform, circle, entity.Index); Renderer2D.DrawCircle(transform.WorldTransform, circle, entity.Index);
} }
Renderer2D.DrawLine(Vector3.Zero, Vector3.Up, .Red);
Renderer2D.DrawLine(Vector3.Zero, Vector3(0, 0, 1), .Green);
Renderer2D.DrawRay(Vector3.Zero, Vector3(1, 0, 0), .Blue);
Renderer2D.DrawRect(Vector2(2.0f, 1.0f), Vector2(1.0f, 2.0f), .Yellow);
Renderer2D.DrawRect(Matrix.Translation(2, 2, 1) * Matrix.RotationZ(MathHelper.PiOverFour), .Purple);
Renderer2D.EndScene(); Renderer2D.EndScene();
Renderer2D.BeginScene(camera, .BackToFront); Renderer2D.BeginScene(camera, .BackToFront);