Fix leftover UI control state when it gets disabled/hidden/reparented

This commit is contained in:
Wojciech Figat
2023-01-16 15:08:57 +01:00
parent 99ce5cd4d4
commit fc65e5a7a6
3 changed files with 46 additions and 26 deletions

View File

@@ -205,6 +205,15 @@ namespace FlaxEngine.GUI
BorderColorHighlighted = BorderColor;
}
/// <inheritdoc />
public override void ClearState()
{
base.ClearState();
if (_isPressed)
OnPressEnd();
}
/// <inheritdoc />
public override void DrawSelf()
{

View File

@@ -750,6 +750,20 @@ namespace FlaxEngine.GUI
}
}
/// <inheritdoc />
public override void ClearState()
{
base.ClearState();
// Clear state for any nested controls
for (int i = 0; i < _children.Count; i++)
{
var child = _children[i];
//if (child.Enabled && child.Enabled)
child.ClearState();
}
}
/// <summary>
/// Draw the control and the children.
/// </summary>

View File

@@ -131,6 +131,7 @@ namespace FlaxEngine.GUI
else
{
oldParentSize = Float2.Zero;
ClearState();
}
_parent = value;
@@ -214,20 +215,8 @@ namespace FlaxEngine.GUI
if (_isEnabled != value)
{
_isEnabled = value;
// Check if control has been disabled
if (!_isEnabled)
{
Defocus();
// Clear flags
if (_isMouseOver)
OnMouseLeave();
if (_isDragOver)
OnDragLeave();
while (_touchOvers != null && _touchOvers.Count != 0)
OnTouchLeave(_touchOvers[0]);
}
ClearState();
}
}
}
@@ -259,20 +248,8 @@ namespace FlaxEngine.GUI
if (_isVisible != value)
{
_isVisible = value;
// Check on control hide event
if (!_isVisible)
{
Defocus();
// Clear flags
if (_isMouseOver)
OnMouseLeave();
if (_isDragOver)
OnDragLeave();
while (_touchOvers != null && _touchOvers.Count != 0)
OnTouchLeave(_touchOvers[0]);
}
ClearState();
OnVisibleChanged();
_parent?.PerformLayout();
@@ -453,6 +430,20 @@ namespace FlaxEngine.GUI
{
}
/// <summary>
/// Called to clear UI state. For example, removes mouse over state or drag and drop when control gets disabled or hidden (including hierarchy).
/// </summary>
public virtual void ClearState()
{
Defocus();
if (_isMouseOver)
OnMouseLeave();
if (_isDragOver)
OnDragLeave();
while (_touchOvers != null && _touchOvers.Count != 0)
OnTouchLeave(_touchOvers[0]);
}
#region Focus
/// <summary>
@@ -1338,6 +1329,12 @@ namespace FlaxEngine.GUI
/// </summary>
protected virtual void OnVisibleChanged()
{
// Clear state when control gets hidden
if (!_isVisible && _isMouseOver)
{
OnMouseLeave();
}
VisibleChanged?.Invoke(this);
}