diff --git a/Source/Engine/UI/GUI/Common/Button.cs b/Source/Engine/UI/GUI/Common/Button.cs
index e1f41a5ff..0e0d29615 100644
--- a/Source/Engine/UI/GUI/Common/Button.cs
+++ b/Source/Engine/UI/GUI/Common/Button.cs
@@ -205,6 +205,15 @@ namespace FlaxEngine.GUI
BorderColorHighlighted = BorderColor;
}
+ ///
+ public override void ClearState()
+ {
+ base.ClearState();
+
+ if (_isPressed)
+ OnPressEnd();
+ }
+
///
public override void DrawSelf()
{
diff --git a/Source/Engine/UI/GUI/ContainerControl.cs b/Source/Engine/UI/GUI/ContainerControl.cs
index 9e226edb5..365889cbf 100644
--- a/Source/Engine/UI/GUI/ContainerControl.cs
+++ b/Source/Engine/UI/GUI/ContainerControl.cs
@@ -750,6 +750,20 @@ namespace FlaxEngine.GUI
}
}
+ ///
+ 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();
+ }
+ }
+
///
/// Draw the control and the children.
///
diff --git a/Source/Engine/UI/GUI/Control.cs b/Source/Engine/UI/GUI/Control.cs
index 027dd0267..d7b226cd1 100644
--- a/Source/Engine/UI/GUI/Control.cs
+++ b/Source/Engine/UI/GUI/Control.cs
@@ -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
{
}
+ ///
+ /// Called to clear UI state. For example, removes mouse over state or drag and drop when control gets disabled or hidden (including hierarchy).
+ ///
+ public virtual void ClearState()
+ {
+ Defocus();
+ if (_isMouseOver)
+ OnMouseLeave();
+ if (_isDragOver)
+ OnDragLeave();
+ while (_touchOvers != null && _touchOvers.Count != 0)
+ OnTouchLeave(_touchOvers[0]);
+ }
+
#region Focus
///
@@ -1338,6 +1329,12 @@ namespace FlaxEngine.GUI
///
protected virtual void OnVisibleChanged()
{
+ // Clear state when control gets hidden
+ if (!_isVisible && _isMouseOver)
+ {
+ OnMouseLeave();
+ }
+
VisibleChanged?.Invoke(this);
}