Fix properties editor labels positioning

#2530 #2609
This commit is contained in:
Wojtek Figat
2024-08-30 12:58:12 +02:00
parent e925af534e
commit 68eb8b9f21
8 changed files with 68 additions and 26 deletions

View File

@@ -53,7 +53,7 @@ namespace FlaxEditor.CustomEditors.Editors
Index = index;
SetupContextMenu += OnSetupContextMenu;
_arrangeButtonRect = new Rectangle(2, 3, 12, 12);
_arrangeButtonRect = new Rectangle(2, 4, 12, 12);
// Extend margin of the label to support a dragging handle.
Margin m = Margin;
@@ -75,7 +75,7 @@ namespace FlaxEditor.CustomEditors.Editors
b = menu.AddButton("Move down", OnMoveDownClicked);
b.Enabled = Index + 1 < Editor.Count && !Editor._readOnly;
b = menu.AddButton("Remove", OnRemoveClicked);
b.Enabled = !Editor._readOnly;
}
@@ -88,13 +88,12 @@ namespace FlaxEditor.CustomEditors.Editors
_arrangeButtonInUse = false;
}
/// <inheritdoc />
public override void Draw()
{
base.Draw();
var style = FlaxEngine.GUI.Style.Current;
var style = FlaxEngine.GUI.Style.Current;
var mousePosition = PointFromScreen(Input.MouseScreenPosition);
var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey;
Render2D.DrawSprite(FlaxEditor.Editor.Instance.Icons.DragBar12, _arrangeButtonRect, _arrangeButtonInUse ? Color.Orange : dragBarColor);
@@ -104,6 +103,14 @@ namespace FlaxEditor.CustomEditors.Editors
}
}
/// <inheritdoc />
protected override void OnSizeChanged()
{
base.OnSizeChanged();
_arrangeButtonRect.Y = (Height - _arrangeButtonRect.Height) * 0.5f;
}
private bool ArrangeAreaCheck(out int index, out Rectangle rect)
{
var child = Editor.ChildrenEditors[0];
@@ -278,10 +285,10 @@ namespace FlaxEditor.CustomEditors.Editors
public override void Draw()
{
base.Draw();
if (_canReorder)
{
var style = FlaxEngine.GUI.Style.Current;
var mousePosition = PointFromScreen(Input.MouseScreenPosition);
var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey;
Render2D.DrawSprite(FlaxEditor.Editor.Instance.Icons.DragBar12, _arrangeButtonRect, _arrangeButtonInUse ? Color.Orange : dragBarColor);
@@ -519,6 +526,7 @@ namespace FlaxEditor.CustomEditors.Editors
(elementType.GetProperties().Length == 1 && elementType.GetFields().Length == 0) ||
elementType.Equals(new ScriptType(typeof(JsonAsset))) ||
elementType.Equals(new ScriptType(typeof(SettingsBase)));
bool prevWasNestedPropertiesList = false;
for (int i = 0; i < size; i++)
{
// Apply spacing
@@ -538,6 +546,7 @@ namespace FlaxEditor.CustomEditors.Editors
itemLabel.LinkedEditor = itemLayout.Object(new ListValueContainer(elementType, i, Values, attributes), overrideEditor);
if (_readOnly && itemLayout.Children.Count > 0)
GenericEditor.OnReadOnlyProperty(itemLayout);
prevWasNestedPropertiesList = false;
}
else if (_displayType == CollectionAttribute.DisplayType.Header || (_displayType == CollectionAttribute.DisplayType.Default && !single))
{
@@ -547,6 +556,7 @@ namespace FlaxEditor.CustomEditors.Editors
cdp.CustomControl.LinkedEditor = itemLayout.Object(new ListValueContainer(elementType, i, Values, attributes), overrideEditor);
if (_readOnly && itemLayout.Children.Count > 0)
GenericEditor.OnReadOnlyProperty(itemLayout);
prevWasNestedPropertiesList = false;
}
}
}
@@ -663,7 +673,7 @@ namespace FlaxEditor.CustomEditors.Editors
cloned[i] = tmp;
}
}
SetValue(cloned);
}

View File

@@ -560,19 +560,19 @@ namespace FlaxEditor.CustomEditors.Editors
internal static void OnReadOnlyProperty(LayoutElementsContainer itemLayout, int labelIndex = -1)
{
PropertiesListElement list = null;
PropertiesList list = null;
int firstChildControlIndex = 0;
bool disableSingle = true;
var control = itemLayout.Children[itemLayout.Children.Count - 1];
if (control is GroupElement group && group.Children.Count > 0)
{
list = group.Children[0] as PropertiesListElement;
list = (group.Children[0] as PropertiesListElement)?.Properties;
disableSingle = false; // Disable all nested editors
}
else if (control is PropertiesListElement list1 && labelIndex != -1)
{
list = list1;
firstChildControlIndex = list.Labels[labelIndex].FirstChildControlIndex;
list = list1.Labels[labelIndex].FirstChildControlContainer ?? list1.Properties;
firstChildControlIndex = list1.Labels[labelIndex].FirstChildControlIndex;
}
else if (control?.Control != null)
{
@@ -582,10 +582,10 @@ namespace FlaxEditor.CustomEditors.Editors
if (list != null)
{
// Disable controls added to the editor
var count = list.Properties.Children.Count;
var count = list.Children.Count;
for (int j = firstChildControlIndex; j < count; j++)
{
var child = list.Properties.Children[j];
var child = list.Children[j];
if (disableSingle && child is PropertyNameLabel)
break;

View File

@@ -33,10 +33,10 @@ namespace FlaxEditor.CustomEditors.Editors
if (layout.Children.Count == 0)
return;
var propList = layout.Children[layout.Children.Count - 1] as PropertiesListElement;
if (propList == null || propList.Children.Count != 2)
if (propList == null || propList.Children.Count < 2)
return;
var idElement = propList.Children[0] as TextBoxElement;
var valueElement = propList.Children[1] as TextBoxElement;
var idElement = propList.Children[propList.Children.Count - 2] as TextBoxElement;
var valueElement = propList.Children[propList.Children.Count - 1] as TextBoxElement;
if (idElement == null || valueElement == null)
return;
_idElement = idElement;

View File

@@ -53,13 +53,7 @@ namespace FlaxEditor.CustomEditors.Elements
internal void OnAddProperty(string name, string tooltip)
{
var label = new PropertyNameLabel(name)
{
Parent = Properties,
TooltipText = tooltip,
FirstChildControlIndex = Properties.Children.Count
};
Labels.Add(label);
OnAddProperty(new PropertyNameLabel(name), tooltip);
}
internal void OnAddProperty(PropertyNameLabel label, string tooltip)
@@ -88,6 +82,7 @@ namespace FlaxEditor.CustomEditors.Elements
public override void ClearLayout()
{
base.ClearLayout();
Labels.Clear();
}
}

View File

@@ -58,6 +58,8 @@ namespace FlaxEditor.CustomEditors.GUI
// Update child controls enabled state
if (FirstChildControlIndex >= 0 && Parent is PropertiesList propertiesList)
{
if (FirstChildControlContainer != null)
propertiesList = FirstChildControlContainer;
var controls = propertiesList.Children;
var labels = propertiesList.Element.Labels;
var thisIndex = labels.IndexOf(this);

View File

@@ -245,16 +245,25 @@ namespace FlaxEditor.CustomEditors.GUI
for (int i = 0; i < count; i++)
{
var label = _element.Labels[i];
var container = label.FirstChildControlContainer ?? this;
if (label.FirstChildControlIndex < 0)
yStarts[i] = 0;
else if (_children.Count <= label.FirstChildControlIndex)
else if (container.ChildrenCount <= label.FirstChildControlIndex)
yStarts[i] = y;
else if (label.FirstChildControlContainer != null)
{
var firstChild = label.FirstChildControlContainer.Children[label.FirstChildControlIndex];
yStarts[i] = firstChild.PointToParent(this, Float2.Zero).Y;
if (i == count - 1)
yStarts[i + 1] = firstChild.PointToParent(this, firstChild.Size).Y;
}
else
{
yStarts[i] = _children[label.FirstChildControlIndex].Top;
var firstChild = _children[label.FirstChildControlIndex];
yStarts[i] = firstChild.Top;
if (i == count - 1)
yStarts[i + 1] = _children[label.FirstChildControlIndex].Bottom;
yStarts[i + 1] = firstChild.Bottom;
}
}

View File

@@ -30,6 +30,11 @@ namespace FlaxEditor.CustomEditors.GUI
/// </summary>
internal int FirstChildControlIndex;
/// <summary>
/// Helper value used by the <see cref="PropertiesList"/> to draw property names in a proper area.
/// </summary>
internal PropertiesList FirstChildControlContainer;
/// <summary>
/// The linked custom editor (shows the label property).
/// </summary>
@@ -154,8 +159,16 @@ namespace FlaxEditor.CustomEditors.GUI
public override void OnDestroy()
{
SetupContextMenu = null;
LinkedEditor = null;
FirstChildControlContainer = null;
base.OnDestroy();
}
/// <inheritdoc />
public override string ToString()
{
return Text.ToString();
}
}
}

View File

@@ -666,7 +666,20 @@ namespace FlaxEditor.CustomEditors
}
var property = AddPropertyItem(name, tooltip);
return property.Object(values, editor);
int start = property.Properties.Children.Count;
var result = property.Object(values, editor);
// Special case when properties list is nested into another properties list (eg. array of structures or LocalizedString editor)
if (this is PropertiesListElement thisPropertiesList &&
editor.ParentEditor != null &&
editor.ParentEditor.LinkedLabel != null &&
editor.ParentEditor.LinkedLabel.FirstChildControlContainer == null)
{
editor.ParentEditor.LinkedLabel.FirstChildControlIndex = start;
editor.ParentEditor.LinkedLabel.FirstChildControlContainer = property.Properties;
}
return result;
}
/// <summary>