mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Moved material editing from component window to asset editor
- EditorContentManager now returns null if asset wasn't found (instead of crashing) - FixUp asset identifier when dragging from asset browser - Lock current asset in properties editor
This commit is contained in:
@@ -24,6 +24,7 @@ class AssetFile
|
||||
private EditorContentManager _contentManager;
|
||||
|
||||
private String _path;
|
||||
private String _identifier;
|
||||
private String _assetConfigPath;
|
||||
|
||||
private AssetConfig _assetConfig ~ delete _;
|
||||
@@ -35,6 +36,7 @@ class AssetFile
|
||||
public bool IsDirectory => _isDirectory;
|
||||
|
||||
public StringView FilePath => _path;
|
||||
public StringView Identifier => _identifier;
|
||||
|
||||
public const String ConfigFileExtension = ".ass";
|
||||
|
||||
@@ -43,11 +45,13 @@ class AssetFile
|
||||
public Object LoadedAsset => _loadedAsset;
|
||||
|
||||
[AllowAppend]
|
||||
public this(EditorContentManager contentManager, StringView path, bool isDirectory)
|
||||
public this(EditorContentManager contentManager, StringView identifier, StringView path, bool isDirectory)
|
||||
{
|
||||
String identifierBuffer = append String(identifier);
|
||||
String pathBuffer = append String(path);
|
||||
String configPathBuffer = append String(path.Length + ConfigFileExtension.Length);
|
||||
|
||||
_identifier = identifierBuffer;
|
||||
_path = pathBuffer;
|
||||
|
||||
configPathBuffer..Append(path).Append(ConfigFileExtension);
|
||||
|
||||
@@ -5,11 +5,34 @@ using System.Collections;
|
||||
using System.IO;
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Renderer;
|
||||
using ImGui;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
namespace GlitchyEditor.Assets;
|
||||
|
||||
class MaterialAssetPropertiesEditor : AssetPropertiesEditor
|
||||
{
|
||||
mixin DropAssetTarget<T>() where T : Asset
|
||||
{
|
||||
Asset asset = null;
|
||||
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
|
||||
|
||||
if (payload != null)
|
||||
{
|
||||
StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize);
|
||||
|
||||
asset = Content.LoadAsset<Asset>(fullpath);
|
||||
}
|
||||
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
|
||||
asset
|
||||
}
|
||||
|
||||
public this(AssetFile asset) : base(asset)
|
||||
{
|
||||
|
||||
@@ -17,7 +40,146 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
|
||||
|
||||
public override void ShowEditor()
|
||||
{
|
||||
Material material = Asset.LoadedAsset as Material;
|
||||
|
||||
if (material == null)
|
||||
return;
|
||||
|
||||
Effect effect = material?.Effect;
|
||||
|
||||
if (effect == null)
|
||||
return;
|
||||
|
||||
ShowTextures(material, effect);
|
||||
|
||||
ShowVariables(material, effect);
|
||||
}
|
||||
|
||||
private void ShowTextures(Material material, Effect effect)
|
||||
{
|
||||
for (let texture in effect.Textures)
|
||||
{
|
||||
ImGui.Button(texture.key);
|
||||
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
|
||||
|
||||
if (payload != null)
|
||||
{
|
||||
StringView path = .((char8*)payload.Data, (int)payload.DataSize);
|
||||
|
||||
using (Texture2D newTexture = Content.LoadAsset<Texture2D>(path))//new Texture2D(path, true))
|
||||
{
|
||||
newTexture.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||
material.SetTexture(texture.key, newTexture);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowVariables(Material material, Effect effect)
|
||||
{
|
||||
bool TryGetValue(Dictionary<String, Variant> parameters, String name, out Variant value)
|
||||
{
|
||||
if (parameters.TryGetValue(name, let param))
|
||||
{
|
||||
value = param;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = ?;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let (name, arguments) in effect.[Friend]_variableDescriptions)
|
||||
{
|
||||
let variable = effect.Variables[name];
|
||||
|
||||
bool hasPreviewName = TryGetValue(arguments, "Preview", var previewName);
|
||||
|
||||
StringView displayName = hasPreviewName ? previewName.Get<String>() : name;
|
||||
|
||||
bool hasPreviewType = TryGetValue(arguments, "Type", var previewType);
|
||||
|
||||
if (hasPreviewType && previewType.Get<String>() == "Color")
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1);
|
||||
|
||||
if (variable.Columns == 3)
|
||||
{
|
||||
material.GetVariable<Vector3>(variable.Name, var value);
|
||||
|
||||
value = (Vector3)ColorRGB.LinearToSRGB((ColorRGB)value);
|
||||
|
||||
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value))
|
||||
{
|
||||
value = (Vector3)ColorRGB.SRgbToLinear((ColorRGB)value);
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
else if (variable.Columns == 4)
|
||||
{
|
||||
material.GetVariable<Vector4>(variable.Name, var value);
|
||||
|
||||
value = (Vector4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
|
||||
|
||||
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value))
|
||||
{
|
||||
value = (Vector4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (variable.Type == .Float && variable.Rows == 1)
|
||||
{
|
||||
bool hasMin = TryGetValue(arguments, "Min", var min);
|
||||
bool hasMax = TryGetValue(arguments, "Max", var max);
|
||||
|
||||
for (int r < variable.Rows)
|
||||
{
|
||||
switch (variable.Columns)
|
||||
{
|
||||
case 1:
|
||||
material.GetVariable<float>(variable.Name, var value);
|
||||
|
||||
float[1] minV = hasMin ? min.Get<float[1]>() : .(float.MinValue);
|
||||
float[1] maxV = hasMax ? max.Get<float[1]>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector<1>(displayName, ref *(float[1]*)&value, .(), 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 2:
|
||||
material.GetVariable<Vector2>(variable.Name, var value);
|
||||
|
||||
Vector2 minV = hasMin ? min.Get<Vector2>() : .(float.MinValue);
|
||||
Vector2 maxV = hasMax ? max.Get<Vector2>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector2(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 3:
|
||||
material.GetVariable<Vector3>(variable.Name, var value);
|
||||
|
||||
Vector3 minV = hasMin ? min.Get<Vector3>() : .(float.MinValue);
|
||||
Vector3 maxV = hasMax ? max.Get<Vector3>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector3(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 4:
|
||||
material.GetVariable<Vector4>(variable.Name, var value);
|
||||
|
||||
Vector4 minV = hasMin ? min.Get<Vector4>() : .(float.MinValue);
|
||||
Vector4 maxV = hasMax ? max.Get<Vector4>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector4(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static AssetPropertiesEditor Factory(AssetFile assetFile)
|
||||
|
||||
@@ -285,9 +285,14 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
private static void ShowMeshRendererComponentEditor(Entity entity, MeshRendererComponent* meshRendererComponent)
|
||||
{
|
||||
// TODO: Editing material options obviously shouldn't be part of the meshrenderer-ui
|
||||
ImGui.TextUnformatted("Material:");
|
||||
ImGui.SameLine();
|
||||
|
||||
Material material = meshRendererComponent.Material;
|
||||
|
||||
StringView identifier = material?.Identifier ?? "None";
|
||||
ImGui.Button(identifier.ToScopeCStr!());
|
||||
|
||||
ImGui.Button("Drag Material here!");
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
|
||||
@@ -296,145 +301,21 @@ namespace GlitchyEditor.EditWindows
|
||||
{
|
||||
StringView fullpath = .((char8*)payload.Data, (int)payload.DataSize);
|
||||
|
||||
using (Material material = Content.LoadAsset<Material>(fullpath))
|
||||
using (Material loadedMaterial = Content.LoadAsset<Material>(fullpath))
|
||||
{
|
||||
meshRendererComponent.Material = material;
|
||||
meshRendererComponent.Material = loadedMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
|
||||
Material material = meshRendererComponent.Material;
|
||||
|
||||
Effect effect = material?.Effect;
|
||||
/*Effect effect = material?.Effect;
|
||||
|
||||
if (effect == null)
|
||||
return;
|
||||
return;*/
|
||||
|
||||
bool TryGetValue(Dictionary<String, Variant> parameters, String name, out Variant value)
|
||||
{
|
||||
if (parameters.TryGetValue(name, let param))
|
||||
{
|
||||
value = param;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = ?;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let texture in effect.Textures)
|
||||
{
|
||||
//ImGui.Text(texture.key);
|
||||
ImGui.Button(texture.key);
|
||||
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
|
||||
|
||||
if (payload != null)
|
||||
{
|
||||
Log.EngineLogger.Warning("");
|
||||
|
||||
StringView path = .((char8*)payload.Data, (int)payload.DataSize);
|
||||
|
||||
using (Texture2D newTexture = Content.LoadAsset<Texture2D>(path))//new Texture2D(path, true))
|
||||
{
|
||||
newTexture.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||
material.SetTexture(texture.key, newTexture);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndDragDropTarget();
|
||||
}
|
||||
}
|
||||
|
||||
for (let (name, arguments) in effect.[Friend]_variableDescriptions)
|
||||
{
|
||||
let variable = effect.Variables[name];
|
||||
|
||||
bool hasPreviewName = TryGetValue(arguments, "Preview", var previewName);
|
||||
|
||||
StringView displayName = hasPreviewName ? previewName.Get<String>() : name;
|
||||
|
||||
bool hasPreviewType = TryGetValue(arguments, "Type", var previewType);
|
||||
|
||||
if (hasPreviewType && previewType.Get<String>() == "Color")
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1);
|
||||
|
||||
if (variable.Columns == 3)
|
||||
{
|
||||
material.GetVariable<Vector3>(variable.Name, var value);
|
||||
|
||||
value = (Vector3)ColorRGB.LinearToSRGB((ColorRGB)value);
|
||||
|
||||
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value))
|
||||
{
|
||||
value = (Vector3)ColorRGB.SRgbToLinear((ColorRGB)value);
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
else if (variable.Columns == 4)
|
||||
{
|
||||
material.GetVariable<Vector4>(variable.Name, var value);
|
||||
|
||||
value = (Vector4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
|
||||
|
||||
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value))
|
||||
{
|
||||
value = (Vector4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (variable.Type == .Float && variable.Rows == 1)
|
||||
{
|
||||
bool hasMin = TryGetValue(arguments, "Min", var min);
|
||||
bool hasMax = TryGetValue(arguments, "Max", var max);
|
||||
|
||||
for (int r < variable.Rows)
|
||||
{
|
||||
switch (variable.Columns)
|
||||
{
|
||||
case 1:
|
||||
material.GetVariable<float>(variable.Name, var value);
|
||||
|
||||
float[1] minV = hasMin ? min.Get<float[1]>() : .(float.MinValue);
|
||||
float[1] maxV = hasMax ? max.Get<float[1]>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector<1>(displayName, ref *(float[1]*)&value, .(), 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 2:
|
||||
material.GetVariable<Vector2>(variable.Name, var value);
|
||||
|
||||
Vector2 minV = hasMin ? min.Get<Vector2>() : .(float.MinValue);
|
||||
Vector2 maxV = hasMax ? max.Get<Vector2>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector2(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 3:
|
||||
material.GetVariable<Vector3>(variable.Name, var value);
|
||||
|
||||
Vector3 minV = hasMin ? min.Get<Vector3>() : .(float.MinValue);
|
||||
Vector3 maxV = hasMax ? max.Get<Vector3>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector3(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
case 4:
|
||||
material.GetVariable<Vector4>(variable.Name, var value);
|
||||
|
||||
Vector4 minV = hasMin ? min.Get<Vector4>() : .(float.MinValue);
|
||||
Vector4 maxV = hasMax ? max.Get<Vector4>() : .(float.MaxValue);
|
||||
|
||||
if (ImGui.EditVector4(displayName, ref value, .Zero, 0.1f, 100.0f, minV, maxV))
|
||||
material.SetVariable(variable.Name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Show a preview of the material here!
|
||||
}
|
||||
|
||||
private static void ShowRigidBody2DComponentEditor(Entity entity, Rigidbody2DComponent* rigidBodyComponent)
|
||||
@@ -580,7 +461,14 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
private static void ShowMeshComponentEditor(Entity entity, MeshComponent* meshComponent)
|
||||
{
|
||||
ImGui.Button("Drag Mesh here!");
|
||||
ImGui.TextUnformatted("Mesh:");
|
||||
ImGui.SameLine();
|
||||
|
||||
GeometryBinding mesh = meshComponent.Mesh;
|
||||
|
||||
StringView identifier = mesh?.Identifier ?? "None";
|
||||
ImGui.Button(identifier.ToScopeCStr!());
|
||||
|
||||
if (ImGui.BeginDragDropTarget())
|
||||
{
|
||||
ImGui.Payload* payload = ImGui.AcceptDragDropPayload("CONTENT_BROWSER_ITEM");
|
||||
@@ -605,7 +493,7 @@ namespace GlitchyEditor.EditWindows
|
||||
meshComponent.Mesh = geometry;
|
||||
}
|
||||
|
||||
// TODO: support multiple primitives (treat every primitive as a single mesh?)
|
||||
// TODO: support multiple primitives (treat every primitive as a single mesh? or: mesh can have multiple primitives)
|
||||
/*using (GeometryBinding binding = ModelLoader.LoadMesh(filePath, meshName, 0))
|
||||
{
|
||||
meshComponent.Mesh = binding;
|
||||
|
||||
@@ -197,6 +197,8 @@ namespace GlitchyEditor.EditWindows
|
||||
if (fullpath.StartsWith(_manager.ContentDirectory, .OrdinalIgnoreCase))
|
||||
fullpath.Remove(0, _manager.ContentDirectory.Length);
|
||||
|
||||
Path.Fixup(fullpath);
|
||||
|
||||
ImGui.SetDragDropPayload("CONTENT_BROWSER_ITEM", fullpath.CStr(), (.)fullpath.Length, .Once);
|
||||
|
||||
ImGui.EndDragDropSource();
|
||||
|
||||
@@ -12,6 +12,14 @@ class PropertiesWindow : EditorWindow
|
||||
{
|
||||
private AssetPropertiesEditor _currentPropertiesEditor ~ delete _;
|
||||
|
||||
private bool _lockCurrentAsset;
|
||||
|
||||
private bool _selectedNewAsset;
|
||||
|
||||
private append String _selectedFileName = .();
|
||||
|
||||
private Asset _currentAsset ~ _.ReleaseRef();
|
||||
|
||||
public this(Editor editor)
|
||||
{
|
||||
_editor = editor;
|
||||
@@ -23,6 +31,10 @@ class PropertiesWindow : EditorWindow
|
||||
if(!ImGui.Begin("Properties", &_open, .None))
|
||||
return;
|
||||
|
||||
// TODO: make a little button in title bar?
|
||||
ImGui.Checkbox("Lock", &_lockCurrentAsset);
|
||||
ImGui.Separator();
|
||||
|
||||
ShowAssetProperties();
|
||||
}
|
||||
|
||||
@@ -30,9 +42,18 @@ class PropertiesWindow : EditorWindow
|
||||
/// @returns the AssetFile for the currently selected asset of null, if no file is selected.
|
||||
private AssetFile GetCurrentAssetFile()
|
||||
{
|
||||
StringView selectedFileName = _editor.ContentBrowserWindow.SelectedFile;
|
||||
// Only grab the currently selected file if we aren't locked
|
||||
if (!_lockCurrentAsset)
|
||||
{
|
||||
StringView selectedInFileBrowser = _editor.ContentBrowserWindow.SelectedFile;
|
||||
|
||||
Result<TreeNode<AssetNode>> treeNode = _editor.ContentManager.AssetHierarchy.GetNodeFromPath(selectedFileName);
|
||||
if (_selectedFileName != selectedInFileBrowser)
|
||||
{
|
||||
_selectedFileName.Set(_editor.ContentBrowserWindow.SelectedFile);
|
||||
}
|
||||
}
|
||||
|
||||
Result<TreeNode<AssetNode>> treeNode = _editor.ContentManager.AssetHierarchy.GetNodeFromPath(_selectedFileName);
|
||||
|
||||
if (treeNode case .Ok(let assetNode))
|
||||
return assetNode->AssetFile;
|
||||
@@ -53,6 +74,13 @@ class PropertiesWindow : EditorWindow
|
||||
if (assetFile == null)
|
||||
return;
|
||||
|
||||
// We need the actual asset for preview and sometimes for editing
|
||||
if (_currentAsset?.Identifier != assetFile.Identifier)
|
||||
{
|
||||
_currentAsset?.ReleaseRef();
|
||||
_currentAsset = _editor.ContentManager.LoadAsset(assetFile.Identifier);
|
||||
}
|
||||
|
||||
// TODO: allow changing AssetLoader
|
||||
// assetFile.AssetConfig.AssetLoade
|
||||
|
||||
@@ -63,6 +91,10 @@ class PropertiesWindow : EditorWindow
|
||||
ImGui.SetTooltip("If checked this file will be ignored and not treated as an asset.");*/
|
||||
|
||||
ShowPropertiesEditor(assetFile);
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
// TODO: preview asset
|
||||
}
|
||||
|
||||
private void ShowPropertiesEditor(AssetFile assetFile)
|
||||
|
||||
@@ -234,7 +234,10 @@ class AssetHierarchy
|
||||
|
||||
void HandleFile(AssetNode node)
|
||||
{
|
||||
node.AssetFile = new AssetFile(_contentManager, node.Path, node.IsDirectory);
|
||||
String identifier = scope .(node.Path.Length);
|
||||
Path.GetRelativePath(node.Path, _contentDirectory, identifier);
|
||||
|
||||
node.AssetFile = new AssetFile(_contentManager, identifier, node.Path, node.IsDirectory);
|
||||
}
|
||||
|
||||
/// Determines the files that belong to the given directory and adds them to the tree.
|
||||
@@ -385,7 +388,9 @@ class AssetHierarchy
|
||||
|
||||
if (!(nodeResult case .Ok(out node)))
|
||||
{
|
||||
Log.EngineLogger.Error($"Could not find node for file \"{fileNameWithContentRoot}\"");
|
||||
// This happens, when we create new files.
|
||||
Log.EngineLogger.Trace($"Could not find node for file \"{fileNameWithContentRoot}\"");
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't fire event for directories.
|
||||
@@ -592,7 +597,10 @@ class EditorContentManager : IContentManager
|
||||
Result<TreeNode<AssetNode>> resultNode = AssetHierarchy.GetNodeFromPath(filePath);
|
||||
|
||||
if (resultNode case .Err)
|
||||
Runtime.FatalError();
|
||||
{
|
||||
Log.EngineLogger.Error($"Could not find asset \"{filePath}\".");
|
||||
return null;
|
||||
}
|
||||
|
||||
//AssetFile file = scope .(this, filePath, false);
|
||||
AssetFile file = resultNode->Value.AssetFile;
|
||||
|
||||
Reference in New Issue
Block a user