Fixes for array nodes in Visject

This commit is contained in:
Wojciech Figat
2021-11-19 16:04:13 +01:00
parent 3f3d8fc494
commit 52d79ca15a
3 changed files with 43 additions and 12 deletions

View File

@@ -50,7 +50,7 @@ namespace FlaxEditor.Surface.Archetypes
Size = new Vector2(150, 40),
ConnectionsHints = ConnectionsHint.Array,
IndependentBoxes = new int[] { 0 },
DependentBoxes = new int[] { },
DependentBoxes = new int[] { 1 },
DependentBoxFilter = GetArrayItemType,
Elements = new[]
{
@@ -66,7 +66,7 @@ namespace FlaxEditor.Surface.Archetypes
Description = "Returns the zero-based index of the item found in the array or -1 if nothing found.",
AlternativeTitles = new[] { "IndexOf", "Find" },
Flags = NodeFlags.VisualScriptGraph | NodeFlags.AnimGraph,
Size = new Vector2(150, 40),
Size = new Vector2(170, 40),
ConnectionsHints = ConnectionsHint.Array,
IndependentBoxes = new int[] { 0 },
DependentBoxes = new int[] { 1, 2 },
@@ -85,7 +85,7 @@ namespace FlaxEditor.Surface.Archetypes
Description = "Returns the zero-based index of the item found in the array or -1 if nothing found (searches from back to front).",
AlternativeTitles = new[] { "LastIndexOf", "FindLast" },
Flags = NodeFlags.VisualScriptGraph | NodeFlags.AnimGraph,
Size = new Vector2(150, 40),
Size = new Vector2(170, 40),
ConnectionsHints = ConnectionsHint.Array,
IndependentBoxes = new int[] { 0 },
DependentBoxes = new int[] { 1, 2 },

View File

@@ -78,6 +78,7 @@ namespace FlaxEditor.Surface.Archetypes
private TypePickerControl _typePicker;
private Button _addButton;
private Button _removeButton;
private bool _isUpdatingUI;
public ArrayNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch)
: base(id, context, nodeArch, groupArch)
@@ -86,9 +87,9 @@ namespace FlaxEditor.Surface.Archetypes
public override void OnValuesChanged()
{
base.OnValuesChanged();
UpdateUI();
base.OnValuesChanged();
}
public override void OnLoaded()
@@ -122,7 +123,13 @@ namespace FlaxEditor.Surface.Archetypes
private void Set(int length)
{
SetValue(0, Array.CreateInstance(TypeUtils.GetType(_typePicker.Value), length));
if (_isUpdatingUI)
return;
var prev = (Array)Values[0];
var next = Array.CreateInstance(TypeUtils.GetType(_typePicker.Value), length);
if (prev.GetType() == next.GetType())
Array.Copy(prev, next, Mathf.Min(prev.Length, next.Length));
SetValue(0, next);
}
public override void OnSurfaceCanEditChanged(bool canEdit)
@@ -146,11 +153,15 @@ namespace FlaxEditor.Surface.Archetypes
private void UpdateUI()
{
if (_isUpdatingUI)
return;
var array = (Array)Values[0];
var arrayType = array.GetType();
var elementType = new ScriptType(arrayType.GetElementType());
_isUpdatingUI = true;
_typePicker.Value = elementType;
_output.CurrentType = new ScriptType(arrayType);
_isUpdatingUI = false;
var count = array.Length;
var countMin = 0;

View File

@@ -928,8 +928,7 @@ namespace FlaxEditor.Surface.Elements
/// </summary>
public void UpdateDefaultValue()
{
var currentType = CurrentType.Type;
if (_defaultValueEditor != null && currentType != null)
if (_defaultValueEditor != null && _currentType.Type != null)
{
_editor.UpdateDefaultValue(this, _defaultValueEditor);
}
@@ -986,7 +985,7 @@ namespace FlaxEditor.Surface.Elements
{
base.OnCurrentTypeChanged();
if (_defaultValueEditor != null && !_editor.IsValid(this, _defaultValueEditor))
if (_defaultValueEditor != null && !(_editor.IsValid(this, _defaultValueEditor) && _editor.CanUse(this, ref _currentType)))
{
_defaultValueEditor.Dispose();
_defaultValueEditor = null;
@@ -1056,6 +1055,19 @@ namespace FlaxEditor.Surface.Elements
return base.OnMouseUp(location, button);
}
/// <inheritdoc />
public override void OnDestroy()
{
if (_defaultValueEditor != null)
{
_defaultValueEditor.Dispose();
_defaultValueEditor = null;
_editor = null;
}
base.OnDestroy();
}
private bool GetClipboardValue(out object result, bool deserialize)
{
@@ -1159,9 +1171,17 @@ namespace FlaxEditor.Surface.Elements
{
var bounds = new Rectangle(X + Width + 8 + Style.Current.FontSmall.MeasureText(Text).X, Y, 90, Height);
_editor = DefaultValueEditors[i];
_defaultValueEditor = _editor.Create(this, ref bounds);
if (_attributes != null)
_editor.UpdateAttributes(this, _attributes, _defaultValueEditor);
try
{
_defaultValueEditor = _editor.Create(this, ref bounds);
if (_attributes != null)
_editor.UpdateAttributes(this, _attributes, _defaultValueEditor);
}
catch (Exception ex)
{
Editor.LogWarning(ex);
_editor = null;
}
break;
}
}