From 826a330f636dacc3c9947bb7035a30e9daea0396 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sun, 31 Dec 2023 16:06:38 -0600 Subject: [PATCH 1/3] Add asset reference to Guid editor for easy picking of specific Guid based on type in editor. --- .../CustomEditors/Editors/GuidEditor.cs | 82 +++++++++++++++++-- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/GuidEditor.cs b/Source/Editor/CustomEditors/Editors/GuidEditor.cs index 28d93aa8e..adba58e26 100644 --- a/Source/Editor/CustomEditors/Editors/GuidEditor.cs +++ b/Source/Editor/CustomEditors/Editors/GuidEditor.cs @@ -1,8 +1,12 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; +using System.Linq; using FlaxEditor.CustomEditors.Elements; +using FlaxEditor.GUI; +using FlaxEditor.Scripting; using FlaxEngine; +using FlaxEngine.Utilities; namespace FlaxEditor.CustomEditors.Editors { @@ -13,6 +17,9 @@ namespace FlaxEditor.CustomEditors.Editors public sealed class GuidEditor : CustomEditor { private TextBoxElement _element; + private AssetPicker _picker; + private bool _isReference; + private bool _isRefreshing; /// public override DisplayStyle Style => DisplayStyle.Inline; @@ -20,8 +27,57 @@ namespace FlaxEditor.CustomEditors.Editors /// public override void Initialize(LayoutElementsContainer layout) { - _element = layout.TextBox(); - _element.TextBox.EditEnd += OnEditEnd; + + var attributes = Values.GetAttributes(); + var assetReference = (AssetReferenceAttribute)attributes?.FirstOrDefault(x => x is AssetReferenceAttribute); + if (assetReference != null) + { + _picker = layout.Custom().CustomControl; + ScriptType assetType = new ScriptType(); + + float height = 48; + if (assetReference.UseSmallPicker) + height = 32; + + if (string.IsNullOrEmpty(assetReference.TypeName)) + { + assetType = ScriptType.Void; + } + else if (assetReference.TypeName.Length > 1 && assetReference.TypeName[0] == '.') + { + // Generic file picker + assetType = ScriptType.Null; + _picker.Validator.FileExtension = assetReference.TypeName; + } + else + { + var customType = TypeUtils.GetType(assetReference.TypeName); + if (customType != ScriptType.Null) + assetType = customType; + else if (!Content.Settings.GameSettings.OptionalPlatformSettings.Contains(assetReference.TypeName)) + Debug.LogWarning(string.Format("Unknown asset type '{0}' to use for asset picker filter.", assetReference.TypeName)); + else + assetType = ScriptType.Void; + } + + _picker.Validator.AssetType = assetType; + _picker.Height = height; + _picker.SelectedItemChanged += OnSelectedItemChanged; + _isReference = true; + + } + else + { + _element = layout.TextBox(); + _element.TextBox.EditEnd += OnEditEnd; + } + } + + private void OnSelectedItemChanged() + { + if (_isRefreshing) + return; + SetValue(_picker.Validator.SelectedID); } private void OnEditEnd() @@ -39,13 +95,27 @@ namespace FlaxEditor.CustomEditors.Editors if (HasDifferentValues) { - _element.TextBox.Text = string.Empty; - _element.TextBox.WatermarkText = "Different values"; + if (_isReference) + { + //_picker.Validator.SelectedID = (Guid)Values[0]; + } + else + { + _element.TextBox.Text = string.Empty; + _element.TextBox.WatermarkText = "Different values"; + } } else { - _element.TextBox.Text = ((Guid)Values[0]).ToString("D"); - _element.TextBox.WatermarkText = string.Empty; + if (_isReference) + { + _picker.Validator.SelectedID = (Guid)Values[0]; + } + else + { + _element.TextBox.Text = ((Guid)Values[0]).ToString("D"); + _element.TextBox.WatermarkText = string.Empty; + } } } } From 57d4f0ff7ddcc016675c5329ec792436d1d5c545 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sun, 31 Dec 2023 16:09:32 -0600 Subject: [PATCH 2/3] Clean up code --- Source/Editor/CustomEditors/Editors/GuidEditor.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/GuidEditor.cs b/Source/Editor/CustomEditors/Editors/GuidEditor.cs index adba58e26..a151f308a 100644 --- a/Source/Editor/CustomEditors/Editors/GuidEditor.cs +++ b/Source/Editor/CustomEditors/Editors/GuidEditor.cs @@ -92,12 +92,12 @@ namespace FlaxEditor.CustomEditors.Editors public override void Refresh() { base.Refresh(); - + _isRefreshing = true; if (HasDifferentValues) { if (_isReference) { - //_picker.Validator.SelectedID = (Guid)Values[0]; + // Not supported } else { @@ -117,6 +117,7 @@ namespace FlaxEditor.CustomEditors.Editors _element.TextBox.WatermarkText = string.Empty; } } + _isRefreshing = false; } } } From 8bff9556defcaa66c28024c5f27f512261ff3be2 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Sun, 31 Dec 2023 16:36:34 -0600 Subject: [PATCH 3/3] Code cleanup --- Source/Editor/CustomEditors/Editors/GuidEditor.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/GuidEditor.cs b/Source/Editor/CustomEditors/Editors/GuidEditor.cs index a151f308a..4573def77 100644 --- a/Source/Editor/CustomEditors/Editors/GuidEditor.cs +++ b/Source/Editor/CustomEditors/Editors/GuidEditor.cs @@ -27,7 +27,6 @@ namespace FlaxEditor.CustomEditors.Editors /// public override void Initialize(LayoutElementsContainer layout) { - var attributes = Values.GetAttributes(); var assetReference = (AssetReferenceAttribute)attributes?.FirstOrDefault(x => x is AssetReferenceAttribute); if (assetReference != null) @@ -64,7 +63,6 @@ namespace FlaxEditor.CustomEditors.Editors _picker.Height = height; _picker.SelectedItemChanged += OnSelectedItemChanged; _isReference = true; - } else {