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; Index = index;
SetupContextMenu += OnSetupContextMenu; 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. // Extend margin of the label to support a dragging handle.
Margin m = Margin; Margin m = Margin;
@@ -88,13 +88,12 @@ namespace FlaxEditor.CustomEditors.Editors
_arrangeButtonInUse = false; _arrangeButtonInUse = false;
} }
/// <inheritdoc /> /// <inheritdoc />
public override void Draw() public override void Draw()
{ {
base.Draw(); base.Draw();
var style = FlaxEngine.GUI.Style.Current;
var style = FlaxEngine.GUI.Style.Current;
var mousePosition = PointFromScreen(Input.MouseScreenPosition); var mousePosition = PointFromScreen(Input.MouseScreenPosition);
var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey; var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey;
Render2D.DrawSprite(FlaxEditor.Editor.Instance.Icons.DragBar12, _arrangeButtonRect, _arrangeButtonInUse ? Color.Orange : dragBarColor); 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) private bool ArrangeAreaCheck(out int index, out Rectangle rect)
{ {
var child = Editor.ChildrenEditors[0]; var child = Editor.ChildrenEditors[0];
@@ -278,10 +285,10 @@ namespace FlaxEditor.CustomEditors.Editors
public override void Draw() public override void Draw()
{ {
base.Draw(); base.Draw();
if (_canReorder) if (_canReorder)
{ {
var style = FlaxEngine.GUI.Style.Current; var style = FlaxEngine.GUI.Style.Current;
var mousePosition = PointFromScreen(Input.MouseScreenPosition); var mousePosition = PointFromScreen(Input.MouseScreenPosition);
var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey; var dragBarColor = _arrangeButtonRect.Contains(mousePosition) ? style.Foreground : style.ForegroundGrey;
Render2D.DrawSprite(FlaxEditor.Editor.Instance.Icons.DragBar12, _arrangeButtonRect, _arrangeButtonInUse ? Color.Orange : dragBarColor); 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.GetProperties().Length == 1 && elementType.GetFields().Length == 0) ||
elementType.Equals(new ScriptType(typeof(JsonAsset))) || elementType.Equals(new ScriptType(typeof(JsonAsset))) ||
elementType.Equals(new ScriptType(typeof(SettingsBase))); elementType.Equals(new ScriptType(typeof(SettingsBase)));
bool prevWasNestedPropertiesList = false;
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
// Apply spacing // Apply spacing
@@ -538,6 +546,7 @@ namespace FlaxEditor.CustomEditors.Editors
itemLabel.LinkedEditor = itemLayout.Object(new ListValueContainer(elementType, i, Values, attributes), overrideEditor); itemLabel.LinkedEditor = itemLayout.Object(new ListValueContainer(elementType, i, Values, attributes), overrideEditor);
if (_readOnly && itemLayout.Children.Count > 0) if (_readOnly && itemLayout.Children.Count > 0)
GenericEditor.OnReadOnlyProperty(itemLayout); GenericEditor.OnReadOnlyProperty(itemLayout);
prevWasNestedPropertiesList = false;
} }
else if (_displayType == CollectionAttribute.DisplayType.Header || (_displayType == CollectionAttribute.DisplayType.Default && !single)) 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); cdp.CustomControl.LinkedEditor = itemLayout.Object(new ListValueContainer(elementType, i, Values, attributes), overrideEditor);
if (_readOnly && itemLayout.Children.Count > 0) if (_readOnly && itemLayout.Children.Count > 0)
GenericEditor.OnReadOnlyProperty(itemLayout); GenericEditor.OnReadOnlyProperty(itemLayout);
prevWasNestedPropertiesList = false;
} }
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@@ -245,16 +245,25 @@ namespace FlaxEditor.CustomEditors.GUI
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var label = _element.Labels[i]; var label = _element.Labels[i];
var container = label.FirstChildControlContainer ?? this;
if (label.FirstChildControlIndex < 0) if (label.FirstChildControlIndex < 0)
yStarts[i] = 0; yStarts[i] = 0;
else if (_children.Count <= label.FirstChildControlIndex) else if (container.ChildrenCount <= label.FirstChildControlIndex)
yStarts[i] = y; 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 else
{ {
yStarts[i] = _children[label.FirstChildControlIndex].Top; var firstChild = _children[label.FirstChildControlIndex];
yStarts[i] = firstChild.Top;
if (i == count - 1) 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> /// </summary>
internal int FirstChildControlIndex; 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> /// <summary>
/// The linked custom editor (shows the label property). /// The linked custom editor (shows the label property).
/// </summary> /// </summary>
@@ -154,8 +159,16 @@ namespace FlaxEditor.CustomEditors.GUI
public override void OnDestroy() public override void OnDestroy()
{ {
SetupContextMenu = null; SetupContextMenu = null;
LinkedEditor = null;
FirstChildControlContainer = null;
base.OnDestroy(); base.OnDestroy();
} }
/// <inheritdoc />
public override string ToString()
{
return Text.ToString();
}
} }
} }

View File

@@ -666,7 +666,20 @@ namespace FlaxEditor.CustomEditors
} }
var property = AddPropertyItem(name, tooltip); 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> /// <summary>